Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: add a new flag disable_mocktime to set_test_params() #6242

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion test/functional/feature_dip3_deterministicmns.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def set_test_params(self):
self.num_initial_mn = 11 # Should be >= 11 to make sure quorums are not always the same MNs
self.num_nodes = 1 + self.num_initial_mn + 2 # +1 for controller, +1 for mn-qt, +1 for mn created after dip3 activation
self.setup_clean_chain = True
self.disable_mocktime = True
self.supports_cli = False

self.extra_args = ["-deprecatedrpc=addresses"]
Expand All @@ -34,7 +35,6 @@ def skip_test_if_missing_module(self):
self.skip_if_no_wallet()

def setup_network(self):
self.disable_mocktime()
self.add_nodes(1)
self.start_controller_node()
self.import_deterministic_coinbase_privkeys()
Expand Down
2 changes: 1 addition & 1 deletion test/functional/feature_sporks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ class SporkTest(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 3
self.setup_clean_chain = True
self.disable_mocktime = True
self.extra_args = [["-sporkkey=cP4EKFyJsHT39LDqgdcB43Y3YXjNyjb5Fuas1GQSeAtjnZWmZEQK"], [], []]

def setup_network(self):
self.disable_mocktime()
self.setup_nodes()
# connect only 2 first nodes at start
self.connect_nodes(0, 1)
Expand Down
5 changes: 1 addition & 4 deletions test/functional/interface_zmq.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ def caused_notification(self, notification):
class ZMQTest (BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 2
self.disable_mocktime = True
if self.is_wallet_compiled():
self.requires_wallet = True
# This test isn't testing txn relay/timing, so set whitelist on the
Expand All @@ -109,10 +110,6 @@ def skip_test_if_missing_module(self):
# TODO: drop this check after migration to MiniWallet, see bitcoin/bitcoin#24653
self.skip_if_no_bdb()

def setup_network(self):
self.disable_mocktime()
super().setup_network()

def run_test(self):
self.ctx = zmq.Context()
try:
Expand Down
5 changes: 1 addition & 4 deletions test/functional/p2p_eviction.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,13 @@ def on_ping(self, message):
class P2PEvict(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
self.disable_mocktime = True
self.num_nodes = 1
# The choice of maxconnections=32 results in a maximum of 21 inbound connections
# (32 - 10 outbound - 1 feeler). 20 inbound peers are protected from eviction:
# 4 by netgroup, 4 that sent us blocks, 4 that sent us transactions and 8 via lowest ping time
self.extra_args = [['-maxconnections=32']]

def setup_network(self):
self.disable_mocktime()
super().setup_network()

def run_test(self):
protected_peers = set() # peers that we expect to be protected from eviction
current_peer = -1
Expand Down
15 changes: 1 addition & 14 deletions test/functional/p2p_initial_headers_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,9 @@
class HeadersSyncTest(BitcoinTestFramework):
def set_test_params(self):
self.setup_clean_chain = True
self.disable_mocktime = True
self.num_nodes = 1

def setup_chain(self):
# This test operates under the assumption that the adjusted time is well ahead of block
# time.
#
# By default when we setup a new chain, we also adjust the mocktime (this is not done in
# Bitcoin's test suite), which violates this test's assumption and causes it to fail. We
# remedy this by ensuring the test's assumptions are met (i.e. we don't adjust mocktime)
#
self.log.info("Initializing test directory " + self.options.tmpdir)
if self.setup_clean_chain:
self._initialize_chain_clean()
else:
self._initialize_chain()

def announce_random_block(self, peers):
new_block_announcement = msg_inv(inv=[CInv(MSG_BLOCK, random.randrange(1<<256))])
for p in peers:
Expand Down
5 changes: 1 addition & 4 deletions test/functional/p2p_leak.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,7 @@ def on_version(self, msg):
class P2PLeakTest(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 1

def setup_network(self):
self.disable_mocktime()
self.setup_nodes()
self.disable_mocktime = True

def run_test(self):
# Another peer that never sends a version, nor any other messages. It shouldn't receive anything from the node.
Expand Down
41 changes: 19 additions & 22 deletions test/functional/test_framework/test_framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ def __init__(self):
"""Sets test framework defaults. Do not override this method. Instead, override the set_test_params() method"""
self.chain: str = 'regtest'
self.setup_clean_chain: bool = False
self.disable_mocktime: bool = False
self.nodes: List[TestNode] = []
self.network_thread = None
self.mocktime = 0
Expand Down Expand Up @@ -409,10 +410,10 @@ def setup_chain(self):
self.log.info("Initializing test directory " + self.options.tmpdir)
if self.setup_clean_chain:
self._initialize_chain_clean()
self.set_genesis_mocktime()
else:
self._initialize_chain()
self.set_cache_mocktime()
if not self.disable_mocktime:
self._initialize_mocktime(is_genesis=self.setup_clean_chain)

def setup_network(self):
"""Override this method to customize test network topology"""
Expand Down Expand Up @@ -451,8 +452,9 @@ def setup_nodes(self):
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 mocktime')
self.bump_mocktime(156 * 200)
if not self.disable_mocktime:
self.log.debug('Generate a block with current mocktime')
self.bump_mocktime(156 * 200)
block_hash = self.nodes[0].generate(1)[0]
block = self.nodes[0].getblock(blockhash=block_hash, verbosity=0)
for n in self.nodes:
Expand Down Expand Up @@ -810,24 +812,19 @@ def sync_all(self, nodes=None):
self.sync_blocks(nodes)
self.sync_mempools(nodes)

def disable_mocktime(self):
self.mocktime = 0
for node in self.nodes:
node.mocktime = 0

def bump_mocktime(self, t, update_nodes=True, nodes=None):
if self.mocktime != 0:
self.mocktime += t
if update_nodes:
set_node_times(nodes or self.nodes, self.mocktime)
if self.mocktime == 0:
return

def set_cache_mocktime(self):
self.mocktime = TIME_GENESIS_BLOCK + (199 * 156)
for node in self.nodes:
node.mocktime = self.mocktime
self.mocktime += t
if update_nodes:
set_node_times(nodes or self.nodes, self.mocktime)

def set_genesis_mocktime(self):
self.mocktime = TIME_GENESIS_BLOCK
def _initialize_mocktime(self, is_genesis):
if is_genesis:
self.mocktime = TIME_GENESIS_BLOCK
else:
self.mocktime = TIME_GENESIS_BLOCK + (199 * 156)
for node in self.nodes:
node.mocktime = self.mocktime

Expand Down Expand Up @@ -884,7 +881,7 @@ def _initialize_chain(self):
cache_node_dir,
chain=self.chain,
extra_conf=["bind=127.0.0.1"],
extra_args=['-disablewallet', "-mocktime=%d" % TIME_GENESIS_BLOCK],
extra_args=['-disablewallet', f"-mocktime={TIME_GENESIS_BLOCK}"],
extra_args_from_options=self.extra_args_from_options,
rpchost=None,
timewait=self.rpc_timeout,
Expand All @@ -911,7 +908,7 @@ def _initialize_chain(self):
# 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().
self.set_genesis_mocktime()
self._initialize_mocktime(is_genesis=True)
gen_addresses = [k.address for k in TestNode.PRIV_KEYS][:3] + [ADDRESS_BCRT1_P2SH_OP_TRUE]
assert_equal(len(gen_addresses), 4)
for i in range(8):
Expand All @@ -926,7 +923,7 @@ def _initialize_chain(self):
# Shut it down, and clean up cache directories:
self.stop_nodes()
self.nodes = []
self.disable_mocktime()
self.mocktime = 0

def cache_path(*paths):
chain = get_chain_folder(cache_node_dir, self.chain)
Expand Down
12 changes: 9 additions & 3 deletions test/functional/test_framework/test_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ def __init__(self, i, datadir, extra_args_from_options, *, chain, rpchost, timew
"-debug",
"-debugexclude=libevent",
"-debugexclude=leveldb",
"-mocktime=" + str(mocktime),
"-uacomment=testnode%d" % i
"-uacomment=testnode%d" % i,
]
if self.mocktime != 0:
self.args.append(f"-mocktime={mocktime}")

if use_valgrind:
default_suppressions_file = os.path.join(
os.path.dirname(os.path.realpath(__file__)),
Expand Down Expand Up @@ -215,7 +217,7 @@ def start(self, extra_args=None, *, cwd=None, stdout=None, stderr=None, **kwargs

all_args = self.args + self.extra_args_from_options + extra_args
if self.mocktime != 0:
all_args = all_args + ["-mocktime=%d" % self.mocktime]
all_args = all_args + [f"-mocktime={self.mocktime}"]

# Delete any existing cookie file -- if such a file exists (eg due to
# unclean shutdown), it will get overwritten anyway by dashd, and
Expand Down Expand Up @@ -777,3 +779,7 @@ def importaddress(self, address, label=None, rescan=None, p2sh=None):
for res in import_res:
if not res['success']:
raise JSONRPCException(res['error'])

def setmocktime(self, mocktime):
self.mocktime = mocktime
return self.__getattr__('setmocktime')(mocktime)
2 changes: 1 addition & 1 deletion test/functional/wallet_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ def read_dump(file_name, addrs, script_addrs, hd_master_addr_old):
class WalletDumpTest(BitcoinTestFramework):
def set_test_params(self):
self.num_nodes = 1
self.disable_mocktime = True
self.extra_args = [["-keypool=90", "-usehd=1"]]
self.rpc_timeout = 120

def skip_test_if_missing_module(self):
self.skip_if_no_wallet()

def setup_network(self):
self.disable_mocktime()
self.add_nodes(self.num_nodes, extra_args=self.extra_args)
self.start_nodes()

Expand Down
Loading