mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-12-08 11:44:14 +01:00
Add -blocksxor boolean option
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
namespace node {
|
||||
util::Result<void> ApplyArgsManOptions(const ArgsManager& args, BlockManager::Options& opts)
|
||||
{
|
||||
if (auto value{args.GetBoolArg("-blocksxor")}) opts.use_xor = *value;
|
||||
// block pruning; get the amount of disk space (in MiB) to allot for block & undo files
|
||||
int64_t nPruneArg{args.GetIntArg("-prune", opts.prune_target)};
|
||||
if (nPruneArg < 0) {
|
||||
|
||||
@@ -19,6 +19,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>
|
||||
@@ -1144,9 +1145,43 @@ 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, "wbx")};
|
||||
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{},
|
||||
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}},
|
||||
|
||||
Reference in New Issue
Block a user