diff options
-rw-r--r-- | src/init.cpp | 2 | ||||
-rwxr-xr-x | test/functional/p2p_invalid_block.py | 3 | ||||
-rwxr-xr-x | test/functional/rpc_blockchain.py | 18 | ||||
-rw-r--r-- | test/functional/test_framework/blocktools.py | 2 |
4 files changed, 21 insertions, 4 deletions
diff --git a/src/init.cpp b/src/init.cpp index d5c3acbaad..22c0d928b5 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1561,7 +1561,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) const CBlockIndex* tip = chainstate->m_chain.Tip(); RPCNotifyBlockChange(tip); - if (tip && tip->nTime > GetAdjustedTime() + 2 * 60 * 60) { + if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) { strLoadError = _("The block database contains a block which appears to be from the future. " "This may be due to your computer's date and time being set incorrectly. " "Only rebuild the block database if you are sure that your computer's date and time are correct"); diff --git a/test/functional/p2p_invalid_block.py b/test/functional/p2p_invalid_block.py index 0dfa25174b..710f3d93e1 100755 --- a/test/functional/p2p_invalid_block.py +++ b/test/functional/p2p_invalid_block.py @@ -16,6 +16,7 @@ import copy import time from test_framework.blocktools import ( + MAX_FUTURE_BLOCK_TIME, create_block, create_coinbase, create_tx_with_script, @@ -26,8 +27,6 @@ from test_framework.script import OP_TRUE from test_framework.test_framework import BitcoinTestFramework from test_framework.util import assert_equal -MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60 - class InvalidBlockRequestTest(BitcoinTestFramework): def set_test_params(self): diff --git a/test/functional/rpc_blockchain.py b/test/functional/rpc_blockchain.py index 4be9616345..8f7d5114fa 100755 --- a/test/functional/rpc_blockchain.py +++ b/test/functional/rpc_blockchain.py @@ -26,9 +26,10 @@ import os import subprocess from test_framework.blocktools import ( + MAX_FUTURE_BLOCK_TIME, + TIME_GENESIS_BLOCK, create_block, create_coinbase, - TIME_GENESIS_BLOCK, ) from test_framework.messages import ( CBlockHeader, @@ -53,6 +54,7 @@ from test_framework.wallet import MiniWallet HEIGHT = 200 # blocks mined TIME_RANGE_STEP = 600 # ten-minute steps TIME_RANGE_MTP = TIME_GENESIS_BLOCK + (HEIGHT - 6) * TIME_RANGE_STEP +TIME_RANGE_TIP = TIME_GENESIS_BLOCK + (HEIGHT - 1) * TIME_RANGE_STEP TIME_RANGE_END = TIME_GENESIS_BLOCK + HEIGHT * TIME_RANGE_STEP @@ -65,6 +67,7 @@ class BlockchainTest(BitcoinTestFramework): def run_test(self): self.wallet = MiniWallet(self.nodes[0]) self.mine_chain() + self._test_max_future_block_time() self.restart_node(0, extra_args=['-stopatheight=207', '-prune=1']) # Set extra args with pruning after rescan is complete self._test_getblockchaininfo() @@ -85,6 +88,19 @@ class BlockchainTest(BitcoinTestFramework): self.generate(self.wallet, 1) assert_equal(self.nodes[0].getblockchaininfo()['blocks'], HEIGHT) + def _test_max_future_block_time(self): + self.stop_node(0) + self.log.info("A block tip of more than MAX_FUTURE_BLOCK_TIME in the future raises an error") + self.nodes[0].assert_start_raises_init_error( + extra_args=[f"-mocktime={TIME_RANGE_TIP - MAX_FUTURE_BLOCK_TIME - 1}"], + expected_msg=": The block database contains a block which appears to be from the future." + " This may be due to your computer's date and time being set incorrectly." + f" Only rebuild the block database if you are sure that your computer's date and time are correct.{os.linesep}" + "Please restart with -reindex or -reindex-chainstate to recover.", + ) + self.log.info("A block tip of MAX_FUTURE_BLOCK_TIME in the future is fine") + self.start_node(0, extra_args=[f"-mocktime={TIME_RANGE_TIP - MAX_FUTURE_BLOCK_TIME}"]) + def _test_getblockchaininfo(self): self.log.info("Test getblockchaininfo") diff --git a/test/functional/test_framework/blocktools.py b/test/functional/test_framework/blocktools.py index eaa193e357..caa9b86969 100644 --- a/test/functional/test_framework/blocktools.py +++ b/test/functional/test_framework/blocktools.py @@ -50,6 +50,8 @@ MAX_BLOCK_SIGOPS_WEIGHT = MAX_BLOCK_SIGOPS * WITNESS_SCALE_FACTOR # Genesis block time (regtest) TIME_GENESIS_BLOCK = 1296688602 +MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60 + # Coinbase transaction outputs can only be spent after this number of new blocks (network rule) COINBASE_MATURITY = 100 |