mirror of
https://github.com/dashpay/dash.git
synced 2024-12-26 04:22:55 +01:00
f55da3aa54
789733891
[tests] Introduce TestNode (John Newbery)
Pull request description:
Continues #10082
TestNode is a class responsible for all state related to a bitcoind node
under test. It stores local state, is responsible for tracking the
bitcoind process and delegates unrecognised messages to the RPC
connection.
This commit changes start_nodes and stop_nodes to start and stop the
bitcoind nodes in parallel, making test setup and teardown much faster.
On my vm, this changeset reduces total test_runner runtime for the base set of tests
(including building the cache) from 263s to 195s (a 25% speedup). Note that the time
reported by test_runner does not include time spent building the cache:
*with TestNode*:
```
→ date +"%T" ; ./test_runner.py -q ; date +"%T"
12:48:04
..................................................................................................................................................................................................................................................................................................................................
TEST | STATUS | DURATION
abandonconflict.py | ✓ Passed | 12 s
bip68-112-113-p2p.py | ✓ Passed | 19 s
blockchain.py | ✓ Passed | 8 s
bumpfee.py | ✓ Passed | 13 s
decodescript.py | ✓ Passed | 3 s
disablewallet.py | ✓ Passed | 3 s
disconnect_ban.py | ✓ Passed | 6 s
fundrawtransaction.py | ✓ Passed | 37 s
getchaintips.py | ✓ Passed | 4 s
httpbasics.py | ✓ Passed | 3 s
import-rescan.py | ✓ Passed | 4 s
importmulti.py | ✓ Passed | 6 s
importprunedfunds.py | ✓ Passed | 3 s
invalidblockrequest.py | ✓ Passed | 4 s
invalidtxrequest.py | ✓ Passed | 4 s
keypool.py | ✓ Passed | 7 s
listsinceblock.py | ✓ Passed | 4 s
listtransactions.py | ✓ Passed | 33 s
mempool_limit.py | ✓ Passed | 4 s
mempool_persist.py | ✓ Passed | 15 s
mempool_reorg.py | ✓ Passed | 4 s
mempool_resurrect_test.py | ✓ Passed | 3 s
mempool_spendcoinbase.py | ✓ Passed | 3 s
merkle_blocks.py | ✓ Passed | 3 s
multi_rpc.py | ✓ Passed | 4 s
net.py | ✓ Passed | 3 s
nulldummy.py | ✓ Passed | 3 s
p2p-compactblocks.py | ✓ Passed | 28 s
p2p-fullblocktest.py | ✓ Passed | 126 s
p2p-leaktests.py | ✓ Passed | 8 s
p2p-mempool.py | ✓ Passed | 3 s
p2p-segwit.py | ✓ Passed | 59 s
p2p-versionbits-warning.py | ✓ Passed | 8 s
preciousblock.py | ✓ Passed | 3 s
prioritise_transaction.py | ✓ Passed | 5 s
proxy_test.py | ✓ Passed | 3 s
rawtransactions.py | ✓ Passed | 9 s
receivedby.py | ✓ Passed | 19 s
reindex.py | ✓ Passed | 12 s
rest.py | ✓ Passed | 9 s
rpcnamedargs.py | ✓ Passed | 3 s
segwit.py | ✓ Passed | 7 s
sendheaders.py | ✓ Passed | 24 s
signmessages.py | ✓ Passed | 3 s
signrawtransactions.py | ✓ Passed | 3 s
txn_clone.py | ✓ Passed | 4 s
txn_doublespend.py --mineblock | ✓ Passed | 4 s
uptime.py | ✓ Passed | 3 s
wallet-accounts.py | ✓ Passed | 3 s
wallet-dump.py | ✓ Passed | 7 s
wallet-encryption.py | ✓ Passed | 8 s
wallet-hd.py | ✓ Passed | 15 s
wallet.py | ✓ Passed | 31 s
walletbackup.py | ✓ Passed | 104 s
zapwallettxes.py | ✓ Passed | 9 s
zmq_test.py | ○ Skipped | 0 s
ALL | ✓ Passed | 735 s (accumulated)
Runtime: 189 s
12:51:19
```
*master*:
```
→ date +"%T" ; ./test_runner.py -q ; date +"%T"
12:40:13
..........................................................................................................................................................................................................................................................................................................................................................................................................................................
TEST | STATUS | DURATION
abandonconflict.py | ✓ Passed | 15 s
bip68-112-113-p2p.py | ✓ Passed | 19 s
blockchain.py | ✓ Passed | 8 s
bumpfee.py | ✓ Passed | 20 s
decodescript.py | ✓ Passed | 3 s
disablewallet.py | ✓ Passed | 3 s
disconnect_ban.py | ✓ Passed | 8 s
fundrawtransaction.py | ✓ Passed | 36 s
getchaintips.py | ✓ Passed | 11 s
httpbasics.py | ✓ Passed | 7 s
import-rescan.py | ✓ Passed | 16 s
importmulti.py | ✓ Passed | 10 s
importprunedfunds.py | ✓ Passed | 5 s
invalidblockrequest.py | ✓ Passed | 4 s
invalidtxrequest.py | ✓ Passed | 3 s
keypool.py | ✓ Passed | 7 s
listsinceblock.py | ✓ Passed | 11 s
listtransactions.py | ✓ Passed | 37 s
mempool_limit.py | ✓ Passed | 4 s
mempool_persist.py | ✓ Passed | 23 s
mempool_reorg.py | ✓ Passed | 7 s
mempool_resurrect_test.py | ✓ Passed | 3 s
mempool_spendcoinbase.py | ✓ Passed | 3 s
merkle_blocks.py | ✓ Passed | 10 s
multi_rpc.py | ✓ Passed | 6 s
net.py | ✓ Passed | 6 s
nulldummy.py | ✓ Passed | 3 s
p2p-compactblocks.py | ✓ Passed | 30 s
p2p-fullblocktest.py | ✓ Passed | 126 s
p2p-leaktests.py | ✓ Passed | 8 s
p2p-mempool.py | ✓ Passed | 3 s
p2p-segwit.py | ✓ Passed | 62 s
p2p-versionbits-warning.py | ✓ Passed | 8 s
preciousblock.py | ✓ Passed | 8 s
prioritise_transaction.py | ✓ Passed | 7 s
proxy_test.py | ✓ Passed | 10 s
rawtransactions.py | ✓ Passed | 15 s
receivedby.py | ✓ Passed | 28 s
reindex.py | ✓ Passed | 12 s
rest.py | ✓ Passed | 12 s
rpcnamedargs.py | ✓ Passed | 3 s
segwit.py | ✓ Passed | 12 s
sendheaders.py | ✓ Passed | 26 s
signmessages.py | ✓ Passed | 3 s
signrawtransactions.py | ✓ Passed | 3 s
txn_clone.py | ✓ Passed | 10 s
txn_doublespend.py --mineblock | ✓ Passed | 10 s
uptime.py | ✓ Passed | 3 s
wallet-accounts.py | ✓ Passed | 3 s
wallet-dump.py | ✓ Passed | 6 s
wallet-encryption.py | ✓ Passed | 8 s
wallet-hd.py | ✓ Passed | 18 s
wallet.py | ✓ Passed | 69 s
walletbackup.py | ✓ Passed | 130 s
zapwallettxes.py | ✓ Passed | 15 s
zmq_test.py | ○ Skipped | 0 s
ALL | ✓ Passed | 936 s (accumulated)
Runtime: 242 s
12:44:36
```
Tree-SHA512: 6dfc4c11fd0caf7de6954c93679cf22c3df0acc6f432e616d1151062a61f456faa8ae2fe670b427868af55bb564802df84c8fd76e90b4b338750dbc23f46ad88
115 lines
5.1 KiB
Python
Executable File
115 lines
5.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# Copyright (c) 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 dumpwallet RPC."""
|
|
import sys
|
|
|
|
from test_framework.test_framework import BitcoinTestFramework
|
|
from test_framework.util import *
|
|
|
|
|
|
def read_dump(file_name, addrs, hd_master_addr_old):
|
|
"""
|
|
Read the given dump, count the addrs that match, count change and reserve.
|
|
Also check that the old hd_master is inactive
|
|
"""
|
|
with open(file_name, encoding='utf8') as inputfile:
|
|
found_addr = 0
|
|
found_addr_chg = 0
|
|
found_addr_rsv = 0
|
|
hd_master_addr_ret = None
|
|
for line in inputfile:
|
|
# only read non comment lines
|
|
if line[0] != "#" and len(line) > 10:
|
|
# split out some data
|
|
key_label, comment = line.split("#")
|
|
# key = key_label.split(" ")[0]
|
|
keytype = key_label.split(" ")[2]
|
|
if len(comment) > 1:
|
|
addr_keypath = comment.split(" addr=")[1]
|
|
addr = addr_keypath.split(" ")[0]
|
|
keypath = None
|
|
if keytype == "inactivehdmaster=1":
|
|
# ensure the old master is still available
|
|
assert(hd_master_addr_old == addr)
|
|
elif keytype == "hdmaster=1":
|
|
# ensure we have generated a new hd master key
|
|
assert(hd_master_addr_old != addr)
|
|
hd_master_addr_ret = addr
|
|
else:
|
|
keypath = addr_keypath.rstrip().split("hdkeypath=")[1]
|
|
|
|
# count key types
|
|
for addrObj in addrs:
|
|
if addrObj['address'] == addr and addrObj['hdkeypath'] == keypath and keytype == "label=":
|
|
found_addr += 1
|
|
break
|
|
elif keytype == "change=1":
|
|
found_addr_chg += 1
|
|
break
|
|
elif keytype == "reserve=1":
|
|
found_addr_rsv += 1
|
|
break
|
|
return found_addr, found_addr_chg, found_addr_rsv, hd_master_addr_ret
|
|
|
|
|
|
class WalletDumpTest(BitcoinTestFramework):
|
|
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.setup_clean_chain = True
|
|
self.num_nodes = 1
|
|
self.extra_args = [["-keypool=90", "-usehd=1"]]
|
|
|
|
def setup_network(self):
|
|
# TODO remove this when usehd=1 becomes the default
|
|
# use our own cache and -usehd=1 as extra arg as the default cache is run with -usehd=0
|
|
self._initialize_chain(os.path.join(self.options.tmpdir, "hd"), self.num_nodes, os.path.join(self.options.cachedir, "hd"), extra_args=self.extra_args[0], stderr=sys.stdout)
|
|
self.set_cache_mocktime()
|
|
# Use 1 minute timeout because the initial getnewaddress RPC can take
|
|
# longer than the default 30 seconds due to an expensive
|
|
# CWallet::TopUpKeyPool call, and the encryptwallet RPC made later in
|
|
# the test often takes even longer.
|
|
self.nodes = self.start_nodes(self.num_nodes, os.path.join(self.options.tmpdir, "hd"), self.extra_args, timewait=60, stderr=sys.stdout)
|
|
|
|
def run_test (self):
|
|
tmpdir = os.path.join(self.options.tmpdir, "hd")
|
|
|
|
# generate 20 addresses to compare against the dump
|
|
test_addr_count = 20
|
|
addrs = []
|
|
for i in range(0,test_addr_count):
|
|
addr = self.nodes[0].getnewaddress()
|
|
vaddr= self.nodes[0].validateaddress(addr) #required to get hd keypath
|
|
addrs.append(vaddr)
|
|
# Should be a no-op:
|
|
self.nodes[0].keypoolrefill()
|
|
|
|
# dump unencrypted wallet
|
|
self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.unencrypted.dump")
|
|
|
|
found_addr, found_addr_chg, found_addr_rsv, hd_master_addr_unenc = \
|
|
read_dump(tmpdir + "/node0/wallet.unencrypted.dump", addrs, None)
|
|
assert_equal(found_addr, test_addr_count) # all keys must be in the dump
|
|
assert_equal(found_addr_chg, 50) # 50 blocks where mined
|
|
assert_equal(found_addr_rsv, 180) # keypool size (external+internal)
|
|
|
|
#encrypt wallet, restart, unlock and dump
|
|
self.nodes[0].node_encrypt_wallet('test')
|
|
self.nodes[0] = self.start_node(0, tmpdir, self.extra_args[0])
|
|
self.nodes[0].walletpassphrase('test', 10)
|
|
# Should be a no-op:
|
|
self.nodes[0].keypoolrefill()
|
|
self.nodes[0].dumpwallet(tmpdir + "/node0/wallet.encrypted.dump")
|
|
|
|
found_addr, found_addr_chg, found_addr_rsv, hd_master_addr_enc = \
|
|
read_dump(tmpdir + "/node0/wallet.encrypted.dump", addrs, hd_master_addr_unenc)
|
|
assert_equal(found_addr, test_addr_count)
|
|
# TODO clarify if we want the behavior that is tested below in Dash (only when HD seed was generated and not user-provided)
|
|
# assert_equal(found_addr_chg, 180 + 50) # old reserve keys are marked as change now
|
|
assert_equal(found_addr_rsv, 180) # keypool size
|
|
|
|
if __name__ == '__main__':
|
|
WalletDumpTest().main ()
|