Skip to content

Commit da985d5

Browse files
Squashed 'libbitcoinkernel-sys/bitcoin/' changes from 2ac9d60c54a7..64d1449ff47d
64d1449ff47d kernel: Fix bitcoin-chainstate for windows ba247a6a7183 kernel: Add Purpose section to header documentation b0697ccbf7fd kernel: Allowing reducing exports 9d23c437bfc8 kernel: Add pure kernel bitcoin-chainstate 10e8e06caf15 Kernel: Add functions for working with outpoints ae64f8984d98 kernel: Add functions to get the block hash from a block 2c2f277d12fa kernel: Add block hash type and block tree utility functions to C header 6062e2eeca3c kernel: Add function to read block undo data from disk to C header e96af0baf5ef kernel: Add functions to read block from disk to C header 60e1515586a8 kernel: Add function for copying block data to C header 7b105b7d02a0 kernel: Add functions for the block validation state to C header 1a58dbb81583 kernel: Add validation interface to C header 86ed87d5a6c4 kernel: Add interrupt function to C header e424c4554653 kernel: Add import blocks function to C header cf7b562f2e31 kernel: Add chainstate load options for in-memory dbs in C header e26a198acfbb kernel: Add options for reindexing in C header 4196583e03d6 kernel: Add block validation to C header a7149076e233 kernel: Add chainstate loading when instantiating a ChainstateManager d36b447e6caf kernel: Add chainstate manager option for setting worker threads d4e612af6701 kernel: Add chainstate manager object to C header f19c80767460 kernel: Add notifications context option to C header cc0043d99b62 kernel: Add chain params context option to C header 8b3ceea4f1df kernel: Add kernel library context object 039e222756ef kernel: Add logging to kernel library C header d192006d7f4e kernel: Introduce initial kernel C header API REVERT: 2ac9d60c54a7 kernel: Fix bitcoin-chainstate for windows REVERT: 08e2a7ebbb97 kernel: Add Purpose section to header documentation REVERT: 9d95715fe46d kernel: Allowing reducing exports REVERT: 241f306df615 kernel: Add pure kernel bitcoin-chainstate REVERT: 3ed3d6b9c25c kernel: Add functions to get the block hash from a block REVERT: 3fe143dc6c36 kernel: Add block index utility functions to C header REVERT: 224dbec1ce53 kernel: Add function to read block undo data from disk to C header REVERT: 4e5b9c66d367 kernel: Add functions to read block from disk to C header REVERT: e4ef0011f7ef kernel: Add function for copying block data to C header REVERT: 3e7711d271d7 kernel: Add functions for the block validation state to C header REVERT: 984305a1afb2 kernel: Add validation interface to C header REVERT: ff1fe96997bf kernel: Add interrupt function to C header REVERT: b45d0abbea2e kernel: Add import blocks function to C header REVERT: dc939ae9471f kernel: Add chainstate load options for in-memory dbs in C header REVERT: 0ea93d862429 kernel: Add options for reindexing in C header REVERT: 5817e2e79bda kernel: Add block validation to C header REVERT: 32d9e4a547b0 kernel: Add chainstate loading when instantiating a ChainstateManager REVERT: e3c03bae40bf kernel: Add chainstate manager option for setting worker threads REVERT: f6978da0f731 kernel: Add chainstate manager object to C header REVERT: 63d6e4fade23 kernel: Add notifications context option to C header REVERT: ee9c2c7ceaca kernel: Add chain params context option to C header REVERT: 2919e083e1bb kernel: Add kernel library context object REVERT: 7967ffa7476d kernel: Add logging to kernel library C header REVERT: c76dcaafbc7b kernel: Introduce initial kernel C header API git-subtree-dir: libbitcoinkernel-sys/bitcoin git-subtree-split: 64d1449ff47dcb2d51a25f4178845538061c8644
1 parent 42aa9a7 commit da985d5

File tree

4 files changed

+737
-105
lines changed

4 files changed

+737
-105
lines changed

src/kernel/bitcoinkernel.cpp

Lines changed: 140 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,41 @@ static const kernel::Context btck_context_static{};
6161

6262
namespace {
6363

64+
bool is_valid_flag_combination(unsigned int 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(std::data(src), src.size(), m_user_data) != 0) {
87+
throw std::runtime_error("Failed to write serilization 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+
6499
template <typename C, typename CPP>
65100
struct Handle {
66101
static C* ref(CPP* cpp_type)
@@ -110,41 +145,6 @@ struct btck_BlockValidationState : Handle<btck_BlockValidationState, BlockValida
110145

111146
namespace {
112147

113-
bool is_valid_flag_combination(unsigned int flags)
114-
{
115-
if (flags & SCRIPT_VERIFY_CLEANSTACK && ~flags & (SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_WITNESS)) return false;
116-
if (flags & SCRIPT_VERIFY_WITNESS && ~flags & SCRIPT_VERIFY_P2SH) return false;
117-
return true;
118-
}
119-
120-
class WriterStream
121-
{
122-
private:
123-
btck_WriteBytes m_writer;
124-
void* m_user_data;
125-
126-
public:
127-
WriterStream(btck_WriteBytes writer, void* user_data)
128-
: m_writer{writer}, m_user_data{user_data} {}
129-
130-
//
131-
// Stream subset
132-
//
133-
void write(std::span<const std::byte> src)
134-
{
135-
if (m_writer(std::data(src), src.size(), m_user_data) != 0) {
136-
throw std::runtime_error("Failed to write serilization data");
137-
}
138-
}
139-
140-
template <typename T>
141-
WriterStream& operator<<(const T& obj)
142-
{
143-
::Serialize(*this, obj);
144-
return *this;
145-
}
146-
};
147-
148148
BCLog::Level get_bclog_level(btck_LogLevel level)
149149
{
150150
switch (level) {
@@ -240,7 +240,7 @@ struct LoggingConnection {
240240
LogInstance().m_log_sourcelocations = options.log_sourcelocations;
241241
LogInstance().m_always_print_category_level = options.always_print_category_levels;
242242

243-
auto connection{LogInstance().PushBackCallback([callback, user_data](const std::string& str) { callback((void*)user_data, str.c_str(), str.length()); })};
243+
auto connection{LogInstance().PushBackCallback([callback, user_data](const std::string& str) { callback(user_data, str.c_str(), str.length()); })};
244244

245245
try {
246246
// Only start logging if we just added the connection.
@@ -353,11 +353,38 @@ class KernelValidationInterface final : public CValidationInterface
353353
void BlockChecked(const std::shared_ptr<const CBlock>& block, const BlockValidationState& stateIn) override
354354
{
355355
if (m_cbs.block_checked) {
356-
m_cbs.block_checked((void*)m_cbs.user_data,
356+
m_cbs.block_checked(m_cbs.user_data,
357357
btck_Block::ref(new std::shared_ptr<const CBlock>{block}),
358358
btck_BlockValidationState::ref(&stateIn));
359359
}
360360
}
361+
362+
void NewPoWValidBlock(const CBlockIndex* pindex, const std::shared_ptr<const CBlock>& block) override
363+
{
364+
if (m_cbs.pow_valid_block) {
365+
m_cbs.pow_valid_block(m_cbs.user_data,
366+
btck_BlockTreeEntry::ref(pindex),
367+
btck_Block::ref(new std::shared_ptr<const CBlock>{block}));
368+
}
369+
}
370+
371+
void BlockConnected(ChainstateRole role, const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override
372+
{
373+
if (m_cbs.block_connected) {
374+
m_cbs.block_connected(m_cbs.user_data,
375+
btck_Block::ref(new std::shared_ptr<const CBlock>{block}),
376+
btck_BlockTreeEntry::ref(pindex));
377+
}
378+
}
379+
380+
void BlockDisconnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex) override
381+
{
382+
if (m_cbs.block_disconnected) {
383+
m_cbs.block_disconnected(m_cbs.user_data,
384+
btck_Block::ref(new std::shared_ptr<const CBlock>{block}),
385+
btck_BlockTreeEntry::ref(pindex));
386+
}
387+
}
361388
};
362389

363390
struct ContextOptions {
@@ -471,6 +498,9 @@ struct btck_BlockSpentOutputs : Handle<btck_BlockSpentOutputs, std::shared_ptr<C
471498
struct btck_TransactionSpentOutputs : Handle<btck_TransactionSpentOutputs, CTxUndo> {};
472499
struct btck_Coin : Handle<btck_Coin, Coin> {};
473500
struct btck_BlockHash : Handle<btck_BlockHash, uint256> {};
501+
struct btck_TransactionInput : Handle<btck_TransactionInput, CTxIn> {};
502+
struct btck_TransactionOutPoint: Handle<btck_TransactionOutPoint, COutPoint> {};
503+
struct btck_Txid: Handle<btck_Txid, Txid> {};
474504

475505
btck_Transaction* btck_transaction_create(const void* raw_transaction, size_t raw_transaction_len)
476506
{
@@ -498,6 +528,17 @@ size_t btck_transaction_count_inputs(const btck_Transaction* transaction)
498528
return btck_Transaction::get(transaction)->vin.size();
499529
}
500530

531+
const btck_TransactionInput* btck_transaction_get_input_at(const btck_Transaction* transaction, size_t input_index)
532+
{
533+
assert(input_index < btck_Transaction::get(transaction)->vin.size());
534+
return btck_TransactionInput::ref(&btck_Transaction::get(transaction)->vin[input_index]);
535+
}
536+
537+
const btck_Txid* btck_transaction_get_txid(const btck_Transaction* transaction)
538+
{
539+
return btck_Txid::ref(&btck_Transaction::get(transaction)->GetHash());
540+
}
541+
501542
btck_Transaction* btck_transaction_copy(const btck_Transaction* transaction)
502543
{
503544
return btck_Transaction::copy(transaction);
@@ -617,6 +658,61 @@ int btck_script_pubkey_verify(const btck_ScriptPubkey* script_pubkey,
617658
return result ? 1 : 0;
618659
}
619660

661+
btck_TransactionInput* btck_transaction_input_copy(const btck_TransactionInput* input)
662+
{
663+
return btck_TransactionInput::copy(input);
664+
}
665+
666+
const btck_TransactionOutPoint* btck_transaction_input_get_out_point(const btck_TransactionInput* input)
667+
{
668+
return btck_TransactionOutPoint::ref(&btck_TransactionInput::get(input).prevout);
669+
}
670+
671+
void btck_transaction_input_destroy(btck_TransactionInput* input)
672+
{
673+
delete input;
674+
}
675+
676+
btck_TransactionOutPoint* btck_transaction_out_point_copy(const btck_TransactionOutPoint* out_point)
677+
{
678+
return btck_TransactionOutPoint::copy(out_point);
679+
}
680+
681+
uint32_t btck_transaction_out_point_get_index(const btck_TransactionOutPoint* out_point)
682+
{
683+
return btck_TransactionOutPoint::get(out_point).n;
684+
}
685+
686+
const btck_Txid* btck_transaction_out_point_get_txid(const btck_TransactionOutPoint* out_point)
687+
{
688+
return btck_Txid::ref(&btck_TransactionOutPoint::get(out_point).hash);
689+
}
690+
691+
void btck_transaction_out_point_destroy(btck_TransactionOutPoint* out_point)
692+
{
693+
delete out_point;
694+
}
695+
696+
btck_Txid* btck_txid_copy(const btck_Txid* txid)
697+
{
698+
return btck_Txid::copy(txid);
699+
}
700+
701+
void btck_txid_to_bytes(const btck_Txid* txid, unsigned char output[32])
702+
{
703+
std::memcpy(output, btck_Txid::get(txid).begin(), 32);
704+
}
705+
706+
int btck_txid_equals(const btck_Txid* txid1, const btck_Txid* txid2)
707+
{
708+
return btck_Txid::get(txid1) == btck_Txid::get(txid2);
709+
}
710+
711+
void btck_txid_destroy(btck_Txid* txid)
712+
{
713+
delete txid;
714+
}
715+
620716
void btck_logging_set_level_category(btck_LogCategory category, btck_LogLevel level)
621717
{
622718
LOCK(cs_main);
@@ -1024,18 +1120,23 @@ void btck_block_hash_to_bytes(const btck_BlockHash* block_hash, unsigned char ou
10241120
std::memcpy(output, btck_BlockHash::get(block_hash).begin(), 32);
10251121
}
10261122

1123+
int btck_block_hash_equals(const btck_BlockHash* hash1, const btck_BlockHash* hash2)
1124+
{
1125+
return btck_BlockHash::get(hash1) == btck_BlockHash::get(hash2);
1126+
}
1127+
10271128
void btck_block_hash_destroy(btck_BlockHash* hash)
10281129
{
10291130
delete hash;
10301131
}
10311132

10321133
btck_BlockSpentOutputs* btck_block_spent_outputs_read(const btck_ChainstateManager* chainman, const btck_BlockTreeEntry* entry)
10331134
{
1135+
auto block_undo{std::make_shared<CBlockUndo>()};
10341136
if (btck_BlockTreeEntry::get(entry).nHeight < 1) {
10351137
LogDebug(BCLog::KERNEL, "The genesis block does not have any spent outputs.");
1036-
return nullptr;
1138+
return btck_BlockSpentOutputs::create(block_undo);
10371139
}
1038-
auto block_undo{std::make_shared<CBlockUndo>()};
10391140
if (!btck_ChainstateManager::get(chainman).m_chainman->m_blockman.ReadBlockUndo(*block_undo, btck_BlockTreeEntry::get(entry))) {
10401141
LogError("Failed to read block spent outputs data.");
10411142
return nullptr;

0 commit comments

Comments
 (0)