refactor: write Obfuscation object when new key is generated in dbwrapper

See:
* https://github.com/bitcoin/bitcoin/pull/31144#discussion_r2215720251
* https://github.com/bitcoin/bitcoin/pull/31144#discussion_r2223539466

Co-authored-by: maflcko <6399679+maflcko@users.noreply.github.com>
Co-authored-by: Ryan Ofsky <ryan@ofsky.org>
This commit is contained in:
Lőrinc
2025-07-22 10:13:16 -07:00
parent e5b1b7c557
commit 13f00345c0

View File

@@ -249,11 +249,12 @@ CDBWrapper::CDBWrapper(const DBParams& params)
LogPrintf("Finished database compaction of %s\n", fs::PathToString(params.path));
}
assert(!m_obfuscation); // Needed for unobfuscated Read()/Write() below
if (!Read(OBFUSCATION_KEY, m_obfuscation) && params.obfuscate && IsEmpty()) {
// Generate, write and read back the new obfuscation key, making sure we don't obfuscate the key itself
Write(OBFUSCATION_KEY, FastRandomContext{}.randbytes(Obfuscation::KEY_SIZE));
Read(OBFUSCATION_KEY, m_obfuscation);
// Generate and write the new obfuscation key.
const Obfuscation obfuscation{FastRandomContext{}.randbytes<Obfuscation::KEY_SIZE>()};
assert(!m_obfuscation); // Make sure the key is written without obfuscation.
Write(OBFUSCATION_KEY, obfuscation);
m_obfuscation = obfuscation;
LogInfo("Wrote new obfuscation key for %s: %s", fs::PathToString(params.path), m_obfuscation.HexKey());
}
LogInfo("Using obfuscation key for %s: %s", fs::PathToString(params.path), m_obfuscation.HexKey());