[tests] Add -blocknotify functional test
[bitcoinplatinum.git] / test / functional / wallet-accounts.py
blob40726d2a760909af1c8fb221e18aa5b9cff49073
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.
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 accounts = ["a", "b", "c", "d", "e"]
76 amount_to_send = 1.0
77 account_addresses = dict()
78 for account in accounts:
79 address = node.getaccountaddress(account)
80 account_addresses[account] = address
82 node.getnewaddress(account)
83 assert_equal(node.getaccount(address), account)
84 assert(address in node.getaddressesbyaccount(account))
86 node.sendfrom("", address, amount_to_send)
88 node.generate(1)
90 for i in range(len(accounts)):
91 from_account = accounts[i]
92 to_account = accounts[(i+1) % len(accounts)]
93 to_address = account_addresses[to_account]
94 node.sendfrom(from_account, to_address, amount_to_send)
96 node.generate(1)
98 for account in accounts:
99 address = node.getaccountaddress(account)
100 assert(address != account_addresses[account])
101 assert_equal(node.getreceivedbyaccount(account), 2)
102 node.move(account, "", node.getbalance(account))
104 node.generate(101)
106 expected_account_balances = {"": 5200}
107 for account in accounts:
108 expected_account_balances[account] = 0
110 assert_equal(node.listaccounts(), expected_account_balances)
112 assert_equal(node.getbalance(""), 5200)
114 for account in accounts:
115 address = node.getaccountaddress("")
116 node.setaccount(address, account)
117 assert(address in node.getaddressesbyaccount(account))
118 assert(address not in node.getaddressesbyaccount(""))
120 for account in accounts:
121 addresses = []
122 for x in range(10):
123 addresses.append(node.getnewaddress())
124 multisig_address = node.addmultisigaddress(5, addresses, account)
125 node.sendfrom("", multisig_address, 50)
127 node.generate(101)
129 for account in accounts:
130 assert_equal(node.getbalance(account), 50)
132 if __name__ == '__main__':
133 WalletAccountsTest().main()