Skip to content

Commit cb21d86

Browse files
author
MarcoFalke
committed
Merge #19401: QA: Use GBT to get block versions correct
d438d60 QA: Use GBT to get block versions correct (Luke Dashjr) 1df2cd1 QA: blocktools: Accept block template to create_block (Luke Dashjr) Pull request description: The goal here is to decouple unrelated tests from the details of block versions. Currently, these tests are forcing specific versions of blocks for no real reason. ACKs for top commit: fjahr: re-ACK d438d60 benthecarman: ACK d438d60 Tree-SHA512: 523b1cd4dac8d65c88432e126ce7f60df96ca4b94f7ecc8e83ba4ffbade23e2afe7055fdf586ce3c195a533f2004e63fff83add4267b39473a581c9f1c6d5340
2 parents 2947ae6 + d438d60 commit cb21d86

File tree

5 files changed

+45
-32
lines changed

5 files changed

+45
-32
lines changed

test/functional/feature_bip68_sequence.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import time
88

9-
from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment
9+
from test_framework.blocktools import create_block, NORMAL_GBT_REQUEST_PARAMS, add_witness_commitment
1010
from test_framework.messages import COIN, COutPoint, CTransaction, CTxIn, CTxOut, FromHex, ToHex
1111
from test_framework.test_framework import BitcoinTestFramework
1212
from test_framework.util import (
@@ -275,6 +275,8 @@ def test_nonzero_locks(orig_tx, node, relayfee, use_height_lock):
275275

276276
# Advance the time on the node so that we can test timelocks
277277
self.nodes[0].setmocktime(cur_time+600)
278+
# Save block template now to use for the reorg later
279+
tmpl = self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)
278280
self.nodes[0].generate(1)
279281
assert tx2.hash not in self.nodes[0].getrawmempool()
280282

@@ -318,16 +320,15 @@ def test_nonzero_locks(orig_tx, node, relayfee, use_height_lock):
318320
# diagram above).
319321
# This would cause tx2 to be added back to the mempool, which in turn causes
320322
# tx3 to be removed.
321-
tip = int(self.nodes[0].getblockhash(self.nodes[0].getblockcount()-1), 16)
322-
height = self.nodes[0].getblockcount()
323323
for i in range(2):
324-
block = create_block(tip, create_coinbase(height), cur_time)
325-
block.nVersion = 3
324+
block = create_block(tmpl=tmpl, ntime=cur_time)
326325
block.rehash()
327326
block.solve()
328327
tip = block.sha256
329-
height += 1
330328
assert_equal(None if i == 1 else 'inconclusive', self.nodes[0].submitblock(ToHex(block)))
329+
tmpl = self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)
330+
tmpl['previousblockhash'] = '%x' % tip
331+
tmpl['transactions'] = []
331332
cur_time += 1
332333

333334
mempool = self.nodes[0].getrawmempool()
@@ -375,9 +376,7 @@ def test_bip68_not_consensus(self):
375376
assert_raises_rpc_error(-26, NOT_FINAL_ERROR, self.nodes[0].sendrawtransaction, ToHex(tx3))
376377

377378
# make a block that violates bip68; ensure that the tip updates
378-
tip = int(self.nodes[0].getbestblockhash(), 16)
379-
block = create_block(tip, create_coinbase(self.nodes[0].getblockcount()+1))
380-
block.nVersion = 3
379+
block = create_block(tmpl=self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS))
381380
block.vtx.extend([tx1, tx2, tx3])
382381
block.hashMerkleRoot = block.calc_merkle_root()
383382
block.rehash()

test/functional/feature_nulldummy.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"""
1515
import time
1616

17-
from test_framework.blocktools import create_coinbase, create_block, create_transaction, add_witness_commitment
17+
from test_framework.blocktools import NORMAL_GBT_REQUEST_PARAMS, create_block, create_transaction, add_witness_commitment
1818
from test_framework.messages import CTransaction
1919
from test_framework.script import CScript
2020
from test_framework.test_framework import BitcoinTestFramework
@@ -37,14 +37,15 @@ def trueDummy(tx):
3737
class NULLDUMMYTest(BitcoinTestFramework):
3838

3939
def set_test_params(self):
40-
self.num_nodes = 1
40+
# Need two nodes only so GBT doesn't complain that it's not connected
41+
self.num_nodes = 2
4142
self.setup_clean_chain = True
4243
# This script tests NULLDUMMY activation, which is part of the 'segwit' deployment, so we go through
4344
# normal segwit activation here (and don't use the default always-on behaviour).
4445
self.extra_args = [[
4546
'-segwitheight=432',
4647
'-addresstype=legacy',
47-
]]
48+
]] * 2
4849

4950
def skip_test_if_missing_module(self):
5051
self.skip_if_no_wallet()
@@ -61,7 +62,6 @@ def run_test(self):
6162
coinbase_txid.append(self.nodes[0].getblock(i)['tx'][0])
6263
self.nodes[0].generate(427) # Block 429
6364
self.lastblockhash = self.nodes[0].getbestblockhash()
64-
self.tip = int("0x" + self.lastblockhash, 0)
6565
self.lastblockheight = 429
6666
self.lastblocktime = int(time.time()) + 429
6767

@@ -102,8 +102,10 @@ def run_test(self):
102102
self.block_submit(self.nodes[0], test6txs, True, True)
103103

104104
def block_submit(self, node, txs, witness=False, accept=False):
105-
block = create_block(self.tip, create_coinbase(self.lastblockheight + 1), self.lastblocktime + 1)
106-
block.nVersion = 4
105+
tmpl = node.getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)
106+
assert_equal(tmpl['previousblockhash'], self.lastblockhash)
107+
assert_equal(tmpl['height'], self.lastblockheight + 1)
108+
block = create_block(tmpl=tmpl, ntime=self.lastblocktime + 1)
107109
for tx in txs:
108110
tx.rehash()
109111
block.vtx.append(tx)
@@ -114,7 +116,6 @@ def block_submit(self, node, txs, witness=False, accept=False):
114116
assert_equal(None if accept else 'block-validation-failed', node.submitblock(block.serialize().hex()))
115117
if (accept):
116118
assert_equal(node.getbestblockhash(), block.hash)
117-
self.tip = block.sha256
118119
self.lastblockhash = block.hash
119120
self.lastblocktime += 1
120121
self.lastblockheight += 1

test/functional/feature_segwit.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,14 +61,12 @@ def set_test_params(self):
6161
],
6262
[
6363
"-acceptnonstdtxn=1",
64-
"-blockversion=4",
6564
"-rpcserialversion=1",
6665
"-segwitheight=432",
6766
"-addresstype=legacy",
6867
],
6968
[
7069
"-acceptnonstdtxn=1",
71-
"-blockversion=536870915",
7270
"-segwitheight=432",
7371
"-addresstype=legacy",
7472
],

test/functional/p2p_compactblocks.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
"""
1010
import random
1111

12-
from test_framework.blocktools import create_block, create_coinbase, add_witness_commitment
12+
from test_framework.blocktools import create_block, NORMAL_GBT_REQUEST_PARAMS, add_witness_commitment
1313
from test_framework.messages import BlockTransactions, BlockTransactionsRequest, calculate_shortid, CBlock, CBlockHeader, CInv, COutPoint, CTransaction, CTxIn, CTxInWitness, CTxOut, FromHex, HeaderAndShortIDs, msg_no_witness_block, msg_no_witness_blocktxn, msg_cmpctblock, msg_getblocktxn, msg_getdata, msg_getheaders, msg_headers, msg_inv, msg_sendcmpct, msg_sendheaders, msg_tx, msg_block, msg_blocktxn, MSG_BLOCK, MSG_CMPCT_BLOCK, MSG_WITNESS_FLAG, NODE_NETWORK, P2PHeaderAndShortIDs, PrefilledTransaction, ser_uint256, ToHex
1414
from test_framework.p2p import p2p_lock, P2PInterface
1515
from test_framework.script import CScript, OP_TRUE, OP_DROP
@@ -104,11 +104,7 @@ def skip_test_if_missing_module(self):
104104
self.skip_if_no_wallet()
105105

106106
def build_block_on_tip(self, node, segwit=False):
107-
height = node.getblockcount()
108-
tip = node.getbestblockhash()
109-
mtp = node.getblockheader(tip)['mediantime']
110-
block = create_block(int(tip, 16), create_coinbase(height + 1), mtp + 1)
111-
block.nVersion = 4
107+
block = create_block(tmpl=node.getblocktemplate(NORMAL_GBT_REQUEST_PARAMS))
112108
if segwit:
113109
add_witness_commitment(block)
114110
block.solve()
@@ -769,6 +765,9 @@ def announce_cmpct_block(node, peer):
769765
assert_equal(int(node.getbestblockhash(), 16), block.sha256)
770766

771767
def run_test(self):
768+
# Get the nodes out of IBD
769+
self.nodes[0].generate(1)
770+
772771
# Setup the p2p connections
773772
self.segwit_node = self.nodes[0].add_p2p_connection(TestP2PConn(cmpct_version=2))
774773
self.old_node = self.nodes[0].add_p2p_connection(TestP2PConn(cmpct_version=1), services=NODE_NETWORK)

test/functional/test_framework/blocktools.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
55
"""Utilities for manipulating blocks and transactions."""
66

7+
from binascii import a2b_hex
8+
import io
9+
import struct
10+
import time
711
import unittest
812

913
from .address import (
@@ -53,19 +57,31 @@
5357
# From BIP141
5458
WITNESS_COMMITMENT_HEADER = b"\xaa\x21\xa9\xed"
5559

60+
NORMAL_GBT_REQUEST_PARAMS = {"rules": ["segwit"]}
5661

57-
def create_block(hashprev, coinbase, ntime=None, *, version=1):
62+
63+
def create_block(hashprev=None, coinbase=None, ntime=None, *, version=None, tmpl=None, txlist=None):
5864
"""Create a block (with regtest difficulty)."""
5965
block = CBlock()
60-
block.nVersion = version
61-
if ntime is None:
62-
import time
63-
block.nTime = int(time.time() + 600)
66+
if tmpl is None:
67+
tmpl = {}
68+
block.nVersion = version or tmpl.get('version') or 1
69+
block.nTime = ntime or tmpl.get('curtime') or int(time.time() + 600)
70+
block.hashPrevBlock = hashprev or int(tmpl['previousblockhash'], 0x10)
71+
if tmpl and not tmpl.get('bits') is None:
72+
block.nBits = struct.unpack('>I', a2b_hex(tmpl['bits']))[0]
6473
else:
65-
block.nTime = ntime
66-
block.hashPrevBlock = hashprev
67-
block.nBits = 0x207fffff # difficulty retargeting is disabled in REGTEST chainparams
74+
block.nBits = 0x207fffff # difficulty retargeting is disabled in REGTEST chainparams
75+
if coinbase is None:
76+
coinbase = create_coinbase(height=tmpl['height'])
6877
block.vtx.append(coinbase)
78+
if txlist:
79+
for tx in txlist:
80+
if not hasattr(tx, 'calc_sha256'):
81+
txo = CTransaction()
82+
txo.deserialize(io.BytesIO(tx))
83+
tx = txo
84+
block.vtx.append(tx)
6985
block.hashMerkleRoot = block.calc_merkle_root()
7086
block.calc_sha256()
7187
return block

0 commit comments

Comments
 (0)