Merge 949b673472 into merged_master (Bitcoin PR bitcoin/bitcoin#28052)

This commit is contained in:
ivanlele 2026-03-11 12:29:52 +00:00
commit e2c90769ca
No known key found for this signature in database
10 changed files with 97 additions and 16 deletions

View file

@ -20,6 +20,7 @@
#include <pow.h>
#include <primitives/block.h>
#include <primitives/transaction.h>
#include <random.h>
#include <reverse_iterator.h>
#include <serialize.h>
#include <signet.h>
@ -911,13 +912,13 @@ void BlockManager::UnlinkPrunedFiles(const std::set<int>& setFilesToPrune) const
AutoFile BlockManager::OpenBlockFile(const FlatFilePos& pos, bool fReadOnly) const
{
return AutoFile{m_block_file_seq.Open(pos, fReadOnly)};
return AutoFile{m_block_file_seq.Open(pos, fReadOnly), m_xor_key};
}
/** Open an undo file (rev?????.dat) */
AutoFile BlockManager::OpenUndoFile(const FlatFilePos& pos, bool fReadOnly) const
{
return AutoFile{m_undo_file_seq.Open(pos, fReadOnly)};
return AutoFile{m_undo_file_seq.Open(pos, fReadOnly), m_xor_key};
}
fs::path BlockManager::GetBlockPosFilename(const FlatFilePos& pos) const
@ -1239,6 +1240,54 @@ FlatFilePos BlockManager::SaveBlockToDisk(const CBlock& block, int nHeight)
return blockPos;
}
static auto InitBlocksdirXorKey(const BlockManager::Options& opts)
{
// Bytes are serialized without length indicator, so this is also the exact
// size of the XOR-key file.
std::array<std::byte, 8> xor_key{};
if (opts.use_xor && fs::is_empty(opts.blocks_dir)) {
// Only use random fresh key when the boolean option is set and on the
// very first start of the program.
FastRandomContext{}.fillrand(xor_key);
}
const fs::path xor_key_path{opts.blocks_dir / "xor.dat"};
if (fs::exists(xor_key_path)) {
// A pre-existing xor key file has priority.
AutoFile xor_key_file{fsbridge::fopen(xor_key_path, "rb")};
xor_key_file >> xor_key;
} else {
// Create initial or missing xor key file
AutoFile xor_key_file{fsbridge::fopen(xor_key_path,
#ifdef __MINGW64__
"wb" // Temporary workaround for https://github.com/bitcoin/bitcoin/issues/30210
#else
"wbx"
#endif
)};
xor_key_file << xor_key;
}
// If the user disabled the key, it must be zero.
if (!opts.use_xor && xor_key != decltype(xor_key){}) {
throw std::runtime_error{
strprintf("The blocksdir XOR-key can not be disabled when a random key was already stored! "
"Stored key: '%s', stored path: '%s'.",
HexStr(xor_key), fs::PathToString(xor_key_path)),
};
}
LogInfo("Using obfuscation key for blocksdir *.dat files (%s): '%s'\n", fs::PathToString(opts.blocks_dir), HexStr(xor_key));
return std::vector<std::byte>{xor_key.begin(), xor_key.end()};
}
BlockManager::BlockManager(const util::SignalInterrupt& interrupt, Options opts)
: m_prune_mode{opts.prune_target > 0},
m_xor_key{InitBlocksdirXorKey(opts)},
m_opts{std::move(opts)},
m_block_file_seq{FlatFileSeq{m_opts.blocks_dir, "blk", m_opts.fast_prune ? 0x4000 /* 16kB */ : BLOCKFILE_CHUNK_SIZE}},
m_undo_file_seq{FlatFileSeq{m_opts.blocks_dir, "rev", UNDOFILE_CHUNK_SIZE}},
m_interrupt{interrupt} {}
class ImportingNow
{
std::atomic<bool>& m_importing;