Merge bitcoin/bitcoin#26649: refactor: Use AutoFile and HashVerifier (without ser-type and ser-version) where possible

eeee61065f Use AutoFile and HashVerifier where possible (MarcoFalke)
fa961141f7 Add HashVerifier (MarcoFalke)

Pull request description:

  This was done in the context of https://github.com/bitcoin/bitcoin/pull/25284 , but I think it also makes sense standalone.

  The basic idea is that serialization type should not be initialized when it is not needed. Same for the serialization version.

  So do this here for `AutoFile` and `HashVerifier`. `CAutoFile` and `CHashVerifier` remain in places where it is not yet possible.

ACKs for top commit:
  stickies-v:
    ACK eeee61065f

Tree-SHA512: 93786778c309ecfdc1ed43552d24ff9d966954d69a47f66faaa6de24daacd25c651f3f62bde5abbb362700298fb3c04ffbd3207a0dd13d0bd8bff7fd6d07dcf8
pull/26998/head
fanquake 2 years ago
commit 0a1d372ad0
No known key found for this signature in database
GPG Key ID: 2EEB9F5CC09526C1

@ -167,6 +167,39 @@ public:
}; };
/** Reads data from an underlying stream, while hashing the read data. */ /** 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> template<typename Source>
class CHashVerifier : public CHashWriter class CHashVerifier : public CHashWriter
{ {

@ -352,7 +352,7 @@ bool BlockManager::LoadBlockIndexDB(const Consensus::Params& consensus_params)
} }
for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++) { for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++) {
FlatFilePos pos(*it, 0); FlatFilePos pos(*it, 0);
if (CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION).IsNull()) { if (AutoFile{OpenBlockFile(pos, true)}.IsNull()) {
return false; return false;
} }
} }
@ -454,13 +454,13 @@ CBlockFileInfo* BlockManager::GetBlockFileInfo(size_t n)
static bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart) static bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart)
{ {
// Open history file to append // Open history file to append
CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION); AutoFile fileout{OpenUndoFile(pos)};
if (fileout.IsNull()) { if (fileout.IsNull()) {
return error("%s: OpenUndoFile failed", __func__); return error("%s: OpenUndoFile failed", __func__);
} }
// Write index header // Write index header
unsigned int nSize = GetSerializeSize(blockundo, fileout.GetVersion()); unsigned int nSize = GetSerializeSize(blockundo, CLIENT_VERSION);
fileout << messageStart << nSize; fileout << messageStart << nSize;
// Write undo data // Write undo data
@ -489,14 +489,14 @@ bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex)
} }
// Open history file to read // Open history file to read
CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION); AutoFile filein{OpenUndoFile(pos, true)};
if (filein.IsNull()) { if (filein.IsNull()) {
return error("%s: OpenUndoFile failed", __func__); return error("%s: OpenUndoFile failed", __func__);
} }
// Read block // Read block
uint256 hashChecksum; 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 { try {
verifier << pindex->pprev->GetBlockHash(); verifier << pindex->pprev->GetBlockHash();
verifier >> blockundo; verifier >> blockundo;
@ -768,7 +768,7 @@ bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, c
{ {
FlatFilePos hpos = pos; FlatFilePos hpos = pos;
hpos.nPos -= 8; // Seek back 8 bytes for meta header 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()) { if (filein.IsNull()) {
return error("%s: OpenBlockFile failed for %s", __func__, pos.ToString()); return error("%s: OpenBlockFile failed for %s", __func__, pos.ToString());
} }

Loading…
Cancel
Save