From a7b65af2a4450663d4a20a617c59dac87b34fb36 Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Fri, 3 Dec 2021 17:17:04 +0100 Subject: [PATCH 1/2] rpc: avoid scriptPubKey<->CTxDestination conversions in `GetReceived` tally --- src/wallet/rpc/coins.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/wallet/rpc/coins.cpp b/src/wallet/rpc/coins.cpp index 326f0f088bb..7c1f4d43cdc 100644 --- a/src/wallet/rpc/coins.cpp +++ b/src/wallet/rpc/coins.cpp @@ -17,12 +17,14 @@ static CAmount GetReceived(const CWallet& wallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet) { - std::set address_set; + std::set output_scripts; if (by_label) { // Get the set of addresses assigned to label std::string label = LabelFromValue(params[0]); - address_set = wallet.GetLabelAddresses(label); + for (const auto& address : wallet.GetLabelAddresses(label)) { + output_scripts.insert(GetScriptForDestination(address)); + } } else { // Get the address CTxDestination dest = DecodeDestination(params[0].get_str()); @@ -33,7 +35,7 @@ static CAmount GetReceived(const CWallet& wallet, const UniValue& params, bool b if (!wallet.IsMine(script_pub_key)) { throw JSONRPCError(RPC_WALLET_ERROR, "Address not found in wallet"); } - address_set.insert(dest); + output_scripts.insert(script_pub_key); } // Minimum confirmations @@ -65,8 +67,7 @@ static CAmount GetReceived(const CWallet& wallet, const UniValue& params, bool b } for (const CTxOut& txout : wtx.tx->vout) { - CTxDestination address; - if (ExtractDestination(txout.scriptPubKey, address) && wallet.IsMine(address) && address_set.count(address)) { + if (wallet.IsMine(txout.scriptPubKey) && output_scripts.count(txout.scriptPubKey) > 0) { amount += txout.nValue; } } From f336ff7f213564909cf5f9742618cc6ec87600fd Mon Sep 17 00:00:00 2001 From: Sebastian Falbesoner Date: Fri, 3 Dec 2021 17:29:26 +0100 Subject: [PATCH 2/2] rpc: avoid expensive `IsMine` calls in `GetReceived` tally --- src/wallet/rpc/coins.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/wallet/rpc/coins.cpp b/src/wallet/rpc/coins.cpp index 7c1f4d43cdc..fec4d385c30 100644 --- a/src/wallet/rpc/coins.cpp +++ b/src/wallet/rpc/coins.cpp @@ -23,7 +23,10 @@ static CAmount GetReceived(const CWallet& wallet, const UniValue& params, bool b // Get the set of addresses assigned to label std::string label = LabelFromValue(params[0]); for (const auto& address : wallet.GetLabelAddresses(label)) { - output_scripts.insert(GetScriptForDestination(address)); + auto output_script{GetScriptForDestination(address)}; + if (wallet.IsMine(output_script)) { + output_scripts.insert(output_script); + } } } else { // Get the address @@ -67,7 +70,7 @@ static CAmount GetReceived(const CWallet& wallet, const UniValue& params, bool b } for (const CTxOut& txout : wtx.tx->vout) { - if (wallet.IsMine(txout.scriptPubKey) && output_scripts.count(txout.scriptPubKey) > 0) { + if (output_scripts.count(txout.scriptPubKey) > 0) { amount += txout.nValue; } }