Skip to content

Commit 7987947

Browse files
committed
Merge commit 'cc50e310686f44fd56b27ddf6839f0ffc2cb6a02'
2 parents 69ac072 + cc50e31 commit 7987947

File tree

4 files changed

+143
-37
lines changed

4 files changed

+143
-37
lines changed

depend/bitcoin/src/kernel/bitcoinkernel.cpp

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ struct btck_Chain : Handle<btck_Chain, CChain> {};
470470
struct btck_BlockSpentOutputs : Handle<btck_BlockSpentOutputs, std::shared_ptr<CBlockUndo>> {};
471471
struct btck_TransactionSpentOutputs : Handle<btck_TransactionSpentOutputs, CTxUndo> {};
472472
struct btck_Coin : Handle<btck_Coin, Coin> {};
473+
struct btck_BlockHash : Handle<btck_BlockHash, uint256> {};
473474

474475
btck_Transaction* btck_transaction_create(const void* raw_transaction, size_t raw_transaction_len)
475476
{
@@ -889,9 +890,8 @@ btck_ChainstateManager* btck_chainstate_manager_create(
889890

890891
const btck_BlockTreeEntry* btck_chainstate_manager_get_block_tree_entry_by_hash(const btck_ChainstateManager* chainman, const btck_BlockHash* block_hash)
891892
{
892-
auto hash = uint256{std::span<const unsigned char>{(*block_hash).hash, 32}};
893893
auto block_index = WITH_LOCK(btck_ChainstateManager::get(chainman).m_chainman->GetMutex(),
894-
return btck_ChainstateManager::get(chainman).m_chainman->m_blockman.LookupBlockIndex(hash));
894+
return btck_ChainstateManager::get(chainman).m_chainman->m_blockman.LookupBlockIndex(btck_BlockHash::get(block_hash)));
895895
if (!block_index) {
896896
LogDebug(BCLog::KERNEL, "A block with the given hash is not indexed.");
897897
return nullptr;
@@ -978,10 +978,7 @@ int btck_block_to_bytes(const btck_Block* block, btck_WriteBytes writer, void* u
978978

979979
btck_BlockHash* btck_block_get_hash(const btck_Block* block)
980980
{
981-
auto hash{btck_Block::get(block)->GetHash()};
982-
auto block_hash = new btck_BlockHash{};
983-
std::memcpy(block_hash->hash, hash.begin(), sizeof(hash));
984-
return block_hash;
981+
return btck_BlockHash::create(btck_Block::get(block)->GetHash());
985982
}
986983

987984
void btck_block_destroy(btck_Block* block)
@@ -1009,9 +1006,22 @@ btck_BlockHash* btck_block_tree_entry_get_block_hash(const btck_BlockTreeEntry*
10091006
if (btck_BlockTreeEntry::get(entry).phashBlock == nullptr) {
10101007
return nullptr;
10111008
}
1012-
auto block_hash = new btck_BlockHash{};
1013-
std::memcpy(block_hash->hash, btck_BlockTreeEntry::get(entry).phashBlock->begin(), sizeof(*btck_BlockTreeEntry::get(entry).phashBlock));
1014-
return block_hash;
1009+
return btck_BlockHash::create(btck_BlockTreeEntry::get(entry).GetBlockHash());
1010+
}
1011+
1012+
btck_BlockHash* btck_block_hash_create(const unsigned char block_hash[32])
1013+
{
1014+
return btck_BlockHash::create(std::span<const unsigned char>{block_hash, 32});
1015+
}
1016+
1017+
btck_BlockHash* btck_block_hash_copy(const btck_BlockHash* block_hash)
1018+
{
1019+
return btck_BlockHash::copy(block_hash);
1020+
}
1021+
1022+
void btck_block_hash_to_bytes(const btck_BlockHash* block_hash, unsigned char output[32])
1023+
{
1024+
std::memcpy(output, btck_BlockHash::get(block_hash).begin(), 32);
10151025
}
10161026

10171027
void btck_block_hash_destroy(btck_BlockHash* hash)

depend/bitcoin/src/kernel/bitcoinkernel.h

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,13 @@ typedef struct btck_TransactionSpentOutputs btck_TransactionSpentOutputs;
249249
*/
250250
typedef struct btck_Coin btck_Coin;
251251

252+
/**
253+
* Opaque data structure for holding a block hash.
254+
*
255+
* This is a type-safe identifier for a block.
256+
*/
257+
typedef struct btck_BlockHash btck_BlockHash;
258+
252259
/** Current sync state passed to tip changed callbacks. */
253260
typedef uint8_t btck_SynchronizationState;
254261
#define btck_SynchronizationState_INIT_REINDEX ((btck_SynchronizationState)(0))
@@ -426,13 +433,6 @@ typedef uint8_t btck_ChainType;
426433
#define btck_ChainType_SIGNET ((btck_ChainType)(3))
427434
#define btck_ChainType_REGTEST ((btck_ChainType)(4))
428435

429-
/**
430-
* A type-safe block identifier.
431-
*/
432-
typedef struct {
433-
unsigned char hash[32];
434-
} btck_BlockHash;
435-
436436
/** @name Transaction
437437
* Functions for working with transactions.
438438
*/
@@ -853,6 +853,15 @@ BITCOINKERNEL_API const btck_BlockTreeEntry* BITCOINKERNEL_WARN_UNUSED_RESULT bt
853853
BITCOINKERNEL_API int32_t BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_tree_entry_get_height(
854854
const btck_BlockTreeEntry* block_tree_entry) BITCOINKERNEL_ARG_NONNULL(1);
855855

856+
/**
857+
* @brief Return the block hash associated with a block tree entry.
858+
*
859+
* @param[in] block_tree_entry Non-null.
860+
* @return The block hash.
861+
*/
862+
BITCOINKERNEL_API btck_BlockHash* BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_tree_entry_get_block_hash(
863+
const btck_BlockTreeEntry* block_tree_entry) BITCOINKERNEL_ARG_NONNULL(1);
864+
856865
///@}
857866

858867
/** @name ChainstateManagerOptions
@@ -1087,7 +1096,7 @@ BITCOINKERNEL_API btck_BlockHash* BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_ge
10871096
* @param[in] writer Non-null, callback to a write bytes function.
10881097
* @param[in] user_data Holds a user-defined opaque structure that will be
10891098
* passed back through the writer callback.
1090-
* @return True on success.
1099+
* @return 0 on success.
10911100
*/
10921101
BITCOINKERNEL_API int btck_block_to_bytes(
10931102
const btck_Block* block,
@@ -1336,13 +1345,28 @@ BITCOINKERNEL_API void btck_coin_destroy(btck_Coin* coin);
13361345
///@{
13371346

13381347
/**
1339-
* @brief Return the block hash associated with a block tree entry.
1348+
* @brief Create a block hash from its raw data.
1349+
*/
1350+
BITCOINKERNEL_API btck_BlockHash* BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_hash_create(
1351+
const unsigned char block_hash[32]) BITCOINKERNEL_ARG_NONNULL(1);
1352+
1353+
/**
1354+
* @brief Copy a block hash.
13401355
*
1341-
* @param[in] block_tree_entry Non-null.
1342-
* @return The block hash.
1356+
* @param[in] block_hash Non-null.
1357+
* @return The copied block hash.
13431358
*/
1344-
BITCOINKERNEL_API btck_BlockHash* BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_tree_entry_get_block_hash(
1345-
const btck_BlockTreeEntry* block_tree_entry) BITCOINKERNEL_ARG_NONNULL(1);
1359+
BITCOINKERNEL_API btck_BlockHash* BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_hash_copy(
1360+
const btck_BlockHash* block_hash) BITCOINKERNEL_ARG_NONNULL(1);
1361+
1362+
/**
1363+
* @brief Serializes the block hash to bytes.
1364+
*
1365+
* @param[in] block_hash Non-null.
1366+
* @param[in] output The serialized block hash.
1367+
*/
1368+
BITCOINKERNEL_API void btck_block_hash_to_bytes(
1369+
const btck_BlockHash* block_hash, unsigned char output[32]) BITCOINKERNEL_ARG_NONNULL(1, 2);
13461370

13471371
/**
13481372
* Destroy the block hash.

depend/bitcoin/src/kernel/bitcoinkernel_wrapper.h

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <kernel/bitcoinkernel.h>
99

10+
#include <array>
1011
#include <functional>
1112
#include <memory>
1213
#include <optional>
@@ -473,7 +474,7 @@ class TransactionApi
473474

474475
auto Outputs() const
475476
{
476-
return Range<Transaction, &TransactionApi::CountOutputs, &TransactionApi::GetOutput>{*static_cast<const Derived*>(this)};
477+
return Range<Derived, &TransactionApi<Derived>::CountOutputs, &TransactionApi<Derived>::GetOutput>{*static_cast<const Derived*>(this)};
477478
}
478479

479480
std::vector<std::byte> ToBytes() const
@@ -527,10 +528,20 @@ bool ScriptPubkeyApi<Derived>::Verify(int64_t amount,
527528
return result == 1;
528529
}
529530

530-
struct BlockHashDeleter {
531-
void operator()(btck_BlockHash* ptr) const
531+
class BlockHash : public Handle<btck_BlockHash, btck_block_hash_copy, btck_block_hash_destroy>
532+
{
533+
public:
534+
explicit BlockHash(const std::array<std::byte, 32>& hash)
535+
: Handle{btck_block_hash_create(reinterpret_cast<const unsigned char*>(hash.data()))} {}
536+
537+
explicit BlockHash(btck_BlockHash* hash)
538+
: Handle{hash} {}
539+
540+
std::array<std::byte, 32> Bytes() const
532541
{
533-
btck_block_hash_destroy(ptr);
542+
std::array<std::byte, 32> hash;
543+
btck_block_hash_to_bytes(get(), reinterpret_cast<unsigned char*>(hash.data()));
544+
return hash;
534545
}
535546
};
536547

@@ -559,9 +570,9 @@ class Block : public Handle<btck_Block, btck_block_copy, btck_block_destroy>
559570
return Range<Block, &Block::CountTransactions, &Block::GetTransaction>{*this};
560571
}
561572

562-
std::unique_ptr<btck_BlockHash, BlockHashDeleter> GetHash() const
573+
BlockHash GetHash() const
563574
{
564-
return std::unique_ptr<btck_BlockHash, BlockHashDeleter>(btck_block_get_hash(get()));
575+
return BlockHash{btck_block_get_hash(get())};
565576
}
566577

567578
std::vector<std::byte> ToBytes() const
@@ -631,9 +642,9 @@ class BlockTreeEntry : public View<btck_BlockTreeEntry>
631642
return btck_block_tree_entry_get_height(get());
632643
}
633644

634-
std::unique_ptr<btck_BlockHash, BlockHashDeleter> GetHash() const
645+
BlockHash GetHash() const
635646
{
636-
return std::unique_ptr<btck_BlockHash, BlockHashDeleter>(btck_block_tree_entry_get_block_hash(get()));
647+
return BlockHash{btck_block_tree_entry_get_block_hash(get())};
637648
}
638649

639650
friend class ChainMan;
@@ -761,6 +772,10 @@ class Context : public Handle<btck_Context, btck_context_copy, btck_context_dest
761772
Context()
762773
: Handle{check(btck_context_create(ContextOptions{}.get()))} {}
763774

775+
bool interrupt() {
776+
return btck_context_interrupt(get()) == 0;
777+
}
778+
764779
friend class ChainstateManagerOptions;
765780
};
766781

@@ -848,7 +863,7 @@ class CoinApi
848863
public:
849864
uint32_t GetConfirmationHeight() const { return btck_coin_confirmation_height(impl()); }
850865

851-
bool IsCoinbase() const { return btck_coin_is_coinbase(impl()); }
866+
bool IsCoinbase() const { return btck_coin_is_coinbase(impl()) == 1; }
852867

853868
TransactionOutputView GetOutput() const
854869
{
@@ -866,6 +881,8 @@ class Coin : Handle<btck_Coin, btck_coin_copy, btck_coin_destroy>, public CoinAp
866881
{
867882
public:
868883
Coin(btck_Coin* coin) : Handle{check(coin)} {}
884+
885+
Coin(const CoinView& view) : Handle{view} {}
869886
};
870887

871888
template <typename Derived>
@@ -908,6 +925,8 @@ class TransactionSpentOutputs : Handle<btck_TransactionSpentOutputs, btck_transa
908925
{
909926
public:
910927
TransactionSpentOutputs(btck_TransactionSpentOutputs* transaction_spent_outputs) : Handle{check(transaction_spent_outputs)} {}
928+
929+
TransactionSpentOutputs(const TransactionSpentOutputsView& view) : Handle{view} {}
911930
};
912931

913932
class BlockSpentOutputs : Handle<btck_BlockSpentOutputs, btck_block_spent_outputs_copy, btck_block_spent_outputs_destroy>
@@ -969,12 +988,12 @@ class ChainMan : UniqueHandle<btck_ChainstateManager, btck_chainstate_manager_de
969988
return ChainView{btck_chainstate_manager_get_active_chain(get())};
970989
}
971990

972-
BlockTreeEntry GetBlockTreeEntry(btck_BlockHash* block_hash) const
991+
BlockTreeEntry GetBlockTreeEntry(const btck_BlockHash* block_hash) const
973992
{
974993
return btck_chainstate_manager_get_block_tree_entry_by_hash(get(), block_hash);
975994
}
976995

977-
std::optional<Block> ReadBlock(BlockTreeEntry& entry) const
996+
std::optional<Block> ReadBlock(const BlockTreeEntry& entry) const
978997
{
979998
auto block{btck_block_read(get(), entry.get())};
980999
if (!block) return std::nullopt;

0 commit comments

Comments
 (0)