diff --git a/src/interfaces/chain.h b/src/interfaces/chain.h --- a/src/interfaces/chain.h +++ b/src/interfaces/chain.h @@ -166,6 +166,15 @@ //! Get adjusted time. virtual int64_t getAdjustedTime() = 0; + + //! Send init message. + virtual void initMessage(const std::string &message) = 0; + + //! Send init warning. + virtual void initWarning(const std::string &message) = 0; + + //! Send init error. + virtual void initError(const std::string &message) = 0; }; //! Interface to let node manage chain clients (wallets, or maybe tools for diff --git a/src/interfaces/chain.cpp b/src/interfaces/chain.cpp --- a/src/interfaces/chain.cpp +++ b/src/interfaces/chain.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include @@ -233,6 +234,15 @@ bool getPruneMode() override { return ::fPruneMode; } bool p2pEnabled() override { return g_connman != nullptr; } int64_t getAdjustedTime() override { return GetAdjustedTime(); } + void initMessage(const std::string &message) override { + ::uiInterface.InitMessage(message); + } + void initWarning(const std::string &message) override { + InitWarning(message); + } + void initError(const std::string &message) override { + InitError(message); + } }; } // namespace diff --git a/src/wallet/init.cpp b/src/wallet/init.cpp --- a/src/wallet/init.cpp +++ b/src/wallet/init.cpp @@ -260,19 +260,22 @@ // environment instances for the same directory fs::path canonical_wallet_dir = fs::canonical(wallet_dir, error); if (error || !fs::exists(wallet_dir)) { - return InitError( + chain.initError( strprintf(_("Specified -walletdir \"%s\" does not exist"), wallet_dir.string())); + return false; } else if (!fs::is_directory(wallet_dir)) { - return InitError( + chain.initError( strprintf(_("Specified -walletdir \"%s\" is not a directory"), wallet_dir.string())); + return false; // The canonical path transforms relative paths into absolute ones, // so we check the non-canonical version } else if (!wallet_dir.is_absolute()) { - return InitError( + chain.initError( strprintf(_("Specified -walletdir \"%s\" is a relative path"), wallet_dir.string())); + return false; } gArgs.ForceSetArg("-walletdir", canonical_wallet_dir.string()); } @@ -294,9 +297,10 @@ WalletLocation location(wallet_file); if (!wallet_paths.insert(location.GetPath()).second) { - return InitError(strprintf(_("Error loading wallet %s. Duplicate " - "-wallet filename specified."), - wallet_file)); + chain.initError(strprintf(_("Error loading wallet %s. Duplicate " + "-wallet filename specified."), + wallet_file)); + return false; } std::string error_string; @@ -305,10 +309,10 @@ CWallet::Verify(chainParams, chain, location, salvage_wallet, error_string, warning_string); if (!error_string.empty()) { - InitError(error_string); + chain.initError(error_string); } if (!warning_string.empty()) { - InitWarning(warning_string); + chain.initWarning(warning_string); } if (!verify_success) { return false; diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -4436,20 +4436,20 @@ std::vector vWtx; if (gArgs.GetBoolArg("-zapwallettxes", false)) { - uiInterface.InitMessage(_("Zapping all transactions from wallet...")); + chain.initMessage(_("Zapping all transactions from wallet...")); std::unique_ptr tempWallet = std::make_unique( chainParams, chain, location, WalletDatabase::Create(location.GetPath())); DBErrors nZapWalletRet = tempWallet->ZapWalletTx(vWtx); if (nZapWalletRet != DBErrors::LOAD_OK) { - InitError( + chain.initError( strprintf(_("Error loading %s: Wallet corrupted"), walletFile)); return nullptr; } } - uiInterface.InitMessage(_("Loading wallet...")); + chain.initMessage(_("Loading wallet...")); int64_t nStart = GetTimeMillis(); bool fFirstRun = true; @@ -4462,29 +4462,29 @@ DBErrors nLoadWalletRet = walletInstance->LoadWallet(fFirstRun); if (nLoadWalletRet != DBErrors::LOAD_OK) { if (nLoadWalletRet == DBErrors::CORRUPT) { - InitError( + chain.initError( strprintf(_("Error loading %s: Wallet corrupted"), walletFile)); return nullptr; } if (nLoadWalletRet == DBErrors::NONCRITICAL_ERROR) { - InitWarning(strprintf( + chain.initError(strprintf( _("Error reading %s! All keys read correctly, but transaction " "data" " or address book entries might be missing or incorrect."), walletFile)); } else if (nLoadWalletRet == DBErrors::TOO_NEW) { - InitError(strprintf( + chain.initError(strprintf( _("Error loading %s: Wallet requires newer version of %s"), walletFile, PACKAGE_NAME)); return nullptr; } else if (nLoadWalletRet == DBErrors::NEED_REWRITE) { - InitError(strprintf( + chain.initError(strprintf( _("Wallet needed to be rewritten: restart %s to complete"), PACKAGE_NAME)); return nullptr; } else { - InitError(strprintf(_("Error loading %s"), walletFile)); + chain.initError(strprintf(_("Error loading %s"), walletFile)); return nullptr; } } @@ -4505,7 +4505,7 @@ } if (nMaxVersion < walletInstance->GetVersion()) { - InitError(_("Cannot downgrade wallet")); + chain.initError(_("Cannot downgrade wallet")); return nullptr; } @@ -4522,7 +4522,7 @@ if (!walletInstance->CanSupportFeature(FEATURE_HD_SPLIT) && max_version >= FEATURE_HD_SPLIT && max_version < FEATURE_PRE_SPLIT_KEYPOOL) { - InitError( + chain.initError( _("Cannot upgrade a non HD split wallet without upgrading to " "support pre split keypool. Please use -upgradewallet=200300 " "or -upgradewallet with no version specified.")); @@ -4555,7 +4555,7 @@ // Regenerate the keypool if upgraded to HD if (hd_upgrade) { if (!walletInstance->TopUpKeyPool()) { - InitError(_("Unable to generate keys")); + chain.initError(_("Unable to generate keys")); return nullptr; } } @@ -4580,7 +4580,7 @@ // Top up the keypool if (walletInstance->CanGenerateKeys() && !walletInstance->TopUpKeyPool()) { - InitError(_("Unable to generate initial keys")); + chain.initError(_("Unable to generate initial keys")); return nullptr; } @@ -4589,18 +4589,20 @@ walletInstance->ChainStateFlushed(locked_chain->getLocator()); } else if (wallet_creation_flags & WALLET_FLAG_DISABLE_PRIVATE_KEYS) { // Make it impossible to disable private keys after creation - InitError(strprintf(_("Error loading %s: Private keys can only be " - "disabled during creation"), - walletFile)); + chain.initError( + strprintf(_("Error loading %s: Private keys can only be " + "disabled during creation"), + walletFile)); return nullptr; } else if (walletInstance->IsWalletFlagSet( WALLET_FLAG_DISABLE_PRIVATE_KEYS)) { LOCK(walletInstance->cs_KeyStore); if (!walletInstance->mapKeys.empty() || !walletInstance->mapCryptedKeys.empty()) { - InitWarning(strprintf(_("Warning: Private keys detected in wallet " - "{%s} with disabled private keys"), - walletFile)); + chain.initWarning( + strprintf(_("Warning: Private keys detected in wallet " + "{%s} with disabled private keys"), + walletFile)); } } @@ -4608,13 +4610,15 @@ Amount n = Amount::zero(); if (!ParseMoney(gArgs.GetArg("-mintxfee", ""), n) || n == Amount::zero()) { - InitError(AmountErrMsg("mintxfee", gArgs.GetArg("-mintxfee", ""))); + chain.initError( + AmountErrMsg("mintxfee", gArgs.GetArg("-mintxfee", ""))); return nullptr; } if (n > HIGH_TX_FEE_PER_KB) { - InitWarning(AmountHighWarn("-mintxfee") + " " + - _("This is the minimum transaction fee you pay on " - "every transaction.")); + chain.initWarning( + AmountHighWarn("-mintxfee") + " " + + _("This is the minimum transaction fee you pay on " + "every transaction.")); } walletInstance->m_min_fee = CFeeRate(n); } @@ -4622,15 +4626,16 @@ if (gArgs.IsArgSet("-fallbackfee")) { Amount nFeePerK = Amount::zero(); if (!ParseMoney(gArgs.GetArg("-fallbackfee", ""), nFeePerK)) { - InitError( + chain.initError( strprintf(_("Invalid amount for -fallbackfee=: '%s'"), gArgs.GetArg("-fallbackfee", ""))); return nullptr; } if (nFeePerK > HIGH_TX_FEE_PER_KB) { - InitWarning(AmountHighWarn("-fallbackfee") + " " + - _("This is the transaction fee you may pay when fee " - "estimates are not available.")); + chain.initWarning( + AmountHighWarn("-fallbackfee") + " " + + _("This is the transaction fee you may pay when fee " + "estimates are not available.")); } walletInstance->m_fallback_fee = CFeeRate(nFeePerK); // disable fallback fee in case value was set to 0, enable if non-null @@ -4640,20 +4645,22 @@ if (gArgs.IsArgSet("-paytxfee")) { Amount nFeePerK = Amount::zero(); if (!ParseMoney(gArgs.GetArg("-paytxfee", ""), nFeePerK)) { - InitError(AmountErrMsg("paytxfee", gArgs.GetArg("-paytxfee", ""))); + chain.initError( + AmountErrMsg("paytxfee", gArgs.GetArg("-paytxfee", ""))); return nullptr; } if (nFeePerK > HIGH_TX_FEE_PER_KB) { - InitWarning(AmountHighWarn("-paytxfee") + " " + - _("This is the transaction fee you will pay if you " - "send a transaction.")); + chain.initWarning( + AmountHighWarn("-paytxfee") + " " + + _("This is the transaction fee you will pay if you " + "send a transaction.")); } walletInstance->m_pay_tx_fee = CFeeRate(nFeePerK, 1000); if (walletInstance->m_pay_tx_fee < ::minRelayTxFee) { - InitError(strprintf(_("Invalid amount for -paytxfee=: '%s' " - "(must be at least %s)"), - gArgs.GetArg("-paytxfee", ""), - ::minRelayTxFee.ToString())); + chain.initError(strprintf( + _("Invalid amount for -paytxfee=: '%s' " + "(must be at least %s)"), + gArgs.GetArg("-paytxfee", ""), ::minRelayTxFee.ToString())); return nullptr; } } @@ -4706,14 +4713,15 @@ } if (rescan_height != block_height) { - InitError(_("Prune: last wallet synchronisation goes beyond " - "pruned data. You need to -reindex (download the " - "whole blockchain again in case of pruned node)")); + chain.initError( + _("Prune: last wallet synchronisation goes beyond " + "pruned data. You need to -reindex (download the " + "whole blockchain again in case of pruned node)")); return nullptr; } } - uiInterface.InitMessage(_("Rescanning...")); + chain.initMessage(_("Rescanning...")); walletInstance->WalletLogPrintf( "Rescanning last %i blocks (from block %i)...\n", *tip_height - rescan_height, rescan_height); @@ -4739,7 +4747,7 @@ locked_chain->getBlockHash(rescan_height), BlockHash(), reserver, true /* update */) .status)) { - InitError( + chain.initError( _("Failed to rescan the wallet during initialization")); return nullptr; }