Skip to content

Commit e5e72a4

Browse files
committed
Squashed 'libbitcoinkernel-sys/bitcoin/' changes from 48158303fe2..26042c71846
26042c71846 kernel: Add pure kernel bitcoin-chainstate bdc4e56deac kernel: Add functions to get the block hash from a block REVERT: 48158303fe2 kernel: Add pure kernel bitcoin-chainstate git-subtree-dir: libbitcoinkernel-sys/bitcoin git-subtree-split: 26042c718462bc4e484526fa14fb962e95e8bc13
1 parent a73b2bd commit e5e72a4

File tree

4 files changed

+52
-1
lines changed

4 files changed

+52
-1
lines changed

src/kernel/bitcoinkernel.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,24 @@ kernel_ByteArray* kernel_copy_block_pointer_data(const kernel_BlockPointer* bloc
980980
return byte_array;
981981
}
982982

983+
kernel_BlockHash* kernel_block_get_hash(kernel_Block* block_)
984+
{
985+
auto block{cast_cblocksharedpointer(block_)};
986+
auto hash{(*block)->GetHash()};
987+
auto block_hash = new kernel_BlockHash{};
988+
std::memcpy(block_hash->hash, hash.begin(), sizeof(hash));
989+
return block_hash;
990+
}
991+
992+
kernel_BlockHash* kernel_block_pointer_get_hash(const kernel_BlockPointer* block_)
993+
{
994+
auto block{cast_const_cblock(block_)};
995+
auto hash{block->GetHash()};
996+
auto block_hash = new kernel_BlockHash{};
997+
std::memcpy(block_hash->hash, hash.begin(), sizeof(hash));
998+
return block_hash;
999+
}
1000+
9831001
void kernel_block_destroy(kernel_Block* block)
9841002
{
9851003
if (block) {

src/kernel/bitcoinkernel.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,16 @@ kernel_Block* BITCOINKERNEL_WARN_UNUSED_RESULT kernel_block_create(
898898
const unsigned char* raw_block, size_t raw_block_len
899899
) BITCOINKERNEL_ARG_NONNULL(1);
900900

901+
/**
902+
* @brief Calculate and return the hash of a block.
903+
*
904+
* @param[in] block Non-null.
905+
* @return The block hash.
906+
*/
907+
kernel_BlockHash* BITCOINKERNEL_WARN_UNUSED_RESULT kernel_block_get_hash(
908+
kernel_Block* block
909+
) BITCOINKERNEL_ARG_NONNULL(1);
910+
901911
/**
902912
* Destroy the block.
903913
*/
@@ -923,6 +933,16 @@ kernel_ByteArray* BITCOINKERNEL_WARN_UNUSED_RESULT kernel_copy_block_pointer_dat
923933
const kernel_BlockPointer* block
924934
) BITCOINKERNEL_ARG_NONNULL(1);
925935

936+
/**
937+
* @brief Calculate and return the hash of a block.
938+
*
939+
* @param[in] block Non-null.
940+
* @return The block hash.
941+
*/
942+
kernel_BlockHash* BITCOINKERNEL_WARN_UNUSED_RESULT kernel_block_pointer_get_hash(
943+
const kernel_BlockPointer* block
944+
) BITCOINKERNEL_ARG_NONNULL(1);
945+
926946
/**
927947
* A helper function for destroying an existing byte array.
928948
*/

src/kernel/bitcoinkernel_wrapper.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,11 @@ class UnownedBlock
312312
UnownedBlock(UnownedBlock&&) = delete;
313313
UnownedBlock& operator=(UnownedBlock&&) = delete;
314314

315+
kernel_BlockHash* GetHash() const noexcept
316+
{
317+
return kernel_block_pointer_get_hash(m_block);
318+
}
319+
315320
std::vector<unsigned char> GetBlockData() const noexcept
316321
{
317322
auto serialized_block{kernel_copy_block_pointer_data(m_block)};
@@ -495,6 +500,11 @@ class Block
495500

496501
Block(kernel_Block* block) noexcept : m_block{block} {}
497502

503+
kernel_BlockHash* GetHash() const noexcept
504+
{
505+
return kernel_block_get_hash(m_block.get());
506+
}
507+
498508
std::vector<unsigned char> GetBlockData() const noexcept
499509
{
500510
auto serialized_block{kernel_copy_block_data(m_block.get())};

src/test/kernel/test_kernel.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,8 @@ void chainman_reindex_test(TestDirectory& test_directory)
567567
auto tip_index{chainman->GetBlockIndexFromTip()};
568568
auto tip_block_string{chainman->ReadBlock(tip_index).value().GetBlockData()};
569569
auto second_index{chainman->GetBlockIndexByHeight(1).value()};
570-
auto second_block_string{chainman->ReadBlock(second_index).value().GetBlockData()};
570+
auto second_block{chainman->ReadBlock(second_index).value()};
571+
auto second_block_string{second_block.GetBlockData()};
571572
auto second_height{second_index.GetHeight()};
572573
assert(second_height == 1);
573574
assert(next_block_string == tip_block_string);
@@ -576,6 +577,8 @@ void chainman_reindex_test(TestDirectory& test_directory)
576577
auto hash{second_index.GetHash()};
577578
auto another_second_index{chainman->GetBlockIndexByHash(hash.get())};
578579
auto another_second_height{another_second_index.GetHeight()};
580+
auto block_hash{second_block.GetHash()};
581+
assert(std::equal(std::begin(block_hash->hash), std::end(block_hash->hash), std::begin(hash->hash)));
579582
assert(second_height == another_second_height);
580583
}
581584

0 commit comments

Comments
 (0)