Skip to content

Commit ffcb437

Browse files
committed
Merge bitcoin/bitcoin#23498: test: remove unnecessary block rehashing prior to solving
a9872e1 test: remove unnecessary block rehashing prior to solving (Sebastian Falbesoner) Pull request description: Solving a block involves continously rehashing it, https://github.com/bitcoin/bitcoin/blob/c9dd5c8d6e59e27af98e99d2844d6ead8eec3162/test/functional/test_framework/messages.py#L759-L764 i.e. any extra `rehash` calls before are not necessary and can be dropped. The instances were identified by searching for all block solving calls via `git grep "solve("` in `./test/functional/`. From 95 instances of `CBlock.solve()` calls, 20 contained an unnecessary rehashing instruction before that are removed in this PR. ACKs for top commit: brunoerg: tACK a9872e1 rajarshimaitra: tACK bitcoin/bitcoin@a9872e1 Tree-SHA512: 160092be717d0d250778b8ab091ebd77cc6865d2754ef150cf3b4d4ac7304d4bf3d2ebb61ec6b04a55040c8895b9fb4d28653ea4b099d56d90776c9111cf173f
2 parents 5cc8d2e + a9872e1 commit ffcb437

11 files changed

+0
-20
lines changed

test/functional/feature_assumevalid.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,6 @@ def run_test(self):
126126
self.block_time += 1
127127
block102.vtx.extend([tx])
128128
block102.hashMerkleRoot = block102.calc_merkle_root()
129-
block102.rehash()
130129
block102.solve()
131130
self.blocks.append(block102)
132131
self.tip = block102.sha256

test/functional/feature_bip68_sequence.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,6 @@ def test_nonzero_locks(orig_tx, node, relayfee, use_height_lock):
335335
# tx3 to be removed.
336336
for i in range(2):
337337
block = create_block(tmpl=tmpl, ntime=cur_time)
338-
block.rehash()
339338
block.solve()
340339
tip = block.sha256
341340
assert_equal(None if i == 1 else 'inconclusive', self.nodes[0].submitblock(block.serialize().hex()))
@@ -392,7 +391,6 @@ def test_bip68_not_consensus(self):
392391
block = create_block(tmpl=self.nodes[0].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS))
393392
block.vtx.extend([tx1, tx2, tx3])
394393
block.hashMerkleRoot = block.calc_merkle_root()
395-
block.rehash()
396394
add_witness_commitment(block)
397395
block.solve()
398396

test/functional/feature_block.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,6 @@ def run_test(self):
612612
b45.nBits = 0x207fffff
613613
b45.vtx.append(non_coinbase)
614614
b45.hashMerkleRoot = b45.calc_merkle_root()
615-
b45.calc_sha256()
616615
b45.solve()
617616
self.block_heights[b45.sha256] = self.block_heights[self.tip.sha256] + 1
618617
self.tip = b45

test/functional/feature_csv_activation.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ def create_test_block(self, txs):
177177
block.nVersion = 4
178178
block.vtx.extend(txs)
179179
block.hashMerkleRoot = block.calc_merkle_root()
180-
block.rehash()
181180
block.solve()
182181
return block
183182

test/functional/feature_dersig.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ def run_test(self):
8888
block = create_block(int(tip, 16), create_coinbase(DERSIG_HEIGHT - 1), block_time)
8989
block.vtx.append(spendtx)
9090
block.hashMerkleRoot = block.calc_merkle_root()
91-
block.rehash()
9291
block.solve()
9392

9493
assert_equal(self.nodes[0].getblockcount(), DERSIG_HEIGHT - 2)
@@ -103,7 +102,6 @@ def run_test(self):
103102
block_time += 1
104103
block = create_block(tip, create_coinbase(DERSIG_HEIGHT), block_time)
105104
block.nVersion = 2
106-
block.rehash()
107105
block.solve()
108106

109107
with self.nodes[0].assert_debug_log(expected_msgs=[f'{block.hash}, bad-version(0x00000002)']):
@@ -133,7 +131,6 @@ def run_test(self):
133131
# Now we verify that a block with this transaction is also invalid.
134132
block.vtx.append(spendtx)
135133
block.hashMerkleRoot = block.calc_merkle_root()
136-
block.rehash()
137134
block.solve()
138135

139136
with self.nodes[0].assert_debug_log(expected_msgs=[f'CheckInputScripts on {block.vtx[-1].hash} failed with non-mandatory-script-verify-flag (Non-canonical DER signature)']):
@@ -144,7 +141,6 @@ def run_test(self):
144141
self.log.info("Test that a block with a DERSIG-compliant transaction is accepted")
145142
block.vtx[1] = self.create_tx(self.coinbase_txids[1])
146143
block.hashMerkleRoot = block.calc_merkle_root()
147-
block.rehash()
148144
block.solve()
149145

150146
self.test_dersig_info(is_active=True) # Not active as of current tip, but next block must obey rules

test/functional/feature_nulldummy.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ def block_submit(self, node, txs, *, with_witness=False, accept):
130130
block.hashMerkleRoot = block.calc_merkle_root()
131131
if with_witness:
132132
add_witness_commitment(block)
133-
block.rehash()
134133
block.solve()
135134
assert_equal(None if accept else NULLDUMMY_ERROR, node.submitblock(block.serialize().hex()))
136135
if accept:

test/functional/feature_taproot.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1240,7 +1240,6 @@ def block_submit(self, node, txs, msg, err_msg, cb_pubkey=None, fees=0, sigops_w
12401240
block.vtx.append(tx)
12411241
block.hashMerkleRoot = block.calc_merkle_root()
12421242
witness and add_witness_commitment(block)
1243-
block.rehash()
12441243
block.solve()
12451244
block_response = node.submitblock(block.serialize().hex())
12461245
if err_msg is not None:
@@ -1488,7 +1487,6 @@ def run_test(self):
14881487
# Mine a block with the transaction
14891488
block = create_block(tmpl=self.nodes[1].getblocktemplate(NORMAL_GBT_REQUEST_PARAMS), txlist=[rawtx])
14901489
add_witness_commitment(block)
1491-
block.rehash()
14921490
block.solve()
14931491
assert_equal(None, self.nodes[1].submitblock(block.serialize().hex()))
14941492
self.sync_blocks()

test/functional/p2p_invalid_block.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ def run_test(self):
7575

7676
block2.vtx.extend([tx1, tx2])
7777
block2.hashMerkleRoot = block2.calc_merkle_root()
78-
block2.rehash()
7978
block2.solve()
8079
orig_hash = block2.sha256
8180
block2_orig = copy.deepcopy(block2)
@@ -95,7 +94,6 @@ def run_test(self):
9594
block2_dup.vtx[2].vin.append(block2_dup.vtx[2].vin[0])
9695
block2_dup.vtx[2].rehash()
9796
block2_dup.hashMerkleRoot = block2_dup.calc_merkle_root()
98-
block2_dup.rehash()
9997
block2_dup.solve()
10098
peer.send_blocks_and_test([block2_dup], node, success=False, reject_reason='bad-txns-inputs-duplicate')
10199

@@ -107,7 +105,6 @@ def run_test(self):
107105
block3.vtx[0].sha256 = None
108106
block3.vtx[0].calc_sha256()
109107
block3.hashMerkleRoot = block3.calc_merkle_root()
110-
block3.rehash()
111108
block3.solve()
112109

113110
peer.send_blocks_and_test([block3], node, success=False, reject_reason='bad-cb-amount')
@@ -134,7 +131,6 @@ def run_test(self):
134131
tx3.rehash()
135132
block4.vtx.append(tx3)
136133
block4.hashMerkleRoot = block4.calc_merkle_root()
137-
block4.rehash()
138134
block4.solve()
139135
self.log.info("Test inflation by duplicating input")
140136
peer.send_blocks_and_test([block4], node, success=False, reject_reason='bad-txns-inputs-duplicate')

test/functional/p2p_segwit.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,6 @@ def test_witness_commitments(self):
790790
block_3.vtx[0].vout.append(CTxOut(0, CScript([OP_RETURN, WITNESS_COMMITMENT_HEADER + ser_uint256(2), 10])))
791791
block_3.vtx[0].rehash()
792792
block_3.hashMerkleRoot = block_3.calc_merkle_root()
793-
block_3.rehash()
794793
block_3.solve()
795794

796795
test_witness_block(self.nodes[0], self.test_node, block_3, accepted=False, reason='bad-witness-merkle-match')
@@ -804,7 +803,6 @@ def test_witness_commitments(self):
804803
block_3.vtx[0].vout[-1].nValue += 1
805804
block_3.vtx[0].rehash()
806805
block_3.hashMerkleRoot = block_3.calc_merkle_root()
807-
block_3.rehash()
808806
assert len(block_3.vtx[0].vout) == 4 # 3 OP_returns
809807
block_3.solve()
810808
test_witness_block(self.nodes[0], self.test_node, block_3, accepted=True)

test/functional/wallet_bumpfee.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,6 @@ def submit_block_with_tx(node, tx):
598598
block_time = node.getblockheader(tip)["mediantime"] + 1
599599
block = create_block(int(tip, 16), create_coinbase(height), block_time)
600600
block.vtx.append(ctx)
601-
block.rehash()
602601
block.hashMerkleRoot = block.calc_merkle_root()
603602
add_witness_commitment(block)
604603
block.solve()

0 commit comments

Comments
 (0)