Changeset View
Changeset View
Standalone View
Standalone View
test/functional/wallet_backup.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2014-2016 The Bitcoin Core developers | # Copyright (c) 2014-2019 The Bitcoin Core developers | ||||
# Distributed under the MIT software license, see the accompanying | # Distributed under the MIT software license, see the accompanying | ||||
# file COPYING or http://www.opensource.org/licenses/mit-license.php. | # file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
"""Test the wallet backup features. | """Test the wallet backup features. | ||||
Test case is: | Test case is: | ||||
4 nodes. 1 2 and 3 send transactions between each other, | 4 nodes. 1 2 and 3 send transactions between each other, | ||||
fourth node is a miner. | fourth node is a miner. | ||||
1 2 3 each mine a block to start, then | 1 2 3 each mine a block to start, then | ||||
Show All 24 Lines | |||||
from random import randint | from random import randint | ||||
import shutil | import shutil | ||||
from test_framework.test_framework import BitcoinTestFramework | from test_framework.test_framework import BitcoinTestFramework | ||||
from test_framework.util import ( | from test_framework.util import ( | ||||
assert_equal, | assert_equal, | ||||
assert_raises_rpc_error, | assert_raises_rpc_error, | ||||
connect_nodes, | connect_nodes, | ||||
sync_blocks, | |||||
sync_mempools, | |||||
) | ) | ||||
class WalletBackupTest(BitcoinTestFramework): | class WalletBackupTest(BitcoinTestFramework): | ||||
def set_test_params(self): | def set_test_params(self): | ||||
self.num_nodes = 4 | self.num_nodes = 4 | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
# nodes 1, 2,3 are spenders, let's give them a keypool=100 | # nodes 1, 2,3 are spenders, let's give them a keypool=100 | ||||
Show All 31 Lines | def do_one_round(self): | ||||
self.one_send(0, a2) | self.one_send(0, a2) | ||||
self.one_send(1, a0) | self.one_send(1, a0) | ||||
self.one_send(1, a2) | self.one_send(1, a2) | ||||
self.one_send(2, a0) | self.one_send(2, a0) | ||||
self.one_send(2, a1) | self.one_send(2, a1) | ||||
# Have the miner (node3) mine a block. | # Have the miner (node3) mine a block. | ||||
# Must sync mempools before mining. | # Must sync mempools before mining. | ||||
sync_mempools(self.nodes) | self.sync_mempools() | ||||
self.nodes[3].generate(1) | self.nodes[3].generate(1) | ||||
sync_blocks(self.nodes) | self.sync_blocks() | ||||
# As above, this mirrors the original bash test. | # As above, this mirrors the original bash test. | ||||
def start_three(self): | def start_three(self): | ||||
self.start_node(0) | self.start_node(0) | ||||
self.start_node(1) | self.start_node(1) | ||||
self.start_node(2) | self.start_node(2) | ||||
connect_nodes(self.nodes[0], self.nodes[3]) | connect_nodes(self.nodes[0], self.nodes[3]) | ||||
connect_nodes(self.nodes[1], self.nodes[3]) | connect_nodes(self.nodes[1], self.nodes[3]) | ||||
Show All 11 Lines | def erase_three(self): | ||||
os.remove(os.path.join( | os.remove(os.path.join( | ||||
self.nodes[1].datadir, 'regtest', 'wallets', 'wallet.dat')) | self.nodes[1].datadir, 'regtest', 'wallets', 'wallet.dat')) | ||||
os.remove(os.path.join( | os.remove(os.path.join( | ||||
self.nodes[2].datadir, 'regtest', 'wallets', 'wallet.dat')) | self.nodes[2].datadir, 'regtest', 'wallets', 'wallet.dat')) | ||||
def run_test(self): | def run_test(self): | ||||
self.log.info("Generating initial blockchain") | self.log.info("Generating initial blockchain") | ||||
self.nodes[0].generate(1) | self.nodes[0].generate(1) | ||||
sync_blocks(self.nodes) | self.sync_blocks() | ||||
self.nodes[1].generate(1) | self.nodes[1].generate(1) | ||||
sync_blocks(self.nodes) | self.sync_blocks() | ||||
self.nodes[2].generate(1) | self.nodes[2].generate(1) | ||||
sync_blocks(self.nodes) | self.sync_blocks() | ||||
self.nodes[3].generate(100) | self.nodes[3].generate(100) | ||||
sync_blocks(self.nodes) | self.sync_blocks() | ||||
assert_equal(self.nodes[0].getbalance(), 50) | assert_equal(self.nodes[0].getbalance(), 50) | ||||
assert_equal(self.nodes[1].getbalance(), 50) | assert_equal(self.nodes[1].getbalance(), 50) | ||||
assert_equal(self.nodes[2].getbalance(), 50) | assert_equal(self.nodes[2].getbalance(), 50) | ||||
assert_equal(self.nodes[3].getbalance(), 0) | assert_equal(self.nodes[3].getbalance(), 0) | ||||
self.log.info("Creating transactions") | self.log.info("Creating transactions") | ||||
# Five rounds of sending each other transactions. | # Five rounds of sending each other transactions. | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
self.nodes[0].datadir, 'regtest', 'wallets', 'wallet.dat')) | self.nodes[0].datadir, 'regtest', 'wallets', 'wallet.dat')) | ||||
shutil.copyfile(os.path.join(self.nodes[1].datadir, 'wallet.bak'), os.path.join( | shutil.copyfile(os.path.join(self.nodes[1].datadir, 'wallet.bak'), os.path.join( | ||||
self.nodes[1].datadir, 'regtest', 'wallets', 'wallet.dat')) | self.nodes[1].datadir, 'regtest', 'wallets', 'wallet.dat')) | ||||
shutil.copyfile(os.path.join(self.nodes[2].datadir, 'wallet.bak'), os.path.join( | shutil.copyfile(os.path.join(self.nodes[2].datadir, 'wallet.bak'), os.path.join( | ||||
self.nodes[2].datadir, 'regtest', 'wallets', 'wallet.dat')) | self.nodes[2].datadir, 'regtest', 'wallets', 'wallet.dat')) | ||||
self.log.info("Re-starting nodes") | self.log.info("Re-starting nodes") | ||||
self.start_three() | self.start_three() | ||||
sync_blocks(self.nodes) | self.sync_blocks() | ||||
assert_equal(self.nodes[0].getbalance(), balance0) | assert_equal(self.nodes[0].getbalance(), balance0) | ||||
assert_equal(self.nodes[1].getbalance(), balance1) | assert_equal(self.nodes[1].getbalance(), balance1) | ||||
assert_equal(self.nodes[2].getbalance(), balance2) | assert_equal(self.nodes[2].getbalance(), balance2) | ||||
self.log.info("Restoring using dumped wallet") | self.log.info("Restoring using dumped wallet") | ||||
self.stop_three() | self.stop_three() | ||||
self.erase_three() | self.erase_three() | ||||
Show All 11 Lines | def run_test(self): | ||||
self.nodes[0].importwallet(os.path.join( | self.nodes[0].importwallet(os.path.join( | ||||
self.nodes[0].datadir, 'wallet.dump')) | self.nodes[0].datadir, 'wallet.dump')) | ||||
self.nodes[1].importwallet(os.path.join( | self.nodes[1].importwallet(os.path.join( | ||||
self.nodes[1].datadir, 'wallet.dump')) | self.nodes[1].datadir, 'wallet.dump')) | ||||
self.nodes[2].importwallet(os.path.join( | self.nodes[2].importwallet(os.path.join( | ||||
self.nodes[2].datadir, 'wallet.dump')) | self.nodes[2].datadir, 'wallet.dump')) | ||||
sync_blocks(self.nodes) | self.sync_blocks() | ||||
assert_equal(self.nodes[0].getbalance(), balance0) | assert_equal(self.nodes[0].getbalance(), balance0) | ||||
assert_equal(self.nodes[1].getbalance(), balance1) | assert_equal(self.nodes[1].getbalance(), balance1) | ||||
assert_equal(self.nodes[2].getbalance(), balance2) | assert_equal(self.nodes[2].getbalance(), balance2) | ||||
# Backup to source wallet file must fail | # Backup to source wallet file must fail | ||||
sourcePaths = [ | sourcePaths = [ | ||||
os.path.join(self.nodes[0].datadir, | os.path.join(self.nodes[0].datadir, | ||||
Show All 13 Lines |