Refactor/fix mocktime usage in tests

This commit is contained in:
UdjinM6 2019-08-09 02:14:11 +03:00 committed by Pasta
parent 9c8365ee62
commit d0288fba58
No known key found for this signature in database
GPG Key ID: D362C9F7142766AE
25 changed files with 91 additions and 99 deletions

View File

@ -203,8 +203,8 @@ class AddressIndexTest(BitcoinTestFramework):
self.nodes[2].invalidateblock(best_hash)
self.nodes[3].invalidateblock(best_hash)
# Allow some time for the reorg to start
set_mocktime(get_mocktime() + 2)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(2)
set_node_times(self.nodes, self.mocktime)
self.sync_all()
balance4 = self.nodes[1].getaddressbalance(address2)
@ -247,8 +247,8 @@ class AddressIndexTest(BitcoinTestFramework):
tx.rehash()
signed_tx = self.nodes[2].signrawtransaction(binascii.hexlify(tx.serialize()).decode("utf-8"))
memtxid1 = self.nodes[2].sendrawtransaction(signed_tx["hex"], True)
set_mocktime(get_mocktime() + 2)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(2)
set_node_times(self.nodes, self.mocktime)
tx2 = CTransaction()
tx2.vin = [CTxIn(COutPoint(int(unspent[1]["txid"], 16), unspent[1]["vout"]))]
@ -262,8 +262,8 @@ class AddressIndexTest(BitcoinTestFramework):
tx2.rehash()
signed_tx2 = self.nodes[2].signrawtransaction(binascii.hexlify(tx2.serialize()).decode("utf-8"))
memtxid2 = self.nodes[2].sendrawtransaction(signed_tx2["hex"], True)
set_mocktime(get_mocktime() + 2)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(2)
set_node_times(self.nodes, self.mocktime)
mempool = self.nodes[2].getaddressmempool({"addresses": [address3]})
assert_equal(len(mempool), 3)
@ -290,8 +290,8 @@ class AddressIndexTest(BitcoinTestFramework):
self.nodes[2].importprivkey(privKey3)
signed_tx3 = self.nodes[2].signrawtransaction(binascii.hexlify(tx.serialize()).decode("utf-8"))
memtxid3 = self.nodes[2].sendrawtransaction(signed_tx3["hex"], True)
set_mocktime(get_mocktime() + 2)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(2)
set_node_times(self.nodes, self.mocktime)
mempool3 = self.nodes[2].getaddressmempool({"addresses": [address3]})
assert_equal(len(mempool3), 2)

View File

@ -183,8 +183,8 @@ class AssumeValidTest(BitcoinTestFramework):
node2.wait_for_verack()
# Make sure nodes actually accept the many headers
set_mocktime(self.block_time)
set_node_times(self.nodes, get_mocktime())
self.set_mocktime(self.block_time)
set_node_times(self.nodes, self.mocktime)
# send header lists to all three nodes.
# node0 does not need to receive all headers

View File

@ -63,7 +63,7 @@ class BIP65Test(ComparisonTestFramework):
height = 3 # height of the next block to build
self.tip = int("0x" + self.nodes[0].getbestblockhash(), 0)
self.nodeaddress = self.nodes[0].getnewaddress()
self.last_block_time = get_mocktime() + 1
self.last_block_time = self.mocktime + 1
''' 398 more version 3 blocks '''
test_blocks = []

View File

@ -43,7 +43,7 @@ bip112txs_vary_OP_CSV_9 - 16 txs with nSequence = 9 evaluated against varying {r
bip112tx_special - test negative argument to OP_CSV
"""
from test_framework.test_framework import ComparisonTestFramework
from test_framework.test_framework import (ComparisonTestFramework, GENESISTIME)
from test_framework.util import *
from test_framework.mininode import ToHex, CTransaction, NetworkThread
from test_framework.blocktools import create_coinbase, create_block

View File

@ -242,7 +242,7 @@ class BIP68Test(BitcoinTestFramework):
# Now mine some blocks, but make sure tx2 doesn't get mined.
# Use prioritisetransaction to lower the effective feerate to 0
self.nodes[0].prioritisetransaction(tx2.hash, int(-self.relayfee*COIN))
cur_time = get_mocktime()
cur_time = self.mocktime
for i in range(10):
self.nodes[0].setmocktime(cur_time + 600)
self.nodes[0].generate(1)
@ -318,7 +318,7 @@ class BIP68Test(BitcoinTestFramework):
assert(tx2.hash in mempool)
# Reset the chain and get rid of the mocktimed-blocks
self.nodes[0].setmocktime(get_mocktime())
self.nodes[0].setmocktime(self.mocktime)
self.nodes[0].invalidateblock(self.nodes[0].getblockhash(cur_height+1))
self.nodes[0].generate(10)
@ -359,7 +359,7 @@ class BIP68Test(BitcoinTestFramework):
# make a block that violates bip68; ensure that the tip updates
tip = int(self.nodes[0].getbestblockhash(), 16)
block = create_block(tip, create_coinbase(self.nodes[0].getblockcount()+1), get_mocktime() + 600)
block = create_block(tip, create_coinbase(self.nodes[0].getblockcount()+1), self.mocktime + 600)
block.nVersion = 3
block.vtx.extend([tx1, tx2, tx3])
block.hashMerkleRoot = block.calc_merkle_root()

View File

@ -82,7 +82,7 @@ class BIP9SoftForksTest(ComparisonTestFramework):
self.height = 3 # height of the next block to build
self.tip = int("0x" + self.nodes[0].getbestblockhash(), 0)
self.nodeaddress = self.nodes[0].getnewaddress()
self.last_block_time = get_mocktime() + 1
self.last_block_time = self.mocktime + 1
assert_equal(self.get_bip9_status(bipName)['status'], 'defined')
assert_equal(self.get_bip9_status(bipName)['since'], 0)

View File

@ -70,7 +70,7 @@ class BIP66Test(ComparisonTestFramework):
height = 3 # height of the next block to build
self.tip = int("0x" + self.nodes[0].getbestblockhash(), 0)
self.nodeaddress = self.nodes[0].getnewaddress()
self.last_block_time = get_mocktime() + 1
self.last_block_time = self.mocktime + 1
''' 298 more version 2 blocks '''
test_blocks = []

View File

@ -28,7 +28,7 @@ class DIP3Test(BitcoinTestFramework):
def setup_network(self):
disable_mocktime()
self.disable_mocktime()
self.start_controller_node()
def start_controller_node(self, extra_args=None):

View File

@ -269,8 +269,8 @@ class LLMQCoinbaseCommitmentsTest(DashTestFramework):
else:
assert_equal(merkleRootQuorums, 0)
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(1)
set_node_times(self.nodes, self.mocktime)
self.nodes[0].spork("SPORK_17_QUORUM_DKG_ENABLED", 0)
self.wait_for_sporks_same()

View File

@ -9,8 +9,6 @@ from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (assert_equal,
assert_raises_jsonrpc,
connect_nodes_bi,
set_mocktime,
get_mocktime,
set_node_times,
)
@ -61,8 +59,8 @@ class DisconnectBanTest(BitcoinTestFramework):
self.nodes[1].setban("2001:4d48:ac57:400:cacf:e9ff:fe1d:9c63/19", "add", 1000) # ban for 1000 seconds
listBeforeShutdown = self.nodes[1].listbanned()
assert_equal("192.168.0.1/32", listBeforeShutdown[2]['address'])
set_mocktime(get_mocktime() + 2)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(2)
set_node_times(self.nodes, self.mocktime)
assert wait_until(lambda: len(self.nodes[1].listbanned()) == 3, timeout=10)
self.stop_node(1)

View File

@ -38,7 +38,7 @@ class InvalidBlockRequestTest(ComparisonTestFramework):
def get_tests(self):
if self.tip is None:
self.tip = int("0x" + self.nodes[0].getbestblockhash(), 0)
self.block_time = get_mocktime() + 1
self.block_time = self.mocktime + 1
'''
Create a new block with an anyone-can-spend coinbase

View File

@ -10,8 +10,6 @@ In this test we connect to one node over p2p, and test tx requests.
from test_framework.test_framework import ComparisonTestFramework
from test_framework.comptool import TestManager, TestInstance, RejectResult
from test_framework.blocktools import *
from test_framework.util import get_mocktime
# Use the ComparisonTestFramework with 1 node: only use --testbinary.
@ -34,7 +32,7 @@ class InvalidTxRequestTest(ComparisonTestFramework):
def get_tests(self):
if self.tip is None:
self.tip = int("0x" + self.nodes[0].getbestblockhash(), 0)
self.block_time = get_mocktime() + 1
self.block_time = self.mocktime + 1
'''
Create a new block with an anyone-can-spend coinbase

View File

@ -93,8 +93,8 @@ class LLMQDKGErrors(DashTestFramework):
self.nodes[0].spork("SPORK_17_QUORUM_DKG_ENABLED", 4070908800)
self.wait_for_sporks_same()
for i in range(blockCount):
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(1)
set_node_times(self.nodes, self.mocktime)
self.nodes[0].generate(1)
self.sync_all()
self.nodes[0].spork("SPORK_17_QUORUM_DKG_ENABLED", 0)

View File

@ -168,8 +168,8 @@ class LLMQ_IS_CL_Conflicts(DashTestFramework):
self.nodes[0].sendrawtransaction(rawtx1)
# fast forward 11 minutes, so that the TX is considered safe and included in the next block
set_mocktime(get_mocktime() + int(60 * 11))
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(int(60 * 11))
set_node_times(self.nodes, self.mocktime)
# Mine the conflicting TX into a block
good_tip = self.nodes[0].getbestblockhash()

View File

@ -76,13 +76,13 @@ class LLMQSigningTest(DashTestFramework):
assert_sigs_nochange(True, False, True, 3)
# fast forward 6.5 days, recovered sig should still be valid
set_mocktime(get_mocktime() + int(60 * 60 * 24 * 6.5))
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(int(60 * 60 * 24 * 6.5))
set_node_times(self.nodes, self.mocktime)
# Cleanup starts every 5 seconds
wait_for_sigs(True, False, True, 15)
# fast forward 1 day, recovered sig should not be valid anymore
set_mocktime(get_mocktime() + int(60 * 60 * 24 * 1))
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(int(60 * 60 * 24 * 1))
set_node_times(self.nodes, self.mocktime)
# Cleanup starts every 5 seconds
wait_for_sigs(False, False, False, 15)

View File

@ -42,9 +42,9 @@ class MaxUploadTest(BitcoinTestFramework):
def run_test(self):
# Advance all nodes 2 weeks in the future
old_mocktime = get_mocktime()
old_mocktime = self.mocktime
current_mocktime = old_mocktime + 2*60*60*24*7
set_mocktime(current_mocktime)
self.set_mocktime(current_mocktime)
set_node_times(self.nodes, current_mocktime)
# Before we connect anything, we first set the time on the node

View File

@ -121,8 +121,8 @@ class MultiKeySporkTest(BitcoinTestFramework):
for node in self.nodes:
assert(self.get_test_spork_state(node) == 4070908800)
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(1)
set_node_times(self.nodes, self.mocktime)
# first and second signers set spork value
self.set_test_spork_state(self.nodes[0], 1)
self.set_test_spork_state(self.nodes[1], 1)
@ -136,8 +136,8 @@ class MultiKeySporkTest(BitcoinTestFramework):
for node in self.nodes:
assert(self.wait_for_test_spork_state(node, 1))
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(1)
set_node_times(self.nodes, self.mocktime)
# now set the spork again with other signers to test
# old and new spork messages interaction
self.set_test_spork_state(self.nodes[2], 2)

View File

@ -55,7 +55,7 @@ class NULLDUMMYTest(BitcoinTestFramework):
self.lastblockhash = self.nodes[0].getbestblockhash()
self.tip = int("0x" + self.lastblockhash, 0)
self.lastblockheight = 429
self.lastblocktime = get_mocktime() + 429
self.lastblocktime = self.mocktime + 429
self.log.info("Test 1: NULLDUMMY compliant base transactions should be accepted to mempool and mined before activation [430]")
test1txs = [self.create_transaction(self.nodes[0], coinbase_txid[0], self.ms_address, 49)]

View File

@ -96,7 +96,7 @@ class AcceptBlockTest(BitcoinTestFramework):
# 2. Send one block that builds on each tip.
# This should be accepted.
blocks_h2 = [] # the height 2 blocks on each node's chain
block_time = get_mocktime() + 1
block_time = self.mocktime + 1
for i in range(2):
blocks_h2.append(create_block(tips[i], create_coinbase(2), block_time + 1))
blocks_h2[i].solve()
@ -168,8 +168,8 @@ class AcceptBlockTest(BitcoinTestFramework):
headers_message.headers.append(CBlockHeader(next_block))
tips[j] = next_block
set_mocktime(get_mocktime() + 2)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(2)
set_node_times(self.nodes, self.mocktime)
# Blocks 1-287 should be accepted, block 288 should be ignored because it's too far ahead
for x in all_blocks[:-1]:
self.nodes[0].getblock(x.hash)

View File

@ -106,7 +106,7 @@ class FullBlockTest(ComparisonTestFramework):
def next_block(self, number, spend=None, additional_coinbase_value=0, script=CScript([OP_TRUE]), solve=True):
if self.tip == None:
base_block_hash = self.genesis_hash
block_time = get_mocktime() + 1
block_time = self.mocktime + 1
else:
base_block_hash = self.tip.sha256
block_time = self.tip.nTime + 1
@ -676,7 +676,7 @@ class FullBlockTest(ComparisonTestFramework):
# A block with timestamp > 2 hrs in the future
tip(44)
b48 = block(48, solve=False)
b48.nTime = get_mocktime() + 60 * 60 * 3
b48.nTime = self.mocktime + 60 * 60 * 3
b48.solve()
yield rejected(RejectResult(16, b'time-too-new'))

View File

@ -56,8 +56,8 @@ class InstantSendTest(DashTestFramework):
# send doublespend transaction to isolated node
isolated.sendrawtransaction(dblspnd_tx['hex'])
# generate block on isolated node with doublespend transaction
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(1)
set_node_times(self.nodes, self.mocktime)
isolated.generate(1)
wrong_block = isolated.getbestblockhash()
# connect isolated block to network
@ -73,8 +73,8 @@ class InstantSendTest(DashTestFramework):
timeout = 1
# mine more blocks
# TODO: mine these blocks on an isolated node
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(1)
set_node_times(self.nodes, self.mocktime)
self.nodes[0].generate(2)
self.sync_all()

View File

@ -117,11 +117,10 @@ class PrioritiseTransactionTest(BitcoinTestFramework):
# Test that calling prioritisetransaction is sufficient to trigger
# getblocktemplate to (eventually) return a new block.
mock_time = get_mocktime()
self.nodes[0].setmocktime(mock_time)
self.nodes[0].setmocktime(self.mocktime)
template = self.nodes[0].getblocktemplate()
self.nodes[0].prioritisetransaction(tx_id, -int(self.relayfee*COIN))
self.nodes[0].setmocktime(mock_time+10)
self.nodes[0].setmocktime(self.mocktime+10)
new_template = self.nodes[0].getblocktemplate()
assert(template != new_template)

View File

@ -19,7 +19,7 @@ class SporkTest(BitcoinTestFramework):
self.extra_args = [["-sporkkey=cP4EKFyJsHT39LDqgdcB43Y3YXjNyjb5Fuas1GQSeAtjnZWmZEQK"], [], []]
def setup_network(self):
disable_mocktime()
self.disable_mocktime()
self.setup_nodes()
# connect only 2 first nodes at start
connect_nodes(self.nodes[0], 1)

View File

@ -25,7 +25,6 @@ from .authproxy import JSONRPCException
from . import coverage
from .util import (
PortSeed,
GENESISTIME,
MAX_NODES,
assert_equal,
check_json_precision,
@ -33,16 +32,12 @@ from .util import (
connect_nodes,
copy_datadir,
disconnect_nodes,
get_mocktime,
get_rpc_proxy,
initialize_datadir,
get_datadir_path,
log_filename,
p2p_port,
rpc_url,
set_cache_mocktime,
set_genesis_mocktime,
set_mocktime,
set_node_times,
satoshi_round,
sync_blocks,
@ -62,6 +57,8 @@ TEST_EXIT_SKIPPED = 77
BITCOIND_PROC_WAIT_TIMEOUT = 60
GENESISTIME = 1417713337
class BitcoinTestFramework(object):
"""Base class for a bitcoin test script.
@ -92,10 +89,10 @@ class BitcoinTestFramework(object):
self.log.info("Initializing test directory " + self.options.tmpdir)
if self.setup_clean_chain:
self._initialize_chain_clean(self.options.tmpdir, self.num_nodes)
set_genesis_mocktime()
self.set_genesis_mocktime()
else:
self._initialize_chain(self.options.tmpdir, self.num_nodes, self.options.cachedir)
set_cache_mocktime()
self.set_cache_mocktime()
def setup_network(self):
self.setup_nodes()
@ -335,21 +332,21 @@ class BitcoinTestFramework(object):
sync_blocks(group)
sync_mempools(group)
def enable_mocktime(self):
"""Enable mocktime for the script.
mocktime may be needed for scripts that use the cached version of the
blockchain. If the cached version of the blockchain is used without
mocktime then the mempools will not sync due to IBD.
For backwared compatibility of the python scripts with previous
versions of the cache, this helper function sets mocktime to Jan 1,
2014 + (201 * 10 * 60)"""
self.mocktime = 1388534400 + (201 * 10 * 60)
def disable_mocktime(self):
self.mocktime = 0
def bump_mocktime(self, t):
self.mocktime += t
def set_cache_mocktime(self):
# For backwared compatibility of the python scripts
# with previous versions of the cache, set MOCKTIME
# to regtest genesis time + (201 * 156)
self.mocktime = GENESISTIME + (201 * 156)
def set_genesis_mocktime(self):
self.mocktime = GENESISTIME
# Private helper methods. These should not be accessed by the subclass test scripts.
def _start_logging(self):
@ -402,7 +399,7 @@ class BitcoinTestFramework(object):
shutil.rmtree(os.path.join(cachedir, "node" + str(i)))
# Create cache directories, run dashds:
set_genesis_mocktime()
self.set_genesis_mocktime()
for i in range(MAX_NODES):
datadir = initialize_datadir(cachedir, i)
args = [os.getenv("DASHD", "dashd"), "-server", "-keypool=1", "-datadir=" + datadir, "-discover=0", "-mocktime="+str(GENESISTIME)]
@ -610,8 +607,8 @@ class DashTestFramework(BitcoinTestFramework):
self.nodes.append(self.start_node(0, self.options.tmpdir, self.extra_args))
required_balance = MASTERNODE_COLLATERAL * self.mn_count + 1
while self.nodes[0].getbalance() < required_balance:
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(1)
set_node_times(self.nodes, self.mocktime)
self.nodes[0].generate(1)
# create connected simple nodes
for i in range(0, self.num_nodes - self.mn_count - 1):
@ -629,13 +626,13 @@ class DashTestFramework(BitcoinTestFramework):
self.prepare_datadirs()
self.start_masternodes()
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(1)
set_node_times(self.nodes, self.mocktime)
self.nodes[0].generate(1)
# sync nodes
self.sync_all()
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(1)
set_node_times(self.nodes, self.mocktime)
mn_info = self.nodes[0].masternodelist("status")
assert (len(mn_info) == self.mn_count)
@ -772,8 +769,8 @@ class DashTestFramework(BitcoinTestFramework):
# move forward to next DKG
skip_count = 24 - (self.nodes[0].getblockcount() % 24)
if skip_count != 0:
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(1)
set_node_times(self.nodes, self.mocktime)
self.nodes[0].generate(skip_count)
sync_blocks(self.nodes)
@ -781,36 +778,36 @@ class DashTestFramework(BitcoinTestFramework):
self.wait_for_quorum_phase(1, None, 0)
# Give nodes some time to connect to neighbors
time.sleep(2)
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(1)
set_node_times(self.nodes, self.mocktime)
self.nodes[0].generate(2)
sync_blocks(self.nodes)
# Make sure all reached phase 2 (contribute) and received all contributions
self.wait_for_quorum_phase(2, "receivedContributions", expected_contributions)
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(1)
set_node_times(self.nodes, self.mocktime)
self.nodes[0].generate(2)
sync_blocks(self.nodes)
# Make sure all reached phase 3 (complain) and received all complaints
self.wait_for_quorum_phase(3, "receivedComplaints", expected_complaints)
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(1)
set_node_times(self.nodes, self.mocktime)
self.nodes[0].generate(2)
sync_blocks(self.nodes)
# Make sure all reached phase 4 (justify)
self.wait_for_quorum_phase(4, "receivedJustifications", expected_justifications)
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(1)
set_node_times(self.nodes, self.mocktime)
self.nodes[0].generate(2)
sync_blocks(self.nodes)
# Make sure all reached phase 5 (commit)
self.wait_for_quorum_phase(5, "receivedPrematureCommitments", expected_commitments)
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(1)
set_node_times(self.nodes, self.mocktime)
self.nodes[0].generate(2)
sync_blocks(self.nodes)
@ -821,13 +818,13 @@ class DashTestFramework(BitcoinTestFramework):
self.wait_for_quorum_commitment()
# mine the final commitment
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(1)
set_node_times(self.nodes, self.mocktime)
self.nodes[0].generate(1)
while quorums == self.nodes[0].quorum("list"):
time.sleep(2)
set_mocktime(get_mocktime() + 1)
set_node_times(self.nodes, get_mocktime())
self.bump_mocktime(1)
set_node_times(self.nodes, self.mocktime)
self.nodes[0].generate(1)
sync_blocks(self.nodes)
new_quorum = self.nodes[0].quorum("list", 1)["llmq_5_60"][0]

View File

@ -65,7 +65,7 @@ class WalletDumpTest(BitcoinTestFramework):
# 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)
set_cache_mocktime()
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