Skip to content

Commit

Permalink
made "tx" be used for both hash and raw output (like lbrycrd & bitcoin)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrannonKing committed Oct 1, 2021
1 parent dc23088 commit c684b0f
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 64 deletions.
60 changes: 25 additions & 35 deletions btcjson/chainsvrresults.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,31 +66,36 @@ type GetBlockStatsResult struct {
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:"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 {
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"`
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"`
Nonce uint32 `json:"nonce"`
Bits string `json:"bits"`
Difficulty float64 `json:"difficulty"`
PreviousHash string `json:"previousblockhash,omitempty"`
NextHash string `json:"nextblockhash,omitempty"`
GetBlockVerboseResultBase
Tx []string `json:"tx,omitempty"`
}

// GetBlockVerboseTxResult models the data from the getblock command when the
Expand All @@ -100,23 +105,8 @@ type GetBlockVerboseResult struct {
// 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.
type GetBlockVerboseTxResult 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"`
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"`
GetBlockVerboseResultBase
Tx []TxRawResult `json:"tx,omitempty"`
}

// GetChainTxStatsResult models the data from the getchaintxstats command.
Expand Down
66 changes: 37 additions & 29 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1192,23 +1192,24 @@ func handleGetBlock(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (i
if blockHeight > 0 {
prevHashString = blockHeader.PrevBlock.String()
}
blockReply := btcjson.GetBlockVerboseResult{
Hash: c.Hash,
Version: blockHeader.Version,
VersionHex: fmt.Sprintf("%08x", blockHeader.Version),
MerkleRoot: blockHeader.MerkleRoot.String(),
PreviousHash: prevHashString,
Nonce: blockHeader.Nonce,
Time: blockHeader.Timestamp.Unix(),
Confirmations: int64(1 + best.Height - blockHeight),
Height: int64(blockHeight),
Size: int32(len(blkBytes)),
StrippedSize: int32(blk.MsgBlock().SerializeSizeStripped()),
Weight: int32(blockchain.GetBlockWeight(blk)),
Bits: strconv.FormatInt(int64(blockHeader.Bits), 16),
Difficulty: getDifficultyRatio(blockHeader.Bits, params),
NextHash: nextHashString,
ClaimTrie: blockHeader.ClaimTrie.String(),

filler := func(base *btcjson.GetBlockVerboseResultBase) {
base.Hash = c.Hash
base.Version = blockHeader.Version
base.VersionHex = fmt.Sprintf("%08x", blockHeader.Version)
base.MerkleRoot = blockHeader.MerkleRoot.String()
base.PreviousHash = prevHashString
base.Nonce = blockHeader.Nonce
base.Time = blockHeader.Timestamp.Unix()
base.Confirmations = int64(1 + best.Height - blockHeight)
base.Height = int64(blockHeight)
base.Size = int32(len(blkBytes))
base.StrippedSize = int32(blk.MsgBlock().SerializeSizeStripped())
base.Weight = int32(blockchain.GetBlockWeight(blk))
base.Bits = strconv.FormatInt(int64(blockHeader.Bits), 16)
base.Difficulty = getDifficultyRatio(blockHeader.Bits, params)
base.NextHash = nextHashString
base.ClaimTrie = blockHeader.ClaimTrie.String()
}

if *c.Verbosity == 1 {
Expand All @@ -1218,21 +1219,28 @@ func handleGetBlock(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (i
txNames[i] = tx.Hash().String()
}

blockReply := btcjson.GetBlockVerboseResult{}
filler(&blockReply.GetBlockVerboseResultBase)
blockReply.Tx = txNames
} else {
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
blockReply.TxCount = len(txNames)
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
}
blockReply.RawTx = rawTxns
rawTxns[i] = *rawTxn
}
blockReply := btcjson.GetBlockVerboseTxResult{}
filler(&blockReply.GetBlockVerboseResultBase)
blockReply.Tx = rawTxns
blockReply.TxCount = len(rawTxns)

return blockReply, nil
}
Expand Down

0 comments on commit c684b0f

Please sign in to comment.