Skip to content

Commit 7fbc0f3

Browse files
MarcoFalkePastaPastaPasta
authored andcommitted
Merge bitcoin#14604: tests: Add test and refactor feature_block.py
5531119 Added new test for future blocks reacceptance (sanket1729) 511a5af Fixed inconsistencies between code and comments (sanket1729) Pull request description: This Commit does 3 things: 1) Adds a test case for checking reacceptance a previously rejected block which was too far in the future. ~~2) clean up uses of rehash or calc_sha256 where it was not needed~~ 3) While constructing block 44, this commit makes the code consistent with the expected figure in the comment just above it by adding a transaction to the block. 4) Fix comment describing `sign_tx()` function ACKs for top commit: duncandean: reACK 5531119 brunoerg: reACK 5531119 Tree-SHA512: d40c72fcdbb0b2a0715adc58441eeea08147ee2ec5e371a4ccc824ebfdc6450698bd40aaeecb7ea7bfdb3cd1b264dd821b890276fff8b8d89b7225cdd9d6b546
1 parent d667e57 commit 7fbc0f3

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

test/functional/feature_block.py

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""Test block processing."""
66
import copy
77
import struct
8+
import time
89

910
from test_framework.blocktools import (
1011
create_block,
@@ -600,6 +601,8 @@ def run_test(self):
600601
b44.hashPrevBlock = self.tip.sha256
601602
b44.nBits = 0x207fffff
602603
b44.vtx.append(coinbase)
604+
tx = self.create_and_sign_transaction(out[14], 1)
605+
b44.vtx.append(tx)
603606
b44.hashMerkleRoot = b44.calc_merkle_root()
604607
b44.solve()
605608
self.tip = b44
@@ -687,7 +690,7 @@ def run_test(self):
687690
# Test block timestamps
688691
# -> b31 (8) -> b33 (9) -> b35 (10) -> b39 (11) -> b42 (12) -> b43 (13) -> b53 (14) -> b55 (15)
689692
# \-> b54 (15)
690-
#
693+
# -> b44 (14)\-> b48 ()
691694
self.move_tip(43)
692695
b53 = self.next_block(53, spend=out[14])
693696
self.send_blocks([b53], False)
@@ -707,6 +710,21 @@ def run_test(self):
707710
self.send_blocks([b55], True)
708711
self.save_spendable_output()
709712

713+
# The block which was previously rejected because of being "too far(3 hours)" must be accepted 2 hours later.
714+
# The new block is only 1 hour into future now and we must reorg onto to the new longer chain.
715+
# The new bestblock b48p is invalidated manually.
716+
# -> b31 (8) -> b33 (9) -> b35 (10) -> b39 (11) -> b42 (12) -> b43 (13) -> b53 (14) -> b55 (15)
717+
# \-> b54 (15)
718+
# -> b44 (14)\-> b48 () -> b48p ()
719+
self.log.info("Accept a previously rejected future block at a later time")
720+
node.setmocktime(int(time.time()) + 2*60*60)
721+
self.move_tip(48)
722+
self.block_heights[b48.sha256] = self.block_heights[b44.sha256] + 1 # b48 is a parent of b44
723+
b48p = self.next_block("48p")
724+
self.send_blocks([b48, b48p], success=True) # Reorg to the longer chain
725+
node.invalidateblock(b48p.hash) # mark b48p as invalid
726+
node.setmocktime(0)
727+
710728
# Test Merkle tree malleability
711729
#
712730
# -> b42 (12) -> b43 (13) -> b53 (14) -> b55 (15) -> b57p2 (16)
@@ -1317,7 +1335,7 @@ def create_tx(self, spend_tx, n, value, script=CScript([OP_TRUE, OP_DROP] * 15 +
13171335
return create_tx_with_script(spend_tx, n, amount=value, script_pub_key=script)
13181336

13191337
# sign a transaction, using the key we know about
1320-
# this signs input 0 in tx, which is assumed to be spending output n in spend_tx
1338+
# this signs input 0 in tx, which is assumed to be spending output 0 in spend_tx
13211339
def sign_tx(self, tx, spend_tx):
13221340
scriptPubKey = bytearray(spend_tx.vout[0].scriptPubKey)
13231341
if (scriptPubKey[0] == OP_TRUE): # an anyone-can-spend

0 commit comments

Comments
 (0)