MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / ReadBlock

Method ReadBlock

src/node/blockstorage.cpp:1050–1089  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1048}
1049
1050bool BlockManager::ReadBlock(CBlock& block, const FlatFilePos& pos, const std::optional<uint256>& expected_hash) const
1051{
1052 block.SetNull();
1053
1054 // Open history file to read
1055 const auto block_data{ReadRawBlock(pos)};
1056 if (!block_data) {
1057 return false;
1058 }
1059
1060 try {
1061 // Read block
1062 SpanReader{*block_data} >> TX_WITH_WITNESS(block);
1063 } catch (const std::exception& e) {
1064 LogError("Deserialize or I/O error - %s at %s while reading block", e.what(), pos.ToString());
1065 return false;
1066 }
1067
1068 const auto block_hash{block.GetHash()};
1069
1070 // Check the header
1071 if (!CheckProofOfWork(block_hash, block.nBits, GetConsensus())) {
1072 LogError("Errors in block header at %s while reading block", pos.ToString());
1073 return false;
1074 }
1075
1076 // Signet only: check block solution
1077 if (GetConsensus().signet_blocks && !CheckSignetBlockSolution(block, GetConsensus())) {
1078 LogError("Errors in block solution at %s while reading block", pos.ToString());
1079 return false;
1080 }
1081
1082 if (expected_hash && block_hash != *expected_hash) {
1083 LogError("GetHash() doesn't match index at %s while reading block (%s != %s)",
1084 pos.ToString(), block_hash.ToString(), expected_hash->ToString());
1085 return false;
1086 }
1087
1088 return true;
1089}
1090
1091bool BlockManager::ReadBlock(CBlock& block, const CBlockIndex& index) const
1092{

Callers 15

ProcessGetBlockDataMethod · 0.45
ProcessMessageMethod · 0.45
SendMessagesMethod · 0.45
DisconnectTipMethod · 0.45
ConnectTipMethod · 0.45
VerifyDBMethod · 0.45
RollforwardBlockMethod · 0.45
ReplayBlocksMethod · 0.45
LoadExternalBlockFileMethod · 0.45
BOOST_FIXTURE_TEST_CASEFunction · 0.45
BOOST_FIXTURE_TEST_CASEFunction · 0.45
BOOST_AUTO_TEST_CASEFunction · 0.45

Calls 8

CheckProofOfWorkFunction · 0.85
CheckSignetBlockSolutionFunction · 0.85
ReadBlockFunction · 0.85
SetNullMethod · 0.45
whatMethod · 0.45
ToStringMethod · 0.45
GetHashMethod · 0.45
GetBlockHashMethod · 0.45

Tested by 7

BOOST_FIXTURE_TEST_CASEFunction · 0.36
BOOST_FIXTURE_TEST_CASEFunction · 0.36
BOOST_AUTO_TEST_CASEFunction · 0.36
chainman_reindex_testFunction · 0.36
BOOST_AUTO_TEST_CASEFunction · 0.36
ComputeFilterFunction · 0.36