mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-04-29 14:59:39 -04:00
refactor: prepare DBWrapper
for obfuscation key change
Since `CDBWrapper::Read` still supports vector only, we can initialize `m_obfuscation` directly instead of using a separate helper. `CreateObfuscation` was also inlined, replaced `key_exists` with `key_missing`, and simplified the `if` condition that writes a new obfuscation key.
This commit is contained in:
parent
9712481ae7
commit
13cc039f20
2 changed files with 19 additions and 27 deletions
|
@ -248,24 +248,23 @@ CDBWrapper::CDBWrapper(const DBParams& params)
|
|||
LogPrintf("Finished database compaction of %s\n", fs::PathToString(params.path));
|
||||
}
|
||||
|
||||
// The base-case obfuscation key, which is a noop.
|
||||
m_obfuscation = std::vector<unsigned char>(OBFUSCATION_SIZE_BYTES, '\000');
|
||||
{
|
||||
m_obfuscation = std::vector<uint8_t>(OBFUSCATION_SIZE_BYTES, '\000'); // Needed for unobfuscated Read() below
|
||||
const bool key_missing{!Read(OBFUSCATION_KEY, m_obfuscation)};
|
||||
if (key_missing && params.obfuscate && IsEmpty()) {
|
||||
// Initialize non-degenerate obfuscation if it won't upset existing, non-obfuscated data.
|
||||
std::vector<uint8_t> new_key(OBFUSCATION_SIZE_BYTES);
|
||||
GetRandBytes(new_key);
|
||||
|
||||
bool key_exists = Read(OBFUSCATION_KEY, m_obfuscation);
|
||||
// Write `new_key` so we don't obfuscate the key with itself
|
||||
Write(OBFUSCATION_KEY, new_key);
|
||||
m_obfuscation = std::move(new_key);
|
||||
|
||||
if (!key_exists && params.obfuscate && IsEmpty()) {
|
||||
// Initialize non-degenerate obfuscation if it won't upset
|
||||
// existing, non-obfuscated data.
|
||||
std::vector<unsigned char> new_key = CreateObfuscation();
|
||||
LogPrintf("Wrote new obfuscate key for %s: %s\n", fs::PathToString(params.path), HexStr(m_obfuscation));
|
||||
}
|
||||
|
||||
// Write `new_key` so we don't obfuscate the key with itself
|
||||
Write(OBFUSCATION_KEY, new_key);
|
||||
m_obfuscation = new_key;
|
||||
|
||||
LogPrintf("Wrote new obfuscate 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(m_obfuscation));
|
||||
}
|
||||
|
||||
LogPrintf("Using obfuscation key for %s: %s\n", fs::PathToString(params.path), HexStr(m_obfuscation));
|
||||
}
|
||||
|
||||
CDBWrapper::~CDBWrapper()
|
||||
|
@ -310,12 +309,6 @@ size_t CDBWrapper::DynamicMemoryUsage() const
|
|||
return parsed.value();
|
||||
}
|
||||
|
||||
// Prefixed with null character to avoid collisions with other keys
|
||||
//
|
||||
// We must use a string constructor which specifies length so that we copy
|
||||
// past the null-terminator.
|
||||
const std::string CDBWrapper::OBFUSCATION_KEY("\000obfuscate_key", 14);
|
||||
|
||||
const unsigned int CDBWrapper::OBFUSCATION_SIZE_BYTES = 8;
|
||||
|
||||
/**
|
||||
|
@ -412,9 +405,6 @@ void CDBIterator::Next() { m_impl_iter->iter->Next(); }
|
|||
|
||||
namespace dbwrapper_private {
|
||||
|
||||
const std::vector<unsigned char>& GetObfuscation(const CDBWrapper &w)
|
||||
{
|
||||
return w.m_obfuscation;
|
||||
}
|
||||
const std::vector<unsigned char>& GetObfuscation(const CDBWrapper &w) { return w.m_obfuscation; }
|
||||
|
||||
} // namespace dbwrapper_private
|
||||
|
|
|
@ -190,9 +190,6 @@ private:
|
|||
//! a key used for optional XOR-obfuscation of the database
|
||||
std::vector<unsigned char> m_obfuscation;
|
||||
|
||||
//! the key under which the obfuscation key is stored
|
||||
static const std::string OBFUSCATION_KEY;
|
||||
|
||||
//! the length of the obfuscate key in number of bytes
|
||||
static const unsigned int OBFUSCATION_SIZE_BYTES;
|
||||
|
||||
|
@ -210,6 +207,11 @@ private:
|
|||
auto& DBContext() const LIFETIMEBOUND { return *Assert(m_db_context); }
|
||||
|
||||
public:
|
||||
// Prefixed with null character to avoid collisions with other keys
|
||||
//
|
||||
// We must use a string constructor which specifies length so that we copy past the null-terminator.
|
||||
inline static const std::string OBFUSCATION_KEY{"\000obfuscate_key", 14};
|
||||
|
||||
CDBWrapper(const DBParams& params);
|
||||
~CDBWrapper();
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue