Skip to content

Commit 2a524b8

Browse files
committed
Merge #8776: Wallet refactoring leading up to multiwallet
5394b39 Wallet: Split main logic from InitLoadWallet into CreateWalletFromFile (Luke Dashjr) fb0c934 Wallet: Let the interval-flushing thread figure out the filename (Luke Dashjr)
2 parents ce5c1f4 + 5394b39 commit 2a524b8

File tree

4 files changed

+65
-31
lines changed

4 files changed

+65
-31
lines changed

src/wallet/wallet.cpp

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3405,16 +3405,8 @@ std::string CWallet::GetWalletHelpString(bool showDebug)
34053405
return strUsage;
34063406
}
34073407

3408-
bool CWallet::InitLoadWallet()
3408+
CWallet* CWallet::CreateWalletFromFile(const std::string walletFile)
34093409
{
3410-
if (GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
3411-
pwalletMain = NULL;
3412-
LogPrintf("Wallet disabled!\n");
3413-
return true;
3414-
}
3415-
3416-
std::string walletFile = GetArg("-wallet", DEFAULT_WALLET_DAT);
3417-
34183410
// needed to restore wallet transaction meta data after -zapwallettxes
34193411
std::vector<CWalletTx> vWtx;
34203412

@@ -3424,7 +3416,8 @@ bool CWallet::InitLoadWallet()
34243416
CWallet *tempWallet = new CWallet(walletFile);
34253417
DBErrors nZapWalletRet = tempWallet->ZapWalletTx(vWtx);
34263418
if (nZapWalletRet != DB_LOAD_OK) {
3427-
return InitError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile));
3419+
InitError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile));
3420+
return NULL;
34283421
}
34293422

34303423
delete tempWallet;
@@ -3439,23 +3432,29 @@ bool CWallet::InitLoadWallet()
34393432
DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun);
34403433
if (nLoadWalletRet != DB_LOAD_OK)
34413434
{
3442-
if (nLoadWalletRet == DB_CORRUPT)
3443-
return InitError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile));
3435+
if (nLoadWalletRet == DB_CORRUPT) {
3436+
InitError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile));
3437+
return NULL;
3438+
}
34443439
else if (nLoadWalletRet == DB_NONCRITICAL_ERROR)
34453440
{
34463441
InitWarning(strprintf(_("Error reading %s! All keys read correctly, but transaction data"
34473442
" or address book entries might be missing or incorrect."),
34483443
walletFile));
34493444
}
3450-
else if (nLoadWalletRet == DB_TOO_NEW)
3451-
return InitError(strprintf(_("Error loading %s: Wallet requires newer version of %s"),
3452-
walletFile, _(PACKAGE_NAME)));
3445+
else if (nLoadWalletRet == DB_TOO_NEW) {
3446+
InitError(strprintf(_("Error loading %s: Wallet requires newer version of %s"), walletFile, _(PACKAGE_NAME)));
3447+
return NULL;
3448+
}
34533449
else if (nLoadWalletRet == DB_NEED_REWRITE)
34543450
{
3455-
return InitError(strprintf(_("Wallet needed to be rewritten: restart %s to complete"), _(PACKAGE_NAME)));
3451+
InitError(strprintf(_("Wallet needed to be rewritten: restart %s to complete"), _(PACKAGE_NAME)));
3452+
return NULL;
3453+
}
3454+
else {
3455+
InitError(strprintf(_("Error loading %s"), walletFile));
3456+
return NULL;
34563457
}
3457-
else
3458-
return InitError(strprintf(_("Error loading %s"), walletFile));
34593458
}
34603459

34613460
if (GetBoolArg("-upgradewallet", fFirstRun))
@@ -3471,7 +3470,8 @@ bool CWallet::InitLoadWallet()
34713470
LogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion);
34723471
if (nMaxVersion < walletInstance->GetVersion())
34733472
{
3474-
return InitError(_("Cannot downgrade wallet"));
3473+
InitError(_("Cannot downgrade wallet"));
3474+
return NULL;
34753475
}
34763476
walletInstance->SetMaxVersion(nMaxVersion);
34773477
}
@@ -3488,18 +3488,24 @@ bool CWallet::InitLoadWallet()
34883488
CPubKey newDefaultKey;
34893489
if (walletInstance->GetKeyFromPool(newDefaultKey)) {
34903490
walletInstance->SetDefaultKey(newDefaultKey);
3491-
if (!walletInstance->SetAddressBook(walletInstance->vchDefaultKey.GetID(), "", "receive"))
3492-
return InitError(_("Cannot write default address") += "\n");
3491+
if (!walletInstance->SetAddressBook(walletInstance->vchDefaultKey.GetID(), "", "receive")) {
3492+
InitError(_("Cannot write default address") += "\n");
3493+
return NULL;
3494+
}
34933495
}
34943496

34953497
walletInstance->SetBestChain(chainActive.GetLocator());
34963498
}
34973499
else if (IsArgSet("-usehd")) {
34983500
bool useHD = GetBoolArg("-usehd", DEFAULT_USE_HD_WALLET);
3499-
if (walletInstance->IsHDEnabled() && !useHD)
3500-
return InitError(strprintf(_("Error loading %s: You can't disable HD on a already existing HD wallet"), walletFile));
3501-
if (!walletInstance->IsHDEnabled() && useHD)
3502-
return InitError(strprintf(_("Error loading %s: You can't enable HD on a already existing non-HD wallet"), walletFile));
3501+
if (walletInstance->IsHDEnabled() && !useHD) {
3502+
InitError(strprintf(_("Error loading %s: You can't disable HD on a already existing HD wallet"), walletFile));
3503+
return NULL;
3504+
}
3505+
if (!walletInstance->IsHDEnabled() && useHD) {
3506+
InitError(strprintf(_("Error loading %s: You can't enable HD on a already existing non-HD wallet"), walletFile));
3507+
return NULL;
3508+
}
35033509
}
35043510

35053511
LogPrintf(" wallet %15dms\n", GetTimeMillis() - nStart);
@@ -3529,8 +3535,10 @@ bool CWallet::InitLoadWallet()
35293535
while (block && block->pprev && (block->pprev->nStatus & BLOCK_HAVE_DATA) && block->pprev->nTx > 0 && pindexRescan != block)
35303536
block = block->pprev;
35313537

3532-
if (pindexRescan != block)
3533-
return InitError(_("Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)"));
3538+
if (pindexRescan != block) {
3539+
InitError(_("Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)"));
3540+
return NULL;
3541+
}
35343542
}
35353543

35363544
uiInterface.InitMessage(_("Rescanning..."));
@@ -3575,19 +3583,40 @@ bool CWallet::InitLoadWallet()
35753583
LogPrintf("mapAddressBook.size() = %u\n", walletInstance->mapAddressBook.size());
35763584
}
35773585

3578-
pwalletMain = walletInstance;
3586+
return walletInstance;
3587+
}
3588+
3589+
bool CWallet::InitLoadWallet()
3590+
{
3591+
if (GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
3592+
pwalletMain = NULL;
3593+
LogPrintf("Wallet disabled!\n");
3594+
return true;
3595+
}
3596+
3597+
std::string walletFile = GetArg("-wallet", DEFAULT_WALLET_DAT);
3598+
3599+
CWallet * const pwallet = CreateWalletFromFile(walletFile);
3600+
if (!pwallet) {
3601+
return false;
3602+
}
3603+
pwalletMain = pwallet;
35793604

35803605
return true;
35813606
}
35823607

3608+
std::atomic<bool> CWallet::fFlushThreadRunning(false);
3609+
35833610
void CWallet::postInitProcess(boost::thread_group& threadGroup)
35843611
{
35853612
// Add wallet transactions that aren't already in a block to mempool
35863613
// Do this here as mempool requires genesis block to be loaded
35873614
ReacceptWalletTransactions();
35883615

35893616
// Run a thread to flush wallet periodically
3590-
threadGroup.create_thread(boost::bind(&ThreadFlushWalletDB, boost::ref(this->strWalletFile)));
3617+
if (!CWallet::fFlushThreadRunning.exchange(true)) {
3618+
threadGroup.create_thread(ThreadFlushWalletDB);
3619+
}
35913620
}
35923621

35933622
bool CWallet::ParameterInteraction()

src/wallet/wallet.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "wallet/rpcwallet.h"
1919

2020
#include <algorithm>
21+
#include <atomic>
2122
#include <map>
2223
#include <set>
2324
#include <stdexcept>
@@ -558,6 +559,8 @@ class CAccountingEntry
558559
class CWallet : public CCryptoKeyStore, public CValidationInterface
559560
{
560561
private:
562+
static std::atomic<bool> fFlushThreadRunning;
563+
561564
/**
562565
* Select a set of coins such that nValueRet >= nTargetValue and at least
563566
* all coins from coinControl are selected; Never select unconfirmed coins
@@ -920,6 +923,7 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
920923
static std::string GetWalletHelpString(bool showDebug);
921924

922925
/* Initializes the wallet, returns a new CWallet instance or a null pointer in case of an error */
926+
static CWallet* CreateWalletFromFile(const std::string walletFile);
923927
static bool InitLoadWallet();
924928

925929
/**

src/wallet/walletdb.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -768,7 +768,7 @@ DBErrors CWalletDB::ZapWalletTx(CWallet* pwallet, vector<CWalletTx>& vWtx)
768768
return DB_LOAD_OK;
769769
}
770770

771-
void ThreadFlushWalletDB(const string& strFile)
771+
void ThreadFlushWalletDB()
772772
{
773773
// Make this thread recognisable as the wallet flushing thread
774774
RenameThread("bitcoin-wallet");
@@ -810,6 +810,7 @@ void ThreadFlushWalletDB(const string& strFile)
810810
if (nRefCount == 0)
811811
{
812812
boost::this_thread::interruption_point();
813+
const std::string& strFile = pwalletMain->strWalletFile;
813814
map<string, int>::iterator _mi = bitdb.mapFileUseCount.find(strFile);
814815
if (_mi != bitdb.mapFileUseCount.end())
815816
{

src/wallet/walletdb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,6 @@ class CWalletDB : public CDB
182182

183183
};
184184

185-
void ThreadFlushWalletDB(const std::string& strFile);
185+
void ThreadFlushWalletDB();
186186

187187
#endif // BITCOIN_WALLET_WALLETDB_H

0 commit comments

Comments
 (0)