mirror of
https://github.com/dashpay/dash.git
synced 2024-12-27 04:52:59 +01:00
e197f976e1
* Merge #16509: test: Adapt test framework for chains other than "regtest" faf36838bdba7393960fce6ad0c56dc1f93f5870 test: Avoid hardcoding the chain name in combine_logs (MarcoFalke) fa8a1d7ba30040f8c74f93fc41a61276c255a6a6 test: Adapt test framework for chains other than "regtest" (MarcoFalke) 68f546635d5de2ccfedadeabc7bc79e12e5eca6a test: Fix “local variable 'e' is assigned to but never used” (Ben Woosley) Pull request description: This is required for various work in progress: * testchains #8994 * signet #16411 * some of my locally written tests While it will be unused in the master branch as of now, it will make all of those pull requests shorter. Thus review for non-regtest tests can focus on the actual changes and not some test framework changes. ACKs for top commit: jonatack: ACK faf36838bdba7393960fce6ad0c56dc1f93f5870, ran tests and reviewed the code. Tree-SHA512: 35add66c12cab68f2fac8f7c7d47c604d3f24eae9336ff78f83e2c92b3dc08a25e7f4217199bac5393dd3fb72f945bba9c001d6fbb8efd298c88858075fcb3d6 * Add devnet support for tests * test: make sure devnet can connect to each other and start * Partial merge bitcoin/bitcoin#16681: Tests: Use self.chain instead of 'regtest' in almost all current tests, revert one TODO while at it Co-authored-by: MarcoFalke <falke.marco@gmail.com> Co-authored-by: Jorge Timón <jtimon@jtimon.cc>
210 lines
7.9 KiB
Python
Executable File
210 lines
7.9 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# Copyright (c) 2014-2016 The Bitcoin Core developers
|
|
# Distributed under the MIT software license, see the accompanying
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
"""Test the wallet backup features.
|
|
|
|
Test case is:
|
|
4 nodes. 1 2 and 3 send transactions between each other,
|
|
fourth node is a miner.
|
|
1 2 3 each mine a block to start, then
|
|
Miner creates 100 blocks so 1 2 3 each have 500 mature
|
|
coins to spend.
|
|
Then 5 iterations of 1/2/3 sending coins amongst
|
|
themselves to get transactions in the wallets,
|
|
and the miner mining one block.
|
|
|
|
Wallets are backed up using dumpwallet/backupwallet.
|
|
Then 5 more iterations of transactions and mining a block.
|
|
|
|
Miner then generates 101 more blocks, so any
|
|
transaction fees paid mature.
|
|
|
|
Sanity check:
|
|
Sum(1,2,3,4 balances) == 114*500
|
|
|
|
1/2/3 are shutdown, and their wallets erased.
|
|
Then restore using wallet.dat backup. And
|
|
confirm 1/2/3/4 balances are same as before.
|
|
|
|
Shutdown again, restore using importwallet,
|
|
and confirm again balances are correct.
|
|
"""
|
|
from random import randint
|
|
import shutil
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
from test_framework.util import *
|
|
|
|
class WalletBackupTest(BitcoinTestFramework):
|
|
def set_test_params(self):
|
|
self.num_nodes = 4
|
|
self.setup_clean_chain = True
|
|
# nodes 1, 2,3 are spenders, let's give them a keypool=100
|
|
self.extra_args = [["-keypool=100"], ["-keypool=100"], ["-keypool=100"], []]
|
|
|
|
def setup_network(self, split=False):
|
|
self.setup_nodes()
|
|
connect_nodes(self.nodes[0], 3)
|
|
connect_nodes(self.nodes[1], 3)
|
|
connect_nodes(self.nodes[2], 3)
|
|
connect_nodes(self.nodes[2], 0)
|
|
self.sync_all()
|
|
|
|
def one_send(self, from_node, to_address):
|
|
if (randint(1,2) == 1):
|
|
amount = Decimal(randint(1,10)) / Decimal(10)
|
|
self.nodes[from_node].sendtoaddress(to_address, amount)
|
|
|
|
def do_one_round(self):
|
|
a0 = self.nodes[0].getnewaddress()
|
|
a1 = self.nodes[1].getnewaddress()
|
|
a2 = self.nodes[2].getnewaddress()
|
|
|
|
self.one_send(0, a1)
|
|
self.one_send(0, a2)
|
|
self.one_send(1, a0)
|
|
self.one_send(1, a2)
|
|
self.one_send(2, a0)
|
|
self.one_send(2, a1)
|
|
|
|
# Have the miner (node3) mine a block.
|
|
# Must sync mempools before mining.
|
|
self.sync_mempools()
|
|
self.nodes[3].generate(1)
|
|
self.sync_blocks()
|
|
|
|
# As above, this mirrors the original bash test.
|
|
def start_three(self):
|
|
self.start_node(0)
|
|
self.start_node(1)
|
|
self.start_node(2)
|
|
connect_nodes(self.nodes[0], 3)
|
|
connect_nodes(self.nodes[1], 3)
|
|
connect_nodes(self.nodes[2], 3)
|
|
connect_nodes(self.nodes[2], 0)
|
|
|
|
def stop_three(self):
|
|
self.stop_node(0)
|
|
self.stop_node(1)
|
|
self.stop_node(2)
|
|
|
|
def erase_three(self):
|
|
os.remove(os.path.join(self.nodes[0].datadir, self.chain, 'wallets', 'wallet.dat'))
|
|
os.remove(os.path.join(self.nodes[1].datadir, self.chain, 'wallets', 'wallet.dat'))
|
|
os.remove(os.path.join(self.nodes[2].datadir, self.chain, 'wallets', 'wallet.dat'))
|
|
|
|
def run_test(self):
|
|
self.log.info("Generating initial blockchain")
|
|
self.nodes[0].generate(1)
|
|
self.sync_blocks()
|
|
self.nodes[1].generate(1)
|
|
self.sync_blocks()
|
|
self.nodes[2].generate(1)
|
|
self.sync_blocks()
|
|
self.nodes[3].generate(100)
|
|
self.sync_blocks()
|
|
|
|
assert_equal(self.nodes[0].getbalance(), 500)
|
|
assert_equal(self.nodes[1].getbalance(), 500)
|
|
assert_equal(self.nodes[2].getbalance(), 500)
|
|
assert_equal(self.nodes[3].getbalance(), 0)
|
|
|
|
self.log.info("Creating transactions")
|
|
# Five rounds of sending each other transactions.
|
|
for i in range(5):
|
|
self.do_one_round()
|
|
|
|
self.log.info("Backing up")
|
|
|
|
self.nodes[0].backupwallet(os.path.join(self.nodes[0].datadir, 'wallet.bak'))
|
|
self.nodes[0].dumpwallet(os.path.join(self.nodes[0].datadir, 'wallet.dump'))
|
|
self.nodes[1].backupwallet(os.path.join(self.nodes[1].datadir, 'wallet.bak'))
|
|
self.nodes[1].dumpwallet(os.path.join(self.nodes[1].datadir, 'wallet.dump'))
|
|
self.nodes[2].backupwallet(os.path.join(self.nodes[2].datadir, 'wallet.bak'))
|
|
self.nodes[2].dumpwallet(os.path.join(self.nodes[2].datadir, 'wallet.dump'))
|
|
|
|
self.log.info("More transactions")
|
|
for i in range(5):
|
|
self.do_one_round()
|
|
|
|
# Generate 101 more blocks, so any fees paid mature
|
|
self.nodes[3].generate(101)
|
|
self.sync_all()
|
|
|
|
balance0 = self.nodes[0].getbalance()
|
|
balance1 = self.nodes[1].getbalance()
|
|
balance2 = self.nodes[2].getbalance()
|
|
balance3 = self.nodes[3].getbalance()
|
|
total = balance0 + balance1 + balance2 + balance3
|
|
|
|
# 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 * 500 = 57000.
|
|
assert_equal(total, 57000)
|
|
|
|
##
|
|
# Test restoring spender wallets from backups
|
|
##
|
|
self.log.info("Restoring using wallet.dat")
|
|
self.stop_three()
|
|
self.erase_three()
|
|
|
|
# Start node2 with no chain
|
|
shutil.rmtree(os.path.join(self.nodes[2].datadir, self.chain, 'blocks'))
|
|
shutil.rmtree(os.path.join(self.nodes[2].datadir, self.chain, 'chainstate'))
|
|
shutil.rmtree(os.path.join(self.nodes[2].datadir, self.chain, 'evodb'))
|
|
shutil.rmtree(os.path.join(self.nodes[2].datadir, self.chain, 'llmq'))
|
|
|
|
# Restore wallets from backup
|
|
shutil.copyfile(os.path.join(self.nodes[0].datadir, 'wallet.bak'), os.path.join(self.nodes[0].datadir, self.chain, 'wallets', 'wallet.dat'))
|
|
shutil.copyfile(os.path.join(self.nodes[1].datadir, 'wallet.bak'), os.path.join(self.nodes[1].datadir, self.chain, 'wallets', 'wallet.dat'))
|
|
shutil.copyfile(os.path.join(self.nodes[2].datadir, 'wallet.bak'), os.path.join(self.nodes[2].datadir, self.chain, 'wallets', 'wallet.dat'))
|
|
|
|
self.log.info("Re-starting nodes")
|
|
self.start_three()
|
|
self.sync_blocks()
|
|
|
|
assert_equal(self.nodes[0].getbalance(), balance0)
|
|
assert_equal(self.nodes[1].getbalance(), balance1)
|
|
assert_equal(self.nodes[2].getbalance(), balance2)
|
|
|
|
self.log.info("Restoring using dumped wallet")
|
|
self.stop_three()
|
|
self.erase_three()
|
|
|
|
#start node2 with no chain
|
|
shutil.rmtree(os.path.join(self.nodes[2].datadir, self.chain, 'blocks'))
|
|
shutil.rmtree(os.path.join(self.nodes[2].datadir, self.chain, 'chainstate'))
|
|
shutil.rmtree(os.path.join(self.nodes[2].datadir, self.chain, 'evodb'))
|
|
shutil.rmtree(os.path.join(self.nodes[2].datadir, self.chain, 'llmq'))
|
|
|
|
self.start_three()
|
|
|
|
assert_equal(self.nodes[0].getbalance(), 0)
|
|
assert_equal(self.nodes[1].getbalance(), 0)
|
|
assert_equal(self.nodes[2].getbalance(), 0)
|
|
|
|
self.nodes[0].importwallet(os.path.join(self.nodes[0].datadir, 'wallet.dump'))
|
|
self.nodes[1].importwallet(os.path.join(self.nodes[1].datadir, 'wallet.dump'))
|
|
self.nodes[2].importwallet(os.path.join(self.nodes[2].datadir, 'wallet.dump'))
|
|
|
|
self.sync_blocks()
|
|
|
|
assert_equal(self.nodes[0].getbalance(), balance0)
|
|
assert_equal(self.nodes[1].getbalance(), balance1)
|
|
assert_equal(self.nodes[2].getbalance(), balance2)
|
|
|
|
# Backup to source wallet file must fail
|
|
sourcePaths = [
|
|
os.path.join(self.nodes[0].datadir, self.chain, 'wallets', 'wallet.dat'),
|
|
os.path.join(self.nodes[0].datadir, self.chain, '.', 'wallets', 'wallet.dat'),
|
|
os.path.join(self.nodes[0].datadir, self.chain, 'wallets', ''),
|
|
os.path.join(self.nodes[0].datadir, self.chain, 'wallets')]
|
|
|
|
for sourcePath in sourcePaths:
|
|
assert_raises_rpc_error(-4, "backup failed", self.nodes[0].backupwallet, sourcePath)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
WalletBackupTest().main()
|