Skip to content

Commit 0b8bec8

Browse files
committed
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-
1 parent 9726979 commit 0b8bec8

File tree

9 files changed

+59
-59
lines changed

9 files changed

+59
-59
lines changed

src/dbwrapper.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ void CDBBatch::Clear()
174174
void CDBBatch::WriteImpl(std::span<const std::byte> key, DataStream& ssValue)
175175
{
176176
leveldb::Slice slKey(CharCast(key.data()), key.size());
177-
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
177+
ssValue.Xor(dbwrapper_private::GetObfuscation(parent));
178178
leveldb::Slice slValue(CharCast(ssValue.data()), ssValue.size());
179179
m_impl_batch->batch.Put(slKey, slValue);
180180
}
@@ -250,23 +250,23 @@ CDBWrapper::CDBWrapper(const DBParams& params)
250250
}
251251

252252
// The base-case obfuscation key, which is a noop.
253-
obfuscate_key = std::vector<unsigned char>(Obfuscation::KEY_SIZE, '\000');
253+
m_obfuscation = std::vector<unsigned char>(Obfuscation::KEY_SIZE, '\000');
254254

255-
bool key_exists = Read(OBFUSCATE_KEY_KEY, obfuscate_key);
255+
bool key_exists = Read(OBFUSCATION_KEY_KEY, m_obfuscation);
256256

257257
if (!key_exists && params.obfuscate && IsEmpty()) {
258258
// Initialize non-degenerate obfuscation if it won't upset
259259
// existing, non-obfuscated data.
260-
std::vector<unsigned char> new_key = CreateObfuscateKey();
260+
std::vector<unsigned char> new_key = CreateObfuscation();
261261

262262
// Write `new_key` so we don't obfuscate the key with itself
263-
Write(OBFUSCATE_KEY_KEY, new_key);
264-
obfuscate_key = new_key;
263+
Write(OBFUSCATION_KEY_KEY, new_key);
264+
m_obfuscation = new_key;
265265

266-
LogPrintf("Wrote new obfuscate key for %s: %s\n", fs::PathToString(params.path), HexStr(obfuscate_key));
266+
LogPrintf("Wrote new obfuscation key for %s: %s\n", fs::PathToString(params.path), HexStr(m_obfuscation));
267267
}
268268

269-
LogPrintf("Using obfuscation key for %s: %s\n", fs::PathToString(params.path), HexStr(obfuscate_key));
269+
LogPrintf("Using obfuscation key for %s: %s\n", fs::PathToString(params.path), HexStr(m_obfuscation));
270270
}
271271

272272
CDBWrapper::~CDBWrapper()
@@ -315,13 +315,13 @@ size_t CDBWrapper::DynamicMemoryUsage() const
315315
//
316316
// We must use a string constructor which specifies length so that we copy
317317
// past the null-terminator.
318-
const std::string CDBWrapper::OBFUSCATE_KEY_KEY("\000obfuscate_key", 14);
318+
const std::string CDBWrapper::OBFUSCATION_KEY_KEY("\000obfuscate_key", 14);
319319

320320
/**
321321
* Returns a string (consisting of 8 random bytes) suitable for use as an
322322
* obfuscating XOR key.
323323
*/
324-
std::vector<unsigned char> CDBWrapper::CreateObfuscateKey() const
324+
std::vector<unsigned char> CDBWrapper::CreateObfuscation() const
325325
{
326326
std::vector<uint8_t> ret(Obfuscation::KEY_SIZE);
327327
GetRandBytes(ret);
@@ -411,9 +411,9 @@ void CDBIterator::Next() { m_impl_iter->iter->Next(); }
411411

412412
namespace dbwrapper_private {
413413

414-
const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper &w)
414+
const std::vector<unsigned char>& GetObfuscation(const CDBWrapper &w)
415415
{
416-
return w.obfuscate_key;
416+
return w.m_obfuscation;
417417
}
418418

419419
} // namespace dbwrapper_private

src/dbwrapper.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ namespace dbwrapper_private {
6363
* Database obfuscation should be considered an implementation detail of the
6464
* specific database.
6565
*/
66-
const std::vector<unsigned char>& GetObfuscateKey(const CDBWrapper &w);
66+
const std::vector<unsigned char>& GetObfuscation(const CDBWrapper &w);
6767

6868
}; // namespace dbwrapper_private
6969

@@ -166,7 +166,7 @@ class CDBIterator
166166
template<typename V> bool GetValue(V& value) {
167167
try {
168168
DataStream ssValue{GetValueImpl()};
169-
ssValue.Xor(dbwrapper_private::GetObfuscateKey(parent));
169+
ssValue.Xor(dbwrapper_private::GetObfuscation(parent));
170170
ssValue >> value;
171171
} catch (const std::exception&) {
172172
return false;
@@ -179,7 +179,7 @@ struct LevelDBContext;
179179

180180
class CDBWrapper
181181
{
182-
friend const std::vector<unsigned char>& dbwrapper_private::GetObfuscateKey(const CDBWrapper &w);
182+
friend const std::vector<unsigned char>& dbwrapper_private::GetObfuscation(const CDBWrapper &w);
183183
private:
184184
//! holds all leveldb-specific fields of this class
185185
std::unique_ptr<LevelDBContext> m_db_context;
@@ -188,12 +188,12 @@ class CDBWrapper
188188
std::string m_name;
189189

190190
//! a key used for optional XOR-obfuscation of the database
191-
std::vector<unsigned char> obfuscate_key;
191+
std::vector<unsigned char> m_obfuscation;
192192

193193
//! the key under which the obfuscation key is stored
194-
static const std::string OBFUSCATE_KEY_KEY;
194+
static const std::string OBFUSCATION_KEY_KEY;
195195

196-
std::vector<unsigned char> CreateObfuscateKey() const;
196+
std::vector<unsigned char> CreateObfuscation() const;
197197

198198
//! path to filesystem storage
199199
const fs::path m_path;
@@ -225,7 +225,7 @@ class CDBWrapper
225225
}
226226
try {
227227
DataStream ssValue{MakeByteSpan(*strValue)};
228-
ssValue.Xor(obfuscate_key);
228+
ssValue.Xor(m_obfuscation);
229229
ssValue >> value;
230230
} catch (const std::exception&) {
231231
return false;

src/node/blockstorage.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -780,13 +780,13 @@ void BlockManager::UnlinkPrunedFiles(const std::set<int>& setFilesToPrune) const
780780

781781
AutoFile BlockManager::OpenBlockFile(const FlatFilePos& pos, bool fReadOnly) const
782782
{
783-
return AutoFile{m_block_file_seq.Open(pos, fReadOnly), m_xor_key};
783+
return AutoFile{m_block_file_seq.Open(pos, fReadOnly), m_obfuscation};
784784
}
785785

786786
/** Open an undo file (rev?????.dat) */
787787
AutoFile BlockManager::OpenUndoFile(const FlatFilePos& pos, bool fReadOnly) const
788788
{
789-
return AutoFile{m_undo_file_seq.Open(pos, fReadOnly), m_xor_key};
789+
return AutoFile{m_undo_file_seq.Open(pos, fReadOnly), m_obfuscation};
790790
}
791791

792792
fs::path BlockManager::GetBlockPosFilename(const FlatFilePos& pos) const
@@ -1124,7 +1124,7 @@ static auto InitBlocksdirXorKey(const BlockManager::Options& opts)
11241124
{
11251125
// Bytes are serialized without length indicator, so this is also the exact
11261126
// size of the XOR-key file.
1127-
std::array<std::byte, Obfuscation::KEY_SIZE> xor_key{};
1127+
std::array<std::byte, Obfuscation::KEY_SIZE> obfuscation{};
11281128

11291129
// Consider this to be the first run if the blocksdir contains only hidden
11301130
// files (those which start with a .). Checking for a fully-empty dir would
@@ -1141,14 +1141,14 @@ static auto InitBlocksdirXorKey(const BlockManager::Options& opts)
11411141
if (opts.use_xor && first_run) {
11421142
// Only use random fresh key when the boolean option is set and on the
11431143
// very first start of the program.
1144-
FastRandomContext{}.fillrand(xor_key);
1144+
FastRandomContext{}.fillrand(obfuscation);
11451145
}
11461146

11471147
const fs::path xor_key_path{opts.blocks_dir / "xor.dat"};
11481148
if (fs::exists(xor_key_path)) {
11491149
// A pre-existing xor key file has priority.
11501150
AutoFile xor_key_file{fsbridge::fopen(xor_key_path, "rb")};
1151-
xor_key_file >> xor_key;
1151+
xor_key_file >> obfuscation;
11521152
} else {
11531153
// Create initial or missing xor key file
11541154
AutoFile xor_key_file{fsbridge::fopen(xor_key_path,
@@ -1158,28 +1158,28 @@ static auto InitBlocksdirXorKey(const BlockManager::Options& opts)
11581158
"wbx"
11591159
#endif
11601160
)};
1161-
xor_key_file << xor_key;
1161+
xor_key_file << obfuscation;
11621162
if (xor_key_file.fclose() != 0) {
11631163
throw std::runtime_error{strprintf("Error closing XOR key file %s: %s",
11641164
fs::PathToString(xor_key_path),
11651165
SysErrorString(errno))};
11661166
}
11671167
}
11681168
// If the user disabled the key, it must be zero.
1169-
if (!opts.use_xor && xor_key != decltype(xor_key){}) {
1169+
if (!opts.use_xor && obfuscation != decltype(obfuscation){}) {
11701170
throw std::runtime_error{
11711171
strprintf("The blocksdir XOR-key can not be disabled when a random key was already stored! "
11721172
"Stored key: '%s', stored path: '%s'.",
1173-
HexStr(xor_key), fs::PathToString(xor_key_path)),
1173+
HexStr(obfuscation), fs::PathToString(xor_key_path)),
11741174
};
11751175
}
1176-
LogInfo("Using obfuscation key for blocksdir *.dat files (%s): '%s'\n", fs::PathToString(opts.blocks_dir), HexStr(xor_key));
1177-
return std::vector<std::byte>{xor_key.begin(), xor_key.end()};
1176+
LogInfo("Using obfuscation key for blocksdir *.dat files (%s): '%s'\n", fs::PathToString(opts.blocks_dir), HexStr(obfuscation));
1177+
return std::vector<std::byte>{obfuscation.begin(), obfuscation.end()};
11781178
}
11791179

11801180
BlockManager::BlockManager(const util::SignalInterrupt& interrupt, Options opts)
11811181
: m_prune_mode{opts.prune_target > 0},
1182-
m_xor_key{InitBlocksdirXorKey(opts)},
1182+
m_obfuscation{InitBlocksdirXorKey(opts)},
11831183
m_opts{std::move(opts)},
11841184
m_block_file_seq{FlatFileSeq{m_opts.blocks_dir, "blk", m_opts.fast_prune ? 0x4000 /* 16kB */ : BLOCKFILE_CHUNK_SIZE}},
11851185
m_undo_file_seq{FlatFileSeq{m_opts.blocks_dir, "rev", UNDOFILE_CHUNK_SIZE}},

src/node/blockstorage.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ class BlockManager
235235

236236
const bool m_prune_mode;
237237

238-
const std::vector<std::byte> m_xor_key;
238+
const std::vector<std::byte> m_obfuscation;
239239

240240
/** Dirty block index entries. */
241241
std::set<CBlockIndex*> m_dirty_blockindex;

src/node/mempool_persist.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ bool LoadMempool(CTxMemPool& pool, const fs::path& load_path, Chainstate& active
6060
try {
6161
uint64_t version;
6262
file >> version;
63-
std::vector<std::byte> xor_key;
63+
std::vector<std::byte> obfuscation;
6464
if (version == MEMPOOL_DUMP_VERSION_NO_XOR_KEY) {
6565
// Leave XOR-key empty
6666
} else if (version == MEMPOOL_DUMP_VERSION) {
67-
file >> xor_key;
67+
file >> obfuscation;
6868
} else {
6969
return false;
7070
}
71-
file.SetXor(xor_key);
71+
file.SetObfuscation(obfuscation);
7272
uint64_t total_txns_to_load;
7373
file >> total_txns_to_load;
7474
uint64_t txns_tried = 0;
@@ -180,12 +180,12 @@ bool DumpMempool(const CTxMemPool& pool, const fs::path& dump_path, FopenFn mock
180180
const uint64_t version{pool.m_opts.persist_v1_dat ? MEMPOOL_DUMP_VERSION_NO_XOR_KEY : MEMPOOL_DUMP_VERSION};
181181
file << version;
182182

183-
std::vector<std::byte> xor_key(Obfuscation::KEY_SIZE);
183+
std::vector<std::byte> obfuscation(Obfuscation::KEY_SIZE);
184184
if (!pool.m_opts.persist_v1_dat) {
185-
FastRandomContext{}.fillrand(xor_key);
186-
file << xor_key;
185+
FastRandomContext{}.fillrand(obfuscation);
186+
file << obfuscation;
187187
}
188-
file.SetXor(xor_key);
188+
file.SetObfuscation(obfuscation);
189189

190190
uint64_t mempool_transactions_to_write(vinfo.size());
191191
file << mempool_transactions_to_write;

src/streams.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
#include <array>
1111

12-
AutoFile::AutoFile(std::FILE* file, std::vector<std::byte> data_xor)
13-
: m_file{file}, m_xor{std::move(data_xor)}
12+
AutoFile::AutoFile(std::FILE* file, std::vector<std::byte> obfuscation)
13+
: m_file{file}, m_obfuscation{std::move(obfuscation)}
1414
{
1515
if (!IsNull()) {
1616
auto pos{std::ftell(m_file)};
@@ -22,9 +22,9 @@ std::size_t AutoFile::detail_fread(std::span<std::byte> dst)
2222
{
2323
if (!m_file) throw std::ios_base::failure("AutoFile::read: file handle is nullptr");
2424
size_t ret = std::fread(dst.data(), 1, dst.size(), m_file);
25-
if (!m_xor.empty()) {
25+
if (!m_obfuscation.empty()) {
2626
if (!m_position.has_value()) throw std::ios_base::failure("AutoFile::read: position unknown");
27-
util::Xor(dst.subspan(0, ret), m_xor, *m_position);
27+
util::Xor(dst.subspan(0, ret), m_obfuscation, *m_position);
2828
}
2929
if (m_position.has_value()) *m_position += ret;
3030
return ret;
@@ -81,7 +81,7 @@ void AutoFile::ignore(size_t nSize)
8181
void AutoFile::write(std::span<const std::byte> src)
8282
{
8383
if (!m_file) throw std::ios_base::failure("AutoFile::write: file handle is nullptr");
84-
if (m_xor.empty()) {
84+
if (m_obfuscation.empty()) {
8585
if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
8686
throw std::ios_base::failure("AutoFile::write: write failed");
8787
}
@@ -101,9 +101,9 @@ void AutoFile::write(std::span<const std::byte> src)
101101
void AutoFile::write_buffer(std::span<std::byte> src)
102102
{
103103
if (!m_file) throw std::ios_base::failure("AutoFile::write_buffer: file handle is nullptr");
104-
if (m_xor.size()) {
104+
if (m_obfuscation.size()) {
105105
if (!m_position) throw std::ios_base::failure("AutoFile::write_buffer: obfuscation position unknown");
106-
util::Xor(src, m_xor, *m_position); // obfuscate in-place
106+
util::Xor(src, m_obfuscation, *m_position); // obfuscate in-place
107107
}
108108
if (std::fwrite(src.data(), 1, src.size(), m_file) != src.size()) {
109109
throw std::ios_base::failure("AutoFile::write_buffer: write failed");

src/streams.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,12 +402,12 @@ class AutoFile
402402
{
403403
protected:
404404
std::FILE* m_file;
405-
std::vector<std::byte> m_xor;
405+
std::vector<std::byte> m_obfuscation;
406406
std::optional<int64_t> m_position;
407407
bool m_was_written{false};
408408

409409
public:
410-
explicit AutoFile(std::FILE* file, std::vector<std::byte> data_xor={});
410+
explicit AutoFile(std::FILE* file, std::vector<std::byte> obfuscation={});
411411

412412
~AutoFile()
413413
{
@@ -455,7 +455,7 @@ class AutoFile
455455
bool IsNull() const { return m_file == nullptr; }
456456

457457
/** Continue with a different XOR key */
458-
void SetXor(std::vector<std::byte> data_xor) { m_xor = data_xor; }
458+
void SetObfuscation(std::vector<std::byte> obfuscation) { m_obfuscation = obfuscation; }
459459

460460
/** Implementation detail, only used internally. */
461461
std::size_t detail_fread(std::span<std::byte> dst);

src/test/dbwrapper_tests.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
4242
BOOST_CHECK_EQUAL(obfuscate, !dbw.IsEmpty());
4343

4444
// Ensure that we're doing real obfuscation when obfuscate=true
45-
obfuscation_key = dbwrapper_private::GetObfuscateKey(dbw);
45+
obfuscation_key = dbwrapper_private::GetObfuscation(dbw);
4646
BOOST_CHECK_EQUAL(obfuscate, !is_null_key(obfuscation_key));
4747

4848
for (uint8_t k{0}; k < 10; ++k) {
@@ -56,15 +56,15 @@ BOOST_AUTO_TEST_CASE(dbwrapper)
5656
// Verify that the obfuscation key is never obfuscated
5757
{
5858
CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = false}};
59-
BOOST_CHECK(obfuscation_key == dbwrapper_private::GetObfuscateKey(dbw));
59+
BOOST_CHECK(obfuscation_key == dbwrapper_private::GetObfuscation(dbw));
6060
}
6161

6262
// Read back the values
6363
{
6464
CDBWrapper dbw{{.path = path, .cache_bytes = CACHE_SIZE, .obfuscate = obfuscate}};
6565

6666
// Ensure obfuscation is read back correctly
67-
BOOST_CHECK(obfuscation_key == dbwrapper_private::GetObfuscateKey(dbw));
67+
BOOST_CHECK(obfuscation_key == dbwrapper_private::GetObfuscation(dbw));
6868
BOOST_CHECK_EQUAL(obfuscate, !is_null_key(obfuscation_key));
6969

7070
// Verify all written values
@@ -89,7 +89,7 @@ BOOST_AUTO_TEST_CASE(dbwrapper_basic_data)
8989
bool res_bool;
9090

9191
// Ensure that we're doing real obfuscation when obfuscate=true
92-
BOOST_CHECK_EQUAL(obfuscate, !is_null_key(dbwrapper_private::GetObfuscateKey(dbw)));
92+
BOOST_CHECK_EQUAL(obfuscate, !is_null_key(dbwrapper_private::GetObfuscation(dbw)));
9393

9494
//Simulate block raw data - "b + block hash"
9595
std::string key_block = "b" + m_rng.rand256().ToString();
@@ -264,7 +264,7 @@ BOOST_AUTO_TEST_CASE(existing_data_no_obfuscate)
264264
BOOST_CHECK_EQUAL(res2.ToString(), in.ToString());
265265

266266
BOOST_CHECK(!odbw.IsEmpty());
267-
BOOST_CHECK(is_null_key(dbwrapper_private::GetObfuscateKey(odbw))); // The key should be an empty string
267+
BOOST_CHECK(is_null_key(dbwrapper_private::GetObfuscation(odbw))); // The key should be an empty string
268268

269269
uint256 in2 = m_rng.rand256();
270270
uint256 res3;
@@ -301,7 +301,7 @@ BOOST_AUTO_TEST_CASE(existing_data_reindex)
301301
// Check that the key/val we wrote with unobfuscated wrapper doesn't exist
302302
uint256 res2;
303303
BOOST_CHECK(!odbw.Read(key, res2));
304-
BOOST_CHECK(!is_null_key(dbwrapper_private::GetObfuscateKey(odbw)));
304+
BOOST_CHECK(!is_null_key(dbwrapper_private::GetObfuscation(odbw)));
305305

306306
uint256 in2 = m_rng.rand256();
307307
uint256 res3;

0 commit comments

Comments
 (0)