diff --git a/src/governance/governance.cpp b/src/governance/governance.cpp index 00a130e7d1..2553181eb0 100644 --- a/src/governance/governance.cpp +++ b/src/governance/governance.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include diff --git a/src/governance/vote.cpp b/src/governance/vote.cpp index 2c1b9f1a04..0e4647566a 100644 --- a/src/governance/vote.cpp +++ b/src/governance/vote.cpp @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include diff --git a/src/init.cpp b/src/init.cpp index 3fd49789df..9f38364005 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -2056,7 +2056,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/src/net.cpp b/src/net.cpp index 579ab7b923..474fbec3e7 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -12,6 +12,7 @@ #include #include +#include #include #include #include diff --git a/src/net.h b/src/net.h index de74b27c54..248a8a6b04 100644 --- a/src/net.h +++ b/src/net.h @@ -6,7 +6,6 @@ #ifndef BITCOIN_NET_H #define BITCOIN_NET_H -#include #include #include #include @@ -50,14 +49,15 @@ #include #include +class AddrMan; +class BanMan; class CConnman; class CDeterministicMNList; class CDeterministicMNManager; class CMasternodeMetaMan; class CMasternodeSync; -class CScheduler; class CNode; -class BanMan; +class CScheduler; struct bilingual_str; /** Default for -whitelistrelay. */ diff --git a/src/rpc/governance.cpp b/src/rpc/governance.cpp index bdee7e7740..68290a6aa5 100644 --- a/src/rpc/governance.cpp +++ b/src/rpc/governance.cpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include #include @@ -19,7 +20,7 @@ #include #include #include -#include +#include #include #include #include diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index 7d5418c829..46fd76fbd5 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -5,6 +5,7 @@ #include +#include #include #include #include diff --git a/src/test/fuzz/connman.cpp b/src/test/fuzz/connman.cpp index ead124552b..75afc58bc4 100644 --- a/src/test/fuzz/connman.cpp +++ b/src/test/fuzz/connman.cpp @@ -2,6 +2,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include #include #include diff --git a/src/test/fuzz/net.cpp b/src/test/fuzz/net.cpp index 9455a05ce3..b3c562ee47 100644 --- a/src/test/fuzz/net.cpp +++ b/src/test/fuzz/net.cpp @@ -2,6 +2,7 @@ // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. +#include #include #include #include diff --git a/test/functional/p2p_invalid_block.py b/test/functional/p2p_invalid_block.py index dafef7412d..65e7aa3adf 100755 --- a/test/functional/p2p_invalid_block.py +++ b/test/functional/p2p_invalid_block.py @@ -15,14 +15,12 @@ becomes valid. import copy import time -from test_framework.blocktools import create_block, create_coinbase, create_tx_with_script +from test_framework.blocktools import MAX_FUTURE_BLOCK_TIME, create_block, create_coinbase, create_tx_with_script from test_framework.messages import COIN from test_framework.p2p import P2PDataStore 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 a36e881c68..8c5c51c511 100755 --- a/test/functional/rpc_blockchain.py +++ b/test/functional/rpc_blockchain.py @@ -27,9 +27,10 @@ import subprocess from test_framework.address import ADDRESS_BCRT1_P2SH_OP_TRUE from test_framework.blocktools import ( + MAX_FUTURE_BLOCK_TIME, + TIME_GENESIS_BLOCK, create_block, create_coinbase, - TIME_GENESIS_BLOCK, ) from test_framework.governance import EXPECTED_STDERR_NO_GOV_PRUNE from test_framework.messages import ( @@ -48,25 +49,27 @@ from test_framework.util import ( assert_is_hex_string, assert_is_hash_string, get_datadir_path, - set_node_times, ) from test_framework.wallet import MiniWallet HEIGHT = 200 # blocks mined -TIME_RANGE_STEP = 156 # ten-minute steps +TIME_RANGE_STEP = 156 # two and a half 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 class BlockchainTest(BitcoinTestFramework): def set_test_params(self): + self.disable_mocktime = True self.setup_clean_chain = True self.num_nodes = 1 self.supports_cli = False def run_test(self): self.mine_chain() + self._test_max_future_block_time() self.restart_node(0, extra_args=['-stopatheight=207', '-prune=1', '-txindex=0']) # Set extra args with pruning after rescan is complete # Actual tests @@ -85,10 +88,23 @@ class BlockchainTest(BitcoinTestFramework): self.log.info(f"Generate {HEIGHT} blocks after the genesis block in 156 sec") for t in range(TIME_GENESIS_BLOCK, TIME_RANGE_END, TIME_RANGE_STEP): # 156 sec steps from genesis block time - set_node_times(self.nodes, t) + self.nodes[0].setmocktime(t) self.generatetoaddress(self.nodes[0], 1, ADDRESS_BCRT1_P2SH_OP_TRUE) assert_equal(self.nodes[0].getblockchaininfo()['blocks'], 200) + 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 96ebd8cd9d..d8bbaa66ee 100644 --- a/test/functional/test_framework/blocktools.py +++ b/test/functional/test_framework/blocktools.py @@ -31,6 +31,8 @@ MAX_BLOCK_SIGOPS = 40000 # Genesis block time (regtest) TIME_GENESIS_BLOCK = 1417713337 +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