diff --git a/src/rpc/client.cpp b/src/rpc/client.cpp index 14a8db459..90013a070 100644 --- a/src/rpc/client.cpp +++ b/src/rpc/client.cpp @@ -152,6 +152,10 @@ static const CRPCConvertParam vRPCConvertParams[] = { "z_shieldcoinbase", 3}, { "z_getoperationstatus", 0}, { "z_consolidationstatus", 0}, + { "z_consolidate", 0}, + { "z_consolidate", 1}, + { "z_consolidate", 2}, + { "z_consolidate", 3}, { "z_getoperationresult", 0}, { "notaries", 2 }, { "minerids", 1 }, diff --git a/src/rpc/server.h b/src/rpc/server.h index a2d5c17cc..1ceeaff99 100644 --- a/src/rpc/server.h +++ b/src/rpc/server.h @@ -368,6 +368,7 @@ extern UniValue z_sendmany(const UniValue& params, bool fHelp, const CPubKey& my extern UniValue z_sweepstatus(const UniValue& params, bool fHelp, const CPubKey& mypk); // in rpcwallet.cpp extern UniValue z_autoshieldstatus(const UniValue& params, bool fHelp, const CPubKey& mypk); // in rpcwallet.cpp extern UniValue z_consolidationstatus(const UniValue& params, bool fHelp, const CPubKey& mypk); // in rpcwallet.cpp +extern UniValue z_consolidate(const UniValue& params, bool fHelp, const CPubKey& mypk); // in rpcwallet.cpp extern UniValue z_shieldcoinbase(const UniValue& params, bool fHelp, const CPubKey& mypk); // in rpcwallet.cpp extern UniValue z_getoperationstatus(const UniValue& params, bool fHelp, const CPubKey& mypk); // in rpcwallet.cpp extern UniValue z_getoperationresult(const UniValue& params, bool fHelp, const CPubKey& mypk); // in rpcwallet.cpp diff --git a/src/wallet/asyncrpcoperation_saplingconsolidation.cpp b/src/wallet/asyncrpcoperation_saplingconsolidation.cpp index 135e29fbd..1ef89f9ce 100644 --- a/src/wallet/asyncrpcoperation_saplingconsolidation.cpp +++ b/src/wallet/asyncrpcoperation_saplingconsolidation.cpp @@ -153,6 +153,7 @@ void AsyncRPCOperation_saplingconsolidation::main() { SaplingConsolidationRound round; round.height = tipHeight; + round.manual = request_.manual; round.time = GetTime(); round.txids = tally_.txids; round.notesMerged = tally_.notesMerged; @@ -175,7 +176,9 @@ static std::set setWitnessSkipLogged; bool AsyncRPCOperation_saplingconsolidation::main_impl() { bool status=true; auto opid=getId(); - LogPrintf("%s: Beginning AsyncRPCOperation_saplingconsolidation\n", opid); + LogPrintf("%s: Beginning AsyncRPCOperation_saplingconsolidation%s (threshold=%d batchsize=%d maxtxs=%d fee=%d)\n", + opid, request_.manual ? " (z_consolidate)" : "", request_.policy.threshold, + request_.policy.batchSize, request_.policy.maxTxs, (int)request_.fee); auto consensusParams = Params().GetConsensus(); int tipHeight; { @@ -460,6 +463,7 @@ UniValue AsyncRPCOperation_saplingconsolidation::getStatus() const { UniValue obj = v.get_obj(); obj.push_back(Pair("method", "saplingconsolidation")); obj.push_back(Pair("target_height", targetHeight_)); + obj.push_back(Pair("manual", request_.manual)); return obj; } diff --git a/src/wallet/asyncrpcoperation_saplingconsolidation.h b/src/wallet/asyncrpcoperation_saplingconsolidation.h index 71eb6245d..8a5fc72b2 100644 --- a/src/wallet/asyncrpcoperation_saplingconsolidation.h +++ b/src/wallet/asyncrpcoperation_saplingconsolidation.h @@ -56,6 +56,7 @@ std::vector PlanSaplingConsolidation( // What a consolidation operation is asked to do, fixed when it is enqueued. struct SaplingConsolidationRequest { + bool manual = false; // z_consolidate rather than the -consolidation scheduler CAmount fee = DEFAULT_CONSOLIDATION_FEE; SaplingConsolidationPolicy policy; std::vector addresses; // empty: every address the wallet can spend diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index e214be8ab..1a254d597 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -42,6 +42,7 @@ #include "wallet/asyncrpcoperation_mergetoaddress.h" #include "wallet/asyncrpcoperation_sendmany.h" #include "wallet/asyncrpcoperation_shieldcoinbase.h" +#include "wallet/asyncrpcoperation_saplingconsolidation.h" #include "consensus/upgrades.h" #include "sodium.h" #include @@ -3325,6 +3326,7 @@ UniValue z_consolidationstatus(const UniValue& params, bool fHelp, const CPubKey " \"notes_consolidated\" : n, (numeric) notes merged since startup\n" " \"last_round\" : { (object, null before the first round)\n" " \"height\" : n, \"time\" : n, (numeric) tip height and time when it finished\n" + " \"manual\" : true|false, (boolean) run by z_consolidate\n" " \"txids\" : [...], (array) the transactions it sent\n" " \"notes_merged\" : n, (numeric)\n" " \"amount_merged\" : n, (numeric) value of the notes spent, in puposhis\n" @@ -3373,6 +3375,7 @@ UniValue z_consolidationstatus(const UniValue& params, bool fHelp, const CPubKey UniValue last(UniValue::VOBJ); last.push_back(Pair("height", r.height)); last.push_back(Pair("time", r.time)); + last.push_back(Pair("manual", r.manual)); UniValue txids(UniValue::VARR); for (const std::string& txid : r.txids) txids.push_back(txid); @@ -3413,6 +3416,107 @@ UniValue z_consolidationstatus(const UniValue& params, bool fHelp, const CPubKey return ret; } +UniValue z_consolidate(const UniValue& params, bool fHelp, const CPubKey& mypk) +{ + if (!EnsureWalletIsAvailable(fHelp)) + return NullUniValue; + + if (fHelp || params.size() > 4) + throw runtime_error( + "z_consolidate ( fee batchsize maxtxs [\"zaddr\",...] )\n" + "\nRun one Sapling consolidation round now, with the -consolidation settings, also when\n" + "-consolidation is off. Every address holding more than -consolidationthreshold eligible notes\n" + "(unspent, unlocked, at least 11 confirmations) gets up to maxtxs transactions, each spending up to\n" + "batchsize of its smallest notes back to that same address, plus 7 zero-value dummy outputs.\n" + "Pass null for an argument to keep its setting.\n" + "\nArguments:\n" + "1. fee (numeric, optional, default=-consolidationtxfee) fee per transaction in " + CURRENCY_UNIT + "\n" + "2. batchsize (numeric, optional, default=-consolidationbatchsize) most notes per transaction, " + + 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" + "\nResult:\n" + "\"operationid\" (string) An operationid to pass to z_getoperationstatus to get the result.\n" + "\nExamples:\n" + + HelpExampleCli("z_consolidate", "") + + HelpExampleCli("z_consolidate", "null 50 20 '[\"zs1...\"]'") + + HelpExampleRpc("z_consolidate", "") + ); + + LOCK2(cs_main, pwalletMain->cs_wallet); + + EnsureWalletIsUnlocked(); + if (AutoWalletOpsSyncing()) + throw JSONRPCError(RPC_CLIENT_IN_INITIAL_DOWNLOAD, "Node is syncing; try again once it has caught up"); + if (pwalletMain->fConsolidationRunning) + throw JSONRPCError(RPC_WALLET_ERROR, "A consolidation round is already running; see z_consolidationstatus"); + if (pwalletMain->fSweepRunning || pwalletMain->fAutoShieldRunning) + throw JSONRPCError(RPC_WALLET_ERROR, strprintf("A %s round is running; try again when it finishes", + pwalletMain->fSweepRunning ? "z_sweep" : "autoshield")); + + SaplingConsolidationRequest request; + request.manual = true; + request.fee = fConsolidationTxFee; + request.policy.threshold = pwalletMain->consolidationThreshold; + request.policy.batchSize = pwalletMain->consolidationBatchSize; + request.policy.maxTxs = pwalletMain->consolidationMaxTxs; + request.addresses = pwalletMain->consolidationAddresses; + + if (params.size() > 0 && !params[0].isNull()) { + request.fee = AmountFromValue(params[0]); + if (request.fee < CONSOLIDATION_MIN_FEE || request.fee > CONSOLIDATION_MAX_FEE) + throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid fee: must be from %s to %s", + FormatMoney(CONSOLIDATION_MIN_FEE), FormatMoney(CONSOLIDATION_MAX_FEE))); + } + if (params.size() > 1 && !params[1].isNull()) { + request.policy.batchSize = params[1].get_int(); + if (request.policy.batchSize < MIN_CONSOLIDATION_BATCHSIZE || request.policy.batchSize > MAX_CONSOLIDATION_BATCHSIZE) + throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid batchsize: must be from %d to %d", + MIN_CONSOLIDATION_BATCHSIZE, MAX_CONSOLIDATION_BATCHSIZE)); + } + if (params.size() > 2 && !params[2].isNull()) { + request.policy.maxTxs = params[2].get_int(); + if (request.policy.maxTxs < 1 || request.policy.maxTxs > MAX_CONSOLIDATION_MAXTXS) + throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("Invalid maxtxs: must be from 1 to %d", MAX_CONSOLIDATION_MAXTXS)); + } + if (params.size() > 3 && !params[3].isNull()) { + UniValue addresses = params[3].get_array(); + if (addresses.size() == 0) + throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid addresses: the array is empty"); + request.addresses.clear(); + for (size_t i = 0; i < addresses.size(); i++) { + const std::string& str = addresses[i].get_str(); + auto zaddr = DecodePaymentAddress(str); + if (!IsValidPaymentAddress(zaddr) || boost::get(&zaddr) == nullptr) + 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); + request.addresses.push_back(boost::get(zaddr)); + } + } + + // Take the scheduler's slot exactly as RunSaplingConsolidation does, so sweep and autoshield + // defer to this round and its completion releases the flag. + pwalletMain->fConsolidationRunning = true; + std::shared_ptr q = getAsyncRPCQueue(); + std::shared_ptr lastOperation = q->getOperationForId(pwalletMain->saplingConsolidationOperationId); + if (lastOperation != nullptr) { + lastOperation->cancel(); + if (!pwalletMain->saplingConsolidationOpManual) + q->popOperationForId(pwalletMain->saplingConsolidationOperationId); + } + std::shared_ptr operation( + new AsyncRPCOperation_saplingconsolidation(chainActive.Height() + AUTO_OP_TARGET_HEIGHT_OFFSET, request)); + pwalletMain->saplingConsolidationOperationId = operation->getId(); + pwalletMain->saplingConsolidationOpManual = true; + if (!q->addOperation(operation)) { + pwalletMain->fConsolidationRunning = false; + throw JSONRPCError(RPC_INTERNAL_ERROR, "The async queue is not accepting operations (shutting down?)"); + } + return operation->getId(); +} + UniValue z_sweepstatus(const UniValue& params, bool fHelp, const CPubKey& mypk) { @@ -6569,6 +6673,7 @@ static const CRPCCommand commands[] = { "wallet", "z_sweepstatus", &z_sweepstatus, true }, { "wallet", "z_autoshieldstatus", &z_autoshieldstatus, true }, { "wallet", "z_consolidationstatus", &z_consolidationstatus, true }, + { "wallet", "z_consolidate", &z_consolidate, true }, { "wallet", "z_sendmany", &z_sendmany, false }, { "wallet", "z_shieldcoinbase", &z_shieldcoinbase, false }, { "wallet", "z_getoperationstatus", &z_getoperationstatus, true }, diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index 1f2cac79d..c3b52c7f3 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -738,8 +738,10 @@ void CWallet::RunSaplingConsolidation(int blockHeight) { lastOperation->cancel(); // Drop it from the queue's map as well. Nothing else ever removes these: // popOperationForId is only reached from z_getoperationresult, so on a node - // running this every interval the map grew without bound. - q->popOperationForId(saplingConsolidationOperationId); + // running this every interval the map grew without bound. A z_consolidate + // round is the user's: its result stays until z_getoperationresult takes it. + if (!saplingConsolidationOpManual) + q->popOperationForId(saplingConsolidationOperationId); } pendingSaplingConsolidationTxs.clear(); SaplingConsolidationRequest request; @@ -750,6 +752,7 @@ void CWallet::RunSaplingConsolidation(int blockHeight) { request.addresses = consolidationAddresses; std::shared_ptr operation(new AsyncRPCOperation_saplingconsolidation(blockHeight + AUTO_OP_TARGET_HEIGHT_OFFSET, request)); saplingConsolidationOperationId = operation->getId(); + saplingConsolidationOpManual = false; if (!q->addOperation(operation)) { // Queue is closing (shutdown). Release the flag we just set, or it stays // set with no operation in flight and blocks every later round. diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 5e2e7e995..183d49972 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -404,6 +404,7 @@ struct SaplingNoteEntry struct SaplingConsolidationRound { int height = 0; //!< chain tip when the round finished + bool manual = false; //!< run by z_consolidate int64_t time = 0; std::vector txids; int notesMerged = 0; @@ -889,6 +890,8 @@ public: // Id of the in-flight consolidation op; read by the op to confirm it is // still the current one before mutating scheduler state. AsyncRPCOperationId saplingConsolidationOperationId; + // That op was started by z_consolidate: its result stays in the queue for z_getoperationresult. + bool saplingConsolidationOpManual = false; bool fSaplingConsolidationEnabled = false; bool fConsolidationRunning = false;