Skip to content

Commit 071c955

Browse files
committed
wallet: Get rid of fFileBacked
Instead, CWalletDB() with a dummy handle will just give you a no-op database in which writes always succeeds and reads always fail. CDB already had functionality for this, so just use that.
1 parent 71afe3c commit 071c955

File tree

4 files changed

+52
-81
lines changed

4 files changed

+52
-81
lines changed

src/wallet/db.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,10 @@ CDB::CDB(CWalletDBWrapper& dbw, const char* pszMode, bool fFlushOnCloseIn) : pdb
364364
int ret;
365365
fReadOnly = (!strchr(pszMode, '+') && !strchr(pszMode, 'w'));
366366
fFlushOnClose = fFlushOnCloseIn;
367-
const std::string& strFilename = dbw.strFile;
367+
if (dbw.IsDummy()) {
368+
return;
369+
}
370+
const std::string &strFilename = dbw.strFile;
368371

369372
bool fCreate = strchr(pszMode, 'c') != NULL;
370373
unsigned int nFlags = DB_THREAD;
@@ -473,7 +476,7 @@ bool CDBEnv::RemoveDb(const std::string& strFile)
473476

474477
bool CDB::Rewrite(CWalletDBWrapper& dbw, const char* pszSkip)
475478
{
476-
if (!dbw.env) {
479+
if (dbw.IsDummy()) {
477480
return true;
478481
}
479482
const std::string& strFile = dbw.strFile;
@@ -601,6 +604,9 @@ void CDBEnv::Flush(bool fShutdown)
601604

602605
bool CDB::PeriodicFlush(CWalletDBWrapper& dbw)
603606
{
607+
if (dbw.IsDummy()) {
608+
return true;
609+
}
604610
bool ret = false;
605611
const std::string& strFile = dbw.strFile;
606612
TRY_LOCK(bitdb.cs_db,lockDb);
@@ -645,7 +651,7 @@ bool CWalletDBWrapper::Rewrite(const char* pszSkip)
645651

646652
bool CWalletDBWrapper::Backup(const std::string& strDest)
647653
{
648-
if (!env) {
654+
if (IsDummy()) {
649655
return false;
650656
}
651657
while (true)

src/wallet/db.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ class CWalletDBWrapper
9393
{
9494
friend class CDB;
9595
public:
96+
/** Create dummy DB handle */
97+
CWalletDBWrapper(): env(nullptr)
98+
{
99+
}
100+
101+
/** Create DB handle to real database */
96102
CWalletDBWrapper(CDBEnv *env_in, const std::string &strFile_in):
97103
env(env_in), strFile(strFile_in)
98104
{
@@ -110,6 +116,12 @@ class CWalletDBWrapper
110116
*/
111117
std::string GetName() const { return strFile; }
112118

119+
/** Return whether this database handle is a dummy for testing.
120+
* Only to be used at a low level, application should ideally not care
121+
* about this.
122+
*/
123+
bool IsDummy() { return env == nullptr; }
124+
113125
private:
114126
/** BerkeleyDB specific */
115127
CDBEnv *env;
@@ -186,7 +198,7 @@ class CDB
186198
bool Write(const K& key, const T& value, bool fOverwrite = true)
187199
{
188200
if (!pdb)
189-
return false;
201+
return true;
190202
if (fReadOnly)
191203
assert(!"Write called on database in read-only mode");
192204

src/wallet/wallet.cpp

Lines changed: 26 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,6 @@ bool CWallet::AddKeyPubKey(const CKey& secret, const CPubKey &pubkey)
180180
if (HaveWatchOnly(script))
181181
RemoveWatchOnly(script);
182182

183-
if (!fFileBacked)
184-
return true;
185183
if (!IsCrypted()) {
186184
return CWalletDB(*dbw).WriteKey(pubkey,
187185
secret.GetPrivKey(),
@@ -195,8 +193,6 @@ bool CWallet::AddCryptedKey(const CPubKey &vchPubKey,
195193
{
196194
if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret))
197195
return false;
198-
if (!fFileBacked)
199-
return true;
200196
{
201197
LOCK(cs_wallet);
202198
if (pwalletdbEncryption)
@@ -240,8 +236,6 @@ bool CWallet::AddCScript(const CScript& redeemScript)
240236
{
241237
if (!CCryptoKeyStore::AddCScript(redeemScript))
242238
return false;
243-
if (!fFileBacked)
244-
return true;
245239
return CWalletDB(*dbw).WriteCScript(Hash160(redeemScript), redeemScript);
246240
}
247241

@@ -268,8 +262,6 @@ bool CWallet::AddWatchOnly(const CScript& dest)
268262
const CKeyMetadata& meta = mapKeyMetadata[CScriptID(dest)];
269263
UpdateTimeFirstKey(meta.nCreateTime);
270264
NotifyWatchonlyChanged(true);
271-
if (!fFileBacked)
272-
return true;
273265
return CWalletDB(*dbw).WriteWatchOnly(dest, meta);
274266
}
275267

@@ -286,9 +278,8 @@ bool CWallet::RemoveWatchOnly(const CScript &dest)
286278
return false;
287279
if (!HaveWatchOnly())
288280
NotifyWatchonlyChanged(false);
289-
if (fFileBacked)
290-
if (!CWalletDB(*dbw).EraseWatchOnly(dest))
291-
return false;
281+
if (!CWalletDB(*dbw).EraseWatchOnly(dest))
282+
return false;
292283

293284
return true;
294285
}
@@ -385,7 +376,6 @@ bool CWallet::SetMinVersion(enum WalletFeature nVersion, CWalletDB* pwalletdbIn,
385376
if (nVersion > nWalletMaxVersion)
386377
nWalletMaxVersion = nVersion;
387378

388-
if (fFileBacked)
389379
{
390380
CWalletDB* pwalletdb = pwalletdbIn ? pwalletdbIn : new CWalletDB(*dbw);
391381
if (nWalletVersion > 40000)
@@ -594,24 +584,19 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
594584
{
595585
LOCK(cs_wallet);
596586
mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
597-
if (fFileBacked)
598-
{
599-
assert(!pwalletdbEncryption);
600-
pwalletdbEncryption = new CWalletDB(*dbw);
601-
if (!pwalletdbEncryption->TxnBegin()) {
602-
delete pwalletdbEncryption;
603-
pwalletdbEncryption = NULL;
604-
return false;
605-
}
606-
pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
587+
assert(!pwalletdbEncryption);
588+
pwalletdbEncryption = new CWalletDB(*dbw);
589+
if (!pwalletdbEncryption->TxnBegin()) {
590+
delete pwalletdbEncryption;
591+
pwalletdbEncryption = NULL;
592+
return false;
607593
}
594+
pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
608595

609596
if (!EncryptKeys(_vMasterKey))
610597
{
611-
if (fFileBacked) {
612-
pwalletdbEncryption->TxnAbort();
613-
delete pwalletdbEncryption;
614-
}
598+
pwalletdbEncryption->TxnAbort();
599+
delete pwalletdbEncryption;
615600
// We now probably have half of our keys encrypted in memory, and half not...
616601
// die and let the user reload the unencrypted wallet.
617602
assert(false);
@@ -620,19 +605,16 @@ bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
620605
// Encryption was introduced in version 0.4.0
621606
SetMinVersion(FEATURE_WALLETCRYPT, pwalletdbEncryption, true);
622607

623-
if (fFileBacked)
624-
{
625-
if (!pwalletdbEncryption->TxnCommit()) {
626-
delete pwalletdbEncryption;
627-
// We now have keys encrypted in memory, but not on disk...
628-
// die to avoid confusion and let the user reload the unencrypted wallet.
629-
assert(false);
630-
}
631-
608+
if (!pwalletdbEncryption->TxnCommit()) {
632609
delete pwalletdbEncryption;
633-
pwalletdbEncryption = NULL;
610+
// We now have keys encrypted in memory, but not on disk...
611+
// die to avoid confusion and let the user reload the unencrypted wallet.
612+
assert(false);
634613
}
635614

615+
delete pwalletdbEncryption;
616+
pwalletdbEncryption = NULL;
617+
636618
Lock();
637619
Unlock(strWalletPassphrase);
638620

@@ -2816,8 +2798,6 @@ CAmount CWallet::GetMinimumFee(unsigned int nTxBytes, unsigned int nConfirmTarge
28162798

28172799
DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
28182800
{
2819-
if (!fFileBacked)
2820-
return DB_LOAD_OK;
28212801
fFirstRunRet = false;
28222802
DBErrors nLoadWalletRet = CWalletDB(*dbw,"cr+").LoadWallet(this);
28232803
if (nLoadWalletRet == DB_NEED_REWRITE)
@@ -2843,8 +2823,6 @@ DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
28432823

28442824
DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256>& vHashOut)
28452825
{
2846-
if (!fFileBacked)
2847-
return DB_LOAD_OK;
28482826
AssertLockHeld(cs_wallet); // mapWallet
28492827
vchDefaultKey = CPubKey();
28502828
DBErrors nZapSelectTxRet = CWalletDB(*dbw,"cr+").ZapSelectTx(vHashIn, vHashOut);
@@ -2873,8 +2851,6 @@ DBErrors CWallet::ZapSelectTx(std::vector<uint256>& vHashIn, std::vector<uint256
28732851

28742852
DBErrors CWallet::ZapWalletTx(std::vector<CWalletTx>& vWtx)
28752853
{
2876-
if (!fFileBacked)
2877-
return DB_LOAD_OK;
28782854
vchDefaultKey = CPubKey();
28792855
DBErrors nZapWalletTxRet = CWalletDB(*dbw,"cr+").ZapWalletTx(vWtx);
28802856
if (nZapWalletTxRet == DB_NEED_REWRITE)
@@ -2909,8 +2885,6 @@ bool CWallet::SetAddressBook(const CTxDestination& address, const std::string& s
29092885
}
29102886
NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address) != ISMINE_NO,
29112887
strPurpose, (fUpdated ? CT_UPDATED : CT_NEW) );
2912-
if (!fFileBacked)
2913-
return false;
29142888
if (!strPurpose.empty() && !CWalletDB(*dbw).WritePurpose(CBitcoinAddress(address).ToString(), strPurpose))
29152889
return false;
29162890
return CWalletDB(*dbw).WriteName(CBitcoinAddress(address).ToString(), strName);
@@ -2921,33 +2895,25 @@ bool CWallet::DelAddressBook(const CTxDestination& address)
29212895
{
29222896
LOCK(cs_wallet); // mapAddressBook
29232897

2924-
if(fFileBacked)
2898+
// Delete destdata tuples associated with address
2899+
std::string strAddress = CBitcoinAddress(address).ToString();
2900+
BOOST_FOREACH(const PAIRTYPE(std::string, std::string) &item, mapAddressBook[address].destdata)
29252901
{
2926-
// Delete destdata tuples associated with address
2927-
std::string strAddress = CBitcoinAddress(address).ToString();
2928-
BOOST_FOREACH(const PAIRTYPE(std::string, std::string) &item, mapAddressBook[address].destdata)
2929-
{
2930-
CWalletDB(*dbw).EraseDestData(strAddress, item.first);
2931-
}
2902+
CWalletDB(*dbw).EraseDestData(strAddress, item.first);
29322903
}
29332904
mapAddressBook.erase(address);
29342905
}
29352906

29362907
NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address) != ISMINE_NO, "", CT_DELETED);
29372908

2938-
if (!fFileBacked)
2939-
return false;
29402909
CWalletDB(*dbw).ErasePurpose(CBitcoinAddress(address).ToString());
29412910
return CWalletDB(*dbw).EraseName(CBitcoinAddress(address).ToString());
29422911
}
29432912

29442913
bool CWallet::SetDefaultKey(const CPubKey &vchPubKey)
29452914
{
2946-
if (fFileBacked)
2947-
{
2948-
if (!CWalletDB(*dbw).WriteDefaultKey(vchPubKey))
2949-
return false;
2950-
}
2915+
if (!CWalletDB(*dbw).WriteDefaultKey(vchPubKey))
2916+
return false;
29512917
vchDefaultKey = vchPubKey;
29522918
return true;
29532919
}
@@ -3081,11 +3047,8 @@ void CWallet::ReserveKeyFromKeyPool(int64_t& nIndex, CKeyPool& keypool, bool int
30813047
void CWallet::KeepKey(int64_t nIndex)
30823048
{
30833049
// Remove from key pool
3084-
if (fFileBacked)
3085-
{
3086-
CWalletDB walletdb(*dbw);
3087-
walletdb.ErasePool(nIndex);
3088-
}
3050+
CWalletDB walletdb(*dbw);
3051+
walletdb.ErasePool(nIndex);
30893052
LogPrintf("keypool keep %d\n", nIndex);
30903053
}
30913054

@@ -3597,17 +3560,13 @@ bool CWallet::AddDestData(const CTxDestination &dest, const std::string &key, co
35973560
return false;
35983561

35993562
mapAddressBook[dest].destdata.insert(std::make_pair(key, value));
3600-
if (!fFileBacked)
3601-
return true;
36023563
return CWalletDB(*dbw).WriteDestData(CBitcoinAddress(dest).ToString(), key, value);
36033564
}
36043565

36053566
bool CWallet::EraseDestData(const CTxDestination &dest, const std::string &key)
36063567
{
36073568
if (!mapAddressBook[dest].destdata.erase(key))
36083569
return false;
3609-
if (!fFileBacked)
3610-
return true;
36113570
return CWalletDB(*dbw).EraseDestData(CBitcoinAddress(dest).ToString(), key);
36123571
}
36133572

@@ -3979,8 +3938,6 @@ bool CWallet::ParameterInteraction()
39793938

39803939
bool CWallet::BackupWallet(const std::string& strDest)
39813940
{
3982-
if (!fFileBacked)
3983-
return false;
39843941
return dbw->Backup(strDest);
39853942
}
39863943

src/wallet/wallet.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -698,8 +698,6 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
698698
/* HD derive new child key (on internal or external chain) */
699699
void DeriveNewChildKey(CKeyMetadata& metadata, CKey& secret, bool internal = false);
700700

701-
bool fFileBacked;
702-
703701
std::set<int64_t> setKeyPool;
704702

705703
int64_t nTimeFirstKey;
@@ -720,9 +718,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
720718
public:
721719
/*
722720
* Main wallet lock.
723-
* This lock protects all the fields added by CWallet
724-
* except for:
725-
* fFileBacked (immutable after instantiation)
721+
* This lock protects all the fields added by CWallet.
726722
*/
727723
mutable CCriticalSection cs_wallet;
728724

@@ -765,15 +761,16 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
765761
MasterKeyMap mapMasterKeys;
766762
unsigned int nMasterKeyMaxID;
767763

768-
CWallet()
764+
// Create wallet with dummy database handle
765+
CWallet(): dbw(new CWalletDBWrapper())
769766
{
770767
SetNull();
771768
}
772769

770+
// Create wallet with passed-in database handle
773771
CWallet(std::unique_ptr<CWalletDBWrapper> dbw_in) : dbw(std::move(dbw_in))
774772
{
775773
SetNull();
776-
fFileBacked = true;
777774
}
778775

779776
~CWallet()
@@ -786,7 +783,6 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
786783
{
787784
nWalletVersion = FEATURE_BASE;
788785
nWalletMaxVersion = FEATURE_BASE;
789-
fFileBacked = false;
790786
nMasterKeyMaxID = 0;
791787
pwalletdbEncryption = NULL;
792788
nOrderPosNext = 0;

0 commit comments

Comments
 (0)