Skip to content

Commit 830b449

Browse files
committed
When people ask us for headers we do not have (over REST or RPC), do something reasonable.
1 parent ca1aded commit 830b449

File tree

4 files changed

+60
-10
lines changed

4 files changed

+60
-10
lines changed

src/node/blockstorage.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,17 @@ bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus
412412
return true;
413413
}
414414

415+
bool ReadBlockHeaderFromDisk(CBlockHeader& header, const CBlockIndex* pindex, const Consensus::Params& consensusParams)
416+
{
417+
// Not very efficient: read a block and throw away all but the header.
418+
CBlock tmp;
419+
if (!ReadBlockFromDisk(tmp, pindex, consensusParams)) {
420+
return false;
421+
}
422+
header = tmp.GetBlockHeader();
423+
return true;
424+
}
425+
415426
bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start)
416427
{
417428
FlatFilePos hpos = pos;

src/node/blockstorage.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ extern bool fTrimHeaders;
4949
/** Minimum number of full untrimmed headers to keep, for blocks we have. */
5050
extern uint64_t nMustKeepFullHeaders;
5151
/** Target number of headers to download beyond the blocks we have. */
52-
// XXX: this currently only operates when in header trim mode, but it's really independent of that.
52+
// NOTE: this currently only operates when in header trim mode, but it's really independent of that.
5353
extern uint64_t nHeaderDownloadBuffer;
5454

5555
//! Check whether the block associated with this index entry is pruned or not.
@@ -78,6 +78,8 @@ bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::P
7878
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams);
7979
bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start);
8080
bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const CBlockIndex* pindex, const CMessageHeader::MessageStartChars& message_start);
81+
// ELEMENTS:
82+
bool ReadBlockHeaderFromDisk(class CBlockHeader& header, const CBlockIndex* pindex, const Consensus::Params& consensusParams);
8183

8284
bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex);
8385
bool WriteUndoDataForBlock(const CBlockUndo& blockundo, BlockValidationState& state, CBlockIndex* pindex, const CChainParams& chainparams);

src/rest.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,14 @@ static bool rest_headers(const std::any& context,
221221
case RetFormat::BINARY: {
222222
CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
223223
for (const CBlockIndex *pindex : headers) {
224-
ssHeader << pindex->GetBlockHeader();
224+
if (pindex->trimmed()) {
225+
CBlockHeader tmp;
226+
ReadBlockHeaderFromDisk(tmp, pindex, Params().GetConsensus());
227+
ssHeader << tmp;
228+
229+
} else {
230+
ssHeader << pindex->GetBlockHeader();
231+
}
225232
}
226233

227234
std::string binaryHeader = ssHeader.str();
@@ -233,8 +240,14 @@ static bool rest_headers(const std::any& context,
233240
case RetFormat::HEX: {
234241
CDataStream ssHeader(SER_NETWORK, PROTOCOL_VERSION);
235242
for (const CBlockIndex *pindex : headers) {
236-
ssHeader << pindex->GetBlockHeader();
237-
}
243+
if (pindex->trimmed()) {
244+
CBlockHeader tmp;
245+
ReadBlockHeaderFromDisk(tmp, pindex, Params().GetConsensus());
246+
ssHeader << tmp;
247+
248+
} else {
249+
ssHeader << pindex->GetBlockHeader();
250+
} }
238251

239252
std::string strHex = HexStr(ssHeader) + "\n";
240253
req->WriteHeader("Content-Type", "text/plain");

src/rpc/blockchain.cpp

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,23 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex
249249
result.pushKV("chainwork", blockindex->nChainWork.GetHex());
250250
} else {
251251
if (blockindex->dynafed_params().IsNull()) {
252-
result.pushKV("signblock_witness_asm", ScriptToAsmStr(blockindex->get_proof().solution));
253-
result.pushKV("signblock_witness_hex", HexStr(blockindex->get_proof().solution));
252+
if (blockindex->trimmed()) {
253+
result.pushKV("signblock_witness_asm", "<trimmed>");
254+
result.pushKV("signblock_witness_hex", "<trimmed>");
255+
result.pushKV("warning", "Fields missing due to -trim_headers flag.");
256+
} else {
257+
result.pushKV("signblock_witness_asm", ScriptToAsmStr(blockindex->get_proof().solution));
258+
result.pushKV("signblock_witness_hex", HexStr(blockindex->get_proof().solution));
259+
}
254260
} else {
255-
result.pushKV("signblock_witness_hex", EncodeHexScriptWitness(blockindex->signblock_witness()));
256-
result.pushKV("dynamic_parameters", dynaParamsToJSON(blockindex->dynafed_params()));
261+
if (blockindex->trimmed()) {
262+
result.pushKV("signblock_witness_hex", "<trimmed>");
263+
result.pushKV("dynamic_parameters", "<trimmed>");
264+
result.pushKV("warning", "Fields missing due to -trim_headers flag.");
265+
} else {
266+
result.pushKV("signblock_witness_hex", EncodeHexScriptWitness(blockindex->signblock_witness()));
267+
result.pushKV("dynamic_parameters", dynaParamsToJSON(blockindex->dynafed_params()));
268+
}
257269
}
258270
}
259271
result.pushKV("nTx", (uint64_t)blockindex->nTx);
@@ -266,7 +278,13 @@ UniValue blockheaderToJSON(const CBlockIndex* tip, const CBlockIndex* blockindex
266278

267279
UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails)
268280
{
269-
UniValue result = blockheaderToJSON(tip, blockindex);
281+
UniValue result;
282+
if (blockindex->trimmed()) {
283+
CBlockIndex tmp = CBlockIndex(block.GetBlockHeader()); // XXX: lifetimes?
284+
result = blockheaderToJSON(tip, &tmp);
285+
} else {
286+
result = blockheaderToJSON(tip, blockindex);
287+
}
270288

271289
result.pushKV("strippedsize", (int)::GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS));
272290
result.pushKV("size", (int)::GetSerializeSize(block, PROTOCOL_VERSION));
@@ -968,7 +986,13 @@ static RPCHelpMan getblockheader()
968986
if (!fVerbose)
969987
{
970988
CDataStream ssBlock(SER_NETWORK, PROTOCOL_VERSION);
971-
ssBlock << pblockindex->GetBlockHeader();
989+
if (pblockindex->trimmed()) {
990+
CBlockHeader tmp;
991+
ReadBlockHeaderFromDisk(tmp, pblockindex, Params().GetConsensus());
992+
ssBlock << tmp;
993+
} else {
994+
ssBlock << pblockindex->GetBlockHeader();
995+
}
972996
std::string strHex = HexStr(ssBlock);
973997
return strHex;
974998
}

0 commit comments

Comments
 (0)