diff --git a/doc/zsweep-consolidation.md b/doc/zsweep-consolidation.md index b99c9cd47..c93ea7349 100644 --- a/doc/zsweep-consolidation.md +++ b/doc/zsweep-consolidation.md @@ -62,6 +62,10 @@ witness rebuilds. Consolidation keeps that in check. * Only the notes a round actually spends are locked while it proves, so sends made meanwhile still see the rest of the wallet. A note with a missing or unusable witness is skipped (and logged once); the round goes on without it. * With the defaults, an address over the threshold loses 27 to 42 notes per round, and a round runs every 25 blocks. +* dragonxd runs one async operation at a time. So that a `z_sendmany` (or any other async RPC) made during a round + does not wait for all of it, the round ends after its current transaction when another operation is queued + (`last_round.yielded`); the notes it did not get to are unlocked and taken up by the next round. A node that is + asked to stop also ends the round after the current transaction. ### Turning it on diff --git a/src/wallet/asyncrpcoperation_saplingconsolidation.cpp b/src/wallet/asyncrpcoperation_saplingconsolidation.cpp index 82a81e7f0..148654336 100644 --- a/src/wallet/asyncrpcoperation_saplingconsolidation.cpp +++ b/src/wallet/asyncrpcoperation_saplingconsolidation.cpp @@ -8,7 +8,9 @@ #include "asyncrpcoperation_saplingconsolidation.h" #include "init.h" #include "key_io.h" +#include "asyncrpcqueue.h" #include "rpc/protocol.h" +#include "rpc/server.h" #include "random.h" #include "sync.h" #include "tinyformat.h" @@ -31,6 +33,20 @@ static const int MIN_ZOUTS = 7; extern string randomSietchZaddr(); +// Whether another operation waits in the async queue. dragonxd has a single async worker, so an +// operation queued during a round (a z_sendmany, say) runs only once the round is over. +static bool OtherAsyncOperationWaiting(const AsyncRPCOperationId& self) { + std::shared_ptr q = getAsyncRPCQueue(); + for (const AsyncRPCOperationId& id : q->getAllOperationIds()) { + if (id == self) + continue; + std::shared_ptr op = q->getOperationForId(id); + if (op && op->isReady()) + return true; + } + return false; +} + int SaplingConsolidationMaxInputs(size_t maxTxSize) { // Everything but the descriptions (header, value balance, binding signature, counts) is a few // hundred bytes; 1000 leaves room to spare. @@ -161,6 +177,7 @@ void AsyncRPCOperation_saplingconsolidation::main() { round.amountConsolidated = tally_.amountConsolidated; round.fees = tally_.fees; round.skippedNotes = tally_.skippedNotes; + round.yielded = tally_.yielded; round.error = tally_.error; if (!success && round.error.empty()) round.error = getErrorMessage(); @@ -323,12 +340,22 @@ bool AsyncRPCOperation_saplingconsolidation::main_impl() { LogPrint("zrpc", "%s: %d consolidation input notes locked for %d transactions\n", opid, z_sapling_inputs_.size(), txs.size()); for (const PlannedTx& planned : txs) { - if (isCancelled()) { - LogPrintf("%s: Canceled. Stopping.\n", opid); - tally.error = "cancelled"; + // The queue's cancel at shutdown never reaches an executing op (AsyncRPCOperation::cancel() is + // not virtual), so look for the shutdown here, or a stop would wait for the whole round. + if (isCancelled() || ShutdownRequested()) { + LogPrintf("%s: %s. Stopping.\n", opid, isCancelled() ? "Canceled" : "Shutting down"); + tally.error = isCancelled() ? "cancelled" : "shutting down"; status=false; break; } + // Once a tx is made, give the worker to anything queued meanwhile: the notes not spent yet are + // unlocked and the next round takes them up. + if (!tally.txids.empty() && OtherAsyncOperationWaiting(opid)) { + LogPrintf("%s: an async operation is waiting; ending the round after %d transactions\n", + opid, tally.txids.size()); + tally.yielded = true; + break; + } const AddressNotes& group = groups[planned.group]; auto builder = TransactionBuilder(consensusParams, tipHeight, pwalletMain); @@ -401,9 +428,9 @@ bool AsyncRPCOperation_saplingconsolidation::main_impl() { } CTransaction tx = maybe_tx.get(); - if (isCancelled()) { - LogPrintf("%s: Canceled. Stopping.\n", opid); - tally.error = "cancelled"; + if (isCancelled() || ShutdownRequested()) { + LogPrintf("%s: %s. Stopping.\n", opid, isCancelled() ? "Canceled" : "Shutting down"); + tally.error = isCancelled() ? "cancelled" : "shutting down"; status=false; break; } @@ -423,8 +450,9 @@ bool AsyncRPCOperation_saplingconsolidation::main_impl() { } } - LogPrintf("%s: Created %d transactions merging %d notes with total Sapling output amount=%s, skipped %d notes, status=%d\n", - opid, tally.txids.size(), tally.notesMerged, FormatMoney(tally.amountConsolidated), tally.skippedNotes, (int)status); + LogPrintf("%s: Created %d transactions merging %d notes with total Sapling output amount=%s, skipped %d notes, yielded=%d, status=%d\n", + opid, tally.txids.size(), tally.notesMerged, FormatMoney(tally.amountConsolidated), tally.skippedNotes, + (int)tally.yielded, (int)status); setConsolidationResult(tally); return status; } @@ -444,6 +472,7 @@ void AsyncRPCOperation_saplingconsolidation::setConsolidationResult(const Consol res.push_back(Pair("amount_merged", FormatMoney(tally.amountMerged))); res.push_back(Pair("fees", FormatMoney(tally.fees))); res.push_back(Pair("skipped_notes", tally.skippedNotes)); + res.push_back(Pair("yielded", tally.yielded)); if (!tally.error.empty()) res.push_back(Pair("error", tally.error)); set_result(res); diff --git a/src/wallet/asyncrpcoperation_saplingconsolidation.h b/src/wallet/asyncrpcoperation_saplingconsolidation.h index 8a5fc72b2..b7555d023 100644 --- a/src/wallet/asyncrpcoperation_saplingconsolidation.h +++ b/src/wallet/asyncrpcoperation_saplingconsolidation.h @@ -70,6 +70,7 @@ struct ConsolidationTally { CAmount amountConsolidated = 0; // value of the notes created (amountMerged minus fees) CAmount fees = 0; int skippedNotes = 0; // left out for a missing or unusable witness + bool yielded = false; // stopped after a tx to let a queued async operation run std::string error; }; diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 72a56282e..e9b0b2d22 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -3333,6 +3333,8 @@ UniValue z_consolidationstatus(const UniValue& params, bool fHelp, const CPubKey " \"amount_consolidated\" : n, (numeric) value of the notes created, in puposhis\n" " \"fees\" : n, (numeric) in puposhis\n" " \"skipped_notes\" : n, (numeric) notes left out for a missing or unusable witness\n" + " \"yielded\" : true|false, (boolean) it stopped after a transaction to let a queued operation\n" + " (z_sendmany, ...) run; the next round goes on\n" " \"error\" : \"...\" (string) why it stopped early; empty if it did not\n" " },\n" " \"disabled_reason\" : \"...\", (string) why no periodic round can start now; empty if one can\n" @@ -3388,6 +3390,7 @@ UniValue z_consolidationstatus(const UniValue& params, bool fHelp, const CPubKey last.push_back(Pair("amount_consolidated", (int64_t)r.amountConsolidated)); last.push_back(Pair("fees", (int64_t)r.fees)); last.push_back(Pair("skipped_notes", r.skippedNotes)); + last.push_back(Pair("yielded", r.yielded)); last.push_back(Pair("error", r.error)); ret.push_back(Pair("last_round", last)); } else { diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 64c6d61eb..b83a718cd 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -412,6 +412,7 @@ struct SaplingConsolidationRound CAmount amountConsolidated = 0; //!< value of the notes created CAmount fees = 0; int skippedNotes = 0; //!< left out for a missing or unusable witness + bool yielded = false; //!< stopped early to let a queued async operation run std::string error; //!< why the round stopped early, if it did };