/ test / functional / p2p_headers_sync_with_minchainwork.py
p2p_headers_sync_with_minchainwork.py
  1  #!/usr/bin/env python3
  2  # Copyright (c) 2019-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 that we reject low difficulty headers to prevent our block tree from filling up with useless bloat"""
  6  
  7  from test_framework.test_framework import BitcoinTestFramework
  8  
  9  from test_framework.p2p import (
 10      P2PInterface,
 11  )
 12  
 13  from test_framework.messages import (
 14      msg_headers,
 15  )
 16  
 17  from test_framework.blocktools import (
 18      NORMAL_GBT_REQUEST_PARAMS,
 19      create_block,
 20  )
 21  
 22  from test_framework.util import assert_equal
 23  
 24  NODE1_BLOCKS_REQUIRED = 15
 25  NODE2_BLOCKS_REQUIRED = 2047
 26  
 27  
 28  class RejectLowDifficultyHeadersTest(BitcoinTestFramework):
 29      def set_test_params(self):
 30          self.rpc_timeout *= 4  # To avoid timeout when generating BLOCKS_TO_MINE
 31          self.setup_clean_chain = True
 32          self.num_nodes = 4
 33          # Node0 has no required chainwork; node1 requires 15 blocks on top of the genesis block; node2 requires 2047
 34          self.extra_args = [["-minimumchainwork=0x0", "-checkblockindex=0"], ["-minimumchainwork=0x1f", "-checkblockindex=0"], ["-minimumchainwork=0x1000", "-checkblockindex=0"], ["-minimumchainwork=0x1000", "-checkblockindex=0", "-whitelist=noban@127.0.0.1"]]
 35  
 36      def setup_network(self):
 37          self.setup_nodes()
 38          self.reconnect_all()
 39          self.sync_all()
 40  
 41      def disconnect_all(self):
 42          self.disconnect_nodes(0, 1)
 43          self.disconnect_nodes(0, 2)
 44          self.disconnect_nodes(0, 3)
 45  
 46      def reconnect_all(self):
 47          self.connect_nodes(0, 1)
 48          self.connect_nodes(0, 2)
 49          self.connect_nodes(0, 3)
 50  
 51      def test_chains_sync_when_long_enough(self):
 52          self.log.info("Generate blocks on the node with no required chainwork, and verify nodes 1 and 2 have no new headers in their headers tree")
 53          with self.nodes[1].assert_debug_log(expected_msgs=["[net] Ignoring low-work chain (height=14)"]), self.nodes[2].assert_debug_log(expected_msgs=["[net] Ignoring low-work chain (height=14)"]), self.nodes[3].assert_debug_log(expected_msgs=["Synchronizing blockheaders, height: 14"]):
 54              self.generate(self.nodes[0], NODE1_BLOCKS_REQUIRED-1, sync_fun=self.no_op)
 55  
 56          # Node3 should always allow headers due to noban permissions
 57          self.log.info("Check that node3 will sync headers (due to noban permissions)")
 58  
 59          def check_node3_chaintips(num_tips, tip_hash, height):
 60              node3_chaintips = self.nodes[3].getchaintips()
 61              assert len(node3_chaintips) == num_tips
 62              assert {
 63                  'height': height,
 64                  'hash': tip_hash,
 65                  'branchlen': height,
 66                  'status': 'headers-only',
 67              } in node3_chaintips
 68  
 69          check_node3_chaintips(2, self.nodes[0].getbestblockhash(), NODE1_BLOCKS_REQUIRED-1)
 70  
 71          for node in self.nodes[1:3]:
 72              chaintips = node.getchaintips()
 73              assert len(chaintips) == 1
 74              assert {
 75                  'height': 0,
 76                  'hash': '0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206',
 77                  'branchlen': 0,
 78                  'status': 'active',
 79              } in chaintips
 80  
 81          self.log.info("Generate more blocks to satisfy node1's minchainwork requirement, and verify node2 still has no new headers in headers tree")
 82          with self.nodes[2].assert_debug_log(expected_msgs=["[net] Ignoring low-work chain (height=15)"]), self.nodes[3].assert_debug_log(expected_msgs=["Synchronizing blockheaders, height: 15"]):
 83              self.generate(self.nodes[0], NODE1_BLOCKS_REQUIRED - self.nodes[0].getblockcount(), sync_fun=self.no_op)
 84          self.sync_blocks(self.nodes[0:2]) # node3 will sync headers (noban permissions) but not blocks (due to minchainwork)
 85  
 86          assert {
 87              'height': 0,
 88              'hash': '0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206',
 89              'branchlen': 0,
 90              'status': 'active',
 91          } in self.nodes[2].getchaintips()
 92  
 93          assert len(self.nodes[2].getchaintips()) == 1
 94  
 95          self.log.info("Check that node3 accepted these headers as well")
 96          check_node3_chaintips(2, self.nodes[0].getbestblockhash(), NODE1_BLOCKS_REQUIRED)
 97  
 98          self.log.info("Generate long chain for node0/node1/node3")
 99          self.generate(self.nodes[0], NODE2_BLOCKS_REQUIRED-self.nodes[0].getblockcount(), sync_fun=self.no_op)
100  
101          self.log.info("Verify that node2 and node3 will sync the chain when it gets long enough")
102          self.sync_blocks()
103  
104      def test_peerinfo_includes_headers_presync_height(self):
105          self.log.info("Test that getpeerinfo() includes headers presync height")
106  
107          # Disconnect network, so that we can find our own peer connection more
108          # easily
109          self.disconnect_all()
110  
111          p2p = self.nodes[0].add_p2p_connection(P2PInterface())
112          node = self.nodes[0]
113  
114          # Ensure we have a long chain already
115          current_height = self.nodes[0].getblockcount()
116          if (current_height < 3000):
117              self.generate(node, 3000-current_height, sync_fun=self.no_op)
118  
119          # Send a group of 2000 headers, forking from genesis.
120          new_blocks = []
121          hashPrevBlock = int(node.getblockhash(0), 16)
122          for i in range(2000):
123              block = create_block(hashprev = hashPrevBlock, tmpl=node.getblocktemplate(NORMAL_GBT_REQUEST_PARAMS))
124              block.solve()
125              new_blocks.append(block)
126              hashPrevBlock = block.sha256
127  
128          headers_message = msg_headers(headers=new_blocks)
129          p2p.send_and_ping(headers_message)
130  
131          # getpeerinfo should show a sync in progress
132          assert_equal(node.getpeerinfo()[0]['presynced_headers'], 2000)
133  
134      def test_large_reorgs_can_succeed(self):
135          self.log.info("Test that a 2000+ block reorg, starting from a point that is more than 2000 blocks before a locator entry, can succeed")
136  
137          self.sync_all() # Ensure all nodes are synced.
138          self.disconnect_all()
139  
140          # locator(block at height T) will have heights:
141          # [T, T-1, ..., T-10, T-12, T-16, T-24, T-40, T-72, T-136, T-264,
142          #  T-520, T-1032, T-2056, T-4104, ...]
143          # So mine a number of blocks > 4104 to ensure that the first window of
144          # received headers during a sync are fully between locator entries.
145          BLOCKS_TO_MINE = 4110
146  
147          self.generate(self.nodes[0], BLOCKS_TO_MINE, sync_fun=self.no_op)
148          self.generate(self.nodes[1], BLOCKS_TO_MINE+2, sync_fun=self.no_op)
149  
150          self.reconnect_all()
151  
152          self.sync_blocks(timeout=300) # Ensure tips eventually agree
153  
154  
155      def run_test(self):
156          self.test_chains_sync_when_long_enough()
157  
158          self.test_large_reorgs_can_succeed()
159  
160          self.test_peerinfo_includes_headers_presync_height()
161  
162  
163  
164  if __name__ == '__main__':
165      RejectLowDifficultyHeadersTest().main()