Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc: Extract scriptPubKey on getreceivedbyaddress #16655

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/wallet/rpcwallet.cpp
Expand Up @@ -633,10 +633,13 @@ static UniValue getreceivedbyaddress(const JSONRPCRequest& request)
continue;
}

for (const CTxOut& txout : wtx.tx->vout)
if (txout.scriptPubKey == scriptPubKey)
if (wtx.GetDepthInMainChain(*locked_chain) >= nMinDepth)
for (const CTxOut& txout : wtx.tx->vout) {
CTxDestination address;
if (ExtractDestination(txout.scriptPubKey, address) &&
GetScriptForDestination(address) == scriptPubKey &&
wtx.GetDepthInMainChain(*locked_chain) >= nMinDepth)
nAmount += txout.nValue;
}
}

return ValueFromAmount(nAmount);
Expand Down
74 changes: 74 additions & 0 deletions test/functional/rpc_getreceivedbyaddress.py
@@ -0,0 +1,74 @@
#!/usr/bin/env python3
# Copyright (c) 2019 The Bitcoin Core developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
"""Test getreceivedbyaddress RPC call."""

from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal

class GetReceivedByAddressTest(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 1

def skip_test_if_missing_module(self):
self.skip_if_no_wallet()

def run_test(self):

address = "mmo5AqgFVkaT8ehMN5wZ1Xec23h8Pu4T9q"
address_priv_key = "cMjqJ6RbM3FGoCaVu7x6dmYKu9ARcCGeBE5qtoKvZEaPeiucaNkg"

# Prepared transaction:
# bitcoin-tx -create -json -regtest outpubkey=10:02352974bf0da1e6c4652888f014446572669cf7f9fd54d2c3b3d26ef60f15bd4c

"""
{
"txid": "24de15cbe041ca8a448bb10635d9dc678bfcfa94b3a2500f69ed98818b9f1de3",
"hash": "24de15cbe041ca8a448bb10635d9dc678bfcfa94b3a2500f69ed98818b9f1de3",
"version": 2,
"size": 54,
"vsize": 54,
"weight": 216,
"locktime": 0,
"vin": [
],
"vout": [
{
"value": 10.00000000,
"n": 0,
"scriptPubKey": {
"asm": "02352974bf0da1e6c4652888f014446572669cf7f9fd54d2c3b3d26ef60f15bd4c OP_CHECKSIG",
"hex": "2102352974bf0da1e6c4652888f014446572669cf7f9fd54d2c3b3d26ef60f15bd4cac",
"reqSigs": 1,
"type": "pubkey",
"addresses": [
"mmo5AqgFVkaT8ehMN5wZ1Xec23h8Pu4T9q"
]
}
}
],
"hex": "02000000000100ca9a3b00000000232102352974bf0da1e6c4652888f014446572669cf7f9fd54d2c3b3d26ef60f15bd4cac00000000"
}
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there is no need to copy paste the full output of the command

tx_vout = "02000000000100ca9a3b00000000232102352974bf0da1e6c4652888f014446572669cf7f9fd54d2c3b3d26ef60f15bd4cac00000000"

self.log.info("Mining blocks...")
self.nodes[0].generate(101)

self.log.info("Sending BTCs...")
raw_tx = self.nodes[0].fundrawtransaction(tx_vout)
raw_tx_signed = self.nodes[0].signrawtransactionwithwallet(raw_tx['hex'])
self.nodes[0].sendrawtransaction(raw_tx_signed['hex'])

self.log.info("Mining transaction...")
self.nodes[0].generate(7)

self.log.info("Test getreceivedbyaddress")
self.nodes[0].importprivkey(address_priv_key)
received=self.nodes[0].getreceivedbyaddress(address)
assert_equal(received, 10)

if __name__ == '__main__':
GetReceivedByAddressTest().main()
1 change: 1 addition & 0 deletions test/functional/test_runner.py
Expand Up @@ -103,6 +103,7 @@
'wallet_abandonconflict.py',
'feature_csv_activation.py',
'rpc_rawtransaction.py',
'rpc_getreceivedbyaddress.py',
'wallet_address_types.py',
'feature_bip68_sequence.py',
'p2p_feefilter.py',
Expand Down