Bitcoin Core  31.0.0
P2P Digital Currency
interfaces_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020-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 <chainparams.h>
6 #include <consensus/validation.h>
7 #include <interfaces/chain.h>
8 #include <test/util/common.h>
10 #include <script/solver.h>
11 #include <validation.h>
12 
13 #include <boost/test/unit_test.hpp>
14 
16 
18 
20 {
21  LOCK(Assert(m_node.chainman)->GetMutex());
22  auto& chain = m_node.chain;
23  const CChain& active = Assert(m_node.chainman)->ActiveChain();
24 
25  uint256 hash;
26  BOOST_CHECK(chain->findBlock(active[10]->GetBlockHash(), FoundBlock().hash(hash)));
27  BOOST_CHECK_EQUAL(hash, active[10]->GetBlockHash());
28 
29  int height = -1;
30  BOOST_CHECK(chain->findBlock(active[20]->GetBlockHash(), FoundBlock().height(height)));
31  BOOST_CHECK_EQUAL(height, active[20]->nHeight);
32 
33  CBlock data;
34  BOOST_CHECK(chain->findBlock(active[30]->GetBlockHash(), FoundBlock().data(data)));
35  BOOST_CHECK_EQUAL(data.GetHash(), active[30]->GetBlockHash());
36 
37  int64_t time = -1;
38  BOOST_CHECK(chain->findBlock(active[40]->GetBlockHash(), FoundBlock().time(time)));
39  BOOST_CHECK_EQUAL(time, active[40]->GetBlockTime());
40 
41  int64_t max_time = -1;
42  BOOST_CHECK(chain->findBlock(active[50]->GetBlockHash(), FoundBlock().maxTime(max_time)));
43  BOOST_CHECK_EQUAL(max_time, active[50]->GetBlockTimeMax());
44 
45  int64_t mtp_time = -1;
46  BOOST_CHECK(chain->findBlock(active[60]->GetBlockHash(), FoundBlock().mtpTime(mtp_time)));
47  BOOST_CHECK_EQUAL(mtp_time, active[60]->GetMedianTimePast());
48 
49  bool cur_active{false}, next_active{false};
50  uint256 next_hash;
51  BOOST_CHECK_EQUAL(active.Height(), 100);
52  BOOST_CHECK(chain->findBlock(active[99]->GetBlockHash(), FoundBlock().inActiveChain(cur_active).nextBlock(FoundBlock().inActiveChain(next_active).hash(next_hash))));
53  BOOST_CHECK(cur_active);
54  BOOST_CHECK(next_active);
55  BOOST_CHECK_EQUAL(next_hash, active[100]->GetBlockHash());
56  cur_active = next_active = false;
57  BOOST_CHECK(chain->findBlock(active[100]->GetBlockHash(), FoundBlock().inActiveChain(cur_active).nextBlock(FoundBlock().inActiveChain(next_active))));
58  BOOST_CHECK(cur_active);
59  BOOST_CHECK(!next_active);
60 
61  BOOST_CHECK(!chain->findBlock({}, FoundBlock()));
62 }
63 
64 BOOST_AUTO_TEST_CASE(findFirstBlockWithTimeAndHeight)
65 {
66  LOCK(Assert(m_node.chainman)->GetMutex());
67  auto& chain = m_node.chain;
68  const CChain& active = Assert(m_node.chainman)->ActiveChain();
69  uint256 hash;
70  int height;
71  BOOST_CHECK(chain->findFirstBlockWithTimeAndHeight(/* min_time= */ 0, /* min_height= */ 5, FoundBlock().hash(hash).height(height)));
72  BOOST_CHECK_EQUAL(hash, active[5]->GetBlockHash());
73  BOOST_CHECK_EQUAL(height, 5);
74  BOOST_CHECK(!chain->findFirstBlockWithTimeAndHeight(/* min_time= */ active.Tip()->GetBlockTimeMax() + 1, /* min_height= */ 0));
75 }
76 
77 BOOST_AUTO_TEST_CASE(findAncestorByHeight)
78 {
79  LOCK(Assert(m_node.chainman)->GetMutex());
80  auto& chain = m_node.chain;
81  const CChain& active = Assert(m_node.chainman)->ActiveChain();
82  uint256 hash;
83  BOOST_CHECK(chain->findAncestorByHeight(active[20]->GetBlockHash(), 10, FoundBlock().hash(hash)));
84  BOOST_CHECK_EQUAL(hash, active[10]->GetBlockHash());
85  BOOST_CHECK(!chain->findAncestorByHeight(active[10]->GetBlockHash(), 20));
86 }
87 
88 BOOST_AUTO_TEST_CASE(findAncestorByHash)
89 {
90  LOCK(Assert(m_node.chainman)->GetMutex());
91  auto& chain = m_node.chain;
92  const CChain& active = Assert(m_node.chainman)->ActiveChain();
93  int height = -1;
94  BOOST_CHECK(chain->findAncestorByHash(active[20]->GetBlockHash(), active[10]->GetBlockHash(), FoundBlock().height(height)));
95  BOOST_CHECK_EQUAL(height, 10);
96  BOOST_CHECK(!chain->findAncestorByHash(active[10]->GetBlockHash(), active[20]->GetBlockHash()));
97 }
98 
99 BOOST_AUTO_TEST_CASE(findCommonAncestor)
100 {
101  auto& chain = m_node.chain;
102  const CChain& active{*WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return &Assert(m_node.chainman)->ActiveChain())};
103  auto* orig_tip = active.Tip();
104  for (int i = 0; i < 10; ++i) {
105  BlockValidationState state;
106  m_node.chainman->ActiveChainstate().InvalidateBlock(state, active.Tip());
107  }
108  BOOST_CHECK_EQUAL(active.Height(), orig_tip->nHeight - 10);
109  coinbaseKey.MakeNewKey(true);
110  for (int i = 0; i < 20; ++i) {
111  CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
112  }
113  BOOST_CHECK_EQUAL(active.Height(), orig_tip->nHeight + 10);
114  uint256 fork_hash;
115  int fork_height;
116  int orig_height;
117  BOOST_CHECK(chain->findCommonAncestor(orig_tip->GetBlockHash(), active.Tip()->GetBlockHash(), FoundBlock().height(fork_height).hash(fork_hash), FoundBlock().height(orig_height)));
118  BOOST_CHECK_EQUAL(orig_height, orig_tip->nHeight);
119  BOOST_CHECK_EQUAL(fork_height, orig_tip->nHeight - 10);
120  BOOST_CHECK_EQUAL(fork_hash, active[fork_height]->GetBlockHash());
121 
122  uint256 active_hash, orig_hash;
123  BOOST_CHECK(!chain->findCommonAncestor(active.Tip()->GetBlockHash(), {}, {}, FoundBlock().hash(active_hash), {}));
124  BOOST_CHECK(!chain->findCommonAncestor({}, orig_tip->GetBlockHash(), {}, {}, FoundBlock().hash(orig_hash)));
125  BOOST_CHECK_EQUAL(active_hash, active.Tip()->GetBlockHash());
126  BOOST_CHECK_EQUAL(orig_hash, orig_tip->GetBlockHash());
127 }
128 
130 {
131  LOCK(::cs_main);
132  auto& chain = m_node.chain;
133  const CChain& active = Assert(m_node.chainman)->ActiveChain();
134 
135  // Test ranges
136  BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));
137  BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));
138  BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));
139  BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));
140  BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));
141  active[5]->nStatus &= ~BLOCK_HAVE_DATA;
142  BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));
143  BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));
144  BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));
145  BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));
146  BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));
147  active[95]->nStatus &= ~BLOCK_HAVE_DATA;
148  BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));
149  BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));
150  BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));
151  BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));
152  BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));
153  active[50]->nStatus &= ~BLOCK_HAVE_DATA;
154  BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));
155  BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));
156  BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));
157  BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));
158  BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));
159 
160  // Test edge cases
161  BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 6, 49));
162  BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 5, 49));
163  BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 6, 50));
164 }
165 
Helper for findBlock to selectively return pieces of block data.
Definition: chain.h:52
std::unique_ptr< interfaces::Chain > chain
Definition: context.h:76
BOOST_AUTO_TEST_CASE(findBlock)
Definition: block.h:73
node::NodeContext m_node
Definition: bitcoin-gui.cpp:43
An in-memory indexed chain of blocks.
Definition: chain.h:379
int Height() const
Return the maximal height in the chain.
Definition: chain.h:425
uint256 GetBlockHash() const
Definition: chain.h:198
FoundBlock & data(CBlock &data)
Read block data from disk.
Definition: chain.h:68
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
#define LOCK(cs)
Definition: sync.h:258
BOOST_AUTO_TEST_SUITE_END()
FoundBlock & maxTime(int64_t &max_time)
Definition: chain.h:58
int64_t GetBlockTimeMax() const
Definition: chain.h:226
Testing fixture that pre-creates a 100-block REGTEST-mode block chain.
Definition: setup_common.h:146
unsigned int nHeight
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:289
256-bit opaque blob.
Definition: uint256.h:195
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:17
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or nullptr if none.
Definition: chain.h:396
FoundBlock & hash(uint256 &hash)
Definition: chain.h:55
CScript GetScriptForRawPubKey(const CPubKey &pubKey)
Generate a P2PK script for the given pubkey.
Definition: solver.cpp:213
FoundBlock & inActiveChain(bool &in_active_chain)
Return whether block is in the active (most-work) chain.
Definition: chain.h:61
FoundBlock & height(int &height)
Definition: chain.h:56
FoundBlock & mtpTime(int64_t &mtp_time)
Definition: chain.h:59
full block available in blk*.dat
Definition: chain.h:75
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: cs_main.cpp:8
FoundBlock & nextBlock(const FoundBlock &next_block)
Return next block in the active chain if current block is in the active chain.
Definition: chain.h:65
FoundBlock & time(int64_t &time)
Definition: chain.h:57
std::unique_ptr< ChainstateManager > chainman
Definition: context.h:72
#define Assert(val)
Identity function.
Definition: check.h:113
#define BOOST_CHECK(expr)
Definition: object.cpp:16