2626import subprocess
2727
2828from test_framework .blocktools import (
29+ MAX_FUTURE_BLOCK_TIME ,
30+ TIME_GENESIS_BLOCK ,
2931 create_block ,
3032 create_coinbase ,
31- TIME_GENESIS_BLOCK ,
3233)
3334from test_framework .messages import (
3435 CBlockHeader ,
5354HEIGHT = 200 # blocks mined
5455TIME_RANGE_STEP = 600 # ten-minute steps
5556TIME_RANGE_MTP = TIME_GENESIS_BLOCK + (HEIGHT - 6 ) * TIME_RANGE_STEP
57+ TIME_RANGE_TIP = TIME_GENESIS_BLOCK + (HEIGHT - 1 ) * TIME_RANGE_STEP
5658TIME_RANGE_END = TIME_GENESIS_BLOCK + HEIGHT * TIME_RANGE_STEP
5759
5860
@@ -65,6 +67,7 @@ def set_test_params(self):
6567 def run_test (self ):
6668 self .wallet = MiniWallet (self .nodes [0 ])
6769 self .mine_chain ()
70+ self ._test_max_future_block_time ()
6871 self .restart_node (0 , extra_args = ['-stopatheight=207' , '-prune=1' ]) # Set extra args with pruning after rescan is complete
6972
7073 self ._test_getblockchaininfo ()
@@ -85,6 +88,19 @@ def mine_chain(self):
8588 self .generate (self .wallet , 1 )
8689 assert_equal (self .nodes [0 ].getblockchaininfo ()['blocks' ], HEIGHT )
8790
91+ def _test_max_future_block_time (self ):
92+ self .stop_node (0 )
93+ self .log .info ("A block tip of more than MAX_FUTURE_BLOCK_TIME in the future raises an error" )
94+ self .nodes [0 ].assert_start_raises_init_error (
95+ extra_args = [f"-mocktime={ TIME_RANGE_TIP - MAX_FUTURE_BLOCK_TIME - 1 } " ],
96+ expected_msg = ": The block database contains a block which appears to be from the future."
97+ " This may be due to your computer's date and time being set incorrectly."
98+ f" Only rebuild the block database if you are sure that your computer's date and time are correct.{ os .linesep } "
99+ "Please restart with -reindex or -reindex-chainstate to recover." ,
100+ )
101+ self .log .info ("A block tip of MAX_FUTURE_BLOCK_TIME in the future is fine" )
102+ self .start_node (0 , extra_args = [f"-mocktime={ TIME_RANGE_TIP - MAX_FUTURE_BLOCK_TIME } " ])
103+
88104 def _test_getblockchaininfo (self ):
89105 self .log .info ("Test getblockchaininfo" )
90106
0 commit comments