From 0b8bec8aa6260c499c2663ab7a1c905da0d312c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Fri, 25 Apr 2025 23:18:48 +0200 Subject: [PATCH] scripted-diff: unify xor-vs-obfuscation nomenclature Mechanical refactor of the low-level "xor" wording to signal the intent instead of the implementation used. The renames are ordered by heaviest-hitting substitutions first, and were constructed such that after each replacement the code is still compilable. -BEGIN VERIFY SCRIPT- sed -i \ -e 's/\bGetObfuscateKey\b/GetObfuscation/g' \ -e 's/\bxor_key\b/obfuscation/g' \ -e 's/\bxor_pat\b/obfuscation/g' \ -e 's/\bm_xor_key\b/m_obfuscation/g' \ -e 's/\bm_xor\b/m_obfuscation/g' \ -e 's/\bobfuscate_key\b/m_obfuscation/g' \ -e 's/\bOBFUSCATE_KEY_KEY\b/OBFUSCATION_KEY_KEY/g' \ -e 's/\bSetXor(/SetObfuscation(/g' \ -e 's/\bdata_xor\b/obfuscation/g' \ -e 's/\bCreateObfuscateKey\b/CreateObfuscation/g' \ -e 's/\bobfuscate key\b/obfuscation key/g' \ $(git ls-files '*.cpp' '*.h') -END VERIFY SCRIPT- --- src/dbwrapper.cpp | 24 ++++++++++++------------ src/dbwrapper.h | 14 +++++++------- src/node/blockstorage.cpp | 22 +++++++++++----------- src/node/blockstorage.h | 2 +- src/node/mempool_persist.cpp | 14 +++++++------- src/streams.cpp | 14 +++++++------- src/streams.h | 6 +++--- src/test/dbwrapper_tests.cpp | 12 ++++++------ src/test/streams_tests.cpp | 10 +++++----- 9 files changed, 59 insertions(+), 59 deletions(-) diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp index 19b1adafca5..45f806705b4 100644 --- a/src/dbwrapper.cpp +++ b/src/dbwrapper.cpp @@ -174,7 +174,7 @@ void CDBBatch::Clear() void CDBBatch::WriteImpl(std::span key, DataStream& ssValue) { leveldb::Slice slKey(CharCast(key.data()), key.size()); - ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent)); + ssValue.Xor(dbwrapper_private::GetObfuscation(parent)); leveldb::Slice slValue(CharCast(ssValue.data()), ssValue.size()); m_impl_batch->batch.Put(slKey, slValue); } @@ -250,23 +250,23 @@ CDBWrapper::CDBWrapper(const DBParams& params) } // The base-case obfuscation key, which is a noop. - obfuscate_key = std::vector(Obfuscation::KEY_SIZE, '\000'); + m_obfuscation = std::vector(Obfuscation::KEY_SIZE, '\000'); - bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key); + bool key_exists = Read(OBFUSCATION_KEY_KEY, m_obfuscation); if (!key_exists && params.obfuscate && IsEmpty()) { // Initialize non-degenerate obfuscation if it won't upset // existing, non-obfuscated data. - std::vector new_key = CreateObfuscateKey(); + std::vector new_key = CreateObfuscation(); // Write `new_key` so we don't obfuscate the key with itself - Write(OBFUSCATE_KEY_KEY, new_key); - obfuscate_key = new_key; + Write(OBFUSCATION_KEY_KEY, new_key); + m_obfuscation = new_key; - LogPrintf("Wrote new obfuscate key for %s: %s\n", fs::PathToString(params.path), HexStr(obfuscate_key)); + LogPrintf("Wrote new obfuscation key for %s: %s\n", fs::PathToString(params.path), HexStr(m_obfuscation)); } - LogPrintf("Using obfuscation key for %s: %s\n", fs::PathToString(params.path), HexStr(obfuscate_key)); + LogPrintf("Using obfuscation key for %s: %s\n", fs::PathToString(params.path), HexStr(m_obfuscation)); } CDBWrapper::~CDBWrapper() @@ -315,13 +315,13 @@ size_t CDBWrapper::DynamicMemoryUsage() const // // We must use a string constructor which specifies length so that we copy // past the null-terminator. -const std::string CDBWrapper::OBFUSCATE_KEY_KEY("\000obfuscate_key", 14); +const std::string CDBWrapper::OBFUSCATION_KEY_KEY("\000obfuscate_key", 14); /** * Returns a string (consisting of 8 random bytes) suitable for use as an * obfuscating XOR key. */ -std::vector CDBWrapper::CreateObfuscateKey() const +std::vector CDBWrapper::CreateObfuscation() const { std::vector ret(Obfuscation::KEY_SIZE); GetRandBytes(ret); @@ -411,9 +411,9 @@ void CDBIterator::Next() { m_impl_iter->iter->Next(); } namespace dbwrapper_private { -const std::vector& GetObfuscateKey(const CDBWrapper &w) +const std::vector& GetObfuscation(const CDBWrapper &w) { - return w.obfuscate_key; + return w.m_obfuscation; } } // namespace dbwrapper_private diff --git a/src/dbwrapper.h b/src/dbwrapper.h index 64d428ce5de..88684cab175 100644 --- a/src/dbwrapper.h +++ b/src/dbwrapper.h @@ -63,7 +63,7 @@ namespace dbwrapper_private { * Database obfuscation should be considered an implementation detail of the * specific database. */ -const std::vector& GetObfuscateKey(const CDBWrapper &w); +const std::vector& GetObfuscation(const CDBWrapper &w); }; // namespace dbwrapper_private @@ -166,7 +166,7 @@ public: template bool GetValue(V& value) { try { DataStream ssValue{GetValueImpl()}; - ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent)); + ssValue.Xor(dbwrapper_private::GetObfuscation(parent)); ssValue >> value; } catch (const std::exception&) { return false; @@ -179,7 +179,7 @@ struct LevelDBContext; class CDBWrapper { - friend const std::vector& dbwrapper_private::GetObfuscateKey(const CDBWrapper &w); + friend const std::vector& dbwrapper_private::GetObfuscation(const CDBWrapper &w); private: //! holds all leveldb-specific fields of this class std::unique_ptr m_db_context; @@ -188,12 +188,12 @@ private: std::string m_name; //! a key used for optional XOR-obfuscation of the database - std::vector obfuscate_key; + std::vector m_obfuscation; //! the key under which the obfuscation key is stored - static const std::string OBFUSCATE_KEY_KEY; + static const std::string OBFUSCATION_KEY_KEY; - std::vector CreateObfuscateKey() const; + std::vector CreateObfuscation() const; //! path to filesystem storage const fs::path m_path; @@ -225,7 +225,7 @@ public: } try { DataStream ssValue{MakeByteSpan(*strValue)}; - ssValue.Xor(obfuscate_key); + ssValue.Xor(m_obfuscation); ssValue >> value; } catch (const std::exception&) { return false; diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index e224c5985d8..53a2fd14c38 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -780,13 +780,13 @@ void BlockManager::UnlinkPrunedFiles(const std::set& setFilesToPrune) const AutoFile BlockManager::OpenBlockFile(const FlatFilePos& pos, bool fReadOnly) const { - return AutoFile{m_block_file_seq.Open(pos, fReadOnly), m_xor_key}; + return AutoFile{m_block_file_seq.Open(pos, fReadOnly), m_obfuscation}; } /** Open an undo file (rev?????.dat) */ AutoFile BlockManager::OpenUndoFile(const FlatFilePos& pos, bool fReadOnly) const { - return AutoFile{m_undo_file_seq.Open(pos, fReadOnly), m_xor_key}; + return AutoFile{m_undo_file_seq.Open(pos, fReadOnly), m_obfuscation}; } fs::path BlockManager::GetBlockPosFilename(const FlatFilePos& pos) const @@ -1124,7 +1124,7 @@ 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 xor_key{}; + std::array obfuscation{}; // Consider this to be the first run if the blocksdir contains only hidden // files (those which start with a .). Checking for a fully-empty dir would @@ -1141,14 +1141,14 @@ static auto InitBlocksdirXorKey(const BlockManager::Options& opts) if (opts.use_xor && first_run) { // Only use random fresh key when the boolean option is set and on the // very first start of the program. - FastRandomContext{}.fillrand(xor_key); + FastRandomContext{}.fillrand(obfuscation); } 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; + xor_key_file >> obfuscation; } else { // Create initial or missing xor key file AutoFile xor_key_file{fsbridge::fopen(xor_key_path, @@ -1158,7 +1158,7 @@ static auto InitBlocksdirXorKey(const BlockManager::Options& opts) "wbx" #endif )}; - xor_key_file << xor_key; + xor_key_file << obfuscation; if (xor_key_file.fclose() != 0) { throw std::runtime_error{strprintf("Error closing XOR key file %s: %s", fs::PathToString(xor_key_path), @@ -1166,20 +1166,20 @@ static auto InitBlocksdirXorKey(const BlockManager::Options& opts) } } // If the user disabled the key, it must be zero. - if (!opts.use_xor && xor_key != decltype(xor_key){}) { + if (!opts.use_xor && obfuscation != decltype(obfuscation){}) { 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)), + HexStr(obfuscation), 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{xor_key.begin(), xor_key.end()}; + LogInfo("Using obfuscation key for blocksdir *.dat files (%s): '%s'\n", fs::PathToString(opts.blocks_dir), HexStr(obfuscation)); + return std::vector{obfuscation.begin(), obfuscation.end()}; } BlockManager::BlockManager(const util::SignalInterrupt& interrupt, Options opts) : m_prune_mode{opts.prune_target > 0}, - m_xor_key{InitBlocksdirXorKey(opts)}, + m_obfuscation{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}}, diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h index f9a03eaf83b..5c3a5f024b8 100644 --- a/src/node/blockstorage.h +++ b/src/node/blockstorage.h @@ -235,7 +235,7 @@ private: const bool m_prune_mode; - const std::vector m_xor_key; + const std::vector m_obfuscation; /** Dirty block index entries. */ std::set m_dirty_blockindex; diff --git a/src/node/mempool_persist.cpp b/src/node/mempool_persist.cpp index eeb690b0877..9054fd7bb88 100644 --- a/src/node/mempool_persist.cpp +++ b/src/node/mempool_persist.cpp @@ -60,15 +60,15 @@ bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active try { uint64_t version; file >> version; - std::vector xor_key; + std::vector obfuscation; if (version == MEMPOOL_DUMP_VERSION_NO_XOR_KEY) { // Leave XOR-key empty } else if (version == MEMPOOL_DUMP_VERSION) { - file >> xor_key; + file >> obfuscation; } else { return false; } - file.SetXor(xor_key); + file.SetObfuscation(obfuscation); uint64_t total_txns_to_load; file >> total_txns_to_load; uint64_t txns_tried = 0; @@ -180,12 +180,12 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock const uint64_t version{pool.m_opts.persist_v1_dat ? MEMPOOL_DUMP_VERSION_NO_XOR_KEY : MEMPOOL_DUMP_VERSION}; file << version; - std::vector xor_key(Obfuscation::KEY_SIZE); + std::vector obfuscation(Obfuscation::KEY_SIZE); if (!pool.m_opts.persist_v1_dat) { - FastRandomContext{}.fillrand(xor_key); - file << xor_key; + FastRandomContext{}.fillrand(obfuscation); + file << obfuscation; } - file.SetXor(xor_key); + file.SetObfuscation(obfuscation); uint64_t mempool_transactions_to_write(vinfo.size()); file << mempool_transactions_to_write; diff --git a/src/streams.cpp b/src/streams.cpp index 2c873e50d3b..ed80d11dcee 100644 --- a/src/streams.cpp +++ b/src/streams.cpp @@ -9,8 +9,8 @@ #include -AutoFile::AutoFile(std::FILE* file, std::vector data_xor) - : m_file{file}, m_xor{std::move(data_xor)} +AutoFile::AutoFile(std::FILE* file, std::vector obfuscation) + : m_file{file}, m_obfuscation{std::move(obfuscation)} { if (!IsNull()) { auto pos{std::ftell(m_file)}; @@ -22,9 +22,9 @@ std::size_t AutoFile::detail_fread(std::span dst) { if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr"); size_t ret = std::fread(dst.data(), 1, dst.size(), m_file); - if (!m_xor.empty()) { + if (!m_obfuscation.empty()) { if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::read: position unknown"); - util::Xor(dst.subspan(0, ret), m_xor, *m_position); + util::Xor(dst.subspan(0, ret), m_obfuscation, *m_position); } if (m_position.has_value()) *m_position += ret; return ret; @@ -81,7 +81,7 @@ void AutoFile::ignore(size_t nSize) void AutoFile::write(std::span src) { if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr"); - if (m_xor.empty()) { + if (m_obfuscation.empty()) { if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) { throw std::ios_base::failure("AutoFile::write: write failed"); } @@ -101,9 +101,9 @@ void AutoFile::write(std::span src) void AutoFile::write_buffer(std::span src) { if (!m_file) throw std::ios_base::failure("AutoFile::write_buffer: file handle is nullptr"); - if (m_xor.size()) { + if (m_obfuscation.size()) { if (!m_position) throw std::ios_base::failure("AutoFile::write_buffer: obfuscation position unknown"); - util::Xor(src, m_xor, *m_position); // obfuscate in-place + util::Xor(src, m_obfuscation, *m_position); // obfuscate in-place } if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) { throw std::ios_base::failure("AutoFile::write_buffer: write failed"); diff --git a/src/streams.h b/src/streams.h index ae9cb94bae3..ac6d5a0de16 100644 --- a/src/streams.h +++ b/src/streams.h @@ -402,12 +402,12 @@ class AutoFile { protected: std::FILE* m_file; - std::vector m_xor; + std::vector m_obfuscation; std::optional m_position; bool m_was_written{false}; public: - explicit AutoFile(std::FILE* file, std::vector data_xor={}); + explicit AutoFile(std::FILE* file, std::vector obfuscation={}); ~AutoFile() { @@ -455,7 +455,7 @@ public: bool IsNull() const { return m_file == nullptr; } /** Continue with a different XOR key */ - void SetXor(std::vector data_xor) { m_xor = data_xor; } + void SetObfuscation(std::vector obfuscation) { m_obfuscation = obfuscation; } /** Implementation detail, only used internally. */ std::size_t detail_fread(std::span dst); diff --git a/src/test/dbwrapper_tests.cpp b/src/test/dbwrapper_tests.cpp index bd50db17c13..9c723351d6b 100644 --- a/src/test/dbwrapper_tests.cpp +++ b/src/test/dbwrapper_tests.cpp @@ -42,7 +42,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper) BOOST_CHECK_EQUAL(obfuscate, !dbw.IsEmpty()); // Ensure that we're doing real obfuscation when obfuscate=true - obfuscation_key = dbwrapper_private::GetObfuscateKey(dbw); + obfuscation_key = dbwrapper_private::GetObfuscation(dbw); BOOST_CHECK_EQUAL(obfuscate, !is_null_key(obfuscation_key)); for (uint8_t k{0}; k < 10; ++k) { @@ -56,7 +56,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper) // Verify that the obfuscation key is never obfuscated { CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = false}}; - BOOST_CHECK(obfuscation_key == dbwrapper_private::GetObfuscateKey(dbw)); + BOOST_CHECK(obfuscation_key == dbwrapper_private::GetObfuscation(dbw)); } // Read back the values @@ -64,7 +64,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper) CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = obfuscate}}; // Ensure obfuscation is read back correctly - BOOST_CHECK(obfuscation_key == dbwrapper_private::GetObfuscateKey(dbw)); + BOOST_CHECK(obfuscation_key == dbwrapper_private::GetObfuscation(dbw)); BOOST_CHECK_EQUAL(obfuscate, !is_null_key(obfuscation_key)); // Verify all written values @@ -89,7 +89,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data) bool res_bool; // Ensure that we're doing real obfuscation when obfuscate=true - BOOST_CHECK_EQUAL(obfuscate, !is_null_key(dbwrapper_private::GetObfuscateKey(dbw))); + BOOST_CHECK_EQUAL(obfuscate, !is_null_key(dbwrapper_private::GetObfuscation(dbw))); //Simulate block raw data - "b + block hash" std::string key_block = "b" + m_rng.rand256().ToString(); @@ -264,7 +264,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate) BOOST_CHECK_EQUAL(res2.ToString(), in.ToString()); BOOST_CHECK(!odbw.IsEmpty()); - BOOST_CHECK(is_null_key(dbwrapper_private::GetObfuscateKey(odbw))); // The key should be an empty string + BOOST_CHECK(is_null_key(dbwrapper_private::GetObfuscation(odbw))); // The key should be an empty string uint256 in2 = m_rng.rand256(); uint256 res3; @@ -301,7 +301,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex) // Check that the key/val we wrote with unobfuscated wrapper doesn't exist uint256 res2; BOOST_CHECK(!odbw.Read(key, res2)); - BOOST_CHECK(!is_null_key(dbwrapper_private::GetObfuscateKey(odbw))); + BOOST_CHECK(!is_null_key(dbwrapper_private::GetObfuscation(odbw))); uint256 in2 = m_rng.rand256(); uint256 res3; diff --git a/src/test/streams_tests.cpp b/src/test/streams_tests.cpp index 9c1e1032a0f..5da1ff99154 100644 --- a/src/test/streams_tests.cpp +++ b/src/test/streams_tests.cpp @@ -79,11 +79,11 @@ BOOST_AUTO_TEST_CASE(xor_file) auto raw_file{[&](const auto& mode) { return fsbridge::fopen(xor_path, mode); }}; const std::vector test1{1, 2, 3}; const std::vector test2{4, 5}; - const auto xor_pat{"ff00ff00ff00ff00"_hex_v}; + const auto obfuscation{"ff00ff00ff00ff00"_hex_v}; { // Check errors for missing file - AutoFile xor_file{raw_file("rb"), xor_pat}; + AutoFile xor_file{raw_file("rb"), obfuscation}; BOOST_CHECK_EXCEPTION(xor_file << std::byte{}, std::ios_base::failure, HasReason{"AutoFile::write: file handle is nullptr"}); BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: file handle is nullptr"}); BOOST_CHECK_EXCEPTION(xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: file handle is nullptr"}); @@ -95,7 +95,7 @@ BOOST_AUTO_TEST_CASE(xor_file) #else const char* mode = "wbx"; #endif - AutoFile xor_file{raw_file(mode), xor_pat}; + AutoFile xor_file{raw_file(mode), obfuscation}; xor_file << test1 << test2; BOOST_REQUIRE_EQUAL(xor_file.fclose(), 0); } @@ -109,7 +109,7 @@ BOOST_AUTO_TEST_CASE(xor_file) BOOST_CHECK_EXCEPTION(non_xor_file.ignore(1), std::ios_base::failure, HasReason{"AutoFile::ignore: end of file"}); } { - AutoFile xor_file{raw_file("rb"), xor_pat}; + AutoFile xor_file{raw_file("rb"), obfuscation}; std::vector read1, read2; xor_file >> read1 >> read2; BOOST_CHECK_EQUAL(HexStr(read1), HexStr(test1)); @@ -118,7 +118,7 @@ BOOST_AUTO_TEST_CASE(xor_file) BOOST_CHECK_EXCEPTION(xor_file >> std::byte{}, std::ios_base::failure, HasReason{"AutoFile::read: end of file"}); } { - AutoFile xor_file{raw_file("rb"), xor_pat}; + AutoFile xor_file{raw_file("rb"), obfuscation}; std::vector read2; // Check that ignore works xor_file.ignore(4);