Skip to content

Commit 606ce05

Browse files
committed
Merge bitcoin/bitcoin#18554: wallet: ensure wallet files are not reused across chains
5f21321 tests: add tests for cross-chain wallet use prevention (Seibart Nedor) 9687659 wallet: ensure wallet files are not reused across chains (Seibart Nedor) Pull request description: This implements a proposal in #12805 and is a rebase of #14533. This seems to be a working approach, but I'm not sure why the `p2p_segwit.py` functional test needed a change, so I'll look into it more. ACKs for top commit: achow101: ACK 5f21321 dongcarl: Code Review ACK 5f21321 [deleted]: tACK bitcoin/bitcoin@5f21321 Tree-SHA512: 2c934300f113e772fc31c16ef5588526300bbc36e4dcef7d77bd0760c5c8f0ec77f766b1bed5503eb0157fa26dc900ed54d2ad1b41863c1f736ce5c1f3b67bec
2 parents 4cf9fa0 + 5f21321 commit 606ce05

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed

src/dummywallet.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ void DummyWalletInit::AddWalletOptions(ArgsManager& argsman) const
5050
"-flushwallet",
5151
"-privdb",
5252
"-walletrejectlongchains",
53+
"-walletcrosschain",
5354
"-unsafesqlitesync",
5455
});
5556
}

src/wallet/init.cpp

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

9696
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);
97+
argsman.AddArg("-walletcrosschain", strprintf("Allow reusing wallet files across chains (default: %u)", DEFAULT_WALLETCROSSCHAIN), ArgsManager::ALLOW_ANY | ArgsManager::DEBUG_ONLY, OptionsCategory::WALLET_DEBUG_TEST);
9798

9899
argsman.AddHiddenArgs({"-zapwallettxes"});
99100
}

src/wallet/wallet.cpp

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

2942+
// Unless allowed, ensure wallet files are not reused across chains:
2943+
if (!gArgs.GetBoolArg("-walletcrosschain", DEFAULT_WALLETCROSSCHAIN)) {
2944+
WalletBatch batch(walletInstance->GetDatabase());
2945+
CBlockLocator locator;
2946+
if (batch.ReadBestBlock(locator) && locator.vHave.size() > 0 && chain.getHeight()) {
2947+
// Wallet is assumed to be from another chain, if genesis block in the active
2948+
// chain differs from the genesis block known to the wallet.
2949+
if (chain.getBlockHash(0) != locator.vHave.back()) {
2950+
error = Untranslated("Wallet files should not be reused across chains. Restart bitcoind with -walletcrosschain to override.");
2951+
return false;
2952+
}
2953+
}
2954+
}
2955+
29422956
// Register wallet with validationinterface. It's done before rescan to avoid
29432957
// missing block connections between end of rescan and validation subscribing.
29442958
// 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
@@ -102,6 +102,7 @@ static const unsigned int DEFAULT_TX_CONFIRM_TARGET = 6;
102102
static const bool DEFAULT_WALLET_RBF = false;
103103
static const bool DEFAULT_WALLETBROADCAST = true;
104104
static const bool DEFAULT_DISABLE_WALLET = false;
105+
static const bool DEFAULT_WALLETCROSSCHAIN = false;
105106
//! -maxtxfee default
106107
constexpr CAmount DEFAULT_TRANSACTION_MAXFEE{COIN / 10};
107108
//! 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
@@ -255,6 +255,7 @@
255255
'rpc_bind.py --ipv4',
256256
'rpc_bind.py --ipv6',
257257
'rpc_bind.py --nonloopback',
258+
'wallet_crosschain.py',
258259
'mining_basic.py',
259260
'feature_signet.py',
260261
'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)