Skip to content

Commit 9b74461

Browse files
committed
refactor: Assert before dereference in CWallet::GetDatabase
1 parent 021feb3 commit 9b74461

File tree

2 files changed

+35
-31
lines changed

2 files changed

+35
-31
lines changed

src/wallet/wallet.cpp

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase,
419419
return false;
420420
if (!crypter.Encrypt(_vMasterKey, pMasterKey.second.vchCryptedKey))
421421
return false;
422-
WalletBatch(*database).WriteMasterKey(pMasterKey.first, pMasterKey.second);
422+
WalletBatch(GetDatabase()).WriteMasterKey(pMasterKey.first, pMasterKey.second);
423423
if (fWasLocked)
424424
Lock();
425425
return true;
@@ -432,7 +432,7 @@ bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase,
432432

433433
void CWallet::chainStateFlushed(const CBlockLocator& loc)
434434
{
435-
WalletBatch batch(*database);
435+
WalletBatch batch(GetDatabase());
436436
batch.WriteBestBlock(loc);
437437
}
438438

@@ -452,7 +452,7 @@ void CWallet::SetMinVersion(enum WalletFeature nVersion, WalletBatch* batch_in,
452452
nWalletMaxVersion = nVersion;
453453

454454
{
455-
WalletBatch* batch = batch_in ? batch_in : new WalletBatch(*database);
455+
WalletBatch* batch = batch_in ? batch_in : new WalletBatch(GetDatabase());
456456
if (nWalletVersion > 40000)
457457
batch->WriteMinVersion(nWalletVersion);
458458
if (!batch_in)
@@ -504,12 +504,12 @@ bool CWallet::HasWalletSpend(const uint256& txid) const
504504

505505
void CWallet::Flush()
506506
{
507-
database->Flush();
507+
GetDatabase().Flush();
508508
}
509509

510510
void CWallet::Close()
511511
{
512-
database->Close();
512+
GetDatabase().Close();
513513
}
514514

515515
void CWallet::SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator> range)
@@ -635,7 +635,7 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
635635
{
636636
LOCK(cs_wallet);
637637
mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
638-
WalletBatch* encrypted_batch = new WalletBatch(*database);
638+
WalletBatch* encrypted_batch = new WalletBatch(GetDatabase());
639639
if (!encrypted_batch->TxnBegin()) {
640640
delete encrypted_batch;
641641
encrypted_batch = nullptr;
@@ -687,12 +687,12 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
687687

688688
// Need to completely rewrite the wallet file; if we don't, bdb might keep
689689
// bits of the unencrypted private key in slack space in the database file.
690-
database->Rewrite();
690+
GetDatabase().Rewrite();
691691

692692
// BDB seems to have a bad habit of writing old data into
693693
// slack space in .dat files; that is bad if the old data is
694694
// unencrypted private keys. So:
695-
database->ReloadDbEnv();
695+
GetDatabase().ReloadDbEnv();
696696

697697
}
698698
NotifyStatusChanged(this);
@@ -703,7 +703,7 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
703703
DBErrors CWallet::ReorderTransactions()
704704
{
705705
LOCK(cs_wallet);
706-
WalletBatch batch(*database);
706+
WalletBatch batch(GetDatabase());
707707

708708
// Old wallets didn't have any defined order for transactions
709709
// Probably a bad idea to change the output of this
@@ -764,7 +764,7 @@ int64_t CWallet::IncOrderPosNext(WalletBatch* batch)
764764
if (batch) {
765765
batch->WriteOrderPosNext(nOrderPosNext);
766766
} else {
767-
WalletBatch(*database).WriteOrderPosNext(nOrderPosNext);
767+
WalletBatch(GetDatabase()).WriteOrderPosNext(nOrderPosNext);
768768
}
769769
return nRet;
770770
}
@@ -794,7 +794,7 @@ bool CWallet::MarkReplaced(const uint256& originalHash, const uint256& newHash)
794794

795795
wtx.mapValue["replaced_by_txid"] = newHash.ToString();
796796

797-
WalletBatch batch(*database);
797+
WalletBatch batch(GetDatabase());
798798

799799
bool success = true;
800800
if (!batch.WriteTx(wtx)) {
@@ -866,7 +866,7 @@ CWalletTx* CWallet::AddToWallet(CTransactionRef tx, const CWalletTx::Confirmatio
866866
{
867867
LOCK(cs_wallet);
868868

869-
WalletBatch batch(*database, fFlushOnClose);
869+
WalletBatch batch(GetDatabase(), fFlushOnClose);
870870

871871
uint256 hash = tx->GetHash();
872872

@@ -1065,7 +1065,7 @@ bool CWallet::AbandonTransaction(const uint256& hashTx)
10651065
{
10661066
LOCK(cs_wallet);
10671067

1068-
WalletBatch batch(*database);
1068+
WalletBatch batch(GetDatabase());
10691069

10701070
std::set<uint256> todo;
10711071
std::set<uint256> done;
@@ -1128,7 +1128,7 @@ void CWallet::MarkConflicted(const uint256& hashBlock, int conflicting_height, c
11281128
return;
11291129

11301130
// Do not flush the wallet here for performance reasons
1131-
WalletBatch batch(*database, false);
1131+
WalletBatch batch(GetDatabase(), false);
11321132

11331133
std::set<uint256> todo;
11341134
std::set<uint256> done;
@@ -1466,13 +1466,13 @@ void CWallet::SetWalletFlag(uint64_t flags)
14661466
{
14671467
LOCK(cs_wallet);
14681468
m_wallet_flags |= flags;
1469-
if (!WalletBatch(*database).WriteWalletFlags(m_wallet_flags))
1469+
if (!WalletBatch(GetDatabase()).WriteWalletFlags(m_wallet_flags))
14701470
throw std::runtime_error(std::string(__func__) + ": writing wallet flags failed");
14711471
}
14721472

14731473
void CWallet::UnsetWalletFlag(uint64_t flag)
14741474
{
1475-
WalletBatch batch(*database);
1475+
WalletBatch batch(GetDatabase());
14761476
UnsetWalletFlagWithDB(batch, flag);
14771477
}
14781478

@@ -1511,7 +1511,7 @@ bool CWallet::AddWalletFlags(uint64_t flags)
15111511
LOCK(cs_wallet);
15121512
// We should never be writing unknown non-tolerable wallet flags
15131513
assert(((flags & KNOWN_WALLET_FLAGS) >> 32) == (flags >> 32));
1514-
if (!WalletBatch(*database).WriteWalletFlags(flags)) {
1514+
if (!WalletBatch(GetDatabase()).WriteWalletFlags(flags)) {
15151515
throw std::runtime_error(std::string(__func__) + ": writing wallet flags failed");
15161516
}
15171517

@@ -1602,7 +1602,7 @@ bool CWallet::ImportScriptPubKeys(const std::string& label, const std::set<CScri
16021602
return false;
16031603
}
16041604
if (apply_label) {
1605-
WalletBatch batch(*database);
1605+
WalletBatch batch(GetDatabase());
16061606
for (const CScript& script : script_pub_keys) {
16071607
CTxDestination dest;
16081608
ExtractDestination(script, dest);
@@ -3193,10 +3193,10 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
31933193
LOCK(cs_wallet);
31943194

31953195
fFirstRunRet = false;
3196-
DBErrors nLoadWalletRet = WalletBatch(*database).LoadWallet(this);
3196+
DBErrors nLoadWalletRet = WalletBatch(GetDatabase()).LoadWallet(this);
31973197
if (nLoadWalletRet == DBErrors::NEED_REWRITE)
31983198
{
3199-
if (database->Rewrite("\x04pool"))
3199+
if (GetDatabase().Rewrite("\x04pool"))
32003200
{
32013201
for (const auto& spk_man_pair : m_spk_managers) {
32023202
spk_man_pair.second->RewriteDB();
@@ -3220,7 +3220,7 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
32203220
DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut)
32213221
{
32223222
AssertLockHeld(cs_wallet);
3223-
DBErrors nZapSelectTxRet = WalletBatch(*database).ZapSelectTx(vHashIn, vHashOut);
3223+
DBErrors nZapSelectTxRet = WalletBatch(GetDatabase()).ZapSelectTx(vHashIn, vHashOut);
32243224
for (const uint256& hash : vHashOut) {
32253225
const auto& it = mapWallet.find(hash);
32263226
wtxOrdered.erase(it->second.m_it_wtxOrdered);
@@ -3232,7 +3232,7 @@ DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256
32323232

32333233
if (nZapSelectTxRet == DBErrors::NEED_REWRITE)
32343234
{
3235-
if (database->Rewrite("\x04pool"))
3235+
if (GetDatabase().Rewrite("\x04pool"))
32363236
{
32373237
for (const auto& spk_man_pair : m_spk_managers) {
32383238
spk_man_pair.second->RewriteDB();
@@ -3270,14 +3270,14 @@ bool CWallet::SetAddressBookWithDB(WalletBatch& batch, const CTxDestination& add
32703270

32713271
bool CWallet::SetAddressBook(const CTxDestination& address, const std::string& strName, const std::string& strPurpose)
32723272
{
3273-
WalletBatch batch(*database);
3273+
WalletBatch batch(GetDatabase());
32743274
return SetAddressBookWithDB(batch, address, strName, strPurpose);
32753275
}
32763276

32773277
bool CWallet::DelAddressBook(const CTxDestination& address)
32783278
{
32793279
bool is_mine;
3280-
WalletBatch batch(*database);
3280+
WalletBatch batch(GetDatabase());
32813281
{
32823282
LOCK(cs_wallet);
32833283
// If we want to delete receiving addresses, we need to take care that DestData "used" (and possibly newer DestData) gets preserved (and the "deleted" address transformed into a change entry instead of actually being deleted)
@@ -4024,7 +4024,7 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::st
40244024
int rescan_height = 0;
40254025
if (!gArgs.GetBoolArg("-rescan", false))
40264026
{
4027-
WalletBatch batch(*walletInstance->database);
4027+
WalletBatch batch(walletInstance->GetDatabase());
40284028
CBlockLocator locator;
40294029
if (batch.ReadBestBlock(locator)) {
40304030
if (const Optional<int> fork_height = chain.findLocatorFork(locator)) {
@@ -4087,7 +4087,7 @@ std::shared_ptr<CWallet> CWallet::Create(interfaces::Chain& chain, const std::st
40874087
}
40884088
}
40894089
walletInstance->chainStateFlushed(chain.getTipLocator());
4090-
walletInstance->database->IncrementUpdateCounter();
4090+
walletInstance->GetDatabase().IncrementUpdateCounter();
40914091
}
40924092

40934093
{
@@ -4168,7 +4168,7 @@ void CWallet::postInitProcess()
41684168

41694169
bool CWallet::BackupWallet(const std::string& strDest) const
41704170
{
4171-
return database->Backup(strDest);
4171+
return GetDatabase().Backup(strDest);
41724172
}
41734173

41744174
CKeyPool::CKeyPool()
@@ -4471,7 +4471,7 @@ void CWallet::SetupDescriptorScriptPubKeyMans()
44714471

44724472
void CWallet::AddActiveScriptPubKeyMan(uint256 id, OutputType type, bool internal)
44734473
{
4474-
WalletBatch batch(*database);
4474+
WalletBatch batch(GetDatabase());
44754475
if (!batch.WriteActiveScriptPubKeyMan(static_cast<uint8_t>(type), id, internal)) {
44764476
throw std::runtime_error(std::string(__func__) + ": writing active ScriptPubKeyMan id failed");
44774477
}

src/wallet/wallet.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
698698
std::string m_name;
699699

700700
/** Internal database handle. */
701-
std::unique_ptr<WalletDatabase> database;
701+
std::unique_ptr<WalletDatabase> const m_database;
702702

703703
/**
704704
* The following is used to keep track of how far behind the wallet is
@@ -732,7 +732,11 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
732732
*/
733733
mutable RecursiveMutex cs_wallet;
734734

735-
WalletDatabase& GetDatabase() const override { return *database; }
735+
WalletDatabase& GetDatabase() const override
736+
{
737+
assert(static_cast<bool>(m_database));
738+
return *m_database;
739+
}
736740

737741
/**
738742
* Select a set of coins such that nValueRet >= nTargetValue and at least
@@ -754,7 +758,7 @@ class CWallet final : public WalletStorage, public interfaces::Chain::Notificati
754758
CWallet(interfaces::Chain* chain, const std::string& name, std::unique_ptr<WalletDatabase> database)
755759
: m_chain(chain),
756760
m_name(name),
757-
database(std::move(database))
761+
m_database(std::move(database))
758762
{
759763
}
760764

0 commit comments

Comments
 (0)