Skip to content

Commit c5e8bd6

Browse files
committed
merge bitcoin#18554: ensure wallet files are not reused across chains
1 parent 0fca914 commit c5e8bd6

File tree

6 files changed

+79
-1
lines changed

6 files changed

+79
-1
lines changed

src/dummywallet.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ void DummyWalletInit::AddWalletOptions(ArgsManager& argsman) const
7878
"-flushwallet",
7979
"-privdb",
8080
"-walletrejectlongchains",
81-
"-unsafesqlitesync"
81+
"-walletcrosschain",
82+
"-unsafesqlitesync",
8283
});
8384
}
8485

src/wallet/init.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ void WalletInit::AddWalletOptions(ArgsManager& argsman) const
121121
#endif
122122

123123
argsman.AddArg("-walletrejectlongchains", strprintf("Wallet will not create transactions that violate mempool chain limits (default: %u)", DEFAULT_WALLET_REJECT_LONG_CHAINS), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::WALLET_DEBUG_TEST);
124+
argsman.AddArg("-walletcrosschain", strprintf("Allow reusing wallet files across chains (default: %u)", DEFAULT_WALLETCROSSCHAIN), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::WALLET_DEBUG_TEST);
124125

125126
argsman.AddHiddenArgs({"-zapwallettxes"});
126127
}

src/wallet/wallet.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3085,6 +3085,20 @@ bool CWallet::AttachChain(const std::shared_ptr<CWallet>& walletInstance, interf
30853085
assert(!walletInstance->m_chain || walletInstance->m_chain == &chain);
30863086
walletInstance->m_chain = &chain;
30873087

3088+
// Unless allowed, ensure wallet files are not reused across chains:
3089+
if (!gArgs.GetBoolArg("-walletcrosschain", DEFAULT_WALLETCROSSCHAIN)) {
3090+
WalletBatch batch(walletInstance->GetDatabase());
3091+
CBlockLocator locator;
3092+
if (batch.ReadBestBlock(locator) && locator.vHave.size() > 0 && chain.getHeight()) {
3093+
// Wallet is assumed to be from another chain, if genesis block in the active
3094+
// chain differs from the genesis block known to the wallet.
3095+
if (chain.getBlockHash(0) != locator.vHave.back()) {
3096+
error = Untranslated("Wallet files should not be reused across chains. Restart dashd with -walletcrosschain to override.");
3097+
return false;
3098+
}
3099+
}
3100+
}
3101+
30883102
// Register wallet with validationinterface. It's done before rescan to avoid
30893103
// missing block connections between end of rescan and validation subscribing.
30903104
// Because of wallet lock being hold, block connection notifications are going to

src/wallet/wallet.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ static const bool DEFAULT_WALLET_REJECT_LONG_CHAINS{true};
108108
static const unsigned int DEFAULT_TX_CONFIRM_TARGET = 6;
109109
static const bool DEFAULT_WALLETBROADCAST = true;
110110
static const bool DEFAULT_DISABLE_WALLET = false;
111+
static const bool DEFAULT_WALLETCROSSCHAIN = false;
111112
//! -maxtxfee default
112113
static const CAmount DEFAULT_TRANSACTION_MAXFEE = COIN / 10;
113114
//! Discourage users to set fees higher than this amount (in satoshis) per kB

test/functional/test_runner.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,7 @@
303303
'rpc_bind.py --ipv4',
304304
'rpc_bind.py --ipv6',
305305
'rpc_bind.py --nonloopback',
306+
'wallet_crosschain.py',
306307
'mining_basic.py',
307308
'rpc_named_arguments.py',
308309
'feature_startupnotify.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)