LCOV - code coverage report
Current view: top level - src/test/kernel - test_kernel.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 95.4 % 937 894
Test Date: 2026-10-11 08:27:57 Functions: 95.5 % 89 85
Branches: 50.2 % 4330 2174

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

Generated by: LCOV version 2.0-1