Changeset View
Changeset View
Standalone View
Standalone View
test/functional/walletbackup.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2014-2016 The Bitcoin Core developers | # Copyright (c) 2014-2016 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. | |||||
""" | |||||
Exercise the wallet backup code. Ported from walletbackup.sh. | |||||
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 | ||||
Miner creates 100 blocks so 1 2 3 each have 50 mature | Miner creates 100 blocks so 1 2 3 each have 50 mature | ||||
coins to spend. | coins to spend. | ||||
Then 5 iterations of 1/2/3 sending coins amongst | Then 5 iterations of 1/2/3 sending coins amongst | ||||
Show All 11 Lines | |||||
1/2/3 are shutdown, and their wallets erased. | 1/2/3 are shutdown, and their wallets erased. | ||||
Then restore using wallet.dat backup. And | Then restore using wallet.dat backup. And | ||||
confirm 1/2/3/4 balances are same as before. | confirm 1/2/3/4 balances are same as before. | ||||
Shutdown again, restore using importwallet, | Shutdown again, restore using importwallet, | ||||
and confirm again balances are correct. | and confirm again balances are correct. | ||||
""" | """ | ||||
from random import randint | |||||
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 * | ||||
from random import randint | |||||
import shutil | |||||
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 | ||||
self.extra_args = [ | self.extra_args = [["-keypool=100"], | ||||
["-keypool=100"], ["-keypool=100"], ["-keypool=100"], []] | ["-keypool=100"], ["-keypool=100"], []] | ||||
# This mirrors how the network was setup in the bash test | def setup_network(self, split=False): | ||||
def setup_network(self): | |||||
self.setup_nodes() | self.setup_nodes() | ||||
connect_nodes(self.nodes[0], 3) | connect_nodes(self.nodes[0], 3) | ||||
connect_nodes(self.nodes[1], 3) | connect_nodes(self.nodes[1], 3) | ||||
connect_nodes(self.nodes[2], 3) | connect_nodes(self.nodes[2], 3) | ||||
connect_nodes(self.nodes[2], 0) | connect_nodes(self.nodes[2], 0) | ||||
self.sync_all() | self.sync_all() | ||||
def one_send(self, from_node, to_address): | def one_send(self, from_node, to_address): | ||||
▲ Show 20 Lines • Show All 82 Lines • ▼ Show 20 Lines | def run_test(self): | ||||
balance2 = self.nodes[2].getbalance() | balance2 = self.nodes[2].getbalance() | ||||
balance3 = self.nodes[3].getbalance() | balance3 = self.nodes[3].getbalance() | ||||
total = balance0 + balance1 + balance2 + balance3 | total = balance0 + balance1 + balance2 + balance3 | ||||
# At this point, there are 214 blocks (103 for setup, then 10 rounds, then 101.) | # At this point, there are 214 blocks (103 for setup, then 10 rounds, then 101.) | ||||
# 114 are mature, so the sum of all wallets should be 114 * 50 = 5700. | # 114 are mature, so the sum of all wallets should be 114 * 50 = 5700. | ||||
assert_equal(total, 5700) | assert_equal(total, 5700) | ||||
# | ## | ||||
# Test restoring spender wallets from backups | # Test restoring spender wallets from backups | ||||
# | ## | ||||
self.log.info("Restoring using wallet.dat") | self.log.info("Restoring using wallet.dat") | ||||
self.stop_three() | self.stop_three() | ||||
self.erase_three() | self.erase_three() | ||||
# Start node2 with no chain | # Start node2 with no chain | ||||
shutil.rmtree(self.options.tmpdir + "/node2/regtest/blocks") | shutil.rmtree(self.options.tmpdir + "/node2/regtest/blocks") | ||||
shutil.rmtree(self.options.tmpdir + "/node2/regtest/chainstate") | shutil.rmtree(self.options.tmpdir + "/node2/regtest/chainstate") | ||||
# Restore wallets from backup | # Restore wallets from backup | ||||
shutil.copyfile( | shutil.copyfile(tmpdir + "/node0/wallet.bak", | ||||
tmpdir + "/node0/wallet.bak", tmpdir + "/node0/regtest/wallet.dat") | tmpdir + "/node0/regtest/wallet.dat") | ||||
shutil.copyfile( | shutil.copyfile(tmpdir + "/node1/wallet.bak", | ||||
tmpdir + "/node1/wallet.bak", tmpdir + "/node1/regtest/wallet.dat") | tmpdir + "/node1/regtest/wallet.dat") | ||||
shutil.copyfile( | shutil.copyfile(tmpdir + "/node2/wallet.bak", | ||||
tmpdir + "/node2/wallet.bak", tmpdir + "/node2/regtest/wallet.dat") | tmpdir + "/node2/regtest/wallet.dat") | ||||
self.log.info("Re-starting nodes") | self.log.info("Re-starting nodes") | ||||
self.start_three() | self.start_three() | ||||
sync_blocks(self.nodes) | sync_blocks(self.nodes) | ||||
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) | ||||
Show All 39 Lines |