Skip to content

Commit

Permalink
use initializer to fill base result
Browse files Browse the repository at this point in the history
  • Loading branch information
roylee17 committed Oct 1, 2021
1 parent bd83575 commit 1e4ef47
Showing 1 changed file with 27 additions and 25 deletions.
52 changes: 27 additions & 25 deletions rpcserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -1193,23 +1193,23 @@ func handleGetBlock(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (i
prevHashString = blockHeader.PrevBlock.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()
base := btcjson.GetBlockVerboseResultBase{
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(),
}

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

blockReply := btcjson.GetBlockVerboseResult{}
filler(&blockReply.GetBlockVerboseResultBase)
blockReply.Tx = txNames
blockReply.TxCount = len(txNames)
base.TxCount = len(txNames)
blockReply := btcjson.GetBlockVerboseResult{
GetBlockVerboseResultBase: base,
Tx: txNames,
}
return blockReply, nil
}

Expand All @@ -1237,10 +1238,11 @@ func handleGetBlock(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (i
}
rawTxns[i] = *rawTxn
}
blockReply := btcjson.GetBlockVerboseTxResult{}
filler(&blockReply.GetBlockVerboseResultBase)
blockReply.Tx = rawTxns
blockReply.TxCount = len(rawTxns)
base.TxCount = len(rawTxns)
blockReply := btcjson.GetBlockVerboseTxResult{
GetBlockVerboseResultBase: base,
Tx: rawTxns,
}

return blockReply, nil
}
Expand Down

0 comments on commit 1e4ef47

Please sign in to comment.