LCOV - code coverage report
Current view: top level - src/test/kernel - test_kernel.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 94.4 % 798 753
Test Date: 2026-04-27 06:44:50 Functions: 93.6 % 78 73
Branches: 50.2 % 3486 1750

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

Generated by: LCOV version 2.0-1