diff --git a/doc/zsweep-consolidation.md b/doc/zsweep-consolidation.md index 0efe4dbb2..caf5bc70c 100644 --- a/doc/zsweep-consolidation.md +++ b/doc/zsweep-consolidation.md @@ -90,7 +90,7 @@ witness rebuilds. Consolidation keeps that in check. | consolidationbatchsize=15 | Most notes one consolidation transaction spends (default 15, clamped to 2..50). | | consolidationmaxtxs=3 | Most consolidation transactions per address per round (default 3, clamped to 1..100). | | consolidationtxfee=10000 | Fee in puposhis per consolidation transaction (default 10000 = 0.0001 DRAGONX; allowed range 1000..10000000, anything else falls back to the default). | - | consolidatesaplingaddress=zs1... | Only consolidate this address (may be given several times). The default is every address the wallet can spend. The wallet must hold the address's spending key, or dragonxd refuses to start. | + | consolidatesaplingaddress=zs1... | Only consolidate this address (may be given several times). The default is every address the wallet can spend. With `consolidation=1` the wallet must hold the address's spending key, or dragonxd refuses to start; with it off, such an address is ignored with a warning (and `z_consolidate` works on all addresses unless given its own). | 1. `dragonx-cli z_consolidationstatus` shows the settings, whether a round is running, when the next one is due, cumulative totals (`notes_consolidated`, `amount_consolidated` in puposhis), the last round (`last_round`: height, @@ -125,9 +125,10 @@ wallet: `N addresses have more than 25 spendable notes; consider -consolidation= 1. Then follow along with the zsweep section above if you want to set specific options for the zsweep behavior. -1. With zsweep on, consolidation only works on the sweep address (a `consolidatesaplingaddress` other than the - sweep address is refused at startup). Sweep and consolidation never run at the same time, and a sweep is not - started when a consolidation round is due within 5 blocks. +1. With zsweep on, consolidation only works on the sweep address: a `consolidatesaplingaddress` other than the + sweep address is refused at startup (with `consolidation=0` it is replaced by the sweep address, with a warning), + and `z_consolidate` refuses any other address too. Sweep and consolidation never run at the same time, and a + sweep is not started when a consolidation round is due within 5 blocks. ### Copyright diff --git a/src/init.cpp b/src/init.cpp index 60af3bce8..69a683dfa 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -2520,46 +2520,55 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) pwalletMain->sweepExcludeAddresses.push_back( vSweepExclude[i] ); } - if (pwalletMain->fSaplingConsolidationEnabled) { - //Validate 1 Consolidation address only that matches the sweep address - vector& vaddresses = mapMultiArgs["-consolidatesaplingaddress"]; - if (vaddresses.size() == 0) { - fConsolidateSweepAddress = true; - mapMultiArgs["-consolidatesaplingaddress"] = vSweep; - } else { - pwalletMain->consolidationAddress = vaddresses[0]; - for (int i = 0; i < vaddresses.size(); i++) { - if (vSweep[0] != vaddresses[i]) { - return InitError("Consolidation can only be used on the sweep address when sweep is enabled."); - } - } - } + // With sweep on, consolidation works on the sweep address only, also for z_consolidate with + // -consolidation off: the two would otherwise fight over the same notes and pay fees for nothing. + vector& vaddresses = mapMultiArgs["-consolidatesaplingaddress"]; + bool fOtherAddress = false; + for (size_t i = 0; i < vaddresses.size(); i++) + fOtherAddress |= vSweep[0] != vaddresses[i]; + if (fOtherAddress) { + if (pwalletMain->fSaplingConsolidationEnabled) + return InitError("Consolidation can only be used on the sweep address when sweep is enabled."); + InitWarning(_("-consolidatesaplingaddress is not the sweep address; with -zsweep, consolidation only works on the sweep address")); + } + if (vaddresses.empty() || fOtherAddress) { + fConsolidateSweepAddress = true; + vaddresses = vSweep; } } // The addresses consolidation works on: -consolidatesaplingaddress, or with -zsweep the sweep address. // A user-given address must be one this wallet can spend, else every round would silently skip it. + // With -consolidation off, where older versions ignored the option, a bad one is only warned about + // and dropped; z_consolidate then works on all addresses unless given its own. { const vector& vaddresses = mapMultiArgs["-consolidatesaplingaddress"]; - fConsolidationMapUsed = !vaddresses.empty(); pwalletMain->consolidationAddresses.clear(); + pwalletMain->consolidationAddress.clear(); for (size_t i = 0; i < vaddresses.size(); i++) { LogPrintf("Consolidating Sapling Address: %s\n", vaddresses[i]); + std::string strProblem; auto zAddress = DecodePaymentAddress(vaddresses[i]); if (!IsValidPaymentAddress(zAddress) || boost::get(&zAddress) == nullptr) { - return InitError("Invalid consolidation address: -consolidatesaplingaddress must be a Sapling z-address"); + strProblem = "Invalid consolidation address: -consolidatesaplingaddress must be a Sapling z-address"; + } else if (!boost::apply_visitor(HaveSpendingKeyForPaymentAddress(pwalletMain), zAddress)) { + if (!fConsolidateSweepAddress) + strProblem = strprintf("Wallet must hold the spending key of -consolidatesaplingaddress %s", vaddresses[i]); + else + LogPrintf("%s: the zsweep address is external to this wallet, so consolidation has nothing to do\n", __func__); } - if (!boost::apply_visitor(HaveSpendingKeyForPaymentAddress(pwalletMain), zAddress)) { - if (!fConsolidateSweepAddress) { - return InitError(strprintf("Wallet must hold the spending key of -consolidatesaplingaddress %s", vaddresses[i])); - } - LogPrintf("%s: the zsweep address is external to this wallet, so consolidation has nothing to do\n", __func__); + if (!strProblem.empty()) { + if (pwalletMain->fSaplingConsolidationEnabled) + return InitError(strProblem); + InitWarning(strProblem + "; ignoring it while -consolidation is off"); + continue; } pwalletMain->consolidationAddresses.push_back(boost::get(zAddress)); + if (pwalletMain->consolidationAddress.empty()) + pwalletMain->consolidationAddress = vaddresses[i]; } - if (!vaddresses.empty()) - pwalletMain->consolidationAddress = vaddresses[0]; + fConsolidationMapUsed = !pwalletMain->consolidationAddresses.empty(); } // Sweep and consolidation pick notes with GetFilteredNotes at minDepth > 1. Older versions diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index e9b0b2d22..fc419b4d7 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -3444,7 +3444,8 @@ UniValue z_consolidate(const UniValue& params, bool fHelp, const CPubKey& mypk) + strprintf("%d to %d", MIN_CONSOLIDATION_BATCHSIZE, MAX_CONSOLIDATION_BATCHSIZE) + "\n" "3. maxtxs (numeric, optional, default=-consolidationmaxtxs) most transactions per address, " + strprintf("1 to %d", MAX_CONSOLIDATION_MAXTXS) + "\n" - "4. addresses (array, optional, default=-consolidatesaplingaddress or all) Sapling addresses to consolidate\n" + "4. addresses (array, optional, default=-consolidatesaplingaddress or all) Sapling addresses to consolidate;\n" + " with -zsweep on, only the sweep address (also the default then)\n" "\nResult:\n" "\"operationid\" (string) An operationid to pass to z_getoperationstatus to get the result.\n" "\nExamples:\n" @@ -3501,6 +3502,9 @@ UniValue z_consolidate(const UniValue& params, bool fHelp, const CPubKey& mypk) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address, must be a Sapling z-address: " + str); if (!boost::apply_visitor(HaveSpendingKeyForPaymentAddress(pwalletMain), zaddr)) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Wallet does not hold the spending key of " + str); + // As at startup: with zsweep on, consolidating any other address only feeds the next sweep. + if (pwalletMain->fSweepEnabled && str != pwalletMain->sweepAddress) + throw JSONRPCError(RPC_INVALID_PARAMETER, "With -zsweep on, only the sweep address can be consolidated: " + str); request.addresses.push_back(boost::get(zaddr)); } }