2016-05-06 11:23:48 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (c) 2014-2016 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
# Distributed under the MIT software license, see the accompanying
|
2014-12-04 22:09:58 +01:00
|
|
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2019-01-07 10:55:35 +01:00
|
|
|
"""Test the wallet backup features.
|
2014-12-04 22:09:58 +01:00
|
|
|
|
|
|
|
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
|
2016-03-06 16:14:39 +01:00
|
|
|
Miner creates 100 blocks so 1 2 3 each have 500 mature
|
2014-12-04 22:09:58 +01:00
|
|
|
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:
|
2016-03-06 16:14:39 +01:00
|
|
|
Sum(1,2,3,4 balances) == 114*500
|
2014-12-04 22:09:58 +01:00
|
|
|
|
|
|
|
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.
|
|
|
|
"""
|
Merge #13054: tests: Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
68400d8b96 tests: Use explicit imports (practicalswift)
Pull request description:
Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
Wildcard imports make it unclear which names are present in the namespace, confusing both readers and many automated tools.
An additional benefit of not using wildcard imports in tests scripts is that readers of a test script then can infer the rough testing scope just by looking at the imports.
Before this commit:
```
$ contrib/devtools/lint-python.sh | head -10
./test/functional/feature_rbf.py:8:1: F403 'from test_framework.util import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:9:1: F403 'from test_framework.script import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:10:1: F403 'from test_framework.mininode import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:15:12: F405 bytes_to_hex_str may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:17:58: F405 CScript may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:25:13: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:31: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:60: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:41: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:68: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
$
```
After this commit:
```
$ contrib/devtools/lint-python.sh | head -10
$
```
Tree-SHA512: 3f826d39cffb6438388e5efcb20a9622ff8238247e882d68f7b38609877421b2a8e10e9229575f8eb6a8fa42dec4256986692e92922c86171f750a0e887438d9
2018-08-13 14:24:43 +02:00
|
|
|
from decimal import Decimal
|
|
|
|
import os
|
2017-06-29 17:37:19 +02:00
|
|
|
from random import randint
|
|
|
|
import shutil
|
2014-12-04 22:09:58 +01:00
|
|
|
|
2015-05-27 16:00:15 +02:00
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
Merge #13054: tests: Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
68400d8b96 tests: Use explicit imports (practicalswift)
Pull request description:
Enable automatic detection of undefined names in Python tests scripts. Remove wildcard imports.
Wildcard imports make it unclear which names are present in the namespace, confusing both readers and many automated tools.
An additional benefit of not using wildcard imports in tests scripts is that readers of a test script then can infer the rough testing scope just by looking at the imports.
Before this commit:
```
$ contrib/devtools/lint-python.sh | head -10
./test/functional/feature_rbf.py:8:1: F403 'from test_framework.util import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:9:1: F403 'from test_framework.script import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:10:1: F403 'from test_framework.mininode import *' used; unable to detect undefined names
./test/functional/feature_rbf.py:15:12: F405 bytes_to_hex_str may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:17:58: F405 CScript may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:25:13: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:31: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:26:60: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:41: F405 satoshi_round may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
./test/functional/feature_rbf.py:30:68: F405 COIN may be undefined, or defined from star imports: test_framework.mininode, test_framework.script, test_framework.util
$
```
After this commit:
```
$ contrib/devtools/lint-python.sh | head -10
$
```
Tree-SHA512: 3f826d39cffb6438388e5efcb20a9622ff8238247e882d68f7b38609877421b2a8e10e9229575f8eb6a8fa42dec4256986692e92922c86171f750a0e887438d9
2018-08-13 14:24:43 +02:00
|
|
|
from test_framework.util import assert_equal, assert_raises_rpc_error, connect_nodes
|
2014-12-04 22:09:58 +01:00
|
|
|
|
|
|
|
class WalletBackupTest(BitcoinTestFramework):
|
2017-09-01 18:47:13 +02:00
|
|
|
def set_test_params(self):
|
2016-05-20 15:16:51 +02:00
|
|
|
self.num_nodes = 4
|
2017-09-01 18:47:13 +02:00
|
|
|
self.setup_clean_chain = True
|
2016-09-19 09:40:58 +02:00
|
|
|
# nodes 1, 2,3 are spenders, let's give them a keypool=100
|
2019-10-13 17:32:53 +02:00
|
|
|
# whitelist all peers to speed up tx relay / mempool sync
|
|
|
|
self.extra_args = [
|
2022-10-15 14:40:17 +02:00
|
|
|
["-keypool=100", "-whitelist=127.0.0.1"],
|
|
|
|
["-keypool=100", "-whitelist=127.0.0.1"],
|
|
|
|
["-keypool=100", "-whitelist=127.0.0.1"],
|
|
|
|
["-whitelist=127.0.0.1"]
|
2019-10-13 17:32:53 +02:00
|
|
|
]
|
2019-09-17 19:36:30 +02:00
|
|
|
self.rpc_timeout = 120
|
2014-12-04 22:09:58 +01:00
|
|
|
|
2018-09-13 12:33:15 +02:00
|
|
|
def skip_test_if_missing_module(self):
|
|
|
|
self.skip_if_no_wallet()
|
|
|
|
|
2018-12-29 20:18:43 +01:00
|
|
|
def setup_network(self):
|
2017-05-02 20:02:55 +02:00
|
|
|
self.setup_nodes()
|
2014-12-04 22:09:58 +01:00
|
|
|
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.
|
2020-04-14 12:00:16 +02:00
|
|
|
self.sync_mempools()
|
2015-04-01 05:28:28 +02:00
|
|
|
self.nodes[3].generate(1)
|
2020-04-14 12:00:16 +02:00
|
|
|
self.sync_blocks()
|
2014-12-04 22:09:58 +01:00
|
|
|
|
|
|
|
# As above, this mirrors the original bash test.
|
|
|
|
def start_three(self):
|
2017-09-01 18:47:13 +02:00
|
|
|
self.start_node(0)
|
|
|
|
self.start_node(1)
|
|
|
|
self.start_node(2)
|
2014-12-04 22:09:58 +01:00
|
|
|
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):
|
2017-06-02 12:08:48 +02:00
|
|
|
self.stop_node(0)
|
|
|
|
self.stop_node(1)
|
|
|
|
self.stop_node(2)
|
2014-12-04 22:09:58 +01:00
|
|
|
|
|
|
|
def erase_three(self):
|
2022-10-15 14:40:17 +02:00
|
|
|
os.remove(os.path.join(self.nodes[0].datadir, self.chain, 'wallets', self.default_wallet_name, self.wallet_data_filename))
|
|
|
|
os.remove(os.path.join(self.nodes[1].datadir, self.chain, 'wallets', self.default_wallet_name, self.wallet_data_filename))
|
|
|
|
os.remove(os.path.join(self.nodes[2].datadir, self.chain, 'wallets', self.default_wallet_name, self.wallet_data_filename))
|
2014-12-04 22:09:58 +01:00
|
|
|
|
|
|
|
def run_test(self):
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Generating initial blockchain")
|
2015-04-01 05:28:28 +02:00
|
|
|
self.nodes[0].generate(1)
|
2020-04-14 12:00:16 +02:00
|
|
|
self.sync_blocks()
|
2015-04-01 05:28:28 +02:00
|
|
|
self.nodes[1].generate(1)
|
2020-04-14 12:00:16 +02:00
|
|
|
self.sync_blocks()
|
2015-04-01 05:28:28 +02:00
|
|
|
self.nodes[2].generate(1)
|
2020-04-14 12:00:16 +02:00
|
|
|
self.sync_blocks()
|
2015-04-01 05:28:28 +02:00
|
|
|
self.nodes[3].generate(100)
|
2020-04-14 12:00:16 +02:00
|
|
|
self.sync_blocks()
|
2014-12-04 22:09:58 +01:00
|
|
|
|
2016-03-06 16:14:39 +01:00
|
|
|
assert_equal(self.nodes[0].getbalance(), 500)
|
|
|
|
assert_equal(self.nodes[1].getbalance(), 500)
|
|
|
|
assert_equal(self.nodes[2].getbalance(), 500)
|
2014-12-04 22:09:58 +01:00
|
|
|
assert_equal(self.nodes[3].getbalance(), 0)
|
|
|
|
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Creating transactions")
|
2014-12-04 22:09:58 +01:00
|
|
|
# Five rounds of sending each other transactions.
|
|
|
|
for i in range(5):
|
|
|
|
self.do_one_round()
|
|
|
|
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Backing up")
|
2018-03-22 11:04:37 +01:00
|
|
|
|
|
|
|
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'))
|
2014-12-04 22:09:58 +01:00
|
|
|
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("More transactions")
|
2014-12-04 22:09:58 +01:00
|
|
|
for i in range(5):
|
|
|
|
self.do_one_round()
|
|
|
|
|
|
|
|
# Generate 101 more blocks, so any fees paid mature
|
2015-04-01 05:28:28 +02:00
|
|
|
self.nodes[3].generate(101)
|
2014-12-04 22:09:58 +01:00
|
|
|
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.)
|
2016-03-06 16:14:39 +01:00
|
|
|
# 114 are mature, so the sum of all wallets should be 114 * 500 = 57000.
|
|
|
|
assert_equal(total, 57000)
|
2014-12-04 22:09:58 +01:00
|
|
|
|
|
|
|
##
|
|
|
|
# Test restoring spender wallets from backups
|
|
|
|
##
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Restoring using wallet.dat")
|
2014-12-04 22:09:58 +01:00
|
|
|
self.stop_three()
|
|
|
|
self.erase_three()
|
|
|
|
|
|
|
|
# Start node2 with no chain
|
2021-01-22 15:58:07 +01:00
|
|
|
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'))
|
2014-12-04 22:09:58 +01:00
|
|
|
|
|
|
|
# Restore wallets from backup
|
2022-10-15 14:40:17 +02:00
|
|
|
shutil.copyfile(os.path.join(self.nodes[0].datadir, 'wallet.bak'), os.path.join(self.nodes[0].datadir, self.chain, 'wallets', self.default_wallet_name, self.wallet_data_filename))
|
|
|
|
shutil.copyfile(os.path.join(self.nodes[1].datadir, 'wallet.bak'), os.path.join(self.nodes[1].datadir, self.chain, 'wallets', self.default_wallet_name, self.wallet_data_filename))
|
|
|
|
shutil.copyfile(os.path.join(self.nodes[2].datadir, 'wallet.bak'), os.path.join(self.nodes[2].datadir, self.chain, 'wallets', self.default_wallet_name, self.wallet_data_filename))
|
2014-12-04 22:09:58 +01:00
|
|
|
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Re-starting nodes")
|
2014-12-04 22:09:58 +01:00
|
|
|
self.start_three()
|
2020-04-14 12:00:16 +02:00
|
|
|
self.sync_blocks()
|
2014-12-04 22:09:58 +01:00
|
|
|
|
|
|
|
assert_equal(self.nodes[0].getbalance(), balance0)
|
|
|
|
assert_equal(self.nodes[1].getbalance(), balance1)
|
|
|
|
assert_equal(self.nodes[2].getbalance(), balance2)
|
|
|
|
|
2017-03-09 21:16:20 +01:00
|
|
|
self.log.info("Restoring using dumped wallet")
|
2014-12-04 22:09:58 +01:00
|
|
|
self.stop_three()
|
|
|
|
self.erase_three()
|
|
|
|
|
|
|
|
#start node2 with no chain
|
2021-01-22 15:58:07 +01:00
|
|
|
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'))
|
2014-12-04 22:09:58 +01:00
|
|
|
|
|
|
|
self.start_three()
|
|
|
|
|
|
|
|
assert_equal(self.nodes[0].getbalance(), 0)
|
|
|
|
assert_equal(self.nodes[1].getbalance(), 0)
|
|
|
|
assert_equal(self.nodes[2].getbalance(), 0)
|
|
|
|
|
2018-03-22 11:04:37 +01:00
|
|
|
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'))
|
2014-12-04 22:09:58 +01:00
|
|
|
|
2020-04-14 12:00:16 +02:00
|
|
|
self.sync_blocks()
|
2014-12-04 22:09:58 +01:00
|
|
|
|
|
|
|
assert_equal(self.nodes[0].getbalance(), balance0)
|
|
|
|
assert_equal(self.nodes[1].getbalance(), balance1)
|
|
|
|
assert_equal(self.nodes[2].getbalance(), balance2)
|
|
|
|
|
2017-11-01 17:26:23 +01:00
|
|
|
# Backup to source wallet file must fail
|
|
|
|
sourcePaths = [
|
2022-10-15 14:40:17 +02:00
|
|
|
os.path.join(self.nodes[0].datadir, self.chain, 'wallets', self.default_wallet_name, self.wallet_data_filename),
|
|
|
|
os.path.join(self.nodes[0].datadir, self.chain, '.', 'wallets', self.default_wallet_name, self.wallet_data_filename),
|
|
|
|
os.path.join(self.nodes[0].datadir, self.chain, 'wallets', self.default_wallet_name),
|
2021-01-22 15:58:07 +01:00
|
|
|
os.path.join(self.nodes[0].datadir, self.chain, 'wallets')]
|
2017-11-01 17:26:23 +01:00
|
|
|
|
|
|
|
for sourcePath in sourcePaths:
|
|
|
|
assert_raises_rpc_error(-4, "backup failed", self.nodes[0].backupwallet, sourcePath)
|
|
|
|
|
2014-12-04 22:09:58 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
WalletBackupTest().main()
|