Skip to content

Commit

Permalink
Merge #15419: qa: Always refresh cache to be out of ibd
Browse files Browse the repository at this point in the history
fa2cdc9 test: Simplify create_cache (MarcoFalke)
fa25210 qa: Fix wallet_txn_doublespend issue (MarcoFalke)
1111aec qa: Always refresh stale cache to be out of ibd (MarcoFalke)
fab0d85 qa: Remove mocktime unless required (MarcoFalke)

Pull request description:

  When starting a test, we are always in IBD because the timestamps on cached blocks are in the past. Usually, we solve that by generating a block at the beginning of the test.

  That is clumsy and might even lead to other problems such as #15360 and #14446 (comment)

  So fix that by getting rid of mocktime and always refreshing the last block of the cache when starting the test framework.

  Should fix #14446

Tree-SHA512: 6af09800f9c86131349a103af617a54551f5f3f3260d38e14e3f30fdd3d91a0feb0100c56cbb12eae4aeac5571ae4b530b16345cbb831d2670237b53351a22c1
  • Loading branch information
MarcoFalke committed Feb 25, 2019
2 parents b4fc525 + fa2cdc9 commit 8f470ec
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 34 deletions.
2 changes: 1 addition & 1 deletion test/functional/feature_proxy.py
Expand Up @@ -44,6 +44,7 @@
class ProxyTest(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 4
self.setup_clean_chain = True

def setup_nodes(self):
self.have_ipv6 = test_ipv6_local()
Expand Down Expand Up @@ -198,4 +199,3 @@ def networks_dict(d):

if __name__ == '__main__':
ProxyTest().main()

4 changes: 1 addition & 3 deletions test/functional/mempool_accept.py
Expand Up @@ -29,7 +29,6 @@
assert_raises_rpc_error,
bytes_to_hex_str,
hex_str_to_bytes,
wait_until,
)


Expand All @@ -38,7 +37,6 @@ def set_test_params(self):
self.num_nodes = 1
self.extra_args = [[
'-txindex',
'-reindex', # Need reindex for txindex
'-acceptnonstdtxn=0', # Try to mimic main-net
]] * self.num_nodes

Expand All @@ -56,7 +54,7 @@ def run_test(self):

self.log.info('Start with empty mempool, and 200 blocks')
self.mempool_size = 0
wait_until(lambda: node.getblockcount() == 200)
assert_equal(node.getblockcount(), 200)
assert_equal(node.getmempoolinfo()['size'], self.mempool_size)
coins = node.listunspent()

Expand Down
12 changes: 12 additions & 0 deletions test/functional/rpc_blockchain.py
Expand Up @@ -35,6 +35,7 @@
from test_framework.blocktools import (
create_block,
create_coinbase,
TIME_GENESIS_BLOCK,
)
from test_framework.messages import (
msg_block,
Expand All @@ -46,9 +47,11 @@

class BlockchainTest(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
self.num_nodes = 1

def run_test(self):
self.mine_chain()
self.restart_node(0, extra_args=['-stopatheight=207', '-prune=1']) # Set extra args with pruning after rescan is complete

self._test_getblockchaininfo()
Expand All @@ -61,6 +64,15 @@ def run_test(self):
self._test_waitforblockheight()
assert self.nodes[0].verifychain(4, 0)

def mine_chain(self):
self.log.info('Create some old blocks')
address = self.nodes[0].get_deterministic_priv_key().address
for t in range(TIME_GENESIS_BLOCK, TIME_GENESIS_BLOCK + 200 * 600, 600):
# ten-minute steps from genesis block time
self.nodes[0].setmocktime(t)
self.nodes[0].generatetoaddress(1, address)
assert_equal(self.nodes[0].getblockchaininfo()['blocks'], 200)

def _test_getblockchaininfo(self):
self.log.info("Test getblockchaininfo")

Expand Down
52 changes: 26 additions & 26 deletions test/functional/test_framework/test_framework.py
Expand Up @@ -29,11 +29,11 @@
get_datadir_path,
initialize_datadir,
p2p_port,
set_node_times,
sync_blocks,
sync_mempools,
)


class TestStatus(Enum):
PASSED = 1
FAILED = 2
Expand Down Expand Up @@ -94,7 +94,6 @@ def __init__(self):
self.setup_clean_chain = False
self.nodes = []
self.network_thread = None
self.mocktime = 0
self.rpc_timeout = 60 # Wait for up to 60 seconds for the RPC server to respond
self.supports_cli = False
self.bind_to_localhost_only = True
Expand Down Expand Up @@ -275,6 +274,19 @@ def setup_nodes(self):
self.add_nodes(self.num_nodes, extra_args)
self.start_nodes()
self.import_deterministic_coinbase_privkeys()
if not self.setup_clean_chain:
for n in self.nodes:
assert_equal(n.getblockchaininfo()["blocks"], 199)
# To ensure that all nodes are out of IBD, the most recent block
# must have a timestamp not too old (see IsInitialBlockDownload()).
self.log.debug('Generate a block with current time')
block_hash = self.nodes[0].generate(1)[0]
block = self.nodes[0].getblock(blockhash=block_hash, verbosity=0)
for n in self.nodes:
n.submitblock(block)
chain_info = n.getblockchaininfo()
assert_equal(chain_info["blocks"], 200)
assert_equal(chain_info["initialblockdownload"], False)

def import_deterministic_coinbase_privkeys(self):
for n in self.nodes:
Expand Down Expand Up @@ -316,7 +328,6 @@ def add_nodes(self, num_nodes, extra_args=None, *, rpchost=None, binary=None):
timewait=self.rpc_timeout,
bitcoind=binary[i],
bitcoin_cli=self.options.bitcoincli,
mocktime=self.mocktime,
coverage_dir=self.options.coveragedir,
cwd=self.options.tmpdir,
extra_conf=extra_confs[i],
Expand Down Expand Up @@ -435,7 +446,7 @@ def _start_logging(self):
def _initialize_chain(self):
"""Initialize a pre-mined blockchain for use by the test.
Create a cache of a 200-block-long chain (with wallet) for MAX_NODES
Create a cache of a 199-block-long chain (with wallet) for MAX_NODES
Afterward, create num_nodes copies from the cache."""

assert self.num_nodes <= MAX_NODES
Expand Down Expand Up @@ -468,7 +479,6 @@ def _initialize_chain(self):
timewait=self.rpc_timeout,
bitcoind=self.options.bitcoind,
bitcoin_cli=self.options.bitcoincli,
mocktime=self.mocktime,
coverage_dir=None,
cwd=self.options.tmpdir,
))
Expand All @@ -479,32 +489,22 @@ def _initialize_chain(self):
for node in self.nodes:
node.wait_for_rpc_connection()

# For backward compatibility of the python scripts with previous
# versions of the cache, set mocktime to Jan 1,
# 2014 + (201 * 10 * 60)"""
self.mocktime = 1388534400 + (201 * 10 * 60)

# Create a 200-block-long chain; each of the 4 first nodes
# Create a 199-block-long chain; each of the 4 first nodes
# gets 25 mature blocks and 25 immature.
# Note: To preserve compatibility with older versions of
# initialize_chain, only 4 nodes will generate coins.
#
# blocks are created with timestamps 10 minutes apart
# starting from 2010 minutes in the past
block_time = self.mocktime - (201 * 10 * 60)
for i in range(2):
for peer in range(4):
for j in range(25):
set_node_times(self.nodes, block_time)
self.nodes[peer].generatetoaddress(1, self.nodes[peer].get_deterministic_priv_key().address)
block_time += 10 * 60
# Must sync before next peer starts generating blocks
sync_blocks(self.nodes)
# The 4th node gets only 24 immature blocks so that the very last
# block in the cache does not age too much (have an old tip age).
# This is needed so that we are out of IBD when the test starts,
# see the tip age check in IsInitialBlockDownload().
for i in range(8):
self.nodes[0].generatetoaddress(25 if i != 7 else 24, self.nodes[i % 4].get_deterministic_priv_key().address)
sync_blocks(self.nodes)

for n in self.nodes:
assert_equal(n.getblockchaininfo()["blocks"], 199)

# Shut them down, and clean up cache directories:
self.stop_nodes()
self.nodes = []
self.mocktime = 0

def cache_path(n, *paths):
return os.path.join(get_datadir_path(self.options.cachedir, n), "regtest", *paths)
Expand Down
5 changes: 2 additions & 3 deletions test/functional/test_framework/test_node.py
Expand Up @@ -61,7 +61,7 @@ class TestNode():
To make things easier for the test writer, any unrecognised messages will
be dispatched to the RPC connection."""

def __init__(self, i, datadir, *, rpchost, timewait, bitcoind, bitcoin_cli, mocktime, coverage_dir, cwd, extra_conf=None, extra_args=None, use_cli=False, start_perf=False):
def __init__(self, i, datadir, *, rpchost, timewait, bitcoind, bitcoin_cli, coverage_dir, cwd, extra_conf=None, extra_args=None, use_cli=False, start_perf=False):
"""
Kwargs:
start_perf (bool): If True, begin profiling the node with `perf` as soon as
Expand Down Expand Up @@ -90,8 +90,7 @@ def __init__(self, i, datadir, *, rpchost, timewait, bitcoind, bitcoin_cli, mock
"-debug",
"-debugexclude=libevent",
"-debugexclude=leveldb",
"-mocktime=" + str(mocktime),
"-uacomment=testnode%d" % i
"-uacomment=testnode%d" % i,
]

self.cli = TestNodeCLI(bitcoin_cli, self.datadir)
Expand Down
10 changes: 9 additions & 1 deletion test/functional/wallet_create_tx.py
Expand Up @@ -7,17 +7,25 @@
from test_framework.util import (
assert_equal,
)
from test_framework.blocktools import (
TIME_GENESIS_BLOCK,
)


class CreateTxWalletTest(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = False
self.setup_clean_chain = True
self.num_nodes = 1

def skip_test_if_missing_module(self):
self.skip_if_no_wallet()

def run_test(self):
self.log.info('Create some old blocks')
self.nodes[0].setmocktime(TIME_GENESIS_BLOCK)
self.nodes[0].generate(200)
self.nodes[0].setmocktime(0)

self.log.info('Check that we have some (old) blocks and that anti-fee-sniping is disabled')
assert_equal(self.nodes[0].getblockchaininfo()['blocks'], 200)
txid = self.nodes[0].sendtoaddress(self.nodes[0].getnewaddress(), 1)
Expand Down
8 changes: 8 additions & 0 deletions test/functional/wallet_txn_doublespend.py
Expand Up @@ -34,6 +34,14 @@ def setup_network(self):
def run_test(self):
# All nodes should start with 1,250 BTC:
starting_balance = 1250

# All nodes should be out of IBD.
# If the nodes are not all out of IBD, that can interfere with
# blockchain sync later in the test when nodes are connected, due to
# timing issues.
for n in self.nodes:
assert n.getblockchaininfo()["initialblockdownload"] == False

for i in range(4):
assert_equal(self.nodes[i].getbalance(), starting_balance)
self.nodes[i].getnewaddress("") # bug workaround, coins generated assigned to first getnewaddress!
Expand Down

0 comments on commit 8f470ec

Please sign in to comment.