Skip to content

Commit 25340b7

Browse files
committed
[Wallet] refactor wallet/init interaction
1 parent a6a8607 commit 25340b7

File tree

5 files changed

+118
-125
lines changed

5 files changed

+118
-125
lines changed

src/init.cpp

Lines changed: 5 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,6 @@
6363

6464
using namespace std;
6565

66-
#ifdef ENABLE_WALLET
67-
CWallet* pwalletMain = NULL;
68-
#endif
6966
bool fFeeEstimatesInitialized = false;
7067
static const bool DEFAULT_PROXYRANDOMIZE = true;
7168
static const bool DEFAULT_REST_ENABLE = false;
@@ -946,56 +943,8 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
946943
nBytesPerSigOp = GetArg("-bytespersigop", nBytesPerSigOp);
947944

948945
#ifdef ENABLE_WALLET
949-
if (mapArgs.count("-mintxfee"))
950-
{
951-
CAmount n = 0;
952-
if (ParseMoney(mapArgs["-mintxfee"], n) && n > 0)
953-
CWallet::minTxFee = CFeeRate(n);
954-
else
955-
return InitError(AmountErrMsg("mintxfee", mapArgs["-mintxfee"]));
956-
}
957-
if (mapArgs.count("-fallbackfee"))
958-
{
959-
CAmount nFeePerK = 0;
960-
if (!ParseMoney(mapArgs["-fallbackfee"], nFeePerK))
961-
return InitError(strprintf(_("Invalid amount for -fallbackfee=<amount>: '%s'"), mapArgs["-fallbackfee"]));
962-
if (nFeePerK > HIGH_TX_FEE_PER_KB)
963-
InitWarning(_("-fallbackfee is set very high! This is the transaction fee you may pay when fee estimates are not available."));
964-
CWallet::fallbackFee = CFeeRate(nFeePerK);
965-
}
966-
if (mapArgs.count("-paytxfee"))
967-
{
968-
CAmount nFeePerK = 0;
969-
if (!ParseMoney(mapArgs["-paytxfee"], nFeePerK))
970-
return InitError(AmountErrMsg("paytxfee", mapArgs["-paytxfee"]));
971-
if (nFeePerK > HIGH_TX_FEE_PER_KB)
972-
InitWarning(_("-paytxfee is set very high! This is the transaction fee you will pay if you send a transaction."));
973-
payTxFee = CFeeRate(nFeePerK, 1000);
974-
if (payTxFee < ::minRelayTxFee)
975-
{
976-
return InitError(strprintf(_("Invalid amount for -paytxfee=<amount>: '%s' (must be at least %s)"),
977-
mapArgs["-paytxfee"], ::minRelayTxFee.ToString()));
978-
}
979-
}
980-
if (mapArgs.count("-maxtxfee"))
981-
{
982-
CAmount nMaxFee = 0;
983-
if (!ParseMoney(mapArgs["-maxtxfee"], nMaxFee))
984-
return InitError(AmountErrMsg("maxtxfee", mapArgs["-maxtxfee"]));
985-
if (nMaxFee > HIGH_MAX_TX_FEE)
986-
InitWarning(_("-maxtxfee is set very high! Fees this large could be paid on a single transaction."));
987-
maxTxFee = nMaxFee;
988-
if (CFeeRate(maxTxFee, 1000) < ::minRelayTxFee)
989-
{
990-
return InitError(strprintf(_("Invalid amount for -maxtxfee=<amount>: '%s' (must be at least the minrelay fee of %s to prevent stuck transactions)"),
991-
mapArgs["-maxtxfee"], ::minRelayTxFee.ToString()));
992-
}
993-
}
994-
nTxConfirmTarget = GetArg("-txconfirmtarget", DEFAULT_TX_CONFIRM_TARGET);
995-
bSpendZeroConfChange = GetBoolArg("-spendzeroconfchange", DEFAULT_SPEND_ZEROCONF_CHANGE);
996-
fSendFreeTransactions = GetBoolArg("-sendfreetransactions", DEFAULT_SEND_FREE_TRANSACTIONS);
997-
998-
std::string strWalletFile = GetArg("-wallet", DEFAULT_WALLET_DAT);
946+
if (!CWallet::ParameterInteraction())
947+
return false;
999948
#endif // ENABLE_WALLET
1000949

1001950
fIsBareMultisigStd = GetBoolArg("-permitbaremultisig", DEFAULT_PERMIT_BAREMULTISIG);
@@ -1032,11 +981,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
1032981
return InitError(strprintf(_("Initialization sanity check failed. %s is shutting down."), _(PACKAGE_NAME)));
1033982

1034983
std::string strDataDir = GetDataDir().string();
1035-
#ifdef ENABLE_WALLET
1036-
// Wallet file must be a plain filename without a directory
1037-
if (strWalletFile != boost::filesystem::basename(strWalletFile) + boost::filesystem::extension(strWalletFile))
1038-
return InitError(strprintf(_("Wallet %s resides outside data directory %s"), strWalletFile, strDataDir));
1039-
#endif
984+
1040985
// Make sure only a single Bitcoin process is using the data directory.
1041986
boost::filesystem::path pathLockFile = GetDataDir() / ".lock";
1042987
FILE* file = fopen(pathLockFile.string().c_str(), "a"); // empty lock file; created if it doesn't exist.
@@ -1097,20 +1042,8 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
10971042
// ********************************************************* Step 5: verify wallet database integrity
10981043
#ifdef ENABLE_WALLET
10991044
if (!fDisableWallet) {
1100-
LogPrintf("Using wallet %s\n", strWalletFile);
1101-
uiInterface.InitMessage(_("Verifying wallet..."));
1102-
1103-
std::string warningString;
1104-
std::string errorString;
1105-
1106-
if (!CWallet::Verify(strWalletFile, warningString, errorString))
1045+
if (!CWallet::Verify())
11071046
return false;
1108-
1109-
if (!warningString.empty())
1110-
InitWarning(warningString);
1111-
if (!errorString.empty())
1112-
return InitError(errorString);
1113-
11141047
} // (!fDisableWallet)
11151048
#endif // ENABLE_WALLET
11161049
// ********************************************************* Step 6: network initialization
@@ -1421,16 +1354,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
14211354
pwalletMain = NULL;
14221355
LogPrintf("Wallet disabled!\n");
14231356
} else {
1424-
std::string warningString;
1425-
std::string errorString;
1426-
pwalletMain = CWallet::InitLoadWallet(fDisableWallet, strWalletFile, warningString, errorString);
1427-
if (!warningString.empty())
1428-
InitWarning(warningString);
1429-
if (!errorString.empty())
1430-
{
1431-
LogPrintf("%s", errorString);
1432-
return InitError(errorString);
1433-
}
1357+
CWallet::InitLoadWallet();
14341358
if (!pwalletMain)
14351359
return false;
14361360
}

src/init.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ namespace boost
1616
class thread_group;
1717
} // namespace boost
1818

19-
extern CWallet* pwalletMain;
20-
2119
void StartShutdown();
2220
bool ShutdownRequested();
2321
/** Interrupt threads */

src/test/test_bitcoin.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
#include <boost/thread.hpp>
3030

3131
CClientUIInterface uiInterface; // Declared but not defined in ui_interface.h
32-
CWallet* pwalletMain;
3332

3433
extern bool fPrintToConsole;
3534
extern void noui_connect();

src/wallet/wallet.cpp

Lines changed: 105 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
using namespace std;
3535

36+
CWallet* pwalletMain = NULL;
3637
/** Transaction fee set by the user */
3738
CFeeRate payTxFee(DEFAULT_TRANSACTION_FEE);
3839
unsigned int nTxConfirmTarget = DEFAULT_TX_CONFIRM_TARGET;
@@ -364,8 +365,33 @@ void CWallet::Flush(bool shutdown)
364365
bitdb.Flush(shutdown);
365366
}
366367

367-
bool CWallet::Verify(const string& walletFile, string& warningString, string& errorString)
368+
bool static UIError(const std::string &str)
368369
{
370+
uiInterface.ThreadSafeMessageBox(str, "", CClientUIInterface::MSG_ERROR);
371+
return false;
372+
}
373+
374+
void static UIWarning(const std::string &str)
375+
{
376+
uiInterface.ThreadSafeMessageBox(str, "", CClientUIInterface::MSG_WARNING);
377+
}
378+
379+
static std::string AmountErrMsg(const char * const optname, const std::string& strValue)
380+
{
381+
return strprintf(_("Invalid amount for -%s=<amount>: '%s'"), optname, strValue);
382+
}
383+
384+
bool CWallet::Verify()
385+
{
386+
std::string walletFile = GetArg("-wallet", DEFAULT_WALLET_DAT);
387+
388+
LogPrintf("Using wallet %s\n", walletFile);
389+
uiInterface.InitMessage(_("Verifying wallet..."));
390+
391+
// Wallet file must be a plain filename without a directory
392+
if (walletFile != boost::filesystem::basename(walletFile) + boost::filesystem::extension(walletFile))
393+
return UIError(strprintf(_("Wallet %s resides outside data directory %s"), walletFile, GetDataDir().string()));
394+
369395
if (!bitdb.Open(GetDataDir()))
370396
{
371397
// try moving the database env out of the way
@@ -381,9 +407,7 @@ bool CWallet::Verify(const string& walletFile, string& warningString, string& er
381407
// try again
382408
if (!bitdb.Open(GetDataDir())) {
383409
// if it still fails, it probably means we can't even create the database env
384-
string msg = strprintf(_("Error initializing wallet database environment %s!"), GetDataDir());
385-
errorString += msg;
386-
return true;
410+
return UIError(strprintf(_("Error initializing wallet database environment %s!"), GetDataDir()));
387411
}
388412
}
389413

@@ -399,14 +423,14 @@ bool CWallet::Verify(const string& walletFile, string& warningString, string& er
399423
CDBEnv::VerifyResult r = bitdb.Verify(walletFile, CWalletDB::Recover);
400424
if (r == CDBEnv::RECOVER_OK)
401425
{
402-
warningString += strprintf(_("Warning: Wallet file corrupt, data salvaged!"
426+
UIWarning(strprintf(_("Warning: Wallet file corrupt, data salvaged!"
403427
" Original %s saved as %s in %s; if"
404428
" your balance or transactions are incorrect you should"
405429
" restore from a backup."),
406-
walletFile, "wallet.{timestamp}.bak", GetDataDir());
430+
walletFile, "wallet.{timestamp}.bak", GetDataDir()));
407431
}
408432
if (r == CDBEnv::RECOVER_FAIL)
409-
errorString += strprintf(_("%s corrupt, salvage failed"), walletFile);
433+
return UIError(strprintf(_("%s corrupt, salvage failed"), walletFile));
410434
}
411435

412436
return true;
@@ -2992,20 +3016,20 @@ std::string CWallet::GetWalletHelpString(bool showDebug)
29923016
return strUsage;
29933017
}
29943018

2995-
CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWalletFile, std::string& warningString, std::string& errorString)
3019+
bool CWallet::InitLoadWallet()
29963020
{
3021+
std::string walletFile = GetArg("-wallet", DEFAULT_WALLET_DAT);
3022+
29973023
// needed to restore wallet transaction meta data after -zapwallettxes
29983024
std::vector<CWalletTx> vWtx;
29993025

30003026
if (GetBoolArg("-zapwallettxes", false)) {
30013027
uiInterface.InitMessage(_("Zapping all transactions from wallet..."));
30023028

3003-
CWallet *tempWallet = new CWallet(strWalletFile);
3029+
CWallet *tempWallet = new CWallet(walletFile);
30043030
DBErrors nZapWalletRet = tempWallet->ZapWalletTx(vWtx);
30053031
if (nZapWalletRet != DB_LOAD_OK) {
3006-
errorString = strprintf(_("Error loading %s: Wallet corrupted"), strWalletFile);
3007-
uiInterface.InitMessage(strprintf(_("Error loading %s: Wallet corrupted"), strWalletFile));
3008-
return NULL;
3032+
return UIError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile));
30093033
}
30103034

30113035
delete tempWallet;
@@ -3016,32 +3040,27 @@ CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWall
30163040

30173041
int64_t nStart = GetTimeMillis();
30183042
bool fFirstRun = true;
3019-
CWallet *walletInstance = new CWallet(strWalletFile);
3043+
CWallet *walletInstance = new CWallet(walletFile);
30203044
DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun);
30213045
if (nLoadWalletRet != DB_LOAD_OK)
30223046
{
30233047
if (nLoadWalletRet == DB_CORRUPT)
3024-
errorString += strprintf(_("Error loading %s: Wallet corrupted"), strWalletFile) + "\n";
3048+
return UIError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile));
30253049
else if (nLoadWalletRet == DB_NONCRITICAL_ERROR)
30263050
{
3027-
warningString += strprintf(_("Error reading %s! All keys read correctly, but transaction data"
3051+
UIWarning(strprintf(_("Error reading %s! All keys read correctly, but transaction data"
30283052
" or address book entries might be missing or incorrect."),
3029-
strWalletFile);
3053+
walletFile));
30303054
}
30313055
else if (nLoadWalletRet == DB_TOO_NEW)
3032-
errorString += strprintf(_("Error loading %s: Wallet requires newer version of %s"),
3033-
strWalletFile, _(PACKAGE_NAME)) +
3034-
"\n";
3056+
return UIError(strprintf(_("Error loading %s: Wallet requires newer version of %s"),
3057+
walletFile, _(PACKAGE_NAME)));
30353058
else if (nLoadWalletRet == DB_NEED_REWRITE)
30363059
{
3037-
errorString += strprintf(_("Wallet needed to be rewritten: restart %s to complete"), _(PACKAGE_NAME)) + "\n";
3038-
LogPrintf("%s", errorString);
3060+
return UIError(strprintf(_("Wallet needed to be rewritten: restart %s to complete"), _(PACKAGE_NAME)));
30393061
}
30403062
else
3041-
errorString += strprintf(_("Error loading %s"), strWalletFile) + "\n";
3042-
3043-
if (!errorString.empty())
3044-
return NULL;
3063+
return UIError(strprintf(_("Error loading %s"), walletFile));
30453064
}
30463065

30473066
if (GetBoolArg("-upgradewallet", fFirstRun))
@@ -3057,8 +3076,7 @@ CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWall
30573076
LogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion);
30583077
if (nMaxVersion < walletInstance->GetVersion())
30593078
{
3060-
errorString += _("Cannot downgrade wallet") + "\n";
3061-
return NULL;
3079+
return UIError(_("Cannot downgrade wallet"));
30623080
}
30633081
walletInstance->SetMaxVersion(nMaxVersion);
30643082
}
@@ -3072,10 +3090,7 @@ CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWall
30723090
if (walletInstance->GetKeyFromPool(newDefaultKey)) {
30733091
walletInstance->SetDefaultKey(newDefaultKey);
30743092
if (!walletInstance->SetAddressBook(walletInstance->vchDefaultKey.GetID(), "", "receive"))
3075-
{
3076-
errorString += _("Cannot write default address") += "\n";
3077-
return NULL;
3078-
}
3093+
return UIError(_("Cannot write default address") += "\n");
30793094
}
30803095

30813096
walletInstance->SetBestChain(chainActive.GetLocator());
@@ -3090,7 +3105,7 @@ CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWall
30903105
pindexRescan = chainActive.Genesis();
30913106
else
30923107
{
3093-
CWalletDB walletdb(strWalletFile);
3108+
CWalletDB walletdb(walletFile);
30943109
CBlockLocator locator;
30953110
if (walletdb.ReadBestBlock(locator))
30963111
pindexRescan = FindForkInGlobalIndex(chainActive, locator);
@@ -3109,10 +3124,7 @@ CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWall
31093124
block = block->pprev;
31103125

31113126
if (pindexRescan != block)
3112-
{
3113-
errorString = _("Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)");
3114-
return NULL;
3115-
}
3127+
return UIError(_("Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)"));
31163128
}
31173129

31183130
uiInterface.InitMessage(_("Rescanning..."));
@@ -3126,7 +3138,7 @@ CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWall
31263138
// Restore wallet transaction metadata after -zapwallettxes=1
31273139
if (GetBoolArg("-zapwallettxes", false) && GetArg("-zapwallettxes", "1") != "2")
31283140
{
3129-
CWalletDB walletdb(strWalletFile);
3141+
CWalletDB walletdb(walletFile);
31303142

31313143
BOOST_FOREACH(const CWalletTx& wtxOld, vWtx)
31323144
{
@@ -3150,7 +3162,62 @@ CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWall
31503162
}
31513163
walletInstance->SetBroadcastTransactions(GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST));
31523164

3153-
return walletInstance;
3165+
pwalletMain = walletInstance;
3166+
return true;
3167+
}
3168+
3169+
bool CWallet::ParameterInteraction()
3170+
{
3171+
if (mapArgs.count("-mintxfee"))
3172+
{
3173+
CAmount n = 0;
3174+
if (ParseMoney(mapArgs["-mintxfee"], n) && n > 0)
3175+
CWallet::minTxFee = CFeeRate(n);
3176+
else
3177+
return UIError(AmountErrMsg("mintxfee", mapArgs["-mintxfee"]));
3178+
}
3179+
if (mapArgs.count("-fallbackfee"))
3180+
{
3181+
CAmount nFeePerK = 0;
3182+
if (!ParseMoney(mapArgs["-fallbackfee"], nFeePerK))
3183+
return UIError(strprintf(_("Invalid amount for -fallbackfee=<amount>: '%s'"), mapArgs["-fallbackfee"]));
3184+
if (nFeePerK > HIGH_TX_FEE_PER_KB)
3185+
UIWarning(_("-fallbackfee is set very high! This is the transaction fee you may pay when fee estimates are not available."));
3186+
CWallet::fallbackFee = CFeeRate(nFeePerK);
3187+
}
3188+
if (mapArgs.count("-paytxfee"))
3189+
{
3190+
CAmount nFeePerK = 0;
3191+
if (!ParseMoney(mapArgs["-paytxfee"], nFeePerK))
3192+
return UIError(AmountErrMsg("paytxfee", mapArgs["-paytxfee"]));
3193+
if (nFeePerK > HIGH_TX_FEE_PER_KB)
3194+
UIWarning(_("-paytxfee is set very high! This is the transaction fee you will pay if you send a transaction."));
3195+
payTxFee = CFeeRate(nFeePerK, 1000);
3196+
if (payTxFee < ::minRelayTxFee)
3197+
{
3198+
return UIError(strprintf(_("Invalid amount for -paytxfee=<amount>: '%s' (must be at least %s)"),
3199+
mapArgs["-paytxfee"], ::minRelayTxFee.ToString()));
3200+
}
3201+
}
3202+
if (mapArgs.count("-maxtxfee"))
3203+
{
3204+
CAmount nMaxFee = 0;
3205+
if (!ParseMoney(mapArgs["-maxtxfee"], nMaxFee))
3206+
return UIError(AmountErrMsg("maxtxfee", mapArgs["-maxtxfee"]));
3207+
if (nMaxFee > HIGH_MAX_TX_FEE)
3208+
UIWarning(_("-maxtxfee is set very high! Fees this large could be paid on a single transaction."));
3209+
maxTxFee = nMaxFee;
3210+
if (CFeeRate(maxTxFee, 1000) < ::minRelayTxFee)
3211+
{
3212+
return UIError(strprintf(_("Invalid amount for -maxtxfee=<amount>: '%s' (must be at least the minrelay fee of %s to prevent stuck transactions)"),
3213+
mapArgs["-maxtxfee"], ::minRelayTxFee.ToString()));
3214+
}
3215+
}
3216+
nTxConfirmTarget = GetArg("-txconfirmtarget", DEFAULT_TX_CONFIRM_TARGET);
3217+
bSpendZeroConfChange = GetBoolArg("-spendzeroconfchange", DEFAULT_SPEND_ZEROCONF_CHANGE);
3218+
fSendFreeTransactions = GetBoolArg("-sendfreetransactions", DEFAULT_SEND_FREE_TRANSACTIONS);
3219+
3220+
return true;
31543221
}
31553222

31563223
CKeyPool::CKeyPool()

0 commit comments

Comments
 (0)