Move the create_empty_fork method to the test framework's blocktools.py module to enable reuse across multiple tests.

This commit is contained in:
yuvicc
2025-10-15 12:22:45 +05:30
parent 5336bcd578
commit 540ed333f6
2 changed files with 27 additions and 27 deletions

View File

@@ -84,6 +84,9 @@ assert_equal(uint256_from_compact(DIFF_4_N_BITS), DIFF_4_TARGET)
# From BIP325
SIGNET_HEADER = b"\xec\xc7\xda\xa2"
# Number of blocks to create in temporary blockchain branch for reorg testing
FORK_LENGTH = 10
def nbits_str(nbits):
return f"{nbits:08x}"
@@ -113,6 +116,26 @@ def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl
block.hashMerkleRoot = block.calc_merkle_root()
return block
def create_empty_fork(node, fork_length=FORK_LENGTH):
'''
Creates a fork using node's chaintip as the starting point.
Returns a list of blocks to submit in order.
'''
tip = int(node.getbestblockhash(), 16)
height = node.getblockcount()
block_time = node.getblock(node.getbestblockhash())['time'] + 1
blocks = []
for _ in range(fork_length):
block = create_block(tip, create_coinbase(height + 1), block_time)
block.solve()
blocks.append(block)
tip = block.hash_int
block_time += 1
height += 1
return blocks
def get_witness_script(witness_root, witness_nonce):
witness_commitment = hash256(ser_uint256(witness_root) + ser_uint256(witness_nonce))
output_data = WITNESS_COMMITMENT_HEADER + witness_commitment