您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

minchainwork.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2017 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 nMinimumChainWork on command line.
  6. Nodes don't consider themselves out of "initial block download" until
  7. their active chain has more work than nMinimumChainWork.
  8. Nodes don't download blocks from a peer unless the peer's best known block
  9. has more work than nMinimumChainWork.
  10. While in initial block download, nodes won't relay blocks to their peers, so
  11. test that this parameter functions as intended by verifying that block relay
  12. only succeeds past a given node once its nMinimumChainWork has been exceeded.
  13. """
  14. import time
  15. from test_framework.test_framework import BitcoinTestFramework
  16. from test_framework.util import sync_blocks, connect_nodes, assert_equal
  17. # 2 hashes required per regtest block (with no difficulty adjustment)
  18. REGTEST_WORK_PER_BLOCK = 2
  19. class MinimumChainWorkTest(BitcoinTestFramework):
  20. def set_test_params(self):
  21. self.setup_clean_chain = True
  22. self.num_nodes = 3
  23. self.extra_args = [[], ["-minimumchainwork=0x65"], ["-minimumchainwork=0x65"]]
  24. self.node_min_work = [0, 101, 101]
  25. def setup_network(self):
  26. # This test relies on the chain setup being:
  27. # node0 <- node1 <- node2
  28. # Before leaving IBD, nodes prefer to download blocks from outbound
  29. # peers, so ensure that we're mining on an outbound peer and testing
  30. # block relay to inbound peers.
  31. self.setup_nodes()
  32. for i in range(self.num_nodes-1):
  33. connect_nodes(self.nodes[i+1], i)
  34. def run_test(self):
  35. # Start building a chain on node0. node2 shouldn't be able to sync until node1's
  36. # minchainwork is exceeded
  37. starting_chain_work = REGTEST_WORK_PER_BLOCK # Genesis block's work
  38. self.log.info("Testing relay across node %d (minChainWork = %d)", 1, self.node_min_work[1])
  39. starting_blockcount = self.nodes[2].getblockcount()
  40. num_blocks_to_generate = int((self.node_min_work[1] - starting_chain_work) / REGTEST_WORK_PER_BLOCK)
  41. self.log.info("Generating %d blocks on node0", num_blocks_to_generate)
  42. hashes = self.nodes[0].generate(num_blocks_to_generate)
  43. self.log.info("Node0 current chain work: %s", self.nodes[0].getblockheader(hashes[-1])['chainwork'])
  44. # Sleep a few seconds and verify that node2 didn't get any new blocks
  45. # or headers. We sleep, rather than sync_blocks(node0, node1) because
  46. # it's reasonable either way for node1 to get the blocks, or not get
  47. # them (since they're below node1's minchainwork).
  48. time.sleep(3)
  49. self.log.info("Verifying node 2 has no more blocks than before")
  50. self.log.info("Blockcounts: %s", [n.getblockcount() for n in self.nodes])
  51. # Node2 shouldn't have any new headers yet, because node1 should not
  52. # have relayed anything.
  53. assert_equal(len(self.nodes[2].getchaintips()), 1)
  54. assert_equal(self.nodes[2].getchaintips()[0]['height'], 0)
  55. assert self.nodes[1].getbestblockhash() != self.nodes[0].getbestblockhash()
  56. assert_equal(self.nodes[2].getblockcount(), starting_blockcount)
  57. self.log.info("Generating one more block")
  58. self.nodes[0].generate(1)
  59. self.log.info("Verifying nodes are all synced")
  60. # Because nodes in regtest are all manual connections (eg using
  61. # addnode), node1 should not have disconnected node0. If not for that,
  62. # we'd expect node1 to have disconnected node0 for serving an
  63. # insufficient work chain, in which case we'd need to reconnect them to
  64. # continue the test.
  65. # NOTE: before commit 44407100ff9b478d6131a1c38ee993b50b1830df, regtest
  66. # connections were not manual, so the reconnect is necessary.
  67. if (len(self.nodes[0].getpeerinfo()) == 0):
  68. connect_nodes(self.nodes[1], 0)
  69. self.sync_all()
  70. self.log.info("Blockcounts: %s", [n.getblockcount() for n in self.nodes])
  71. if __name__ == '__main__':
  72. MinimumChainWorkTest().main()