You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

receivedby.py 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. # Exercise the listreceivedbyaddress API
  6. from test_framework.test_framework import BitcoinTestFramework
  7. from test_framework.util import *
  8. def get_sub_array_from_array(object_array, to_match):
  9. '''
  10. Finds and returns a sub array from an array of arrays.
  11. to_match should be a unique idetifier of a sub array
  12. '''
  13. num_matched = 0
  14. for item in object_array:
  15. all_match = True
  16. for key,value in to_match.items():
  17. if item[key] != value:
  18. all_match = False
  19. if not all_match:
  20. continue
  21. return item
  22. return []
  23. def check_array_result(object_array, to_match, expected, should_not_find = False):
  24. """
  25. Pass in array of JSON objects, a dictionary with key/value pairs
  26. to match against, and another dictionary with expected key/value
  27. pairs.
  28. If the should_not_find flag is true, to_match should not be found in object_array
  29. """
  30. if should_not_find == True:
  31. expected = { }
  32. num_matched = 0
  33. for item in object_array:
  34. all_match = True
  35. for key,value in to_match.items():
  36. if item[key] != value:
  37. all_match = False
  38. if not all_match:
  39. continue
  40. for key,value in expected.items():
  41. if item[key] != value:
  42. raise AssertionError("%s : expected %s=%s"%(str(item), str(key), str(value)))
  43. num_matched = num_matched+1
  44. if num_matched == 0 and should_not_find != True:
  45. raise AssertionError("No objects matched %s"%(str(to_match)))
  46. if num_matched > 0 and should_not_find == True:
  47. raise AssertionError("Objects was matched %s"%(str(to_match)))
  48. class ReceivedByTest(BitcoinTestFramework):
  49. def run_test(self):
  50. '''
  51. listreceivedbyaddress Test
  52. '''
  53. # Send from node 0 to 1
  54. addr = self.nodes[1].getnewaddress()
  55. txid = self.nodes[0].sendtoaddress(addr, 0.1)
  56. self.sync_all()
  57. #Check not listed in listreceivedbyaddress because has 0 confirmations
  58. check_array_result(self.nodes[1].listreceivedbyaddress(),
  59. {"address":addr},
  60. { },
  61. True)
  62. #Bury Tx under 10 block so it will be returned by listreceivedbyaddress
  63. self.nodes[1].generate(10)
  64. self.sync_all()
  65. check_array_result(self.nodes[1].listreceivedbyaddress(),
  66. {"address":addr},
  67. {"address":addr, "account":"", "amount":Decimal("0.1"), "confirmations":10, "txids":[txid,]})
  68. #With min confidence < 10
  69. check_array_result(self.nodes[1].listreceivedbyaddress(5),
  70. {"address":addr},
  71. {"address":addr, "account":"", "amount":Decimal("0.1"), "confirmations":10, "txids":[txid,]})
  72. #With min confidence > 10, should not find Tx
  73. check_array_result(self.nodes[1].listreceivedbyaddress(11),{"address":addr},{ },True)
  74. #Empty Tx
  75. addr = self.nodes[1].getnewaddress()
  76. check_array_result(self.nodes[1].listreceivedbyaddress(0,True),
  77. {"address":addr},
  78. {"address":addr, "account":"", "amount":0, "confirmations":0, "txids":[]})
  79. '''
  80. getreceivedbyaddress Test
  81. '''
  82. # Send from node 0 to 1
  83. addr = self.nodes[1].getnewaddress()
  84. txid = self.nodes[0].sendtoaddress(addr, 0.1)
  85. self.sync_all()
  86. #Check balance is 0 because of 0 confirmations
  87. balance = self.nodes[1].getreceivedbyaddress(addr)
  88. if balance != Decimal("0.0"):
  89. raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
  90. #Check balance is 0.1
  91. balance = self.nodes[1].getreceivedbyaddress(addr,0)
  92. if balance != Decimal("0.1"):
  93. raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
  94. #Bury Tx under 10 block so it will be returned by the default getreceivedbyaddress
  95. self.nodes[1].generate(10)
  96. self.sync_all()
  97. balance = self.nodes[1].getreceivedbyaddress(addr)
  98. if balance != Decimal("0.1"):
  99. raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
  100. '''
  101. listreceivedbyaccount + getreceivedbyaccount Test
  102. '''
  103. #set pre-state
  104. addrArr = self.nodes[1].getnewaddress()
  105. account = self.nodes[1].getaccount(addrArr)
  106. received_by_account_json = get_sub_array_from_array(self.nodes[1].listreceivedbyaccount(),{"account":account})
  107. if len(received_by_account_json) == 0:
  108. raise AssertionError("No accounts found in node")
  109. balance_by_account = rec_by_accountArr = self.nodes[1].getreceivedbyaccount(account)
  110. txid = self.nodes[0].sendtoaddress(addr, 0.1)
  111. self.sync_all()
  112. # listreceivedbyaccount should return received_by_account_json because of 0 confirmations
  113. check_array_result(self.nodes[1].listreceivedbyaccount(),
  114. {"account":account},
  115. received_by_account_json)
  116. # getreceivedbyaddress should return same balance because of 0 confirmations
  117. balance = self.nodes[1].getreceivedbyaccount(account)
  118. if balance != balance_by_account:
  119. raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
  120. self.nodes[1].generate(10)
  121. self.sync_all()
  122. # listreceivedbyaccount should return updated account balance
  123. check_array_result(self.nodes[1].listreceivedbyaccount(),
  124. {"account":account},
  125. {"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))
  130. #Create a new account named "mynewaccount" that has a 0 balance
  131. self.nodes[1].getaccountaddress("mynewaccount")
  132. received_by_account_json = get_sub_array_from_array(self.nodes[1].listreceivedbyaccount(0,True),{"account":"mynewaccount"})
  133. if len(received_by_account_json) == 0:
  134. raise AssertionError("No accounts found in node")
  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. # Test getreceivedbyaccount for 0 amount accounts
  139. balance = self.nodes[1].getreceivedbyaccount("mynewaccount")
  140. if balance != Decimal("0.0"):
  141. raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
  142. if __name__ == '__main__':
  143. ReceivedByTest().main()