Dash Core  0.12.2.1
P2P Digital Currency
receivedby.py
Go to the documentation of this file.
1 #!/usr/bin/env python2
2 # Copyright (c) 2014-2015 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 
6 # Exercise the listreceivedbyaddress API
7 
8 from test_framework.test_framework import BitcoinTestFramework
9 from test_framework.util import *
10 
11 
12 def get_sub_array_from_array(object_array, to_match):
13  '''
14  Finds and returns a sub array from an array of arrays.
15  to_match should be a unique idetifier of a sub array
16  '''
17  num_matched = 0
18  for item in object_array:
19  all_match = True
20  for key,value in to_match.items():
21  if item[key] != value:
22  all_match = False
23  if not all_match:
24  continue
25  return item
26  return []
27 
29 
30  def setup_nodes(self):
31  #This test requires mocktime
33  return start_nodes(4, self.options.tmpdir)
34 
35  def run_test(self):
36  '''
37  listreceivedbyaddress Test
38  '''
39  # Send from node 0 to 1
40  addr = self.nodes[1].getnewaddress()
41  txid = self.nodes[0].sendtoaddress(addr, 0.1)
42  self.sync_all()
43 
44  #Check not listed in listreceivedbyaddress because has 0 confirmations
46  {"address":addr},
47  { },
48  True)
49  #Bury Tx under 10 block so it will be returned by listreceivedbyaddress
50  self.nodes[1].generate(10)
51  self.sync_all()
53  {"address":addr},
54  {"address":addr, "account":"", "amount":Decimal("0.1"), "confirmations":10, "txids":[txid,]})
55  #With min confidence < 10
57  {"address":addr},
58  {"address":addr, "account":"", "amount":Decimal("0.1"), "confirmations":10, "txids":[txid,]})
59  #With min confidence > 10, should not find Tx
60  assert_array_result(self.nodes[1].listreceivedbyaddress(11),{"address":addr},{ },True)
61 
62  #Empty Tx
63  addr = self.nodes[1].getnewaddress()
64  assert_array_result(self.nodes[1].listreceivedbyaddress(0, False, True),
65  {"address":addr},
66  {"address":addr, "account":"", "amount":0, "confirmations":0, "txids":[]})
67 
68  '''
69  getreceivedbyaddress Test
70  '''
71  # Send from node 0 to 1
72  addr = self.nodes[1].getnewaddress()
73  txid = self.nodes[0].sendtoaddress(addr, 0.1)
74  self.sync_all()
75 
76  #Check balance is 0 because of 0 confirmations
77  balance = self.nodes[1].getreceivedbyaddress(addr)
78  if balance != Decimal("0.0"):
79  raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
80 
81  #Check balance is 0.1
82  balance = self.nodes[1].getreceivedbyaddress(addr,0)
83  if balance != Decimal("0.1"):
84  raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
85 
86  #Bury Tx under 10 block so it will be returned by the default getreceivedbyaddress
87  self.nodes[1].generate(10)
88  self.sync_all()
89  balance = self.nodes[1].getreceivedbyaddress(addr)
90  if balance != Decimal("0.1"):
91  raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
92 
93  '''
94  listreceivedbyaccount + getreceivedbyaccount Test
95  '''
96  #set pre-state
97  addrArr = self.nodes[1].getnewaddress()
98  account = self.nodes[1].getaccount(addrArr)
99  received_by_account_json = get_sub_array_from_array(self.nodes[1].listreceivedbyaccount(),{"account":account})
100  if len(received_by_account_json) == 0:
101  raise AssertionError("No accounts found in node")
102  balance_by_account = rec_by_accountArr = self.nodes[1].getreceivedbyaccount(account)
103 
104  txid = self.nodes[0].sendtoaddress(addr, 0.1)
105  self.sync_all()
106 
107  # listreceivedbyaccount should return received_by_account_json because of 0 confirmations
109  {"account":account},
110  received_by_account_json)
111 
112  # getreceivedbyaddress should return same balance because of 0 confirmations
113  balance = self.nodes[1].getreceivedbyaccount(account)
114  if balance != balance_by_account:
115  raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
116 
117  self.nodes[1].generate(10)
118  self.sync_all()
119  # listreceivedbyaccount should return updated account balance
121  {"account":account},
122  {"account":received_by_account_json["account"], "amount":(received_by_account_json["amount"] + Decimal("0.1"))})
123 
124  # getreceivedbyaddress should return updates balance
125  balance = self.nodes[1].getreceivedbyaccount(account)
126  if balance != balance_by_account + Decimal("0.1"):
127  raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
128 
129  #Create a new account named "mynewaccount" that has a 0 balance
130  self.nodes[1].getaccountaddress("mynewaccount")
131  received_by_account_json = get_sub_array_from_array(self.nodes[1].listreceivedbyaccount(0, False, True),{"account":"mynewaccount"})
132  if len(received_by_account_json) == 0:
133  raise AssertionError("No accounts found in node")
134 
135  # Test includeempty of listreceivedbyaccount
136  if received_by_account_json["amount"] != Decimal("0.0"):
137  raise AssertionError("Wrong balance returned by listreceivedbyaccount, %0.2f"%(received_by_account_json["amount"]))
138 
139  # Test getreceivedbyaccount for 0 amount accounts
140  balance = self.nodes[1].getreceivedbyaccount("mynewaccount")
141  if balance != Decimal("0.0"):
142  raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
143 
144 if __name__ == '__main__':
145  ReceivedByTest().main()
UniValue listreceivedbyaccount(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:1380
def assert_array_result(object_array, to_match, expected, should_not_find=False)
Definition: util.py:496
UniValue getreceivedbyaccount(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:701
def start_nodes(num_nodes, dirname, extra_args=None, rpchost=None, binary=None)
Definition: util.py:305
UniValue getreceivedbyaddress(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:641
UniValue getaccount(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:305
UniValue getaccountaddress(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:192
UniValue sendtoaddress(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:409
UniValue getnewaddress(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:113
def enable_mocktime()
Definition: util.py:38
def get_sub_array_from_array(object_array, to_match)
Definition: receivedby.py:12
UniValue generate(const UniValue &params, bool fHelp)
Definition: mining.cpp:122
UniValue listreceivedbyaddress(const UniValue &params, bool fHelp)
Definition: rpcwallet.cpp:1339