diff --git a/src/init.cpp b/src/init.cpp index d7c574bf6..1e80eb76a 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -2771,6 +2771,10 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) if (GetBoolArg("-notecachewarmup", true) && !ShutdownRequested()) { uiInterface.InitMessage(_("Decrypting wallet notes...")); pwalletMain->WarmSaplingNoteCache(); + // With the notes decrypted, counting them per address is cheap: suggest consolidation to a + // wallet that has piled up notes (typically a miner's, one note per autoshield round). + if (!pwalletMain->fSaplingConsolidationEnabled && !ShutdownRequested()) + pwalletMain->RefreshConsolidationSuggestion(); } pwalletMain->SetBroadcastTransactions(GetBoolArg("-walletbroadcast", true)); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 1a254d597..72a56282e 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -3339,7 +3339,10 @@ UniValue z_consolidationstatus(const UniValue& params, bool fHelp, const CPubKey " \"addresses\" : [ (array, verbose only) every address with eligible notes\n" " { \"address\" : \"zaddr\", \"eligible_notes\" : n, \"over_threshold\" : true|false, \"included\" : true|false }\n" " ],\n" - " \"addresses_over_threshold\" : n (numeric, verbose only) included addresses over the threshold\n" + " \"addresses_over_threshold\" : n, (numeric, verbose only) included addresses over the threshold\n" + " \"consolidation_suggested\" : true|false, (boolean, verbose only) -consolidation is off and some\n" + " address is over the threshold: consider -consolidation=1 or z_consolidate\n" + " \"consolidation_suggested_count\" : n (numeric, verbose only) addresses over the threshold while it is off\n" "}\n" "\nExamples:\n" + HelpExampleCli("z_consolidationstatus", "") @@ -3411,6 +3414,9 @@ UniValue z_consolidationstatus(const UniValue& params, bool fHelp, const CPubKey } ret.push_back(Pair("addresses", list)); ret.push_back(Pair("addresses_over_threshold", over)); + pwalletMain->SetConsolidationSuggestion(over); + ret.push_back(Pair("consolidation_suggested", !pwalletMain->fSaplingConsolidationEnabled && over > 0)); + ret.push_back(Pair("consolidation_suggested_count", pwalletMain->consolidationSuggestCount)); } return ret; @@ -3576,7 +3582,9 @@ UniValue z_autoshieldstatus(const UniValue& params, bool fHelp, const CPubKey& m " \"hdseedorigin\" : n, (numeric) 0 unrecorded, 1 created, 2 restored, 3 retrofit, 4 unknown\n" " \"hdseedorigin_desc\" : \"...\", (string) readable form of hdseedorigin\n" " \"seed_recoverable\" : true|false, (boolean) whether a seed phrase can be exported\n" - " \"disabled_reason\" : \"...\" (string) why auto-shielding is not running, if it is not\n" + " \"disabled_reason\" : \"...\", (string) why auto-shielding is not running, if it is not\n" + " \"consolidation_suggested\" : true|false (boolean) -consolidation is off and some address holds more\n" + " than -consolidationthreshold spendable notes (see z_consolidationstatus)\n" "}\n" "\nExamples:\n" + HelpExampleCli("z_autoshieldstatus", "") @@ -3628,6 +3636,16 @@ UniValue z_autoshieldstatus(const UniValue& params, bool fHelp, const CPubKey& m } ret.push_back(Pair("disabled_reason", why)); + // Autoshield is what fills a miner's wallet with notes, one per round, so say here when consolidation + // would help. The count is refreshed at most once per consolidation interval: it walks the wallet. + if (!pwalletMain->fSaplingConsolidationEnabled && + (pwalletMain->consolidationSuggestHeight < 0 || + chainActive.Height() - pwalletMain->consolidationSuggestHeight >= pwalletMain->consolidationInterval)) { + pwalletMain->RefreshConsolidationSuggestion(); + } + ret.push_back(Pair("consolidation_suggested", + !pwalletMain->fSaplingConsolidationEnabled && pwalletMain->consolidationSuggestCount > 0)); + return ret; } diff --git a/src/wallet/wallet.cpp b/src/wallet/wallet.cpp index c3b52c7f3..d13fe8492 100644 --- a/src/wallet/wallet.cpp +++ b/src/wallet/wallet.cpp @@ -772,6 +772,34 @@ std::map CWallet::CountConsolidationEligib return counts; } +void CWallet::SetConsolidationSuggestion(int overThreshold) { + LOCK2(cs_main, cs_wallet); + if (fSaplingConsolidationEnabled) + overThreshold = 0; + consolidationSuggestCount = overThreshold; + consolidationSuggestHeight = chainActive.Height(); + if (overThreshold > 0 && !fConsolidationSuggestLogged) { + fConsolidationSuggestLogged = true; + LogPrintf("%d %s more than %d spendable notes; consider -consolidation=1 or z_consolidate\n", + overThreshold, overThreshold == 1 ? "address has" : "addresses have", consolidationThreshold); + } +} + +void CWallet::RefreshConsolidationSuggestion() { + LOCK2(cs_main, cs_wallet); + if (fSaplingConsolidationEnabled) { + SetConsolidationSuggestion(0); + return; + } + std::set included(consolidationAddresses.begin(), consolidationAddresses.end()); + int over = 0; + for (const auto& p : CountConsolidationEligibleNotes()) { + if ((included.empty() || included.count(p.first)) && p.second > consolidationThreshold) + over++; + } + SetConsolidationSuggestion(over); +} + // Periodically drain matured transparent coinbase into a wallet-owned Sapling // z-address. Default-ON but conditional: this is enqueue-only (all gathering // happens on the async worker thread inside the op, which is why we must not diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 183d49972..64c6d61eb 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -932,6 +932,11 @@ public: int amountSwept = 0; // What consolidation rounds have done since startup. Guarded by cs_wallet. SaplingConsolidationStats consolidationStats; + // With -consolidation off: how many addresses are over the threshold (-1 until counted), at which + // height that was counted, and whether the "consider -consolidation" hint has been logged. + int consolidationSuggestCount = -1; + int consolidationSuggestHeight = -1; + bool fConsolidationSuggestLogged = false; int sweepInterval = 10; int consolidationInterval = DEFAULT_CONSOLIDATION_INTERVAL; int consolidationThreshold = DEFAULT_CONSOLIDATION_THRESHOLD; @@ -1379,6 +1384,11 @@ public: /** Notes a consolidation round would count as eligible, per address. Walks the whole wallet * under cs_main + cs_wallet, so it is only run on request. */ std::map CountConsolidationEligibleNotes(); + /** With -consolidation off, counts the addresses over the threshold into consolidationSuggestCount + * and logs, once, that consolidation would help. Walks the wallet like the above. */ + void RefreshConsolidationSuggestion(); + /** Records a count of addresses over the threshold (from either walk) and logs the hint once. */ + void SetConsolidationSuggestion(int overThreshold); void RunAutoShieldCoinbase(int blockHeight); bool CommitAutomatedTx(const CTransaction& tx); /** Saves witness caches and best block locator to disk. Overrides CValidationInterface. */