Skip to content

Commit f4bb9c6

Browse files
committed
wallet: Remove unused db functions
SOme db functions were for BDB, these are no longer needed.
1 parent 28ab942 commit f4bb9c6

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>
@@ -130,18 +129,14 @@ class WalletDatabase
130129
{
131130
public:
132131
/** Create dummy DB handle */
133-
WalletDatabase() : nUpdateCounter(0) {}
132+
WalletDatabase() = default;
134133
virtual ~WalletDatabase() = default;
135134

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

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

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

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

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

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

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

183163
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
@@ -152,13 +152,6 @@ void StartWallets(WalletContext& context)
152152
context.scheduler->scheduleEvery([&context] { MaybeResendWalletTxs(context); }, 1min);
153153
}
154154

155-
void FlushWallets(WalletContext& context)
156-
{
157-
for (const std::shared_ptr<CWallet>& pwallet : GetWallets(context)) {
158-
pwallet->Flush();
159-
}
160-
}
161-
162155
void StopWallets(WalletContext& context)
163156
{
164157
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;
@@ -139,36 +136,18 @@ class SQLiteDatabase : public WalletDatabase
139136
/** Close the database */
140137
void Close() override;
141138

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

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

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

170149
/** Make a SQLiteBatch connected to this database */
171-
std::unique_ptr<DatabaseBatch> MakeBatch(bool flush_on_close = true) override;
150+
std::unique_ptr<DatabaseBatch> MakeBatch() override;
172151

173152
/** Return true if there is an on-going txn in this connection */
174153
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
@@ -102,20 +101,14 @@ class MockableDatabase : public WalletDatabase
102101
~MockableDatabase() = default;
103102

104103
void Open() override {}
105-
void AddRef() override {}
106-
void RemoveRef() override {}
107104

108105
bool Rewrite(const char* pszSkip=nullptr) override { return m_pass; }
109106
bool Backup(const std::string& strDest) const override { return m_pass; }
110-
void Flush() override {}
111107
void Close() override {}
112-
bool PeriodicFlush() override { return m_pass; }
113-
void IncrementUpdateCounter() override {}
114-
void ReloadDbEnv() override {}
115108

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

121114
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

@@ -3179,7 +3166,6 @@ bool CWallet::AttachChain(const std::shared_ptr<CWallet>& walletInstance, interf
31793166
}
31803167
walletInstance->m_attaching_chain = false;
31813168
walletInstance->chainStateFlushed(ChainstateRole::NORMAL, chain.getTipLocator());
3182-
walletInstance->GetDatabase().IncrementUpdateCounter();
31833169
}
31843170
walletInstance->m_attaching_chain = false;
31853171

src/wallet/wallet.h

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

817-
//! Flush wallet (bitdb flush)
818-
void Flush();
819-
820817
//! Close wallet database
821818
void Close();
822819

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)