|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2022 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 nMaxTipAge 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 nMaxTipAge 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): |
| 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 | + |
| 32 | + # tips older than maximum age -> stay in IBD |
| 33 | + cur_time = int(time.time()) |
| 34 | + node_ibd.setmocktime(cur_time) |
| 35 | + for delta in [5, 4, 3, 2, 1]: |
| 36 | + node_miner.setmocktime(cur_time - maxtipage - delta) |
| 37 | + self.generate(node_miner, 1) |
| 38 | + assert_equal(node_ibd.getblockchaininfo()['initialblockdownload'], True) |
| 39 | + |
| 40 | + # tip within maximum age -> leave IBD |
| 41 | + node_miner.setmocktime(cur_time - maxtipage) |
| 42 | + self.generate(node_miner, 1) |
| 43 | + assert_equal(node_ibd.getblockchaininfo()['initialblockdownload'], False) |
| 44 | + |
| 45 | + def run_test(self): |
| 46 | + self.log.info("Test IBD with maximum tip age of 24 hours (default).") |
| 47 | + self.test_maxtipage(DEFAULT_MAX_TIP_AGE, set_parameter=False) |
| 48 | + |
| 49 | + for hours in [20, 10, 5, 2, 1]: |
| 50 | + maxtipage = hours * 60 * 60 |
| 51 | + self.log.info(f"Test IBD with maximum tip age of {hours} hours (-maxtipage={maxtipage}).") |
| 52 | + self.test_maxtipage(maxtipage) |
| 53 | + |
| 54 | + |
| 55 | +if __name__ == '__main__': |
| 56 | + MaxTipAgeTest().main() |
0 commit comments