Changeset View
Changeset View
Standalone View
Standalone View
qa/rpc-tests/walletbackup.py
Show All 31 Lines | |||||
Shutdown again, restore using importwallet, | Shutdown again, restore using importwallet, | ||||
and confirm again balances are correct. | and confirm again balances are correct. | ||||
""" | """ | ||||
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 | from random import randint | ||||
import logging | import logging | ||||
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO, stream=sys.stdout) | logging.basicConfig( | ||||
format='%(levelname)s: %(message)s', level=logging.INFO, stream=sys.stdout) | |||||
class WalletBackupTest(BitcoinTestFramework): | class WalletBackupTest(BitcoinTestFramework): | ||||
def __init__(self): | def __init__(self): | ||||
super().__init__() | super().__init__() | ||||
self.setup_clean_chain = True | self.setup_clean_chain = True | ||||
self.num_nodes = 4 | self.num_nodes = 4 | ||||
# 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 = [["-keypool=100"], ["-keypool=100"], ["-keypool=100"], []] | self.extra_args = [ | ||||
["-keypool=100"], ["-keypool=100"], ["-keypool=100"], []] | |||||
# This mirrors how the network was setup in the bash test | # This mirrors how the network was setup in the bash test | ||||
def setup_network(self, split=False): | def setup_network(self, split=False): | ||||
self.nodes = start_nodes(self.num_nodes, self.options.tmpdir, self.extra_args) | self.nodes = start_nodes( | ||||
self.num_nodes, self.options.tmpdir, self.extra_args) | |||||
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.is_network_split=False | self.is_network_split = False | ||||
self.sync_all() | self.sync_all() | ||||
def one_send(self, from_node, to_address): | def one_send(self, from_node, to_address): | ||||
if (randint(1,2) == 1): | if (randint(1, 2) == 1): | ||||
amount = Decimal(randint(1,10)) / Decimal(10) | amount = Decimal(randint(1, 10)) / Decimal(10) | ||||
self.nodes[from_node].sendtoaddress(to_address, amount) | self.nodes[from_node].sendtoaddress(to_address, amount) | ||||
def do_one_round(self): | def do_one_round(self): | ||||
a0 = self.nodes[0].getnewaddress() | a0 = self.nodes[0].getnewaddress() | ||||
a1 = self.nodes[1].getnewaddress() | a1 = self.nodes[1].getnewaddress() | ||||
a2 = self.nodes[2].getnewaddress() | a2 = self.nodes[2].getnewaddress() | ||||
self.one_send(0, a1) | self.one_send(0, a1) | ||||
▲ Show 20 Lines • Show All 72 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 | ||||
## | # | ||||
logging.info("Restoring using wallet.dat") | logging.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(tmpdir + "/node0/wallet.bak", tmpdir + "/node0/regtest/wallet.dat") | shutil.copyfile( | ||||
shutil.copyfile(tmpdir + "/node1/wallet.bak", tmpdir + "/node1/regtest/wallet.dat") | tmpdir + "/node0/wallet.bak", tmpdir + "/node0/regtest/wallet.dat") | ||||
shutil.copyfile(tmpdir + "/node2/wallet.bak", tmpdir + "/node2/regtest/wallet.dat") | shutil.copyfile( | ||||
tmpdir + "/node1/wallet.bak", tmpdir + "/node1/regtest/wallet.dat") | |||||
shutil.copyfile( | |||||
tmpdir + "/node2/wallet.bak", tmpdir + "/node2/regtest/wallet.dat") | |||||
logging.info("Re-starting nodes") | logging.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) | ||||
logging.info("Restoring using dumped wallet") | logging.info("Restoring using dumped wallet") | ||||
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") | ||||
self.start_three() | self.start_three() | ||||
assert_equal(self.nodes[0].getbalance(), 0) | assert_equal(self.nodes[0].getbalance(), 0) | ||||
assert_equal(self.nodes[1].getbalance(), 0) | assert_equal(self.nodes[1].getbalance(), 0) | ||||
assert_equal(self.nodes[2].getbalance(), 0) | assert_equal(self.nodes[2].getbalance(), 0) | ||||
Show All 14 Lines |