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