Merge bitcoin/bitcoin#23636: Remove GetAdjustedTime from init.cpp

fa551b3bdd Remove GetAdjustedTime from init.cpp (MarcoFalke)
fa815f8473 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 fa551b3bdd, noticed the exact same thing here: e073634c37
  mzumsande:
    Code Review ACK fa551b3bdd
  jnewbery:
    Code review ACK fa551b3bdd
  shaavan:
    ACK fa551b3bdd
  theStack:
    Code-review ACK fa551b3bdd

Tree-SHA512: 15807a0e943e3e8d8c5250c8f6d7b56afb26002b1e290bf93636a2c747f27e78f01f1de04ce1a83d6339e27284c69c43e077a8467545c4078746f4c1ecb1164d
This commit is contained in:
MarcoFalke 2021-12-02 15:24:47 +01:00
commit 26a1147ce5
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
8 changed files with 27 additions and 7 deletions

View file

@ -1561,7 +1561,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)
const CBlockIndex* tip = chainstate->m_chain.Tip(); const CBlockIndex* tip = chainstate->m_chain.Tip();
RPCNotifyBlockChange(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. " 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. " "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"); "Only rebuild the block database if you are sure that your computer's date and time are correct");

View file

@ -10,6 +10,7 @@
#include <net.h> #include <net.h>
#include <addrdb.h> #include <addrdb.h>
#include <addrman.h>
#include <banman.h> #include <banman.h>
#include <clientversion.h> #include <clientversion.h>
#include <compat.h> #include <compat.h>

View file

@ -6,7 +6,6 @@
#ifndef BITCOIN_NET_H #ifndef BITCOIN_NET_H
#define BITCOIN_NET_H #define BITCOIN_NET_H
#include <addrman.h>
#include <chainparams.h> #include <chainparams.h>
#include <common/bloom.h> #include <common/bloom.h>
#include <compat.h> #include <compat.h>
@ -37,9 +36,10 @@
#include <thread> #include <thread>
#include <vector> #include <vector>
class CScheduler; class AddrMan;
class CNode;
class BanMan; class BanMan;
class CNode;
class CScheduler;
struct bilingual_str; struct bilingual_str;
/** Default for -whitelistrelay. */ /** Default for -whitelistrelay. */

View file

@ -4,6 +4,7 @@
#include <rpc/server.h> #include <rpc/server.h>
#include <addrman.h>
#include <banman.h> #include <banman.h>
#include <chainparams.h> #include <chainparams.h>
#include <clientversion.h> #include <clientversion.h>

View file

@ -2,6 +2,7 @@
// Distributed under the MIT software license, see the accompanying // Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <addrman.h>
#include <chainparams.h> #include <chainparams.h>
#include <chainparamsbase.h> #include <chainparamsbase.h>
#include <net.h> #include <net.h>

View file

@ -16,6 +16,7 @@ import copy
import time import time
from test_framework.blocktools import ( from test_framework.blocktools import (
MAX_FUTURE_BLOCK_TIME,
create_block, create_block,
create_coinbase, create_coinbase,
create_tx_with_script, 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.test_framework import BitcoinTestFramework
from test_framework.util import assert_equal from test_framework.util import assert_equal
MAX_FUTURE_BLOCK_TIME = 2 * 60 * 60
class InvalidBlockRequestTest(BitcoinTestFramework): class InvalidBlockRequestTest(BitcoinTestFramework):
def set_test_params(self): def set_test_params(self):

View file

@ -26,9 +26,10 @@ import os
import subprocess import subprocess
from test_framework.blocktools import ( from test_framework.blocktools import (
MAX_FUTURE_BLOCK_TIME,
TIME_GENESIS_BLOCK,
create_block, create_block,
create_coinbase, create_coinbase,
TIME_GENESIS_BLOCK,
) )
from test_framework.messages import ( from test_framework.messages import (
CBlockHeader, CBlockHeader,
@ -53,6 +54,7 @@ from test_framework.wallet import MiniWallet
HEIGHT = 200 # blocks mined HEIGHT = 200 # blocks mined
TIME_RANGE_STEP = 600 # ten-minute steps TIME_RANGE_STEP = 600 # ten-minute steps
TIME_RANGE_MTP = TIME_GENESIS_BLOCK + (HEIGHT - 6) * TIME_RANGE_STEP 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 TIME_RANGE_END = TIME_GENESIS_BLOCK + HEIGHT * TIME_RANGE_STEP
@ -65,6 +67,7 @@ class BlockchainTest(BitcoinTestFramework):
def run_test(self): def run_test(self):
self.wallet = MiniWallet(self.nodes[0]) self.wallet = MiniWallet(self.nodes[0])
self.mine_chain() 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.restart_node(0, extra_args=['-stopatheight=207', '-prune=1']) # Set extra args with pruning after rescan is complete
self._test_getblockchaininfo() self._test_getblockchaininfo()
@ -85,6 +88,19 @@ class BlockchainTest(BitcoinTestFramework):
self.generate(self.wallet, 1) self.generate(self.wallet, 1)
assert_equal(self.nodes[0].getblockchaininfo()['blocks'], HEIGHT) 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): def _test_getblockchaininfo(self):
self.log.info("Test getblockchaininfo") self.log.info("Test getblockchaininfo")

View file

@ -50,6 +50,8 @@ MAX_BLOCK_SIGOPS_WEIGHT = MAX_BLOCK_SIGOPS * WITNESS_SCALE_FACTOR
# Genesis block time (regtest) # Genesis block time (regtest)
TIME_GENESIS_BLOCK = 1296688602 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 transaction outputs can only be spent after this number of new blocks (network rule)
COINBASE_MATURITY = 100 COINBASE_MATURITY = 100