Skip to content

Commit 3c514cf

Browse files
committed
Merge #21114: Deduplicate some block-to-JSON code
fa2c521 Deduplicate some block-to-JSON code. (Daniel Kraft) Pull request description: Some of the logic converting blocks and block headers to JSON for the blockchain RPC methods (`getblock`, `getlockheader`) was duplicated. Instead of that, the `blockToJSON` RPC method now calls `blockheaderToJSON` first, and then fills in the missing, block-specific bits explicitly. ACKs for top commit: laanwj: Code review ACK fa2c521 Tree-SHA512: 1b9b269e251d9c8c1056f253cfc2a795170d609f4c26ecc85b1ff9cdd567095a673dd89564e0d587b32dfc152ea01b2682169b018f2c9c3004c511a9998d772e
2 parents b3dda0a + fa2c521 commit 3c514cf

File tree

1 file changed

+1
-22
lines changed

1 file changed

+1
-22
lines changed

src/rpc/blockchain.cpp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -156,21 +156,11 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex
156156

157157
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails)
158158
{
159-
// Serialize passed information without accessing chain state of the active chain!
160-
AssertLockNotHeld(cs_main); // For performance reasons
159+
UniValue result = blockheaderToJSON(tip, blockindex);
161160

162-
UniValue result(UniValue::VOBJ);
163-
result.pushKV("hash", blockindex->GetBlockHash().GetHex());
164-
const CBlockIndex* pnext;
165-
int confirmations = ComputeNextBlockAndDepth(tip, blockindex, pnext);
166-
result.pushKV("confirmations", confirmations);
167161
result.pushKV("strippedsize", (int)::GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS));
168162
result.pushKV("size", (int)::GetSerializeSize(block, PROTOCOL_VERSION));
169163
result.pushKV("weight", (int)::GetBlockWeight(block));
170-
result.pushKV("height", blockindex->nHeight);
171-
result.pushKV("version", block.nVersion);
172-
result.pushKV("versionHex", strprintf("%08x", block.nVersion));
173-
result.pushKV("merkleroot", block.hashMerkleRoot.GetHex());
174164
UniValue txs(UniValue::VARR);
175165
if (txDetails) {
176166
CBlockUndo blockUndo;
@@ -189,18 +179,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIn
189179
}
190180
}
191181
result.pushKV("tx", txs);
192-
result.pushKV("time", block.GetBlockTime());
193-
result.pushKV("mediantime", (int64_t)blockindex->GetMedianTimePast());
194-
result.pushKV("nonce", (uint64_t)block.nNonce);
195-
result.pushKV("bits", strprintf("%08x", block.nBits));
196-
result.pushKV("difficulty", GetDifficulty(blockindex));
197-
result.pushKV("chainwork", blockindex->nChainWork.GetHex());
198-
result.pushKV("nTx", (uint64_t)blockindex->nTx);
199182

200-
if (blockindex->pprev)
201-
result.pushKV("previousblockhash", blockindex->pprev->GetBlockHash().GetHex());
202-
if (pnext)
203-
result.pushKV("nextblockhash", pnext->GetBlockHash().GetHex());
204183
return result;
205184
}
206185

0 commit comments

Comments
 (0)