LCOV - code coverage report
Current view: top level - src/kernel - bitcoinkernel.cpp (source / functions) Coverage Total Hit
Test: total_coverage.info Lines: 82.9 % 619 513
Test Date: 2025-12-25 05:18:09 Functions: 95.7 % 164 157
Branches: 51.1 % 595 304

             Branch data     Line data    Source code
       1                 :             : // Copyright (c) 2022-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                 :             : #define BITCOINKERNEL_BUILD
       6                 :             : 
       7                 :             : #include <kernel/bitcoinkernel.h>
       8                 :             : 
       9                 :             : #include <chain.h>
      10                 :             : #include <coins.h>
      11                 :             : #include <consensus/amount.h>
      12                 :             : #include <consensus/validation.h>
      13                 :             : #include <kernel/caches.h>
      14                 :             : #include <kernel/chainparams.h>
      15                 :             : #include <kernel/checks.h>
      16                 :             : #include <kernel/context.h>
      17                 :             : #include <kernel/cs_main.h>
      18                 :             : #include <kernel/notifications_interface.h>
      19                 :             : #include <kernel/warning.h>
      20                 :             : #include <logging.h>
      21                 :             : #include <node/blockstorage.h>
      22                 :             : #include <node/chainstate.h>
      23                 :             : #include <primitives/block.h>
      24                 :             : #include <primitives/transaction.h>
      25                 :             : #include <script/interpreter.h>
      26                 :             : #include <script/script.h>
      27                 :             : #include <serialize.h>
      28                 :             : #include <streams.h>
      29                 :             : #include <sync.h>
      30                 :             : #include <tinyformat.h>
      31                 :             : #include <uint256.h>
      32                 :             : #include <undo.h>
      33                 :             : #include <util/fs.h>
      34                 :             : #include <util/result.h>
      35                 :             : #include <util/signalinterrupt.h>
      36                 :             : #include <util/task_runner.h>
      37                 :             : #include <util/translation.h>
      38                 :             : #include <validation.h>
      39                 :             : #include <validationinterface.h>
      40                 :             : 
      41                 :             : #include <cassert>
      42                 :             : #include <cstddef>
      43                 :             : #include <cstring>
      44                 :             : #include <exception>
      45                 :             : #include <functional>
      46                 :             : #include <list>
      47                 :             : #include <memory>
      48                 :             : #include <span>
      49                 :             : #include <string>
      50                 :             : #include <tuple>
      51                 :             : #include <utility>
      52                 :             : #include <vector>
      53                 :             : 
      54                 :             : using kernel::ChainstateRole;
      55                 :             : using util::ImmediateTaskRunner;
      56                 :             : 
      57                 :             : // Define G_TRANSLATION_FUN symbol in libbitcoinkernel library so users of the
      58                 :             : // library aren't required to export this symbol
      59                 :             : extern const std::function<std::string(const char*)> G_TRANSLATION_FUN{nullptr};
      60                 :             : 
      61                 :             : static const kernel::Context btck_context_static{};
      62                 :             : 
      63                 :             : namespace {
      64                 :             : 
      65                 :          35 : bool is_valid_flag_combination(script_verify_flags flags)
      66                 :             : {
      67   [ -  +  -  - ]:          35 :     if (flags & SCRIPT_VERIFY_CLEANSTACK && ~flags & (SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS)) return false;
      68   [ +  +  -  + ]:          35 :     if (flags & SCRIPT_VERIFY_WITNESS && ~flags & SCRIPT_VERIFY_P2SH) return false;
      69                 :             :     return true;
      70                 :             : }
      71                 :             : 
      72                 :             : class WriterStream
      73                 :             : {
      74                 :             : private:
      75                 :             :     btck_WriteBytes m_writer;
      76                 :             :     void* m_user_data;
      77                 :             : 
      78                 :             : public:
      79                 :          32 :     WriterStream(btck_WriteBytes writer, void* user_data)
      80                 :          32 :         : m_writer{writer}, m_user_data{user_data} {}
      81                 :             : 
      82                 :             :     //
      83                 :             :     // Stream subset
      84                 :             :     //
      85                 :         785 :     void write(std::span<const std::byte> src)
      86                 :             :     {
      87         [ -  + ]:         785 :         if (m_writer(std::data(src), src.size(), m_user_data) != 0) {
      88         [ #  # ]:           0 :             throw std::runtime_error("Failed to write serialization data");
      89                 :             :         }
      90                 :         785 :     }
      91                 :             : 
      92                 :             :     template <typename T>
      93                 :          32 :     WriterStream& operator<<(const T& obj)
      94                 :             :     {
      95   [ +  -  +  - ]:          32 :         ::Serialize(*this, obj);
      96                 :          32 :         return *this;
      97                 :             :     }
      98                 :             : };
      99                 :             : 
     100                 :             : template <typename C, typename CPP>
     101                 :             : struct Handle {
     102                 :             :     static C* ref(CPP* cpp_type)
     103                 :             :     {
     104                 :             :         return reinterpret_cast<C*>(cpp_type);
     105                 :             :     }
     106                 :             : 
     107                 :             :     static const C* ref(const CPP* cpp_type)
     108                 :             :     {
     109                 :             :         return reinterpret_cast<const C*>(cpp_type);
     110                 :             :     }
     111                 :             : 
     112                 :             :     template <typename... Args>
     113                 :        3309 :     static C* create(Args&&... args)
     114                 :             :     {
     115                 :        3309 :         auto cpp_obj{std::make_unique<CPP>(std::forward<Args>(args)...)};
     116                 :        6618 :         return reinterpret_cast<C*>(cpp_obj.release());
     117                 :        3309 :     }
     118                 :             : 
     119                 :         197 :     static C* copy(const C* ptr)
     120                 :             :     {
     121                 :         197 :         auto cpp_obj{std::make_unique<CPP>(get(ptr))};
     122                 :         394 :         return reinterpret_cast<C*>(cpp_obj.release());
     123                 :         197 :     }
     124                 :             : 
     125                 :             :     static const CPP& get(const C* ptr)
     126                 :             :     {
     127                 :             :         return *reinterpret_cast<const CPP*>(ptr);
     128                 :             :     }
     129                 :             : 
     130                 :             :     static CPP& get(C* ptr)
     131                 :             :     {
     132                 :             :         return *reinterpret_cast<CPP*>(ptr);
     133                 :             :     }
     134                 :             : 
     135                 :        3519 :     static void operator delete(void* ptr)
     136                 :             :     {
     137         [ +  - ]:        6943 :         delete reinterpret_cast<CPP*>(ptr);
     138                 :        3519 :     }
     139                 :             : };
     140                 :             : 
     141                 :             : } // namespace
     142                 :             : 
     143                 :             : struct btck_BlockTreeEntry: Handle<btck_BlockTreeEntry, CBlockIndex> {};
     144                 :             : struct btck_Block : Handle<btck_Block, std::shared_ptr<const CBlock>> {};
     145                 :             : struct btck_BlockValidationState : Handle<btck_BlockValidationState, BlockValidationState> {};
     146                 :             : 
     147                 :             : namespace {
     148                 :             : 
     149                 :           2 : BCLog::Level get_bclog_level(btck_LogLevel level)
     150                 :             : {
     151   [ -  +  -  - ]:           2 :     switch (level) {
     152                 :             :     case btck_LogLevel_INFO: {
     153                 :             :         return BCLog::Level::Info;
     154                 :             :     }
     155                 :           0 :     case btck_LogLevel_DEBUG: {
     156                 :           0 :         return BCLog::Level::Debug;
     157                 :             :     }
     158                 :           2 :     case btck_LogLevel_TRACE: {
     159                 :           2 :         return BCLog::Level::Trace;
     160                 :             :     }
     161                 :             :     }
     162                 :           0 :     assert(false);
     163                 :             : }
     164                 :             : 
     165                 :           6 : BCLog::LogFlags get_bclog_flag(btck_LogCategory category)
     166                 :             : {
     167   [ -  -  -  -  :           6 :     switch (category) {
          -  -  -  +  +  
                -  -  + ]
     168                 :             :     case btck_LogCategory_BENCH: {
     169                 :             :         return BCLog::LogFlags::BENCH;
     170                 :             :     }
     171                 :           0 :     case btck_LogCategory_BLOCKSTORAGE: {
     172                 :           0 :         return BCLog::LogFlags::BLOCKSTORAGE;
     173                 :             :     }
     174                 :           0 :     case btck_LogCategory_COINDB: {
     175                 :           0 :         return BCLog::LogFlags::COINDB;
     176                 :             :     }
     177                 :           0 :     case btck_LogCategory_LEVELDB: {
     178                 :           0 :         return BCLog::LogFlags::LEVELDB;
     179                 :             :     }
     180                 :           0 :     case btck_LogCategory_MEMPOOL: {
     181                 :           0 :         return BCLog::LogFlags::MEMPOOL;
     182                 :             :     }
     183                 :           0 :     case btck_LogCategory_PRUNE: {
     184                 :           0 :         return BCLog::LogFlags::PRUNE;
     185                 :             :     }
     186                 :           0 :     case btck_LogCategory_RAND: {
     187                 :           0 :         return BCLog::LogFlags::RAND;
     188                 :             :     }
     189                 :           0 :     case btck_LogCategory_REINDEX: {
     190                 :           0 :         return BCLog::LogFlags::REINDEX;
     191                 :             :     }
     192                 :           2 :     case btck_LogCategory_VALIDATION: {
     193                 :           2 :         return BCLog::LogFlags::VALIDATION;
     194                 :             :     }
     195                 :           2 :     case btck_LogCategory_KERNEL: {
     196                 :           2 :         return BCLog::LogFlags::KERNEL;
     197                 :             :     }
     198                 :           0 :     case btck_LogCategory_ALL: {
     199                 :           0 :         return BCLog::LogFlags::ALL;
     200                 :             :     }
     201                 :             :     }
     202                 :           0 :     assert(false);
     203                 :             : }
     204                 :             : 
     205                 :         851 : btck_SynchronizationState cast_state(SynchronizationState state)
     206                 :             : {
     207   [ +  -  -  + ]:         851 :     switch (state) {
     208                 :             :     case SynchronizationState::INIT_REINDEX:
     209                 :             :         return btck_SynchronizationState_INIT_REINDEX;
     210                 :         848 :     case SynchronizationState::INIT_DOWNLOAD:
     211                 :         848 :         return btck_SynchronizationState_INIT_DOWNLOAD;
     212                 :           0 :     case SynchronizationState::POST_INIT:
     213                 :           0 :         return btck_SynchronizationState_POST_INIT;
     214                 :             :     } // no default case, so the compiler can warn about missing cases
     215                 :           0 :     assert(false);
     216                 :             : }
     217                 :             : 
     218                 :         431 : btck_Warning cast_btck_warning(kernel::Warning warning)
     219                 :             : {
     220      [ +  -  - ]:         431 :     switch (warning) {
     221                 :             :     case kernel::Warning::UNKNOWN_NEW_RULES_ACTIVATED:
     222                 :             :         return btck_Warning_UNKNOWN_NEW_RULES_ACTIVATED;
     223                 :         431 :     case kernel::Warning::LARGE_WORK_INVALID_CHAIN:
     224                 :         431 :         return btck_Warning_LARGE_WORK_INVALID_CHAIN;
     225                 :             :     } // no default case, so the compiler can warn about missing cases
     226                 :           0 :     assert(false);
     227                 :             : }
     228                 :             : 
     229                 :             : struct LoggingConnection {
     230                 :             :     std::unique_ptr<std::list<std::function<void(const std::string&)>>::iterator> m_connection;
     231                 :             :     void* m_user_data;
     232                 :             :     std::function<void(void* user_data)> m_deleter;
     233                 :             : 
     234                 :           8 :     LoggingConnection(btck_LogCallback callback, void* user_data, btck_DestroyCallback user_data_destroy_callback)
     235         [ +  - ]:           8 :     {
     236         [ +  - ]:           8 :         LOCK(cs_main);
     237                 :             : 
     238   [ -  +  +  -  :         171 :         auto connection{LogInstance().PushBackCallback([callback, user_data](const std::string& str) { callback(user_data, str.c_str(), str.length()); })};
                   +  - ]
     239                 :             : 
     240                 :             :         // Only start logging if we just added the connection.
     241   [ +  -  +  -  :           8 :         if (LogInstance().NumConnections() == 1 && !LogInstance().StartLogging()) {
          +  +  +  -  +  
                -  -  + ]
     242         [ #  # ]:           0 :             LogError("Logger start failed.");
     243   [ #  #  #  # ]:           0 :             LogInstance().DeleteCallback(connection);
     244   [ #  #  #  # ]:           0 :             if (user_data && user_data_destroy_callback) {
     245         [ #  # ]:           0 :                 user_data_destroy_callback(user_data);
     246                 :             :             }
     247         [ #  # ]:           0 :             throw std::runtime_error("Failed to start logging");
     248                 :             :         }
     249                 :             : 
     250         [ +  - ]:           8 :         m_connection = std::make_unique<std::list<std::function<void(const std::string&)>>::iterator>(connection);
     251                 :           8 :         m_user_data = user_data;
     252                 :           8 :         m_deleter = user_data_destroy_callback;
     253                 :             : 
     254   [ +  -  +  +  :           8 :         LogDebug(BCLog::KERNEL, "Logger connected.");
             +  -  +  - ]
     255                 :           8 :     }
     256                 :             : 
     257                 :           8 :     ~LoggingConnection()
     258                 :             :     {
     259                 :           8 :         LOCK(cs_main);
     260         [ +  + ]:           8 :         LogDebug(BCLog::KERNEL, "Logger disconnecting.");
     261                 :             : 
     262                 :             :         // Switch back to buffering by calling DisconnectTestLogger if the
     263                 :             :         // connection that we are about to remove is the last one.
     264         [ +  + ]:           8 :         if (LogInstance().NumConnections() == 1) {
     265                 :           7 :             LogInstance().DisconnectTestLogger();
     266                 :             :         } else {
     267                 :           1 :             LogInstance().DeleteCallback(*m_connection);
     268                 :             :         }
     269                 :             : 
     270         [ +  - ]:           8 :         m_connection.reset();
     271   [ +  -  +  - ]:           8 :         if (m_user_data && m_deleter) {
     272                 :           8 :             m_deleter(m_user_data);
     273                 :             :         }
     274                 :           8 :     }
     275                 :             : };
     276                 :             : 
     277                 :             : class KernelNotifications final : public kernel::Notifications
     278                 :             : {
     279                 :             : private:
     280                 :             :     btck_NotificationInterfaceCallbacks m_cbs;
     281                 :             : 
     282                 :             : public:
     283                 :          18 :     KernelNotifications(btck_NotificationInterfaceCallbacks cbs)
     284                 :          18 :         : m_cbs{cbs}
     285                 :             :     {
     286                 :             :     }
     287                 :             : 
     288                 :          18 :     ~KernelNotifications()
     289                 :          18 :     {
     290   [ +  +  +  - ]:          18 :         if (m_cbs.user_data && m_cbs.user_data_destroy) {
     291                 :          12 :             m_cbs.user_data_destroy(m_cbs.user_data);
     292                 :             :         }
     293                 :          18 :         m_cbs.user_data_destroy = nullptr;
     294                 :          18 :         m_cbs.user_data = nullptr;
     295                 :          18 :     }
     296                 :             : 
     297                 :         433 :     kernel::InterruptResult blockTip(SynchronizationState state, const CBlockIndex& index, double verification_progress) override
     298                 :             :     {
     299         [ +  + ]:         433 :         if (m_cbs.block_tip) m_cbs.block_tip(m_cbs.user_data, cast_state(state), btck_BlockTreeEntry::ref(&index), verification_progress);
     300                 :         433 :         return {};
     301                 :             :     }
     302                 :         420 :     void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) override
     303                 :             :     {
     304   [ +  -  +  - ]:         840 :         if (m_cbs.header_tip) m_cbs.header_tip(m_cbs.user_data, cast_state(state), height, timestamp, presync ? 1 : 0);
     305                 :         420 :     }
     306                 :          24 :     void progress(const bilingual_str& title, int progress_percent, bool resume_possible) override
     307                 :             :     {
     308   [ +  +  +  -  :          46 :         if (m_cbs.progress) m_cbs.progress(m_cbs.user_data, title.original.c_str(), title.original.length(), progress_percent, resume_possible ? 1 : 0);
                   -  + ]
     309                 :          24 :     }
     310                 :           0 :     void warningSet(kernel::Warning id, const bilingual_str& message) override
     311                 :             :     {
     312   [ #  #  #  # ]:           0 :         if (m_cbs.warning_set) m_cbs.warning_set(m_cbs.user_data, cast_btck_warning(id), message.original.c_str(), message.original.length());
     313                 :           0 :     }
     314                 :         433 :     void warningUnset(kernel::Warning id) override
     315                 :             :     {
     316         [ +  + ]:         433 :         if (m_cbs.warning_unset) m_cbs.warning_unset(m_cbs.user_data, cast_btck_warning(id));
     317                 :         433 :     }
     318                 :           0 :     void flushError(const bilingual_str& message) override
     319                 :             :     {
     320   [ #  #  #  # ]:           0 :         if (m_cbs.flush_error) m_cbs.flush_error(m_cbs.user_data, message.original.c_str(), message.original.length());
     321                 :           0 :     }
     322                 :           0 :     void fatalError(const bilingual_str& message) override
     323                 :             :     {
     324   [ #  #  #  # ]:           0 :         if (m_cbs.fatal_error) m_cbs.fatal_error(m_cbs.user_data, message.original.c_str(), message.original.length());
     325                 :           0 :     }
     326                 :             : };
     327                 :             : 
     328                 :             : class KernelValidationInterface final : public CValidationInterface
     329                 :             : {
     330                 :             : public:
     331                 :             :     btck_ValidationInterfaceCallbacks m_cbs;
     332                 :             : 
     333                 :           5 :     explicit KernelValidationInterface(const btck_ValidationInterfaceCallbacks vi_cbs) : m_cbs{vi_cbs} {}
     334                 :             : 
     335                 :           5 :     ~KernelValidationInterface()
     336                 :           5 :     {
     337   [ +  -  +  - ]:           5 :         if (m_cbs.user_data && m_cbs.user_data_destroy) {
     338                 :           5 :             m_cbs.user_data_destroy(m_cbs.user_data);
     339                 :             :         }
     340                 :           5 :         m_cbs.user_data = nullptr;
     341                 :           5 :         m_cbs.user_data_destroy = nullptr;
     342                 :           5 :     }
     343                 :             : 
     344                 :             : protected:
     345                 :           4 :     void BlockChecked(const std::shared_ptr<const CBlock>& block, const BlockValidationState& stateIn) override
     346                 :             :     {
     347         [ +  - ]:           4 :         if (m_cbs.block_checked) {
     348                 :           4 :             m_cbs.block_checked(m_cbs.user_data,
     349                 :             :                                 btck_Block::copy(btck_Block::ref(&block)),
     350                 :             :                                 btck_BlockValidationState::ref(&stateIn));
     351                 :             :         }
     352                 :           4 :     }
     353                 :             : 
     354                 :           0 :     void NewPoWValidBlock(const CBlockIndex* pindex, const std::shared_ptr<const CBlock>& block) override
     355                 :             :     {
     356         [ #  # ]:           0 :         if (m_cbs.pow_valid_block) {
     357                 :           0 :             m_cbs.pow_valid_block(m_cbs.user_data,
     358                 :             :                                   btck_Block::copy(btck_Block::ref(&block)),
     359                 :             :                                   btck_BlockTreeEntry::ref(pindex));
     360                 :             :         }
     361                 :           0 :     }
     362                 :             : 
     363                 :           3 :     void BlockConnected(const ChainstateRole& role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override
     364                 :             :     {
     365         [ +  - ]:           3 :         if (m_cbs.block_connected) {
     366                 :           3 :             m_cbs.block_connected(m_cbs.user_data,
     367                 :             :                                   btck_Block::copy(btck_Block::ref(&block)),
     368                 :             :                                   btck_BlockTreeEntry::ref(pindex));
     369                 :             :         }
     370                 :           3 :     }
     371                 :             : 
     372                 :           0 :     void BlockDisconnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override
     373                 :             :     {
     374         [ #  # ]:           0 :         if (m_cbs.block_disconnected) {
     375                 :           0 :             m_cbs.block_disconnected(m_cbs.user_data,
     376                 :             :                                      btck_Block::copy(btck_Block::ref(&block)),
     377                 :             :                                      btck_BlockTreeEntry::ref(pindex));
     378                 :             :         }
     379                 :           0 :     }
     380                 :             : };
     381                 :             : 
     382                 :          18 : struct ContextOptions {
     383                 :             :     mutable Mutex m_mutex;
     384                 :             :     std::unique_ptr<const CChainParams> m_chainparams GUARDED_BY(m_mutex);
     385                 :             :     std::shared_ptr<KernelNotifications> m_notifications GUARDED_BY(m_mutex);
     386                 :             :     std::shared_ptr<KernelValidationInterface> m_validation_interface GUARDED_BY(m_mutex);
     387                 :             : };
     388                 :             : 
     389                 :             : class Context
     390                 :             : {
     391                 :             : public:
     392                 :             :     std::unique_ptr<kernel::Context> m_context;
     393                 :             : 
     394                 :             :     std::shared_ptr<KernelNotifications> m_notifications;
     395                 :             : 
     396                 :             :     std::unique_ptr<util::SignalInterrupt> m_interrupt;
     397                 :             : 
     398                 :             :     std::unique_ptr<ValidationSignals> m_signals;
     399                 :             : 
     400                 :             :     std::unique_ptr<const CChainParams> m_chainparams;
     401                 :             : 
     402                 :             :     std::shared_ptr<KernelValidationInterface> m_validation_interface;
     403                 :             : 
     404                 :          18 :     Context(const ContextOptions* options, bool& sane)
     405                 :          18 :         : m_context{std::make_unique<kernel::Context>()},
     406   [ +  -  +  - ]:          18 :           m_interrupt{std::make_unique<util::SignalInterrupt>()}
     407                 :             :     {
     408         [ +  - ]:          18 :         if (options) {
     409         [ +  - ]:          18 :             LOCK(options->m_mutex);
     410         [ +  + ]:          18 :             if (options->m_chainparams) {
     411         [ +  - ]:          12 :                 m_chainparams = std::make_unique<const CChainParams>(*options->m_chainparams);
     412                 :             :             }
     413         [ +  + ]:          18 :             if (options->m_notifications) {
     414                 :          12 :                 m_notifications = options->m_notifications;
     415                 :             :             }
     416         [ +  + ]:          18 :             if (options->m_validation_interface) {
     417   [ +  -  +  - ]:           5 :                 m_signals = std::make_unique<ValidationSignals>(std::make_unique<ImmediateTaskRunner>());
     418                 :           5 :                 m_validation_interface = options->m_validation_interface;
     419   [ +  -  +  - ]:          15 :                 m_signals->RegisterSharedValidationInterface(m_validation_interface);
     420                 :             :             }
     421                 :          18 :         }
     422                 :             : 
     423         [ +  + ]:          18 :         if (!m_chainparams) {
     424         [ +  - ]:           6 :             m_chainparams = CChainParams::Main();
     425                 :             :         }
     426         [ +  + ]:          18 :         if (!m_notifications) {
     427         [ +  - ]:          12 :             m_notifications = std::make_shared<KernelNotifications>(btck_NotificationInterfaceCallbacks{
     428         [ -  + ]:           6 :                 nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr});
     429                 :             :         }
     430                 :             : 
     431   [ +  -  -  + ]:          18 :         if (!kernel::SanityChecks(*m_context)) {
     432                 :           0 :             sane = false;
     433                 :             :         }
     434   [ -  -  -  - ]:          18 :     }
     435                 :             : 
     436                 :          18 :     ~Context()
     437                 :             :     {
     438         [ +  + ]:          18 :         if (m_signals) {
     439   [ +  -  +  - ]:          15 :             m_signals->UnregisterSharedValidationInterface(m_validation_interface);
     440                 :             :         }
     441   [ +  +  +  - ]:          41 :     }
     442                 :             : };
     443                 :             : 
     444                 :             : //! Helper struct to wrap the ChainstateManager-related Options
     445                 :             : struct ChainstateManagerOptions {
     446                 :             :     mutable Mutex m_mutex;
     447                 :             :     ChainstateManager::Options m_chainman_options GUARDED_BY(m_mutex);
     448                 :             :     node::BlockManager::Options m_blockman_options GUARDED_BY(m_mutex);
     449                 :             :     std::shared_ptr<const Context> m_context;
     450                 :             :     node::ChainstateLoadOptions m_chainstate_load_options GUARDED_BY(m_mutex);
     451                 :             : 
     452                 :          14 :     ChainstateManagerOptions(const std::shared_ptr<const Context>& context, const fs::path& data_dir, const fs::path& blocks_dir)
     453         [ +  - ]:          14 :         : m_chainman_options{ChainstateManager::Options{
     454                 :          14 :               .chainparams = *context->m_chainparams,
     455                 :             :               .datadir = data_dir,
     456         [ +  - ]:          14 :               .notifications = *context->m_notifications,
     457         [ +  - ]:          14 :               .signals = context->m_signals.get()}},
     458         [ +  - ]:          28 :           m_blockman_options{node::BlockManager::Options{
     459         [ +  - ]:          14 :               .chainparams = *context->m_chainparams,
     460                 :             :               .blocks_dir = blocks_dir,
     461         [ +  - ]:          14 :               .notifications = *context->m_notifications,
     462                 :             :               .block_tree_db_params = DBParams{
     463   [ +  -  +  - ]:          42 :                   .path = data_dir / "blocks" / "index",
     464                 :          14 :                   .cache_bytes = kernel::CacheSizes{DEFAULT_KERNEL_CACHE}.block_tree_db,
     465                 :             :               }}},
     466         [ +  - ]:          42 :           m_context{context}, m_chainstate_load_options{node::ChainstateLoadOptions{}}
     467                 :             :     {
     468                 :          14 :     }
     469                 :             : };
     470                 :             : 
     471                 :             : struct ChainMan {
     472                 :             :     std::unique_ptr<ChainstateManager> m_chainman;
     473                 :             :     std::shared_ptr<const Context> m_context;
     474                 :             : 
     475                 :          14 :     ChainMan(std::unique_ptr<ChainstateManager> chainman, std::shared_ptr<const Context> context)
     476                 :          14 :         : m_chainman(std::move(chainman)), m_context(std::move(context)) {}
     477                 :             : };
     478                 :             : 
     479                 :             : } // namespace
     480                 :             : 
     481                 :             : struct btck_Transaction : Handle<btck_Transaction, std::shared_ptr<const CTransaction>> {};
     482                 :             : struct btck_TransactionOutput : Handle<btck_TransactionOutput, CTxOut> {};
     483                 :             : struct btck_ScriptPubkey : Handle<btck_ScriptPubkey, CScript> {};
     484                 :             : struct btck_LoggingConnection : Handle<btck_LoggingConnection, LoggingConnection> {};
     485                 :             : struct btck_ContextOptions : Handle<btck_ContextOptions, ContextOptions> {};
     486                 :             : struct btck_Context : Handle<btck_Context, std::shared_ptr<const Context>> {};
     487                 :             : struct btck_ChainParameters : Handle<btck_ChainParameters, CChainParams> {};
     488                 :             : struct btck_ChainstateManagerOptions : Handle<btck_ChainstateManagerOptions, ChainstateManagerOptions> {};
     489                 :             : struct btck_ChainstateManager : Handle<btck_ChainstateManager, ChainMan> {};
     490                 :             : struct btck_Chain : Handle<btck_Chain, CChain> {};
     491                 :             : struct btck_BlockSpentOutputs : Handle<btck_BlockSpentOutputs, std::shared_ptr<CBlockUndo>> {};
     492                 :             : struct btck_TransactionSpentOutputs : Handle<btck_TransactionSpentOutputs, CTxUndo> {};
     493                 :             : struct btck_Coin : Handle<btck_Coin, Coin> {};
     494                 :             : struct btck_BlockHash : Handle<btck_BlockHash, uint256> {};
     495                 :             : struct btck_TransactionInput : Handle<btck_TransactionInput, CTxIn> {};
     496                 :             : struct btck_TransactionOutPoint: Handle<btck_TransactionOutPoint, COutPoint> {};
     497                 :             : struct btck_Txid: Handle<btck_Txid, Txid> {};
     498                 :             : 
     499                 :          11 : btck_Transaction* btck_transaction_create(const void* raw_transaction, size_t raw_transaction_len)
     500                 :             : {
     501         [ +  - ]:          11 :     if (raw_transaction == nullptr && raw_transaction_len != 0) {
     502                 :             :         return nullptr;
     503                 :             :     }
     504                 :          11 :     try {
     505         [ +  - ]:          11 :         DataStream stream{std::span{reinterpret_cast<const std::byte*>(raw_transaction), raw_transaction_len}};
     506   [ +  +  +  -  :          11 :         return btck_Transaction::create(std::make_shared<const CTransaction>(deserialize, TX_WITH_WITNESS, stream));
                   -  + ]
     507                 :          11 :     } catch (...) {
     508                 :           3 :         return nullptr;
     509                 :           3 :     }
     510                 :             : }
     511                 :             : 
     512                 :         260 : size_t btck_transaction_count_outputs(const btck_Transaction* transaction)
     513                 :             : {
     514         [ -  + ]:         260 :     return btck_Transaction::get(transaction)->vout.size();
     515                 :             : }
     516                 :             : 
     517                 :         511 : const btck_TransactionOutput* btck_transaction_get_output_at(const btck_Transaction* transaction, size_t output_index)
     518                 :             : {
     519         [ -  + ]:         511 :     const CTransaction& tx = *btck_Transaction::get(transaction);
     520   [ -  +  -  + ]:         511 :     assert(output_index < tx.vout.size());
     521                 :         511 :     return btck_TransactionOutput::ref(&tx.vout[output_index]);
     522                 :             : }
     523                 :             : 
     524                 :         257 : size_t btck_transaction_count_inputs(const btck_Transaction* transaction)
     525                 :             : {
     526         [ -  + ]:         257 :     return btck_Transaction::get(transaction)->vin.size();
     527                 :             : }
     528                 :             : 
     529                 :         249 : const btck_TransactionInput* btck_transaction_get_input_at(const btck_Transaction* transaction, size_t input_index)
     530                 :             : {
     531   [ -  +  -  + ]:         249 :     assert(input_index < btck_Transaction::get(transaction)->vin.size());
     532                 :         249 :     return btck_TransactionInput::ref(&btck_Transaction::get(transaction)->vin[input_index]);
     533                 :             : }
     534                 :             : 
     535                 :        2500 : const btck_Txid* btck_transaction_get_txid(const btck_Transaction* transaction)
     536                 :             : {
     537                 :        2500 :     return btck_Txid::ref(&btck_Transaction::get(transaction)->GetHash());
     538                 :             : }
     539                 :             : 
     540                 :          58 : btck_Transaction* btck_transaction_copy(const btck_Transaction* transaction)
     541                 :             : {
     542                 :          58 :     return btck_Transaction::copy(transaction);
     543                 :             : }
     544                 :             : 
     545                 :          12 : int btck_transaction_to_bytes(const btck_Transaction* transaction, btck_WriteBytes writer, void* user_data)
     546                 :             : {
     547                 :          12 :     try {
     548                 :          12 :         WriterStream ws{writer, user_data};
     549         [ +  - ]:          24 :         ws << TX_WITH_WITNESS(btck_Transaction::get(transaction));
     550                 :             :         return 0;
     551                 :           0 :     } catch (...) {
     552                 :           0 :         return -1;
     553                 :           0 :     }
     554                 :             : }
     555                 :             : 
     556                 :          94 : void btck_transaction_destroy(btck_Transaction* transaction)
     557                 :             : {
     558         [ +  + ]:          94 :     delete transaction;
     559                 :          94 : }
     560                 :             : 
     561                 :           9 : btck_ScriptPubkey* btck_script_pubkey_create(const void* script_pubkey, size_t script_pubkey_len)
     562                 :             : {
     563         [ +  - ]:           9 :     if (script_pubkey == nullptr && script_pubkey_len != 0) {
     564                 :             :         return nullptr;
     565                 :             :     }
     566                 :           9 :     auto data = std::span{reinterpret_cast<const uint8_t*>(script_pubkey), script_pubkey_len};
     567                 :           9 :     return btck_ScriptPubkey::create(data.begin(), data.end());
     568                 :             : }
     569                 :             : 
     570                 :         492 : int btck_script_pubkey_to_bytes(const btck_ScriptPubkey* script_pubkey_, btck_WriteBytes writer, void* user_data)
     571                 :             : {
     572                 :         492 :     const auto& script_pubkey{btck_ScriptPubkey::get(script_pubkey_)};
     573   [ +  +  +  + ]:        1191 :     return writer(script_pubkey.data(), script_pubkey.size(), user_data);
     574                 :             : }
     575                 :             : 
     576                 :          12 : btck_ScriptPubkey* btck_script_pubkey_copy(const btck_ScriptPubkey* script_pubkey)
     577                 :             : {
     578                 :          12 :     return btck_ScriptPubkey::copy(script_pubkey);
     579                 :             : }
     580                 :             : 
     581                 :          25 : void btck_script_pubkey_destroy(btck_ScriptPubkey* script_pubkey)
     582                 :             : {
     583         [ +  + ]:          25 :     delete script_pubkey;
     584                 :          25 : }
     585                 :             : 
     586                 :           3 : btck_TransactionOutput* btck_transaction_output_create(const btck_ScriptPubkey* script_pubkey, int64_t amount)
     587                 :             : {
     588                 :           3 :     return btck_TransactionOutput::create(amount, btck_ScriptPubkey::get(script_pubkey));
     589                 :             : }
     590                 :             : 
     591                 :          33 : btck_TransactionOutput* btck_transaction_output_copy(const btck_TransactionOutput* output)
     592                 :             : {
     593                 :          33 :     return btck_TransactionOutput::copy(output);
     594                 :             : }
     595                 :             : 
     596                 :         493 : const btck_ScriptPubkey* btck_transaction_output_get_script_pubkey(const btck_TransactionOutput* output)
     597                 :             : {
     598                 :         493 :     return btck_ScriptPubkey::ref(&btck_TransactionOutput::get(output).scriptPubKey);
     599                 :             : }
     600                 :             : 
     601                 :         297 : int64_t btck_transaction_output_get_amount(const btck_TransactionOutput* output)
     602                 :             : {
     603                 :         297 :     return btck_TransactionOutput::get(output).nValue;
     604                 :             : }
     605                 :             : 
     606                 :          39 : void btck_transaction_output_destroy(btck_TransactionOutput* output)
     607                 :             : {
     608         [ +  + ]:          39 :     delete output;
     609                 :          39 : }
     610                 :             : 
     611                 :          35 : int btck_script_pubkey_verify(const btck_ScriptPubkey* script_pubkey,
     612                 :             :                               const int64_t amount,
     613                 :             :                               const btck_Transaction* tx_to,
     614                 :             :                               const btck_TransactionOutput** spent_outputs_, size_t spent_outputs_len,
     615                 :             :                               const unsigned int input_index,
     616                 :             :                               const btck_ScriptVerificationFlags flags,
     617                 :             :                               btck_ScriptVerifyStatus* status)
     618                 :             : {
     619                 :             :     // Assert that all specified flags are part of the interface before continuing
     620         [ -  + ]:          35 :     assert((flags & ~btck_ScriptVerificationFlags_ALL) == 0);
     621                 :             : 
     622         [ -  + ]:          35 :     if (!is_valid_flag_combination(script_verify_flags::from_int(flags))) {
     623         [ #  # ]:           0 :         if (status) *status = btck_ScriptVerifyStatus_ERROR_INVALID_FLAGS_COMBINATION;
     624                 :           0 :         return 0;
     625                 :             :     }
     626                 :             : 
     627   [ +  +  +  + ]:          35 :     if (flags & btck_ScriptVerificationFlags_TAPROOT && spent_outputs_ == nullptr) {
     628         [ +  - ]:           2 :         if (status) *status = btck_ScriptVerifyStatus_ERROR_SPENT_OUTPUTS_REQUIRED;
     629                 :           2 :         return 0;
     630                 :             :     }
     631                 :             : 
     632         [ +  - ]:          33 :     if (status) *status = btck_ScriptVerifyStatus_OK;
     633                 :             : 
     634         [ +  + ]:          33 :     const CTransaction& tx{*btck_Transaction::get(tx_to)};
     635                 :          33 :     std::vector<CTxOut> spent_outputs;
     636         [ +  + ]:          33 :     if (spent_outputs_ != nullptr) {
     637   [ -  +  -  + ]:          29 :         assert(spent_outputs_len == tx.vin.size());
     638         [ +  - ]:          29 :         spent_outputs.reserve(spent_outputs_len);
     639         [ +  + ]:          60 :         for (size_t i = 0; i < spent_outputs_len; i++) {
     640                 :          31 :             const CTxOut& tx_out{btck_TransactionOutput::get(spent_outputs_[i])};
     641         [ +  - ]:          31 :             spent_outputs.push_back(tx_out);
     642                 :             :         }
     643                 :             :     }
     644                 :             : 
     645   [ -  +  -  + ]:          33 :     assert(input_index < tx.vin.size());
     646         [ +  - ]:          33 :     PrecomputedTransactionData txdata{tx};
     647                 :             : 
     648   [ +  +  +  + ]:          33 :     if (spent_outputs_ != nullptr && flags & btck_ScriptVerificationFlags_TAPROOT) {
     649         [ +  - ]:          27 :         txdata.Init(tx, std::move(spent_outputs));
     650                 :             :     }
     651                 :             : 
     652         [ +  - ]:          33 :     bool result = VerifyScript(tx.vin[input_index].scriptSig,
     653                 :             :                                btck_ScriptPubkey::get(script_pubkey),
     654         [ +  - ]:          33 :                                &tx.vin[input_index].scriptWitness,
     655                 :             :                                script_verify_flags::from_int(flags),
     656         [ +  - ]:          33 :                                TransactionSignatureChecker(&tx, input_index, amount, txdata, MissingDataBehavior::FAIL),
     657                 :             :                                nullptr);
     658         [ -  + ]:          33 :     return result ? 1 : 0;
     659                 :          33 : }
     660                 :             : 
     661                 :          33 : btck_TransactionInput* btck_transaction_input_copy(const btck_TransactionInput* input)
     662                 :             : {
     663                 :          33 :     return btck_TransactionInput::copy(input);
     664                 :             : }
     665                 :             : 
     666                 :         235 : const btck_TransactionOutPoint* btck_transaction_input_get_out_point(const btck_TransactionInput* input)
     667                 :             : {
     668                 :         235 :     return btck_TransactionOutPoint::ref(&btck_TransactionInput::get(input).prevout);
     669                 :             : }
     670                 :             : 
     671                 :          36 : void btck_transaction_input_destroy(btck_TransactionInput* input)
     672                 :             : {
     673         [ +  + ]:          36 :     delete input;
     674                 :          36 : }
     675                 :             : 
     676                 :           7 : btck_TransactionOutPoint* btck_transaction_out_point_copy(const btck_TransactionOutPoint* out_point)
     677                 :             : {
     678                 :           7 :     return btck_TransactionOutPoint::copy(out_point);
     679                 :             : }
     680                 :             : 
     681                 :         259 : uint32_t btck_transaction_out_point_get_index(const btck_TransactionOutPoint* out_point)
     682                 :             : {
     683                 :         259 :     return btck_TransactionOutPoint::get(out_point).n;
     684                 :             : }
     685                 :             : 
     686                 :          78 : const btck_Txid* btck_transaction_out_point_get_txid(const btck_TransactionOutPoint* out_point)
     687                 :             : {
     688                 :          78 :     return btck_Txid::ref(&btck_TransactionOutPoint::get(out_point).hash);
     689                 :             : }
     690                 :             : 
     691                 :           9 : void btck_transaction_out_point_destroy(btck_TransactionOutPoint* out_point)
     692                 :             : {
     693         [ +  + ]:           9 :     delete out_point;
     694                 :           9 : }
     695                 :             : 
     696                 :           7 : btck_Txid* btck_txid_copy(const btck_Txid* txid)
     697                 :             : {
     698                 :           7 :     return btck_Txid::copy(txid);
     699                 :             : }
     700                 :             : 
     701                 :           1 : void btck_txid_to_bytes(const btck_Txid* txid, unsigned char output[32])
     702                 :             : {
     703                 :           1 :     std::memcpy(output, btck_Txid::get(txid).begin(), 32);
     704                 :           1 : }
     705                 :             : 
     706                 :        2499 : int btck_txid_equals(const btck_Txid* txid1, const btck_Txid* txid2)
     707                 :             : {
     708                 :        2499 :     return btck_Txid::get(txid1) == btck_Txid::get(txid2);
     709                 :             : }
     710                 :             : 
     711                 :           9 : void btck_txid_destroy(btck_Txid* txid)
     712                 :             : {
     713         [ +  + ]:           9 :     delete txid;
     714                 :           9 : }
     715                 :             : 
     716                 :           5 : void btck_logging_set_options(const btck_LoggingOptions options)
     717                 :             : {
     718                 :           5 :     LOCK(cs_main);
     719         [ +  - ]:           5 :     LogInstance().m_log_timestamps = options.log_timestamps;
     720         [ +  - ]:           5 :     LogInstance().m_log_time_micros = options.log_time_micros;
     721         [ +  - ]:           5 :     LogInstance().m_log_threadnames = options.log_threadnames;
     722         [ +  - ]:           5 :     LogInstance().m_log_sourcelocations = options.log_sourcelocations;
     723   [ +  -  +  - ]:           5 :     LogInstance().m_always_print_category_level = options.always_print_category_levels;
     724                 :           5 : }
     725                 :             : 
     726                 :           2 : void btck_logging_set_level_category(btck_LogCategory category, btck_LogLevel level)
     727                 :             : {
     728                 :           2 :     LOCK(cs_main);
     729         [ -  + ]:           2 :     if (category == btck_LogCategory_ALL) {
     730         [ #  # ]:           0 :         LogInstance().SetLogLevel(get_bclog_level(level));
     731                 :             :     }
     732                 :             : 
     733   [ +  -  +  - ]:           2 :     LogInstance().AddCategoryLogLevel(get_bclog_flag(category), get_bclog_level(level));
     734                 :           2 : }
     735                 :             : 
     736                 :           2 : void btck_logging_enable_category(btck_LogCategory category)
     737                 :             : {
     738                 :           2 :     LogInstance().EnableCategory(get_bclog_flag(category));
     739                 :           2 : }
     740                 :             : 
     741                 :           2 : void btck_logging_disable_category(btck_LogCategory category)
     742                 :             : {
     743                 :           2 :     LogInstance().DisableCategory(get_bclog_flag(category));
     744                 :           2 : }
     745                 :             : 
     746                 :           0 : void btck_logging_disable()
     747                 :             : {
     748                 :           0 :     LogInstance().DisableLogging();
     749                 :           0 : }
     750                 :             : 
     751                 :           8 : btck_LoggingConnection* btck_logging_connection_create(btck_LogCallback callback, void* user_data, btck_DestroyCallback user_data_destroy_callback)
     752                 :             : {
     753                 :           8 :     try {
     754         [ +  - ]:           8 :         return btck_LoggingConnection::create(callback, user_data, user_data_destroy_callback);
     755         [ -  - ]:           0 :     } catch (const std::exception&) {
     756                 :           0 :         return nullptr;
     757                 :           0 :     }
     758                 :             : }
     759                 :             : 
     760                 :           8 : void btck_logging_connection_destroy(btck_LoggingConnection* connection)
     761                 :             : {
     762         [ +  - ]:           8 :     delete connection;
     763                 :           8 : }
     764                 :             : 
     765                 :          13 : btck_ChainParameters* btck_chain_parameters_create(const btck_ChainType chain_type)
     766                 :             : {
     767   [ +  -  -  -  :          13 :     switch (chain_type) {
                   +  - ]
     768                 :           9 :     case btck_ChainType_MAINNET: {
     769                 :           9 :         return btck_ChainParameters::ref(const_cast<CChainParams*>(CChainParams::Main().release()));
     770                 :             :     }
     771                 :           0 :     case btck_ChainType_TESTNET: {
     772                 :           0 :         return btck_ChainParameters::ref(const_cast<CChainParams*>(CChainParams::TestNet().release()));
     773                 :             :     }
     774                 :           0 :     case btck_ChainType_TESTNET_4: {
     775                 :           0 :         return btck_ChainParameters::ref(const_cast<CChainParams*>(CChainParams::TestNet4().release()));
     776                 :             :     }
     777                 :           0 :     case btck_ChainType_SIGNET: {
     778         [ #  # ]:           0 :         return btck_ChainParameters::ref(const_cast<CChainParams*>(CChainParams::SigNet({}).release()));
     779                 :             :     }
     780                 :           4 :     case btck_ChainType_REGTEST: {
     781                 :           8 :         return btck_ChainParameters::ref(const_cast<CChainParams*>(CChainParams::RegTest({}).release()));
     782                 :             :     }
     783                 :             :     }
     784                 :           0 :     assert(false);
     785         [ +  - ]:           4 : }
     786                 :             : 
     787                 :           5 : btck_ChainParameters* btck_chain_parameters_copy(const btck_ChainParameters* chain_parameters)
     788                 :             : {
     789                 :           5 :     return btck_ChainParameters::copy(chain_parameters);
     790                 :             : }
     791                 :             : 
     792                 :          20 : void btck_chain_parameters_destroy(btck_ChainParameters* chain_parameters)
     793                 :             : {
     794         [ +  + ]:          20 :     delete chain_parameters;
     795                 :          20 : }
     796                 :             : 
     797                 :          18 : btck_ContextOptions* btck_context_options_create()
     798                 :             : {
     799                 :          18 :     return btck_ContextOptions::create();
     800                 :             : }
     801                 :             : 
     802                 :          12 : void btck_context_options_set_chainparams(btck_ContextOptions* options, const btck_ChainParameters* chain_parameters)
     803                 :             : {
     804                 :             :     // Copy the chainparams, so the caller can free it again
     805                 :          12 :     LOCK(btck_ContextOptions::get(options).m_mutex);
     806   [ +  -  +  - ]:          12 :     btck_ContextOptions::get(options).m_chainparams = std::make_unique<const CChainParams>(btck_ChainParameters::get(chain_parameters));
     807                 :          12 : }
     808                 :             : 
     809                 :          12 : void btck_context_options_set_notifications(btck_ContextOptions* options, btck_NotificationInterfaceCallbacks notifications)
     810                 :             : {
     811                 :             :     // The KernelNotifications are copy-initialized, so the caller can free them again.
     812                 :          12 :     LOCK(btck_ContextOptions::get(options).m_mutex);
     813   [ +  -  -  +  :          12 :     btck_ContextOptions::get(options).m_notifications = std::make_shared<KernelNotifications>(notifications);
                   +  - ]
     814                 :          12 : }
     815                 :             : 
     816                 :           5 : void btck_context_options_set_validation_interface(btck_ContextOptions* options, btck_ValidationInterfaceCallbacks vi_cbs)
     817                 :             : {
     818                 :           5 :     LOCK(btck_ContextOptions::get(options).m_mutex);
     819   [ +  -  -  +  :           5 :     btck_ContextOptions::get(options).m_validation_interface = std::make_shared<KernelValidationInterface>(vi_cbs);
                   +  - ]
     820                 :           5 : }
     821                 :             : 
     822                 :          18 : void btck_context_options_destroy(btck_ContextOptions* options)
     823                 :             : {
     824         [ +  - ]:          18 :     delete options;
     825                 :          18 : }
     826                 :             : 
     827                 :          18 : btck_Context* btck_context_create(const btck_ContextOptions* options)
     828                 :             : {
     829                 :          18 :     bool sane{true};
     830                 :          18 :     const ContextOptions* opts = options ? &btck_ContextOptions::get(options) : nullptr;
     831                 :          18 :     auto context{std::make_shared<const Context>(opts, sane)};
     832         [ -  + ]:          18 :     if (!sane) {
     833         [ #  # ]:           0 :         LogError("Kernel context sanity check failed.");
     834                 :             :         return nullptr;
     835                 :             :     }
     836         [ +  - ]:          18 :     return btck_Context::create(context);
     837                 :          18 : }
     838                 :             : 
     839                 :           5 : btck_Context* btck_context_copy(const btck_Context* context)
     840                 :             : {
     841                 :           5 :     return btck_Context::copy(context);
     842                 :             : }
     843                 :             : 
     844                 :           1 : int btck_context_interrupt(btck_Context* context)
     845                 :             : {
     846         [ -  + ]:           1 :     return (*btck_Context::get(context)->m_interrupt)() ? 0 : -1;
     847                 :             : }
     848                 :             : 
     849                 :          25 : void btck_context_destroy(btck_Context* context)
     850                 :             : {
     851         [ +  + ]:          25 :     delete context;
     852                 :          25 : }
     853                 :             : 
     854                 :           6 : const btck_BlockTreeEntry* btck_block_tree_entry_get_previous(const btck_BlockTreeEntry* entry)
     855                 :             : {
     856         [ +  + ]:           6 :     if (!btck_BlockTreeEntry::get(entry).pprev) {
     857                 :           2 :         LogInfo("Genesis block has no previous.");
     858                 :           2 :         return nullptr;
     859                 :             :     }
     860                 :             : 
     861                 :             :     return btck_BlockTreeEntry::ref(btck_BlockTreeEntry::get(entry).pprev);
     862                 :             : }
     863                 :             : 
     864                 :           4 : btck_ValidationMode btck_block_validation_state_get_validation_mode(const btck_BlockValidationState* block_validation_state_)
     865                 :             : {
     866                 :           4 :     auto& block_validation_state = btck_BlockValidationState::get(block_validation_state_);
     867         [ +  + ]:           4 :     if (block_validation_state.IsValid()) return btck_ValidationMode_VALID;
     868         [ +  - ]:           1 :     if (block_validation_state.IsInvalid()) return btck_ValidationMode_INVALID;
     869                 :             :     return btck_ValidationMode_INTERNAL_ERROR;
     870                 :             : }
     871                 :             : 
     872                 :           1 : btck_BlockValidationResult btck_block_validation_state_get_block_validation_result(const btck_BlockValidationState* block_validation_state_)
     873                 :             : {
     874                 :           1 :     auto& block_validation_state = btck_BlockValidationState::get(block_validation_state_);
     875   [ -  -  +  -  :           1 :     switch (block_validation_state.GetResult()) {
          -  -  -  -  -  
                      - ]
     876                 :             :     case BlockValidationResult::BLOCK_RESULT_UNSET:
     877                 :             :         return btck_BlockValidationResult_UNSET;
     878                 :           0 :     case BlockValidationResult::BLOCK_CONSENSUS:
     879                 :           0 :         return btck_BlockValidationResult_CONSENSUS;
     880                 :           0 :     case BlockValidationResult::BLOCK_CACHED_INVALID:
     881                 :           0 :         return btck_BlockValidationResult_CACHED_INVALID;
     882                 :           1 :     case BlockValidationResult::BLOCK_INVALID_HEADER:
     883                 :           1 :         return btck_BlockValidationResult_INVALID_HEADER;
     884                 :           0 :     case BlockValidationResult::BLOCK_MUTATED:
     885                 :           0 :         return btck_BlockValidationResult_MUTATED;
     886                 :           0 :     case BlockValidationResult::BLOCK_MISSING_PREV:
     887                 :           0 :         return btck_BlockValidationResult_MISSING_PREV;
     888                 :           0 :     case BlockValidationResult::BLOCK_INVALID_PREV:
     889                 :           0 :         return btck_BlockValidationResult_INVALID_PREV;
     890                 :           0 :     case BlockValidationResult::BLOCK_TIME_FUTURE:
     891                 :           0 :         return btck_BlockValidationResult_TIME_FUTURE;
     892                 :           0 :     case BlockValidationResult::BLOCK_HEADER_LOW_WORK:
     893                 :           0 :         return btck_BlockValidationResult_HEADER_LOW_WORK;
     894                 :             :     } // no default case, so the compiler can warn about missing cases
     895                 :           0 :     assert(false);
     896                 :             : }
     897                 :             : 
     898                 :          18 : btck_ChainstateManagerOptions* btck_chainstate_manager_options_create(const btck_Context* context, const char* data_dir, size_t data_dir_len, const char* blocks_dir, size_t blocks_dir_len)
     899                 :             : {
     900   [ +  +  +  + ]:          18 :     if (data_dir == nullptr || data_dir_len == 0 || blocks_dir == nullptr || blocks_dir_len == 0) {
     901                 :           4 :         LogError("Failed to create chainstate manager options: dir must be non-null and non-empty");
     902                 :           4 :         return nullptr;
     903                 :             :     }
     904                 :          14 :     try {
     905   [ +  -  +  -  :          28 :         fs::path abs_data_dir{fs::absolute(fs::PathFromString({data_dir, data_dir_len}))};
                   +  - ]
     906         [ +  - ]:          14 :         fs::create_directories(abs_data_dir);
     907   [ +  -  +  -  :          28 :         fs::path abs_blocks_dir{fs::absolute(fs::PathFromString({blocks_dir, blocks_dir_len}))};
                   +  - ]
     908         [ +  - ]:          14 :         fs::create_directories(abs_blocks_dir);
     909         [ +  - ]:          14 :         return btck_ChainstateManagerOptions::create(btck_Context::get(context), abs_data_dir, abs_blocks_dir);
     910         [ -  - ]:          28 :     } catch (const std::exception& e) {
     911         [ -  - ]:           0 :         LogError("Failed to create chainstate manager options: %s", e.what());
     912                 :           0 :         return nullptr;
     913                 :           0 :     }
     914                 :             : }
     915                 :             : 
     916                 :           5 : void btck_chainstate_manager_options_set_worker_threads_num(btck_ChainstateManagerOptions* opts, int worker_threads)
     917                 :             : {
     918                 :           5 :     LOCK(btck_ChainstateManagerOptions::get(opts).m_mutex);
     919         [ +  - ]:           5 :     btck_ChainstateManagerOptions::get(opts).m_chainman_options.worker_threads_num = worker_threads;
     920                 :           5 : }
     921                 :             : 
     922                 :          14 : void btck_chainstate_manager_options_destroy(btck_ChainstateManagerOptions* options)
     923                 :             : {
     924         [ +  - ]:          14 :     delete options;
     925                 :          14 : }
     926                 :             : 
     927                 :           6 : int btck_chainstate_manager_options_set_wipe_dbs(btck_ChainstateManagerOptions* chainman_opts, int wipe_block_tree_db, int wipe_chainstate_db)
     928                 :             : {
     929         [ +  + ]:           6 :     if (wipe_block_tree_db == 1 && wipe_chainstate_db != 1) {
     930                 :           1 :         LogError("Wiping the block tree db without also wiping the chainstate db is currently unsupported.");
     931                 :           1 :         return -1;
     932                 :             :     }
     933                 :           5 :     auto& opts{btck_ChainstateManagerOptions::get(chainman_opts)};
     934                 :           5 :     LOCK(opts.m_mutex);
     935                 :           5 :     opts.m_blockman_options.block_tree_db_params.wipe_data = wipe_block_tree_db == 1;
     936                 :           5 :     opts.m_chainstate_load_options.wipe_chainstate_db = wipe_chainstate_db == 1;
     937         [ +  - ]:           5 :     return 0;
     938                 :           5 : }
     939                 :             : 
     940                 :           2 : void btck_chainstate_manager_options_update_block_tree_db_in_memory(
     941                 :             :     btck_ChainstateManagerOptions* chainman_opts,
     942                 :             :     int block_tree_db_in_memory)
     943                 :             : {
     944                 :           2 :     auto& opts{btck_ChainstateManagerOptions::get(chainman_opts)};
     945                 :           2 :     LOCK(opts.m_mutex);
     946         [ +  - ]:           2 :     opts.m_blockman_options.block_tree_db_params.memory_only = block_tree_db_in_memory == 1;
     947                 :           2 : }
     948                 :             : 
     949                 :           2 : void btck_chainstate_manager_options_update_chainstate_db_in_memory(
     950                 :             :     btck_ChainstateManagerOptions* chainman_opts,
     951                 :             :     int chainstate_db_in_memory)
     952                 :             : {
     953                 :           2 :     auto& opts{btck_ChainstateManagerOptions::get(chainman_opts)};
     954                 :           2 :     LOCK(opts.m_mutex);
     955         [ +  - ]:           2 :     opts.m_chainstate_load_options.coins_db_in_memory = chainstate_db_in_memory == 1;
     956                 :           2 : }
     957                 :             : 
     958                 :          14 : btck_ChainstateManager* btck_chainstate_manager_create(
     959                 :             :     const btck_ChainstateManagerOptions* chainman_opts)
     960                 :             : {
     961                 :          14 :     auto& opts{btck_ChainstateManagerOptions::get(chainman_opts)};
     962                 :          14 :     std::unique_ptr<ChainstateManager> chainman;
     963                 :          14 :     try {
     964         [ +  - ]:          14 :         LOCK(opts.m_mutex);
     965   [ +  -  +  - ]:          28 :         chainman = std::make_unique<ChainstateManager>(*opts.m_context->m_interrupt, opts.m_chainman_options, opts.m_blockman_options);
     966         [ -  - ]:           0 :     } catch (const std::exception& e) {
     967         [ -  - ]:           0 :         LogError("Failed to create chainstate manager: %s", e.what());
     968                 :           0 :         return nullptr;
     969                 :           0 :     }
     970                 :             : 
     971                 :          14 :     try {
     972   [ +  -  +  - ]:          42 :         const auto chainstate_load_opts{WITH_LOCK(opts.m_mutex, return opts.m_chainstate_load_options)};
     973                 :             : 
     974         [ +  - ]:          14 :         kernel::CacheSizes cache_sizes{DEFAULT_KERNEL_CACHE};
     975   [ +  -  -  + ]:          14 :         auto [status, chainstate_err]{node::LoadChainstate(*chainman, cache_sizes, chainstate_load_opts)};
     976         [ -  + ]:          14 :         if (status != node::ChainstateLoadStatus::SUCCESS) {
     977         [ #  # ]:           0 :             LogError("Failed to load chain state from your data directory: %s", chainstate_err.original);
     978                 :             :             return nullptr;
     979                 :             :         }
     980         [ +  - ]:          14 :         std::tie(status, chainstate_err) = node::VerifyLoadedChainstate(*chainman, chainstate_load_opts);
     981         [ -  + ]:          14 :         if (status != node::ChainstateLoadStatus::SUCCESS) {
     982         [ #  # ]:           0 :             LogError("Failed to verify loaded chain state from your datadir: %s", chainstate_err.original);
     983                 :             :             return nullptr;
     984                 :             :         }
     985   [ +  -  -  + ]:          14 :         if (auto result = chainman->ActivateBestChains(); !result) {
     986   [ #  #  #  # ]:           0 :             LogError("%s", util::ErrorString(result).original);
     987                 :           0 :             return nullptr;
     988                 :          14 :         }
     989         [ -  - ]:          14 :     } catch (const std::exception& e) {
     990         [ -  - ]:           0 :         LogError("Failed to load chainstate: %s", e.what());
     991                 :           0 :         return nullptr;
     992                 :           0 :     }
     993                 :             : 
     994         [ +  - ]:          14 :     return btck_ChainstateManager::create(std::move(chainman), opts.m_context);
     995                 :          14 : }
     996                 :             : 
     997                 :           1 : const btck_BlockTreeEntry* btck_chainstate_manager_get_block_tree_entry_by_hash(const btck_ChainstateManager* chainman, const btck_BlockHash* block_hash)
     998                 :             : {
     999   [ +  -  +  - ]:           3 :     auto block_index = WITH_LOCK(btck_ChainstateManager::get(chainman).m_chainman->GetMutex(),
    1000                 :             :                                  return btck_ChainstateManager::get(chainman).m_chainman->m_blockman.LookupBlockIndex(btck_BlockHash::get(block_hash)));
    1001         [ -  + ]:           1 :     if (!block_index) {
    1002         [ #  # ]:           0 :         LogDebug(BCLog::KERNEL, "A block with the given hash is not indexed.");
    1003                 :           0 :         return nullptr;
    1004                 :             :     }
    1005                 :             :     return btck_BlockTreeEntry::ref(block_index);
    1006                 :             : }
    1007                 :             : 
    1008                 :          14 : void btck_chainstate_manager_destroy(btck_ChainstateManager* chainman)
    1009                 :             : {
    1010                 :          14 :     {
    1011                 :          14 :         LOCK(btck_ChainstateManager::get(chainman).m_chainman->GetMutex());
    1012         [ +  + ]:          28 :         for (const auto& chainstate : btck_ChainstateManager::get(chainman).m_chainman->m_chainstates) {
    1013         [ +  - ]:          28 :             if (chainstate->CanFlushToDisk()) {
    1014         [ +  - ]:          14 :                 chainstate->ForceFlushStateToDisk();
    1015                 :          14 :                 chainstate->ResetCoinsViews();
    1016                 :             :             }
    1017                 :             :         }
    1018                 :          14 :     }
    1019                 :             : 
    1020         [ +  - ]:          14 :     delete chainman;
    1021                 :          14 : }
    1022                 :             : 
    1023                 :           2 : int btck_chainstate_manager_import_blocks(btck_ChainstateManager* chainman, const char** block_file_paths_data, size_t* block_file_paths_lens, size_t block_file_paths_data_len)
    1024                 :             : {
    1025                 :           2 :     try {
    1026                 :           2 :         std::vector<fs::path> import_files;
    1027         [ +  - ]:           2 :         import_files.reserve(block_file_paths_data_len);
    1028         [ +  + ]:           3 :         for (uint32_t i = 0; i < block_file_paths_data_len; i++) {
    1029         [ +  - ]:           1 :             if (block_file_paths_data[i] != nullptr) {
    1030   [ +  -  +  - ]:           2 :                 import_files.emplace_back(std::string{block_file_paths_data[i], block_file_paths_lens[i]}.c_str());
    1031                 :             :             }
    1032                 :             :         }
    1033   [ -  +  +  - ]:           2 :         node::ImportBlocks(*btck_ChainstateManager::get(chainman).m_chainman, import_files);
    1034         [ -  - ]:           0 :     } catch (const std::exception& e) {
    1035         [ -  - ]:           0 :         LogError("Failed to import blocks: %s", e.what());
    1036                 :           0 :         return -1;
    1037                 :           0 :     }
    1038                 :           2 :     return 0;
    1039                 :             : }
    1040                 :             : 
    1041                 :         425 : btck_Block* btck_block_create(const void* raw_block, size_t raw_block_length)
    1042                 :             : {
    1043         [ +  - ]:         425 :     if (raw_block == nullptr && raw_block_length != 0) {
    1044                 :             :         return nullptr;
    1045                 :             :     }
    1046                 :         425 :     auto block{std::make_shared<CBlock>()};
    1047                 :             : 
    1048         [ +  - ]:         425 :     DataStream stream{std::span{reinterpret_cast<const std::byte*>(raw_block), raw_block_length}};
    1049                 :             : 
    1050                 :         425 :     try {
    1051         [ +  + ]:         425 :         stream >> TX_WITH_WITNESS(*block);
    1052                 :           3 :     } catch (...) {
    1053   [ +  -  +  +  :           3 :         LogDebug(BCLog::KERNEL, "Block decode failed.");
                   +  - ]
    1054                 :           3 :         return nullptr;
    1055         [ +  - ]:           3 :     }
    1056                 :             : 
    1057         [ +  - ]:         422 :     return btck_Block::create(block);
    1058         [ +  - ]:         850 : }
    1059                 :             : 
    1060                 :           5 : btck_Block* btck_block_copy(const btck_Block* block)
    1061                 :             : {
    1062                 :           5 :     return btck_Block::copy(block);
    1063                 :             : }
    1064                 :             : 
    1065                 :        2802 : size_t btck_block_count_transactions(const btck_Block* block)
    1066                 :             : {
    1067         [ -  + ]:        2802 :     return btck_Block::get(block)->vtx.size();
    1068                 :             : }
    1069                 :             : 
    1070                 :        2928 : const btck_Transaction* btck_block_get_transaction_at(const btck_Block* block, size_t index)
    1071                 :             : {
    1072   [ -  +  -  + ]:        2928 :     assert(index < btck_Block::get(block)->vtx.size());
    1073                 :        2928 :     return btck_Transaction::ref(&btck_Block::get(block)->vtx[index]);
    1074                 :             : }
    1075                 :             : 
    1076                 :          20 : int btck_block_to_bytes(const btck_Block* block, btck_WriteBytes writer, void* user_data)
    1077                 :             : {
    1078                 :          20 :     try {
    1079                 :          20 :         WriterStream ws{writer, user_data};
    1080         [ +  - ]:          40 :         ws << TX_WITH_WITNESS(*btck_Block::get(block));
    1081                 :             :         return 0;
    1082                 :           0 :     } catch (...) {
    1083                 :           0 :         return -1;
    1084                 :           0 :     }
    1085                 :             : }
    1086                 :             : 
    1087                 :           1 : btck_BlockHash* btck_block_get_hash(const btck_Block* block)
    1088                 :             : {
    1089                 :           1 :     return btck_BlockHash::create(btck_Block::get(block)->GetHash());
    1090                 :             : }
    1091                 :             : 
    1092                 :        3228 : void btck_block_destroy(btck_Block* block)
    1093                 :             : {
    1094         [ +  + ]:        3228 :     delete block;
    1095                 :        3228 : }
    1096                 :             : 
    1097                 :        2789 : btck_Block* btck_block_read(const btck_ChainstateManager* chainman, const btck_BlockTreeEntry* entry)
    1098                 :             : {
    1099                 :        2789 :     auto block{std::make_shared<CBlock>()};
    1100   [ +  -  +  + ]:        2789 :     if (!btck_ChainstateManager::get(chainman).m_chainman->m_blockman.ReadBlock(*block, btck_BlockTreeEntry::get(entry))) {
    1101         [ +  - ]:           1 :         LogError("Failed to read block.");
    1102                 :             :         return nullptr;
    1103                 :             :     }
    1104         [ +  - ]:        2788 :     return btck_Block::create(block);
    1105                 :        2789 : }
    1106                 :             : 
    1107                 :         211 : int32_t btck_block_tree_entry_get_height(const btck_BlockTreeEntry* entry)
    1108                 :             : {
    1109                 :         211 :     return btck_BlockTreeEntry::get(entry).nHeight;
    1110                 :             : }
    1111                 :             : 
    1112                 :           1 : const btck_BlockHash* btck_block_tree_entry_get_block_hash(const btck_BlockTreeEntry* entry)
    1113                 :             : {
    1114                 :           1 :     return btck_BlockHash::ref(btck_BlockTreeEntry::get(entry).phashBlock);
    1115                 :             : }
    1116                 :             : 
    1117                 :           7 : int btck_block_tree_entry_equals(const btck_BlockTreeEntry* entry1, const btck_BlockTreeEntry* entry2)
    1118                 :             : {
    1119                 :           7 :     return &btck_BlockTreeEntry::get(entry1) == &btck_BlockTreeEntry::get(entry2);
    1120                 :             : }
    1121                 :             : 
    1122                 :           2 : btck_BlockHash* btck_block_hash_create(const unsigned char block_hash[32])
    1123                 :             : {
    1124                 :           2 :     return btck_BlockHash::create(std::span<const unsigned char>{block_hash, 32});
    1125                 :             : }
    1126                 :             : 
    1127                 :           6 : btck_BlockHash* btck_block_hash_copy(const btck_BlockHash* block_hash)
    1128                 :             : {
    1129                 :           6 :     return btck_BlockHash::copy(block_hash);
    1130                 :             : }
    1131                 :             : 
    1132                 :           2 : void btck_block_hash_to_bytes(const btck_BlockHash* block_hash, unsigned char output[32])
    1133                 :             : {
    1134                 :           2 :     std::memcpy(output, btck_BlockHash::get(block_hash).begin(), 32);
    1135                 :           2 : }
    1136                 :             : 
    1137                 :           2 : int btck_block_hash_equals(const btck_BlockHash* hash1, const btck_BlockHash* hash2)
    1138                 :             : {
    1139                 :           2 :     return btck_BlockHash::get(hash1) == btck_BlockHash::get(hash2);
    1140                 :             : }
    1141                 :             : 
    1142                 :          11 : void btck_block_hash_destroy(btck_BlockHash* hash)
    1143                 :             : {
    1144         [ +  + ]:          11 :     delete hash;
    1145                 :          11 : }
    1146                 :             : 
    1147                 :           5 : btck_BlockSpentOutputs* btck_block_spent_outputs_read(const btck_ChainstateManager* chainman, const btck_BlockTreeEntry* entry)
    1148                 :             : {
    1149                 :           5 :     auto block_undo{std::make_shared<CBlockUndo>()};
    1150         [ +  + ]:           5 :     if (btck_BlockTreeEntry::get(entry).nHeight < 1) {
    1151   [ +  -  +  -  :           1 :         LogDebug(BCLog::KERNEL, "The genesis block does not have any spent outputs.");
                   +  - ]
    1152         [ +  - ]:           1 :         return btck_BlockSpentOutputs::create(block_undo);
    1153                 :             :     }
    1154   [ +  -  +  + ]:           4 :     if (!btck_ChainstateManager::get(chainman).m_chainman->m_blockman.ReadBlockUndo(*block_undo, btck_BlockTreeEntry::get(entry))) {
    1155         [ +  - ]:           1 :         LogError("Failed to read block spent outputs data.");
    1156                 :             :         return nullptr;
    1157                 :             :     }
    1158         [ +  - ]:           3 :     return btck_BlockSpentOutputs::create(block_undo);
    1159                 :           5 : }
    1160                 :             : 
    1161                 :           5 : btck_BlockSpentOutputs* btck_block_spent_outputs_copy(const btck_BlockSpentOutputs* block_spent_outputs)
    1162                 :             : {
    1163                 :           5 :     return btck_BlockSpentOutputs::copy(block_spent_outputs);
    1164                 :             : }
    1165                 :             : 
    1166                 :          28 : size_t btck_block_spent_outputs_count(const btck_BlockSpentOutputs* block_spent_outputs)
    1167                 :             : {
    1168         [ -  + ]:          28 :     return btck_BlockSpentOutputs::get(block_spent_outputs)->vtxundo.size();
    1169                 :             : }
    1170                 :             : 
    1171                 :          20 : const btck_TransactionSpentOutputs* btck_block_spent_outputs_get_transaction_spent_outputs_at(const btck_BlockSpentOutputs* block_spent_outputs, size_t transaction_index)
    1172                 :             : {
    1173   [ -  +  -  + ]:          20 :     assert(transaction_index < btck_BlockSpentOutputs::get(block_spent_outputs)->vtxundo.size());
    1174                 :          20 :     const auto* tx_undo{&btck_BlockSpentOutputs::get(block_spent_outputs)->vtxundo.at(transaction_index)};
    1175                 :          20 :     return btck_TransactionSpentOutputs::ref(tx_undo);
    1176                 :             : }
    1177                 :             : 
    1178                 :          11 : void btck_block_spent_outputs_destroy(btck_BlockSpentOutputs* block_spent_outputs)
    1179                 :             : {
    1180         [ +  + ]:          11 :     delete block_spent_outputs;
    1181                 :          11 : }
    1182                 :             : 
    1183                 :           7 : btck_TransactionSpentOutputs* btck_transaction_spent_outputs_copy(const btck_TransactionSpentOutputs* transaction_spent_outputs)
    1184                 :             : {
    1185                 :           7 :     return btck_TransactionSpentOutputs::copy(transaction_spent_outputs);
    1186                 :             : }
    1187                 :             : 
    1188                 :          24 : size_t btck_transaction_spent_outputs_count(const btck_TransactionSpentOutputs* transaction_spent_outputs)
    1189                 :             : {
    1190         [ -  + ]:          24 :     return btck_TransactionSpentOutputs::get(transaction_spent_outputs).vprevout.size();
    1191                 :             : }
    1192                 :             : 
    1193                 :           9 : void btck_transaction_spent_outputs_destroy(btck_TransactionSpentOutputs* transaction_spent_outputs)
    1194                 :             : {
    1195         [ +  + ]:           9 :     delete transaction_spent_outputs;
    1196                 :           9 : }
    1197                 :             : 
    1198                 :          18 : const btck_Coin* btck_transaction_spent_outputs_get_coin_at(const btck_TransactionSpentOutputs* transaction_spent_outputs, size_t coin_index)
    1199                 :             : {
    1200   [ -  +  -  + ]:          18 :     assert(coin_index < btck_TransactionSpentOutputs::get(transaction_spent_outputs).vprevout.size());
    1201                 :          18 :     const Coin* coin{&btck_TransactionSpentOutputs::get(transaction_spent_outputs).vprevout.at(coin_index)};
    1202                 :          18 :     return btck_Coin::ref(coin);
    1203                 :             : }
    1204                 :             : 
    1205                 :           7 : btck_Coin* btck_coin_copy(const btck_Coin* coin)
    1206                 :             : {
    1207                 :           7 :     return btck_Coin::copy(coin);
    1208                 :             : }
    1209                 :             : 
    1210                 :           1 : uint32_t btck_coin_confirmation_height(const btck_Coin* coin)
    1211                 :             : {
    1212                 :           1 :     return btck_Coin::get(coin).nHeight;
    1213                 :             : }
    1214                 :             : 
    1215                 :           1 : int btck_coin_is_coinbase(const btck_Coin* coin)
    1216                 :             : {
    1217         [ +  - ]:           1 :     return btck_Coin::get(coin).IsCoinBase() ? 1 : 0;
    1218                 :             : }
    1219                 :             : 
    1220                 :           3 : const btck_TransactionOutput* btck_coin_get_output(const btck_Coin* coin)
    1221                 :             : {
    1222                 :           3 :     return btck_TransactionOutput::ref(&btck_Coin::get(coin).out);
    1223                 :             : }
    1224                 :             : 
    1225                 :           9 : void btck_coin_destroy(btck_Coin* coin)
    1226                 :             : {
    1227         [ +  + ]:           9 :     delete coin;
    1228                 :           9 : }
    1229                 :             : 
    1230                 :         420 : int btck_chainstate_manager_process_block(
    1231                 :             :     btck_ChainstateManager* chainman,
    1232                 :             :     const btck_Block* block,
    1233                 :             :     int* _new_block)
    1234                 :             : {
    1235                 :         420 :     bool new_block;
    1236                 :         420 :     auto result = btck_ChainstateManager::get(chainman).m_chainman->ProcessNewBlock(btck_Block::get(block), /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/&new_block);
    1237         [ +  - ]:         420 :     if (_new_block) {
    1238         [ +  + ]:         423 :         *_new_block = new_block ? 1 : 0;
    1239                 :             :     }
    1240         [ +  + ]:         420 :     return result ? 0 : -1;
    1241                 :             : }
    1242                 :             : 
    1243                 :          30 : const btck_Chain* btck_chainstate_manager_get_active_chain(const btck_ChainstateManager* chainman)
    1244                 :             : {
    1245   [ +  -  +  - ]:          90 :     return btck_Chain::ref(&WITH_LOCK(btck_ChainstateManager::get(chainman).m_chainman->GetMutex(), return btck_ChainstateManager::get(chainman).m_chainman->ActiveChain()));
    1246                 :             : }
    1247                 :             : 
    1248                 :         260 : int btck_chain_get_height(const btck_Chain* chain)
    1249                 :             : {
    1250                 :         260 :     LOCK(::cs_main);
    1251   [ -  +  +  - ]:         260 :     return btck_Chain::get(chain).Height();
    1252                 :         260 : }
    1253                 :             : 
    1254                 :        3627 : const btck_BlockTreeEntry* btck_chain_get_by_height(const btck_Chain* chain, int height)
    1255                 :             : {
    1256                 :        3627 :     LOCK(::cs_main);
    1257   [ +  -  +  - ]:        7254 :     return btck_BlockTreeEntry::ref(btck_Chain::get(chain)[height]);
    1258                 :        3627 : }
    1259                 :             : 
    1260                 :           1 : int btck_chain_contains(const btck_Chain* chain, const btck_BlockTreeEntry* entry)
    1261                 :             : {
    1262                 :           1 :     LOCK(::cs_main);
    1263         [ -  + ]:           1 :     return btck_Chain::get(chain).Contains(&btck_BlockTreeEntry::get(entry)) ? 1 : 0;
    1264                 :           1 : }
        

Generated by: LCOV version 2.0-1