/ src / test / blockchain_tests.cpp
blockchain_tests.cpp
  1  // Copyright (c) 2017-present The Bitcoin Core developers
  2  // Distributed under the MIT software license, see the accompanying
  3  // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  4  
  5  #include <chain.h>
  6  #include <node/blockstorage.h>
  7  #include <rpc/blockchain.h>
  8  #include <sync.h>
  9  #include <test/util/setup_common.h>
 10  #include <util/string.h>
 11  
 12  #include <boost/test/unit_test.hpp>
 13  
 14  #include <cstdlib>
 15  
 16  using util::ToString;
 17  
 18  /* Equality between doubles is imprecise. Comparison should be done
 19   * with a small threshold of tolerance, rather than exact equality.
 20   */
 21  static bool DoubleEquals(double a, double b, double epsilon)
 22  {
 23      return std::abs(a - b) < epsilon;
 24  }
 25  
 26  static CBlockIndex* CreateBlockIndexWithNbits(uint32_t nbits)
 27  {
 28      CBlockIndex* block_index = new CBlockIndex();
 29      block_index->nHeight = 46367;
 30      block_index->nTime = 1269211443;
 31      block_index->nBits = nbits;
 32      return block_index;
 33  }
 34  
 35  static void RejectDifficultyMismatch(double difficulty, double expected_difficulty) {
 36       BOOST_CHECK_MESSAGE(
 37          DoubleEquals(difficulty, expected_difficulty, 0.00001),
 38          "Difficulty was " + ToString(difficulty)
 39              + " but was expected to be " + ToString(expected_difficulty));
 40  }
 41  
 42  /* Given a BlockIndex with the provided nbits,
 43   * verify that the expected difficulty results.
 44   */
 45  static void TestDifficulty(uint32_t nbits, double expected_difficulty)
 46  {
 47      CBlockIndex* block_index = CreateBlockIndexWithNbits(nbits);
 48      double difficulty = GetDifficulty(*block_index);
 49      delete block_index;
 50  
 51      RejectDifficultyMismatch(difficulty, expected_difficulty);
 52  }
 53  
 54  BOOST_FIXTURE_TEST_SUITE(blockchain_tests, BasicTestingSetup)
 55  
 56  BOOST_AUTO_TEST_CASE(get_difficulty_for_very_low_target)
 57  {
 58      TestDifficulty(0x1f111111, 0.000001);
 59  }
 60  
 61  BOOST_AUTO_TEST_CASE(get_difficulty_for_low_target)
 62  {
 63      TestDifficulty(0x1ef88f6f, 0.000016);
 64  }
 65  
 66  BOOST_AUTO_TEST_CASE(get_difficulty_for_mid_target)
 67  {
 68      TestDifficulty(0x1df88f6f, 0.004023);
 69  }
 70  
 71  BOOST_AUTO_TEST_CASE(get_difficulty_for_high_target)
 72  {
 73      TestDifficulty(0x1cf88f6f, 1.029916);
 74  }
 75  
 76  BOOST_AUTO_TEST_CASE(get_difficulty_for_very_high_target)
 77  {
 78      TestDifficulty(0x12345678, 5913134931067755359633408.0);
 79  }
 80  
 81  //! Prune chain from height down to genesis block and check that
 82  //! GetPruneHeight returns the correct value
 83  static void CheckGetPruneHeight(const node::BlockManager& blockman, const CChain& chain, int height) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
 84  {
 85      AssertLockHeld(::cs_main);
 86  
 87      // Emulate pruning all blocks from `height` down to the genesis block
 88      // by unsetting the `BLOCK_HAVE_DATA` flag from `nStatus`
 89      for (CBlockIndex* it{chain[height]}; it != nullptr && it->nHeight > 0; it = it->pprev) {
 90          it->nStatus &= ~BLOCK_HAVE_DATA;
 91      }
 92  
 93      const auto prune_height{GetPruneHeight(blockman, chain)};
 94      BOOST_REQUIRE(prune_height.has_value());
 95      BOOST_CHECK_EQUAL(*prune_height, height);
 96  }
 97  
 98  BOOST_FIXTURE_TEST_CASE(get_prune_height, TestChain100Setup)
 99  {
100      LOCK(::cs_main);
101      const auto& chain = m_node.chainman->ActiveChain();
102      const auto& blockman = m_node.chainman->m_blockman;
103  
104      // Fresh chain of 100 blocks without any pruned blocks, so std::nullopt should be returned
105      BOOST_CHECK(!GetPruneHeight(blockman, chain).has_value());
106  
107      // Start pruning
108      CheckGetPruneHeight(blockman, chain, 1);
109      CheckGetPruneHeight(blockman, chain, 99);
110      CheckGetPruneHeight(blockman, chain, 100);
111  }
112  
113  BOOST_AUTO_TEST_CASE(num_chain_tx_max)
114  {
115      CBlockIndex block_index{};
116      block_index.m_chain_tx_count = std::numeric_limits<uint64_t>::max();
117      BOOST_CHECK_EQUAL(block_index.m_chain_tx_count, std::numeric_limits<uint64_t>::max());
118  }
119  
120  BOOST_FIXTURE_TEST_CASE(invalidate_block, TestChain100Setup)
121  {
122      const CChain& active{*WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return &Assert(m_node.chainman)->ActiveChain())};
123  
124      // Check BlockStatus when doing InvalidateBlock()
125      BlockValidationState state;
126      auto* orig_tip = active.Tip();
127      int height_to_invalidate = orig_tip->nHeight - 10;
128      auto* tip_to_invalidate = active[height_to_invalidate];
129      m_node.chainman->ActiveChainstate().InvalidateBlock(state, tip_to_invalidate);
130  
131      // tip_to_invalidate just got invalidated, so it's BLOCK_FAILED_VALID
132      WITH_LOCK(::cs_main, assert(tip_to_invalidate->nStatus & BLOCK_FAILED_VALID));
133  
134      // check all ancestors of the invalidated block are validated up to BLOCK_VALID_TRANSACTIONS and are not invalid
135      auto pindex = tip_to_invalidate->pprev;
136      while (pindex) {
137          WITH_LOCK(::cs_main, assert(pindex->IsValid(BLOCK_VALID_TRANSACTIONS)));
138          WITH_LOCK(::cs_main, assert((pindex->nStatus & BLOCK_FAILED_VALID) == 0));
139          pindex = pindex->pprev;
140      }
141  
142      // check all descendants of the invalidated block are BLOCK_FAILED_VALID
143      pindex = orig_tip;
144      while (pindex && pindex != tip_to_invalidate) {
145          WITH_LOCK(::cs_main, assert(pindex->nStatus & BLOCK_FAILED_VALID));
146          pindex = pindex->pprev;
147      }
148  }
149  
150  BOOST_AUTO_TEST_SUITE_END()