Skip to content

Commit 6c289b1

Browse files
committed
wallet: Remove unused db functions
SOme db functions were for BDB, these are no longer needed.
1 parent a59f267 commit 6c289b1

14 files changed

+19
-150
lines changed

src/wallet/db.h

+2-22
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ class DatabaseBatch
6262
DatabaseBatch(const DatabaseBatch&) = delete;
6363
DatabaseBatch& operator=(const DatabaseBatch&) = delete;
6464

65-
virtual void Flush() = 0;
6665
virtual void Close() = 0;
6766

6867
template <typename K, typename T>
@@ -131,18 +130,14 @@ class WalletDatabase
131130
{
132131
public:
133132
/** Create dummy DB handle */
134-
WalletDatabase() : nUpdateCounter(0) {}
133+
WalletDatabase() = default;
135134
virtual ~WalletDatabase() = default;
136135

137136
/** Open the database if it is not already opened. */
138137
virtual void Open() = 0;
139138

140139
//! Counts the number of active database users to be sure that the database is not closed while someone is using it
141140
std::atomic<int> m_refcount{0};
142-
/** Indicate the a new database user has began using the database. Increments m_refcount */
143-
virtual void AddRef() = 0;
144-
/** Indicate that database user has stopped using the database and that it could be flushed or closed. Decrement m_refcount */
145-
virtual void RemoveRef() = 0;
146141

147142
/** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
148143
*/
@@ -152,33 +147,18 @@ class WalletDatabase
152147
*/
153148
virtual bool Backup(const std::string& strDest) const = 0;
154149

155-
/** Make sure all changes are flushed to database file.
156-
*/
157-
virtual void Flush() = 0;
158150
/** Flush to the database file and close the database.
159151
* Also close the environment if no other databases are open in it.
160152
*/
161153
virtual void Close() = 0;
162-
/* flush the wallet passively (TRY_LOCK)
163-
ideal to be called periodically */
164-
virtual bool PeriodicFlush() = 0;
165-
166-
virtual void IncrementUpdateCounter() = 0;
167-
168-
virtual void ReloadDbEnv() = 0;
169154

170155
/** Return path to main database file for logs and error messages. */
171156
virtual std::string Filename() = 0;
172157

173158
virtual std::string Format() = 0;
174159

175-
std::atomic<unsigned int> nUpdateCounter;
176-
unsigned int nLastSeen{0};
177-
unsigned int nLastFlushed{0};
178-
int64_t nLastWalletUpdate{0};
179-
180160
/** Make a DatabaseBatch connected to this database */
181-
virtual std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) = 0;
161+
virtual std::unique_ptr<DatabaseBatch> MakeBatch() = 0;
182162
};
183163

184164
enum class DatabaseFormat {

src/wallet/interfaces.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ class WalletLoaderImpl : public WalletLoader
587587
m_context.scheduler = &scheduler;
588588
return StartWallets(m_context);
589589
}
590-
void flush() override { return FlushWallets(m_context); }
590+
void flush() override {}
591591
void stop() override { return StopWallets(m_context); }
592592
void setMockTime(int64_t time) override { return SetMockTime(time); }
593593
void schedulerMockForward(std::chrono::seconds delta) override { Assert(m_context.scheduler)->MockForward(delta); }

src/wallet/load.cpp

-7
Original file line numberDiff line numberDiff line change
@@ -162,13 +162,6 @@ void StartWallets(WalletContext& context)
162162
context.scheduler->scheduleEvery([&context] { MaybeResendWalletTxs(context); }, 1min);
163163
}
164164

165-
void FlushWallets(WalletContext& context)
166-
{
167-
for (const std::shared_ptr<CWallet>& pwallet : GetWallets(context)) {
168-
pwallet->Flush();
169-
}
170-
}
171-
172165
void StopWallets(WalletContext& context)
173166
{
174167
for (const std::shared_ptr<CWallet>& pwallet : GetWallets(context)) {

src/wallet/load.h

-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ bool LoadWallets(WalletContext& context);
2828
//! Complete startup of wallets.
2929
void StartWallets(WalletContext& context);
3030

31-
//! Flush all wallets in preparation for shutdown.
32-
void FlushWallets(WalletContext& context);
33-
3431
//! Stop all wallets. Wallets will be flushed first.
3532
void StopWallets(WalletContext& context);
3633

src/wallet/migrate.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ void BerkeleyRODatabase::Open()
699699
}
700700
}
701701

702-
std::unique_ptr<DatabaseBatch> BerkeleyRODatabase::MakeBatch(bool flush_on_close)
702+
std::unique_ptr<DatabaseBatch> BerkeleyRODatabase::MakeBatch()
703703
{
704704
return std::make_unique<BerkeleyROBatch>(*this);
705705
}

src/wallet/migrate.h

+1-17
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ class BerkeleyRODatabase : public WalletDatabase
3535
/** Open the database if it is not already opened. */
3636
void Open() override;
3737

38-
/** Indicate the a new database user has began using the database. Increments m_refcount */
39-
void AddRef() override {}
40-
/** Indicate that database user has stopped using the database and that it could be flushed or closed. Decrement m_refcount */
41-
void RemoveRef() override {}
42-
4338
/** Rewrite the entire database on disk, with the exception of key pszSkip if non-zero
4439
*/
4540
bool Rewrite(const char* pszSkip = nullptr) override { return false; }
@@ -48,28 +43,18 @@ class BerkeleyRODatabase : public WalletDatabase
4843
*/
4944
bool Backup(const std::string& strDest) const override;
5045

51-
/** Make sure all changes are flushed to database file.
52-
*/
53-
void Flush() override {}
5446
/** Flush to the database file and close the database.
5547
* Also close the environment if no other databases are open in it.
5648
*/
5749
void Close() override {}
58-
/* flush the wallet passively (TRY_LOCK)
59-
ideal to be called periodically */
60-
bool PeriodicFlush() override { return false; }
61-
62-
void IncrementUpdateCounter() override {}
63-
64-
void ReloadDbEnv() override {}
6550

6651
/** Return path to main database file for logs and error messages. */
6752
std::string Filename() override { return fs::PathToString(m_filepath); }
6853

6954
std::string Format() override { return "bdb_ro"; }
7055

7156
/** Make a DatabaseBatch connected to this database */
72-
std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override;
57+
std::unique_ptr<DatabaseBatch> MakeBatch() override;
7358
};
7459

7560
class BerkeleyROCursor : public DatabaseCursor
@@ -107,7 +92,6 @@ class BerkeleyROBatch : public DatabaseBatch
10792
BerkeleyROBatch(const BerkeleyROBatch&) = delete;
10893
BerkeleyROBatch& operator=(const BerkeleyROBatch&) = delete;
10994

110-
void Flush() override {}
11195
void Close() override {}
11296

11397
std::unique_ptr<DatabaseCursor> GetNewCursor() override { return std::make_unique<BerkeleyROCursor>(m_database); }

src/wallet/sqlite.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ int SQliteExecHandler::Exec(SQLiteDatabase& database, const std::string& stateme
390390
return sqlite3_exec(database.m_db, statement.data(), nullptr, nullptr, nullptr);
391391
}
392392

393-
std::unique_ptr<DatabaseBatch> SQLiteDatabase::MakeBatch(bool flush_on_close)
393+
std::unique_ptr<DatabaseBatch> SQLiteDatabase::MakeBatch()
394394
{
395395
// We ignore flush_on_close because we don't do manual flushing for SQLite
396396
return std::make_unique<SQLiteBatch>(*this);

src/wallet/sqlite.h

+1-22
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,6 @@ class SQLiteBatch : public DatabaseBatch
8585

8686
void SetExecHandler(std::unique_ptr<SQliteExecHandler>&& handler) { m_exec_handler = std::move(handler); }
8787

88-
/* No-op. See comment on SQLiteDatabase::Flush */
89-
void Flush() override {}
90-
9188
void Close() override;
9289

9390
std::unique_ptr<DatabaseCursor> GetNewCursor() override;
@@ -140,36 +137,18 @@ class SQLiteDatabase : public WalletDatabase
140137
/** Close the database */
141138
void Close() override;
142139

143-
/* These functions are unused */
144-
void AddRef() override { assert(false); }
145-
void RemoveRef() override { assert(false); }
146-
147140
/** Rewrite the entire database on disk */
148141
bool Rewrite(const char* skip = nullptr) override;
149142

150143
/** Back up the entire database to a file.
151144
*/
152145
bool Backup(const std::string& dest) const override;
153146

154-
/** No-ops
155-
*
156-
* SQLite always flushes everything to the database file after each transaction
157-
* (each Read/Write/Erase that we do is its own transaction unless we called
158-
* TxnBegin) so there is no need to have Flush or Periodic Flush.
159-
*
160-
* There is no DB env to reload, so ReloadDbEnv has nothing to do
161-
*/
162-
void Flush() override {}
163-
bool PeriodicFlush() override { return false; }
164-
void ReloadDbEnv() override {}
165-
166-
void IncrementUpdateCounter() override { ++nUpdateCounter; }
167-
168147
std::string Filename() override { return m_file_path; }
169148
std::string Format() override { return "sqlite"; }
170149

171150
/** Make a SQLiteBatch connected to this database */
172-
std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override;
151+
std::unique_ptr<DatabaseBatch> MakeBatch() override;
173152

174153
/** Return true if there is an on-going txn in this connection */
175154
bool HasActiveTxn();

src/wallet/test/util.h

+1-8
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ class MockableBatch : public DatabaseBatch
7575
explicit MockableBatch(MockableData& records, bool pass) : m_records(records), m_pass(pass) {}
7676
~MockableBatch() = default;
7777

78-
void Flush() override {}
7978
void Close() override {}
8079

8180
std::unique_ptr<DatabaseCursor> GetNewCursor() override
@@ -103,20 +102,14 @@ class MockableDatabase : public WalletDatabase
103102
~MockableDatabase() = default;
104103

105104
void Open() override {}
106-
void AddRef() override {}
107-
void RemoveRef() override {}
108105

109106
bool Rewrite(const char* pszSkip=nullptr) override { return m_pass; }
110107
bool Backup(const std::string& strDest) const override { return m_pass; }
111-
void Flush() override {}
112108
void Close() override {}
113-
bool PeriodicFlush() override { return m_pass; }
114-
void IncrementUpdateCounter() override {}
115-
void ReloadDbEnv() override {}
116109

117110
std::string Filename() override { return "mockable"; }
118111
std::string Format() override { return "mock"; }
119-
std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override { return std::make_unique<MockableBatch>(m_records, m_pass); }
112+
std::unique_ptr<DatabaseBatch> MakeBatch() override { return std::make_unique<MockableBatch>(m_records, m_pass); }
120113
};
121114

122115
std::unique_ptr<WalletDatabase> CreateMockableWalletDatabase(MockableData records = {});

src/wallet/test/walletload_tests.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ BOOST_FIXTURE_TEST_CASE(wallet_load_descriptors, TestingSetup)
4242
std::unique_ptr<WalletDatabase> database = CreateMockableWalletDatabase();
4343
{
4444
// Write unknown active descriptor
45-
WalletBatch batch(*database, false);
45+
WalletBatch batch(*database);
4646
std::string unknown_desc = "trx(tpubD6NzVbkrYhZ4Y4S7m6Y5s9GD8FqEMBy56AGphZXuagajudVZEnYyBahZMgHNCTJc2at82YX6s8JiL1Lohu5A3v1Ur76qguNH4QVQ7qYrBQx/86'/1'/0'/0/*)#8pn8tzdt";
4747
WalletDescriptor wallet_descriptor(std::make_shared<DummyDescriptor>(unknown_desc), 0, 0, 0, 0);
4848
BOOST_CHECK(batch.WriteDescriptor(uint256(), wallet_descriptor));
@@ -69,7 +69,7 @@ BOOST_FIXTURE_TEST_CASE(wallet_load_descriptors, TestingSetup)
6969

7070
{
7171
// Write valid descriptor with invalid ID
72-
WalletBatch batch(*database, false);
72+
WalletBatch batch(*database);
7373
std::string desc = "wpkh([d34db33f/84h/0h/0h]xpub6DJ2dNUysrn5Vt36jH2KLBT2i1auw1tTSSomg8PhqNiUtx8QX2SvC9nrHu81fT41fvDUnhMjEzQgXnQjKEu3oaqMSzhSrHMxyyoEAmUHQbY/0/*)#cjjspncu";
7474
WalletDescriptor wallet_descriptor(std::make_shared<DummyDescriptor>(desc), 0, 0, 0, 0);
7575
BOOST_CHECK(batch.WriteDescriptor(uint256::ONE, wallet_descriptor));

src/wallet/wallet.cpp

+6-20
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,7 @@ static std::set<std::string> g_unloading_wallet_set GUARDED_BY(g_wallet_release_
235235
static void FlushAndDeleteWallet(CWallet* wallet)
236236
{
237237
const std::string name = wallet->GetName();
238-
wallet->WalletLogPrintf("Releasing wallet %s..\n", name);
239-
wallet->Flush();
238+
wallet->WalletLogPrintf("Releasing wallet\n");
240239
delete wallet;
241240
// Wallet is now released, notify WaitForDeleteWallet, if any.
242241
{
@@ -682,11 +681,6 @@ bool CWallet::HasWalletSpend(const CTransactionRef& tx) const
682681
return false;
683682
}
684683

685-
void CWallet::Flush()
686-
{
687-
GetDatabase().Flush();
688-
}
689-
690684
void CWallet::Close()
691685
{
692686
GetDatabase().Close();
@@ -859,15 +853,9 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
859853
}
860854
Lock();
861855

862-
// Need to completely rewrite the wallet file; if we don't, bdb might keep
856+
// Need to completely rewrite the wallet file; if we don't, the database might keep
863857
// bits of the unencrypted private key in slack space in the database file.
864858
GetDatabase().Rewrite();
865-
866-
// BDB seems to have a bad habit of writing old data into
867-
// slack space in .dat files; that is bad if the old data is
868-
// unencrypted private keys. So:
869-
GetDatabase().ReloadDbEnv();
870-
871859
}
872860
NotifyStatusChanged(this);
873861

@@ -1016,11 +1004,11 @@ bool CWallet::IsSpentKey(const CScript& scriptPubKey) const
10161004
return false;
10171005
}
10181006

1019-
CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const TxState& state, const UpdateWalletTxFn& update_wtx, bool fFlushOnClose, bool rescanning_old_block)
1007+
CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const TxState& state, const UpdateWalletTxFn& update_wtx, bool rescanning_old_block)
10201008
{
10211009
LOCK(cs_wallet);
10221010

1023-
WalletBatch batch(GetDatabase(), fFlushOnClose);
1011+
WalletBatch batch(GetDatabase());
10241012

10251013
uint256 hash = tx->GetHash();
10261014

@@ -1230,7 +1218,7 @@ bool CWallet::AddToWalletIfInvolvingMe(const CTransactionRef& ptx, const SyncTxS
12301218
// Block disconnection override an abandoned tx as unconfirmed
12311219
// which means user may have to call abandontransaction again
12321220
TxState tx_state = std::visit([](auto&& s) -> TxState { return s; }, state);
1233-
CWalletTx* wtx = AddToWallet(MakeTransactionRef(tx), tx_state, /*update_wtx=*/nullptr, /*fFlushOnClose=*/false, rescanning_old_block);
1221+
CWalletTx* wtx = AddToWallet(MakeTransactionRef(tx), tx_state, /*update_wtx=*/nullptr, rescanning_old_block);
12341222
if (!wtx) {
12351223
// Can only be nullptr if there was a db write error (missing db, read-only db or a db engine internal writing error).
12361224
// As we only store arriving transaction in this process, and we don't want an inconsistent state, let's throw an error.
@@ -1325,8 +1313,7 @@ void CWallet::MarkConflicted(const uint256& hashBlock, int conflicting_height, c
13251313
}
13261314

13271315
void CWallet::RecursiveUpdateTxState(const uint256& tx_hash, const TryUpdatingStateFn& try_updating_state) {
1328-
// Do not flush the wallet here for performance reasons
1329-
WalletBatch batch(GetDatabase(), false);
1316+
WalletBatch batch(GetDatabase());
13301317
RecursiveUpdateTxState(&batch, tx_hash, try_updating_state);
13311318
}
13321319

@@ -3190,7 +3177,6 @@ bool CWallet::AttachChain(const std::shared_ptr<CWallet>& walletInstance, interf
31903177
}
31913178
walletInstance->m_attaching_chain = false;
31923179
walletInstance->chainStateFlushed(ChainstateRole::NORMAL, chain.getTipLocator());
3193-
walletInstance->GetDatabase().IncrementUpdateCounter();
31943180
}
31953181
walletInstance->m_attaching_chain = false;
31963182

src/wallet/wallet.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
603603
* Add the transaction to the wallet, wrapping it up inside a CWalletTx
604604
* @return the recently added wtx pointer or nullptr if there was a db write error.
605605
*/
606-
CWalletTx* AddToWallet(CTransactionRef tx, const TxState& state, const UpdateWalletTxFn& update_wtx=nullptr, bool fFlushOnClose=true, bool rescanning_old_block = false);
606+
CWalletTx* AddToWallet(CTransactionRef tx, const TxState& state, const UpdateWalletTxFn& update_wtx=nullptr, bool rescanning_old_block = false);
607607
bool LoadToWallet(const uint256& hash, const UpdateWalletTxFn& fill_wtx) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
608608
void transactionAddedToMempool(const CTransactionRef& tx) override;
609609
void blockConnected(ChainstateRole role, const interfaces::BlockInfo& block) override;
@@ -818,9 +818,6 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
818818
//! Check if a given transaction has any of its outputs spent by another transaction in the wallet
819819
bool HasWalletSpend(const CTransactionRef& tx) const EXCLUSIVE_LOCKS_REQUIRED(cs_wallet);
820820

821-
//! Flush wallet (bitdb flush)
822-
void Flush();
823-
824821
//! Close wallet database
825822
void Close();
826823

src/wallet/walletdb.cpp

-27
Original file line numberDiff line numberDiff line change
@@ -1256,33 +1256,6 @@ bool RunWithinTxn(WalletDatabase& database, std::string_view process_desc, const
12561256
return RunWithinTxn(batch, process_desc, func);
12571257
}
12581258

1259-
void MaybeCompactWalletDB(WalletContext& context)
1260-
{
1261-
static std::atomic<bool> fOneThread(false);
1262-
if (fOneThread.exchange(true)) {
1263-
return;
1264-
}
1265-
1266-
for (const std::shared_ptr<CWallet>& pwallet : GetWallets(context)) {
1267-
WalletDatabase& dbh = pwallet->GetDatabase();
1268-
1269-
unsigned int nUpdateCounter = dbh.nUpdateCounter;
1270-
1271-
if (dbh.nLastSeen != nUpdateCounter) {
1272-
dbh.nLastSeen = nUpdateCounter;
1273-
dbh.nLastWalletUpdate = GetTime();
1274-
}
1275-
1276-
if (dbh.nLastFlushed != nUpdateCounter && GetTime() - dbh.nLastWalletUpdate >= 2) {
1277-
if (dbh.PeriodicFlush()) {
1278-
dbh.nLastFlushed = nUpdateCounter;
1279-
}
1280-
}
1281-
}
1282-
1283-
fOneThread = false;
1284-
}
1285-
12861259
bool WalletBatch::WriteAddressPreviouslySpent(const CTxDestination& dest, bool previously_spent)
12871260
{
12881261
auto key{std::make_pair(DBKeys::DESTDATA, std::make_pair(EncodeDestination(dest), std::string("used")))};

0 commit comments

Comments
 (0)