Skip to content

Commit 61e77bb

Browse files
committed
[test framework] make it easier to fast-forward setmocktime
Have each TestNode keep track of the last timestamp it called setmocktime with, and add a bumpmocktime() function to bump by a number of seconds. Makes it easy to fast forward n seconds without keeping track of what the last timestamp was.
1 parent e92013e commit 61e77bb

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

test/functional/test_framework/test_node.py

+18
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,8 @@ def __init__(self, i, datadir, *, chain, rpchost, timewait, timeout_factor, bitc
144144
self.p2ps = []
145145
self.timeout_factor = timeout_factor
146146

147+
self.mocktime = None
148+
147149
AddressKeyPair = collections.namedtuple('AddressKeyPair', ['address', 'key'])
148150
PRIV_KEYS = [
149151
# address , privkey
@@ -324,6 +326,15 @@ def generatetodescriptor(self, *args, invalid_call, **kwargs):
324326
assert not invalid_call
325327
return self.__getattr__('generatetodescriptor')(*args, **kwargs)
326328

329+
def setmocktime(self, timestamp):
330+
"""Wrapper for setmocktime RPC, sets self.mocktime"""
331+
if timestamp == 0:
332+
# setmocktime(0) resets to system time.
333+
self.mocktime = None
334+
else:
335+
self.mocktime = timestamp
336+
return self.__getattr__('setmocktime')(timestamp)
337+
327338
def get_wallet_rpc(self, wallet_name):
328339
if self.use_cli:
329340
return RPCOverloadWrapper(self.cli("-rpcwallet={}".format(wallet_name)), True, self.descriptors)
@@ -699,6 +710,13 @@ def disconnect_p2ps(self):
699710

700711
wait_until_helper(lambda: self.num_test_p2p_connections() == 0, timeout_factor=self.timeout_factor)
701712

713+
def bumpmocktime(self, seconds):
714+
"""Fast forward using setmocktime to self.mocktime + seconds. Requires setmocktime to have
715+
been called at some point in the past."""
716+
assert self.mocktime
717+
self.mocktime += seconds
718+
self.setmocktime(self.mocktime)
719+
702720

703721
class TestNodeCLIAttr:
704722
def __init__(self, cli, command):

0 commit comments

Comments
 (0)