Skip to content

Commit df5d783

Browse files
committed
test: refactor: take use of create_block txlist parameter
Passing a list of transactions `txlist` to `create_block` appends them to the block, hence we don't need to do that manually anymore. The merkle root calculation can also be removed, since this is done in the end of the helper.
1 parent ae9df4e commit df5d783

11 files changed

+19
-56
lines changed

test/functional/feature_assumevalid.py

+1-3
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

test/functional/feature_bip68_sequence.py

+1-3
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

+2-3
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

+1-3
Original file line numberDiff line numberDiff line change
@@ -120,9 +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, version=3)
124-
block.vtx.extend(invalid_cltv_txs)
125-
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)
126124
block.solve()
127125

128126
self.test_cltv_info(is_active=False) # Not active as of current tip and next block does not need to obey rules

test/functional/feature_csv_activation.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -173,9 +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.vtx.extend(txs)
178-
block.hashMerkleRoot = block.calc_merkle_root()
176+
block = create_block(self.tip, create_coinbase(self.tipheight + 1), self.last_block_time + 600, txlist=txs)
179177
block.solve()
180178
return block
181179

test/functional/feature_dersig.py

+1-3
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)

test/functional/feature_taproot.py

+2-5
Original file line numberDiff line numberDiff line change
@@ -1266,11 +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-
for tx in txs:
1271-
tx.rehash()
1272-
block.vtx.append(tx)
1273-
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)
12741271
witness and add_witness_commitment(block)
12751272
block.solve()
12761273
block_response = node.submitblock(block.serialize().hex())

test/functional/interface_zmq.py

+2-7
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

+5-18
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,11 @@ def run_test(self):
6666
# For more information on merkle-root malleability see src/consensus/merkle.cpp.
6767
self.log.info("Test merkle root malleability.")
6868

69-
block2 = create_block(tip, create_coinbase(height), block_time)
70-
block_time += 1
71-
7269
# b'0x51' is OP_TRUE
7370
tx1 = create_tx_with_script(block1.vtx[0], 0, script_sig=b'\x51', amount=50 * COIN)
7471
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()
72+
block2 = create_block(tip, create_coinbase(height), block_time, txlist=[tx1, tx2])
73+
block_time += 1
7874
block2.solve()
7975
orig_hash = block2.sha256
8076
block2_orig = copy.deepcopy(block2)
@@ -99,12 +95,8 @@ def run_test(self):
9995

10096
self.log.info("Test very broken block.")
10197

102-
block3 = create_block(tip, create_coinbase(height), block_time)
98+
block3 = create_block(tip, create_coinbase(height, nValue=100), block_time)
10399
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()
108100
block3.solve()
109101

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

124116
# Complete testing of CVE-2018-17144, by checking for the inflation bug.
125117
# Create a block that spends the output of a tx in a previous block.
126-
block4 = create_block(tip, create_coinbase(height), block_time)
127118
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])
119+
tx3.vin.append(tx3.vin[0]) # Duplicates input
131120
tx3.rehash()
132-
block4.vtx.append(tx3)
133-
block4.hashMerkleRoot = block4.calc_merkle_root()
121+
block4 = create_block(tip, create_coinbase(height), block_time, txlist=[tx3])
134122
block4.solve()
135123
self.log.info("Test inflation by duplicating input")
136124
peer.send_blocks_and_test([block4], node, success=False, reject_reason='bad-txns-inputs-duplicate')
@@ -140,7 +128,6 @@ def run_test(self):
140128
node.setmocktime(t)
141129
# Set block time +1 second past max future validity
142130
block = create_block(tip, create_coinbase(height), t + MAX_FUTURE_BLOCK_TIME + 1)
143-
block.hashMerkleRoot = block.calc_merkle_root()
144131
block.solve()
145132
# Need force_send because the block will get rejected without a getdata otherwise
146133
peer.send_blocks_and_test([block], node, force_send=True, success=False, reject_reason='time-too-new')

test/functional/p2p_unrequested_blocks.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,9 @@ def run_test(self):
225225
block_289f.solve()
226226
block_290f = create_block(block_289f.sha256, create_coinbase(290), block_289f.nTime+1)
227227
block_290f.solve()
228-
block_291 = create_block(block_290f.sha256, create_coinbase(291), block_290f.nTime+1)
229228
# block_291 spends a coinbase below maturity!
230-
block_291.vtx.append(create_tx_with_script(block_290f.vtx[0], 0, script_sig=b"42", amount=1))
231-
block_291.hashMerkleRoot = block_291.calc_merkle_root()
229+
tx_to_add = create_tx_with_script(block_290f.vtx[0], 0, script_sig=b"42", amount=1)
230+
block_291 = create_block(block_290f.sha256, create_coinbase(291), block_290f.nTime+1, txlist=[tx_to_add])
232231
block_291.solve()
233232
block_292 = create_block(block_291.sha256, create_coinbase(292), block_291.nTime+1)
234233
block_292.solve()

test/functional/wallet_bumpfee.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
)
2525
from test_framework.messages import (
2626
BIP125_SEQUENCE_NUMBER,
27-
tx_from_hex,
2827
)
2928
from test_framework.test_framework import BitcoinTestFramework
3029
from test_framework.util import (
@@ -588,13 +587,10 @@ def spend_one_input(node, dest_address, change_size=Decimal("0.00049000")):
588587

589588

590589
def submit_block_with_tx(node, tx):
591-
ctx = tx_from_hex(tx)
592590
tip = node.getbestblockhash()
593591
height = node.getblockcount() + 1
594592
block_time = node.getblockheader(tip)["mediantime"] + 1
595-
block = create_block(int(tip, 16), create_coinbase(height), block_time)
596-
block.vtx.append(ctx)
597-
block.hashMerkleRoot = block.calc_merkle_root()
593+
block = create_block(int(tip, 16), create_coinbase(height), block_time, txlist=[tx])
598594
add_witness_commitment(block)
599595
block.solve()
600596
node.submitblock(block.serialize().hex())

0 commit comments

Comments
 (0)