Skip to content

Commit d6576e3

Browse files
committed
Have WalletBatch automatically flush every 1000 updates
Since it now automatically flushes, we don't need to have UpgradeKeyMetadata count and flush separately
1 parent 366fe0b commit d6576e3

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

src/wallet/db.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,9 @@ void BerkeleyBatch::Flush()
614614
if (fReadOnly)
615615
nMinutes = 1;
616616

617-
env->dbenv->txn_checkpoint(nMinutes ? gArgs.GetArg("-dblogsize", DEFAULT_WALLET_DBLOGSIZE) * 1024 : 0, nMinutes, 0);
617+
if (env) { // env is nullptr for dummy databases (i.e. in tests). Don't actually flush if env is nullptr so we don't segfault
618+
env->dbenv->txn_checkpoint(nMinutes ? gArgs.GetArg("-dblogsize", DEFAULT_WALLET_DBLOGSIZE) * 1024 : 0, nMinutes, 0);
619+
}
618620
}
619621

620622
void BerkeleyDatabase::IncrementUpdateCounter()

src/wallet/wallet.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@ void CWallet::UpgradeKeyMetadata()
370370
}
371371

372372
std::unique_ptr<WalletBatch> batch = MakeUnique<WalletBatch>(*database);
373-
size_t cnt = 0;
374373
for (auto& meta_pair : mapKeyMetadata) {
375374
CKeyMetadata& meta = meta_pair.second;
376375
if (!meta.hd_seed_id.IsNull() && !meta.has_key_origin && meta.hdKeypath != "s") { // If the hdKeypath is "s", that's the seed and it doesn't have a key origin
@@ -393,10 +392,6 @@ void CWallet::UpgradeKeyMetadata()
393392
CPubKey pubkey;
394393
if (GetPubKey(meta_pair.first, pubkey)) {
395394
batch->WriteKeyMetadata(meta, pubkey, true);
396-
if (++cnt % 1000 == 0) {
397-
// avoid creating overlarge in-memory batches in case the wallet contains large amounts of keys
398-
batch.reset(new WalletBatch(*database));
399-
}
400395
}
401396
}
402397
}

src/wallet/walletdb.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,11 @@ class CKeyMetadata
143143
};
144144

145145
/** Access to the wallet database.
146-
* This represents a single transaction at the
147-
* database. It will be committed when the object goes out of scope.
148-
* Optionally (on by default) it will flush to disk as well.
146+
* Opens the database and provides read and write access to it. Each read and write is its own transaction.
147+
* Multiple operation transactions can be started using TxnBegin() and committed using TxnCommit()
148+
* Otherwise the transaction will be committed when the object goes out of scope.
149+
* Optionally (on by default) it will flush to disk on close.
150+
* Every 1000 writes will automatically trigger a flush to disk.
149151
*/
150152
class WalletBatch
151153
{
@@ -157,6 +159,9 @@ class WalletBatch
157159
return false;
158160
}
159161
m_database.IncrementUpdateCounter();
162+
if (m_database.nUpdateCounter % 1000 == 0) {
163+
m_batch.Flush();
164+
}
160165
return true;
161166
}
162167

@@ -167,6 +172,9 @@ class WalletBatch
167172
return false;
168173
}
169174
m_database.IncrementUpdateCounter();
175+
if (m_database.nUpdateCounter % 1000 == 0) {
176+
m_batch.Flush();
177+
}
170178
return true;
171179
}
172180

0 commit comments

Comments
 (0)