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