Skip to content

Commit 3be119c

Browse files
author
MarcoFalke
committed
Merge #17579: [refactor] Merge getreceivedby tally into GetReceived function
a1d5b12 Merge getreceivedby tally into GetReceived function (Andrew Toth) Pull request description: This PR merges the tally code of `getreceivedbyaddress` and `getreceivedbylabel` into a single function `GetReceived`. This reduces repeated code and makes it similar to `listreceivedbyaddress` and `listreceivedbylabel`, which use the function `ListReceived`. It will also make the change in #14707 simpler and easier to review. ACKs for top commit: theStack: re-ACK bitcoin/bitcoin@a1d5b12 meshcollider: utACK a1d5b12 Tree-SHA512: 43d9cd92f7c2c6a8b9c7509aa85a9b9233a6cfec1c43a9062e3bdfb83515413d1feafa8938c828351278ba22bd31c47e62ab5341e4bddc2493103b094d73b047
2 parents 5e5dd99 + a1d5b12 commit 3be119c

File tree

1 file changed

+48
-58
lines changed

1 file changed

+48
-58
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 48 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,52 @@ static UniValue signmessage(const JSONRPCRequest& request)
572572
return signature;
573573
}
574574

575+
static CAmount GetReceived(interfaces::Chain::Lock& locked_chain, const CWallet& wallet, const UniValue& params, bool by_label) EXCLUSIVE_LOCKS_REQUIRED(wallet.cs_wallet)
576+
{
577+
std::set<CTxDestination> address_set;
578+
579+
if (by_label) {
580+
// Get the set of addresses assigned to label
581+
std::string label = LabelFromValue(params[0]);
582+
address_set = wallet.GetLabelAddresses(label);
583+
} else {
584+
// Get the address
585+
CTxDestination dest = DecodeDestination(params[0].get_str());
586+
if (!IsValidDestination(dest)) {
587+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
588+
}
589+
CScript script_pub_key = GetScriptForDestination(dest);
590+
if (!wallet.IsMine(script_pub_key)) {
591+
throw JSONRPCError(RPC_WALLET_ERROR, "Address not found in wallet");
592+
}
593+
address_set.insert(dest);
594+
}
595+
596+
// Minimum confirmations
597+
int min_depth = 1;
598+
if (!params[1].isNull())
599+
min_depth = params[1].get_int();
600+
601+
// Tally
602+
CAmount amount = 0;
603+
for (const std::pair<const uint256, CWalletTx>& wtx_pair : wallet.mapWallet) {
604+
const CWalletTx& wtx = wtx_pair.second;
605+
if (wtx.IsCoinBase() || !locked_chain.checkFinalTx(*wtx.tx) || wtx.GetDepthInMainChain() < min_depth) {
606+
continue;
607+
}
608+
609+
for (const CTxOut& txout : wtx.tx->vout) {
610+
CTxDestination address;
611+
if (ExtractDestination(txout.scriptPubKey, address) && wallet.IsMine(address) && address_set.count(address)) {
612+
amount += txout.nValue;
613+
}
614+
}
615+
}
616+
617+
return amount;
618+
}
619+
620+
575621
static UniValue getreceivedbyaddress(const JSONRPCRequest& request)
576622
{
577623
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
@@ -609,36 +655,7 @@ static UniValue getreceivedbyaddress(const JSONRPCRequest& request)
609655
auto locked_chain = pwallet->chain().lock();
610656
LOCK(pwallet->cs_wallet);
611657

612-
// Bitcoin address
613-
CTxDestination dest = DecodeDestination(request.params[0].get_str());
614-
if (!IsValidDestination(dest)) {
615-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Bitcoin address");
616-
}
617-
CScript scriptPubKey = GetScriptForDestination(dest);
618-
if (!pwallet->IsMine(scriptPubKey)) {
619-
throw JSONRPCError(RPC_WALLET_ERROR, "Address not found in wallet");
620-
}
621-
622-
// Minimum confirmations
623-
int nMinDepth = 1;
624-
if (!request.params[1].isNull())
625-
nMinDepth = request.params[1].get_int();
626-
627-
// Tally
628-
CAmount nAmount = 0;
629-
for (const std::pair<const uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
630-
const CWalletTx& wtx = pairWtx.second;
631-
if (wtx.IsCoinBase() || !locked_chain->checkFinalTx(*wtx.tx)) {
632-
continue;
633-
}
634-
635-
for (const CTxOut& txout : wtx.tx->vout)
636-
if (txout.scriptPubKey == scriptPubKey)
637-
if (wtx.GetDepthInMainChain() >= nMinDepth)
638-
nAmount += txout.nValue;
639-
}
640-
641-
return ValueFromAmount(nAmount);
658+
return ValueFromAmount(GetReceived(*locked_chain, *pwallet, request.params, /* by_label */ false));
642659
}
643660

644661

@@ -679,34 +696,7 @@ static UniValue getreceivedbylabel(const JSONRPCRequest& request)
679696
auto locked_chain = pwallet->chain().lock();
680697
LOCK(pwallet->cs_wallet);
681698

682-
// Minimum confirmations
683-
int nMinDepth = 1;
684-
if (!request.params[1].isNull())
685-
nMinDepth = request.params[1].get_int();
686-
687-
// Get the set of pub keys assigned to label
688-
std::string label = LabelFromValue(request.params[0]);
689-
std::set<CTxDestination> setAddress = pwallet->GetLabelAddresses(label);
690-
691-
// Tally
692-
CAmount nAmount = 0;
693-
for (const std::pair<const uint256, CWalletTx>& pairWtx : pwallet->mapWallet) {
694-
const CWalletTx& wtx = pairWtx.second;
695-
if (wtx.IsCoinBase() || !locked_chain->checkFinalTx(*wtx.tx)) {
696-
continue;
697-
}
698-
699-
for (const CTxOut& txout : wtx.tx->vout)
700-
{
701-
CTxDestination address;
702-
if (ExtractDestination(txout.scriptPubKey, address) && pwallet->IsMine(address) && setAddress.count(address)) {
703-
if (wtx.GetDepthInMainChain() >= nMinDepth)
704-
nAmount += txout.nValue;
705-
}
706-
}
707-
}
708-
709-
return ValueFromAmount(nAmount);
699+
return ValueFromAmount(GetReceived(*locked_chain, *pwallet, request.params, /* by_label */ true));
710700
}
711701

712702

0 commit comments

Comments
 (0)