Branch data Line data Source code
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>
9 : : #include <test/util/setup_common.h>
10 : : #include <script/solver.h>
11 : : #include <validation.h>
12 : :
13 : : #include <boost/test/unit_test.hpp>
14 : :
15 : : using interfaces::FoundBlock;
16 : :
17 : : BOOST_FIXTURE_TEST_SUITE(interfaces_tests, TestChain100Setup)
18 : :
19 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(findBlock)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
20 : : {
21 [ - + ]: 1 : LOCK(Assert(m_node.chainman)->GetMutex());
22 : 1 : auto& chain = m_node.chain;
23 [ - + + - ]: 1 : const CChain& active = Assert(m_node.chainman)->ActiveChain();
24 : :
25 : 1 : uint256 hash;
26 [ + - - + : 3 : BOOST_CHECK(chain->findBlock(active[10]->GetBlockHash(), FoundBlock().hash(hash)));
+ - + - +
- ]
27 [ + - - + : 2 : BOOST_CHECK_EQUAL(hash, active[10]->GetBlockHash());
+ - ]
28 : :
29 : 1 : int height = -1;
30 [ + - - + : 3 : BOOST_CHECK(chain->findBlock(active[20]->GetBlockHash(), FoundBlock().height(height)));
+ - + - +
- ]
31 [ + - - + : 2 : BOOST_CHECK_EQUAL(height, active[20]->nHeight);
+ - ]
32 : :
33 : 1 : CBlock data;
34 [ + - - + : 3 : BOOST_CHECK(chain->findBlock(active[30]->GetBlockHash(), FoundBlock().data(data)));
+ - + - +
- ]
35 [ + - - + : 2 : BOOST_CHECK_EQUAL(data.GetHash(), active[30]->GetBlockHash());
+ - + - ]
36 : :
37 : 1 : int64_t time = -1;
38 [ + - - + : 3 : BOOST_CHECK(chain->findBlock(active[40]->GetBlockHash(), FoundBlock().time(time)));
+ - + - +
- ]
39 [ + - - + : 2 : BOOST_CHECK_EQUAL(time, active[40]->GetBlockTime());
+ - ]
40 : :
41 : 1 : int64_t max_time = -1;
42 [ + - - + : 3 : BOOST_CHECK(chain->findBlock(active[50]->GetBlockHash(), FoundBlock().maxTime(max_time)));
+ - + - +
- ]
43 [ + - - + : 2 : BOOST_CHECK_EQUAL(max_time, active[50]->GetBlockTimeMax());
+ - ]
44 : :
45 : 1 : int64_t mtp_time = -1;
46 [ + - - + : 3 : BOOST_CHECK(chain->findBlock(active[60]->GetBlockHash(), FoundBlock().mtpTime(mtp_time)));
+ - + - +
- ]
47 [ + - - + : 2 : BOOST_CHECK_EQUAL(mtp_time, active[60]->GetMedianTimePast());
+ - ]
48 : :
49 : 1 : bool cur_active{false}, next_active{false};
50 : 1 : uint256 next_hash;
51 [ + - - + : 1 : BOOST_CHECK_EQUAL(active.Height(), 100);
+ - ]
52 [ + - - + : 3 : BOOST_CHECK(chain->findBlock(active[99]->GetBlockHash(), FoundBlock().inActiveChain(cur_active).nextBlock(FoundBlock().inActiveChain(next_active).hash(next_hash))));
+ - + - +
- ]
53 [ + - + - : 2 : BOOST_CHECK(cur_active);
+ - ]
54 [ + - + - : 2 : BOOST_CHECK(next_active);
+ - ]
55 [ + - - + : 2 : BOOST_CHECK_EQUAL(next_hash, active[100]->GetBlockHash());
+ - ]
56 : 1 : cur_active = next_active = false;
57 [ + - - + : 3 : BOOST_CHECK(chain->findBlock(active[100]->GetBlockHash(), FoundBlock().inActiveChain(cur_active).nextBlock(FoundBlock().inActiveChain(next_active))));
+ - + - +
- ]
58 [ + - + - : 2 : BOOST_CHECK(cur_active);
+ - ]
59 [ + - + - : 2 : BOOST_CHECK(!next_active);
+ - ]
60 : :
61 [ + - + - : 2 : BOOST_CHECK(!chain->findBlock({}, FoundBlock()));
+ - ]
62 [ + - ]: 2 : }
63 : :
64 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(findFirstBlockWithTimeAndHeight)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
65 : : {
66 [ - + ]: 1 : LOCK(Assert(m_node.chainman)->GetMutex());
67 : 1 : auto& chain = m_node.chain;
68 [ - + + - ]: 1 : const CChain& active = Assert(m_node.chainman)->ActiveChain();
69 : 1 : uint256 hash;
70 : 1 : int height;
71 [ + - + - : 2 : BOOST_CHECK(chain->findFirstBlockWithTimeAndHeight(/* min_time= */ 0, /* min_height= */ 5, FoundBlock().hash(hash).height(height)));
+ - + - ]
72 [ + - - + : 2 : BOOST_CHECK_EQUAL(hash, active[5]->GetBlockHash());
+ - ]
73 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(height, 5);
74 [ + - - + : 3 : BOOST_CHECK(!chain->findFirstBlockWithTimeAndHeight(/* min_time= */ active.Tip()->GetBlockTimeMax() + 1, /* min_height= */ 0));
+ - + - +
- ]
75 : 1 : }
76 : :
77 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(findAncestorByHeight)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
78 : : {
79 [ - + ]: 1 : LOCK(Assert(m_node.chainman)->GetMutex());
80 : 1 : auto& chain = m_node.chain;
81 [ - + + - ]: 1 : const CChain& active = Assert(m_node.chainman)->ActiveChain();
82 : 1 : uint256 hash;
83 [ + - - + : 3 : BOOST_CHECK(chain->findAncestorByHeight(active[20]->GetBlockHash(), 10, FoundBlock().hash(hash)));
- + + - +
- + - ]
84 [ + - - + : 2 : BOOST_CHECK_EQUAL(hash, active[10]->GetBlockHash());
+ - ]
85 [ + - - + : 3 : BOOST_CHECK(!chain->findAncestorByHeight(active[10]->GetBlockHash(), 20));
+ - + - +
- ]
86 : 1 : }
87 : :
88 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(findAncestorByHash)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
89 : : {
90 [ - + ]: 1 : LOCK(Assert(m_node.chainman)->GetMutex());
91 : 1 : auto& chain = m_node.chain;
92 [ - + + - ]: 1 : const CChain& active = Assert(m_node.chainman)->ActiveChain();
93 : 1 : int height = -1;
94 [ + - - + : 4 : BOOST_CHECK(chain->findAncestorByHash(active[20]->GetBlockHash(), active[10]->GetBlockHash(), FoundBlock().height(height)));
+ - + - +
- + - ]
95 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(height, 10);
96 [ + - - + : 4 : BOOST_CHECK(!chain->findAncestorByHash(active[10]->GetBlockHash(), active[20]->GetBlockHash()));
+ - + - +
- + - ]
97 : 1 : }
98 : :
99 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(findCommonAncestor)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
100 : : {
101 : 1 : auto& chain = m_node.chain;
102 [ - + - + : 3 : const CChain& active{*WITH_LOCK(Assert(m_node.chainman)->GetMutex(), return &Assert(m_node.chainman)->ActiveChain())};
+ - + - ]
103 [ - + ]: 1 : auto* orig_tip = active.Tip();
104 [ + + ]: 11 : for (int i = 0; i < 10; ++i) {
105 [ + - ]: 10 : BlockValidationState state;
106 [ + - - + : 20 : m_node.chainman->ActiveChainstate().InvalidateBlock(state, active.Tip());
+ - ]
107 : 10 : }
108 [ - + + - ]: 1 : BOOST_CHECK_EQUAL(active.Height(), orig_tip->nHeight - 10);
109 : 1 : coinbaseKey.MakeNewKey(true);
110 [ + + ]: 21 : for (int i = 0; i < 20; ++i) {
111 [ + - ]: 40 : CreateAndProcessBlock({}, GetScriptForRawPubKey(coinbaseKey.GetPubKey()));
112 : : }
113 [ - + + - ]: 1 : BOOST_CHECK_EQUAL(active.Height(), orig_tip->nHeight + 10);
114 : 1 : uint256 fork_hash;
115 : 1 : int fork_height;
116 : 1 : int orig_height;
117 [ - + + - : 3 : BOOST_CHECK(chain->findCommonAncestor(orig_tip->GetBlockHash(), active.Tip()->GetBlockHash(), FoundBlock().height(fork_height).hash(fork_hash), FoundBlock().height(orig_height)));
+ - ]
118 [ + - ]: 1 : BOOST_CHECK_EQUAL(orig_height, orig_tip->nHeight);
119 [ + - ]: 1 : BOOST_CHECK_EQUAL(fork_height, orig_tip->nHeight - 10);
120 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(fork_hash, active[fork_height]->GetBlockHash());
121 : :
122 : 1 : uint256 active_hash, orig_hash;
123 [ - + + - : 3 : BOOST_CHECK(!chain->findCommonAncestor(active.Tip()->GetBlockHash(), {}, {}, FoundBlock().hash(active_hash), {}));
+ - ]
124 [ + - + - ]: 2 : BOOST_CHECK(!chain->findCommonAncestor({}, orig_tip->GetBlockHash(), {}, {}, FoundBlock().hash(orig_hash)));
125 [ - + + - ]: 2 : BOOST_CHECK_EQUAL(active_hash, active.Tip()->GetBlockHash());
126 [ + - ]: 1 : BOOST_CHECK_EQUAL(orig_hash, orig_tip->GetBlockHash());
127 : 1 : }
128 : :
129 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(hasBlocks)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
130 : : {
131 : 1 : LOCK(::cs_main);
132 : 1 : auto& chain = m_node.chain;
133 [ - + + - ]: 1 : const CChain& active = Assert(m_node.chainman)->ActiveChain();
134 : :
135 : : // Test ranges
136 [ + - - + : 3 : BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));
+ - + - +
- ]
137 [ + - - + : 3 : BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));
+ - + - +
- ]
138 [ + - - + : 3 : BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));
+ - + - +
- ]
139 [ + - - + : 3 : BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));
+ - + - +
- ]
140 [ + - - + : 3 : BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));
+ - + - -
+ ]
141 [ - + ]: 1 : active[5]->nStatus &= ~BLOCK_HAVE_DATA;
142 [ + - - + : 3 : BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));
+ - + - +
- ]
143 [ + - - + : 3 : BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));
+ - + - +
- ]
144 [ + - - + : 3 : BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));
+ - + - +
- ]
145 [ + - - + : 3 : BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));
+ - + - +
- ]
146 [ + - - + : 3 : BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));
+ - + - -
+ ]
147 [ - + ]: 1 : active[95]->nStatus &= ~BLOCK_HAVE_DATA;
148 [ + - - + : 3 : BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));
+ - + - +
- ]
149 [ + - - + : 3 : BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));
+ - + - +
- ]
150 [ + - - + : 3 : BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));
+ - + - +
- ]
151 [ + - - + : 3 : BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));
+ - + - +
- ]
152 [ + - - + : 3 : BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));
+ - + - -
+ ]
153 [ - + ]: 1 : active[50]->nStatus &= ~BLOCK_HAVE_DATA;
154 [ + - - + : 3 : BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 10, 90));
+ - + - +
- ]
155 [ + - - + : 3 : BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 10, {}));
+ - + - +
- ]
156 [ + - - + : 3 : BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, 90));
+ - + - +
- ]
157 [ + - - + : 3 : BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 0, {}));
+ - + - +
- ]
158 [ + - - + : 3 : BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), -1000, 1000));
+ - + - +
- ]
159 : :
160 : : // Test edge cases
161 [ + - - + : 3 : BOOST_CHECK(chain->hasBlocks(active.Tip()->GetBlockHash(), 6, 49));
+ - + - +
- ]
162 [ + - - + : 3 : BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 5, 49));
+ - + - +
- ]
163 [ + - - + : 3 : BOOST_CHECK(!chain->hasBlocks(active.Tip()->GetBlockHash(), 6, 50));
+ - + - +
- ]
164 : 1 : }
165 : :
166 : : BOOST_AUTO_TEST_SUITE_END()
|