]> Git Repo - VerusCoin.git/blob - qa/rpc-tests/key_import_export.py
Merge pull request #97 from miketout/dev
[VerusCoin.git] / qa / rpc-tests / key_import_export.py
1 #!/usr/bin/env python
2 # Copyright (c) 2017 The Zcash developers
3 # Distributed under the MIT software license, see the accompanying
4 # file COPYING or https://www.opensource.org/licenses/mit-license.php .
5
6 import sys; assert sys.version_info < (3,), ur"This script does not run under Python 3. Please use Python 2.7.x."
7
8 from decimal import Decimal
9 from test_framework.test_framework import BitcoinTestFramework
10 from test_framework.util import assert_equal, assert_greater_than, start_nodes, initialize_chain_clean, connect_nodes_bi
11
12 import logging
13
14 logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.INFO)
15
16
17 class KeyImportExportTest (BitcoinTestFramework):
18
19     def setup_chain(self):
20         print("Initializing test directory "+self.options.tmpdir)
21         initialize_chain_clean(self.options.tmpdir, 4)
22
23     def setup_network(self, split=False):
24         self.nodes = start_nodes(4, self.options.tmpdir )
25         connect_nodes_bi(self.nodes,0,1)
26         connect_nodes_bi(self.nodes,1,2)
27         connect_nodes_bi(self.nodes,0,2)
28         connect_nodes_bi(self.nodes,0,3)
29         self.is_network_split=False
30         self.sync_all()
31
32     def run_test(self):
33         [alice, bob, charlie, miner] = self.nodes
34
35         def alice_to_bob(amount):
36             alice.sendtoaddress(addr, Decimal(amount))
37             self.sync_all()
38             miner.generate(1)
39             self.sync_all()
40
41         def verify_utxos(node, amounts):
42             utxos = node.listunspent(1, 10**9, [addr])
43
44             def cmp_confirmations_high_to_low(a, b):
45                 return cmp(b["confirmations"], a["confirmations"])
46
47             utxos.sort(cmp_confirmations_high_to_low)
48
49             try:
50                 assert_equal(amounts, [utxo["amount"] for utxo in utxos])
51             except AssertionError:
52                 logging.error(
53                     'Expected amounts: %r; utxos: %r',
54                     amounts, utxos)
55                 raise
56
57         # Seed Alice with some funds
58         alice.generate(10)
59         self.sync_all()
60         miner.generate(100)
61         self.sync_all()
62
63         # Now get a pristine address for receiving transfers:
64         addr = bob.getnewaddress()
65         verify_utxos(bob, [])
66         verify_utxos(charlie, [])
67
68         # the amounts of each txn embodied which generates a single UTXO:
69         amounts = map(Decimal, ['2.3', '3.7', '0.1', '0.5', '1.0', '0.19'])
70
71         # Internal test consistency assertion:
72         assert_greater_than(
73             alice.getbalance(),
74             reduce(Decimal.__add__, amounts))
75
76         logging.info("Sending pre-export txns...")
77         for amount in amounts[0:2]:
78             alice_to_bob(amount)
79
80         logging.info("Exporting privkey from bob...")
81         privkey = bob.dumpprivkey(addr)
82
83         logging.info("Sending post-export txns...")
84         for amount in amounts[2:4]:
85             alice_to_bob(amount)
86
87         verify_utxos(bob, amounts[:4])
88         verify_utxos(charlie, [])
89
90         logging.info("Importing privkey into charlie...")
91         ipkaddr = charlie.importprivkey(privkey, '', True)
92         assert_equal(addr, ipkaddr)
93
94         # importprivkey should have rescanned, so this should pass:
95         verify_utxos(charlie, amounts[:4])
96
97         # Verify idempotent behavior:
98         ipkaddr2 = charlie.importprivkey(privkey, '', True)
99         assert_equal(addr, ipkaddr2)
100
101         # amounts should be unchanged
102         verify_utxos(charlie, amounts[:4])
103
104         logging.info("Sending post-import txns...")
105         for amount in amounts[4:]:
106             alice_to_bob(amount)
107
108         verify_utxos(bob, amounts)
109         verify_utxos(charlie, amounts)
110
111
112 if __name__ == '__main__':
113     KeyImportExportTest().main()
This page took 0.026949 seconds and 4 git commands to generate.