Merge pull request #2552 from rnicoll/1.21-read-block-from-disk
Introduce ReadBlockOrHeaderFromDisk
This commit is contained in:
commit
dbed8284f2
|
@ -1571,7 +1571,7 @@ void static ProcessGetBlockData(CNode& pfrom, const CChainParams& chainparams, c
|
|||
} else {
|
||||
// Send block from disk
|
||||
std::shared_ptr<CBlock> pblockRead = std::make_shared<CBlock>();
|
||||
if (!ReadBlockFromDisk(*pblockRead, pindex, consensusParams))
|
||||
if (!ReadBlockFromDisk(*pblockRead, pindex, consensusParams, false))
|
||||
assert(!"cannot load block from disk");
|
||||
pblock = pblockRead;
|
||||
}
|
||||
|
@ -2847,7 +2847,7 @@ void PeerManager::ProcessMessage(CNode& pfrom, const std::string& msg_type, CDat
|
|||
|
||||
if (pindex->nHeight >= ::ChainActive().Height() - MAX_BLOCKTXN_DEPTH) {
|
||||
CBlock block;
|
||||
bool ret = ReadBlockFromDisk(block, pindex, m_chainparams.GetConsensus());
|
||||
bool ret = ReadBlockFromDisk(block, pindex, m_chainparams.GetConsensus(), false);
|
||||
assert(ret);
|
||||
|
||||
SendBlockTransactions(pfrom, block, req);
|
||||
|
@ -4252,7 +4252,7 @@ bool PeerManager::SendMessages(CNode* pto)
|
|||
}
|
||||
if (!fGotBlockFromCache) {
|
||||
CBlock block;
|
||||
bool ret = ReadBlockFromDisk(block, pBestIndex, consensusParams);
|
||||
bool ret = ReadBlockFromDisk(block, pBestIndex, consensusParams, false);
|
||||
assert(ret);
|
||||
CBlockHeaderAndShortTxIDs cmpctblock(block, state.fWantsCmpctWitness);
|
||||
m_connman.PushMessage(pto, msgMaker.Make(nSendFlags, NetMsgType::CMPCTBLOCK, cmpctblock));
|
||||
|
|
|
@ -1143,14 +1143,19 @@ static bool WriteBlockToDisk(const CBlock& block, FlatFilePos& pos, const CMessa
|
|||
return true;
|
||||
}
|
||||
|
||||
bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams)
|
||||
|
||||
/* Generic implementation of block reading that can handle
|
||||
both a block and its header. */
|
||||
|
||||
template<typename T>
|
||||
static bool ReadBlockOrHeader(T& block, const FlatFilePos& pos, const Consensus::Params& consensusParams, const bool fCheckPOW)
|
||||
{
|
||||
block.SetNull();
|
||||
|
||||
// Open history file to read
|
||||
CAutoFile filein(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION);
|
||||
if (filein.IsNull())
|
||||
return error("ReadBlockFromDisk: OpenBlockFile failed for %s", pos.ToString());
|
||||
return error("ReadBlockOrHeader: OpenBlockFile failed for %s", pos.ToString());
|
||||
|
||||
// Read block
|
||||
try {
|
||||
|
@ -1161,18 +1166,19 @@ bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::P
|
|||
}
|
||||
|
||||
// Check the header
|
||||
if (!CheckProofOfWork(block.GetPoWHash(), block.nBits, consensusParams))
|
||||
return error("ReadBlockFromDisk: Errors in block header at %s", pos.ToString());
|
||||
if (fCheckPOW && !CheckProofOfWork(block.GetPoWHash(), block.nBits, consensusParams))
|
||||
return error("ReadBlockOrHeader: Errors in block header at %s", pos.ToString());
|
||||
|
||||
// Signet only: check block solution
|
||||
if (consensusParams.signet_blocks && !CheckSignetBlockSolution(block, consensusParams)) {
|
||||
return error("ReadBlockFromDisk: Errors in block solution at %s", pos.ToString());
|
||||
return error("ReadBlockOrHeader: Errors in block solution at %s", pos.ToString());
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams)
|
||||
template<typename T>
|
||||
static bool ReadBlockOrHeader(T& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams, bool fCheckPOW)
|
||||
{
|
||||
FlatFilePos blockPos;
|
||||
{
|
||||
|
@ -1180,14 +1186,29 @@ bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus
|
|||
blockPos = pindex->GetBlockPos();
|
||||
}
|
||||
|
||||
if (!ReadBlockFromDisk(block, blockPos, consensusParams))
|
||||
if (!ReadBlockOrHeader(block, blockPos, consensusParams, fCheckPOW))
|
||||
return false;
|
||||
if (block.GetHash() != pindex->GetBlockHash())
|
||||
return error("ReadBlockFromDisk(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
|
||||
return error("ReadBlockOrHeader(CBlock&, CBlockIndex*): GetHash() doesn't match index for %s at %s",
|
||||
pindex->ToString(), pindex->GetBlockPos().ToString());
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams, const bool fCheckPOW)
|
||||
{
|
||||
return ReadBlockOrHeader(block, pos, consensusParams, fCheckPOW);
|
||||
}
|
||||
|
||||
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams, const bool fCheckPOW)
|
||||
{
|
||||
return ReadBlockOrHeader(block, pindex, consensusParams, fCheckPOW);
|
||||
}
|
||||
|
||||
bool ReadBlockHeaderFromDisk(CBlockHeader& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams, const bool fCheckPOW)
|
||||
{
|
||||
return ReadBlockOrHeader(block, pindex, consensusParams, fCheckPOW);
|
||||
}
|
||||
|
||||
bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start)
|
||||
{
|
||||
FlatFilePos hpos = pos;
|
||||
|
|
|
@ -285,10 +285,11 @@ void InitScriptExecutionCache();
|
|||
|
||||
|
||||
/** Functions for disk access for blocks */
|
||||
bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams);
|
||||
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams);
|
||||
bool ReadBlockFromDisk(CBlock& block, const FlatFilePos& pos, const Consensus::Params& consensusParams, const bool fCheckPOW = true);
|
||||
bool ReadBlockFromDisk(CBlock& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams, const bool fCheckPOW = true);
|
||||
bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, const CMessageHeader::MessageStartChars& message_start);
|
||||
bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const CBlockIndex* pindex, const CMessageHeader::MessageStartChars& message_start);
|
||||
bool ReadBlockHeaderFromDisk(CBlockHeader& block, const CBlockIndex* pindex, const Consensus::Params& consensusParams, const bool fCheckPOW = true);
|
||||
|
||||
bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex);
|
||||
|
||||
|
|
Loading…
Reference in a new issue