Skip to content

Commit 754e802

Browse files
luke-jrwillcl-ark
andcommitted
test: check rejected future block later accepted
(Luke) was unsure if the code sufficiently avoided caching a time-too-new rejection, so wrote this test to check it. It looks like despite only exempting BLOCK_MUTATED, it is still okay because header failures never cache block invalidity. This test will help ensure that if this ever changes, BLOCK_TIME_FUTURE gets excluded at the same time. Co-authored-by: Will Clark <[email protected]>
1 parent 1186910 commit 754e802

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

test/functional/p2p_invalid_block.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,21 @@
99
2) Invalid block with duplicated transaction should be re-requested.
1010
3) Invalid block with bad coinbase value should be rejected and not
1111
re-requested.
12+
4) Invalid block due to future timestamp is later accepted when that timestamp
13+
becomes valid.
1214
"""
1315
import copy
16+
import time
1417

1518
from test_framework.blocktools import create_block, create_coinbase, create_tx_with_script
1619
from test_framework.messages import COIN
1720
from test_framework.p2p import P2PDataStore
1821
from test_framework.test_framework import BitcoinTestFramework
1922
from test_framework.util import assert_equal
2023

24+
MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60
25+
26+
2127
class InvalidBlockRequestTest(BitcoinTestFramework):
2228
def set_test_params(self):
2329
self.num_nodes = 1
@@ -133,5 +139,18 @@ def run_test(self):
133139
self.log.info("Test inflation by duplicating input")
134140
peer.send_blocks_and_test([block4], node, success=False, reject_reason='bad-txns-inputs-duplicate')
135141

142+
self.log.info("Test accepting identical block after rejecting it due to a future timestamp.")
143+
t = int(time.time())
144+
node.setmocktime(t)
145+
# Set block time +1 second past max future validity
146+
block = create_block(tip, create_coinbase(height), t + MAX_FUTURE_BLOCK_TIME + 1)
147+
block.hashMerkleRoot = block.calc_merkle_root()
148+
block.solve()
149+
# Need force_send because the block will get rejected without a getdata otherwise
150+
peer.send_blocks_and_test([block], node, force_send=True, success=False, reject_reason='time-too-new')
151+
node.setmocktime(t + 1)
152+
peer.send_blocks_and_test([block], node, success=True)
153+
154+
136155
if __name__ == '__main__':
137156
InvalidBlockRequestTest().main()

0 commit comments

Comments
 (0)