Branch data Line data Source code
1 : : // Copyright (c) 2024-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 <kernel/bitcoinkernel.h>
6 : : #include <kernel/bitcoinkernel_wrapper.h>
7 : :
8 : : #define BOOST_TEST_MODULE Bitcoin Kernel Test Suite
9 : : #include <boost/test/included/unit_test.hpp>
10 : :
11 : : #include <test/kernel/block_data.h>
12 : :
13 : : #include <charconv>
14 : : #include <cstdint>
15 : : #include <cstdlib>
16 : : #include <filesystem>
17 : : #include <iostream>
18 : : #include <memory>
19 : : #include <optional>
20 : : #include <random>
21 : : #include <ranges>
22 : : #include <span>
23 : : #include <string>
24 : : #include <string_view>
25 : : #include <vector>
26 : :
27 : : using namespace btck;
28 : :
29 : 5 : std::string random_string(uint32_t length)
30 : : {
31 : 5 : const std::string chars = "0123456789"
32 : : "abcdefghijklmnopqrstuvwxyz"
33 : 5 : "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
34 : :
35 [ + + + - : 6 : static std::random_device rd;
+ - ]
36 [ + + + - : 5 : static std::default_random_engine dre{rd()};
+ - ]
37 [ + + + - : 5 : static std::uniform_int_distribution<> distribution(0, chars.size() - 1);
- + ]
38 : :
39 [ + - ]: 5 : std::string random;
40 [ + - ]: 5 : random.reserve(length);
41 [ + + ]: 85 : for (uint32_t i = 0; i < length; i++) {
42 [ + - ]: 160 : random += chars[distribution(dre)];
43 : : }
44 : 5 : return random;
45 : 5 : }
46 : :
47 : 437 : std::vector<std::byte> hex_string_to_byte_vec(std::string_view hex)
48 : : {
49 : 437 : std::vector<std::byte> bytes;
50 [ + - ]: 437 : bytes.reserve(hex.length() / 2);
51 : :
52 : 120675 : for (size_t i{0}; i < hex.length(); i += 2) {
53 : 120238 : uint8_t byte_value;
54 : 120238 : auto [ptr, ec] = std::from_chars(hex.data() + i, hex.data() + i + 2, byte_value, 16);
55 : :
56 [ + - - + ]: 120238 : if (ec != std::errc{} || ptr != hex.data() + i + 2) {
57 [ # # ]: 0 : throw std::invalid_argument("Invalid hex character");
58 : : }
59 [ + - + + ]: 240913 : bytes.push_back(static_cast<std::byte>(byte_value));
60 : : }
61 : 437 : return bytes;
62 : 0 : }
63 : :
64 : 1 : std::string byte_span_to_hex_string_reversed(std::span<const std::byte> bytes)
65 : : {
66 : 1 : std::ostringstream oss;
67 : :
68 : : // Iterate in reverse order
69 [ + + ]: 33 : for (auto it = bytes.rbegin(); it != bytes.rend(); ++it) {
70 [ + - ]: 32 : oss << std::hex << std::setw(2) << std::setfill('0')
71 [ + - ]: 32 : << static_cast<unsigned int>(static_cast<uint8_t>(*it));
72 : : }
73 : :
74 [ + - ]: 2 : return oss.str();
75 : 1 : }
76 : :
77 : : constexpr auto VERIFY_ALL_PRE_SEGWIT{ScriptVerificationFlags::P2SH | ScriptVerificationFlags::DERSIG |
78 : : ScriptVerificationFlags::NULLDUMMY | ScriptVerificationFlags::CHECKLOCKTIMEVERIFY |
79 : : ScriptVerificationFlags::CHECKSEQUENCEVERIFY};
80 : : constexpr auto VERIFY_ALL_PRE_TAPROOT{VERIFY_ALL_PRE_SEGWIT | ScriptVerificationFlags::WITNESS};
81 : :
82 : 29 : void check_equal(std::span<const std::byte> _actual, std::span<const std::byte> _expected, bool equal = true)
83 : : {
84 : 29 : std::span<const uint8_t> actual{reinterpret_cast<const unsigned char*>(_actual.data()), _actual.size()};
85 : 29 : std::span<const uint8_t> expected{reinterpret_cast<const unsigned char*>(_expected.data()), _expected.size()};
86 [ + - + - ]: 58 : BOOST_CHECK_EQUAL_COLLECTIONS(
87 : : actual.begin(), actual.end(),
88 : : expected.begin(), expected.end());
89 : 29 : }
90 : :
91 : : class TestLog
92 : : {
93 : : public:
94 : 72 : void LogMessage(std::string_view message)
95 : : {
96 : 72 : std::cout << "kernel: " << message;
97 : 72 : }
98 : : };
99 : :
100 : : struct TestDirectory {
101 : : std::filesystem::path m_directory;
102 : 5 : TestDirectory(std::string directory_name)
103 [ + - + - : 15 : : m_directory{std::filesystem::temp_directory_path() / (directory_name + random_string(16))}
+ - + - ]
104 : : {
105 [ + - ]: 5 : std::filesystem::create_directories(m_directory);
106 : 5 : }
107 : :
108 : 5 : ~TestDirectory()
109 : : {
110 : 5 : std::filesystem::remove_all(m_directory);
111 : 5 : }
112 : : };
113 : :
114 : 8 : class TestKernelNotifications : public KernelNotifications
115 : : {
116 : : public:
117 : 418 : void HeaderTipHandler(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) override
118 : : {
119 [ + - ]: 418 : BOOST_CHECK_GT(timestamp, 0);
120 : 418 : }
121 : :
122 : 0 : void WarningSetHandler(Warning warning, std::string_view message) override
123 : : {
124 : 0 : std::cout << "Kernel warning is set: " << message << std::endl;
125 : 0 : }
126 : :
127 : 426 : void WarningUnsetHandler(Warning warning) override
128 : : {
129 : 426 : std::cout << "Kernel warning was unset." << std::endl;
130 : 426 : }
131 : :
132 : 0 : void FlushErrorHandler(std::string_view error) override
133 : : {
134 : 0 : std::cout << error << std::endl;
135 : 0 : }
136 : :
137 : 0 : void FatalErrorHandler(std::string_view error) override
138 : : {
139 : 0 : std::cout << error << std::endl;
140 : 0 : }
141 : : };
142 : :
143 : 1 : class TestValidationInterface : public ValidationInterface
144 : : {
145 : : public:
146 : : std::optional<std::vector<std::byte>> m_expected_valid_block = std::nullopt;
147 : :
148 : 3 : void BlockChecked(Block block, const BlockValidationState state) override
149 : : {
150 [ + + ]: 3 : if (m_expected_valid_block.has_value()) {
151 : 1 : auto ser_block{block.ToBytes()};
152 [ - + + - : 1 : check_equal(m_expected_valid_block.value(), ser_block);
- + + - ]
153 : 1 : }
154 : :
155 : 3 : auto mode{state.GetValidationMode()};
156 [ + + - - ]: 3 : switch (mode) {
157 : 2 : case ValidationMode::VALID: {
158 : 2 : std::cout << "Valid block" << std::endl;
159 : 2 : return;
160 : : }
161 : 1 : case ValidationMode::INVALID: {
162 : 1 : std::cout << "Invalid block: ";
163 : 1 : auto result{state.GetBlockValidationResult()};
164 [ - - - - : 1 : switch (result) {
+ - - - -
- ]
165 : 0 : case BlockValidationResult::UNSET:
166 : 0 : std::cout << "initial value. Block has not yet been rejected" << std::endl;
167 : 0 : break;
168 : 0 : case BlockValidationResult::HEADER_LOW_WORK:
169 : 0 : std::cout << "the block header may be on a too-little-work chain" << std::endl;
170 : 0 : break;
171 : 0 : case BlockValidationResult::CONSENSUS:
172 : 0 : std::cout << "invalid by consensus rules (excluding any below reasons)" << std::endl;
173 : 0 : break;
174 : 0 : case BlockValidationResult::CACHED_INVALID:
175 : 0 : std::cout << "this block was cached as being invalid and we didn't store the reason why" << std::endl;
176 : 0 : break;
177 : 1 : case BlockValidationResult::INVALID_HEADER:
178 : 1 : std::cout << "invalid proof of work or time too old" << std::endl;
179 : 1 : break;
180 : 0 : case BlockValidationResult::MUTATED:
181 : 0 : std::cout << "the block's data didn't match the data committed to by the PoW" << std::endl;
182 : 0 : break;
183 : 0 : case BlockValidationResult::MISSING_PREV:
184 : 0 : std::cout << "We don't have the previous block the checked one is built on" << std::endl;
185 : 0 : break;
186 : 0 : case BlockValidationResult::INVALID_PREV:
187 : 0 : std::cout << "A block this one builds on is invalid" << std::endl;
188 : 0 : break;
189 : 0 : case BlockValidationResult::TIME_FUTURE:
190 : 0 : std::cout << "block timestamp was > 2 hours in the future (or our clock is bad)" << std::endl;
191 : 0 : break;
192 : : }
193 : : return;
194 : : }
195 : 0 : case ValidationMode::INTERNAL_ERROR: {
196 : 0 : std::cout << "Internal error" << std::endl;
197 : 0 : return;
198 : : }
199 : : }
200 : : }
201 : :
202 : 2 : void BlockConnected(Block block, BlockTreeEntry entry) override
203 : : {
204 : 2 : std::cout << "Block connected." << std::endl;
205 : 2 : }
206 : :
207 : 0 : void PowValidBlock(BlockTreeEntry entry, Block block) override
208 : : {
209 : 0 : std::cout << "Block passed pow verification" << std::endl;
210 : 0 : }
211 : :
212 : 0 : void BlockDisconnected(Block block, BlockTreeEntry entry) override
213 : : {
214 : 0 : std::cout << "Block disconnected." << std::endl;
215 : 0 : }
216 : : };
217 : :
218 : 3 : void run_verify_test(
219 : : const ScriptPubkey& spent_script_pubkey,
220 : : const Transaction& spending_tx,
221 : : std::span<TransactionOutput> spent_outputs,
222 : : int64_t amount,
223 : : unsigned int input_index,
224 : : bool taproot)
225 : : {
226 : 3 : auto status = ScriptVerifyStatus::OK;
227 : :
228 [ + + ]: 3 : if (taproot) {
229 [ + - + - ]: 2 : BOOST_CHECK(spent_script_pubkey.Verify(
230 : : amount,
231 : : spending_tx,
232 : : spent_outputs,
233 : : input_index,
234 : : ScriptVerificationFlags::ALL,
235 : : status));
236 [ + - ]: 2 : BOOST_CHECK(status == ScriptVerifyStatus::OK);
237 : : } else {
238 [ + - + - ]: 4 : BOOST_CHECK(!spent_script_pubkey.Verify(
239 : : amount,
240 : : spending_tx,
241 : : spent_outputs,
242 : : input_index,
243 : : ScriptVerificationFlags::ALL,
244 : : status));
245 [ + - ]: 4 : BOOST_CHECK(status == ScriptVerifyStatus::ERROR_SPENT_OUTPUTS_REQUIRED);
246 : : }
247 : :
248 [ + - + - ]: 6 : BOOST_CHECK(spent_script_pubkey.Verify(
249 : : amount,
250 : : spending_tx,
251 : : spent_outputs,
252 : : input_index,
253 : : VERIFY_ALL_PRE_TAPROOT,
254 : : status));
255 [ + - ]: 6 : BOOST_CHECK(status == ScriptVerifyStatus::OK);
256 : :
257 [ + - + - ]: 6 : BOOST_CHECK(spent_script_pubkey.Verify(
258 : : 0,
259 : : spending_tx,
260 : : spent_outputs,
261 : : input_index,
262 : : VERIFY_ALL_PRE_SEGWIT,
263 : : status));
264 [ + - ]: 6 : BOOST_CHECK(status == ScriptVerifyStatus::OK);
265 : 3 : }
266 : :
267 : : template <typename T>
268 : : concept HasToBytes = requires(T t) {
269 : : { t.ToBytes() } -> std::convertible_to<std::vector<std::byte>>;
270 : : };
271 : :
272 : : template <typename T>
273 : 14 : void CheckHandle(T object, T distinct_object)
274 : : {
275 [ + - ]: 28 : BOOST_CHECK(object.get() != nullptr);
276 [ + - ]: 28 : BOOST_CHECK(distinct_object.get() != nullptr);
277 [ + - ]: 28 : BOOST_CHECK(object.get() != distinct_object.get());
278 : :
279 : : if constexpr (HasToBytes<T>) {
280 [ - + + - : 4 : BOOST_CHECK_NE(object.ToBytes().size(), distinct_object.ToBytes().size());
- + + - ]
281 : : }
282 : :
283 : : // Copy constructor
284 : 14 : T object2(distinct_object);
285 [ + - + - : 14 : BOOST_CHECK_NE(distinct_object.get(), object2.get());
+ - ]
286 : : if constexpr (HasToBytes<T>) {
287 [ - + + - : 8 : check_equal(distinct_object.ToBytes(), object2.ToBytes());
- + + - +
- ]
288 : : }
289 : :
290 : : // Copy assignment
291 [ + - ]: 14 : T object3{distinct_object};
292 : 14 : object2 = object3;
293 [ + - + - : 14 : BOOST_CHECK_NE(object3.get(), object2.get());
+ - ]
294 : : if constexpr (HasToBytes<T>) {
295 [ - + + - : 8 : check_equal(object3.ToBytes(), object2.ToBytes());
- + + - +
- ]
296 : : }
297 : :
298 : : // Move constructor
299 [ + - ]: 14 : auto* original_ptr = object2.get();
300 : 14 : T object4{std::move(object2)};
301 [ + - + - ]: 14 : BOOST_CHECK_EQUAL(object4.get(), original_ptr);
302 [ + - + - : 14 : BOOST_CHECK_EQUAL(object2.get(), nullptr); // NOLINT(bugprone-use-after-move)
+ - ]
303 : : if constexpr (HasToBytes<T>) {
304 [ - + + - : 8 : check_equal(object4.ToBytes(), object3.ToBytes());
- + + - ]
305 : : }
306 : :
307 : : // Move assignment
308 : 14 : original_ptr = object4.get();
309 : 14 : object2 = std::move(object4);
310 [ + - + - ]: 14 : BOOST_CHECK_EQUAL(object2.get(), original_ptr);
311 [ + - + - : 14 : BOOST_CHECK_EQUAL(object4.get(), nullptr); // NOLINT(bugprone-use-after-move)
+ - ]
312 : : if constexpr (HasToBytes<T>) {
313 [ - + + - : 8 : check_equal(object2.ToBytes(), object3.ToBytes());
- + + - ]
314 : : }
315 : 14 : }
316 : :
317 : : template <typename RangeType>
318 : : requires std::ranges::random_access_range<RangeType>
319 : 6 : void CheckRange(const RangeType& range, size_t expected_size)
320 : : {
321 : : using value_type = std::ranges::range_value_t<RangeType>;
322 : :
323 [ + - ]: 6 : BOOST_CHECK_EQUAL(range.size(), expected_size);
324 [ + - ]: 6 : BOOST_CHECK_EQUAL(range.empty(), (expected_size == 0));
325 : :
326 [ + - + - : 18 : BOOST_CHECK(range.begin() != range.end());
+ - ]
327 [ + - ]: 6 : BOOST_CHECK_EQUAL(std::distance(range.begin(), range.end()), static_cast<std::ptrdiff_t>(expected_size));
328 [ + - ]: 12 : BOOST_CHECK(range.cbegin() == range.begin());
329 [ + - + - : 24 : BOOST_CHECK(range.cend() == range.end());
+ - ]
330 : :
331 [ + + ]: 224 : for (size_t i = 0; i < range.size(); ++i) {
332 [ + - ]: 218 : BOOST_CHECK_EQUAL(range[i].get(), (*(range.begin() + i)).get());
333 : : }
334 : :
335 [ + - ]: 6 : BOOST_CHECK_NE(range.at(0).get(), range.at(expected_size - 1).get());
336 [ + - - + : 12 : BOOST_CHECK_THROW(range.at(expected_size), std::out_of_range);
- - - - -
+ + - +
- ]
337 : :
338 [ + - ]: 6 : BOOST_CHECK_EQUAL(range.front().get(), range[0].get());
339 [ + - ]: 6 : BOOST_CHECK_EQUAL(range.back().get(), range[expected_size - 1].get());
340 : :
341 : 6 : auto it = range.begin();
342 : 6 : auto it_copy = it;
343 : 6 : ++it;
344 [ + - ]: 12 : BOOST_CHECK(it != it_copy);
345 : 6 : --it;
346 [ + - ]: 12 : BOOST_CHECK(it == it_copy);
347 : 6 : it = range.begin();
348 : 6 : auto old_it = it++;
349 [ + - ]: 12 : BOOST_CHECK(old_it == range.begin());
350 [ + - ]: 12 : BOOST_CHECK(it == range.begin() + 1);
351 : 6 : old_it = it--;
352 [ + - ]: 12 : BOOST_CHECK(old_it == range.begin() + 1);
353 [ + - ]: 12 : BOOST_CHECK(it == range.begin());
354 : :
355 : 6 : it = range.begin();
356 : 6 : it += 2;
357 [ + - ]: 12 : BOOST_CHECK(it == range.begin() + 2);
358 : 6 : it -= 2;
359 [ + - ]: 12 : BOOST_CHECK(it == range.begin());
360 : :
361 [ + - + - : 18 : BOOST_CHECK(range.begin() < range.end());
+ - ]
362 [ + - + - : 18 : BOOST_CHECK(range.begin() <= range.end());
+ - ]
363 [ + - + - ]: 18 : BOOST_CHECK(range.end() > range.begin());
364 [ + - + - ]: 18 : BOOST_CHECK(range.end() >= range.begin());
365 [ + - ]: 12 : BOOST_CHECK(range.begin() == range.begin());
366 : :
367 [ + - ]: 6 : BOOST_CHECK_EQUAL(range.begin()[0].get(), range[0].get());
368 : :
369 : 6 : size_t count = 0;
370 [ + - ]: 224 : for (auto rit = range.end(); rit != range.begin();) {
371 : 218 : --rit;
372 : 218 : ++count;
373 : : }
374 [ + - ]: 6 : BOOST_CHECK_EQUAL(count, expected_size);
375 : :
376 : 6 : std::vector<value_type> collected;
377 [ + - + - : 442 : for (const auto& elem : range) {
+ - ]
378 [ + - ]: 218 : collected.push_back(elem);
379 : : }
380 [ + - + - ]: 12 : BOOST_CHECK_EQUAL(collected.size(), expected_size);
381 : :
382 [ + - + - : 12 : BOOST_CHECK_EQUAL(std::ranges::size(range), expected_size);
+ - ]
383 : :
384 [ + - ]: 6 : it = range.begin();
385 : 6 : auto it2 = 1 + it;
386 [ + - + - ]: 12 : BOOST_CHECK(it2 == it + 1);
387 : 6 : }
388 : :
389 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_transaction_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
390 : : {
391 : 1 : auto tx_data{hex_string_to_byte_vec("02000000013f7cebd65c27431a90bba7f796914fe8cc2ddfc3f2cbd6f7e5f2fc854534da95000000006b483045022100de1ac3bcdfb0332207c4a91f3832bd2c2915840165f876ab47c5f8996b971c3602201c6c053d750fadde599e6f5c4e1963df0f01fc0d97815e8157e3d59fe09ca30d012103699b464d1d8bc9e47d4fb1cdaa89a1c5783d68363c4dbc4b524ed3d857148617feffffff02836d3c01000000001976a914fc25d6d5c94003bf5b0c7b640a248e2c637fcfb088ac7ada8202000000001976a914fbed3d9b11183209a57999d54d59f67c019e756c88ac6acb0700")};
392 [ - + + - ]: 1 : auto tx{Transaction{tx_data}};
393 [ + - ]: 1 : auto tx_data_2{hex_string_to_byte_vec("02000000000101904f4ee5c87d20090b642f116e458cd6693292ad9ece23e72f15fb6c05b956210500000000fdffffff02e2010000000000002251200839a723933b56560487ec4d67dda58f09bae518ffa7e148313c5696ac837d9f10060000000000002251205826bcdae7abfb1c468204170eab00d887b61ab143464a4a09e1450bdc59a3340140f26e7af574e647355830772946356c27e7bbc773c5293688890f58983499581be84de40be7311a14e6d6422605df086620e75adae84ff06b75ce5894de5e994a00000000")};
394 [ - + + - ]: 1 : auto tx2{Transaction{tx_data_2}};
395 [ + - + - : 2 : CheckHandle(tx, tx2);
+ - ]
396 : :
397 [ + - ]: 1 : auto invalid_data = hex_string_to_byte_vec("012300");
398 [ + - - + : 2 : BOOST_CHECK_THROW(Transaction{invalid_data}, std::runtime_error);
- + - - -
- - + + -
+ - ]
399 [ + - ]: 1 : auto empty_data = hex_string_to_byte_vec("");
400 [ + - - + : 2 : BOOST_CHECK_THROW(Transaction{empty_data}, std::runtime_error);
- + - - -
- - + + -
+ - ]
401 : :
402 [ + - + - : 1 : BOOST_CHECK_EQUAL(tx.CountOutputs(), 2);
+ - ]
403 [ + - + - : 1 : BOOST_CHECK_EQUAL(tx.CountInputs(), 1);
+ - ]
404 [ + - ]: 1 : auto broken_tx_data{std::span<std::byte>{tx_data.begin(), tx_data.begin() + 10}};
405 [ + - - + : 2 : BOOST_CHECK_THROW(Transaction{broken_tx_data}, std::runtime_error);
- - - - -
+ + - +
- ]
406 [ + - + - ]: 1 : auto output{tx.GetOutput(tx.CountOutputs() - 1)};
407 [ + - + - : 1 : BOOST_CHECK_EQUAL(output.Amount(), 42130042);
+ - ]
408 [ + - ]: 1 : auto script_pubkey{output.GetScriptPubkey()};
409 : 1 : {
410 [ - + + - ]: 1 : auto tx_new{Transaction{tx_data}};
411 : : // This is safe, because we now use copy assignment
412 [ + - + - ]: 1 : TransactionOutput output = tx_new.GetOutput(tx_new.CountOutputs() - 1);
413 [ + - ]: 1 : ScriptPubkey script = output.GetScriptPubkey();
414 : :
415 [ + - + - ]: 1 : TransactionOutputView output2 = tx_new.GetOutput(tx_new.CountOutputs() - 1);
416 [ + - + - ]: 1 : BOOST_CHECK_NE(output.get(), output2.get());
417 [ + - + - : 1 : BOOST_CHECK_EQUAL(output.Amount(), output2.Amount());
+ - + - ]
418 [ + - ]: 1 : TransactionOutput output3 = output2;
419 [ + - + - ]: 1 : BOOST_CHECK_NE(output3.get(), output2.get());
420 [ + - + - : 1 : BOOST_CHECK_EQUAL(output3.Amount(), output2.Amount());
+ - + - ]
421 : :
422 : : // Non-owned view
423 [ + - ]: 1 : ScriptPubkeyView script2 = output.GetScriptPubkey();
424 [ + - + - ]: 1 : BOOST_CHECK_NE(script.get(), script2.get());
425 [ + - - + : 2 : check_equal(script.ToBytes(), script2.ToBytes());
+ - - + +
- ]
426 : :
427 : : // Non-owned to owned
428 [ + - ]: 1 : ScriptPubkey script3 = script2;
429 [ + - + - ]: 1 : BOOST_CHECK_NE(script3.get(), script2.get());
430 [ + - - + : 2 : check_equal(script3.ToBytes(), script2.ToBytes());
+ - - + +
- ]
431 : 1 : }
432 [ + - + - : 1 : BOOST_CHECK_EQUAL(output.Amount(), 42130042);
+ - ]
433 : :
434 [ + - - + : 1 : auto tx_roundtrip{Transaction{tx.ToBytes()}};
+ - ]
435 [ - + + - : 1 : check_equal(tx_roundtrip.ToBytes(), tx_data);
- + + - ]
436 : :
437 : : // The following code is unsafe, but left here to show limitations of the
438 : : // API, because we preserve the output view beyond the lifetime of the
439 : : // transaction. The view type wrapper should make this clear to the user.
440 : : // auto get_output = [&]() -> TransactionOutputView {
441 : : // auto tx{Transaction{tx_data}};
442 : : // return tx.GetOutput(0);
443 : : // };
444 : : // auto output_new = get_output();
445 : : // BOOST_CHECK_EQUAL(output_new.Amount(), 20737411);
446 : :
447 : 1 : int64_t total_amount{0};
448 [ + - + - : 5 : for (const auto output : tx.Outputs()) {
+ - ]
449 [ + - ]: 2 : total_amount += output.Amount();
450 : : }
451 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(total_amount, 62867453);
452 : :
453 [ + - + - ]: 3 : auto amount = *(tx.Outputs() | std::ranges::views::filter([](const auto& output) {
454 [ + + ]: 2 : return output.Amount() == 42130042;
455 [ + - ]: 1 : }) |
456 : 1 : std::views::transform([](const auto& output) {
457 : 1 : return output.Amount();
458 [ + - ]: 1 : })).begin();
459 [ + - + - : 2 : BOOST_REQUIRE(amount);
+ - ]
460 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(amount, 42130042);
461 : :
462 [ + - + - ]: 1 : CheckRange(tx.Outputs(), tx.CountOutputs());
463 : :
464 [ + - - + : 1 : ScriptPubkey script_pubkey_roundtrip{script_pubkey.ToBytes()};
+ - ]
465 [ + - - + : 2 : check_equal(script_pubkey_roundtrip.ToBytes(), script_pubkey.ToBytes());
+ - - + +
- ]
466 : 1 : }
467 : :
468 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_script_pubkey)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
469 : : {
470 : 1 : auto script_data{hex_string_to_byte_vec("76a9144bfbaf6afb76cc5771bc6404810d1cc041a6933988ac")};
471 [ + - ]: 1 : std::vector<std::byte> script_data_2 = script_data;
472 [ + - ]: 1 : script_data_2.push_back(std::byte{0x51});
473 [ - + + - ]: 1 : ScriptPubkey script{script_data};
474 [ - + + - ]: 1 : ScriptPubkey script2{script_data_2};
475 [ + - + - : 2 : CheckHandle(script, script2);
+ - ]
476 : :
477 : 1 : std::span<std::byte> empty_data{};
478 [ + - ]: 1 : ScriptPubkey empty_script{empty_data};
479 [ + - + - : 2 : CheckHandle(script, empty_script);
+ - ]
480 : 1 : }
481 : :
482 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_transaction_output)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
483 : : {
484 [ - + + - ]: 1 : ScriptPubkey script{hex_string_to_byte_vec("76a9144bfbaf6afb76cc5771bc6404810d1cc041a6933988ac")};
485 [ + - ]: 1 : TransactionOutput output{script, 1};
486 [ + - ]: 1 : TransactionOutput output2{script, 2};
487 [ + - + - : 2 : CheckHandle(output, output2);
+ - ]
488 : 1 : }
489 : :
490 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_transaction_input)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
491 : : {
492 [ - + + - ]: 1 : Transaction tx{hex_string_to_byte_vec("020000000248c03e66fd371c7033196ce24298628e59ebefa00363026044e0f35e0325a65d000000006a473044022004893432347f39beaa280e99da595681ddb20fc45010176897e6e055d716dbfa022040a9e46648a5d10c33ef7cee5e6cf4b56bd513eae3ae044f0039824b02d0f44c012102982331a52822fd9b62e9b5d120da1d248558fac3da3a3c51cd7d9c8ad3da760efeffffffb856678c6e4c3c84e39e2ca818807049d6fba274b42af3c6d3f9d4b6513212d2000000006a473044022068bcedc7fe39c9f21ad318df2c2da62c2dc9522a89c28c8420ff9d03d2e6bf7b0220132afd752754e5cb1ea2fd0ed6a38ec666781e34b0e93dc9a08f2457842cf5660121033aeb9c079ea3e08ea03556182ab520ce5c22e6b0cb95cee6435ee17144d860cdfeffffff0260d50b00000000001976a914363cc8d55ea8d0500de728ef6d63804ddddbdc9888ac67040f00000000001976a914c303bdc5064bf9c9a8b507b5496bd0987285707988ac6acb0700")};
493 [ + - ]: 1 : TransactionInput input_0 = tx.GetInput(0);
494 [ + - ]: 1 : TransactionInput input_1 = tx.GetInput(1);
495 [ + - + - : 2 : CheckHandle(input_0, input_1);
+ - ]
496 [ + - + - ]: 1 : CheckRange(tx.Inputs(), tx.CountInputs());
497 [ + - ]: 1 : OutPoint point_0 = input_0.OutPoint();
498 [ + - ]: 1 : OutPoint point_1 = input_1.OutPoint();
499 [ + - + - : 2 : CheckHandle(point_0, point_1);
+ - ]
500 : 1 : }
501 : :
502 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_script_verify_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
503 : : {
504 : : // Legacy transaction aca326a724eda9a461c10a876534ecd5ae7b27f10f26c3862fb996f80ea2d45d
505 [ + - ]: 1 : run_verify_test(
506 [ + - + - ]: 3 : /*spent_script_pubkey*/ ScriptPubkey{hex_string_to_byte_vec("76a9144bfbaf6afb76cc5771bc6404810d1cc041a6933988ac")},
507 [ - + + - ]: 2 : /*spending_tx*/ Transaction{hex_string_to_byte_vec("02000000013f7cebd65c27431a90bba7f796914fe8cc2ddfc3f2cbd6f7e5f2fc854534da95000000006b483045022100de1ac3bcdfb0332207c4a91f3832bd2c2915840165f876ab47c5f8996b971c3602201c6c053d750fadde599e6f5c4e1963df0f01fc0d97815e8157e3d59fe09ca30d012103699b464d1d8bc9e47d4fb1cdaa89a1c5783d68363c4dbc4b524ed3d857148617feffffff02836d3c01000000001976a914fc25d6d5c94003bf5b0c7b640a248e2c637fcfb088ac7ada8202000000001976a914fbed3d9b11183209a57999d54d59f67c019e756c88ac6acb0700")},
508 : : /*spent_outputs*/ {},
509 : : /*amount*/ 0,
510 : : /*input_index*/ 0,
511 : : /*is_taproot*/ false);
512 : :
513 : : // Segwit transaction 1a3e89644985fbbb41e0dcfe176739813542b5937003c46a07de1e3ee7a4a7f3
514 [ + - ]: 1 : run_verify_test(
515 [ + - + - ]: 3 : /*spent_script_pubkey*/ ScriptPubkey{hex_string_to_byte_vec("0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d")},
516 [ - + + - ]: 2 : /*spending_tx*/ Transaction{hex_string_to_byte_vec("010000000001011f97548fbbe7a0db7588a66e18d803d0089315aa7d4cc28360b6ec50ef36718a0100000000ffffffff02df1776000000000017a9146c002a686959067f4866b8fb493ad7970290ab728757d29f0000000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d04004730440220565d170eed95ff95027a69b313758450ba84a01224e1f7f130dda46e94d13f8602207bdd20e307f062594022f12ed5017bbf4a055a06aea91c10110a0e3bb23117fc014730440220647d2dc5b15f60bc37dc42618a370b2a1490293f9e5c8464f53ec4fe1dfe067302203598773895b4b16d37485cbe21b337f4e4b650739880098c592553add7dd4355016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000")},
517 : : /*spent_outputs*/ {},
518 : : /*amount*/ 18393430,
519 : : /*input_index*/ 0,
520 : : /*is_taproot*/ false);
521 : :
522 : : // Taproot transaction 33e794d097969002ee05d336686fc03c9e15a597c1b9827669460fac98799036
523 [ - + + - ]: 1 : auto taproot_spent_script_pubkey{ScriptPubkey{hex_string_to_byte_vec("5120339ce7e165e67d93adb3fef88a6d4beed33f01fa876f05a225242b82a631abc0")}};
524 : 1 : std::vector<TransactionOutput> spent_outputs;
525 [ + - ]: 1 : spent_outputs.emplace_back(taproot_spent_script_pubkey, 88480);
526 [ - + + - ]: 2 : run_verify_test(
527 : : /*spent_script_pubkey*/ taproot_spent_script_pubkey,
528 [ + - ]: 2 : /*spending_tx*/ Transaction{hex_string_to_byte_vec("01000000000101d1f1c1f8cdf6759167b90f52c9ad358a369f95284e841d7a2536cef31c0549580100000000fdffffff020000000000000000316a2f49206c696b65205363686e6f7272207369677320616e6420492063616e6e6f74206c69652e204062697462756734329e06010000000000225120a37c3903c8d0db6512e2b40b0dffa05e5a3ab73603ce8c9c4b7771e5412328f90140a60c383f71bac0ec919b1d7dbc3eb72dd56e7aa99583615564f9f99b8ae4e837b758773a5b2e4c51348854c8389f008e05029db7f464a5ff2e01d5e6e626174affd30a00")},
529 [ + - ]: 1 : /*spent_outputs*/ spent_outputs,
530 : : /*amount*/ 88480,
531 : : /*input_index*/ 0,
532 : : /*is_taproot*/ true);
533 : 1 : }
534 : :
535 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(logging_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
536 : : {
537 : 1 : btck_LoggingOptions logging_options = {
538 : : .log_timestamps = true,
539 : : .log_time_micros = true,
540 : : .log_threadnames = false,
541 : : .log_sourcelocations = false,
542 : : .always_print_category_levels = true,
543 : : };
544 : :
545 : 1 : logging_set_options(logging_options);
546 : 1 : logging_set_level_category(LogCategory::BENCH, LogLevel::TRACE_LEVEL);
547 : 1 : logging_disable_category(LogCategory::BENCH);
548 : 1 : logging_enable_category(LogCategory::VALIDATION);
549 : 1 : logging_disable_category(LogCategory::VALIDATION);
550 : :
551 : : // Check that connecting, connecting another, and then disconnecting and connecting a logger again works.
552 : 1 : {
553 : 1 : logging_set_level_category(LogCategory::KERNEL, LogLevel::TRACE_LEVEL);
554 : 1 : logging_enable_category(LogCategory::KERNEL);
555 [ + - ]: 1 : Logger logger{std::make_unique<TestLog>()};
556 [ + - + - : 1 : Logger logger_2{std::make_unique<TestLog>()};
+ - ]
557 [ + - ]: 1 : }
558 [ + - + - ]: 1 : Logger logger{std::make_unique<TestLog>()};
559 : 1 : }
560 : :
561 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_context_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
562 : : {
563 : 1 : { // test default context
564 : 1 : Context context{};
565 [ + - ]: 1 : Context context2{};
566 [ + - + - : 2 : CheckHandle(context, context2);
+ - ]
567 : 1 : }
568 : :
569 : 1 : { // test with context options, but not options set
570 : 1 : ContextOptions options{};
571 [ + - ]: 1 : Context context{options};
572 [ + - ]: 1 : }
573 : :
574 : 1 : { // test with context options
575 : 1 : ContextOptions options{};
576 [ + - ]: 1 : ChainParams params{ChainType::MAINNET};
577 [ + - ]: 1 : ChainParams regtest_params{ChainType::REGTEST};
578 [ + - + - : 2 : CheckHandle(params, regtest_params);
+ - ]
579 [ + - ]: 1 : options.SetChainParams(params);
580 [ + - + - ]: 1 : options.SetNotifications(std::make_shared<TestKernelNotifications>());
581 [ + - ]: 1 : Context context{options};
582 [ + - ]: 1 : }
583 : 1 : }
584 : :
585 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_block)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
586 : : {
587 [ - + + - ]: 1 : Block block{hex_string_to_byte_vec(REGTEST_BLOCK_DATA[0])};
588 [ + - + - ]: 2 : Block block_100{hex_string_to_byte_vec(REGTEST_BLOCK_DATA[100])};
589 [ + - + - : 2 : CheckHandle(block, block_100);
+ - ]
590 [ + - + - ]: 2 : Block block_tx{hex_string_to_byte_vec(REGTEST_BLOCK_DATA[205])};
591 [ + - + - ]: 1 : CheckRange(block_tx.Transactions(), block_tx.CountTransactions());
592 [ + - ]: 1 : auto invalid_data = hex_string_to_byte_vec("012300");
593 [ + - - + : 2 : BOOST_CHECK_THROW(Block{invalid_data}, std::runtime_error);
- + - - -
- - + + -
+ - ]
594 [ + - ]: 1 : auto empty_data = hex_string_to_byte_vec("");
595 [ + - - + : 2 : BOOST_CHECK_THROW(Block{empty_data}, std::runtime_error);
- + - - -
- - + + -
+ - ]
596 : 1 : }
597 : :
598 : 7 : Context create_context(std::shared_ptr<TestKernelNotifications> notifications, ChainType chain_type, std::shared_ptr<TestValidationInterface> validation_interface = nullptr)
599 : : {
600 : 7 : ContextOptions options{};
601 [ + - ]: 7 : ChainParams params{chain_type};
602 [ + - ]: 7 : options.SetChainParams(params);
603 [ + - + - ]: 14 : options.SetNotifications(notifications);
604 [ + + ]: 7 : if (validation_interface) {
605 [ + - + - ]: 3 : options.SetValidationInterface(validation_interface);
606 : : }
607 [ + - ]: 7 : auto context{Context{options}};
608 : 7 : return context;
609 [ + - ]: 14 : }
610 : :
611 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_chainman_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
612 : : {
613 [ + - ]: 1 : Logger logger{std::make_unique<TestLog>()};
614 [ + - + - ]: 1 : auto test_directory{TestDirectory{"chainman_test_bitcoin_kernel"}};
615 : :
616 : 1 : { // test with default context
617 [ + - ]: 1 : Context context{};
618 [ - + - + : 6 : ChainstateManagerOptions chainman_opts{context, test_directory.m_directory.string(), (test_directory.m_directory / "blocks").string()};
+ - + - -
+ + - ]
619 [ + - ]: 1 : ChainMan chainman{context, chainman_opts};
620 [ + - ]: 2 : }
621 : :
622 : 1 : { // test with default context options
623 [ + - ]: 1 : ContextOptions options{};
624 [ + - ]: 1 : Context context{options};
625 [ - + - + : 6 : ChainstateManagerOptions chainman_opts{context, test_directory.m_directory.string(), (test_directory.m_directory / "blocks").string()};
+ - + - -
+ + - ]
626 [ + - ]: 1 : ChainMan chainman{context, chainman_opts};
627 [ + - + - ]: 2 : }
628 : 1 : { // null or empty data_directory or blocks_directory are not allowed
629 [ + - ]: 1 : Context context{};
630 [ - + ]: 1 : auto valid_dir{test_directory.m_directory.string()};
631 : 1 : std::vector<std::pair<std::string_view, std::string_view>> illegal_cases{
632 : : {"", valid_dir},
633 : 1 : {valid_dir, {nullptr, 0}},
634 : : {"", ""},
635 : : {{nullptr, 0}, {nullptr, 0}},
636 [ - + + - ]: 1 : };
637 [ + - + + ]: 5 : for (auto& [data_dir, blocks_dir] : illegal_cases) {
638 [ + - - + : 8 : BOOST_CHECK_THROW(ChainstateManagerOptions(context, data_dir, blocks_dir),
- - - - -
+ + - +
- ]
639 : : std::runtime_error);
640 : 1 : };
641 : 1 : }
642 : :
643 [ + - ]: 1 : auto notifications{std::make_shared<TestKernelNotifications>()};
644 [ + - + - : 3 : auto context{create_context(notifications, ChainType::MAINNET)};
- + ]
645 : :
646 [ - + - + : 6 : ChainstateManagerOptions chainman_opts{context, test_directory.m_directory.string(), (test_directory.m_directory / "blocks").string()};
+ - + - -
+ + - ]
647 [ + - ]: 1 : chainman_opts.SetWorkerThreads(4);
648 [ + - + - : 2 : BOOST_CHECK(!chainman_opts.SetWipeDbs(/*wipe_block_tree=*/true, /*wipe_chainstate=*/false));
+ - + - ]
649 [ + - + - : 2 : BOOST_CHECK(chainman_opts.SetWipeDbs(/*wipe_block_tree=*/true, /*wipe_chainstate=*/true));
+ - + - ]
650 [ + - + - : 2 : BOOST_CHECK(chainman_opts.SetWipeDbs(/*wipe_block_tree=*/false, /*wipe_chainstate=*/true));
+ - + - ]
651 [ + - + - : 2 : BOOST_CHECK(chainman_opts.SetWipeDbs(/*wipe_block_tree=*/false, /*wipe_chainstate=*/false));
+ - + - ]
652 [ + - ]: 1 : ChainMan chainman{context, chainman_opts};
653 [ + - + - : 4 : }
+ - ]
654 : :
655 : 7 : std::unique_ptr<ChainMan> create_chainman(TestDirectory& test_directory,
656 : : bool reindex,
657 : : bool wipe_chainstate,
658 : : bool block_tree_db_in_memory,
659 : : bool chainstate_db_in_memory,
660 : : Context& context)
661 : : {
662 [ - + - + : 42 : ChainstateManagerOptions chainman_opts{context, test_directory.m_directory.string(), (test_directory.m_directory / "blocks").string()};
+ - + - -
+ + - ]
663 : :
664 [ + + ]: 7 : if (reindex) {
665 [ + - ]: 1 : chainman_opts.SetWipeDbs(/*wipe_block_tree=*/reindex, /*wipe_chainstate=*/reindex);
666 : : }
667 [ + + ]: 7 : if (wipe_chainstate) {
668 [ + - ]: 1 : chainman_opts.SetWipeDbs(/*wipe_block_tree=*/false, /*wipe_chainstate=*/wipe_chainstate);
669 : : }
670 [ + + ]: 7 : if (block_tree_db_in_memory) {
671 [ + - ]: 2 : chainman_opts.UpdateBlockTreeDbInMemory(block_tree_db_in_memory);
672 : : }
673 [ + + ]: 7 : if (chainstate_db_in_memory) {
674 [ + - ]: 2 : chainman_opts.UpdateChainstateDbInMemory(chainstate_db_in_memory);
675 : : }
676 : :
677 [ + - ]: 7 : auto chainman{std::make_unique<ChainMan>(context, chainman_opts)};
678 [ + - ]: 7 : return chainman;
679 : 7 : }
680 : :
681 : 1 : void chainman_reindex_test(TestDirectory& test_directory)
682 : : {
683 : 1 : auto notifications{std::make_shared<TestKernelNotifications>()};
684 [ + - + - : 3 : auto context{create_context(notifications, ChainType::MAINNET)};
- + ]
685 [ + - ]: 1 : auto chainman{create_chainman(test_directory, true, false, false, false, context)};
686 : :
687 : 1 : std::vector<std::string> import_files;
688 [ + - - + : 2 : BOOST_CHECK(chainman->ImportBlocks(import_files));
+ - + - +
- ]
689 : :
690 : : // Sanity check some block retrievals
691 [ + - ]: 1 : auto chain{chainman->GetChain()};
692 [ + - - + : 2 : BOOST_CHECK_THROW(chain.GetByHeight(1000), std::runtime_error);
- - - - -
+ + - +
- ]
693 [ + - ]: 1 : auto genesis_index{chain.Entries().front()};
694 [ + - + - : 2 : BOOST_CHECK(!genesis_index.GetPrevious());
+ - + - ]
695 [ + - + - ]: 2 : auto genesis_block_raw{chainman->ReadBlock(genesis_index).value().ToBytes()};
696 [ + - ]: 1 : auto first_index{chain.GetByHeight(0)};
697 [ + - + - ]: 2 : auto first_block_raw{chainman->ReadBlock(genesis_index).value().ToBytes()};
698 [ - + - + : 1 : check_equal(genesis_block_raw, first_block_raw);
+ - ]
699 [ + - ]: 1 : auto height{first_index.GetHeight()};
700 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(height, 0);
701 : :
702 [ + - + - ]: 1 : auto next_index{chain.GetByHeight(first_index.GetHeight() + 1)};
703 [ + - + - : 2 : BOOST_CHECK(chain.Contains(next_index));
+ - + - ]
704 [ + - + - ]: 2 : auto next_block_data{chainman->ReadBlock(next_index).value().ToBytes()};
705 [ + - ]: 1 : auto tip_index{chain.Entries().back()};
706 [ + - + - ]: 2 : auto tip_block_data{chainman->ReadBlock(tip_index).value().ToBytes()};
707 [ + - ]: 1 : auto second_index{chain.GetByHeight(1)};
708 [ + - ]: 2 : auto second_block{chainman->ReadBlock(second_index).value()};
709 [ + - ]: 1 : auto second_block_data{second_block.ToBytes()};
710 [ + - ]: 1 : auto second_height{second_index.GetHeight()};
711 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(second_height, 1);
712 [ - + - + : 1 : check_equal(next_block_data, tip_block_data);
+ - ]
713 [ - + - + : 1 : check_equal(next_block_data, second_block_data);
+ - ]
714 : :
715 [ + - ]: 1 : auto second_hash{second_index.GetHash()};
716 [ + - + - ]: 1 : auto another_second_index{chainman->GetBlockTreeEntry(second_hash)};
717 [ + - + - : 2 : BOOST_CHECK(another_second_index);
+ - ]
718 [ + - ]: 1 : auto another_second_height{another_second_index->GetHeight()};
719 [ + - ]: 1 : auto second_block_hash{second_block.GetHash()};
720 [ + - + - : 3 : check_equal(second_block_hash.ToBytes(), second_hash.ToBytes());
+ - ]
721 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(second_height, another_second_height);
722 [ + - ]: 2 : }
723 : :
724 : 1 : void chainman_reindex_chainstate_test(TestDirectory& test_directory)
725 : : {
726 : 1 : auto notifications{std::make_shared<TestKernelNotifications>()};
727 [ + - + - : 3 : auto context{create_context(notifications, ChainType::MAINNET)};
- + ]
728 [ + - ]: 1 : auto chainman{create_chainman(test_directory, false, true, false, false, context)};
729 : :
730 : 1 : std::vector<std::string> import_files;
731 [ + - + - : 5 : import_files.push_back((test_directory.m_directory / "blocks" / "blk00000.dat").string());
+ - + - +
- ]
732 [ + - - + : 2 : BOOST_CHECK(chainman->ImportBlocks(import_files));
+ - + - ]
733 [ + - ]: 2 : }
734 : :
735 : 1 : void chainman_mainnet_validation_test(TestDirectory& test_directory)
736 : : {
737 : 1 : auto notifications{std::make_shared<TestKernelNotifications>()};
738 [ + - ]: 1 : auto validation_interface{std::make_shared<TestValidationInterface>()};
739 [ + - + - : 4 : auto context{create_context(notifications, ChainType::MAINNET, validation_interface)};
+ - + - ]
740 [ + - ]: 1 : auto chainman{create_chainman(test_directory, false, false, false, false, context)};
741 : :
742 : : // mainnet block 1
743 [ + - ]: 1 : auto raw_block = hex_string_to_byte_vec("010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e61bc6649ffff001d01e362990101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac00000000");
744 [ - + + - ]: 1 : Block block{raw_block};
745 [ + - + - ]: 1 : TransactionView tx{block.GetTransaction(block.CountTransactions() - 1)};
746 [ + - + - : 2 : BOOST_CHECK_EQUAL(byte_span_to_hex_string_reversed(tx.Txid().ToBytes()), "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098");
+ - + - ]
747 [ + - + - : 1 : BOOST_CHECK_EQUAL(tx.CountInputs(), 1);
+ - ]
748 [ + - ]: 1 : Transaction tx2 = tx;
749 [ + - + - : 1 : BOOST_CHECK_EQUAL(tx2.CountInputs(), 1);
+ - ]
750 [ + - + - : 3 : for (auto transaction : block.Transactions()) {
+ - ]
751 [ + - + - : 1 : BOOST_CHECK_EQUAL(transaction.CountInputs(), 1);
+ - ]
752 : : }
753 [ + - ]: 2 : auto output_counts = *(block.Transactions() | std::views::transform([](const auto& tx) {
754 : 1 : return tx.CountOutputs();
755 [ + - ]: 1 : })).begin();
756 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(output_counts, 1);
757 : :
758 [ + - ]: 1 : validation_interface->m_expected_valid_block.emplace(raw_block);
759 [ + - ]: 1 : auto ser_block{block.ToBytes()};
760 [ - + - + : 1 : check_equal(ser_block, raw_block);
+ - ]
761 : 1 : bool new_block = false;
762 [ + - + - : 2 : BOOST_CHECK(chainman->ProcessBlock(block, &new_block));
+ - + - ]
763 [ + - + - ]: 2 : BOOST_CHECK(new_block);
764 : :
765 : 1 : validation_interface->m_expected_valid_block = std::nullopt;
766 : 1 : new_block = false;
767 [ + - + - ]: 2 : Block invalid_block{hex_string_to_byte_vec(REGTEST_BLOCK_DATA[REGTEST_BLOCK_DATA.size() - 1])};
768 [ + - + - : 2 : BOOST_CHECK(!chainman->ProcessBlock(invalid_block, &new_block));
+ - + - ]
769 [ + - + - : 2 : BOOST_CHECK(!new_block);
+ - ]
770 : :
771 [ + - ]: 1 : auto chain{chainman->GetChain()};
772 [ + - + - : 1 : BOOST_CHECK_EQUAL(chain.Height(), 1);
+ - ]
773 [ + - ]: 1 : auto tip{chain.Entries().back()};
774 [ + - ]: 1 : auto read_block{chainman->ReadBlock(tip)};
775 [ + - + - : 2 : BOOST_REQUIRE(read_block);
- + ]
776 [ - + + - : 1 : check_equal(read_block.value().ToBytes(), raw_block);
+ - - + +
- ]
777 : :
778 : : // Check that we can read the previous block
779 [ + - ]: 1 : BlockTreeEntry tip_2{*tip.GetPrevious()};
780 [ + - ]: 1 : Block read_block_2{*chainman->ReadBlock(tip_2)};
781 [ + - + - : 2 : BOOST_CHECK_EQUAL(chainman->ReadBlockSpentOutputs(tip_2).Count(), 0);
+ - ]
782 [ + - + - : 2 : BOOST_CHECK_EQUAL(chainman->ReadBlockSpentOutputs(tip).Count(), 0);
+ - ]
783 : :
784 : : // It should be an error if we go another block back, since the genesis has no ancestor
785 [ + - + - : 2 : BOOST_CHECK(!tip_2.GetPrevious());
+ - + - ]
786 : :
787 : : // If we try to validate it again, it should be a duplicate
788 [ + - + - : 2 : BOOST_CHECK(chainman->ProcessBlock(block, &new_block));
+ - + - ]
789 [ + - + - ]: 2 : BOOST_CHECK(!new_block);
790 [ + - + - ]: 3 : }
791 : :
792 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_chainman_mainnet_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
793 : : {
794 [ + - ]: 1 : auto test_directory{TestDirectory{"mainnet_test_bitcoin_kernel"}};
795 [ + - ]: 1 : chainman_mainnet_validation_test(test_directory);
796 [ + - ]: 1 : chainman_reindex_test(test_directory);
797 [ + - ]: 1 : chainman_reindex_chainstate_test(test_directory);
798 : 1 : }
799 : :
800 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_block_hash_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
801 : : {
802 : 1 : std::array<std::byte, 32> test_hash;
803 : 1 : std::array<std::byte, 32> test_hash_2;
804 [ + + ]: 33 : for (int i = 0; i < 32; ++i) {
805 : 32 : test_hash[i] = static_cast<std::byte>(i);
806 : 32 : test_hash_2[i] = static_cast<std::byte>(i + 1);
807 : : }
808 : 1 : BlockHash block_hash{test_hash};
809 [ + - ]: 1 : BlockHash block_hash_2{test_hash_2};
810 [ + - + - : 2 : BOOST_CHECK(block_hash != block_hash_2);
+ - + - ]
811 [ + - + - : 2 : BOOST_CHECK(block_hash == block_hash);
+ - + - ]
812 [ + - + - : 2 : CheckHandle(block_hash, block_hash_2);
+ - ]
813 : 1 : }
814 : :
815 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_block_tree_entry_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
816 : : {
817 [ + - ]: 1 : auto test_directory{TestDirectory{"block_tree_entry_test_bitcoin_kernel"}};
818 [ + - ]: 1 : auto notifications{std::make_shared<TestKernelNotifications>()};
819 [ + - + - : 3 : auto context{create_context(notifications, ChainType::REGTEST)};
- + ]
820 : 1 : auto chainman{create_chainman(
821 : : test_directory,
822 : : /*reindex=*/false,
823 : : /*wipe_chainstate=*/false,
824 : : /*block_tree_db_in_memory=*/true,
825 : : /*chainstate_db_in_memory=*/true,
826 [ + - ]: 1 : context)};
827 : :
828 : : // Process a couple of blocks
829 [ + + ]: 4 : for (size_t i{0}; i < 3; i++) {
830 [ + - + - ]: 6 : Block block{hex_string_to_byte_vec(REGTEST_BLOCK_DATA[i])};
831 : 3 : bool new_block{false};
832 [ + - ]: 3 : chainman->ProcessBlock(block, &new_block);
833 [ + - + - ]: 6 : BOOST_CHECK(new_block);
834 : 3 : }
835 : :
836 [ + - ]: 1 : auto chain{chainman->GetChain()};
837 [ + - ]: 1 : auto entry_0{chain.GetByHeight(0)};
838 [ + - ]: 1 : auto entry_1{chain.GetByHeight(1)};
839 [ + - ]: 1 : auto entry_2{chain.GetByHeight(2)};
840 : :
841 : : // Test inequality
842 [ + - + - : 2 : BOOST_CHECK(entry_0 != entry_1);
+ - + - ]
843 [ + - + - : 2 : BOOST_CHECK(entry_1 != entry_2);
+ - + - ]
844 [ + - + - : 2 : BOOST_CHECK(entry_0 != entry_2);
+ - + - ]
845 : :
846 : : // Test equality with same entry
847 [ + - + - : 3 : BOOST_CHECK(entry_0 == chain.GetByHeight(0));
+ - + - ]
848 [ + - + - : 2 : BOOST_CHECK(entry_0 == BlockTreeEntry{entry_0});
+ - + - ]
849 [ + - + - : 2 : BOOST_CHECK(entry_1 == entry_1);
+ - + - ]
850 : :
851 : : // Test GetPrevious
852 [ + - ]: 1 : auto prev{entry_1.GetPrevious()};
853 [ + - + - : 2 : BOOST_CHECK(prev.has_value());
+ - ]
854 [ + - + - : 2 : BOOST_CHECK(prev.value() == entry_0);
+ - + - ]
855 [ + - ]: 2 : }
856 : :
857 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_chainman_in_memory_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
858 : : {
859 [ + - ]: 1 : auto in_memory_test_directory{TestDirectory{"in-memory_test_bitcoin_kernel"}};
860 : :
861 [ + - ]: 1 : auto notifications{std::make_shared<TestKernelNotifications>()};
862 [ + - + - : 3 : auto context{create_context(notifications, ChainType::REGTEST)};
- + ]
863 [ + - ]: 1 : auto chainman{create_chainman(in_memory_test_directory, false, false, true, true, context)};
864 : :
865 [ + + ]: 207 : for (auto& raw_block : REGTEST_BLOCK_DATA) {
866 [ + - + - ]: 412 : Block block{hex_string_to_byte_vec(raw_block)};
867 : 206 : bool new_block{false};
868 [ + - ]: 206 : chainman->ProcessBlock(block, &new_block);
869 [ + - + - ]: 412 : BOOST_CHECK(new_block);
870 : 206 : }
871 : :
872 [ + - + - : 4 : BOOST_CHECK(std::filesystem::exists(in_memory_test_directory.m_directory / "blocks"));
+ - + - +
- + - ]
873 [ + - + - : 6 : BOOST_CHECK(!std::filesystem::exists(in_memory_test_directory.m_directory / "blocks" / "index"));
+ - + - +
- + - + -
+ - ]
874 [ + - + - : 4 : BOOST_CHECK(!std::filesystem::exists(in_memory_test_directory.m_directory / "chainstate"));
+ - + - +
- + - ]
875 : :
876 [ + - + - : 2 : BOOST_CHECK(context.interrupt());
+ - ]
877 [ + - ]: 2 : }
878 : :
879 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_chainman_regtest_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
880 : : {
881 [ + - ]: 1 : auto test_directory{TestDirectory{"regtest_test_bitcoin_kernel"}};
882 : :
883 [ + - ]: 1 : auto notifications{std::make_shared<TestKernelNotifications>()};
884 [ + - + - : 3 : auto context{create_context(notifications, ChainType::REGTEST)};
- + ]
885 : :
886 : : // Validate 206 regtest blocks in total.
887 : : // Stop halfway to check that it is possible to continue validating starting
888 : : // from prior state.
889 : 1 : const size_t mid{REGTEST_BLOCK_DATA.size() / 2};
890 : :
891 : 1 : {
892 [ + - ]: 1 : auto chainman{create_chainman(test_directory, false, false, false, false, context)};
893 [ + + ]: 104 : for (size_t i{0}; i < mid; i++) {
894 [ + - + - ]: 206 : Block block{hex_string_to_byte_vec(REGTEST_BLOCK_DATA[i])};
895 : 103 : bool new_block{false};
896 [ + - + - : 206 : BOOST_CHECK(chainman->ProcessBlock(block, &new_block));
+ - + - ]
897 [ + - + - ]: 206 : BOOST_CHECK(new_block);
898 : 103 : }
899 : 1 : }
900 : :
901 [ + - ]: 1 : auto chainman{create_chainman(test_directory, false, false, false, false, context)};
902 : :
903 [ + + ]: 104 : for (size_t i{mid}; i < REGTEST_BLOCK_DATA.size(); i++) {
904 [ + - + - ]: 206 : Block block{hex_string_to_byte_vec(REGTEST_BLOCK_DATA[i])};
905 : 103 : bool new_block{false};
906 [ + - + - : 206 : BOOST_CHECK(chainman->ProcessBlock(block, &new_block));
+ - + - ]
907 [ + - + - ]: 206 : BOOST_CHECK(new_block);
908 : 103 : }
909 : :
910 [ + - ]: 1 : auto chain = chainman->GetChain();
911 [ + - ]: 1 : auto tip = chain.Entries().back();
912 [ + - ]: 2 : auto read_block = chainman->ReadBlock(tip).value();
913 [ + - + - : 3 : check_equal(read_block.ToBytes(), hex_string_to_byte_vec(REGTEST_BLOCK_DATA[REGTEST_BLOCK_DATA.size() - 1]));
- + + - ]
914 : :
915 [ + - ]: 1 : auto tip_2 = tip.GetPrevious().value();
916 [ + - ]: 2 : auto read_block_2 = chainman->ReadBlock(tip_2).value();
917 [ + - + - : 3 : check_equal(read_block_2.ToBytes(), hex_string_to_byte_vec(REGTEST_BLOCK_DATA[REGTEST_BLOCK_DATA.size() - 2]));
- + + - ]
918 : :
919 [ + - + - ]: 1 : Txid txid = read_block.Transactions()[0].Txid();
920 [ + - + - ]: 1 : Txid txid_2 = read_block_2.Transactions()[0].Txid();
921 [ + - + - : 2 : BOOST_CHECK(txid != txid_2);
+ - + - ]
922 [ + - + - : 2 : BOOST_CHECK(txid == txid);
+ - + - ]
923 [ + - + - : 2 : CheckHandle(txid, txid_2);
+ - ]
924 : :
925 : 27 : auto find_transaction = [&chainman](const TxidView& target_txid) -> std::optional<Transaction> {
926 : 26 : auto chain = chainman->GetChain();
927 [ + - ]: 4730 : for (const auto block_tree_entry : chain.Entries()) {
928 : 2365 : auto block{chainman->ReadBlock(block_tree_entry)};
929 [ + - + - : 11933 : for (const TransactionView transaction : block->Transactions()) {
+ - ]
930 [ + - + + ]: 4890 : if (transaction.Txid() == target_txid) {
931 [ + - ]: 26 : return Transaction{transaction};
932 : : }
933 : : }
934 : 2365 : }
935 : 0 : return std::nullopt;
936 : 1 : };
937 : :
938 [ + - + - : 415 : for (const auto block_tree_entry : chain.Entries()) {
+ - ]
939 [ + - ]: 207 : auto block{chainman->ReadBlock(block_tree_entry)};
940 [ + - + - : 878 : for (const auto transaction : block->Transactions()) {
+ - ]
941 : 232 : std::vector<TransactionInput> inputs;
942 : 232 : std::vector<TransactionOutput> spent_outputs;
943 [ + - + - : 698 : for (const auto input : transaction.Inputs()) {
+ - ]
944 [ + - ]: 233 : OutPointView point = input.OutPoint();
945 [ + - + + ]: 233 : if (point.index() == std::numeric_limits<uint32_t>::max()) {
946 : 207 : continue;
947 : : }
948 [ + - ]: 26 : inputs.emplace_back(input);
949 [ + - + - : 78 : BOOST_CHECK(point.Txid() != transaction.Txid());
+ - + - +
- ]
950 [ + - + - ]: 26 : std::optional<Transaction> tx = find_transaction(point.Txid());
951 [ + - + - : 52 : BOOST_CHECK(tx.has_value());
+ - ]
952 [ + - + - : 78 : BOOST_CHECK(point.Txid() == tx->Txid());
+ - + - +
- ]
953 [ + - + - : 26 : spent_outputs.emplace_back(tx->GetOutput(point.index()));
+ - ]
954 : 26 : }
955 [ + - - + : 464 : BOOST_CHECK(inputs.size() == spent_outputs.size());
- + + - ]
956 : 232 : ScriptVerifyStatus status = ScriptVerifyStatus::OK;
957 [ - + + + ]: 258 : for (size_t i{0}; i < inputs.size(); ++i) {
958 [ + - + - : 52 : BOOST_CHECK(spent_outputs[i].GetScriptPubkey().Verify(spent_outputs[i].Amount(), transaction, spent_outputs, i, ScriptVerificationFlags::ALL, status));
- + + - +
- + - +
- ]
959 : : }
960 : 232 : }
961 : 207 : }
962 : :
963 : : // Read spent outputs for current tip and its previous block
964 [ + - ]: 1 : BlockSpentOutputs block_spent_outputs{chainman->ReadBlockSpentOutputs(tip)};
965 [ + - + - ]: 1 : BlockSpentOutputs block_spent_outputs_prev{chainman->ReadBlockSpentOutputs(*tip.GetPrevious())};
966 [ + - + - : 2 : CheckHandle(block_spent_outputs, block_spent_outputs_prev);
+ - ]
967 [ + - + - ]: 1 : CheckRange(block_spent_outputs_prev.TxsSpentOutputs(), block_spent_outputs_prev.Count());
968 [ + - + - : 1 : BOOST_CHECK_EQUAL(block_spent_outputs.Count(), 1);
+ - ]
969 : :
970 : : // Get transaction spent outputs from the last transaction in the two blocks
971 [ + - + - ]: 1 : TransactionSpentOutputsView transaction_spent_outputs{block_spent_outputs.GetTxSpentOutputs(block_spent_outputs.Count() - 1)};
972 [ + - ]: 1 : TransactionSpentOutputs owned_transaction_spent_outputs{transaction_spent_outputs};
973 [ + - + - ]: 1 : TransactionSpentOutputs owned_transaction_spent_outputs_prev{block_spent_outputs_prev.GetTxSpentOutputs(block_spent_outputs_prev.Count() - 1)};
974 [ + - + - : 2 : CheckHandle(owned_transaction_spent_outputs, owned_transaction_spent_outputs_prev);
+ - ]
975 [ + - + - ]: 1 : CheckRange(transaction_spent_outputs.Coins(), transaction_spent_outputs.Count());
976 : :
977 : : // Get the last coin from the transaction spent outputs
978 [ + - + - ]: 1 : CoinView coin{transaction_spent_outputs.GetCoin(transaction_spent_outputs.Count() - 1)};
979 [ + - + - : 2 : BOOST_CHECK(!coin.IsCoinbase());
+ - + - ]
980 [ + - ]: 1 : Coin owned_coin{coin};
981 [ + - + - ]: 1 : Coin owned_coin_prev{owned_transaction_spent_outputs_prev.GetCoin(owned_transaction_spent_outputs_prev.Count() - 1)};
982 [ + - + - : 2 : CheckHandle(owned_coin, owned_coin_prev);
+ - ]
983 : :
984 : : // Validate coin properties
985 [ + - ]: 1 : TransactionOutputView output = coin.GetOutput();
986 [ + - ]: 1 : uint32_t coin_height = coin.GetConfirmationHeight();
987 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(coin_height, 205);
988 [ + - + - : 1 : BOOST_CHECK_EQUAL(output.Amount(), 100000000);
+ - ]
989 : :
990 : : // Test script pubkey serialization
991 [ + - ]: 1 : auto script_pubkey = output.GetScriptPubkey();
992 [ + - ]: 1 : auto script_pubkey_bytes{script_pubkey.ToBytes()};
993 [ + - - + : 1 : BOOST_CHECK_EQUAL(script_pubkey_bytes.size(), 22);
+ - ]
994 [ - + + - ]: 1 : auto round_trip_script_pubkey{ScriptPubkey(script_pubkey_bytes)};
995 [ + - + - : 1 : BOOST_CHECK_EQUAL(round_trip_script_pubkey.ToBytes().size(), 22);
- + + - ]
996 : :
997 [ + - + - : 3 : for (const auto tx_spent_outputs : block_spent_outputs.TxsSpentOutputs()) {
+ - ]
998 [ + - + - : 6 : for (const auto coins : tx_spent_outputs.Coins()) {
+ - ]
999 [ + - + - : 4 : BOOST_CHECK_GT(coins.GetOutput().Amount(), 1);
+ - ]
1000 : : }
1001 : : }
1002 : :
1003 [ + - + - ]: 1 : CheckRange(chain.Entries(), chain.CountEntries());
1004 : :
1005 [ + - + - : 415 : for (const BlockTreeEntry entry : chain.Entries()) {
+ - ]
1006 [ + - ]: 207 : std::optional<Block> block{chainman->ReadBlock(entry)};
1007 [ + - ]: 207 : if (block) {
1008 [ + - + - : 878 : for (const TransactionView transaction : block->Transactions()) {
+ - ]
1009 [ + - + - : 1390 : for (const TransactionOutputView output : transaction.Outputs()) {
+ - ]
1010 : : // skip data carrier outputs
1011 [ + - + + ]: 926 : if ((unsigned char)output.GetScriptPubkey().ToBytes()[0] == 0x6a) {
1012 : 206 : continue;
1013 : : }
1014 [ + - + - : 463 : BOOST_CHECK_GT(output.Amount(), 1);
+ - ]
1015 : : }
1016 : : }
1017 : : }
1018 : 207 : }
1019 : :
1020 : 1 : int32_t count{0};
1021 [ + - + - : 415 : for (const auto entry : chain.Entries()) {
+ - ]
1022 [ + - + - : 207 : BOOST_CHECK_EQUAL(entry.GetHeight(), count);
+ - ]
1023 : 207 : ++count;
1024 : : }
1025 [ + - + - : 1 : BOOST_CHECK_EQUAL(count, chain.CountEntries());
+ - ]
1026 : :
1027 : :
1028 [ + - + - : 4 : std::filesystem::remove_all(test_directory.m_directory / "blocks" / "blk00000.dat");
+ - + - +
- ]
1029 [ + - + - : 2 : BOOST_CHECK(!chainman->ReadBlock(tip_2).has_value());
+ - + - ]
1030 [ + - + - : 4 : std::filesystem::remove_all(test_directory.m_directory / "blocks" / "rev00000.dat");
+ - + - +
- ]
1031 [ + - - + : 2 : BOOST_CHECK_THROW(chainman->ReadBlockSpentOutputs(tip), std::runtime_error);
- - - - -
+ + - +
- ]
1032 [ + - ]: 2 : }
|