Skip to content

Commit 3a36ec8

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#23521: test: refactor: dedup code by taking use of create_block parameters
e57c0eb test: refactor: replace OP_1/OP_TRUE magic numbers by constants (Sebastian Falbesoner) df5d783 test: refactor: take use of `create_block` txlist parameter (Sebastian Falbesoner) ae9df4e test: refactor: take use of `create_block` version parameter (or use default) (Sebastian Falbesoner) Pull request description: The helper `create_block` offers two parameters `version` and `txlist` which set the `nVersion` field / extend the `vtx` array of the block, respectively. By taking use of those, we can remove a lot of code, including the recalculation of the merkle root. Both passing txs in string and `CTransaction` format is supported, i.e. we also save potential calls to `tx_from_hex`. The PR also contains another commit which replaces magic numbers for OP_TRUE/OP_1 (0x51) with the proper constant from the `script` module. Instances setting the block version of 4 explicitely after calling `create_block` are removed, as this is the default since #16333 got merged (see bitcoin/bitcoin#23521 (comment)). ACKs for top commit: stratospher: tested ACK e57c0eb. Tree-SHA512: a56965168d36b40b84e7f334b00472b82c31e8482c9e2651c97a791abd7fee3b40ca15e943a7acafa3acf172066fdace38bb13240084b789fd6ff4f6e510e23a
2 parents 47fe744 + e57c0eb commit 3a36ec8

14 files changed

+37
-73
lines changed

test/functional/feature_assumevalid.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,8 @@ def run_test(self):
122122
tx.vout.append(CTxOut(49 * 100000000, CScript([OP_TRUE])))
123123
tx.calc_sha256()
124124

125-
block102 = create_block(self.tip, create_coinbase(height), self.block_time)
125+
block102 = create_block(self.tip, create_coinbase(height), self.block_time, txlist=[tx])
126126
self.block_time += 1
127-
block102.vtx.extend([tx])
128-
block102.hashMerkleRoot = block102.calc_merkle_root()
129127
block102.solve()
130128
self.blocks.append(block102)
131129
self.tip = block102.sha256
@@ -135,7 +133,6 @@ def run_test(self):
135133
# Bury the assumed valid block 2100 deep
136134
for _ in range(2100):
137135
block = create_block(self.tip, create_coinbase(height), self.block_time)
138-
block.nVersion = 4
139136
block.solve()
140137
self.blocks.append(block)
141138
self.tip = block.sha256

test/functional/feature_bip68_sequence.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,7 @@ def test_bip68_not_consensus(self):
388388
assert_raises_rpc_error(-26, NOT_FINAL_ERROR, self.nodes[0].sendrawtransaction, tx3.serialize().hex())
389389

390390
# make a block that violates bip68; ensure that the tip updates
391-
block = create_block(tmpl=self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS))
392-
block.vtx.extend([tx1, tx2, tx3])
393-
block.hashMerkleRoot = block.calc_merkle_root()
391+
block = create_block(tmpl=self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS), txlist=[tx1, tx2, tx3])
394392
add_witness_commitment(block)
395393
block.solve()
396394

test/functional/feature_block.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,11 +1361,10 @@ def next_block(self, number, spend=None, additional_coinbase_value=0, script=CSc
13611361
else:
13621362
coinbase.vout[0].nValue += spend.vout[0].nValue - 1 # all but one satoshi to fees
13631363
coinbase.rehash()
1364-
block = create_block(base_block_hash, coinbase, block_time, version=version)
13651364
tx = self.create_tx(spend, 0, 1, script) # spend 1 satoshi
13661365
self.sign_tx(tx, spend)
1367-
self.add_transactions_to_block(block, [tx])
1368-
block.hashMerkleRoot = block.calc_merkle_root()
1366+
tx.rehash()
1367+
block = create_block(base_block_hash, coinbase, block_time, version=version, txlist=[tx])
13691368
# Block is created. Find a valid nonce.
13701369
block.solve()
13711370
self.tip = block

test/functional/feature_cltv.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,7 @@ def run_test(self):
120120

121121
tip = self.nodes[0].getbestblockhash()
122122
block_time = self.nodes[0].getblockheader(tip)['mediantime'] + 1
123-
block = create_block(int(tip, 16), create_coinbase(CLTV_HEIGHT - 1), block_time)
124-
block.nVersion = 3
125-
block.vtx.extend(invalid_cltv_txs)
126-
block.hashMerkleRoot = block.calc_merkle_root()
123+
block = create_block(int(tip, 16), create_coinbase(CLTV_HEIGHT - 1), block_time, version=3, txlist=invalid_cltv_txs)
127124
block.solve()
128125

129126
self.test_cltv_info(is_active=False) # Not active as of current tip and next block does not need to obey rules
@@ -134,8 +131,7 @@ def run_test(self):
134131
self.log.info("Test that blocks must now be at least version 4")
135132
tip = block.sha256
136133
block_time += 1
137-
block = create_block(tip, create_coinbase(CLTV_HEIGHT), block_time)
138-
block.nVersion = 3
134+
block = create_block(tip, create_coinbase(CLTV_HEIGHT), block_time, version=3)
139135
block.solve()
140136

141137
with self.nodes[0].assert_debug_log(expected_msgs=[f'{block.hash}, bad-version(0x00000003)']):

test/functional/feature_csv_activation.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,7 @@ def generate_blocks(self, number):
173173
return test_blocks
174174

175175
def create_test_block(self, txs):
176-
block = create_block(self.tip, create_coinbase(self.tipheight + 1), self.last_block_time + 600)
177-
block.nVersion = 4
178-
block.vtx.extend(txs)
179-
block.hashMerkleRoot = block.calc_merkle_root()
176+
block = create_block(self.tip, create_coinbase(self.tipheight + 1), self.last_block_time + 600, txlist=txs)
180177
block.solve()
181178
return block
182179

test/functional/feature_dersig.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ def run_test(self):
8585

8686
tip = self.nodes[0].getbestblockhash()
8787
block_time = self.nodes[0].getblockheader(tip)['mediantime'] + 1
88-
block = create_block(int(tip, 16), create_coinbase(DERSIG_HEIGHT - 1), block_time)
89-
block.vtx.append(spendtx)
90-
block.hashMerkleRoot = block.calc_merkle_root()
88+
block = create_block(int(tip, 16), create_coinbase(DERSIG_HEIGHT - 1), block_time, txlist=[spendtx])
9189
block.solve()
9290

9391
assert_equal(self.nodes[0].getblockcount(), DERSIG_HEIGHT - 2)
@@ -100,8 +98,7 @@ def run_test(self):
10098
self.log.info("Test that blocks must now be at least version 3")
10199
tip = block.sha256
102100
block_time += 1
103-
block = create_block(tip, create_coinbase(DERSIG_HEIGHT), block_time)
104-
block.nVersion = 2
101+
block = create_block(tip, create_coinbase(DERSIG_HEIGHT), block_time, version=2)
105102
block.solve()
106103

107104
with self.nodes[0].assert_debug_log(expected_msgs=[f'{block.hash}, bad-version(0x00000002)']):

test/functional/feature_taproot.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,12 +1266,8 @@ def block_submit(self, node, txs, msg, err_msg, cb_pubkey=None, fees=0, sigops_w
12661266
# transactions.
12671267
extra_output_script = CScript([OP_CHECKSIG]*((MAX_BLOCK_SIGOPS_WEIGHT - sigops_weight) // WITNESS_SCALE_FACTOR))
12681268

1269-
block = create_block(self.tip, create_coinbase(self.lastblockheight + 1, pubkey=cb_pubkey, extra_output_script=extra_output_script, fees=fees), self.lastblocktime + 1)
1270-
block.nVersion = 4
1271-
for tx in txs:
1272-
tx.rehash()
1273-
block.vtx.append(tx)
1274-
block.hashMerkleRoot = block.calc_merkle_root()
1269+
coinbase_tx = create_coinbase(self.lastblockheight + 1, pubkey=cb_pubkey, extra_output_script=extra_output_script, fees=fees)
1270+
block = create_block(self.tip, coinbase_tx, self.lastblocktime + 1, txlist=txs)
12751271
witness and add_witness_commitment(block)
12761272
block.solve()
12771273
block_response = node.submitblock(block.serialize().hex())

test/functional/feature_versionbits_warning.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ def send_blocks_with_version(self, peer, numblocks, version):
4848
tip = int(tip, 16)
4949

5050
for _ in range(numblocks):
51-
block = create_block(tip, create_coinbase(height + 1), block_time)
52-
block.nVersion = version
51+
block = create_block(tip, create_coinbase(height + 1), block_time, version=version)
5352
block.solve()
5453
peer.send_message(msg_block(block))
5554
block_time += 1

test/functional/interface_zmq.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from test_framework.messages import (
1919
CTransaction,
2020
hash256,
21-
tx_from_hex,
2221
)
2322
from test_framework.util import (
2423
assert_equal,
@@ -402,12 +401,8 @@ def test_sequence(self):
402401
raw_tx = self.nodes[0].getrawtransaction(orig_txid)
403402
bump_info = self.nodes[0].bumpfee(orig_txid)
404403
# Mine the pre-bump tx
405-
block = create_block(int(self.nodes[0].getbestblockhash(), 16), create_coinbase(self.nodes[0].getblockcount()+1))
406-
tx = tx_from_hex(raw_tx)
407-
block.vtx.append(tx)
408-
for txid in more_tx:
409-
tx = tx_from_hex(self.nodes[0].getrawtransaction(txid))
410-
block.vtx.append(tx)
404+
txs_to_add = [raw_tx] + [self.nodes[0].getrawtransaction(txid) for txid in more_tx]
405+
block = create_block(int(self.nodes[0].getbestblockhash(), 16), create_coinbase(self.nodes[0].getblockcount()+1), txlist=txs_to_add)
411406
add_witness_commitment(block)
412407
block.solve()
413408
assert_equal(self.nodes[0].submitblock(block.serialize().hex()), None)

test/functional/p2p_invalid_block.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,14 @@
1515
import copy
1616
import time
1717

18-
from test_framework.blocktools import create_block, create_coinbase, create_tx_with_script
18+
from test_framework.blocktools import (
19+
create_block,
20+
create_coinbase,
21+
create_tx_with_script,
22+
)
1923
from test_framework.messages import COIN
2024
from test_framework.p2p import P2PDataStore
25+
from test_framework.script import OP_TRUE
2126
from test_framework.test_framework import BitcoinTestFramework
2227
from test_framework.util import assert_equal
2328

@@ -66,15 +71,10 @@ def run_test(self):
6671
# For more information on merkle-root malleability see src/consensus/merkle.cpp.
6772
self.log.info("Test merkle root malleability.")
6873

69-
block2 = create_block(tip, create_coinbase(height), block_time)
74+
tx1 = create_tx_with_script(block1.vtx[0], 0, script_sig=bytes([OP_TRUE]), amount=50 * COIN)
75+
tx2 = create_tx_with_script(tx1, 0, script_sig=bytes([OP_TRUE]), amount=50 * COIN)
76+
block2 = create_block(tip, create_coinbase(height), block_time, txlist=[tx1, tx2])
7077
block_time += 1
71-
72-
# b'0x51' is OP_TRUE
73-
tx1 = create_tx_with_script(block1.vtx[0], 0, script_sig=b'\x51', amount=50 * COIN)
74-
tx2 = create_tx_with_script(tx1, 0, script_sig=b'\x51', amount=50 * COIN)
75-
76-
block2.vtx.extend([tx1, tx2])
77-
block2.hashMerkleRoot = block2.calc_merkle_root()
7878
block2.solve()
7979
orig_hash = block2.sha256
8080
block2_orig = copy.deepcopy(block2)
@@ -99,12 +99,8 @@ def run_test(self):
9999

100100
self.log.info("Test very broken block.")
101101

102-
block3 = create_block(tip, create_coinbase(height), block_time)
102+
block3 = create_block(tip, create_coinbase(height, nValue=100), block_time)
103103
block_time += 1
104-
block3.vtx[0].vout[0].nValue = 100 * COIN # Too high!
105-
block3.vtx[0].sha256 = None
106-
block3.vtx[0].calc_sha256()
107-
block3.hashMerkleRoot = block3.calc_merkle_root()
108104
block3.solve()
109105

110106
peer.send_blocks_and_test([block3], node, success=False, reject_reason='bad-cb-amount')
@@ -123,14 +119,10 @@ def run_test(self):
123119

124120
# Complete testing of CVE-2018-17144, by checking for the inflation bug.
125121
# Create a block that spends the output of a tx in a previous block.
126-
block4 = create_block(tip, create_coinbase(height), block_time)
127-
tx3 = create_tx_with_script(tx2, 0, script_sig=b'\x51', amount=50 * COIN)
128-
129-
# Duplicates input
130-
tx3.vin.append(tx3.vin[0])
122+
tx3 = create_tx_with_script(tx2, 0, script_sig=bytes([OP_TRUE]), amount=50 * COIN)
123+
tx3.vin.append(tx3.vin[0]) # Duplicates input
131124
tx3.rehash()
132-
block4.vtx.append(tx3)
133-
block4.hashMerkleRoot = block4.calc_merkle_root()
125+
block4 = create_block(tip, create_coinbase(height), block_time, txlist=[tx3])
134126
block4.solve()
135127
self.log.info("Test inflation by duplicating input")
136128
peer.send_blocks_and_test([block4], node, success=False, reject_reason='bad-txns-inputs-duplicate')
@@ -140,7 +132,6 @@ def run_test(self):
140132
node.setmocktime(t)
141133
# Set block time +1 second past max future validity
142134
block = create_block(tip, create_coinbase(height), t + MAX_FUTURE_BLOCK_TIME + 1)
143-
block.hashMerkleRoot = block.calc_merkle_root()
144135
block.solve()
145136
# Need force_send because the block will get rejected without a getdata otherwise
146137
peer.send_blocks_and_test([block], node, force_send=True, success=False, reject_reason='time-too-new')

0 commit comments

Comments
 (0)