Skip to content

Commit 5f21321

Browse files
committed
tests: add tests for cross-chain wallet use prevention
1 parent 9687659 commit 5f21321

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@
252252
'rpc_bind.py --ipv4',
253253
'rpc_bind.py --ipv6',
254254
'rpc_bind.py --nonloopback',
255+
'wallet_crosschain.py',
255256
'mining_basic.py',
256257
'feature_signet.py',
257258
'wallet_bumpfee.py --legacy-wallet',

test/functional/wallet_crosschain.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) 2020 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+
import os
7+
8+
from test_framework.test_framework import BitcoinTestFramework
9+
from test_framework.util import assert_raises_rpc_error
10+
11+
class WalletCrossChain(BitcoinTestFramework):
12+
def set_test_params(self):
13+
self.num_nodes = 2
14+
self.setup_clean_chain = True
15+
16+
def skip_test_if_missing_module(self):
17+
self.skip_if_no_wallet()
18+
19+
def setup_network(self):
20+
self.add_nodes(self.num_nodes)
21+
22+
# Switch node 1 to testnet before starting it.
23+
self.nodes[1].chain = 'testnet3'
24+
self.nodes[1].extra_args = ['-maxconnections=0'] # disable testnet sync
25+
with open(self.nodes[1].bitcoinconf, 'r', encoding='utf8') as conf:
26+
conf_data = conf.read()
27+
with open (self.nodes[1].bitcoinconf, 'w', encoding='utf8') as conf:
28+
conf.write(conf_data.replace('regtest=', 'testnet=').replace('[regtest]', '[test]'))
29+
30+
self.start_nodes()
31+
32+
def run_test(self):
33+
self.log.info("Creating wallets")
34+
35+
node0_wallet = os.path.join(self.nodes[0].datadir, 'node0_wallet')
36+
self.nodes[0].createwallet(node0_wallet)
37+
self.nodes[0].unloadwallet(node0_wallet)
38+
node1_wallet = os.path.join(self.nodes[1].datadir, 'node1_wallet')
39+
self.nodes[1].createwallet(node1_wallet)
40+
self.nodes[1].unloadwallet(node1_wallet)
41+
42+
self.log.info("Loading wallets into nodes with a different genesis blocks")
43+
44+
if self.options.descriptors:
45+
assert_raises_rpc_error(-18, 'Wallet file verification failed.', self.nodes[0].loadwallet, node1_wallet)
46+
assert_raises_rpc_error(-18, 'Wallet file verification failed.', self.nodes[1].loadwallet, node0_wallet)
47+
else:
48+
assert_raises_rpc_error(-4, 'Wallet files should not be reused across chains.', self.nodes[0].loadwallet, node1_wallet)
49+
assert_raises_rpc_error(-4, 'Wallet files should not be reused across chains.', self.nodes[1].loadwallet, node0_wallet)
50+
51+
if not self.options.descriptors:
52+
self.log.info("Override cross-chain wallet load protection")
53+
self.stop_nodes()
54+
self.start_nodes([['-walletcrosschain']] * self.num_nodes)
55+
self.nodes[0].loadwallet(node1_wallet)
56+
self.nodes[1].loadwallet(node0_wallet)
57+
58+
59+
if __name__ == '__main__':
60+
WalletCrossChain().main()

0 commit comments

Comments
 (0)