Merge #12079: Improve prioritisetransaction test coverage
[bitcoinplatinum.git] / test / functional / wallet-accounts.py
blob287436877f60501bccf9f2874a21e2037d13646c
1 #!/usr/bin/env python3
2 # Copyright (c) 2016-2017 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.
7 RPCs tested are:
8 - getaccountaddress
9 - getaddressesbyaccount
10 - listaddressgroupings
11 - setaccount
12 - sendfrom (with account arguments)
13 - move (with account arguments)
14 """
16 from test_framework.test_framework import BitcoinTestFramework
17 from test_framework.util import assert_equal
19 class WalletAccountsTest(BitcoinTestFramework):
20 def set_test_params(self):
21 self.setup_clean_chain = True
22 self.num_nodes = 1
23 self.extra_args = [[]]
25 def run_test(self):
26 node = self.nodes[0]
27 # Check that there's no UTXO on any of the nodes
28 assert_equal(len(node.listunspent()), 0)
30 # Note each time we call generate, all generated coins go into
31 # the same address, so we call twice to get two addresses w/50 each
32 node.generate(1)
33 node.generate(101)
34 assert_equal(node.getbalance(), 100)
36 # there should be 2 address groups
37 # each with 1 address with a balance of 50 Bitcoins
38 address_groups = node.listaddressgroupings()
39 assert_equal(len(address_groups), 2)
40 # the addresses aren't linked now, but will be after we send to the
41 # common address
42 linked_addresses = set()
43 for address_group in address_groups:
44 assert_equal(len(address_group), 1)
45 assert_equal(len(address_group[0]), 2)
46 assert_equal(address_group[0][1], 50)
47 linked_addresses.add(address_group[0][0])
49 # send 50 from each address to a third address not in this wallet
50 # There's some fee that will come back to us when the miner reward
51 # matures.
52 common_address = "msf4WtN1YQKXvNtvdFYt9JBnUD2FB41kjr"
53 txid = node.sendmany(
54 fromaccount="",
55 amounts={common_address: 100},
56 subtractfeefrom=[common_address],
57 minconf=1,
59 tx_details = node.gettransaction(txid)
60 fee = -tx_details['details'][0]['fee']
61 # there should be 1 address group, with the previously
62 # unlinked addresses now linked (they both have 0 balance)
63 address_groups = node.listaddressgroupings()
64 assert_equal(len(address_groups), 1)
65 assert_equal(len(address_groups[0]), 2)
66 assert_equal(set([a[0] for a in address_groups[0]]), linked_addresses)
67 assert_equal([a[1] for a in address_groups[0]], [0, 0])
69 node.generate(1)
71 # we want to reset so that the "" account has what's expected.
72 # otherwise we're off by exactly the fee amount as that's mined
73 # and matures in the next 100 blocks
74 node.sendfrom("", common_address, fee)
75 amount_to_send = 1.0
77 # Create accounts and make sure subsequent account API calls
78 # recognize the account/address associations.
79 accounts = [Account(name) for name in ("a", "b", "c", "d", "e")]
80 for account in accounts:
81 account.add_receive_address(node.getaccountaddress(account.name))
82 account.verify(node)
84 # Send a transaction to each account, and make sure this forces
85 # getaccountaddress to generate a new receiving address.
86 for account in accounts:
87 node.sendtoaddress(account.receive_address, amount_to_send)
88 account.add_receive_address(node.getaccountaddress(account.name))
89 account.verify(node)
91 # Check the amounts received.
92 node.generate(1)
93 for account in accounts:
94 assert_equal(
95 node.getreceivedbyaddress(account.addresses[0]), amount_to_send)
96 assert_equal(node.getreceivedbyaccount(account.name), amount_to_send)
98 # Check that sendfrom account reduces listaccounts balances.
99 for i, account in enumerate(accounts):
100 to_account = accounts[(i+1) % len(accounts)]
101 node.sendfrom(account.name, to_account.receive_address, amount_to_send)
102 node.generate(1)
103 for account in accounts:
104 account.add_receive_address(node.getaccountaddress(account.name))
105 account.verify(node)
106 assert_equal(node.getreceivedbyaccount(account.name), 2)
107 node.move(account.name, "", node.getbalance(account.name))
108 account.verify(node)
109 node.generate(101)
110 expected_account_balances = {"": 5200}
111 for account in accounts:
112 expected_account_balances[account.name] = 0
113 assert_equal(node.listaccounts(), expected_account_balances)
114 assert_equal(node.getbalance(""), 5200)
116 # Check that setaccount can assign an account to a new unused address.
117 for account in accounts:
118 address = node.getaccountaddress("")
119 node.setaccount(address, account.name)
120 account.add_address(address)
121 account.verify(node)
122 assert(address not in node.getaddressesbyaccount(""))
124 # Check that addmultisigaddress can assign accounts.
125 for account in accounts:
126 addresses = []
127 for x in range(10):
128 addresses.append(node.getnewaddress())
129 multisig_address = node.addmultisigaddress(5, addresses, account.name)
130 account.add_address(multisig_address)
131 account.verify(node)
132 node.sendfrom("", multisig_address, 50)
133 node.generate(101)
134 for account in accounts:
135 assert_equal(node.getbalance(account.name), 50)
137 # Check that setaccount can change the account of an address from a
138 # different account.
139 change_account(node, accounts[0].addresses[0], accounts[0], accounts[1])
141 # Check that setaccount can change the account of an address which
142 # is the receiving address of a different account.
143 change_account(node, accounts[0].receive_address, accounts[0], accounts[1])
145 # Check that setaccount can set the account of an address already
146 # in the account. This is a no-op.
147 change_account(node, accounts[2].addresses[0], accounts[2], accounts[2])
149 # Check that setaccount can set the account of an address which is
150 # already the receiving address of the account. It would probably make
151 # sense for this to be a no-op, but right now it resets the receiving
152 # address, causing getaccountaddress to return a brand new address.
153 change_account(node, accounts[2].receive_address, accounts[2], accounts[2])
155 class Account:
156 def __init__(self, name):
157 # Account name
158 self.name = name
159 # Current receiving address associated with this account.
160 self.receive_address = None
161 # List of all addresses assigned with this account
162 self.addresses = []
164 def add_address(self, address):
165 assert_equal(address not in self.addresses, True)
166 self.addresses.append(address)
168 def add_receive_address(self, address):
169 self.add_address(address)
170 self.receive_address = address
172 def verify(self, node):
173 if self.receive_address is not None:
174 assert self.receive_address in self.addresses
175 assert_equal(node.getaccountaddress(self.name), self.receive_address)
177 for address in self.addresses:
178 assert_equal(node.getaccount(address), self.name)
180 assert_equal(
181 set(node.getaddressesbyaccount(self.name)), set(self.addresses))
184 def change_account(node, address, old_account, new_account):
185 assert_equal(address in old_account.addresses, True)
186 node.setaccount(address, new_account.name)
188 old_account.addresses.remove(address)
189 new_account.add_address(address)
191 # Calling setaccount on an address which was previously the receiving
192 # address of a different account should reset the receiving address of
193 # the old account, causing getaccountaddress to return a brand new
194 # address.
195 if address == old_account.receive_address:
196 new_address = node.getaccountaddress(old_account.name)
197 assert_equal(new_address not in old_account.addresses, True)
198 assert_equal(new_address not in new_account.addresses, True)
199 old_account.add_receive_address(new_address)
201 old_account.verify(node)
202 new_account.verify(node)
205 if __name__ == '__main__':
206 WalletAccountsTest().main()