Skip to content

Commit c892cb7

Browse files
author
MacroFake
committed
Merge bitcoin#25383: wallet: don't read db every time that a new 'WalletBatch' is created
c318211 walletdb: fix last client version update (furszy) bda8ebe wallet: don't read db every time that a new WalletBatch is created (furszy) Pull request description: Found it while was working on bitcoin#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 c318211 w0xlt: reACK bitcoin@c318211 Tree-SHA512: 7fb780c656e169e8eb21e7212242494a647f6506d6da2cca828703713d440d29c82bec9e7d2c410f37b49361226ccd80846d3eeb8168383d0c2a11d85d73bee2
2 parents 5d68d68 + c318211 commit c892cb7

File tree

2 files changed

+4
-12
lines changed

2 files changed

+4
-12
lines changed

src/wallet/bdb.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,12 +315,6 @@ BerkeleyBatch::BerkeleyBatch(BerkeleyDatabase& database, const bool read_only, b
315315
env = database.env.get();
316316
pdb = database.m_db.get();
317317
strFile = fs::PathToString(database.m_filename);
318-
if (!Exists(std::string("version"))) {
319-
bool fTmp = fReadOnly;
320-
fReadOnly = false;
321-
Write(std::string("version"), CLIENT_VERSION);
322-
fReadOnly = fTmp;
323-
}
324318
}
325319

326320
void BerkeleyDatabase::Open()

src/wallet/walletdb.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -883,12 +883,10 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
883883
if (result != DBErrors::LOAD_OK)
884884
return result;
885885

886-
// Last client version to open this wallet, was previously the file version number
886+
// Last client version to open this wallet
887887
int last_client = CLIENT_VERSION;
888-
m_batch->Read(DBKeys::VERSION, last_client);
889-
890-
int wallet_version = pwallet->GetVersion();
891-
pwallet->WalletLogPrintf("Wallet File Version = %d\n", wallet_version > 0 ? wallet_version : last_client);
888+
bool has_last_client = m_batch->Read(DBKeys::VERSION, last_client);
889+
pwallet->WalletLogPrintf("Wallet file version = %d, last client version = %d\n", pwallet->GetVersion(), last_client);
892890

893891
pwallet->WalletLogPrintf("Keys: %u plaintext, %u encrypted, %u w/ metadata, %u total. Unknown wallet records: %u\n",
894892
wss.nKeys, wss.nCKeys, wss.nKeyMeta, wss.nKeys + wss.nCKeys, wss.m_unknown_records);
@@ -909,7 +907,7 @@ DBErrors WalletBatch::LoadWallet(CWallet* pwallet)
909907
if (wss.fIsEncrypted && (last_client == 40000 || last_client == 50000))
910908
return DBErrors::NEED_REWRITE;
911909

912-
if (last_client < CLIENT_VERSION) // Update
910+
if (!has_last_client || last_client != CLIENT_VERSION) // Update
913911
m_batch->Write(DBKeys::VERSION, CLIENT_VERSION);
914912

915913
if (wss.fAnyUnordered)

0 commit comments

Comments
 (0)