Skip to content

Commit 0a1d372

Browse files
committed
Merge bitcoin/bitcoin#26649: refactor: Use AutoFile and HashVerifier (without ser-type and ser-version) where possible
eeee610 Use AutoFile and HashVerifier where possible (MarcoFalke) fa96114 Add HashVerifier (MarcoFalke) Pull request description: This was done in the context of bitcoin/bitcoin#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 eeee610 Tree-SHA512: 93786778c309ecfdc1ed43552d24ff9d966954d69a47f66faaa6de24daacd25c651f3f62bde5abbb362700298fb3c04ffbd3207a0dd13d0bd8bff7fd6d07dcf8
2 parents 228edaf + eeee610 commit 0a1d372

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

src/hash.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,39 @@ class CHashWriter : public HashWriter
167167
};
168168

169169
/** Reads data from an underlying stream, while hashing the read data. */
170+
template <typename Source>
171+
class HashVerifier : public HashWriter
172+
{
173+
private:
174+
Source& m_source;
175+
176+
public:
177+
explicit HashVerifier(Source& source LIFETIMEBOUND) : m_source{source} {}
178+
179+
void read(Span<std::byte> dst)
180+
{
181+
m_source.read(dst);
182+
this->write(dst);
183+
}
184+
185+
void ignore(size_t num_bytes)
186+
{
187+
std::byte data[1024];
188+
while (num_bytes > 0) {
189+
size_t now = std::min<size_t>(num_bytes, 1024);
190+
read({data, now});
191+
num_bytes -= now;
192+
}
193+
}
194+
195+
template <typename T>
196+
HashVerifier<Source>& operator>>(T&& obj)
197+
{
198+
::Unserialize(*this, obj);
199+
return *this;
200+
}
201+
};
202+
170203
template<typename Source>
171204
class CHashVerifier : public CHashWriter
172205
{

src/node/blockstorage.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ bool BlockManager::LoadBlockIndexDB(const Consensus::Params& consensus_params)
352352
}
353353
for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++) {
354354
FlatFilePos pos(*it, 0);
355-
if (CAutoFile(OpenBlockFile(pos, true), SER_DISK, CLIENT_VERSION).IsNull()) {
355+
if (AutoFile{OpenBlockFile(pos, true)}.IsNull()) {
356356
return false;
357357
}
358358
}
@@ -454,13 +454,13 @@ CBlockFileInfo* BlockManager::GetBlockFileInfo(size_t n)
454454
static bool UndoWriteToDisk(const CBlockUndo& blockundo, FlatFilePos& pos, const uint256& hashBlock, const CMessageHeader::MessageStartChars& messageStart)
455455
{
456456
// Open history file to append
457-
CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
457+
AutoFile fileout{OpenUndoFile(pos)};
458458
if (fileout.IsNull()) {
459459
return error("%s: OpenUndoFile failed", __func__);
460460
}
461461

462462
// Write index header
463-
unsigned int nSize = GetSerializeSize(blockundo, fileout.GetVersion());
463+
unsigned int nSize = GetSerializeSize(blockundo, CLIENT_VERSION);
464464
fileout << messageStart << nSize;
465465

466466
// Write undo data
@@ -489,14 +489,14 @@ bool UndoReadFromDisk(CBlockUndo& blockundo, const CBlockIndex* pindex)
489489
}
490490

491491
// Open history file to read
492-
CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
492+
AutoFile filein{OpenUndoFile(pos, true)};
493493
if (filein.IsNull()) {
494494
return error("%s: OpenUndoFile failed", __func__);
495495
}
496496

497497
// Read block
498498
uint256 hashChecksum;
499-
CHashVerifier<CAutoFile> verifier(&filein); // We need a CHashVerifier as reserializing may lose data
499+
HashVerifier verifier{filein}; // Use HashVerifier as reserializing may lose data, c.f. commit d342424301013ec47dc146a4beb49d5c9319d80a
500500
try {
501501
verifier << pindex->pprev->GetBlockHash();
502502
verifier >> blockundo;
@@ -768,7 +768,7 @@ bool ReadRawBlockFromDisk(std::vector<uint8_t>& block, const FlatFilePos& pos, c
768768
{
769769
FlatFilePos hpos = pos;
770770
hpos.nPos -= 8; // Seek back 8 bytes for meta header
771-
CAutoFile filein(OpenBlockFile(hpos, true), SER_DISK, CLIENT_VERSION);
771+
AutoFile filein{OpenBlockFile(hpos, true)};
772772
if (filein.IsNull()) {
773773
return error("%s: OpenBlockFile failed for %s", __func__, pos.ToString());
774774
}

0 commit comments

Comments
 (0)