@@ -242,6 +242,13 @@ typedef struct btck_TransactionSpentOutputs btck_TransactionSpentOutputs;
242242 */
243243typedef struct btck_Coin btck_Coin;
244244
245+ /* *
246+ * Opaque data structure for holding a block hash.
247+ *
248+ * This is a type-safe identifier for a block.
249+ */
250+ typedef struct btck_BlockHash btck_BlockHash;
251+
245252/* * Current sync state passed to tip changed callbacks. */
246253typedef uint8_t btck_SynchronizationState;
247254#define btck_SynchronizationState_INIT_REINDEX ((btck_SynchronizationState)(0 ))
@@ -842,7 +849,7 @@ BITCOINKERNEL_API void btck_context_destroy(btck_Context* context);
842849// /@{
843850
844851/* *
845- * @brief Returns the previous block tree entry in the chain , or null if the current
852+ * @brief Returns the previous block tree entry in the tree , or null if the current
846853 * block tree entry is the genesis block.
847854 *
848855 * @param[in] block_tree_entry Non-null.
@@ -851,6 +858,24 @@ BITCOINKERNEL_API void btck_context_destroy(btck_Context* context);
851858BITCOINKERNEL_API const btck_BlockTreeEntry* BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_tree_entry_get_previous (
852859 const btck_BlockTreeEntry* block_tree_entry) BITCOINKERNEL_ARG_NONNULL(1 );
853860
861+ /* *
862+ * @brief Return the height of a certain block tree entry.
863+ *
864+ * @param[in] block_tree_entry Non-null.
865+ * @return The block height.
866+ */
867+ BITCOINKERNEL_API int32_t BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_tree_entry_get_height (
868+ const btck_BlockTreeEntry* block_tree_entry) BITCOINKERNEL_ARG_NONNULL(1 );
869+
870+ /* *
871+ * @brief Return the block hash associated with a block tree entry.
872+ *
873+ * @param[in] block_tree_entry Non-null.
874+ * @return The block hash.
875+ */
876+ BITCOINKERNEL_API const btck_BlockHash* BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_tree_entry_get_block_hash (
877+ const btck_BlockTreeEntry* block_tree_entry) BITCOINKERNEL_ARG_NONNULL(1 );
878+
854879// /@}
855880
856881/* * @name ChainstateManagerOptions
@@ -1001,6 +1026,18 @@ BITCOINKERNEL_API int BITCOINKERNEL_WARN_UNUSED_RESULT btck_chainstate_manager_p
10011026BITCOINKERNEL_API const btck_Chain* BITCOINKERNEL_WARN_UNUSED_RESULT btck_chainstate_manager_get_active_chain (
10021027 const btck_ChainstateManager* chainstate_manager) BITCOINKERNEL_ARG_NONNULL(1 );
10031028
1029+ /* *
1030+ * @brief Retrieve a block tree entry by its block hash.
1031+ *
1032+ * @param[in] chainstate_manager Non-null.
1033+ * @param[in] block_hash Non-null.
1034+ * @return The block tree entry of the block with the passed in hash, or null if
1035+ * the block hash is not found.
1036+ */
1037+ BITCOINKERNEL_API const btck_BlockTreeEntry* BITCOINKERNEL_WARN_UNUSED_RESULT btck_chainstate_manager_get_block_tree_entry_by_hash (
1038+ const btck_ChainstateManager* chainstate_manager,
1039+ const btck_BlockHash* block_hash) BITCOINKERNEL_ARG_NONNULL(1 , 2 );
1040+
10041041/* *
10051042 * Destroy the chainstate manager.
10061043 */
@@ -1065,6 +1102,15 @@ BITCOINKERNEL_API size_t BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_count_trans
10651102BITCOINKERNEL_API const btck_Transaction* BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_get_transaction_at (
10661103 const btck_Block* block, size_t transaction_index) BITCOINKERNEL_ARG_NONNULL(1 );
10671104
1105+ /* *
1106+ * @brief Calculate and return the hash of a block.
1107+ *
1108+ * @param[in] block Non-null.
1109+ * @return The block hash.
1110+ */
1111+ BITCOINKERNEL_API btck_BlockHash* BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_get_hash (
1112+ const btck_Block* block) BITCOINKERNEL_ARG_NONNULL(1 );
1113+
10681114/* *
10691115 * @brief Serializes the block through the passed in callback to bytes.
10701116 * This is consensus serialization that is also used for the P2P network.
@@ -1130,6 +1176,40 @@ BITCOINKERNEL_API const btck_BlockTreeEntry* BITCOINKERNEL_WARN_UNUSED_RESULT bt
11301176BITCOINKERNEL_API int32_t BITCOINKERNEL_WARN_UNUSED_RESULT btck_chain_get_height (
11311177 const btck_Chain* chain) BITCOINKERNEL_ARG_NONNULL(1 );
11321178
1179+ /* *
1180+ * @brief Get the block tree entry of the genesis block.
1181+ *
1182+ * @param[in] chain Non-null.
1183+ * @return The block tree entry of the genesis block, or null if the chain is empty.
1184+ */
1185+ BITCOINKERNEL_API const btck_BlockTreeEntry* BITCOINKERNEL_WARN_UNUSED_RESULT btck_chain_get_genesis (
1186+ const btck_Chain* chain) BITCOINKERNEL_ARG_NONNULL(1 );
1187+
1188+ /* *
1189+ * @brief Retrieve a block tree entry by its height in the currently active chain.
1190+ * Once retrieved there is no guarantee that it remains in the active chain.
1191+ *
1192+ * @param[in] chain Non-null.
1193+ * @param[in] block_height Height in the chain of the to be retrieved block tree entry.
1194+ * @return The block tree entry at a certain height in the currently active chain, or null
1195+ * if the height is out of bounds.
1196+ */
1197+ BITCOINKERNEL_API const btck_BlockTreeEntry* BITCOINKERNEL_WARN_UNUSED_RESULT btck_chain_get_by_height (
1198+ const btck_Chain* chain,
1199+ int block_height) BITCOINKERNEL_ARG_NONNULL(1 );
1200+
1201+ /* *
1202+ * @brief Return true if the passed in chain contains the block tree entry.
1203+ *
1204+ * @param[in] chain Non-null.
1205+ * @param[in] block_tree_entry Non-null.
1206+ * @return 1 if the block_tree_entry is in the chain, 0 otherwise.
1207+ *
1208+ */
1209+ BITCOINKERNEL_API int BITCOINKERNEL_WARN_UNUSED_RESULT btck_chain_contains (
1210+ const btck_Chain* chain,
1211+ const btck_BlockTreeEntry* block_tree_entry) BITCOINKERNEL_ARG_NONNULL(1 , 2 );
1212+
11331213// /@}
11341214
11351215/* * @name BlockSpentOutputs
@@ -1282,6 +1362,52 @@ BITCOINKERNEL_API void btck_coin_destroy(btck_Coin* coin);
12821362
12831363// /@}
12841364
1365+ /* * @name BlockHash
1366+ * Functions for working with block hashes.
1367+ */
1368+ // /@{
1369+
1370+ /* *
1371+ * @brief Create a block hash from its raw data.
1372+ */
1373+ BITCOINKERNEL_API btck_BlockHash* BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_hash_create (
1374+ const unsigned char block_hash[32 ]) BITCOINKERNEL_ARG_NONNULL(1 );
1375+
1376+ /* *
1377+ * @brief Check if two block hashes are equal.
1378+ *
1379+ * @param[in] hash1 Non-null.
1380+ * @param[in] hash2 Non-null.
1381+ * @return 0 if the block hashes are not equal.
1382+ */
1383+ BITCOINKERNEL_API int BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_hash_equals (
1384+ const btck_BlockHash* hash1, const btck_BlockHash* hash2) BITCOINKERNEL_ARG_NONNULL(1 , 2 );
1385+
1386+ /* *
1387+ * @brief Copy a block hash.
1388+ *
1389+ * @param[in] block_hash Non-null.
1390+ * @return The copied block hash.
1391+ */
1392+ BITCOINKERNEL_API btck_BlockHash* BITCOINKERNEL_WARN_UNUSED_RESULT btck_block_hash_copy (
1393+ const btck_BlockHash* block_hash) BITCOINKERNEL_ARG_NONNULL(1 );
1394+
1395+ /* *
1396+ * @brief Serializes the block hash to bytes.
1397+ *
1398+ * @param[in] block_hash Non-null.
1399+ * @param[in] output The serialized block hash.
1400+ */
1401+ BITCOINKERNEL_API void btck_block_hash_to_bytes (
1402+ const btck_BlockHash* block_hash, unsigned char output[32 ]) BITCOINKERNEL_ARG_NONNULL(1 , 2 );
1403+
1404+ /* *
1405+ * Destroy the block hash.
1406+ */
1407+ BITCOINKERNEL_API void btck_block_hash_destroy (btck_BlockHash* block_hash);
1408+
1409+ // /@}
1410+
12851411#ifdef __cplusplus
12861412} // extern "C"
12871413#endif // __cplusplus
0 commit comments