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 : : #include <util/byte_units.h>
8 : : #include <util/fs.h>
9 : :
10 : : #define BOOST_TEST_MODULE Bitcoin Kernel Test Suite
11 : : #include <boost/test/included/unit_test.hpp>
12 : :
13 : : #include <test/kernel/block_data.h>
14 : : #include <test/util/common.h>
15 : :
16 : : #include <charconv>
17 : : #include <cstdint>
18 : : #include <cstdlib>
19 : : #include <iostream>
20 : : #include <memory>
21 : : #include <optional>
22 : : #include <random>
23 : : #include <ranges>
24 : : #include <span>
25 : : #include <string>
26 : : #include <string_view>
27 : : #include <vector>
28 : :
29 : : using namespace btck;
30 : :
31 : 5 : std::string random_string(uint32_t length)
32 : : {
33 : 5 : const std::string chars = "0123456789"
34 : : "abcdefghijklmnopqrstuvwxyz"
35 : 5 : "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
36 : :
37 [ + + + - : 6 : static std::random_device rd;
+ - ]
38 [ + + + - : 5 : static std::default_random_engine dre{rd()};
+ - ]
39 [ + + + - : 5 : static std::uniform_int_distribution<> distribution(0, chars.size() - 1);
- + ]
40 : :
41 [ + - ]: 5 : std::string random;
42 [ + - ]: 5 : random.reserve(length);
43 [ + + ]: 85 : for (uint32_t i = 0; i < length; i++) {
44 [ + - ]: 160 : random += chars[distribution(dre)];
45 : : }
46 : 5 : return random;
47 : 5 : }
48 : :
49 : 673 : std::vector<std::byte> hex_string_to_byte_vec(std::string_view hex)
50 : : {
51 : 673 : std::vector<std::byte> bytes;
52 [ + - ]: 673 : bytes.reserve(hex.length() / 2);
53 : :
54 : 203614 : for (size_t i{0}; i < hex.length(); i += 2) {
55 : 202941 : uint8_t byte_value;
56 : 202941 : auto [ptr, ec] = std::from_chars(hex.data() + i, hex.data() + i + 2, byte_value, 16);
57 : :
58 [ + - - + ]: 202941 : if (ec != std::errc{} || ptr != hex.data() + i + 2) {
59 [ # # ]: 0 : throw std::invalid_argument("Invalid hex character");
60 : : }
61 [ + - + + ]: 406555 : bytes.push_back(static_cast<std::byte>(byte_value));
62 : : }
63 : 673 : return bytes;
64 : 0 : }
65 : :
66 : 5 : std::string byte_span_to_hex_string_reversed(std::span<const std::byte> bytes)
67 : : {
68 : 5 : std::ostringstream oss;
69 : :
70 : : // Iterate in reverse order
71 [ + + ]: 165 : for (auto it = bytes.rbegin(); it != bytes.rend(); ++it) {
72 [ + - ]: 160 : oss << std::hex << std::setw(2) << std::setfill('0')
73 [ + - ]: 160 : << static_cast<unsigned int>(static_cast<uint8_t>(*it));
74 : : }
75 : :
76 [ + - ]: 10 : return oss.str();
77 : 5 : }
78 : :
79 : : constexpr auto VERIFY_ALL_PRE_SEGWIT{ScriptVerificationFlags::P2SH | ScriptVerificationFlags::DERSIG |
80 : : ScriptVerificationFlags::NULLDUMMY | ScriptVerificationFlags::CHECKLOCKTIMEVERIFY |
81 : : ScriptVerificationFlags::CHECKSEQUENCEVERIFY};
82 : : constexpr auto VERIFY_ALL_PRE_TAPROOT{VERIFY_ALL_PRE_SEGWIT | ScriptVerificationFlags::WITNESS};
83 : :
84 : 50 : void check_equal(std::span<const std::byte> _actual, std::span<const std::byte> _expected, bool equal = true)
85 : : {
86 : 50 : std::span<const uint8_t> actual{reinterpret_cast<const unsigned char*>(_actual.data()), _actual.size()};
87 : 50 : std::span<const uint8_t> expected{reinterpret_cast<const unsigned char*>(_expected.data()), _expected.size()};
88 [ + - + - ]: 100 : BOOST_CHECK_EQUAL_COLLECTIONS(
89 : : actual.begin(), actual.end(),
90 : : expected.begin(), expected.end());
91 : 50 : }
92 : :
93 : : class TestLog
94 : : {
95 : : public:
96 : 125 : void LogMessage(std::string_view message)
97 : : {
98 : 125 : std::cout << "kernel: " << message;
99 : 125 : }
100 : : };
101 : :
102 : : struct TestDirectory {
103 : : fs::path m_directory;
104 : 5 : TestDirectory(std::string directory_name)
105 [ + - + - : 35 : : m_directory{fs::path{fs::temp_directory_path()} / fs::u8path(directory_name + "_🌽_" + random_string(16))}
+ - + - +
- ]
106 : : {
107 [ + - ]: 5 : fs::create_directories(m_directory);
108 : 5 : }
109 : :
110 : 5 : ~TestDirectory()
111 : : {
112 : 5 : fs::remove_all(m_directory);
113 : 5 : }
114 : : };
115 : :
116 : 8 : class TestKernelNotifications : public KernelNotifications
117 : : {
118 : : public:
119 : 420 : void HeaderTipHandler(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) override
120 : : {
121 [ + - ]: 420 : BOOST_CHECK_GT(timestamp, 0);
122 : 420 : }
123 : :
124 : 0 : void FlushErrorHandler(std::string_view error) override
125 : : {
126 : 0 : std::cout << error << std::endl;
127 : 0 : }
128 : :
129 : 0 : void FatalErrorHandler(std::string_view error) override
130 : : {
131 : 0 : std::cout << error << std::endl;
132 : 0 : }
133 : : };
134 : :
135 : 1 : class TestValidationInterface : public ValidationInterface
136 : : {
137 : : public:
138 : : std::optional<std::vector<std::byte>> m_expected_valid_block = std::nullopt;
139 : :
140 : 3 : void BlockChecked(Block block, BlockValidationStateView state) override
141 : : {
142 [ + + ]: 3 : if (m_expected_valid_block.has_value()) {
143 : 1 : auto ser_block{block.ToBytes()};
144 [ - + + - : 1 : check_equal(m_expected_valid_block.value(), ser_block);
- + + - ]
145 : 1 : }
146 : :
147 : 3 : auto mode{state.GetValidationMode()};
148 [ + + - - ]: 3 : switch (mode) {
149 : 2 : case ValidationMode::VALID: {
150 : 2 : std::cout << "Valid block" << std::endl;
151 : 2 : return;
152 : : }
153 : 1 : case ValidationMode::INVALID: {
154 : 1 : std::cout << "Invalid block: ";
155 : 1 : auto result{state.GetBlockValidationResult()};
156 [ - - - - : 1 : switch (result) {
+ - - - -
- ]
157 : 0 : case BlockValidationResult::UNSET:
158 : 0 : std::cout << "initial value. Block has not yet been rejected" << std::endl;
159 : 0 : break;
160 : 0 : case BlockValidationResult::HEADER_LOW_WORK:
161 : 0 : std::cout << "the block header may be on a too-little-work chain" << std::endl;
162 : 0 : break;
163 : 0 : case BlockValidationResult::CONSENSUS:
164 : 0 : std::cout << "invalid by consensus rules (excluding any below reasons)" << std::endl;
165 : 0 : break;
166 : 0 : case BlockValidationResult::CACHED_INVALID:
167 : 0 : std::cout << "this block was cached as being invalid and we didn't store the reason why" << std::endl;
168 : 0 : break;
169 : 1 : case BlockValidationResult::INVALID_HEADER:
170 : 1 : std::cout << "invalid proof of work or time too old" << std::endl;
171 : 1 : break;
172 : 0 : case BlockValidationResult::MUTATED:
173 : 0 : std::cout << "the block's data didn't match the data committed to by the PoW" << std::endl;
174 : 0 : break;
175 : 0 : case BlockValidationResult::MISSING_PREV:
176 : 0 : std::cout << "We don't have the previous block the checked one is built on" << std::endl;
177 : 0 : break;
178 : 0 : case BlockValidationResult::INVALID_PREV:
179 : 0 : std::cout << "A block this one builds on is invalid" << std::endl;
180 : 0 : break;
181 : 0 : case BlockValidationResult::TIME_FUTURE:
182 : 0 : std::cout << "block timestamp was > 2 hours in the future (or our clock is bad)" << std::endl;
183 : 0 : break;
184 : : }
185 : : return;
186 : : }
187 : 0 : case ValidationMode::INTERNAL_ERROR: {
188 : 0 : std::cout << "Internal error" << std::endl;
189 : 0 : return;
190 : : }
191 : : }
192 : : }
193 : :
194 : 2 : void BlockConnected(Block block, BlockTreeEntry entry) override
195 : : {
196 : 2 : std::cout << "Block connected." << std::endl;
197 : 2 : }
198 : :
199 : 0 : void PowValidBlock(BlockTreeEntry entry, Block block) override
200 : : {
201 : 0 : std::cout << "Block passed pow verification" << std::endl;
202 : 0 : }
203 : :
204 : 0 : void BlockDisconnected(Block block, BlockTreeEntry entry) override
205 : : {
206 : 0 : std::cout << "Block disconnected." << std::endl;
207 : 0 : }
208 : : };
209 : :
210 : 7 : void run_verify_test(
211 : : const ScriptPubkey& spent_script_pubkey,
212 : : const Transaction& spending_tx,
213 : : const PrecomputedTransactionData* precomputed_txdata,
214 : : int64_t amount,
215 : : unsigned int input_index,
216 : : bool taproot)
217 : : {
218 : 7 : auto status = ScriptVerifyStatus::OK;
219 : :
220 [ + + ]: 7 : if (taproot) {
221 [ + - + - ]: 6 : BOOST_CHECK(spent_script_pubkey.Verify(
222 : : amount,
223 : : spending_tx,
224 : : precomputed_txdata,
225 : : input_index,
226 : : ScriptVerificationFlags::ALL,
227 : : status));
228 [ + - ]: 6 : BOOST_CHECK(status == ScriptVerifyStatus::OK);
229 : : } else {
230 [ + - + - ]: 8 : BOOST_CHECK(!spent_script_pubkey.Verify(
231 : : amount,
232 : : spending_tx,
233 : : precomputed_txdata,
234 : : input_index,
235 : : ScriptVerificationFlags::ALL,
236 : : status));
237 [ + - ]: 8 : BOOST_CHECK(status == ScriptVerifyStatus::ERROR_SPENT_OUTPUTS_REQUIRED);
238 : : }
239 : :
240 [ + - + - ]: 14 : BOOST_CHECK(spent_script_pubkey.Verify(
241 : : amount,
242 : : spending_tx,
243 : : precomputed_txdata,
244 : : input_index,
245 : : VERIFY_ALL_PRE_TAPROOT,
246 : : status));
247 [ + - ]: 14 : BOOST_CHECK(status == ScriptVerifyStatus::OK);
248 : :
249 [ + - + - ]: 14 : BOOST_CHECK(spent_script_pubkey.Verify(
250 : : 0,
251 : : spending_tx,
252 : : precomputed_txdata,
253 : : input_index,
254 : : VERIFY_ALL_PRE_SEGWIT,
255 : : status));
256 [ + - ]: 14 : BOOST_CHECK(status == ScriptVerifyStatus::OK);
257 : 7 : }
258 : :
259 : : template <typename T>
260 : : concept HasToBytes = requires(T t) {
261 : : { t.ToBytes() } -> std::convertible_to<std::span<const std::byte>>;
262 : : };
263 : :
264 : : template <typename T>
265 : 18 : void CheckHandle(T object, T distinct_object)
266 : : {
267 [ + - ]: 36 : BOOST_CHECK(object.get() != nullptr);
268 [ + - ]: 36 : BOOST_CHECK(distinct_object.get() != nullptr);
269 [ + - ]: 36 : BOOST_CHECK(object.get() != distinct_object.get());
270 : :
271 : : if constexpr (HasToBytes<T>) {
272 [ + - ]: 7 : const auto object_bytes = object.ToBytes();
273 : 7 : const auto distinct_bytes = distinct_object.ToBytes();
274 [ + - + - ]: 17 : BOOST_CHECK(!std::ranges::equal(object_bytes, distinct_bytes));
275 : 4 : }
276 : :
277 : : // Copy constructor
278 : 18 : T object2(distinct_object);
279 [ + - + - : 18 : BOOST_CHECK_NE(distinct_object.get(), object2.get());
+ - ]
280 : : if constexpr (HasToBytes<T>) {
281 [ + + + - : 13 : check_equal(distinct_object.ToBytes(), object2.ToBytes());
+ + + - +
- ]
282 : : }
283 : :
284 : : // Copy assignment
285 [ + - ]: 18 : T object3{distinct_object};
286 : 18 : object2 = object3;
287 [ + - + - : 18 : BOOST_CHECK_NE(object3.get(), object2.get());
+ - ]
288 : : if constexpr (HasToBytes<T>) {
289 [ + + + - : 13 : check_equal(object3.ToBytes(), object2.ToBytes());
+ + + - +
- ]
290 : : }
291 : :
292 : : // Move constructor
293 [ + - ]: 18 : auto* original_ptr = object2.get();
294 : 18 : T object4{std::move(object2)};
295 [ + - + - ]: 18 : BOOST_CHECK_EQUAL(object4.get(), original_ptr);
296 [ + - + - : 18 : BOOST_CHECK_EQUAL(object2.get(), nullptr); // NOLINT(bugprone-use-after-move)
+ - ]
297 : : if constexpr (HasToBytes<T>) {
298 [ + + + - : 13 : check_equal(object4.ToBytes(), object3.ToBytes());
+ + + - ]
299 : : }
300 : :
301 : : // Move assignment
302 : 18 : original_ptr = object4.get();
303 : 18 : object2 = std::move(object4);
304 [ + - + - ]: 18 : BOOST_CHECK_EQUAL(object2.get(), original_ptr);
305 [ + - + - : 18 : BOOST_CHECK_EQUAL(object4.get(), nullptr); // NOLINT(bugprone-use-after-move)
+ - ]
306 : : if constexpr (HasToBytes<T>) {
307 [ + + + - : 13 : check_equal(object2.ToBytes(), object3.ToBytes());
+ + + - +
- ]
308 : : }
309 : :
310 : : // Self move-assignment must not destroy the held resource.
311 : : // Use a reference to avoid -Wself-move warnings.
312 : 18 : original_ptr = object2.get();
313 [ + - ]: 18 : auto& object2_ref = object2;
314 : 18 : object2 = std::move(object2_ref);
315 [ + - + - : 18 : BOOST_CHECK_EQUAL(object2.get(), original_ptr);
+ - ]
316 : : if constexpr (HasToBytes<T>) {
317 [ + + + - : 13 : check_equal(object2.ToBytes(), object3.ToBytes());
+ + + - ]
318 : : }
319 : 18 : }
320 : :
321 : : template <typename RangeType>
322 : : requires std::ranges::random_access_range<RangeType>
323 : 6 : void CheckRange(const RangeType& range, size_t expected_size)
324 : : {
325 : : using value_type = std::ranges::range_value_t<RangeType>;
326 : :
327 [ + - ]: 6 : BOOST_CHECK_EQUAL(range.size(), expected_size);
328 [ + - + - ]: 12 : BOOST_REQUIRE(range.size() > 0); // Some checks below assume a non-empty range
329 [ + - + - ]: 12 : BOOST_REQUIRE(!range.empty());
330 : :
331 [ + - + - : 18 : BOOST_CHECK(range.begin() != range.end());
+ - ]
332 [ + - ]: 6 : BOOST_CHECK_EQUAL(std::distance(range.begin(), range.end()), static_cast<std::ptrdiff_t>(expected_size));
333 [ + - ]: 12 : BOOST_CHECK(range.cbegin() == range.begin());
334 [ + - + - : 24 : BOOST_CHECK(range.cend() == range.end());
+ - ]
335 : :
336 [ + + ]: 221 : for (size_t i = 0; i < range.size(); ++i) {
337 [ + - ]: 215 : BOOST_CHECK_EQUAL(range[i].get(), (*(range.begin() + i)).get());
338 : : }
339 : :
340 [ + - - + : 12 : BOOST_CHECK_THROW(range.at(expected_size), std::out_of_range);
- - - - -
+ + - +
- ]
341 : :
342 [ + - ]: 6 : BOOST_CHECK_EQUAL(range.front().get(), range[0].get());
343 [ + - ]: 6 : BOOST_CHECK_EQUAL(range.back().get(), range[expected_size - 1].get());
344 : :
345 : 6 : auto it = range.begin();
346 : 6 : auto it_copy = it;
347 : 6 : ++it;
348 [ + - ]: 12 : BOOST_CHECK(it != it_copy);
349 : 6 : --it;
350 [ + - ]: 12 : BOOST_CHECK(it == it_copy);
351 : 6 : it = range.begin();
352 : 6 : auto old_it = it++;
353 [ + - ]: 12 : BOOST_CHECK(old_it == range.begin());
354 [ + - ]: 12 : BOOST_CHECK(it == range.begin() + 1);
355 : 6 : old_it = it--;
356 [ + - ]: 12 : BOOST_CHECK(old_it == range.begin() + 1);
357 [ + - ]: 12 : BOOST_CHECK(it == range.begin());
358 : :
359 : 6 : it = range.begin();
360 : 6 : it += 2;
361 [ + - ]: 12 : BOOST_CHECK(it == range.begin() + 2);
362 : 6 : it -= 2;
363 [ + - ]: 12 : BOOST_CHECK(it == range.begin());
364 : :
365 [ + - + - : 18 : BOOST_CHECK(range.begin() < range.end());
+ - ]
366 [ + - + - : 18 : BOOST_CHECK(range.begin() <= range.end());
+ - ]
367 [ + - + - ]: 18 : BOOST_CHECK(range.end() > range.begin());
368 [ + - + - ]: 18 : BOOST_CHECK(range.end() >= range.begin());
369 [ + - ]: 12 : BOOST_CHECK(range.begin() == range.begin());
370 : :
371 [ + - ]: 6 : BOOST_CHECK_EQUAL(range.begin()[0].get(), range[0].get());
372 : :
373 : 6 : size_t count = 0;
374 [ + - ]: 221 : for (auto rit = range.end(); rit != range.begin();) {
375 : 215 : --rit;
376 : 215 : ++count;
377 : : }
378 [ + - ]: 6 : BOOST_CHECK_EQUAL(count, expected_size);
379 : :
380 : 6 : std::vector<value_type> collected;
381 [ + - + - : 436 : for (const auto& elem : range) {
+ - ]
382 [ + - ]: 215 : collected.push_back(elem);
383 : : }
384 [ + - + - ]: 12 : BOOST_CHECK_EQUAL(collected.size(), expected_size);
385 : :
386 [ + - + - : 12 : BOOST_CHECK_EQUAL(std::ranges::size(range), expected_size);
+ - ]
387 : :
388 [ + - ]: 6 : it = range.begin();
389 : 6 : auto it2 = 1 + it;
390 [ + - + - ]: 12 : BOOST_CHECK(it2 == it + 1);
391 : 6 : }
392 : :
393 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_transaction_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
394 : : {
395 : 1 : auto tx_data{hex_string_to_byte_vec("02000000013f7cebd65c27431a90bba7f796914fe8cc2ddfc3f2cbd6f7e5f2fc854534da95000000006b483045022100de1ac3bcdfb0332207c4a91f3832bd2c2915840165f876ab47c5f8996b971c3602201c6c053d750fadde599e6f5c4e1963df0f01fc0d97815e8157e3d59fe09ca30d012103699b464d1d8bc9e47d4fb1cdaa89a1c5783d68363c4dbc4b524ed3d857148617feffffff02836d3c01000000001976a914fc25d6d5c94003bf5b0c7b640a248e2c637fcfb088ac7ada8202000000001976a914fbed3d9b11183209a57999d54d59f67c019e756c88ac6acb0700")};
396 [ - + + - ]: 1 : auto tx{Transaction{tx_data}};
397 [ + - ]: 1 : auto tx_data_2{hex_string_to_byte_vec("02000000000101904f4ee5c87d20090b642f116e458cd6693292ad9ece23e72f15fb6c05b956210500000000fdffffff02e2010000000000002251200839a723933b56560487ec4d67dda58f09bae518ffa7e148313c5696ac837d9f10060000000000002251205826bcdae7abfb1c468204170eab00d887b61ab143464a4a09e1450bdc59a3340140f26e7af574e647355830772946356c27e7bbc773c5293688890f58983499581be84de40be7311a14e6d6422605df086620e75adae84ff06b75ce5894de5e994a00000000")};
398 [ - + + - ]: 1 : auto tx2{Transaction{tx_data_2}};
399 [ + - + - : 2 : CheckHandle(tx, tx2);
+ - ]
400 : :
401 [ + - ]: 1 : auto invalid_data = hex_string_to_byte_vec("012300");
402 [ + - - + : 2 : BOOST_CHECK_THROW(Transaction{invalid_data}, std::runtime_error);
- + - - -
- - + + -
+ - ]
403 [ + - ]: 1 : auto empty_data = hex_string_to_byte_vec("");
404 [ + - - + : 2 : BOOST_CHECK_THROW(Transaction{empty_data}, std::runtime_error);
- + - - -
- - + + -
+ - ]
405 : :
406 [ + - + - : 1 : BOOST_CHECK_EQUAL(tx.CountOutputs(), 2);
+ - ]
407 [ + - + - : 1 : BOOST_CHECK_EQUAL(tx.CountInputs(), 1);
+ - ]
408 [ + - + - : 1 : BOOST_CHECK_EQUAL(tx.GetLocktime(), 510826);
+ - ]
409 [ + - ]: 1 : auto broken_tx_data{std::span<std::byte>{tx_data.begin(), tx_data.begin() + 10}};
410 [ + - - + : 2 : BOOST_CHECK_THROW(Transaction{broken_tx_data}, std::runtime_error);
- - - - -
+ + - +
- ]
411 [ + - ]: 1 : auto input{tx.GetInput(0)};
412 [ + - + - : 1 : BOOST_CHECK_EQUAL(input.GetSequence(), 0xfffffffe);
+ - ]
413 [ + - + - ]: 1 : auto output{tx.GetOutput(tx.CountOutputs() - 1)};
414 [ + - + - : 1 : BOOST_CHECK_EQUAL(output.Amount(), 42130042);
+ - ]
415 [ + - ]: 1 : auto script_pubkey{output.GetScriptPubkey()};
416 : 1 : {
417 [ - + + - ]: 1 : auto tx_new{Transaction{tx_data}};
418 : : // This is safe, because we now use copy assignment
419 [ + - + - ]: 1 : TransactionOutput output = tx_new.GetOutput(tx_new.CountOutputs() - 1);
420 [ + - ]: 1 : ScriptPubkey script = output.GetScriptPubkey();
421 : :
422 [ + - + - ]: 1 : TransactionOutputView output2 = tx_new.GetOutput(tx_new.CountOutputs() - 1);
423 [ + - + - ]: 1 : BOOST_CHECK_NE(output.get(), output2.get());
424 [ + - + - : 1 : BOOST_CHECK_EQUAL(output.Amount(), output2.Amount());
+ - + - ]
425 [ + - ]: 1 : TransactionOutput output3 = output2;
426 [ + - + - ]: 1 : BOOST_CHECK_NE(output3.get(), output2.get());
427 [ + - + - : 1 : BOOST_CHECK_EQUAL(output3.Amount(), output2.Amount());
+ - + - ]
428 : :
429 : : // Non-owned view
430 [ + - ]: 1 : ScriptPubkeyView script2 = output.GetScriptPubkey();
431 [ + - + - ]: 1 : BOOST_CHECK_NE(script.get(), script2.get());
432 [ + - - + : 2 : check_equal(script.ToBytes(), script2.ToBytes());
+ - - + +
- ]
433 : :
434 : : // Non-owned to owned
435 [ + - ]: 1 : ScriptPubkey script3 = script2;
436 [ + - + - ]: 1 : BOOST_CHECK_NE(script3.get(), script2.get());
437 [ + - - + : 2 : check_equal(script3.ToBytes(), script2.ToBytes());
+ - - + +
- ]
438 : 1 : }
439 [ + - + - : 1 : BOOST_CHECK_EQUAL(output.Amount(), 42130042);
+ - ]
440 : :
441 [ + - - + : 1 : auto tx_roundtrip{Transaction{tx.ToBytes()}};
+ - ]
442 [ - + + - : 1 : check_equal(tx_roundtrip.ToBytes(), tx_data);
- + + - ]
443 : :
444 : : // The following code is unsafe, but left here to show limitations of the
445 : : // API, because we preserve the output view beyond the lifetime of the
446 : : // transaction. The view type wrapper should make this clear to the user.
447 : : // auto get_output = [&]() -> TransactionOutputView {
448 : : // auto tx{Transaction{tx_data}};
449 : : // return tx.GetOutput(0);
450 : : // };
451 : : // auto output_new = get_output();
452 : : // BOOST_CHECK_EQUAL(output_new.Amount(), 20737411);
453 : :
454 : 1 : int64_t total_amount{0};
455 [ + - + - : 5 : for (const auto output : tx.Outputs()) {
+ - ]
456 [ + - ]: 2 : total_amount += output.Amount();
457 : : }
458 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(total_amount, 62867453);
459 : :
460 [ + - + - ]: 3 : auto amount = *(tx.Outputs() | std::ranges::views::filter([](const auto& output) {
461 [ + + ]: 2 : return output.Amount() == 42130042;
462 [ + - ]: 1 : }) |
463 : 1 : std::views::transform([](const auto& output) {
464 : 1 : return output.Amount();
465 [ + - ]: 1 : })).begin();
466 [ + - + - : 2 : BOOST_REQUIRE(amount);
+ - ]
467 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(amount, 42130042);
468 : :
469 [ + - + - ]: 1 : CheckRange(tx.Outputs(), tx.CountOutputs());
470 : :
471 [ + - - + : 1 : ScriptPubkey script_pubkey_roundtrip{script_pubkey.ToBytes()};
+ - ]
472 [ + - - + : 2 : check_equal(script_pubkey_roundtrip.ToBytes(), script_pubkey.ToBytes());
+ - - + +
- ]
473 : 1 : }
474 : :
475 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_script_pubkey)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
476 : : {
477 : 1 : auto script_data{hex_string_to_byte_vec("76a9144bfbaf6afb76cc5771bc6404810d1cc041a6933988ac")};
478 [ + - ]: 1 : std::vector<std::byte> script_data_2 = script_data;
479 [ + - ]: 1 : script_data_2.push_back(std::byte{0x51});
480 [ - + + - ]: 1 : ScriptPubkey script{script_data};
481 [ - + + - ]: 1 : ScriptPubkey script2{script_data_2};
482 [ + - + - : 2 : CheckHandle(script, script2);
+ - ]
483 : :
484 : 1 : std::span<std::byte> empty_data{};
485 [ + - ]: 1 : ScriptPubkey empty_script{empty_data};
486 [ + - + - : 2 : CheckHandle(script, empty_script);
+ - ]
487 : 1 : }
488 : :
489 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_transaction_output)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
490 : : {
491 [ - + + - ]: 1 : ScriptPubkey script{hex_string_to_byte_vec("76a9144bfbaf6afb76cc5771bc6404810d1cc041a6933988ac")};
492 [ + - ]: 1 : TransactionOutput output{script, 1};
493 [ + - ]: 1 : TransactionOutput output2{script, 2};
494 [ + - + - : 2 : CheckHandle(output, output2);
+ - ]
495 : 1 : }
496 : :
497 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_transaction_input)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
498 : : {
499 [ - + + - ]: 1 : Transaction tx{hex_string_to_byte_vec("020000000248c03e66fd371c7033196ce24298628e59ebefa00363026044e0f35e0325a65d000000006a473044022004893432347f39beaa280e99da595681ddb20fc45010176897e6e055d716dbfa022040a9e46648a5d10c33ef7cee5e6cf4b56bd513eae3ae044f0039824b02d0f44c012102982331a52822fd9b62e9b5d120da1d248558fac3da3a3c51cd7d9c8ad3da760efeffffffb856678c6e4c3c84e39e2ca818807049d6fba274b42af3c6d3f9d4b6513212d2000000006a473044022068bcedc7fe39c9f21ad318df2c2da62c2dc9522a89c28c8420ff9d03d2e6bf7b0220132afd752754e5cb1ea2fd0ed6a38ec666781e34b0e93dc9a08f2457842cf5660121033aeb9c079ea3e08ea03556182ab520ce5c22e6b0cb95cee6435ee17144d860cdfeffffff0260d50b00000000001976a914363cc8d55ea8d0500de728ef6d63804ddddbdc9888ac67040f00000000001976a914c303bdc5064bf9c9a8b507b5496bd0987285707988ac6acb0700")};
500 [ + - ]: 1 : TransactionInput input_0 = tx.GetInput(0);
501 [ + - ]: 1 : TransactionInput input_1 = tx.GetInput(1);
502 [ + - + - : 2 : CheckHandle(input_0, input_1);
+ - ]
503 [ + - + - ]: 1 : CheckRange(tx.Inputs(), tx.CountInputs());
504 [ + - ]: 1 : OutPoint point_0 = input_0.OutPoint();
505 [ + - ]: 1 : OutPoint point_1 = input_1.OutPoint();
506 [ + - + - : 2 : CheckHandle(point_0, point_1);
+ - ]
507 : :
508 [ + - ]: 1 : WitnessStackView ws_0 = input_0.GetWitnessStack();
509 [ + - + - : 1 : BOOST_CHECK_EQUAL(ws_0.CountItems(), 0);
+ - ]
510 [ + - + - : 2 : BOOST_CHECK(ws_0.Items().empty());
+ - + - ]
511 : :
512 : : // P2PKH: DER sig + compressed pubkey push.
513 [ + - + - : 3 : BOOST_CHECK(input_0.GetScriptSig() == hex_string_to_byte_vec("473044022004893432347f39beaa280e99da595681ddb20fc45010176897e6e055d716dbfa022040a9e46648a5d10c33ef7cee5e6cf4b56bd513eae3ae044f0039824b02d0f44c012102982331a52822fd9b62e9b5d120da1d248558fac3da3a3c51cd7d9c8ad3da760e"));
+ - + - ]
514 [ + - + - : 3 : BOOST_CHECK(input_1.GetScriptSig() == hex_string_to_byte_vec("473044022068bcedc7fe39c9f21ad318df2c2da62c2dc9522a89c28c8420ff9d03d2e6bf7b0220132afd752754e5cb1ea2fd0ed6a38ec666781e34b0e93dc9a08f2457842cf5660121033aeb9c079ea3e08ea03556182ab520ce5c22e6b0cb95cee6435ee17144d860cd"));
+ - + - ]
515 : :
516 : : // P2WSH input: OP_0, sig, sig, redeem_script (0, 71, 71, 105 bytes); no scriptSig.
517 [ + - + - ]: 2 : Transaction segwit_tx{hex_string_to_byte_vec("010000000001011f97548fbbe7a0db7588a66e18d803d0089315aa7d4cc28360b6ec50ef36718a0100000000ffffffff02df1776000000000017a9146c002a686959067f4866b8fb493ad7970290ab728757d29f0000000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d04004730440220565d170eed95ff95027a69b313758450ba84a01224e1f7f130dda46e94d13f8602207bdd20e307f062594022f12ed5017bbf4a055a06aea91c10110a0e3bb23117fc014730440220647d2dc5b15f60bc37dc42618a370b2a1490293f9e5c8464f53ec4fe1dfe067302203598773895b4b16d37485cbe21b337f4e4b650739880098c592553add7dd4355016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000")};
518 [ + - ]: 1 : TransactionInputView segwit_input = segwit_tx.GetInput(0);
519 [ + - ]: 1 : WitnessStackView ws = segwit_input.GetWitnessStack();
520 [ + - + - : 1 : BOOST_CHECK_EQUAL(ws.CountItems(), 4);
+ - ]
521 [ + - + - : 2 : BOOST_CHECK(ws.GetItem(0).empty());
+ - + - ]
522 [ + - + - : 3 : BOOST_CHECK(ws.GetItem(1) == hex_string_to_byte_vec("30440220565d170eed95ff95027a69b313758450ba84a01224e1f7f130dda46e94d13f8602207bdd20e307f062594022f12ed5017bbf4a055a06aea91c10110a0e3bb23117fc01"));
+ - + - ]
523 [ + - + - : 3 : BOOST_CHECK(ws.GetItem(2) == hex_string_to_byte_vec("30440220647d2dc5b15f60bc37dc42618a370b2a1490293f9e5c8464f53ec4fe1dfe067302203598773895b4b16d37485cbe21b337f4e4b650739880098c592553add7dd435501"));
+ - + - ]
524 [ + - + - : 3 : BOOST_CHECK(ws.GetItem(3) == hex_string_to_byte_vec("52210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae"));
+ - + - ]
525 [ + - ]: 1 : auto items = ws.Items();
526 [ + - + - : 1 : BOOST_CHECK_EQUAL(items.size(), 4);
+ - ]
527 [ + - + + ]: 5 : for (size_t i = 0; i < items.size(); ++i) {
528 [ + - + - : 8 : BOOST_CHECK(items[i] == ws.GetItem(i));
+ - + - ]
529 : : }
530 [ + - ]: 1 : WitnessStack owned_ws_0{ws_0};
531 [ + - ]: 1 : WitnessStack owned_ws{ws};
532 [ + - + - : 2 : CheckHandle(owned_ws_0, owned_ws);
+ - ]
533 [ + - + - : 2 : BOOST_CHECK(segwit_input.GetScriptSig().empty());
+ - ]
534 : 1 : }
535 : :
536 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_precomputed_txdata) {
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
537 : 1 : auto tx_data{hex_string_to_byte_vec("02000000013f7cebd65c27431a90bba7f796914fe8cc2ddfc3f2cbd6f7e5f2fc854534da95000000006b483045022100de1ac3bcdfb0332207c4a91f3832bd2c2915840165f876ab47c5f8996b971c3602201c6c053d750fadde599e6f5c4e1963df0f01fc0d97815e8157e3d59fe09ca30d012103699b464d1d8bc9e47d4fb1cdaa89a1c5783d68363c4dbc4b524ed3d857148617feffffff02836d3c01000000001976a914fc25d6d5c94003bf5b0c7b640a248e2c637fcfb088ac7ada8202000000001976a914fbed3d9b11183209a57999d54d59f67c019e756c88ac6acb0700")};
538 [ - + + - ]: 1 : auto tx{Transaction{tx_data}};
539 [ + - ]: 1 : auto tx_data_2{hex_string_to_byte_vec("02000000000101904f4ee5c87d20090b642f116e458cd6693292ad9ece23e72f15fb6c05b956210500000000fdffffff02e2010000000000002251200839a723933b56560487ec4d67dda58f09bae518ffa7e148313c5696ac837d9f10060000000000002251205826bcdae7abfb1c468204170eab00d887b61ab143464a4a09e1450bdc59a3340140f26e7af574e647355830772946356c27e7bbc773c5293688890f58983499581be84de40be7311a14e6d6422605df086620e75adae84ff06b75ce5894de5e994a00000000")};
540 [ - + + - ]: 1 : auto tx2{Transaction{tx_data_2}};
541 : 1 : auto precomputed_txdata{PrecomputedTransactionData{
542 : : /*tx_to=*/tx,
543 : : /*spent_outputs=*/{},
544 [ + - ]: 1 : }};
545 : 1 : auto precomputed_txdata_2{PrecomputedTransactionData{
546 : : /*tx_to=*/tx2,
547 : : /*spent_outputs=*/{},
548 [ + - ]: 1 : }};
549 [ + - + - : 2 : CheckHandle(precomputed_txdata, precomputed_txdata_2);
+ - ]
550 : 1 : }
551 : :
552 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_script_verify_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
553 : : {
554 : : // Legacy transaction aca326a724eda9a461c10a876534ecd5ae7b27f10f26c3862fb996f80ea2d45d
555 [ - + + - ]: 1 : auto legacy_spent_script_pubkey{ScriptPubkey{hex_string_to_byte_vec("76a9144bfbaf6afb76cc5771bc6404810d1cc041a6933988ac")}};
556 [ + - + - ]: 2 : auto legacy_spending_tx{Transaction{hex_string_to_byte_vec("02000000013f7cebd65c27431a90bba7f796914fe8cc2ddfc3f2cbd6f7e5f2fc854534da95000000006b483045022100de1ac3bcdfb0332207c4a91f3832bd2c2915840165f876ab47c5f8996b971c3602201c6c053d750fadde599e6f5c4e1963df0f01fc0d97815e8157e3d59fe09ca30d012103699b464d1d8bc9e47d4fb1cdaa89a1c5783d68363c4dbc4b524ed3d857148617feffffff02836d3c01000000001976a914fc25d6d5c94003bf5b0c7b640a248e2c637fcfb088ac7ada8202000000001976a914fbed3d9b11183209a57999d54d59f67c019e756c88ac6acb0700")}};
557 [ + - ]: 1 : run_verify_test(
558 : : /*spent_script_pubkey=*/legacy_spent_script_pubkey,
559 : : /*spending_tx=*/legacy_spending_tx,
560 : : /*precomputed_txdata=*/nullptr,
561 : : /*amount=*/0,
562 : : /*input_index=*/0,
563 : : /*taproot=*/false);
564 : :
565 : : // Legacy transaction aca326a724eda9a461c10a876534ecd5ae7b27f10f26c3862fb996f80ea2d45d with precomputed_txdata
566 : 1 : auto legacy_precomputed_txdata{PrecomputedTransactionData{
567 : : /*tx_to=*/legacy_spending_tx,
568 : : /*spent_outputs=*/{},
569 [ + - ]: 1 : }};
570 [ + - ]: 1 : run_verify_test(
571 : : /*spent_script_pubkey=*/legacy_spent_script_pubkey,
572 : : /*spending_tx=*/legacy_spending_tx,
573 : : /*precomputed_txdata=*/&legacy_precomputed_txdata,
574 : : /*amount=*/0,
575 : : /*input_index=*/0,
576 : : /*taproot=*/false);
577 : :
578 : : // Segwit transaction 1a3e89644985fbbb41e0dcfe176739813542b5937003c46a07de1e3ee7a4a7f3
579 [ + - + - ]: 2 : auto segwit_spent_script_pubkey{ScriptPubkey{hex_string_to_byte_vec("0020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d")}};
580 [ + - + - ]: 2 : auto segwit_spending_tx{Transaction{hex_string_to_byte_vec("010000000001011f97548fbbe7a0db7588a66e18d803d0089315aa7d4cc28360b6ec50ef36718a0100000000ffffffff02df1776000000000017a9146c002a686959067f4866b8fb493ad7970290ab728757d29f0000000000220020701a8d401c84fb13e6baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d04004730440220565d170eed95ff95027a69b313758450ba84a01224e1f7f130dda46e94d13f8602207bdd20e307f062594022f12ed5017bbf4a055a06aea91c10110a0e3bb23117fc014730440220647d2dc5b15f60bc37dc42618a370b2a1490293f9e5c8464f53ec4fe1dfe067302203598773895b4b16d37485cbe21b337f4e4b650739880098c592553add7dd4355016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000")}};
581 [ + - ]: 1 : run_verify_test(
582 : : /*spent_script_pubkey=*/segwit_spent_script_pubkey,
583 : : /*spending_tx=*/segwit_spending_tx,
584 : : /*precomputed_txdata=*/nullptr,
585 : : /*amount=*/18393430,
586 : : /*input_index=*/0,
587 : : /*taproot=*/false);
588 : :
589 : : // Segwit transaction 1a3e89644985fbbb41e0dcfe176739813542b5937003c46a07de1e3ee7a4a7f3 with precomputed_txdata
590 : 1 : auto segwit_precomputed_txdata{PrecomputedTransactionData{
591 : : /*tx_to=*/segwit_spending_tx,
592 : : /*spent_outputs=*/{},
593 [ + - ]: 1 : }};
594 [ + - ]: 1 : run_verify_test(
595 : : /*spent_script_pubkey=*/segwit_spent_script_pubkey,
596 : : /*spending_tx=*/segwit_spending_tx,
597 : : /*precomputed_txdata=*/&segwit_precomputed_txdata,
598 : : /*amount=*/18393430,
599 : : /*input_index=*/0,
600 : : /*taproot=*/false);
601 : :
602 : : // Taproot transaction 33e794d097969002ee05d336686fc03c9e15a597c1b9827669460fac98799036
603 [ + - + - ]: 2 : auto taproot_spent_script_pubkey{ScriptPubkey{hex_string_to_byte_vec("5120339ce7e165e67d93adb3fef88a6d4beed33f01fa876f05a225242b82a631abc0")}};
604 [ + - + - ]: 2 : auto taproot_spending_tx{Transaction{hex_string_to_byte_vec("01000000000101d1f1c1f8cdf6759167b90f52c9ad358a369f95284e841d7a2536cef31c0549580100000000fdffffff020000000000000000316a2f49206c696b65205363686e6f7272207369677320616e6420492063616e6e6f74206c69652e204062697462756734329e06010000000000225120a37c3903c8d0db6512e2b40b0dffa05e5a3ab73603ce8c9c4b7771e5412328f90140a60c383f71bac0ec919b1d7dbc3eb72dd56e7aa99583615564f9f99b8ae4e837b758773a5b2e4c51348854c8389f008e05029db7f464a5ff2e01d5e6e626174affd30a00")}};
605 : 1 : std::vector<TransactionOutput> taproot_spent_outputs;
606 [ + - ]: 1 : taproot_spent_outputs.emplace_back(taproot_spent_script_pubkey, 88480);
607 [ - + ]: 1 : auto taproot_precomputed_txdata{PrecomputedTransactionData{
608 : : /*tx_to=*/taproot_spending_tx,
609 : 1 : /*spent_outputs=*/taproot_spent_outputs,
610 [ + - ]: 1 : }};
611 [ + - ]: 1 : run_verify_test(
612 : : /*spent_script_pubkey=*/taproot_spent_script_pubkey,
613 : : /*spending_tx=*/taproot_spending_tx,
614 : : /*precomputed_txdata=*/&taproot_precomputed_txdata,
615 : : /*amount=*/88480,
616 : : /*input_index=*/0,
617 : : /*taproot=*/true);
618 : :
619 : : // Two-input taproot transaction e8e8320f40c31ed511570e9cdf1d241f8ec9a5cc392e6105240ac8dbea2098de
620 [ + - + - ]: 2 : auto taproot2_spent_script_pubkey0{ScriptPubkey{hex_string_to_byte_vec("5120b7da80f57e36930b0515eb09293e25858d13e6b91fee6184943f5a584cb4248e")}};
621 [ + - + - ]: 2 : auto taproot2_spent_script_pubkey1{ScriptPubkey{hex_string_to_byte_vec("5120ab78e077d062e7b8acd7063668b4db5355a1b5d5fd2a46a8e98e62e5e63fab77")}};
622 [ + - + - ]: 2 : auto taproot2_spending_tx{Transaction{hex_string_to_byte_vec("02000000000102c0f01ead18750892c84b1d4f595149ad38f16847df1fbf490e235b3b78c1f98a0100000000ffffffff456764a19c2682bf5b1567119f06a421849ad1664cf42b5ef95b69d6e2159e9d0000000000ffffffff022202000000000000225120b6c0c2a8ee25a2ae0322ab7f1a06f01746f81f6b90d179c3c2a51a356e6188f1d70e020000000000225120b7da80f57e36930b0515eb09293e25858d13e6b91fee6184943f5a584cb4248e0141933fdc49eb1af1f08ed1e9cf5559259309a8acd25ff1e6999b6955124438aef4fceaa4e6a5f85286631e24837329563595bc3cf4b31e1c687442abb01c4206818101401c9620faf1e8c84187762ad14d04ae3857f59a2f03f1dcbb99290e16dfc572a63b4ea435780a5787af59beb5742fd71cda8a95381517a1ff14b4c67996c4bf8100000000")}};
623 : 1 : std::vector<TransactionOutput> taproot2_spent_outputs;
624 [ + - ]: 1 : taproot2_spent_outputs.emplace_back(taproot2_spent_script_pubkey0, 546);
625 [ + - ]: 1 : taproot2_spent_outputs.emplace_back(taproot2_spent_script_pubkey1, 135125);
626 [ - + ]: 1 : auto taproot2_precomputed_txdata{PrecomputedTransactionData{
627 : : /*tx_to=*/taproot2_spending_tx,
628 : 1 : /*spent_outputs=*/taproot2_spent_outputs,
629 [ + - ]: 1 : }};
630 [ + - ]: 1 : run_verify_test(
631 : : /*spent_script_pubkey=*/taproot2_spent_script_pubkey0,
632 : : /*spending_tx=*/taproot2_spending_tx,
633 : : /*precomputed_txdata=*/&taproot2_precomputed_txdata,
634 : : /*amount=*/546,
635 : : /*input_index=*/0,
636 : : /*taproot=*/true);
637 [ + - ]: 1 : run_verify_test(
638 : : /*spent_script_pubkey=*/taproot2_spent_script_pubkey1,
639 : : /*spending_tx=*/taproot2_spending_tx,
640 : : /*precomputed_txdata=*/&taproot2_precomputed_txdata,
641 : : /*amount=*/135125,
642 : : /*input_index=*/1,
643 : : /*taproot=*/true);
644 : 1 : }
645 : :
646 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(logging_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
647 : : {
648 : 1 : btck_LoggingOptions logging_options = {
649 : : .log_timestamps = true,
650 : : .log_time_micros = true,
651 : : .log_threadnames = false,
652 : : .log_sourcelocations = false,
653 : : .always_print_category_levels = true,
654 : : };
655 : :
656 : 1 : logging_set_options(logging_options);
657 : 1 : logging_set_level_category(LogCategory::BENCH, LogLevel::TRACE_LEVEL);
658 : 1 : logging_disable_category(LogCategory::BENCH);
659 : 1 : logging_enable_category(LogCategory::VALIDATION);
660 : 1 : logging_disable_category(LogCategory::VALIDATION);
661 : :
662 : : // Check that connecting, connecting another, and then disconnecting and connecting a logger again works.
663 : 1 : {
664 : 1 : logging_set_level_category(LogCategory::KERNEL, LogLevel::TRACE_LEVEL);
665 : 1 : logging_enable_category(LogCategory::KERNEL);
666 [ + - ]: 1 : Logger logger{std::make_unique<TestLog>()};
667 [ + - + - : 1 : Logger logger_2{std::make_unique<TestLog>()};
+ - ]
668 [ + - ]: 1 : }
669 [ + - + - ]: 1 : Logger logger{std::make_unique<TestLog>()};
670 : 1 : }
671 : :
672 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_chainparams_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
673 : : {
674 : 1 : ChainParams params_signet{ChainType::SIGNET};
675 [ + - + - ]: 2 : ChainParams params_signet_challenge{hex_string_to_byte_vec("51")};
676 [ + - + - : 2 : CheckHandle(params_signet, params_signet_challenge);
+ - ]
677 : 1 : }
678 : :
679 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_context_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
680 : : {
681 : 1 : { // test default context
682 : 1 : Context context{};
683 [ + - ]: 1 : Context context2{};
684 [ + - + - : 2 : CheckHandle(context, context2);
+ - ]
685 : 1 : }
686 : :
687 : 1 : { // test with context options, but not options set
688 : 1 : ContextOptions options{};
689 [ + - ]: 1 : Context context{options};
690 [ + - ]: 1 : }
691 : :
692 : 1 : { // test with context options
693 : 1 : ContextOptions options{};
694 [ + - ]: 1 : ChainParams params{ChainType::MAINNET};
695 [ + - ]: 1 : ChainParams regtest_params{ChainType::REGTEST};
696 [ + - + - : 2 : CheckHandle(params, regtest_params);
+ - ]
697 [ + - ]: 1 : options.SetChainParams(params);
698 [ + - + - ]: 1 : options.SetNotifications(std::make_shared<TestKernelNotifications>());
699 [ + - ]: 1 : Context context{options};
700 [ + - ]: 1 : }
701 : 1 : }
702 : :
703 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_block_header_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
704 : : {
705 : : // Block header format: version(4) + prev_hash(32) + merkle_root(32) + timestamp(4) + bits(4) + nonce(4) = 80 bytes
706 [ - + + - ]: 1 : BlockHeader header_0{hex_string_to_byte_vec("00e07a26beaaeee2e71d7eb19279545edbaf15de0999983626ec00000000000000000000579cf78b65229bfb93f4a11463af2eaa5ad91780f27f5d147a423bea5f7e4cdf2a47e268b4dd01173a9662ee")};
707 [ + - + - : 2 : BOOST_CHECK_EQUAL(byte_span_to_hex_string_reversed(header_0.Hash().ToBytes()), "00000000000000000000325c7e14a4ee3b4fcb2343089a839287308a0ddbee4f");
+ - + - ]
708 [ + - + - ]: 2 : BlockHeader header_1{hex_string_to_byte_vec("00c00020e7cb7b4de21d26d55bd384017b8bb9333ac3b2b55bed00000000000000000000d91b4484f801b99f03d36b9d26cfa83420b67f81da12d7e6c1e7f364e743c5ba9946e268b4dd011799c8533d")};
709 [ + - + - : 2 : CheckHandle(header_0, header_1);
+ - ]
710 : :
711 : : // Test all header field accessors using mainnet block 1
712 [ + - ]: 1 : auto mainnet_block_1_header = hex_string_to_byte_vec("010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e61bc6649ffff001d01e36299");
713 [ - + + - ]: 1 : BlockHeader header{mainnet_block_1_header};
714 [ + - + - : 1 : BOOST_CHECK_EQUAL(header.Version(), 1);
+ - ]
715 [ + - + - : 1 : BOOST_CHECK_EQUAL(header.Timestamp(), 1231469665);
+ - ]
716 [ + - + - : 1 : BOOST_CHECK_EQUAL(header.Bits(), 0x1d00ffff);
+ - ]
717 [ + - + - : 1 : BOOST_CHECK_EQUAL(header.Nonce(), 2573394689);
+ - ]
718 [ + - + - : 2 : BOOST_CHECK_EQUAL(byte_span_to_hex_string_reversed(header.Hash().ToBytes()), "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048");
+ - + - ]
719 [ + - ]: 1 : auto prev_hash = header.PrevHash();
720 [ + - + - : 2 : BOOST_CHECK_EQUAL(byte_span_to_hex_string_reversed(prev_hash.ToBytes()), "000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f");
+ - + - ]
721 : :
722 : : // Test round-trip serialization of block header
723 [ + - + - ]: 1 : auto header_roundtrip{BlockHeader{header.ToBytes()}};
724 [ - + + - : 1 : check_equal(header_roundtrip.ToBytes(), mainnet_block_1_header);
+ - ]
725 : :
726 [ + - ]: 1 : auto raw_block = hex_string_to_byte_vec("010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e61bc6649ffff001d01e362990101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac00000000");
727 [ - + + - ]: 1 : Block block{raw_block};
728 [ + - ]: 1 : BlockHeader block_header{block.GetHeader()};
729 [ + - + - : 1 : BOOST_CHECK_EQUAL(block_header.Version(), 1);
+ - ]
730 [ + - + - : 1 : BOOST_CHECK_EQUAL(block_header.Timestamp(), 1231469665);
+ - ]
731 [ + - + - : 1 : BOOST_CHECK_EQUAL(block_header.Bits(), 0x1d00ffff);
+ - ]
732 [ + - + - : 1 : BOOST_CHECK_EQUAL(block_header.Nonce(), 2573394689);
+ - ]
733 [ + - + - : 2 : BOOST_CHECK_EQUAL(byte_span_to_hex_string_reversed(block_header.Hash().ToBytes()), "00000000839a8e6886ab5951d76f411475428afc90947ee320161bbf18eb6048");
+ - + - ]
734 : :
735 : : // Verify header from block serializes to first 80 bytes of raw block
736 [ + - ]: 1 : auto block_header_bytes = block_header.ToBytes();
737 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(block_header_bytes.size(), 80);
738 [ + - ]: 1 : check_equal(block_header_bytes, std::span<const std::byte>(raw_block.data(), 80));
739 : 1 : }
740 : :
741 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_block)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
742 : : {
743 [ - + + - ]: 1 : Block block{hex_string_to_byte_vec(REGTEST_BLOCK_DATA[0])};
744 [ + - + - ]: 2 : Block block_100{hex_string_to_byte_vec(REGTEST_BLOCK_DATA[100])};
745 [ + - + - : 2 : CheckHandle(block, block_100);
+ - ]
746 [ + - + - ]: 2 : Block block_tx{hex_string_to_byte_vec(REGTEST_BLOCK_DATA[205])};
747 [ + - + - ]: 1 : CheckRange(block_tx.Transactions(), block_tx.CountTransactions());
748 [ + - ]: 1 : auto invalid_data = hex_string_to_byte_vec("012300");
749 [ + - - + : 2 : BOOST_CHECK_THROW(Block{invalid_data}, std::runtime_error);
- + - - -
- - + + -
+ - ]
750 [ + - ]: 1 : auto empty_data = hex_string_to_byte_vec("");
751 [ + - - + : 2 : BOOST_CHECK_THROW(Block{empty_data}, std::runtime_error);
- + - - -
- - + + -
+ - ]
752 : 1 : }
753 : :
754 : 7 : Context create_context(std::shared_ptr<TestKernelNotifications> notifications, ChainType chain_type, std::shared_ptr<TestValidationInterface> validation_interface = nullptr)
755 : : {
756 : 7 : ContextOptions options{};
757 [ + - ]: 7 : ChainParams params{chain_type};
758 [ + - ]: 7 : options.SetChainParams(params);
759 [ + - + - ]: 14 : options.SetNotifications(notifications);
760 [ + + ]: 7 : if (validation_interface) {
761 [ + - + - ]: 3 : options.SetValidationInterface(validation_interface);
762 : : }
763 [ + - ]: 7 : auto context{Context{options}};
764 : 7 : return context;
765 [ + - ]: 14 : }
766 : :
767 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_chainman_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
768 : : {
769 [ + - ]: 1 : Logger logger{std::make_unique<TestLog>()};
770 [ + - + - ]: 1 : auto test_directory{TestDirectory{"chainman_test_bitcoin_kernel"}};
771 : :
772 : 1 : { // test with default context
773 [ + - ]: 1 : Context context{};
774 [ - + - + : 7 : ChainstateManagerOptions chainman_opts{context, PathToString(test_directory.m_directory), PathToString(test_directory.m_directory / "blocks")};
+ - + - -
+ - + +
- ]
775 [ + - ]: 1 : ChainMan chainman{context, chainman_opts};
776 [ + - ]: 2 : }
777 : :
778 : 1 : { // test with default context options
779 [ + - ]: 1 : ContextOptions options{};
780 [ + - ]: 1 : Context context{options};
781 [ - + - + : 7 : ChainstateManagerOptions chainman_opts{context, PathToString(test_directory.m_directory), PathToString(test_directory.m_directory / "blocks")};
+ - + - -
+ - + +
- ]
782 [ + - ]: 1 : ChainMan chainman{context, chainman_opts};
783 [ + - + - ]: 2 : }
784 : 1 : { // null or empty data_directory or blocks_directory are not allowed
785 [ + - ]: 1 : Context context{};
786 [ - + ]: 1 : auto valid_dir{PathToString(test_directory.m_directory)};
787 : 1 : std::vector<std::pair<std::string_view, std::string_view>> illegal_cases{
788 : : {"", valid_dir},
789 : 1 : {valid_dir, {nullptr, 0}},
790 : : {"", ""},
791 : : {{nullptr, 0}, {nullptr, 0}},
792 [ - + + - ]: 1 : };
793 [ + - + + ]: 5 : for (auto& [data_dir, blocks_dir] : illegal_cases) {
794 [ + - - + : 8 : BOOST_CHECK_THROW(ChainstateManagerOptions(context, data_dir, blocks_dir),
- - - - -
+ + - +
- ]
795 : : std::runtime_error);
796 : 1 : };
797 : 1 : }
798 : :
799 [ + - ]: 1 : auto notifications{std::make_shared<TestKernelNotifications>()};
800 [ + - + - : 3 : auto context{create_context(notifications, ChainType::MAINNET)};
- + ]
801 : :
802 [ - + - + : 7 : ChainstateManagerOptions chainman_opts{context, PathToString(test_directory.m_directory), PathToString(test_directory.m_directory / "blocks")};
+ - + - -
+ - + +
- ]
803 [ + - ]: 1 : chainman_opts.SetWorkerThreads(4);
804 [ + - + - : 2 : BOOST_CHECK(!chainman_opts.SetDatabaseCacheBytes(4_MiB - 1));
+ - + - ]
805 : 1 : if constexpr (sizeof(void*) == 4) BOOST_CHECK(!chainman_opts.SetDatabaseCacheBytes(2_GiB));
806 [ + - + - : 2 : BOOST_CHECK(chainman_opts.SetDatabaseCacheBytes(4_MiB));
+ - + - ]
807 [ + - + - : 2 : BOOST_CHECK(!chainman_opts.SetWipeDbs(/*wipe_block_tree=*/true, /*wipe_chainstate=*/false));
+ - + - ]
808 [ + - + - : 2 : BOOST_CHECK(chainman_opts.SetWipeDbs(/*wipe_block_tree=*/true, /*wipe_chainstate=*/true));
+ - + - ]
809 [ + - + - : 2 : BOOST_CHECK(chainman_opts.SetWipeDbs(/*wipe_block_tree=*/false, /*wipe_chainstate=*/true));
+ - + - ]
810 [ + - + - : 2 : BOOST_CHECK(chainman_opts.SetWipeDbs(/*wipe_block_tree=*/false, /*wipe_chainstate=*/false));
+ - + - ]
811 [ + - ]: 1 : ChainMan chainman{context, chainman_opts};
812 [ + - + - : 4 : }
+ - ]
813 : :
814 : 8 : std::unique_ptr<ChainMan> create_chainman(TestDirectory& test_directory,
815 : : bool reindex,
816 : : bool wipe_chainstate,
817 : : bool block_tree_db_in_memory,
818 : : bool chainstate_db_in_memory,
819 : : Context& context)
820 : : {
821 [ - + - + : 56 : ChainstateManagerOptions chainman_opts{context, PathToString(test_directory.m_directory), PathToString(test_directory.m_directory / "blocks")};
+ - + - -
+ - + +
- ]
822 : :
823 [ + + ]: 8 : if (reindex) {
824 [ + - ]: 1 : chainman_opts.SetWipeDbs(/*wipe_block_tree=*/reindex, /*wipe_chainstate=*/reindex);
825 : : }
826 [ + + ]: 8 : if (wipe_chainstate) {
827 [ + - ]: 1 : chainman_opts.SetWipeDbs(/*wipe_block_tree=*/false, /*wipe_chainstate=*/wipe_chainstate);
828 : : }
829 [ + + ]: 8 : if (block_tree_db_in_memory) {
830 [ + - ]: 2 : chainman_opts.UpdateBlockTreeDbInMemory(block_tree_db_in_memory);
831 : : }
832 [ + + ]: 8 : if (chainstate_db_in_memory) {
833 [ + - ]: 2 : chainman_opts.UpdateChainstateDbInMemory(chainstate_db_in_memory);
834 : : }
835 : :
836 [ + - ]: 8 : auto chainman{std::make_unique<ChainMan>(context, chainman_opts)};
837 [ + - ]: 8 : return chainman;
838 : 8 : }
839 : :
840 : 1 : void chainman_reindex_test(TestDirectory& test_directory)
841 : : {
842 : 1 : auto notifications{std::make_shared<TestKernelNotifications>()};
843 [ + - + - : 3 : auto context{create_context(notifications, ChainType::MAINNET)};
- + ]
844 : 1 : auto chainman{create_chainman(
845 : : test_directory, /*reindex=*/true, /*wipe_chainstate=*/false,
846 [ + - ]: 1 : /*block_tree_db_in_memory=*/false, /*chainstate_db_in_memory=*/false, context)};
847 : :
848 : 1 : std::vector<std::string> import_files;
849 [ + - - + : 2 : BOOST_CHECK(chainman->ImportBlocks(import_files));
+ - + - +
- ]
850 : :
851 : : // Sanity check some block retrievals
852 [ + - ]: 1 : auto chain{chainman->GetChain()};
853 [ + - - + : 2 : BOOST_CHECK_THROW(chain.GetByHeight(1000), std::runtime_error);
- - - - -
+ + - +
- ]
854 [ + - ]: 1 : auto genesis_index{chain.Entries().front()};
855 [ + - + - : 2 : BOOST_CHECK(!genesis_index.GetPrevious());
+ - + - ]
856 [ + - + - ]: 2 : auto genesis_block_raw{chainman->ReadBlock(genesis_index).value().ToBytes()};
857 [ + - ]: 1 : auto first_index{chain.GetByHeight(0)};
858 [ + - + - ]: 2 : auto first_block_raw{chainman->ReadBlock(genesis_index).value().ToBytes()};
859 [ - + - + : 1 : check_equal(genesis_block_raw, first_block_raw);
+ - ]
860 [ + - ]: 1 : auto height{first_index.GetHeight()};
861 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(height, 0);
862 : :
863 [ + - + - ]: 1 : auto next_index{chain.GetByHeight(first_index.GetHeight() + 1)};
864 [ + - + - : 2 : BOOST_CHECK(chain.Contains(next_index));
+ - + - ]
865 [ + - + - ]: 2 : auto next_block_data{chainman->ReadBlock(next_index).value().ToBytes()};
866 [ + - ]: 1 : auto tip_index{chain.Entries().back()};
867 [ + - + - ]: 2 : auto tip_block_data{chainman->ReadBlock(tip_index).value().ToBytes()};
868 [ + - ]: 1 : auto second_index{chain.GetByHeight(1)};
869 [ + - ]: 2 : auto second_block{chainman->ReadBlock(second_index).value()};
870 [ + - ]: 1 : auto second_block_data{second_block.ToBytes()};
871 [ + - ]: 1 : auto second_height{second_index.GetHeight()};
872 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(second_height, 1);
873 [ - + - + : 1 : check_equal(next_block_data, tip_block_data);
+ - ]
874 [ - + - + : 1 : check_equal(next_block_data, second_block_data);
+ - ]
875 : :
876 [ + - ]: 1 : auto second_hash{second_index.GetHash()};
877 [ + - + - ]: 1 : auto another_second_index{chainman->GetBlockTreeEntry(second_hash)};
878 [ + - + - : 2 : BOOST_CHECK(another_second_index);
+ - ]
879 [ + - ]: 1 : auto another_second_height{another_second_index->GetHeight()};
880 [ + - ]: 1 : auto second_block_hash{second_block.GetHash()};
881 [ + - + - : 3 : check_equal(second_block_hash.ToBytes(), second_hash.ToBytes());
+ - ]
882 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(second_height, another_second_height);
883 [ + - ]: 2 : }
884 : :
885 : 1 : void chainman_reindex_chainstate_test(TestDirectory& test_directory)
886 : : {
887 : 1 : auto notifications{std::make_shared<TestKernelNotifications>()};
888 [ + - + - : 3 : auto context{create_context(notifications, ChainType::MAINNET)};
- + ]
889 : 1 : auto chainman{create_chainman(
890 : : test_directory, /*reindex=*/false, /*wipe_chainstate=*/true,
891 [ + - ]: 1 : /*block_tree_db_in_memory=*/false, /*chainstate_db_in_memory=*/false, context)};
892 : :
893 : 1 : std::vector<std::string> import_files;
894 [ + - + - : 6 : import_files.push_back(PathToString(test_directory.m_directory / "blocks" / "blk00000.dat"));
+ - - + +
- ]
895 [ + - - + : 2 : BOOST_CHECK(chainman->ImportBlocks(import_files));
+ - + - ]
896 [ + - ]: 2 : }
897 : :
898 : 1 : void chainman_mainnet_validation_test(TestDirectory& test_directory)
899 : : {
900 : 1 : auto notifications{std::make_shared<TestKernelNotifications>()};
901 [ + - ]: 1 : auto validation_interface{std::make_shared<TestValidationInterface>()};
902 [ + - + - : 4 : auto context{create_context(notifications, ChainType::MAINNET, validation_interface)};
+ - + - ]
903 : 1 : auto chainman{create_chainman(
904 : : test_directory, /*reindex=*/false, /*wipe_chainstate=*/false,
905 [ + - ]: 1 : /*block_tree_db_in_memory=*/false, /*chainstate_db_in_memory=*/false, context)};
906 : :
907 : : // mainnet block 1
908 [ + - ]: 1 : auto raw_block = hex_string_to_byte_vec("010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e61bc6649ffff001d01e362990101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac00000000");
909 [ - + + - ]: 1 : Block block{raw_block};
910 [ + - ]: 1 : BlockHeader header{block.GetHeader()};
911 [ + - + - ]: 1 : TransactionView tx{block.GetTransaction(block.CountTransactions() - 1)};
912 [ + - + - : 2 : BOOST_CHECK_EQUAL(byte_span_to_hex_string_reversed(tx.Txid().ToBytes()), "0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098");
+ - + - ]
913 [ + - + - : 1 : BOOST_CHECK_EQUAL(header.Version(), 1);
+ - ]
914 [ + - + - : 1 : BOOST_CHECK_EQUAL(header.Timestamp(), 1231469665);
+ - ]
915 [ + - + - : 1 : BOOST_CHECK_EQUAL(header.Bits(), 0x1d00ffff);
+ - ]
916 [ + - + - : 1 : BOOST_CHECK_EQUAL(header.Nonce(), 2573394689);
+ - ]
917 [ + - + - : 1 : BOOST_CHECK_EQUAL(tx.CountInputs(), 1);
+ - ]
918 [ + - ]: 1 : Transaction tx2 = tx;
919 [ + - + - : 1 : BOOST_CHECK_EQUAL(tx2.CountInputs(), 1);
+ - ]
920 [ + - + - : 3 : for (auto transaction : block.Transactions()) {
+ - ]
921 [ + - + - : 1 : BOOST_CHECK_EQUAL(transaction.CountInputs(), 1);
+ - ]
922 : : }
923 [ + - ]: 2 : auto output_counts = *(block.Transactions() | std::views::transform([](const auto& tx) {
924 : 1 : return tx.CountOutputs();
925 [ + - ]: 1 : })).begin();
926 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(output_counts, 1);
927 : :
928 [ + - ]: 1 : validation_interface->m_expected_valid_block.emplace(raw_block);
929 [ + - ]: 1 : auto ser_block{block.ToBytes()};
930 [ - + - + : 1 : check_equal(ser_block, raw_block);
+ - ]
931 : 1 : bool new_block = false;
932 [ + - + - : 2 : BOOST_CHECK(chainman->ProcessBlock(block, &new_block));
+ - + - ]
933 [ + - + - ]: 2 : BOOST_CHECK(new_block);
934 : :
935 : 1 : validation_interface->m_expected_valid_block = std::nullopt;
936 : 1 : new_block = false;
937 [ + - + - ]: 2 : Block invalid_block{hex_string_to_byte_vec(REGTEST_BLOCK_DATA[REGTEST_BLOCK_DATA.size() - 1])};
938 [ + - + - : 2 : BOOST_CHECK(!chainman->ProcessBlock(invalid_block, &new_block));
+ - + - ]
939 [ + - + - : 2 : BOOST_CHECK(!new_block);
+ - ]
940 : :
941 [ + - ]: 1 : auto chain{chainman->GetChain()};
942 [ + - + - : 1 : BOOST_CHECK_EQUAL(chain.Height(), 1);
+ - ]
943 [ + - ]: 1 : auto tip{chain.Entries().back()};
944 [ + - ]: 1 : auto read_block{chainman->ReadBlock(tip)};
945 [ + - + - : 2 : BOOST_REQUIRE(read_block);
- + ]
946 [ - + + - : 1 : check_equal(read_block.value().ToBytes(), raw_block);
+ - - + +
- ]
947 : :
948 : : // Check that we can read the previous block
949 [ + - ]: 1 : BlockTreeEntry tip_2{*tip.GetPrevious()};
950 [ + - ]: 1 : Block read_block_2{*chainman->ReadBlock(tip_2)};
951 [ + - + - : 2 : BOOST_CHECK_EQUAL(chainman->ReadBlockSpentOutputs(tip_2).Count(), 0);
+ - ]
952 [ + - + - : 2 : BOOST_CHECK_EQUAL(chainman->ReadBlockSpentOutputs(tip).Count(), 0);
+ - ]
953 : :
954 : : // It should be an error if we go another block back, since the genesis has no ancestor
955 [ + - + - : 2 : BOOST_CHECK(!tip_2.GetPrevious());
+ - + - ]
956 : :
957 : : // If we try to validate it again, it should be a duplicate
958 [ + - + - : 2 : BOOST_CHECK(chainman->ProcessBlock(block, &new_block));
+ - + - ]
959 [ + - + - ]: 2 : BOOST_CHECK(!new_block);
960 [ + - + - ]: 3 : }
961 : :
962 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_check_block_context_free)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
963 : : {
964 : 1 : constexpr size_t MERKLE_ROOT_OFFSET{4 + 32};
965 : 1 : constexpr size_t NBITS_OFFSET{4 + 32 + 32 + 4};
966 : 1 : constexpr size_t COINBASE_PREVOUT_N_OFFSET{4 + 32 + 32 + 4 + 4 + 4 + 1 + 4 + 1 + 32};
967 : :
968 : : // Mainnet block 1
969 : 1 : auto raw_block = hex_string_to_byte_vec("010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e61bc6649ffff001d01e362990101000000010000000000000000000000000000000000000000000000000000000000000000ffffffff0704ffff001d0104ffffffff0100f2052a0100000043410496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52da7589379515d4e0a604f8141781e62294721166bf621e73a82cbf2342c858eeac00000000");
970 : :
971 : : // Context-free block checks still need consensus params for the optional
972 : : // proof-of-work validation path.
973 [ + - ]: 1 : ChainParams mainnet_params{ChainType::MAINNET};
974 [ + - ]: 1 : auto consensus_params = mainnet_params.GetConsensusParams();
975 : :
976 [ - + + - ]: 1 : Block block{raw_block};
977 [ + - ]: 1 : BlockValidationState state;
978 : :
979 [ + - + - : 2 : BOOST_CHECK(block.Check(consensus_params, BlockCheckFlags::BASE, state));
+ - + - ]
980 [ + - + - : 2 : BOOST_CHECK(state.GetValidationMode() == ValidationMode::VALID);
+ - + - ]
981 : :
982 [ + - + - : 2 : BOOST_CHECK(block.Check(consensus_params, BlockCheckFlags::ALL, state));
+ - + - ]
983 [ + - + - : 2 : BOOST_CHECK(state.GetValidationMode() == ValidationMode::VALID);
+ - + - ]
984 : :
985 [ + - ]: 1 : auto bad_merkle_block_data = raw_block;
986 [ - + ]: 1 : bad_merkle_block_data[MERKLE_ROOT_OFFSET] ^= std::byte{0x01};
987 [ - + + - ]: 1 : Block bad_merkle_block{bad_merkle_block_data};
988 : :
989 [ + - + - : 2 : BOOST_CHECK(!bad_merkle_block.Check(consensus_params, BlockCheckFlags::MERKLE, state));
+ - + - ]
990 [ + - + - : 2 : BOOST_CHECK(state.GetValidationMode() == ValidationMode::INVALID);
+ - + - ]
991 [ + - + - : 2 : BOOST_CHECK(state.GetBlockValidationResult() == BlockValidationResult::MUTATED);
+ - + - ]
992 : :
993 [ + - + - : 2 : BOOST_CHECK(bad_merkle_block.Check(consensus_params, BlockCheckFlags::BASE, state));
+ - + - ]
994 [ + - + - : 2 : BOOST_CHECK(state.GetValidationMode() == ValidationMode::VALID);
+ - + - ]
995 : :
996 [ + - ]: 1 : auto bad_pow_block_data = raw_block;
997 [ - + ]: 1 : bad_pow_block_data[NBITS_OFFSET + 3] = std::byte{0x1c};
998 [ - + + - ]: 1 : Block bad_pow_block{bad_pow_block_data};
999 : :
1000 [ + - + - : 2 : BOOST_CHECK(!bad_pow_block.Check(consensus_params, BlockCheckFlags::POW, state));
+ - + - ]
1001 [ + - + - : 2 : BOOST_CHECK(state.GetValidationMode() == ValidationMode::INVALID);
+ - + - ]
1002 [ + - + - : 2 : BOOST_CHECK(state.GetBlockValidationResult() == BlockValidationResult::INVALID_HEADER);
+ - + - ]
1003 : :
1004 [ + - + - : 2 : BOOST_CHECK(bad_pow_block.Check(consensus_params, BlockCheckFlags::MERKLE, state));
+ - + - ]
1005 [ + - + - : 2 : BOOST_CHECK(state.GetValidationMode() == ValidationMode::VALID);
+ - + - ]
1006 : :
1007 [ + - ]: 1 : auto bad_base_block_data = raw_block;
1008 [ - + ]: 1 : bad_base_block_data[COINBASE_PREVOUT_N_OFFSET] = std::byte{0x00};
1009 [ - + + - ]: 1 : Block bad_base_block{bad_base_block_data};
1010 : :
1011 [ + - + - : 2 : BOOST_CHECK(!bad_base_block.Check(consensus_params, BlockCheckFlags::BASE, state));
+ - + - ]
1012 [ + - + - : 2 : BOOST_CHECK(state.GetValidationMode() == ValidationMode::INVALID);
+ - + - ]
1013 [ + - + - : 2 : BOOST_CHECK(state.GetBlockValidationResult() == BlockValidationResult::CONSENSUS);
+ - + - ]
1014 : :
1015 : : // Test with invalid truncated block data.
1016 [ + - ]: 1 : auto truncated_block_data = hex_string_to_byte_vec("010000006fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000982051fd1e4ba744bbbe680e1fee14677ba1a3c3540bf7b1cdb606e857233e0e61bc6649ffff001d01e36299");
1017 [ + - - + : 2 : BOOST_CHECK_EXCEPTION(Block{truncated_block_data}, std::runtime_error,
- + - - -
- - + + -
+ - + - ]
1018 : : HasReason{"failed to instantiate btck object"});
1019 : 1 : }
1020 : :
1021 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_chainman_mainnet_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
1022 : : {
1023 [ + - ]: 1 : auto test_directory{TestDirectory{"mainnet_test_bitcoin_kernel"}};
1024 [ + - ]: 1 : chainman_mainnet_validation_test(test_directory);
1025 [ + - ]: 1 : chainman_reindex_test(test_directory);
1026 [ + - ]: 1 : chainman_reindex_chainstate_test(test_directory);
1027 : 1 : }
1028 : :
1029 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_block_hash_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
1030 : : {
1031 : 1 : std::array<std::byte, 32> test_hash;
1032 : 1 : std::array<std::byte, 32> test_hash_2;
1033 [ + + ]: 33 : for (int i = 0; i < 32; ++i) {
1034 : 32 : test_hash[i] = static_cast<std::byte>(i);
1035 : 32 : test_hash_2[i] = static_cast<std::byte>(i + 1);
1036 : : }
1037 : 1 : BlockHash block_hash{test_hash};
1038 [ + - ]: 1 : BlockHash block_hash_2{test_hash_2};
1039 [ + - + - : 2 : BOOST_CHECK(block_hash != block_hash_2);
+ - + - ]
1040 [ + - + - : 2 : BOOST_CHECK(block_hash == block_hash);
+ - + - ]
1041 [ + - + - : 2 : CheckHandle(block_hash, block_hash_2);
+ - ]
1042 : 1 : }
1043 : :
1044 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_block_tree_entry_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
1045 : : {
1046 [ + - ]: 1 : auto test_directory{TestDirectory{"block_tree_entry_test_bitcoin_kernel"}};
1047 [ + - ]: 1 : auto notifications{std::make_shared<TestKernelNotifications>()};
1048 [ + - + - : 3 : auto context{create_context(notifications, ChainType::REGTEST)};
- + ]
1049 : 1 : auto chainman{create_chainman(
1050 : : test_directory,
1051 : : /*reindex=*/false,
1052 : : /*wipe_chainstate=*/false,
1053 : : /*block_tree_db_in_memory=*/true,
1054 : : /*chainstate_db_in_memory=*/true,
1055 [ + - ]: 1 : context)};
1056 : :
1057 : : // Process a couple of blocks
1058 [ + + ]: 4 : for (size_t i{0}; i < 3; i++) {
1059 [ + - + - ]: 6 : Block block{hex_string_to_byte_vec(REGTEST_BLOCK_DATA[i])};
1060 : 3 : bool new_block{false};
1061 [ + - ]: 3 : chainman->ProcessBlock(block, &new_block);
1062 [ + - + - ]: 6 : BOOST_CHECK(new_block);
1063 : 3 : }
1064 : :
1065 [ + - ]: 1 : auto chain{chainman->GetChain()};
1066 [ + - ]: 1 : auto entry_0{chain.GetByHeight(0)};
1067 [ + - ]: 1 : auto entry_1{chain.GetByHeight(1)};
1068 [ + - ]: 1 : auto entry_2{chain.GetByHeight(2)};
1069 : :
1070 : : // Test inequality
1071 [ + - + - : 2 : BOOST_CHECK(entry_0 != entry_1);
+ - + - ]
1072 [ + - + - : 2 : BOOST_CHECK(entry_1 != entry_2);
+ - + - ]
1073 [ + - + - : 2 : BOOST_CHECK(entry_0 != entry_2);
+ - + - ]
1074 : :
1075 : : // Test equality with same entry
1076 [ + - + - : 3 : BOOST_CHECK(entry_0 == chain.GetByHeight(0));
+ - + - ]
1077 [ + - + - : 2 : BOOST_CHECK(entry_0 == BlockTreeEntry{entry_0});
+ - + - ]
1078 [ + - + - : 2 : BOOST_CHECK(entry_1 == entry_1);
+ - + - ]
1079 : :
1080 : : // Test GetPrevious
1081 [ + - ]: 1 : auto prev{entry_1.GetPrevious()};
1082 [ + - + - : 2 : BOOST_CHECK(prev.has_value());
+ - ]
1083 [ + - + - : 2 : BOOST_CHECK(prev.value() == entry_0);
+ - + - +
- ]
1084 : :
1085 : : // Test GetAncestor
1086 [ + - + - : 3 : BOOST_CHECK(entry_2.GetAncestor(2) == entry_2);
+ - + - ]
1087 [ + - + - : 3 : BOOST_CHECK(entry_2.GetAncestor(1) == entry_1);
+ - + - ]
1088 [ + - + - : 3 : BOOST_CHECK(entry_2.GetAncestor(0) == entry_0);
+ - ]
1089 [ + - ]: 2 : }
1090 : :
1091 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_chainman_in_memory_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
1092 : : {
1093 [ + - ]: 1 : auto in_memory_test_directory{TestDirectory{"in-memory_test_bitcoin_kernel"}};
1094 : :
1095 [ + - ]: 1 : auto notifications{std::make_shared<TestKernelNotifications>()};
1096 [ + - + - : 3 : auto context{create_context(notifications, ChainType::REGTEST)};
- + ]
1097 : 1 : auto chainman{create_chainman(
1098 : : in_memory_test_directory, /*reindex=*/false, /*wipe_chainstate=*/false,
1099 [ + - ]: 1 : /*block_tree_db_in_memory=*/true, /*chainstate_db_in_memory=*/true, context)};
1100 : :
1101 [ + + ]: 207 : for (auto& raw_block : REGTEST_BLOCK_DATA) {
1102 [ + - + - ]: 412 : Block block{hex_string_to_byte_vec(raw_block)};
1103 : 206 : bool new_block{false};
1104 [ + - ]: 206 : chainman->ProcessBlock(block, &new_block);
1105 [ + - + - ]: 412 : BOOST_CHECK(new_block);
1106 : 206 : }
1107 : :
1108 [ + - + - : 5 : BOOST_CHECK(fs::exists(in_memory_test_directory.m_directory / "blocks"));
+ - + - +
- + - ]
1109 [ + - + - : 7 : BOOST_CHECK(!fs::exists(in_memory_test_directory.m_directory / "blocks" / "index"));
+ - + - +
- + - +
- ]
1110 [ + - + - : 5 : BOOST_CHECK(!fs::exists(in_memory_test_directory.m_directory / "chainstate"));
+ - + - +
- + - ]
1111 : :
1112 [ + - + - : 2 : BOOST_CHECK(context.interrupt());
+ - ]
1113 [ + - ]: 2 : }
1114 : :
1115 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_chainman_regtest_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
1116 : : {
1117 [ + - ]: 1 : auto test_directory{TestDirectory{"regtest_test_bitcoin_kernel"}};
1118 : :
1119 [ + - ]: 1 : auto notifications{std::make_shared<TestKernelNotifications>()};
1120 [ + - + - : 3 : auto context{create_context(notifications, ChainType::REGTEST)};
- + ]
1121 : :
1122 : 1 : {
1123 : 1 : auto chainman{create_chainman(
1124 : : test_directory, /*reindex=*/false, /*wipe_chainstate=*/false,
1125 [ + - ]: 1 : /*block_tree_db_in_memory=*/false, /*chainstate_db_in_memory=*/false, context)};
1126 [ + + ]: 207 : for (const auto& data : REGTEST_BLOCK_DATA) {
1127 [ + - + - ]: 412 : Block block{hex_string_to_byte_vec(data)};
1128 [ + - ]: 206 : BlockHeader header = block.GetHeader();
1129 [ + - ]: 206 : BlockValidationState state = chainman->ProcessBlockHeader(header);
1130 [ + - + - : 412 : BOOST_CHECK(state.GetValidationMode() == ValidationMode::VALID);
+ - + - ]
1131 [ + - + - : 412 : BOOST_CHECK(state.GetBlockValidationResult() == BlockValidationResult::UNSET);
+ - + - ]
1132 [ + - + - ]: 206 : BlockTreeEntry entry{*chainman->GetBlockTreeEntry(header.Hash())};
1133 [ + - + - : 618 : BOOST_CHECK(!chainman->GetChain().Contains(entry));
+ - + - ]
1134 [ + - ]: 206 : BlockTreeEntry best_entry{chainman->GetBestEntry()};
1135 [ + - ]: 206 : BlockHash hash{entry.GetHash()};
1136 [ + - + - : 618 : BOOST_CHECK(hash == best_entry.GetHeader().Hash());
+ - + - ]
1137 : 206 : }
1138 : 1 : }
1139 : :
1140 : : // Validate 206 regtest blocks in total.
1141 : : // Stop halfway to check that it is possible to continue validating starting
1142 : : // from prior state.
1143 : 1 : const size_t mid{REGTEST_BLOCK_DATA.size() / 2};
1144 : :
1145 : 1 : {
1146 : 1 : auto chainman{create_chainman(
1147 : : test_directory, /*reindex=*/false, /*wipe_chainstate=*/false,
1148 [ + - ]: 1 : /*block_tree_db_in_memory=*/false, /*chainstate_db_in_memory=*/false, context)};
1149 [ + + ]: 104 : for (size_t i{0}; i < mid; i++) {
1150 [ + - + - ]: 206 : Block block{hex_string_to_byte_vec(REGTEST_BLOCK_DATA[i])};
1151 : 103 : bool new_block{false};
1152 [ + - + - : 206 : BOOST_CHECK(chainman->ProcessBlock(block, &new_block));
+ - + - ]
1153 [ + - + - ]: 206 : BOOST_CHECK(new_block);
1154 : 103 : }
1155 : 1 : }
1156 : :
1157 : 1 : auto chainman{create_chainman(
1158 : : test_directory, /*reindex=*/false, /*wipe_chainstate=*/false,
1159 [ + - ]: 1 : /*block_tree_db_in_memory=*/false, /*chainstate_db_in_memory=*/false, context)};
1160 : :
1161 [ + + ]: 104 : for (size_t i{mid}; i < REGTEST_BLOCK_DATA.size(); i++) {
1162 [ + - + - ]: 206 : Block block{hex_string_to_byte_vec(REGTEST_BLOCK_DATA[i])};
1163 : 103 : bool new_block{false};
1164 [ + - + - : 206 : BOOST_CHECK(chainman->ProcessBlock(block, &new_block));
+ - + - ]
1165 [ + - + - ]: 206 : BOOST_CHECK(new_block);
1166 : 103 : }
1167 : :
1168 [ + - ]: 1 : auto chain = chainman->GetChain();
1169 [ + - ]: 1 : auto tip = chain.Entries().back();
1170 [ + - ]: 2 : auto read_block = chainman->ReadBlock(tip).value();
1171 [ + - + - : 3 : check_equal(read_block.ToBytes(), hex_string_to_byte_vec(REGTEST_BLOCK_DATA[REGTEST_BLOCK_DATA.size() - 1]));
- + + - ]
1172 : :
1173 [ + - ]: 1 : auto tip_2 = tip.GetPrevious().value();
1174 [ + - ]: 2 : auto read_block_2 = chainman->ReadBlock(tip_2).value();
1175 [ + - + - : 3 : check_equal(read_block_2.ToBytes(), hex_string_to_byte_vec(REGTEST_BLOCK_DATA[REGTEST_BLOCK_DATA.size() - 2]));
- + + - ]
1176 : :
1177 [ + - + - ]: 1 : Txid txid = read_block.Transactions()[0].Txid();
1178 [ + - + - ]: 1 : Txid txid_2 = read_block_2.Transactions()[0].Txid();
1179 [ + - + - : 2 : BOOST_CHECK(txid != txid_2);
+ - + - ]
1180 [ + - + - : 2 : BOOST_CHECK(txid == txid);
+ - + - ]
1181 [ + - + - : 2 : CheckHandle(txid, txid_2);
+ - ]
1182 : :
1183 : 60 : auto find_transaction = [&chainman](const TxidView& target_txid) -> std::optional<Transaction> {
1184 : 59 : auto chain = chainman->GetChain();
1185 [ + - ]: 12504 : for (const auto block_tree_entry : chain.Entries()) {
1186 : 6252 : auto block{chainman->ReadBlock(block_tree_entry)};
1187 [ + - + - : 33662 : for (const TransactionView transaction : block->Transactions()) {
+ - ]
1188 [ + - + + ]: 15024 : if (transaction.Txid() == target_txid) {
1189 [ + - ]: 59 : return Transaction{transaction};
1190 : : }
1191 : : }
1192 : 6252 : }
1193 : 0 : return std::nullopt;
1194 : 1 : };
1195 : :
1196 [ + - + - : 415 : for (const auto block_tree_entry : chain.Entries()) {
+ - ]
1197 [ + - ]: 207 : auto block{chainman->ReadBlock(block_tree_entry)};
1198 [ + - + - : 934 : for (const auto transaction : block->Transactions()) {
+ - ]
1199 : 260 : std::vector<TransactionInput> inputs;
1200 : 260 : std::vector<TransactionOutput> spent_outputs;
1201 [ + - + - : 792 : for (const auto input : transaction.Inputs()) {
+ - ]
1202 [ + - ]: 266 : OutPointView point = input.OutPoint();
1203 [ + - + + ]: 266 : if (point.index() == std::numeric_limits<uint32_t>::max()) {
1204 : 207 : continue;
1205 : : }
1206 [ + - ]: 59 : inputs.emplace_back(input);
1207 [ + - + - : 177 : BOOST_CHECK(point.Txid() != transaction.Txid());
+ - + - +
- ]
1208 [ + - + - ]: 59 : std::optional<Transaction> tx = find_transaction(point.Txid());
1209 [ + - + - : 118 : BOOST_CHECK(tx.has_value());
+ - ]
1210 [ + - + - : 177 : BOOST_CHECK(point.Txid() == tx->Txid());
+ - + - +
- ]
1211 [ + - + - : 59 : spent_outputs.emplace_back(tx->GetOutput(point.index()));
+ - ]
1212 : 59 : }
1213 [ + - - + : 520 : BOOST_CHECK(inputs.size() == spent_outputs.size());
- + + - +
- ]
1214 : 260 : ScriptVerifyStatus status = ScriptVerifyStatus::OK;
1215 [ + - - + : 260 : const PrecomputedTransactionData precomputed_txdata{transaction, spent_outputs};
+ - ]
1216 [ - + + + ]: 319 : for (size_t i{0}; i < inputs.size(); ++i) {
1217 [ + - + - : 118 : BOOST_CHECK(spent_outputs[i].GetScriptPubkey().Verify(spent_outputs[i].Amount(), transaction, &precomputed_txdata, i, ScriptVerificationFlags::ALL, status));
+ - + - +
- + - ]
1218 : : }
1219 : 260 : }
1220 : 207 : }
1221 : :
1222 : : // Read spent outputs for current tip and its previous block
1223 [ + - ]: 1 : BlockSpentOutputs block_spent_outputs{chainman->ReadBlockSpentOutputs(tip)};
1224 [ + - + - ]: 1 : BlockSpentOutputs block_spent_outputs_prev{chainman->ReadBlockSpentOutputs(*tip.GetPrevious())};
1225 [ + - + - : 2 : CheckHandle(block_spent_outputs, block_spent_outputs_prev);
+ - ]
1226 [ + - + - ]: 1 : CheckRange(block_spent_outputs_prev.TxsSpentOutputs(), block_spent_outputs_prev.Count());
1227 [ + - + - : 1 : BOOST_CHECK_EQUAL(block_spent_outputs.Count(), 1);
+ - ]
1228 : :
1229 : : // Get transaction spent outputs from the last transaction in the two blocks
1230 [ + - + - ]: 1 : TransactionSpentOutputsView transaction_spent_outputs{block_spent_outputs.GetTxSpentOutputs(block_spent_outputs.Count() - 1)};
1231 [ + - ]: 1 : TransactionSpentOutputs owned_transaction_spent_outputs{transaction_spent_outputs};
1232 [ + - + - ]: 1 : TransactionSpentOutputs owned_transaction_spent_outputs_prev{block_spent_outputs_prev.GetTxSpentOutputs(block_spent_outputs_prev.Count() - 1)};
1233 [ + - + - : 2 : CheckHandle(owned_transaction_spent_outputs, owned_transaction_spent_outputs_prev);
+ - ]
1234 [ + - + - ]: 1 : CheckRange(transaction_spent_outputs.Coins(), transaction_spent_outputs.Count());
1235 : :
1236 : : // Get the last coin from the transaction spent outputs
1237 [ + - + - ]: 1 : CoinView coin{transaction_spent_outputs.GetCoin(transaction_spent_outputs.Count() - 1)};
1238 [ + - + - : 2 : BOOST_CHECK(!coin.IsCoinbase());
+ - + - ]
1239 [ + - ]: 1 : Coin owned_coin{coin};
1240 [ + - + - ]: 1 : Coin owned_coin_prev{owned_transaction_spent_outputs_prev.GetCoin(owned_transaction_spent_outputs_prev.Count() - 1)};
1241 [ + - + - : 2 : CheckHandle(owned_coin, owned_coin_prev);
+ - ]
1242 : :
1243 : : // Validate coin properties
1244 [ + - ]: 1 : TransactionOutputView output = coin.GetOutput();
1245 [ + - ]: 1 : uint32_t coin_height = coin.GetConfirmationHeight();
1246 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(coin_height, 143);
1247 [ + - + - : 1 : BOOST_CHECK_EQUAL(output.Amount(), 3949990974);
+ - ]
1248 : :
1249 : : // Test script pubkey serialization
1250 [ + - ]: 1 : auto script_pubkey = output.GetScriptPubkey();
1251 [ + - ]: 1 : auto script_pubkey_bytes{script_pubkey.ToBytes()};
1252 [ + - - + : 1 : BOOST_CHECK_EQUAL(script_pubkey_bytes.size(), 34);
+ - ]
1253 [ - + + - ]: 1 : auto round_trip_script_pubkey{ScriptPubkey(script_pubkey_bytes)};
1254 [ + - + - : 1 : BOOST_CHECK_EQUAL(round_trip_script_pubkey.ToBytes().size(), 34);
- + + - ]
1255 : :
1256 [ + - + - : 3 : for (const auto tx_spent_outputs : block_spent_outputs.TxsSpentOutputs()) {
+ - ]
1257 [ + - + - : 4 : for (const auto coins : tx_spent_outputs.Coins()) {
+ - ]
1258 [ + - + - : 2 : BOOST_CHECK_GT(coins.GetOutput().Amount(), 1);
+ - ]
1259 : : }
1260 : : }
1261 : :
1262 [ + - + - ]: 1 : CheckRange(chain.Entries(), chain.CountEntries());
1263 : :
1264 [ + - + - : 415 : for (const BlockTreeEntry entry : chain.Entries()) {
+ - ]
1265 [ + - ]: 207 : std::optional<Block> block{chainman->ReadBlock(entry)};
1266 [ + - ]: 207 : if (block) {
1267 [ + - + - : 934 : for (const TransactionView transaction : block->Transactions()) {
+ - ]
1268 [ + - + - : 1564 : for (const TransactionOutputView output : transaction.Outputs()) {
+ - ]
1269 : : // skip data carrier outputs
1270 [ + - + + ]: 1044 : if ((unsigned char)output.GetScriptPubkey().ToBytes()[0] == 0x6a) {
1271 : 206 : continue;
1272 : : }
1273 [ + - + - : 522 : BOOST_CHECK_GT(output.Amount(), 1);
+ - ]
1274 : : }
1275 : : }
1276 : : }
1277 : 207 : }
1278 : :
1279 : 1 : int32_t count{0};
1280 [ + - + - : 415 : for (const auto entry : chain.Entries()) {
+ - ]
1281 [ + - + - : 207 : BOOST_CHECK_EQUAL(entry.GetHeight(), count);
+ - ]
1282 : 207 : ++count;
1283 : : }
1284 [ + - + - : 1 : BOOST_CHECK_EQUAL(count, chain.CountEntries());
+ - ]
1285 : :
1286 : :
1287 [ + - + - : 5 : fs::remove(test_directory.m_directory / "blocks" / "blk00000.dat");
+ - + - ]
1288 [ + - + - : 2 : BOOST_CHECK(!chainman->ReadBlock(tip_2).has_value());
+ - + - ]
1289 [ + - + - : 5 : fs::remove(test_directory.m_directory / "blocks" / "rev00000.dat");
+ - + - ]
1290 [ + - - + : 2 : BOOST_CHECK_THROW(chainman->ReadBlockSpentOutputs(tip), std::runtime_error);
- - - - -
+ + - +
- ]
1291 [ + - ]: 2 : }
1292 : :
1293 : : // -----------------------------------------------------------------------------
1294 : : // CheckTransaction tests
1295 : : //
1296 : : // Transaction hex below is copied from src/test/data/tx_invalid.json (entries
1297 : : // marked "BADTX") and tx_valid.json. CheckTransaction performs only basic context-free
1298 : : // consensus checks and can only produce two outcomes:
1299 : : // - VALID (ValidationMode::VALID, TxValidationResult::UNSET)
1300 : : // - INVALID (ValidationMode::INVALID, TxValidationResult::CONSENSUS)
1301 : : // Other TxValidationResult values are set by higher-level validation and are
1302 : : // not reachable through btck_transaction_check.
1303 : : // -----------------------------------------------------------------------------
1304 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(btck_transaction_check_tests)
+ - + - -
+ + - + -
+ - + - +
- - + + -
+ - + - +
- + - - +
+ - + - +
- + - + -
- + + - +
- + - + -
+ - - + +
- ]
1305 : : {
1306 : 1 : using namespace btck;
1307 : :
1308 : 1 : constexpr std::string_view valid_tx_hex{
1309 : : "01000000010001000000000000000000000000000000000000000000000000000000000000"
1310 : : "000000006a473044022067288ea50aa799543a536ff9306f8e1cba05b9c6b10951175b92"
1311 : : "4f96732555ed022026d7b5265f38d21541519e4a1e55044d5b9e17e15cdbaf29ae3792e9"
1312 : : "9e883e7a012103ba8c8b86dea131c22ab967e6dd99bdae8eff7a1f75a2c35f1f944109e3"
1313 : : "fe5e22ffffffff010000000000000000015100000000"};
1314 : 1 : constexpr std::string_view no_outputs_tx_hex{
1315 : : "01000000010001000000000000000000000000000000000000000000000000000000000000"
1316 : : "000000006d483045022100f16703104aab4e4088317c862daec83440242411b039d14280e0"
1317 : : "3dd33b487ab802201318a7be236672c5c56083eb7a5a195bc57a40af7923ff8545016cd3b5"
1318 : : "71e2a601232103c40e5d339df3f30bf753e7e04450ae4ef76c9e45587d1d993bdc4cd06f06"
1319 : : "51c7acffffffff0000000000"};
1320 : :
1321 : 3 : auto expect_valid = [](std::string_view hex) {
1322 [ - + + - ]: 2 : Transaction tx{hex_string_to_byte_vec(hex)};
1323 [ + - ]: 2 : TxValidationState st;
1324 [ + - + - : 4 : BOOST_CHECK(CheckTransaction(tx, st));
+ - + - ]
1325 [ + - + - : 4 : BOOST_CHECK(st.GetValidationMode() == ValidationMode::VALID);
+ - + - ]
1326 [ + - + - : 4 : BOOST_CHECK(st.GetTxValidationResult() == TxValidationResult::UNSET);
+ - + - ]
1327 : 2 : };
1328 : :
1329 : 9 : auto expect_invalid = [](std::string_view hex) {
1330 [ - + + - ]: 8 : Transaction tx{hex_string_to_byte_vec(hex)};
1331 [ + - ]: 8 : TxValidationState st;
1332 [ + - + - : 16 : BOOST_CHECK(!CheckTransaction(tx, st));
+ - + - ]
1333 [ + - + - : 16 : BOOST_CHECK(st.GetValidationMode() == ValidationMode::INVALID);
+ - + - ]
1334 [ + - + - : 16 : BOOST_CHECK(st.GetTxValidationResult() == TxValidationResult::CONSENSUS);
+ - + - ]
1335 : 8 : };
1336 : :
1337 : : // Valid: simple 1-in 1-out transaction (from tx_valid.json)
1338 : 1 : expect_valid(valid_tx_hex);
1339 : :
1340 : : // Valid coinbase with scriptSig size 2 (from tx_valid.json)
1341 : 1 : expect_valid(
1342 : : "01000000010000000000000000000000000000000000000000000000000000000000000000"
1343 : : "ffffffff025151ffffffff010000000000000000015100000000");
1344 : :
1345 : : // No outputs (BADTX from tx_invalid.json)
1346 : 1 : expect_invalid(no_outputs_tx_hex);
1347 : :
1348 : 1 : {
1349 [ - + + - ]: 1 : Transaction valid_tx{hex_string_to_byte_vec(valid_tx_hex)};
1350 [ + - + - ]: 2 : Transaction invalid_tx{hex_string_to_byte_vec(no_outputs_tx_hex)};
1351 [ + - ]: 1 : TxValidationState state;
1352 : :
1353 [ + - + - : 2 : BOOST_CHECK(btck_transaction_check(valid_tx.get(), state.get()) == 1);
+ - + - ]
1354 [ + - + - : 2 : BOOST_CHECK(state.GetValidationMode() == ValidationMode::VALID);
+ - + - ]
1355 [ + - + - : 2 : BOOST_CHECK(state.GetTxValidationResult() == TxValidationResult::UNSET);
+ - + - ]
1356 : :
1357 [ + - + - : 2 : BOOST_CHECK(btck_transaction_check(invalid_tx.get(), state.get()) == 0);
+ - + - ]
1358 [ + - + - : 2 : BOOST_CHECK(state.GetValidationMode() == ValidationMode::INVALID);
+ - + - ]
1359 [ + - + - : 2 : BOOST_CHECK(state.GetTxValidationResult() == TxValidationResult::CONSENSUS);
+ - + - ]
1360 : 1 : }
1361 : :
1362 : : // Negative output (BADTX)
1363 : 1 : expect_invalid(
1364 : : "01000000010001000000000000000000000000000000000000000000000000000000000000"
1365 : : "000000006d4830450220063222cbb128731fc09de0d7323746539166544d6c1df84d867cce"
1366 : : "a84bcc8903022100bf568e8552844de664cd41648a031554327aa8844af34b4f27397c65b9"
1367 : : "2c04de0123210243ec37dee0e2e053a9c976f43147e79bc7d9dc606ea51010af1ac80db6b0"
1368 : : "69e1acffffffff01ffffffffffffffff015100000000");
1369 : :
1370 : : // MAX_MONEY + 1 output (BADTX)
1371 : 1 : expect_invalid(
1372 : : "01000000010001000000000000000000000000000000000000000000000000000000000000"
1373 : : "000000006e493046022100e1eadba00d9296c743cb6ecc703fd9ddc9b3cd12906176a226ae"
1374 : : "4c18d6b00796022100a71aef7d2874deff681ba6080f1b278bac7bb99c61b08a85f4311970"
1375 : : "ffe7f63f012321030c0588dc44d92bdcbf8e72093466766fdc265ead8db64517b0c542275b"
1376 : : "70fffbacffffffff010140075af0750700015100000000");
1377 : :
1378 : : // MAX_MONEY output + 1 output: sum exceeds MAX_MONEY (BADTX)
1379 : 1 : expect_invalid(
1380 : : "01000000010001000000000000000000000000000000000000000000000000000000000000"
1381 : : "000000006d483045022027deccc14aa6668e78a8c9da3484fbcd4f9dcc9bb7d1b85146314b"
1382 : : "21b9ae4d86022100d0b43dece8cfb07348de0ca8bc5b86276fa88f7f2138381128b7c36ab2"
1383 : : "e42264012321029bb13463ddd5d2cc05da6e84e37536cb9525703cfd8f43afdb414988987a"
1384 : : "92f6acffffffff020040075af075070001510001000000000000015100000000");
1385 : :
1386 : : // Duplicate inputs (BADTX)
1387 : 1 : expect_invalid(
1388 : : "01000000020001000000000000000000000000000000000000000000000000000000000000"
1389 : : "000000006c47304402204bb1197053d0d7799bf1b30cd503c44b58d6240cccbdc85b6fe76d"
1390 : : "087980208f02204beeed78200178ffc6c74237bb74b3f276bbb4098b5605d814304fe128bf"
1391 : : "1431012321039e8815e15952a7c3fada1905f8cf55419837133bd7756c0ef14fc8dfe50c0d"
1392 : : "eaacffffffff0001000000000000000000000000000000000000000000000000000000000000"
1393 : : "000000006c47304402202306489afef52a6f62e90bf750bbcdf40c06f5c6b138286e6b6b8617"
1394 : : "6bb9341802200dba98486ea68380f47ebb19a7df173b99e6bc9c681d6ccf3bde31465d1f16"
1395 : : "b3012321039e8815e15952a7c3fada1905f8cf55419837133bd7756c0ef14fc8dfe50c0dea"
1396 : : "acffffffff010000000000000000015100000000");
1397 : :
1398 : : // Coinbase with scriptSig size 1: too small (BADTX)
1399 : 1 : expect_invalid(
1400 : : "01000000010000000000000000000000000000000000000000000000000000000000000000"
1401 : : "ffffffff0151ffffffff010000000000000000015100000000");
1402 : :
1403 : : // Coinbase with scriptSig size 101: too large (BADTX)
1404 : 1 : expect_invalid(
1405 : : "01000000010000000000000000000000000000000000000000000000000000000000000000"
1406 : : "ffffffff6551515151515151515151515151515151515151515151515151515151515151515151"
1407 : : "515151515151515151515151515151515151515151515151515151515151515151515151515151"
1408 : : "51515151515151515151515151515151515151515151515151515151ffffffff01000000000000"
1409 : : "0000015100000000");
1410 : :
1411 : : // Null prevout in non-coinbase: two inputs, one is null (BADTX)
1412 : 1 : expect_invalid(
1413 : : "01000000020000000000000000000000000000000000000000000000000000000000000000"
1414 : : "ffffffff00ffffffff000100000000000000000000000000000000000000000000000000000000"
1415 : : "00000000000000ffffffff010000000000000000015100000000");
1416 : 1 : }
|