From a2b57b6902bf125fef3de87d49d193967e69999d Mon Sep 17 00:00:00 2001 From: k-matsuzawa Date: Fri, 4 Jun 2021 21:59:14 +0900 Subject: [PATCH 1/2] feat: add GetTransactionCount and GetTxid API --- include/cfdc/cfdcapi_block.h | 23 +++++++++ src/capi/cfdcapi_block.cpp | 88 +++++++++++++++++++++++++++++++- test/capi/test_cfdcapi_block.cpp | 13 +++++ 3 files changed, 122 insertions(+), 2 deletions(-) diff --git a/include/cfdc/cfdcapi_block.h b/include/cfdc/cfdcapi_block.h index 083e322d..04959e85 100644 --- a/include/cfdc/cfdcapi_block.h +++ b/include/cfdc/cfdcapi_block.h @@ -109,6 +109,29 @@ CFDC_API int CfdGetTxOutProof( CFDC_API int CfdExistTxidInBlock( void* handle, void* block_handle, const char* txid); +/** + * @brief Get transaction count in block. + * @param[in] handle cfd handle. + * @param[in] block_handle block handle. + * @param[out] tx_count tx count in block. + * @return CfdErrorCode + */ +CFDC_API int CfdGetTxCountInBlock( + void* handle, void* block_handle, uint32_t* tx_count); + +/** + * @brief Get txid from block. + * @param[in] handle cfd handle. + * @param[in] block_handle block handle. + * @param[in] index index. (max: tx count) + * @param[out] txid txid. + * If 'CfdFreeStringBuffer' is implemented, + * Call 'CfdFreeStringBuffer' after you are finished using it. + * @return CfdErrorCode + */ +CFDC_API int CfdGetTxidFromBlock( + void* handle, void* block_handle, uint32_t index, char** txid); + #ifdef __cplusplus #if 0 { diff --git a/src/capi/cfdcapi_block.cpp b/src/capi/cfdcapi_block.cpp index 8f191dad..2fa687d0 100644 --- a/src/capi/cfdcapi_block.cpp +++ b/src/capi/cfdcapi_block.cpp @@ -322,8 +322,7 @@ int CfdGetTxOutProof( CfdError::kCfdIllegalStateError, "Invalid handle state. block is null"); } else if (block_data->net_type <= NetType::kRegtest) { - auto proof = block_data->block->GetTxOutProof( - std::vector{Txid(std::string(txid))}); + auto proof = block_data->block->GetTxOutProof(Txid(txid)); *txout_proof = CreateString(proof.GetHex()); } else { #ifndef CFD_DISABLE_ELEMENTS @@ -389,6 +388,91 @@ int CfdExistTxidInBlock(void* handle, void* block_handle, const char* txid) { return result; } +int CfdGetTxCountInBlock( + void* handle, void* block_handle, uint32_t* tx_count) { + int result = CfdErrorCode::kCfdUnknownError; + try { + cfd::Initialize(); + CheckBuffer(block_handle, kPrefixBlock); + CfdCapiBlockData* block_data = + static_cast(block_handle); + if (tx_count == nullptr) { + warn(CFD_LOG_SOURCE, "tx_count is null."); + throw CfdException( + CfdError::kCfdIllegalArgumentError, + "Failed to parameter. tx_count is null."); + } + + if (block_data->block == nullptr) { + throw CfdException( + CfdError::kCfdIllegalStateError, + "Invalid handle state. block is null"); + } else if (block_data->net_type <= NetType::kRegtest) { + *tx_count = block_data->block->GetTransactionCount(); + } else { +#ifndef CFD_DISABLE_ELEMENTS + throw CfdException( + CfdError::kCfdIllegalArgumentError, "Elements is not supported."); +#else + throw CfdException( + CfdError::kCfdIllegalArgumentError, "Elements is not supported."); +#endif // CFD_DISABLE_ELEMENTS + } + + return CfdErrorCode::kCfdSuccess; + } catch (const CfdException& except) { + result = SetLastError(handle, except); + } catch (const std::exception& std_except) { + SetLastFatalError(handle, std_except.what()); + } catch (...) { + SetLastFatalError(handle, "unknown error."); + } + return result; +} + +int CfdGetTxidFromBlock( + void* handle, void* block_handle, uint32_t index, char** txid) { + int result = CfdErrorCode::kCfdUnknownError; + try { + cfd::Initialize(); + CheckBuffer(block_handle, kPrefixBlock); + CfdCapiBlockData* block_data = + static_cast(block_handle); + if (txid == nullptr) { + warn(CFD_LOG_SOURCE, "txid is null."); + throw CfdException( + CfdError::kCfdIllegalArgumentError, + "Failed to parameter. txid is null."); + } + + if (block_data->block == nullptr) { + throw CfdException( + CfdError::kCfdIllegalStateError, + "Invalid handle state. block is null"); + } else if (block_data->net_type <= NetType::kRegtest) { + auto txid_obj = block_data->block->GetTxid(index); + *txid = CreateString(txid_obj.GetHex()); + } else { +#ifndef CFD_DISABLE_ELEMENTS + throw CfdException( + CfdError::kCfdIllegalArgumentError, "Elements is not supported."); +#else + throw CfdException( + CfdError::kCfdIllegalArgumentError, "Elements is not supported."); +#endif // CFD_DISABLE_ELEMENTS + } + + return CfdErrorCode::kCfdSuccess; + } catch (const CfdException& except) { + result = SetLastError(handle, except); + } catch (const std::exception& std_except) { + SetLastFatalError(handle, std_except.what()); + } catch (...) { + SetLastFatalError(handle, "unknown error."); + } + return result; +} + }; // extern "C" #endif // CFD_DISABLE_CAPI diff --git a/test/capi/test_cfdcapi_block.cpp b/test/capi/test_cfdcapi_block.cpp index 10ab5b75..600bdd9d 100644 --- a/test/capi/test_cfdcapi_block.cpp +++ b/test/capi/test_cfdcapi_block.cpp @@ -89,6 +89,19 @@ TEST(cfdcapi_block, GetBlockData) { "7f5fb624f5cdce391362aa6befea307c4e778e008e799b40ca7119046f26ab31"); EXPECT_EQ(kCfdNotFoundError, ret); + uint32_t tx_count = 0; + ret = CfdGetTxCountInBlock(handle, block_handle, &tx_count); + EXPECT_EQ(kCfdSuccess, ret); + EXPECT_EQ(1, tx_count); + + char* temp_txid = nullptr; + ret = CfdGetTxidFromBlock(handle, block_handle, 0, &temp_txid); + EXPECT_EQ(kCfdSuccess, ret); + if (ret == kCfdSuccess) { + EXPECT_STREQ(txid, temp_txid); + CfdFreeStringBuffer(temp_txid); + } + ret = CfdFreeBlockHandle(handle, block_handle); EXPECT_EQ(kCfdSuccess, ret); } From e19583cd4283ce7ac67d78f416a870e941c66fd7 Mon Sep 17 00:00:00 2001 From: k-matsuzawa Date: Mon, 7 Jun 2021 08:53:56 +0900 Subject: [PATCH 2/2] upgrade: update cfd-core version --- local_resource/external_project_local_setting.config | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/local_resource/external_project_local_setting.config b/local_resource/external_project_local_setting.config index 058f4ae0..ed851082 100644 --- a/local_resource/external_project_local_setting.config +++ b/local_resource/external_project_local_setting.config @@ -1,4 +1,4 @@ -CFD_VERSION=0.3.14 -CFDCORE_TARGET_VERSION=refs/tags/v0.3.11 +CFD_VERSION=0.3.15 +CFDCORE_TARGET_VERSION=refs/tags/v0.3.12 LIBWALLY_TARGET_VERSION=refs/tags/cfd-0.3.7 CFDCORE_TARGET_URL=cryptogarageinc/cfd-core.git