From f796d5d14eb57b21d6bb30559dd4329facf9aa14 Mon Sep 17 00:00:00 2001 From: "Jonathan \"Duke\" Leto" Date: Fri, 16 Sep 2022 10:08:53 -0700 Subject: [PATCH] Support multiple zsweepexclude zaddrs --- src/init.cpp | 4 +-- src/wallet/asyncrpcoperation_sweep.cpp | 37 ++++++++++++++++++++------ src/wallet/wallet.h | 2 +- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 727a4e946..cec462cf5 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -2121,17 +2121,17 @@ bool AppInit2(boost::thread_group& threadGroup, CScheduler& scheduler) for (int i = 0; i < vSweepExclude.size(); i++) { LogPrintf("Sweep Excluded Address: %s\n", vSweepExclude[i]); - pwalletMain->sweepExcludeAddress = vSweepExclude[i]; auto zSweepExcluded = DecodePaymentAddress(vSweepExclude[i]); if (!IsValidPaymentAddress(zSweepExcluded)) { return InitError("Invalid zsweep address"); } auto hasSpendingKey = boost::apply_visitor(HaveSpendingKeyForPaymentAddress(pwalletMain), zSweepExcluded); - pwalletMain->sweepExcludeAddress = vSweepExclude[i]; if (!hasSpendingKey) { return InitError("Wallet must have the spending key of zsweepexclude address"); } + // Add this validated zaddr to the list of excluded sweep zaddrs + pwalletMain->sweepExcludeAddresses.push_back( vSweepExclude[i] ); } if (pwalletMain->fSaplingConsolidationEnabled) { diff --git a/src/wallet/asyncrpcoperation_sweep.cpp b/src/wallet/asyncrpcoperation_sweep.cpp index 82dc1a40c..46962564d 100644 --- a/src/wallet/asyncrpcoperation_sweep.cpp +++ b/src/wallet/asyncrpcoperation_sweep.cpp @@ -74,6 +74,26 @@ void AsyncRPCOperation_sweep::main() { LogPrintf("%s", s); } +// Is this zaddr excluded from zsweep ? +bool IsExcludedAddress(libzcash::SaplingPaymentAddress zaddr) { + for( auto & sweepExcludeAddress : pwalletMain->sweepExcludeAddresses ) { + auto zAddressExclude = DecodePaymentAddress(sweepExcludeAddress); + + if (boost::get(&zAddressExclude) != nullptr) { + sweepExcludeAddress = boost::get(zAddressExclude); + } else { + // This is an invalid sapling zaddr + LogPrintf("%s: Invalid zsweepexclude zaddr %s, ignoring\n", opid, sweepExcludeAddress); + } + + if (sweepExcludeAddress == entry.address) { + return true; + } + } + + return false; +} + bool AsyncRPCOperation_sweep::main_impl() { bool status=true; auto opid=getId(); @@ -88,7 +108,6 @@ bool AsyncRPCOperation_sweep::main_impl() { std::vector saplingEntries; libzcash::SaplingPaymentAddress sweepAddress; - libzcash::SaplingPaymentAddress sweepExcludeAddress; std::map> mapAddresses; { @@ -96,16 +115,15 @@ bool AsyncRPCOperation_sweep::main_impl() { pwalletMain->GetFilteredNotes(saplingEntries, "", 11); if (!fromRPC_) { - auto zAddressExclude = DecodePaymentAddress(pwalletMain->sweepExcludeAddress); - if (boost::get(&zAddressExclude) != nullptr) { - sweepExcludeAddress = boost::get(zAddressExclude); - } if (fSweepMapUsed) { const vector& v = mapMultiArgs["-zsweepaddress"]; for(int i = 0; i < v.size(); i++) { auto zAddress = DecodePaymentAddress(v[i]); if (boost::get(&zAddress) != nullptr) { sweepAddress = boost::get(zAddress); + } else { + LogPrintf("%s: Invalid zsweepaddress configured, exiting\n", opid); + return false; } } } else { @@ -121,10 +139,13 @@ bool AsyncRPCOperation_sweep::main_impl() { } } - // Map all notes by address + // Map all notes (zutxos) by address for (auto & entry : saplingEntries) { - // do not need to sweep Excluded Address - if (sweepExcludeAddress == entry.address) { continue; } + // do not need to sweep Excluded Addresses + if(IsExcludedAddress(entry.address) { + continue; + } + // do not need to sweep the sweepAddress as that is the destination if (sweepAddress == entry.address) { continue; diff --git a/src/wallet/wallet.h b/src/wallet/wallet.h index 2dfb9e6b1..3b932c4f5 100644 --- a/src/wallet/wallet.h +++ b/src/wallet/wallet.h @@ -815,7 +815,7 @@ public: int sweepFee = 10000; int sweepMaxInputs = 200; std::string sweepAddress = ""; - std::string sweepExcludeAddress = ""; + std::vector sweepExcludeAddresses = ""; std::string consolidationAddress = ""; void ClearNoteWitnessCache();