From 13f00345c061a8df2fe41ff9d0a6aadfb6137fd8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C5=91rinc?= Date: Tue, 22 Jul 2025 10:13:16 -0700 Subject: [PATCH] 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 --- src/dbwrapper.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/dbwrapper.cpp b/src/dbwrapper.cpp index f43e07bc6bc..8939ff84f8f 100644 --- a/src/dbwrapper.cpp +++ b/src/dbwrapper.cpp @@ -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()}; + 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());