Merge 0a1d372ad0 into merged_master (Bitcoin PR bitcoin/bitcoin#26649)

This commit is contained in:
Tom Trevethan 2025-04-04 15:50:36 +00:00
commit 669ddecaca
2 changed files with 39 additions and 6 deletions

View file

@ -167,6 +167,39 @@ public:
};
/** Reads data from an underlying stream, while hashing the read data. */
template <typename Source>
class HashVerifier : public HashWriter
{
private:
Source& m_source;
public:
explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}
void read(Span<std::byte> dst)
{
m_source.read(dst);
this->write(dst);
}
void ignore(size_t num_bytes)
{
std::byte data[1024];
while (num_bytes > 0) {
size_t now = std::min<size_t>(num_bytes, 1024);
read({data, now});
num_bytes -= now;
}
}
template <typename T>
HashVerifier<Source>& operator>>(T&& obj)
{
::Unserialize(*this, obj);
return *this;
}
};
template<typename Source>
class CHashVerifier : public CHashWriter
{

View file

@ -363,7 +363,7 @@ bool BlockManager::LoadBlockIndexDB(ChainstateManager& chainman, const Consensus
}
for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++) {
FlatFilePos pos(*it, 0);
if (CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION).IsNull()) {
if (AutoFile{OpenBlockFile(pos, true)}.IsNull()) {
return false;
}
}
@ -465,13 +465,13 @@ CBlockFileInfo* BlockManager::GetBlockFileInfo(size_t n)
static bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart)
{
// Open history file to append
CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
AutoFile fileout{OpenUndoFile(pos)};
if (fileout.IsNull()) {
return error("%s: OpenUndoFile failed", __func__);
}
// Write index header
unsigned int nSize = GetSerializeSize(blockundo, fileout.GetVersion());
unsigned int nSize = GetSerializeSize(blockundo, CLIENT_VERSION);
fileout << messageStart << nSize;
// Write undo data
@ -500,14 +500,14 @@ bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex)
}
// Open history file to read
CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
AutoFile filein{OpenUndoFile(pos, true)};
if (filein.IsNull()) {
return error("%s: OpenUndoFile failed", __func__);
}
// Read block
uint256 hashChecksum;
CHashVerifier<CAutoFile> verifier(&filein); // We need a CHashVerifier as reserializing may lose data
HashVerifier verifier{filein}; // Use HashVerifier as reserializing may lose data, c.f. commit d342424301013ec47dc146a4beb49d5c9319d80a
try {
verifier << pindex->pprev->GetBlockHash();
verifier >> blockundo;
@ -779,7 +779,7 @@ bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, c
{
FlatFilePos hpos = pos;
hpos.nPos -= 8; // Seek back 8 bytes for meta header
CAutoFile filein(OpenBlockFile(hpos, true), SER_DISK, CLIENT_VERSION);
AutoFile filein{OpenBlockFile(hpos, true)};
if (filein.IsNull()) {
return error("%s: OpenBlockFile failed for %s", __func__, pos.ToString());
}