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 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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."""
  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. for item in object_array:
  14. all_match = True
  15. for key,value in to_match.items():
  16. if item[key] != value:
  17. all_match = False
  18. if not all_match:
  19. continue
  20. return item
  21. return []
  22. class ReceivedByTest(BitcoinTestFramework):
  23. def set_test_params(self):
  24. self.num_nodes = 2
  25. self.enable_mocktime()
  26. def run_test(self):
  27. '''
  28. listreceivedbyaddress Test
  29. '''
  30. # Send from node 0 to 1
  31. addr = self.nodes[1].getnewaddress()
  32. txid = self.nodes[0].sendtoaddress(addr, 0.1)
  33. self.sync_all()
  34. #Check not listed in listreceivedbyaddress because has 0 confirmations
  35. assert_array_result(self.nodes[1].listreceivedbyaddress(),
  36. {"address":addr},
  37. { },
  38. True)
  39. #Bury Tx under 10 block so it will be returned by listreceivedbyaddress
  40. self.nodes[1].generate(10)
  41. self.sync_all()
  42. assert_array_result(self.nodes[1].listreceivedbyaddress(),
  43. {"address":addr},
  44. {"address":addr, "account":"", "amount":Decimal("0.1"), "confirmations":10, "txids":[txid,]})
  45. #With min confidence < 10
  46. assert_array_result(self.nodes[1].listreceivedbyaddress(5),
  47. {"address":addr},
  48. {"address":addr, "account":"", "amount":Decimal("0.1"), "confirmations":10, "txids":[txid,]})
  49. #With min confidence > 10, should not find Tx
  50. assert_array_result(self.nodes[1].listreceivedbyaddress(11),{"address":addr},{ },True)
  51. #Empty Tx
  52. addr = self.nodes[1].getnewaddress()
  53. assert_array_result(self.nodes[1].listreceivedbyaddress(0,True),
  54. {"address":addr},
  55. {"address":addr, "account":"", "amount":0, "confirmations":0, "txids":[]})
  56. '''
  57. getreceivedbyaddress Test
  58. '''
  59. # Send from node 0 to 1
  60. addr = self.nodes[1].getnewaddress()
  61. txid = self.nodes[0].sendtoaddress(addr, 0.1)
  62. self.sync_all()
  63. #Check balance is 0 because of 0 confirmations
  64. balance = self.nodes[1].getreceivedbyaddress(addr)
  65. if balance != Decimal("0.0"):
  66. raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
  67. #Check balance is 0.1
  68. balance = self.nodes[1].getreceivedbyaddress(addr,0)
  69. if balance != Decimal("0.1"):
  70. raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
  71. #Bury Tx under 10 block so it will be returned by the default getreceivedbyaddress
  72. self.nodes[1].generate(10)
  73. self.sync_all()
  74. balance = self.nodes[1].getreceivedbyaddress(addr)
  75. if balance != Decimal("0.1"):
  76. raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
  77. '''
  78. listreceivedbyaccount + getreceivedbyaccount Test
  79. '''
  80. #set pre-state
  81. addrArr = self.nodes[1].getnewaddress()
  82. account = self.nodes[1].getaccount(addrArr)
  83. received_by_account_json = get_sub_array_from_array(self.nodes[1].listreceivedbyaccount(),{"account":account})
  84. if len(received_by_account_json) == 0:
  85. raise AssertionError("No accounts found in node")
  86. balance_by_account = self.nodes[1].getreceivedbyaccount(account)
  87. txid = self.nodes[0].sendtoaddress(addr, 0.1)
  88. self.sync_all()
  89. # listreceivedbyaccount should return received_by_account_json because of 0 confirmations
  90. assert_array_result(self.nodes[1].listreceivedbyaccount(),
  91. {"account":account},
  92. received_by_account_json)
  93. # getreceivedbyaddress should return same balance because of 0 confirmations
  94. balance = self.nodes[1].getreceivedbyaccount(account)
  95. if balance != balance_by_account:
  96. raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
  97. self.nodes[1].generate(10)
  98. self.sync_all()
  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"))})
  103. # getreceivedbyaddress should return updates balance
  104. balance = self.nodes[1].getreceivedbyaccount(account)
  105. if balance != balance_by_account + Decimal("0.1"):
  106. raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
  107. #Create a new account named "mynewaccount" that has a 0 balance
  108. self.nodes[1].getaccountaddress("mynewaccount")
  109. received_by_account_json = get_sub_array_from_array(self.nodes[1].listreceivedbyaccount(0,True),{"account":"mynewaccount"})
  110. if len(received_by_account_json) == 0:
  111. raise AssertionError("No accounts found in node")
  112. # Test includeempty of listreceivedbyaccount
  113. if received_by_account_json["amount"] != Decimal("0.0"):
  114. raise AssertionError("Wrong balance returned by listreceivedbyaccount, %0.2f"%(received_by_account_json["amount"]))
  115. # Test getreceivedbyaccount for 0 amount accounts
  116. balance = self.nodes[1].getreceivedbyaccount("mynewaccount")
  117. if balance != Decimal("0.0"):
  118. raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
  119. if __name__ == '__main__':
  120. ReceivedByTest().main()