Skip to content

Commit bb2646a

Browse files
committed
Merge #8309: [qa] Add wallet-hd test
fade505 [qa] Add wallet-hd test (MarcoFalke) fa9976b [qa] test_framework: Add wrapper for stop_node (MarcoFalke)
2 parents 6ae20df + fade505 commit bb2646a

File tree

3 files changed

+82
-0
lines changed

3 files changed

+82
-0
lines changed

qa/pull-tester/rpc-tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@
106106
'walletbackup.py',
107107
'bip68-112-113-p2p.py',
108108
'wallet.py',
109+
'wallet-hd.py',
109110
'listtransactions.py',
110111
'receivedby.py',
111112
'mempool_resurrect_test.py',

qa/rpc-tests/test_framework/test_framework.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
sync_blocks,
2121
sync_mempools,
2222
stop_nodes,
23+
stop_node,
2324
wait_bitcoinds,
2425
enable_coverage,
2526
check_json_precision,
@@ -49,6 +50,9 @@ def setup_chain(self):
4950
else:
5051
initialize_chain(self.options.tmpdir, self.num_nodes)
5152

53+
def stop_node(self, num_node):
54+
stop_node(self.nodes[num_node], num_node)
55+
5256
def setup_nodes(self):
5357
return start_nodes(self.num_nodes, self.options.tmpdir)
5458

qa/rpc-tests/wallet-hd.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
6+
from test_framework.test_framework import BitcoinTestFramework
7+
from test_framework.util import (
8+
start_nodes,
9+
start_node,
10+
assert_equal,
11+
connect_nodes_bi,
12+
)
13+
import os
14+
import shutil
15+
16+
17+
class WalletHDTest(BitcoinTestFramework):
18+
19+
def __init__(self):
20+
super().__init__()
21+
self.setup_clean_chain = True
22+
self.num_nodes = 2
23+
self.node_args = [['-usehd=0'], ['-usehd=1', '-keypool=0']]
24+
25+
def setup_network(self):
26+
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, self.node_args)
27+
self.is_network_split = False
28+
connect_nodes_bi(self.nodes, 0, 1)
29+
30+
def run_test (self):
31+
tmpdir = self.options.tmpdir
32+
33+
# Import a non-HD private key in the HD wallet
34+
non_hd_add = self.nodes[0].getnewaddress()
35+
self.nodes[1].importprivkey(self.nodes[0].dumpprivkey(non_hd_add))
36+
37+
# This should be enough to keep the master key and the non-HD key
38+
self.nodes[1].backupwallet(tmpdir + "hd.bak")
39+
#self.nodes[1].dumpwallet(tmpdir + "hd.dump")
40+
41+
# Derive some HD addresses and remember the last
42+
# Also send funds to each add
43+
self.nodes[0].generate(101)
44+
hd_add = None
45+
num_hd_adds = 300
46+
for _ in range(num_hd_adds):
47+
hd_add = self.nodes[1].getnewaddress()
48+
self.nodes[0].sendtoaddress(hd_add, 1)
49+
self.nodes[0].generate(1)
50+
self.nodes[0].sendtoaddress(non_hd_add, 1)
51+
self.nodes[0].generate(1)
52+
53+
self.sync_all()
54+
assert_equal(self.nodes[1].getbalance(), num_hd_adds + 1)
55+
56+
print("Restore backup ...")
57+
self.stop_node(1)
58+
os.remove(self.options.tmpdir + "/node1/regtest/wallet.dat")
59+
shutil.copyfile(tmpdir + "hd.bak", tmpdir + "/node1/regtest/wallet.dat")
60+
self.nodes[1] = start_node(1, self.options.tmpdir, self.node_args[1])
61+
#connect_nodes_bi(self.nodes, 0, 1)
62+
63+
# Assert that derivation is deterministic
64+
hd_add_2 = None
65+
for _ in range(num_hd_adds):
66+
hd_add_2 = self.nodes[1].getnewaddress()
67+
assert_equal(hd_add, hd_add_2)
68+
69+
# Needs rescan
70+
self.stop_node(1)
71+
self.nodes[1] = start_node(1, self.options.tmpdir, self.node_args[1] + ['-rescan'])
72+
#connect_nodes_bi(self.nodes, 0, 1)
73+
assert_equal(self.nodes[1].getbalance(), num_hd_adds + 1)
74+
75+
76+
if __name__ == '__main__':
77+
WalletHDTest().main ()

0 commit comments

Comments
 (0)