made "tx" be used for both hash and raw output (like lbrycrd & bitcoin)

We want the empty tx array


use initializer to fill base result
This commit is contained in:
Brannon King 2021-10-01 22:24:03 -04:00
parent dc230888ef
commit 55abc9d25c
2 changed files with 49 additions and 49 deletions

View file

@ -66,13 +66,7 @@ type GetBlockStatsResult struct {
UTXOSizeIncrease int64 `json:"utxo_size_inc"` UTXOSizeIncrease int64 `json:"utxo_size_inc"`
} }
// GetBlockVerboseResult models the data from the getblock command when the type GetBlockVerboseResultBase struct {
// 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
// whose tx field is an array of transaction hashes. When the verbose flag is set to 2,
// 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.
type GetBlockVerboseResult struct {
Hash string `json:"hash"` Hash string `json:"hash"`
Confirmations int64 `json:"confirmations"` Confirmations int64 `json:"confirmations"`
StrippedSize int32 `json:"strippedsize"` StrippedSize int32 `json:"strippedsize"`
@ -82,15 +76,26 @@ type GetBlockVerboseResult 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,omitempty"`
Tx []string `json:"tx,omitempty"`
RawTx []TxRawResult `json:"rawtx,omitempty"` // Note: this field is always empty when verbose != 2.
Time int64 `json:"time"` Time int64 `json:"time"`
Nonce uint32 `json:"nonce"` Nonce uint32 `json:"nonce"`
Bits string `json:"bits"` Bits string `json:"bits"`
Difficulty float64 `json:"difficulty"` Difficulty float64 `json:"difficulty"`
PreviousHash string `json:"previousblockhash,omitempty"` PreviousHash string `json:"previousblockhash,omitempty"`
NextHash string `json:"nextblockhash,omitempty"` NextHash string `json:"nextblockhash,omitempty"`
ClaimTrie string `json:"claimTrie,omitempty"`
TxCount int `json:"nTx"` // For backwards compatibility only
}
// 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
// hex-encoded string. When the verbose flag is set to 1, getblock returns an object
// whose tx field is an array of transaction hashes. When the verbose flag is set to 2,
// 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.
type GetBlockVerboseResult struct {
GetBlockVerboseResultBase
Tx []string `json:"tx"`
} }
// 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

@ -1192,7 +1192,8 @@ func handleGetBlock(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (i
if blockHeight > 0 { if blockHeight > 0 {
prevHashString = blockHeader.PrevBlock.String() prevHashString = blockHeader.PrevBlock.String()
} }
blockReply := btcjson.GetBlockVerboseResult{
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),
@ -1218,8 +1219,14 @@ 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{
GetBlockVerboseResultBase: base,
Tx: txNames,
}
return blockReply, nil
}
txns := blk.Transactions() txns := blk.Transactions()
rawTxns := make([]btcjson.TxRawResult, len(txns)) rawTxns := make([]btcjson.TxRawResult, len(txns))
for i, tx := range txns { for i, tx := range txns {
@ -1231,7 +1238,10 @@ func handleGetBlock(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (i
} }
rawTxns[i] = *rawTxn rawTxns[i] = *rawTxn
} }
blockReply.RawTx = rawTxns base.TxCount = len(rawTxns)
blockReply := btcjson.GetBlockVerboseTxResult{
GetBlockVerboseResultBase: base,
Tx: rawTxns,
} }
return blockReply, nil return blockReply, nil