Merge #11286: [depends] Don't build libevent sample code
[bitcoinplatinum.git] / src / wallet / init.cpp
blobc984df1df83dd0b32bbc7c22af8dfd4a0fa3e13b
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #include "wallet/init.h"
8 #include "net.h"
9 #include "util.h"
10 #include "utilmoneystr.h"
11 #include "validation.h"
12 #include "wallet/wallet.h"
13 #include "wallet/rpcwallet.h"
15 std::string GetWalletHelpString(bool showDebug)
17 std::string strUsage = HelpMessageGroup(_("Wallet options:"));
18 strUsage += HelpMessageOpt("-disablewallet", _("Do not load the wallet and disable wallet RPC calls"));
19 strUsage += HelpMessageOpt("-keypool=<n>", strprintf(_("Set key pool size to <n> (default: %u)"), DEFAULT_KEYPOOL_SIZE));
20 strUsage += HelpMessageOpt("-fallbackfee=<amt>", strprintf(_("A fee rate (in %s/kB) that will be used when fee estimation has insufficient data (default: %s)"),
21 CURRENCY_UNIT, FormatMoney(DEFAULT_FALLBACK_FEE)));
22 strUsage += HelpMessageOpt("-discardfee=<amt>", strprintf(_("The fee rate (in %s/kB) that indicates your tolerance for discarding change by adding it to the fee (default: %s). "
23 "Note: An output is discarded if it is dust at this rate, but we will always discard up to the dust relay fee and a discard fee above that is limited by the fee estimate for the longest target"),
24 CURRENCY_UNIT, FormatMoney(DEFAULT_DISCARD_FEE)));
25 strUsage += HelpMessageOpt("-mintxfee=<amt>", strprintf(_("Fees (in %s/kB) smaller than this are considered zero fee for transaction creation (default: %s)"),
26 CURRENCY_UNIT, FormatMoney(DEFAULT_TRANSACTION_MINFEE)));
27 strUsage += HelpMessageOpt("-paytxfee=<amt>", strprintf(_("Fee (in %s/kB) to add to transactions you send (default: %s)"),
28 CURRENCY_UNIT, FormatMoney(payTxFee.GetFeePerK())));
29 strUsage += HelpMessageOpt("-rescan", _("Rescan the block chain for missing wallet transactions on startup"));
30 strUsage += HelpMessageOpt("-salvagewallet", _("Attempt to recover private keys from a corrupt wallet on startup"));
31 strUsage += HelpMessageOpt("-spendzeroconfchange", strprintf(_("Spend unconfirmed change when sending transactions (default: %u)"), DEFAULT_SPEND_ZEROCONF_CHANGE));
32 strUsage += HelpMessageOpt("-txconfirmtarget=<n>", strprintf(_("If paytxfee is not set, include enough fee so transactions begin confirmation on average within n blocks (default: %u)"), DEFAULT_TX_CONFIRM_TARGET));
33 strUsage += HelpMessageOpt("-walletrbf", strprintf(_("Send transactions with full-RBF opt-in enabled (default: %u)"), DEFAULT_WALLET_RBF));
34 strUsage += HelpMessageOpt("-upgradewallet", _("Upgrade wallet to latest format on startup"));
35 strUsage += HelpMessageOpt("-wallet=<file>", _("Specify wallet file (within data directory)") + " " + strprintf(_("(default: %s)"), DEFAULT_WALLET_DAT));
36 strUsage += HelpMessageOpt("-walletbroadcast", _("Make the wallet broadcast transactions") + " " + strprintf(_("(default: %u)"), DEFAULT_WALLETBROADCAST));
37 strUsage += HelpMessageOpt("-walletnotify=<cmd>", _("Execute command when a wallet transaction changes (%s in cmd is replaced by TxID)"));
38 strUsage += HelpMessageOpt("-zapwallettxes=<mode>", _("Delete all wallet transactions and only recover those parts of the blockchain through -rescan on startup") +
39 " " + _("(1 = keep tx meta data e.g. account owner and payment request information, 2 = drop tx meta data)"));
41 if (showDebug)
43 strUsage += HelpMessageGroup(_("Wallet debugging/testing options:"));
45 strUsage += HelpMessageOpt("-dblogsize=<n>", strprintf("Flush wallet database activity from memory to disk log every <n> megabytes (default: %u)", DEFAULT_WALLET_DBLOGSIZE));
46 strUsage += HelpMessageOpt("-flushwallet", strprintf("Run a thread to flush wallet periodically (default: %u)", DEFAULT_FLUSHWALLET));
47 strUsage += HelpMessageOpt("-privdb", strprintf("Sets the DB_PRIVATE flag in the wallet db environment (default: %u)", DEFAULT_WALLET_PRIVDB));
48 strUsage += HelpMessageOpt("-walletrejectlongchains", strprintf(_("Wallet will not create transactions that violate mempool chain limits (default: %u)"), DEFAULT_WALLET_REJECT_LONG_CHAINS));
51 return strUsage;
54 bool WalletParameterInteraction()
56 gArgs.SoftSetArg("-wallet", DEFAULT_WALLET_DAT);
57 const bool is_multiwallet = gArgs.GetArgs("-wallet").size() > 1;
59 if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET))
60 return true;
62 if (gArgs.GetBoolArg("-blocksonly", DEFAULT_BLOCKSONLY) && gArgs.SoftSetBoolArg("-walletbroadcast", false)) {
63 LogPrintf("%s: parameter interaction: -blocksonly=1 -> setting -walletbroadcast=0\n", __func__);
66 if (gArgs.GetBoolArg("-salvagewallet", false)) {
67 if (is_multiwallet) {
68 return InitError(strprintf("%s is only allowed with a single wallet file", "-salvagewallet"));
70 // Rewrite just private keys: rescan to find transactions
71 if (gArgs.SoftSetBoolArg("-rescan", true)) {
72 LogPrintf("%s: parameter interaction: -salvagewallet=1 -> setting -rescan=1\n", __func__);
76 int zapwallettxes = gArgs.GetArg("-zapwallettxes", 0);
77 // -zapwallettxes implies dropping the mempool on startup
78 if (zapwallettxes != 0 && gArgs.SoftSetBoolArg("-persistmempool", false)) {
79 LogPrintf("%s: parameter interaction: -zapwallettxes=%s -> setting -persistmempool=0\n", __func__, zapwallettxes);
82 // -zapwallettxes implies a rescan
83 if (zapwallettxes != 0) {
84 if (is_multiwallet) {
85 return InitError(strprintf("%s is only allowed with a single wallet file", "-zapwallettxes"));
87 if (gArgs.SoftSetBoolArg("-rescan", true)) {
88 LogPrintf("%s: parameter interaction: -zapwallettxes=%s -> setting -rescan=1\n", __func__, zapwallettxes);
92 if (is_multiwallet) {
93 if (gArgs.GetBoolArg("-upgradewallet", false)) {
94 return InitError(strprintf("%s is only allowed with a single wallet file", "-upgradewallet"));
98 if (gArgs.GetBoolArg("-sysperms", false))
99 return InitError("-sysperms is not allowed in combination with enabled wallet functionality");
100 if (gArgs.GetArg("-prune", 0) && gArgs.GetBoolArg("-rescan", false))
101 return InitError(_("Rescans are not possible in pruned mode. You will need to use -reindex which will download the whole blockchain again."));
103 if (::minRelayTxFee.GetFeePerK() > HIGH_TX_FEE_PER_KB)
104 InitWarning(AmountHighWarn("-minrelaytxfee") + " " +
105 _("The wallet will avoid paying less than the minimum relay fee."));
107 if (gArgs.IsArgSet("-mintxfee"))
109 CAmount n = 0;
110 if (!ParseMoney(gArgs.GetArg("-mintxfee", ""), n) || 0 == n)
111 return InitError(AmountErrMsg("mintxfee", gArgs.GetArg("-mintxfee", "")));
112 if (n > HIGH_TX_FEE_PER_KB)
113 InitWarning(AmountHighWarn("-mintxfee") + " " +
114 _("This is the minimum transaction fee you pay on every transaction."));
115 CWallet::minTxFee = CFeeRate(n);
117 if (gArgs.IsArgSet("-fallbackfee"))
119 CAmount nFeePerK = 0;
120 if (!ParseMoney(gArgs.GetArg("-fallbackfee", ""), nFeePerK))
121 return InitError(strprintf(_("Invalid amount for -fallbackfee=<amount>: '%s'"), gArgs.GetArg("-fallbackfee", "")));
122 if (nFeePerK > HIGH_TX_FEE_PER_KB)
123 InitWarning(AmountHighWarn("-fallbackfee") + " " +
124 _("This is the transaction fee you may pay when fee estimates are not available."));
125 CWallet::fallbackFee = CFeeRate(nFeePerK);
127 if (gArgs.IsArgSet("-discardfee"))
129 CAmount nFeePerK = 0;
130 if (!ParseMoney(gArgs.GetArg("-discardfee", ""), nFeePerK))
131 return InitError(strprintf(_("Invalid amount for -discardfee=<amount>: '%s'"), gArgs.GetArg("-discardfee", "")));
132 if (nFeePerK > HIGH_TX_FEE_PER_KB)
133 InitWarning(AmountHighWarn("-discardfee") + " " +
134 _("This is the transaction fee you may discard if change is smaller than dust at this level"));
135 CWallet::m_discard_rate = CFeeRate(nFeePerK);
137 if (gArgs.IsArgSet("-paytxfee"))
139 CAmount nFeePerK = 0;
140 if (!ParseMoney(gArgs.GetArg("-paytxfee", ""), nFeePerK))
141 return InitError(AmountErrMsg("paytxfee", gArgs.GetArg("-paytxfee", "")));
142 if (nFeePerK > HIGH_TX_FEE_PER_KB)
143 InitWarning(AmountHighWarn("-paytxfee") + " " +
144 _("This is the transaction fee you will pay if you send a transaction."));
146 payTxFee = CFeeRate(nFeePerK, 1000);
147 if (payTxFee < ::minRelayTxFee)
149 return InitError(strprintf(_("Invalid amount for -paytxfee=<amount>: '%s' (must be at least %s)"),
150 gArgs.GetArg("-paytxfee", ""), ::minRelayTxFee.ToString()));
153 if (gArgs.IsArgSet("-maxtxfee"))
155 CAmount nMaxFee = 0;
156 if (!ParseMoney(gArgs.GetArg("-maxtxfee", ""), nMaxFee))
157 return InitError(AmountErrMsg("maxtxfee", gArgs.GetArg("-maxtxfee", "")));
158 if (nMaxFee > HIGH_MAX_TX_FEE)
159 InitWarning(_("-maxtxfee is set very high! Fees this large could be paid on a single transaction."));
160 maxTxFee = nMaxFee;
161 if (CFeeRate(maxTxFee, 1000) < ::minRelayTxFee)
163 return InitError(strprintf(_("Invalid amount for -maxtxfee=<amount>: '%s' (must be at least the minrelay fee of %s to prevent stuck transactions)"),
164 gArgs.GetArg("-maxtxfee", ""), ::minRelayTxFee.ToString()));
167 nTxConfirmTarget = gArgs.GetArg("-txconfirmtarget", DEFAULT_TX_CONFIRM_TARGET);
168 bSpendZeroConfChange = gArgs.GetBoolArg("-spendzeroconfchange", DEFAULT_SPEND_ZEROCONF_CHANGE);
169 fWalletRbf = gArgs.GetBoolArg("-walletrbf", DEFAULT_WALLET_RBF);
171 return true;
174 void RegisterWalletRPC(CRPCTable &t)
176 if (gArgs.GetBoolArg("-disablewallet", false)) return;
178 RegisterWalletRPCCommands(t);
181 bool VerifyWallets()
183 if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET))
184 return true;
186 uiInterface.InitMessage(_("Verifying wallet(s)..."));
188 // Keep track of each wallet absolute path to detect duplicates.
189 std::set<fs::path> wallet_paths;
191 for (const std::string& walletFile : gArgs.GetArgs("-wallet")) {
192 if (boost::filesystem::path(walletFile).filename() != walletFile) {
193 return InitError(strprintf(_("Error loading wallet %s. -wallet parameter must only specify a filename (not a path)."), walletFile));
196 if (SanitizeString(walletFile, SAFE_CHARS_FILENAME) != walletFile) {
197 return InitError(strprintf(_("Error loading wallet %s. Invalid characters in -wallet filename."), walletFile));
200 fs::path wallet_path = fs::absolute(walletFile, GetDataDir());
202 if (fs::exists(wallet_path) && (!fs::is_regular_file(wallet_path) || fs::is_symlink(wallet_path))) {
203 return InitError(strprintf(_("Error loading wallet %s. -wallet filename must be a regular file."), walletFile));
206 if (!wallet_paths.insert(wallet_path).second) {
207 return InitError(strprintf(_("Error loading wallet %s. Duplicate -wallet filename specified."), walletFile));
210 std::string strError;
211 if (!CWalletDB::VerifyEnvironment(walletFile, GetDataDir().string(), strError)) {
212 return InitError(strError);
215 if (gArgs.GetBoolArg("-salvagewallet", false)) {
216 // Recover readable keypairs:
217 CWallet dummyWallet;
218 std::string backup_filename;
219 if (!CWalletDB::Recover(walletFile, (void *)&dummyWallet, CWalletDB::RecoverKeysOnlyFilter, backup_filename)) {
220 return false;
224 std::string strWarning;
225 bool dbV = CWalletDB::VerifyDatabaseFile(walletFile, GetDataDir().string(), strWarning, strError);
226 if (!strWarning.empty()) {
227 InitWarning(strWarning);
229 if (!dbV) {
230 InitError(strError);
231 return false;
235 return true;
238 bool OpenWallets()
240 if (gArgs.GetBoolArg("-disablewallet", DEFAULT_DISABLE_WALLET)) {
241 LogPrintf("Wallet disabled!\n");
242 return true;
245 for (const std::string& walletFile : gArgs.GetArgs("-wallet")) {
246 CWallet * const pwallet = CWallet::CreateWalletFromFile(walletFile);
247 if (!pwallet) {
248 return false;
250 vpwallets.push_back(pwallet);
253 return true;
256 void StartWallets(CScheduler& scheduler) {
257 for (CWalletRef pwallet : vpwallets) {
258 pwallet->postInitProcess(scheduler);
262 void FlushWallets() {
263 for (CWalletRef pwallet : vpwallets) {
264 pwallet->Flush(false);
268 void StopWallets() {
269 for (CWalletRef pwallet : vpwallets) {
270 pwallet->Flush(true);
274 void CloseWallets() {
275 for (CWalletRef pwallet : vpwallets) {
276 delete pwallet;
278 vpwallets.clear();