Merge bitcoin/bitcoin#25383: wallet: don't read db every time that a new 'WalletBatch' is created

c318211ddd48d44dd81dded553afeee3bc41c89e walletdb: fix last client version update (furszy)
bda8ebe608e6572eaaf40cd28dab6954241c9b0d wallet: don't read db every time that a new WalletBatch is created (furszy)

Pull request description:

  Found it while was working on #25297.

  We are performing a db read operation every time that a new `WalletBatch` is created, inside the constructor, just to check if the client version field is inside the db or not.

  As the client version field does not change in the entire db lifecycle, this operation can be done only once: The first time that the db is accessed/opened and the client version value can be cached.

ACKs for top commit:
  achow101:
    ACK c318211ddd48d44dd81dded553afeee3bc41c89e
  w0xlt:
    reACK c318211ddd

Tree-SHA512: 7fb780c656e169e8eb21e7212242494a647f6506d6da2cca828703713d440d29c82bec9e7d2c410f37b49361226ccd80846d3eeb8168383d0c2a11d85d73bee2
This commit is contained in:
MacroFake 2022-06-30 18:38:11 +02:00
commit c892cb7d8d
No known key found for this signature in database
GPG Key ID: CE2B75697E69A548
2 changed files with 4 additions and 12 deletions

View File

@ -315,12 +315,6 @@ BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const bool read_only, b
env = database.env.get(); env = database.env.get();
pdb = database.m_db.get(); pdb = database.m_db.get();
strFile = fs::PathToString(database.m_filename); strFile = fs::PathToString(database.m_filename);
if (!Exists(std::string("version"))) {
bool fTmp = fReadOnly;
fReadOnly = false;
Write(std::string("version"), CLIENT_VERSION);
fReadOnly = fTmp;
}
} }
void BerkeleyDatabase::Open() void BerkeleyDatabase::Open()

View File

@ -883,12 +883,10 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
if (result != DBErrors::LOAD_OK) if (result != DBErrors::LOAD_OK)
return result; return result;
// Last client version to open this wallet, was previously the file version number // Last client version to open this wallet
int last_client = CLIENT_VERSION; int last_client = CLIENT_VERSION;
m_batch->Read(DBKeys::VERSION, last_client); bool has_last_client = m_batch->Read(DBKeys::VERSION, last_client);
pwallet->WalletLogPrintf("Wallet file version = %d, last client version = %d\n", pwallet->GetVersion(), last_client);
int wallet_version = pwallet->GetVersion();
pwallet->WalletLogPrintf("Wallet File Version = %d\n", wallet_version > 0 ? wallet_version : last_client);
pwallet->WalletLogPrintf("Keys: %u plaintext, %u encrypted, %u w/ metadata, %u total. Unknown wallet records: %u\n", pwallet->WalletLogPrintf("Keys: %u plaintext, %u encrypted, %u w/ metadata, %u total. Unknown wallet records: %u\n",
wss.nKeys, wss.nCKeys, wss.nKeyMeta, wss.nKeys + wss.nCKeys, wss.m_unknown_records); wss.nKeys, wss.nCKeys, wss.nKeyMeta, wss.nKeys + wss.nCKeys, wss.m_unknown_records);
@ -909,7 +907,7 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
if (wss.fIsEncrypted && (last_client == 40000 || last_client == 50000)) if (wss.fIsEncrypted && (last_client == 40000 || last_client == 50000))
return DBErrors::NEED_REWRITE; return DBErrors::NEED_REWRITE;
if (last_client < CLIENT_VERSION) // Update if (!has_last_client || last_client != CLIENT_VERSION) // Update
m_batch->Write(DBKeys::VERSION, CLIENT_VERSION); m_batch->Write(DBKeys::VERSION, CLIENT_VERSION);
if (wss.fAnyUnordered) if (wss.fAnyUnordered)