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