Branch data Line data Source code
1 : : // Copyright (c) 2024-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #ifndef BITCOIN_KERNEL_BITCOINKERNEL_WRAPPER_H
6 : : #define BITCOIN_KERNEL_BITCOINKERNEL_WRAPPER_H
7 : :
8 : : #include <kernel/bitcoinkernel.h>
9 : :
10 : : #include <array>
11 : : #include <chrono>
12 : : #include <exception>
13 : : #include <functional>
14 : : #include <memory>
15 : : #include <optional>
16 : : #include <span>
17 : : #include <stdexcept>
18 : : #include <string>
19 : : #include <string_view>
20 : : #include <type_traits>
21 : : #include <utility>
22 : : #include <vector>
23 : :
24 : : namespace btck {
25 : :
26 : : enum class LogCategory : btck_LogCategory {
27 : : NONE = btck_LogCategory_NONE,
28 : : BENCH = btck_LogCategory_BENCH,
29 : : BLOCKSTORAGE = btck_LogCategory_BLOCKSTORAGE,
30 : : COINDB = btck_LogCategory_COINDB,
31 : : LEVELDB = btck_LogCategory_LEVELDB,
32 : : MEMPOOL = btck_LogCategory_MEMPOOL,
33 : : PRUNE = btck_LogCategory_PRUNE,
34 : : RAND = btck_LogCategory_RAND,
35 : : REINDEX = btck_LogCategory_REINDEX,
36 : : VALIDATION = btck_LogCategory_VALIDATION,
37 : : KERNEL = btck_LogCategory_KERNEL,
38 : : TXPACKAGES = btck_LogCategory_TXPACKAGES,
39 : : LOCK = btck_LogCategory_LOCK,
40 : : };
41 : :
42 : : enum class LogLevel : btck_LogLevel {
43 : : TRACE_LEVEL = btck_LogLevel_TRACE,
44 : : DEBUG_LEVEL = btck_LogLevel_DEBUG,
45 : : INFO_LEVEL = btck_LogLevel_INFO,
46 : : WARNING_LEVEL = btck_LogLevel_WARNING,
47 : : ERROR_LEVEL = btck_LogLevel_ERROR,
48 : : };
49 : :
50 : 372 : constexpr std::string_view Name(LogLevel level) noexcept
51 : : {
52 [ - - + - : 372 : switch (level) {
+ - ]
53 : 0 : case LogLevel::TRACE_LEVEL: return "trace";
54 : 0 : case LogLevel::DEBUG_LEVEL: return "debug";
55 : 366 : case LogLevel::INFO_LEVEL: return "info";
56 : 0 : case LogLevel::WARNING_LEVEL: return "warning";
57 : 6 : case LogLevel::ERROR_LEVEL: return "error";
58 : : } // no default case, so the compiler can warn about missing cases
59 : 0 : return "unknown";
60 : : }
61 : :
62 : 372 : constexpr std::string_view Name(LogCategory category) noexcept
63 : : {
64 [ + - - - : 372 : switch (category) {
- - - - -
- - - -
- ]
65 : 372 : case LogCategory::NONE: return "none";
66 : 0 : case LogCategory::BENCH: return "bench";
67 : 0 : case LogCategory::BLOCKSTORAGE: return "blockstorage";
68 : 0 : case LogCategory::COINDB: return "coindb";
69 : 0 : case LogCategory::LEVELDB: return "leveldb";
70 : 0 : case LogCategory::MEMPOOL: return "mempool";
71 : 0 : case LogCategory::PRUNE: return "prune";
72 : 0 : case LogCategory::RAND: return "rand";
73 : 0 : case LogCategory::REINDEX: return "reindex";
74 : 0 : case LogCategory::TXPACKAGES: return "txpackages";
75 : 0 : case LogCategory::VALIDATION: return "validation";
76 : 0 : case LogCategory::KERNEL: return "kernel";
77 : 0 : case LogCategory::LOCK: return "lock";
78 : : } // no default case, so the compiler can warn about missing cases
79 : 0 : return "unknown";
80 : : }
81 : :
82 : : enum class ChainType : btck_ChainType {
83 : : MAINNET = btck_ChainType_MAINNET,
84 : : TESTNET = btck_ChainType_TESTNET,
85 : : TESTNET_4 = btck_ChainType_TESTNET_4,
86 : : SIGNET = btck_ChainType_SIGNET,
87 : : REGTEST = btck_ChainType_REGTEST
88 : : };
89 : :
90 : : enum class SynchronizationState : btck_SynchronizationState {
91 : : INIT_REINDEX = btck_SynchronizationState_INIT_REINDEX,
92 : : INIT_DOWNLOAD = btck_SynchronizationState_INIT_DOWNLOAD,
93 : : POST_INIT = btck_SynchronizationState_POST_INIT
94 : : };
95 : :
96 : : enum class Warning : btck_Warning {
97 : : UNKNOWN_NEW_RULES_ACTIVATED = btck_Warning_UNKNOWN_NEW_RULES_ACTIVATED,
98 : : LARGE_WORK_INVALID_CHAIN = btck_Warning_LARGE_WORK_INVALID_CHAIN
99 : : };
100 : :
101 : : enum class ValidationMode : btck_ValidationMode {
102 : : VALID = btck_ValidationMode_VALID,
103 : : INVALID = btck_ValidationMode_INVALID,
104 : : INTERNAL_ERROR = btck_ValidationMode_INTERNAL_ERROR
105 : : };
106 : :
107 : : enum class BlockValidationResult : btck_BlockValidationResult {
108 : : UNSET = btck_BlockValidationResult_UNSET,
109 : : CONSENSUS = btck_BlockValidationResult_CONSENSUS,
110 : : CACHED_INVALID = btck_BlockValidationResult_CACHED_INVALID,
111 : : INVALID_HEADER = btck_BlockValidationResult_INVALID_HEADER,
112 : : MUTATED = btck_BlockValidationResult_MUTATED,
113 : : MISSING_PREV = btck_BlockValidationResult_MISSING_PREV,
114 : : INVALID_PREV = btck_BlockValidationResult_INVALID_PREV,
115 : : TIME_FUTURE = btck_BlockValidationResult_TIME_FUTURE,
116 : : HEADER_LOW_WORK = btck_BlockValidationResult_HEADER_LOW_WORK
117 : : };
118 : :
119 : : enum class TxValidationResult : btck_TxValidationResult {
120 : : UNSET = btck_TxValidationResult_UNSET,
121 : : CONSENSUS = btck_TxValidationResult_CONSENSUS,
122 : : INPUTS_NOT_STANDARD = btck_TxValidationResult_INPUTS_NOT_STANDARD,
123 : : NOT_STANDARD = btck_TxValidationResult_NOT_STANDARD,
124 : : MISSING_INPUTS = btck_TxValidationResult_MISSING_INPUTS,
125 : : PREMATURE_SPEND = btck_TxValidationResult_PREMATURE_SPEND,
126 : : WITNESS_MUTATED = btck_TxValidationResult_WITNESS_MUTATED,
127 : : WITNESS_STRIPPED = btck_TxValidationResult_WITNESS_STRIPPED,
128 : : CONFLICT = btck_TxValidationResult_CONFLICT,
129 : : MEMPOOL_POLICY = btck_TxValidationResult_MEMPOOL_POLICY,
130 : : NO_MEMPOOL = btck_TxValidationResult_NO_MEMPOOL,
131 : : RECONSIDERABLE = btck_TxValidationResult_RECONSIDERABLE,
132 : : UNKNOWN = btck_TxValidationResult_UNKNOWN
133 : : };
134 : :
135 : : enum class ScriptVerifyStatus : btck_ScriptVerifyStatus {
136 : : OK = btck_ScriptVerifyStatus_OK,
137 : : ERROR_INVALID_FLAGS_COMBINATION = btck_ScriptVerifyStatus_ERROR_INVALID_FLAGS_COMBINATION,
138 : : ERROR_SPENT_OUTPUTS_REQUIRED = btck_ScriptVerifyStatus_ERROR_SPENT_OUTPUTS_REQUIRED,
139 : : };
140 : :
141 : : enum class ScriptVerificationFlags : btck_ScriptVerificationFlags {
142 : : NONE = btck_ScriptVerificationFlags_NONE,
143 : : P2SH = btck_ScriptVerificationFlags_P2SH,
144 : : DERSIG = btck_ScriptVerificationFlags_DERSIG,
145 : : NULLDUMMY = btck_ScriptVerificationFlags_NULLDUMMY,
146 : : CHECKLOCKTIMEVERIFY = btck_ScriptVerificationFlags_CHECKLOCKTIMEVERIFY,
147 : : CHECKSEQUENCEVERIFY = btck_ScriptVerificationFlags_CHECKSEQUENCEVERIFY,
148 : : WITNESS = btck_ScriptVerificationFlags_WITNESS,
149 : : TAPROOT = btck_ScriptVerificationFlags_TAPROOT,
150 : : ALL = btck_ScriptVerificationFlags_ALL
151 : : };
152 : :
153 : : enum class BlockCheckFlags : btck_BlockCheckFlags {
154 : : BASE = btck_BlockCheckFlags_BASE,
155 : : POW = btck_BlockCheckFlags_POW,
156 : : MERKLE = btck_BlockCheckFlags_MERKLE,
157 : : ALL = btck_BlockCheckFlags_ALL
158 : : };
159 : :
160 : : template <typename T>
161 : : struct is_bitmask_enum : std::false_type {
162 : : };
163 : :
164 : : template <>
165 : : struct is_bitmask_enum<ScriptVerificationFlags> : std::true_type {
166 : : };
167 : :
168 : : template <>
169 : : struct is_bitmask_enum<BlockCheckFlags> : std::true_type {
170 : : };
171 : :
172 : : template <typename T>
173 : : concept BitmaskEnum = is_bitmask_enum<T>::value;
174 : :
175 : : template <BitmaskEnum T>
176 : : constexpr T operator|(T lhs, T rhs)
177 : : {
178 : : return static_cast<T>(
179 : : static_cast<std::underlying_type_t<T>>(lhs) | static_cast<std::underlying_type_t<T>>(rhs));
180 : : }
181 : :
182 : : template <BitmaskEnum T>
183 : : constexpr T operator&(T lhs, T rhs)
184 : : {
185 : : return static_cast<T>(
186 : : static_cast<std::underlying_type_t<T>>(lhs) & static_cast<std::underlying_type_t<T>>(rhs));
187 : : }
188 : :
189 : : template <BitmaskEnum T>
190 : : constexpr T operator^(T lhs, T rhs)
191 : : {
192 : : return static_cast<T>(
193 : : static_cast<std::underlying_type_t<T>>(lhs) ^ static_cast<std::underlying_type_t<T>>(rhs));
194 : : }
195 : :
196 : : template <BitmaskEnum T>
197 : : constexpr T operator~(T value)
198 : : {
199 : : return static_cast<T>(~static_cast<std::underlying_type_t<T>>(value));
200 : : }
201 : :
202 : : template <BitmaskEnum T>
203 : : constexpr T& operator|=(T& lhs, T rhs)
204 : : {
205 : : return lhs = lhs | rhs;
206 : : }
207 : :
208 : : template <BitmaskEnum T>
209 : : constexpr T& operator&=(T& lhs, T rhs)
210 : : {
211 : : return lhs = lhs & rhs;
212 : : }
213 : :
214 : : template <BitmaskEnum T>
215 : : constexpr T& operator^=(T& lhs, T rhs)
216 : : {
217 : : return lhs = lhs ^ rhs;
218 : : }
219 : :
220 : : template <typename T>
221 : 36508 : T check(T ptr)
222 : : {
223 [ + + ]: 36508 : if (ptr == nullptr) {
224 [ + - ]: 12 : throw std::runtime_error("failed to instantiate btck object");
225 : : }
226 : 36496 : return ptr;
227 : : }
228 : :
229 : 378 : inline std::string_view ToStringView(btck_StringView view)
230 : : {
231 : 378 : return {view.data, view.len};
232 : : }
233 : :
234 : : template <typename Collection, typename ValueType, auto GetFunc>
235 : : class Iterator
236 : : {
237 : : public:
238 : : using iterator_category = std::random_access_iterator_tag;
239 : : using iterator_concept = std::random_access_iterator_tag;
240 : : using difference_type = std::ptrdiff_t;
241 : : using value_type = ValueType;
242 : :
243 : : private:
244 : : const Collection* m_collection;
245 : : size_t m_idx;
246 : :
247 : : public:
248 : : Iterator() = default;
249 : : Iterator(const Collection* ptr) : m_collection{ptr}, m_idx{0} {}
250 : 15139 : Iterator(const Collection* ptr, size_t idx) : m_collection{ptr}, m_idx{idx} {}
251 : :
252 : : // This is just a view, so return a copy.
253 : 32278 : auto operator*() const { return std::invoke(GetFunc, *m_collection, m_idx); }
254 : :
255 : 15801 : auto& operator++() { m_idx++; return *this; }
256 : 6 : auto operator++(int) { Iterator tmp = *this; ++(*this); return tmp; }
257 : :
258 : 221 : auto& operator--() { m_idx--; return *this; }
259 : 6 : auto operator--(int) { auto temp = *this; --m_idx; return temp; }
260 : :
261 : 6 : auto& operator+=(difference_type n) { m_idx += n; return *this; }
262 : 6 : auto& operator-=(difference_type n) { m_idx -= n; return *this; }
263 : :
264 [ + - + - : 6 : auto operator+(difference_type n) const { return Iterator(m_collection, m_idx + n); }
+ - + - +
- + - ]
265 : : auto operator-(difference_type n) const { return Iterator(m_collection, m_idx - n); }
266 : :
267 [ + - + - : 7 : auto operator-(const Iterator& other) const { return static_cast<difference_type>(m_idx) - static_cast<difference_type>(other.m_idx); }
+ - + - +
- + - ]
268 : :
269 : 6 : ValueType operator[](difference_type n) const { return *(*this + n); }
270 : :
271 [ + - + - : 24 : auto operator<=>(const Iterator& other) const { return m_idx <=> other.m_idx; }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
272 : :
273 [ + - + - : 23288 : bool operator==(const Iterator& other) const { return m_collection == other.m_collection && m_idx == other.m_idx; }
+ - + - +
- - + + +
+ - + + +
- + - + -
- + + + +
- + + + -
+ - + - -
+ + + + -
+ + + - +
- + - - +
+ + + - +
+ + - + -
+ - - + +
+ + - + +
+ - + - +
- - + + +
+ - + + +
- + + + -
+ + + - +
+ + - + +
+ - + + +
- + + + -
+ + + - +
+ + - + +
+ - + + +
- + - + -
+ + + - +
+ ]
274 : :
275 : : private:
276 [ + - + - : 6 : friend Iterator operator+(difference_type n, const Iterator& it) { return it + n; }
+ - + - +
- + - ]
277 : : };
278 : :
279 : : template <typename Container, typename SizeFunc, typename GetFunc>
280 : : concept IndexedContainer = requires(const Container& c, SizeFunc size_func, GetFunc get_func, std::size_t i) {
281 : : { std::invoke(size_func, c) } -> std::convertible_to<std::size_t>;
282 : : { std::invoke(get_func, c, i) }; // Return type is deduced
283 : : };
284 : :
285 : : template <typename Container, auto SizeFunc, auto GetFunc>
286 : : requires IndexedContainer<Container, decltype(SizeFunc), decltype(GetFunc)>
287 : : class Range
288 : : {
289 : : public:
290 : : using value_type = std::invoke_result_t<decltype(GetFunc), const Container&, size_t>;
291 : : using difference_type = std::ptrdiff_t;
292 : : using iterator = Iterator<Container, value_type, GetFunc>;
293 : : using const_iterator = iterator;
294 : :
295 : : private:
296 : : const Container* m_container;
297 : :
298 : : public:
299 : 7270 : explicit Range(const Container& container) : m_container(&container)
300 : : {
301 : : static_assert(std::ranges::random_access_range<Range>);
302 : : }
303 : :
304 [ + - + - : 534 : iterator begin() const { return iterator(m_container, 0); }
- + - + -
+ - + - +
- + + - +
- + - + -
+ - + - +
- + - + -
+ - - + -
+ - + - +
- + - + +
- + - + -
+ - + - +
- + - + -
+ - + - -
+ - + - +
- + - + -
+ + - + -
+ - + - +
- + - + -
+ - + - +
- - + - +
- + - + -
+ - + + -
+ - + - +
- + - + -
+ - + - +
- + - - +
- + - + -
+ - + - +
+ - + - +
- + - + -
+ - + - +
- + - + -
- + - + -
+ - + - +
- + + - +
- + - + -
+ - + - +
- + - + -
+ - ]
305 : 7313 : iterator end() const { return iterator(m_container, size()); }
306 : :
307 [ + - + - : 6 : const_iterator cbegin() const { return begin(); }
+ - + - +
- + - ]
308 [ + - + - : 6 : const_iterator cend() const { return end(); }
+ - + - +
- + - ]
309 : :
310 : 15160 : size_t size() const { return std::invoke(SizeFunc, *m_container); }
311 : :
312 [ + - + - : 7 : bool empty() const { return size() == 0; }
+ - + - +
- + - +
- ]
313 : :
314 : 510 : value_type operator[](size_t index) const { return std::invoke(GetFunc, *m_container, index); }
315 : :
316 : 6 : value_type at(size_t index) const
317 : : {
318 [ + - ]: 6 : if (index >= size()) {
319 [ + - ]: 6 : throw std::out_of_range("Index out of range");
320 : : }
321 : 0 : return (*this)[index];
322 : : }
323 : :
324 : 7 : value_type front() const { return (*this)[0]; }
325 : 9 : value_type back() const { return (*this)[size() - 1]; }
326 : : };
327 : :
328 : : #define MAKE_RANGE_METHOD(method_name, ContainerType, SizeFunc, GetFunc, container_expr) \
329 : : auto method_name() const & { \
330 : : return Range<ContainerType, SizeFunc, GetFunc>{container_expr}; \
331 : : } \
332 : : auto method_name() const && = delete;
333 : :
334 : : template <typename T>
335 : 606 : std::vector<std::byte> write_bytes(const T* object, int (*to_bytes)(const T*, btck_WriteBytes, void*))
336 : : {
337 : 606 : std::vector<std::byte> bytes;
338 [ - - ]: 0 : struct UserData {
339 : : std::vector<std::byte>* bytes;
340 : : std::exception_ptr exception;
341 : : };
342 [ + - ]: 606 : UserData user_data = UserData{.bytes = &bytes, .exception = nullptr};
343 : :
344 : 1944 : constexpr auto const write = +[](const void* buffer, size_t len, void* user_data) -> int {
345 : 1338 : auto& data = *reinterpret_cast<UserData*>(user_data);
346 : 1338 : auto& bytes = *data.bytes;
347 : : try {
348 : 1338 : auto const* first = static_cast<const std::byte*>(buffer);
349 [ + - + - : 1338 : auto const* last = first + len;
+ - + - +
- ]
350 [ + - + - : 1338 : bytes.insert(bytes.end(), first, last);
+ - + - +
- ]
351 : : return 0;
352 : 0 : } catch (...) {
353 [ - - - - : 0 : data.exception = std::current_exception();
- - - - -
- ]
354 : : return -1;
355 : : }
356 : : };
357 : :
358 [ + - - + ]: 606 : if (to_bytes(object, write, &user_data) != 0) {
359 : 0 : std::rethrow_exception(user_data.exception);
360 : : }
361 [ - + ]: 606 : return bytes;
362 : 606 : }
363 : :
364 : : template <typename CType>
365 : : class View
366 : : {
367 : : protected:
368 : : const CType* m_ptr;
369 : :
370 : : public:
371 : 53758 : explicit View(const CType* ptr) : m_ptr{check(ptr)} {}
372 : :
373 [ + - + - : 1287 : const CType* get() const { return m_ptr; }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
374 : : };
375 : :
376 : : template <typename CType, CType* (*CopyFunc)(const CType*), void (*DestroyFunc)(CType*)>
377 : : class Handle
378 : : {
379 : : protected:
380 : : CType* m_ptr;
381 : :
382 : : public:
383 : 18894 : explicit Handle(CType* ptr) : m_ptr{check(ptr)} {}
384 : :
385 : : // Copy constructors
386 : 100 : Handle(const Handle& other)
387 : 100 : : m_ptr{check(CopyFunc(other.m_ptr))} {}
388 : 20 : Handle& operator=(const Handle& other)
389 : : {
390 [ + - ]: 20 : if (this != &other) {
391 : 20 : Handle temp(other);
392 : 20 : std::swap(m_ptr, temp.m_ptr);
393 : 20 : }
394 : 20 : return *this;
395 : : }
396 : :
397 : : // Move constructors
398 : 96 : Handle(Handle&& other) noexcept : m_ptr(other.m_ptr) { other.m_ptr = nullptr; }
399 : 20 : Handle& operator=(Handle&& other) noexcept
400 : : {
401 : : if (this != &other) {
402 : 20 : DestroyFunc(m_ptr);
403 [ + - + - : 20 : m_ptr = std::exchange(other.m_ptr, nullptr);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
404 : : }
405 : : return *this;
406 : : }
407 : :
408 : : template <typename ViewType>
409 : : requires std::derived_from<ViewType, View<CType>>
410 : 726 : Handle(const ViewType& view)
411 : 726 : : Handle{CopyFunc(view.get())}
412 : : {
413 : 726 : }
414 : :
415 : 121 : ~Handle() { DestroyFunc(m_ptr); }
416 : :
417 [ + - + - : 182 : CType* get() { return m_ptr; }
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
418 [ + - + - ]: 31 : const CType* get() const { return m_ptr; }
419 : : };
420 : :
421 : : template <typename CType, void (*DestroyFunc)(CType*)>
422 : 139 : class UniqueHandle
423 : : {
424 : : protected:
425 : : struct Deleter {
426 : 74 : void operator()(CType* ptr) const noexcept
427 : : {
428 : : if (ptr) DestroyFunc(ptr);
429 : 74 : }
430 : : };
431 : : std::unique_ptr<CType, Deleter> m_ptr;
432 : :
433 : : public:
434 : 152 : explicit UniqueHandle(CType* ptr) : m_ptr{check(ptr)} {}
435 : :
436 [ + - + - : 32 : CType* get() { return m_ptr.get(); }
+ - ]
437 : 7404 : const CType* get() const { return m_ptr.get(); }
438 : : };
439 : :
440 : : class PrecomputedTransactionData;
441 : : class Transaction;
442 : : class TransactionOutput;
443 : : class BlockValidationState;
444 : :
445 : : template <typename Derived>
446 : : class ScriptPubkeyApi
447 : : {
448 : : private:
449 : 632 : auto impl() const
450 : : {
451 : 635 : return static_cast<const Derived*>(this)->get();
452 : : }
453 : :
454 : : friend Derived;
455 : : ScriptPubkeyApi() = default;
456 : :
457 : : public:
458 : : bool Verify(int64_t amount,
459 : : const Transaction& tx_to,
460 : : const PrecomputedTransactionData* precomputed_txdata,
461 : : unsigned int input_index,
462 : : ScriptVerificationFlags flags,
463 : : ScriptVerifyStatus& status) const;
464 : :
465 : 555 : std::vector<std::byte> ToBytes() const
466 : : {
467 [ + - + - : 553 : return write_bytes(impl(), btck_script_pubkey_to_bytes);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
468 : : }
469 : : };
470 : :
471 : : class ScriptPubkeyView : public View<btck_ScriptPubkey>, public ScriptPubkeyApi<ScriptPubkeyView>
472 : : {
473 : : public:
474 : 1170 : explicit ScriptPubkeyView(const btck_ScriptPubkey* ptr) : View{ptr} {}
475 : : };
476 : :
477 [ + - + - : 25 : class ScriptPubkey : public Handle<btck_ScriptPubkey, btck_script_pubkey_copy, btck_script_pubkey_destroy>, public ScriptPubkeyApi<ScriptPubkey>
+ - + - +
- + - + -
+ - + - +
- ]
478 : : {
479 : : public:
480 : 11 : explicit ScriptPubkey(std::span<const std::byte> raw)
481 : 11 : : Handle{btck_script_pubkey_create(raw.data(), raw.size())} {}
482 : :
483 : 2 : ScriptPubkey(const ScriptPubkeyView& view)
484 [ + - + - ]: 2 : : Handle(view) {}
485 : : };
486 : :
487 : : template <typename Derived>
488 : : class TransactionOutputApi
489 : : {
490 : : private:
491 : 973 : auto impl() const
492 : : {
493 : 973 : return static_cast<const Derived*>(this)->get();
494 : : }
495 : :
496 : : friend Derived;
497 : : TransactionOutputApi() = default;
498 : :
499 : : public:
500 : 388 : int64_t Amount() const
501 : : {
502 [ + - + - : 386 : return btck_transaction_output_get_amount(impl());
+ - + - +
- + - + -
+ - + - +
- + - ]
503 : : }
504 : :
505 : 585 : ScriptPubkeyView GetScriptPubkey() const
506 : : {
507 : 585 : return ScriptPubkeyView{btck_transaction_output_get_script_pubkey(impl())};
508 : : }
509 : : };
510 : :
511 : : class TransactionOutputView : public View<btck_TransactionOutput>, public TransactionOutputApi<TransactionOutputView>
512 : : {
513 : : public:
514 : 1206 : explicit TransactionOutputView(const btck_TransactionOutput* ptr) : View{ptr} {}
515 : : };
516 : :
517 [ + - + - : 79 : class TransactionOutput : public Handle<btck_TransactionOutput, btck_transaction_output_copy, btck_transaction_output_destroy>, public TransactionOutputApi<TransactionOutput>
+ - + - +
- + - +
- ]
518 : : {
519 : : public:
520 : 5 : explicit TransactionOutput(const ScriptPubkey& script_pubkey, int64_t amount)
521 : 5 : : Handle{btck_transaction_output_create(script_pubkey.get(), amount)} {}
522 : :
523 : 61 : TransactionOutput(const TransactionOutputView& view)
524 [ + - + - : 61 : : Handle(view) {}
+ - ]
525 : : };
526 : :
527 : : template <typename Derived>
528 : : class TxidApi
529 : : {
530 : : private:
531 : 7664 : auto impl() const
532 : : {
533 : 7666 : return static_cast<const Derived*>(this)->get();
534 : : }
535 : :
536 : : friend Derived;
537 : : TxidApi() = default;
538 : :
539 : : public:
540 : 7575 : bool operator==(const TxidApi& other) const
541 : : {
542 [ + - + - : 7575 : return btck_txid_equals(impl(), other.impl()) != 0;
+ - + - +
- + - + -
+ - + - +
- + - ]
543 : : }
544 : :
545 : 62 : bool operator!=(const TxidApi& other) const
546 : : {
547 [ + - + - : 62 : return btck_txid_equals(impl(), other.impl()) == 0;
+ - + - +
- + - + -
+ - ]
548 : : }
549 : :
550 [ + - + - : 27 : std::array<std::byte, 32> ToBytes() const
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
551 : : {
552 : : std::array<std::byte, 32> hash;
553 [ + - + - : 27 : btck_txid_to_bytes(impl(), reinterpret_cast<unsigned char*>(hash.data()));
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
554 : 25 : return hash;
555 : : }
556 : : };
557 : :
558 : : class TxidView : public View<btck_Txid>, public TxidApi<TxidView>
559 : : {
560 : : public:
561 : 15646 : explicit TxidView(const btck_Txid* ptr) : View{ptr} {}
562 : : };
563 : :
564 [ + - + - : 22 : class Txid : public Handle<btck_Txid, btck_txid_copy, btck_txid_destroy>, public TxidApi<Txid>
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
565 : : {
566 : : public:
567 : 5 : Txid(const TxidView& view)
568 [ + - + - : 5 : : Handle(view) {}
+ - + - +
- ]
569 : : };
570 : :
571 : : template <typename Derived>
572 : : class WtxidApi
573 : : {
574 : : private:
575 : 18 : auto impl() const
576 : : {
577 : 19 : return static_cast<const Derived*>(this)->get();
578 : : }
579 : :
580 : : friend Derived;
581 : : WtxidApi() = default;
582 : :
583 : : public:
584 : 2 : bool operator==(const WtxidApi& other) const
585 : : {
586 [ + - + - : 2 : return btck_wtxid_equals(impl(), other.impl()) != 0;
+ - + - ]
587 : : }
588 : :
589 : 2 : bool operator!=(const WtxidApi& other) const
590 : : {
591 [ + - + - : 2 : return btck_wtxid_equals(impl(), other.impl()) == 0;
+ - + - ]
592 : : }
593 : :
594 [ + - + - : 14 : std::array<std::byte, 32> ToBytes() const
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
595 : : {
596 : : std::array<std::byte, 32> hash;
597 [ + - + - : 14 : btck_wtxid_to_bytes(impl(), reinterpret_cast<unsigned char*>(hash.data()));
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
598 : 13 : return hash;
599 : : }
600 : : };
601 : :
602 : : class WtxidView : public View<btck_Wtxid>, public WtxidApi<WtxidView>
603 : : {
604 : : public:
605 : 20 : explicit WtxidView(const btck_Wtxid* ptr) : View{ptr} {}
606 : : };
607 : :
608 [ + - + - : 12 : class Wtxid : public Handle<btck_Wtxid, btck_wtxid_copy, btck_wtxid_destroy>, public WtxidApi<Wtxid>
+ - + - +
- + - + -
+ - + - ]
609 : : {
610 : : public:
611 : 3 : Wtxid(const WtxidView& view)
612 [ + - + - : 3 : : Handle(view) {}
+ - ]
613 : : };
614 : :
615 : : template <typename Derived>
616 : : class OutPointApi
617 : : {
618 : : private:
619 : 502 : auto impl() const
620 : : {
621 : 502 : return static_cast<const Derived*>(this)->get();
622 : : }
623 : :
624 : : friend Derived;
625 : : OutPointApi() = default;
626 : :
627 : : public:
628 : 325 : uint32_t index() const
629 : : {
630 [ + - + - ]: 325 : return btck_transaction_out_point_get_index(impl());
631 : : }
632 : :
633 : 177 : TxidView Txid() const
634 : : {
635 : 177 : return TxidView{btck_transaction_out_point_get_txid(impl())};
636 : : }
637 : : };
638 : :
639 : : class OutPointView : public View<btck_TransactionOutPoint>, public OutPointApi<OutPointView>
640 : : {
641 : : public:
642 : 536 : explicit OutPointView(const btck_TransactionOutPoint* ptr) : View{ptr} {}
643 : : };
644 : :
645 [ + - + - : 10 : class OutPoint : public Handle<btck_TransactionOutPoint, btck_transaction_out_point_copy, btck_transaction_out_point_destroy>, public OutPointApi<OutPoint>
+ - + - +
- + - + -
+ - ]
646 : : {
647 : : public:
648 : 2 : OutPoint(const OutPointView& view)
649 [ + - + - ]: 2 : : Handle(view) {}
650 : : };
651 : :
652 : : template <typename Derived>
653 : : class WitnessStackApi
654 : : {
655 : : private:
656 : 21 : auto impl() const
657 : : {
658 : 21 : return static_cast<const Derived*>(this)->get();
659 : : }
660 : :
661 : : friend Derived;
662 : : WitnessStackApi() = default;
663 : :
664 : : public:
665 : 9 : size_t CountItems() const
666 : : {
667 [ + - + - ]: 9 : return btck_witness_stack_count_items(impl());
668 : : }
669 : :
670 : 12 : std::vector<std::byte> GetItem(size_t index) const
671 : : {
672 : : struct Item { const btck_WitnessStack* stack; size_t index; };
673 : 12 : Item item{impl(), index};
674 : : return write_bytes(&item, +[](const Item* c, btck_WriteBytes w, void* ud) {
675 : : return btck_witness_stack_get_item_at(c->stack, c->index, w, ud);
676 [ + - + - : 12 : });
+ - + - +
- ]
677 : : }
678 : :
679 [ + - + - ]: 13 : MAKE_RANGE_METHOD(Items, Derived, &WitnessStackApi<Derived>::CountItems, &WitnessStackApi<Derived>::GetItem, *static_cast<const Derived*>(this))
680 : : };
681 : :
682 : : class WitnessStackView : public View<btck_WitnessStack>, public WitnessStackApi<WitnessStackView>
683 : : {
684 : : public:
685 : 4 : explicit WitnessStackView(const btck_WitnessStack* ptr) : View{ptr} {}
686 : : };
687 : :
688 [ + - + - : 10 : class WitnessStack : public Handle<btck_WitnessStack, btck_witness_stack_copy, btck_witness_stack_destroy>, public WitnessStackApi<WitnessStack>
+ - + - +
- + - + -
+ - ]
689 : : {
690 : : public:
691 : 2 : WitnessStack(const WitnessStackView& view)
692 [ + - + - ]: 1 : : Handle(view) {}
693 : : };
694 : :
695 : : template <typename Derived>
696 : : class TransactionInputApi
697 : : {
698 : : private:
699 : 274 : auto impl() const
700 : : {
701 : 274 : return static_cast<const Derived*>(this)->get();
702 : : }
703 : :
704 : : friend Derived;
705 : : TransactionInputApi() = default;
706 : :
707 : : public:
708 : 268 : OutPointView OutPoint() const
709 : : {
710 : 268 : return OutPointView{btck_transaction_input_get_out_point(impl())};
711 : : }
712 : :
713 : 1 : uint32_t GetSequence() const
714 : : {
715 [ + - ]: 1 : return btck_transaction_input_get_sequence(impl());
716 : : }
717 : :
718 : 2 : WitnessStackView GetWitnessStack() const
719 : : {
720 : 2 : return WitnessStackView{btck_transaction_input_get_witness_stack(impl())};
721 : : }
722 : :
723 : 3 : std::vector<std::byte> GetScriptSig() const
724 : : {
725 [ + - + - : 3 : return write_bytes(impl(), btck_transaction_input_get_script_sig);
+ - ]
726 : : }
727 : : };
728 : :
729 : : class TransactionInputView : public View<btck_TransactionInput>, public TransactionInputApi<TransactionInputView>
730 : : {
731 : : public:
732 : 564 : explicit TransactionInputView(const btck_TransactionInput* ptr) : View{ptr} {}
733 : : };
734 : :
735 [ + - + - : 75 : class TransactionInput : public Handle<btck_TransactionInput, btck_transaction_input_copy, btck_transaction_input_destroy>, public TransactionInputApi<TransactionInput>
+ - + - +
- + - + -
+ - ]
736 : : {
737 : : public:
738 : 61 : TransactionInput(const TransactionInputView& view)
739 [ + - + - : 61 : : Handle(view) {}
+ - ]
740 : : };
741 : :
742 : : template <typename Derived>
743 : : class TransactionApi
744 : : {
745 : : private:
746 : 9129 : auto impl() const
747 : : {
748 : 9130 : return static_cast<const Derived*>(this)->get();
749 : : }
750 : :
751 : : public:
752 : 287 : size_t CountOutputs() const
753 : : {
754 [ + - + - : 287 : return btck_transaction_count_outputs(impl());
+ - + - +
- ]
755 : : }
756 : :
757 : 284 : size_t CountInputs() const
758 : : {
759 [ + - + - : 284 : return btck_transaction_count_inputs(impl());
+ - + - +
- ]
760 : : }
761 : :
762 : 601 : TransactionOutputView GetOutput(size_t index) const
763 : : {
764 : 601 : return TransactionOutputView{btck_transaction_get_output_at(impl(), index)};
765 : : }
766 : :
767 : 282 : TransactionInputView GetInput(size_t index) const
768 : : {
769 : 282 : return TransactionInputView{btck_transaction_get_input_at(impl(), index)};
770 : : }
771 : :
772 : 3 : uint32_t GetVersion() const
773 : : {
774 [ + - + - ]: 3 : return btck_transaction_get_version(impl());
775 : : }
776 : :
777 : 1 : uint32_t GetLocktime() const
778 : : {
779 [ + - ]: 1 : return btck_transaction_get_locktime(impl());
780 : : }
781 : :
782 : 2 : bool HasWitness() const
783 : : {
784 [ + - + - : 2 : return btck_transaction_has_witness(impl()) != 0;
+ - ]
785 : : }
786 : :
787 : 7646 : TxidView Txid() const
788 : : {
789 : 7646 : return TxidView{btck_transaction_get_txid(impl())};
790 : : }
791 : :
792 : 10 : WtxidView Wtxid() const
793 : : {
794 : 10 : return WtxidView{btck_transaction_get_wtxid(impl())};
795 : : }
796 : :
797 [ + - + - : 1083 : MAKE_RANGE_METHOD(Outputs, Derived, &TransactionApi<Derived>::CountOutputs, &TransactionApi<Derived>::GetOutput, *static_cast<const Derived*>(this))
+ - + - ]
798 : :
799 [ + - + - ]: 818 : MAKE_RANGE_METHOD(Inputs, Derived, &TransactionApi<Derived>::CountInputs, &TransactionApi<Derived>::GetInput, *static_cast<const Derived*>(this))
800 : :
801 : 14 : std::vector<std::byte> ToBytes() const
802 : : {
803 [ + - + - : 13 : return write_bytes(impl(), btck_transaction_to_bytes);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - ]
804 : : }
805 : : };
806 : :
807 : : class TransactionView : public View<btck_Transaction>, public TransactionApi<TransactionView>
808 : : {
809 : : public:
810 : 16102 : explicit TransactionView(const btck_Transaction* ptr) : View{ptr} {}
811 : : };
812 : :
813 [ + - + - : 471 : class Transaction : public Handle<btck_Transaction, btck_transaction_copy, btck_transaction_destroy>, public TransactionApi<Transaction>
+ - + - +
- + - + -
+ - - - -
- - - +
- ]
814 : : {
815 : : public:
816 : 31 : explicit Transaction(std::span<const std::byte> raw_transaction)
817 : 31 : : Handle{btck_transaction_create(raw_transaction.data(), raw_transaction.size())} {}
818 : :
819 : 379 : Transaction(const TransactionView& view)
820 [ + - + - : 379 : : Handle{view} {}
+ - + - ]
821 : : };
822 : :
823 [ + - + - : 273 : class PrecomputedTransactionData : public Handle<btck_PrecomputedTransactionData, btck_precomputed_transaction_data_copy, btck_precomputed_transaction_data_destroy>
+ - + - +
- + - +
- ]
824 : : {
825 : : public:
826 : 266 : explicit PrecomputedTransactionData(const Transaction& tx_to, std::span<const TransactionOutput> spent_outputs)
827 : 266 : : Handle{btck_precomputed_transaction_data_create(
828 : : tx_to.get(),
829 : : reinterpret_cast<const btck_TransactionOutput**>(
830 : 266 : const_cast<TransactionOutput*>(spent_outputs.data())),
831 : 266 : spent_outputs.size())} {}
832 : : };
833 : :
834 : : template <typename Derived>
835 : 80 : bool ScriptPubkeyApi<Derived>::Verify(int64_t amount,
836 : : const Transaction& tx_to,
837 : : const PrecomputedTransactionData* precomputed_txdata,
838 : : unsigned int input_index,
839 : : ScriptVerificationFlags flags,
840 : : ScriptVerifyStatus& status) const
841 : : {
842 [ + + ]: 154 : auto result = btck_script_pubkey_verify(
843 : : impl(),
844 : : amount,
845 : : tx_to.get(),
846 : 74 : precomputed_txdata ? precomputed_txdata->get() : nullptr,
847 : : input_index,
848 : : static_cast<btck_ScriptVerificationFlags>(flags),
849 : : reinterpret_cast<btck_ScriptVerifyStatus*>(&status));
850 : 80 : return result == 1;
851 : : }
852 : :
853 : : template <typename Derived>
854 : : class BlockHashApi
855 : : {
856 : : private:
857 : 224 : auto impl() const
858 : : {
859 : 226 : return static_cast<const Derived*>(this)->get();
860 : : }
861 : :
862 : : public:
863 : 207 : bool operator==(const Derived& other) const
864 : : {
865 [ + - + - : 207 : return btck_block_hash_equals(impl(), other.get()) != 0;
+ - + - ]
866 : : }
867 : :
868 : 1 : bool operator!=(const Derived& other) const
869 : : {
870 [ + - + - ]: 1 : return btck_block_hash_equals(impl(), other.get()) == 0;
871 : : }
872 : :
873 [ + - + - : 17 : std::array<std::byte, 32> ToBytes() const
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
874 : : {
875 : : std::array<std::byte, 32> hash;
876 [ + - + - : 17 : btck_block_hash_to_bytes(impl(), reinterpret_cast<unsigned char*>(hash.data()));
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - ]
877 : 16 : return hash;
878 : : }
879 : : };
880 : :
881 : : class BlockHashView : public View<btck_BlockHash>, public BlockHashApi<BlockHashView>
882 : : {
883 : : public:
884 : 416 : explicit BlockHashView(const btck_BlockHash* ptr) : View{ptr} {}
885 : : };
886 : :
887 [ + - + - : 426 : class BlockHash : public Handle<btck_BlockHash, btck_block_hash_copy, btck_block_hash_destroy>, public BlockHashApi<BlockHash>
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
888 : : {
889 : : public:
890 : 2 : explicit BlockHash(const std::array<std::byte, 32>& hash)
891 : 2 : : Handle{btck_block_hash_create(reinterpret_cast<const unsigned char*>(hash.data()))} {}
892 : :
893 : 416 : explicit BlockHash(btck_BlockHash* hash)
894 : 832 : : Handle{hash} {}
895 : :
896 : 207 : BlockHash(const BlockHashView& view)
897 [ + - + - ]: 207 : : Handle{view} {}
898 : : };
899 : :
900 : : template <typename Derived>
901 : : class BlockHeaderApi
902 : : {
903 : : private:
904 : 446 : auto impl() const
905 : : {
906 : 446 : return static_cast<const Derived*>(this)->get();
907 : : }
908 : :
909 : : friend Derived;
910 : : BlockHeaderApi() = default;
911 : :
912 : : public:
913 : 415 : BlockHash Hash() const
914 : : {
915 : 415 : return BlockHash{btck_block_header_get_hash(impl())};
916 : : }
917 : :
918 : 1 : BlockHashView PrevHash() const
919 : : {
920 : 1 : return BlockHashView{btck_block_header_get_prev_hash(impl())};
921 : : }
922 : :
923 [ + - + - ]: 2 : std::array<std::byte, 32> MerkleRoot() const
924 : : {
925 : : std::array<std::byte, 32> merkle_root;
926 [ + - + - ]: 2 : btck_block_header_get_merkle_root(impl(), reinterpret_cast<unsigned char*>(merkle_root.data()));
927 : 2 : return merkle_root;
928 : : }
929 : :
930 : 4 : uint32_t Timestamp() const
931 : : {
932 [ + - + - : 4 : return btck_block_header_get_timestamp(impl());
+ - + - ]
933 : : }
934 : :
935 : 3 : uint32_t Bits() const
936 : : {
937 [ + - + - : 3 : return btck_block_header_get_bits(impl());
+ - ]
938 : : }
939 : :
940 : 3 : int32_t Version() const
941 : : {
942 [ + - + - : 3 : return btck_block_header_get_version(impl());
+ - ]
943 : : }
944 : :
945 : 3 : uint32_t Nonce() const
946 : : {
947 [ + - + - : 3 : return btck_block_header_get_nonce(impl());
+ - ]
948 : : }
949 : :
950 : 15 : std::array<std::byte, 80> ToBytes() const
951 : : {
952 : : std::array<std::byte, 80> header;
953 : 15 : int res{btck_block_header_to_bytes(impl(), reinterpret_cast<unsigned char*>(header.data()))};
954 [ - + ]: 15 : if (res != 0) {
955 [ # # ]: 0 : throw std::runtime_error("Failed to serialize block header");
956 : : }
957 : 15 : return header;
958 : : }
959 : : };
960 : :
961 : : class BlockHeaderView : public View<btck_BlockHeader>, public BlockHeaderApi<BlockHeaderView>
962 : : {
963 : : public:
964 : : explicit BlockHeaderView(const btck_BlockHeader* ptr) : View{ptr} {}
965 : : };
966 : :
967 [ + - + - : 425 : class BlockHeader : public Handle<btck_BlockHeader, btck_block_header_copy, btck_block_header_destroy>, public BlockHeaderApi<BlockHeader>
+ - + - +
- + - + -
+ - ]
968 : : {
969 : : public:
970 : 4 : explicit BlockHeader(std::span<const std::byte> raw_header)
971 : 4 : : Handle{btck_block_header_create(reinterpret_cast<const unsigned char*>(raw_header.data()), raw_header.size())} {}
972 : :
973 : : BlockHeader(const BlockHeaderView& view)
974 : : : Handle{view} {}
975 : :
976 : 415 : BlockHeader(btck_BlockHeader* header)
977 : 830 : : Handle{header} {}
978 : : };
979 : :
980 : : class ConsensusParamsView : public View<btck_ConsensusParams>
981 : : {
982 : : public:
983 : 2 : explicit ConsensusParamsView(const btck_ConsensusParams* ptr) : View{ptr} {}
984 : : };
985 : :
986 [ + - + - : 7330 : class Block : public Handle<btck_Block, btck_block_copy, btck_block_destroy>
+ - + - +
- - - + -
+ - + - -
- - - ]
987 : : {
988 : : public:
989 : 639 : Block(const std::span<const std::byte> raw_block)
990 : 639 : : Handle{btck_block_create(raw_block.data(), raw_block.size())}
991 : : {
992 : 635 : }
993 : :
994 : 6675 : Block(btck_Block* block) : Handle{block} {}
995 : :
996 : 6688 : size_t CountTransactions() const
997 : : {
998 [ + - + - ]: 6688 : return btck_block_count_transactions(get());
999 : : }
1000 : :
1001 : 8051 : TransactionView GetTransaction(size_t index) const
1002 : : {
1003 : 8051 : return TransactionView{btck_block_get_transaction_at(get(), index)};
1004 : : }
1005 : :
1006 : : bool Check(const ConsensusParamsView& consensus_params,
1007 : : BlockCheckFlags flags,
1008 : : BlockValidationState& state) const;
1009 : :
1010 [ + - + - : 21407 : MAKE_RANGE_METHOD(Transactions, Block, &Block::CountTransactions, &Block::GetTransaction, *this)
+ - + - +
- + - + -
+ - + - +
- ]
1011 : :
1012 : 1 : BlockHash GetHash() const
1013 : : {
1014 : 1 : return BlockHash{btck_block_get_hash(get())};
1015 : : }
1016 : :
1017 : 209 : BlockHeader GetHeader() const
1018 : : {
1019 : 209 : return BlockHeader{btck_block_get_header(get())};
1020 : : }
1021 : :
1022 : 22 : std::vector<std::byte> ToBytes() const
1023 : : {
1024 [ + - + - : 21 : return write_bytes(get(), btck_block_to_bytes);
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- ]
1025 : : }
1026 : : };
1027 : :
1028 : 3 : inline void logging_set_min_level(LogLevel level)
1029 : : {
1030 [ + - + - : 3 : btck_logging_set_min_level(static_cast<btck_LogLevel>(level));
+ - ]
1031 : 3 : }
1032 : :
1033 : : //! Non-owning view over a btck_LogEntry. The referenced entry is only valid for the duration of the
1034 : : //! logging callback, so a LogEntry (and any string_view obtained from it) must not be stored or
1035 : : //! used after the callback returns.
1036 : : class LogEntry : public View<btck_LogEntry>
1037 : : {
1038 : : public:
1039 : 756 : explicit LogEntry(const btck_LogEntry* entry) : View{entry} {}
1040 : :
1041 : 378 : std::string_view Message() const { return ToStringView(btck_log_entry_get_message(get())); }
1042 : : std::string_view ThreadName() const { return ToStringView(btck_log_entry_get_thread_name(get())); }
1043 : 372 : std::chrono::sys_time<std::chrono::nanoseconds> Timestamp() const
1044 : : {
1045 : 372 : return std::chrono::sys_time<std::chrono::nanoseconds>{std::chrono::nanoseconds{btck_log_entry_get_timestamp_ns(get())}};
1046 : : }
1047 : : std::optional<std::chrono::sys_seconds> MockTime() const
1048 : : {
1049 : : const int64_t mocktime_s{btck_log_entry_get_mocktime_s(get())};
1050 : : if (mocktime_s == 0) return std::nullopt;
1051 : : return std::chrono::sys_seconds{std::chrono::seconds{mocktime_s}};
1052 : : }
1053 : : std::string_view FileName() const { return ToStringView(btck_log_entry_get_file_name(get())); }
1054 : : std::string_view FunctionName() const { return ToStringView(btck_log_entry_get_function_name(get())); }
1055 : : uint32_t Line() const { return btck_log_entry_get_line(get()); }
1056 : 372 : LogLevel Level() const { return static_cast<LogLevel>(btck_log_entry_get_level(get())); }
1057 : 372 : LogCategory Category() const { return static_cast<LogCategory>(btck_log_entry_get_category(get())); }
1058 : : };
1059 : :
1060 : : template <typename T>
1061 : : concept Log = requires(T a, const LogEntry& entry) {
1062 : : { a.LogMessage(entry) } -> std::same_as<void>;
1063 : : };
1064 : :
1065 : : template <Log T>
1066 [ + - - - : 17 : class Logger : UniqueHandle<btck_LoggingConnection, btck_logging_connection_destroy>
+ - + - +
- + - + -
- - - - ]
[ # # # #
# # ]
1067 : : {
1068 : : public:
1069 : 9 : Logger(std::unique_ptr<T> log)
1070 : : : UniqueHandle{btck_logging_connection_create(
1071 : 378 : +[](void* user_data, const btck_LogEntry* entry) { static_cast<T*>(user_data)->LogMessage(LogEntry{entry}); },
1072 : 9 : log.release(),
1073 [ + - ]: 12 : +[](void* user_data) { delete static_cast<T*>(user_data); })}
1074 : : {
1075 : 9 : }
1076 : : };
1077 : :
1078 : : class BlockTreeEntry : public View<btck_BlockTreeEntry>
1079 : : {
1080 : : public:
1081 : 8370 : BlockTreeEntry(const btck_BlockTreeEntry* entry)
1082 : 650 : : View{entry}
1083 : : {
1084 : : }
1085 : :
1086 : 10 : bool operator==(const BlockTreeEntry& other) const
1087 : : {
1088 [ + - + - : 10 : return btck_block_tree_entry_equals(get(), other.get()) != 0;
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
1089 : : }
1090 : :
1091 : 6 : std::optional<BlockTreeEntry> GetPrevious() const
1092 : : {
1093 : 6 : auto entry{btck_block_tree_entry_get_previous(get())};
1094 [ + + ]: 6 : if (!entry) return std::nullopt;
1095 : 4 : return entry;
1096 : : }
1097 : :
1098 : 211 : int32_t GetHeight() const
1099 : : {
1100 [ + - + - : 211 : return btck_block_tree_entry_get_height(get());
+ - + - +
- ]
1101 : : }
1102 : :
1103 : 207 : BlockHashView GetHash() const
1104 : : {
1105 : 207 : return BlockHashView{btck_block_tree_entry_get_block_hash(get())};
1106 : : }
1107 : :
1108 : 206 : BlockHeader GetHeader() const
1109 : : {
1110 : 206 : return BlockHeader{btck_block_tree_entry_get_block_header(get())};
1111 : : }
1112 : :
1113 : 3 : BlockTreeEntry GetAncestor(int32_t height) const
1114 : : {
1115 : 3 : return BlockTreeEntry{btck_block_tree_entry_get_ancestor(get(), height)};
1116 : : }
1117 : :
1118 : : };
1119 : :
1120 : 14 : class KernelNotifications
1121 : : {
1122 : : public:
1123 : 0 : virtual ~KernelNotifications() = default;
1124 : :
1125 : 428 : virtual void BlockTipHandler(SynchronizationState state, BlockTreeEntry entry, double verification_progress) {}
1126 : :
1127 : 3 : virtual void HeaderTipHandler(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) {}
1128 : :
1129 : 13 : virtual void ProgressHandler(std::string_view title, int progress_percent, bool resume_possible) {}
1130 : :
1131 : 0 : virtual void WarningSetHandler(Warning warning, std::string_view message) {}
1132 : :
1133 : 428 : virtual void WarningUnsetHandler(Warning warning) {}
1134 : :
1135 : 0 : virtual void FlushErrorHandler(std::string_view error) {}
1136 : :
1137 : 0 : virtual void FatalErrorHandler(std::string_view error) {}
1138 : : };
1139 : :
1140 : : template <typename Derived>
1141 : : class BlockValidationStateApi
1142 : : {
1143 : : private:
1144 : 431 : auto impl() const
1145 : : {
1146 : 432 : return static_cast<const Derived*>(this)->get();
1147 : : }
1148 : :
1149 : : friend Derived;
1150 : : BlockValidationStateApi() = default;
1151 : :
1152 : : public:
1153 : 220 : ValidationMode GetValidationMode() const
1154 : : {
1155 [ + - + - : 220 : return static_cast<ValidationMode>(btck_block_validation_state_get_validation_mode(impl()));
+ - + - +
- + - + -
+ - + - +
- ]
1156 : : }
1157 : :
1158 : 212 : BlockValidationResult GetBlockValidationResult() const
1159 : : {
1160 [ + - + - : 212 : return static_cast<BlockValidationResult>(btck_block_validation_state_get_block_validation_result(impl()));
+ - + - +
- + - ]
1161 : : }
1162 : : };
1163 : :
1164 : : class BlockValidationStateView : public View<btck_BlockValidationState>, public BlockValidationStateApi<BlockValidationStateView>
1165 : : {
1166 : : public:
1167 : 5 : explicit BlockValidationStateView(const btck_BlockValidationState* ptr) : View{ptr} {}
1168 : : };
1169 : :
1170 : 208 : class BlockValidationState : public Handle<btck_BlockValidationState, btck_block_validation_state_copy, btck_block_validation_state_destroy>, public BlockValidationStateApi<BlockValidationState>
1171 : : {
1172 : : public:
1173 : 1 : explicit BlockValidationState() : Handle{btck_block_validation_state_create()} {}
1174 : :
1175 : : explicit BlockValidationState(const BlockValidationStateView& view) : Handle{view} {}
1176 : :
1177 : 416 : explicit BlockValidationState(btck_BlockValidationState* state) : Handle{state} {}
1178 : : };
1179 : :
1180 : 7 : inline bool Block::Check(const ConsensusParamsView& consensus_params,
1181 : : BlockCheckFlags flags,
1182 : : BlockValidationState& state) const
1183 : : {
1184 [ + - + - : 7 : return btck_block_check(get(), consensus_params.get(), static_cast<btck_BlockCheckFlags>(flags), state.get()) == 1;
+ - + - +
- + - + -
+ - + - +
- + - ]
1185 : : }
1186 : :
1187 [ + - - - : 22 : class TxValidationState : public UniqueHandle<btck_TxValidationState, btck_tx_validation_state_destroy>
+ - - - +
- - - ]
1188 : : {
1189 : : public:
1190 : : using UniqueHandle::UniqueHandle; // inherit ctor
1191 : 11 : explicit TxValidationState() : UniqueHandle{btck_tx_validation_state_create()} {}
1192 : :
1193 : 12 : ValidationMode GetValidationMode() const
1194 : : {
1195 [ + - + - : 12 : return static_cast<ValidationMode>(btck_tx_validation_state_get_validation_mode(get()));
+ - + - ]
1196 : : }
1197 : :
1198 : 12 : TxValidationResult GetTxValidationResult() const
1199 : : {
1200 [ + - + - : 12 : return static_cast<TxValidationResult>(btck_tx_validation_state_get_tx_validation_result(get()));
+ - + - ]
1201 : : }
1202 : : };
1203 : :
1204 : 10 : inline bool CheckTransaction(const Transaction& tx, TxValidationState& state)
1205 : : {
1206 [ + - + - : 10 : return btck_transaction_check(tx.get(), state.get()) == 1;
+ - ]
1207 : : }
1208 : :
1209 : 6 : class ValidationInterface
1210 : : {
1211 : : public:
1212 : 0 : virtual ~ValidationInterface() = default;
1213 : :
1214 : 0 : virtual void BlockChecked(Block block, BlockValidationStateView state) {}
1215 : :
1216 : 0 : virtual void PowValidBlock(BlockTreeEntry entry, Block block) {}
1217 : :
1218 : 2 : virtual void BlockConnected(Block block, BlockTreeEntry entry) {}
1219 : :
1220 : 0 : virtual void BlockDisconnected(Block block, BlockTreeEntry entry) {}
1221 : : };
1222 : :
1223 [ + - + - : 33 : class ChainParams : public Handle<btck_ChainParameters, btck_chain_parameters_copy, btck_chain_parameters_destroy>
+ - + - +
- + - + -
+ - + - +
- + - +
- ]
[ # # # # ]
1224 : : {
1225 : : public:
1226 : 17 : explicit ChainParams(ChainType chain_type)
1227 : 17 : : Handle{btck_chain_parameters_create(static_cast<btck_ChainType>(chain_type))} {}
1228 : :
1229 : 1 : explicit ChainParams(std::span<const std::byte> signet_challenge)
1230 : 1 : : Handle{btck_chain_parameters_create_signet(signet_challenge.data(), signet_challenge.size())} {}
1231 : :
1232 : 1 : ConsensusParamsView GetConsensusParams() const
1233 : : {
1234 : 1 : return ConsensusParamsView{btck_chain_parameters_get_consensus_params(get())};
1235 : : }
1236 : : };
1237 : :
1238 [ + - + - : 36 : class ContextOptions : public UniqueHandle<btck_ContextOptions, btck_context_options_destroy>
- - + - -
- + - + -
- - - - -
- ][ # # #
# # # # #
# # ]
1239 : : {
1240 : : public:
1241 : 20 : ContextOptions() : UniqueHandle{btck_context_options_create()} {}
1242 : :
1243 : 14 : void SetChainParams(ChainParams& chain_params)
1244 : : {
1245 [ + - + - ]: 14 : btck_context_options_set_chainparams(get(), chain_params.get());
[ # # ]
1246 : 14 : }
1247 : :
1248 : : template <typename T>
1249 : 14 : void SetNotifications(std::shared_ptr<T> notifications)
1250 : : {
1251 : : static_assert(std::is_base_of_v<KernelNotifications, T>);
1252 [ + - ]: 14 : auto heap_notifications = std::make_unique<std::shared_ptr<T>>(std::move(notifications));
1253 : : using user_type = std::shared_ptr<T>*;
1254 : 14 : btck_context_options_set_notifications(
1255 : : get(),
1256 : : btck_NotificationInterfaceCallbacks{
1257 [ + - ]: 14 : .user_data = heap_notifications.release(),
1258 [ + - ]: 28 : .user_data_destroy = +[](void* user_data) { delete static_cast<user_type>(user_data); },
1259 : 435 : .block_tip = +[](void* user_data, btck_SynchronizationState state, const btck_BlockTreeEntry* entry, double verification_progress) { (*static_cast<user_type>(user_data))->BlockTipHandler(static_cast<SynchronizationState>(state), BlockTreeEntry{entry}, verification_progress); },
1260 : : .header_tip = +[](void* user_data, btck_SynchronizationState state, int64_t height, int64_t timestamp, int presync) { (*static_cast<user_type>(user_data))->HeaderTipHandler(static_cast<SynchronizationState>(state), height, timestamp, presync == 1); },
1261 : : .progress = +[](void* user_data, const char* title, size_t title_len, int progress_percent, int resume_possible) { (*static_cast<user_type>(user_data))->ProgressHandler({title, title_len}, progress_percent, resume_possible == 1); },
1262 : : .warning_set = +[](void* user_data, btck_Warning warning, const char* message, size_t message_len) { (*static_cast<user_type>(user_data))->WarningSetHandler(static_cast<Warning>(warning), {message, message_len}); },
1263 : : .warning_unset = +[](void* user_data, btck_Warning warning) { (*static_cast<user_type>(user_data))->WarningUnsetHandler(static_cast<Warning>(warning)); },
1264 : : .flush_error = +[](void* user_data, const char* error, size_t error_len) { (*static_cast<user_type>(user_data))->FlushErrorHandler({error, error_len}); },
1265 : : .fatal_error = +[](void* user_data, const char* error, size_t error_len) { (*static_cast<user_type>(user_data))->FatalErrorHandler({error, error_len}); },
1266 : : });
1267 : 14 : }
1268 : :
1269 : : template <typename T>
1270 : 6 : void SetValidationInterface(std::shared_ptr<T> validation_interface)
1271 : : {
1272 : : static_assert(std::is_base_of_v<ValidationInterface, T>);
1273 [ + - ]: 6 : auto heap_vi = std::make_unique<std::shared_ptr<T>>(std::move(validation_interface));
1274 : : using user_type = std::shared_ptr<T>*;
1275 : 6 : btck_context_options_set_validation_interface(
1276 : : get(),
1277 : : btck_ValidationInterfaceCallbacks{
1278 [ + - ]: 6 : .user_data = heap_vi.release(),
1279 [ + - ]: 12 : .user_data_destroy = +[](void* user_data) { delete static_cast<user_type>(user_data); },
1280 [ + - ]: 10 : .block_checked = +[](void* user_data, btck_Block* block, const btck_BlockValidationState* state) { (*static_cast<user_type>(user_data))->BlockChecked(Block{block}, BlockValidationStateView{state}); },
1281 [ # # # # ]: 0 : .pow_valid_block = +[](void* user_data, btck_Block* block, const btck_BlockTreeEntry* entry) { (*static_cast<user_type>(user_data))->PowValidBlock(BlockTreeEntry{entry}, Block{block}); },
1282 [ + - ]: 8 : .block_connected = +[](void* user_data, btck_Block* block, const btck_BlockTreeEntry* entry) { (*static_cast<user_type>(user_data))->BlockConnected(Block{block}, BlockTreeEntry{entry}); },
1283 [ # # ]: 0 : .block_disconnected = +[](void* user_data, btck_Block* block, const btck_BlockTreeEntry* entry) { (*static_cast<user_type>(user_data))->BlockDisconnected(Block{block}, BlockTreeEntry{entry}); },
1284 : : });
1285 : 6 : }
1286 : : };
1287 : :
1288 [ + - + - : 27 : class Context : public Handle<btck_Context, btck_context_copy, btck_context_destroy>
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - +
- + - + -
+ - + - ]
1289 : : {
1290 : : public:
1291 : 16 : Context(ContextOptions& opts)
1292 : 16 : : Handle{btck_context_create(opts.get())} {}
1293 : :
1294 : 4 : Context()
1295 [ + - + - : 8 : : Handle{btck_context_create(ContextOptions{}.get())} {}
+ - ]
1296 : :
1297 : 1 : bool interrupt()
1298 : : {
1299 [ + - + - ]: 1 : return btck_context_interrupt(get()) == 0;
1300 : : }
1301 : : };
1302 : :
1303 [ + - - - : 34 : class ChainstateManagerOptions : public UniqueHandle<btck_ChainstateManagerOptions, btck_chainstate_manager_options_destroy>
+ - + - -
- - - + -
- - - - -
- ][ # # #
# # # ]
1304 : : {
1305 : : public:
1306 : 21 : ChainstateManagerOptions(const Context& context, std::string_view data_dir, std::string_view blocks_dir)
1307 : 21 : : UniqueHandle{btck_chainstate_manager_options_create(
1308 : 21 : context.get(), data_dir.data(), data_dir.length(), blocks_dir.data(), blocks_dir.length())}
1309 : : {
1310 : 17 : }
1311 : :
1312 : 6 : void SetWorkerThreads(int worker_threads)
1313 : : {
1314 [ + - ]: 6 : btck_chainstate_manager_options_set_worker_threads_num(get(), worker_threads);
1315 : 6 : }
1316 : :
1317 : 2 : bool SetDatabaseCacheBytes(uint64_t database_cache_bytes)
1318 : : {
1319 [ + - + - : 2 : return btck_chainstate_manager_options_set_database_cache_bytes(get(), database_cache_bytes) == 0;
+ - ]
1320 : : }
1321 : :
1322 : 6 : bool SetWipeDbs(bool wipe_block_tree, bool wipe_chainstate)
1323 : : {
1324 [ + - + - : 6 : return btck_chainstate_manager_options_set_wipe_dbs(get(), wipe_block_tree, wipe_chainstate) == 0;
+ - + - +
- + - + -
+ - + - ]
1325 : : }
1326 : :
1327 : 3 : void UpdateBlockTreeDbInMemory(bool block_tree_db_in_memory)
1328 : : {
1329 [ + - ]: 3 : btck_chainstate_manager_options_update_block_tree_db_in_memory(get(), block_tree_db_in_memory);
1330 : 3 : }
1331 : :
1332 : 3 : void UpdateChainstateDbInMemory(bool chainstate_db_in_memory)
1333 : : {
1334 [ + - ]: 3 : btck_chainstate_manager_options_update_chainstate_db_in_memory(get(), chainstate_db_in_memory);
1335 : 3 : }
1336 : : };
1337 : :
1338 : : class ChainView : public View<btck_Chain>
1339 : : {
1340 : : public:
1341 : 538 : explicit ChainView(const btck_Chain* ptr) : View{ptr} {}
1342 : :
1343 : 1 : int32_t Height() const
1344 : : {
1345 [ + - ]: 1 : return btck_chain_get_height(get());
1346 : : }
1347 : :
1348 : 290 : int32_t CountEntries() const
1349 : : {
1350 [ + - + - : 291 : return btck_chain_get_height(get()) + 1;
+ - + - ]
1351 : : }
1352 : :
1353 : 7512 : BlockTreeEntry GetByHeight(int32_t height) const
1354 : : {
1355 : 7512 : auto index{btck_chain_get_by_height(get(), height)};
1356 [ + + + - ]: 7512 : if (!index) throw std::runtime_error("No entry in the chain at the provided height");
1357 : 7511 : return index;
1358 : : }
1359 : :
1360 : 207 : bool Contains(BlockTreeEntry& entry) const
1361 : : {
1362 [ + - + - : 207 : return btck_chain_contains(get(), entry.get());
+ - ]
1363 : : }
1364 : :
1365 [ + - + - : 7859 : MAKE_RANGE_METHOD(Entries, ChainView, &ChainView::CountEntries, &ChainView::GetByHeight, *this)
+ - + - +
- + - + -
+ - ]
1366 : : };
1367 : :
1368 : : template <typename Derived>
1369 : : class CoinApi
1370 : : {
1371 : : private:
1372 : 4 : auto impl() const
1373 : : {
1374 : 4 : return static_cast<const Derived*>(this)->get();
1375 : : }
1376 : :
1377 : : friend Derived;
1378 : : CoinApi() = default;
1379 : :
1380 : : public:
1381 [ + - ]: 1 : uint32_t GetConfirmationHeight() const { return btck_coin_confirmation_height(impl()); }
1382 : :
1383 [ + - ]: 1 : bool IsCoinbase() const { return btck_coin_is_coinbase(impl()) == 1; }
1384 : :
1385 : 2 : TransactionOutputView GetOutput() const
1386 : : {
1387 : 2 : return TransactionOutputView{btck_coin_get_output(impl())};
1388 : : }
1389 : : };
1390 : :
1391 : : class CoinView : public View<btck_Coin>, public CoinApi<CoinView>
1392 : : {
1393 : : public:
1394 : 24 : explicit CoinView(const btck_Coin* ptr) : View{ptr} {}
1395 : : };
1396 : :
1397 [ + - + - : 10 : class Coin : public Handle<btck_Coin, btck_coin_copy, btck_coin_destroy>, public CoinApi<Coin>
+ - + - +
- + - + -
+ - ]
1398 : : {
1399 : : public:
1400 : : Coin(btck_Coin* coin) : Handle{coin} {}
1401 : :
1402 [ + - + - ]: 2 : Coin(const CoinView& view) : Handle{view} {}
1403 : : };
1404 : :
1405 : : template <typename Derived>
1406 : : class TransactionSpentOutputsApi
1407 : : {
1408 : : private:
1409 : 34 : auto impl() const
1410 : : {
1411 : 34 : return static_cast<const Derived*>(this)->get();
1412 : : }
1413 : :
1414 : : friend Derived;
1415 : : TransactionSpentOutputsApi() = default;
1416 : :
1417 : : public:
1418 : 22 : size_t Count() const
1419 : : {
1420 [ + - + - : 22 : return btck_transaction_spent_outputs_count(impl());
+ - ]
1421 : : }
1422 : :
1423 : 12 : CoinView GetCoin(size_t index) const
1424 : : {
1425 : 12 : return CoinView{btck_transaction_spent_outputs_get_coin_at(impl(), index)};
1426 : : }
1427 : :
1428 [ + - + - ]: 31 : MAKE_RANGE_METHOD(Coins, Derived, &TransactionSpentOutputsApi<Derived>::Count, &TransactionSpentOutputsApi<Derived>::GetCoin, *static_cast<const Derived*>(this))
1429 : : };
1430 : :
1431 : : class TransactionSpentOutputsView : public View<btck_TransactionSpentOutputs>, public TransactionSpentOutputsApi<TransactionSpentOutputsView>
1432 : : {
1433 : : public:
1434 : 24 : explicit TransactionSpentOutputsView(const btck_TransactionSpentOutputs* ptr) : View{ptr} {}
1435 : : };
1436 : :
1437 [ + - + - : 10 : class TransactionSpentOutputs : public Handle<btck_TransactionSpentOutputs, btck_transaction_spent_outputs_copy, btck_transaction_spent_outputs_destroy>,
+ - + - +
- + - + -
+ - ]
1438 : : public TransactionSpentOutputsApi<TransactionSpentOutputs>
1439 : : {
1440 : : public:
1441 : : TransactionSpentOutputs(btck_TransactionSpentOutputs* transaction_spent_outputs) : Handle{transaction_spent_outputs} {}
1442 : :
1443 [ + - + - ]: 2 : TransactionSpentOutputs(const TransactionSpentOutputsView& view) : Handle{view} {}
1444 : : };
1445 : :
1446 [ + - + - : 12 : class BlockSpentOutputs : public Handle<btck_BlockSpentOutputs, btck_block_spent_outputs_copy, btck_block_spent_outputs_destroy>
+ - + - +
- + - + -
+ - - - +
- + - ]
1447 : : {
1448 : : public:
1449 : 5 : BlockSpentOutputs(btck_BlockSpentOutputs* block_spent_outputs)
1450 : 9 : : Handle{block_spent_outputs}
1451 : : {
1452 : : }
1453 : :
1454 : 25 : size_t Count() const
1455 : : {
1456 [ + - + - : 25 : return btck_block_spent_outputs_count(get());
+ - + - +
- + - ]
1457 : : }
1458 : :
1459 : 12 : TransactionSpentOutputsView GetTxSpentOutputs(size_t tx_undo_index) const
1460 : : {
1461 : 12 : return TransactionSpentOutputsView{btck_block_spent_outputs_get_transaction_spent_outputs_at(get(), tx_undo_index)};
1462 : : }
1463 : :
1464 [ + - + - ]: 31 : MAKE_RANGE_METHOD(TxsSpentOutputs, BlockSpentOutputs, &BlockSpentOutputs::Count, &BlockSpentOutputs::GetTxSpentOutputs, *this)
1465 : : };
1466 : :
1467 [ + - + - : 34 : class ChainMan : UniqueHandle<btck_ChainstateManager, btck_chainstate_manager_destroy>
+ - + - +
- + - +
- ][ # # ]
1468 : : {
1469 : : public:
1470 : 17 : ChainMan(const Context& context, const ChainstateManagerOptions& chainman_opts)
1471 : 17 : : UniqueHandle{btck_chainstate_manager_create(chainman_opts.get())}
1472 : : {
1473 : 17 : }
1474 : :
1475 : 2 : bool ImportBlocks(const std::span<const std::string> paths)
1476 : : {
1477 : 2 : std::vector<const char*> c_paths;
1478 : 2 : std::vector<size_t> c_paths_lens;
1479 [ + - ]: 2 : c_paths.reserve(paths.size());
1480 [ + - ]: 2 : c_paths_lens.reserve(paths.size());
1481 [ + + ]: 3 : for (const auto& path : paths) {
1482 [ + - ]: 1 : c_paths.push_back(path.c_str());
1483 [ - + + - ]: 1 : c_paths_lens.push_back(path.length());
1484 : : }
1485 : :
1486 [ - + + - ]: 2 : return btck_chainstate_manager_import_blocks(get(), c_paths.data(), c_paths_lens.data(), c_paths.size()) == 0;
1487 : 2 : }
1488 : :
1489 : 421 : bool ProcessBlock(const Block& block, bool* new_block)
1490 : : {
1491 : 421 : int _new_block;
1492 : 421 : int res = btck_chainstate_manager_process_block(get(), block.get(), &_new_block);
1493 [ + - ]: 421 : if (new_block) *new_block = _new_block == 1;
1494 : 421 : return res == 0;
1495 : : }
1496 : :
1497 : 208 : BlockValidationState ProcessBlockHeader(const BlockHeader& header)
1498 : : {
1499 : 208 : auto state = btck_chainstate_manager_process_block_header(get(), header.get());
1500 : 208 : return BlockValidationState{state};
1501 : : }
1502 : :
1503 : 269 : ChainView GetChain() const
1504 : : {
1505 : 269 : return ChainView{btck_chainstate_manager_get_active_chain(get())};
1506 : : }
1507 : :
1508 : 207 : std::optional<BlockTreeEntry> GetBlockTreeEntry(const BlockHash& block_hash) const
1509 : : {
1510 : 207 : auto entry{btck_chainstate_manager_get_block_tree_entry_by_hash(get(), block_hash.get())};
1511 [ - + ]: 207 : if (!entry) return std::nullopt;
1512 : 207 : return entry;
1513 : : }
1514 : :
1515 : 206 : BlockTreeEntry GetBestEntry() const
1516 : : {
1517 : 206 : return btck_chainstate_manager_get_best_entry(get());
1518 : : }
1519 : :
1520 : 6676 : std::optional<Block> ReadBlock(const BlockTreeEntry& entry) const
1521 : : {
1522 : 6676 : auto block{btck_block_read(get(), entry.get())};
1523 [ + + ]: 6676 : if (!block) return std::nullopt;
1524 : 6675 : return block;
1525 : : }
1526 : :
1527 : 5 : BlockSpentOutputs ReadBlockSpentOutputs(const BlockTreeEntry& entry) const
1528 : : {
1529 : 5 : return btck_block_spent_outputs_read(get(), entry.get());
1530 : : }
1531 : : };
1532 : :
1533 : 5 : inline void set_mock_time(std::chrono::seconds timestamp)
1534 : : {
1535 [ + + ]: 5 : if (btck_set_mock_time(timestamp.count()) != 0) {
1536 [ + - ]: 2 : throw std::runtime_error("timestamp out of range");
1537 : : }
1538 : 3 : }
1539 : :
1540 : : } // namespace btck
1541 : :
1542 : : #endif // BITCOIN_KERNEL_BITCOINKERNEL_WRAPPER_H
|