Make sure we use nTxConfirmTarget during Qt fee bumps
[bitcoinplatinum.git] / test / functional / receivedby.py
bloba1cae301c598ae7fec888f81b220fb42fade15f3
1 #!/usr/bin/env python3
2 # Copyright (c) 2014-2016 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."""
7 from test_framework.test_framework import BitcoinTestFramework
8 from test_framework.util import *
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 []
25 class ReceivedByTest(BitcoinTestFramework):
27 def __init__(self):
28 super().__init__()
29 self.num_nodes = 4
30 self.setup_clean_chain = False
32 def setup_nodes(self):
33 #This test requires mocktime
34 enable_mocktime()
35 self.nodes = start_nodes(self.num_nodes, self.options.tmpdir)
37 def run_test(self):
38 '''
39 listreceivedbyaddress Test
40 '''
41 # Send from node 0 to 1
42 addr = self.nodes[1].getnewaddress()
43 txid = self.nodes[0].sendtoaddress(addr, 0.1)
44 self.sync_all()
46 #Check not listed in listreceivedbyaddress because has 0 confirmations
47 assert_array_result(self.nodes[1].listreceivedbyaddress(),
48 {"address":addr},
49 { },
50 True)
51 #Bury Tx under 10 block so it will be returned by listreceivedbyaddress
52 self.nodes[1].generate(10)
53 self.sync_all()
54 assert_array_result(self.nodes[1].listreceivedbyaddress(),
55 {"address":addr},
56 {"address":addr, "account":"", "amount":Decimal("0.1"), "confirmations":10, "txids":[txid,]})
57 #With min confidence < 10
58 assert_array_result(self.nodes[1].listreceivedbyaddress(5),
59 {"address":addr},
60 {"address":addr, "account":"", "amount":Decimal("0.1"), "confirmations":10, "txids":[txid,]})
61 #With min confidence > 10, should not find Tx
62 assert_array_result(self.nodes[1].listreceivedbyaddress(11),{"address":addr},{ },True)
64 #Empty Tx
65 addr = self.nodes[1].getnewaddress()
66 assert_array_result(self.nodes[1].listreceivedbyaddress(0,True),
67 {"address":addr},
68 {"address":addr, "account":"", "amount":0, "confirmations":0, "txids":[]})
70 '''
71 getreceivedbyaddress Test
72 '''
73 # Send from node 0 to 1
74 addr = self.nodes[1].getnewaddress()
75 txid = self.nodes[0].sendtoaddress(addr, 0.1)
76 self.sync_all()
78 #Check balance is 0 because of 0 confirmations
79 balance = self.nodes[1].getreceivedbyaddress(addr)
80 if balance != Decimal("0.0"):
81 raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
83 #Check balance is 0.1
84 balance = self.nodes[1].getreceivedbyaddress(addr,0)
85 if balance != Decimal("0.1"):
86 raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
88 #Bury Tx under 10 block so it will be returned by the default getreceivedbyaddress
89 self.nodes[1].generate(10)
90 self.sync_all()
91 balance = self.nodes[1].getreceivedbyaddress(addr)
92 if balance != Decimal("0.1"):
93 raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
95 '''
96 listreceivedbyaccount + getreceivedbyaccount Test
97 '''
98 #set pre-state
99 addrArr = self.nodes[1].getnewaddress()
100 account = self.nodes[1].getaccount(addrArr)
101 received_by_account_json = get_sub_array_from_array(self.nodes[1].listreceivedbyaccount(),{"account":account})
102 if len(received_by_account_json) == 0:
103 raise AssertionError("No accounts found in node")
104 balance_by_account = self.nodes[1].getreceivedbyaccount(account)
106 txid = self.nodes[0].sendtoaddress(addr, 0.1)
107 self.sync_all()
109 # listreceivedbyaccount should return received_by_account_json because of 0 confirmations
110 assert_array_result(self.nodes[1].listreceivedbyaccount(),
111 {"account":account},
112 received_by_account_json)
114 # getreceivedbyaddress should return same balance because of 0 confirmations
115 balance = self.nodes[1].getreceivedbyaccount(account)
116 if balance != balance_by_account:
117 raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
119 self.nodes[1].generate(10)
120 self.sync_all()
121 # listreceivedbyaccount should return updated account balance
122 assert_array_result(self.nodes[1].listreceivedbyaccount(),
123 {"account":account},
124 {"account":received_by_account_json["account"], "amount":(received_by_account_json["amount"] + Decimal("0.1"))})
126 # getreceivedbyaddress should return updates balance
127 balance = self.nodes[1].getreceivedbyaccount(account)
128 if balance != balance_by_account + Decimal("0.1"):
129 raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
131 #Create a new account named "mynewaccount" that has a 0 balance
132 self.nodes[1].getaccountaddress("mynewaccount")
133 received_by_account_json = get_sub_array_from_array(self.nodes[1].listreceivedbyaccount(0,True),{"account":"mynewaccount"})
134 if len(received_by_account_json) == 0:
135 raise AssertionError("No accounts found in node")
137 # Test includeempty of listreceivedbyaccount
138 if received_by_account_json["amount"] != Decimal("0.0"):
139 raise AssertionError("Wrong balance returned by listreceivedbyaccount, %0.2f"%(received_by_account_json["amount"]))
141 # Test getreceivedbyaccount for 0 amount accounts
142 balance = self.nodes[1].getreceivedbyaccount("mynewaccount")
143 if balance != Decimal("0.0"):
144 raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
146 if __name__ == '__main__':
147 ReceivedByTest().main()