Skip to content

Commit 9670a84

Browse files
committed
Move UndoWriteToDisk() and UndoReadFromDisk() to anon namespace
[backports bitcoin/bitcoin@87fb310]
1 parent 4fe609f commit 9670a84

File tree

1 file changed

+56
-56
lines changed

1 file changed

+56
-56
lines changed

src/main.cpp

Lines changed: 56 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,8 +1933,62 @@ static bool AbortNode(CValidationState& state, const std::string& strMessage, co
19331933
return state.Error(strMessage);
19341934
}
19351935

1936-
bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint256& hashBlock);
1937-
bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uint256& hashBlock);
1936+
namespace {
1937+
1938+
bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint256& hashBlock)
1939+
{
1940+
// Open history file to append
1941+
CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
1942+
if (fileout.IsNull())
1943+
return error("%s : OpenUndoFile failed", __func__);
1944+
1945+
// Write index header
1946+
unsigned int nSize = GetSerializeSize(fileout, blockundo);
1947+
fileout << FLATDATA(Params().MessageStart()) << nSize;
1948+
1949+
// Write undo data
1950+
long fileOutPos = ftell(fileout.Get());
1951+
if (fileOutPos < 0)
1952+
return error("%s : ftell failed", __func__);
1953+
pos.nPos = (unsigned int)fileOutPos;
1954+
fileout << blockundo;
1955+
1956+
// calculate & write checksum
1957+
CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
1958+
hasher << hashBlock;
1959+
hasher << blockundo;
1960+
fileout << hasher.GetHash();
1961+
1962+
return true;
1963+
}
1964+
1965+
bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uint256& hashBlock)
1966+
{
1967+
// Open history file to read
1968+
CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
1969+
if (filein.IsNull())
1970+
return error("%s : OpenBlockFile failed", __func__);
1971+
1972+
// Read block
1973+
uint256 hashChecksum;
1974+
try {
1975+
filein >> blockundo;
1976+
filein >> hashChecksum;
1977+
} catch (const std::exception& e) {
1978+
return error("%s : Deserialize or I/O error - %s", __func__, e.what());
1979+
}
1980+
1981+
// Verify checksum
1982+
CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
1983+
hasher << hashBlock;
1984+
hasher << blockundo;
1985+
if (hashChecksum != hasher.GetHash())
1986+
return error("%s : Checksum mismatch", __func__);
1987+
1988+
return true;
1989+
}
1990+
1991+
} // anon namespace
19381992

19391993
bool DisconnectBlock(CBlock& block, CValidationState& state, CBlockIndex* pindex, CCoinsViewCache& view, bool* pfClean)
19401994
{
@@ -6220,60 +6274,6 @@ bool SendMessages(CNode* pto)
62206274
return true;
62216275
}
62226276

6223-
6224-
bool UndoWriteToDisk(const CBlockUndo& blockundo, CDiskBlockPos& pos, const uint256& hashBlock)
6225-
{
6226-
// Open history file to append
6227-
CAutoFile fileout(OpenUndoFile(pos), SER_DISK, CLIENT_VERSION);
6228-
if (fileout.IsNull())
6229-
return error("%s : OpenUndoFile failed", __func__);
6230-
6231-
// Write index header
6232-
unsigned int nSize = GetSerializeSize(fileout, blockundo);
6233-
fileout << FLATDATA(Params().MessageStart()) << nSize;
6234-
6235-
// Write undo data
6236-
long fileOutPos = ftell(fileout.Get());
6237-
if (fileOutPos < 0)
6238-
return error("%s : ftell failed", __func__);
6239-
pos.nPos = (unsigned int)fileOutPos;
6240-
fileout << blockundo;
6241-
6242-
// calculate & write checksum
6243-
CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
6244-
hasher << hashBlock;
6245-
hasher << blockundo;
6246-
fileout << hasher.GetHash();
6247-
6248-
return true;
6249-
}
6250-
6251-
bool UndoReadFromDisk(CBlockUndo& blockundo, const CDiskBlockPos& pos, const uint256& hashBlock)
6252-
{
6253-
// Open history file to read
6254-
CAutoFile filein(OpenUndoFile(pos, true), SER_DISK, CLIENT_VERSION);
6255-
if (filein.IsNull())
6256-
return error("%s : OpenBlockFile failed", __func__);
6257-
6258-
// Read block
6259-
uint256 hashChecksum;
6260-
try {
6261-
filein >> blockundo;
6262-
filein >> hashChecksum;
6263-
} catch (const std::exception& e) {
6264-
return error("%s : Deserialize or I/O error - %s", __func__, e.what());
6265-
}
6266-
6267-
// Verify checksum
6268-
CHashWriter hasher(SER_GETHASH, PROTOCOL_VERSION);
6269-
hasher << hashBlock;
6270-
hasher << blockundo;
6271-
if (hashChecksum != hasher.GetHash())
6272-
return error("%s : Checksum mismatch", __func__);
6273-
6274-
return true;
6275-
}
6276-
62776277
std::string CBlockFileInfo::ToString() const
62786278
{
62796279
return strprintf("CBlockFileInfo(blocks=%u, size=%u, heights=%u...%u, time=%s...%s)", nBlocks, nSize, nHeightFirst, nHeightLast, DateTimeStrFormat("%Y-%m-%d", nTimeFirst), DateTimeStrFormat("%Y-%m-%d", nTimeLast));

0 commit comments

Comments
 (0)