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