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.

wallet-accounts.py 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #!/usr/bin/env python3
  2. # Copyright (c) 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 account RPCs.
  6. RPCs tested are:
  7. - getaccountaddress
  8. - getaddressesbyaccount
  9. - listaddressgroupings
  10. - setaccount
  11. - sendfrom (with account arguments)
  12. - move (with account arguments)
  13. """
  14. from test_framework.test_framework import BitcoinTestFramework
  15. from test_framework.util import assert_equal
  16. class WalletAccountsTest(BitcoinTestFramework):
  17. def set_test_params(self):
  18. self.setup_clean_chain = True
  19. self.num_nodes = 1
  20. self.extra_args = [[]]
  21. def run_test(self):
  22. node = self.nodes[0]
  23. # Check that there's no UTXO on any of the nodes
  24. assert_equal(len(node.listunspent()), 0)
  25. # Note each time we call generate, all generated coins go into
  26. # the same address, so we call twice to get two addresses w/50 each
  27. node.generate(1)
  28. node.generate(101)
  29. assert_equal(node.getbalance(), 100)
  30. # there should be 2 address groups
  31. # each with 1 address with a balance of 50 Bitcoins
  32. address_groups = node.listaddressgroupings()
  33. assert_equal(len(address_groups), 2)
  34. # the addresses aren't linked now, but will be after we send to the
  35. # common address
  36. linked_addresses = set()
  37. for address_group in address_groups:
  38. assert_equal(len(address_group), 1)
  39. assert_equal(len(address_group[0]), 2)
  40. assert_equal(address_group[0][1], 50)
  41. linked_addresses.add(address_group[0][0])
  42. # send 50 from each address to a third address not in this wallet
  43. # There's some fee that will come back to us when the miner reward
  44. # matures.
  45. common_address = "msf4WtN1YQKXvNtvdFYt9JBnUD2FB41kjr"
  46. txid = node.sendmany(
  47. fromaccount="",
  48. amounts={common_address: 100},
  49. subtractfeefrom=[common_address],
  50. minconf=1,
  51. )
  52. tx_details = node.gettransaction(txid)
  53. fee = -tx_details['details'][0]['fee']
  54. # there should be 1 address group, with the previously
  55. # unlinked addresses now linked (they both have 0 balance)
  56. address_groups = node.listaddressgroupings()
  57. assert_equal(len(address_groups), 1)
  58. assert_equal(len(address_groups[0]), 2)
  59. assert_equal(set([a[0] for a in address_groups[0]]), linked_addresses)
  60. assert_equal([a[1] for a in address_groups[0]], [0, 0])
  61. node.generate(1)
  62. # we want to reset so that the "" account has what's expected.
  63. # otherwise we're off by exactly the fee amount as that's mined
  64. # and matures in the next 100 blocks
  65. node.sendfrom("", common_address, fee)
  66. accounts = ["a", "b", "c", "d", "e"]
  67. amount_to_send = 1.0
  68. account_addresses = dict()
  69. for account in accounts:
  70. address = node.getaccountaddress(account)
  71. account_addresses[account] = address
  72. node.getnewaddress(account)
  73. assert_equal(node.getaccount(address), account)
  74. assert(address in node.getaddressesbyaccount(account))
  75. node.sendfrom("", address, amount_to_send)
  76. node.generate(1)
  77. for i in range(len(accounts)):
  78. from_account = accounts[i]
  79. to_account = accounts[(i+1) % len(accounts)]
  80. to_address = account_addresses[to_account]
  81. node.sendfrom(from_account, to_address, amount_to_send)
  82. node.generate(1)
  83. for account in accounts:
  84. address = node.getaccountaddress(account)
  85. assert(address != account_addresses[account])
  86. assert_equal(node.getreceivedbyaccount(account), 2)
  87. node.move(account, "", node.getbalance(account))
  88. node.generate(101)
  89. expected_account_balances = {"": 5200}
  90. for account in accounts:
  91. expected_account_balances[account] = 0
  92. assert_equal(node.listaccounts(), expected_account_balances)
  93. assert_equal(node.getbalance(""), 5200)
  94. for account in accounts:
  95. address = node.getaccountaddress("")
  96. node.setaccount(address, account)
  97. assert(address in node.getaddressesbyaccount(account))
  98. assert(address not in node.getaddressesbyaccount(""))
  99. for account in accounts:
  100. addresses = []
  101. for x in range(10):
  102. addresses.append(node.getnewaddress())
  103. multisig_address = node.addmultisigaddress(5, addresses, account)
  104. node.sendfrom("", multisig_address, 50)
  105. node.generate(101)
  106. for account in accounts:
  107. assert_equal(node.getbalance(account), 50)
  108. if __name__ == '__main__':
  109. WalletAccountsTest().main()