Skip to content

Commit 1601515

Browse files
committed
Squashed 'libbitcoinkernel-sys/bitcoin/' changes from 81cec737e68..a755e00a135
a755e00a135 kernel: Fix bitcoin-chainstate for windows 670eb49289e kernel: Add Purpose section to header documentation 06ee1c7336b kernel: Allowing reducing exports 57b20b5a4d9 kernel: Add pure kernel bitcoin-chainstate 7cf58f8c4cb Kernel: Add functions for working with outpoints 64a4564ccb6 kernel: Add block hash type and block tree utility functions to C header 43855d656eb kernel: Add function to read block undo data from disk to C header e080e80da7d kernel: Add functions to read block from disk to C header 51634ca37ab kernel: Add function for copying block data to C header 5ed5a9724fd kernel: Add functions for the block validation state to C header 38e5c526a86 kernel: Add validation interface to C header REVERT: 81cec737e68 kernel: Fix bitcoin-chainstate for windows REVERT: 1826c485ddc kernel: Add Purpose section to header documentation REVERT: d7e618aa981 kernel: Allowing reducing exports REVERT: fb7f5241331 kernel: Add pure kernel bitcoin-chainstate REVERT: dd0bdf279ef Kernel: Add functions for working with outpoints REVERT: eaa6abfc733 kernel: Add block hash type and block tree utility functions to C header REVERT: 824ddf2885a kernel: Add function to read block undo data from disk to C header REVERT: 76cab0768b1 kernel: Add functions to read block from disk to C header REVERT: e41f6f459a2 kernel: Add function for copying block data to C header REVERT: 39c647647a2 kernel: Add functions for the block validation state to C header REVERT: 8a19a9d6070 kernel: Add validation interface to C header git-subtree-dir: libbitcoinkernel-sys/bitcoin git-subtree-split: a755e00a13541b3b5a707cf385f1cbec0449c6a9
1 parent 0fa2fda commit 1601515

File tree

3 files changed

+51
-31
lines changed

3 files changed

+51
-31
lines changed

src/kernel/bitcoinkernel.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -363,8 +363,8 @@ class KernelValidationInterface final : public CValidationInterface
363363
{
364364
if (m_cbs.pow_valid_block) {
365365
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}));
366+
btck_Block::ref(new std::shared_ptr<const CBlock>{block}),
367+
btck_BlockTreeEntry::ref(pindex));
368368
}
369369
}
370370

@@ -1095,12 +1095,9 @@ int32_t btck_block_tree_entry_get_height(const btck_BlockTreeEntry* entry)
10951095
return btck_BlockTreeEntry::get(entry).nHeight;
10961096
}
10971097

1098-
btck_BlockHash* btck_block_tree_entry_get_block_hash(const btck_BlockTreeEntry* entry)
1098+
const btck_BlockHash* btck_block_tree_entry_get_block_hash(const btck_BlockTreeEntry* entry)
10991099
{
1100-
if (btck_BlockTreeEntry::get(entry).phashBlock == nullptr) {
1101-
return nullptr;
1102-
}
1103-
return btck_BlockHash::create(btck_BlockTreeEntry::get(entry).GetBlockHash());
1100+
return btck_BlockHash::ref(btck_BlockTreeEntry::get(entry).phashBlock);
11041101
}
11051102

11061103
btck_BlockHash* btck_block_hash_create(const unsigned char block_hash[32])

src/kernel/bitcoinkernel.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ typedef struct btck_TransactionInput btck_TransactionInput;
266266
/**
267267
* Opaque data structure for holding a transaction out point.
268268
*
269-
* Holds the transaction id and output index it is pointing to.
269+
* Holds the txid and output index it is pointing to.
270270
*/
271271
typedef struct btck_TransactionOutPoint btck_TransactionOutPoint;
272272

@@ -311,7 +311,7 @@ typedef void (*btck_NotifyFatalError)(void* user_data, const char* message, size
311311
* Function signatures for the validation interface.
312312
*/
313313
typedef void (*btck_ValidationInterfaceBlockChecked)(void* user_data, btck_Block* block, const btck_BlockValidationState* state);
314-
typedef void (*btck_ValidationInterfacePowValidBlock)(void* user_data, const btck_BlockTreeEntry* entry, btck_Block* block);
314+
typedef void (*btck_ValidationInterfacePoWValidBlock)(void* user_data, btck_Block* block, const btck_BlockTreeEntry* entry);
315315
typedef void (*btck_ValidationInterfaceBlockConnected)(void* user_data, btck_Block* block, const btck_BlockTreeEntry* entry);
316316
typedef void (*btck_ValidationInterfaceBlockDisconnected)(void* user_data, btck_Block* block, const btck_BlockTreeEntry* entry);
317317

@@ -356,7 +356,7 @@ typedef struct {
356356
btck_DestroyCallback user_data_destroy; //!< Frees the provided user data structure.
357357
btck_ValidationInterfaceBlockChecked block_checked; //!< Called when a new block has been fully validated. Contains the
358358
//!< result of its validation.
359-
btck_ValidationInterfacePowValidBlock pow_valid_block; //!< Called when a new block extends the header chain and has a valid transaction
359+
btck_ValidationInterfacePoWValidBlock pow_valid_block; //!< Called when a new block extends the header chain and has a valid transaction
360360
//!< and segwit merkle root.
361361
btck_ValidationInterfaceBlockConnected block_connected; //!< Called when a block is valid and has now been connected to the best chain.
362362
btck_ValidationInterfaceBlockDisconnected block_disconnected; //!< Called during a re-org when a block has been removed from the best chain.
@@ -539,7 +539,8 @@ BITCOINKERNEL_API size_t BITCOINKERNEL_WARN_UNUSED_RESULT btck_transaction_count
539539
const btck_Transaction* transaction) BITCOINKERNEL_ARG_NONNULL(1);
540540

541541
/**
542-
* @brief Get the txid of a transaction.
542+
* @brief Get the txid of a transaction. The returned txid is not owned and
543+
* depends on the lifetime of the transaction.
543544
*
544545
* @param[in] transaction Non-null.
545546
* @return The txid.
@@ -879,7 +880,7 @@ BITCOINKERNEL_API void btck_context_destroy(btck_Context* context);
879880
///@{
880881

881882
/**
882-
* @brief Returns the previous block tree entry in the chain, or null if the current
883+
* @brief Returns the previous block tree entry in the tree, or null if the current
883884
* block tree entry is the genesis block.
884885
*
885886
* @param[in] block_tree_entry Non-null.
@@ -903,7 +904,7 @@ BITCOINKERNEL_API int32_t BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_tree_entry
903904
* @param[in] block_tree_entry Non-null.
904905
* @return The block hash.
905906
*/
906-
BITCOINKERNEL_API btck_BlockHash* BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_tree_entry_get_block_hash(
907+
BITCOINKERNEL_API const btck_BlockHash* BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_tree_entry_get_block_hash(
907908
const btck_BlockTreeEntry* block_tree_entry) BITCOINKERNEL_ARG_NONNULL(1);
908909

909910
///@}
@@ -1387,7 +1388,7 @@ BITCOINKERNEL_API btck_TransactionOutPoint* BITCOINKERNEL_WARN_UNUSED_RESULT btc
13871388
const btck_TransactionOutPoint* transaction_out_point) BITCOINKERNEL_ARG_NONNULL(1);
13881389

13891390
/**
1390-
* @brief Get the output position from the out point.
1391+
* @brief Get the output position from the transaction out point.
13911392
*
13921393
* @param[in] transaction_out_point Non-null.
13931394
* @return The output index.
@@ -1396,7 +1397,8 @@ BITCOINKERNEL_API uint32_t btck_transaction_out_point_get_index(
13961397
const btck_TransactionOutPoint* transaction_out_point) BITCOINKERNEL_ARG_NONNULL(1);
13971398

13981399
/**
1399-
* @brief Get the txid from the out point.
1400+
* @brief Get the txid from the transaction out point. The returned txid is
1401+
* not owned and depends on the lifetime of the transaction out point.
14001402
*
14011403
* @param[in] transaction_out_point Non-null.
14021404
* @return The txid.
@@ -1468,7 +1470,8 @@ BITCOINKERNEL_API btck_Coin* BITCOINKERNEL_WARN_UNUSED_RESULT btck_coin_copy(
14681470
const btck_Coin* coin) BITCOINKERNEL_ARG_NONNULL(1);
14691471

14701472
/**
1471-
* @brief Returns the height of the block that contains the coin's prevout.
1473+
* @brief Returns the block height where the transaction that
1474+
* created this coin was included in.
14721475
*
14731476
* @param[in] coin Non-null.
14741477
* @return The block height of the coin.

src/kernel/bitcoinkernel_wrapper.h

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -657,33 +657,53 @@ bool ScriptPubkeyApi<Derived>::Verify(int64_t amount,
657657
return result == 1;
658658
}
659659

660-
class BlockHash : public Handle<btck_BlockHash, btck_block_hash_copy, btck_block_hash_destroy>
660+
template <typename Derived>
661+
class BlockHashApi
661662
{
662-
public:
663-
explicit BlockHash(const std::array<std::byte, 32>& hash)
664-
: Handle{btck_block_hash_create(reinterpret_cast<const unsigned char*>(hash.data()))} {}
665-
666-
explicit BlockHash(btck_BlockHash* hash)
667-
: Handle{hash} {}
663+
private:
664+
auto impl() const
665+
{
666+
return static_cast<const Derived*>(this)->get();
667+
}
668668

669-
bool operator==(const BlockHash& other) const
669+
public:
670+
bool operator==(const Derived& other) const
670671
{
671-
return btck_block_hash_equals(get(), other.get()) != 0;
672+
return btck_block_hash_equals(impl(), other.get()) != 0;
672673
}
673674

674-
bool operator!=(const BlockHash& other) const
675+
bool operator!=(const Derived& other) const
675676
{
676-
return btck_block_hash_equals(get(), other.get()) == 0;
677+
return btck_block_hash_equals(impl(), other.get()) == 0;
677678
}
678679

679680
std::array<std::byte, 32> ToBytes() const
680681
{
681682
std::array<std::byte, 32> hash;
682-
btck_block_hash_to_bytes(get(), reinterpret_cast<unsigned char*>(hash.data()));
683+
btck_block_hash_to_bytes(impl(), reinterpret_cast<unsigned char*>(hash.data()));
683684
return hash;
684685
}
685686
};
686687

688+
class BlockHashView: public View<btck_BlockHash>, public BlockHashApi<BlockHashView>
689+
{
690+
public:
691+
explicit BlockHashView(const btck_BlockHash* ptr) : View{ptr} {}
692+
};
693+
694+
class BlockHash : public Handle<btck_BlockHash, btck_block_hash_copy, btck_block_hash_destroy>, public BlockHashApi<BlockHash>
695+
{
696+
public:
697+
explicit BlockHash(const std::array<std::byte, 32>& hash)
698+
: Handle{btck_block_hash_create(reinterpret_cast<const unsigned char*>(hash.data()))} {}
699+
700+
explicit BlockHash(btck_BlockHash* hash)
701+
: Handle{hash} {}
702+
703+
BlockHash(const BlockHashView& view)
704+
: Handle{view} {}
705+
};
706+
687707
class Block : public Handle<btck_Block, btck_block_copy, btck_block_destroy>
688708
{
689709
public:
@@ -781,9 +801,9 @@ class BlockTreeEntry : public View<btck_BlockTreeEntry>
781801
return btck_block_tree_entry_get_height(get());
782802
}
783803

784-
BlockHash GetHash() const
804+
BlockHashView GetHash() const
785805
{
786-
return BlockHash{btck_block_tree_entry_get_block_hash(get())};
806+
return BlockHashView{btck_block_tree_entry_get_block_hash(get())};
787807
}
788808

789809
friend class ChainMan;
@@ -902,7 +922,7 @@ class ContextOptions : UniqueHandle<btck_ContextOptions, btck_context_options_de
902922
.user_data = heap_vi.release(),
903923
.user_data_destroy = +[](void* user_data) { delete static_cast<user_type>(user_data); },
904924
.block_checked = +[](void* user_data, btck_Block* block, const btck_BlockValidationState* state) { (*static_cast<user_type>(user_data))->BlockChecked(Block{block}, BlockValidationState{state}); },
905-
.pow_valid_block = +[](void* user_data, const btck_BlockTreeEntry* entry, btck_Block* block) { (*static_cast<user_type>(user_data))->PowValidBlock(BlockTreeEntry{entry}, Block{block}); },
925+
.pow_valid_block = +[](void* user_data, btck_Block* block, const btck_BlockTreeEntry* entry) { (*static_cast<user_type>(user_data))->PowValidBlock(BlockTreeEntry{entry}, Block{block}); },
906926
.block_connected = +[](void* user_data, btck_Block* block, const btck_BlockTreeEntry* entry) { (*static_cast<user_type>(user_data))->BlockConnected(Block{block}, BlockTreeEntry{entry}); },
907927
.block_disconnected = +[](void* user_data, btck_Block* block, const btck_BlockTreeEntry* entry) { (*static_cast<user_type>(user_data))->BlockDisconnected(Block{block}, BlockTreeEntry{entry}); },
908928
});

0 commit comments

Comments
 (0)