Skip to content

Commit

Permalink
Merge pull request #320 from ko-matsu/feature/get-txid-from-block
Browse files Browse the repository at this point in the history
feat: (C-API) get txid from block
  • Loading branch information
k-matsuzawa authored Jun 7, 2021
2 parents 5dd1662 + e19583c commit 65af1a5
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 4 deletions.
23 changes: 23 additions & 0 deletions include/cfdc/cfdcapi_block.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
4 changes: 2 additions & 2 deletions local_resource/external_project_local_setting.config
Original file line number Diff line number Diff line change
@@ -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
88 changes: 86 additions & 2 deletions src/capi/cfdcapi_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>{Txid(std::string(txid))});
auto proof = block_data->block->GetTxOutProof(Txid(txid));
*txout_proof = CreateString(proof.GetHex());
} else {
#ifndef CFD_DISABLE_ELEMENTS
Expand Down Expand Up @@ -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<CfdCapiBlockData*>(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<CfdCapiBlockData*>(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
13 changes: 13 additions & 0 deletions test/capi/test_cfdcapi_block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 65af1a5

Please sign in to comment.