2 # Copyright (c) 2014-2017 The Bitcoin Core developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 """Test the listreceivedbyaddress RPC."""
6 from decimal
import Decimal
8 from test_framework
.test_framework
import BitcoinTestFramework
9 from test_framework
.util
import (assert_array_result
,
11 assert_raises_rpc_error
,
14 class ReceivedByTest(BitcoinTestFramework
):
15 def set_test_params(self
):
19 # Generate block to get out of IBD
20 self
.nodes
[0].generate(1)
22 self
.log
.info("listreceivedbyaddress Test")
24 # Send from node 0 to 1
25 addr
= self
.nodes
[1].getnewaddress()
26 txid
= self
.nodes
[0].sendtoaddress(addr
, 0.1)
29 # Check not listed in listreceivedbyaddress because has 0 confirmations
30 assert_array_result(self
.nodes
[1].listreceivedbyaddress(),
34 # Bury Tx under 10 block so it will be returned by listreceivedbyaddress
35 self
.nodes
[1].generate(10)
37 assert_array_result(self
.nodes
[1].listreceivedbyaddress(),
39 {"address": addr
, "account": "", "amount": Decimal("0.1"), "confirmations": 10, "txids": [txid
, ]})
40 # With min confidence < 10
41 assert_array_result(self
.nodes
[1].listreceivedbyaddress(5),
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)
48 addr
= self
.nodes
[1].getnewaddress()
49 assert_array_result(self
.nodes
[1].listreceivedbyaddress(0, True),
51 {"address": addr
, "account": "", "amount": 0, "confirmations": 0, "txids": []})
53 self
.log
.info("getreceivedbyaddress Test")
55 # Send from node 0 to 1
56 addr
= self
.nodes
[1].getnewaddress()
57 txid
= self
.nodes
[0].sendtoaddress(addr
, 0.1)
60 # Check balance is 0 because of 0 confirmations
61 balance
= self
.nodes
[1].getreceivedbyaddress(addr
)
62 assert_equal(balance
, Decimal("0.0"))
64 # Check balance is 0.1
65 balance
= self
.nodes
[1].getreceivedbyaddress(addr
, 0)
66 assert_equal(balance
, Decimal("0.1"))
68 # Bury Tx under 10 block so it will be returned by the default getreceivedbyaddress
69 self
.nodes
[1].generate(10)
71 balance
= self
.nodes
[1].getreceivedbyaddress(addr
)
72 assert_equal(balance
, Decimal("0.1"))
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
)
77 self
.log
.info("listreceivedbyaccount + getreceivedbyaccount Test")
80 addrArr
= self
.nodes
[1].getnewaddress()
81 account
= self
.nodes
[1].getaccount(addrArr
)
82 received_by_account_json
= [r
for r
in self
.nodes
[1].listreceivedbyaccount() if r
["account"] == account
][0]
83 balance_by_account
= self
.nodes
[1].getreceivedbyaccount(account
)
85 txid
= self
.nodes
[0].sendtoaddress(addr
, 0.1)
88 # listreceivedbyaccount should return received_by_account_json because of 0 confirmations
89 assert_array_result(self
.nodes
[1].listreceivedbyaccount(),
91 received_by_account_json
)
93 # getreceivedbyaddress should return same balance because of 0 confirmations
94 balance
= self
.nodes
[1].getreceivedbyaccount(account
)
95 assert_equal(balance
, balance_by_account
)
97 self
.nodes
[1].generate(10)
99 # listreceivedbyaccount should return updated account balance
100 assert_array_result(self
.nodes
[1].listreceivedbyaccount(),
101 {"account": account
},
102 {"account": received_by_account_json
["account"], "amount": (received_by_account_json
["amount"] + Decimal("0.1"))})
104 # getreceivedbyaddress should return updates balance
105 balance
= self
.nodes
[1].getreceivedbyaccount(account
)
106 assert_equal(balance
, balance_by_account
+ Decimal("0.1"))
108 # Create a new account named "mynewaccount" that has a 0 balance
109 self
.nodes
[1].getaccountaddress("mynewaccount")
110 received_by_account_json
= [r
for r
in self
.nodes
[1].listreceivedbyaccount(0, True) if r
["account"] == "mynewaccount"][0]
112 # Test includeempty of listreceivedbyaccount
113 assert_equal(received_by_account_json
["amount"], Decimal("0.0"))
115 # Test getreceivedbyaccount for 0 amount accounts
116 balance
= self
.nodes
[1].getreceivedbyaccount("mynewaccount")
117 assert_equal(balance
, Decimal("0.0"))
119 if __name__
== '__main__':
120 ReceivedByTest().main()