Merge #7691: [Wallet] refactor wallet/init interaction

25340b7 [Wallet] refactor wallet/init interaction (Jonas Schnelli)
This commit is contained in:
Wladimir J. van der Laan 2016-04-02 11:06:56 +02:00 committed by Alexander Block
parent a5505e7e8c
commit 298224b3b7
5 changed files with 118 additions and 125 deletions

View File

@ -94,9 +94,6 @@ using namespace std;
extern void ThreadSendAlert(CConnman& connman); extern void ThreadSendAlert(CConnman& connman);
#ifdef ENABLE_WALLET
CWallet* pwalletMain = NULL;
#endif
bool fFeeEstimatesInitialized = false; bool fFeeEstimatesInitialized = false;
bool fRestartRequested = false; // true: restart false: shutdown bool fRestartRequested = false; // true: restart false: shutdown
static const bool DEFAULT_PROXYRANDOMIZE = true; static const bool DEFAULT_PROXYRANDOMIZE = true;
@ -1116,56 +1113,8 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
nBytesPerSigOp = GetArg("-bytespersigop", nBytesPerSigOp); nBytesPerSigOp = GetArg("-bytespersigop", nBytesPerSigOp);
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
if (mapArgs.count("-mintxfee")) if (!CWallet::ParameterInteraction())
{ return false;
CAmount n = 0;
if (ParseMoney(mapArgs["-mintxfee"], n) && n > 0)
CWallet::minTxFee = CFeeRate(n);
else
return InitError(AmountErrMsg("mintxfee", mapArgs["-mintxfee"]));
}
if (mapArgs.count("-fallbackfee"))
{
CAmount nFeePerK = 0;
if (!ParseMoney(mapArgs["-fallbackfee"], nFeePerK))
return InitError(AmountErrMsg("fallbackfee", mapArgs["-fallbackfee"]));
if (nFeePerK > HIGH_TX_FEE_PER_KB)
InitWarning(_("-fallbackfee is set very high! This is the transaction fee you may pay when fee estimates are not available."));
CWallet::fallbackFee = CFeeRate(nFeePerK);
}
if (mapArgs.count("-paytxfee"))
{
CAmount nFeePerK = 0;
if (!ParseMoney(mapArgs["-paytxfee"], nFeePerK))
return InitError(AmountErrMsg("paytxfee", mapArgs["-paytxfee"]));
if (nFeePerK > HIGH_TX_FEE_PER_KB)
InitWarning(_("-paytxfee is set very high! This is the transaction fee you will pay if you send a transaction."));
payTxFee = CFeeRate(nFeePerK, 1000);
if (payTxFee < ::minRelayTxFee)
{
return InitError(strprintf(_("Invalid amount for -paytxfee=<amount>: '%s' (must be at least %s)"),
mapArgs["-paytxfee"], ::minRelayTxFee.ToString()));
}
}
if (mapArgs.count("-maxtxfee"))
{
CAmount nMaxFee = 0;
if (!ParseMoney(mapArgs["-maxtxfee"], nMaxFee))
return InitError(AmountErrMsg("maxtxfee", mapArgs["-maxtxfee"]));
if (nMaxFee > HIGH_MAX_TX_FEE)
InitWarning(_("-maxtxfee is set very high! Fees this large could be paid on a single transaction."));
maxTxFee = nMaxFee;
if (CFeeRate(maxTxFee, 1000) < ::minRelayTxFee)
{
return InitError(strprintf(_("Invalid amount for -maxtxfee=<amount>: '%s' (must be at least the minrelay fee of %s to prevent stuck transactions)"),
mapArgs["-maxtxfee"], ::minRelayTxFee.ToString()));
}
}
nTxConfirmTarget = GetArg("-txconfirmtarget", DEFAULT_TX_CONFIRM_TARGET);
bSpendZeroConfChange = GetBoolArg("-spendzeroconfchange", DEFAULT_SPEND_ZEROCONF_CHANGE);
fSendFreeTransactions = GetBoolArg("-sendfreetransactions", DEFAULT_SEND_FREE_TRANSACTIONS);
std::string strWalletFile = GetArg("-wallet", DEFAULT_WALLET_DAT);
#endif // ENABLE_WALLET #endif // ENABLE_WALLET
fIsBareMultisigStd = GetBoolArg("-permitbaremultisig", DEFAULT_PERMIT_BAREMULTISIG); fIsBareMultisigStd = GetBoolArg("-permitbaremultisig", DEFAULT_PERMIT_BAREMULTISIG);
@ -1208,11 +1157,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
return InitError(strprintf(_("Initialization sanity check failed. %s is shutting down."), _(PACKAGE_NAME))); return InitError(strprintf(_("Initialization sanity check failed. %s is shutting down."), _(PACKAGE_NAME)));
std::string strDataDir = GetDataDir().string(); std::string strDataDir = GetDataDir().string();
#ifdef ENABLE_WALLET
// Wallet file must be a plain filename without a directory
if (strWalletFile != boost::filesystem::basename(strWalletFile) + boost::filesystem::extension(strWalletFile))
return InitError(strprintf(_("Wallet %s resides outside data directory %s"), strWalletFile, strDataDir));
#endif
// Make sure only a single Dash Core process is using the data directory. // Make sure only a single Dash Core process is using the data directory.
boost::filesystem::path pathLockFile = GetDataDir() / ".lock"; boost::filesystem::path pathLockFile = GetDataDir() / ".lock";
FILE* file = fopen(pathLockFile.string().c_str(), "a"); // empty lock file; created if it doesn't exist. FILE* file = fopen(pathLockFile.string().c_str(), "a"); // empty lock file; created if it doesn't exist.
@ -1296,21 +1241,9 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
return InitError(strError); return InitError(strError);
} }
LogPrintf("Using wallet %s\n", strWalletFile); if (!CWallet::Verify())
uiInterface.InitMessage(_("Verifying wallet..."));
// reset warning string
strWarning = "";
if (!CWallet::Verify(strWalletFile, strWarning, strError))
return false; return false;
if (!strWarning.empty())
InitWarning(strWarning);
if (!strError.empty())
return InitError(strError);
// Initialize KeePass Integration // Initialize KeePass Integration
keePassInt.init(); keePassInt.init();
@ -1653,16 +1586,7 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler)
pwalletMain = NULL; pwalletMain = NULL;
LogPrintf("Wallet disabled!\n"); LogPrintf("Wallet disabled!\n");
} else { } else {
std::string warningString; CWallet::InitLoadWallet();
std::string errorString;
pwalletMain = CWallet::InitLoadWallet(fDisableWallet, strWalletFile, warningString, errorString);
if (!warningString.empty())
InitWarning(warningString);
if (!errorString.empty())
{
LogPrintf("%s", errorString);
return InitError(errorString);
}
if (!pwalletMain) if (!pwalletMain)
return false; return false;
} }

View File

@ -16,8 +16,6 @@ namespace boost
class thread_group; class thread_group;
} // namespace boost } // namespace boost
extern CWallet* pwalletMain;
void StartShutdown(); void StartShutdown();
bool ShutdownRequested(); bool ShutdownRequested();
/** Interrupt threads */ /** Interrupt threads */

View File

@ -32,7 +32,6 @@
#include <boost/thread.hpp> #include <boost/thread.hpp>
CClientUIInterface uiInterface; // Declared but not defined in ui_interface.h CClientUIInterface uiInterface; // Declared but not defined in ui_interface.h
CWallet* pwalletMain;
std::unique_ptr<CConnman> g_connman; std::unique_ptr<CConnman> g_connman;

View File

@ -41,6 +41,7 @@
using namespace std; using namespace std;
CWallet* pwalletMain = NULL;
/** Transaction fee set by the user */ /** Transaction fee set by the user */
CFeeRate payTxFee(DEFAULT_TRANSACTION_FEE); CFeeRate payTxFee(DEFAULT_TRANSACTION_FEE);
unsigned int nTxConfirmTarget = DEFAULT_TX_CONFIRM_TARGET; unsigned int nTxConfirmTarget = DEFAULT_TX_CONFIRM_TARGET;
@ -592,8 +593,33 @@ void CWallet::Flush(bool shutdown)
bitdb.Flush(shutdown); bitdb.Flush(shutdown);
} }
bool CWallet::Verify(const string& walletFile, string& warningString, string& errorString) bool static UIError(const std::string &str)
{ {
uiInterface.ThreadSafeMessageBox(str, "", CClientUIInterface::MSG_ERROR);
return false;
}
void static UIWarning(const std::string &str)
{
uiInterface.ThreadSafeMessageBox(str, "", CClientUIInterface::MSG_WARNING);
}
static std::string AmountErrMsg(const char * const optname, const std::string& strValue)
{
return strprintf(_("Invalid amount for -%s=<amount>: '%s'"), optname, strValue);
}
bool CWallet::Verify()
{
std::string walletFile = GetArg("-wallet", DEFAULT_WALLET_DAT);
LogPrintf("Using wallet %s\n", walletFile);
uiInterface.InitMessage(_("Verifying wallet..."));
// Wallet file must be a plain filename without a directory
if (walletFile != boost::filesystem::basename(walletFile) + boost::filesystem::extension(walletFile))
return UIError(strprintf(_("Wallet %s resides outside data directory %s"), walletFile, GetDataDir().string()));
if (!bitdb.Open(GetDataDir())) if (!bitdb.Open(GetDataDir()))
{ {
// try moving the database env out of the way // try moving the database env out of the way
@ -609,9 +635,7 @@ bool CWallet::Verify(const string& walletFile, string& warningString, string& er
// try again // try again
if (!bitdb.Open(GetDataDir())) { if (!bitdb.Open(GetDataDir())) {
// if it still fails, it probably means we can't even create the database env // if it still fails, it probably means we can't even create the database env
string msg = strprintf(_("Error initializing wallet database environment %s!"), GetDataDir()); return UIError(strprintf(_("Error initializing wallet database environment %s!"), GetDataDir()));
errorString += msg;
return true;
} }
} }
@ -627,14 +651,14 @@ bool CWallet::Verify(const string& walletFile, string& warningString, string& er
CDBEnv::VerifyResult r = bitdb.Verify(walletFile, CWalletDB::Recover); CDBEnv::VerifyResult r = bitdb.Verify(walletFile, CWalletDB::Recover);
if (r == CDBEnv::RECOVER_OK) if (r == CDBEnv::RECOVER_OK)
{ {
warningString += strprintf(_("Warning: Wallet file corrupt, data salvaged!" UIWarning(strprintf(_("Warning: Wallet file corrupt, data salvaged!"
" Original %s saved as %s in %s; if" " Original %s saved as %s in %s; if"
" your balance or transactions are incorrect you should" " your balance or transactions are incorrect you should"
" restore from a backup."), " restore from a backup."),
walletFile, "wallet.{timestamp}.bak", GetDataDir()); walletFile, "wallet.{timestamp}.bak", GetDataDir()));
} }
if (r == CDBEnv::RECOVER_FAIL) if (r == CDBEnv::RECOVER_FAIL)
errorString += strprintf(_("%s corrupt, salvage failed"), walletFile); return UIError(strprintf(_("%s corrupt, salvage failed"), walletFile));
} }
return true; return true;
@ -4428,20 +4452,20 @@ std::string CWallet::GetWalletHelpString(bool showDebug)
return strUsage; return strUsage;
} }
CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWalletFile, std::string& warningString, std::string& errorString) bool CWallet::InitLoadWallet()
{ {
std::string walletFile = GetArg("-wallet", DEFAULT_WALLET_DAT);
// needed to restore wallet transaction meta data after -zapwallettxes // needed to restore wallet transaction meta data after -zapwallettxes
std::vector<CWalletTx> vWtx; std::vector<CWalletTx> vWtx;
if (GetBoolArg("-zapwallettxes", false)) { if (GetBoolArg("-zapwallettxes", false)) {
uiInterface.InitMessage(_("Zapping all transactions from wallet...")); uiInterface.InitMessage(_("Zapping all transactions from wallet..."));
CWallet *tempWallet = new CWallet(strWalletFile); CWallet *tempWallet = new CWallet(walletFile);
DBErrors nZapWalletRet = tempWallet->ZapWalletTx(vWtx); DBErrors nZapWalletRet = tempWallet->ZapWalletTx(vWtx);
if (nZapWalletRet != DB_LOAD_OK) { if (nZapWalletRet != DB_LOAD_OK) {
errorString = strprintf(_("Error loading %s: Wallet corrupted"), strWalletFile); return UIError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile));
uiInterface.InitMessage(strprintf(_("Error loading %s: Wallet corrupted"), strWalletFile));
return NULL;
} }
delete tempWallet; delete tempWallet;
@ -4452,32 +4476,27 @@ CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWall
int64_t nStart = GetTimeMillis(); int64_t nStart = GetTimeMillis();
bool fFirstRun = true; bool fFirstRun = true;
CWallet *walletInstance = new CWallet(strWalletFile); CWallet *walletInstance = new CWallet(walletFile);
DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun); DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun);
if (nLoadWalletRet != DB_LOAD_OK) if (nLoadWalletRet != DB_LOAD_OK)
{ {
if (nLoadWalletRet == DB_CORRUPT) if (nLoadWalletRet == DB_CORRUPT)
errorString += strprintf(_("Error loading %s: Wallet corrupted"), strWalletFile) + "\n"; return UIError(strprintf(_("Error loading %s: Wallet corrupted"), walletFile));
else if (nLoadWalletRet == DB_NONCRITICAL_ERROR) else if (nLoadWalletRet == DB_NONCRITICAL_ERROR)
{ {
warningString += strprintf(_("Error reading %s! All keys read correctly, but transaction data" UIWarning(strprintf(_("Error reading %s! All keys read correctly, but transaction data"
" or address book entries might be missing or incorrect."), " or address book entries might be missing or incorrect."),
strWalletFile); walletFile));
} }
else if (nLoadWalletRet == DB_TOO_NEW) else if (nLoadWalletRet == DB_TOO_NEW)
errorString += strprintf(_("Error loading %s: Wallet requires newer version of %s"), return UIError(strprintf(_("Error loading %s: Wallet requires newer version of %s"),
strWalletFile, _(PACKAGE_NAME)) + walletFile, _(PACKAGE_NAME)));
"\n";
else if (nLoadWalletRet == DB_NEED_REWRITE) else if (nLoadWalletRet == DB_NEED_REWRITE)
{ {
errorString += strprintf(_("Wallet needed to be rewritten: restart %s to complete"), _(PACKAGE_NAME)) + "\n"; return UIError(strprintf(_("Wallet needed to be rewritten: restart %s to complete"), _(PACKAGE_NAME)));
LogPrintf("%s", errorString);
} }
else else
errorString += strprintf(_("Error loading %s"), strWalletFile) + "\n"; return UIError(strprintf(_("Error loading %s"), walletFile));
if (!errorString.empty())
return NULL;
} }
if (GetBoolArg("-upgradewallet", fFirstRun)) if (GetBoolArg("-upgradewallet", fFirstRun))
@ -4493,8 +4512,7 @@ CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWall
LogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion); LogPrintf("Allowing wallet upgrade up to %i\n", nMaxVersion);
if (nMaxVersion < walletInstance->GetVersion()) if (nMaxVersion < walletInstance->GetVersion())
{ {
errorString += _("Cannot downgrade wallet") + "\n"; return UIError(_("Cannot downgrade wallet"));
return NULL;
} }
walletInstance->SetMaxVersion(nMaxVersion); walletInstance->SetMaxVersion(nMaxVersion);
} }
@ -4520,10 +4538,7 @@ CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWall
if (walletInstance->GetKeyFromPool(newDefaultKey, false)) { if (walletInstance->GetKeyFromPool(newDefaultKey, false)) {
walletInstance->SetDefaultKey(newDefaultKey); walletInstance->SetDefaultKey(newDefaultKey);
if (!walletInstance->SetAddressBook(walletInstance->vchDefaultKey.GetID(), "", "receive")) if (!walletInstance->SetAddressBook(walletInstance->vchDefaultKey.GetID(), "", "receive"))
{ return UIError(_("Cannot write default address") += "\n");
errorString += _("Cannot write default address") += "\n";
return NULL;
}
} }
walletInstance->SetBestChain(chainActive.GetLocator()); walletInstance->SetBestChain(chainActive.GetLocator());
@ -4574,7 +4589,7 @@ CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWall
pindexRescan = chainActive.Genesis(); pindexRescan = chainActive.Genesis();
else else
{ {
CWalletDB walletdb(strWalletFile); CWalletDB walletdb(walletFile);
CBlockLocator locator; CBlockLocator locator;
if (walletdb.ReadBestBlock(locator)) if (walletdb.ReadBestBlock(locator))
pindexRescan = FindForkInGlobalIndex(chainActive, locator); pindexRescan = FindForkInGlobalIndex(chainActive, locator);
@ -4593,10 +4608,7 @@ CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWall
block = block->pprev; block = block->pprev;
if (pindexRescan != block) if (pindexRescan != block)
{ return UIError(_("Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)"));
errorString = _("Prune: last wallet synchronisation goes beyond pruned data. You need to -reindex (download the whole blockchain again in case of pruned node)");
return NULL;
}
} }
uiInterface.InitMessage(_("Rescanning...")); uiInterface.InitMessage(_("Rescanning..."));
@ -4610,7 +4622,7 @@ CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWall
// Restore wallet transaction metadata after -zapwallettxes=1 // Restore wallet transaction metadata after -zapwallettxes=1
if (GetBoolArg("-zapwallettxes", false) && GetArg("-zapwallettxes", "1") != "2") if (GetBoolArg("-zapwallettxes", false) && GetArg("-zapwallettxes", "1") != "2")
{ {
CWalletDB walletdb(strWalletFile); CWalletDB walletdb(walletFile);
BOOST_FOREACH(const CWalletTx& wtxOld, vWtx) BOOST_FOREACH(const CWalletTx& wtxOld, vWtx)
{ {
@ -4634,7 +4646,62 @@ CWallet* CWallet::InitLoadWallet(bool fDisableWallet, const std::string& strWall
} }
walletInstance->SetBroadcastTransactions(GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST)); walletInstance->SetBroadcastTransactions(GetBoolArg("-walletbroadcast", DEFAULT_WALLETBROADCAST));
return walletInstance; pwalletMain = walletInstance;
return true;
}
bool CWallet::ParameterInteraction()
{
if (mapArgs.count("-mintxfee"))
{
CAmount n = 0;
if (ParseMoney(mapArgs["-mintxfee"], n) && n > 0)
CWallet::minTxFee = CFeeRate(n);
else
return UIError(AmountErrMsg("mintxfee", mapArgs["-mintxfee"]));
}
if (mapArgs.count("-fallbackfee"))
{
CAmount nFeePerK = 0;
if (!ParseMoney(mapArgs["-fallbackfee"], nFeePerK))
return UIError(strprintf(_("Invalid amount for -fallbackfee=<amount>: '%s'"), mapArgs["-fallbackfee"]));
if (nFeePerK > HIGH_TX_FEE_PER_KB)
UIWarning(_("-fallbackfee is set very high! This is the transaction fee you may pay when fee estimates are not available."));
CWallet::fallbackFee = CFeeRate(nFeePerK);
}
if (mapArgs.count("-paytxfee"))
{
CAmount nFeePerK = 0;
if (!ParseMoney(mapArgs["-paytxfee"], nFeePerK))
return UIError(AmountErrMsg("paytxfee", mapArgs["-paytxfee"]));
if (nFeePerK > HIGH_TX_FEE_PER_KB)
UIWarning(_("-paytxfee is set very high! This is the transaction fee you will pay if you send a transaction."));
payTxFee = CFeeRate(nFeePerK, 1000);
if (payTxFee < ::minRelayTxFee)
{
return UIError(strprintf(_("Invalid amount for -paytxfee=<amount>: '%s' (must be at least %s)"),
mapArgs["-paytxfee"], ::minRelayTxFee.ToString()));
}
}
if (mapArgs.count("-maxtxfee"))
{
CAmount nMaxFee = 0;
if (!ParseMoney(mapArgs["-maxtxfee"], nMaxFee))
return UIError(AmountErrMsg("maxtxfee", mapArgs["-maxtxfee"]));
if (nMaxFee > HIGH_MAX_TX_FEE)
UIWarning(_("-maxtxfee is set very high! Fees this large could be paid on a single transaction."));
maxTxFee = nMaxFee;
if (CFeeRate(maxTxFee, 1000) < ::minRelayTxFee)
{
return UIError(strprintf(_("Invalid amount for -maxtxfee=<amount>: '%s' (must be at least the minrelay fee of %s to prevent stuck transactions)"),
mapArgs["-maxtxfee"], ::minRelayTxFee.ToString()));
}
}
nTxConfirmTarget = GetArg("-txconfirmtarget", DEFAULT_TX_CONFIRM_TARGET);
bSpendZeroConfChange = GetBoolArg("-spendzeroconfchange", DEFAULT_SPEND_ZEROCONF_CHANGE);
fSendFreeTransactions = GetBoolArg("-sendfreetransactions", DEFAULT_SEND_FREE_TRANSACTIONS);
return true;
} }
CKeyPool::CKeyPool() CKeyPool::CKeyPool()

View File

@ -33,6 +33,8 @@
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
extern CWallet* pwalletMain;
/** /**
* Settings * Settings
*/ */
@ -988,7 +990,7 @@ public:
void Flush(bool shutdown=false); void Flush(bool shutdown=false);
//! Verify the wallet database and perform salvage if required //! Verify the wallet database and perform salvage if required
static bool Verify(const std::string& walletFile, std::string& warningString, std::string& errorString); static bool Verify();
/** /**
* Address book entry changed. * Address book entry changed.
@ -1023,8 +1025,11 @@ public:
/* Returns the wallets help message */ /* Returns the wallets help message */
static std::string GetWalletHelpString(bool showDebug); static std::string GetWalletHelpString(bool showDebug);
/* initializes the wallet, returns a new CWallet instance or a null pointer in case of an error */ /* Initializes the wallet, returns a new CWallet instance or a null pointer in case of an error */
static CWallet* InitLoadWallet(bool fDisableWallet, const std::string& strWalletFile, std::string& warningString, std::string& errorString); static bool InitLoadWallet();
/* Wallets parameter interaction */
static bool ParameterInteraction();
/** /**
* HD Wallet Functions * HD Wallet Functions