Bitcoin Core  31.0.0
P2P Digital Currency
bitcoinkernel.cpp
Go to the documentation of this file.
1 // Copyright (c) 2022-present The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #define BITCOINKERNEL_BUILD
6 
7 #include <kernel/bitcoinkernel.h>
8 
9 #include <chain.h>
10 #include <coins.h>
11 #include <consensus/validation.h>
12 #include <dbwrapper.h>
13 #include <kernel/caches.h>
14 #include <kernel/chainparams.h>
15 #include <kernel/checks.h>
16 #include <kernel/context.h>
18 #include <kernel/warning.h>
19 #include <logging.h>
20 #include <node/blockstorage.h>
21 #include <node/chainstate.h>
22 #include <primitives/block.h>
23 #include <primitives/transaction.h>
24 #include <script/interpreter.h>
25 #include <script/script.h>
26 #include <serialize.h>
27 #include <streams.h>
28 #include <sync.h>
29 #include <uint256.h>
30 #include <undo.h>
31 #include <util/check.h>
32 #include <util/fs.h>
33 #include <util/result.h>
34 #include <util/signalinterrupt.h>
35 #include <util/task_runner.h>
36 #include <util/translation.h>
37 #include <validation.h>
38 #include <validationinterface.h>
39 
40 #include <cstddef>
41 #include <cstring>
42 #include <exception>
43 #include <functional>
44 #include <list>
45 #include <memory>
46 #include <span>
47 #include <stdexcept>
48 #include <string>
49 #include <tuple>
50 #include <utility>
51 #include <vector>
52 
55 
56 // Define G_TRANSLATION_FUN symbol in libbitcoinkernel library so users of the
57 // library aren't required to export this symbol
58 extern const TranslateFn G_TRANSLATION_FUN{nullptr};
59 
61 
62 namespace {
63 
64 bool is_valid_flag_combination(script_verify_flags flags)
65 {
66  if (flags & SCRIPT_VERIFY_CLEANSTACK && ~flags & (SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS)) return false;
67  if (flags & SCRIPT_VERIFY_WITNESS && ~flags & SCRIPT_VERIFY_P2SH) return false;
68  return true;
69 }
70 
71 class WriterStream
72 {
73 private:
74  btck_WriteBytes m_writer;
75  void* m_user_data;
76 
77 public:
78  WriterStream(btck_WriteBytes writer, void* user_data)
79  : m_writer{writer}, m_user_data{user_data} {}
80 
81  //
82  // Stream subset
83  //
84  void write(std::span<const std::byte> src)
85  {
86  if (m_writer(src.data(), src.size(), m_user_data) != 0) {
87  throw std::runtime_error("Failed to write serialization data");
88  }
89  }
90 
91  template <typename T>
92  WriterStream& operator<<(const T& obj)
93  {
94  ::Serialize(*this, obj);
95  return *this;
96  }
97 };
98 
99 template <typename C, typename CPP>
100 struct Handle {
101  static C* ref(CPP* cpp_type)
102  {
103  return reinterpret_cast<C*>(cpp_type);
104  }
105 
106  static const C* ref(const CPP* cpp_type)
107  {
108  return reinterpret_cast<const C*>(cpp_type);
109  }
110 
111  template <typename... Args>
112  static C* create(Args&&... args)
113  {
114  auto cpp_obj{std::make_unique<CPP>(std::forward<Args>(args)...)};
115  return ref(cpp_obj.release());
116  }
117 
118  static C* copy(const C* ptr)
119  {
120  auto cpp_obj{std::make_unique<CPP>(get(ptr))};
121  return ref(cpp_obj.release());
122  }
123 
124  static const CPP& get(const C* ptr)
125  {
126  return *reinterpret_cast<const CPP*>(ptr);
127  }
128 
129  static CPP& get(C* ptr)
130  {
131  return *reinterpret_cast<CPP*>(ptr);
132  }
133 
134  static void operator delete(void* ptr)
135  {
136  delete reinterpret_cast<CPP*>(ptr);
137  }
138 };
139 
140 } // namespace
141 
142 struct btck_BlockTreeEntry: Handle<btck_BlockTreeEntry, CBlockIndex> {};
143 struct btck_Block : Handle<btck_Block, std::shared_ptr<const CBlock>> {};
144 struct btck_BlockValidationState : Handle<btck_BlockValidationState, BlockValidationState> {};
145 
146 namespace {
147 
148 BCLog::Level get_bclog_level(btck_LogLevel level)
149 {
150  switch (level) {
151  case btck_LogLevel_INFO: {
152  return BCLog::Level::Info;
153  }
154  case btck_LogLevel_DEBUG: {
155  return BCLog::Level::Debug;
156  }
157  case btck_LogLevel_TRACE: {
158  return BCLog::Level::Trace;
159  }
160  }
161  assert(false);
162 }
163 
164 BCLog::LogFlags get_bclog_flag(btck_LogCategory category)
165 {
166  switch (category) {
167  case btck_LogCategory_BENCH: {
168  return BCLog::LogFlags::BENCH;
169  }
172  }
175  }
178  }
181  }
182  case btck_LogCategory_PRUNE: {
183  return BCLog::LogFlags::PRUNE;
184  }
185  case btck_LogCategory_RAND: {
186  return BCLog::LogFlags::RAND;
187  }
190  }
193  }
196  }
197  case btck_LogCategory_ALL: {
198  return BCLog::LogFlags::ALL;
199  }
200  }
201  assert(false);
202 }
203 
205 {
206  switch (state) {
213  } // no default case, so the compiler can warn about missing cases
214  assert(false);
215 }
216 
217 btck_Warning cast_btck_warning(kernel::Warning warning)
218 {
219  switch (warning) {
224  } // no default case, so the compiler can warn about missing cases
225  assert(false);
226 }
227 
228 struct LoggingConnection {
229  std::unique_ptr<std::list<std::function<void(const std::string&)>>::iterator> m_connection;
230  void* m_user_data;
231  std::function<void(void* user_data)> m_deleter;
232 
233  LoggingConnection(btck_LogCallback callback, void* user_data, btck_DestroyCallback user_data_destroy_callback)
234  {
235  LOCK(cs_main);
236 
237  auto connection{LogInstance().PushBackCallback([callback, user_data](const std::string& str) { callback(user_data, str.c_str(), str.length()); })};
238 
239  // Only start logging if we just added the connection.
240  if (LogInstance().NumConnections() == 1 && !LogInstance().StartLogging()) {
241  LogError("Logger start failed.");
242  LogInstance().DeleteCallback(connection);
243  if (user_data && user_data_destroy_callback) {
244  user_data_destroy_callback(user_data);
245  }
246  throw std::runtime_error("Failed to start logging");
247  }
248 
249  m_connection = std::make_unique<std::list<std::function<void(const std::string&)>>::iterator>(connection);
250  m_user_data = user_data;
251  m_deleter = user_data_destroy_callback;
252 
253  LogDebug(BCLog::KERNEL, "Logger connected.");
254  }
255 
256  ~LoggingConnection()
257  {
258  LOCK(cs_main);
259  LogDebug(BCLog::KERNEL, "Logger disconnecting.");
260 
261  // Switch back to buffering by calling DisconnectTestLogger if the
262  // connection that we are about to remove is the last one.
263  if (LogInstance().NumConnections() == 1) {
265  } else {
267  }
268 
269  m_connection.reset();
270  if (m_user_data && m_deleter) {
271  m_deleter(m_user_data);
272  }
273  }
274 };
275 
276 class KernelNotifications final : public kernel::Notifications
277 {
278 private:
280 
281 public:
282  KernelNotifications(btck_NotificationInterfaceCallbacks cbs)
283  : m_cbs{cbs}
284  {
285  }
286 
287  ~KernelNotifications()
288  {
289  if (m_cbs.user_data && m_cbs.user_data_destroy) {
290  m_cbs.user_data_destroy(m_cbs.user_data);
291  }
292  m_cbs.user_data_destroy = nullptr;
293  m_cbs.user_data = nullptr;
294  }
295 
296  kernel::InterruptResult blockTip(SynchronizationState state, const CBlockIndex& index, double verification_progress) override
297  {
298  if (m_cbs.block_tip) m_cbs.block_tip(m_cbs.user_data, cast_state(state), btck_BlockTreeEntry::ref(&index), verification_progress);
299  return {};
300  }
301  void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync) override
302  {
303  if (m_cbs.header_tip) m_cbs.header_tip(m_cbs.user_data, cast_state(state), height, timestamp, presync ? 1 : 0);
304  }
305  void progress(const bilingual_str& title, int progress_percent, bool resume_possible) override
306  {
307  if (m_cbs.progress) m_cbs.progress(m_cbs.user_data, title.original.c_str(), title.original.length(), progress_percent, resume_possible ? 1 : 0);
308  }
309  void warningSet(kernel::Warning id, const bilingual_str& message) override
310  {
311  if (m_cbs.warning_set) m_cbs.warning_set(m_cbs.user_data, cast_btck_warning(id), message.original.c_str(), message.original.length());
312  }
313  void warningUnset(kernel::Warning id) override
314  {
315  if (m_cbs.warning_unset) m_cbs.warning_unset(m_cbs.user_data, cast_btck_warning(id));
316  }
317  void flushError(const bilingual_str& message) override
318  {
319  if (m_cbs.flush_error) m_cbs.flush_error(m_cbs.user_data, message.original.c_str(), message.original.length());
320  }
321  void fatalError(const bilingual_str& message) override
322  {
323  if (m_cbs.fatal_error) m_cbs.fatal_error(m_cbs.user_data, message.original.c_str(), message.original.length());
324  }
325 };
326 
327 class KernelValidationInterface final : public CValidationInterface
328 {
329 public:
331 
332  explicit KernelValidationInterface(const btck_ValidationInterfaceCallbacks vi_cbs) : m_cbs{vi_cbs} {}
333 
334  ~KernelValidationInterface()
335  {
336  if (m_cbs.user_data && m_cbs.user_data_destroy) {
337  m_cbs.user_data_destroy(m_cbs.user_data);
338  }
339  m_cbs.user_data = nullptr;
340  m_cbs.user_data_destroy = nullptr;
341  }
342 
343 protected:
344  void BlockChecked(const std::shared_ptr<const CBlock>& block, const BlockValidationState& stateIn) override
345  {
346  if (m_cbs.block_checked) {
347  m_cbs.block_checked(m_cbs.user_data,
348  btck_Block::copy(btck_Block::ref(&block)),
349  btck_BlockValidationState::ref(&stateIn));
350  }
351  }
352 
353  void NewPoWValidBlock(const CBlockIndex* pindex, const std::shared_ptr<const CBlock>& block) override
354  {
355  if (m_cbs.pow_valid_block) {
356  m_cbs.pow_valid_block(m_cbs.user_data,
357  btck_Block::copy(btck_Block::ref(&block)),
358  btck_BlockTreeEntry::ref(pindex));
359  }
360  }
361 
362  void BlockConnected(const ChainstateRole& role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override
363  {
364  if (m_cbs.block_connected) {
365  m_cbs.block_connected(m_cbs.user_data,
366  btck_Block::copy(btck_Block::ref(&block)),
367  btck_BlockTreeEntry::ref(pindex));
368  }
369  }
370 
371  void BlockDisconnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override
372  {
373  if (m_cbs.block_disconnected) {
374  m_cbs.block_disconnected(m_cbs.user_data,
375  btck_Block::copy(btck_Block::ref(&block)),
376  btck_BlockTreeEntry::ref(pindex));
377  }
378  }
379 };
380 
381 struct ContextOptions {
382  mutable Mutex m_mutex;
383  std::unique_ptr<const CChainParams> m_chainparams GUARDED_BY(m_mutex);
384  std::shared_ptr<KernelNotifications> m_notifications GUARDED_BY(m_mutex);
385  std::shared_ptr<KernelValidationInterface> m_validation_interface GUARDED_BY(m_mutex);
386 };
387 
388 class Context
389 {
390 public:
391  std::unique_ptr<kernel::Context> m_context;
392 
393  std::shared_ptr<KernelNotifications> m_notifications;
394 
395  std::unique_ptr<util::SignalInterrupt> m_interrupt;
396 
397  std::unique_ptr<ValidationSignals> m_signals;
398 
399  std::unique_ptr<const CChainParams> m_chainparams;
400 
401  std::shared_ptr<KernelValidationInterface> m_validation_interface;
402 
403  Context(const ContextOptions* options, bool& sane)
404  : m_context{std::make_unique<kernel::Context>()},
405  m_interrupt{std::make_unique<util::SignalInterrupt>()}
406  {
407  if (options) {
408  LOCK(options->m_mutex);
409  if (options->m_chainparams) {
410  m_chainparams = std::make_unique<const CChainParams>(*options->m_chainparams);
411  }
412  if (options->m_notifications) {
413  m_notifications = options->m_notifications;
414  }
415  if (options->m_validation_interface) {
416  m_signals = std::make_unique<ValidationSignals>(std::make_unique<ImmediateTaskRunner>());
417  m_validation_interface = options->m_validation_interface;
418  m_signals->RegisterSharedValidationInterface(m_validation_interface);
419  }
420  }
421 
422  if (!m_chainparams) {
423  m_chainparams = CChainParams::Main();
424  }
425  if (!m_notifications) {
426  m_notifications = std::make_shared<KernelNotifications>(btck_NotificationInterfaceCallbacks{
427  nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr});
428  }
429 
431  sane = false;
432  }
433  }
434 
435  ~Context()
436  {
437  if (m_signals) {
438  m_signals->UnregisterSharedValidationInterface(m_validation_interface);
439  }
440  }
441 };
442 
444 struct ChainstateManagerOptions {
445  mutable Mutex m_mutex;
446  ChainstateManager::Options m_chainman_options GUARDED_BY(m_mutex);
447  node::BlockManager::Options m_blockman_options GUARDED_BY(m_mutex);
448  std::shared_ptr<const Context> m_context;
449  node::ChainstateLoadOptions m_chainstate_load_options GUARDED_BY(m_mutex);
450 
451  ChainstateManagerOptions(const std::shared_ptr<const Context>& context, const fs::path& data_dir, const fs::path& blocks_dir)
452  : m_chainman_options{ChainstateManager::Options{
453  .chainparams = *context->m_chainparams,
454  .datadir = data_dir,
455  .notifications = *context->m_notifications,
456  .signals = context->m_signals.get()}},
457  m_blockman_options{node::BlockManager::Options{
458  .chainparams = *context->m_chainparams,
459  .blocks_dir = blocks_dir,
460  .notifications = *context->m_notifications,
461  .block_tree_db_params = DBParams{
462  .path = data_dir / "blocks" / "index",
463  .cache_bytes = kernel::CacheSizes{DEFAULT_KERNEL_CACHE}.block_tree_db,
464  }}},
465  m_context{context}, m_chainstate_load_options{node::ChainstateLoadOptions{}}
466  {
467  }
468 };
469 
470 struct ChainMan {
471  std::unique_ptr<ChainstateManager> m_chainman;
472  std::shared_ptr<const Context> m_context;
473 
474  ChainMan(std::unique_ptr<ChainstateManager> chainman, std::shared_ptr<const Context> context)
475  : m_chainman(std::move(chainman)), m_context(std::move(context)) {}
476 };
477 
478 } // namespace
479 
480 struct btck_Transaction : Handle<btck_Transaction, std::shared_ptr<const CTransaction>> {};
481 struct btck_TransactionOutput : Handle<btck_TransactionOutput, CTxOut> {};
482 struct btck_ScriptPubkey : Handle<btck_ScriptPubkey, CScript> {};
483 struct btck_LoggingConnection : Handle<btck_LoggingConnection, LoggingConnection> {};
484 struct btck_ContextOptions : Handle<btck_ContextOptions, ContextOptions> {};
485 struct btck_Context : Handle<btck_Context, std::shared_ptr<const Context>> {};
486 struct btck_ChainParameters : Handle<btck_ChainParameters, CChainParams> {};
487 struct btck_ChainstateManagerOptions : Handle<btck_ChainstateManagerOptions, ChainstateManagerOptions> {};
488 struct btck_ChainstateManager : Handle<btck_ChainstateManager, ChainMan> {};
489 struct btck_Chain : Handle<btck_Chain, CChain> {};
490 struct btck_BlockSpentOutputs : Handle<btck_BlockSpentOutputs, std::shared_ptr<CBlockUndo>> {};
491 struct btck_TransactionSpentOutputs : Handle<btck_TransactionSpentOutputs, CTxUndo> {};
492 struct btck_Coin : Handle<btck_Coin, Coin> {};
493 struct btck_BlockHash : Handle<btck_BlockHash, uint256> {};
494 struct btck_TransactionInput : Handle<btck_TransactionInput, CTxIn> {};
495 struct btck_TransactionOutPoint: Handle<btck_TransactionOutPoint, COutPoint> {};
496 struct btck_Txid: Handle<btck_Txid, Txid> {};
497 struct btck_PrecomputedTransactionData : Handle<btck_PrecomputedTransactionData, PrecomputedTransactionData> {};
498 struct btck_BlockHeader: Handle<btck_BlockHeader, CBlockHeader> {};
499 
500 btck_Transaction* btck_transaction_create(const void* raw_transaction, size_t raw_transaction_len)
501 {
502  if (raw_transaction == nullptr && raw_transaction_len != 0) {
503  return nullptr;
504  }
505  try {
506  SpanReader stream{std::span{reinterpret_cast<const std::byte*>(raw_transaction), raw_transaction_len}};
507  return btck_Transaction::create(std::make_shared<const CTransaction>(deserialize, TX_WITH_WITNESS, stream));
508  } catch (...) {
509  return nullptr;
510  }
511 }
512 
514 {
515  return btck_Transaction::get(transaction)->vout.size();
516 }
517 
518 const btck_TransactionOutput* btck_transaction_get_output_at(const btck_Transaction* transaction, size_t output_index)
519 {
520  const CTransaction& tx = *btck_Transaction::get(transaction);
521  assert(output_index < tx.vout.size());
522  return btck_TransactionOutput::ref(&tx.vout[output_index]);
523 }
524 
526 {
527  return btck_Transaction::get(transaction)->vin.size();
528 }
529 
530 const btck_TransactionInput* btck_transaction_get_input_at(const btck_Transaction* transaction, size_t input_index)
531 {
532  assert(input_index < btck_Transaction::get(transaction)->vin.size());
533  return btck_TransactionInput::ref(&btck_Transaction::get(transaction)->vin[input_index]);
534 }
535 
537 {
538  return btck_Txid::ref(&btck_Transaction::get(transaction)->GetHash());
539 }
540 
542 {
543  return btck_Transaction::copy(transaction);
544 }
545 
546 int btck_transaction_to_bytes(const btck_Transaction* transaction, btck_WriteBytes writer, void* user_data)
547 {
548  try {
549  WriterStream ws{writer, user_data};
550  ws << TX_WITH_WITNESS(btck_Transaction::get(transaction));
551  return 0;
552  } catch (...) {
553  return -1;
554  }
555 }
556 
558 {
559  delete transaction;
560 }
561 
562 btck_ScriptPubkey* btck_script_pubkey_create(const void* script_pubkey, size_t script_pubkey_len)
563 {
564  if (script_pubkey == nullptr && script_pubkey_len != 0) {
565  return nullptr;
566  }
567  auto data = std::span{reinterpret_cast<const uint8_t*>(script_pubkey), script_pubkey_len};
568  return btck_ScriptPubkey::create(data.begin(), data.end());
569 }
570 
571 int btck_script_pubkey_to_bytes(const btck_ScriptPubkey* script_pubkey_, btck_WriteBytes writer, void* user_data)
572 {
573  const auto& script_pubkey{btck_ScriptPubkey::get(script_pubkey_)};
574  return writer(script_pubkey.data(), script_pubkey.size(), user_data);
575 }
576 
578 {
579  return btck_ScriptPubkey::copy(script_pubkey);
580 }
581 
583 {
584  delete script_pubkey;
585 }
586 
588 {
589  return btck_TransactionOutput::create(amount, btck_ScriptPubkey::get(script_pubkey));
590 }
591 
593 {
594  return btck_TransactionOutput::copy(output);
595 }
596 
598 {
599  return btck_ScriptPubkey::ref(&btck_TransactionOutput::get(output).scriptPubKey);
600 }
601 
603 {
604  return btck_TransactionOutput::get(output).nValue;
605 }
606 
608 {
609  delete output;
610 }
611 
613  const btck_Transaction* tx_to,
614  const btck_TransactionOutput** spent_outputs_, size_t spent_outputs_len)
615 {
616  try {
617  const CTransaction& tx{*btck_Transaction::get(tx_to)};
618  auto txdata{btck_PrecomputedTransactionData::create()};
619  if (spent_outputs_ != nullptr && spent_outputs_len > 0) {
620  assert(spent_outputs_len == tx.vin.size());
621  std::vector<CTxOut> spent_outputs;
622  spent_outputs.reserve(spent_outputs_len);
623  for (size_t i = 0; i < spent_outputs_len; i++) {
624  const CTxOut& tx_out{btck_TransactionOutput::get(spent_outputs_[i])};
625  spent_outputs.push_back(tx_out);
626  }
627  btck_PrecomputedTransactionData::get(txdata).Init(tx, std::move(spent_outputs));
628  } else {
629  btck_PrecomputedTransactionData::get(txdata).Init(tx, {});
630  }
631 
632  return txdata;
633  } catch (...) {
634  return nullptr;
635  }
636 }
637 
639 {
640  return btck_PrecomputedTransactionData::copy(precomputed_txdata);
641 }
642 
644 {
645  delete precomputed_txdata;
646 }
647 
649  const int64_t amount,
650  const btck_Transaction* tx_to,
651  const btck_PrecomputedTransactionData* precomputed_txdata,
652  const unsigned int input_index,
654  btck_ScriptVerifyStatus* status)
655 {
656  // Assert that all specified flags are part of the interface before continuing
658 
659  if (!is_valid_flag_combination(script_verify_flags::from_int(flags))) {
661  return 0;
662  }
663 
664  const CTransaction& tx{*btck_Transaction::get(tx_to)};
665  assert(input_index < tx.vin.size());
666 
667  const PrecomputedTransactionData& txdata{precomputed_txdata ? btck_PrecomputedTransactionData::get(precomputed_txdata) : PrecomputedTransactionData(tx)};
668 
669  if (flags & btck_ScriptVerificationFlags_TAPROOT && txdata.m_spent_outputs.empty()) {
671  return 0;
672  }
673 
674  if (status) *status = btck_ScriptVerifyStatus_OK;
675 
676  bool result = VerifyScript(tx.vin[input_index].scriptSig,
677  btck_ScriptPubkey::get(script_pubkey),
678  &tx.vin[input_index].scriptWitness,
680  TransactionSignatureChecker(&tx, input_index, amount, txdata, MissingDataBehavior::FAIL),
681  nullptr);
682  return result ? 1 : 0;
683 }
684 
686 {
687  return btck_TransactionInput::copy(input);
688 }
689 
691 {
692  return btck_TransactionOutPoint::ref(&btck_TransactionInput::get(input).prevout);
693 }
694 
696 {
697  delete input;
698 }
699 
701 {
702  return btck_TransactionOutPoint::copy(out_point);
703 }
704 
706 {
707  return btck_TransactionOutPoint::get(out_point).n;
708 }
709 
711 {
712  return btck_Txid::ref(&btck_TransactionOutPoint::get(out_point).hash);
713 }
714 
716 {
717  delete out_point;
718 }
719 
721 {
722  return btck_Txid::copy(txid);
723 }
724 
725 void btck_txid_to_bytes(const btck_Txid* txid, unsigned char output[32])
726 {
727  std::memcpy(output, btck_Txid::get(txid).begin(), 32);
728 }
729 
730 int btck_txid_equals(const btck_Txid* txid1, const btck_Txid* txid2)
731 {
732  return btck_Txid::get(txid1) == btck_Txid::get(txid2);
733 }
734 
736 {
737  delete txid;
738 }
739 
741 {
742  LOCK(cs_main);
748 }
749 
751 {
752  LOCK(cs_main);
753  if (category == btck_LogCategory_ALL) {
754  LogInstance().SetLogLevel(get_bclog_level(level));
755  }
756 
757  LogInstance().AddCategoryLogLevel(get_bclog_flag(category), get_bclog_level(level));
758 }
759 
761 {
762  LogInstance().EnableCategory(get_bclog_flag(category));
763 }
764 
766 {
767  LogInstance().DisableCategory(get_bclog_flag(category));
768 }
769 
771 {
773 }
774 
776 {
777  try {
778  return btck_LoggingConnection::create(callback, user_data, user_data_destroy_callback);
779  } catch (const std::exception&) {
780  return nullptr;
781  }
782 }
783 
785 {
786  delete connection;
787 }
788 
790 {
791  switch (chain_type) {
792  case btck_ChainType_MAINNET: {
793  return btck_ChainParameters::ref(const_cast<CChainParams*>(CChainParams::Main().release()));
794  }
795  case btck_ChainType_TESTNET: {
796  return btck_ChainParameters::ref(const_cast<CChainParams*>(CChainParams::TestNet().release()));
797  }
799  return btck_ChainParameters::ref(const_cast<CChainParams*>(CChainParams::TestNet4().release()));
800  }
801  case btck_ChainType_SIGNET: {
802  return btck_ChainParameters::ref(const_cast<CChainParams*>(CChainParams::SigNet({}).release()));
803  }
804  case btck_ChainType_REGTEST: {
805  return btck_ChainParameters::ref(const_cast<CChainParams*>(CChainParams::RegTest({}).release()));
806  }
807  }
808  assert(false);
809 }
810 
812 {
813  return btck_ChainParameters::copy(chain_parameters);
814 }
815 
817 {
818  delete chain_parameters;
819 }
820 
822 {
823  return btck_ContextOptions::create();
824 }
825 
827 {
828  // Copy the chainparams, so the caller can free it again
829  LOCK(btck_ContextOptions::get(options).m_mutex);
830  btck_ContextOptions::get(options).m_chainparams = std::make_unique<const CChainParams>(btck_ChainParameters::get(chain_parameters));
831 }
832 
834 {
835  // The KernelNotifications are copy-initialized, so the caller can free them again.
836  LOCK(btck_ContextOptions::get(options).m_mutex);
837  btck_ContextOptions::get(options).m_notifications = std::make_shared<KernelNotifications>(notifications);
838 }
839 
841 {
842  LOCK(btck_ContextOptions::get(options).m_mutex);
843  btck_ContextOptions::get(options).m_validation_interface = std::make_shared<KernelValidationInterface>(vi_cbs);
844 }
845 
847 {
848  delete options;
849 }
850 
852 {
853  bool sane{true};
854  const ContextOptions* opts = options ? &btck_ContextOptions::get(options) : nullptr;
855  auto context{std::make_shared<const Context>(opts, sane)};
856  if (!sane) {
857  LogError("Kernel context sanity check failed.");
858  return nullptr;
859  }
860  return btck_Context::create(context);
861 }
862 
864 {
865  return btck_Context::copy(context);
866 }
867 
869 {
870  return (*btck_Context::get(context)->m_interrupt)() ? 0 : -1;
871 }
872 
874 {
875  delete context;
876 }
877 
879 {
880  if (!btck_BlockTreeEntry::get(entry).pprev) {
881  LogInfo("Genesis block has no previous.");
882  return nullptr;
883  }
884 
885  return btck_BlockTreeEntry::ref(btck_BlockTreeEntry::get(entry).pprev);
886 }
887 
889 {
890  return btck_BlockValidationState::create();
891 }
892 
894 {
895  return btck_BlockValidationState::copy(state);
896 }
897 
899 {
900  delete state;
901 }
902 
904 {
905  auto& block_validation_state = btck_BlockValidationState::get(block_validation_state_);
906  if (block_validation_state.IsValid()) return btck_ValidationMode_VALID;
907  if (block_validation_state.IsInvalid()) return btck_ValidationMode_INVALID;
909 }
910 
912 {
913  auto& block_validation_state = btck_BlockValidationState::get(block_validation_state_);
914  switch (block_validation_state.GetResult()) {
933  } // no default case, so the compiler can warn about missing cases
934  assert(false);
935 }
936 
937 btck_ChainstateManagerOptions* btck_chainstate_manager_options_create(const btck_Context* context, const char* data_dir, size_t data_dir_len, const char* blocks_dir, size_t blocks_dir_len)
938 {
939  if (data_dir == nullptr || data_dir_len == 0 || blocks_dir == nullptr || blocks_dir_len == 0) {
940  LogError("Failed to create chainstate manager options: dir must be non-null and non-empty");
941  return nullptr;
942  }
943  try {
944  fs::path abs_data_dir{fs::absolute(fs::PathFromString({data_dir, data_dir_len}))};
945  fs::create_directories(abs_data_dir);
946  fs::path abs_blocks_dir{fs::absolute(fs::PathFromString({blocks_dir, blocks_dir_len}))};
947  fs::create_directories(abs_blocks_dir);
948  return btck_ChainstateManagerOptions::create(btck_Context::get(context), abs_data_dir, abs_blocks_dir);
949  } catch (const std::exception& e) {
950  LogError("Failed to create chainstate manager options: %s", e.what());
951  return nullptr;
952  }
953 }
954 
956 {
957  LOCK(btck_ChainstateManagerOptions::get(opts).m_mutex);
958  btck_ChainstateManagerOptions::get(opts).m_chainman_options.worker_threads_num = worker_threads;
959 }
960 
962 {
963  delete options;
964 }
965 
966 int btck_chainstate_manager_options_set_wipe_dbs(btck_ChainstateManagerOptions* chainman_opts, int wipe_block_tree_db, int wipe_chainstate_db)
967 {
968  if (wipe_block_tree_db == 1 && wipe_chainstate_db != 1) {
969  LogError("Wiping the block tree db without also wiping the chainstate db is currently unsupported.");
970  return -1;
971  }
972  auto& opts{btck_ChainstateManagerOptions::get(chainman_opts)};
973  LOCK(opts.m_mutex);
974  opts.m_blockman_options.block_tree_db_params.wipe_data = wipe_block_tree_db == 1;
975  opts.m_chainstate_load_options.wipe_chainstate_db = wipe_chainstate_db == 1;
976  return 0;
977 }
978 
980  btck_ChainstateManagerOptions* chainman_opts,
981  int block_tree_db_in_memory)
982 {
983  auto& opts{btck_ChainstateManagerOptions::get(chainman_opts)};
984  LOCK(opts.m_mutex);
985  opts.m_blockman_options.block_tree_db_params.memory_only = block_tree_db_in_memory == 1;
986 }
987 
989  btck_ChainstateManagerOptions* chainman_opts,
990  int chainstate_db_in_memory)
991 {
992  auto& opts{btck_ChainstateManagerOptions::get(chainman_opts)};
993  LOCK(opts.m_mutex);
994  opts.m_chainstate_load_options.coins_db_in_memory = chainstate_db_in_memory == 1;
995 }
996 
998  const btck_ChainstateManagerOptions* chainman_opts)
999 {
1000  auto& opts{btck_ChainstateManagerOptions::get(chainman_opts)};
1001  std::unique_ptr<ChainstateManager> chainman;
1002  try {
1003  LOCK(opts.m_mutex);
1004  chainman = std::make_unique<ChainstateManager>(*opts.m_context->m_interrupt, opts.m_chainman_options, opts.m_blockman_options);
1005  } catch (const std::exception& e) {
1006  LogError("Failed to create chainstate manager: %s", e.what());
1007  return nullptr;
1008  }
1009 
1010  try {
1011  const auto chainstate_load_opts{WITH_LOCK(opts.m_mutex, return opts.m_chainstate_load_options)};
1012 
1014  auto [status, chainstate_err]{node::LoadChainstate(*chainman, cache_sizes, chainstate_load_opts)};
1015  if (status != node::ChainstateLoadStatus::SUCCESS) {
1016  LogError("Failed to load chain state from your data directory: %s", chainstate_err.original);
1017  return nullptr;
1018  }
1019  std::tie(status, chainstate_err) = node::VerifyLoadedChainstate(*chainman, chainstate_load_opts);
1020  if (status != node::ChainstateLoadStatus::SUCCESS) {
1021  LogError("Failed to verify loaded chain state from your datadir: %s", chainstate_err.original);
1022  return nullptr;
1023  }
1024  if (auto result = chainman->ActivateBestChains(); !result) {
1025  LogError("%s", util::ErrorString(result).original);
1026  return nullptr;
1027  }
1028  } catch (const std::exception& e) {
1029  LogError("Failed to load chainstate: %s", e.what());
1030  return nullptr;
1031  }
1032 
1033  return btck_ChainstateManager::create(std::move(chainman), opts.m_context);
1034 }
1035 
1037 {
1038  auto block_index = WITH_LOCK(btck_ChainstateManager::get(chainman).m_chainman->GetMutex(),
1039  return btck_ChainstateManager::get(chainman).m_chainman->m_blockman.LookupBlockIndex(btck_BlockHash::get(block_hash)));
1040  if (!block_index) {
1041  LogDebug(BCLog::KERNEL, "A block with the given hash is not indexed.");
1042  return nullptr;
1043  }
1044  return btck_BlockTreeEntry::ref(block_index);
1045 }
1046 
1048 {
1049  auto& chainman = *btck_ChainstateManager::get(chainstate_manager).m_chainman;
1050  return btck_BlockTreeEntry::ref(WITH_LOCK(chainman.GetMutex(), return chainman.m_best_header));
1051 }
1052 
1054 {
1055  {
1056  LOCK(btck_ChainstateManager::get(chainman).m_chainman->GetMutex());
1057  for (const auto& chainstate : btck_ChainstateManager::get(chainman).m_chainman->m_chainstates) {
1058  if (chainstate->CanFlushToDisk()) {
1059  chainstate->ForceFlushStateToDisk();
1060  chainstate->ResetCoinsViews();
1061  }
1062  }
1063  }
1064 
1065  delete chainman;
1066 }
1067 
1068 int btck_chainstate_manager_import_blocks(btck_ChainstateManager* chainman, const char** block_file_paths_data, size_t* block_file_paths_lens, size_t block_file_paths_data_len)
1069 {
1070  try {
1071  std::vector<fs::path> import_files;
1072  import_files.reserve(block_file_paths_data_len);
1073  for (uint32_t i = 0; i < block_file_paths_data_len; i++) {
1074  if (block_file_paths_data[i] != nullptr) {
1075  import_files.emplace_back(std::string{block_file_paths_data[i], block_file_paths_lens[i]}.c_str());
1076  }
1077  }
1078  auto& chainman_ref{*btck_ChainstateManager::get(chainman).m_chainman};
1079  node::ImportBlocks(chainman_ref, import_files);
1080  WITH_LOCK(::cs_main, chainman_ref.UpdateIBDStatus());
1081  } catch (const std::exception& e) {
1082  LogError("Failed to import blocks: %s", e.what());
1083  return -1;
1084  }
1085  return 0;
1086 }
1087 
1088 btck_Block* btck_block_create(const void* raw_block, size_t raw_block_length)
1089 {
1090  if (raw_block == nullptr && raw_block_length != 0) {
1091  return nullptr;
1092  }
1093  auto block{std::make_shared<CBlock>()};
1094 
1095  SpanReader stream{std::span{reinterpret_cast<const std::byte*>(raw_block), raw_block_length}};
1096 
1097  try {
1098  stream >> TX_WITH_WITNESS(*block);
1099  } catch (...) {
1100  LogDebug(BCLog::KERNEL, "Block decode failed.");
1101  return nullptr;
1102  }
1103 
1104  return btck_Block::create(block);
1105 }
1106 
1108 {
1109  return btck_Block::copy(block);
1110 }
1111 
1113 {
1114  return btck_Block::get(block)->vtx.size();
1115 }
1116 
1118 {
1119  assert(index < btck_Block::get(block)->vtx.size());
1120  return btck_Transaction::ref(&btck_Block::get(block)->vtx[index]);
1121 }
1122 
1124 {
1125  const auto& block_ptr = btck_Block::get(block);
1126  return btck_BlockHeader::create(static_cast<const CBlockHeader&>(*block_ptr));
1127 }
1128 
1129 int btck_block_to_bytes(const btck_Block* block, btck_WriteBytes writer, void* user_data)
1130 {
1131  try {
1132  WriterStream ws{writer, user_data};
1133  ws << TX_WITH_WITNESS(*btck_Block::get(block));
1134  return 0;
1135  } catch (...) {
1136  return -1;
1137  }
1138 }
1139 
1141 {
1142  return btck_BlockHash::create(btck_Block::get(block)->GetHash());
1143 }
1144 
1146 {
1147  delete block;
1148 }
1149 
1151 {
1152  auto block{std::make_shared<CBlock>()};
1153  if (!btck_ChainstateManager::get(chainman).m_chainman->m_blockman.ReadBlock(*block, btck_BlockTreeEntry::get(entry))) {
1154  LogError("Failed to read block.");
1155  return nullptr;
1156  }
1157  return btck_Block::create(block);
1158 }
1159 
1161 {
1162  return btck_BlockHeader::create(btck_BlockTreeEntry::get(entry).GetBlockHeader());
1163 }
1164 
1166 {
1167  return btck_BlockTreeEntry::get(entry).nHeight;
1168 }
1169 
1171 {
1172  return btck_BlockHash::ref(btck_BlockTreeEntry::get(entry).phashBlock);
1173 }
1174 
1176 {
1177  return &btck_BlockTreeEntry::get(entry1) == &btck_BlockTreeEntry::get(entry2);
1178 }
1179 
1180 btck_BlockHash* btck_block_hash_create(const unsigned char block_hash[32])
1181 {
1182  return btck_BlockHash::create(std::span<const unsigned char>{block_hash, 32});
1183 }
1184 
1186 {
1187  return btck_BlockHash::copy(block_hash);
1188 }
1189 
1190 void btck_block_hash_to_bytes(const btck_BlockHash* block_hash, unsigned char output[32])
1191 {
1192  std::memcpy(output, btck_BlockHash::get(block_hash).begin(), 32);
1193 }
1194 
1196 {
1197  return btck_BlockHash::get(hash1) == btck_BlockHash::get(hash2);
1198 }
1199 
1201 {
1202  delete hash;
1203 }
1204 
1206 {
1207  auto block_undo{std::make_shared<CBlockUndo>()};
1208  if (btck_BlockTreeEntry::get(entry).nHeight < 1) {
1209  LogDebug(BCLog::KERNEL, "The genesis block does not have any spent outputs.");
1210  return btck_BlockSpentOutputs::create(block_undo);
1211  }
1212  if (!btck_ChainstateManager::get(chainman).m_chainman->m_blockman.ReadBlockUndo(*block_undo, btck_BlockTreeEntry::get(entry))) {
1213  LogError("Failed to read block spent outputs data.");
1214  return nullptr;
1215  }
1216  return btck_BlockSpentOutputs::create(block_undo);
1217 }
1218 
1220 {
1221  return btck_BlockSpentOutputs::copy(block_spent_outputs);
1222 }
1223 
1224 size_t btck_block_spent_outputs_count(const btck_BlockSpentOutputs* block_spent_outputs)
1225 {
1226  return btck_BlockSpentOutputs::get(block_spent_outputs)->vtxundo.size();
1227 }
1228 
1230 {
1231  assert(transaction_index < btck_BlockSpentOutputs::get(block_spent_outputs)->vtxundo.size());
1232  const auto* tx_undo{&btck_BlockSpentOutputs::get(block_spent_outputs)->vtxundo.at(transaction_index)};
1233  return btck_TransactionSpentOutputs::ref(tx_undo);
1234 }
1235 
1237 {
1238  delete block_spent_outputs;
1239 }
1240 
1242 {
1243  return btck_TransactionSpentOutputs::copy(transaction_spent_outputs);
1244 }
1245 
1247 {
1248  return btck_TransactionSpentOutputs::get(transaction_spent_outputs).vprevout.size();
1249 }
1250 
1252 {
1253  delete transaction_spent_outputs;
1254 }
1255 
1256 const btck_Coin* btck_transaction_spent_outputs_get_coin_at(const btck_TransactionSpentOutputs* transaction_spent_outputs, size_t coin_index)
1257 {
1258  assert(coin_index < btck_TransactionSpentOutputs::get(transaction_spent_outputs).vprevout.size());
1259  const Coin* coin{&btck_TransactionSpentOutputs::get(transaction_spent_outputs).vprevout.at(coin_index)};
1260  return btck_Coin::ref(coin);
1261 }
1262 
1264 {
1265  return btck_Coin::copy(coin);
1266 }
1267 
1269 {
1270  return btck_Coin::get(coin).nHeight;
1271 }
1272 
1274 {
1275  return btck_Coin::get(coin).IsCoinBase() ? 1 : 0;
1276 }
1277 
1279 {
1280  return btck_TransactionOutput::ref(&btck_Coin::get(coin).out);
1281 }
1282 
1284 {
1285  delete coin;
1286 }
1287 
1289  btck_ChainstateManager* chainman,
1290  const btck_Block* block,
1291  int* _new_block)
1292 {
1293  bool new_block;
1294  auto result = btck_ChainstateManager::get(chainman).m_chainman->ProcessNewBlock(btck_Block::get(block), /*force_processing=*/true, /*min_pow_checked=*/true, /*new_block=*/&new_block);
1295  if (_new_block) {
1296  *_new_block = new_block ? 1 : 0;
1297  }
1298  return result ? 0 : -1;
1299 }
1300 
1302  btck_ChainstateManager* chainstate_manager,
1303  const btck_BlockHeader* header,
1305 {
1306  try {
1307  auto& chainman = btck_ChainstateManager::get(chainstate_manager).m_chainman;
1308  auto result = chainman->ProcessNewBlockHeaders({&btck_BlockHeader::get(header), 1}, /*min_pow_checked=*/true, btck_BlockValidationState::get(state), /*ppindex=*/nullptr);
1309 
1310  return result ? 0 : -1;
1311  } catch (const std::exception& e) {
1312  LogError("Failed to process block header: %s", e.what());
1313  return -1;
1314  }
1315 }
1316 
1318 {
1319  return btck_Chain::ref(&WITH_LOCK(btck_ChainstateManager::get(chainman).m_chainman->GetMutex(), return btck_ChainstateManager::get(chainman).m_chainman->ActiveChain()));
1320 }
1321 
1323 {
1324  LOCK(::cs_main);
1325  return btck_Chain::get(chain).Height();
1326 }
1327 
1329 {
1330  LOCK(::cs_main);
1331  return btck_BlockTreeEntry::ref(btck_Chain::get(chain)[height]);
1332 }
1333 
1334 int btck_chain_contains(const btck_Chain* chain, const btck_BlockTreeEntry* entry)
1335 {
1336  LOCK(::cs_main);
1337  return btck_Chain::get(chain).Contains(&btck_BlockTreeEntry::get(entry)) ? 1 : 0;
1338 }
1339 
1340 btck_BlockHeader* btck_block_header_create(const void* raw_block_header, size_t raw_block_header_len)
1341 {
1342  if (raw_block_header == nullptr && raw_block_header_len != 0) {
1343  return nullptr;
1344  }
1345  auto header{std::make_unique<CBlockHeader>()};
1346  SpanReader stream{std::span{reinterpret_cast<const std::byte*>(raw_block_header), raw_block_header_len}};
1347 
1348  try {
1349  stream >> *header;
1350  } catch (...) {
1351  LogError("Block header decode failed.");
1352  return nullptr;
1353  }
1354 
1355  return btck_BlockHeader::ref(header.release());
1356 }
1357 
1359 {
1360  return btck_BlockHeader::copy(header);
1361 }
1362 
1364 {
1365  return btck_BlockHash::create(btck_BlockHeader::get(header).GetHash());
1366 }
1367 
1369 {
1370  return btck_BlockHash::ref(&btck_BlockHeader::get(header).hashPrevBlock);
1371 }
1372 
1374 {
1375  return btck_BlockHeader::get(header).nTime;
1376 }
1377 
1379 {
1380  return btck_BlockHeader::get(header).nBits;
1381 }
1382 
1384 {
1385  return btck_BlockHeader::get(header).nVersion;
1386 }
1387 
1389 {
1390  return btck_BlockHeader::get(header).nNonce;
1391 }
1392 
1394 {
1395  delete header;
1396 }
size_t btck_block_spent_outputs_count(const btck_BlockSpentOutputs *block_spent_outputs)
Returns the number of transaction spent outputs whose data is contained in block spent outputs...
btck_BlockSpentOutputs * btck_block_spent_outputs_copy(const btck_BlockSpentOutputs *block_spent_outputs)
Copy a block&#39;s spent outputs.
int btck_chain_contains(const btck_Chain *chain, const btck_BlockTreeEntry *entry)
static path absolute(const path &p)
Definition: fs.h:88
const btck_Chain * btck_chainstate_manager_get_active_chain(const btck_ChainstateManager *chainman)
Returns the best known currently active chain.
void EnableCategory(LogFlags flag)
Definition: logging.cpp:123
static std::unique_ptr< const CChainParams > TestNet4()
void btck_transaction_spent_outputs_destroy(btck_TransactionSpentOutputs *transaction_spent_outputs)
Destroy the transaction spent outputs.
const TranslateFn G_TRANSLATION_FUN
Definition: bitcoin-cli.cpp:53
void(* btck_DestroyCallback)(void *user_data)
Function signature for freeing user data.
void btck_transaction_input_destroy(btck_TransactionInput *input)
Destroy the transaction input.
void btck_transaction_destroy(btck_Transaction *transaction)
Destroy the transaction.
void DisableLogging() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Disable logging This offers a slight speedup and slightly smaller memory usage compared to leaving th...
Definition: logging.cpp:111
virtual void warningUnset(Warning id)
fs::path path
Location in the filesystem where leveldb data will be stored.
Definition: dbwrapper.h:35
int btck_transaction_to_bytes(const btck_Transaction *transaction, btck_WriteBytes writer, void *user_data)
btck_PrecomputedTransactionData * btck_precomputed_transaction_data_create(const btck_Transaction *tx_to, const btck_TransactionOutput **spent_outputs_, size_t spent_outputs_len)
Create precomputed transaction data for script verification.
int btck_chain_get_height(const btck_Chain *chain)
Return the height of the tip of the chain.
int(* btck_WriteBytes)(const void *bytes, size_t size, void *userdata)
Function signature for serializing data.
btck_Transaction * btck_transaction_copy(const btck_Transaction *transaction)
Copy a transaction.
void btck_context_options_set_chainparams(btck_ContextOptions *options, const btck_ChainParameters *chain_parameters)
uint32_t btck_ScriptVerificationFlags
Script verification flags that may be composed with each other.
const btck_BlockTreeEntry * btck_chainstate_manager_get_block_tree_entry_by_hash(const btck_ChainstateManager *chainman, const btck_BlockHash *block_hash)
SynchronizationState
Current sync state passed to tip changed callbacks.
Definition: validation.h:93
BCLog::Logger & LogInstance()
Definition: logging.cpp:26
void btck_logging_enable_category(btck_LogCategory category)
Enable a specific log category for the global internal logger.
btck_PrecomputedTransactionData * btck_precomputed_transaction_data_copy(const btck_PrecomputedTransactionData *precomputed_txdata)
Copy precomputed transaction data.
btck_Block * btck_block_read(const btck_ChainstateManager *chainman, const btck_BlockTreeEntry *entry)
#define btck_ValidationMode_INTERNAL_ERROR
assert(!tx.IsCoinBase())
const btck_BlockTreeEntry * btck_chain_get_by_height(const btck_Chain *chain, int height)
Retrieve a block tree entry by its height in the currently active chain.
btck_BlockHeader * btck_block_header_create(const void *raw_block_header, size_t raw_block_header_len)
Create a btck_BlockHeader from serialized data.
virtual void BlockDisconnected(const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being disconnected Provides the block that was disconnected.
const btck_TransactionInput * btck_transaction_get_input_at(const btck_Transaction *transaction, size_t input_index)
Get the transaction input at the provided index.
int btck_chainstate_manager_process_block(btck_ChainstateManager *chainman, const btck_Block *block, int *_new_block)
void btck_logging_disable()
This disables the global internal logger.
#define btck_ChainType_SIGNET
A UTXO entry.
Definition: coins.h:34
Bilingual messages:
Definition: translation.h:24
#define btck_ScriptVerifyStatus_OK
Level
Definition: log.h:41
ChainstateLoadResult VerifyLoadedChainstate(ChainstateManager &chainman, const ChainstateLoadOptions &options)
Definition: chainstate.cpp:240
We don&#39;t have the previous block the checked one is built on.
void * user_data
Holds a user-defined opaque structure that is passed to the validation interface callbacks.
uint32_t btck_block_header_get_nonce(const btck_BlockHeader *header)
Get the nonce from btck_BlockHeader.
const btck_TransactionOutput * btck_transaction_get_output_at(const btck_Transaction *transaction, size_t output_index)
Get the transaction outputs at the provided index.
#define btck_SynchronizationState_POST_INIT
const btck_BlockTreeEntry * btck_block_tree_entry_get_previous(const btck_BlockTreeEntry *entry)
Returns the previous block tree entry in the tree, or null if the current block tree entry is the gen...
btck_Coin * btck_coin_copy(const btck_Coin *coin)
Copy a coin.
invalid proof of work or time too old
void btck_context_options_set_validation_interface(btck_ContextOptions *options, btck_ValidationInterfaceCallbacks vi_cbs)
Set the validation interface callbacks for the context options.
void btck_logging_connection_destroy(btck_LoggingConnection *connection)
Stop logging and destroy the logging connection.
btck_ValidationInterfaceBlockConnected block_connected
Called when a block is valid and has now been connected to the best chain.
Information about chainstate that notifications are sent from.
Definition: types.h:18
virtual void NewPoWValidBlock(const CBlockIndex *pindex, const std::shared_ptr< const CBlock > &block)
Notifies listeners that a block which builds directly on our current tip has been received and connec...
std::variant< std::monostate, Interrupted > InterruptResult
Simple result type for functions that need to propagate an interrupt status and don&#39;t have other retu...
btck_BlockHash * btck_block_hash_create(const unsigned char block_hash[32])
Create a block hash from its raw data.
btck_ValidationInterfaceBlockChecked block_checked
Called when a new block has been fully validated.
constexpr deserialize_type deserialize
Definition: serialize.h:49
virtual void progress(const bilingual_str &title, int progress_percent, bool resume_possible)
value Serialize(wrapper)
void btck_chainstate_manager_options_update_chainstate_db_in_memory(btck_ChainstateManagerOptions *chainman_opts, int chainstate_db_in_memory)
Sets chainstate db in memory in the options.
size_t NumConnections()
Definition: logging.h:214
int btck_block_to_bytes(const btck_Block *block, btck_WriteBytes writer, void *user_data)
An options struct for ChainstateManager, more ergonomically referred to as ChainstateManager::Options...
#define btck_ChainType_TESTNET_4
void btck_transaction_out_point_destroy(btck_TransactionOutPoint *out_point)
Destroy the transaction out point.
#define btck_ChainType_TESTNET
#define btck_BlockValidationResult_CACHED_INVALID
this block was cached as being invalid and we didn&#39;t store the reason why
#define btck_Warning_UNKNOWN_NEW_RULES_ACTIVATED
#define btck_LogCategory_RAND
btck_Block * btck_block_create(const void *raw_block, size_t raw_block_length)
Parse a serialized raw block into a new block object.
#define btck_Warning_LARGE_WORK_INVALID_CHAIN
Definition: common.h:29
btck_BlockHeader * btck_block_tree_entry_get_block_header(const btck_BlockTreeEntry *entry)
Return the btck_BlockHeader associated with this entry.
void btck_logging_disable_category(btck_LogCategory category)
Disable a specific log category for the global internal logger.
void btck_logging_set_level_category(btck_LogCategory category, btck_LogLevel level)
Set the log level of the global internal logger.
the block header may be on a too-little-work chain
#define btck_LogCategory_BENCH
util::Result< void > SanityChecks(const Context &)
Ensure a usable environment with all necessary library support.
Definition: checks.cpp:15
btck_BlockHeader * btck_block_header_copy(const btck_BlockHeader *header)
Copy a btck_BlockHeader.
memcpy(result.begin(), stream.data(), stream.size())
const btck_BlockHash * btck_block_tree_entry_get_block_hash(const btck_BlockTreeEntry *entry)
Return the block hash associated with a block tree entry.
LogFlags
Definition: categories.h:14
int btck_script_pubkey_verify(const btck_ScriptPubkey *script_pubkey, const int64_t amount, const btck_Transaction *tx_to, const btck_PrecomputedTransactionData *precomputed_txdata, const unsigned int input_index, const btck_ScriptVerificationFlags flags, btck_ScriptVerifyStatus *status)
#define btck_ValidationMode_VALID
const btck_Transaction * btck_block_get_transaction_at(const btck_Block *block, size_t index)
Get the transaction at the provided index.
btck_BlockHash * btck_block_header_get_hash(const btck_BlockHeader *header)
Get the btck_BlockHash.
#define btck_ChainType_REGTEST
uint8_t btck_Warning
Possible warning types issued by validation.
uint32_t btck_coin_confirmation_height(const btck_Coin *coin)
Returns the block height where the transaction that created this coin was included in...
Context m_context
Definition: protocol.cpp:141
bool m_log_sourcelocations
Definition: logging.h:182
#define btck_ScriptVerificationFlags_ALL
size_t btck_block_count_transactions(const btck_Block *block)
Count the number of transactions contained in a block.
void ImportBlocks(ChainstateManager &chainman, std::span< const fs::path > import_paths)
Warning
Definition: warning.h:9
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, script_verify_flags flags, const BaseSignatureChecker &checker, ScriptError *serror)
int log_sourcelocations
Prepend the source location to log messages.
std::ostream & operator<<(std::ostream &os, BigO const &bigO)
Implement this to subscribe to events generated in validation and mempool.
void btck_txid_to_bytes(const btck_Txid *txid, unsigned char output[32])
#define btck_LogCategory_COINDB
static std::unique_ptr< const CChainParams > Main()
virtual void fatalError(const bilingual_str &message)
The fatal error notification is sent to notify the user when an error occurs in kernel code that can&#39;...
void AddCategoryLogLevel(LogFlags category, Level level)
Definition: logging.h:251
static std::unique_ptr< const CChainParams > RegTest(const RegTestOptions &options)
uint8_t btck_ChainType
#define btck_ScriptVerifyStatus_ERROR_INVALID_FLAGS_COMBINATION
The flags were combined in an invalid way.
void btck_block_validation_state_destroy(btck_BlockValidationState *state)
Destroy the btck_BlockValidationState.
btck_TransactionInput * btck_transaction_input_copy(const btck_TransactionInput *input)
Copy a transaction input.
btck_TransactionOutPoint * btck_transaction_out_point_copy(const btck_TransactionOutPoint *out_point)
Copy a transaction out point.
void btck_chainstate_manager_destroy(btck_ChainstateManager *chainman)
Destroy the chainstate manager.
uint8_t btck_LogLevel
The level at which logs should be produced.
#define btck_BlockValidationResult_UNSET
initial value. Block has not yet been rejected
Minimal stream for reading from an existing byte array by std::span.
Definition: streams.h:82
int btck_chainstate_manager_import_blocks(btck_ChainstateManager *chainman, const char **block_file_paths_data, size_t *block_file_paths_lens, size_t block_file_paths_data_len)
Triggers the start of a reindex if the wipe options were previously set for the chainstate manager...
void * user_data
Holds a user-defined opaque structure that is passed to the notification callbacks.
#define btck_BlockValidationResult_CONSENSUS
invalid by consensus rules (excluding any below reasons)
const btck_BlockTreeEntry * btck_chainstate_manager_get_best_entry(const btck_ChainstateManager *chainstate_manager)
Get the btck_BlockTreeEntry whose associated btck_BlockHeader has the most known cumulative proof of ...
int log_time_micros
Log timestamps in microsecond precision.
btck_ChainstateManager * btck_chainstate_manager_create(const btck_ChainstateManagerOptions *chainman_opts)
Create a chainstate manager.
btck_BlockHash * btck_block_get_hash(const btck_Block *block)
Calculate and return the hash of a block.
uint32_t btck_block_header_get_bits(const btck_BlockHeader *header)
Get the nBits difficulty target from btck_BlockHeader.
#define btck_LogCategory_BLOCKSTORAGE
btck_ChainParameters * btck_chain_parameters_copy(const btck_ChainParameters *chain_parameters)
Copy the chain parameters.
initial value. Block has not yet been rejected
const btck_TransactionOutPoint * btck_transaction_input_get_out_point(const btck_TransactionInput *input)
Get the transaction out point.
virtual void flushError(const bilingual_str &message)
The flush error notification is sent to notify the user that an error occurred while flushing block d...
An options struct for BlockManager, more ergonomically referred to as BlockManager::Options due to th...
int btck_context_interrupt(btck_Context *context)
Interrupt can be used to halt long-running validation functions like when reindexing, importing or processing blocks.
virtual void headerTip(SynchronizationState state, int64_t height, int64_t timestamp, bool presync)
ArgsManager & args
Definition: bitcoind.cpp:277
#define btck_LogCategory_ALL
int always_print_category_levels
Prepend the log category and level to log messages.
bool m_log_threadnames
Definition: logging.h:181
int log_timestamps
Prepend a timestamp to log messages.
NumConnections
Definition: clientmodel.h:48
#define btck_LogLevel_DEBUG
btck_NotifyWarningUnset warning_unset
A previous condition leading to the issuance of a warning is no longer given.
btck_ContextOptions * btck_context_options_create()
Creates an empty context options.
int btck_script_pubkey_to_bytes(const btck_ScriptPubkey *script_pubkey_, btck_WriteBytes writer, void *user_data)
bool m_always_print_category_level
Definition: logging.h:183
this block was cached as being invalid and we didn&#39;t store the reason why
bool m_log_time_micros
Definition: logging.h:180
#define LOCK(cs)
Definition: sync.h:258
boost::signals2::scoped_connection m_connection
Definition: interfaces.cpp:30
#define LogInfo(...)
Definition: log.h:95
btck_BlockValidationResult btck_block_validation_state_get_block_validation_result(const btck_BlockValidationState *block_validation_state_)
Returns the validation result from an opaque btck_BlockValidationState pointer.
void btck_block_hash_destroy(btck_BlockHash *hash)
Destroy the block hash.
void btck_block_header_destroy(btck_BlockHeader *header)
Destroy the btck_BlockHeader.
void btck_chainstate_manager_options_set_worker_threads_num(btck_ChainstateManagerOptions *opts, int worker_threads)
Set the number of available worker threads used during validation.
virtual InterruptResult blockTip(SynchronizationState state, const CBlockIndex &index, double verification_progress)
std::function< std::string(const char *)> TranslateFn
Translate a message to the native language of the user.
Definition: translation.h:16
#define btck_LogCategory_KERNEL
btck_BlockValidationState * btck_block_validation_state_copy(const btck_BlockValidationState *state)
Copies the btck_BlockValidationState.
#define btck_LogCategory_MEMPOOL
#define btck_SynchronizationState_INIT_DOWNLOAD
btck_DestroyCallback user_data_destroy
Frees the provided user data structure.
const std::vector< CTxOut > vout
Definition: transaction.h:292
void btck_block_destroy(btck_Block *block)
Destroy the block.
const btck_Coin * btck_transaction_spent_outputs_get_coin_at(const btck_TransactionSpentOutputs *transaction_spent_outputs, size_t coin_index)
Returns a coin contained in the transaction spent outputs at a certain index.
Just act as if the signature was invalid.
void DisableCategory(LogFlags flag)
Definition: logging.cpp:136
int32_t btck_block_header_get_version(const btck_BlockHeader *header)
Get the version from btck_BlockHeader.
btck_BlockHash * btck_block_hash_copy(const btck_BlockHash *block_hash)
Copy a block hash.
const CChainParams & chainparams
virtual void BlockConnected(const kernel::ChainstateRole &role, const std::shared_ptr< const CBlock > &block, const CBlockIndex *pindex)
Notifies listeners of a block being connected.
An output of a transaction.
Definition: transaction.h:139
uint8_t btck_LogCategory
A collection of logging categories that may be encountered by kernel code.
btck_NotifyFatalError fatal_error
An unrecoverable system error encountered by the library.
virtual void warningSet(Warning id, const bilingual_str &message)
static constexpr size_t DEFAULT_KERNEL_CACHE
Suggested default amount of cache reserved for the kernel (bytes)
Definition: caches.h:13
void btck_context_destroy(btck_Context *context)
Destroy the context.
void btck_block_spent_outputs_destroy(btck_BlockSpentOutputs *block_spent_outputs)
Destroy the block spent outputs.
void SetLogLevel(Level level)
Definition: logging.h:259
#define btck_BlockValidationResult_TIME_FUTURE
block timestamp was > 2 hours in the future (or our clock is bad)
btck_ScriptPubkey * btck_script_pubkey_copy(const btck_ScriptPubkey *script_pubkey)
Copy a script pubkey.
A base class defining functions for notifying about certain kernel events.
void DeleteCallback(std::list< std::function< void(const std::string &)>>::iterator it) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Delete a connection.
Definition: logging.h:208
uint32_t btck_BlockValidationResult
A granular "reason" why a block was invalid.
#define WITH_LOCK(cs, code)
Run code while locking a mutex.
Definition: sync.h:289
btck_BlockHeader * btck_block_get_header(const btck_Block *block)
Get the btck_BlockHeader from the block.
#define btck_BlockValidationResult_INVALID_PREV
A block this one builds on is invalid.
btck_LoggingConnection * btck_logging_connection_create(btck_LogCallback callback, void *user_data, btck_DestroyCallback user_data_destroy_callback)
Start logging messages through the provided callback.
void DisconnectTestLogger() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Only for testing.
Definition: logging.cpp:98
btck_BlockSpentOutputs * btck_block_spent_outputs_read(const btck_ChainstateManager *chainman, const btck_BlockTreeEntry *entry)
btck_NotifyWarningSet warning_set
A warning issued by the kernel library during validation.
const btck_ScriptPubkey * btck_transaction_output_get_script_pubkey(const btck_TransactionOutput *output)
Get the script pubkey of the output.
This header provides an interface and simple implementation for a task runner.
int btck_chainstate_manager_options_set_wipe_dbs(btck_ChainstateManagerOptions *chainman_opts, int wipe_block_tree_db, int wipe_chainstate_db)
Sets wipe db in the options.
void btck_chainstate_manager_options_update_block_tree_db_in_memory(btck_ChainstateManagerOptions *chainman_opts, int block_tree_db_in_memory)
Sets block tree db in memory in the options.
#define btck_BlockValidationResult_MISSING_PREV
We don&#39;t have the previous block the checked one is built on.
btck_TransactionOutput * btck_transaction_output_copy(const btck_TransactionOutput *output)
Copy a transaction output.
void btck_transaction_output_destroy(btck_TransactionOutput *output)
Destroy the transaction output.
int flags
Definition: bitcoin-tx.cpp:529
btck_NotifyBlockTip block_tip
The chain&#39;s tip was updated to the provided block entry.
btck_ValidationInterfacePoWValidBlock pow_valid_block
Called when a new block extends the header chain and has a valid transaction and segwit merkle root...
btck_Context * btck_context_copy(const btck_Context *context)
Copy the context.
btck_Block * btck_block_copy(const btck_Block *block)
Copy a block.
#define LogDebug(category,...)
Definition: log.h:115
invalid by consensus rules (excluding any below reasons)
const btck_TransactionOutput * btck_coin_get_output(const btck_Coin *coin)
Return the transaction output of a coin.
uint8_t btck_ValidationMode
Whether a validated data structure is valid, invalid, or an error was encountered during processing...
void btck_precomputed_transaction_data_destroy(btck_PrecomputedTransactionData *precomputed_txdata)
Destroy the precomputed transaction data.
int btck_coin_is_coinbase(const btck_Coin *coin)
Returns whether the containing transaction was a coinbase.
void btck_block_hash_to_bytes(const btck_BlockHash *block_hash, unsigned char output[32])
void btck_context_options_destroy(btck_ContextOptions *options)
Destroy the context options.
the block&#39;s data didn&#39;t match the data committed to by the PoW
btck_ChainParameters * btck_chain_parameters_create(const btck_ChainType chain_type)
Creates a chain parameters struct with default parameters based on the passed in chain type...
auto result
Definition: common-types.h:74
static constexpr script_verify_flags from_int(value_type f)
Definition: verify_flags.h:35
void RegisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Register subscriber.
btck_ScriptPubkey * btck_script_pubkey_create(const void *script_pubkey, size_t script_pubkey_len)
Create a script pubkey from serialized data.
static std::unique_ptr< const CChainParams > SigNet(const SigNetOptions &options)
std::string original
Definition: translation.h:25
btck_ValidationMode btck_block_validation_state_get_validation_mode(const btck_BlockValidationState *block_validation_state_)
Returns the validation mode from an opaque btck_BlockValidationState pointer.
std::list< std::function< void(const std::string &)> >::iterator PushBackCallback(std::function< void(const std::string &)> fun) EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Connect a slot to the print signal and return the connection.
Definition: logging.h:200
static std::unique_ptr< const CChainParams > TestNet()
The block chain is a tree shaped structure starting with the genesis block at the root...
Definition: chain.h:93
btck_DestroyCallback user_data_destroy
Frees the provided user data structure.
size_t btck_transaction_spent_outputs_count(const btck_TransactionSpentOutputs *transaction_spent_outputs)
Returns the number of previous transaction outputs contained in the transaction spent outputs data...
btck_TransactionOutput * btck_transaction_output_create(const btck_ScriptPubkey *script_pubkey, int64_t amount)
Create a transaction output from a script pubkey and an amount.
A block this one builds on is invalid.
btck_ChainstateManagerOptions * btck_chainstate_manager_options_create(const btck_Context *context, const char *data_dir, size_t data_dir_len, const char *blocks_dir, size_t blocks_dir_len)
Create options for the chainstate manager.
btck_Context * btck_context_create(const btck_ContextOptions *options)
Create a new kernel context.
btck_NotifyHeaderTip header_tip
A new best block header was added.
#define btck_LogCategory_REINDEX
uint8_t btck_ScriptVerifyStatus
A collection of status codes that may be issued by the script verify function.
A struct for holding the kernel notification callbacks.
void btck_chainstate_manager_options_destroy(btck_ChainstateManagerOptions *options)
Destroy the chainstate manager options.
Context struct holding the kernel library&#39;s logically global state, and passed to external libbitcoin...
Definition: context.h:16
int64_t btck_transaction_output_get_amount(const btck_TransactionOutput *output)
Get the amount in the output.
btck_Txid * btck_txid_copy(const btck_Txid *txid)
Copy a txid.
Application-specific storage settings.
Definition: dbwrapper.h:33
uint32_t btck_block_header_get_timestamp(const btck_BlockHeader *header)
Get the timestamp from btck_BlockHeader.
btck_TransactionSpentOutputs * btck_transaction_spent_outputs_copy(const btck_TransactionSpentOutputs *transaction_spent_outputs)
Copy a transaction&#39;s spent outputs.
const btck_TransactionSpentOutputs * btck_block_spent_outputs_get_transaction_spent_outputs_at(const btck_BlockSpentOutputs *block_spent_outputs, size_t transaction_index)
Returns a transaction spent outputs contained in the block spent outputs at a certain index...
size_t btck_transaction_count_inputs(const btck_Transaction *transaction)
Get the number of inputs of a transaction.
int btck_chainstate_manager_process_block_header(btck_ChainstateManager *chainstate_manager, const btck_BlockHeader *header, btck_BlockValidationState *state)
bool StartLogging() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
Start logging (and flush all buffered messages)
Definition: logging.cpp:54
#define btck_LogCategory_VALIDATION
#define btck_ValidationMode_INVALID
bilingual_str ErrorString(const Result< T > &result)
Definition: result.h:93
#define GUARDED_BY(x)
Definition: threadsafety.h:39
#define btck_LogCategory_PRUNE
btck_NotifyFlushError flush_error
An error encountered when flushing data to disk.
void btck_txid_destroy(btck_Txid *txid)
Destroy the txid.
virtual void BlockChecked(const std::shared_ptr< const CBlock > &, const BlockValidationState &)
Notifies listeners of a block validation result.
#define btck_ChainType_MAINNET
Holds the validation interface callbacks.
block timestamp was > 2 hours in the future (or our clock is bad)
Options controlling the format of log messages.
bool m_log_timestamps
Definition: logging.h:179
size_t btck_transaction_count_outputs(const btck_Transaction *transaction)
Get the number of outputs of a transaction.
#define btck_SynchronizationState_INIT_REINDEX
btck_NotifyProgress progress
Reports on current block synchronization progress.
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:280
void btck_coin_destroy(btck_Coin *coin)
Destroy the coin.
ChainstateLoadResult LoadChainstate(ChainstateManager &chainman, const CacheSizes &cache_sizes, const ChainstateLoadOptions &options)
Definition: chainstate.cpp:151
void btck_context_options_set_notifications(btck_ContextOptions *options, btck_NotificationInterfaceCallbacks notifications)
Set the kernel notifications for the context options.
#define btck_LogLevel_INFO
static const kernel::Context btck_context_static
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition: fs.h:180
#define btck_BlockValidationResult_HEADER_LOW_WORK
the block header may be on a too-little-work chain
int32_t btck_block_tree_entry_get_height(const btck_BlockTreeEntry *entry)
Return the height of a certain block tree entry.
void btck_logging_set_options(const btck_LoggingOptions options)
Set some options for the global internal logger.
int btck_block_hash_equals(const btck_BlockHash *hash1, const btck_BlockHash *hash2)
ValidationSignals & m_signals
Definition: interfaces.cpp:502
uint8_t btck_SynchronizationState
Current sync state passed to tip changed callbacks.
#define btck_ScriptVerificationFlags_TAPROOT
enable TAPROOT (BIPs 341 & 342)
btck_ValidationInterfaceBlockDisconnected block_disconnected
Called during a re-org when a block has been removed from the best chain.
const btck_Txid * btck_transaction_out_point_get_txid(const btck_TransactionOutPoint *out_point)
Get the txid from the transaction out point.
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Definition: fs.h:33
void btck_chain_parameters_destroy(btck_ChainParameters *chain_parameters)
Destroy the chain parameters.
RecursiveMutex cs_main
Mutex to guard access to validation specific variables, such as reading or changing the chainstate...
Definition: cs_main.cpp:8
btck_BlockValidationState * btck_block_validation_state_create()
Create a new btck_BlockValidationState.
#define btck_BlockValidationResult_INVALID_HEADER
invalid proof of work or time too old
#define btck_LogCategory_LEVELDB
const btck_Txid * btck_transaction_get_txid(const btck_Transaction *transaction)
Get the txid of a transaction.
int btck_block_tree_entry_equals(const btck_BlockTreeEntry *entry1, const btck_BlockTreeEntry *entry2)
uint32_t btck_transaction_out_point_get_index(const btck_TransactionOutPoint *out_point)
Get the output position from the transaction out point.
void UnregisterSharedValidationInterface(std::shared_ptr< CValidationInterface > callbacks)
Unregister subscriber.
#define btck_ScriptVerifyStatus_ERROR_SPENT_OUTPUTS_REQUIRED
The taproot flag was set, so valid spent_outputs have to be provided.
#define btck_BlockValidationResult_MUTATED
the block&#39;s data didn&#39;t match the data committed to by the PoW
btck_Transaction * btck_transaction_create(const void *raw_transaction, size_t raw_transaction_len)
Create a new transaction from the serialized data.
std::shared_ptr< Chain::Notifications > m_notifications
Definition: interfaces.cpp:483
#define LogError(...)
Definition: log.h:97
int log_threadnames
Prepend the name of the thread to log messages.
static constexpr TransactionSerParams TX_WITH_WITNESS
Definition: transaction.h:180
const btck_BlockHash * btck_block_header_get_prev_hash(const btck_BlockHeader *header)
Get the previous btck_BlockHash from btck_BlockHeader.
void(* btck_LogCallback)(void *user_data, const char *message, size_t message_len)
Callback function types.
void btck_script_pubkey_destroy(btck_ScriptPubkey *script_pubkey)
Destroy the script pubkey.
int btck_txid_equals(const btck_Txid *txid1, const btck_Txid *txid2)
#define btck_LogLevel_TRACE