[lbry] rpc: fix getblock reponse

This commit is contained in:
Brannon King 2021-08-03 22:10:26 -07:00 committed by Roy Lee
parent ddb735fe4c
commit 939eeca0cf
3 changed files with 57 additions and 52 deletions

View file

@ -25,7 +25,7 @@ type GetBlockHeaderVerboseResult struct {
Version int32 `json:"version"` Version int32 `json:"version"`
VersionHex string `json:"versionHex"` VersionHex string `json:"versionHex"`
MerkleRoot string `json:"merkleroot"` MerkleRoot string `json:"merkleroot"`
ClaimTrie string `json:"claimtrie"` ClaimTrie string `json:"nameclaimroot,omitempty"`
Time int64 `json:"time"` Time int64 `json:"time"`
Nonce uint64 `json:"nonce"` Nonce uint64 `json:"nonce"`
Bits string `json:"bits"` Bits string `json:"bits"`
@ -66,6 +66,27 @@ type GetBlockStatsResult struct {
UTXOSizeIncrease int64 `json:"utxo_size_inc"` UTXOSizeIncrease int64 `json:"utxo_size_inc"`
} }
type GetBlockVerboseResultBase struct {
Hash string `json:"hash"`
Confirmations int64 `json:"confirmations"`
StrippedSize int32 `json:"strippedsize"`
Size int32 `json:"size"`
Weight int32 `json:"weight"`
Height int64 `json:"height"`
Version int32 `json:"version"`
VersionHex string `json:"versionHex"`
MerkleRoot string `json:"merkleroot"`
Time int64 `json:"time"`
Nonce uint32 `json:"nonce"`
Bits string `json:"bits"`
Difficulty float64 `json:"difficulty"`
PreviousHash string `json:"previousblockhash,omitempty"`
NextHash string `json:"nextblockhash,omitempty"`
ClaimTrie string `json:"nameclaimroot,omitempty"`
TxCount int `json:"nTx"` // For backwards compatibility only
}
// GetBlockVerboseResult models the data from the getblock command when the // GetBlockVerboseResult models the data from the getblock command when the
// verbose flag is set to 1. When the verbose flag is set to 0, getblock returns a // verbose flag is set to 1. When the verbose flag is set to 0, getblock returns a
// hex-encoded string. When the verbose flag is set to 1, getblock returns an object // hex-encoded string. When the verbose flag is set to 1, getblock returns an object
@ -73,24 +94,8 @@ type GetBlockStatsResult struct {
// getblock returns an object whose tx field is an array of raw transactions. // getblock returns an object whose tx field is an array of raw transactions.
// Use GetBlockVerboseTxResult to unmarshal data received from passing verbose=2 to getblock. // Use GetBlockVerboseTxResult to unmarshal data received from passing verbose=2 to getblock.
type GetBlockVerboseResult struct { type GetBlockVerboseResult struct {
Hash string `json:"hash"` GetBlockVerboseResultBase
Confirmations int64 `json:"confirmations"` Tx []string `json:"tx"`
StrippedSize int32 `json:"strippedsize"`
Size int32 `json:"size"`
Weight int32 `json:"weight"`
Height int64 `json:"height"`
Version int32 `json:"version"`
VersionHex string `json:"versionHex"`
MerkleRoot string `json:"merkleroot"`
ClaimTrie string `json:"claimTrie"`
Tx []string `json:"tx,omitempty"`
RawTx []TxRawResult `json:"rawtx,omitempty"` // Note: this field is always empty when verbose != 2.
Time int64 `json:"time"`
Nonce uint32 `json:"nonce"`
Bits string `json:"bits"`
Difficulty float64 `json:"difficulty"`
PreviousHash string `json:"previousblockhash"`
NextHash string `json:"nextblockhash,omitempty"`
} }
// GetBlockVerboseTxResult models the data from the getblock command when the // GetBlockVerboseTxResult models the data from the getblock command when the
@ -100,23 +105,8 @@ type GetBlockVerboseResult struct {
// getblock returns an object whose tx field is an array of raw transactions. // getblock returns an object whose tx field is an array of raw transactions.
// Use GetBlockVerboseResult to unmarshal data received from passing verbose=1 to getblock. // Use GetBlockVerboseResult to unmarshal data received from passing verbose=1 to getblock.
type GetBlockVerboseTxResult struct { type GetBlockVerboseTxResult struct {
Hash string `json:"hash"` GetBlockVerboseResultBase
Confirmations int64 `json:"confirmations"` Tx []TxRawResult `json:"tx"`
StrippedSize int32 `json:"strippedsize"`
Size int32 `json:"size"`
Weight int32 `json:"weight"`
Height int64 `json:"height"`
Version int32 `json:"version"`
VersionHex string `json:"versionHex"`
MerkleRoot string `json:"merkleroot"`
Tx []TxRawResult `json:"tx,omitempty"`
RawTx []TxRawResult `json:"rawtx,omitempty"` // Deprecated: removed in Bitcoin Core
Time int64 `json:"time"`
Nonce uint32 `json:"nonce"`
Bits string `json:"bits"`
Difficulty float64 `json:"difficulty"`
PreviousHash string `json:"previousblockhash"`
NextHash string `json:"nextblockhash,omitempty"`
} }
// GetChainTxStatsResult models the data from the getchaintxstats command. // GetChainTxStatsResult models the data from the getchaintxstats command.

View file

@ -1188,12 +1188,17 @@ func handleGetBlock(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (i
params := s.cfg.ChainParams params := s.cfg.ChainParams
blockHeader := &blk.MsgBlock().Header blockHeader := &blk.MsgBlock().Header
blockReply := btcjson.GetBlockVerboseResult{ var prevHashString string
if blockHeight > 0 {
prevHashString = blockHeader.PrevBlock.String()
}
base := btcjson.GetBlockVerboseResultBase{
Hash: c.Hash, Hash: c.Hash,
Version: blockHeader.Version, Version: blockHeader.Version,
VersionHex: fmt.Sprintf("%08x", blockHeader.Version), VersionHex: fmt.Sprintf("%08x", blockHeader.Version),
MerkleRoot: blockHeader.MerkleRoot.String(), MerkleRoot: blockHeader.MerkleRoot.String(),
PreviousHash: blockHeader.PrevBlock.String(), PreviousHash: prevHashString,
Nonce: blockHeader.Nonce, Nonce: blockHeader.Nonce,
Time: blockHeader.Timestamp.Unix(), Time: blockHeader.Timestamp.Unix(),
Confirmations: int64(1 + best.Height - blockHeight), Confirmations: int64(1 + best.Height - blockHeight),
@ -1204,6 +1209,7 @@ func handleGetBlock(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (i
Bits: strconv.FormatInt(int64(blockHeader.Bits), 16), Bits: strconv.FormatInt(int64(blockHeader.Bits), 16),
Difficulty: getDifficultyRatio(blockHeader.Bits, params), Difficulty: getDifficultyRatio(blockHeader.Bits, params),
NextHash: nextHashString, NextHash: nextHashString,
ClaimTrie: blockHeader.ClaimTrie.String(),
} }
if *c.Verbosity == 1 { if *c.Verbosity == 1 {
@ -1213,20 +1219,29 @@ func handleGetBlock(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (i
txNames[i] = tx.Hash().String() txNames[i] = tx.Hash().String()
} }
blockReply.Tx = txNames base.TxCount = len(txNames)
} else { blockReply := btcjson.GetBlockVerboseResult{
txns := blk.Transactions() GetBlockVerboseResultBase: base,
rawTxns := make([]btcjson.TxRawResult, len(txns)) Tx: txNames,
for i, tx := range txns {
rawTxn, err := createTxRawResult(params, tx.MsgTx(),
tx.Hash().String(), blockHeader, hash.String(),
blockHeight, best.Height)
if err != nil {
return nil, err
}
rawTxns[i] = *rawTxn
} }
blockReply.RawTx = rawTxns return blockReply, nil
}
txns := blk.Transactions()
rawTxns := make([]btcjson.TxRawResult, len(txns))
for i, tx := range txns {
rawTxn, err := createTxRawResult(params, tx.MsgTx(),
tx.Hash().String(), blockHeader, hash.String(),
blockHeight, best.Height)
if err != nil {
return nil, err
}
rawTxns[i] = *rawTxn
}
base.TxCount = len(rawTxns)
blockReply := btcjson.GetBlockVerboseTxResult{
GetBlockVerboseResultBase: base,
Tx: rawTxns,
} }
return blockReply, nil return blockReply, nil

View file

@ -249,7 +249,7 @@ var helpDescsEnUS = map[string]string{
"getblockverboseresult-merkleroot": "Root hash of the merkle tree", "getblockverboseresult-merkleroot": "Root hash of the merkle tree",
"getblockverboseresult-nameclaimroot": "Root hash of the claim trie", "getblockverboseresult-nameclaimroot": "Root hash of the claim trie",
"getblockverboseresult-tx": "The transaction hashes (only when verbosity=1)", "getblockverboseresult-tx": "The transaction hashes (only when verbosity=1)",
"getblockverboseresult-rawtx": "The transactions as JSON objects (only when verbosity=2)", "getblockverboseresult-nTx": "The number of transactions (aka, count of TX)",
"getblockverboseresult-time": "The block time in seconds since 1 Jan 1970 GMT", "getblockverboseresult-time": "The block time in seconds since 1 Jan 1970 GMT",
"getblockverboseresult-nonce": "The block nonce", "getblockverboseresult-nonce": "The block nonce",
"getblockverboseresult-bits": "The bits which represent the block difficulty", "getblockverboseresult-bits": "The bits which represent the block difficulty",