|
3 | 3 | # Distributed under the MIT software license, see the accompanying
|
4 | 4 | # file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
5 | 5 | """Test the listreceivedbyaddress RPC."""
|
| 6 | +from decimal import Decimal |
6 | 7 |
|
7 | 8 | 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, assert_equal |
24 | 10 |
|
25 | 11 | class ReceivedByTest(BitcoinTestFramework):
|
26 | 12 | def set_test_params(self):
|
27 | 13 | self.num_nodes = 2
|
28 |
| - self.enable_mocktime() |
29 | 14 |
|
30 | 15 | def run_test(self):
|
31 |
| - ''' |
32 |
| - listreceivedbyaddress Test |
33 |
| - ''' |
| 16 | + # Generate block to get out of IBD |
| 17 | + self.nodes[0].generate(1) |
| 18 | + |
| 19 | + self.log.info("listreceivedbyaddress Test") |
| 20 | + |
34 | 21 | # Send from node 0 to 1
|
35 | 22 | addr = self.nodes[1].getnewaddress()
|
36 | 23 | txid = self.nodes[0].sendtoaddress(addr, 0.1)
|
37 | 24 | self.sync_all()
|
38 | 25 |
|
39 |
| - #Check not listed in listreceivedbyaddress because has 0 confirmations |
| 26 | + # Check not listed in listreceivedbyaddress because has 0 confirmations |
40 | 27 | 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 |
| 28 | + {"address": addr}, |
| 29 | + {}, |
| 30 | + True) |
| 31 | + # Bury Tx under 10 block so it will be returned by listreceivedbyaddress |
45 | 32 | self.nodes[1].generate(10)
|
46 | 33 | self.sync_all()
|
47 | 34 | 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 |
| 35 | + {"address": addr}, |
| 36 | + {"address": addr, "account": "", "amount": Decimal("0.1"), "confirmations": 10, "txids": [txid, ]}) |
| 37 | + # With min confidence < 10 |
51 | 38 | 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) |
| 39 | + {"address": addr}, |
| 40 | + {"address": addr, "account": "", "amount": Decimal("0.1"), "confirmations": 10, "txids": [txid, ]}) |
| 41 | + # With min confidence > 10, should not find Tx |
| 42 | + assert_array_result(self.nodes[1].listreceivedbyaddress(11), {"address": addr}, {}, True) |
56 | 43 |
|
57 |
| - #Empty Tx |
| 44 | + # Empty Tx |
58 | 45 | 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":[]}) |
| 46 | + assert_array_result(self.nodes[1].listreceivedbyaddress(0, True), |
| 47 | + {"address": addr}, |
| 48 | + {"address": addr, "account": "", "amount": 0, "confirmations": 0, "txids": []}) |
| 49 | + |
| 50 | + self.log.info("getreceivedbyaddress Test") |
62 | 51 |
|
63 |
| - ''' |
64 |
| - getreceivedbyaddress Test |
65 |
| - ''' |
66 | 52 | # Send from node 0 to 1
|
67 | 53 | addr = self.nodes[1].getnewaddress()
|
68 | 54 | txid = self.nodes[0].sendtoaddress(addr, 0.1)
|
69 | 55 | self.sync_all()
|
70 | 56 |
|
71 |
| - #Check balance is 0 because of 0 confirmations |
| 57 | + # Check balance is 0 because of 0 confirmations |
72 | 58 | balance = self.nodes[1].getreceivedbyaddress(addr)
|
73 |
| - if balance != Decimal("0.0"): |
74 |
| - raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance)) |
| 59 | + assert_equal(balance, Decimal("0.0")) |
75 | 60 |
|
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)) |
| 61 | + # Check balance is 0.1 |
| 62 | + balance = self.nodes[1].getreceivedbyaddress(addr, 0) |
| 63 | + assert_equal(balance, Decimal("0.1")) |
80 | 64 |
|
81 |
| - #Bury Tx under 10 block so it will be returned by the default getreceivedbyaddress |
| 65 | + # Bury Tx under 10 block so it will be returned by the default getreceivedbyaddress |
82 | 66 | self.nodes[1].generate(10)
|
83 | 67 | self.sync_all()
|
84 | 68 | balance = self.nodes[1].getreceivedbyaddress(addr)
|
85 |
| - if balance != Decimal("0.1"): |
86 |
| - raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance)) |
| 69 | + assert_equal(balance, Decimal("0.1")) |
| 70 | + |
| 71 | + self.log.info("listreceivedbyaccount + getreceivedbyaccount Test") |
87 | 72 |
|
88 |
| - ''' |
89 |
| - listreceivedbyaccount + getreceivedbyaccount Test |
90 |
| - ''' |
91 |
| - #set pre-state |
| 73 | + # set pre-state |
92 | 74 | addrArr = self.nodes[1].getnewaddress()
|
93 | 75 | 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") |
| 76 | + received_by_account_json = [r for r in self.nodes[1].listreceivedbyaccount() if r["account"] == account][0] |
97 | 77 | balance_by_account = self.nodes[1].getreceivedbyaccount(account)
|
98 | 78 |
|
99 | 79 | txid = self.nodes[0].sendtoaddress(addr, 0.1)
|
100 | 80 | self.sync_all()
|
101 | 81 |
|
102 | 82 | # listreceivedbyaccount should return received_by_account_json because of 0 confirmations
|
103 | 83 | assert_array_result(self.nodes[1].listreceivedbyaccount(),
|
104 |
| - {"account":account}, |
105 |
| - received_by_account_json) |
| 84 | + {"account": account}, |
| 85 | + received_by_account_json) |
106 | 86 |
|
107 | 87 | # getreceivedbyaddress should return same balance because of 0 confirmations
|
108 | 88 | balance = self.nodes[1].getreceivedbyaccount(account)
|
109 |
| - if balance != balance_by_account: |
110 |
| - raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance)) |
| 89 | + assert_equal(balance, balance_by_account) |
111 | 90 |
|
112 | 91 | self.nodes[1].generate(10)
|
113 | 92 | self.sync_all()
|
114 | 93 | # listreceivedbyaccount should return updated account balance
|
115 | 94 | 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"))}) |
| 95 | + {"account": account}, |
| 96 | + {"account": received_by_account_json["account"], "amount": (received_by_account_json["amount"] + Decimal("0.1"))}) |
118 | 97 |
|
119 | 98 | # getreceivedbyaddress should return updates balance
|
120 | 99 | 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)) |
| 100 | + assert_equal(balance, balance_by_account + Decimal("0.1")) |
123 | 101 |
|
124 |
| - #Create a new account named "mynewaccount" that has a 0 balance |
| 102 | + # Create a new account named "mynewaccount" that has a 0 balance |
125 | 103 | 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") |
| 104 | + received_by_account_json = [r for r in self.nodes[1].listreceivedbyaccount(0, True) if r["account"] == "mynewaccount"][0] |
129 | 105 |
|
130 | 106 | # 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"])) |
| 107 | + assert_equal(received_by_account_json["amount"], Decimal("0.0")) |
133 | 108 |
|
134 | 109 | # Test getreceivedbyaccount for 0 amount accounts
|
135 | 110 | balance = self.nodes[1].getreceivedbyaccount("mynewaccount")
|
136 |
| - if balance != Decimal("0.0"): |
137 |
| - raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance)) |
| 111 | + assert_equal(balance, Decimal("0.0")) |
138 | 112 |
|
139 | 113 | if __name__ == '__main__':
|
140 | 114 | ReceivedByTest().main()
|
0 commit comments