/ test / functional / feature_maxtipage.py
feature_maxtipage.py
 1  #!/usr/bin/env python3
 2  # Copyright (c) 2022-present The Bitcoin Core developers
 3  # Distributed under the MIT software license, see the accompanying
 4  # file COPYING or http://www.opensource.org/licenses/mit-license.php.
 5  """Test logic for setting -maxtipage on command line.
 6  
 7  Nodes don't consider themselves out of "initial block download" as long as
 8  their best known block header time is more than -maxtipage in the past.
 9  """
10  
11  import time
12  
13  from test_framework.test_framework import BitcoinTestFramework
14  from test_framework.util import assert_equal
15  
16  
17  DEFAULT_MAX_TIP_AGE = 24 * 60 * 60
18  
19  
20  class MaxTipAgeTest(BitcoinTestFramework):
21      def set_test_params(self):
22          self.setup_clean_chain = True
23          self.num_nodes = 2
24  
25      def test_maxtipage(self, maxtipage, set_parameter=True, test_deltas=True):
26          node_miner = self.nodes[0]
27          node_ibd = self.nodes[1]
28  
29          self.restart_node(1, [f'-maxtipage={maxtipage}'] if set_parameter else None)
30          self.connect_nodes(0, 1)
31          cur_time = int(time.time())
32  
33          if test_deltas:
34              # tips older than maximum age -> stay in IBD
35              node_ibd.setmocktime(cur_time)
36              for delta in [5, 4, 3, 2, 1]:
37                  node_miner.setmocktime(cur_time - maxtipage - delta)
38                  self.generate(node_miner, 1)
39                  assert_equal(node_ibd.getblockchaininfo()['initialblockdownload'], True)
40  
41          # tip within maximum age -> leave IBD
42          node_miner.setmocktime(max(cur_time - maxtipage, 0))
43          self.generate(node_miner, 1)
44          assert_equal(node_ibd.getblockchaininfo()['initialblockdownload'], False)
45  
46          # reset time to system time so we don't have a time offset with the ibd node the next
47          # time we connect to it, ensuring TimeOffsets::WarnIfOutOfSync() doesn't output to stderr
48          node_miner.setmocktime(0)
49  
50      def run_test(self):
51          self.log.info("Test IBD with maximum tip age of 24 hours (default).")
52          self.test_maxtipage(DEFAULT_MAX_TIP_AGE, set_parameter=False)
53  
54          for hours in [20, 10, 5, 2, 1]:
55              maxtipage = hours * 60 * 60
56              self.log.info(f"Test IBD with maximum tip age of {hours} hours (-maxtipage={maxtipage}).")
57              self.test_maxtipage(maxtipage)
58  
59          max_long_val = 9223372036854775807
60          self.log.info(f"Test IBD with highest allowable maximum tip age ({max_long_val}).")
61          self.test_maxtipage(max_long_val, test_deltas=False)
62  
63  
64  if __name__ == '__main__':
65      MaxTipAgeTest(__file__).main()