Skip to content

Commit fa2c521

Browse files
committed
Deduplicate some block-to-JSON code.
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.
1 parent cf26ca3 commit fa2c521

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)