Skip to content

Commit 26a1147

Browse files
author
MarcoFalke
committed
Merge bitcoin/bitcoin#23636: Remove GetAdjustedTime from init.cpp
fa551b3 Remove GetAdjustedTime from init.cpp (MarcoFalke) fa815f8 Replace addrman.h include with forward decl in net.h (MarcoFalke) Pull request description: It seems confusing to call `GetAdjustedTime` there, because no offset could have been retrieved from the network at this point. Even if connman was started, `timedata` needs at least 5 peer connections to calculate an offset. Fix the confusion by replacing `GetAdjustedTime` with `GetTime`, which does not change behavior. Also: * Replace magic number with `MAX_FUTURE_BLOCK_TIME` to clarify the context * Add test, which passes both on current master and this pull request * An unrelated refactoring commit, happy to drop ACKs for top commit: dongcarl: Code Review ACK fa551b3, noticed the exact same thing here: bitcoin/bitcoin@e073634 mzumsande: Code Review ACK fa551b3 jnewbery: Code review ACK fa551b3 shaavan: ACK fa551b3 theStack: Code-review ACK fa551b3 Tree-SHA512: 15807a0e943e3e8d8c5250c8f6d7b56afb26002b1e290bf93636a2c747f27e78f01f1de04ce1a83d6339e27284c69c43e077a8467545c4078746f4c1ecb1164d
2 parents 0bd7ca9 + fa551b3 commit 26a1147

File tree

8 files changed

+27
-7
lines changed

8 files changed

+27
-7
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");

src/net.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <net.h>
1111

1212
#include <addrdb.h>
13+
#include <addrman.h>
1314
#include <banman.h>
1415
#include <clientversion.h>
1516
#include <compat.h>

src/net.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#ifndef BITCOIN_NET_H
77
#define BITCOIN_NET_H
88

9-
#include <addrman.h>
109
#include <chainparams.h>
1110
#include <common/bloom.h>
1211
#include <compat.h>
@@ -37,9 +36,10 @@
3736
#include <thread>
3837
#include <vector>
3938

40-
class CScheduler;
41-
class CNode;
39+
class AddrMan;
4240
class BanMan;
41+
class CNode;
42+
class CScheduler;
4343
struct bilingual_str;
4444

4545
/** Default for -whitelistrelay. */

src/rpc/net.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <rpc/server.h>
66

7+
#include <addrman.h>
78
#include <banman.h>
89
#include <chainparams.h>
910
#include <clientversion.h>

src/test/fuzz/connman.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
#include <addrman.h>
56
#include <chainparams.h>
67
#include <chainparamsbase.h>
78
#include <net.h>

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)