LCOV - code coverage report
Current view: top level - src/kernel - bitcoinkernel.cpp (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 82.9 % 621 515
Test Date: 2025-12-10 04:45:08 Functions: 95.8 % 165 158
Branches: 50.7 % 609 309

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

Generated by: LCOV version 2.0-1