Changeset View
Changeset View
Standalone View
Standalone View
qa/rpc-tests/walletbackup.py
Show All 30 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 | |||||
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 | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | def stop_three(self): | ||||
stop_node(self.nodes[2], 2) | stop_node(self.nodes[2], 2) | ||||
def erase_three(self): | def erase_three(self): | ||||
os.remove(self.options.tmpdir + "/node0/regtest/wallet.dat") | os.remove(self.options.tmpdir + "/node0/regtest/wallet.dat") | ||||
os.remove(self.options.tmpdir + "/node1/regtest/wallet.dat") | os.remove(self.options.tmpdir + "/node1/regtest/wallet.dat") | ||||
os.remove(self.options.tmpdir + "/node2/regtest/wallet.dat") | os.remove(self.options.tmpdir + "/node2/regtest/wallet.dat") | ||||
def run_test(self): | def run_test(self): | ||||
logging.info("Generating initial blockchain") | self.log.info("Generating initial blockchain") | ||||
self.nodes[0].generate(1) | self.nodes[0].generate(1) | ||||
sync_blocks(self.nodes) | sync_blocks(self.nodes) | ||||
self.nodes[1].generate(1) | self.nodes[1].generate(1) | ||||
sync_blocks(self.nodes) | sync_blocks(self.nodes) | ||||
self.nodes[2].generate(1) | self.nodes[2].generate(1) | ||||
sync_blocks(self.nodes) | sync_blocks(self.nodes) | ||||
self.nodes[3].generate(100) | self.nodes[3].generate(100) | ||||
sync_blocks(self.nodes) | sync_blocks(self.nodes) | ||||
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) | ||||
logging.info("Creating transactions") | self.log.info("Creating transactions") | ||||
# Five rounds of sending each other transactions. | # Five rounds of sending each other transactions. | ||||
for i in range(5): | for i in range(5): | ||||
self.do_one_round() | self.do_one_round() | ||||
logging.info("Backing up") | self.log.info("Backing up") | ||||
tmpdir = self.options.tmpdir | tmpdir = self.options.tmpdir | ||||
self.nodes[0].backupwallet(tmpdir + "/node0/wallet.bak") | self.nodes[0].backupwallet(tmpdir + "/node0/wallet.bak") | ||||
self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.dump") | self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.dump") | ||||
self.nodes[1].backupwallet(tmpdir + "/node1/wallet.bak") | self.nodes[1].backupwallet(tmpdir + "/node1/wallet.bak") | ||||
self.nodes[1].dumpwallet(tmpdir + "/node1/wallet.dump") | self.nodes[1].dumpwallet(tmpdir + "/node1/wallet.dump") | ||||
self.nodes[2].backupwallet(tmpdir + "/node2/wallet.bak") | self.nodes[2].backupwallet(tmpdir + "/node2/wallet.bak") | ||||
self.nodes[2].dumpwallet(tmpdir + "/node2/wallet.dump") | self.nodes[2].dumpwallet(tmpdir + "/node2/wallet.dump") | ||||
logging.info("More transactions") | self.log.info("More transactions") | ||||
for i in range(5): | for i in range(5): | ||||
self.do_one_round() | self.do_one_round() | ||||
# Generate 101 more blocks, so any fees paid mature | # Generate 101 more blocks, so any fees paid mature | ||||
self.nodes[3].generate(101) | self.nodes[3].generate(101) | ||||
self.sync_all() | self.sync_all() | ||||
balance0 = self.nodes[0].getbalance() | balance0 = self.nodes[0].getbalance() | ||||
balance1 = self.nodes[1].getbalance() | balance1 = self.nodes[1].getbalance() | ||||
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") | 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/regtest/wallet.dat") | tmpdir + "/node0/wallet.bak", tmpdir + "/node0/regtest/wallet.dat") | ||||
shutil.copyfile( | shutil.copyfile( | ||||
tmpdir + "/node1/wallet.bak", tmpdir + "/node1/regtest/wallet.dat") | tmpdir + "/node1/wallet.bak", tmpdir + "/node1/regtest/wallet.dat") | ||||
shutil.copyfile( | shutil.copyfile( | ||||
tmpdir + "/node2/wallet.bak", tmpdir + "/node2/regtest/wallet.dat") | tmpdir + "/node2/wallet.bak", tmpdir + "/node2/regtest/wallet.dat") | ||||
logging.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) | ||||
logging.info("Restoring using dumped wallet") | self.log.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() | ||||
Show All 18 Lines |