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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 __init__(self):
  24. super().__init__()
  25. self.num_nodes = 4
  26. self.setup_clean_chain = False
  27. def setup_nodes(self):
  28. #This test requires mocktime
  29. enable_mocktime()
  30. return start_nodes(self.num_nodes, self.options.tmpdir)
  31. def run_test(self):
  32. '''
  33. listreceivedbyaddress Test
  34. '''
  35. # Send from node 0 to 1
  36. addr = self.nodes[1].getnewaddress()
  37. txid = self.nodes[0].sendtoaddress(addr, 0.1)
  38. self.sync_all()
  39. #Check not listed in listreceivedbyaddress because has 0 confirmations
  40. 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
  45. self.nodes[1].generate(10)
  46. self.sync_all()
  47. 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
  51. 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)
  56. #Empty Tx
  57. addr = self.nodes[1].getnewaddress()
  58. assert_array_result(self.nodes[1].listreceivedbyaddress(0,True),
  59. {"address":addr},
  60. {"address":addr, "account":"", "amount":0, "confirmations":0, "txids":[]})
  61. '''
  62. getreceivedbyaddress Test
  63. '''
  64. # Send from node 0 to 1
  65. addr = self.nodes[1].getnewaddress()
  66. txid = self.nodes[0].sendtoaddress(addr, 0.1)
  67. self.sync_all()
  68. #Check balance is 0 because of 0 confirmations
  69. balance = self.nodes[1].getreceivedbyaddress(addr)
  70. if balance != Decimal("0.0"):
  71. raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
  72. #Check balance is 0.1
  73. balance = self.nodes[1].getreceivedbyaddress(addr,0)
  74. if balance != Decimal("0.1"):
  75. raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
  76. #Bury Tx under 10 block so it will be returned by the default getreceivedbyaddress
  77. self.nodes[1].generate(10)
  78. self.sync_all()
  79. balance = self.nodes[1].getreceivedbyaddress(addr)
  80. if balance != Decimal("0.1"):
  81. raise AssertionError("Wrong balance returned by getreceivedbyaddress, %0.2f"%(balance))
  82. '''
  83. listreceivedbyaccount + getreceivedbyaccount Test
  84. '''
  85. #set pre-state
  86. addrArr = self.nodes[1].getnewaddress()
  87. account = self.nodes[1].getaccount(addrArr)
  88. received_by_account_json = get_sub_array_from_array(self.nodes[1].listreceivedbyaccount(),{"account":account})
  89. if len(received_by_account_json) == 0:
  90. raise AssertionError("No accounts found in node")
  91. balance_by_account = self.nodes[1].getreceivedbyaccount(account)
  92. txid = self.nodes[0].sendtoaddress(addr, 0.1)
  93. self.sync_all()
  94. # listreceivedbyaccount should return received_by_account_json because of 0 confirmations
  95. assert_array_result(self.nodes[1].listreceivedbyaccount(),
  96. {"account":account},
  97. received_by_account_json)
  98. # getreceivedbyaddress should return same balance because of 0 confirmations
  99. balance = self.nodes[1].getreceivedbyaccount(account)
  100. if balance != balance_by_account:
  101. raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
  102. self.nodes[1].generate(10)
  103. self.sync_all()
  104. # listreceivedbyaccount should return updated account balance
  105. assert_array_result(self.nodes[1].listreceivedbyaccount(),
  106. {"account":account},
  107. {"account":received_by_account_json["account"], "amount":(received_by_account_json["amount"] + Decimal("0.1"))})
  108. # getreceivedbyaddress should return updates balance
  109. balance = self.nodes[1].getreceivedbyaccount(account)
  110. if balance != balance_by_account + Decimal("0.1"):
  111. raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
  112. #Create a new account named "mynewaccount" that has a 0 balance
  113. self.nodes[1].getaccountaddress("mynewaccount")
  114. received_by_account_json = get_sub_array_from_array(self.nodes[1].listreceivedbyaccount(0,True),{"account":"mynewaccount"})
  115. if len(received_by_account_json) == 0:
  116. raise AssertionError("No accounts found in node")
  117. # Test includeempty of listreceivedbyaccount
  118. if received_by_account_json["amount"] != Decimal("0.0"):
  119. raise AssertionError("Wrong balance returned by listreceivedbyaccount, %0.2f"%(received_by_account_json["amount"]))
  120. # Test getreceivedbyaccount for 0 amount accounts
  121. balance = self.nodes[1].getreceivedbyaccount("mynewaccount")
  122. if balance != Decimal("0.0"):
  123. raise AssertionError("Wrong balance returned by getreceivedbyaccount, %0.2f"%(balance))
  124. if __name__ == '__main__':
  125. ReceivedByTest().main()