Skip to content

Commit a1d5b12

Browse files
committed
Merge getreceivedby tally into GetReceived function
1 parent 210b533 commit a1d5b12

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
@@ -576,6 +576,52 @@ static UniValue signmessage(const JSONRPCRequest& request)
576576
return signature;
577577
}
578578

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

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

648665

@@ -683,34 +700,7 @@ static UniValue getreceivedbylabel(const JSONRPCRequest& request)
683700
auto locked_chain = pwallet->chain().lock();
684701
LOCK(pwallet->cs_wallet);
685702

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

716706

0 commit comments

Comments
 (0)