Skip to content

Commit 95e14dc

Browse files
author
MarcoFalke
committed
Merge #11055: [wallet] [rpc] getreceivedbyaddress should return error if called with address not owned by the wallet
5e0ba8f [wallet] getreceivedbyaddress should return error if address is not mine (John Newbery) ea0cd24 [tests] Tidy up receivedby.py (John Newbery) Pull request description: Two commits: - First commit tidies up the `receivedby.py` test (and speeds it up by factor of two) - Second commit changes getreceivedbyaddress to return error if the address is not found in wallet, and adds test to `receivedby.py` Tree-SHA512: e41342dcbd037a6b440cbe4ecd3b8ed589e18e477333f0d866f3564e948e0f5231e497d5ffb66da4e6680eb772d9f0cf839125098bb68b92d04a5ee35c6c0a81
2 parents 6de3203 + 5e0ba8f commit 95e14dc

File tree

2 files changed

+53
-73
lines changed

2 files changed

+53
-73
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ UniValue getreceivedbyaddress(const JSONRPCRequest& request)
654654
}
655655
CScript scriptPubKey = GetScriptForDestination(dest);
656656
if (!IsMine(*pwallet, scriptPubKey)) {
657-
return ValueFromAmount(0);
657+
throw JSONRPCError(RPC_WALLET_ERROR, "Address not found in wallet");
658658
}
659659

660660
// Minimum confirmations

test/functional/receivedby.py

Lines changed: 52 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,138 +3,118 @@
33
# Distributed under the MIT software license, see the accompanying
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Test the listreceivedbyaddress RPC."""
6+
from decimal import Decimal
67

78
from test_framework.test_framework import BitcoinTestFramework
8-
from test_framework.util import *
9-
10-
def get_sub_array_from_array(object_array, to_match):
11-
'''
12-
Finds and returns a sub array from an array of arrays.
13-
to_match should be a unique idetifier of a sub array
14-
'''
15-
for item in object_array:
16-
all_match = True
17-
for key,value in to_match.items():
18-
if item[key] != value:
19-
all_match = False
20-
if not all_match:
21-
continue
22-
return item
23-
return []
9+
from test_framework.util import (assert_array_result,
10+
assert_equal,
11+
assert_raises_rpc_error,
12+
)
2413

2514
class ReceivedByTest(BitcoinTestFramework):
2615
def set_test_params(self):
2716
self.num_nodes = 2
28-
self.enable_mocktime()
2917

3018
def run_test(self):
31-
'''
32-
listreceivedbyaddress Test
33-
'''
19+
# Generate block to get out of IBD
20+
self.nodes[0].generate(1)
21+
22+
self.log.info("listreceivedbyaddress Test")
23+
3424
# Send from node 0 to 1
3525
addr = self.nodes[1].getnewaddress()
3626
txid = self.nodes[0].sendtoaddress(addr, 0.1)
3727
self.sync_all()
3828

39-
#Check not listed in listreceivedbyaddress because has 0 confirmations
29+
# Check not listed in listreceivedbyaddress because has 0 confirmations
4030
assert_array_result(self.nodes[1].listreceivedbyaddress(),
41-
{"address":addr},
42-
{ },
43-
True)
44-
#Bury Tx under 10 block so it will be returned by listreceivedbyaddress
31+
{"address": addr},
32+
{},
33+
True)
34+
# Bury Tx under 10 block so it will be returned by listreceivedbyaddress
4535
self.nodes[1].generate(10)
4636
self.sync_all()
4737
assert_array_result(self.nodes[1].listreceivedbyaddress(),
48-
{"address":addr},
49-
{"address":addr, "account":"", "amount":Decimal("0.1"), "confirmations":10, "txids":[txid,]})
50-
#With min confidence < 10
38+
{"address": addr},
39+
{"address": addr, "account": "", "amount": Decimal("0.1"), "confirmations": 10, "txids": [txid, ]})
40+
# With min confidence < 10
5141
assert_array_result(self.nodes[1].listreceivedbyaddress(5),
52-
{"address":addr},
53-
{"address":addr, "account":"", "amount":Decimal("0.1"), "confirmations":10, "txids":[txid,]})
54-
#With min confidence > 10, should not find Tx
55-
assert_array_result(self.nodes[1].listreceivedbyaddress(11),{"address":addr},{ },True)
42+
{"address": addr},
43+
{"address": addr, "account": "", "amount": Decimal("0.1"), "confirmations": 10, "txids": [txid, ]})
44+
# With min confidence > 10, should not find Tx
45+
assert_array_result(self.nodes[1].listreceivedbyaddress(11), {"address": addr}, {}, True)
5646

57-
#Empty Tx
47+
# Empty Tx
5848
addr = self.nodes[1].getnewaddress()
59-
assert_array_result(self.nodes[1].listreceivedbyaddress(0,True),
60-
{"address":addr},
61-
{"address":addr, "account":"", "amount":0, "confirmations":0, "txids":[]})
49+
assert_array_result(self.nodes[1].listreceivedbyaddress(0, True),
50+
{"address": addr},
51+
{"address": addr, "account": "", "amount": 0, "confirmations": 0, "txids": []})
52+
53+
self.log.info("getreceivedbyaddress Test")
6254

63-
'''
64-
getreceivedbyaddress Test
65-
'''
6655
# Send from node 0 to 1
6756
addr = self.nodes[1].getnewaddress()
6857
txid = self.nodes[0].sendtoaddress(addr, 0.1)
6958
self.sync_all()
7059

71-
#Check balance is 0 because of 0 confirmations
60+
# Check balance is 0 because of 0 confirmations
7261
balance = self.nodes[1].getreceivedbyaddress(addr)
73-
if balance != Decimal("0.0"):
74-
raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
62+
assert_equal(balance, Decimal("0.0"))
7563

76-
#Check balance is 0.1
77-
balance = self.nodes[1].getreceivedbyaddress(addr,0)
78-
if balance != Decimal("0.1"):
79-
raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
64+
# Check balance is 0.1
65+
balance = self.nodes[1].getreceivedbyaddress(addr, 0)
66+
assert_equal(balance, Decimal("0.1"))
8067

81-
#Bury Tx under 10 block so it will be returned by the default getreceivedbyaddress
68+
# Bury Tx under 10 block so it will be returned by the default getreceivedbyaddress
8269
self.nodes[1].generate(10)
8370
self.sync_all()
8471
balance = self.nodes[1].getreceivedbyaddress(addr)
85-
if balance != Decimal("0.1"):
86-
raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
72+
assert_equal(balance, Decimal("0.1"))
73+
74+
# Trying to getreceivedby for an address the wallet doesn't own should return an error
75+
assert_raises_rpc_error(-4, "Address not found in wallet", self.nodes[0].getreceivedbyaddress, addr)
76+
77+
self.log.info("listreceivedbyaccount + getreceivedbyaccount Test")
8778

88-
'''
89-
listreceivedbyaccount + getreceivedbyaccount Test
90-
'''
91-
#set pre-state
79+
# set pre-state
9280
addrArr = self.nodes[1].getnewaddress()
9381
account = self.nodes[1].getaccount(addrArr)
94-
received_by_account_json = get_sub_array_from_array(self.nodes[1].listreceivedbyaccount(),{"account":account})
95-
if len(received_by_account_json) == 0:
96-
raise AssertionError("No accounts found in node")
82+
received_by_account_json = [r for r in self.nodes[1].listreceivedbyaccount() if r["account"] == account][0]
9783
balance_by_account = self.nodes[1].getreceivedbyaccount(account)
9884

9985
txid = self.nodes[0].sendtoaddress(addr, 0.1)
10086
self.sync_all()
10187

10288
# listreceivedbyaccount should return received_by_account_json because of 0 confirmations
10389
assert_array_result(self.nodes[1].listreceivedbyaccount(),
104-
{"account":account},
105-
received_by_account_json)
90+
{"account": account},
91+
received_by_account_json)
10692

10793
# getreceivedbyaddress should return same balance because of 0 confirmations
10894
balance = self.nodes[1].getreceivedbyaccount(account)
109-
if balance != balance_by_account:
110-
raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
95+
assert_equal(balance, balance_by_account)
11196

11297
self.nodes[1].generate(10)
11398
self.sync_all()
11499
# listreceivedbyaccount should return updated account balance
115100
assert_array_result(self.nodes[1].listreceivedbyaccount(),
116-
{"account":account},
117-
{"account":received_by_account_json["account"], "amount":(received_by_account_json["amount"] + Decimal("0.1"))})
101+
{"account": account},
102+
{"account": received_by_account_json["account"], "amount": (received_by_account_json["amount"] + Decimal("0.1"))})
118103

119104
# getreceivedbyaddress should return updates balance
120105
balance = self.nodes[1].getreceivedbyaccount(account)
121-
if balance != balance_by_account + Decimal("0.1"):
122-
raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
106+
assert_equal(balance, balance_by_account + Decimal("0.1"))
123107

124-
#Create a new account named "mynewaccount" that has a 0 balance
108+
# Create a new account named "mynewaccount" that has a 0 balance
125109
self.nodes[1].getaccountaddress("mynewaccount")
126-
received_by_account_json = get_sub_array_from_array(self.nodes[1].listreceivedbyaccount(0,True),{"account":"mynewaccount"})
127-
if len(received_by_account_json) == 0:
128-
raise AssertionError("No accounts found in node")
110+
received_by_account_json = [r for r in self.nodes[1].listreceivedbyaccount(0, True) if r["account"] == "mynewaccount"][0]
129111

130112
# Test includeempty of listreceivedbyaccount
131-
if received_by_account_json["amount"] != Decimal("0.0"):
132-
raise AssertionError("Wrong balance returned by listreceivedbyaccount, %0.2f"%(received_by_account_json["amount"]))
113+
assert_equal(received_by_account_json["amount"], Decimal("0.0"))
133114

134115
# Test getreceivedbyaccount for 0 amount accounts
135116
balance = self.nodes[1].getreceivedbyaccount("mynewaccount")
136-
if balance != Decimal("0.0"):
137-
raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
117+
assert_equal(balance, Decimal("0.0"))
138118

139119
if __name__ == '__main__':
140120
ReceivedByTest().main()

0 commit comments

Comments
 (0)