Skip to content

Commit d438d60

Browse files
committed
QA: Use GBT to get block versions correct
1 parent 1df2cd1 commit d438d60

File tree

4 files changed

+21
-24
lines changed

4 files changed

+21
-24
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 (
@@ -272,6 +272,8 @@ def test_nonzero_locks(orig_tx, node, relayfee, use_height_lock):
272272

273273
# Advance the time on the node so that we can test timelocks
274274
self.nodes[0].setmocktime(cur_time+600)
275+
# Save block template now to use for the reorg later
276+
tmpl = self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)
275277
self.nodes[0].generate(1)
276278
assert tx2.hash not in self.nodes[0].getrawmempool()
277279

@@ -315,16 +317,15 @@ def test_nonzero_locks(orig_tx, node, relayfee, use_height_lock):
315317
# diagram above).
316318
# This would cause tx2 to be added back to the mempool, which in turn causes
317319
# tx3 to be removed.
318-
tip = int(self.nodes[0].getblockhash(self.nodes[0].getblockcount()-1), 16)
319-
height = self.nodes[0].getblockcount()
320320
for i in range(2):
321-
block = create_block(tip, create_coinbase(height), cur_time)
322-
block.nVersion = 3
321+
block = create_block(tmpl=tmpl, ntime=cur_time)
323322
block.rehash()
324323
block.solve()
325324
tip = block.sha256
326-
height += 1
327325
assert_equal(None if i == 1 else 'inconclusive', self.nodes[0].submitblock(ToHex(block)))
326+
tmpl = self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS)
327+
tmpl['previousblockhash'] = '%x' % tip
328+
tmpl['transactions'] = []
328329
cur_time += 1
329330

330331
mempool = self.nodes[0].getrawmempool()
@@ -372,9 +373,7 @@ def test_bip68_not_consensus(self):
372373
assert_raises_rpc_error(-26, NOT_FINAL_ERROR, self.nodes[0].sendrawtransaction, ToHex(tx3))
373374

374375
# make a block that violates bip68; ensure that the tip updates
375-
tip = int(self.nodes[0].getbestblockhash(), 16)
376-
block = create_block(tip, create_coinbase(self.nodes[0].getblockcount()+1))
377-
block.nVersion = 3
376+
block = create_block(tmpl=self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS))
378377
block.vtx.extend([tx1, tx2, tx3])
379378
block.hashMerkleRoot = block.calc_merkle_root()
380379
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.mininode import mininode_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()
@@ -789,6 +785,9 @@ def announce_cmpct_block(node, peer):
789785
assert_equal(int(node.getbestblockhash(), 16), block.sha256)
790786

791787
def run_test(self):
788+
# Get the nodes out of IBD
789+
self.nodes[0].generate(1)
790+
792791
# Setup the p2p connections
793792
self.segwit_node = self.nodes[0].add_p2p_connection(TestP2PConn(cmpct_version=2))
794793
self.old_node = self.nodes[0].add_p2p_connection(TestP2PConn(cmpct_version=1), services=NODE_NETWORK)

0 commit comments

Comments
 (0)