feature_minchainwork.py raw

   1  #!/usr/bin/env python3
   2  # Copyright (c) 2017-2022 The Limenka 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 nMinimumChainWork on command line.
   6  
   7  Nodes don't consider themselves out of "initial block download" until
   8  their active chain has more work than nMinimumChainWork.
   9  
  10  Nodes don't download blocks from a peer unless the peer's best known block
  11  has more work than nMinimumChainWork.
  12  
  13  While in initial block download, nodes won't relay blocks to their peers, so
  14  test that this parameter functions as intended by verifying that block relay
  15  only succeeds past a given node once its nMinimumChainWork has been exceeded.
  16  """
  17  
  18  import time
  19  
  20  from test_framework.p2p import P2PInterface, msg_getheaders
  21  from test_framework.test_framework import LimenkaTestFramework
  22  from test_framework.util import (
  23      assert_equal,
  24      ensure_for,
  25  )
  26  
  27  # 2 hashes required per regtest block (with no difficulty adjustment)
  28  REGTEST_WORK_PER_BLOCK = 2
  29  
  30  class MinimumChainWorkTest(LimenkaTestFramework):
  31      def set_test_params(self):
  32          self.setup_clean_chain = True
  33          self.num_nodes = 3
  34  
  35          self.extra_args = [[], ["-minimumchainwork=0x65"], ["-minimumchainwork=0x65"]]
  36          self.node_min_work = [0, 101, 101]
  37  
  38      def setup_network(self):
  39          # This test relies on the chain setup being:
  40          # node0 <- node1 <- node2
  41          # Before leaving IBD, nodes prefer to download blocks from outbound
  42          # peers, so ensure that we're mining on an outbound peer and testing
  43          # block relay to inbound peers.
  44          self.setup_nodes()
  45          for i in range(self.num_nodes-1):
  46              self.connect_nodes(i+1, i)
  47  
  48          # Set clock of node2 2 days ahead, to keep it in IBD during this test.
  49          self.nodes[2].setmocktime(int(time.time()) + 48*60*60)
  50  
  51      def run_test(self):
  52          # Start building a chain on node0.  node2 shouldn't be able to sync until node1's
  53          # minchainwork is exceeded
  54          starting_chain_work = REGTEST_WORK_PER_BLOCK # Genesis block's work
  55          self.log.info(f"Testing relay across node 1 (minChainWork = {self.node_min_work[1]})")
  56  
  57          starting_blockcount = self.nodes[2].getblockcount()
  58  
  59          num_blocks_to_generate = int((self.node_min_work[1] - starting_chain_work) / REGTEST_WORK_PER_BLOCK)
  60          self.log.info(f"Generating {num_blocks_to_generate} blocks on node0")
  61          hashes = self.generate(self.nodes[0], num_blocks_to_generate, sync_fun=self.no_op)
  62  
  63          self.log.info(f"Node0 current chain work: {self.nodes[0].getblockheader(hashes[-1])['chainwork']}")
  64          self.log.info("Verifying node 2 has no more blocks than before")
  65          self.log.info(f"Blockcounts: {[n.getblockcount() for n in self.nodes]}")
  66          # Node2 shouldn't have any new headers yet, because node1 should not
  67          # have relayed anything.
  68          # We wait 3 seconds, rather than sync_blocks(node0, node1) because
  69          # it's reasonable either way for node1 to get the blocks, or not get
  70          # them (since they're below node1's minchainwork).
  71          ensure_for(duration=3, f=lambda: len(self.nodes[2].getchaintips()) == 1)
  72          assert_equal(self.nodes[2].getchaintips()[0]['height'], 0)
  73  
  74          assert self.nodes[1].getbestblockhash() != self.nodes[0].getbestblockhash()
  75          assert_equal(self.nodes[2].getblockcount(), starting_blockcount)
  76  
  77          self.log.info("Check that getheaders requests to node2 are ignored")
  78          peer = self.nodes[2].add_p2p_connection(P2PInterface())
  79          msg = msg_getheaders()
  80          msg.locator.vHave = [int(self.nodes[2].getbestblockhash(), 16)]
  81          msg.hashstop = 0
  82          peer.send_and_ping(msg)
  83          ensure_for(duration=5, f=lambda: "headers" not in peer.last_message or len(peer.last_message["headers"].headers) == 0)
  84  
  85          self.log.info("Generating one more block")
  86          self.generate(self.nodes[0], 1)
  87  
  88          self.log.info("Verifying nodes are all synced")
  89  
  90          # Because nodes in regtest are all manual connections (eg using
  91          # addnode), node1 should not have disconnected node0. If not for that,
  92          # we'd expect node1 to have disconnected node0 for serving an
  93          # insufficient work chain, in which case we'd need to reconnect them to
  94          # continue the test.
  95  
  96          self.sync_all()
  97          self.log.info(f"Blockcounts: {[n.getblockcount() for n in self.nodes]}")
  98  
  99          self.log.info("Test that getheaders requests to node2 are not ignored")
 100          peer.send_and_ping(msg)
 101          assert "headers" in peer.last_message
 102  
 103          # Verify that node2 is in fact still in IBD (otherwise this test may
 104          # not be exercising the logic we want!)
 105          assert_equal(self.nodes[2].getblockchaininfo()['initialblockdownload'], True)
 106  
 107          self.log.info("Test -minimumchainwork with a non-hex value")
 108          self.stop_node(0)
 109          self.nodes[0].assert_start_raises_init_error(
 110              ["-minimumchainwork=test"],
 111              expected_msg='Error: Invalid minimum work specified (test), must be up to 64 hex digits',
 112          )
 113  
 114  
 115  if __name__ == '__main__':
 116      MinimumChainWorkTest(__file__).main()
 117