Skip to content

Commit fa551b3

Browse files
author
MarcoFalke
committed
Remove GetAdjustedTime from init.cpp
1 parent fa815f8 commit fa551b3

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

src/init.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1561,7 +1561,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
15611561

15621562
const CBlockIndex* tip = chainstate->m_chain.Tip();
15631563
RPCNotifyBlockChange(tip);
1564-
if (tip && tip->nTime > GetAdjustedTime() + 2 * 60 * 60) {
1564+
if (tip && tip->nTime > GetTime() + MAX_FUTURE_BLOCK_TIME) {
15651565
strLoadError = _("The block database contains a block which appears to be from the future. "
15661566
"This may be due to your computer's date and time being set incorrectly. "
15671567
"Only rebuild the block database if you are sure that your computer's date and time are correct");

test/functional/p2p_invalid_block.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import time
1717

1818
from test_framework.blocktools import (
19+
MAX_FUTURE_BLOCK_TIME,
1920
create_block,
2021
create_coinbase,
2122
create_tx_with_script,
@@ -26,8 +27,6 @@
2627
from test_framework.test_framework import BitcoinTestFramework
2728
from test_framework.util import assert_equal
2829

29-
MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60
30-
3130

3231
class InvalidBlockRequestTest(BitcoinTestFramework):
3332
def set_test_params(self):

test/functional/rpc_blockchain.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
import subprocess
2727

2828
from test_framework.blocktools import (
29+
MAX_FUTURE_BLOCK_TIME,
30+
TIME_GENESIS_BLOCK,
2931
create_block,
3032
create_coinbase,
31-
TIME_GENESIS_BLOCK,
3233
)
3334
from test_framework.messages import (
3435
CBlockHeader,
@@ -53,6 +54,7 @@
5354
HEIGHT = 200 # blocks mined
5455
TIME_RANGE_STEP = 600 # ten-minute steps
5556
TIME_RANGE_MTP = TIME_GENESIS_BLOCK + (HEIGHT - 6) * TIME_RANGE_STEP
57+
TIME_RANGE_TIP = TIME_GENESIS_BLOCK + (HEIGHT - 1) * TIME_RANGE_STEP
5658
TIME_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

test/functional/test_framework/blocktools.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
# Genesis block time (regtest)
5151
TIME_GENESIS_BLOCK = 1296688602
5252

53+
MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60
54+
5355
# Coinbase transaction outputs can only be spent after this number of new blocks (network rule)
5456
COINBASE_MATURITY = 100
5557

0 commit comments

Comments
 (0)