From e6c6432cde47c650082d28786d21af6784dba2b8 Mon Sep 17 00:00:00 2001 From: lucasli <410567249@qq.com> Date: Wed, 14 Jun 2023 15:11:26 +0800 Subject: [PATCH 01/29] (tx): add tx function for transaction struct (#163) --- bcos-c-sdk/bcos_sdk_c_common.h | 66 +- bcos-c-sdk/bcos_sdk_c_uti_tx.cpp | 29 +- bcos-c-sdk/bcos_sdk_c_uti_tx.h | 18 +- bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp | 839 ++++++++++++++++++++++++ bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h | 171 +++++ cmake/Options.cmake | 3 + cmake/config.cmake | 4 +- sample/tx/CMakeLists.txt | 3 + sample/tx/tx_struct_test.c | 417 ++++++++++++ 9 files changed, 1520 insertions(+), 30 deletions(-) create mode 100644 bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp create mode 100644 bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h create mode 100644 sample/tx/tx_struct_test.c diff --git a/bcos-c-sdk/bcos_sdk_c_common.h b/bcos-c-sdk/bcos_sdk_c_common.h index 00a2aafc6..ac3c8467d 100644 --- a/bcos-c-sdk/bcos_sdk_c_common.h +++ b/bcos-c-sdk/bcos_sdk_c_common.h @@ -97,19 +97,18 @@ struct bcos_sdk_c_config struct bcos_sdk_c_sm_cert_config* sm_cert_config; }; -struct bcos_sdk_c_signature_result -{ - uint8_t r[32]; - uint8_t s[32]; - uint8_t v[512]; -}; - /** * @brief create bcos_sdk_c_config in default value * @return struct bcos_sdk_c_config* */ struct bcos_sdk_c_config* bcos_sdk_c_config_create_empty(); +/** + * @brief duplicate string + * @return char* + */ +char* my_strdup(const char* s); + /** * @brief create bcos_sdk_c_config * @return struct bcos_sdk_c_config* @@ -174,6 +173,59 @@ void bcos_sdk_c_free(void* p); //--------------- callback end------------ + +//--------------- transaction begin--------------------------------- + +/** + * @brief: signature result + * + */ +struct bcos_sdk_c_signature_result +{ + uint8_t r[32]; + uint8_t s[32]; + uint8_t v[512]; +}; + +struct bcos_sdk_c_bytes +{ + uint8_t* buffer; + uint32_t length; +}; + +/** + * @brief: transaction data + * + */ +struct bcos_sdk_c_transaction_data +{ + int32_t version; + int64_t block_limit; + char* chain_id; + char* group_id; + char* nonce; + char* to; + char* abi; + struct bcos_sdk_c_bytes* input; +}; + +/** + * @brief: transaction + * + */ +struct bcos_sdk_c_transaction +{ + struct bcos_sdk_c_transaction_data* transaction_data; + struct bcos_sdk_c_bytes* data_hash; + struct bcos_sdk_c_bytes* signature; + struct bcos_sdk_c_bytes* sender; + int64_t import_time; + int32_t attribute; + char* extra_data; +}; + +//--------------- transaction end--------------------------------- + #ifdef __cplusplus } #endif diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx.cpp b/bcos-c-sdk/bcos_sdk_c_uti_tx.cpp index cb19e3bbd..bb82bbe5c 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx.cpp +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx.cpp @@ -20,6 +20,7 @@ #include "bcos_sdk_c_uti_tx.h" #include "bcos_sdk_c_error.h" +#include #include #include #include @@ -33,6 +34,7 @@ using namespace bcos; using namespace bcos::cppsdk; using namespace bcos::cppsdk::utilities; + /** * @brief * @@ -92,8 +94,9 @@ void* bcos_sdk_create_transaction_data_with_json(const char* json) catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_data") << LOG_DESC("exception") - << LOG_KV("json", json) << LOG_KV("error", errorMsg); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_data_with_json") + << LOG_DESC("exception") << LOG_KV("json", json) + << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); } @@ -316,44 +319,44 @@ void bcos_sdk_create_signed_transaction_ver_extra_data(void* key_pair, const cha * @brief * * @param transaction_data - * @param signed_transaction_data + * @param signature * @param transaction_data_hash * @param attribute * @return const char* */ const char* bcos_sdk_create_signed_transaction_with_signed_data(void* transaction_data, - const char* signed_transaction_data, const char* transaction_data_hash, int32_t attribute) + const char* signature, const char* transaction_data_hash, int32_t attribute) { return bcos_sdk_create_signed_transaction_with_signed_data_ver_extra_data( - transaction_data, signed_transaction_data, transaction_data_hash, attribute, NULL); + transaction_data, signature, transaction_data_hash, attribute, NULL); } /** * @brief * * @param transaction_data - * @param signed_transaction_data + * @param signature * @param transaction_data_hash * @param attribute * @param extra_data * @return const char* */ const char* bcos_sdk_create_signed_transaction_with_signed_data_ver_extra_data( - void* transaction_data, const char* signed_transaction_data, const char* transaction_data_hash, + void* transaction_data, const char* signature, const char* transaction_data_hash, int32_t attribute, const char* extra_data) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); - BCOS_SDK_C_PARAMS_VERIFICATION(signed_transaction_data, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(signature, NULL); BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data_hash, NULL); try { auto builder = std::make_shared(); - auto signedBytes = builder->createSignedTransaction( - *(bcostars::TransactionData*)transaction_data, *fromHexString(signed_transaction_data), - bcos::crypto::HashType(transaction_data_hash), attribute, - extra_data ? std::string(extra_data) : std::string()); + auto signedBytes = + builder->createSignedTransaction(*(bcostars::TransactionData*)transaction_data, + *fromHexString(signature), bcos::crypto::HashType(transaction_data_hash), attribute, + extra_data ? std::string(extra_data) : std::string()); return strdup(bcos::toHexStringWithPrefix(*signedBytes).c_str()); } catch (const std::exception& e) @@ -361,7 +364,7 @@ const char* bcos_sdk_create_signed_transaction_with_signed_data_ver_extra_data( std::string errorMsg = boost::diagnostic_information(e); BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_signed_transaction_with_signed_data_ver_extra_data") - << LOG_DESC("exception") << LOG_KV("signed_transaction_data", signed_transaction_data) + << LOG_DESC("exception") << LOG_KV("signature", signature) << LOG_KV("transaction_data_hash", transaction_data_hash) << LOG_KV("attribute", attribute) << LOG_KV("extra_data", extra_data) << LOG_KV("error", errorMsg); diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx.h b/bcos-c-sdk/bcos_sdk_c_uti_tx.h index a721ae6c6..d86372716 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx.h +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx.h @@ -81,8 +81,8 @@ const char* bcos_sdk_decode_transaction_data(const char* transaction_bytes); /** * @brief * - * @param crypto_type - * @param transaction_data + * @param crypto_type: int + * @param transaction_data: void* * @return const char* */ const char* bcos_sdk_calc_transaction_data_hash(int crypto_type, void* transaction_data); @@ -109,6 +109,7 @@ const char* bcos_sdk_sign_transaction_data_hash(void* keypair, const char* trans * @param attribute * @param tx_hash * @param signed_tx + * @return void* */ void bcos_sdk_create_signed_transaction(void* key_pair, const char* group_id, const char* chain_id, const char* to, const char* data, const char* abi, int64_t block_limit, int32_t attribute, @@ -128,6 +129,7 @@ void bcos_sdk_create_signed_transaction(void* key_pair, const char* group_id, co * @param extra_data * @param tx_hash * @param signed_tx + * @return void */ void bcos_sdk_create_signed_transaction_ver_extra_data(void* key_pair, const char* group_id, const char* chain_id, const char* to, const char* data, const char* abi, int64_t block_limit, @@ -136,27 +138,27 @@ void bcos_sdk_create_signed_transaction_ver_extra_data(void* key_pair, const cha /** * @brief * - * @param transaction_data - * @param signed_transaction_data + * @param transaction_data: void* + * @param signature * @param transaction_data_hash * @param attribute * @return const char* */ const char* bcos_sdk_create_signed_transaction_with_signed_data(void* transaction_data, - const char* signed_transaction_data, const char* transaction_data_hash, int32_t attribute); + const char* signature, const char* transaction_data_hash, int32_t attribute); /** * @brief * - * @param transaction_data - * @param signed_transaction_data + * @param transaction_data: void* + * @param signature * @param transaction_data_hash * @param attribute * @param extra_data * @return const char* */ const char* bcos_sdk_create_signed_transaction_with_signed_data_ver_extra_data( - void* transaction_data, const char* signed_transaction_data, const char* transaction_data_hash, + void* transaction_data, const char* signature, const char* transaction_data_hash, int32_t attribute, const char* extra_data); /** diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp new file mode 100644 index 000000000..150c50393 --- /dev/null +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp @@ -0,0 +1,839 @@ +/* + * Copyright (C) 2021 FISCO BCOS. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @file bcos_sdk_c_util_tx_struct.cpp + * @author: lucasli + * @date 2023-05-09 + */ + +#include "bcos_sdk_c_uti_tx_struct.h" +#include "bcos_sdk_c_error.h" +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace bcos; +using namespace bcos::cppsdk; +using namespace bcos::cppsdk::utilities; + +struct bcos_sdk_c_bytes* create_bytes_struct(uint32_t field_size, const char* field_data) +{ + bcos_sdk_clear_last_error(); + if (field_size <= 0) + { + return NULL; + } + + try + { + struct bcos_sdk_c_bytes* field_bytes = + (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); + uint32_t length = field_size; + uint8_t* buffer = (uint8_t*)malloc(length); + memcpy(buffer, field_data, length); + field_bytes->buffer = buffer; + field_bytes->length = length; + + return field_bytes; + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("create_bytes_struct") << LOG_DESC("exception") + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +struct bcos_sdk_c_bytes* bytes_struct_copy(const struct bcos_sdk_c_bytes* bytes_struct_src) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(bytes_struct_src, NULL); + + try + { + struct bcos_sdk_c_bytes* bytes_struct = + (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); + + uint32_t length = bytes_struct_src->length; + uint8_t* buffer = (uint8_t*)malloc(length); + memcpy(buffer, bytes_struct_src->buffer, length); + bytes_struct->buffer = buffer; + bytes_struct->length = length; + + return bytes_struct; + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bytes_struct_copy") << LOG_DESC("exception") + << LOG_KV("bytes_struct_src", bytes_struct_src) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +struct bcos_sdk_c_transaction_data* transaction_data_copy( + const bcos_sdk_c_transaction_data* tx_data_src) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(tx_data_src, NULL); + + try + { + struct bcos_sdk_c_transaction_data* transaction_data_struct = + (struct bcos_sdk_c_transaction_data*)malloc(sizeof(struct bcos_sdk_c_transaction_data)); + transaction_data_struct->version = tx_data_src->version; + transaction_data_struct->block_limit = tx_data_src->block_limit; + transaction_data_struct->chain_id = my_strdup(tx_data_src->chain_id); + transaction_data_struct->group_id = my_strdup(tx_data_src->group_id); + transaction_data_struct->nonce = my_strdup(tx_data_src->nonce); + transaction_data_struct->to = my_strdup(tx_data_src->to); + transaction_data_struct->abi = my_strdup(tx_data_src->abi); + transaction_data_struct->input = bytes_struct_copy(tx_data_src->input); + + return transaction_data_struct; + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("transaction_data_copy") << LOG_DESC("exception") + << LOG_KV("tx_data_src", tx_data_src) << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +/** + * @brief: convert tars transaction data to struct bcos_sdk_c_transaction_data + * + * @param tars_transaction_data: response error, pointer to Error::Ptr in cpp-sdk + * @return bcos_sdk_c_transaction_data*: struct bcos_sdk_c_transaction_data pointer + */ +struct bcos_sdk_c_transaction_data* convert_tars_transaction_data_to_struct( + bcostars::TransactionDataUniquePtr tars_transaction_data) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(tars_transaction_data, NULL); + + try + { + struct bcos_sdk_c_transaction_data* transaction_data_struct = + (struct bcos_sdk_c_transaction_data*)malloc(sizeof(struct bcos_sdk_c_transaction_data)); + struct bcos_sdk_c_bytes* input_bytes = create_bytes_struct( + tars_transaction_data->input.size(), tars_transaction_data->input.data()); + + transaction_data_struct->input = input_bytes; + transaction_data_struct->version = tars_transaction_data->version; + transaction_data_struct->block_limit = tars_transaction_data->blockLimit; + transaction_data_struct->chain_id = my_strdup(tars_transaction_data->chainID.data()); + transaction_data_struct->group_id = my_strdup(tars_transaction_data->groupID.data()); + transaction_data_struct->nonce = my_strdup(tars_transaction_data->nonce.data()); + transaction_data_struct->to = my_strdup(tars_transaction_data->to.data()); + transaction_data_struct->abi = my_strdup(tars_transaction_data->abi.data()); + + return transaction_data_struct; + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("convert_tars_transaction_data_to_struct") + << LOG_DESC("exception") + << LOG_KV("transaction data", tars_transaction_data) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +bcostars::TransactionDataUniquePtr convert_transaction_data_to_tars( + struct bcos_sdk_c_transaction_data* transaction_data) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); + + try + { + auto tars_transaction_data = std::make_unique(); + std::vector input; + for (size_t i = 0; i < transaction_data->input->length; ++i) + { + input.push_back(transaction_data->input->buffer[i]); + } + tars_transaction_data->input = std::move(input); + tars_transaction_data->version = (tars::Int32)transaction_data->version; + tars_transaction_data->blockLimit = (tars::Int64)transaction_data->block_limit; + tars_transaction_data->chainID = std::string(transaction_data->chain_id); + tars_transaction_data->groupID = std::string(transaction_data->group_id); + tars_transaction_data->nonce = std::string(transaction_data->nonce); + tars_transaction_data->to = std::string(transaction_data->to); + tars_transaction_data->abi = std::string(transaction_data->abi); + tars_transaction_data->groupID = std::string(transaction_data->group_id); + + return tars_transaction_data; + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("convert_transaction_data_to_tars") << LOG_DESC("exception") + << LOG_KV("transaction data", transaction_data) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +bcostars::TransactionUniquePtr convert_transaction_to_tars( + struct bcos_sdk_c_transaction* transaction) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction, NULL); + + try + { + auto tars_transaction = std::make_unique(); + auto TransactionDataUniquePtr = + convert_transaction_data_to_tars(transaction->transaction_data); + tars_transaction->data = *TransactionDataUniquePtr; + std::vector data_hash_vec, signature_vec, sender_vec; + if (transaction->data_hash) + { + for (size_t i = 0; i < transaction->data_hash->length; ++i) + { + data_hash_vec.push_back(transaction->data_hash->buffer[i]); + } + } + if (transaction->signature) + { + for (size_t i = 0; i < transaction->signature->length; ++i) + { + signature_vec.push_back(transaction->signature->buffer[i]); + } + } + if (transaction->sender) + { + for (size_t i = 0; i < transaction->sender->length; ++i) + { + sender_vec.push_back(transaction->sender->buffer[i]); + } + } + tars_transaction->dataHash = data_hash_vec; + tars_transaction->signature = signature_vec; + tars_transaction->sender = sender_vec; + tars_transaction->importTime = (tars::Int32)transaction->import_time; + tars_transaction->attribute = (tars::Int64)transaction->attribute; + tars_transaction->extraData = std::string(transaction->extra_data); + + return tars_transaction; + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("convert_transaction_to_tars") << LOG_DESC("exception") + << LOG_KV("transaction", transaction) << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +struct bcos_sdk_c_transaction* convert_tars_transaction_to_struct( + bcostars::TransactionUniquePtr tars_transaction) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(tars_transaction, NULL); + + try + { + struct bcos_sdk_c_transaction* transaction_struct = + (struct bcos_sdk_c_transaction*)malloc(sizeof(struct bcos_sdk_c_transaction)); + struct bcos_sdk_c_bytes* data_hash_bytes = create_bytes_struct( + tars_transaction->dataHash.size(), tars_transaction->dataHash.data()); + struct bcos_sdk_c_bytes* signature_bytes = create_bytes_struct( + tars_transaction->signature.size(), tars_transaction->signature.data()); + struct bcos_sdk_c_bytes* sender_bytes = + create_bytes_struct(tars_transaction->sender.size(), tars_transaction->sender.data()); + auto transactionDataUniquePtr = + std::make_unique(tars_transaction->data); + + transaction_struct->transaction_data = + convert_tars_transaction_data_to_struct(std::move(transactionDataUniquePtr)); + transaction_struct->data_hash = data_hash_bytes; + transaction_struct->signature = signature_bytes; + transaction_struct->sender = sender_bytes; + transaction_struct->import_time = tars_transaction->importTime; + transaction_struct->attribute = tars_transaction->attribute; + transaction_struct->extra_data = my_strdup(tars_transaction->extraData.data()); + return transaction_struct; + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("convert_tars_transaction_to_struct") + << LOG_DESC("exception") << LOG_KV("transaction", tars_transaction) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +/** + * @brief + * + * @param group_id + * @param chain_id + * @param to + * @param data + * @param abi + * @param block_limit + * @return bcos_sdk_c_transaction_data*: transaction data struct pointer, return unassigned struct + * on failure according to the function called bcos_sdk_get_last_error(if create failed, return -1) + */ +struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct(const char* group_id, + const char* chain_id, const char* to, const char* data, const char* abi, int64_t block_limit) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(group_id, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(chain_id, NULL); + // BCOS_SDK_C_PARAMS_VERIFICATION(to, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(data, NULL); + // BCOS_SDK_C_PARAMS_VERIFICATION(abi, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must >= 0", NULL); + + try + { + struct bcos_sdk_c_transaction_data* transaction_data_struct = + (struct bcos_sdk_c_transaction_data*)malloc(sizeof(struct bcos_sdk_c_transaction_data)); + auto bytesData = fromHexString(data); + std::string toStr = to ? to : ""; + std::string abiStr = abi ? abi : ""; + TransactionBuilder builder; + std::string nonceStr = builder.generateRandomStr(); + + transaction_data_struct->version = 0; + transaction_data_struct->block_limit = block_limit; + transaction_data_struct->group_id = my_strdup(group_id); + transaction_data_struct->chain_id = my_strdup(chain_id); + transaction_data_struct->to = my_strdup(toStr.data()); + transaction_data_struct->abi = my_strdup(abiStr.data()); + transaction_data_struct->nonce = my_strdup(nonceStr.data()); + transaction_data_struct->input = + create_bytes_struct(bytesData->size(), reinterpret_cast(bytesData->data())); + + return transaction_data_struct; + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_data_struct") + << LOG_DESC("exception") << LOG_KV("group_id", group_id) + << LOG_KV("chain_id", chain_id) << LOG_KV("to", std::string(to ? to : "")) + << LOG_KV("data", data) << LOG_KV("abi", std::string(abi ? abi : "")) + << LOG_KV("block_limit", block_limit) << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +/** + * @brief + * + * @param transaction_data: struct bcos_sdk_c_transaction_data* + */ +void bcos_sdk_destroy_transaction_data_struct(struct bcos_sdk_c_transaction_data* transaction_data) +{ + if (transaction_data == NULL) + { + return; + } + + if (transaction_data && transaction_data->chain_id) + { + bcos_sdk_c_free(transaction_data->chain_id); + } + + if (transaction_data && transaction_data->group_id) + { + bcos_sdk_c_free(transaction_data->group_id); + } + + if (transaction_data && transaction_data->nonce) + { + bcos_sdk_c_free(transaction_data->nonce); + } + + if (transaction_data && transaction_data->to) + { + bcos_sdk_c_free(transaction_data->to); + } + + if (transaction_data && transaction_data->abi) + { + bcos_sdk_c_free((void*)transaction_data->abi); + } + + if (transaction_data && transaction_data->input) + { + if (transaction_data->input->buffer) + { + bcos_sdk_c_free(transaction_data->input->buffer); + } + bcos_sdk_c_free((void*)transaction_data->input); + } + + bcos_sdk_c_free(transaction_data); +} + +/** + * @brief encode transaction data into hex format + * + * @param transaction_data: struct bcos_sdk_c_transaction_data* + * @return const char* + */ +const char* bcos_sdk_encode_transaction_data_struct( + struct bcos_sdk_c_transaction_data* transaction_data) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); + + try + { + auto tars_transaction_data = convert_transaction_data_to_tars(transaction_data); + TransactionBuilder builder; + auto transactionData = builder.encodeTransactionData(*tars_transaction_data); + auto hex_tx_data_str = toHexString(*transactionData); + return strdup(hex_tx_data_str->c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_data_struct") + << LOG_DESC("exception") << LOG_KV("transaction_data", transaction_data) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +/** + * @brief convert transaction data into json format + * + * @param transaction_data: struct bcos_sdk_c_transaction_data* + * @return const char* + */ +const char* bcos_sdk_encode_transaction_data_struct_to_json( + struct bcos_sdk_c_transaction_data* transaction_data) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); + + try + { + auto tars_transaction_data = convert_transaction_data_to_tars(transaction_data); + auto json_str = tars_transaction_data->writeToJsonString(); + return strdup(json_str.c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_data_struct_to_json") + << LOG_DESC("exception") << LOG_KV("transaction_data", transaction_data) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +/** + * @param transaction_data_hex_str + * @return struct bcos_sdk_c_transaction_data* + */ +struct bcos_sdk_c_transaction_data* bcos_sdk_decode_transaction_data_struct( + const char* transaction_data_hex_str) +{ + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data_hex_str, NULL); + try + { + TransactionBuilder builder; + auto tx_data_bytes = fromHexString(transaction_data_hex_str); + auto tars_tx_data = builder.decodeTransactionData(*tx_data_bytes); + return convert_tars_transaction_data_to_struct(std::move(tars_tx_data)); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_data_struct") + << LOG_DESC("exception") << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +/** + * @param transaction_data_json_str + * version:number + * groupID:string + * chainID:string + * to:string + * data:hex string + * abi:string + * blockLimit:number + * nonce:string + * @return struct bcos_sdk_c_transaction* + */ +struct bcos_sdk_c_transaction_data* bcos_sdk_decode_transaction_data_struct_with_json( + const char* transaction_data_json_str) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data_json_str, NULL); + + try + { + TransactionBuilder builder; + auto transactionData = + builder.createTransactionDataWithJson(std::string(transaction_data_json_str)); + + return convert_tars_transaction_data_to_struct(std::move(transactionData)); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_data_struct_with_json") + << LOG_DESC("exception") + << LOG_KV("transaction_data_json_str", transaction_data_json_str) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +/** + * @brief + * + * @param crypto_type: int + * @param transaction_data: struct bcos_sdk_c_transaction_data* + * @return const char* + */ +const char* bcos_sdk_calc_transaction_data_struct_hash( + int crypto_type, struct bcos_sdk_c_transaction_data* transaction_data) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (crypto_type == BCOS_C_SDK_ECDSA_TYPE || crypto_type == BCOS_C_SDK_SM_TYPE), + "invalid crypto type, it must be BCOS_C_SDK_ECDSA_TYPE(ecdsa crypto type) or " + "BCOS_C_SDK_SM_TYPE(sm crypto type)", + NULL); + + try + { + auto tars_transaction_data = convert_transaction_data_to_tars(transaction_data); + TransactionBuilder builder; + auto transactionDataHash = builder.calculateTransactionDataHash( + crypto_type == BCOS_C_SDK_ECDSA_TYPE ? CryptoType::Secp256K1 : CryptoType::SM2, + *tars_transaction_data); + return strdup(bcos::toHexStringWithPrefix(transactionDataHash).c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_calc_transaction_data_struct_hash") + << LOG_DESC("exception") << LOG_KV("crypto_type", crypto_type) + << LOG_KV("transaction_data", transaction_data) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +/** + * @brief + * + * @param transaction_data: struct bcos_sdk_c_transaction_data* + * @param signature + * @param transaction_data_hash + * @param attribute + * @return struct bcos_sdk_c_transaction* + */ +struct bcos_sdk_c_transaction* bcos_sdk_create_transaction_struct( + struct bcos_sdk_c_transaction_data* transaction_data, const char* signature, + const char* transaction_data_hash, int32_t attribute, const char* extra_data) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(signature, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data_hash, NULL); + + try + { + struct bcos_sdk_c_transaction* transaction_struct = + (struct bcos_sdk_c_transaction*)malloc(sizeof(struct bcos_sdk_c_transaction)); + transaction_struct->transaction_data = transaction_data_copy(transaction_data); + transaction_struct->data_hash = + create_bytes_struct(std::strlen(transaction_data_hash), transaction_data_hash); + transaction_struct->signature = create_bytes_struct(std::strlen(signature), signature); + transaction_struct->sender = NULL; + transaction_struct->import_time = 0; + transaction_struct->attribute = attribute; + transaction_struct->extra_data = my_strdup(extra_data); + + return transaction_struct; + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_struct") + << LOG_DESC("exception") << LOG_KV("signature", signature) + << LOG_KV("transaction_data_hash", transaction_data_hash) + << LOG_KV("attribute", attribute) << LOG_KV("extra_data", extra_data) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + return NULL; +} + +/** + * @brief + * + * @param transaction: struct bcos_sdk_c_transaction* + */ +void bcos_sdk_destroy_transaction_struct(struct bcos_sdk_c_transaction* transaction) +{ + if (transaction == NULL) + { + return; + } + + if (transaction && transaction->transaction_data) + { + bcos_sdk_destroy_transaction_data_struct(transaction->transaction_data); + } + + if (transaction && transaction->data_hash) + { + if (transaction->data_hash->buffer) + { + bcos_sdk_c_free(transaction->data_hash->buffer); + } + bcos_sdk_c_free(transaction->data_hash); + } + + if (transaction && transaction->signature) + { + if (transaction->signature->buffer) + { + bcos_sdk_c_free(transaction->signature->buffer); + } + bcos_sdk_c_free(transaction->signature); + } + + if (transaction && transaction->sender) + { + if (transaction->sender->buffer) + { + bcos_sdk_c_free(transaction->sender->buffer); + } + bcos_sdk_c_free(transaction->sender); + } + + if (transaction && transaction->extra_data) + { + bcos_sdk_c_free(transaction->extra_data); + } + + bcos_sdk_c_free(transaction); +} + +/** + * @brief + * + * @param transaction_data: struct bcos_sdk_c_transaction_data* + * @param signature + * @param transaction_data_hash + * @param attribute + * @return const char* + */ +const char* bcos_sdk_create_transaction(struct bcos_sdk_c_transaction_data* transaction_data, + const char* signature, const char* transaction_data_hash, int32_t attribute, + const char* extra_data) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(signature, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data_hash, NULL); + + try + { + TransactionBuilder builder; + auto tars_tx_data = convert_transaction_data_to_tars(transaction_data); + auto signedBytes = builder.createSignedTransaction(*tars_tx_data, *fromHexString(signature), + bcos::crypto::HashType(transaction_data_hash), attribute, + extra_data ? std::string(extra_data) : std::string()); + return strdup(bcos::toHexStringWithPrefix(*signedBytes).c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction") << LOG_DESC("exception") + << LOG_KV("signature", signature) + << LOG_KV("transaction_data_hash", transaction_data_hash) + << LOG_KV("attribute", attribute) << LOG_KV("extra_data", extra_data) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + return NULL; +} + + +/** + * @brief encode transaction into hex format + * + * @param transaction: struct bcos_sdk_c_transaction* + * @return const char* + */ +const char* bcos_sdk_encode_transaction_struct(struct bcos_sdk_c_transaction* transaction) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction, NULL); + + try + { + auto tars_transaction = convert_transaction_to_tars(transaction); + TransactionBuilder builder; + auto transaction = builder.encodeTransaction(*tars_transaction); + auto hex_tx_str = toHexString(*transaction); + return strdup(hex_tx_str->c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_struct") + << LOG_DESC("exception") << LOG_KV("transaction", transaction) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +/** + * @brief convert transaction into json format + * + * @param transaction: struct bcos_sdk_c_transaction* + * @return const char* + */ +const char* bcos_sdk_encode_transaction_struct_to_json(struct bcos_sdk_c_transaction* transaction) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction, NULL); + + try + { + auto tars_transaction = convert_transaction_to_tars(transaction); + auto json_str = tars_transaction->writeToJsonString(); + return strdup(json_str.c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_struct_to_json") + << LOG_DESC("exception") << LOG_KV("transaction", transaction) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +/** + * @brief + * + * @param transaction_hex_str + * @return struct bcos_sdk_c_transaction* + */ +struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct(const char* transaction_hex_str) +{ + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_hex_str, NULL); + + try + { + TransactionBuilder builder; + auto tx_bytes = fromHexString(transaction_hex_str); + auto tars_tx = builder.decodeTransaction(*tx_bytes); + return convert_tars_transaction_to_struct(std::move(tars_tx)); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_struct") + << LOG_DESC("exception") + << LOG_KV("transaction_hex_str", transaction_hex_str) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +/** + * @brief + * + * @param transaction_json_str + * @return struct bcos_sdk_c_transaction* + */ +struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct_with_json( + const char* transaction_json_str) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_json_str, NULL); + + try + { + TransactionBuilder builder; + auto transaction = builder.createTransactionWithJson(std::string(transaction_json_str)); + + return convert_tars_transaction_to_struct(std::move(transaction)); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_struct_with_json") + << LOG_DESC("exception") + << LOG_KV("transaction_json_str", transaction_json_str) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h new file mode 100644 index 000000000..fadc033a7 --- /dev/null +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h @@ -0,0 +1,171 @@ +/* + * Copyright (C) 2021 FISCO BCOS. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @file bcos_sdk_c_util_tx_struct.h + * @author: lucasli + * @date 2023-05-08 + */ + +#ifndef __INCLUDE_BCOS_SDK_C_UTIL_TX_STRUCT__ +#define __INCLUDE_BCOS_SDK_C_UTIL_TX_STRUCT__ + +#include "bcos_sdk_c_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * @brief + * + * @param group_id + * @param chain_id + * @param to + * @param data + * @param abi + * @param block_limit + * @return bcos_sdk_c_transaction_data*: transaction data struct pointer, return unassigned struct + * on failure according to the function called bcos_sdk_get_last_error(if create failed, return -1) + */ +struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct(const char* group_id, + const char* chain_id, const char* to, const char* data, const char* abi, int64_t block_limit); + +/** + * @brief + * + * @param transaction_data: struct bcos_sdk_c_transaction_data* + */ +void bcos_sdk_destroy_transaction_data_struct(struct bcos_sdk_c_transaction_data* transaction_data); + +/** + * @brief encode transaction data into hex format + * + * @param transaction_data: struct bcos_sdk_c_transaction_data* + * @return const char* + */ +const char* bcos_sdk_encode_transaction_data_struct( + struct bcos_sdk_c_transaction_data* transaction_data); + +/** + * @brief convert transaction data into json format + * + * @param transaction_data: struct bcos_sdk_c_transaction_data* + * @return const char* + */ +const char* bcos_sdk_encode_transaction_data_struct_to_json( + struct bcos_sdk_c_transaction_data* transaction_data); + +/** + * @param transaction_data_hex_str + * @return struct bcos_sdk_c_transaction_data* + */ +struct bcos_sdk_c_transaction_data* bcos_sdk_decode_transaction_data_struct( + const char* transaction_data_hex_str); + +/** + * @param transaction_data_json_str + * version:number + * groupID:string + * chainID:string + * to:string + * data:hex string + * abi:string + * blockLimit:number + * nonce:string + * @return struct bcos_sdk_c_transaction_data* + */ +struct bcos_sdk_c_transaction_data* bcos_sdk_decode_transaction_data_struct_with_json( + const char* transaction_data_json_str); + +/** + * @brief + * + * @param crypto_type: int + * @param transaction_data: struct bcos_sdk_c_transaction_data* + * @return const char* + */ +const char* bcos_sdk_calc_transaction_data_struct_hash( + int crypto_type, struct bcos_sdk_c_transaction_data* transaction_data); + +/** + * @brief + * + * @param transaction_data: struct bcos_sdk_c_transaction_data* + * @param signature + * @param transaction_data_hash + * @param attribute + * @return struct bcos_sdk_c_transaction* + */ +struct bcos_sdk_c_transaction* bcos_sdk_create_transaction_struct( + struct bcos_sdk_c_transaction_data* transaction_data, const char* signature, + const char* transaction_data_hash, int32_t attribute, const char* extra_data); + +/** + * @brief + * + * @param transaction: struct bcos_sdk_c_transaction* + */ +void bcos_sdk_destroy_transaction_struct(struct bcos_sdk_c_transaction* transaction); + +/** + * @brief + * + * @param transaction_data: struct bcos_sdk_c_transaction_data* + * @param signature + * @param transaction_data_hash + * @param attribute + * @return const char* + */ +const char* bcos_sdk_create_transaction(struct bcos_sdk_c_transaction_data* transaction_data, + const char* signature, const char* transaction_data_hash, int32_t attribute, + const char* extra_data); + +/** + * @brief encode transaction into hex format + * + * @param transaction: struct bcos_sdk_c_transaction* + * @return const char* + */ +const char* bcos_sdk_encode_transaction_struct(struct bcos_sdk_c_transaction* transaction); + +/** + * @brief convert transaction into json format + * + * @param transaction: struct bcos_sdk_c_transaction* + * @return const char* + */ +const char* bcos_sdk_encode_transaction_struct_to_json(struct bcos_sdk_c_transaction* transaction); + +/** + * @brief + * + * @param transaction_hex_str + * @return struct bcos_sdk_c_transaction* + */ +struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct(const char* transaction_hex_str); + +/** + * @brief + * + * @param transaction_json_str + * @return struct bcos_sdk_c_transaction* + */ +struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct_with_json( + const char* transaction_json_str); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/cmake/Options.cmake b/cmake/Options.cmake index e56cf625a..1adaa21a9 100644 --- a/cmake/Options.cmake +++ b/cmake/Options.cmake @@ -56,6 +56,8 @@ macro(configure_project) set(MARCH_TYPE "-march=native -mtune=native") endif() + # build jni + default_option(BUILD_JNI OFF) # unit tests default_option(TESTS OFF) # code coverage @@ -84,6 +86,7 @@ macro(print_config NAME) message("-- CMAKE_BUILD_TYPE Build type ${CMAKE_BUILD_TYPE}") message("-- TARGET_PLATFORM Target platform ${CMAKE_SYSTEM_NAME} ${ARCHITECTURE}") message("-- BUILD_STATIC Build static ${BUILD_STATIC}") + message("-- BUILD_JNI Build jni ${BUILD_JNI}") message("-- COVERAGE Build code coverage ${COVERAGE}") message("-- TESTS Build tests ${TESTS}") message("-- ARCH_NATIVE Enable native code ${ARCH_NATIVE}") diff --git a/cmake/config.cmake b/cmake/config.cmake index 8ee5868d1..fb21ae64f 100644 --- a/cmake/config.cmake +++ b/cmake/config.cmake @@ -51,8 +51,8 @@ hunter_config(bcos-boostssl hunter_config(bcos-cpp-sdk VERSION 3.3.0-local - URL https://${URL_BASE}/FISCO-BCOS/bcos-cpp-sdk/archive/d47a751fe5f840fd22b38bcf9bedd61e1b1ad478.tar.gz - SHA1 debd13410b98474dc0e4d55b7b08d233999fa0e5 + URL https://${URL_BASE}/FISCO-BCOS/bcos-cpp-sdk/archive/0bb3761ae0ec02548981e78567d6ac8d2bc676c8.tar.gz + SHA1 d598a834d440df1bcfb309ce3b42069e87fe3bbb ) hunter_config(range-v3 VERSION 1.0.0 diff --git a/sample/tx/CMakeLists.txt b/sample/tx/CMakeLists.txt index f3e389f2c..4b6e1352f 100644 --- a/sample/tx/CMakeLists.txt +++ b/sample/tx/CMakeLists.txt @@ -4,5 +4,8 @@ target_link_libraries(hello_sample PUBLIC ${BCOS_C_SDK_STATIC_TARGET}) add_executable(hello_sample_hsm hello_sample_hsm.c) target_link_libraries(hello_sample_hsm PUBLIC ${BCOS_C_SDK_STATIC_TARGET}) +add_executable(tx_struct_test tx_struct_test.c) +target_link_libraries(tx_struct_test PUBLIC ${BCOS_C_SDK_STATIC_TARGET}) + # add_executable(tx_sign_perf tx_sign_perf.cpp) # target_link_libraries(tx_sign_perf PUBLIC ${BCOS_C_SDK_STATIC_TARGET}) \ No newline at end of file diff --git a/sample/tx/tx_struct_test.c b/sample/tx/tx_struct_test.c new file mode 100644 index 000000000..b2dd3b6b3 --- /dev/null +++ b/sample/tx/tx_struct_test.c @@ -0,0 +1,417 @@ +/* + * Copyright (C) 2021 FISCO BCOS. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @file tx_struct_test.c + * @author: lucasli + * @date 2023-04-25 + */ +#include "bcos-c-sdk/bcos_sdk_c_error.h" +#include "bcos-c-sdk/bcos_sdk_c_rpc.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_abi.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_keypair.h" +#include +#include +#include +#include +#include +#include + +#ifdef _WIN32 +#include +#else +#include +#endif + +//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ + +// HelloWorld Source Code: + +// HelloWorld Source Code: +/** +pragma solidity>=0.4.24 <0.6.11; + +contract HelloWorld { + string name; + + constructor() public { + name = "Hello, World!"; + } + + function get() public view returns (string memory) { + return name; + } + + function set(string memory n) public { + name = n; + } +} +*/ +const char* g_hw_bin = + "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f72" + "6c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50" + "610107565b828054600181600116156101000203166002900490600052602060002090601f01602090048101928260" + "1f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100" + "d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b8082" + "11156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe60806040" + "5234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c1461" + "00f6575b600080fd5b6100f46004803603602081101561005157600080fd5b81019080803590602001906401000000" + "0081111561006e57600080fd5b82018360208201111561008057600080fd5b80359060200191846001830284011164" + "0100000000831117156100a257600080fd5b91908080601f0160208091040260200160405190810160405280939291" + "90818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050" + "50610179565b005b6100fe610193565b60405180806020018281038252838181518152602001915080519060200190" + "80838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f1680" + "1561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b8060" + "00908051906020019061018f929190610235565b5050565b6060600080546001816001161561010002031660029004" + "80601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203" + "1660029004801561022b5780601f106102005761010080835404028352916020019161022b565b8201919060005260" + "20600020905b81548152906001019060200180831161020e57829003601f168201915b5050505050905090565b8280" + "54600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102765780" + "5160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591" + "602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760" + "008160009055506001016102bb565b5090565b9056fea2646970667358221220b5943f43c48cc93c6d71cdcf27aee5" + "072566c88755ce9186e32ce83b24e8dc6c64736f6c634300060a0033"; + +const char* g_hw_sm_bin = + "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f72" + "6c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50" + "610107565b828054600181600116156101000203166002900490600052602060002090601f01602090048101928260" + "1f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100" + "d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b8082" + "11156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe60806040" + "5234801561001057600080fd5b50600436106100365760003560e01c8063299f7f9d1461003b5780633590b49f1461" + "00be575b600080fd5b610043610179565b604051808060200182810382528381815181526020019150805190602001" + "9080838360005b83811015610083578082015181840152602081019050610068565b50505050905090810190601f16" + "80156100b05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b61" + "0177600480360360208110156100d457600080fd5b81019080803590602001906401000000008111156100f1576000" + "80fd5b82018360208201111561010357600080fd5b8035906020019184600183028401116401000000008311171561" + "012557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380" + "828437600081840152601f19601f82011690508083019250505050505050919291929050505061021b565b005b6060" + "60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190" + "818152602001828054600181600116156101000203166002900480156102115780601f106101e65761010080835404" + "0283529160200191610211565b820191906000526020600020905b8154815290600101906020018083116101f45782" + "9003601f168201915b5050505050905090565b8060009080519060200190610231929190610235565b5050565b8280" + "54600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102765780" + "5160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591" + "602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760" + "008160009055506001016102bb565b5090565b9056fea26469706673582212209871cb2bcf390d53645807cbaedfe0" + "52d739ef9cff9d84787f74c4f379e1854664736f6c634300060a0033"; + +const char* g_hw_abi = + "[{\"inputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"inputs\":[]," + "\"name\":\"get\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}]" + ",\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":" + "\"string\",\"name\":\"n\",\"type\":\"string\"}],\"name\":\"set\",\"outputs\":[]," + "\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]"; + +/* +{ + "6d4ce63c": "get()", + "4ed3885e": "set(string)" +} + +{ + "299f7f9d": "get()", + "3590b49f": "set(string)" +} +*/ + +void usage() +{ + printf("Desc: tx struct test sample\n"); + printf("Usage: tx_struct_test \n"); + printf("Example:\n"); + printf(" ./tx_struct_test ./config_sample.ini group0\n"); + exit(0); +} + +// contract address +char* contract_address = NULL; + +// callback for rpc interfaces +void on_deploy_resp_callback(struct bcos_sdk_c_struct_response* resp) +{ + if (resp->error != BCOS_SDK_C_SUCCESS) + { + printf("\t deploy contract failed, error: %d, message: %s\n", resp->error, resp->desc); + exit(-1); + } + + const char* cflag = "contractAddress\" : \""; + // find the "contractAddress": "0xxxxx" + char* p0 = strstr((char*)resp->data, cflag); + if (p0 == NULL) + { + printf("\t cannot find the \"contractAddress\" filed, resp: %s\n", (char*)resp->data); + exit(-1); + } + + char* p1 = (char*)p0 + strlen(cflag); + char* p2 = strstr(p1, "\""); + if (p2 == NULL) + { + printf("\t cannot find the \"contractAddress\" filed, resp: %s\n", (char*)resp->data); + exit(-1); + } + + contract_address = (char*)malloc(p2 - p1 + 1); + + memcpy(contract_address, p1, p2 - p1); + contract_address[p2 - p1] = '\0'; + + printf(" [TxStructTest] contractAddress ===>>>>: %s\n", contract_address); + printf(" [TxStructTest] transaction receipt ===>>>>: %s\n", (char*)resp->data); +} + +/* resp->data 的数据结构 +{ + "id" : 2, + "jsonrpc" : "2.0", + "result" : + { + "blockNumber" : 113, + "checksumContractAddress" : "", + "contractAddress" : "", + "extraData" : "ExtraData", + "from" : "0x69df04bec1c36551be6298f7e4c2f867592a4b37", + "gasUsed" : "13063", + "hash" : "0x7d816bbde4aef3bd4c084b0887982f2c50cb9a50975a4d07328ec5fd5dd4e6e6", + "input" : "0x3590b49f0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001748656c6c6f20464953434f2d42434f5320332e30212121000000000000000000", + "logEntries" : [], + "message" : "", + "output" : "0x", + "status" : 0, + "to" : "0xcd6787f79a8da1b4a607dd59c79c7c08209230c1", + "transactionHash" : "0x05424cb9f92e1f1cd9b7ebe7a7e86d628d71c67edc6cf9cadac1967d279f8017", + "version" : 0 + } +} +*/ +void on_send_tx_resp_callback(struct bcos_sdk_c_struct_response* resp) +{ + if (resp->error != BCOS_SDK_C_SUCCESS) + { + printf("\t send tx failed, error: %d, message: %s\n", resp->error, resp->desc); + exit(-1); + } + + printf(" ===>> send tx resp: %s\n", (char*)resp->data); +} + +void on_call_resp_callback(struct bcos_sdk_c_struct_response* resp) +{ + if (resp->error != BCOS_SDK_C_SUCCESS) + { + printf("\t call failed, error: %d, message: %s\n", resp->error, resp->desc); + exit(-1); + } + + printf(" ===>> call resp: %s\n", (char*)resp->data); +} + +int main(int argc, char** argv) +{ + if (argc < 3) + { + usage(); + } + + const char* config = argv[1]; + const char* group_id = argv[2]; + + printf("[TxStructTest] params ===>>>> \n"); + printf("\t # config: %s\n", config); + printf("\t # group_id: %s\n", group_id); + + // const char* version = bcos_sdk_version(); + // printf("\t # c-sdk version: \n%s", version); + // bcos_sdk_c_free((void*)version); + + // 1. create sdk object by config + void* sdk = bcos_sdk_create_by_config_file(config); + // check success or not + if (!bcos_sdk_is_last_opr_success()) + { + printf( + " bcos_sdk_create_by_config_file failed, error: %s\n", bcos_sdk_get_last_error_msg()); + exit(-1); + } + + printf(" [TxStructTest] start sdk ... \n"); + + // 2. start bcos c sdk + bcos_sdk_start(sdk); + if (!bcos_sdk_is_last_opr_success()) + { + printf(" [TxStructTest] bcos_sdk_start failed, error: %s\n", bcos_sdk_get_last_error_msg()); + exit(-1); + } + + int sm_crypto = 0; + int wasm = 0; + + // 3. get sm_crypto of the group_id + bcos_sdk_get_group_wasm_and_crypto(sdk, group_id, &wasm, &sm_crypto); + if (!bcos_sdk_is_last_opr_success()) + { + printf(" [CallHello] bcos_sdk_group_sm_crypto failed, error: %s\n", + bcos_sdk_get_last_error_msg()); + exit(-1); + } + + printf(" [TxStructTest] sm crypto: %d\n", sm_crypto); + // 4. get chain_id of the group_id + const char* chain_id = bcos_sdk_get_group_chain_id(sdk, group_id); + if (!bcos_sdk_is_last_opr_success()) + { + printf(" [TxStructTest] bcos_sdk_get_group_chain_id failed, error: %s\n", + bcos_sdk_get_last_error_msg()); + exit(-1); + } + + printf(" [TxStructTest] chain id: %s\n", chain_id); + // 5. get blocklimit of the group_id + int64_t block_limit = bcos_rpc_get_block_limit(sdk, group_id); + if (block_limit < 0) + { + printf(" [TxStructTest] group not exist, group: %s\n", group_id); + exit(-1); + } + + printf(" [TxStructTest] block limit: %d\n", (int32_t)block_limit); + // 6. load or create keypair for transaction sign + void* key_pair = bcos_sdk_create_keypair(sm_crypto); + if (!key_pair) + { + printf(" [TxStructTest] create keypair failed, error: %s\n", bcos_sdk_get_last_error_msg()); + exit(-1); + } + + // printf(" [TxStructTest] bcos_sdk_get_keypair_type: %d\n", + // bcos_sdk_get_keypair_type(key_pair)); + + // 7. get account address of the keypair + const char* address = bcos_sdk_get_keypair_address(key_pair); + printf(" [TxStructTest] new account, address: %s\n", address); + + char* tx_hash = NULL; + char* signed_tx = NULL; + const char* extra_data = "ExtraData"; + + printf(" [TxStructTest] extra_data: %s\n", extra_data); + // 8. deploy HelloWorld contract + // 8.1 create signed transaction + bcos_sdk_create_signed_transaction_ver_extra_data(key_pair, group_id, chain_id, "", + sm_crypto ? g_hw_sm_bin : g_hw_bin, "", block_limit, 0, extra_data, &tx_hash, &signed_tx); + + printf(" [TxStructTest] create deploy contract transaction success, tx_hash: %s\n", tx_hash); + // 8.2 call rpc interface, send transaction + bcos_rpc_send_transaction(sdk, group_id, "", signed_tx, 0, on_deploy_resp_callback, NULL); + + // wait for async operation done, just for sample + sleep(5); + + printf(" [TxStructTest] set operation\n"); + // 9. HelloWorld set + // 9.1 abi encode params + const char* set_data = + bcos_sdk_abi_encode_method(g_hw_abi, "set", "[\"Hello FISCO-BCOS 3.0!!!\"]", sm_crypto); + // 9.2 create signed transaction + { + // 9.2.1 create transaction data + struct bcos_sdk_c_transaction_data* transaction_data = bcos_sdk_create_transaction_data_struct( + group_id, chain_id, contract_address, set_data, g_hw_abi, block_limit); + + // 9.2.1.1 encode tx data to hex + const char* hex_tx_data = bcos_sdk_encode_transaction_data_struct(transaction_data); + printf(" [TxStructTest] tx_data_hex: %s\n", hex_tx_data); + // 9.2.1.2 decode hex tx data + struct bcos_sdk_c_transaction_data* decode_tx_data = bcos_sdk_decode_transaction_data_struct(hex_tx_data); + // 9.2.1.3 encode tx data to json + const char* json_tx_data = bcos_sdk_encode_transaction_data_struct_to_json(transaction_data); + printf(" [TxStructTest] tx_data_json: %s\n", json_tx_data); + // 9.2.1.4 decode json to tx data struct + decode_tx_data = bcos_sdk_decode_transaction_data_struct_with_json(json_tx_data); + + // 9.2.2 calc transaction data hash + const char* transaction_data_hash = + bcos_sdk_calc_transaction_data_struct_hash(sm_crypto, decode_tx_data); + printf(" [TxStructTest] set tx hash: %s\n", transaction_data_hash); + // 9.2.3 sign transaction hash + const char* signed_hash = + bcos_sdk_sign_transaction_data_hash(key_pair, transaction_data_hash); + + // 9.2.4 create signed transaction + const char* signed_tx = bcos_sdk_create_transaction( + decode_tx_data, signed_hash, transaction_data_hash, 0, extra_data); + + // 9.2.4.1 create transaction struct + struct bcos_sdk_c_transaction* transaction = bcos_sdk_create_transaction_struct(decode_tx_data, + signed_hash, transaction_data_hash, 0, extra_data); + // 9.2.4.2 encode tx to hex + const char* hex_tx = bcos_sdk_encode_transaction_struct(transaction); + printf(" [TxStructTest] tx_hex: %s\n", hex_tx); + // 9.2.4.3 decode hex to tx + struct bcos_sdk_c_transaction* decode_tx = bcos_sdk_decode_transaction_struct(hex_tx); + // 9.2.4.4 encode tx to json + const char* json_tx = bcos_sdk_encode_transaction_struct_to_json(transaction); + printf(" [TxStructTest] tx_json: %s\n", json_tx); + // 9.2.4.5 decode json to tx + decode_tx = bcos_sdk_decode_transaction_struct_with_json(json_tx); + + // 9.3 call rpc interface, sendTransaction + bcos_rpc_send_transaction(sdk, group_id, "", signed_tx, 0, on_send_tx_resp_callback, NULL); + + // wait for async operation done, just for sample + sleep(3); + + bcos_sdk_destroy_transaction_data_struct(transaction_data); + bcos_sdk_destroy_transaction_data_struct(decode_tx_data); + bcos_sdk_destroy_transaction_struct(transaction); + bcos_sdk_destroy_transaction_struct(decode_tx); + bcos_sdk_c_free((void*)transaction_data_hash); + bcos_sdk_c_free((void*)signed_hash); + bcos_sdk_c_free((void*)signed_tx); + bcos_sdk_c_free((void*)hex_tx_data); + bcos_sdk_c_free((void*)json_tx_data); + bcos_sdk_c_free((void*)hex_tx); + bcos_sdk_c_free((void*)json_tx); + } + + // wait for async operation done, just for sample + sleep(3); + + // free chain_id + bcos_sdk_c_free((void*)chain_id); + // free tx_hash + bcos_sdk_c_free((void*)tx_hash); + // free signed_tx + bcos_sdk_c_free((void*)signed_tx); + // free address + bcos_sdk_c_free((void*)address); + if (contract_address) + { + bcos_sdk_c_free((void*)contract_address); + } + + // stop sdk + bcos_sdk_stop(sdk); + // release sdk + bcos_sdk_destroy(sdk); + // release keypair + bcos_sdk_destroy_keypair(key_pair); + + return 0; +} From bf1e8ca7c31c1abcefa35360a01f828edd02f6f6 Mon Sep 17 00:00:00 2001 From: lucasli <410567249@qq.com> Date: Sun, 25 Jun 2023 16:28:37 +0800 Subject: [PATCH 02/29] (tx): add tx struct api and UT in c-sdk / jni (#170) --- bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp | 87 ++- bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h | 30 +- bindings/java/jni/build.gradle | 2 +- ...ties_tx_TransactionStructBuilderJniObj.cpp | 554 ++++++++++++++++++ ...lities_tx_TransactionStructBuilderJniObj.h | 87 +++ .../sdk/jni/utilities/tx/Transaction.java | 67 +++ .../jni/utilities/tx/TransactionBytes.java | 22 + .../sdk/jni/utilities/tx/TransactionData.java | 76 +++ .../tx/TransactionStructBuilderJniObj.java | 74 +++ .../bcos/sdk/jni/test/tx/TestTxStruct.java | 212 +++++++ sample/tx/tx_struct_test.c | 56 +- 11 files changed, 1245 insertions(+), 22 deletions(-) create mode 100644 bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp create mode 100644 bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.h create mode 100644 bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/Transaction.java create mode 100644 bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes.java create mode 100644 bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionData.java create mode 100644 bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderJniObj.java create mode 100644 bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp index 150c50393..d3cbbcbd7 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp @@ -308,20 +308,21 @@ struct bcos_sdk_c_transaction* convert_tars_transaction_to_struct( * @param group_id * @param chain_id * @param to - * @param data + * @param input * @param abi * @param block_limit * @return bcos_sdk_c_transaction_data*: transaction data struct pointer, return unassigned struct * on failure according to the function called bcos_sdk_get_last_error(if create failed, return -1) */ -struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct(const char* group_id, - const char* chain_id, const char* to, const char* data, const char* abi, int64_t block_limit) +struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct_with_hex_input( + const char* group_id, const char* chain_id, const char* to, const char* input, const char* abi, + int64_t block_limit) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(group_id, NULL); BCOS_SDK_C_PARAMS_VERIFICATION(chain_id, NULL); // BCOS_SDK_C_PARAMS_VERIFICATION(to, NULL); - BCOS_SDK_C_PARAMS_VERIFICATION(data, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(input, NULL); // BCOS_SDK_C_PARAMS_VERIFICATION(abi, NULL); BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must >= 0", NULL); @@ -329,7 +330,7 @@ struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct(cons { struct bcos_sdk_c_transaction_data* transaction_data_struct = (struct bcos_sdk_c_transaction_data*)malloc(sizeof(struct bcos_sdk_c_transaction_data)); - auto bytesData = fromHexString(data); + auto bytesInput = fromHexString(input); std::string toStr = to ? to : ""; std::string abiStr = abi ? abi : ""; TransactionBuilder builder; @@ -343,7 +344,7 @@ struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct(cons transaction_data_struct->abi = my_strdup(abiStr.data()); transaction_data_struct->nonce = my_strdup(nonceStr.data()); transaction_data_struct->input = - create_bytes_struct(bytesData->size(), reinterpret_cast(bytesData->data())); + create_bytes_struct(bytesInput->size(), reinterpret_cast(bytesInput->data())); return transaction_data_struct; } @@ -353,7 +354,69 @@ struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct(cons BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_data_struct") << LOG_DESC("exception") << LOG_KV("group_id", group_id) << LOG_KV("chain_id", chain_id) << LOG_KV("to", std::string(to ? to : "")) - << LOG_KV("data", data) << LOG_KV("abi", std::string(abi ? abi : "")) + << LOG_KV("input", input) << LOG_KV("abi", std::string(abi ? abi : "")) + << LOG_KV("block_limit", block_limit) << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +/** + * @brief + * + * @param group_id + * @param chain_id + * @param to + * @param bytes_input + * @param abi + * @param block_limit + * @return bcos_sdk_c_transaction_data*: transaction data struct pointer, return unassigned struct + * on failure according to the function called bcos_sdk_get_last_error(if create failed, return -1) + */ +struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct_with_bytes( + const char* group_id, const char* chain_id, const char* to, const unsigned char* bytes_input, + uint32_t bytes_input_length, const char* abi, int64_t block_limit) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(group_id, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(chain_id, NULL); + // BCOS_SDK_C_PARAMS_VERIFICATION(to, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(bytes_input, NULL); + // BCOS_SDK_C_PARAMS_VERIFICATION(abi, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (bytes_input_length > 0), "bytes input length must >= 0", NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must >= 0", NULL); + + try + { + struct bcos_sdk_c_transaction_data* transaction_data_struct = + (struct bcos_sdk_c_transaction_data*)malloc(sizeof(struct bcos_sdk_c_transaction_data)); + std::string toStr = to ? to : ""; + std::string abiStr = abi ? abi : ""; + TransactionBuilder builder; + std::string nonceStr = builder.generateRandomStr(); + + transaction_data_struct->version = 0; + transaction_data_struct->block_limit = block_limit; + transaction_data_struct->group_id = my_strdup(group_id); + transaction_data_struct->chain_id = my_strdup(chain_id); + transaction_data_struct->to = my_strdup(toStr.data()); + transaction_data_struct->abi = my_strdup(abiStr.data()); + transaction_data_struct->nonce = my_strdup(nonceStr.data()); + transaction_data_struct->input = create_bytes_struct( + bytes_input_length, const_cast(reinterpret_cast(bytes_input))); + + return transaction_data_struct; + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_data_struct") + << LOG_DESC("exception") << LOG_KV("group_id", group_id) + << LOG_KV("chain_id", chain_id) << LOG_KV("to", std::string(to ? to : "")) + << LOG_KV("bytes_input", bytes_input) + << LOG_KV("abi", std::string(abi ? abi : "")) << LOG_KV("block_limit", block_limit) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); } @@ -684,9 +747,9 @@ void bcos_sdk_destroy_transaction_struct(struct bcos_sdk_c_transaction* transact * @param attribute * @return const char* */ -const char* bcos_sdk_create_transaction(struct bcos_sdk_c_transaction_data* transaction_data, - const char* signature, const char* transaction_data_hash, int32_t attribute, - const char* extra_data) +const char* bcos_sdk_create_encoded_transaction( + struct bcos_sdk_c_transaction_data* transaction_data, const char* signature, + const char* transaction_data_hash, int32_t attribute, const char* extra_data) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); @@ -705,8 +768,8 @@ const char* bcos_sdk_create_transaction(struct bcos_sdk_c_transaction_data* tran catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction") << LOG_DESC("exception") - << LOG_KV("signature", signature) + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_encoded_transaction") + << LOG_DESC("exception") << LOG_KV("signature", signature) << LOG_KV("transaction_data_hash", transaction_data_hash) << LOG_KV("attribute", attribute) << LOG_KV("extra_data", extra_data) << LOG_KV("error", errorMsg); diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h index fadc033a7..e55078c6e 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h @@ -33,14 +33,32 @@ extern "C" { * @param group_id * @param chain_id * @param to - * @param data + * @param input * @param abi * @param block_limit * @return bcos_sdk_c_transaction_data*: transaction data struct pointer, return unassigned struct * on failure according to the function called bcos_sdk_get_last_error(if create failed, return -1) */ -struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct(const char* group_id, - const char* chain_id, const char* to, const char* data, const char* abi, int64_t block_limit); +struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct_with_hex_input( + const char* group_id, const char* chain_id, const char* to, const char* input, const char* abi, + int64_t block_limit); + +/** + * @brief + * + * @param group_id + * @param chain_id + * @param to + * @param bytes_input + * @param bytes_input_length + * @param abi + * @param block_limit + * @return bcos_sdk_c_transaction_data*: transaction data struct pointer, return unassigned struct + * on failure according to the function called bcos_sdk_get_last_error(if create failed, return -1) + */ +struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct_with_bytes( + const char* group_id, const char* chain_id, const char* to, const unsigned char* bytes_input, + uint32_t bytes_input_length, const char* abi, int64_t block_limit); /** * @brief @@ -128,9 +146,9 @@ void bcos_sdk_destroy_transaction_struct(struct bcos_sdk_c_transaction* transact * @param attribute * @return const char* */ -const char* bcos_sdk_create_transaction(struct bcos_sdk_c_transaction_data* transaction_data, - const char* signature, const char* transaction_data_hash, int32_t attribute, - const char* extra_data); +const char* bcos_sdk_create_encoded_transaction( + struct bcos_sdk_c_transaction_data* transaction_data, const char* signature, + const char* transaction_data_hash, int32_t attribute, const char* extra_data); /** * @brief encode transaction into hex format diff --git a/bindings/java/jni/build.gradle b/bindings/java/jni/build.gradle index 47a4be168..e7e1a0ee6 100644 --- a/bindings/java/jni/build.gradle +++ b/bindings/java/jni/build.gradle @@ -31,7 +31,7 @@ dependencies { archivesBaseName = 'bcos-sdk-jni' group = 'org.fisco-bcos' -version = '3.3.0' +version = '3.3.0-txStruct-SNAPSHOT' // Additional attribute definition ext { diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp new file mode 100644 index 000000000..0ab13580a --- /dev/null +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp @@ -0,0 +1,554 @@ +#include "org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.h" +#include "bcos-c-sdk/bcos_sdk_c_error.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_keypair.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h" +#include "org_fisco_bcos_sdk_exception.h" + +struct bcos_sdk_c_bytes* convert_to_bytes_struct(JNIEnv* env, jobject bytesObj) +{ + if (bytesObj == NULL) + { + return NULL; + } + + struct bcos_sdk_c_bytes* bytes = + (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); + jclass bytesClass = env->GetObjectClass(bytesObj); + jfieldID bufferField = env->GetFieldID(bytesClass, "buffer", "[B"); + jfieldID lengthField = env->GetFieldID(bytesClass, "length", "I"); + + jbyteArray bufferArray = (jbyteArray)env->GetObjectField(bytesObj, bufferField); + jbyte* bufferData = env->GetByteArrayElements(bufferArray, nullptr); + jsize bufferLength = env->GetArrayLength(bufferArray); + bytes->length = (uint32_t)env->GetIntField(bytesObj, lengthField); + bytes->buffer = (uint8_t*)malloc(bufferLength * sizeof(uint8_t)); + memcpy(bytes->buffer, bufferData, bufferLength); + + env->ReleaseByteArrayElements(bufferArray, bufferData, 0); + + return bytes; +} + +jobject convert_to_bytes_jobject(JNIEnv* env, const struct bcos_sdk_c_bytes* bytes_struct) +{ + if (bytes_struct == NULL) + { + return NULL; + } + + jclass txBytesClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes"); + if (txBytesClass == NULL) + { + env->FatalError( + "No such class, className: " + "org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes"); + } + jmethodID txBytesMtd = env->GetMethodID(txBytesClass, "", "()V"); + if (txBytesMtd == NULL) + { + env->FatalError("No such constructor in TransactionBytes, constructor()"); + } + jfieldID bufferField = env->GetFieldID(txBytesClass, "buffer", "[B"); + jfieldID lengthField = env->GetFieldID(txBytesClass, "length", "I"); + + jobject jTxBytesObj = env->NewObject(txBytesClass, txBytesMtd); + // buffer + jbyteArray bufferArray = env->NewByteArray(bytes_struct->length); + env->SetByteArrayRegion( + bufferArray, 0, bytes_struct->length, reinterpret_cast(bytes_struct->buffer)); + env->SetObjectField(jTxBytesObj, bufferField, bufferArray); + // length + env->SetIntField(jTxBytesObj, lengthField, bytes_struct->length); + + env->DeleteLocalRef(bufferArray); + + return jTxBytesObj; +} + +struct bcos_sdk_c_transaction_data* convert_to_tx_data_struct( + JNIEnv* env, jobject transactionObject) +{ + bcos_sdk_c_transaction_data* tx_data_struct = + (struct bcos_sdk_c_transaction_data*)malloc(sizeof(struct bcos_sdk_c_transaction_data)); + jclass txDataClass = env->GetObjectClass(transactionObject); + + // version + jfieldID versionField = env->GetFieldID(txDataClass, "version", "I"); + tx_data_struct->version = env->GetIntField(transactionObject, versionField); + + // blockLimit + jfieldID blockLimitField = env->GetFieldID(txDataClass, "blockLimit", "J"); + tx_data_struct->block_limit = env->GetLongField(transactionObject, blockLimitField); + + // chainId + jfieldID chainIdField = env->GetFieldID(txDataClass, "chainId", "Ljava/lang/String;"); + jstring chainIdString = (jstring)env->GetObjectField(transactionObject, chainIdField); + const char* chainIdValue = env->GetStringUTFChars(chainIdString, nullptr); + tx_data_struct->chain_id = strdup(chainIdValue); + + // groupId + jfieldID groupIdField = env->GetFieldID(txDataClass, "groupId", "Ljava/lang/String;"); + jstring groupIdString = (jstring)env->GetObjectField(transactionObject, groupIdField); + const char* groupIdValue = env->GetStringUTFChars(groupIdString, nullptr); + tx_data_struct->group_id = strdup(groupIdValue); + + // nonce + jfieldID nonceField = env->GetFieldID(txDataClass, "nonce", "Ljava/lang/String;"); + jstring nonceString = (jstring)env->GetObjectField(transactionObject, nonceField); + const char* nonceValue = env->GetStringUTFChars(nonceString, nullptr); + tx_data_struct->nonce = strdup(nonceValue); + + // to + jfieldID toField = env->GetFieldID(txDataClass, "to", "Ljava/lang/String;"); + jstring toString = (jstring)env->GetObjectField(transactionObject, toField); + const char* toValue = env->GetStringUTFChars(toString, nullptr); + tx_data_struct->to = strdup(toValue); + + // abi + jfieldID abiField = env->GetFieldID(txDataClass, "abi", "Ljava/lang/String;"); + jstring abiString = (jstring)env->GetObjectField(transactionObject, abiField); + const char* abiValue = env->GetStringUTFChars(abiString, nullptr); + tx_data_struct->abi = strdup(abiValue); + + // input + jfieldID inputField = + env->GetFieldID(txDataClass, "input", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jobject inputObject = env->GetObjectField(transactionObject, inputField); + tx_data_struct->input = convert_to_bytes_struct(env, inputObject); + + // release source + env->ReleaseStringUTFChars(chainIdString, chainIdValue); + env->ReleaseStringUTFChars(groupIdString, groupIdValue); + env->ReleaseStringUTFChars(nonceString, nonceValue); + env->ReleaseStringUTFChars(toString, toValue); + env->ReleaseStringUTFChars(abiString, abiValue); + + return tx_data_struct; +} + +jobject convert_to_tx_data_jobject( + JNIEnv* env, const struct bcos_sdk_c_transaction_data* transactionData) +{ + jclass txDataClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionData"); + if (txDataClass == NULL) + { + env->FatalError( + "No such class, className: " + "org/fisco/bcos/sdk/jni/utilities/tx/TransactionData"); + } + jmethodID txDataMtd = env->GetMethodID(txDataClass, "", "()V"); + if (txDataMtd == NULL) + { + env->FatalError("No such constructor in TransactionData, constructor()"); + } + jobject jTxDataObj = env->NewObject(txDataClass, txDataMtd); + + // version + jfieldID versionField = env->GetFieldID(txDataClass, "version", "I"); + env->SetIntField(jTxDataObj, versionField, transactionData->version); + // block_limit + jfieldID blockLimitField = env->GetFieldID(txDataClass, "blockLimit", "J"); + env->SetLongField(jTxDataObj, blockLimitField, transactionData->block_limit); + // chain_id + jfieldID chainIdField = env->GetFieldID(txDataClass, "chainId", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, chainIdField, env->NewStringUTF(transactionData->chain_id)); + // group_id + jfieldID groupIdField = env->GetFieldID(txDataClass, "groupId", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, groupIdField, env->NewStringUTF(transactionData->group_id)); + // nonce + jfieldID nonceField = env->GetFieldID(txDataClass, "nonce", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, nonceField, env->NewStringUTF(transactionData->nonce)); + // to + jfieldID toField = env->GetFieldID(txDataClass, "to", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, toField, env->NewStringUTF(transactionData->to)); + // abi + jfieldID abiField = env->GetFieldID(txDataClass, "abi", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, abiField, env->NewStringUTF(transactionData->abi)); + + // input + jobject jInput = convert_to_bytes_jobject(env, transactionData->input); + jfieldID inputField = + env->GetFieldID(txDataClass, "input", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + env->SetObjectField(jTxDataObj, inputField, jInput); + + // release + env->DeleteLocalRef(jInput); + + return jTxDataObj; +} + +jobject convert_to_tx_jobject(JNIEnv* env, const struct bcos_sdk_c_transaction* tx_struct) +{ + jclass txClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/Transaction"); + if (txClass == NULL) + { + env->FatalError( + "No such class, className: " + "org/fisco/bcos/sdk/jni/utilities/tx/Transaction"); + } + jmethodID txMtd = env->GetMethodID(txClass, "", "()V"); + if (txMtd == NULL) + { + env->FatalError("No such constructor in Transaction, constructor()"); + } + jfieldID txDataField = + env->GetFieldID(txClass, "transactionData", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;"); + jfieldID dataHashField = + env->GetFieldID(txClass, "dataHash", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jfieldID signatureField = + env->GetFieldID(txClass, "signature", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jfieldID senderField = + env->GetFieldID(txClass, "sender", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jfieldID importTimeField = env->GetFieldID(txClass, "importTime", "J"); + jfieldID attributeField = env->GetFieldID(txClass, "attribute", "I"); + jfieldID extraDataField = env->GetFieldID(txClass, "extraData", "Ljava/lang/String;"); + + jobject javaTxObj = env->NewObject(txClass, txMtd); + // TransactionData + jobject javaTxDataObj = convert_to_tx_data_jobject(env, tx_struct->transaction_data); + env->SetObjectField(javaTxObj, txDataField, javaTxDataObj); + // DataHash + jobject javaDataHashObj = convert_to_bytes_jobject(env, tx_struct->data_hash); + env->SetObjectField(javaTxObj, dataHashField, javaDataHashObj); + // Signature + jobject javaSignatureObj = convert_to_bytes_jobject(env, tx_struct->signature); + env->SetObjectField(javaTxObj, signatureField, javaSignatureObj); + // Sender + jobject javaSenderObj = convert_to_bytes_jobject(env, tx_struct->sender); + env->SetObjectField(javaTxObj, senderField, javaSenderObj); + // ImportTime + env->SetLongField(javaTxObj, importTimeField, tx_struct->import_time); + // Attribute + env->SetIntField(javaTxObj, attributeField, tx_struct->attribute); + // ExtraData + jstring javaExtraData = env->NewStringUTF(tx_struct->extra_data); + env->SetObjectField(javaTxObj, extraDataField, javaExtraData); + + return javaTxObj; +} + +struct bcos_sdk_c_transaction* convert_to_tx_struct(JNIEnv* env, jobject jTxObj) +{ + jclass javaTxClass = env->GetObjectClass(jTxObj); + jfieldID transactionDataField = env->GetFieldID( + javaTxClass, "transactionData", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;"); + jfieldID dataHashField = + env->GetFieldID(javaTxClass, "dataHash", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jfieldID signatureField = + env->GetFieldID(javaTxClass, "signature", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jfieldID senderField = + env->GetFieldID(javaTxClass, "sender", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jfieldID importTimeField = env->GetFieldID(javaTxClass, "importTime", "J"); + jfieldID attributeField = env->GetFieldID(javaTxClass, "attribute", "I"); + jfieldID extraDataField = env->GetFieldID(javaTxClass, "extraData", "Ljava/lang/String;"); + + struct bcos_sdk_c_transaction* txStruct = + (struct bcos_sdk_c_transaction*)malloc(sizeof(struct bcos_sdk_c_transaction)); + // TransactionData + jobject javaTxDataObj = env->GetObjectField(jTxObj, transactionDataField); + struct bcos_sdk_c_transaction_data* txDataStruct = + convert_to_tx_data_struct(env, javaTxDataObj); + txStruct->transaction_data = txDataStruct; + // DataHash + jobject javaDataHashObj = env->GetObjectField(jTxObj, dataHashField); + struct bcos_sdk_c_bytes* dataHashStruct = convert_to_bytes_struct(env, javaDataHashObj); + txStruct->data_hash = dataHashStruct; + // Signature + jobject javaSignatureObj = env->GetObjectField(jTxObj, signatureField); + struct bcos_sdk_c_bytes* signatureStruct = convert_to_bytes_struct(env, javaSignatureObj); + txStruct->signature = signatureStruct; + // Sender + jobject javaSenderObj = env->GetObjectField(jTxObj, senderField); + struct bcos_sdk_c_bytes* senderStruct = convert_to_bytes_struct(env, javaSenderObj); + txStruct->sender = senderStruct; + // ImportTime + jlong importTimeValue = env->GetLongField(jTxObj, importTimeField); + txStruct->import_time = (int64_t)importTimeValue; + // Attribute + jint attributeValue = env->GetIntField(jTxObj, attributeField); + txStruct->attribute = (int32_t)attributeValue; + // ExtraData + jstring javaExtraData = (jstring)env->GetObjectField(jTxObj, extraDataField); + const char* extraDataValue = env->GetStringUTFChars(javaExtraData, NULL); + txStruct->extra_data = strdup(extraDataValue); + + env->ReleaseStringUTFChars(javaExtraData, extraDataValue); + + return txStruct; +} + +void destroy_bytes_jobject(JNIEnv* env, jobject jTxBytes) +{ + if (jTxBytes == NULL) + { + return; + } + + jclass jTxBytesClass = env->GetObjectClass(jTxBytes); + jfieldID jBufferFieldID = env->GetFieldID(jTxBytesClass, "buffer", "[B"); + jbyteArray jBufferArray = (jbyteArray)env->GetObjectField(jTxBytes, jBufferFieldID); + + env->DeleteLocalRef(jBufferArray); +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: encodeTransactionDataStruct + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTransactionDataStruct( + JNIEnv* env, jclass, jobject jTransactionDataObj) +{ + struct bcos_sdk_c_transaction_data* tx_data_struct = + convert_to_tx_data_struct(env, jTransactionDataObj); + const char* tx_data_hex = bcos_sdk_encode_transaction_data_struct(tx_data_struct); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + } + + jstring jTxDataHex = env->NewStringUTF(tx_data_hex); + + if (tx_data_hex) + { + free((void*)tx_data_hex); + tx_data_hex = NULL; + } + if (tx_data_struct) + { + bcos_sdk_destroy_transaction_data_struct(tx_data_struct); + } + + return jTxDataHex; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: encodeTransactionDataStructToJson + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTransactionDataStructToJson( + JNIEnv* env, jclass, jobject jTransactionDataObj) +{ + struct bcos_sdk_c_transaction_data* tx_data_struct = + convert_to_tx_data_struct(env, jTransactionDataObj); + const char* tx_data_json = bcos_sdk_encode_transaction_data_struct_to_json(tx_data_struct); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + } + + jstring jTxDataJson = env->NewStringUTF(tx_data_json); + + if (tx_data_json) + { + free((void*)tx_data_json); + tx_data_json = NULL; + } + if (tx_data_struct) + { + bcos_sdk_destroy_transaction_data_struct(tx_data_struct); + } + + return jTxDataJson; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: decodeTransactionDataStruct + * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData; + */ +JNIEXPORT jobject JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionDataStruct( + JNIEnv* env, jclass, jstring jTxDataHexStr) +{ + const char* tx_data_hex_str = env->GetStringUTFChars(jTxDataHexStr, nullptr); + struct bcos_sdk_c_transaction_data* tx_data_struct = + bcos_sdk_decode_transaction_data_struct(tx_data_hex_str); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + } + + jobject jTxDataObj = convert_to_tx_data_jobject(env, tx_data_struct); + + // release source + if (tx_data_struct) + { + bcos_sdk_destroy_transaction_data_struct(tx_data_struct); + } + env->ReleaseStringUTFChars(jTxDataHexStr, tx_data_hex_str); + + return jTxDataObj; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: calcTransactionDataStructHash + * Signature: (ILorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_calcTransactionDataStructHash( + JNIEnv* env, jclass, jint jCrytpTyte, jobject jTransactionDataObj) +{ + struct bcos_sdk_c_transaction_data* tx_data_struct = + convert_to_tx_data_struct(env, jTransactionDataObj); + int crypto_type = jCrytpTyte; + const char* tx_data_hash = + bcos_sdk_calc_transaction_data_struct_hash(crypto_type, tx_data_struct); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + } + + jstring jTxDataHash = env->NewStringUTF(tx_data_hash); + + // release source + if (tx_data_struct) + { + bcos_sdk_destroy_transaction_data_struct(tx_data_struct); + } + if (tx_data_hash) + { + free((void*)tx_data_hash); + tx_data_hash = NULL; + } + + return jTxDataHash; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: createEncodedTransaction + * Signature: + * (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_createEncodedTransaction( + JNIEnv* env, jclass, jobject jTxDataObj, jstring jSignature, jstring jTxDataHash, + jint jAttribute, jstring jExtraData) +{ + struct bcos_sdk_c_transaction_data* tx_data_struct = convert_to_tx_data_struct(env, jTxDataObj); + const char* signature = env->GetStringUTFChars(jSignature, NULL); + const char* tx_data_hash = env->GetStringUTFChars(jTxDataHash, NULL); + int attribute = jAttribute; + const char* extra_data = env->GetStringUTFChars(jExtraData, NULL); + + const char* tx_str = bcos_sdk_create_encoded_transaction( + tx_data_struct, signature, tx_data_hash, attribute, extra_data); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + } + + jstring jTxStr = env->NewStringUTF(tx_str); + + // release resource + env->ReleaseStringUTFChars(jSignature, signature); + env->ReleaseStringUTFChars(jTxDataHash, tx_data_hash); + env->ReleaseStringUTFChars(jExtraData, extra_data); + if (tx_data_struct) + { + bcos_sdk_destroy_transaction_data_struct(tx_data_struct); + } + if (tx_str) + { + free((void*)tx_str); + tx_str = NULL; + } + + return jTxStr; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: encodeTransactionStruct + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/Transaction;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTransactionStruct( + JNIEnv* env, jclass, jobject jTransactionObj) +{ + struct bcos_sdk_c_transaction* tx_struct = convert_to_tx_struct(env, jTransactionObj); + const char* tx_hex = bcos_sdk_encode_transaction_struct(tx_struct); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + } + + jstring jTxHex = env->NewStringUTF(tx_hex); + + // release source + if (tx_hex) + { + free((void*)tx_hex); + tx_hex = NULL; + } + if (tx_struct) + { + bcos_sdk_destroy_transaction_struct(tx_struct); + } + + return jTxHex; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: encodeTransactionStructToJson + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/Transaction;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTransactionStructToJson( + JNIEnv* env, jclass, jobject jTransactionObj) +{ + struct bcos_sdk_c_transaction* tx_struct = convert_to_tx_struct(env, jTransactionObj); + const char* tx_json = bcos_sdk_encode_transaction_struct_to_json(tx_struct); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + } + + jstring jTxJson = env->NewStringUTF(tx_json); + + if (tx_json) + { + free((void*)tx_json); + tx_json = NULL; + } + if (tx_struct) + { + bcos_sdk_destroy_transaction_struct(tx_struct); + } + + return jTxJson; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: decodeTransactionStruct + * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/Transaction; + */ +JNIEXPORT jobject JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionStruct( + JNIEnv* env, jclass, jstring jTxHexStr) +{ + const char* tx_hex_str = env->GetStringUTFChars(jTxHexStr, nullptr); + struct bcos_sdk_c_transaction* tx_struct = bcos_sdk_decode_transaction_struct(tx_hex_str); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + } + + jobject jTxObj = convert_to_tx_jobject(env, tx_struct); + + // release source + if (tx_struct) + { + bcos_sdk_destroy_transaction_struct(tx_struct); + } + env->ReleaseStringUTFChars(jTxHexStr, tx_hex_str); + + return jTxObj; +} diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.h b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.h new file mode 100644 index 000000000..0c3eba092 --- /dev/null +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.h @@ -0,0 +1,87 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj */ + +#ifndef _Included_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj +#define _Included_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj +#ifdef __cplusplus +extern "C" { +#endif + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: encodeTransactionDataStruct + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TxData;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTransactionDataStruct( + JNIEnv*, jclass, jobject); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: encodeTransactionDataStructToJson + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TxData;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTransactionDataStructToJson( + JNIEnv*, jclass, jobject); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: decodeTransactionDataStruct + * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxData; + */ +JNIEXPORT jobject JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionDataStruct( + JNIEnv*, jclass, jstring); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: calcTransactionDataStructHash + * Signature: (ILorg/fisco/bcos/sdk/jni/utilities/tx/TxData;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_calcTransactionDataStructHash( + JNIEnv*, jclass, jint, jobject); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: createEncodedTransaction + * Signature: + * (Lorg/fisco/bcos/sdk/jni/utilities/tx/TxData;Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_createEncodedTransaction( + JNIEnv*, jclass, jobject, jstring, jstring, jint, jstring); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: encodeTransactionStruct + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/Tx;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTransactionStruct( + JNIEnv*, jclass, jobject); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: encodeTransactionStructToJson + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/Tx;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTransactionStructToJson( + JNIEnv*, jclass, jobject); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: decodeTransactionStruct + * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/Tx; + */ +JNIEXPORT jobject JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionStruct( + JNIEnv*, jclass, jstring); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/Transaction.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/Transaction.java new file mode 100644 index 000000000..6b78ad2cb --- /dev/null +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/Transaction.java @@ -0,0 +1,67 @@ +package org.fisco.bcos.sdk.jni.utilities.tx; + +public class Transaction { + private TransactionData transactionData; + private TransactionBytes dataHash; + private TransactionBytes signature; + private TransactionBytes sender; + private long importTime; + private int attribute; + private String extraData; + + public TransactionData getTransactionData() { + return transactionData; + } + + public void setTransactionData(TransactionData transactionData) { + this.transactionData = transactionData; + } + + public TransactionBytes getDataHash() { + return dataHash; + } + + public void setDataHash(TransactionBytes dataHash) { + this.dataHash = dataHash; + } + + public TransactionBytes getSignature() { + return signature; + } + + public void setSignature(TransactionBytes signature) { + this.signature = signature; + } + + public TransactionBytes getSender() { + return sender; + } + + public void setSender(TransactionBytes sender) { + this.sender = sender; + } + + public long getImportTime() { + return importTime; + } + + public void setImportTime(long importTime) { + this.importTime = importTime; + } + + public int getAttribute() { + return attribute; + } + + public void setAttribute(int attribute) { + this.attribute = attribute; + } + + public String getExtraData() { + return extraData; + } + + public void setExtraData(String extraData) { + this.extraData = extraData; + } +} diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes.java new file mode 100644 index 000000000..d00f01c8a --- /dev/null +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes.java @@ -0,0 +1,22 @@ +package org.fisco.bcos.sdk.jni.utilities.tx; + +public class TransactionBytes { + private byte[] buffer; + private int length; + + public byte[] getBuffer() { + return buffer; + } + + public void setBuffer(byte[] buffer) { + this.buffer = buffer; + } + + public int getLength() { + return length; + } + + public void setLength(int length) { + this.length = length; + } +} diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionData.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionData.java new file mode 100644 index 000000000..30560a938 --- /dev/null +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionData.java @@ -0,0 +1,76 @@ +package org.fisco.bcos.sdk.jni.utilities.tx; + +public class TransactionData { + private int version; + private long blockLimit; + private String chainId; + private String groupId; + private String nonce; + private String to; + private String abi; + private TransactionBytes input; + + public int getVersion() { + return version; + } + + public void setVersion(int version) { + this.version = version; + } + + public long getBlockLimit() { + return blockLimit; + } + + public void setBlockLimit(long blockLimit) { + this.blockLimit = blockLimit; + } + + public String getChainId() { + return chainId; + } + + public void setChainId(String chainId) { + this.chainId = chainId; + } + + public String getGroupId() { + return groupId; + } + + public void setGroupId(String groupId) { + this.groupId = groupId; + } + + public String getNonce() { + return nonce; + } + + public void setNonce(String nonce) { + this.nonce = nonce; + } + + public String getTo() { + return to; + } + + public void setTo(String to) { + this.to = to; + } + + public String getAbi() { + return abi; + } + + public void setAbi(String abi) { + this.abi = abi; + } + + public TransactionBytes getInput() { + return input; + } + + public void setInput(TransactionBytes input) { + this.input = input; + } +} diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderJniObj.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderJniObj.java new file mode 100644 index 000000000..15f30b09b --- /dev/null +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderJniObj.java @@ -0,0 +1,74 @@ +package org.fisco.bcos.sdk.jni.utilities.tx; + +import org.fisco.bcos.sdk.jni.common.JniException; +import org.fisco.bcos.sdk.jni.common.JniLibLoader; + +public class TransactionStructBuilderJniObj { + + static { + JniLibLoader.loadJniLibrary(); + } + + /** + * @param transactionData Transaction Data struct + * @return Hexed Transaction Data + */ + public static native String encodeTransactionDataStruct(TransactionData transactionData) + throws JniException; + + /** + * @param transactionData Transaction Data struct + * @return Json Transaction Data + */ + public static native String encodeTransactionDataStructToJson(TransactionData transactionData) + throws JniException; + + /** + * @param transactionDataHex transactionData hex string + * @return TxData + */ + public static native TransactionData decodeTransactionDataStruct(String transactionDataHex) + throws JniException; + + /** + * @param cryptoType crypto type + * @param transactionData Transaction Data struct + * @return Hash hex string + */ + public static native String calcTransactionDataStructHash(int cryptoType, TransactionData transactionData) + throws JniException; + + /** + * @param transactionData + * @param signature + * @param transactionDataHash + * @param attribute + * @param extraData + * @return signedTransaction string + */ + public static native String createEncodedTransaction( + TransactionData transactionData, + String signature, + String transactionDataHash, + int attribute, + String extraData) + throws JniException; + + /** + * @param transaction Transaction struct + * @return Hexed Transaction + */ + public static native String encodeTransactionStruct(Transaction transaction) throws JniException; + + /** + * @param transaction Transaction struct + * @return Json Transaction + */ + public static native String encodeTransactionStructToJson(Transaction transaction) throws JniException; + + /** + * @param transactionHex transaction hex string + * @return Tx + */ + public static native Transaction decodeTransactionStruct(String transactionHex) throws JniException; +} diff --git a/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java b/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java new file mode 100644 index 000000000..fef23329c --- /dev/null +++ b/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java @@ -0,0 +1,212 @@ +package org.fisco.bcos.sdk.jni.test.tx; + +import org.fisco.bcos.sdk.jni.BcosSDKJniObj; +import org.fisco.bcos.sdk.jni.common.JniConfig; +import org.fisco.bcos.sdk.jni.common.JniException; +import org.fisco.bcos.sdk.jni.common.Response; +import org.fisco.bcos.sdk.jni.rpc.RpcCallback; +import org.fisco.bcos.sdk.jni.rpc.RpcJniObj; +import org.fisco.bcos.sdk.jni.test.Utility; +import org.fisco.bcos.sdk.jni.utilities.keypair.KeyPairJniObj; +import org.fisco.bcos.sdk.jni.utilities.tx.*; +import org.junit.Assert; + +import java.security.SecureRandom; +import java.util.Arrays; + +public class TestTxStruct { + + // ------------------------------------------------------------------------------ + // ------------------------------------------------------------------------------ + + // HelloWorld Source Code: + + // HelloWorld Source Code: + /** + * pragma solidity>=0.4.24 <0.6.11; + * + *

contract HelloWorld { string name; + * + *

constructor() public { name = "Hello, World!"; } + * + *

function get() public view returns (string memory) { return name; } + * + *

function set(string memory n) public { name = n; } } + */ + private static final String hwBIN = "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610235565b5050565b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561022b5780601f106102005761010080835404028352916020019161022b565b820191906000526020600020905b81548152906001019060200180831161020e57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea2646970667358221220b5943f43c48cc93c6d71cdcf27aee5072566c88755ce9186e32ce83b24e8dc6c64736f6c634300060a0033"; + + private static final String hwSmBIN = "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063299f7f9d1461003b5780633590b49f146100be575b600080fd5b610043610179565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610083578082015181840152602081019050610068565b50505050905090810190601f1680156100b05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610177600480360360208110156100d457600080fd5b81019080803590602001906401000000008111156100f157600080fd5b82018360208201111561010357600080fd5b8035906020019184600183028401116401000000008311171561012557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061021b565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102115780601f106101e657610100808354040283529160200191610211565b820191906000526020600020905b8154815290600101906020018083116101f457829003601f168201915b5050505050905090565b8060009080519060200190610231929190610235565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea26469706673582212209871cb2bcf390d53645807cbaedfe052d739ef9cff9d84787f74c4f379e1854664736f6c634300060a0033"; + + /* + { + "6d4ce63c": "get()", + "4ed3885e": "set(string)" + } + + { + "299f7f9d": "get()", + "3590b49f": "set(string)" + } + */ + + public static byte[] fromHex(String hexString) { + if (hexString.startsWith("0x")) { + hexString = hexString.substring(2); + } + + if (hexString.length() % 2 != 0) { + throw new IllegalArgumentException("Invalid hex string: " + hexString); + } + + int length = hexString.length() / 2; + byte[] bytes = new byte[length]; + + for (int i = 0; i < length; i++) { + String twoChars = hexString.substring(i * 2, i * 2 + 2); + bytes[i] = (byte) Integer.parseInt(twoChars, 16); + } + + return bytes; + } + + public static String generateNonce() { + byte[] nonceBytes = new byte[16]; + SecureRandom secureRandom = new SecureRandom(); + secureRandom.nextBytes(nonceBytes); + StringBuilder hex = new StringBuilder(); + for (byte b : nonceBytes) { + hex.append(String.format("%02x", b)); + } + return hex.toString(); + } + + public static String getBinary(boolean isSM) { + return isSM ? hwSmBIN : hwBIN; + } + + public static void Usage() { + System.out.println("Desc: test transaction struct [HelloWorld set]"); + System.out.println("Usage: java -cp 'conf/:lib/*:apps/*' org.fisco.bcos.sdk.jni.test.tx.TestTxStruct"); + System.exit(0); + } + + public static void main(String[] args) throws JniException, InterruptedException { + if (args.length > 1) { + Usage(); + } + + String endpoint = "127.0.0.1:22200"; + String group = "group0"; + String node = ""; + JniConfig jniConfig = Utility.newJniConfig(Arrays.asList(endpoint)); + jniConfig.setDisableSsl(true); + BcosSDKJniObj bcosSDKJni = BcosSDKJniObj.build(jniConfig); + RpcJniObj rpcJniObj = RpcJniObj.build(bcosSDKJni.getNativePointer()); + System.out.println("build Rpc"); + rpcJniObj.start(); + + boolean smCrypto = true; + + long keyPair = KeyPairJniObj.createJniKeyPair(smCrypto ? 1 : 0); + String jniKeyPairAddress = KeyPairJniObj.getJniKeyPairAddress(keyPair); + + long blockLimit = 1111; + String groupID = "group0"; + String chainID = "chain0"; + String data = getBinary(smCrypto); + System.out.printf(" [test Tx Struct] new account, address: %s\n", jniKeyPairAddress); + + // construct TransactionData + TransactionData transactionDataStruct = new TransactionData(); + transactionDataStruct.setGroupId(groupID); + transactionDataStruct.setChainId(chainID); + transactionDataStruct.setTo(""); + transactionDataStruct.setAbi(""); + transactionDataStruct.setVersion(0); + transactionDataStruct.setNonce(generateNonce()); + transactionDataStruct.setBlockLimit(blockLimit); + // input + TransactionBytes inputBytes = new TransactionBytes(); + byte[] bytesInput = fromHex(data); + inputBytes.setBuffer(bytesInput); + inputBytes.setLength(bytesInput.length); + transactionDataStruct.setInput(inputBytes); + + // encode TxData to hex tx data + String txDataHex = TransactionStructBuilderJniObj.encodeTransactionDataStruct(transactionDataStruct); + // decode hex tx data to TxData + TransactionData decodeTransactionDataStructHex = TransactionStructBuilderJniObj.decodeTransactionDataStruct(txDataHex); + // assert + Assert.assertEquals(transactionDataStruct.getChainId(), decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals(transactionDataStruct.getGroupId(), decodeTransactionDataStructHex.getGroupId()); + Assert.assertEquals(transactionDataStruct.getAbi(), decodeTransactionDataStructHex.getAbi()); + Assert.assertEquals(transactionDataStruct.getBlockLimit(), decodeTransactionDataStructHex.getBlockLimit()); + + // encode TxData to json tx data + String txDataJson = TransactionStructBuilderJniObj.encodeTransactionDataStructToJson(transactionDataStruct); + System.out.printf(" [test Tx Struct] txDataJson: %s\n", txDataJson); + + // calc tx data hash + String txDataHash = TransactionStructBuilderJniObj.calcTransactionDataStructHash(smCrypto ? 1 : 0, decodeTransactionDataStructHex); + System.out.printf(" [test Tx Struct] txDataHash: %s\n", txDataHash); + // signature tx data hash + String signature = TransactionBuilderJniObj.signTransactionDataHash(keyPair, txDataHash); + System.out.printf(" [test Tx Struct] signature: %s\n", signature); + + // construct tx + Transaction transactionStruct = new Transaction(); + TransactionBytes dataHashBytes = new TransactionBytes(); + dataHashBytes.setBuffer(txDataHash.getBytes()); + dataHashBytes.setLength(txDataHash.getBytes().length); + TransactionBytes signatureBytes = new TransactionBytes(); + signatureBytes.setBuffer(signature.getBytes()); + signatureBytes.setLength(signature.getBytes().length); + transactionStruct.setTransactionData(decodeTransactionDataStructHex); + transactionStruct.setDataHash(dataHashBytes); + transactionStruct.setSignature(signatureBytes); + transactionStruct.setSender(null); + transactionStruct.setImportTime(0); + transactionStruct.setAttribute(0); + transactionStruct.setExtraData(""); + // assert + Assert.assertEquals(transactionStruct.getTransactionData().getBlockLimit(), decodeTransactionDataStructHex.getBlockLimit()); + Assert.assertEquals(transactionStruct.getTransactionData().getGroupId(), decodeTransactionDataStructHex.getGroupId()); + Assert.assertEquals(transactionStruct.getTransactionData().getChainId(), decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals(transactionStruct.getTransactionData().getAbi(), decodeTransactionDataStructHex.getAbi()); + Assert.assertArrayEquals(transactionStruct.getDataHash().getBuffer(), txDataHash.getBytes()); + Assert.assertArrayEquals(transactionStruct.getSignature().getBuffer(), signature.getBytes()); + + // encode Tx to hex tx + String txHex = TransactionStructBuilderJniObj.encodeTransactionStruct(transactionStruct); + // decode hex tx to Tx + Transaction decodeTransactionStructHex = TransactionStructBuilderJniObj.decodeTransactionStruct(txHex); + // assert + Assert.assertEquals(transactionStruct.getTransactionData().getBlockLimit(), decodeTransactionStructHex.getTransactionData().getBlockLimit()); + Assert.assertEquals(transactionStruct.getTransactionData().getGroupId(), decodeTransactionStructHex.getTransactionData().getGroupId()); + Assert.assertEquals(transactionStruct.getTransactionData().getChainId(), decodeTransactionStructHex.getTransactionData().getChainId()); + Assert.assertEquals(transactionStruct.getTransactionData().getAbi(), decodeTransactionStructHex.getTransactionData().getAbi()); + Assert.assertArrayEquals(transactionStruct.getDataHash().getBuffer(), decodeTransactionStructHex.getDataHash().getBuffer()); + Assert.assertArrayEquals(transactionStruct.getSignature().getBuffer(), decodeTransactionStructHex.getSignature().getBuffer()); + + // encode Tx to json tx + String txJson = TransactionStructBuilderJniObj.encodeTransactionStructToJson(transactionStruct); + System.out.printf(" [test Tx Struct] txJson: %s\n", txJson); + // create tx string + String txString = TransactionStructBuilderJniObj.createEncodedTransaction(decodeTransactionDataStructHex, signature, txDataHash, 0, ""); +// System.out.printf(" [test Tx Struct] txString: %s\n", txString); + + // rpc send tx + rpcJniObj.sendTransaction(group, node, txString, false, new RpcCallback() { + @Override + public void onResponse(Response response) { + System.out.println("response error code: ==>>> " + response.getErrorCode()); + String dataStr = new String(response.getData()); + System.out.println("response data: ==>>> " + dataStr); + } + }); + + Thread.sleep(2000); + rpcJniObj.stop(); + System.out.printf(" [test Tx Struct] finish !! \n"); + } +} diff --git a/sample/tx/tx_struct_test.c b/sample/tx/tx_struct_test.c index b2dd3b6b3..37c66468a 100644 --- a/sample/tx/tx_struct_test.c +++ b/sample/tx/tx_struct_test.c @@ -28,6 +28,7 @@ #include #include + #ifdef _WIN32 #include #else @@ -137,6 +138,46 @@ void usage() exit(0); } +int convertCharToHexNumber(char hexChar) { + if (hexChar >= '0' && hexChar <= '9') + return hexChar - '0'; + if (hexChar >= 'a' && hexChar <= 'f') + return hexChar - 'a' + 10; + if (hexChar >= 'A' && hexChar <= 'F') + return hexChar - 'A' + 10; + return -1; +} + +struct bcos_sdk_c_bytes* fromHexString(const char* hexedString) { + unsigned startIndex = (strlen(hexedString) >= 2 && hexedString[0] == '0' && hexedString[1] == 'x') ? 2 : 0; + struct bcos_sdk_c_bytes* bytesData = (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); + bytesData->buffer = (uint8_t*)malloc((strlen(hexedString) - startIndex + 1) / 2); + bytesData->length = 0; + + if (strlen(hexedString) % 2) { + int h = convertCharToHexNumber(hexedString[startIndex++]); + if (h == -1) { + // Handle error + free(bytesData->buffer); + free(bytesData); + return NULL; + } + bytesData->buffer[bytesData->length++] = (uint8_t)h; + } + for (unsigned i = startIndex; i < strlen(hexedString); i += 2) { + int highValue = convertCharToHexNumber(hexedString[i]); + int lowValue = convertCharToHexNumber(hexedString[i + 1]); + if (highValue == -1 || lowValue == -1) { + // Handle error + free(bytesData->buffer); + free(bytesData); + return NULL; + } + bytesData->buffer[bytesData->length++] = (uint8_t)((highValue << 4) + lowValue); + } + return bytesData; +} + // contract address char* contract_address = NULL; @@ -330,9 +371,13 @@ int main(int argc, char** argv) bcos_sdk_abi_encode_method(g_hw_abi, "set", "[\"Hello FISCO-BCOS 3.0!!!\"]", sm_crypto); // 9.2 create signed transaction { - // 9.2.1 create transaction data - struct bcos_sdk_c_transaction_data* transaction_data = bcos_sdk_create_transaction_data_struct( + // 9.2.1 create transaction data with hex input + struct bcos_sdk_c_transaction_data* transaction_data = bcos_sdk_create_transaction_data_struct_with_hex_input( group_id, chain_id, contract_address, set_data, g_hw_abi, block_limit); + // create transaction data with bytes input + struct bcos_sdk_c_bytes* input_bytes = fromHexString(set_data); + transaction_data = bcos_sdk_create_transaction_data_struct_with_bytes( + group_id, chain_id, contract_address, input_bytes->buffer, input_bytes->length, g_hw_abi, block_limit); // 9.2.1.1 encode tx data to hex const char* hex_tx_data = bcos_sdk_encode_transaction_data_struct(transaction_data); @@ -354,7 +399,7 @@ int main(int argc, char** argv) bcos_sdk_sign_transaction_data_hash(key_pair, transaction_data_hash); // 9.2.4 create signed transaction - const char* signed_tx = bcos_sdk_create_transaction( + const char* signed_tx = bcos_sdk_create_encoded_transaction( decode_tx_data, signed_hash, transaction_data_hash, 0, extra_data); // 9.2.4.1 create transaction struct @@ -377,6 +422,11 @@ int main(int argc, char** argv) // wait for async operation done, just for sample sleep(3); + // free + if (input_bytes && input_bytes->buffer) + { + bcos_sdk_c_free(input_bytes->buffer); + } bcos_sdk_destroy_transaction_data_struct(transaction_data); bcos_sdk_destroy_transaction_data_struct(decode_tx_data); bcos_sdk_destroy_transaction_struct(transaction); From 3b1316ac07814f8a8cf9f463b4eb1d79366b7131 Mon Sep 17 00:00:00 2001 From: lucasli <410567249@qq.com> Date: Mon, 26 Jun 2023 12:40:30 +0800 Subject: [PATCH 03/29] (txStruct): add an API that calculate hash with tx data hex (#171) --- bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp | 42 +++++++++++++++++++++++++ bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h | 10 ++++++ sample/tx/tx_struct_test.c | 3 ++ 3 files changed, 55 insertions(+) diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp index d3cbbcbd7..966933713 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp @@ -640,6 +640,48 @@ const char* bcos_sdk_calc_transaction_data_struct_hash( return NULL; } +/** + * @brief + * + * @param crypto_type: int + * @param transaction_data_hex: const char* + * @return const char* + */ +const char* bcos_sdk_calc_transaction_data_struct_hash_with_hex( + int crypto_type, const char* transaction_data_hex) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data_hex, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (crypto_type == BCOS_C_SDK_ECDSA_TYPE || crypto_type == BCOS_C_SDK_SM_TYPE), + "invalid crypto type, it must be BCOS_C_SDK_ECDSA_TYPE(ecdsa crypto type) or " + "BCOS_C_SDK_SM_TYPE(sm crypto type)", + NULL); + + try + { + struct bcos_sdk_c_transaction_data* transaction_data_struct = + bcos_sdk_decode_transaction_data_struct(transaction_data_hex); + auto tars_transaction_data = convert_transaction_data_to_tars(transaction_data_struct); + TransactionBuilder builder; + auto transactionDataHash = builder.calculateTransactionDataHash( + crypto_type == BCOS_C_SDK_ECDSA_TYPE ? CryptoType::Secp256K1 : CryptoType::SM2, + *tars_transaction_data); + return strdup(bcos::toHexStringWithPrefix(transactionDataHash).c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_calc_transaction_data_struct_hash_with_hex") + << LOG_DESC("exception") << LOG_KV("crypto_type", crypto_type) + << LOG_KV("transaction_data_hex", transaction_data_hex) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + /** * @brief * diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h index e55078c6e..e275b093d 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h @@ -117,6 +117,16 @@ struct bcos_sdk_c_transaction_data* bcos_sdk_decode_transaction_data_struct_with const char* bcos_sdk_calc_transaction_data_struct_hash( int crypto_type, struct bcos_sdk_c_transaction_data* transaction_data); +/** + * @brief + * + * @param crypto_type: int + * @param transaction_data_hex: const char* + * @return const char* + */ +const char* bcos_sdk_calc_transaction_data_struct_hash_with_hex( + int crypto_type, const char* transaction_data_hex); + /** * @brief * diff --git a/sample/tx/tx_struct_test.c b/sample/tx/tx_struct_test.c index 37c66468a..b06fe9a22 100644 --- a/sample/tx/tx_struct_test.c +++ b/sample/tx/tx_struct_test.c @@ -394,6 +394,9 @@ int main(int argc, char** argv) const char* transaction_data_hash = bcos_sdk_calc_transaction_data_struct_hash(sm_crypto, decode_tx_data); printf(" [TxStructTest] set tx hash: %s\n", transaction_data_hash); + transaction_data_hash = bcos_sdk_calc_transaction_data_struct_hash_with_hex(sm_crypto, hex_tx_data); + printf(" [TxStructTest] set tx hash with tx_data_hex: %s\n", transaction_data_hash); + // 9.2.3 sign transaction hash const char* signed_hash = bcos_sdk_sign_transaction_data_hash(key_pair, transaction_data_hash); From c9602b10d518e7d55c43b90f0550d9a2db9ac694 Mon Sep 17 00:00:00 2001 From: lucasli <410567249@qq.com> Date: Wed, 5 Jul 2023 14:15:55 +0800 Subject: [PATCH 04/29] =?UTF-8?q?(txStruct):=20fix=20bcos=5Fsdk=5Fenc?= =?UTF-8?q?ode=5Ftransaction=5Fstruct=20that=20send=20hex=E2=80=A6=20(#172?= =?UTF-8?q?)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp | 33 ++++++++++++++++--------- sample/tx/tx_struct_test.c | 7 ++++-- 2 files changed, 26 insertions(+), 14 deletions(-) diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp index 966933713..57f7dacae 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp @@ -219,31 +219,27 @@ bcostars::TransactionUniquePtr convert_transaction_to_tars( auto TransactionDataUniquePtr = convert_transaction_data_to_tars(transaction->transaction_data); tars_transaction->data = *TransactionDataUniquePtr; - std::vector data_hash_vec, signature_vec, sender_vec; if (transaction->data_hash) { for (size_t i = 0; i < transaction->data_hash->length; ++i) { - data_hash_vec.push_back(transaction->data_hash->buffer[i]); + tars_transaction->dataHash.push_back(transaction->data_hash->buffer[i]); } } if (transaction->signature) { for (size_t i = 0; i < transaction->signature->length; ++i) { - signature_vec.push_back(transaction->signature->buffer[i]); + tars_transaction->signature.push_back(transaction->signature->buffer[i]); } } if (transaction->sender) { for (size_t i = 0; i < transaction->sender->length; ++i) { - sender_vec.push_back(transaction->sender->buffer[i]); + tars_transaction->sender.push_back(transaction->sender->buffer[i]); } } - tars_transaction->dataHash = data_hash_vec; - tars_transaction->signature = signature_vec; - tars_transaction->sender = sender_vec; tars_transaction->importTime = (tars::Int32)transaction->import_time; tars_transaction->attribute = (tars::Int64)transaction->attribute; tars_transaction->extraData = std::string(transaction->extra_data); @@ -705,13 +701,26 @@ struct bcos_sdk_c_transaction* bcos_sdk_create_transaction_struct( struct bcos_sdk_c_transaction* transaction_struct = (struct bcos_sdk_c_transaction*)malloc(sizeof(struct bcos_sdk_c_transaction)); transaction_struct->transaction_data = transaction_data_copy(transaction_data); - transaction_struct->data_hash = - create_bytes_struct(std::strlen(transaction_data_hash), transaction_data_hash); - transaction_struct->signature = create_bytes_struct(std::strlen(signature), signature); transaction_struct->sender = NULL; transaction_struct->import_time = 0; transaction_struct->attribute = attribute; transaction_struct->extra_data = my_strdup(extra_data); + // signature + auto signatureWithoutHex = fromHexString(signature); + struct bcos_sdk_c_bytes* signature_bytes = + (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); + signature_bytes->length = signatureWithoutHex->size(); + signature_bytes->buffer = (uint8_t*)malloc(signatureWithoutHex->size()); + memcpy(signature_bytes->buffer, signatureWithoutHex->data(), signatureWithoutHex->size()); + transaction_struct->signature = signature_bytes; + // data_hash + auto dataHashArray = bcos::crypto::HashType(transaction_data_hash); + struct bcos_sdk_c_bytes* data_hash_bytes = + (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); + data_hash_bytes->length = dataHashArray.size(); + data_hash_bytes->buffer = (uint8_t*)malloc(dataHashArray.size()); + memcpy(data_hash_bytes->buffer, dataHashArray.data(), dataHashArray.size()); + transaction_struct->data_hash = data_hash_bytes; return transaction_struct; } @@ -837,8 +846,8 @@ const char* bcos_sdk_encode_transaction_struct(struct bcos_sdk_c_transaction* tr auto tars_transaction = convert_transaction_to_tars(transaction); TransactionBuilder builder; auto transaction = builder.encodeTransaction(*tars_transaction); - auto hex_tx_str = toHexString(*transaction); - return strdup(hex_tx_str->c_str()); + auto hex_tx_str = toHexStringWithPrefix(*transaction); + return strdup(hex_tx_str.c_str()); } catch (const std::exception& e) { diff --git a/sample/tx/tx_struct_test.c b/sample/tx/tx_struct_test.c index b06fe9a22..14b8af52b 100644 --- a/sample/tx/tx_struct_test.c +++ b/sample/tx/tx_struct_test.c @@ -418,9 +418,12 @@ int main(int argc, char** argv) printf(" [TxStructTest] tx_json: %s\n", json_tx); // 9.2.4.5 decode json to tx decode_tx = bcos_sdk_decode_transaction_struct_with_json(json_tx); - + + printf(" [TxStructTest] signed_tx: %s\n", signed_tx); + printf(" [TxStructTest] hex_tx: %s\n", hex_tx); + // 9.3 call rpc interface, sendTransaction - bcos_rpc_send_transaction(sdk, group_id, "", signed_tx, 0, on_send_tx_resp_callback, NULL); + bcos_rpc_send_transaction(sdk, group_id, "", hex_tx, 0, on_send_tx_resp_callback, NULL); // wait for async operation done, just for sample sleep(3); From 63defc831458525d8065019c2103c5faf32145b5 Mon Sep 17 00:00:00 2001 From: lucasli <410567249@qq.com> Date: Thu, 6 Jul 2023 11:23:09 +0800 Subject: [PATCH 05/29] Remove duplicate variables and assignments (#173) --- bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp | 13 ++++++------- sample/tx/tx_struct_test.c | 6 ++++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp index 57f7dacae..b14d71cc5 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp @@ -191,7 +191,6 @@ bcostars::TransactionDataUniquePtr convert_transaction_data_to_tars( tars_transaction_data->nonce = std::string(transaction_data->nonce); tars_transaction_data->to = std::string(transaction_data->to); tars_transaction_data->abi = std::string(transaction_data->abi); - tars_transaction_data->groupID = std::string(transaction_data->group_id); return tars_transaction_data; } @@ -347,7 +346,7 @@ struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct_with catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_data_struct") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_data_struct_with_hex_input") << LOG_DESC("exception") << LOG_KV("group_id", group_id) << LOG_KV("chain_id", chain_id) << LOG_KV("to", std::string(to ? to : "")) << LOG_KV("input", input) << LOG_KV("abi", std::string(abi ? abi : "")) @@ -408,7 +407,7 @@ struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct_with catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_data_struct") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_data_struct_with_bytes") << LOG_DESC("exception") << LOG_KV("group_id", group_id) << LOG_KV("chain_id", chain_id) << LOG_KV("to", std::string(to ? to : "")) << LOG_KV("bytes_input", bytes_input) @@ -485,8 +484,8 @@ const char* bcos_sdk_encode_transaction_data_struct( { auto tars_transaction_data = convert_transaction_data_to_tars(transaction_data); TransactionBuilder builder; - auto transactionData = builder.encodeTransactionData(*tars_transaction_data); - auto hex_tx_data_str = toHexString(*transactionData); + auto encodedTransactionData = builder.encodeTransactionData(*tars_transaction_data); + auto hex_tx_data_str = toHexString(*encodedTransactionData); return strdup(hex_tx_data_str->c_str()); } catch (const std::exception& e) @@ -845,8 +844,8 @@ const char* bcos_sdk_encode_transaction_struct(struct bcos_sdk_c_transaction* tr { auto tars_transaction = convert_transaction_to_tars(transaction); TransactionBuilder builder; - auto transaction = builder.encodeTransaction(*tars_transaction); - auto hex_tx_str = toHexStringWithPrefix(*transaction); + auto encodedTransaction = builder.encodeTransaction(*tars_transaction); + auto hex_tx_str = toHexStringWithPrefix(*encodedTransaction); return strdup(hex_tx_str.c_str()); } catch (const std::exception& e) diff --git a/sample/tx/tx_struct_test.c b/sample/tx/tx_struct_test.c index 14b8af52b..869c8cefb 100644 --- a/sample/tx/tx_struct_test.c +++ b/sample/tx/tx_struct_test.c @@ -414,16 +414,18 @@ int main(int argc, char** argv) // 9.2.4.3 decode hex to tx struct bcos_sdk_c_transaction* decode_tx = bcos_sdk_decode_transaction_struct(hex_tx); // 9.2.4.4 encode tx to json - const char* json_tx = bcos_sdk_encode_transaction_struct_to_json(transaction); + const char* json_tx = bcos_sdk_encode_transaction_struct_to_json(decode_tx); printf(" [TxStructTest] tx_json: %s\n", json_tx); // 9.2.4.5 decode json to tx decode_tx = bcos_sdk_decode_transaction_struct_with_json(json_tx); + const char* hex_tx2 = bcos_sdk_encode_transaction_struct(decode_tx); printf(" [TxStructTest] signed_tx: %s\n", signed_tx); printf(" [TxStructTest] hex_tx: %s\n", hex_tx); + printf(" [TxStructTest] hex_tx2: %s\n", hex_tx2); // 9.3 call rpc interface, sendTransaction - bcos_rpc_send_transaction(sdk, group_id, "", hex_tx, 0, on_send_tx_resp_callback, NULL); + bcos_rpc_send_transaction(sdk, group_id, "", hex_tx2, 0, on_send_tx_resp_callback, NULL); // wait for async operation done, just for sample sleep(3); From 78b9ccb1721c7c1bfaa410686904161c4fc683e5 Mon Sep 17 00:00:00 2001 From: XingQiang Bai Date: Thu, 6 Jul 2023 17:01:05 +0800 Subject: [PATCH 06/29] add uinttest (#174) --- CMakeLists.txt | 12 ++--- cmake/SearchTestCases.cmake | 66 +++++++++++++++++++++++++++ test/CMakeLists.txt | 18 ++++---- test/testMain.cpp | 4 ++ test/testTransactionStruct.cpp | 81 ++++++++++++++++++++++++++++++++++ 5 files changed, 167 insertions(+), 14 deletions(-) create mode 100644 cmake/SearchTestCases.cmake create mode 100644 test/testMain.cpp create mode 100644 test/testTransactionStruct.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f834b6953..df7cf5b61 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,7 +24,7 @@ if(NOT MSVC) set(CMAKE_CXX_FLAGS "-pthread -fPIC -fvisibility=hidden -fvisibility-inlines-hidden -fexceptions") endif() -set(CMAKE_OSX_DEPLOYMENT_TARGET "12" CACHE STRING "Minimum OS X deployment version") +set(CMAKE_OSX_DEPLOYMENT_TARGET "13" CACHE STRING "Minimum OS X deployment version") # export windows dll symbol if(WIN32) @@ -137,11 +137,11 @@ if(BUILD_JNI) endif() # ut and binary exe -# if (TESTS) -# enable_testing() -# set(CTEST_OUTPUT_ON_FAILURE TRUE) -# add_subdirectory(test) -# endif() +if (TESTS) + enable_testing() + set(CTEST_OUTPUT_ON_FAILURE TRUE) + add_subdirectory(test) +endif() # SET(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib) install( diff --git a/cmake/SearchTestCases.cmake b/cmake/SearchTestCases.cmake new file mode 100644 index 000000000..7750d2ae1 --- /dev/null +++ b/cmake/SearchTestCases.cmake @@ -0,0 +1,66 @@ +# ------------------------------------------------------------------------------ +# Copyright (C) 2021 FISCO BCOS. +# SPDX-License-Identifier: Apache-2.0 +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ------------------------------------------------------------------------------ +# File: SearchTestCases.cmake +# Function: cmake to help search test cases +# ------------------------------------------------------------------------------ +function(config_test_cases TEST_ARGS SOURCES TEST_BINARY_PATH EXCLUDE_SUITES) + foreach(file ${SOURCES}) + file(STRINGS ${file} test_list_raw REGEX "BOOST_.*TEST_(SUITE|CASE|SUITE_END)") + set(TestSuite "DEFAULT") + set(TestSuitePath "") + foreach(test_raw ${test_list_raw}) + string(REGEX REPLACE ".*TEST_(SUITE|CASE)\\(([^ ,\\)]*).*" "\\1 \\2" test ${test_raw}) + + #skip disabled + if (";${EXCLUDE_SUITES};" MATCHES ";${TestSuite};") + continue() + endif() + + if(test MATCHES "^SUITE .*") + + string(SUBSTRING ${test} 6 -1 TestSuite) + set(TestSuitePath "${TestSuitePath}/${TestSuite}") + + if(FASTCTEST) + if (";${EXCLUDE_SUITES};" MATCHES ";${TestSuite};") + continue() + endif() + if (NOT ";${allSuites};" MATCHES ";${TestSuite};") + string(SUBSTRING ${TestSuitePath} 1 -1 TestSuitePathFixed) + list(APPEND allSuites ${TestSuite}) + separate_arguments(TEST_ARGS) + set(TestArgs -t ${TestSuitePathFixed} -- ${TEST_ARGS}) + add_test(NAME ${TestSuitePathFixed} WORKING_DIRECTORY ${CMAKE_BINARY_DIR} COMMAND ${TEST_BINARY_PATH} ${TestArgs}) + endif() + endif() + elseif(test MATCHES "^CASE .*") + if(NOT FASTCTEST) + if(NOT test MATCHES "^CASE &createRandom.*") + string(SUBSTRING ${test} 5 -1 TestCase) + string(SUBSTRING ${TestSuitePath} 1 -1 TestSuitePathFixed) + separate_arguments(TEST_ARGS) + set(TestArgs -t ${TestSuitePathFixed}/${TestCase} -- ${TEST_ARGS}) + add_test(NAME ${TestSuitePathFixed}/${TestCase} WORKING_DIRECTORY ${CMAKE_BINARY_DIR} COMMAND ${TEST_BINARY_PATH} ${TestArgs}) + endif() + endif() + elseif (";${test_raw};" MATCHES "BOOST_AUTO_TEST_SUITE_END()") + #encountered SUITE_END block. remove one test suite from the suite path. + string(FIND ${TestSuitePath} "/" Position REVERSE) + string(SUBSTRING ${TestSuitePath} 0 ${Position} TestSuitePath) + endif() + endforeach(test_raw) + endforeach(file) +endfunction() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 0fab5d5f1..9bb8c03e8 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,5 @@ #------------------------------------------------------------------------------ -# Top-level CMake file for ut of bcos-c-sdk +# Top-level CMake file for ut of bcos-rpc # ------------------------------------------------------------------------------ # Copyright (C) 2021 FISCO BCOS. # SPDX-License-Identifier: Apache-2.0 @@ -15,14 +15,16 @@ # See the License for the specific language governing permissions and # limitations under the License. # ------------------------------------------------------------------------------ -file(GLOB_RECURSE SOURCES "unittests/*.cpp" "unittests/*.h" "unittests/*.sol") + +file(GLOB_RECURSE SOURCES "*.cpp" "*.h") # cmake settings include(SearchTestCases) -set(TEST_BINARY_NAME test-bcos-rpc) -config_test_cases("" "${SOURCES}" bin/${TEST_BINARY_NAME} "") +set(TEST_BINARY_NAME test-bcos-c-sdk) +config_test_cases("" "${SOURCES}" ${TEST_BINARY_NAME} "") + +add_executable(${TEST_BINARY_NAME} ${SOURCES}) +target_include_directories(${TEST_BINARY_NAME} PRIVATE . ${CMAKE_SOURCE_DIR}) -# ut -# add_executable(${TEST_BINARY_NAME} ${SOURCES}) -# target_include_directories(${TEST_BINARY_NAME} PRIVATE .) -# target_link_libraries(${TEST_BINARY_NAME}) +find_package(Boost CONFIG QUIET REQUIRED unit_test_framework) +target_link_libraries(${TEST_BINARY_NAME} ${BCOS_C_SDK_TARGET} Boost::unit_test_framework) diff --git a/test/testMain.cpp b/test/testMain.cpp new file mode 100644 index 000000000..539a4cfb1 --- /dev/null +++ b/test/testMain.cpp @@ -0,0 +1,4 @@ +#define BOOST_TEST_MODULE bcos_c_sdk_test +#define BOOST_TEST_MAIN + +#include \ No newline at end of file diff --git a/test/testTransactionStruct.cpp b/test/testTransactionStruct.cpp new file mode 100644 index 000000000..309a8212b --- /dev/null +++ b/test/testTransactionStruct.cpp @@ -0,0 +1,81 @@ +/* + * Copyright (C) 2021 FISCO BCOS. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @file testTransactionStruct.cpp + * @author: xingqiangbai + * @date 2023-07-06 + */ + +#include "bcos-c-sdk/bcos_sdk_c.h" +#include "bcos-c-sdk/bcos_sdk_c_error.h" +#include "bcos-c-sdk/bcos_sdk_c_rpc.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_abi.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_keypair.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_tx.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h" +#include +#include + +struct TestTxStructFixture +{ +}; + +BOOST_FIXTURE_TEST_SUITE(TransactionStructTest, TestTxStructFixture) + +BOOST_AUTO_TEST_CASE(test_create_transaction_data_struct_with_hex_input) +{ + auto group_id = ""; + auto chain_id = ""; + auto to = ""; + auto input = ""; + auto abi = ""; + int64_t block_limit = 0; + auto txData = bcos_sdk_create_transaction_data_struct_with_hex_input( + group_id, chain_id, to, input, abi, block_limit); + auto success = bcos_sdk_is_last_opr_success(); + // if (!success) + // { + // std::cerr << bcos_sdk_get_last_error_msg() << std::endl; + // } + BOOST_TEST(success == false); + BOOST_TEST(txData == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("block limit must >= 0")); + + block_limit = 1; + txData = bcos_sdk_create_transaction_data_struct_with_hex_input( + group_id, chain_id, to, input, abi, block_limit); + success = bcos_sdk_is_last_opr_success(); + if (!success) + { + // BOOST_TEST_MESSAGE(bcos_sdk_get_last_error_msg()); + std::cerr << bcos_sdk_get_last_error_msg() << std::endl; + } + BOOST_TEST(success == true); + BOOST_TEST(txData != nullptr); + BOOST_TEST(txData->group_id == group_id); + BOOST_TEST(txData->chain_id == chain_id); + BOOST_TEST(txData->to == to); + BOOST_TEST(txData->abi == abi); + BOOST_TEST(txData->block_limit == block_limit); + + // auto json = bcos_sdk_encode_transaction_data_struct(txData); + // BOOST_TEST(json == std::string("{\"groupId\":\"\",\"chainId\":\"\",\"to\":\"\",\"input\":\"\"," + // "\"abi\":\"\",\"blockLimit\":1}")); + + bcos_sdk_destroy_transaction_data_struct(txData); +} + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file From e964c1c1b0cb6db9fdcb9c9693af0c6a3b1a51de Mon Sep 17 00:00:00 2001 From: lucasli <410567249@qq.com> Date: Tue, 5 Sep 2023 16:56:32 +0800 Subject: [PATCH 07/29] add tx struct UT (#175) --- .github/workflows/workflow.yml | 24 +- CMakeLists.txt | 6 + bcos-c-sdk/bcos_sdk_c_uti_tx.cpp | 2 +- bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp | 59 +- cmake/Coverage.cmake | 41 ++ test/testTransactionStruct.cpp | 792 +++++++++++++++++++++++- 6 files changed, 881 insertions(+), 43 deletions(-) create mode 100644 cmake/Coverage.cmake diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 5a96c2b55..c2f61208f 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -46,20 +46,22 @@ jobs: hunter-clang-v3-notest-${{ runner.temp }}- - name: install macOS dependencies if: runner.os == 'macOS' - run: brew install ccache + run: brew install ccache lcov - name: install Ubuntu dependencies if: runner.os == 'Linux' - run: sudo apt install -y git curl openssl build-essential clang cmake ccache + run: sudo apt install -y git curl openssl build-essential clang cmake ccache lcov - name: configure if: runner.os == 'macOS' - run: export SDKROOT=$(xcrun --sdk macosx --show-sdk-path) && CC=/usr/bin/clang CXX=/usr/bin/clang++ mkdir build && cd build && cmake ../ -DBUILD_JNI=ON -DBUILD_SAMPLE=ON -DTESTS=ON + run: export SDKROOT=$(xcrun --sdk macosx --show-sdk-path) && CC=/usr/bin/clang CXX=/usr/bin/clang++ mkdir build && cd build && cmake ../ -DBUILD_JNI=ON -DBUILD_SAMPLE=ON -DTESTS=ON -DCOVERAGE=ON - name: configure if: runner.os == 'Linux' - run: CC=/usr/bin/clang CXX=/usr/bin/clang++ mkdir build && cd build && cmake ../ -DBUILD_JNI=ON -DBUILD_SAMPLE=ON -DTESTS=ON + run: CC=/usr/bin/clang CXX=/usr/bin/clang++ mkdir build && cd build && cmake ../ -DBUILD_JNI=ON -DBUILD_SAMPLE=ON -DTESTS=ON -DCOVERAGE=ON - name: compile run: export CFLAGS="${CFLAGS} -fPIC";export CXXFLAGS="${CXXFLAGS} -fPIC"; cd build && make -j4 - name: run test - run: CTEST_OUTPUT_ON_FAILURE=TRUE make test + run: cd build && CTEST_OUTPUT_ON_FAILURE=TRUE make test + - name: run coverage + run: cd build && make cov - uses: actions/upload-artifact@v2 with: name: libbcos-c-sdk.dylib.zip @@ -105,15 +107,17 @@ jobs: cache-gcc-v2-notest-${{ runner.temp }}-${{ github.base_ref }}- cache-gcc-v2-notest-${{ runner.temp }}- - name: install Ubuntu dependencies - run: sudo apt install -y git curl openssl build-essential cmake ccache + run: sudo apt install -y git curl openssl build-essential cmake ccache lcov - name: configure run: | export CC='gcc-10'; export CXX='g++-10' - export CFLAGS="${CFLAGS} -fPIC";export CXXFLAGS="${CXXFLAGS} -fPIC"; mkdir -p build && cd build && cmake ../ -DBUILD_JNI=ON -DBUILD_SAMPLE=ON -DTESTS=ON + export CFLAGS="${CFLAGS} -fPIC";export CXXFLAGS="${CXXFLAGS} -fPIC"; mkdir -p build && cd build && cmake ../ -DBUILD_JNI=ON -DBUILD_SAMPLE=ON -DTESTS=ON -DCOVERAGE=ON - name: compile run: cd build && make -j4 - name: run test - run: CTEST_OUTPUT_ON_FAILURE=TRUE make test + run: cd build && CTEST_OUTPUT_ON_FAILURE=TRUE make test + - name: run coverage + run: cd build && make cov build_with_centos: name: build_with_centos runs-on: ubuntu-latest @@ -164,8 +168,10 @@ jobs: export CFLAGS="${CFLAGS} -fPIC" export CXXFLAGS="${CXXFLAGS} -fPIC" mkdir -p build && cd build - cmake3 ../ -DHUNTER_STATUS_DEBUG=ON -DBUILD_JNI=ON -DBUILD_SAMPLE=ON + cmake3 ../ -DHUNTER_STATUS_DEBUG=ON -DBUILD_JNI=ON -DBUILD_SAMPLE=ON -DTESTS=ON make -j4 + - name: run test + run: cd build && CTEST_OUTPUT_ON_FAILURE=TRUE make test - uses: actions/upload-artifact@v2 with: name: libbcos-c-sdk.so.zip diff --git a/CMakeLists.txt b/CMakeLists.txt index df7cf5b61..a23295730 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -143,6 +143,12 @@ if (TESTS) add_subdirectory(test) endif() +# for code coverage +if (COVERAGE) + include(Coverage) + config_coverage("cov" "'/usr*' '${CMAKE_CURRENT_SOURCE_DIR}/bcos-cmake-scripts*' '${CMAKE_CURRENT_SOURCE_DIR}/test/Main*'") +endif () + # SET(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib) install( TARGETS ${BCOS_C_SDK_TARGET} diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx.cpp b/bcos-c-sdk/bcos_sdk_c_uti_tx.cpp index bb82bbe5c..e7c6a6c43 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx.cpp +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx.cpp @@ -55,7 +55,7 @@ void* bcos_sdk_create_transaction_data(const char* group_id, const char* chain_i // BCOS_SDK_C_PARAMS_VERIFICATION(to, NULL); BCOS_SDK_C_PARAMS_VERIFICATION(data, NULL); // BCOS_SDK_C_PARAMS_VERIFICATION(abi, NULL); - BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must >= 0", NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must > 0", NULL); try { diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp index b14d71cc5..286bacf28 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp @@ -174,16 +174,17 @@ bcostars::TransactionDataUniquePtr convert_transaction_data_to_tars( { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data->group_id, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data->chain_id, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data->input, NULL); try { auto tars_transaction_data = std::make_unique(); - std::vector input; for (size_t i = 0; i < transaction_data->input->length; ++i) { - input.push_back(transaction_data->input->buffer[i]); + tars_transaction_data->input.push_back(transaction_data->input->buffer[i]); } - tars_transaction_data->input = std::move(input); tars_transaction_data->version = (tars::Int32)transaction_data->version; tars_transaction_data->blockLimit = (tars::Int64)transaction_data->block_limit; tars_transaction_data->chainID = std::string(transaction_data->chain_id); @@ -211,6 +212,8 @@ bcostars::TransactionUniquePtr convert_transaction_to_tars( { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(transaction, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction->data_hash, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction->signature, NULL); try { @@ -218,19 +221,13 @@ bcostars::TransactionUniquePtr convert_transaction_to_tars( auto TransactionDataUniquePtr = convert_transaction_data_to_tars(transaction->transaction_data); tars_transaction->data = *TransactionDataUniquePtr; - if (transaction->data_hash) + for (size_t i = 0; i < transaction->data_hash->length; ++i) { - for (size_t i = 0; i < transaction->data_hash->length; ++i) - { - tars_transaction->dataHash.push_back(transaction->data_hash->buffer[i]); - } + tars_transaction->dataHash.push_back(transaction->data_hash->buffer[i]); } - if (transaction->signature) + for (size_t i = 0; i < transaction->signature->length; ++i) { - for (size_t i = 0; i < transaction->signature->length; ++i) - { - tars_transaction->signature.push_back(transaction->signature->buffer[i]); - } + tars_transaction->signature.push_back(transaction->signature->buffer[i]); } if (transaction->sender) { @@ -319,15 +316,16 @@ struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct_with // BCOS_SDK_C_PARAMS_VERIFICATION(to, NULL); BCOS_SDK_C_PARAMS_VERIFICATION(input, NULL); // BCOS_SDK_C_PARAMS_VERIFICATION(abi, NULL); - BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must >= 0", NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((input[0] != '\0'), "input can not be empty string", NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must > 0", NULL); try { struct bcos_sdk_c_transaction_data* transaction_data_struct = (struct bcos_sdk_c_transaction_data*)malloc(sizeof(struct bcos_sdk_c_transaction_data)); - auto bytesInput = fromHexString(input); std::string toStr = to ? to : ""; std::string abiStr = abi ? abi : ""; + auto bytesInput = fromHexString(input); TransactionBuilder builder; std::string nonceStr = builder.generateRandomStr(); @@ -380,8 +378,8 @@ struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct_with BCOS_SDK_C_PARAMS_VERIFICATION(bytes_input, NULL); // BCOS_SDK_C_PARAMS_VERIFICATION(abi, NULL); BCOS_SDK_C_PARAMS_VERIFY_CONDITION( - (bytes_input_length > 0), "bytes input length must >= 0", NULL); - BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must >= 0", NULL); + (bytes_input_length > 0), "bytes input length must > 0", NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must > 0", NULL); try { @@ -485,7 +483,7 @@ const char* bcos_sdk_encode_transaction_data_struct( auto tars_transaction_data = convert_transaction_data_to_tars(transaction_data); TransactionBuilder builder; auto encodedTransactionData = builder.encodeTransactionData(*tars_transaction_data); - auto hex_tx_data_str = toHexString(*encodedTransactionData); + auto hex_tx_data_str = bcos::toHexString(*encodedTransactionData); return strdup(hex_tx_data_str->c_str()); } catch (const std::exception& e) @@ -537,7 +535,13 @@ const char* bcos_sdk_encode_transaction_data_struct_to_json( struct bcos_sdk_c_transaction_data* bcos_sdk_decode_transaction_data_struct( const char* transaction_data_hex_str) { + bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data_hex_str, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((transaction_data_hex_str[0] != '\0'), + "transaction_data_hex_str can not be empty string", NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((strlen(transaction_data_hex_str) % 2 != 1), + "the length of transaction_data_hex_str must be an even number", NULL); + try { TransactionBuilder builder; @@ -573,6 +577,8 @@ struct bcos_sdk_c_transaction_data* bcos_sdk_decode_transaction_data_struct_with { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data_json_str, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((transaction_data_json_str[0] != '\0'), + "transaction_data_json_str can not be empty string", NULL); try { @@ -652,6 +658,8 @@ const char* bcos_sdk_calc_transaction_data_struct_hash_with_hex( "invalid crypto type, it must be BCOS_C_SDK_ECDSA_TYPE(ecdsa crypto type) or " "BCOS_C_SDK_SM_TYPE(sm crypto type)", NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (transaction_data_hex[0] != '\0'), "transaction_data_hex can not be empty string", NULL); try { @@ -694,6 +702,10 @@ struct bcos_sdk_c_transaction* bcos_sdk_create_transaction_struct( BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); BCOS_SDK_C_PARAMS_VERIFICATION(signature, NULL); BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data_hash, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (signature[0] != '\0'), "signature can not be empty string", NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (transaction_data_hash[0] != '\0'), "transaction_data_hash can not be empty string", NULL); try { @@ -805,6 +817,10 @@ const char* bcos_sdk_create_encoded_transaction( BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); BCOS_SDK_C_PARAMS_VERIFICATION(signature, NULL); BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data_hash, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (signature[0] != '\0'), "signature can not be empty string", NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (transaction_data_hash[0] != '\0'), "transaction_data_hash can not be empty string", NULL); try { @@ -897,7 +913,12 @@ const char* bcos_sdk_encode_transaction_struct_to_json(struct bcos_sdk_c_transac */ struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct(const char* transaction_hex_str) { + bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(transaction_hex_str, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (transaction_hex_str[0] != '\0'), "transaction_hex_str can not be empty string", NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((strlen(transaction_hex_str) % 2 != 1), + "the length of transaction_hex_str must be an even number", NULL); try { @@ -930,6 +951,8 @@ struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct_with_json( { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(transaction_json_str, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (transaction_json_str[0] != '\0'), "transaction_json_str can not be empty string", NULL); try { diff --git a/cmake/Coverage.cmake b/cmake/Coverage.cmake new file mode 100644 index 000000000..979ac212c --- /dev/null +++ b/cmake/Coverage.cmake @@ -0,0 +1,41 @@ +# ------------------------------------------------------------------------------ +# Copyright (C) 2021 FISCO BCOS. +# SPDX-License-Identifier: Apache-2.0 +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# ------------------------------------------------------------------------------ +# File: Coverage.cmake +# Function: Define coverage related functions +# ------------------------------------------------------------------------------ +# REMOVE_FILE_PATTERN eg.: '/usr*' '${CMAKE_SOURCE_DIR}/deps**' '${CMAKE_SOURCE_DIR}/evmc*' ‘${CMAKE_SOURCE_DIR}/fisco-bcos*’ +function(config_coverage TARGET REMOVE_FILE_PATTERN) + find_program(LCOV_TOOL lcov) + message(STATUS "lcov tool: ${LCOV_TOOL}") + if (LCOV_TOOL) + message(STATUS "coverage dir: " ${CMAKE_BINARY_DIR}) + message(STATUS "coverage TARGET: " ${TARGET}) + message(STATUS "coverage REMOVE_FILE_PATTERN: " ${REMOVE_FILE_PATTERN}) + if (APPLE) + add_custom_target(${TARGET} + COMMAND ${LCOV_TOOL} -keep-going --ignore-errors inconsistent,source --rc lcov_branch_coverage=1 -o ${CMAKE_BINARY_DIR}/coverage.info.in -c -d ${CMAKE_BINARY_DIR}/ + COMMAND ${LCOV_TOOL} -keep-going --ignore-errors inconsistent,source --rc lcov_branch_coverage=1 -r ${CMAKE_BINARY_DIR}/coverage.info.in '*MacOS*' '/usr*' '.*vcpkg_installed*' '.*boost/*' '*test*' '*build*' '*deps*' ${REMOVE_FILE_PATTERN} -o ${CMAKE_BINARY_DIR}/coverage.info + COMMAND genhtml --keep-going --ignore-errors inconsistent,source --rc lcov_branch_coverage=1 -q -o ${CMAKE_BINARY_DIR}/CodeCoverage ${CMAKE_BINARY_DIR}/coverage.info) + else() + add_custom_target(${TARGET} + COMMAND ${LCOV_TOOL} --rc lcov_branch_coverage=1 -o ${CMAKE_BINARY_DIR}/coverage.info.in -c -d ${CMAKE_BINARY_DIR}/ + COMMAND ${LCOV_TOOL} --rc lcov_branch_coverage=1 -r ${CMAKE_BINARY_DIR}/coverage.info.in '*MacOS*' '/usr*' '*test*' '*build*' '*deps*' ${REMOVE_FILE_PATTERN} -o ${CMAKE_BINARY_DIR}/coverage.info + COMMAND genhtml --rc lcov_branch_coverage=1 -q -o ${CMAKE_BINARY_DIR}/CodeCoverage ${CMAKE_BINARY_DIR}/coverage.info) + endif() + else () + message(FATAL_ERROR "Can't find lcov tool. Please install lcov") + endif() +endfunction() \ No newline at end of file diff --git a/test/testTransactionStruct.cpp b/test/testTransactionStruct.cpp index 309a8212b..b4f668d61 100644 --- a/test/testTransactionStruct.cpp +++ b/test/testTransactionStruct.cpp @@ -25,44 +25,218 @@ #include "bcos-c-sdk/bcos_sdk_c_uti_keypair.h" #include "bcos-c-sdk/bcos_sdk_c_uti_tx.h" #include "bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h" +#include #include #include +using namespace bcos; + struct TestTxStructFixture { }; BOOST_FIXTURE_TEST_SUITE(TransactionStructTest, TestTxStructFixture) -BOOST_AUTO_TEST_CASE(test_create_transaction_data_struct_with_hex_input) +const auto input = + "0x3590b49f00000000000000000000000000000000000000000000000000000000000000200000000000000000" + "00000000000000000000000000000000000000000000001748656c6c6f20464953434f2d42434f5320332e3021" + "2121000000000000000000"; + +const char* abi_with_chinese = + "[{\"inputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"inputs\":[]," + "\"name\":\"get\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"李伟光\",\"type\":" + "\"string\"}]" + ",\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":" + "\"string\",\"name\":\"n\",\"type\":\"string\"}],\"name\":\"set\",\"outputs\":[]," + "\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]"; + +struct bcos_sdk_c_transaction_data* createTxDataStructWithHexInput() +{ + auto group_id = ""; + auto chain_id = ""; + auto to = ""; + auto abi = ""; + int64_t block_limit = 1; + auto txDataWithHex = bcos_sdk_create_transaction_data_struct_with_hex_input( + group_id, chain_id, to, input, abi, block_limit); + + return txDataWithHex; +} + +struct bcos_sdk_c_transaction_data* createTxDataStructWithByteInput() { auto group_id = ""; auto chain_id = ""; auto to = ""; - auto input = ""; + auto abi = ""; + int64_t block_limit = 1; + auto bytesInput = fromHexString(input); + + auto txDataWithBytes = bcos_sdk_create_transaction_data_struct_with_bytes( + group_id, chain_id, to, bytesInput->data(), bytesInput->size(), abi, block_limit); + + return txDataWithBytes; +} + +void compareTxStruct(struct bcos_sdk_c_transaction* txStruct, + struct bcos_sdk_c_transaction_data* txDataStruct, const char* signature, const char* extraData, + int64_t attribute) +{ + // transaction_data + BOOST_TEST(txStruct != nullptr); + BOOST_TEST(txStruct->transaction_data->version == txDataStruct->version); + BOOST_TEST(txStruct->transaction_data->block_limit == txDataStruct->block_limit); + BOOST_TEST(txStruct->transaction_data->chain_id == txDataStruct->chain_id); + BOOST_TEST(txStruct->transaction_data->version == txDataStruct->version); + BOOST_TEST(txStruct->transaction_data->group_id == txDataStruct->group_id); + BOOST_TEST(txStruct->transaction_data->nonce == txDataStruct->nonce); + BOOST_TEST(txStruct->transaction_data->to == txDataStruct->to); + BOOST_TEST(txStruct->transaction_data->abi == txDataStruct->abi); + std::string txDataInputString( + reinterpret_cast(txDataStruct->input->buffer), txDataStruct->input->length); + std::string txInputString(reinterpret_cast(txStruct->transaction_data->input->buffer), + txStruct->transaction_data->input->length); + BOOST_TEST(txDataInputString == txInputString); + + // signature + std::string txStructSignatureString( + reinterpret_cast(txStruct->signature->buffer), txStruct->signature->length); + auto bytesSignature = fromHexString(signature); + std::string fromSignatureHexString(bytesSignature->begin(), bytesSignature->end()); + BOOST_TEST(fromSignatureHexString == txStructSignatureString); + + // attribute + BOOST_TEST(txStruct->attribute == attribute); + + // extra_data + BOOST_TEST(txStruct->extra_data == extraData); +} + +BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithHexInput) +{ + // create transaction_data_struct failed (block_limit = 0) + auto group_id = ""; + auto chain_id = ""; + auto to = ""; auto abi = ""; int64_t block_limit = 0; auto txData = bcos_sdk_create_transaction_data_struct_with_hex_input( group_id, chain_id, to, input, abi, block_limit); auto success = bcos_sdk_is_last_opr_success(); - // if (!success) - // { - // std::cerr << bcos_sdk_get_last_error_msg() << std::endl; - // } + BOOST_TEST(success == false); BOOST_TEST(txData == nullptr); BOOST_TEST(bcos_sdk_get_last_error() == -1); - BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("block limit must >= 0")); + BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("block limit must > 0")); + // create transaction_data_struct failed (input = "") block_limit = 1; + txData = bcos_sdk_create_transaction_data_struct_with_hex_input( + group_id, chain_id, to, "", abi, block_limit); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txData == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("input can not be empty string")); + + // create transaction_data_struct success txData = bcos_sdk_create_transaction_data_struct_with_hex_input( group_id, chain_id, to, input, abi, block_limit); success = bcos_sdk_is_last_opr_success(); - if (!success) - { - // BOOST_TEST_MESSAGE(bcos_sdk_get_last_error_msg()); - std::cerr << bcos_sdk_get_last_error_msg() << std::endl; - } + + BOOST_TEST(success == true); + BOOST_TEST(txData != nullptr); + BOOST_TEST(txData->group_id == group_id); + BOOST_TEST(txData->chain_id == chain_id); + BOOST_TEST(txData->to == to); + BOOST_TEST(txData->abi == abi); + BOOST_TEST(txData->block_limit == block_limit); + auto bytesInput = fromHexString(input); + std::string fromInputHexString(bytesInput->begin(), bytesInput->end()); + std::string txDataInputString( + reinterpret_cast(txData->input->buffer), txData->input->length); + BOOST_TEST(txDataInputString == fromInputHexString); + + bcos_sdk_destroy_transaction_data_struct(txData); +} + +BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithByteInput) +{ + auto bytesInput = fromHexString(input); + + // create transaction_data_struct failed (block_limit = 0) + auto group_id = ""; + auto chain_id = ""; + auto to = ""; + auto abi = ""; + uint32_t bytes_input_length = 10; + int64_t block_limit = 0; + auto txData = bcos_sdk_create_transaction_data_struct_with_bytes( + group_id, chain_id, to, bytesInput->data(), bytes_input_length, abi, block_limit); + auto success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txData == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("block limit must > 0")); + + // create transaction_data_struct failed (bytes_input_length = 0) + bytes_input_length = 0; + block_limit = 1; + txData = bcos_sdk_create_transaction_data_struct_with_bytes( + group_id, chain_id, to, bytesInput->data(), bytes_input_length, abi, block_limit); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txData == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("bytes input length must > 0")); + + // create transaction_data_struct failed (group_id = NULL) + group_id = nullptr; + txData = bcos_sdk_create_transaction_data_struct_with_bytes( + group_id, chain_id, to, bytesInput->data(), bytesInput->size(), abi, block_limit); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txData == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // create transaction_data_struct failed (group_id has Chinese characters) + group_id = "区块链群组1"; + txData = bcos_sdk_create_transaction_data_struct_with_bytes( + group_id, chain_id, to, bytesInput->data(), bytesInput->size(), abi, block_limit); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == true); + BOOST_TEST(txData != nullptr); + BOOST_TEST(txData->group_id == group_id); + BOOST_TEST(txData->chain_id == chain_id); + BOOST_TEST(txData->to == to); + BOOST_TEST(txData->abi == abi); + BOOST_TEST(txData->block_limit == block_limit); + std::string fromInputHexString1(bytesInput->begin(), bytesInput->end()); + std::string txDataInputString1( + reinterpret_cast(txData->input->buffer), txData->input->length); + BOOST_TEST(txDataInputString1 == fromInputHexString1); + + // create transaction_data_struct failed (chain_id = NULL) + chain_id = nullptr; + txData = bcos_sdk_create_transaction_data_struct_with_bytes( + group_id, chain_id, to, bytesInput->data(), bytesInput->size(), abi, block_limit); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txData == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // create transaction_data_struct failed (chain_id has Chinese characters) + chain_id = "区块链1"; + txData = bcos_sdk_create_transaction_data_struct_with_bytes( + group_id, chain_id, to, bytesInput->data(), bytesInput->size(), abi, block_limit); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == true); BOOST_TEST(txData != nullptr); BOOST_TEST(txData->group_id == group_id); @@ -70,12 +244,600 @@ BOOST_AUTO_TEST_CASE(test_create_transaction_data_struct_with_hex_input) BOOST_TEST(txData->to == to); BOOST_TEST(txData->abi == abi); BOOST_TEST(txData->block_limit == block_limit); + std::string fromInputHexString2(bytesInput->begin(), bytesInput->end()); + std::string txDataInputString2( + reinterpret_cast(txData->input->buffer), txData->input->length); + BOOST_TEST(txDataInputString2 == fromInputHexString2); - // auto json = bcos_sdk_encode_transaction_data_struct(txData); - // BOOST_TEST(json == std::string("{\"groupId\":\"\",\"chainId\":\"\",\"to\":\"\",\"input\":\"\"," - // "\"abi\":\"\",\"blockLimit\":1}")); + // create transaction_data_struct failed (abi has Chinese characters) + chain_id = ""; + txData = bcos_sdk_create_transaction_data_struct_with_bytes(group_id, chain_id, to, + bytesInput->data(), bytesInput->size(), abi_with_chinese, block_limit); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == true); + BOOST_TEST(txData != nullptr); + BOOST_TEST(txData->group_id == group_id); + BOOST_TEST(txData->chain_id == chain_id); + BOOST_TEST(txData->to == to); + BOOST_TEST(txData->abi == abi_with_chinese); + BOOST_TEST(txData->block_limit == block_limit); + std::string fromInputHexString3(bytesInput->begin(), bytesInput->end()); + std::string txDataInputString3( + reinterpret_cast(txData->input->buffer), txData->input->length); + BOOST_TEST(txDataInputString3 == fromInputHexString3); + + // create transaction_data_struct success + group_id = ""; + txData = bcos_sdk_create_transaction_data_struct_with_bytes( + group_id, chain_id, to, bytesInput->data(), bytesInput->size(), abi, block_limit); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == true); + BOOST_TEST(txData != nullptr); + BOOST_TEST(txData->group_id == group_id); + BOOST_TEST(txData->chain_id == chain_id); + BOOST_TEST(txData->to == to); + BOOST_TEST(txData->abi == abi); + BOOST_TEST(txData->block_limit == block_limit); + std::string fromInputHexString(bytesInput->begin(), bytesInput->end()); + std::string txDataInputString( + reinterpret_cast(txData->input->buffer), txData->input->length); + BOOST_TEST(txDataInputString == fromInputHexString); bcos_sdk_destroy_transaction_data_struct(txData); } +BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStruct) +{ + auto txDataWithHex = createTxDataStructWithHexInput(); + auto txDataWithBytes = createTxDataStructWithByteInput(); + + // 1. test tx data encode decode hex + // encode to hex failed (transaction_data == NULL) + auto encodedTxDataHex = bcos_sdk_encode_transaction_data_struct(nullptr); + auto hexSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(hexSuccess == false); + BOOST_TEST(encodedTxDataHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // encode to hex success + encodedTxDataHex = bcos_sdk_encode_transaction_data_struct(txDataWithHex); + hexSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(hexSuccess == true); + BOOST_TEST(encodedTxDataHex != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + encodedTxDataHex = bcos_sdk_encode_transaction_data_struct(txDataWithBytes); + hexSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(hexSuccess == true); + BOOST_TEST(encodedTxDataHex != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // decode hex failed (transaction_data_hex_str == NULL) + auto decodedTxDataHex = bcos_sdk_decode_transaction_data_struct(nullptr); + hexSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(hexSuccess == false); + BOOST_TEST(decodedTxDataHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // decode hex failed (transaction_data_hex_str == "") + decodedTxDataHex = bcos_sdk_decode_transaction_data_struct(""); + hexSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(hexSuccess == false); + BOOST_TEST(decodedTxDataHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("transaction_data_hex_str can not be empty string")); + + // decode hex failed (the length of transaction_data_hex_str is not even number) + decodedTxDataHex = bcos_sdk_decode_transaction_data_struct("0xabc"); + hexSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(hexSuccess == false); + BOOST_TEST(decodedTxDataHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("the length of transaction_data_hex_str must be an even number")); + + // decode hex success + decodedTxDataHex = bcos_sdk_decode_transaction_data_struct(encodedTxDataHex); + hexSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(hexSuccess == true); + BOOST_TEST(decodedTxDataHex != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + BOOST_TEST(decodedTxDataHex->version == txDataWithBytes->version); + BOOST_TEST(decodedTxDataHex->group_id == txDataWithBytes->group_id); + BOOST_TEST(decodedTxDataHex->chain_id == txDataWithBytes->chain_id); + BOOST_TEST(decodedTxDataHex->to == txDataWithBytes->to); + BOOST_TEST(decodedTxDataHex->abi == txDataWithBytes->abi); + BOOST_TEST(decodedTxDataHex->block_limit == txDataWithBytes->block_limit); + BOOST_TEST(decodedTxDataHex->nonce == txDataWithBytes->nonce); + std::string txDataInputString( + reinterpret_cast(txDataWithBytes->input->buffer), txDataWithBytes->input->length); + std::string decodedHexTxDataInputString( + reinterpret_cast(decodedTxDataHex->input->buffer), decodedTxDataHex->input->length); + BOOST_TEST(txDataInputString == decodedHexTxDataInputString); + + // 2. test tx data encode decode json + // encode to json failed (transaction_data == NULL) + auto encodedTxDataJson = bcos_sdk_encode_transaction_data_struct_to_json(nullptr); + auto jsonSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(jsonSuccess == false); + BOOST_TEST(encodedTxDataJson == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // encode to json success + encodedTxDataJson = bcos_sdk_encode_transaction_data_struct_to_json(txDataWithHex); + jsonSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(jsonSuccess == true); + BOOST_TEST(encodedTxDataJson != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + encodedTxDataJson = bcos_sdk_encode_transaction_data_struct_to_json(txDataWithBytes); + jsonSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(jsonSuccess == true); + BOOST_TEST(encodedTxDataJson != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // decode json failed (transaction_data_json_str == NULL) + auto decodedTxDataJson = bcos_sdk_decode_transaction_data_struct_with_json(nullptr); + jsonSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(jsonSuccess == false); + BOOST_TEST(decodedTxDataJson == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // decode json failed (transaction_data_json_str == "") + decodedTxDataJson = bcos_sdk_decode_transaction_data_struct_with_json(""); + jsonSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(jsonSuccess == false); + BOOST_TEST(decodedTxDataJson == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("transaction_data_json_str can not be empty string")); + + // decode json success + decodedTxDataJson = bcos_sdk_decode_transaction_data_struct_with_json(encodedTxDataJson); + jsonSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(jsonSuccess == true); + BOOST_TEST(decodedTxDataJson != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + BOOST_TEST(decodedTxDataJson->version == txDataWithBytes->version); + BOOST_TEST(decodedTxDataJson->group_id == txDataWithBytes->group_id); + BOOST_TEST(decodedTxDataJson->chain_id == txDataWithBytes->chain_id); + BOOST_TEST(decodedTxDataJson->to == txDataWithBytes->to); + BOOST_TEST(decodedTxDataJson->abi == txDataWithBytes->abi); + BOOST_TEST(decodedTxDataJson->block_limit == txDataWithBytes->block_limit); + BOOST_TEST(decodedTxDataJson->nonce == txDataWithBytes->nonce); + std::string decodedJsonTxDataInputJson( + reinterpret_cast(decodedTxDataJson->input->buffer), + decodedTxDataJson->input->length); + BOOST_TEST(txDataInputString == decodedJsonTxDataInputJson); + + bcos_sdk_destroy_transaction_data_struct(txDataWithHex); + bcos_sdk_destroy_transaction_data_struct(txDataWithBytes); + bcos_sdk_destroy_transaction_data_struct(decodedTxDataHex); + bcos_sdk_destroy_transaction_data_struct(decodedTxDataJson); +} + +BOOST_AUTO_TEST_CASE(testCalculateTxDataHash) +{ + auto txDataWithHex = createTxDataStructWithHexInput(); + auto txDataWithByte = createTxDataStructWithByteInput(); + auto encodedtxDataWithHex = bcos_sdk_encode_transaction_data_struct(txDataWithHex); + auto encodedtxDataWithByte = bcos_sdk_encode_transaction_data_struct(txDataWithByte); + + // 1. test cal tx data hash with struct + // calculate tx data hash failed (cryptoType not in 0,1) + auto cryptoType = 2; + auto txDataHash = bcos_sdk_calc_transaction_data_struct_hash(cryptoType, txDataWithHex); + auto success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txDataHash == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("invalid crypto type, it must be BCOS_C_SDK_ECDSA_TYPE(ecdsa crypto " + "type) or BCOS_C_SDK_SM_TYPE(sm crypto type)")); + + // calculate tx data hash failed (cryptoType is negative number) + cryptoType = -11; + txDataHash = bcos_sdk_calc_transaction_data_struct_hash(cryptoType, txDataWithHex); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txDataHash == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("invalid crypto type, it must be BCOS_C_SDK_ECDSA_TYPE(ecdsa crypto " + "type) or BCOS_C_SDK_SM_TYPE(sm crypto type)")); + + // calculate tx data hash failed (transaction_data == NULL) + cryptoType = 1; + txDataHash = bcos_sdk_calc_transaction_data_struct_hash(cryptoType, nullptr); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txDataHash == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // calculate tx data hash success (txDataWithHex) + cryptoType = 1; + txDataHash = bcos_sdk_calc_transaction_data_struct_hash(cryptoType, txDataWithHex); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == true); + BOOST_TEST(txDataHash != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // calculate tx data hash success (txDataWithByte) + txDataHash = bcos_sdk_calc_transaction_data_struct_hash(cryptoType, txDataWithByte); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == true); + BOOST_TEST(txDataHash != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // 2. test cal tx data hash with tx_data_hex + // calculate tx data hash with hex failed (cryptoType not in 0,1) + cryptoType = 2; + auto txDataHashWithHex = + bcos_sdk_calc_transaction_data_struct_hash_with_hex(cryptoType, encodedtxDataWithHex); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txDataHashWithHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("invalid crypto type, it must be BCOS_C_SDK_ECDSA_TYPE(ecdsa crypto " + "type) or BCOS_C_SDK_SM_TYPE(sm crypto type)")); + + // calculate tx data hash failed (cryptoType is negative number) + cryptoType = -11; + txDataHashWithHex = + bcos_sdk_calc_transaction_data_struct_hash_with_hex(cryptoType, encodedtxDataWithHex); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txDataHashWithHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("invalid crypto type, it must be BCOS_C_SDK_ECDSA_TYPE(ecdsa crypto " + "type) or BCOS_C_SDK_SM_TYPE(sm crypto type)")); + + // calculate tx data hash with hex failed (transaction_data_hex == NULL) + cryptoType = 1; + txDataHashWithHex = bcos_sdk_calc_transaction_data_struct_hash_with_hex(cryptoType, nullptr); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txDataHashWithHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // calculate tx data hash with hex failed (transaction_data_hex == "") + txDataHashWithHex = bcos_sdk_calc_transaction_data_struct_hash_with_hex(cryptoType, ""); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txDataHashWithHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("transaction_data_hex can not be empty string")); + + // calculate tx data hash with hex success (encodedtxDataWithHex) + cryptoType = 1; + txDataHashWithHex = + bcos_sdk_calc_transaction_data_struct_hash_with_hex(cryptoType, encodedtxDataWithHex); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == true); + BOOST_TEST(txDataHashWithHex != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // calculate tx data hash with hex success (encodedtxDataWithByte) + txDataHashWithHex = + bcos_sdk_calc_transaction_data_struct_hash_with_hex(cryptoType, encodedtxDataWithByte); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == true); + BOOST_TEST(txDataHashWithHex != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // 3. compare hash + BOOST_TEST(txDataHash == txDataHashWithHex); + + bcos_sdk_destroy_transaction_data_struct(txDataWithHex); + bcos_sdk_destroy_transaction_data_struct(txDataWithByte); +} + +BOOST_AUTO_TEST_CASE(testCreateTxStruct) +{ + auto txDataStruct = createTxDataStructWithHexInput(); + auto transactionDataHash = bcos_sdk_calc_transaction_data_struct_hash(1, txDataStruct); + void* key_pair = bcos_sdk_create_keypair(1); + auto signature = bcos_sdk_sign_transaction_data_hash(key_pair, transactionDataHash); + auto extraData = ""; + int64_t attribute = 0; + + // create transaction_struct failed (transaction_data_struct = NULL) + auto txStruct = bcos_sdk_create_transaction_struct( + nullptr, signature, transactionDataHash, attribute, extraData); + auto success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txStruct == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // create transaction_struct failed (transaction_data_hash = NULL) + txStruct = + bcos_sdk_create_transaction_struct(txDataStruct, signature, nullptr, attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txStruct == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // create transaction_struct failed (signature = NULL) + txStruct = bcos_sdk_create_transaction_struct( + txDataStruct, nullptr, transactionDataHash, attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txStruct == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // create transaction_struct failed (transaction_data_hash = "") + txStruct = + bcos_sdk_create_transaction_struct(txDataStruct, signature, "", attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txStruct == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("transaction_data_hash can not be empty string")); + + // create transaction_struct failed (signature = "") + txStruct = bcos_sdk_create_transaction_struct( + txDataStruct, "", transactionDataHash, attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txStruct == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("signature can not be empty string")); + + // create transaction_struct success + txStruct = bcos_sdk_create_transaction_struct( + txDataStruct, signature, transactionDataHash, attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == true); + + compareTxStruct(txStruct, txDataStruct, signature, extraData, attribute); + + bcos_sdk_destroy_transaction_data_struct(txDataStruct); + bcos_sdk_destroy_transaction_struct(txStruct); +} + +BOOST_AUTO_TEST_CASE(testCreateEncodedTx) +{ + auto txDataStruct = createTxDataStructWithHexInput(); + auto transactionDataHash = bcos_sdk_calc_transaction_data_struct_hash(1, txDataStruct); + void* key_pair = bcos_sdk_create_keypair(1); + auto signature = bcos_sdk_sign_transaction_data_hash(key_pair, transactionDataHash); + auto extraData = ""; + int64_t attribute = 0; + auto txStruct = bcos_sdk_create_transaction_struct( + txDataStruct, signature, transactionDataHash, attribute, extraData); + + // 1. test bcos_sdk_create_encoded_transaction + // create encoded tx failed (transaction_data_struct = NULL) + auto encodedTx = bcos_sdk_create_encoded_transaction( + nullptr, signature, transactionDataHash, attribute, extraData); + auto success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(encodedTx == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // create encoded tx failed (transaction_data_hash = NULL) + encodedTx = + bcos_sdk_create_encoded_transaction(txDataStruct, signature, nullptr, attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(encodedTx == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // create encoded tx failed (signature = NULL) + encodedTx = bcos_sdk_create_encoded_transaction( + txDataStruct, nullptr, transactionDataHash, attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(encodedTx == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // create encoded tx failed (transaction_data_hash = "") + encodedTx = + bcos_sdk_create_encoded_transaction(txDataStruct, signature, "", attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(encodedTx == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("transaction_data_hash can not be empty string")); + + // create encoded tx failed (signature = "") + encodedTx = bcos_sdk_create_encoded_transaction( + txDataStruct, "", transactionDataHash, attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(encodedTx == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("signature can not be empty string")); + + // create encoded tx success + encodedTx = bcos_sdk_create_encoded_transaction( + txDataStruct, signature, transactionDataHash, attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == true); + BOOST_TEST(encodedTx != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // 2. test bcos_sdk_encode_transaction_struct + // create encoded tx with tx_struct failed (transaction_struct == "") + auto encodedTxWithTxStruct = bcos_sdk_encode_transaction_struct(nullptr); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(encodedTxWithTxStruct == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // create encoded tx with tx_struct success + encodedTxWithTxStruct = bcos_sdk_encode_transaction_struct(txStruct); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == true); + BOOST_TEST(encodedTxWithTxStruct != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // 3. compare encodedTx encodedTxWithTxStruct + BOOST_TEST(encodedTx == encodedTxWithTxStruct); + + bcos_sdk_destroy_transaction_data_struct(txDataStruct); + bcos_sdk_destroy_transaction_struct(txStruct); +} + +BOOST_AUTO_TEST_CASE(testEncodeDecodeTxStruct) +{ + auto txDataStruct = createTxDataStructWithHexInput(); + auto transactionDataHash = bcos_sdk_calc_transaction_data_struct_hash(1, txDataStruct); + void* key_pair = bcos_sdk_create_keypair(1); + auto signature = bcos_sdk_sign_transaction_data_hash(key_pair, transactionDataHash); + auto extraData = ""; + int64_t attribute = 0; + + auto txStruct = bcos_sdk_create_transaction_struct( + txDataStruct, signature, transactionDataHash, attribute, extraData); + + // 1. test tx encode decode hex + // encode tx to hex failed (transaction == NULL) + auto encodedTxHex = bcos_sdk_encode_transaction_struct(nullptr); + auto hexSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(hexSuccess == false); + BOOST_TEST(encodedTxHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // encode tx to hex success + encodedTxHex = bcos_sdk_encode_transaction_struct(txStruct); + hexSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(hexSuccess == true); + BOOST_TEST(encodedTxHex != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // decode hex failed (transaction_hex_str == NULL) + auto decodedTxHex = bcos_sdk_decode_transaction_struct(nullptr); + hexSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(hexSuccess == false); + BOOST_TEST(decodedTxHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // decode hex failed (transaction_hex_str == "") + decodedTxHex = bcos_sdk_decode_transaction_struct(""); + hexSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(hexSuccess == false); + BOOST_TEST(decodedTxHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("transaction_hex_str can not be empty string")); + + // decode hex failed (the length of transaction_hex_str is not even number) + decodedTxHex = bcos_sdk_decode_transaction_struct("0xabc"); + hexSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(hexSuccess == false); + BOOST_TEST(decodedTxHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("the length of transaction_hex_str must be an even number")); + + // decode hex success + decodedTxHex = bcos_sdk_decode_transaction_struct(encodedTxHex); + hexSuccess = bcos_sdk_is_last_opr_success(); + BOOST_TEST(hexSuccess == true); + BOOST_TEST(decodedTxHex != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + compareTxStruct(decodedTxHex, txDataStruct, signature, extraData, attribute); + + // 2. test tx encode decode json + // encode tx to json failed (transaction == NULL) + auto encodedTxJson = bcos_sdk_encode_transaction_struct_to_json(nullptr); + auto jsonSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(jsonSuccess == false); + BOOST_TEST(encodedTxJson == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // encode tx to json success + encodedTxJson = bcos_sdk_encode_transaction_struct_to_json(txStruct); + jsonSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(jsonSuccess == true); + BOOST_TEST(encodedTxJson != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // decode json failed (transaction_json_str == NULL) + auto decodedTxJson = bcos_sdk_decode_transaction_struct_with_json(nullptr); + jsonSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(jsonSuccess == false); + BOOST_TEST(decodedTxJson == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // decode json failed (transaction_json_str == "") + decodedTxJson = bcos_sdk_decode_transaction_struct_with_json(""); + jsonSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(jsonSuccess == false); + BOOST_TEST(decodedTxJson == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("transaction_json_str can not be empty string")); + + // decode json success + decodedTxJson = bcos_sdk_decode_transaction_struct_with_json(encodedTxJson); + jsonSuccess = bcos_sdk_is_last_opr_success(); + BOOST_TEST(jsonSuccess == true); + BOOST_TEST(decodedTxJson != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + compareTxStruct(decodedTxJson, txDataStruct, signature, extraData, attribute); + + bcos_sdk_destroy_transaction_data_struct(txDataStruct); + bcos_sdk_destroy_transaction_struct(txStruct); + bcos_sdk_destroy_transaction_struct(decodedTxHex); + bcos_sdk_destroy_transaction_struct(decodedTxJson); +} + BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file From 2d4c87626350fd2bb67886c258e35a8e1138a38a Mon Sep 17 00:00:00 2001 From: lucasli <410567249@qq.com> Date: Mon, 11 Sep 2023 16:09:00 +0800 Subject: [PATCH 08/29] add coverage report (#179) --- .github/workflows/workflow.yml | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index c2f61208f..3c8f6913d 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -118,6 +118,16 @@ jobs: run: cd build && CTEST_OUTPUT_ON_FAILURE=TRUE make test - name: run coverage run: cd build && make cov + - name: upload coverage report + uses: codecov/codecov-action@v3 + with: + token: ${{ secrets.CODECOV_TOKEN }} + files: ./build/coverage.info + version: "v0.1.15" + flags: unittests + name: c sdk coverage + fail_ci_if_error: false + verbose: true build_with_centos: name: build_with_centos runs-on: ubuntu-latest From 0da26d3079d348f0f5ce104374806a0c01d08ac5 Mon Sep 17 00:00:00 2001 From: lucasli <410567249@qq.com> Date: Wed, 13 Sep 2023 10:10:29 +0800 Subject: [PATCH 09/29] add tx UT (#182) --- .github/workflows/workflow.yml | 2 +- test/testTransaction.cpp | 83 ++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 test/testTransaction.cpp diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 3c8f6913d..db0610935 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -126,7 +126,7 @@ jobs: version: "v0.1.15" flags: unittests name: c sdk coverage - fail_ci_if_error: false + fail_ci_if_error: true verbose: true build_with_centos: name: build_with_centos diff --git a/test/testTransaction.cpp b/test/testTransaction.cpp new file mode 100644 index 000000000..45a553608 --- /dev/null +++ b/test/testTransaction.cpp @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2021 FISCO BCOS. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @file testTransaction.cpp + * @author: lucasli + * @date 2023-09-12 + */ + +#include "bcos-c-sdk/bcos_sdk_c.h" +#include "bcos-c-sdk/bcos_sdk_c_error.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_abi.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_keypair.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_tx.h" +#include +#include +#include + +using namespace bcos; + +struct TestTxFixture +{ +}; + +BOOST_FIXTURE_TEST_SUITE(TransactionTest, TestTxFixture) + +const char* g_hw_abi = + "[{\"inputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"inputs\":[]," + "\"name\":\"get\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}]" + ",\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":" + "\"string\",\"name\":\"n\",\"type\":\"string\"}],\"name\":\"set\",\"outputs\":[]," + "\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]"; + +BOOST_AUTO_TEST_CASE(testTxData) +{ + auto sm_crypto = 0; + auto group_id = "group0"; + auto chain_id = "chain0"; + int64_t block_limit = 11; + const char* extra_data = "ExtraData"; + void* key_pair = bcos_sdk_create_keypair(sm_crypto); + + const char* set_data = + bcos_sdk_abi_encode_method(g_hw_abi, "set", "[\"Hello FISCO-BCOS 3.0!!!\"]", sm_crypto); + + void* transaction_data = bcos_sdk_create_transaction_data( + group_id, chain_id, nullptr, set_data, g_hw_abi, block_limit); + + auto success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == true); + + const char* transaction_data_hash = + bcos_sdk_calc_transaction_data_hash(sm_crypto, transaction_data); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == true); + + const char* signed_hash = bcos_sdk_sign_transaction_data_hash(key_pair, transaction_data_hash); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == true); + + const char* signed_tx = bcos_sdk_create_signed_transaction_with_signed_data_ver_extra_data( + transaction_data, signed_hash, transaction_data_hash, 0, extra_data); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == true); + + bcos_sdk_destroy_transaction_data(transaction_data); + bcos_sdk_c_free((void*)transaction_data_hash); + bcos_sdk_c_free((void*)signed_hash); + bcos_sdk_c_free((void*)signed_tx); +} + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file From ed2f1d4d5a7a51ce536599edf7947ed767c6e6c1 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Thu, 23 Nov 2023 16:39:10 +0800 Subject: [PATCH 10/29] (project): project adapt vcpkg construct. (#188) --- .github/workflows/workflow.yml | 204 ++++++++---------- .gitignore | 1 + .gitmodules | 3 + CMakeLists.txt | 128 ++++++----- bcos-c-sdk/bcos_sdk_c.h | 2 +- bcos-c-sdk/bcos_sdk_c_common.h | 2 +- bcos-c-sdk/bcos_sdk_c_error.h | 8 +- bindings/java/jni/.gitignore | 24 +++ bindings/java/jni/build.gradle | 2 +- bindings/java/jni/src/main/c/CMakeLists.txt | 13 +- .../main/c/jni/org_fisco_bcos_sdk_common.h | 17 ++ .../org_fisco_bcos_sdk_jni_BcosSDKJniObj.cpp | 2 + ...org_fisco_bcos_sdk_jni_amop_AmopJniObj.cpp | 10 + ...isco_bcos_sdk_jni_event_EventSubJniObj.cpp | 4 + .../org_fisco_bcos_sdk_jni_rpc_RpcJniObj.cpp | 83 +++++-- ...isco_bcos_sdk_jni_rpc_RpcServiceJniObj.cpp | 11 +- ...dk_jni_utilities_keypair_KeyPairJniObj.cpp | 5 + ...utilities_receipt_ReceiptBuilderJniObj.cpp | 7 + ...ni_utilities_signature_SignatureJniObj.cpp | 48 +++-- ..._utilities_tx_TransactionBuilderJniObj.cpp | 40 +++- .../c/test/config/bcos_c_sdk_ipv6_sample.ini | 29 --- .../main/c/test/config/bcos_c_sdk_sample.ini | 30 --- .../test/config/bcos_c_sdk_sm_ssl_sample.ini | 29 --- .../jni/src/main/c/test/exe/CMakeLists.txt | 19 -- .../java/jni/src/main/c/test/exe/broadcast.c | 63 ------ .../src/main/c/test/exe/get-block-number.c | 72 ------- bindings/java/jni/src/main/c/test/exe/pub.c | 70 ------ .../java/jni/src/main/c/test/exe/sdk-start.c | 72 ------- bindings/java/jni/src/main/c/test/exe/sub.c | 68 ------ cmake/CompilerSettings.cmake | 44 +++- cmake/IncludeDirectories.cmake | 9 + sample/amop/broadcast.c | 4 +- sample/amop/publish.c | 4 +- sample/amop/subscribe.c | 4 +- sample/eventsub/eventsub.c | 4 +- sample/rpc/rpc.c | 2 +- sample/tx/hello_sample.c | 2 +- sample/tx/hello_sample_hsm.c | 2 +- sample/tx/tx_struct_test.c | 2 +- test/CMakeLists.txt | 7 +- vcpkg | 1 + vcpkg-configuration.json | 16 ++ vcpkg.json | 108 ++++++++++ 43 files changed, 549 insertions(+), 726 deletions(-) create mode 100644 .gitmodules create mode 100644 bindings/java/jni/.gitignore delete mode 100644 bindings/java/jni/src/main/c/test/config/bcos_c_sdk_ipv6_sample.ini delete mode 100644 bindings/java/jni/src/main/c/test/config/bcos_c_sdk_sample.ini delete mode 100644 bindings/java/jni/src/main/c/test/config/bcos_c_sdk_sm_ssl_sample.ini delete mode 100644 bindings/java/jni/src/main/c/test/exe/CMakeLists.txt delete mode 100644 bindings/java/jni/src/main/c/test/exe/broadcast.c delete mode 100644 bindings/java/jni/src/main/c/test/exe/get-block-number.c delete mode 100644 bindings/java/jni/src/main/c/test/exe/pub.c delete mode 100644 bindings/java/jni/src/main/c/test/exe/sdk-start.c delete mode 100644 bindings/java/jni/src/main/c/test/exe/sub.c create mode 100644 cmake/IncludeDirectories.cmake create mode 160000 vcpkg create mode 100644 vcpkg-configuration.json create mode 100644 vcpkg.json diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index db0610935..ef392fd8d 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -11,7 +11,7 @@ on: - "Changelog.md" - "README.md" release: - types: [published, created, edited] + types: [ published, created, edited ] env: CCACHE_DIR: ${{ github.workspace }}/ccache @@ -21,47 +21,39 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [macos-latest] + os: [ macos-latest ] steps: - uses: actions/checkout@v2 with: fetch-depth: 5 - name: Nightly default run: rustup default nightly - - uses: actions/cache@v2 + - uses: actions/cache@v3 id: cache with: path: | - /home/runner/.hunter - /github/home/.hunter - /Users/runner/.hunter - /home/runner/.hunter/_Base/Download/ - /github/home/.hunter/_Base/Download/ - /Users/runner/.hunter/_Base/Download/ + /usr/local/share/vcpkg ccache - key: hunter-clang-v3-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }} + key: vcpkg-clang-v1-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }} restore-keys: | - hunter-clang-v3-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }} - hunter-clang-v3-notest-${{ runner.temp }}-${{ github.base_ref }}- - hunter-clang-v3-notest-${{ runner.temp }}- + vcpkg-clang-v1-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }} + vcpkg-clang-v1-notest-${{ runner.temp }}-${{ github.base_ref }}- + vcpkg-clang-v1-notest-${{ runner.temp }}- - name: install macOS dependencies if: runner.os == 'macOS' run: brew install ccache lcov - - name: install Ubuntu dependencies - if: runner.os == 'Linux' - run: sudo apt install -y git curl openssl build-essential clang cmake ccache lcov + - name: fetch vcpkg + run: cd /usr/local/share/vcpkg/ && git fetch && git reset --hard && git pull && cd - - name: configure if: runner.os == 'macOS' - run: export SDKROOT=$(xcrun --sdk macosx --show-sdk-path) && CC=/usr/bin/clang CXX=/usr/bin/clang++ mkdir build && cd build && cmake ../ -DBUILD_JNI=ON -DBUILD_SAMPLE=ON -DTESTS=ON -DCOVERAGE=ON - - name: configure - if: runner.os == 'Linux' - run: CC=/usr/bin/clang CXX=/usr/bin/clang++ mkdir build && cd build && cmake ../ -DBUILD_JNI=ON -DBUILD_SAMPLE=ON -DTESTS=ON -DCOVERAGE=ON + run: | + export SDKROOT=$(xcrun --sdk macosx --show-sdk-path) && CC=/usr/bin/clang CXX=/usr/bin/clang++ + mkdir build && cd build + cmake ../ -DBUILD_JNI=ON -DBUILD_SAMPLE=ON -DTESTS=ON -DCOVERAGE=ON -DCMAKE_TOOLCHAIN_FILE=/usr/local/share/vcpkg/scripts/buildsystems/vcpkg.cmake - name: compile run: export CFLAGS="${CFLAGS} -fPIC";export CXXFLAGS="${CXXFLAGS} -fPIC"; cd build && make -j4 - name: run test run: cd build && CTEST_OUTPUT_ON_FAILURE=TRUE make test - - name: run coverage - run: cd build && make cov - uses: actions/upload-artifact@v2 with: name: libbcos-c-sdk.dylib.zip @@ -75,43 +67,32 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [ubuntu-20.04] + os: [ ubuntu-20.04 ] steps: - uses: actions/checkout@v2 with: fetch-depth: 5 - - uses: actions/cache@v2 + - uses: actions/cache@v3 id: cache with: path: | - /home/runner/.hunter - /github/home/.hunter - /Users/runner/.hunter - /home/runner/.hunter/_Base/Download/ - /github/home/.hunter/_Base/Download/ - /Users/runner/.hunter/_Base/Download/ - deps/src + /usr/local/share/vcpkg ccache - key: hunter-gcc-v2-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }} - restore-keys: | - hunter-gcc-v2-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }} - hunter-gcc-v2-notest-${{ runner.temp }}-${{ github.base_ref }}- - hunter-gcc-v2-notest-${{ runner.temp }}- - - uses: actions/cache@v2 - id: ccache - with: - path: ccache - key: cache-gcc-v2-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }} + key: vcpkg-gcc-v1-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }} restore-keys: | - ccache-gcc-v2-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }} - cache-gcc-v2-notest-${{ runner.temp }}-${{ github.base_ref }}- - cache-gcc-v2-notest-${{ runner.temp }}- + vcpkg-gcc-v1-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }} + vcpkg-gcc-v1-notest-${{ runner.temp }}-${{ github.base_ref }}- + vcpkg-gcc-v1-notest-${{ runner.temp }}- - name: install Ubuntu dependencies run: sudo apt install -y git curl openssl build-essential cmake ccache lcov + - name: fetch vcpkg + run: cd /usr/local/share/vcpkg/ && git fetch && git reset --hard && git pull && cd - - name: configure run: | export CC='gcc-10'; export CXX='g++-10' - export CFLAGS="${CFLAGS} -fPIC";export CXXFLAGS="${CXXFLAGS} -fPIC"; mkdir -p build && cd build && cmake ../ -DBUILD_JNI=ON -DBUILD_SAMPLE=ON -DTESTS=ON -DCOVERAGE=ON + export CFLAGS="${CFLAGS} -fPIC";export CXXFLAGS="${CXXFLAGS} -fPIC"; + mkdir -p build && cd build + cmake ../ -DBUILD_JNI=ON -DBUILD_SAMPLE=ON -DTESTS=ON -DCOVERAGE=ON -DCMAKE_TOOLCHAIN_FILE=/usr/local/share/vcpkg/scripts/buildsystems/vcpkg.cmake - name: compile run: cd build && make -j4 - name: run test @@ -134,7 +115,7 @@ jobs: container: image: docker.io/centos:7 volumes: - - /github/home/.hunter:/github/home/.hunter + - /usr/local/share/vcpkg:/usr/local/share/vcpkg steps: - uses: actions/checkout@v2 with: @@ -144,28 +125,31 @@ jobs: with: toolchain: nightly-2021-06-17 override: true - - uses: actions/cache@v2 + - uses: actions/cache@v3 id: cache with: path: | - /home/runner/.hunter - /github/home/.hunter - /Users/runner/.hunter - /home/runner/.hunter/_Base/Download/ - /github/home/.hunter/_Base/Download/ - /Users/runner/.hunter/_Base/Download/ + /usr/local/share/vcpkg ccache - deps/src - ccache - key: hunter-centos-v2-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }} + key: vcpkg-centos-v1-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }} restore-keys: | - hunter-centos-v2-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }} - hunter-centos-v2-notest-${{ runner.temp }}-${{ github.base_ref }}- - hunter-centos-v2-notest-${{ runner.temp }}- + vcpkg-centos-v1-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }} + vcpkg-centos-v1-notest-${{ runner.temp }}-${{ github.base_ref }}- + vcpkg-centos-v1-notest-${{ runner.temp }}- - name: install CentOS dependencies run: | yum install -y epel-release centos-release-scl - yum install -y java-11-openjdk-devel git make gcc gcc-c++ glibc-static glibc-devel openssl cmake3 ccache devtoolset-11 llvm-toolset-7.0 rh-perl530-perl libzstd-devel zlib-devel flex bison python-devel python3-devel + yum install -y https://packages.endpointdev.com/rhel/7/os/x86_64/endpoint-repo.x86_64.rpm + yum install -y java-11-openjdk-devel wget git make gcc gcc-c++ glibc-static glibc-devel openssl openssl-devel ccache devtoolset-11 llvm-toolset-7.0 rh-perl530-perl libzstd-devel zlib-devel flex bison python-devel python3-devel + - name: reinstall cmake + run: | + wget -c https://github.com/Kitware/CMake/releases/download/v3.27.8/cmake-3.27.8-linux-x86_64.tar.gz + tar -zxvf cmake-3.27.8-linux-x86_64.tar.gz && mkdir -p /usr/local/cmake && cd cmake-3.27.8-linux-x86_64 && cp -r * /usr/local/cmake + ln -s /usr/local/cmake/bin/cmake /usr/bin/cmake + cmake --version + git --version + - name: fetch vcpkg + run: cd /usr/local/share/vcpkg/ && git fetch && git reset --hard && git pull && cd - - name: configure run: | export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH" @@ -173,12 +157,11 @@ jobs: . /opt/rh/rh-perl530/enable export LIBCLANG_PATH=/opt/rh/llvm-toolset-7.0/root/lib64/ . /opt/rh/llvm-toolset-7.0/enable - alias cmake='cmake3' java -version export CFLAGS="${CFLAGS} -fPIC" export CXXFLAGS="${CXXFLAGS} -fPIC" mkdir -p build && cd build - cmake3 ../ -DHUNTER_STATUS_DEBUG=ON -DBUILD_JNI=ON -DBUILD_SAMPLE=ON -DTESTS=ON + cmake ../ -DBUILD_JNI=ON -DBUILD_SAMPLE=ON -DTESTS=ON -DCMAKE_TOOLCHAIN_FILE=/usr/local/share/vcpkg/scripts/buildsystems/vcpkg.cmake make -j4 - name: run test run: cd build && CTEST_OUTPUT_ON_FAILURE=TRUE make test @@ -191,53 +174,50 @@ jobs: name: libbcos-sdk-jni.so.zip path: bindings/java/jni/src/main/resources/META-INF/native/libbcos-sdk-jni.so build_with_windows: - name: build_with_windows - runs-on: ${{ matrix.os }} - strategy: - matrix: - os: [windows-2019] - steps: - - uses: actions/checkout@v2 - with: - fetch-depth: 5 - - uses: actions/cache@v2 - id: cache - with: - path: | - /home/runner/.hunter - /github/home/.hunter - /Users/runner/.hunter - /home/runner/.hunter/_Base/Download/ - /github/home/.hunter/_Base/Download/ - /Users/runner/.hunter/_Base/Download/ - C:/.hunter/ - C:/.hunter/_Base/Download/ - ccache - key: hunter-msvc-v3-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }} - restore-keys: | - hunter-msvc-v3-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }} - hunter-msvc-v3-notest-${{ runner.temp }}-${{ github.base_ref }}- - hunter-msvc-v3-notest-${{ runner.temp }}- - - name: Add MSbuild to PATH - uses: microsoft/setup-msbuild@v1.1 - - name: configure - if: runner.os == 'Windows' - run: mkdir -p build && cd build && cmake -G "Visual Studio 16 2019" -A x64 ../ -DHUNTER_CONFIGURATION_TYPES=Release -DHUNTER_STATUS_DEBUG=ON -DTESTS=ON -DBUILD_JNI=ON - - name: compile - run: cd build && MSBuild bcos-c-sdk.sln /p:Configuration=Release /p:Platform=x64 - - uses: actions/upload-artifact@v2 - with: - name: bcos-c-sdk.lib.zip - path: D:\a\bcos-c-sdk\bcos-c-sdk\build\Release\bcos-c-sdk.lib - - uses: actions/upload-artifact@v2 - with: - name: bcos-c-sdk.dll.zip - path: D:\a\bcos-c-sdk\bcos-c-sdk\build\Release\bcos-c-sdk.dll - - uses: actions/upload-artifact@v2 - with: - name: bcos-sdk-jni.lib.zip - path: D:\a\bcos-c-sdk\bcos-c-sdk\bindings\java\jni\src\main\resources\META-INF\native\Release\bcos-sdk-jni.lib - - uses: actions/upload-artifact@v2 - with: - name: bcos-sdk-jni.dll.zip - path: D:\a\bcos-c-sdk\bcos-c-sdk\bindings\java\jni\src\main\resources\META-INF\native\Release\bcos-sdk-jni.dll + name: build_with_windows + runs-on: ${{ matrix.os }} + strategy: + matrix: + os: [ windows-2019 ] + steps: + - uses: actions/checkout@v2 + with: + fetch-depth: 5 + - uses: actions/cache@v3 + id: cache + with: + path: | + C:\vcpkg + ccache + key: vcpkg-msvc-v1-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }} + restore-keys: | + vcpkg-msvc-v1-notest-${{ runner.temp }}-${{ github.base_ref }}-${{ hashFiles('.github/workflows/workflow.yml') }} + vcpkg-msvc-v1-notest-${{ runner.temp }}-${{ github.base_ref }}- + vcpkg-msvc-v1-notest-${{ runner.temp }}- + - name: Add MSbuild to PATH + uses: microsoft/setup-msbuild@v1.1 + - name: fetch vcpkg + run: cd C:\vcpkg && git fetch && git reset --hard && git pull && cd - + - name: configure + if: runner.os == 'Windows' + run: | + mkdir -p build && cd build + cmake -G "Visual Studio 16 2019" -A x64 ../ -DCMAKE_BUILD_TYPE=Release -DBOOST_USE_WINAPI_VERSION=BOOST_WINAPI_VERSION_WIN7 -D_WIN32_WINNT=0x0601 -DTESTS=ON -DBUILD_JNI=ON -DVCPKG_TARGET_TRIPLET='x64-windows-static' -DCMAKE_TOOLCHAIN_FILE=C:\vcpkg\scripts\buildsystems\vcpkg.cmake + - name: compile + run: cd build && MSBuild bcos-c-sdk.sln /p:Configuration=Release /p:Platform=x64 + - uses: actions/upload-artifact@v2 + with: + name: bcos-c-sdk.lib.zip + path: D:\a\bcos-c-sdk\bcos-c-sdk\build\Release\bcos-c-sdk.lib + - uses: actions/upload-artifact@v2 + with: + name: bcos-c-sdk.dll.zip + path: D:\a\bcos-c-sdk\bcos-c-sdk\build\Release\bcos-c-sdk.dll + - uses: actions/upload-artifact@v2 + with: + name: bcos-sdk-jni.lib.zip + path: D:\a\bcos-c-sdk\bcos-c-sdk\bindings\java\jni\src\main\resources\META-INF\native\Release\bcos-sdk-jni.lib + - uses: actions/upload-artifact@v2 + with: + name: bcos-sdk-jni.dll.zip + path: D:\a\bcos-c-sdk\bcos-c-sdk\bindings\java\jni\src\main\resources\META-INF\native\Release\bcos-sdk-jni.dll diff --git a/.gitignore b/.gitignore index 640c9e20c..0feed158d 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,4 @@ deps .vscode/ .idea/ .DS_Store +vcpkg_installed** diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 000000000..fe07c999a --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "vcpkg"] + path = vcpkg + url = https://github.com/microsoft/vcpkg diff --git a/CMakeLists.txt b/CMakeLists.txt index 727168e96..1ff519b90 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,67 +20,46 @@ cmake_minimum_required(VERSION 3.14) set(CMAKE_CXX_STANDARD 20) -if(NOT MSVC) - set(CMAKE_CXX_FLAGS "-pthread -fPIC -fvisibility=hidden -fvisibility-inlines-hidden -fexceptions") -endif() - -set(CMAKE_OSX_DEPLOYMENT_TARGET "13" CACHE STRING "Minimum OS X deployment version") - -# export windows dll symbol -if(WIN32) - message(STATUS "Compile on Windows") - set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS "ON") -endif() +#set(CMAKE_OSX_DEPLOYMENT_TARGET "13" CACHE STRING "Minimum OS X deployment version") # URL_BASE download domain name -include(FetchContent) - -if(NOT DEFINED URL_BASE) +if (NOT DEFINED URL_BASE) set(URL_BASE "github.com") +endif () + +if (WIN32) + add_definitions(-D_WIN32_WINNT=0x0601) + add_definitions(-DBOOST_USE_WINAPI_VERSION=BOOST_WINAPI_VERSION_WIN7) + set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS "ON") +endif () - # set(URL_BASE "github.com.cnpmjs.org") -endif() - -# include(FetchContent) -# set(BCOS_CMAKE_SCRIPTS_DIR ${CMAKE_CURRENT_BINARY_DIR}/bcos-cmake-scripts) -# FetchContent_Declare(bcos-cmake-scripts -# GIT_REPOSITORY https://${URL_BASE}/FISCO-BCOS/bcos-cmake-scripts.git -# GIT_TAG 364f603f4ec112cb79840ea4026243399d7c419b -# SOURCE_DIR ${BCOS_CMAKE_SCRIPTS_DIR} -# ) -# FetchContent_MakeAvailable(bcos-cmake-scripts) set(JAVA_SDK_JNI_CMAKE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/cmake" CACHE PATH "The cmake path for the library") # list(APPEND CMAKE_MODULE_PATH ${BCOS_CMAKE_SCRIPTS_DIR}) +list(APPEND CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake) list(APPEND CMAKE_MODULE_PATH ${JAVA_SDK_JNI_CMAKE_DIR}) -# include hunter include(Options) set(ARCH_NATIVE OFF) -if("${ARCHITECTURE}" MATCHES "aarch64") +if ("${ARCHITECTURE}" MATCHES "aarch64") set(ARCH_NATIVE ON) -endif() +endif () -include(HunterGate) -HunterGate( - URL "https://${URL_BASE}/FISCO-BCOS/hunter/archive/2b5b25ecffaba74393740bf0b8758c4e8d9297cf.tar.gz" - SHA1 "5d8365a327be260cf24d2b125a71ee7069aca28f" - FILEPATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake/config.cmake" -) +if (NOT DEFINED CMAKE_TOOLCHAIN_FILE) + find_package(Git REQUIRED) + execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive -- vcpkg WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) + set(CMAKE_TOOLCHAIN_FILE "${CMAKE_CURRENT_SOURCE_DIR}/vcpkg/scripts/buildsystems/vcpkg.cmake" + CACHE STRING "Vcpkg toolchain file") +endif () # project name -project(bcos-c-sdk VERSION "3.4.0") +project(bcos-c-sdk VERSION "3.6.0") # Debug, Release, RelWithDebInfo, MinSizeRel -if(NOT CMAKE_BUILD_TYPE) +if (NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) -endif() - -include_directories(${CMAKE_INSTALL_INCLUDEDIR}) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}) -include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) -include(InstallDependencies) +endif () # basic settings include(Options) @@ -91,15 +70,28 @@ include(InstallConfig) # install the include files for hash include(InstallInclude) +include(IncludeDirectories) +include(GNUInstallDirs) + +find_package(Boost COMPONENTS log_setup log serialization chrono system filesystem thread iostreams unit_test_framework) +find_package(OpenSSL REQUIRED) +find_package(Microsoft.GSL CONFIG REQUIRED) +find_package(fmt REQUIRED) +find_package(TBB REQUIRED) +find_package(unofficial-secp256k1 CONFIG REQUIRED) +find_package(jsoncpp CONFIG REQUIRED) +find_package(wedprcrypto REQUIRED) +find_package(HSM-CRYPTO REQUIRED) +find_package(fiscobcos REQUIRED) file(GLOB_RECURSE SOURCES "bcos-c-sdk/*.h" "bcos-c-sdk/*.cpp" "bcos-c-sdk/*.c") # rust static library linking requirements for macos -if(APPLE) +if (APPLE) set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -framework Security -framework Foundation") -else() +else () set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-as-needed -ldl") -endif() +endif () # static lib set(BCOS_C_SDK_STATIC_TARGET "bcos-c-sdk-static") @@ -108,40 +100,42 @@ set(BCOS_C_SDK_STATIC_TARGET "bcos-c-sdk-static") set(BCOS_C_SDK_TARGET "bcos-c-sdk") # add suffix for arm -if(CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "arm64" OR CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "aarch64") +if (CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "arm64" OR CMAKE_HOST_SYSTEM_PROCESSOR STREQUAL "aarch64") message(STATUS "Building arm architecture, CMAKE_HOST_SYSTEM_PROCESSOR => ${CMAKE_HOST_SYSTEM_PROCESSOR}") set(BCOS_C_SDK_TARGET "bcos-c-sdk-aarch64") set(BCOS_C_SDK_STATIC_TARGET "bcos-c-sdk-static-aarch64") -endif() +endif () add_library(${BCOS_C_SDK_STATIC_TARGET} ${SOURCES}) -target_link_libraries(${BCOS_C_SDK_STATIC_TARGET} PUBLIC bcos-cpp-sdk::bcos-cpp-sdk) +target_include_directories(${BCOS_C_SDK_STATIC_TARGET} PUBLIC $ $) +target_link_libraries(${BCOS_C_SDK_STATIC_TARGET} PUBLIC fiscobcos::bcos-cpp-sdk) add_library(${BCOS_C_SDK_TARGET} SHARED ${SOURCES}) -target_link_libraries(${BCOS_C_SDK_TARGET} PUBLIC bcos-cpp-sdk::bcos-cpp-sdk) +target_include_directories(${BCOS_C_SDK_TARGET} PUBLIC $ $) +target_link_libraries(${BCOS_C_SDK_TARGET} PUBLIC fiscobcos::bcos-cpp-sdk) add_dependencies(${BCOS_C_SDK_STATIC_TARGET} BuildInfo.h) add_dependencies(${BCOS_C_SDK_TARGET} BuildInfo.h) # build sample exec -if(BUILD_SAMPLE) +if (BUILD_SAMPLE) add_subdirectory(sample) -endif() +endif () # build sample exec -if(BUILD_JNI) +if (BUILD_JNI) # find JNI find_package(JNI REQUIRED) include_directories(${JNI_INCLUDE_DIRS}) - add_subdirectory(bindings/java/jni/src/main/c) -endif() + add_subdirectory(${CMAKE_SOURCE_DIR}/bindings/java/jni/src/main/c) +endif () # ut and binary exe if (TESTS) enable_testing() set(CTEST_OUTPUT_ON_FAILURE TRUE) add_subdirectory(test) -endif() +endif () # for code coverage if (COVERAGE) @@ -151,19 +145,19 @@ endif () # SET(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/lib) install( - TARGETS ${BCOS_C_SDK_TARGET} - EXPORT "${TARGETS_EXPORT_NAME}" - LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" - ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" - RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" - INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + TARGETS ${BCOS_C_SDK_TARGET} + EXPORT "${TARGETS_EXPORT_NAME}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" ) install( - TARGETS ${BCOS_C_SDK_STATIC_TARGET} - EXPORT "${TARGETS_EXPORT_NAME}" - LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" - ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" - RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" - INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" + TARGETS ${BCOS_C_SDK_STATIC_TARGET} + EXPORT "${TARGETS_EXPORT_NAME}" + LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" + ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" + RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" + INCLUDES DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" ) diff --git a/bcos-c-sdk/bcos_sdk_c.h b/bcos-c-sdk/bcos_sdk_c.h index 87b8abf3a..48fec951d 100644 --- a/bcos-c-sdk/bcos_sdk_c.h +++ b/bcos-c-sdk/bcos_sdk_c.h @@ -37,7 +37,7 @@ extern "C" { * * @return const char* */ -const char* bcos_sdk_version(); +const char* bcos_sdk_version(void); /** * @brief: create bcos sdk object by config object diff --git a/bcos-c-sdk/bcos_sdk_c_common.h b/bcos-c-sdk/bcos_sdk_c_common.h index ac3c8467d..b5f7811d6 100644 --- a/bcos-c-sdk/bcos_sdk_c_common.h +++ b/bcos-c-sdk/bcos_sdk_c_common.h @@ -101,7 +101,7 @@ struct bcos_sdk_c_config * @brief create bcos_sdk_c_config in default value * @return struct bcos_sdk_c_config* */ -struct bcos_sdk_c_config* bcos_sdk_c_config_create_empty(); +struct bcos_sdk_c_config* bcos_sdk_c_config_create_empty(void); /** * @brief duplicate string diff --git a/bcos-c-sdk/bcos_sdk_c_error.h b/bcos-c-sdk/bcos_sdk_c_error.h index eac0af3f6..a6e5edf43 100644 --- a/bcos-c-sdk/bcos_sdk_c_error.h +++ b/bcos-c-sdk/bcos_sdk_c_error.h @@ -53,7 +53,7 @@ extern "C" { * * @return int */ -extern int bcos_sdk_is_last_opr_success(); +extern int bcos_sdk_is_last_opr_success(void); /** * @brief gets status of the recent sync operation @@ -61,7 +61,7 @@ extern int bcos_sdk_is_last_opr_success(); * * @return int */ -extern int bcos_sdk_get_last_error(); +extern int bcos_sdk_get_last_error(void); /** * @brief gets error message of the recent sync operation, effect if bcos_sdk_get_last_error, @@ -70,12 +70,12 @@ extern int bcos_sdk_get_last_error(); * * @return const char* */ -extern const char* bcos_sdk_get_last_error_msg(); +extern const char* bcos_sdk_get_last_error_msg(void); /** * @brief clear the last error */ -extern void bcos_sdk_clear_last_error(); +extern void bcos_sdk_clear_last_error(void); /** * @brief set the last error and error message diff --git a/bindings/java/jni/.gitignore b/bindings/java/jni/.gitignore new file mode 100644 index 000000000..f5b848482 --- /dev/null +++ b/bindings/java/jni/.gitignore @@ -0,0 +1,24 @@ +.gradle/ +.idea/ +log/ +build/ +dist/ +.vscode/ +.settings/** +java-sdk.iml + +## eclipse ## +.classpath +.project +*/.settings/** +out/ +*/dist/** +*/build/** +conf/ + +## integration test files +gradle.properties +gpg.gpg + +## macOS +.DS_Store diff --git a/bindings/java/jni/build.gradle b/bindings/java/jni/build.gradle index 41f56fa4f..91b3cc879 100644 --- a/bindings/java/jni/build.gradle +++ b/bindings/java/jni/build.gradle @@ -31,7 +31,7 @@ dependencies { archivesBaseName = 'bcos-sdk-jni' group = 'org.fisco-bcos' -version = '3.5.0' +version = '3.5.1-SNAPSHOT' // Additional attribute definition ext { diff --git a/bindings/java/jni/src/main/c/CMakeLists.txt b/bindings/java/jni/src/main/c/CMakeLists.txt index 0da1f5904..ace0b0b3c 100644 --- a/bindings/java/jni/src/main/c/CMakeLists.txt +++ b/bindings/java/jni/src/main/c/CMakeLists.txt @@ -16,11 +16,6 @@ # limitations under the License. # ------------------------------------------------------------------------------ -set(JAVA_SDK_JNI_CMAKE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/cmake" CACHE PATH "The cmake path for the library") - -# list(APPEND CMAKE_MODULE_PATH ${BCOS_CMAKE_SCRIPTS_DIR}) -list(APPEND CMAKE_MODULE_PATH ${JAVA_SDK_JNI_CMAKE_DIR}) - # Debug, Release, RelWithDebInfo, MinSizeRel if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) @@ -53,14 +48,10 @@ else() endif() add_library(${FISCO_BCOS_JNI_TARGET} SHARED ${SOURCES}) -target_link_libraries(${FISCO_BCOS_JNI_TARGET} PUBLIC wedpr-crypto::crypto wedpr-crypto::extend-crypto bcos-cpp-sdk::bcos-cpp-sdk ${BCOS_C_SDK_STATIC_TARGET}) +target_link_libraries(${FISCO_BCOS_JNI_TARGET} PUBLIC wedprcrypto::crypto wedprcrypto::zkp fiscobcos::bcos-cpp-sdk ${BCOS_C_SDK_STATIC_TARGET}) add_library(${FISCO_BCOS_JNI_STATIC_TARGET} ${SOURCES}) -target_link_libraries(${FISCO_BCOS_JNI_STATIC_TARGET} PUBLIC wedpr-crypto::crypto wedpr-crypto::extend-crypto bcos-cpp-sdk::bcos-cpp-sdk ${BCOS_C_SDK_STATIC_TARGET}) - -if(BUILD_EXEC_TEST) - add_subdirectory(test/exe) -endif() +target_link_libraries(${FISCO_BCOS_JNI_STATIC_TARGET} PUBLIC wedprcrypto::crypto wedprcrypto::zkp fiscobcos::bcos-cpp-sdk ${BCOS_C_SDK_STATIC_TARGET}) SET(LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/../resources/META-INF/native/) diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_common.h b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_common.h index ddb2cd618..52724e0c5 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_common.h +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_common.h @@ -21,6 +21,23 @@ jclass bcos_sdk_c_find_jclass(JNIEnv* env, const char* className); struct bcos_sdk_c_config* create_config_from_java_obj(JNIEnv* env, jobject jconfig); +// check jstring not null +inline void checkJString(JNIEnv* env, jstring jstr) +{ + if (jstr == NULL) + { + THROW_JNI_EXCEPTION(env, "illegal NULL string parameter"); + } +} +// check jByteArray not null +inline void checkJByteArray(JNIEnv* env, jbyteArray jbyte_array) +{ + if (jbyte_array == NULL) + { + THROW_JNI_EXCEPTION(env, "illegal NULL byteArray parameter"); + } +} + #ifdef __cplusplus } #endif diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.cpp index e498093f4..a2181f843 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.cpp @@ -136,6 +136,8 @@ static void on_receive_block_notifier(const char* group, int64_t block_number, v JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_registerBlockNotifier( JNIEnv* env, jobject self, jstring jgroup, jobject jcallback) { + checkJString(env, jgroup); + void* sdk = bcos_sdk_get_native_pointer(env, self); if (!sdk) { diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_amop_AmopJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_amop_AmopJniObj.cpp index d4f527140..1dbaeceae 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_amop_AmopJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_amop_AmopJniObj.cpp @@ -181,6 +181,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_subscribeTopic__Ljava_lang_String_2Lorg_fisco_bcos_sdk_jni_amop_AmopRequestCallback_2( JNIEnv* env, jobject self, jstring jtopic, jobject jcallback) { + checkJString(env, jtopic); + void* sdk = bcos_sdk_get_native_pointer(env, self); const char* topic = env->GetStringUTFChars(jtopic, 0); @@ -261,6 +263,7 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_subscribeTopic__Ljava_lang_String_2Lorg_fisco_bcos_sdk_jni_amop_AmopJniObjRequestCallback_2( JNIEnv* env, jobject self, jstring jtopic, jobject jcallback) { + checkJString(env, jtopic); void* sdk = bcos_sdk_get_native_pointer(env, self); const char* topic = env->GetStringUTFChars(jtopic, 0); @@ -363,6 +366,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_setCallback( JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_sendAmopMsg( JNIEnv* env, jobject self, jstring jtopic, jbyteArray jdata, jint jtimeout, jobject jcallback) { + checkJString(env, jtopic); + checkJByteArray(env, jdata); void* sdk = bcos_sdk_get_native_pointer(env, self); // Note: The JNIEnv pointer, passed as the first argument to every native method, can only be @@ -399,6 +404,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_sendAmopMsg( JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_broadcastAmopMsg( JNIEnv* env, jobject self, jstring jtopic, jbyteArray jdata) { + checkJString(env, jtopic); + checkJByteArray(env, jdata); void* sdk = bcos_sdk_get_native_pointer(env, self); const char* topic = env->GetStringUTFChars(jtopic, 0); @@ -419,6 +426,9 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_broadcastAmop JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_sendResponse( JNIEnv* env, jobject self, jstring jendpoint, jstring jseq, jbyteArray jdata) { + checkJString(env, jendpoint); + checkJString(env, jseq); + checkJByteArray(env, jdata); void* sdk = bcos_sdk_get_native_pointer(env, self); const char* endpoint = env->GetStringUTFChars(jendpoint, 0); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_event_EventSubJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_event_EventSubJniObj.cpp index cb8faeec3..2ccf4a1f3 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_event_EventSubJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_event_EventSubJniObj.cpp @@ -130,6 +130,9 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_event_EventSubJniObj_stop( JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_event_EventSubJniObj_subscribeEvent( JNIEnv* env, jobject self, jstring jgroup, jstring jparams, jobject jcallback) { + checkJString(env, jgroup); + checkJString(env, jparams); + void* sdk = bcos_sdk_get_native_pointer(env, self); const char* group = env->GetStringUTFChars(jgroup, 0); const char* params = env->GetStringUTFChars(jparams, 0); @@ -168,6 +171,7 @@ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_event_EventSubJniObj_subsc JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_event_EventSubJniObj_unsubscribeEvent( JNIEnv* env, jobject self, jstring jeventId) { + checkJString(env, jeventId); void* sdk = bcos_sdk_get_native_pointer(env, self); const char* eventid = env->GetStringUTFChars(jeventId, 0); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcJniObj.cpp index 36fb232cd..4998a452e 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcJniObj.cpp @@ -5,6 +5,7 @@ #include "bcos-c-sdk/bcos_sdk_c_rpc.h" #include "jni/org_fisco_bcos_sdk_common.h" #include "org_fisco_bcos_sdk_class_cache.h" +#include "org_fisco_bcos_sdk_common.h" #include "org_fisco_bcos_sdk_exception.h" #include #include @@ -150,6 +151,7 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_genericMethod__Ljava_lang_String_2Lorg_fisco_bcos_sdk_jni_rpc_RpcCallback_2( JNIEnv* env, jobject self, jstring jdata, jobject jcallback) { + checkJString(env, jdata); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); @@ -182,6 +184,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_genericMethod__Ljava_lang_String_2Ljava_lang_String_2Lorg_fisco_bcos_sdk_jni_rpc_RpcCallback_2( JNIEnv* env, jobject self, jstring jgroup, jstring jdata, jobject jcallback) { + checkJString(env, jgroup); + checkJString(env, jdata); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); @@ -219,6 +223,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_genericMethod__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2Lorg_fisco_bcos_sdk_jni_rpc_RpcCallback_2( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jstring jdata, jobject jcallback) { + checkJString(env, jgroup); + checkJString(env, jdata); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); @@ -235,7 +241,7 @@ Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_genericMethod__Ljava_lang_String_2Ljav // group const char* group = env->GetStringUTFChars(jgroup, NULL); // node - const char* node = env->GetStringUTFChars(jnode, NULL); + const char* node = jnode ? env->GetStringUTFChars(jnode, NULL) : NULL; // data const char* data = env->GetStringUTFChars(jdata, NULL); @@ -259,12 +265,15 @@ Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_genericMethod__Ljava_lang_String_2Ljav JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_call(JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jstring jto, jstring jdata, jobject callback) { + checkJString(env, jgroup); + checkJString(env, jto); + checkJString(env, jdata); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group const char* group = env->GetStringUTFChars(jgroup, NULL); // node - const char* node = env->GetStringUTFChars(jnode, NULL); + const char* node = jnode ? env->GetStringUTFChars(jnode, NULL) : NULL; // to const char* to = env->GetStringUTFChars(jto, NULL); // data @@ -300,12 +309,15 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_call(JNIEnv* en JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_sendTransaction(JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jstring jdata, jboolean jproof, jobject callback) { + checkJString(env, jgroup); + checkJString(env, jdata); + // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group const char* group = env->GetStringUTFChars(jgroup, NULL); // node - const char* node = env->GetStringUTFChars(jnode, NULL); + const char* node = jnode ? env->GetStringUTFChars(jnode, NULL) : NULL; // data const char* data = env->GetStringUTFChars(jdata, NULL); // proof @@ -341,12 +353,14 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getTransaction( jobject self, jstring jgroup, jstring jnode, jstring jtx_hash, jboolean jproof, jobject callback) { + checkJString(env, jgroup); + checkJString(env, jtx_hash); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group const char* group = env->GetStringUTFChars(jgroup, NULL); // node - const char* node = env->GetStringUTFChars(jnode, NULL); + const char* node = jnode ? env->GetStringUTFChars(jnode, NULL) : NULL; // data const char* tx_hash = env->GetStringUTFChars(jtx_hash, NULL); // proof @@ -382,12 +396,14 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getTransactionR jobject self, jstring jgroup, jstring jnode, jstring jtx_hash, jboolean jproof, jobject callback) { + checkJString(env, jgroup); + checkJString(env, jtx_hash); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group const char* group = env->GetStringUTFChars(jgroup, NULL); // node - const char* node = env->GetStringUTFChars(jnode, NULL); + const char* node = jnode ? env->GetStringUTFChars(jnode, NULL) : NULL; // data const char* tx_hash = env->GetStringUTFChars(jtx_hash, NULL); // proof @@ -424,12 +440,14 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getBlockByHash( jobject self, jstring jgroup, jstring jnode, jstring jblock_hash, jboolean jonly_header, jboolean jonly_txhash, jobject callback) { + checkJString(env, jgroup); + checkJString(env, jblock_hash); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group const char* group = env->GetStringUTFChars(jgroup, NULL); // node - const char* node = env->GetStringUTFChars(jnode, NULL); + const char* node = jnode ? env->GetStringUTFChars(jnode, NULL) : NULL; // data const char* block_hash = env->GetStringUTFChars(jblock_hash, NULL); // only_header @@ -467,12 +485,13 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getBlockByNumbe jobject self, jstring jgroup, jstring jnode, jlong jnumber, jboolean jonly_header, jboolean jonly_txhash, jobject callback) { + checkJString(env, jgroup); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group const char* group = env->GetStringUTFChars(jgroup, NULL); // node - const char* node = env->GetStringUTFChars(jnode, NULL); + const char* node = jnode ? env->GetStringUTFChars(jnode, NULL) : NULL; // block number long block_number = static_cast(jnumber); // only_header @@ -508,12 +527,14 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getBlockByNumbe JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getBlockHashByNumber( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jlong jnumber, jobject callback) { + checkJString(env, jgroup); + // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group const char* group = env->GetStringUTFChars(jgroup, NULL); // node - const char* node = env->GetStringUTFChars(jnode, NULL); + const char* node = jnode ? env->GetStringUTFChars(jnode, NULL) : NULL; // Note: The JNIEnv pointer, passed as the first argument to every native method, can only be // used in the thread with which it is associated. It is wrong to cache the JNIEnv interface @@ -545,6 +566,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getBlockHashByN JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getBlockLimit( JNIEnv* env, jobject self, jstring jgroup) { + checkJString(env, jgroup); + // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group @@ -563,12 +586,14 @@ JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getBlockLimit( JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getBlockNumber( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jobject callback) { + checkJString(env, jgroup); + // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group const char* group = env->GetStringUTFChars(jgroup, NULL); // node - const char* node = env->GetStringUTFChars(jnode, NULL); + const char* node = jnode ? env->GetStringUTFChars(jnode, NULL) : NULL; // Note: The JNIEnv pointer, passed as the first argument to every native method, can only be // used in the thread with which it is associated. It is wrong to cache the JNIEnv interface @@ -598,12 +623,14 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getBlockNumber( JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getCode( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jstring jaddress, jobject callback) { + checkJString(env, jgroup); + checkJString(env, jaddress); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group const char* group = env->GetStringUTFChars(jgroup, NULL); // node - const char* node = env->GetStringUTFChars(jnode, NULL); + const char* node = jnode ? env->GetStringUTFChars(jnode, NULL) : NULL; // data const char* address = env->GetStringUTFChars(jaddress, NULL); @@ -635,12 +662,14 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getCode( JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getSealerList( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jobject callback) { + checkJString(env, jgroup); + // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group const char* group = env->GetStringUTFChars(jgroup, NULL); // node - const char* node = env->GetStringUTFChars(jnode, NULL); + const char* node = jnode ? env->GetStringUTFChars(jnode, NULL) : NULL; // Note: The JNIEnv pointer, passed as the first argument to every native method, can only be // used in the thread with which it is associated. It is wrong to cache the JNIEnv interface @@ -669,12 +698,14 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getSealerList( JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getObserverList( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jobject callback) { + checkJString(env, jgroup); + // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group const char* group = env->GetStringUTFChars(jgroup, NULL); // node - const char* node = env->GetStringUTFChars(jnode, NULL); + const char* node = jnode ? env->GetStringUTFChars(jnode, NULL) : NULL; // Note: The JNIEnv pointer, passed as the first argument to every native method, can only be // used in the thread with which it is associated. It is wrong to cache the JNIEnv interface @@ -703,12 +734,14 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getObserverList JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getPbftView( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jobject callback) { + checkJString(env, jgroup); + // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group const char* group = env->GetStringUTFChars(jgroup, NULL); // node - const char* node = env->GetStringUTFChars(jnode, NULL); + const char* node = jnode ? env->GetStringUTFChars(jnode, NULL) : NULL; // Note: The JNIEnv pointer, passed as the first argument to every native method, can only be // used in the thread with which it is associated. It is wrong to cache the JNIEnv interface // pointer obtained from one thread, and use that pointer in another thread. @@ -736,12 +769,14 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getPbftView( JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getPendingTxSize( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jobject callback) { + checkJString(env, jgroup); + // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group const char* group = env->GetStringUTFChars(jgroup, NULL); // node - const char* node = env->GetStringUTFChars(jnode, NULL); + const char* node = jnode ? env->GetStringUTFChars(jnode, NULL) : NULL; // Note: The JNIEnv pointer, passed as the first argument to every native method, can only be // used in the thread with which it is associated. It is wrong to cache the JNIEnv interface @@ -770,12 +805,14 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getPendingTxSiz JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getSyncStatus( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jobject callback) { + checkJString(env, jgroup); + // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group const char* group = env->GetStringUTFChars(jgroup, NULL); // node - const char* node = env->GetStringUTFChars(jnode, NULL); + const char* node = jnode ? env->GetStringUTFChars(jnode, NULL) : NULL; // Note: The JNIEnv pointer, passed as the first argument to every native method, can only be // used in the thread with which it is associated. It is wrong to cache the JNIEnv interface @@ -804,12 +841,14 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getSyncStatus( JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getSystemConfigByKey( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jstring jkey, jobject callback) { + checkJString(env, jgroup); + checkJString(env, jkey); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group const char* group = env->GetStringUTFChars(jgroup, NULL); // node - const char* node = env->GetStringUTFChars(jnode, NULL); + const char* node = jnode ? env->GetStringUTFChars(jnode, NULL) : NULL; // data const char* key = env->GetStringUTFChars(jkey, NULL); @@ -841,12 +880,14 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getSystemConfig JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getTotalTransactionCount( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jobject callback) { + checkJString(env, jgroup); + // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group const char* group = env->GetStringUTFChars(jgroup, NULL); // node - const char* node = env->GetStringUTFChars(jnode, NULL); + const char* node = jnode ? env->GetStringUTFChars(jnode, NULL) : NULL; // Note: The JNIEnv pointer, passed as the first argument to every native method, can only be // used in the thread with which it is associated. It is wrong to cache the JNIEnv interface @@ -875,6 +916,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getTotalTransac JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getGroupPeers( JNIEnv* env, jobject self, jstring jgroup, jobject callback) { + checkJString(env, jgroup); + // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group @@ -960,6 +1003,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getGroupList( JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getGroupInfo( JNIEnv* env, jobject self, jstring jgroup, jobject callback) { + checkJString(env, jgroup); + // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group @@ -1018,12 +1063,14 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getGroupInfoLis JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getGroupNodeInfo( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jobject callback) { + checkJString(env, jgroup); + // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group const char* group = env->GetStringUTFChars(jgroup, NULL); // node - const char* node = env->GetStringUTFChars(jnode, NULL); + const char* node = jnode ? env->GetStringUTFChars(jnode, NULL) : NULL; // Note: The JNIEnv pointer, passed as the first argument to every native method, can only be // used in the thread with which it is associated. It is wrong to cache the JNIEnv interface diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcServiceJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcServiceJniObj.cpp index d749e4cb7..bc94f3ff0 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcServiceJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcServiceJniObj.cpp @@ -121,16 +121,19 @@ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcServiceJniObj_sendT JNIEnv* env, jclass, jlong jsdk, jlong jkeypair, jstring jgroup, jstring jnode, jstring jto, jbyteArray jdata, jstring jabi, jint jatti, jstring jextra_data, jobject jcallback) { + checkJString(env, jgroup); + checkJByteArray(env, jdata); + void* sdk = reinterpret_cast(jsdk); void* keypair = reinterpret_cast(jkeypair); const char* group = env->GetStringUTFChars(jgroup, NULL); - const char* node = env->GetStringUTFChars(jnode, NULL); - const char* to = env->GetStringUTFChars(jto, NULL); + const char* node = jnode ? env->GetStringUTFChars(jnode, NULL) : NULL; + const char* to = jto ? env->GetStringUTFChars(jto, NULL) : NULL; jbyte* data = (jbyte*)env->GetByteArrayElements(jdata, 0); jsize len = env->GetArrayLength(jdata); - const char* abi = env->GetStringUTFChars(jabi, NULL); + const char* abi = jabi ? env->GetStringUTFChars(jabi, NULL) : NULL; int attr = jatti; - const char* extra_data = env->GetStringUTFChars(jextra_data, NULL); + const char* extra_data = jextra_data ? env->GetStringUTFChars(jextra_data, NULL) : NULL; // Note: The JNIEnv pointer, passed as the first argument to every native method, can only be // used in the thread with which it is associated. It is wrong to cache the JNIEnv interface diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj.cpp index 5a531bf2d..36845332a 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj.cpp @@ -2,6 +2,7 @@ #include "bcos-c-sdk/bcos_sdk_c_error.h" #include "bcos-c-sdk/bcos_sdk_c_uti_keypair.h" #include "org_fisco_bcos_sdk_exception.h" +#include "org_fisco_bcos_sdk_common.h" #include /* @@ -32,6 +33,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_createJniKeyPair__I( JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_createHsmKeyPair__Ljava_lang_String_2 (JNIEnv* env, jclass, jstring jhsm_lib_path) { + checkJString(env, jhsm_lib_path); const char* hsm_lib_path = env->GetStringUTFChars(jhsm_lib_path, NULL); void* keypair = bcos_sdk_create_hsm_keypair(hsm_lib_path); @@ -75,6 +77,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_createJniKeyPair__I_ JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_createHsmKeyPair___3BLjava_lang_String_2 (JNIEnv* env, jclass, jbyteArray jdata, jstring jhsm_lib_path) { + checkJString(env, jhsm_lib_path); jbyte* data = (jbyte*)env->GetByteArrayElements(jdata, 0); const char* hsm_lib_path = env->GetStringUTFChars(jhsm_lib_path, NULL); jsize len = env->GetArrayLength(jdata); @@ -98,6 +101,8 @@ JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJni JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_useHsmKeyPair (JNIEnv* env, jclass, jint jkey_index, jstring jpass_word, jstring jhsm_lib_path) { + checkJString(env, jpass_word); + checkJString(env, jhsm_lib_path); const char* password = env->GetStringUTFChars(jpass_word, NULL); const char* hsm_lib_path = env->GetStringUTFChars(jhsm_lib_path, NULL); void* keypair = bcos_sdk_use_hsm_keypair_by_keyindex_and_password((unsigned int)jkey_index, password, hsm_lib_path); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj.cpp index 330920277..1a3c1d350 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj.cpp @@ -3,6 +3,7 @@ #include "bcos-c-sdk/bcos_sdk_c_uti_keypair.h" #include "bcos-c-sdk/bcos_sdk_c_uti_receipt.h" #include "org_fisco_bcos_sdk_exception.h" +#include "org_fisco_bcos_sdk_common.h" #include /* @@ -15,6 +16,10 @@ Java_org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj_createReceipt jclass, jstring jgas_used, jstring jcontract_address, jstring jout_put, jlong jblock_number) { + checkJString(env, jgas_used); + checkJString(env, jcontract_address); + checkJString(env, jout_put); + // group id const char* gas_used = env->GetStringUTFChars(jgas_used, NULL); // chain id @@ -48,6 +53,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj_createReceipt JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj_createReceiptDataWithJson (JNIEnv *env, jclass, jstring jjson) { + checkJString(env, jjson); const char* json = env->GetStringUTFChars(jjson, NULL); void* receipt_data = bcos_sdk_create_receipt_data_with_json(json); @@ -108,6 +114,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj_encodeReceipt JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj_decodeReceiptDataToJsonObj (JNIEnv* env, jclass, jstring jreceipt_bytes) { + checkJString(env, jreceipt_bytes); const char* receipt_data = env->GetStringUTFChars(jreceipt_bytes, NULL); const char* receipt_data_json = bcos_sdk_decode_receipt_data(receipt_data); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj.cpp index b7d5319ea..c45e817e3 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj.cpp @@ -2,6 +2,7 @@ #include "bcos-c-sdk/bcos_sdk_c_error.h" #include "bcos-c-sdk/bcos_sdk_c_uti_keypair.h" #include "bcos-c-sdk/bcos_sdk_c_uti_signature.h" +#include "org_fisco_bcos_sdk_common.h" #include "org_fisco_bcos_sdk_exception.h" #include @@ -10,26 +11,29 @@ * Method: sign * Signature: (JLjava/lang/String;Ljava/lang/String;)Ljava/lang/String; */ -JNIEXPORT jbyteArray JNICALL Java_org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj_sign - (JNIEnv* env, jclass, jlong jkeypair, jstring jtx_data_hash, jstring jhsm_lib_path) +JNIEXPORT jbyteArray JNICALL Java_org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj_sign( + JNIEnv* env, jclass, jlong jkeypair, jstring jtx_data_hash, jstring jhsm_lib_path) { - const char* hsm_lib_path = env->GetStringUTFChars(jhsm_lib_path, NULL); - const char* tx_data_hash = env->GetStringUTFChars(jtx_data_hash, NULL); - void* keypair = reinterpret_cast(jkeypair); - struct bcos_sdk_c_signature_result sign_result = bcos_sdk_sign(keypair, tx_data_hash, hsm_lib_path); + checkJString(env, jtx_data_hash); + checkJString(env, jhsm_lib_path); + const char* hsm_lib_path = env->GetStringUTFChars(jhsm_lib_path, NULL); + const char* tx_data_hash = env->GetStringUTFChars(jtx_data_hash, NULL); + void* keypair = reinterpret_cast(jkeypair); + struct bcos_sdk_c_signature_result sign_result = + bcos_sdk_sign(keypair, tx_data_hash, hsm_lib_path); - env->ReleaseStringUTFChars(jhsm_lib_path, hsm_lib_path); - env->ReleaseStringUTFChars(jtx_data_hash, tx_data_hash); + env->ReleaseStringUTFChars(jhsm_lib_path, hsm_lib_path); + env->ReleaseStringUTFChars(jtx_data_hash, tx_data_hash); - int operation_status = bcos_sdk_get_last_error(); - if (operation_status == -1) - { + int operation_status = bcos_sdk_get_last_error(); + if (operation_status == -1) + { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); - } + } - jbyte *signed_data_r = (jbyte*)sign_result.r; - jbyte *signed_data_s = (jbyte*)sign_result.s; - jbyte *signed_data_v = (jbyte*)sign_result.v; + jbyte* signed_data_r = (jbyte*)sign_result.r; + jbyte* signed_data_s = (jbyte*)sign_result.s; + jbyte* signed_data_v = (jbyte*)sign_result.v; jbyteArray jsigned_data = env->NewByteArray(576); env->SetByteArrayRegion(jsigned_data, 0, 32, signed_data_r); env->SetByteArrayRegion(jsigned_data, 32, 32, signed_data_s); @@ -43,10 +47,14 @@ JNIEXPORT jbyteArray JNICALL Java_org_fisco_bcos_sdk_jni_utilities_signature_Sig * Method: verify * Signature: (I[BLjava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z */ -JNIEXPORT jboolean JNICALL Java_org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj_verify - (JNIEnv* env, jclass, jint jcrypto_type, jbyteArray jpub_key, jstring jtx_data_hash, jstring jsigned_data, - jstring jhsm_lib_path) +JNIEXPORT jboolean JNICALL Java_org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj_verify( + JNIEnv* env, jclass, jint jcrypto_type, jbyteArray jpub_key, jstring jtx_data_hash, + jstring jsigned_data, jstring jhsm_lib_path) { + checkJString(env, jtx_data_hash); + checkJString(env, jsigned_data); + checkJString(env, jhsm_lib_path); + int crypto_type = (int)jcrypto_type; jbyte* public_key = (jbyte*)env->GetByteArrayElements(jpub_key, 0); jsize pubkey_len = env->GetArrayLength(jpub_key); @@ -55,8 +63,8 @@ JNIEXPORT jboolean JNICALL Java_org_fisco_bcos_sdk_jni_utilities_signature_Signa jsize signed_data_len = strlen(signed_data); const char* hsm_lib_path = env->GetStringUTFChars(jhsm_lib_path, NULL); - bool is_verify_success = bcos_sdk_verify(crypto_type, (void*)public_key, pubkey_len, tx_data_hash, - (void*)signed_data, signed_data_len, hsm_lib_path); + bool is_verify_success = bcos_sdk_verify(crypto_type, (void*)public_key, pubkey_len, + tx_data_hash, (void*)signed_data, signed_data_len, hsm_lib_path); env->ReleaseStringUTFChars(jtx_data_hash, tx_data_hash); env->ReleaseStringUTFChars(jsigned_data, signed_data); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj.cpp index bf1eb7492..e9a1a20f8 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj.cpp @@ -2,6 +2,7 @@ #include "bcos-c-sdk/bcos_sdk_c_error.h" #include "bcos-c-sdk/bcos_sdk_c_uti_keypair.h" #include "bcos-c-sdk/bcos_sdk_c_uti_tx.h" +#include "org_fisco_bcos_sdk_common.h" #include "org_fisco_bcos_sdk_exception.h" #include @@ -16,16 +17,20 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createTransact jclass, jstring jgroup_id, jstring jchain_id, jstring jto, jstring jdata, jstring jabi, jlong jblock_limit) { + checkJString(env, jgroup_id); + checkJString(env, jchain_id); + checkJString(env, jdata); + // group id const char* group_id = env->GetStringUTFChars(jgroup_id, NULL); // chain id const char* chain_id = env->GetStringUTFChars(jchain_id, NULL); - // code - const char* to = env->GetStringUTFChars(jto, NULL); + // to + const char* to = (jto == NULL) ? NULL : env->GetStringUTFChars(jto, NULL); // data const char* data = env->GetStringUTFChars(jdata, NULL); // abi - const char* abi = env->GetStringUTFChars(jabi, NULL); + const char* abi = (jabi == NULL) ? NULL : env->GetStringUTFChars(jabi, NULL); // block limit int64_t block_limit = (int64_t)jblock_limit; @@ -55,6 +60,7 @@ JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createTransactionDataWithJson( JNIEnv* env, jclass, jstring jjson) { + checkJString(env, jjson); const char* json = env->GetStringUTFChars(jjson, NULL); void* transaction_data = bcos_sdk_create_transaction_data_with_json(json); @@ -119,6 +125,8 @@ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_decodeTransactionDataToJsonObj( JNIEnv* env, jclass, jstring jtransaction_bytes) { + checkJString(env, jtransaction_bytes); + const char* transaction_data = env->GetStringUTFChars(jtransaction_bytes, NULL); const char* transaction_data_json = bcos_sdk_decode_transaction_data(transaction_data); @@ -175,7 +183,8 @@ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_signTransactionDataHash( JNIEnv* env, jclass, jlong jkeypair, jstring jtransaction_data_hash) { - // + checkJString(env, jtransaction_data_hash); + void* keypair = reinterpret_cast(jkeypair); // transaction_data_hash const char* transaction_data_hash = env->GetStringUTFChars(jtransaction_data_hash, NULL); @@ -210,6 +219,8 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createSignedTr JNIEnv* env, jclass, jlong jtransaction_data, jstring jtransaction_data_signed_data, jstring jtransaction_data_hash, jint jattr) { + checkJString(env, jtransaction_data_hash); + checkJString(env, jtransaction_data_signed_data); void* transaction_data = reinterpret_cast(jtransaction_data); const char* transaction_data_signed_data = @@ -250,6 +261,9 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createSignedTr JNIEnv* env, jclass, jlong jtransaction_data, jstring jtransaction_data_signed_data, jstring jtransaction_data_hash, jint jattr, jstring jextra_data) { + checkJString(env, jtransaction_data_hash); + checkJString(env, jtransaction_data_signed_data); + checkJString(env, jextra_data); void* transaction_data = reinterpret_cast(jtransaction_data); const char* transaction_data_signed_data = @@ -293,6 +307,10 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createSignedTr JNIEnv* env, jclass, jlong jkeypair, jstring jgroup_id, jstring jchain_id, jstring jto, jstring jdata, jstring jabi, jlong jblock_limit, jint jattr) { + checkJString(env, jgroup_id); + checkJString(env, jchain_id); + checkJString(env, jdata); + // keypair void* keypair = reinterpret_cast(jkeypair); // group id @@ -300,11 +318,11 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createSignedTr // chain id const char* chain_id = env->GetStringUTFChars(jchain_id, NULL); // to - const char* to = env->GetStringUTFChars(jto, NULL); + const char* to = jto ? env->GetStringUTFChars(jto, NULL) : NULL; // data const char* data = env->GetStringUTFChars(jdata, NULL); // abi - const char* abi = env->GetStringUTFChars(jabi, NULL); + const char* abi = jabi ? env->GetStringUTFChars(jabi, NULL) : NULL; // block limit int64_t block_limit = (int64_t)jblock_limit; // attr @@ -375,6 +393,10 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createSignedTr JNIEnv* env, jclass, jlong jkeypair, jstring jgroup_id, jstring jchain_id, jstring jto, jstring jdata, jstring jabi, jlong jblock_limit, jint jattr, jstring jextra_data) { + checkJString(env, jgroup_id); + checkJString(env, jchain_id); + checkJString(env, jdata); + // keypair void* keypair = reinterpret_cast(jkeypair); // group id @@ -382,17 +404,17 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createSignedTr // chain id const char* chain_id = env->GetStringUTFChars(jchain_id, NULL); // to - const char* to = env->GetStringUTFChars(jto, NULL); + const char* to = jto ? env->GetStringUTFChars(jto, NULL) : NULL; // data const char* data = env->GetStringUTFChars(jdata, NULL); // abi - const char* abi = env->GetStringUTFChars(jabi, NULL); + const char* abi = jabi ? env->GetStringUTFChars(jabi, NULL) : NULL; // block limit int64_t block_limit = (int64_t)jblock_limit; // attr int attr = (int64_t)jattr; // extra data - const char* extra_data = env->GetStringUTFChars(jextra_data, NULL); + const char* extra_data = jextra_data ? env->GetStringUTFChars(jextra_data, NULL) : NULL; char* tx_hash = NULL; char* signed_tx = NULL; diff --git a/bindings/java/jni/src/main/c/test/config/bcos_c_sdk_ipv6_sample.ini b/bindings/java/jni/src/main/c/test/config/bcos_c_sdk_ipv6_sample.ini deleted file mode 100644 index fc3480bc2..000000000 --- a/bindings/java/jni/src/main/c/test/config/bcos_c_sdk_ipv6_sample.ini +++ /dev/null @@ -1,29 +0,0 @@ -; common -[common] - ; thread pool size for network send/receive/handle message - thread_pool_size = 8 - ; timeout for send message(ms) - message_timeout_ms = 10000 - -; ssl -[ssl] - ; if ssl connection is disabled - disable = false - ; type: ssl or sm_ssl - type = sm_ssl - ; directory the certificates located in - ca_path=./conf - ; the ca certificate file - ca_cert=ca.crt - ; the node private key file - sdk_key=sdk.key - ; the node certificate file - sdk_cert=sdk.crt - -; connected peers -[peers] -# supported ipv4 and ipv6 - node.0=[::1]:30300 - node.1=[::1]:30304 - node.2=[::1]:30308 - node.3=[::1]:30312 \ No newline at end of file diff --git a/bindings/java/jni/src/main/c/test/config/bcos_c_sdk_sample.ini b/bindings/java/jni/src/main/c/test/config/bcos_c_sdk_sample.ini deleted file mode 100644 index dae22a16b..000000000 --- a/bindings/java/jni/src/main/c/test/config/bcos_c_sdk_sample.ini +++ /dev/null @@ -1,30 +0,0 @@ - -; common -[common] - ; thread pool size for network send/receive/handle message - thread_pool_size = 8 - ; timeout for send message(ms) - message_timeout_ms = 10000 - -; ssl -[ssl] - ; if ssl connection is disabled - disable = false - ; type: ssl or sm_ssl - type = ssl - ; directory the certificates located in - ca_path=./conf - ; the ca certificate file - ca_cert=ca.crt - ; the node private key file - sdk_key=sdk.key - ; the node certificate file - sdk_cert=sdk.crt - -; connected peers -[peers] -# supported ipv4 and ipv6 - node.0=127.0.0.1:30300 - node.1=127.0.0.1:30304 - node.2=127.0.0.1:30308 - node.3=127.0.0.1:30312 \ No newline at end of file diff --git a/bindings/java/jni/src/main/c/test/config/bcos_c_sdk_sm_ssl_sample.ini b/bindings/java/jni/src/main/c/test/config/bcos_c_sdk_sm_ssl_sample.ini deleted file mode 100644 index c8b6beee2..000000000 --- a/bindings/java/jni/src/main/c/test/config/bcos_c_sdk_sm_ssl_sample.ini +++ /dev/null @@ -1,29 +0,0 @@ -; common -[common] - ; thread pool size for network send/receive/handle message - thread_pool_size = 8 - ; timeout for send message(ms) - message_timeout_ms = 10000 - -; ssl -[ssl] - ; if ssl connection is disabled - disable = false - ; type: ssl or sm_ssl - type = sm_ssl - ; directory the certificates located in - ca_path=./conf - ; the ca certificate file - ca_cert=ca.crt - ; the node private key file - sdk_key=sdk.key - ; the node certificate file - sdk_cert=sdk.crt - -; connected peers -[peers] -# supported ipv4 and ipv6 - node.0=127.0.0.1:30300 - node.1=127.0.0.1:30304 - node.2=127.0.0.1:30308 - node.3=127.0.0.1:30312 \ No newline at end of file diff --git a/bindings/java/jni/src/main/c/test/exe/CMakeLists.txt b/bindings/java/jni/src/main/c/test/exe/CMakeLists.txt deleted file mode 100644 index 0cc7c8aee..000000000 --- a/bindings/java/jni/src/main/c/test/exe/CMakeLists.txt +++ /dev/null @@ -1,19 +0,0 @@ -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DSTATICLIB") - -file(GLOB SRC_LIST "*.cpp" "*.c") -file(GLOB HEADERS "*.h") - -add_executable(get-block-number get-block-number.c) -target_link_libraries(get-block-number PRIVATE ${FISCO_BCOS_JNI_STATIC_TARGET}) - -add_executable(pub pub.c) -target_link_libraries(pub PRIVATE ${FISCO_BCOS_JNI_STATIC_TARGET}) - -add_executable(broadcast broadcast.c) -target_link_libraries(broadcast PRIVATE ${FISCO_BCOS_JNI_STATIC_TARGET}) - -add_executable(sub sub.c) -target_link_libraries(sub PRIVATE ${FISCO_BCOS_JNI_STATIC_TARGET}) - -add_executable(sdk-start sdk-start.c) -target_link_libraries(sdk-start PRIVATE ${FISCO_BCOS_JNI_STATIC_TARGET}) \ No newline at end of file diff --git a/bindings/java/jni/src/main/c/test/exe/broadcast.c b/bindings/java/jni/src/main/c/test/exe/broadcast.c deleted file mode 100644 index 74ee0e19d..000000000 --- a/bindings/java/jni/src/main/c/test/exe/broadcast.c +++ /dev/null @@ -1,63 +0,0 @@ -#include "bcos-c-sdk/bcos_sdk_c.h" -#include "bcos-c-sdk/bcos_sdk_c_amop.h" -#include "bcos-c-sdk/bcos_sdk_c_rpc.h" -#include -#include -#include -#include - -void usage() -{ - printf("Usage: broadcast \n"); - printf("Example:\n"); - printf(" ./broadcast 127.0.0.1 20200 topic aaaaaa\n"); - exit(0); -} - -int main(int argc, char** argv) -{ - if (argc < 5) - { - usage(); - } - - char* host = argv[1]; - uint16_t port = atoi(argv[2]); - char* topic = argv[3]; - char* msg = argv[4]; - - // ini bcos_sdk_c_config - struct bcos_sdk_c_config config; - - config.heartbeat_period_ms = 10000; - config.reconnect_period_ms = 20000; - config.message_timeout_ms = 20000; - config.thread_pool_size = 4; - - struct bcos_sdk_c_endpoint ep; - ep.host = host; - ep.port = port; - - config.peers = &ep; - config.peers_count = 1; - - config.send_rpc_request_to_highest_block_node = 1; - - void* sdk = bcos_sdk_create(&config); - if (!sdk) - { - printf("bcos_sdk_create failed.\n"); - return 0; - } - - bcos_sdk_start(sdk); - - while (1) - { - sleep(5); - printf("==> broadcast, topic: %s, message: %s\n", topic, msg); - bcos_amop_broadcast(sdk, topic, (void*)msg, strlen(msg)); - } - - return 0; -} diff --git a/bindings/java/jni/src/main/c/test/exe/get-block-number.c b/bindings/java/jni/src/main/c/test/exe/get-block-number.c deleted file mode 100644 index 0e03cad51..000000000 --- a/bindings/java/jni/src/main/c/test/exe/get-block-number.c +++ /dev/null @@ -1,72 +0,0 @@ -#include "bcos-c-sdk/bcos_sdk_c.h" -#include "bcos-c-sdk/bcos_sdk_c_rpc.h" -#include -#include -#include - -void usage() -{ - printf("Usage: get-block-number (optional)\n"); - printf("Example:\n"); - printf(" ./get-block-number 127.0.0.1 20200\n"); - printf(" ./get-block-number 127.0.0.1 20200 node0\n"); - exit(0); -} - -void callback(struct bcos_sdk_c_struct_response* resp) -{ - printf("status = %d \n", resp->error); - printf("desc = %s \n", resp->desc); - printf("data = %s \n", (char*)resp->data); - printf("size = %zu \n", resp->size); -} - -int main(int argc, char** argv) -{ - if (argc < 3) - { - usage(); - } - - char* host = argv[1]; - uint16_t port = atoi(argv[2]); - - char* node = argc > 3 ? argv[3] : ""; - - // ini bcos_sdk_c_config - struct bcos_sdk_c_config config; - - config.heartbeat_period_ms = 20000; - config.reconnect_period_ms = 20000; - config.message_timeout_ms = 20000; - config.thread_pool_size = 4; - - struct bcos_sdk_c_endpoint ep; - ep.host = host; - ep.port = port; - - config.peers = &ep; - config.peers_count = 1; - - config.send_rpc_request_to_highest_block_node = 1; - - void* sdk = bcos_sdk_create(&config); - if (!sdk) - { - printf("bcos_sdk_create failed.\n"); - return 0; - } - - bcos_sdk_start(sdk); - - printf("create sdk service.\n"); - const char* group = "group"; - - while (1) - { - sleep(5); - bcos_rpc_get_block_number(sdk, group, node, callback, sdk); - } - - return 0; -} diff --git a/bindings/java/jni/src/main/c/test/exe/pub.c b/bindings/java/jni/src/main/c/test/exe/pub.c deleted file mode 100644 index 921ef4d99..000000000 --- a/bindings/java/jni/src/main/c/test/exe/pub.c +++ /dev/null @@ -1,70 +0,0 @@ -#include "bcos-c-sdk/bcos_sdk_c.h" -#include "bcos-c-sdk/bcos_sdk_c_amop.h" -#include "bcos-c-sdk/bcos_sdk_c_rpc.h" -#include -#include -#include -#include - -void usage() -{ - printf("Usage: pub \n"); - printf("Example:\n"); - printf(" ./pub 127.0.0.1 20200 topic aaaaaa\n"); - exit(0); -} - -void callback(struct bcos_sdk_c_struct_response* resp) -{ - printf(" ==> receive response, status: %d, data: %s\n", resp->error, (char*)resp->data); -} - -int main(int argc, char** argv) -{ - if (argc < 5) - { - usage(); - } - - char* host = argv[1]; - uint16_t port = atoi(argv[2]); - char* topic = argv[3]; - char* msg = argv[4]; - - // ini bcos_sdk_c_config - struct bcos_sdk_c_config config; - - config.heartbeat_period_ms = 10000; - config.reconnect_period_ms = 20000; - config.message_timeout_ms = 20000; - config.thread_pool_size = 4; - - struct bcos_sdk_c_endpoint ep; - ep.host = host; - ep.port = port; - - config.peers = &ep; - config.peers_count = 1; - - config.send_rpc_request_to_highest_block_node = 1; - - void* sdk = bcos_sdk_create(&config); - if (!sdk) - { - printf("bcos_sdk_create failed.\n"); - return 0; - } - - printf("start sdk service.\n"); - - bcos_sdk_start(sdk); - - while (1) - { - sleep(5); - printf("==> publish, topic: %s, message: %s\n", topic, msg); - bcos_amop_publish(sdk, topic, (void*)msg, strlen(msg), 0, callback, sdk); - } - - return 0; -} diff --git a/bindings/java/jni/src/main/c/test/exe/sdk-start.c b/bindings/java/jni/src/main/c/test/exe/sdk-start.c deleted file mode 100644 index c4c73fc81..000000000 --- a/bindings/java/jni/src/main/c/test/exe/sdk-start.c +++ /dev/null @@ -1,72 +0,0 @@ -#include "bcos-c-sdk/bcos_sdk_c.h" -#include "bcos-c-sdk/bcos_sdk_c_rpc.h" -#include -#include -#include - -void usage() -{ - printf("Usage: sdk-start \n"); - printf("Example:\n"); - printf(" ./sdk-start 127.0.0.1 20200\n"); - exit(0); -} - -void callback(struct bcos_sdk_c_struct_response* resp) -{ - printf("status = %d \n", resp->error); - printf("desc = %s \n", resp->desc); - printf("data = %s \n", (char*)resp->data); - printf("size = %zu \n", resp->size); -} - -int main(int argc, char** argv) -{ - if (argc < 3) - { - usage(); - } - - char* host = argv[1]; - uint16_t port = atoi(argv[2]); - - // ini bcos_sdk_c_config - struct bcos_sdk_c_config config; - - config.heartbeat_period_ms = 20000; - config.reconnect_period_ms = 20000; - config.message_timeout_ms = 20000; - config.thread_pool_size = 4; - config.send_rpc_request_to_highest_block_node = 1; - - struct bcos_sdk_c_endpoint ep; - ep.host = host; - ep.port = port; - - config.peers = &ep; - config.peers_count = 1; - - void* sdk = bcos_sdk_create(&config); - if (!sdk) - { - printf("bcos_sdk_create failed.\n"); - return 0; - } - printf("create bcos sdk.\n"); - - bcos_sdk_start(sdk); - printf("start bcos sdk.\n"); - - printf("get group list.\n"); - bcos_rpc_get_group_list(sdk, callback, sdk); - - sleep(5); - bcos_sdk_stop(sdk); - printf("stop bcos sdk.\n"); - - sleep(5); - bcos_sdk_destroy(sdk); - printf("destroy bcos sdk.\n"); - - return 0; -} diff --git a/bindings/java/jni/src/main/c/test/exe/sub.c b/bindings/java/jni/src/main/c/test/exe/sub.c deleted file mode 100644 index e73d4272e..000000000 --- a/bindings/java/jni/src/main/c/test/exe/sub.c +++ /dev/null @@ -1,68 +0,0 @@ -#include "bcos-c-sdk/bcos_sdk_c.h" -#include "bcos-c-sdk/bcos_sdk_c_amop.h" -#include "bcos-c-sdk/bcos_sdk_c_rpc.h" -#include -#include -#include - -void usage() -{ - printf("Usage: sub \n"); - printf("Example:\n"); - printf(" ./sub 127.0.0.1 20200 topic\n"); - exit(0); -} - -void sub_cb(const char* endpoint, const char* seq, struct bcos_sdk_c_struct_response* resp) -{ - printf(" ==> receive request, status: %d, data: %s\n", resp->error, (char*)resp->data); - bcos_amop_send_response(resp->context, endpoint, seq, resp->data, resp->size); -} - -int main(int argc, char** argv) -{ - if (argc < 4) - { - usage(); - } - - char* host = argv[1]; - uint16_t port = atoi(argv[2]); - const char* topic = argv[3]; - - // ini bcos_sdk_c_config - struct bcos_sdk_c_config config; - - config.heartbeat_period_ms = 10000; - config.reconnect_period_ms = 20000; - config.message_timeout_ms = 20000; - config.thread_pool_size = 4; - - struct bcos_sdk_c_endpoint ep; - ep.host = host; - ep.port = port; - - config.peers = &ep; - config.peers_count = 1; - - void* sdk = bcos_sdk_create(&config); - if (!sdk) - { - printf("bcos_sdk_create failed.\n"); - return 0; - } - - printf("start sdk service.\n"); - - printf(" ==> subscribe topic topic: %s\n", topic); - - bcos_amop_subscribe_topic_with_cb(sdk, topic, sub_cb, sdk); - - while (1) - { - sleep(5); - printf(" ==> main <== \n"); - } - - return 0; -} diff --git a/cmake/CompilerSettings.cmake b/cmake/CompilerSettings.cmake index f78abd201..0356df8c3 100644 --- a/cmake/CompilerSettings.cmake +++ b/cmake/CompilerSettings.cmake @@ -16,6 +16,15 @@ # File: CompilerSettings.cmake # Function: Common cmake file for setting compilation environment variables # ------------------------------------------------------------------------------ +set(CMAKE_CXX_STANDARD 20) +set(Boost_NO_WARN_NEW_VERSIONS ON) +message(STATUS "COMPILER_ID: ${CMAKE_CXX_COMPILER_ID}") + +# export windows dll symbol +if(WIN32) + message(STATUS "Compile on Windows") + set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS "ON") +endif() if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MATCHES "Clang")) find_program(CCACHE_PROGRAM ccache) @@ -37,13 +46,15 @@ if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MA add_compile_options(-Wall) add_compile_options(-pedantic) add_compile_options(-Wextra) - # add_compile_options(-Wno-unused-variable) - # add_compile_options(-Wno-unused-parameter) - # add_compile_options(-Wno-unused-function) - # add_compile_options(-Wno-missing-field-initializers) - # Disable warnings about unknown pragmas (which is enabled by -Wall). - add_compile_options(-Wno-unknown-pragmas) + + # Ignore warnings + add_compile_options(-Wno-unused-parameter) + add_compile_options(-Wno-unused-variable) + add_compile_options(-Wno-error=unknown-pragmas) + add_compile_options(-Wno-error=deprecated-declarations) add_compile_options(-fno-omit-frame-pointer) + add_compile_options(-Wno-error=strict-aliasing) + # for boost json spirit add_compile_options(-DBOOST_SPIRIT_THREADSAFE) # for tbb, TODO: https://software.intel.com/sites/default/files/managed/b2/d2/TBBRevamp.pdf @@ -89,8 +100,8 @@ if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MA # Additional GCC-specific compiler settings. if ("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") - # Check that we've got GCC 7.0 or newer. - set(GCC_MIN_VERSION "7.0") + # Check that we've got GCC 10.0 or newer. + set(GCC_MIN_VERSION "10.0") execute_process( COMMAND ${CMAKE_CXX_COMPILER} -dumpversion OUTPUT_VARIABLE GCC_VERSION) if (NOT (GCC_VERSION VERSION_GREATER ${GCC_MIN_VERSION} OR GCC_VERSION VERSION_EQUAL ${GCC_MIN_VERSION})) @@ -137,6 +148,7 @@ if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "GNU") OR ("${CMAKE_CXX_COMPILER_ID}" MA endif() endif () elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC") + add_compile_definitions(NOMINMAX) # Only support visual studio 2017 and visual studio 2019 set(MSVC_MIN_VERSION "1914") # VS2017 15.7, for full-ish C++17 support @@ -155,6 +167,20 @@ elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC") add_compile_options(/std:c++latest) add_compile_options(-bigobj) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc") + if(BUILD_SHARED_LIBS) + if(CMAKE_BUILD_TYPE MATCHES "Debug") + add_compile_options(/MDd) + else() + add_compile_options(/MD) + endif() + else () + if(CMAKE_BUILD_TYPE MATCHES "Debug") + add_compile_options(/MTd) + else() + add_compile_options(/MT) + endif () + endif () + # set(CMAKE_CXX_FLAGS_DEBUG "/MTd /DEBUG") # set(CMAKE_CXX_FLAGS_MINSIZEREL "/MT /Os") # set(CMAKE_CXX_FLAGS_RELEASE "/MT") @@ -177,4 +203,6 @@ if(APPLE) else() set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -ldl") endif() +message("CMAKE_EXE_LINKER_FLAGS: ${CMAKE_EXE_LINKER_FLAGS}") +message("CMAKE_SHARED_LINKER_FLAGS: ${CMAKE_SHARED_LINKER_FLAGS}") set(CMAKE_SKIP_INSTALL_ALL_DEPENDENCY ON) diff --git a/cmake/IncludeDirectories.cmake b/cmake/IncludeDirectories.cmake new file mode 100644 index 000000000..22bf8a1f1 --- /dev/null +++ b/cmake/IncludeDirectories.cmake @@ -0,0 +1,9 @@ +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) +message(STATUS "CMAKE_CURRENT_SOURCE_DIR: ${CMAKE_CURRENT_SOURCE_DIR}") +message(STATUS "CMAKE_CURRENT_BINARY_DIR: ${CMAKE_CURRENT_BINARY_DIR}") +include_directories(${CMAKE_INSTALL_INCLUDEDIR}) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/bcos-c-sdk) +include_directories(${CMAKE_CURRENT_BINARY_DIR}/include) +include_directories(${VCPKG_INSTALLED_DIR}/${VCPKG_TARGET_TRIPLET}/include) diff --git a/sample/amop/broadcast.c b/sample/amop/broadcast.c index 552477ed4..b3c46cd52 100644 --- a/sample/amop/broadcast.c +++ b/sample/amop/broadcast.c @@ -31,7 +31,7 @@ //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void usage() +void usage(void) { printf("Desc: broadcast amop message by command params\n"); printf("Usage: broadcast \n"); @@ -76,13 +76,11 @@ int main(int argc, char** argv) printf(" [AMOP][Broadcast] start sdk ... \n"); - int i = 0; while (1) { printf("[AMOP][Broadcast] broadcast message, topic: %s, msg: %s\n", topic, msg); bcos_amop_broadcast(sdk, topic, (void*)msg, strlen(msg)); sleep(10); - i++; } return EXIT_SUCCESS; diff --git a/sample/amop/publish.c b/sample/amop/publish.c index 37e816e64..b84224d02 100644 --- a/sample/amop/publish.c +++ b/sample/amop/publish.c @@ -31,7 +31,7 @@ //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void usage() +void usage(void) { printf("Desc: publish amop message by command params\n"); printf("Usage: publish \n"); @@ -87,7 +87,6 @@ int main(int argc, char** argv) exit(-1); } - int i = 0; while (1) { printf("[AMOP][Publish] publish message, topic: %s, msg size: %lu\n", topic, strlen(msg)); @@ -95,7 +94,6 @@ int main(int argc, char** argv) bcos_amop_publish(sdk, topic, (void*)msg, strlen(msg), 0, on_recv_amop_publish_resp, sdk); sleep(10); - i++; } return EXIT_SUCCESS; diff --git a/sample/amop/subscribe.c b/sample/amop/subscribe.c index 2bf9f2a4a..9e9bbddd7 100644 --- a/sample/amop/subscribe.c +++ b/sample/amop/subscribe.c @@ -29,7 +29,7 @@ #include //------------------------------------------------------------------------------ -void usage() +void usage(void) { printf("Desc: subscribe amop topic by command params\n"); printf("Usage: subscribe \n"); @@ -92,12 +92,10 @@ int main(int argc, char** argv) bcos_amop_subscribe_topic_with_cb(sdk, topic, on_recv_amop_subscribe_resp, sdk); - int i = 0; while (1) { printf(" Main thread running \n"); sleep(10); - i++; } return EXIT_SUCCESS; diff --git a/sample/eventsub/eventsub.c b/sample/eventsub/eventsub.c index 763ab1a87..a97f72e2c 100644 --- a/sample/eventsub/eventsub.c +++ b/sample/eventsub/eventsub.c @@ -31,7 +31,7 @@ //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void usage() +void usage(void) { printf("Desc: subscribe contract events by command params\n"); printf("Usage: eventsub

[Optional]\n"); @@ -124,12 +124,10 @@ int main(int argc, char** argv) bcos_event_sub_subscribe_event(sdk, group, params, on_event_sub_callback, sdk); - int i = 0; while (1) { printf(" Main thread running "); sleep(10); - i++; } return EXIT_SUCCESS; diff --git a/sample/rpc/rpc.c b/sample/rpc/rpc.c index 0e515ffce..de2973af9 100644 --- a/sample/rpc/rpc.c +++ b/sample/rpc/rpc.c @@ -30,7 +30,7 @@ //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ -void usage() +void usage(void) { printf("Desc: rpc methods call test\n"); printf("Usage: rpc \n"); diff --git a/sample/tx/hello_sample.c b/sample/tx/hello_sample.c index cc1987295..493d44cc6 100644 --- a/sample/tx/hello_sample.c +++ b/sample/tx/hello_sample.c @@ -127,7 +127,7 @@ const char* g_hw_abi = } */ -void usage() +void usage(void) { printf("Desc: HelloWorld contract sample\n"); printf("Usage: hello_sample \n"); diff --git a/sample/tx/hello_sample_hsm.c b/sample/tx/hello_sample_hsm.c index c862e1dcb..15ded5ff3 100644 --- a/sample/tx/hello_sample_hsm.c +++ b/sample/tx/hello_sample_hsm.c @@ -128,7 +128,7 @@ const char* g_hw_abi = } */ -void usage() +void usage(void) { printf("Desc: HelloWorld contract sample with hsm key pair\n"); printf( diff --git a/sample/tx/tx_struct_test.c b/sample/tx/tx_struct_test.c index 869c8cefb..74143922d 100644 --- a/sample/tx/tx_struct_test.c +++ b/sample/tx/tx_struct_test.c @@ -129,7 +129,7 @@ const char* g_hw_abi = } */ -void usage() +void usage(void) { printf("Desc: tx struct test sample\n"); printf("Usage: tx_struct_test \n"); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 9bb8c03e8..3b710b476 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -26,5 +26,8 @@ config_test_cases("" "${SOURCES}" ${TEST_BINARY_NAME} "") add_executable(${TEST_BINARY_NAME} ${SOURCES}) target_include_directories(${TEST_BINARY_NAME} PRIVATE . ${CMAKE_SOURCE_DIR}) -find_package(Boost CONFIG QUIET REQUIRED unit_test_framework) -target_link_libraries(${TEST_BINARY_NAME} ${BCOS_C_SDK_TARGET} Boost::unit_test_framework) +if(BUILD_SHARED_LIBS) + target_link_libraries(${TEST_BINARY_NAME} ${BCOS_C_SDK_TARGET} Boost::log Boost::unit_test_framework) +else () + target_link_libraries(${TEST_BINARY_NAME} ${BCOS_C_SDK_STATIC_TARGET} Boost::log Boost::unit_test_framework) +endif () diff --git a/vcpkg b/vcpkg new file mode 160000 index 000000000..51b14cd4e --- /dev/null +++ b/vcpkg @@ -0,0 +1 @@ +Subproject commit 51b14cd4e1230dd51c11ffeff6f7d53c61cc5297 diff --git a/vcpkg-configuration.json b/vcpkg-configuration.json new file mode 100644 index 000000000..403657515 --- /dev/null +++ b/vcpkg-configuration.json @@ -0,0 +1,16 @@ +{ + "registries": [ + { + "kind": "git", + "repository": "https://github.com/kyonRay/registry", + "baseline": "8d37c1d05935bc5d5796c6e73da7b15d19da2e3f", + "packages": [ + "openssl", + "hsm-crypto", + "fisco-bcos-cpp-sdk", + "wedprcrypto", + "secp256k1" + ] + } + ] +} \ No newline at end of file diff --git a/vcpkg.json b/vcpkg.json new file mode 100644 index 000000000..967d55446 --- /dev/null +++ b/vcpkg.json @@ -0,0 +1,108 @@ +{ + "name": "fisco-bcos-c-sdk", + "version-string": "3.6.0", + "homepage": "https://github.com/FISCO-BCOS/c-sdk", + "description": "FISCO BCOS C SDK", + "dependencies": [ + { + "name": "vcpkg-cmake", + "host": true + }, + { + "name": "vcpkg-cmake-config", + "host": true + }, + "boost-log", + "boost-beast", + "boost-uuid", + "boost-heap", + "boost-graph", + "boost-context", + "boost-property-map", + "boost-chrono", + "boost-iostreams", + "boost-thread", + "boost-test", + "boost-compute", + "boost-container-hash", + "boost-multiprecision", + "boost-program-options", + "boost-variant2", + "jsoncpp", + "openssl", + "secp256k1", + "wedprcrypto", + "fisco-bcos-cpp-sdk" + ], + "builtin-baseline": "51b14cd4e1230dd51c11ffeff6f7d53c61cc5297", + "overrides": [ + { + "name": "boost-log", + "version": "1.83.0" + }, + { + "name": "boost-beast", + "version": "1.83.0" + }, + { + "name": "boost-uuid", + "version": "1.83.0" + }, + { + "name": "boost-heap", + "version": "1.83.0" + }, + { + "name": "boost-graph", + "version": "1.83.0" + }, + { + "name": "boost-context", + "version": "1.80.0" + }, + { + "name": "boost-property-map", + "version": "1.83.0" + }, + { + "name": "boost-chrono", + "version": "1.83.0" + }, + { + "name": "boost-iostreams", + "version": "1.83.0" + }, + { + "name": "boost-thread", + "version": "1.83.0" + }, + { + "name": "boost-test", + "version": "1.83.0" + }, + { + "name": "boost-compute", + "version": "1.83.0" + }, + { + "name": "boost-container-hash", + "version": "1.83.0" + }, + { + "name": "boost-multiprecision", + "version": "1.83.0" + }, + { + "name": "boost-program-options", + "version": "1.83.0" + }, + { + "name": "boost-variant2", + "version": "1.83.0" + }, + { + "name": "jsoncpp", + "version": "1.9.5" + } + ] +} \ No newline at end of file From d0c730df8593153b9904230fe4e8e2efcaec79e6 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Mon, 27 Nov 2023 19:59:16 +0800 Subject: [PATCH 11/29] (build): fix windows compile error. (#190) --- .github/workflows/workflow.yml | 10 ++++++++-- CMakeLists.txt | 5 ++++- bindings/java/jni/src/main/c/CMakeLists.txt | 8 ++++++++ vcpkg-configuration.json | 4 ++-- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index ef392fd8d..cb4580b44 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -54,6 +54,8 @@ jobs: run: export CFLAGS="${CFLAGS} -fPIC";export CXXFLAGS="${CXXFLAGS} -fPIC"; cd build && make -j4 - name: run test run: cd build && CTEST_OUTPUT_ON_FAILURE=TRUE make test + - name: print link.txt + run: cat build/CMakeFiles/bcos-c-sdk*.dir/link.txt - uses: actions/upload-artifact@v2 with: name: libbcos-c-sdk.dylib.zip @@ -97,6 +99,8 @@ jobs: run: cd build && make -j4 - name: run test run: cd build && CTEST_OUTPUT_ON_FAILURE=TRUE make test + - name: print link.txt + run: cat build/CMakeFiles/bcos-c-sdk*.dir/link.txt - name: run coverage run: cd build && make cov - name: upload coverage report @@ -107,7 +111,7 @@ jobs: version: "v0.1.15" flags: unittests name: c sdk coverage - fail_ci_if_error: true + fail_ci_if_error: false verbose: true build_with_centos: name: build_with_centos @@ -165,6 +169,8 @@ jobs: make -j4 - name: run test run: cd build && CTEST_OUTPUT_ON_FAILURE=TRUE make test + - name: print link.txt + run: cat build/CMakeFiles/bcos-c-sdk*.dir/link.txt - uses: actions/upload-artifact@v2 with: name: libbcos-c-sdk.so.zip @@ -202,7 +208,7 @@ jobs: if: runner.os == 'Windows' run: | mkdir -p build && cd build - cmake -G "Visual Studio 16 2019" -A x64 ../ -DCMAKE_BUILD_TYPE=Release -DBOOST_USE_WINAPI_VERSION=BOOST_WINAPI_VERSION_WIN7 -D_WIN32_WINNT=0x0601 -DTESTS=ON -DBUILD_JNI=ON -DVCPKG_TARGET_TRIPLET='x64-windows-static' -DCMAKE_TOOLCHAIN_FILE=C:\vcpkg\scripts\buildsystems\vcpkg.cmake + cmake -G "Visual Studio 16 2019" -A x64 ../ -DCMAKE_BUILD_TYPE=Release -DBOOST_USE_WINAPI_VERSION=BOOST_WINAPI_VERSION_WIN7 -D_WIN32_WINNT=0x0601 -DTESTS=ON -DBUILD_JNI=ON -DVCPKG_TARGET_TRIPLET='x64-windows-static' -DCMAKE_TOOLCHAIN_FILE=C:\vcpkg\scripts\buildsystems\vcpkg.cmake || cat C:\vcpkg\buildtrees\fisco-bcos-cpp-sdk\build-x64-windows-static-dbg-out.log - name: compile run: cd build && MSBuild bcos-c-sdk.sln /p:Configuration=Release /p:Platform=x64 - uses: actions/upload-artifact@v2 diff --git a/CMakeLists.txt b/CMakeLists.txt index 1ff519b90..cba6832f8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,8 @@ if (NOT DEFINED URL_BASE) endif () if (WIN32) + set(VCPKG_C_FLAGS -D_WIN32_WINNT=0x0601 -DWINVER=0x0601) + set(VCPKG_CXX_FLAGS -D_WIN32_WINNT=0x0601 -DWINVER=0x0601) add_definitions(-D_WIN32_WINNT=0x0601) add_definitions(-DBOOST_USE_WINAPI_VERSION=BOOST_WINAPI_VERSION_WIN7) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS "ON") @@ -46,6 +48,7 @@ if ("${ARCHITECTURE}" MATCHES "aarch64") set(ARCH_NATIVE ON) endif () +set(VCPKG_BUILD_TYPE "Release") if (NOT DEFINED CMAKE_TOOLCHAIN_FILE) find_package(Git REQUIRED) execute_process(COMMAND ${GIT_EXECUTABLE} submodule update --init --recursive -- vcpkg WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}) @@ -58,7 +61,7 @@ project(bcos-c-sdk VERSION "3.6.0") # Debug, Release, RelWithDebInfo, MinSizeRel if (NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release) + set(CMAKE_BUILD_TYPE MinSizeRel) endif () # basic settings diff --git a/bindings/java/jni/src/main/c/CMakeLists.txt b/bindings/java/jni/src/main/c/CMakeLists.txt index ace0b0b3c..1b6052b1a 100644 --- a/bindings/java/jni/src/main/c/CMakeLists.txt +++ b/bindings/java/jni/src/main/c/CMakeLists.txt @@ -21,6 +21,14 @@ if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release) endif() +if (WIN32) + add_definitions(-D_WIN32_WINNT=0x0601) + add_definitions(-DBOOST_USE_WINAPI_VERSION=BOOST_WINAPI_VERSION_WIN7) + set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS "ON") + set(VCPKG_C_FLAGS -D_WIN32_WINNT=0x0601 -DWINVER=0x0601) + set(VCPKG_CXX_FLAGS -D_WIN32_WINNT=0x0601 -DWINVER=0x0601) +endif () + SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fPIC") SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC") diff --git a/vcpkg-configuration.json b/vcpkg-configuration.json index 403657515..98c7092a7 100644 --- a/vcpkg-configuration.json +++ b/vcpkg-configuration.json @@ -2,8 +2,8 @@ "registries": [ { "kind": "git", - "repository": "https://github.com/kyonRay/registry", - "baseline": "8d37c1d05935bc5d5796c6e73da7b15d19da2e3f", + "repository": "https://github.com/FISCO-BCOS/registry", + "baseline": "717d8236aae1c032cfdd9469fd8248c8e49da6d3", "packages": [ "openssl", "hsm-crypto", From 93776145c26670f6976b6f4d0ab842daa65fab74 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Thu, 30 Nov 2023 11:19:44 +0800 Subject: [PATCH 12/29] (sdk): impl transaction builder v2 in c sdk. (#191) --- .github/workflows/workflow.yml | 2 +- bcos-c-sdk/bcos_sdk_c.cpp | 14 + bcos-c-sdk/bcos_sdk_c.h | 4 + bcos-c-sdk/bcos_sdk_c_uti_receipt.cpp | 32 +- bcos-c-sdk/bcos_sdk_c_uti_receipt.h | 2 + bcos-c-sdk/bcos_sdk_c_uti_tx_v2.cpp | 324 ++++++++++++++++++ bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h | 200 +++++++++++ .../main/resources/META-INF/native/file.list | 7 + vcpkg-configuration.json | 2 +- 9 files changed, 583 insertions(+), 4 deletions(-) create mode 100644 bcos-c-sdk/bcos_sdk_c_uti_tx_v2.cpp create mode 100644 bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h create mode 100644 bindings/java/jni/src/main/resources/META-INF/native/file.list diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index cb4580b44..d18a0c70c 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -43,7 +43,7 @@ jobs: if: runner.os == 'macOS' run: brew install ccache lcov - name: fetch vcpkg - run: cd /usr/local/share/vcpkg/ && git fetch && git reset --hard && git pull && cd - + run: cd /usr/local/share/vcpkg/ && git status && git reset --hard && git stash && git fetch && git pull && cd - - name: configure if: runner.os == 'macOS' run: | diff --git a/bcos-c-sdk/bcos_sdk_c.cpp b/bcos-c-sdk/bcos_sdk_c.cpp index cdd91f57d..5c815e3ce 100644 --- a/bcos-c-sdk/bcos_sdk_c.cpp +++ b/bcos-c-sdk/bcos_sdk_c.cpp @@ -318,4 +318,18 @@ const char* bcos_sdk_get_group_chain_id(void* sdk, const char* group) auto chainID = groupInfo->chainID(); return strdup(chainID.c_str()); +} + +uint32_t bcos_sdk_get_local_protocol_info(void* sdk) { + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(sdk, 0); + + return ((bcos::cppsdk::Sdk*)sdk)->localProtocolInfo(); +} + +uint32_t bcos_sdk_get_negotiated_protocol_info(void* sdk) { + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(sdk, 0); + + return ((bcos::cppsdk::Sdk*)sdk)->negotiatedProtocolInfo(); } \ No newline at end of file diff --git a/bcos-c-sdk/bcos_sdk_c.h b/bcos-c-sdk/bcos_sdk_c.h index 48fec951d..335cf2f90 100644 --- a/bcos-c-sdk/bcos_sdk_c.h +++ b/bcos-c-sdk/bcos_sdk_c.h @@ -115,6 +115,10 @@ void bcos_sdk_get_group_wasm_and_crypto(void* sdk, const char* group, int* wasm, * @return const char* : chain id */ const char* bcos_sdk_get_group_chain_id(void* sdk, const char* group); + +uint32_t bcos_sdk_get_local_protocol_info(void* sdk); + +uint32_t bcos_sdk_get_negotiated_protocol_info(void* sdk); // -------------------------------------------------------------------- #ifdef __cplusplus diff --git a/bcos-c-sdk/bcos_sdk_c_uti_receipt.cpp b/bcos-c-sdk/bcos_sdk_c_uti_receipt.cpp index b86b8fc13..61b17284e 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_receipt.cpp +++ b/bcos-c-sdk/bcos_sdk_c_uti_receipt.cpp @@ -80,8 +80,7 @@ void* bcos_sdk_create_receipt_data_with_json(const char* json) { std::string errorMsg = boost::diagnostic_information(e); BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_receipt_data") << LOG_DESC("exception") - << LOG_KV("json", json) - << LOG_KV("error", errorMsg); + << LOG_KV("json", json) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); } @@ -167,4 +166,33 @@ const char* bcos_sdk_calc_receipt_data_hash(int crypto_type, void* _receiptData) bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); } return NULL; +} + +const char* bcos_sdk_calc_receipt_data_hash_with_json(int crypto_type, const char* json) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(json, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (crypto_type == BCOS_C_SDK_ECDSA_TYPE || crypto_type == BCOS_C_SDK_SM_TYPE), + "invalid crypto type, it must be BCOS_C_SDK_ECDSA_TYPE(ecdsa crypto type) or " + "BCOS_C_SDK_SM_TYPE(sm crypto type)", + NULL); + + try + { + ReceiptBuilder builder; + auto transactionDataHash = builder.calculateReceiptDataHashWithJson( + crypto_type == BCOS_C_SDK_ECDSA_TYPE ? CryptoType::Secp256K1 : CryptoType::SM2, + std::string(json)); + return strdup(bcos::toHexStringWithPrefix(transactionDataHash).c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_calc_transaction_data_hash_with_json") + << LOG_DESC("exception") << LOG_KV("crypto_type", crypto_type) + << LOG_KV("json", json) << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + return NULL; } \ No newline at end of file diff --git a/bcos-c-sdk/bcos_sdk_c_uti_receipt.h b/bcos-c-sdk/bcos_sdk_c_uti_receipt.h index 4b9665bda..5b6cea480 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_receipt.h +++ b/bcos-c-sdk/bcos_sdk_c_uti_receipt.h @@ -52,6 +52,8 @@ void bcos_sdk_destroy_receipt_data(void* transaction_data); const char* bcos_sdk_calc_receipt_data_hash(int crypto_type, void* _receiptData); +const char* bcos_sdk_calc_receipt_data_hash_with_json(int crypto_type, const char* json); + const char* bcos_sdk_encode_receipt_data(void* receipt_data); const char* bcos_sdk_decode_receipt_data(const char* receipt_bytes); diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.cpp b/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.cpp new file mode 100644 index 000000000..3216e4921 --- /dev/null +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.cpp @@ -0,0 +1,324 @@ +/** + * Copyright (C) 2022 FISCO BCOS. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @file bcos_sdk_c_uti_tx_v2.cpp + * @author: kyonGuo + * @date 2023/11/28 + */ + +#include "bcos_sdk_c_uti_tx_v2.h" +#include "bcos_sdk_c_error.h" +#include +#include +#include +#include + +using namespace bcos; +using namespace bcos::cppsdk; +using namespace bcos::cppsdk::utilities; + +#include +#include +#include + +void* bcos_sdk_create_transaction_v2_data(const char* group_id, const char* chain_id, + const char* to, const char* input, const char* abi, int64_t block_limit, const char* value, + const char* gas_price, int64_t gas_limit) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(group_id, NULL) + BCOS_SDK_C_PARAMS_VERIFICATION(chain_id, NULL) + BCOS_SDK_C_PARAMS_VERIFICATION(input, NULL) + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must > 0", NULL) + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((gas_limit >= 0), "gas limit must >= 0", NULL) + + try + { + TransactionBuilderV2 builder; + auto bytesData = fromHexString(input); + auto transactionData = + builder.createTransactionData(1, group_id, chain_id, to, "", *bytesData, abi, + block_limit, value ? value : "", gas_price ? gas_price : "", gas_limit); + return transactionData.release(); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_data") << LOG_DESC("exception") + << LOG_KV("group_id", group_id) << LOG_KV("chain_id", chain_id) + << LOG_KV("to", std::string(to ? to : "")) << LOG_KV("input", input) + << LOG_KV("abi", std::string(abi ? abi : "")) + << LOG_KV("block_limit", block_limit) + << LOG_KV("value", std::string(value ? value : "")) + << LOG_KV("gas_price", std::string(gas_price ? gas_price : "")) + << LOG_KV("gas_limit", gas_limit) << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +void* bcos_sdk_create_eip1559_transaction_data(const char* group_id, const char* chain_id, + const char* to, const char* input, const char* abi, int64_t block_limit, const char* value, + int64_t gas_limit, const char* max_fee_per_gas, const char* max_priority_fee_per_gas) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(group_id, NULL) + BCOS_SDK_C_PARAMS_VERIFICATION(chain_id, NULL) + BCOS_SDK_C_PARAMS_VERIFICATION(input, NULL) + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must > 0", NULL) + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((gas_limit >= 0), "gas limit must >= 0", NULL) + + try + { + TransactionBuilderV2 builder; + auto bytesData = fromHexString(input); + auto transactionData = builder.createTransactionData(1, group_id, chain_id, to, "", + *bytesData, abi, block_limit, value ? value : "", "", gas_limit, + max_fee_per_gas ? max_fee_per_gas : "", + max_priority_fee_per_gas ? max_priority_fee_per_gas : ""); + return transactionData.release(); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) + << LOG_BADGE("bcos_sdk_create_eip1559_transaction_data") << LOG_DESC("exception") + << LOG_KV("group_id", group_id) << LOG_KV("chain_id", chain_id) + << LOG_KV("to", std::string(to ? to : "")) << LOG_KV("input", input) + << LOG_KV("abi", std::string(abi ? abi : "")) << LOG_KV("block_limit", block_limit) + << LOG_KV("value", std::string(value ? value : "")) << LOG_KV("gas_limit", gas_limit) + << LOG_KV("max_fee_per_gas", std::string(max_fee_per_gas ? max_fee_per_gas : "")) + << LOG_KV("max_priority_fee_per_gas", + std::string(max_priority_fee_per_gas ? max_priority_fee_per_gas : "")) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + return nullptr; +} + + +const char* bcos_sdk_calc_transaction_data_hash_with_full_fields(int crypto_type, + transaction_version version, const char* group_id, const char* chain_id, const char* to, + const char* nonce, const char* input, const char* abi, int64_t block_limit, const char* value, + const char* gas_price, int64_t gas_limit, const char* max_fee_per_gas, + const char* max_priority_fee_per_gas) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(group_id, NULL) + BCOS_SDK_C_PARAMS_VERIFICATION(chain_id, NULL) + BCOS_SDK_C_PARAMS_VERIFICATION(input, NULL) + BCOS_SDK_C_PARAMS_VERIFICATION(nonce, NULL) + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must > 0", NULL) + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((gas_limit >= 0), "gas limit must >= 0", NULL) + + try + { + auto bytesData = fromHexString(input); + TransactionBuilderV2 builder; + auto transactionDataHash = builder.calculateTransactionDataHash( + crypto_type == BCOS_C_SDK_ECDSA_TYPE ? CryptoType::Secp256K1 : CryptoType::SM2, version, + group_id, chain_id, to ? to : "", nonce, *bytesData, abi ? abi : "", block_limit, + value ? value : "", gas_price ? gas_price : "", gas_limit, + max_fee_per_gas ? max_fee_per_gas : "", + max_priority_fee_per_gas ? max_priority_fee_per_gas : ""); + return strdup(bcos::toHexStringWithPrefix(transactionDataHash).c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) + << LOG_BADGE("bcos_sdk_calc_transaction_data_hash_with_full_fields") + << LOG_DESC("exception") << LOG_KV("group_id", group_id) << LOG_KV("chain_id", chain_id) + << LOG_KV("to", std::string(to ? to : "")) + << LOG_KV("nonce", std::string(nonce ? nonce : "")) << LOG_KV("input", input) + << LOG_KV("abi", std::string(abi ? abi : "")) << LOG_KV("block_limit", block_limit) + << LOG_KV("value", std::string(value ? value : "")) + << LOG_KV("gas_price", std::string(gas_price ? gas_price : "")) + << LOG_KV("gas_limit", gas_limit) + << LOG_KV("max_fee_per_gas", std::string(max_fee_per_gas ? max_fee_per_gas : "")) + << LOG_KV("max_priority_fee_per_gas", + std::string(max_priority_fee_per_gas ? max_priority_fee_per_gas : "")) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + return nullptr; +} + +const char* bcos_sdk_calc_transaction_data_hash_with_json(int crypto_type, const char* json) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(json, NULL) + + try + { + TransactionBuilderV2 builder; + auto transactionDataHash = builder.calculateTransactionDataHashWithJson( + crypto_type == BCOS_C_SDK_ECDSA_TYPE ? CryptoType::Secp256K1 : CryptoType::SM2, json); + return strdup(bcos::toHexStringWithPrefix(transactionDataHash).c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_calc_transaction_data_hash_with_json") + << LOG_DESC("exception") << LOG_KV("json", json) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + return nullptr; +} + +const char* bcos_sdk_create_signed_transaction_with_signature(const char* signature, + const char* transaction_hash, transaction_version version, const char* group_id, + const char* chain_id, const char* to, const char* nonce, const char* input, const char* abi, + int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit, + const char* max_fee_per_gas, const char* max_priority_fee_per_gas, int32_t attribute, + const char* extra_data) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(signature, NULL) + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_hash, NULL) + BCOS_SDK_C_PARAMS_VERIFICATION(group_id, NULL) + BCOS_SDK_C_PARAMS_VERIFICATION(chain_id, NULL) + BCOS_SDK_C_PARAMS_VERIFICATION(input, NULL) + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must > 0", NULL) + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((gas_limit >= 0), "gas limit must >= 0", NULL) + + try + { + TransactionBuilderV2 builder; + auto bytesData = fromHexString(input); + crypto::HashType tx_hash(fromHex(std::string_view(transaction_hash))); + bytes sign = fromHex(std::string_view(signature)); + + auto transaction = builder.createTransaction(std::move(sign), tx_hash, attribute, + (int32_t)version, group_id, chain_id, to ? to : "", nonce ? nonce : "", *bytesData, + abi ? abi : "", block_limit, value ? value : "", gas_price ? gas_price : "", gas_limit, + max_fee_per_gas ? max_fee_per_gas : "", + max_priority_fee_per_gas ? max_priority_fee_per_gas : "", extra_data ? extra_data : ""); + + auto bytes = builder.encodeTransaction(*transaction); + + return strdup(bcos::toHexStringWithPrefix(*bytes).c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_signed_transaction_with_signature") + << LOG_DESC("exception") << LOG_KV("signature", signature) + << LOG_KV("transaction_hash", transaction_hash) + << LOG_KV("group_id", group_id) << LOG_KV("chain_id", chain_id) + << LOG_KV("to", std::string(to ? to : "")) + << LOG_KV("nonce", std::string(nonce ? nonce : "")) + << LOG_KV("input", input) << LOG_KV("abi", std::string(abi ? abi : "")) + << LOG_KV("block_limit", block_limit) + << LOG_KV("value", std::string(value ? value : "")) + << LOG_KV("gas_price", std::string(gas_price ? gas_price : "")) + << LOG_KV("gas_limit", gas_limit) + << LOG_KV("max_fee_per_gas", + std::string(max_fee_per_gas ? max_fee_per_gas : "")); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + return nullptr; +} + +void bcos_sdk_create_signed_transaction_with_full_fields(void* key_pair, const char* group_id, + const char* chain_id, const char* to, const char* input, const char* abi, int64_t block_limit, + const char* value, const char* gas_price, int64_t gas_limit, int32_t attribute, + const char* extra_data, char** tx_hash, char** signed_tx) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(key_pair, ) + BCOS_SDK_C_PARAMS_VERIFICATION(group_id, ) + BCOS_SDK_C_PARAMS_VERIFICATION(chain_id, ) + BCOS_SDK_C_PARAMS_VERIFICATION(input, ) + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must > 0", ) + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((gas_limit >= 0), "gas limit must >= 0", ) + BCOS_SDK_C_PARAMS_VERIFICATION(tx_hash, ) + BCOS_SDK_C_PARAMS_VERIFICATION(signed_tx, ) + + try + { + TransactionBuilderV2 builder; + auto bytesData = fromHexString(input); + auto result = builder.createSignedTransaction(*((bcos::crypto::KeyPairInterface*)key_pair), + attribute, 1, group_id, chain_id, to ? to : "", "", *bytesData, abi ? abi : "", + block_limit, value ? value : "", gas_price ? gas_price : "", gas_limit, "", "", + extra_data ? extra_data : ""); + *tx_hash = strdup(result.first.c_str()); + *signed_tx = strdup(result.second.c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_signed_transaction_with_full_fields") + << LOG_DESC("exception") << LOG_KV("group_id", group_id) + << LOG_KV("chain_id", chain_id) << LOG_KV("to", std::string(to ? to : "")) + << LOG_KV("input", input) << LOG_KV("abi", std::string(abi ? abi : "")) + << LOG_KV("block_limit", block_limit) + << LOG_KV("value", std::string(value ? value : "")) + << LOG_KV("gas_price", std::string(gas_price ? gas_price : "")) + << LOG_KV("gas_limit", gas_limit) << LOG_KV("attribute", attribute) + << LOG_KV("extra_data", std::string(extra_data ? extra_data : "")) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } +} + +void bcos_sdk_create_signed_eip1559_transaction_with_full_fields(void* key_pair, + const char* group_id, const char* chain_id, const char* to, const char* input, const char* abi, + int64_t block_limit, const char* value, int64_t gas_limit, const char* max_fee_per_gas, + const char* max_priority_fee_per_gas, int32_t attribute, const char* extra_data, char** tx_hash, + char** signed_tx) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(key_pair, ) + BCOS_SDK_C_PARAMS_VERIFICATION(group_id, ) + BCOS_SDK_C_PARAMS_VERIFICATION(chain_id, ) + BCOS_SDK_C_PARAMS_VERIFICATION(input, ) + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must > 0", ) + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((gas_limit >= 0), "gas limit must >= 0", ) + BCOS_SDK_C_PARAMS_VERIFICATION(tx_hash, ) + BCOS_SDK_C_PARAMS_VERIFICATION(signed_tx, ) + + try + { + TransactionBuilderV2 builder; + auto bytesData = fromHexString(input); + auto result = builder.createSignedTransaction(*((bcos::crypto::KeyPairInterface*)key_pair), + attribute, 1, group_id, chain_id, to ? to : "", "", *bytesData, abi ? abi : "", + block_limit, value ? value : "", "", gas_limit, max_fee_per_gas ? max_fee_per_gas : "", + max_priority_fee_per_gas ? max_priority_fee_per_gas : "", extra_data ? extra_data : ""); + *tx_hash = strdup(result.first.c_str()); + *signed_tx = strdup(result.second.c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) + << LOG_BADGE("bcos_sdk_create_signed_eip1559_transaction_with_full_fields") + << LOG_DESC("exception") << LOG_KV("group_id", group_id) << LOG_KV("chain_id", chain_id) + << LOG_KV("to", std::string(to ? to : "")) << LOG_KV("input", input) + << LOG_KV("abi", std::string(abi ? abi : "")) << LOG_KV("block_limit", block_limit) + << LOG_KV("value", std::string(value ? value : "")) << LOG_KV("gas_limit", gas_limit) + << LOG_KV("max_fee_per_gas", std::string(max_fee_per_gas ? max_fee_per_gas : "")) + << LOG_KV("max_priority_fee_per_gas", + std::string(max_priority_fee_per_gas ? max_priority_fee_per_gas : "")) + << LOG_KV("attribute", attribute) + << LOG_KV("extra_data", std::string(extra_data ? extra_data : "")) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } +} diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h b/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h new file mode 100644 index 000000000..3812cb030 --- /dev/null +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h @@ -0,0 +1,200 @@ +/** + * Copyright (C) 2022 FISCO BCOS. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @file bcos_sdk_c_uti_tx_v2.h + * @author: kyonGuo + * @date 2023/11/28 + */ + +#ifndef BCOS_C_SDK_BCOS_SDK_C_UTI_TX_V2_H +#define BCOS_C_SDK_BCOS_SDK_C_UTI_TX_V2_H + +#include "bcos_sdk_c_common.h" + +#ifdef __cplusplus +extern "C" { +#endif + +enum transaction_version +{ + TRANSACTION_VERSION_0 = 0, + /* + * @brief enable (value,gasPrice,gasLimit,maxFeePerGas,maxPriorityFeePerGas) fields + * @note version 1 transaction only supported in FISCO BCOS 3.6.0 and later + */ + TRANSACTION_VERSION_1 = 1, +}; + +/** + * @brief create transaction data with version 1 by default + * @note version 1 transaction only supported in FISCO BCOS 3.6.0 and later + * + * @param group_id group id + * @param chain_id chain id + * @param to contract address, if it is a contract creation transaction, it can be empty + * @param input encoded contract method and params + * @param abi contract abi, only create contract need + * @param block_limit block limit + * @param value transfer value + * @param gas_price gas price + * @param gas_limit gas limit + * @return void* transaction data pointer, you should release it after use + */ +void* bcos_sdk_create_transaction_v2_data(const char* group_id, const char* chain_id, + const char* to, const char* input, const char* abi, int64_t block_limit, const char* value, + const char* gas_price, int64_t gas_limit); + +/** + * @brief create eip1559 transaction data with version 1 by default + * @note version 1 transaction only supported in FISCO BCOS 3.6.0 and later + * + * @param group_id group id + * @param chain_id chain id + * @param to contract address, if it is a contract creation transaction, it can be empty + * @param input encoded contract method and params + * @param abi contract abi, only create contract need + * @param block_limit block limit + * @param value transfer value + * @param gas_limit gas limit + * @param max_fee_per_gas max fee per gas + * @param max_priority_fee_per_gas max priority fee per gas + * @return void* transaction data pointer, you should release it after use + */ +void* bcos_sdk_create_eip1559_transaction_data(const char* group_id, const char* chain_id, + const char* to, const char* input, const char* abi, int64_t block_limit, const char* value, + int64_t gas_limit, const char* max_fee_per_gas, const char* max_priority_fee_per_gas); + +/** + * @brief create transaction data with full fields + * @note version 1 transaction only supported in FISCO BCOS 3.6.0 and later + * + * @param crypto_type 0: keccak256, 1: sm3 + * @param version tx version, if version==1, then enable + * (value,gasPrice,gasLimit,maxFeePerGas,maxPriorityFeePerGas) fields + * @param group_id group id + * @param chain_id chain id + * @param to contract address, if it is a contract creation transaction, it can be empty + * @param nonce nonce, random number to avoid duplicate transactions + * @param input encoded contract method and params + * @param abi contract abi, only create contract need + * @param block_limit block limit + * @param value transfer value + * @param gas_price gas price + * @param gas_limit gas limit + * @param max_fee_per_gas max fee per gas + * @param max_priority_fee_per_gas max priority fee per gas + * @return const char* transaction data hash hex string + */ +const char* bcos_sdk_calc_transaction_data_hash_with_full_fields(int crypto_type, transaction_version version, + const char* group_id, const char* chain_id, const char* to, const char* nonce, + const char* input, const char* abi, int64_t block_limit, const char* value, + const char* gas_price, int64_t gas_limit, const char* max_fee_per_gas, + const char* max_priority_fee_per_gas); + +/** + * @brief create transaction data with json string + * @note version 1 transaction only supported in FISCO BCOS 3.6.0 and later + * + * @param crypto_type 0: keccak256, 1: sm3 + * @param json transaction data json string + * @return const char* transaction data hash hex string + * @throw exception if lack of some required fields, or some fields are invalid + */ +const char* bcos_sdk_calc_transaction_data_hash_with_json(int crypto_type, const char* json); + +/** + * @brief create encoded transaction data with external signature + * @note version 1 transaction only supported in FISCO BCOS 3.6.0 and later + * + * @param signature signature hex string, if ECDSA, it is r||s||v, if SM2, it is r||s||pk + * @param transaction_hash transactionData hash hex string + * @param version tx version, only support 0 and 1 now, if version==1, then enable (value,gasPrice,gasLimit,maxFeePerGas,maxPriorityFeePerGas) fields + * @param group_id group id + * @param chain_id chain id + * @param to contract address, if it is a contract creation transaction, it can be empty + * @param nonce nonce, random number to avoid duplicate transactions + * @param input encoded contract method and params + * @param abi contract abi, only create contract need + * @param block_limit block limit + * @param value transfer value + * @param gas_price gas price + * @param gas_limit gas limit + * @param max_fee_per_gas max fee per gas + * @param max_priority_fee_per_gas max priority fee per gas + * @param attribute transaction attribute + * @param extra_data extra data in transaction + * @return const char* encoded transaction hex string + */ +const char* bcos_sdk_create_signed_transaction_with_signature(const char* signature, + const char* transaction_hash, transaction_version version, const char* group_id, const char* chain_id, + const char* to, const char* nonce, const char* input, const char* abi, int64_t block_limit, + const char* value, const char* gas_price, int64_t gas_limit, const char* max_fee_per_gas, + const char* max_priority_fee_per_gas, int32_t attribute, const char* extra_data); + +/** + * @brief create transaction with full fields, with version 1 by default + * @note version 1 transaction only supported in FISCO BCOS 3.6.0 and later + * + * @param key_pair key pair pointer + * @param group_id group id + * @param chain_id chain id + * @param to contract address, if it is a contract creation transaction, it can be empty + * @param nonce nonce, random number to avoid duplicate transactions + * @param input encoded contract method and params + * @param abi contract abi, only create contract need + * @param block_limit block limit + * @param value transfer value + * @param gas_price gas price + * @param gas_limit gas limit + * @param attribute transaction attribute + * @param extra_data extra data in transaction + * @param tx_hash output transaction hash hex string + * @param signed_tx output signed transaction hex string + */ +void bcos_sdk_create_signed_transaction_with_full_fields(void* key_pair, const char* group_id, + const char* chain_id, const char* to, const char* input, const char* abi, int64_t block_limit, + const char* value, const char* gas_price, int64_t gas_limit, int32_t attribute, + const char* extra_data, char** tx_hash, char** signed_tx); + +/** + * @brief create eip1559 transaction with full fields, with version 1 by default + * @note version 1 transaction only supported in FISCO BCOS 3.6.0 and later + * + * @param key_pair key pair pointer + * @param group_id group id + * @param chain_id chain id + * @param to contract address, if it is a contract creation transaction, it can be empty + * @param input encoded contract method and params + * @param abi contract abi, only create contract need + * @param block_limit block limit + * @param value transfer value + * @param gas_limit gas limit + * @param max_fee_per_gas max fee per gas + * @param max_priority_fee_per_gas max priority fee per gas + * @param attribute transaction attribute + * @param extra_data extra data in transaction + * @param tx_hash output transaction hash hex string + * @param signed_tx output signed transaction hex string + */ +void bcos_sdk_create_signed_eip1559_transaction_with_full_fields(void* key_pair, + const char* group_id, const char* chain_id, const char* to, const char* input, const char* abi, + int64_t block_limit, const char* value, int64_t gas_limit, const char* max_fee_per_gas, + const char* max_priority_fee_per_gas, int32_t attribute, const char* extra_data, char** tx_hash, + char** signed_tx); + +#ifdef __cplusplus +} +#endif +#endif // BCOS_C_SDK_BCOS_SDK_C_UTI_TX_V2_H diff --git a/bindings/java/jni/src/main/resources/META-INF/native/file.list b/bindings/java/jni/src/main/resources/META-INF/native/file.list new file mode 100644 index 000000000..5e3f02a16 --- /dev/null +++ b/bindings/java/jni/src/main/resources/META-INF/native/file.list @@ -0,0 +1,7 @@ +bcos-sdk-jni.dll +bcos-sdk-jni.lib +libbcos-sdk-jni.dylib +libbcos-sdk-jni.so +libbcos-sdk-jni-aarch64.dylib +libbcos-sdk-jni-aarch64.so +libbcos-sdk-jni-static-aarch64.a \ No newline at end of file diff --git a/vcpkg-configuration.json b/vcpkg-configuration.json index 98c7092a7..446f88340 100644 --- a/vcpkg-configuration.json +++ b/vcpkg-configuration.json @@ -3,7 +3,7 @@ { "kind": "git", "repository": "https://github.com/FISCO-BCOS/registry", - "baseline": "717d8236aae1c032cfdd9469fd8248c8e49da6d3", + "baseline": "c15466a352b8daede8d2c447670a761ad736c132", "packages": [ "openssl", "hsm-crypto", From c943e8183ba8f2e87a37ae19f44efe7bf86b6523 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Thu, 30 Nov 2023 15:11:10 +0800 Subject: [PATCH 13/29] (jni): impl transaction builder v2 in jni. (#192) --- bindings/java/jni/build.gradle | 2 +- bindings/java/jni/scripts/java2jni.sh | 2 +- .../main/c/jni/org_fisco_bcos_sdk_common.h | 5 + .../org_fisco_bcos_sdk_jni_BcosSDKJniObj.cpp | 27 + .../org_fisco_bcos_sdk_jni_BcosSDKJniObj.h | 16 + ...i_utilities_receipt_ReceiptBuilderJniObj.h | 8 + ..._jni_utilities_signature_SignatureJniObj.h | 2 +- ...tilities_tx_TransactionBuilderV2JniObj.cpp | 470 ++++++++++++++++++ ..._utilities_tx_TransactionBuilderV2JniObj.h | 69 +++ ...lities_tx_TransactionStructBuilderJniObj.h | 17 +- .../org/fisco/bcos/sdk/jni/BcosSDKJniObj.java | 4 + .../receipt/ReceiptBuilderJniObj.java | 3 + .../sdk/jni/utilities/tx/Transaction.java | 98 ++-- .../tx/TransactionBuilderV2JniObj.java | 231 +++++++++ .../jni/utilities/tx/TransactionBytes.java | 28 +- .../sdk/jni/utilities/tx/TransactionData.java | 144 +++--- .../tx/TransactionStructBuilderJniObj.java | 118 ++--- .../jni/utilities/tx/TransactionVersion.java | 16 + 18 files changed, 1055 insertions(+), 205 deletions(-) create mode 100644 bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp create mode 100644 bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.h create mode 100644 bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV2JniObj.java create mode 100644 bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion.java diff --git a/bindings/java/jni/build.gradle b/bindings/java/jni/build.gradle index 91b3cc879..84d24d28b 100644 --- a/bindings/java/jni/build.gradle +++ b/bindings/java/jni/build.gradle @@ -31,7 +31,7 @@ dependencies { archivesBaseName = 'bcos-sdk-jni' group = 'org.fisco-bcos' -version = '3.5.1-SNAPSHOT' +version = '3.6.0-SNAPSHOT' // Additional attribute definition ext { diff --git a/bindings/java/jni/scripts/java2jni.sh b/bindings/java/jni/scripts/java2jni.sh index 3afbb5c06..35c0f91ec 100644 --- a/bindings/java/jni/scripts/java2jni.sh +++ b/bindings/java/jni/scripts/java2jni.sh @@ -30,7 +30,7 @@ function convert_java_to_jni() { # convert_java_to_jni ${JAVA_SOURCE_PATH} # classes="rpc.RpcJniObj amop.AmopJniObj event.EventSubJniObj BcosSDKJniObj" -classes="utilities.keypair.KeyPairJniObj utilities.tx.TransactionBuilderJniObj utilities.receipt.ReceiptBuilderJniObj utilities.signature.SignatureJniObj" +classes="BcosSDKJniObj utilities.keypair.KeyPairJniObj utilities.tx.TransactionBuilderJniObj utilities.tx.TransactionBuilderV2JniObj utilities.tx.TransactionStructBuilderJniObj utilities.receipt.ReceiptBuilderJniObj utilities.signature.SignatureJniObj" for class in ${classes} do diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_common.h b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_common.h index 52724e0c5..c154028db 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_common.h +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_common.h @@ -9,6 +9,11 @@ extern "C" { #endif +#define GET_J_STRING_CONTENT(env, jstr) \ + (env)->GetStringUTFChars((jstr), NULL) +#define GET_J_STRING_CONTENT_DEF(env, jstr, def) \ + ((jstr) == NULL ? (def) : GET_J_STRING_CONTENT((env), (jstr))) + struct cb_context { JavaVM* jvm; diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.cpp index a2181f843..2dde68587 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.cpp @@ -128,6 +128,33 @@ static void on_receive_block_notifier(const char* group, int64_t block_number, v // env->DeleteGlobalRef(jcallback); } + +/* + * Class: org_fisco_bcos_sdk_jni_BcosSDKJniObj + * Method: localProtocolInfo + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_localProtocolInfo( + JNIEnv* env, jobject self) +{ + void* sdk = bcos_sdk_get_native_pointer(env, self); + uint32_t info = bcos_sdk_get_local_protocol_info(sdk); + return (jint)info; +} + +/* + * Class: org_fisco_bcos_sdk_jni_BcosSDKJniObj + * Method: negotiatedProtocolInfo + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_negotiatedProtocolInfo( + JNIEnv* env, jobject self) +{ + void* sdk = bcos_sdk_get_native_pointer(env, self); + uint32_t info = bcos_sdk_get_negotiated_protocol_info(sdk); + return (jint)info; +} + /* * Class: org_fisco_bcos_sdk_jni_BcosSDKJniObj * Method: registerBlockNotifier diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.h b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.h index 6bbf194a1..11835a621 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.h +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.h @@ -35,6 +35,22 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_stop(JNIEnv*, j */ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_destroy(JNIEnv*, jobject); +/* + * Class: org_fisco_bcos_sdk_jni_BcosSDKJniObj + * Method: localProtocolInfo + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_localProtocolInfo( + JNIEnv*, jobject); + +/* + * Class: org_fisco_bcos_sdk_jni_BcosSDKJniObj + * Method: negotiatedProtocolInfo + * Signature: ()I + */ +JNIEXPORT jint JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_negotiatedProtocolInfo( + JNIEnv*, jobject); + /* * Class: org_fisco_bcos_sdk_jni_BcosSDKJniObj * Method: registerBlockNotifier diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj.h b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj.h index bde36c1cd..bdab48722 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj.h +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj.h @@ -55,6 +55,14 @@ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptB JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj_calcReceiptDataHash (JNIEnv *, jclass, jint, jlong); +/* + * Class: org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj + * Method: calcReceiptDataHashWithJson + * Signature: (ILjava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj_calcReceiptDataHashWithJson + (JNIEnv *, jclass, jint, jstring); + #ifdef __cplusplus } #endif diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj.h b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj.h index 29c7bf6b2..b261e9a02 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj.h +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj.h @@ -10,7 +10,7 @@ extern "C" { /* * Class: org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj * Method: sign - * Signature: (JLjava/lang/String;Ljava/lang/String;)Ljava/lang/String; + * Signature: (JLjava/lang/String;Ljava/lang/String;)[B */ JNIEXPORT jbyteArray JNICALL Java_org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj_sign (JNIEnv *, jclass, jlong, jstring, jstring); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp new file mode 100644 index 000000000..d1579942b --- /dev/null +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp @@ -0,0 +1,470 @@ +/** + * Copyright (C) 2022 FISCO BCOS. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @file org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp + * @author: kyonGuo + * @date 2023/11/29 + */ + +#include "org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.h" +#include "bcos-c-sdk/bcos_sdk_c_error.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_keypair.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h" +#include "org_fisco_bcos_sdk_common.h" +#include + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Method: createTransactionData + * Signature: + * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;J)J + */ +JNIEXPORT jlong JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createTransactionData( + JNIEnv* env, jclass, jstring j_group_id, jstring j_chain_id, jstring j_to, jstring j_input, + jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_gas_price, jlong j_gas_limit) +{ + checkJString(env, j_group_id); + checkJString(env, j_chain_id); + checkJString(env, j_input); + + const char* group_id = GET_J_STRING_CONTENT(env, j_group_id); + const char* chain_id = GET_J_STRING_CONTENT(env, j_chain_id); + const char* to = GET_J_STRING_CONTENT_DEF(env, j_to, NULL); + const char* input = GET_J_STRING_CONTENT(env, j_input); + const char* abi = GET_J_STRING_CONTENT_DEF(env, j_abi, NULL); + const char* value = GET_J_STRING_CONTENT_DEF(env, j_value, NULL); + const char* gas_price = GET_J_STRING_CONTENT_DEF(env, j_gas_price, NULL); + int64_t block_limit = (int64_t)j_block_limit; + int64_t gas_limit = (int64_t)j_gas_limit; + + void* transaction_data = bcos_sdk_create_transaction_v2_data( + group_id, chain_id, to, input, abi, block_limit, value, gas_price, gas_limit); + + env->ReleaseStringUTFChars(j_group_id, group_id); + env->ReleaseStringUTFChars(j_chain_id, chain_id); + env->ReleaseStringUTFChars(j_to, to); + env->ReleaseStringUTFChars(j_input, input); + env->ReleaseStringUTFChars(j_abi, abi); + env->ReleaseStringUTFChars(j_value, value); + env->ReleaseStringUTFChars(j_gas_price, gas_price); + + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + } + + return reinterpret_cast(transaction_data); +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Method: createEIP1559TransactionData + * Signature: + * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;JLjava/lang/String;Ljava/lang/String;)J + */ +JNIEXPORT jlong JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createEIP1559TransactionData( + JNIEnv* env, jclass, jstring j_group_id, jstring j_chain_id, jstring j_to, jstring j_input, + jstring j_abi, jlong j_block_limit, jstring j_value, jlong j_gas_limit, + jstring j_max_fee_per_gas, jstring j_max_priority_fee_per_gas) +{ + checkJString(env, j_group_id); + checkJString(env, j_chain_id); + checkJString(env, j_input); + + const char* group_id = GET_J_STRING_CONTENT(env, j_group_id); + const char* chain_id = GET_J_STRING_CONTENT(env, j_chain_id); + const char* to = GET_J_STRING_CONTENT_DEF(env, j_to, NULL); + const char* input = GET_J_STRING_CONTENT(env, j_input); + const char* abi = GET_J_STRING_CONTENT_DEF(env, j_abi, NULL); + const char* value = GET_J_STRING_CONTENT_DEF(env, j_value, NULL); + const char* max_fee_per_gas = GET_J_STRING_CONTENT_DEF(env, j_max_fee_per_gas, NULL); + const char* max_priority_fee_per_gas = + GET_J_STRING_CONTENT_DEF(env, j_max_priority_fee_per_gas, NULL); + int64_t block_limit = (int64_t)j_block_limit; + int64_t gas_limit = (int64_t)j_gas_limit; + + void* transaction_data = bcos_sdk_create_eip1559_transaction_data(group_id, chain_id, to, input, + abi, block_limit, value, gas_limit, max_fee_per_gas, max_priority_fee_per_gas); + + env->ReleaseStringUTFChars(j_group_id, group_id); + env->ReleaseStringUTFChars(j_chain_id, chain_id); + env->ReleaseStringUTFChars(j_to, to); + env->ReleaseStringUTFChars(j_input, input); + env->ReleaseStringUTFChars(j_abi, abi); + env->ReleaseStringUTFChars(j_value, value); + env->ReleaseStringUTFChars(j_max_fee_per_gas, max_fee_per_gas); + env->ReleaseStringUTFChars(j_max_priority_fee_per_gas, max_priority_fee_per_gas); + + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + } + + return reinterpret_cast(transaction_data); +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Method: calcTransactionDataHashWithFullFields + * Signature: + * (ILorg/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransactionDataHashWithFullFields( + JNIEnv* env, jclass, jint j_crypto_type, jobject j_tx_version, jstring j_group_id, + jstring j_chain_id, jstring j_to, jstring j_nonce, jstring j_input, jstring j_abi, + jlong j_block_limit, jstring j_value, jstring j_gasPrice, jlong j_gas_limit, + jstring j_max_fee_per_gas, jstring j_max_priority_fee_per_gas) +{ + checkJString(env, j_group_id); + checkJString(env, j_chain_id); + checkJString(env, j_input); + checkJString(env, j_nonce); + + const char* group_id = GET_J_STRING_CONTENT(env, j_group_id); + const char* chain_id = GET_J_STRING_CONTENT(env, j_chain_id); + const char* to = GET_J_STRING_CONTENT_DEF(env, j_to, NULL); + const char* nonce = GET_J_STRING_CONTENT(env, j_nonce); + const char* input = GET_J_STRING_CONTENT(env, j_input); + const char* abi = GET_J_STRING_CONTENT_DEF(env, j_abi, NULL); + const char* value = GET_J_STRING_CONTENT_DEF(env, j_value, NULL); + const char* gas_price = GET_J_STRING_CONTENT_DEF(env, j_gasPrice, NULL); + const char* max_fee_per_gas = GET_J_STRING_CONTENT_DEF(env, j_max_fee_per_gas, NULL); + const char* max_priority_fee_per_gas = + GET_J_STRING_CONTENT_DEF(env, j_max_priority_fee_per_gas, NULL); + int crypto_type = (int)j_crypto_type; + int64_t block_limit = (int64_t)j_block_limit; + int64_t gas_limit = (int64_t)j_gas_limit; + + jclass tx_version = env->GetObjectClass(j_tx_version); + jmethodID j_methodId = env->GetMethodID(tx_version, "getValue", "()I"); + int32_t ver = (int32_t)env->CallIntMethod(j_tx_version, j_methodId); + + const char* tx_hash = bcos_sdk_calc_transaction_data_hash_with_full_fields(crypto_type, + (transaction_version)ver, group_id, chain_id, to, nonce, input, abi, block_limit, value, + gas_price, gas_limit, max_fee_per_gas, max_priority_fee_per_gas); + + env->ReleaseStringUTFChars(j_group_id, group_id); + env->ReleaseStringUTFChars(j_chain_id, chain_id); + env->ReleaseStringUTFChars(j_to, to); + env->ReleaseStringUTFChars(j_nonce, nonce); + env->ReleaseStringUTFChars(j_input, input); + env->ReleaseStringUTFChars(j_abi, abi); + env->ReleaseStringUTFChars(j_value, value); + env->ReleaseStringUTFChars(j_gasPrice, gas_price); + env->ReleaseStringUTFChars(j_max_fee_per_gas, max_fee_per_gas); + env->ReleaseStringUTFChars(j_max_priority_fee_per_gas, max_priority_fee_per_gas); + + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + } + + jstring j_transaction_data_hash = env->NewStringUTF(tx_hash); + + if (tx_hash) + { + free((void*)tx_hash); + tx_hash = NULL; + } + + return j_transaction_data_hash; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Method: calcTransactionDataHashWithJson + * Signature: (ILjava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransactionDataHashWithJson( + JNIEnv* env, jclass, jint j_crypto_type, jstring j_json) +{ + checkJString(env, j_json); + const char* json = env->GetStringUTFChars(j_json, NULL); + int crypto_type = (int)j_crypto_type; + + const char* tx_hash = bcos_sdk_calc_transaction_data_hash_with_json(crypto_type, json); + + env->ReleaseStringUTFChars(j_json, json); + + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + } + + jstring j_transaction_data_hash = env->NewStringUTF(tx_hash); + + if (tx_hash) + { + free((void*)tx_hash); + tx_hash = NULL; + } + + return j_transaction_data_hash; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Method: createSignedTransactionWithSignature + * Signature: + * (Ljava/lang/String;Ljava/lang/String;Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;ILjava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedTransactionWithSignature( + JNIEnv* env, jclass, jstring j_signature, jstring j_tx_hash, jobject j_tx_version, + jstring j_group_id, jstring j_chain_id, jstring j_to, jstring j_nonce, jstring j_input, + jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_gas_price, jlong j_gas_limit, + jstring j_max_fee_per_gas, jstring j_max_priority_fee_per_gas, jint j_attribute, + jstring j_extra_data) +{ + checkJString(env, j_signature); + checkJString(env, j_tx_hash); + checkJString(env, j_group_id); + checkJString(env, j_chain_id); + checkJString(env, j_input); + checkJString(env, j_nonce); + + int64_t block_limit = (int64_t)j_block_limit; + int64_t gas_limit = (int64_t)j_gas_limit; + int32_t attribute = (int32_t)j_attribute; + const char* signature = GET_J_STRING_CONTENT(env, j_signature); + const char* tx_hash = GET_J_STRING_CONTENT(env, j_tx_hash); + const char* group_id = GET_J_STRING_CONTENT(env, j_group_id); + const char* chain_id = GET_J_STRING_CONTENT(env, j_chain_id); + const char* to = GET_J_STRING_CONTENT_DEF(env, j_to, NULL); + const char* nonce = GET_J_STRING_CONTENT(env, j_nonce); + const char* input = GET_J_STRING_CONTENT(env, j_input); + const char* abi = GET_J_STRING_CONTENT_DEF(env, j_abi, NULL); + const char* value = GET_J_STRING_CONTENT_DEF(env, j_value, NULL); + const char* gas_price = GET_J_STRING_CONTENT_DEF(env, j_gas_price, NULL); + const char* max_fee_per_gas = GET_J_STRING_CONTENT_DEF(env, j_max_fee_per_gas, NULL); + const char* max_priority_fee_per_gas = + GET_J_STRING_CONTENT_DEF(env, j_max_priority_fee_per_gas, NULL); + const char* extra_data = GET_J_STRING_CONTENT_DEF(env, j_extra_data, NULL); + + jclass tx_version = env->GetObjectClass(j_tx_version); + jmethodID j_methodId = env->GetMethodID(tx_version, "getValue", "()I"); + int32_t ver = (int32_t)env->CallIntMethod(j_tx_version, j_methodId); + + const char* encodedTx = bcos_sdk_create_signed_transaction_with_signature(signature, tx_hash, + (transaction_version)ver, group_id, chain_id, to, nonce, input, abi, block_limit, value, + gas_price, gas_limit, max_fee_per_gas, max_priority_fee_per_gas, attribute, extra_data); + + env->ReleaseStringUTFChars(j_signature, signature); + env->ReleaseStringUTFChars(j_tx_hash, tx_hash); + env->ReleaseStringUTFChars(j_group_id, group_id); + env->ReleaseStringUTFChars(j_chain_id, chain_id); + env->ReleaseStringUTFChars(j_to, to); + env->ReleaseStringUTFChars(j_nonce, nonce); + env->ReleaseStringUTFChars(j_input, input); + env->ReleaseStringUTFChars(j_abi, abi); + env->ReleaseStringUTFChars(j_value, value); + env->ReleaseStringUTFChars(j_gas_price, gas_price); + env->ReleaseStringUTFChars(j_max_fee_per_gas, max_fee_per_gas); + env->ReleaseStringUTFChars(j_max_priority_fee_per_gas, max_priority_fee_per_gas); + env->ReleaseStringUTFChars(j_extra_data, extra_data); + + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + } + + jstring encoded_tx_hex = env->NewStringUTF(encodedTx); + + if (encodedTx) + { + free((void*)encodedTx); + encodedTx = NULL; + } + + return encoded_tx_hex; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Method: createSignedTransactionWithFullFields + * Signature: + * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; + */ +JNIEXPORT jobject JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedTransactionWithFullFields( + JNIEnv* env, jclass, jlong j_key_pair, jstring j_group_id, jstring j_chain_id, jstring j_to, + jstring j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_gas_price, + jlong j_gas_limit, jint j_attribute, jstring j_extra_data) +{ + checkJString(env, j_group_id); + checkJString(env, j_chain_id); + checkJString(env, j_input); + + void* keypair = reinterpret_cast(j_key_pair); + int64_t block_limit = (int64_t)j_block_limit; + int64_t gas_limit = (int64_t)j_gas_limit; + int32_t attribute = (int32_t)j_attribute; + const char* group_id = GET_J_STRING_CONTENT(env, j_group_id); + const char* chain_id = GET_J_STRING_CONTENT(env, j_chain_id); + const char* to = GET_J_STRING_CONTENT_DEF(env, j_to, NULL); + const char* input = GET_J_STRING_CONTENT(env, j_input); + const char* abi = GET_J_STRING_CONTENT_DEF(env, j_abi, NULL); + const char* value = GET_J_STRING_CONTENT_DEF(env, j_value, NULL); + const char* gas_price = GET_J_STRING_CONTENT_DEF(env, j_gas_price, NULL); + const char* extra_data = GET_J_STRING_CONTENT_DEF(env, j_extra_data, NULL); + + char* tx_hash = NULL; + char* signed_tx = NULL; + bcos_sdk_create_signed_transaction_with_full_fields(keypair, group_id, chain_id, to, input, abi, + block_limit, value, gas_price, gas_limit, attribute, extra_data, &tx_hash, &signed_tx); + + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + } + + jstring j_tx_hash = env->NewStringUTF(tx_hash); + jstring j_signed_tx = env->NewStringUTF(signed_tx); + + jclass j_txpair = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TxPair"); + if (j_txpair == NULL) + { + env->FatalError( + "No such class, className: " + "org/fisco/bcos/sdk/jni/utilities/tx/TxPair"); + } + jmethodID txpair_mtd = + env->GetMethodID(j_txpair, "", "(Ljava/lang/String;Ljava/lang/String;)V"); + if (txpair_mtd == NULL) + { + env->FatalError("No such constructor in TxPair, constructor(String, String)"); + } + + jobject txpair = env->NewObject(j_txpair, txpair_mtd, j_tx_hash, j_signed_tx); + + env->ReleaseStringUTFChars(j_group_id, group_id); + env->ReleaseStringUTFChars(j_chain_id, chain_id); + env->ReleaseStringUTFChars(j_to, to); + env->ReleaseStringUTFChars(j_input, input); + env->ReleaseStringUTFChars(j_abi, abi); + env->ReleaseStringUTFChars(j_value, value); + env->ReleaseStringUTFChars(j_gas_price, gas_price); + env->ReleaseStringUTFChars(j_extra_data, extra_data); + + env->DeleteLocalRef(j_tx_hash); + env->DeleteLocalRef(j_signed_tx); + + if (tx_hash) + { + free((void*)tx_hash); + tx_hash = NULL; + } + + if (signed_tx) + { + free((void*)signed_tx); + signed_tx = NULL; + } + + return txpair; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Method: createSignedEIP1559TransactionWithFullFields + * Signature: + * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; + */ +JNIEXPORT jobject JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedEIP1559TransactionWithFullFields( + JNIEnv* env, jclass, jlong j_key_pair, jstring j_group_id, jstring j_chain_id, jstring j_to, + jstring j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_max_fee_per_gas, + jstring j_max_priority_fee_per_gas, jlong j_gas_limit, jint j_attribute, jstring j_extra_data) +{ + checkJString(env, j_group_id); + checkJString(env, j_chain_id); + checkJString(env, j_input); + + void* keypair = reinterpret_cast(j_key_pair); + int64_t block_limit = (int64_t)j_block_limit; + int64_t gas_limit = (int64_t)j_gas_limit; + int32_t attribute = (int32_t)j_attribute; + const char* group_id = GET_J_STRING_CONTENT(env, j_group_id); + const char* chain_id = GET_J_STRING_CONTENT(env, j_chain_id); + const char* to = GET_J_STRING_CONTENT_DEF(env, j_to, NULL); + const char* input = GET_J_STRING_CONTENT(env, j_input); + const char* abi = GET_J_STRING_CONTENT_DEF(env, j_abi, NULL); + const char* value = GET_J_STRING_CONTENT_DEF(env, j_value, NULL); + const char* max_fee_per_gas = GET_J_STRING_CONTENT_DEF(env, j_max_fee_per_gas, NULL); + const char* max_priority_fee_per_gas = + GET_J_STRING_CONTENT_DEF(env, j_max_priority_fee_per_gas, NULL); + const char* extra_data = GET_J_STRING_CONTENT_DEF(env, j_extra_data, NULL); + + char* tx_hash = NULL; + char* signed_tx = NULL; + bcos_sdk_create_signed_eip1559_transaction_with_full_fields(keypair, group_id, chain_id, to, + input, abi, block_limit, value, gas_limit, max_fee_per_gas, max_priority_fee_per_gas, + attribute, extra_data, &tx_hash, &signed_tx); + + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + } + + jstring j_tx_hash = env->NewStringUTF(tx_hash); + jstring j_signed_tx = env->NewStringUTF(signed_tx); + + jclass j_txpair = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TxPair"); + if (j_txpair == NULL) + { + env->FatalError( + "No such class, className: " + "org/fisco/bcos/sdk/jni/utilities/tx/TxPair"); + } + jmethodID txpair_mtd = + env->GetMethodID(j_txpair, "", "(Ljava/lang/String;Ljava/lang/String;)V"); + if (txpair_mtd == NULL) + { + env->FatalError("No such constructor in TxPair, constructor(String, String)"); + } + + jobject txpair = env->NewObject(j_txpair, txpair_mtd, j_tx_hash, j_signed_tx); + + env->ReleaseStringUTFChars(j_group_id, group_id); + env->ReleaseStringUTFChars(j_chain_id, chain_id); + env->ReleaseStringUTFChars(j_to, to); + env->ReleaseStringUTFChars(j_input, input); + env->ReleaseStringUTFChars(j_abi, abi); + env->ReleaseStringUTFChars(j_value, value); + env->ReleaseStringUTFChars(j_max_fee_per_gas, max_fee_per_gas); + env->ReleaseStringUTFChars(j_max_priority_fee_per_gas, max_priority_fee_per_gas); + env->ReleaseStringUTFChars(j_extra_data, extra_data); + + env->DeleteLocalRef(j_tx_hash); + env->DeleteLocalRef(j_signed_tx); + + if (tx_hash) + { + free((void*)tx_hash); + tx_hash = NULL; + } + + if (signed_tx) + { + free((void*)signed_tx); + signed_tx = NULL; + } + + return txpair; +} diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.h b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.h new file mode 100644 index 000000000..00643340e --- /dev/null +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.h @@ -0,0 +1,69 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj */ + +#ifndef _Included_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj +#define _Included_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Method: createTransactionData + * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;J)J + */ +JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createTransactionData + (JNIEnv *, jclass, jstring, jstring, jstring, jstring, jstring, jlong, jstring, jstring, jlong); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Method: createEIP1559TransactionData + * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;JLjava/lang/String;Ljava/lang/String;)J + */ +JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createEIP1559TransactionData + (JNIEnv *, jclass, jstring, jstring, jstring, jstring, jstring, jlong, jstring, jlong, jstring, jstring); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Method: calcTransactionDataHashWithFullFields + * Signature: (ILorg/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransactionDataHashWithFullFields + (JNIEnv *, jclass, jint, jobject, jstring, jstring, jstring, jstring, jstring, jstring, jlong, jstring, jstring, jlong, jstring, jstring); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Method: calcTransactionDataHashWithJson + * Signature: (ILjava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransactionDataHashWithJson + (JNIEnv *, jclass, jint, jstring); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Method: createSignedTransactionWithSignature + * Signature: (Ljava/lang/String;Ljava/lang/String;Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;ILjava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedTransactionWithSignature + (JNIEnv *, jclass, jstring, jstring, jobject, jstring, jstring, jstring, jstring, jstring, jstring, jlong, jstring, jstring, jlong, jstring, jstring, jint, jstring); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Method: createSignedTransactionWithFullFields + * Signature: (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; + */ +JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedTransactionWithFullFields + (JNIEnv *, jclass, jlong, jstring, jstring, jstring, jstring, jstring, jlong, jstring, jstring, jlong, jint, jstring); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Method: createSignedEIP1559TransactionWithFullFields + * Signature: (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; + */ +JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedEIP1559TransactionWithFullFields + (JNIEnv *, jclass, jlong, jstring, jstring, jstring, jstring, jstring, jlong, jstring, jstring, jstring, jlong, jint, jstring); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.h b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.h index 0c3eba092..ba591e8a8 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.h +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.h @@ -7,11 +7,10 @@ #ifdef __cplusplus extern "C" { #endif - /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj * Method: encodeTransactionDataStruct - * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TxData;)Ljava/lang/String; + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTransactionDataStruct( @@ -20,7 +19,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj * Method: encodeTransactionDataStructToJson - * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TxData;)Ljava/lang/String; + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTransactionDataStructToJson( @@ -29,7 +28,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj * Method: decodeTransactionDataStruct - * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxData; + * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData; */ JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionDataStruct( @@ -38,7 +37,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTr /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj * Method: calcTransactionDataStructHash - * Signature: (ILorg/fisco/bcos/sdk/jni/utilities/tx/TxData;)Ljava/lang/String; + * Signature: (ILorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_calcTransactionDataStructHash( @@ -48,7 +47,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_calcTran * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj * Method: createEncodedTransaction * Signature: - * (Lorg/fisco/bcos/sdk/jni/utilities/tx/TxData;Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;)Ljava/lang/String; + * (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_createEncodedTransaction( @@ -57,7 +56,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_createEn /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj * Method: encodeTransactionStruct - * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/Tx;)Ljava/lang/String; + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/Transaction;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTransactionStruct( @@ -66,7 +65,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj * Method: encodeTransactionStructToJson - * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/Tx;)Ljava/lang/String; + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/Transaction;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTransactionStructToJson( @@ -75,7 +74,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj * Method: decodeTransactionStruct - * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/Tx; + * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/Transaction; */ JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionStruct( diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/BcosSDKJniObj.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/BcosSDKJniObj.java index de1e31400..4f48ae79c 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/BcosSDKJniObj.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/BcosSDKJniObj.java @@ -74,6 +74,10 @@ private void setNativePointer(long nativePointer) { public native void destroy(); + public native int localProtocolInfo(); + + public native int negotiatedProtocolInfo(); + // -------------------------native method end------------------------------------------- // ------------------------ block notifier begin---------------------------------------- diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/receipt/ReceiptBuilderJniObj.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/receipt/ReceiptBuilderJniObj.java index 6a3ca5f6c..1b2ff5554 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/receipt/ReceiptBuilderJniObj.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/receipt/ReceiptBuilderJniObj.java @@ -51,4 +51,7 @@ public static native long createReceiptData( */ public static native String calcReceiptDataHash(int cryptoType, long ReceiptData) throws JniException; + + public static native String calcReceiptDataHashWithJson(int cryptoType, String json) + throws JniException; } diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/Transaction.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/Transaction.java index 6b78ad2cb..be12e487c 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/Transaction.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/Transaction.java @@ -1,67 +1,67 @@ package org.fisco.bcos.sdk.jni.utilities.tx; public class Transaction { - private TransactionData transactionData; - private TransactionBytes dataHash; - private TransactionBytes signature; - private TransactionBytes sender; - private long importTime; - private int attribute; - private String extraData; + private TransactionData transactionData; + private TransactionBytes dataHash; + private TransactionBytes signature; + private TransactionBytes sender; + private long importTime; + private int attribute; + private String extraData; - public TransactionData getTransactionData() { - return transactionData; - } + public TransactionData getTransactionData() { + return transactionData; + } - public void setTransactionData(TransactionData transactionData) { - this.transactionData = transactionData; - } + public void setTransactionData(TransactionData transactionData) { + this.transactionData = transactionData; + } - public TransactionBytes getDataHash() { - return dataHash; - } + public TransactionBytes getDataHash() { + return dataHash; + } - public void setDataHash(TransactionBytes dataHash) { - this.dataHash = dataHash; - } + public void setDataHash(TransactionBytes dataHash) { + this.dataHash = dataHash; + } - public TransactionBytes getSignature() { - return signature; - } + public TransactionBytes getSignature() { + return signature; + } - public void setSignature(TransactionBytes signature) { - this.signature = signature; - } + public void setSignature(TransactionBytes signature) { + this.signature = signature; + } - public TransactionBytes getSender() { - return sender; - } + public TransactionBytes getSender() { + return sender; + } - public void setSender(TransactionBytes sender) { - this.sender = sender; - } + public void setSender(TransactionBytes sender) { + this.sender = sender; + } - public long getImportTime() { - return importTime; - } + public long getImportTime() { + return importTime; + } - public void setImportTime(long importTime) { - this.importTime = importTime; - } + public void setImportTime(long importTime) { + this.importTime = importTime; + } - public int getAttribute() { - return attribute; - } + public int getAttribute() { + return attribute; + } - public void setAttribute(int attribute) { - this.attribute = attribute; - } + public void setAttribute(int attribute) { + this.attribute = attribute; + } - public String getExtraData() { - return extraData; - } + public String getExtraData() { + return extraData; + } - public void setExtraData(String extraData) { - this.extraData = extraData; - } + public void setExtraData(String extraData) { + this.extraData = extraData; + } } diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV2JniObj.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV2JniObj.java new file mode 100644 index 000000000..cee0c9f91 --- /dev/null +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV2JniObj.java @@ -0,0 +1,231 @@ +package org.fisco.bcos.sdk.jni.utilities.tx; + +import org.fisco.bcos.sdk.jni.common.JniException; +import org.fisco.bcos.sdk.jni.common.JniLibLoader; + +public class TransactionBuilderV2JniObj extends TransactionBuilderJniObj { + + static { + JniLibLoader.loadJniLibrary(); + } + + /** + * create transaction data with version 1 by default + * + * @param groupID group id + * @param chainID chain id + * @param to contract address, if it is a contract creation transaction, it can be empty + * @param input encoded contract method and params + * @param abi contract abi, only create contract need + * @param blockLimit block limit + * @param value transfer value + * @param gasPrice gas price + * @param gasLimit gas limit + * @return void* transaction data pointer, you should release it after use, check + * `destroyTransactionData` + * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later + */ + public static native long createTransactionData( + String groupID, + String chainID, + String to, + String input, + String abi, + long blockLimit, + String value, + String gasPrice, + long gasLimit) + throws JniException; + + /** + * create eip1559 transaction data with version 1 by default + * + * @param groupID group id + * @param chainID chain id + * @param to contract address, if it is a contract creation transaction, it can be empty + * @param input encoded contract method and params + * @param abi contract abi, only create contract need + * @param blockLimit block limit + * @param value transfer value + * @param gasLimit gas limit + * @param maxFeePerGas max fee per gas + * @param maxPriorityFeePerGas max priority fee per gas + * @return void* transaction data pointer, you should release it after use, check + * `destroyTransactionData` + * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later + */ + public static native long createEIP1559TransactionData( + String groupID, + String chainID, + String to, + String input, + String abi, + long blockLimit, + String value, + long gasLimit, + String maxFeePerGas, + String maxPriorityFeePerGas) + throws JniException; + + /** + * create transaction data with full fields + * + * @param cryptoType 0: keccak256, 1: sm3 + * @param version tx version, if version==1, then enable + * (value,gasPrice,gasLimit,maxFeePerGas,maxPriorityFeePerGas) fields + * @param groupID group id + * @param chainID chain id + * @param to contract address, if it is a contract creation transaction, it can be empty + * @param nonce nonce, random number to avoid duplicate transactions + * @param input encoded contract method and params + * @param abi contract abi, only create contract need + * @param blockLimit block limit + * @param value transfer value + * @param gasPrice gas price + * @param gasLimit gas limit + * @param maxFeePerGas max fee per gas + * @param maxPriorityFeePerGas max priority fee per gas + * @return const char* transaction data hash hex string + * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later + */ + public static native String calcTransactionDataHashWithFullFields( + int cryptoType, + TransactionVersion version, + String groupID, + String chainID, + String to, + String nonce, + String input, + String abi, + long blockLimit, + String value, + String gasPrice, + long gasLimit, + String maxFeePerGas, + String maxPriorityFeePerGas) + throws JniException; + + /** + * create transaction data with json string + * + * @param cryptoType 0: keccak256, 1: sm3 + * @param json transaction data json string + * @return const char* transaction data hash hex string + * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later + * @throws JniException if lack of some required fields, or some fields are invalid + */ + public static native String calcTransactionDataHashWithJson(int cryptoType, String json) + throws JniException; + + /** + * create encoded transaction data with external signature + * + * @param signature signature hex string, if ECDSA, it is r||s||v, if SM2, it is r||s||pk + * @param txDataHash transactionData hash hex string + * @param version tx version, only support 0 and 1 now, if version==1, then enable + * (value,gasPrice,gasLimit,maxFeePerGas,maxPriorityFeePerGas) fields + * @param groupID group id + * @param chainID chain id + * @param to contract address, if it is a contract creation transaction, it can be empty + * @param nonce nonce, random number to avoid duplicate transactions + * @param input encoded contract method and params + * @param abi contract abi, only create contract need + * @param blockLimit block limit + * @param value transfer value + * @param gasPrice gas price + * @param gasLimit gas limit + * @param maxFeePerGas max fee per gas + * @param maxPriorityFeePerGas max priority fee per gas + * @param attribute transaction attribute + * @param extraData extra data in transaction + * @return const char* encoded transaction hex string + * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later + */ + public static native String createSignedTransactionWithSignature( + String signature, + String txDataHash, + TransactionVersion version, + String groupID, + String chainID, + String to, + String nonce, + String input, + String abi, + long blockLimit, + String value, + String gasPrice, + long gasLimit, + String maxFeePerGas, + String maxPriorityFeePerGas, + int attribute, + String extraData) + throws JniException; + + /** + * create transaction with full fields, with version 1 by default + * + * @param keyPair key pair pointer + * @param groupID group id + * @param chainID chain id + * @param to contract address, if it is a contract creation transaction, it can be empty + * @param input encoded contract method and params + * @param abi contract abi, only create contract need + * @param blockLimit block limit + * @param value transfer value + * @param gasPrice gas price + * @param gasLimit gas limit + * @param attribute transaction attribute + * @param extraData extra data in transaction + * @return TxPair + * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later + */ + public static native TxPair createSignedTransactionWithFullFields( + long keyPair, + String groupID, + String chainID, + String to, + String input, + String abi, + long blockLimit, + String value, + String gasPrice, + long gasLimit, + int attribute, + String extraData) + throws JniException; + + /** + * create eip1559 transaction with full fields, with version 1 by default + * + * @param keyPair key pair pointer + * @param groupID group id + * @param chainID chain id + * @param to contract address, if it is a contract creation transaction, it can be empty + * @param input encoded contract method and params + * @param abi contract abi, only create contract need + * @param blockLimit block limit + * @param value transfer value + * @param gasLimit gas limit + * @param maxFeePerGas max fee per gas + * @param maxPriorityFeePerGas max priority fee per gas + * @param attribute transaction attribute + * @param extraData extra data in transaction + * @return TxPair + * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later + */ + public static native TxPair createSignedEIP1559TransactionWithFullFields( + long keyPair, + String groupID, + String chainID, + String to, + String input, + String abi, + long blockLimit, + String value, + String maxFeePerGas, + String maxPriorityFeePerGas, + long gasLimit, + int attribute, + String extraData) + throws JniException; +} diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes.java index d00f01c8a..09e2b1495 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes.java @@ -1,22 +1,22 @@ package org.fisco.bcos.sdk.jni.utilities.tx; public class TransactionBytes { - private byte[] buffer; - private int length; + private byte[] buffer; + private int length; - public byte[] getBuffer() { - return buffer; - } + public byte[] getBuffer() { + return buffer; + } - public void setBuffer(byte[] buffer) { - this.buffer = buffer; - } + public void setBuffer(byte[] buffer) { + this.buffer = buffer; + } - public int getLength() { - return length; - } + public int getLength() { + return length; + } - public void setLength(int length) { - this.length = length; - } + public void setLength(int length) { + this.length = length; + } } diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionData.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionData.java index 30560a938..6baea446e 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionData.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionData.java @@ -1,76 +1,76 @@ package org.fisco.bcos.sdk.jni.utilities.tx; public class TransactionData { - private int version; - private long blockLimit; - private String chainId; - private String groupId; - private String nonce; - private String to; - private String abi; - private TransactionBytes input; - - public int getVersion() { - return version; - } - - public void setVersion(int version) { - this.version = version; - } - - public long getBlockLimit() { - return blockLimit; - } - - public void setBlockLimit(long blockLimit) { - this.blockLimit = blockLimit; - } - - public String getChainId() { - return chainId; - } - - public void setChainId(String chainId) { - this.chainId = chainId; - } - - public String getGroupId() { - return groupId; - } - - public void setGroupId(String groupId) { - this.groupId = groupId; - } - - public String getNonce() { - return nonce; - } - - public void setNonce(String nonce) { - this.nonce = nonce; - } - - public String getTo() { - return to; - } - - public void setTo(String to) { - this.to = to; - } - - public String getAbi() { - return abi; - } - - public void setAbi(String abi) { - this.abi = abi; - } - - public TransactionBytes getInput() { - return input; - } - - public void setInput(TransactionBytes input) { - this.input = input; - } + private int version; + private long blockLimit; + private String chainId; + private String groupId; + private String nonce; + private String to; + private String abi; + private TransactionBytes input; + + public int getVersion() { + return version; + } + + public void setVersion(int version) { + this.version = version; + } + + public long getBlockLimit() { + return blockLimit; + } + + public void setBlockLimit(long blockLimit) { + this.blockLimit = blockLimit; + } + + public String getChainId() { + return chainId; + } + + public void setChainId(String chainId) { + this.chainId = chainId; + } + + public String getGroupId() { + return groupId; + } + + public void setGroupId(String groupId) { + this.groupId = groupId; + } + + public String getNonce() { + return nonce; + } + + public void setNonce(String nonce) { + this.nonce = nonce; + } + + public String getTo() { + return to; + } + + public void setTo(String to) { + this.to = to; + } + + public String getAbi() { + return abi; + } + + public void setAbi(String abi) { + this.abi = abi; + } + + public TransactionBytes getInput() { + return input; + } + + public void setInput(TransactionBytes input) { + this.input = input; + } } diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderJniObj.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderJniObj.java index 15f30b09b..ec2c29e60 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderJniObj.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderJniObj.java @@ -5,70 +5,72 @@ public class TransactionStructBuilderJniObj { - static { - JniLibLoader.loadJniLibrary(); - } + static { + JniLibLoader.loadJniLibrary(); + } - /** - * @param transactionData Transaction Data struct - * @return Hexed Transaction Data - */ - public static native String encodeTransactionDataStruct(TransactionData transactionData) - throws JniException; + /** + * @param transactionData Transaction Data struct + * @return Hexed Transaction Data + */ + public static native String encodeTransactionDataStruct(TransactionData transactionData) + throws JniException; - /** - * @param transactionData Transaction Data struct - * @return Json Transaction Data - */ - public static native String encodeTransactionDataStructToJson(TransactionData transactionData) - throws JniException; + /** + * @param transactionData Transaction Data struct + * @return Json Transaction Data + */ + public static native String encodeTransactionDataStructToJson(TransactionData transactionData) + throws JniException; - /** - * @param transactionDataHex transactionData hex string - * @return TxData - */ - public static native TransactionData decodeTransactionDataStruct(String transactionDataHex) - throws JniException; + /** + * @param transactionDataHex transactionData hex string + * @return TxData + */ + public static native TransactionData decodeTransactionDataStruct(String transactionDataHex) + throws JniException; - /** - * @param cryptoType crypto type - * @param transactionData Transaction Data struct - * @return Hash hex string - */ - public static native String calcTransactionDataStructHash(int cryptoType, TransactionData transactionData) - throws JniException; + /** + * @param cryptoType crypto type + * @param transactionData Transaction Data struct + * @return Hash hex string + */ + public static native String calcTransactionDataStructHash( + int cryptoType, TransactionData transactionData) throws JniException; - /** - * @param transactionData - * @param signature - * @param transactionDataHash - * @param attribute - * @param extraData - * @return signedTransaction string - */ - public static native String createEncodedTransaction( - TransactionData transactionData, - String signature, - String transactionDataHash, - int attribute, - String extraData) - throws JniException; + /** + * @param transactionData + * @param signature + * @param transactionDataHash + * @param attribute + * @param extraData + * @return signedTransaction string + */ + public static native String createEncodedTransaction( + TransactionData transactionData, + String signature, + String transactionDataHash, + int attribute, + String extraData) + throws JniException; - /** - * @param transaction Transaction struct - * @return Hexed Transaction - */ - public static native String encodeTransactionStruct(Transaction transaction) throws JniException; + /** + * @param transaction Transaction struct + * @return Hexed Transaction + */ + public static native String encodeTransactionStruct(Transaction transaction) throws JniException; - /** - * @param transaction Transaction struct - * @return Json Transaction - */ - public static native String encodeTransactionStructToJson(Transaction transaction) throws JniException; + /** + * @param transaction Transaction struct + * @return Json Transaction + */ + public static native String encodeTransactionStructToJson(Transaction transaction) + throws JniException; - /** - * @param transactionHex transaction hex string - * @return Tx - */ - public static native Transaction decodeTransactionStruct(String transactionHex) throws JniException; + /** + * @param transactionHex transaction hex string + * @return Tx + */ + public static native Transaction decodeTransactionStruct(String transactionHex) + throws JniException; } diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion.java new file mode 100644 index 000000000..df0543acf --- /dev/null +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion.java @@ -0,0 +1,16 @@ +package org.fisco.bcos.sdk.jni.utilities.tx; + +public enum TransactionVersion { + V0(0), + V1(1); + + private int value; + + TransactionVersion(int value) { + this.value = value; + } + + public int getValue() { + return value; + } +} From 6734721d7471f79398f64462f45e26bb929b6607 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Fri, 1 Dec 2023 14:29:56 +0800 Subject: [PATCH 14/29] (build): try to strip dynamic lib. (#193) --- .github/workflows/workflow.yml | 8 + .../tx/TransactionBuilderV2JniObj.java | 432 +++++++++--------- .../bcos/sdk/jni/test/tx/TestTxStruct.java | 379 ++++++++------- 3 files changed, 432 insertions(+), 387 deletions(-) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index d18a0c70c..bef94f7ba 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -56,6 +56,10 @@ jobs: run: cd build && CTEST_OUTPUT_ON_FAILURE=TRUE make test - name: print link.txt run: cat build/CMakeFiles/bcos-c-sdk*.dir/link.txt + - name: strip libbcos-c-sdk.dylib + run: strip build/libbcos-c-sdk.dylib || strip -x build/libbcos-c-sdk.dylib + - name: strip libbcos-sdk-jni.dylib + run: strip bindings/java/jni/src/main/resources/META-INF/native/libbcos-sdk-jni.dylib || strip -x bindings/java/jni/src/main/resources/META-INF/native/libbcos-sdk-jni.dylib - uses: actions/upload-artifact@v2 with: name: libbcos-c-sdk.dylib.zip @@ -171,6 +175,10 @@ jobs: run: cd build && CTEST_OUTPUT_ON_FAILURE=TRUE make test - name: print link.txt run: cat build/CMakeFiles/bcos-c-sdk*.dir/link.txt + - name: strip libbcos-c-sdk.so + run: strip build/libbcos-c-sdk.so || strip -x build/libbcos-c-sdk.so + - name: strip libbcos-sdk-jni.so + run: strip bindings/java/jni/src/main/resources/META-INF/native/libbcos-sdk-jni.so || strip -x bindings/java/jni/src/main/resources/META-INF/native/libbcos-sdk-jni.so - uses: actions/upload-artifact@v2 with: name: libbcos-c-sdk.so.zip diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV2JniObj.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV2JniObj.java index cee0c9f91..ad5e49718 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV2JniObj.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV2JniObj.java @@ -5,227 +5,227 @@ public class TransactionBuilderV2JniObj extends TransactionBuilderJniObj { - static { - JniLibLoader.loadJniLibrary(); - } + static { + JniLibLoader.loadJniLibrary(); + } - /** - * create transaction data with version 1 by default - * - * @param groupID group id - * @param chainID chain id - * @param to contract address, if it is a contract creation transaction, it can be empty - * @param input encoded contract method and params - * @param abi contract abi, only create contract need - * @param blockLimit block limit - * @param value transfer value - * @param gasPrice gas price - * @param gasLimit gas limit - * @return void* transaction data pointer, you should release it after use, check - * `destroyTransactionData` - * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later - */ - public static native long createTransactionData( - String groupID, - String chainID, - String to, - String input, - String abi, - long blockLimit, - String value, - String gasPrice, - long gasLimit) - throws JniException; + /** + * create transaction data with version 1 by default + * + * @param groupID group id + * @param chainID chain id + * @param to contract address, if it is a contract creation transaction, it can be empty + * @param input encoded contract method and params + * @param abi contract abi, only create contract need + * @param blockLimit block limit + * @param value transfer value + * @param gasPrice gas price + * @param gasLimit gas limit + * @return void* transaction data pointer, you should release it after use, check + * `destroyTransactionData` + * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later + */ + public static native long createTransactionData( + String groupID, + String chainID, + String to, + String input, + String abi, + long blockLimit, + String value, + String gasPrice, + long gasLimit) + throws JniException; - /** - * create eip1559 transaction data with version 1 by default - * - * @param groupID group id - * @param chainID chain id - * @param to contract address, if it is a contract creation transaction, it can be empty - * @param input encoded contract method and params - * @param abi contract abi, only create contract need - * @param blockLimit block limit - * @param value transfer value - * @param gasLimit gas limit - * @param maxFeePerGas max fee per gas - * @param maxPriorityFeePerGas max priority fee per gas - * @return void* transaction data pointer, you should release it after use, check - * `destroyTransactionData` - * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later - */ - public static native long createEIP1559TransactionData( - String groupID, - String chainID, - String to, - String input, - String abi, - long blockLimit, - String value, - long gasLimit, - String maxFeePerGas, - String maxPriorityFeePerGas) - throws JniException; + /** + * create eip1559 transaction data with version 1 by default + * + * @param groupID group id + * @param chainID chain id + * @param to contract address, if it is a contract creation transaction, it can be empty + * @param input encoded contract method and params + * @param abi contract abi, only create contract need + * @param blockLimit block limit + * @param value transfer value + * @param gasLimit gas limit + * @param maxFeePerGas max fee per gas + * @param maxPriorityFeePerGas max priority fee per gas + * @return void* transaction data pointer, you should release it after use, check + * `destroyTransactionData` + * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later + */ + public static native long createEIP1559TransactionData( + String groupID, + String chainID, + String to, + String input, + String abi, + long blockLimit, + String value, + long gasLimit, + String maxFeePerGas, + String maxPriorityFeePerGas) + throws JniException; - /** - * create transaction data with full fields - * - * @param cryptoType 0: keccak256, 1: sm3 - * @param version tx version, if version==1, then enable - * (value,gasPrice,gasLimit,maxFeePerGas,maxPriorityFeePerGas) fields - * @param groupID group id - * @param chainID chain id - * @param to contract address, if it is a contract creation transaction, it can be empty - * @param nonce nonce, random number to avoid duplicate transactions - * @param input encoded contract method and params - * @param abi contract abi, only create contract need - * @param blockLimit block limit - * @param value transfer value - * @param gasPrice gas price - * @param gasLimit gas limit - * @param maxFeePerGas max fee per gas - * @param maxPriorityFeePerGas max priority fee per gas - * @return const char* transaction data hash hex string - * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later - */ - public static native String calcTransactionDataHashWithFullFields( - int cryptoType, - TransactionVersion version, - String groupID, - String chainID, - String to, - String nonce, - String input, - String abi, - long blockLimit, - String value, - String gasPrice, - long gasLimit, - String maxFeePerGas, - String maxPriorityFeePerGas) - throws JniException; + /** + * create transaction data with full fields + * + * @param cryptoType 0: keccak256, 1: sm3 + * @param version tx version, if version==1, then enable + * (value,gasPrice,gasLimit,maxFeePerGas,maxPriorityFeePerGas) fields + * @param groupID group id + * @param chainID chain id + * @param to contract address, if it is a contract creation transaction, it can be empty + * @param nonce nonce, random number to avoid duplicate transactions + * @param input encoded contract method and params + * @param abi contract abi, only create contract need + * @param blockLimit block limit + * @param value transfer value + * @param gasPrice gas price + * @param gasLimit gas limit + * @param maxFeePerGas max fee per gas + * @param maxPriorityFeePerGas max priority fee per gas + * @return const char* transaction data hash hex string + * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later + */ + public static native String calcTransactionDataHashWithFullFields( + int cryptoType, + TransactionVersion version, + String groupID, + String chainID, + String to, + String nonce, + String input, + String abi, + long blockLimit, + String value, + String gasPrice, + long gasLimit, + String maxFeePerGas, + String maxPriorityFeePerGas) + throws JniException; - /** - * create transaction data with json string - * - * @param cryptoType 0: keccak256, 1: sm3 - * @param json transaction data json string - * @return const char* transaction data hash hex string - * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later - * @throws JniException if lack of some required fields, or some fields are invalid - */ - public static native String calcTransactionDataHashWithJson(int cryptoType, String json) - throws JniException; + /** + * create transaction data with json string + * + * @param cryptoType 0: keccak256, 1: sm3 + * @param json transaction data json string + * @return const char* transaction data hash hex string + * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later + * @throws JniException if lack of some required fields, or some fields are invalid + */ + public static native String calcTransactionDataHashWithJson(int cryptoType, String json) + throws JniException; - /** - * create encoded transaction data with external signature - * - * @param signature signature hex string, if ECDSA, it is r||s||v, if SM2, it is r||s||pk - * @param txDataHash transactionData hash hex string - * @param version tx version, only support 0 and 1 now, if version==1, then enable - * (value,gasPrice,gasLimit,maxFeePerGas,maxPriorityFeePerGas) fields - * @param groupID group id - * @param chainID chain id - * @param to contract address, if it is a contract creation transaction, it can be empty - * @param nonce nonce, random number to avoid duplicate transactions - * @param input encoded contract method and params - * @param abi contract abi, only create contract need - * @param blockLimit block limit - * @param value transfer value - * @param gasPrice gas price - * @param gasLimit gas limit - * @param maxFeePerGas max fee per gas - * @param maxPriorityFeePerGas max priority fee per gas - * @param attribute transaction attribute - * @param extraData extra data in transaction - * @return const char* encoded transaction hex string - * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later - */ - public static native String createSignedTransactionWithSignature( - String signature, - String txDataHash, - TransactionVersion version, - String groupID, - String chainID, - String to, - String nonce, - String input, - String abi, - long blockLimit, - String value, - String gasPrice, - long gasLimit, - String maxFeePerGas, - String maxPriorityFeePerGas, - int attribute, - String extraData) - throws JniException; + /** + * create encoded transaction data with external signature + * + * @param signature signature hex string, if ECDSA, it is r||s||v, if SM2, it is r||s||pk + * @param txDataHash transactionData hash hex string + * @param version tx version, only support 0 and 1 now, if version==1, then enable + * (value,gasPrice,gasLimit,maxFeePerGas,maxPriorityFeePerGas) fields + * @param groupID group id + * @param chainID chain id + * @param to contract address, if it is a contract creation transaction, it can be empty + * @param nonce nonce, random number to avoid duplicate transactions + * @param input encoded contract method and params + * @param abi contract abi, only create contract need + * @param blockLimit block limit + * @param value transfer value + * @param gasPrice gas price + * @param gasLimit gas limit + * @param maxFeePerGas max fee per gas + * @param maxPriorityFeePerGas max priority fee per gas + * @param attribute transaction attribute + * @param extraData extra data in transaction + * @return const char* encoded transaction hex string + * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later + */ + public static native String createSignedTransactionWithSignature( + String signature, + String txDataHash, + TransactionVersion version, + String groupID, + String chainID, + String to, + String nonce, + String input, + String abi, + long blockLimit, + String value, + String gasPrice, + long gasLimit, + String maxFeePerGas, + String maxPriorityFeePerGas, + int attribute, + String extraData) + throws JniException; - /** - * create transaction with full fields, with version 1 by default - * - * @param keyPair key pair pointer - * @param groupID group id - * @param chainID chain id - * @param to contract address, if it is a contract creation transaction, it can be empty - * @param input encoded contract method and params - * @param abi contract abi, only create contract need - * @param blockLimit block limit - * @param value transfer value - * @param gasPrice gas price - * @param gasLimit gas limit - * @param attribute transaction attribute - * @param extraData extra data in transaction - * @return TxPair - * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later - */ - public static native TxPair createSignedTransactionWithFullFields( - long keyPair, - String groupID, - String chainID, - String to, - String input, - String abi, - long blockLimit, - String value, - String gasPrice, - long gasLimit, - int attribute, - String extraData) - throws JniException; + /** + * create transaction with full fields, with version 1 by default + * + * @param keyPair key pair pointer + * @param groupID group id + * @param chainID chain id + * @param to contract address, if it is a contract creation transaction, it can be empty + * @param input encoded contract method and params + * @param abi contract abi, only create contract need + * @param blockLimit block limit + * @param value transfer value + * @param gasPrice gas price + * @param gasLimit gas limit + * @param attribute transaction attribute + * @param extraData extra data in transaction + * @return TxPair + * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later + */ + public static native TxPair createSignedTransactionWithFullFields( + long keyPair, + String groupID, + String chainID, + String to, + String input, + String abi, + long blockLimit, + String value, + String gasPrice, + long gasLimit, + int attribute, + String extraData) + throws JniException; - /** - * create eip1559 transaction with full fields, with version 1 by default - * - * @param keyPair key pair pointer - * @param groupID group id - * @param chainID chain id - * @param to contract address, if it is a contract creation transaction, it can be empty - * @param input encoded contract method and params - * @param abi contract abi, only create contract need - * @param blockLimit block limit - * @param value transfer value - * @param gasLimit gas limit - * @param maxFeePerGas max fee per gas - * @param maxPriorityFeePerGas max priority fee per gas - * @param attribute transaction attribute - * @param extraData extra data in transaction - * @return TxPair - * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later - */ - public static native TxPair createSignedEIP1559TransactionWithFullFields( - long keyPair, - String groupID, - String chainID, - String to, - String input, - String abi, - long blockLimit, - String value, - String maxFeePerGas, - String maxPriorityFeePerGas, - long gasLimit, - int attribute, - String extraData) - throws JniException; + /** + * create eip1559 transaction with full fields, with version 1 by default + * + * @param keyPair key pair pointer + * @param groupID group id + * @param chainID chain id + * @param to contract address, if it is a contract creation transaction, it can be empty + * @param input encoded contract method and params + * @param abi contract abi, only create contract need + * @param blockLimit block limit + * @param value transfer value + * @param gasLimit gas limit + * @param maxFeePerGas max fee per gas + * @param maxPriorityFeePerGas max priority fee per gas + * @param attribute transaction attribute + * @param extraData extra data in transaction + * @return TxPair + * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later + */ + public static native TxPair createSignedEIP1559TransactionWithFullFields( + long keyPair, + String groupID, + String chainID, + String to, + String input, + String abi, + long blockLimit, + String value, + String maxFeePerGas, + String maxPriorityFeePerGas, + long gasLimit, + int attribute, + String extraData) + throws JniException; } diff --git a/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java b/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java index fef23329c..1a49588c5 100644 --- a/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java +++ b/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java @@ -1,5 +1,7 @@ package org.fisco.bcos.sdk.jni.test.tx; +import java.security.SecureRandom; +import java.util.Arrays; import org.fisco.bcos.sdk.jni.BcosSDKJniObj; import org.fisco.bcos.sdk.jni.common.JniConfig; import org.fisco.bcos.sdk.jni.common.JniException; @@ -11,31 +13,30 @@ import org.fisco.bcos.sdk.jni.utilities.tx.*; import org.junit.Assert; -import java.security.SecureRandom; -import java.util.Arrays; - public class TestTxStruct { - // ------------------------------------------------------------------------------ - // ------------------------------------------------------------------------------ - - // HelloWorld Source Code: - - // HelloWorld Source Code: - /** - * pragma solidity>=0.4.24 <0.6.11; - * - *

contract HelloWorld { string name; - * - *

constructor() public { name = "Hello, World!"; } - * - *

function get() public view returns (string memory) { return name; } - * - *

function set(string memory n) public { name = n; } } - */ - private static final String hwBIN = "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610235565b5050565b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561022b5780601f106102005761010080835404028352916020019161022b565b820191906000526020600020905b81548152906001019060200180831161020e57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea2646970667358221220b5943f43c48cc93c6d71cdcf27aee5072566c88755ce9186e32ce83b24e8dc6c64736f6c634300060a0033"; - - private static final String hwSmBIN = "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063299f7f9d1461003b5780633590b49f146100be575b600080fd5b610043610179565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610083578082015181840152602081019050610068565b50505050905090810190601f1680156100b05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610177600480360360208110156100d457600080fd5b81019080803590602001906401000000008111156100f157600080fd5b82018360208201111561010357600080fd5b8035906020019184600183028401116401000000008311171561012557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061021b565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102115780601f106101e657610100808354040283529160200191610211565b820191906000526020600020905b8154815290600101906020018083116101f457829003601f168201915b5050505050905090565b8060009080519060200190610231929190610235565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea26469706673582212209871cb2bcf390d53645807cbaedfe052d739ef9cff9d84787f74c4f379e1854664736f6c634300060a0033"; + // ------------------------------------------------------------------------------ + // ------------------------------------------------------------------------------ + + // HelloWorld Source Code: + + // HelloWorld Source Code: + /** + * pragma solidity>=0.4.24 <0.6.11; + * + *

contract HelloWorld { string name; + * + *

constructor() public { name = "Hello, World!"; } + * + *

function get() public view returns (string memory) { return name; } + * + *

function set(string memory n) public { name = n; } } + */ + private static final String hwBIN = + "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610235565b5050565b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561022b5780601f106102005761010080835404028352916020019161022b565b820191906000526020600020905b81548152906001019060200180831161020e57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea2646970667358221220b5943f43c48cc93c6d71cdcf27aee5072566c88755ce9186e32ce83b24e8dc6c64736f6c634300060a0033"; + + private static final String hwSmBIN = + "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063299f7f9d1461003b5780633590b49f146100be575b600080fd5b610043610179565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610083578082015181840152602081019050610068565b50505050905090810190601f1680156100b05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610177600480360360208110156100d457600080fd5b81019080803590602001906401000000008111156100f157600080fd5b82018360208201111561010357600080fd5b8035906020019184600183028401116401000000008311171561012557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061021b565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102115780601f106101e657610100808354040283529160200191610211565b820191906000526020600020905b8154815290600101906020018083116101f457829003601f168201915b5050505050905090565b8060009080519060200190610231929190610235565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea26469706673582212209871cb2bcf390d53645807cbaedfe052d739ef9cff9d84787f74c4f379e1854664736f6c634300060a0033"; /* { @@ -49,164 +50,200 @@ public class TestTxStruct { } */ - public static byte[] fromHex(String hexString) { - if (hexString.startsWith("0x")) { - hexString = hexString.substring(2); - } - - if (hexString.length() % 2 != 0) { - throw new IllegalArgumentException("Invalid hex string: " + hexString); - } + public static byte[] fromHex(String hexString) { + if (hexString.startsWith("0x")) { + hexString = hexString.substring(2); + } - int length = hexString.length() / 2; - byte[] bytes = new byte[length]; + if (hexString.length() % 2 != 0) { + throw new IllegalArgumentException("Invalid hex string: " + hexString); + } - for (int i = 0; i < length; i++) { - String twoChars = hexString.substring(i * 2, i * 2 + 2); - bytes[i] = (byte) Integer.parseInt(twoChars, 16); - } + int length = hexString.length() / 2; + byte[] bytes = new byte[length]; - return bytes; + for (int i = 0; i < length; i++) { + String twoChars = hexString.substring(i * 2, i * 2 + 2); + bytes[i] = (byte) Integer.parseInt(twoChars, 16); } - public static String generateNonce() { - byte[] nonceBytes = new byte[16]; - SecureRandom secureRandom = new SecureRandom(); - secureRandom.nextBytes(nonceBytes); - StringBuilder hex = new StringBuilder(); - for (byte b : nonceBytes) { - hex.append(String.format("%02x", b)); - } - return hex.toString(); - } + return bytes; + } - public static String getBinary(boolean isSM) { - return isSM ? hwSmBIN : hwBIN; + public static String generateNonce() { + byte[] nonceBytes = new byte[16]; + SecureRandom secureRandom = new SecureRandom(); + secureRandom.nextBytes(nonceBytes); + StringBuilder hex = new StringBuilder(); + for (byte b : nonceBytes) { + hex.append(String.format("%02x", b)); } + return hex.toString(); + } + + public static String getBinary(boolean isSM) { + return isSM ? hwSmBIN : hwBIN; + } + + public static void Usage() { + System.out.println("Desc: test transaction struct [HelloWorld set]"); + System.out.println( + "Usage: java -cp 'conf/:lib/*:apps/*' org.fisco.bcos.sdk.jni.test.tx.TestTxStruct"); + System.exit(0); + } - public static void Usage() { - System.out.println("Desc: test transaction struct [HelloWorld set]"); - System.out.println("Usage: java -cp 'conf/:lib/*:apps/*' org.fisco.bcos.sdk.jni.test.tx.TestTxStruct"); - System.exit(0); + public static void main(String[] args) throws JniException, InterruptedException { + if (args.length > 1) { + Usage(); } - public static void main(String[] args) throws JniException, InterruptedException { - if (args.length > 1) { - Usage(); - } - - String endpoint = "127.0.0.1:22200"; - String group = "group0"; - String node = ""; - JniConfig jniConfig = Utility.newJniConfig(Arrays.asList(endpoint)); - jniConfig.setDisableSsl(true); - BcosSDKJniObj bcosSDKJni = BcosSDKJniObj.build(jniConfig); - RpcJniObj rpcJniObj = RpcJniObj.build(bcosSDKJni.getNativePointer()); - System.out.println("build Rpc"); - rpcJniObj.start(); - - boolean smCrypto = true; - - long keyPair = KeyPairJniObj.createJniKeyPair(smCrypto ? 1 : 0); - String jniKeyPairAddress = KeyPairJniObj.getJniKeyPairAddress(keyPair); - - long blockLimit = 1111; - String groupID = "group0"; - String chainID = "chain0"; - String data = getBinary(smCrypto); - System.out.printf(" [test Tx Struct] new account, address: %s\n", jniKeyPairAddress); - - // construct TransactionData - TransactionData transactionDataStruct = new TransactionData(); - transactionDataStruct.setGroupId(groupID); - transactionDataStruct.setChainId(chainID); - transactionDataStruct.setTo(""); - transactionDataStruct.setAbi(""); - transactionDataStruct.setVersion(0); - transactionDataStruct.setNonce(generateNonce()); - transactionDataStruct.setBlockLimit(blockLimit); - // input - TransactionBytes inputBytes = new TransactionBytes(); - byte[] bytesInput = fromHex(data); - inputBytes.setBuffer(bytesInput); - inputBytes.setLength(bytesInput.length); - transactionDataStruct.setInput(inputBytes); - - // encode TxData to hex tx data - String txDataHex = TransactionStructBuilderJniObj.encodeTransactionDataStruct(transactionDataStruct); - // decode hex tx data to TxData - TransactionData decodeTransactionDataStructHex = TransactionStructBuilderJniObj.decodeTransactionDataStruct(txDataHex); - // assert - Assert.assertEquals(transactionDataStruct.getChainId(), decodeTransactionDataStructHex.getChainId()); - Assert.assertEquals(transactionDataStruct.getGroupId(), decodeTransactionDataStructHex.getGroupId()); - Assert.assertEquals(transactionDataStruct.getAbi(), decodeTransactionDataStructHex.getAbi()); - Assert.assertEquals(transactionDataStruct.getBlockLimit(), decodeTransactionDataStructHex.getBlockLimit()); - - // encode TxData to json tx data - String txDataJson = TransactionStructBuilderJniObj.encodeTransactionDataStructToJson(transactionDataStruct); - System.out.printf(" [test Tx Struct] txDataJson: %s\n", txDataJson); - - // calc tx data hash - String txDataHash = TransactionStructBuilderJniObj.calcTransactionDataStructHash(smCrypto ? 1 : 0, decodeTransactionDataStructHex); - System.out.printf(" [test Tx Struct] txDataHash: %s\n", txDataHash); - // signature tx data hash - String signature = TransactionBuilderJniObj.signTransactionDataHash(keyPair, txDataHash); - System.out.printf(" [test Tx Struct] signature: %s\n", signature); - - // construct tx - Transaction transactionStruct = new Transaction(); - TransactionBytes dataHashBytes = new TransactionBytes(); - dataHashBytes.setBuffer(txDataHash.getBytes()); - dataHashBytes.setLength(txDataHash.getBytes().length); - TransactionBytes signatureBytes = new TransactionBytes(); - signatureBytes.setBuffer(signature.getBytes()); - signatureBytes.setLength(signature.getBytes().length); - transactionStruct.setTransactionData(decodeTransactionDataStructHex); - transactionStruct.setDataHash(dataHashBytes); - transactionStruct.setSignature(signatureBytes); - transactionStruct.setSender(null); - transactionStruct.setImportTime(0); - transactionStruct.setAttribute(0); - transactionStruct.setExtraData(""); - // assert - Assert.assertEquals(transactionStruct.getTransactionData().getBlockLimit(), decodeTransactionDataStructHex.getBlockLimit()); - Assert.assertEquals(transactionStruct.getTransactionData().getGroupId(), decodeTransactionDataStructHex.getGroupId()); - Assert.assertEquals(transactionStruct.getTransactionData().getChainId(), decodeTransactionDataStructHex.getChainId()); - Assert.assertEquals(transactionStruct.getTransactionData().getAbi(), decodeTransactionDataStructHex.getAbi()); - Assert.assertArrayEquals(transactionStruct.getDataHash().getBuffer(), txDataHash.getBytes()); - Assert.assertArrayEquals(transactionStruct.getSignature().getBuffer(), signature.getBytes()); - - // encode Tx to hex tx - String txHex = TransactionStructBuilderJniObj.encodeTransactionStruct(transactionStruct); - // decode hex tx to Tx - Transaction decodeTransactionStructHex = TransactionStructBuilderJniObj.decodeTransactionStruct(txHex); - // assert - Assert.assertEquals(transactionStruct.getTransactionData().getBlockLimit(), decodeTransactionStructHex.getTransactionData().getBlockLimit()); - Assert.assertEquals(transactionStruct.getTransactionData().getGroupId(), decodeTransactionStructHex.getTransactionData().getGroupId()); - Assert.assertEquals(transactionStruct.getTransactionData().getChainId(), decodeTransactionStructHex.getTransactionData().getChainId()); - Assert.assertEquals(transactionStruct.getTransactionData().getAbi(), decodeTransactionStructHex.getTransactionData().getAbi()); - Assert.assertArrayEquals(transactionStruct.getDataHash().getBuffer(), decodeTransactionStructHex.getDataHash().getBuffer()); - Assert.assertArrayEquals(transactionStruct.getSignature().getBuffer(), decodeTransactionStructHex.getSignature().getBuffer()); - - // encode Tx to json tx - String txJson = TransactionStructBuilderJniObj.encodeTransactionStructToJson(transactionStruct); - System.out.printf(" [test Tx Struct] txJson: %s\n", txJson); - // create tx string - String txString = TransactionStructBuilderJniObj.createEncodedTransaction(decodeTransactionDataStructHex, signature, txDataHash, 0, ""); -// System.out.printf(" [test Tx Struct] txString: %s\n", txString); - - // rpc send tx - rpcJniObj.sendTransaction(group, node, txString, false, new RpcCallback() { - @Override - public void onResponse(Response response) { - System.out.println("response error code: ==>>> " + response.getErrorCode()); - String dataStr = new String(response.getData()); - System.out.println("response data: ==>>> " + dataStr); - } + String endpoint = "127.0.0.1:22200"; + String group = "group0"; + String node = ""; + JniConfig jniConfig = Utility.newJniConfig(Arrays.asList(endpoint)); + jniConfig.setDisableSsl(true); + BcosSDKJniObj bcosSDKJni = BcosSDKJniObj.build(jniConfig); + RpcJniObj rpcJniObj = RpcJniObj.build(bcosSDKJni.getNativePointer()); + System.out.println("build Rpc"); + rpcJniObj.start(); + + boolean smCrypto = true; + + long keyPair = KeyPairJniObj.createJniKeyPair(smCrypto ? 1 : 0); + String jniKeyPairAddress = KeyPairJniObj.getJniKeyPairAddress(keyPair); + + long blockLimit = 1111; + String groupID = "group0"; + String chainID = "chain0"; + String data = getBinary(smCrypto); + System.out.printf(" [test Tx Struct] new account, address: %s\n", jniKeyPairAddress); + + // construct TransactionData + TransactionData transactionDataStruct = new TransactionData(); + transactionDataStruct.setGroupId(groupID); + transactionDataStruct.setChainId(chainID); + transactionDataStruct.setTo(""); + transactionDataStruct.setAbi(""); + transactionDataStruct.setVersion(0); + transactionDataStruct.setNonce(generateNonce()); + transactionDataStruct.setBlockLimit(blockLimit); + // input + TransactionBytes inputBytes = new TransactionBytes(); + byte[] bytesInput = fromHex(data); + inputBytes.setBuffer(bytesInput); + inputBytes.setLength(bytesInput.length); + transactionDataStruct.setInput(inputBytes); + + // encode TxData to hex tx data + String txDataHex = + TransactionStructBuilderJniObj.encodeTransactionDataStruct(transactionDataStruct); + // decode hex tx data to TxData + TransactionData decodeTransactionDataStructHex = + TransactionStructBuilderJniObj.decodeTransactionDataStruct(txDataHex); + // assert + Assert.assertEquals( + transactionDataStruct.getChainId(), decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals( + transactionDataStruct.getGroupId(), decodeTransactionDataStructHex.getGroupId()); + Assert.assertEquals(transactionDataStruct.getAbi(), decodeTransactionDataStructHex.getAbi()); + Assert.assertEquals( + transactionDataStruct.getBlockLimit(), decodeTransactionDataStructHex.getBlockLimit()); + + // encode TxData to json tx data + String txDataJson = + TransactionStructBuilderJniObj.encodeTransactionDataStructToJson(transactionDataStruct); + System.out.printf(" [test Tx Struct] txDataJson: %s\n", txDataJson); + + // calc tx data hash + String txDataHash = + TransactionStructBuilderJniObj.calcTransactionDataStructHash( + smCrypto ? 1 : 0, decodeTransactionDataStructHex); + System.out.printf(" [test Tx Struct] txDataHash: %s\n", txDataHash); + // signature tx data hash + String signature = TransactionBuilderJniObj.signTransactionDataHash(keyPair, txDataHash); + System.out.printf(" [test Tx Struct] signature: %s\n", signature); + + // construct tx + Transaction transactionStruct = new Transaction(); + TransactionBytes dataHashBytes = new TransactionBytes(); + dataHashBytes.setBuffer(txDataHash.getBytes()); + dataHashBytes.setLength(txDataHash.getBytes().length); + TransactionBytes signatureBytes = new TransactionBytes(); + signatureBytes.setBuffer(signature.getBytes()); + signatureBytes.setLength(signature.getBytes().length); + transactionStruct.setTransactionData(decodeTransactionDataStructHex); + transactionStruct.setDataHash(dataHashBytes); + transactionStruct.setSignature(signatureBytes); + transactionStruct.setSender(null); + transactionStruct.setImportTime(0); + transactionStruct.setAttribute(0); + transactionStruct.setExtraData(""); + // assert + Assert.assertEquals( + transactionStruct.getTransactionData().getBlockLimit(), + decodeTransactionDataStructHex.getBlockLimit()); + Assert.assertEquals( + transactionStruct.getTransactionData().getGroupId(), + decodeTransactionDataStructHex.getGroupId()); + Assert.assertEquals( + transactionStruct.getTransactionData().getChainId(), + decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals( + transactionStruct.getTransactionData().getAbi(), decodeTransactionDataStructHex.getAbi()); + Assert.assertArrayEquals(transactionStruct.getDataHash().getBuffer(), txDataHash.getBytes()); + Assert.assertArrayEquals(transactionStruct.getSignature().getBuffer(), signature.getBytes()); + + // encode Tx to hex tx + String txHex = TransactionStructBuilderJniObj.encodeTransactionStruct(transactionStruct); + // decode hex tx to Tx + Transaction decodeTransactionStructHex = + TransactionStructBuilderJniObj.decodeTransactionStruct(txHex); + // assert + Assert.assertEquals( + transactionStruct.getTransactionData().getBlockLimit(), + decodeTransactionStructHex.getTransactionData().getBlockLimit()); + Assert.assertEquals( + transactionStruct.getTransactionData().getGroupId(), + decodeTransactionStructHex.getTransactionData().getGroupId()); + Assert.assertEquals( + transactionStruct.getTransactionData().getChainId(), + decodeTransactionStructHex.getTransactionData().getChainId()); + Assert.assertEquals( + transactionStruct.getTransactionData().getAbi(), + decodeTransactionStructHex.getTransactionData().getAbi()); + Assert.assertArrayEquals( + transactionStruct.getDataHash().getBuffer(), + decodeTransactionStructHex.getDataHash().getBuffer()); + Assert.assertArrayEquals( + transactionStruct.getSignature().getBuffer(), + decodeTransactionStructHex.getSignature().getBuffer()); + + // encode Tx to json tx + String txJson = TransactionStructBuilderJniObj.encodeTransactionStructToJson(transactionStruct); + System.out.printf(" [test Tx Struct] txJson: %s\n", txJson); + // create tx string + String txString = + TransactionStructBuilderJniObj.createEncodedTransaction( + decodeTransactionDataStructHex, signature, txDataHash, 0, ""); + // System.out.printf(" [test Tx Struct] txString: %s\n", txString); + + // rpc send tx + rpcJniObj.sendTransaction( + group, + node, + txString, + false, + new RpcCallback() { + @Override + public void onResponse(Response response) { + System.out.println("response error code: ==>>> " + response.getErrorCode()); + String dataStr = new String(response.getData()); + System.out.println("response data: ==>>> " + dataStr); + } }); - Thread.sleep(2000); - rpcJniObj.stop(); - System.out.printf(" [test Tx Struct] finish !! \n"); - } + Thread.sleep(2000); + rpcJniObj.stop(); + System.out.printf(" [test Tx Struct] finish !! \n"); + } } From a6c64cf7839cf0044afb2c4be9bd7ab35652d705 Mon Sep 17 00:00:00 2001 From: lucasli <410567249@qq.com> Date: Wed, 13 Dec 2023 16:13:10 +0800 Subject: [PATCH 15/29] add(tx struct): add new api for tx struct to adapt to balance (#189) --- .github/workflows/workflow.yml | 8 +- bcos-c-sdk/bcos_sdk_c_common.h | 28 + bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp | 852 ++++++++++++++++++ bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h | 46 + ...ties_tx_TransactionStructBuilderJniObj.cpp | 592 +++++++----- .../bcos/sdk/jni/test/tx/TestTxStruct.java | 4 +- sample/tx/CMakeLists.txt | 3 + sample/tx/tx_struct_v2_test.c | 479 ++++++++++ 8 files changed, 1766 insertions(+), 246 deletions(-) create mode 100644 sample/tx/tx_struct_v2_test.c diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index bef94f7ba..5e8652c64 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -91,8 +91,8 @@ jobs: vcpkg-gcc-v1-notest-${{ runner.temp }}- - name: install Ubuntu dependencies run: sudo apt install -y git curl openssl build-essential cmake ccache lcov - - name: fetch vcpkg - run: cd /usr/local/share/vcpkg/ && git fetch && git reset --hard && git pull && cd - + # - name: fetch vcpkg + # run: cd /usr/local/share/vcpkg/ && git fetch --all && git checkout master && git pull && cd - - name: configure run: | export CC='gcc-10'; export CXX='g++-10' @@ -156,8 +156,8 @@ jobs: ln -s /usr/local/cmake/bin/cmake /usr/bin/cmake cmake --version git --version - - name: fetch vcpkg - run: cd /usr/local/share/vcpkg/ && git fetch && git reset --hard && git pull && cd - + # - name: fetch vcpkg + # run: cd /usr/local/share/vcpkg/ && git fetch --all && git checkout master && git pull && cd - - name: configure run: | export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH" diff --git a/bcos-c-sdk/bcos_sdk_c_common.h b/bcos-c-sdk/bcos_sdk_c_common.h index b5f7811d6..f381f2182 100644 --- a/bcos-c-sdk/bcos_sdk_c_common.h +++ b/bcos-c-sdk/bcos_sdk_c_common.h @@ -209,6 +209,23 @@ struct bcos_sdk_c_transaction_data struct bcos_sdk_c_bytes* input; }; +struct bcos_sdk_c_transaction_data_v2 +{ + int32_t version; + int64_t block_limit; + char* chain_id; + char* group_id; + char* nonce; + char* to; + char* abi; + struct bcos_sdk_c_bytes* input; + char* value; + char* gas_price; + int64_t gas_limit; + char* max_fee_per_gas; + char* max_priority_fee_per_gas; +}; + /** * @brief: transaction * @@ -224,6 +241,17 @@ struct bcos_sdk_c_transaction char* extra_data; }; +struct bcos_sdk_c_transaction_v2 +{ + struct bcos_sdk_c_transaction_data_v2* transaction_data; + struct bcos_sdk_c_bytes* data_hash; + struct bcos_sdk_c_bytes* signature; + struct bcos_sdk_c_bytes* sender; + int64_t import_time; + int32_t attribute; + char* extra_data; +}; + //--------------- transaction end--------------------------------- #ifdef __cplusplus diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp index 286bacf28..fea933613 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp @@ -126,6 +126,45 @@ struct bcos_sdk_c_transaction_data* transaction_data_copy( return NULL; } +struct bcos_sdk_c_transaction_data_v2* transaction_data_copy_v2( + const bcos_sdk_c_transaction_data_v2* tx_data_src) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(tx_data_src, NULL); + + try + { + struct bcos_sdk_c_transaction_data_v2* transaction_data_struct_v2 = + (struct bcos_sdk_c_transaction_data_v2*)malloc( + sizeof(struct bcos_sdk_c_transaction_data_v2)); + transaction_data_struct_v2->version = tx_data_src->version; + transaction_data_struct_v2->block_limit = tx_data_src->block_limit; + transaction_data_struct_v2->chain_id = my_strdup(tx_data_src->chain_id); + transaction_data_struct_v2->group_id = my_strdup(tx_data_src->group_id); + transaction_data_struct_v2->nonce = my_strdup(tx_data_src->nonce); + transaction_data_struct_v2->to = my_strdup(tx_data_src->to); + transaction_data_struct_v2->abi = my_strdup(tx_data_src->abi); + transaction_data_struct_v2->input = bytes_struct_copy(tx_data_src->input); + transaction_data_struct_v2->value = my_strdup(tx_data_src->value); + transaction_data_struct_v2->gas_price = my_strdup(tx_data_src->gas_price); + transaction_data_struct_v2->gas_limit = tx_data_src->gas_limit; + transaction_data_struct_v2->max_fee_per_gas = my_strdup(tx_data_src->max_fee_per_gas); + transaction_data_struct_v2->max_priority_fee_per_gas = + my_strdup(tx_data_src->max_priority_fee_per_gas); + + return transaction_data_struct_v2; + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("transaction_data_copy_v2") << LOG_DESC("exception") + << LOG_KV("tx_data_src", tx_data_src) << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + /** * @brief: convert tars transaction data to struct bcos_sdk_c_transaction_data * @@ -169,6 +208,54 @@ struct bcos_sdk_c_transaction_data* convert_tars_transaction_data_to_struct( return NULL; } +struct bcos_sdk_c_transaction_data_v2* convert_tars_transaction_data_to_struct_v2( + bcostars::TransactionDataUniquePtr tars_transaction_data) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(tars_transaction_data, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (tars_transaction_data->version == 1), "version of tars tx data V2 must be 1", NULL); + + try + { + struct bcos_sdk_c_transaction_data_v2* transaction_data_struct_v2 = + (struct bcos_sdk_c_transaction_data_v2*)malloc( + sizeof(struct bcos_sdk_c_transaction_data_v2)); + struct bcos_sdk_c_bytes* input_bytes = create_bytes_struct( + tars_transaction_data->input.size(), tars_transaction_data->input.data()); + + transaction_data_struct_v2->input = input_bytes; + transaction_data_struct_v2->version = tars_transaction_data->version; + transaction_data_struct_v2->block_limit = tars_transaction_data->blockLimit; + transaction_data_struct_v2->chain_id = my_strdup(tars_transaction_data->chainID.data()); + transaction_data_struct_v2->group_id = my_strdup(tars_transaction_data->groupID.data()); + transaction_data_struct_v2->nonce = my_strdup(tars_transaction_data->nonce.data()); + transaction_data_struct_v2->to = my_strdup(tars_transaction_data->to.data()); + transaction_data_struct_v2->abi = my_strdup(tars_transaction_data->abi.data()); + transaction_data_struct_v2->value = my_strdup(tars_transaction_data->value.data()); + transaction_data_struct_v2->gas_price = my_strdup(tars_transaction_data->gasPrice.data()); + transaction_data_struct_v2->gas_limit = tars_transaction_data->gasLimit; + transaction_data_struct_v2->max_fee_per_gas = + my_strdup(tars_transaction_data->maxFeePerGas.data()); + transaction_data_struct_v2->max_priority_fee_per_gas = + my_strdup(tars_transaction_data->maxPriorityFeePerGas.data()); + + return transaction_data_struct_v2; + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("convert_tars_transaction_data_to_struct_v2") + << LOG_DESC("exception") + << LOG_KV("transaction data", tars_transaction_data) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + + bcostars::TransactionDataUniquePtr convert_transaction_data_to_tars( struct bcos_sdk_c_transaction_data* transaction_data) { @@ -207,6 +294,53 @@ bcostars::TransactionDataUniquePtr convert_transaction_data_to_tars( return NULL; } +bcostars::TransactionDataUniquePtr convert_transaction_data_to_tars_v2( + struct bcos_sdk_c_transaction_data_v2* transaction_data) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data->group_id, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data->chain_id, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data->input, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data->value, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (transaction_data->version == 1), "version of tx data struct V2 must be 1", NULL); + + try + { + auto tars_transaction_data_v2 = std::make_unique(); + for (size_t i = 0; i < transaction_data->input->length; ++i) + { + tars_transaction_data_v2->input.push_back(transaction_data->input->buffer[i]); + } + tars_transaction_data_v2->version = (tars::Int32)transaction_data->version; + tars_transaction_data_v2->blockLimit = (tars::Int64)transaction_data->block_limit; + tars_transaction_data_v2->chainID = std::string(transaction_data->chain_id); + tars_transaction_data_v2->groupID = std::string(transaction_data->group_id); + tars_transaction_data_v2->nonce = std::string(transaction_data->nonce); + tars_transaction_data_v2->to = std::string(transaction_data->to); + tars_transaction_data_v2->abi = std::string(transaction_data->abi); + tars_transaction_data_v2->value = std::string(transaction_data->value); + tars_transaction_data_v2->gasPrice = std::string(transaction_data->gas_price); + tars_transaction_data_v2->gasLimit = (tars::Int64)transaction_data->gas_limit; + tars_transaction_data_v2->maxFeePerGas = std::string(transaction_data->max_fee_per_gas); + tars_transaction_data_v2->maxPriorityFeePerGas = + std::string(transaction_data->max_priority_fee_per_gas); + + return tars_transaction_data_v2; + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("convert_transaction_data_to_tars_v2") + << LOG_DESC("exception") << LOG_KV("transaction data", transaction_data) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + bcostars::TransactionUniquePtr convert_transaction_to_tars( struct bcos_sdk_c_transaction* transaction) { @@ -253,6 +387,52 @@ bcostars::TransactionUniquePtr convert_transaction_to_tars( return NULL; } +bcostars::TransactionUniquePtr convert_transaction_to_tars_v2( + struct bcos_sdk_c_transaction_v2* transaction) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction->data_hash, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction->signature, NULL); + + try + { + auto tars_transaction = std::make_unique(); + auto TransactionDataUniquePtr = + convert_transaction_data_to_tars_v2(transaction->transaction_data); + tars_transaction->data = *TransactionDataUniquePtr; + for (size_t i = 0; i < transaction->data_hash->length; ++i) + { + tars_transaction->dataHash.push_back(transaction->data_hash->buffer[i]); + } + for (size_t i = 0; i < transaction->signature->length; ++i) + { + tars_transaction->signature.push_back(transaction->signature->buffer[i]); + } + if (transaction->sender) + { + for (size_t i = 0; i < transaction->sender->length; ++i) + { + tars_transaction->sender.push_back(transaction->sender->buffer[i]); + } + } + tars_transaction->importTime = (tars::Int32)transaction->import_time; + tars_transaction->attribute = (tars::Int64)transaction->attribute; + tars_transaction->extraData = std::string(transaction->extra_data); + + return tars_transaction; + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("convert_transaction_to_tars_v2") << LOG_DESC("exception") + << LOG_KV("transaction", transaction) << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + struct bcos_sdk_c_transaction* convert_tars_transaction_to_struct( bcostars::TransactionUniquePtr tars_transaction) { @@ -294,6 +474,47 @@ struct bcos_sdk_c_transaction* convert_tars_transaction_to_struct( return NULL; } +struct bcos_sdk_c_transaction_v2* convert_tars_transaction_to_struct_v2( + bcostars::TransactionUniquePtr tars_transaction) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(tars_transaction, NULL); + + try + { + struct bcos_sdk_c_transaction_v2* transaction_struct_v2 = + (struct bcos_sdk_c_transaction_v2*)malloc(sizeof(struct bcos_sdk_c_transaction_v2)); + struct bcos_sdk_c_bytes* data_hash_bytes = create_bytes_struct( + tars_transaction->dataHash.size(), tars_transaction->dataHash.data()); + struct bcos_sdk_c_bytes* signature_bytes = create_bytes_struct( + tars_transaction->signature.size(), tars_transaction->signature.data()); + struct bcos_sdk_c_bytes* sender_bytes = + create_bytes_struct(tars_transaction->sender.size(), tars_transaction->sender.data()); + auto transactionDataUniquePtr = + std::make_unique(tars_transaction->data); + + transaction_struct_v2->transaction_data = + convert_tars_transaction_data_to_struct_v2(std::move(transactionDataUniquePtr)); + transaction_struct_v2->data_hash = data_hash_bytes; + transaction_struct_v2->signature = signature_bytes; + transaction_struct_v2->sender = sender_bytes; + transaction_struct_v2->import_time = tars_transaction->importTime; + transaction_struct_v2->attribute = tars_transaction->attribute; + transaction_struct_v2->extra_data = my_strdup(tars_transaction->extraData.data()); + return transaction_struct_v2; + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("convert_tars_transaction_to_struct_v2") + << LOG_DESC("exception") << LOG_KV("transaction", tars_transaction) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + /** * @brief * @@ -409,6 +630,7 @@ struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct_with << LOG_DESC("exception") << LOG_KV("group_id", group_id) << LOG_KV("chain_id", chain_id) << LOG_KV("to", std::string(to ? to : "")) << LOG_KV("bytes_input", bytes_input) + << LOG_KV("bytes_input_length", bytes_input_length) << LOG_KV("abi", std::string(abi ? abi : "")) << LOG_KV("block_limit", block_limit) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); @@ -973,3 +1195,633 @@ struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct_with_json( return NULL; } + +struct bcos_sdk_c_transaction_data_v2* bcos_sdk_create_transaction_data_struct_with_hex_input_v2( + const char* group_id, const char* chain_id, const char* to, const char* input, const char* abi, + int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit, + const char* max_fee_per_gas, const char* max_priority_fee_per_gas) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(group_id, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(chain_id, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(input, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((input[0] != '\0'), "input can not be empty string", NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must > 0", NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(value, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((value[0] != '\0'), "value can not be empty string", NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(gas_price, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (gas_price[0] != '\0'), "gas_price can not be empty string", NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((gas_limit >= 0), "gas limit must >= 0", NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(max_fee_per_gas, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (max_fee_per_gas[0] != '\0'), "max_fee_per_gas can not be empty string", NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(max_priority_fee_per_gas, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((max_priority_fee_per_gas[0] != '\0'), + "max_priority_fee_per_gas can not be empty string", NULL); + + try + { + struct bcos_sdk_c_transaction_data_v2* transaction_data_struct_v2 = + (struct bcos_sdk_c_transaction_data_v2*)malloc( + sizeof(struct bcos_sdk_c_transaction_data_v2)); + std::string toStr = to ? to : ""; + std::string abiStr = abi ? abi : ""; + auto bytesInput = fromHexString(input); + TransactionBuilder builder; + std::string nonceStr = builder.generateRandomStr(); + + transaction_data_struct_v2->version = 1; + transaction_data_struct_v2->block_limit = block_limit; + transaction_data_struct_v2->group_id = my_strdup(group_id); + transaction_data_struct_v2->chain_id = my_strdup(chain_id); + transaction_data_struct_v2->to = my_strdup(toStr.data()); + transaction_data_struct_v2->abi = my_strdup(abiStr.data()); + transaction_data_struct_v2->nonce = my_strdup(nonceStr.data()); + transaction_data_struct_v2->input = + create_bytes_struct(bytesInput->size(), reinterpret_cast(bytesInput->data())); + transaction_data_struct_v2->value = my_strdup(value); + transaction_data_struct_v2->gas_price = my_strdup(gas_price); + transaction_data_struct_v2->gas_limit = gas_limit; + transaction_data_struct_v2->max_fee_per_gas = my_strdup(max_fee_per_gas); + transaction_data_struct_v2->max_priority_fee_per_gas = my_strdup(max_priority_fee_per_gas); + + return transaction_data_struct_v2; + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_data_struct_with_hex_input_v2") + << LOG_DESC("exception") << LOG_KV("group_id", group_id) + << LOG_KV("chain_id", chain_id) << LOG_KV("to", std::string(to ? to : "")) + << LOG_KV("input", input) << LOG_KV("abi", std::string(abi ? abi : "")) + << LOG_KV("block_limit", block_limit) << LOG_KV("value", value) + << LOG_KV("gas_price", gas_price) << LOG_KV("gas_limit", gas_limit) + << LOG_KV("max_fee_per_gas", max_fee_per_gas) + << LOG_KV("max_priority_fee_per_gas", max_priority_fee_per_gas) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +struct bcos_sdk_c_transaction_data_v2* bcos_sdk_create_transaction_data_struct_with_bytes_v2( + const char* group_id, const char* chain_id, const char* to, const unsigned char* bytes_input, + uint32_t bytes_input_length, const char* abi, int64_t block_limit, const char* value, + const char* gas_price, int64_t gas_limit, const char* max_fee_per_gas, + const char* max_priority_fee_per_gas) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(group_id, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(chain_id, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(bytes_input, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (bytes_input_length > 0), "bytes input length must > 0", NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must > 0", NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(value, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((value[0] != '\0'), "value can not be empty string", NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(gas_price, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (gas_price[0] != '\0'), "gas_price can not be empty string", NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((gas_limit >= 0), "gas limit must >= 0", NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(max_fee_per_gas, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (max_fee_per_gas[0] != '\0'), "max_fee_per_gas can not be empty string", NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(max_priority_fee_per_gas, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((max_priority_fee_per_gas[0] != '\0'), + "max_priority_fee_per_gas can not be empty string", NULL); + + try + { + struct bcos_sdk_c_transaction_data_v2* transaction_data_struct_v2 = + (struct bcos_sdk_c_transaction_data_v2*)malloc( + sizeof(struct bcos_sdk_c_transaction_data_v2)); + std::string toStr = to ? to : ""; + std::string abiStr = abi ? abi : ""; + TransactionBuilder builder; + std::string nonceStr = builder.generateRandomStr(); + + transaction_data_struct_v2->version = 1; + transaction_data_struct_v2->block_limit = block_limit; + transaction_data_struct_v2->group_id = my_strdup(group_id); + transaction_data_struct_v2->chain_id = my_strdup(chain_id); + transaction_data_struct_v2->to = my_strdup(toStr.data()); + transaction_data_struct_v2->abi = my_strdup(abiStr.data()); + transaction_data_struct_v2->nonce = my_strdup(nonceStr.data()); + transaction_data_struct_v2->input = create_bytes_struct( + bytes_input_length, const_cast(reinterpret_cast(bytes_input))); + transaction_data_struct_v2->value = my_strdup(value); + transaction_data_struct_v2->gas_price = my_strdup(gas_price); + transaction_data_struct_v2->gas_limit = gas_limit; + transaction_data_struct_v2->max_fee_per_gas = my_strdup(max_fee_per_gas); + transaction_data_struct_v2->max_priority_fee_per_gas = my_strdup(max_priority_fee_per_gas); + + return transaction_data_struct_v2; + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_data_struct_with_bytes_v2") + << LOG_DESC("exception") << LOG_KV("group_id", group_id) + << LOG_KV("chain_id", chain_id) << LOG_KV("to", std::string(to ? to : "")) + << LOG_KV("bytes_input", bytes_input) + << LOG_KV("bytes_input_length", bytes_input_length) + << LOG_KV("abi", std::string(abi ? abi : "")) + << LOG_KV("block_limit", block_limit) << LOG_KV("value", value) + << LOG_KV("gas_price", gas_price) << LOG_KV("gas_limit", gas_limit) + << LOG_KV("max_fee_per_gas", max_fee_per_gas) + << LOG_KV("max_priority_fee_per_gas", max_priority_fee_per_gas) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +void bcos_sdk_destroy_transaction_data_struct_v2( + struct bcos_sdk_c_transaction_data_v2* transaction_data) +{ + if (transaction_data == NULL) + { + return; + } + + if (transaction_data && transaction_data->chain_id) + { + bcos_sdk_c_free(transaction_data->chain_id); + } + + if (transaction_data && transaction_data->group_id) + { + bcos_sdk_c_free(transaction_data->group_id); + } + + if (transaction_data && transaction_data->nonce) + { + bcos_sdk_c_free(transaction_data->nonce); + } + + if (transaction_data && transaction_data->to) + { + bcos_sdk_c_free(transaction_data->to); + } + + if (transaction_data && transaction_data->abi) + { + bcos_sdk_c_free((void*)transaction_data->abi); + } + + if (transaction_data && transaction_data->input) + { + if (transaction_data->input->buffer) + { + bcos_sdk_c_free(transaction_data->input->buffer); + } + bcos_sdk_c_free((void*)transaction_data->input); + } + + if (transaction_data && transaction_data->value) + { + bcos_sdk_c_free(transaction_data->value); + } + + if (transaction_data && transaction_data->gas_price) + { + bcos_sdk_c_free(transaction_data->gas_price); + } + + if (transaction_data && transaction_data->max_fee_per_gas) + { + bcos_sdk_c_free(transaction_data->max_fee_per_gas); + } + + if (transaction_data && transaction_data->max_priority_fee_per_gas) + { + bcos_sdk_c_free(transaction_data->max_priority_fee_per_gas); + } + + bcos_sdk_c_free(transaction_data); +} + +const char* bcos_sdk_encode_transaction_data_struct_v2( + struct bcos_sdk_c_transaction_data_v2* transaction_data) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); + + try + { + auto tars_transaction_data = convert_transaction_data_to_tars_v2(transaction_data); + TransactionBuilder builder; + auto encodedTransactionData = builder.encodeTransactionData(*tars_transaction_data); + auto hex_tx_data_str = bcos::toHexString(*encodedTransactionData); + return strdup(hex_tx_data_str->c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_data_struct_v2") + << LOG_DESC("exception") << LOG_KV("transaction_data", transaction_data) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +const char* bcos_sdk_encode_transaction_data_struct_to_json_v2( + struct bcos_sdk_c_transaction_data_v2* transaction_data) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); + + try + { + auto tars_transaction_data_v2 = convert_transaction_data_to_tars_v2(transaction_data); + auto json_str = tars_transaction_data_v2->writeToJsonString(); + return strdup(json_str.c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_data_struct_to_json_v2") + << LOG_DESC("exception") << LOG_KV("transaction_data", transaction_data) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_v2( + const char* transaction_data_hex_str) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data_hex_str, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((transaction_data_hex_str[0] != '\0'), + "transaction_data_hex_str can not be empty string", NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((strlen(transaction_data_hex_str) % 2 != 1), + "the length of transaction_data_hex_str must be an even number", NULL); + + try + { + TransactionBuilder builder; + auto tx_data_bytes = fromHexString(transaction_data_hex_str); + auto tars_tx_data_v2 = builder.decodeTransactionData(*tx_data_bytes); + return convert_tars_transaction_data_to_struct_v2(std::move(tars_tx_data_v2)); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_data_struct_v2") + << LOG_DESC("exception") << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_with_json_v2( + const char* transaction_data_json_str) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data_json_str, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((transaction_data_json_str[0] != '\0'), + "transaction_data_json_str can not be empty string", NULL); + + try + { + TransactionBuilder builder; + auto transactionData = + builder.createTransactionDataWithJson(std::string(transaction_data_json_str)); + + return convert_tars_transaction_data_to_struct_v2(std::move(transactionData)); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_data_struct_with_json_v2") + << LOG_DESC("exception") + << LOG_KV("transaction_data_json_str", transaction_data_json_str) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +const char* bcos_sdk_calc_transaction_data_struct_hash_v2( + int crypto_type, struct bcos_sdk_c_transaction_data_v2* transaction_data) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (crypto_type == BCOS_C_SDK_ECDSA_TYPE || crypto_type == BCOS_C_SDK_SM_TYPE), + "invalid crypto type, it must be BCOS_C_SDK_ECDSA_TYPE(ecdsa crypto type) or " + "BCOS_C_SDK_SM_TYPE(sm crypto type)", + NULL); + + try + { + auto tars_transaction_data_v2 = convert_transaction_data_to_tars_v2(transaction_data); + TransactionBuilder builder; + auto transactionDataHash = builder.calculateTransactionDataHash( + crypto_type == BCOS_C_SDK_ECDSA_TYPE ? CryptoType::Secp256K1 : CryptoType::SM2, + *tars_transaction_data_v2); + return strdup(bcos::toHexStringWithPrefix(transactionDataHash).c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_calc_transaction_data_struct_hash_v2") + << LOG_DESC("exception") << LOG_KV("crypto_type", crypto_type) + << LOG_KV("transaction_data", transaction_data) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +const char* bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2( + int crypto_type, const char* transaction_data_hex) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data_hex, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (crypto_type == BCOS_C_SDK_ECDSA_TYPE || crypto_type == BCOS_C_SDK_SM_TYPE), + "invalid crypto type, it must be BCOS_C_SDK_ECDSA_TYPE(ecdsa crypto type) or " + "BCOS_C_SDK_SM_TYPE(sm crypto type)", + NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (transaction_data_hex[0] != '\0'), "transaction_data_hex can not be empty string", NULL); + + try + { + struct bcos_sdk_c_transaction_data_v2* transaction_data_struct_v2 = + bcos_sdk_decode_transaction_data_struct_v2(transaction_data_hex); + auto tars_transaction_data_v2 = + convert_transaction_data_to_tars_v2(transaction_data_struct_v2); + TransactionBuilder builder; + auto transactionDataHash = builder.calculateTransactionDataHash( + crypto_type == BCOS_C_SDK_ECDSA_TYPE ? CryptoType::Secp256K1 : CryptoType::SM2, + *tars_transaction_data_v2); + return strdup(bcos::toHexStringWithPrefix(transactionDataHash).c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2") + << LOG_DESC("exception") << LOG_KV("crypto_type", crypto_type) + << LOG_KV("transaction_data_hex", transaction_data_hex) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +struct bcos_sdk_c_transaction_v2* bcos_sdk_create_transaction_struct_v2( + struct bcos_sdk_c_transaction_data_v2* transaction_data, const char* signature, + const char* transaction_data_hash, int32_t attribute, const char* extra_data) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(signature, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data_hash, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (signature[0] != '\0'), "signature can not be empty string", NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (transaction_data_hash[0] != '\0'), "transaction_data_hash can not be empty string", NULL); + + try + { + struct bcos_sdk_c_transaction_v2* transaction_struct_v2 = + (struct bcos_sdk_c_transaction_v2*)malloc(sizeof(struct bcos_sdk_c_transaction_v2)); + transaction_struct_v2->transaction_data = transaction_data_copy_v2(transaction_data); + transaction_struct_v2->sender = NULL; + transaction_struct_v2->import_time = 0; + transaction_struct_v2->attribute = attribute; + transaction_struct_v2->extra_data = my_strdup(extra_data); + // signature + auto signatureWithoutHex = fromHexString(signature); + struct bcos_sdk_c_bytes* signature_bytes = + (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); + signature_bytes->length = signatureWithoutHex->size(); + signature_bytes->buffer = (uint8_t*)malloc(signatureWithoutHex->size()); + memcpy(signature_bytes->buffer, signatureWithoutHex->data(), signatureWithoutHex->size()); + transaction_struct_v2->signature = signature_bytes; + // data_hash + auto dataHashArray = bcos::crypto::HashType(transaction_data_hash); + struct bcos_sdk_c_bytes* data_hash_bytes = + (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); + data_hash_bytes->length = dataHashArray.size(); + data_hash_bytes->buffer = (uint8_t*)malloc(dataHashArray.size()); + memcpy(data_hash_bytes->buffer, dataHashArray.data(), dataHashArray.size()); + transaction_struct_v2->data_hash = data_hash_bytes; + + return transaction_struct_v2; + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_struct_v2") + << LOG_DESC("exception") << LOG_KV("signature", signature) + << LOG_KV("transaction_data_hash", transaction_data_hash) + << LOG_KV("attribute", attribute) << LOG_KV("extra_data", extra_data) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +void bcos_sdk_destroy_transaction_struct_v2(struct bcos_sdk_c_transaction_v2* transaction) +{ + if (transaction == NULL) + { + return; + } + + if (transaction && transaction->transaction_data) + { + bcos_sdk_destroy_transaction_data_struct_v2(transaction->transaction_data); + } + + if (transaction && transaction->data_hash) + { + if (transaction->data_hash->buffer) + { + bcos_sdk_c_free(transaction->data_hash->buffer); + } + bcos_sdk_c_free(transaction->data_hash); + } + + if (transaction && transaction->signature) + { + if (transaction->signature->buffer) + { + bcos_sdk_c_free(transaction->signature->buffer); + } + bcos_sdk_c_free(transaction->signature); + } + + if (transaction && transaction->sender) + { + if (transaction->sender->buffer) + { + bcos_sdk_c_free(transaction->sender->buffer); + } + bcos_sdk_c_free(transaction->sender); + } + + if (transaction && transaction->extra_data) + { + bcos_sdk_c_free(transaction->extra_data); + } + + bcos_sdk_c_free(transaction); +} + +const char* bcos_sdk_create_encoded_transaction_v2( + struct bcos_sdk_c_transaction_data_v2* transaction_data, const char* signature, + const char* transaction_data_hash, int32_t attribute, const char* extra_data) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(signature, NULL); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data_hash, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (signature[0] != '\0'), "signature can not be empty string", NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (transaction_data_hash[0] != '\0'), "transaction_data_hash can not be empty string", NULL); + + try + { + TransactionBuilder builder; + auto tars_tx_data_v2 = convert_transaction_data_to_tars_v2(transaction_data); + auto signedBytes = builder.createSignedTransaction(*tars_tx_data_v2, + *fromHexString(signature), bcos::crypto::HashType(transaction_data_hash), attribute, + extra_data ? std::string(extra_data) : std::string()); + return strdup(bcos::toHexStringWithPrefix(*signedBytes).c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_encoded_transaction_v2") + << LOG_DESC("exception") << LOG_KV("signature", signature) + << LOG_KV("transaction_data_hash", transaction_data_hash) + << LOG_KV("attribute", attribute) << LOG_KV("extra_data", extra_data) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + return NULL; +} + +const char* bcos_sdk_encode_transaction_struct_v2(struct bcos_sdk_c_transaction_v2* transaction) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction, NULL); + + try + { + auto tars_transaction_v2 = convert_transaction_to_tars_v2(transaction); + TransactionBuilder builder; + auto encodedTransaction = builder.encodeTransaction(*tars_transaction_v2); + auto hex_tx_str = toHexStringWithPrefix(*encodedTransaction); + return strdup(hex_tx_str.c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_struct_v2") + << LOG_DESC("exception") << LOG_KV("transaction", transaction) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +const char* bcos_sdk_encode_transaction_struct_to_json_v2( + struct bcos_sdk_c_transaction_v2* transaction) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction, NULL); + + try + { + auto tars_transaction_v2 = convert_transaction_to_tars_v2(transaction); + auto json_str = tars_transaction_v2->writeToJsonString(); + return strdup(json_str.c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_struct_to_json_v2") + << LOG_DESC("exception") << LOG_KV("transaction", transaction) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_v2( + const char* transaction_hex_str) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_hex_str, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (transaction_hex_str[0] != '\0'), "transaction_hex_str can not be empty string", NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION((strlen(transaction_hex_str) % 2 != 1), + "the length of transaction_hex_str must be an even number", NULL); + + try + { + TransactionBuilder builder; + auto tx_bytes = fromHexString(transaction_hex_str); + auto tars_tx = builder.decodeTransaction(*tx_bytes); + return convert_tars_transaction_to_struct_v2(std::move(tars_tx)); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_struct") + << LOG_DESC("exception") + << LOG_KV("transaction_hex_str", transaction_hex_str) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} + +struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_with_json_v2( + const char* transaction_json_str) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_json_str, NULL); + BCOS_SDK_C_PARAMS_VERIFY_CONDITION( + (transaction_json_str[0] != '\0'), "transaction_json_str can not be empty string", NULL); + + try + { + TransactionBuilder builder; + auto transaction = builder.createTransactionWithJson(std::string(transaction_json_str)); + + return convert_tars_transaction_to_struct_v2(std::move(transaction)); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_struct_with_json_v2") + << LOG_DESC("exception") + << LOG_KV("transaction_json_str", transaction_json_str) + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + + return NULL; +} diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h index e275b093d..4075c51c3 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h @@ -193,6 +193,52 @@ struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct(const char* tr struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct_with_json( const char* transaction_json_str); +struct bcos_sdk_c_transaction_data_v2* bcos_sdk_create_transaction_data_struct_with_hex_input_v2( + const char* group_id, const char* chain_id, const char* to, const char* input, const char* abi, + int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit, const char* max_fee_per_gas, const char* max_priority_fee_per_gas); + +struct bcos_sdk_c_transaction_data_v2* bcos_sdk_create_transaction_data_struct_with_bytes_v2( + const char* group_id, const char* chain_id, const char* to, const unsigned char* bytes_input, + uint32_t bytes_input_length, const char* abi, int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit, const char* max_fee_per_gas, const char* max_priority_fee_per_gas); + +void bcos_sdk_destroy_transaction_data_struct_v2(struct bcos_sdk_c_transaction_data_v2* transaction_data); + +const char* bcos_sdk_encode_transaction_data_struct_v2(struct bcos_sdk_c_transaction_data_v2* transaction_data); + +const char* bcos_sdk_encode_transaction_data_struct_to_json_v2(struct bcos_sdk_c_transaction_data_v2* transaction_data); + +struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_v2( + const char* transaction_data_hex_str); + +struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_with_json_v2( + const char* transaction_data_json_str); + +const char* bcos_sdk_calc_transaction_data_struct_hash_v2( + int crypto_type, struct bcos_sdk_c_transaction_data_v2* transaction_data); + +const char* bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2( + int crypto_type, const char* transaction_data_hex); + +struct bcos_sdk_c_transaction_v2* bcos_sdk_create_transaction_struct_v2( + struct bcos_sdk_c_transaction_data_v2* transaction_data, const char* signature, + const char* transaction_data_hash, int32_t attribute, const char* extra_data); + +void bcos_sdk_destroy_transaction_struct_v2(struct bcos_sdk_c_transaction_v2* transaction); + +const char* bcos_sdk_create_encoded_transaction_v2( + struct bcos_sdk_c_transaction_data_v2* transaction_data, const char* signature, + const char* transaction_data_hash, int32_t attribute, const char* extra_data); + +const char* bcos_sdk_encode_transaction_struct_v2(struct bcos_sdk_c_transaction_v2* transaction); + +const char* bcos_sdk_encode_transaction_struct_to_json_v2(struct bcos_sdk_c_transaction_v2* transaction); + +struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_v2( + const char* transaction_hex_str); + +struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_with_json_v2( + const char* transaction_json_str); + #ifdef __cplusplus } #endif diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp index 0ab13580a..e96679b59 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp @@ -6,275 +6,359 @@ struct bcos_sdk_c_bytes* convert_to_bytes_struct(JNIEnv* env, jobject bytesObj) { - if (bytesObj == NULL) + try + { + if (bytesObj == NULL) + { + return NULL; + } + + struct bcos_sdk_c_bytes* bytes = + (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); + jclass bytesClass = env->GetObjectClass(bytesObj); + jfieldID bufferField = env->GetFieldID(bytesClass, "buffer", "[B"); + jfieldID lengthField = env->GetFieldID(bytesClass, "length", "I"); + + jbyteArray bufferArray = (jbyteArray)env->GetObjectField(bytesObj, bufferField); + jbyte* bufferData = env->GetByteArrayElements(bufferArray, nullptr); + jsize bufferLength = env->GetArrayLength(bufferArray); + bytes->length = (uint32_t)env->GetIntField(bytesObj, lengthField); + bytes->buffer = (uint8_t*)malloc(bufferLength * sizeof(uint8_t)); + memcpy(bytes->buffer, bufferData, bufferLength); + + env->ReleaseByteArrayElements(bufferArray, bufferData, 0); + + return bytes; + } + catch (...) { return NULL; } - - struct bcos_sdk_c_bytes* bytes = - (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); - jclass bytesClass = env->GetObjectClass(bytesObj); - jfieldID bufferField = env->GetFieldID(bytesClass, "buffer", "[B"); - jfieldID lengthField = env->GetFieldID(bytesClass, "length", "I"); - - jbyteArray bufferArray = (jbyteArray)env->GetObjectField(bytesObj, bufferField); - jbyte* bufferData = env->GetByteArrayElements(bufferArray, nullptr); - jsize bufferLength = env->GetArrayLength(bufferArray); - bytes->length = (uint32_t)env->GetIntField(bytesObj, lengthField); - bytes->buffer = (uint8_t*)malloc(bufferLength * sizeof(uint8_t)); - memcpy(bytes->buffer, bufferData, bufferLength); - - env->ReleaseByteArrayElements(bufferArray, bufferData, 0); - - return bytes; } jobject convert_to_bytes_jobject(JNIEnv* env, const struct bcos_sdk_c_bytes* bytes_struct) { - if (bytes_struct == NULL) - { - return NULL; - } - - jclass txBytesClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes"); - if (txBytesClass == NULL) + try { - env->FatalError( - "No such class, className: " - "org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes"); + if (bytes_struct == NULL) + { + return NULL; + } + + jclass txBytesClass = + env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes"); + if (txBytesClass == NULL) + { + env->FatalError( + "No such class, className: " + "org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes"); + } + jmethodID txBytesMtd = env->GetMethodID(txBytesClass, "", "()V"); + if (txBytesMtd == NULL) + { + env->FatalError("No such constructor in TransactionBytes, constructor()"); + } + jfieldID bufferField = env->GetFieldID(txBytesClass, "buffer", "[B"); + jfieldID lengthField = env->GetFieldID(txBytesClass, "length", "I"); + + jobject jTxBytesObj = env->NewObject(txBytesClass, txBytesMtd); + // buffer + jbyteArray bufferArray = env->NewByteArray(bytes_struct->length); + env->SetByteArrayRegion( + bufferArray, 0, bytes_struct->length, reinterpret_cast(bytes_struct->buffer)); + env->SetObjectField(jTxBytesObj, bufferField, bufferArray); + // length + env->SetIntField(jTxBytesObj, lengthField, bytes_struct->length); + + env->DeleteLocalRef(bufferArray); + + return jTxBytesObj; } - jmethodID txBytesMtd = env->GetMethodID(txBytesClass, "", "()V"); - if (txBytesMtd == NULL) + catch (...) { - env->FatalError("No such constructor in TransactionBytes, constructor()"); + return NULL; } - jfieldID bufferField = env->GetFieldID(txBytesClass, "buffer", "[B"); - jfieldID lengthField = env->GetFieldID(txBytesClass, "length", "I"); - - jobject jTxBytesObj = env->NewObject(txBytesClass, txBytesMtd); - // buffer - jbyteArray bufferArray = env->NewByteArray(bytes_struct->length); - env->SetByteArrayRegion( - bufferArray, 0, bytes_struct->length, reinterpret_cast(bytes_struct->buffer)); - env->SetObjectField(jTxBytesObj, bufferField, bufferArray); - // length - env->SetIntField(jTxBytesObj, lengthField, bytes_struct->length); - - env->DeleteLocalRef(bufferArray); - - return jTxBytesObj; } struct bcos_sdk_c_transaction_data* convert_to_tx_data_struct( JNIEnv* env, jobject transactionObject) { - bcos_sdk_c_transaction_data* tx_data_struct = - (struct bcos_sdk_c_transaction_data*)malloc(sizeof(struct bcos_sdk_c_transaction_data)); - jclass txDataClass = env->GetObjectClass(transactionObject); - - // version - jfieldID versionField = env->GetFieldID(txDataClass, "version", "I"); - tx_data_struct->version = env->GetIntField(transactionObject, versionField); - - // blockLimit - jfieldID blockLimitField = env->GetFieldID(txDataClass, "blockLimit", "J"); - tx_data_struct->block_limit = env->GetLongField(transactionObject, blockLimitField); - - // chainId - jfieldID chainIdField = env->GetFieldID(txDataClass, "chainId", "Ljava/lang/String;"); - jstring chainIdString = (jstring)env->GetObjectField(transactionObject, chainIdField); - const char* chainIdValue = env->GetStringUTFChars(chainIdString, nullptr); - tx_data_struct->chain_id = strdup(chainIdValue); - - // groupId - jfieldID groupIdField = env->GetFieldID(txDataClass, "groupId", "Ljava/lang/String;"); - jstring groupIdString = (jstring)env->GetObjectField(transactionObject, groupIdField); - const char* groupIdValue = env->GetStringUTFChars(groupIdString, nullptr); - tx_data_struct->group_id = strdup(groupIdValue); - - // nonce - jfieldID nonceField = env->GetFieldID(txDataClass, "nonce", "Ljava/lang/String;"); - jstring nonceString = (jstring)env->GetObjectField(transactionObject, nonceField); - const char* nonceValue = env->GetStringUTFChars(nonceString, nullptr); - tx_data_struct->nonce = strdup(nonceValue); - - // to - jfieldID toField = env->GetFieldID(txDataClass, "to", "Ljava/lang/String;"); - jstring toString = (jstring)env->GetObjectField(transactionObject, toField); - const char* toValue = env->GetStringUTFChars(toString, nullptr); - tx_data_struct->to = strdup(toValue); - - // abi - jfieldID abiField = env->GetFieldID(txDataClass, "abi", "Ljava/lang/String;"); - jstring abiString = (jstring)env->GetObjectField(transactionObject, abiField); - const char* abiValue = env->GetStringUTFChars(abiString, nullptr); - tx_data_struct->abi = strdup(abiValue); - - // input - jfieldID inputField = - env->GetFieldID(txDataClass, "input", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jobject inputObject = env->GetObjectField(transactionObject, inputField); - tx_data_struct->input = convert_to_bytes_struct(env, inputObject); - - // release source - env->ReleaseStringUTFChars(chainIdString, chainIdValue); - env->ReleaseStringUTFChars(groupIdString, groupIdValue); - env->ReleaseStringUTFChars(nonceString, nonceValue); - env->ReleaseStringUTFChars(toString, toValue); - env->ReleaseStringUTFChars(abiString, abiValue); - - return tx_data_struct; + try + { + bcos_sdk_c_transaction_data* tx_data_struct = + (struct bcos_sdk_c_transaction_data*)malloc(sizeof(struct bcos_sdk_c_transaction_data)); + jclass txDataClass = env->GetObjectClass(transactionObject); + + // version + jfieldID versionField = env->GetFieldID(txDataClass, "version", "I"); + tx_data_struct->version = env->GetIntField(transactionObject, versionField); + + // blockLimit + jfieldID blockLimitField = env->GetFieldID(txDataClass, "blockLimit", "J"); + tx_data_struct->block_limit = env->GetLongField(transactionObject, blockLimitField); + + // chainId + jfieldID chainIdField = env->GetFieldID(txDataClass, "chainId", "Ljava/lang/String;"); + jstring chainIdString = (jstring)env->GetObjectField(transactionObject, chainIdField); + const char* chainIdValue = env->GetStringUTFChars(chainIdString, nullptr); + tx_data_struct->chain_id = strdup(chainIdValue); + + // groupId + jfieldID groupIdField = env->GetFieldID(txDataClass, "groupId", "Ljava/lang/String;"); + jstring groupIdString = (jstring)env->GetObjectField(transactionObject, groupIdField); + const char* groupIdValue = env->GetStringUTFChars(groupIdString, nullptr); + tx_data_struct->group_id = strdup(groupIdValue); + + // nonce + jfieldID nonceField = env->GetFieldID(txDataClass, "nonce", "Ljava/lang/String;"); + jstring nonceString = (jstring)env->GetObjectField(transactionObject, nonceField); + const char* nonceValue = env->GetStringUTFChars(nonceString, nullptr); + tx_data_struct->nonce = strdup(nonceValue); + + // to + jfieldID toField = env->GetFieldID(txDataClass, "to", "Ljava/lang/String;"); + jstring toString = (jstring)env->GetObjectField(transactionObject, toField); + const char* toValue = env->GetStringUTFChars(toString, nullptr); + tx_data_struct->to = strdup(toValue); + + // abi + jfieldID abiField = env->GetFieldID(txDataClass, "abi", "Ljava/lang/String;"); + jstring abiString = (jstring)env->GetObjectField(transactionObject, abiField); + const char* abiValue = env->GetStringUTFChars(abiString, nullptr); + tx_data_struct->abi = strdup(abiValue); + + // input + jfieldID inputField = env->GetFieldID( + txDataClass, "input", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jobject inputObject = env->GetObjectField(transactionObject, inputField); + struct bcos_sdk_c_bytes* inputStruct = convert_to_bytes_struct(env, inputObject); + if (inputStruct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_struct"); + } + tx_data_struct->input = inputStruct; + + // release source + env->ReleaseStringUTFChars(chainIdString, chainIdValue); + env->ReleaseStringUTFChars(groupIdString, groupIdValue); + env->ReleaseStringUTFChars(nonceString, nonceValue); + env->ReleaseStringUTFChars(toString, toValue); + env->ReleaseStringUTFChars(abiString, abiValue); + + return tx_data_struct; + } + catch (...) + { + return NULL; + } } jobject convert_to_tx_data_jobject( JNIEnv* env, const struct bcos_sdk_c_transaction_data* transactionData) { - jclass txDataClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionData"); - if (txDataClass == NULL) - { - env->FatalError( - "No such class, className: " - "org/fisco/bcos/sdk/jni/utilities/tx/TransactionData"); - } - jmethodID txDataMtd = env->GetMethodID(txDataClass, "", "()V"); - if (txDataMtd == NULL) - { - env->FatalError("No such constructor in TransactionData, constructor()"); - } - jobject jTxDataObj = env->NewObject(txDataClass, txDataMtd); - - // version - jfieldID versionField = env->GetFieldID(txDataClass, "version", "I"); - env->SetIntField(jTxDataObj, versionField, transactionData->version); - // block_limit - jfieldID blockLimitField = env->GetFieldID(txDataClass, "blockLimit", "J"); - env->SetLongField(jTxDataObj, blockLimitField, transactionData->block_limit); - // chain_id - jfieldID chainIdField = env->GetFieldID(txDataClass, "chainId", "Ljava/lang/String;"); - env->SetObjectField(jTxDataObj, chainIdField, env->NewStringUTF(transactionData->chain_id)); - // group_id - jfieldID groupIdField = env->GetFieldID(txDataClass, "groupId", "Ljava/lang/String;"); - env->SetObjectField(jTxDataObj, groupIdField, env->NewStringUTF(transactionData->group_id)); - // nonce - jfieldID nonceField = env->GetFieldID(txDataClass, "nonce", "Ljava/lang/String;"); - env->SetObjectField(jTxDataObj, nonceField, env->NewStringUTF(transactionData->nonce)); - // to - jfieldID toField = env->GetFieldID(txDataClass, "to", "Ljava/lang/String;"); - env->SetObjectField(jTxDataObj, toField, env->NewStringUTF(transactionData->to)); - // abi - jfieldID abiField = env->GetFieldID(txDataClass, "abi", "Ljava/lang/String;"); - env->SetObjectField(jTxDataObj, abiField, env->NewStringUTF(transactionData->abi)); - - // input - jobject jInput = convert_to_bytes_jobject(env, transactionData->input); - jfieldID inputField = - env->GetFieldID(txDataClass, "input", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - env->SetObjectField(jTxDataObj, inputField, jInput); - - // release - env->DeleteLocalRef(jInput); - - return jTxDataObj; + try + { + jclass txDataClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionData"); + if (txDataClass == NULL) + { + env->FatalError( + "No such class, className: " + "org/fisco/bcos/sdk/jni/utilities/tx/TransactionData"); + } + jmethodID txDataMtd = env->GetMethodID(txDataClass, "", "()V"); + if (txDataMtd == NULL) + { + env->FatalError("No such constructor in TransactionData, constructor()"); + } + jobject jTxDataObj = env->NewObject(txDataClass, txDataMtd); + + // version + jfieldID versionField = env->GetFieldID(txDataClass, "version", "I"); + env->SetIntField(jTxDataObj, versionField, transactionData->version); + // block_limit + jfieldID blockLimitField = env->GetFieldID(txDataClass, "blockLimit", "J"); + env->SetLongField(jTxDataObj, blockLimitField, transactionData->block_limit); + // chain_id + jfieldID chainIdField = env->GetFieldID(txDataClass, "chainId", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, chainIdField, env->NewStringUTF(transactionData->chain_id)); + // group_id + jfieldID groupIdField = env->GetFieldID(txDataClass, "groupId", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, groupIdField, env->NewStringUTF(transactionData->group_id)); + // nonce + jfieldID nonceField = env->GetFieldID(txDataClass, "nonce", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, nonceField, env->NewStringUTF(transactionData->nonce)); + // to + jfieldID toField = env->GetFieldID(txDataClass, "to", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, toField, env->NewStringUTF(transactionData->to)); + // abi + jfieldID abiField = env->GetFieldID(txDataClass, "abi", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, abiField, env->NewStringUTF(transactionData->abi)); + + // input + jobject jInput = convert_to_bytes_jobject(env, transactionData->input); + if (jInput == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_jobject"); + } + jfieldID inputField = env->GetFieldID( + txDataClass, "input", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + env->SetObjectField(jTxDataObj, inputField, jInput); + + // release + env->DeleteLocalRef(jInput); + + return jTxDataObj; + } + catch (...) + { + return NULL; + } } jobject convert_to_tx_jobject(JNIEnv* env, const struct bcos_sdk_c_transaction* tx_struct) { - jclass txClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/Transaction"); - if (txClass == NULL) - { - env->FatalError( - "No such class, className: " - "org/fisco/bcos/sdk/jni/utilities/tx/Transaction"); - } - jmethodID txMtd = env->GetMethodID(txClass, "", "()V"); - if (txMtd == NULL) - { - env->FatalError("No such constructor in Transaction, constructor()"); - } - jfieldID txDataField = - env->GetFieldID(txClass, "transactionData", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;"); - jfieldID dataHashField = - env->GetFieldID(txClass, "dataHash", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jfieldID signatureField = - env->GetFieldID(txClass, "signature", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jfieldID senderField = - env->GetFieldID(txClass, "sender", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jfieldID importTimeField = env->GetFieldID(txClass, "importTime", "J"); - jfieldID attributeField = env->GetFieldID(txClass, "attribute", "I"); - jfieldID extraDataField = env->GetFieldID(txClass, "extraData", "Ljava/lang/String;"); - - jobject javaTxObj = env->NewObject(txClass, txMtd); - // TransactionData - jobject javaTxDataObj = convert_to_tx_data_jobject(env, tx_struct->transaction_data); - env->SetObjectField(javaTxObj, txDataField, javaTxDataObj); - // DataHash - jobject javaDataHashObj = convert_to_bytes_jobject(env, tx_struct->data_hash); - env->SetObjectField(javaTxObj, dataHashField, javaDataHashObj); - // Signature - jobject javaSignatureObj = convert_to_bytes_jobject(env, tx_struct->signature); - env->SetObjectField(javaTxObj, signatureField, javaSignatureObj); - // Sender - jobject javaSenderObj = convert_to_bytes_jobject(env, tx_struct->sender); - env->SetObjectField(javaTxObj, senderField, javaSenderObj); - // ImportTime - env->SetLongField(javaTxObj, importTimeField, tx_struct->import_time); - // Attribute - env->SetIntField(javaTxObj, attributeField, tx_struct->attribute); - // ExtraData - jstring javaExtraData = env->NewStringUTF(tx_struct->extra_data); - env->SetObjectField(javaTxObj, extraDataField, javaExtraData); - - return javaTxObj; + try + { + jclass txClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/Transaction"); + if (txClass == NULL) + { + env->FatalError( + "No such class, className: " + "org/fisco/bcos/sdk/jni/utilities/tx/Transaction"); + } + jmethodID txMtd = env->GetMethodID(txClass, "", "()V"); + if (txMtd == NULL) + { + env->FatalError("No such constructor in Transaction, constructor()"); + } + jfieldID txDataField = env->GetFieldID( + txClass, "transactionData", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;"); + jfieldID dataHashField = env->GetFieldID( + txClass, "dataHash", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jfieldID signatureField = env->GetFieldID( + txClass, "signature", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jfieldID senderField = env->GetFieldID( + txClass, "sender", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jfieldID importTimeField = env->GetFieldID(txClass, "importTime", "J"); + jfieldID attributeField = env->GetFieldID(txClass, "attribute", "I"); + jfieldID extraDataField = env->GetFieldID(txClass, "extraData", "Ljava/lang/String;"); + + jobject javaTxObj = env->NewObject(txClass, txMtd); + // TransactionData + jobject javaTxDataObj = convert_to_tx_data_jobject(env, tx_struct->transaction_data); + if (javaTxDataObj == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_jobject"); + } + env->SetObjectField(javaTxObj, txDataField, javaTxDataObj); + // DataHash + jobject javaDataHashObj = convert_to_bytes_jobject(env, tx_struct->data_hash); + if (javaDataHashObj == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_jobject"); + } + env->SetObjectField(javaTxObj, dataHashField, javaDataHashObj); + // Signature + jobject javaSignatureObj = convert_to_bytes_jobject(env, tx_struct->signature); + if (javaSignatureObj == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_jobject"); + } + env->SetObjectField(javaTxObj, signatureField, javaSignatureObj); + // Sender + jobject javaSenderObj = convert_to_bytes_jobject(env, tx_struct->sender); + if (javaSenderObj == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_jobject"); + } + env->SetObjectField(javaTxObj, senderField, javaSenderObj); + // ImportTime + env->SetLongField(javaTxObj, importTimeField, tx_struct->import_time); + // Attribute + env->SetIntField(javaTxObj, attributeField, tx_struct->attribute); + // ExtraData + jstring javaExtraData = env->NewStringUTF(tx_struct->extra_data); + env->SetObjectField(javaTxObj, extraDataField, javaExtraData); + + return javaTxObj; + } + catch (...) + { + return NULL; + } } struct bcos_sdk_c_transaction* convert_to_tx_struct(JNIEnv* env, jobject jTxObj) { - jclass javaTxClass = env->GetObjectClass(jTxObj); - jfieldID transactionDataField = env->GetFieldID( - javaTxClass, "transactionData", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;"); - jfieldID dataHashField = - env->GetFieldID(javaTxClass, "dataHash", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jfieldID signatureField = - env->GetFieldID(javaTxClass, "signature", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jfieldID senderField = - env->GetFieldID(javaTxClass, "sender", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jfieldID importTimeField = env->GetFieldID(javaTxClass, "importTime", "J"); - jfieldID attributeField = env->GetFieldID(javaTxClass, "attribute", "I"); - jfieldID extraDataField = env->GetFieldID(javaTxClass, "extraData", "Ljava/lang/String;"); - - struct bcos_sdk_c_transaction* txStruct = - (struct bcos_sdk_c_transaction*)malloc(sizeof(struct bcos_sdk_c_transaction)); - // TransactionData - jobject javaTxDataObj = env->GetObjectField(jTxObj, transactionDataField); - struct bcos_sdk_c_transaction_data* txDataStruct = - convert_to_tx_data_struct(env, javaTxDataObj); - txStruct->transaction_data = txDataStruct; - // DataHash - jobject javaDataHashObj = env->GetObjectField(jTxObj, dataHashField); - struct bcos_sdk_c_bytes* dataHashStruct = convert_to_bytes_struct(env, javaDataHashObj); - txStruct->data_hash = dataHashStruct; - // Signature - jobject javaSignatureObj = env->GetObjectField(jTxObj, signatureField); - struct bcos_sdk_c_bytes* signatureStruct = convert_to_bytes_struct(env, javaSignatureObj); - txStruct->signature = signatureStruct; - // Sender - jobject javaSenderObj = env->GetObjectField(jTxObj, senderField); - struct bcos_sdk_c_bytes* senderStruct = convert_to_bytes_struct(env, javaSenderObj); - txStruct->sender = senderStruct; - // ImportTime - jlong importTimeValue = env->GetLongField(jTxObj, importTimeField); - txStruct->import_time = (int64_t)importTimeValue; - // Attribute - jint attributeValue = env->GetIntField(jTxObj, attributeField); - txStruct->attribute = (int32_t)attributeValue; - // ExtraData - jstring javaExtraData = (jstring)env->GetObjectField(jTxObj, extraDataField); - const char* extraDataValue = env->GetStringUTFChars(javaExtraData, NULL); - txStruct->extra_data = strdup(extraDataValue); - - env->ReleaseStringUTFChars(javaExtraData, extraDataValue); - - return txStruct; + try + { + jclass javaTxClass = env->GetObjectClass(jTxObj); + jfieldID transactionDataField = env->GetFieldID(javaTxClass, "transactionData", + "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;"); + jfieldID dataHashField = env->GetFieldID( + javaTxClass, "dataHash", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jfieldID signatureField = env->GetFieldID( + javaTxClass, "signature", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jfieldID senderField = env->GetFieldID( + javaTxClass, "sender", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jfieldID importTimeField = env->GetFieldID(javaTxClass, "importTime", "J"); + jfieldID attributeField = env->GetFieldID(javaTxClass, "attribute", "I"); + jfieldID extraDataField = env->GetFieldID(javaTxClass, "extraData", "Ljava/lang/String;"); + + struct bcos_sdk_c_transaction* txStruct = + (struct bcos_sdk_c_transaction*)malloc(sizeof(struct bcos_sdk_c_transaction)); + // TransactionData + jobject javaTxDataObj = env->GetObjectField(jTxObj, transactionDataField); + struct bcos_sdk_c_transaction_data* txDataStruct = + convert_to_tx_data_struct(env, javaTxDataObj); + if (txDataStruct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); + } + txStruct->transaction_data = txDataStruct; + // DataHash + jobject javaDataHashObj = env->GetObjectField(jTxObj, dataHashField); + struct bcos_sdk_c_bytes* dataHashStruct = convert_to_bytes_struct(env, javaDataHashObj); + if (dataHashStruct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_struct"); + } + txStruct->data_hash = dataHashStruct; + // Signature + jobject javaSignatureObj = env->GetObjectField(jTxObj, signatureField); + struct bcos_sdk_c_bytes* signatureStruct = convert_to_bytes_struct(env, javaSignatureObj); + if (signatureStruct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_struct"); + } + txStruct->signature = signatureStruct; + // Sender + jobject javaSenderObj = env->GetObjectField(jTxObj, senderField); + struct bcos_sdk_c_bytes* senderStruct = convert_to_bytes_struct(env, javaSenderObj); + if (senderStruct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_struct"); + } + txStruct->sender = senderStruct; + // ImportTime + jlong importTimeValue = env->GetLongField(jTxObj, importTimeField); + txStruct->import_time = (int64_t)importTimeValue; + // Attribute + jint attributeValue = env->GetIntField(jTxObj, attributeField); + txStruct->attribute = (int32_t)attributeValue; + // ExtraData + jstring javaExtraData = (jstring)env->GetObjectField(jTxObj, extraDataField); + const char* extraDataValue = env->GetStringUTFChars(javaExtraData, NULL); + txStruct->extra_data = strdup(extraDataValue); + + env->ReleaseStringUTFChars(javaExtraData, extraDataValue); + + return txStruct; + } + catch (...) + { + return NULL; + } } void destroy_bytes_jobject(JNIEnv* env, jobject jTxBytes) @@ -302,6 +386,10 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr { struct bcos_sdk_c_transaction_data* tx_data_struct = convert_to_tx_data_struct(env, jTransactionDataObj); + if (tx_data_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); + } const char* tx_data_hex = bcos_sdk_encode_transaction_data_struct(tx_data_struct); if (!bcos_sdk_is_last_opr_success()) { @@ -334,6 +422,10 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr { struct bcos_sdk_c_transaction_data* tx_data_struct = convert_to_tx_data_struct(env, jTransactionDataObj); + if (tx_data_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); + } const char* tx_data_json = bcos_sdk_encode_transaction_data_struct_to_json(tx_data_struct); if (!bcos_sdk_is_last_opr_success()) { @@ -373,6 +465,10 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTr } jobject jTxDataObj = convert_to_tx_data_jobject(env, tx_data_struct); + if (jTxDataObj == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_jobject"); + } // release source if (tx_data_struct) @@ -395,6 +491,10 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_calcTran { struct bcos_sdk_c_transaction_data* tx_data_struct = convert_to_tx_data_struct(env, jTransactionDataObj); + if (tx_data_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); + } int crypto_type = jCrytpTyte; const char* tx_data_hash = bcos_sdk_calc_transaction_data_struct_hash(crypto_type, tx_data_struct); @@ -431,6 +531,10 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_createEn jint jAttribute, jstring jExtraData) { struct bcos_sdk_c_transaction_data* tx_data_struct = convert_to_tx_data_struct(env, jTxDataObj); + if (tx_data_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); + } const char* signature = env->GetStringUTFChars(jSignature, NULL); const char* tx_data_hash = env->GetStringUTFChars(jTxDataHash, NULL); int attribute = jAttribute; @@ -472,6 +576,10 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr JNIEnv* env, jclass, jobject jTransactionObj) { struct bcos_sdk_c_transaction* tx_struct = convert_to_tx_struct(env, jTransactionObj); + if (tx_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct"); + } const char* tx_hex = bcos_sdk_encode_transaction_struct(tx_struct); if (!bcos_sdk_is_last_opr_success()) { @@ -504,6 +612,10 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr JNIEnv* env, jclass, jobject jTransactionObj) { struct bcos_sdk_c_transaction* tx_struct = convert_to_tx_struct(env, jTransactionObj); + if (tx_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct"); + } const char* tx_json = bcos_sdk_encode_transaction_struct_to_json(tx_struct); if (!bcos_sdk_is_last_opr_success()) { diff --git a/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java b/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java index 1a49588c5..81c8f77d0 100644 --- a/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java +++ b/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java @@ -97,7 +97,7 @@ public static void main(String[] args) throws JniException, InterruptedException Usage(); } - String endpoint = "127.0.0.1:22200"; + String endpoint = "127.0.0.1:20200"; String group = "group0"; String node = ""; JniConfig jniConfig = Utility.newJniConfig(Arrays.asList(endpoint)); @@ -107,7 +107,7 @@ public static void main(String[] args) throws JniException, InterruptedException System.out.println("build Rpc"); rpcJniObj.start(); - boolean smCrypto = true; + boolean smCrypto = false; long keyPair = KeyPairJniObj.createJniKeyPair(smCrypto ? 1 : 0); String jniKeyPairAddress = KeyPairJniObj.getJniKeyPairAddress(keyPair); diff --git a/sample/tx/CMakeLists.txt b/sample/tx/CMakeLists.txt index 4b6e1352f..f6a8783e0 100644 --- a/sample/tx/CMakeLists.txt +++ b/sample/tx/CMakeLists.txt @@ -7,5 +7,8 @@ target_link_libraries(hello_sample_hsm PUBLIC ${BCOS_C_SDK_STATIC_TARGET}) add_executable(tx_struct_test tx_struct_test.c) target_link_libraries(tx_struct_test PUBLIC ${BCOS_C_SDK_STATIC_TARGET}) +add_executable(tx_struct_v2_test tx_struct_v2_test.c) +target_link_libraries(tx_struct_v2_test PUBLIC ${BCOS_C_SDK_STATIC_TARGET}) + # add_executable(tx_sign_perf tx_sign_perf.cpp) # target_link_libraries(tx_sign_perf PUBLIC ${BCOS_C_SDK_STATIC_TARGET}) \ No newline at end of file diff --git a/sample/tx/tx_struct_v2_test.c b/sample/tx/tx_struct_v2_test.c new file mode 100644 index 000000000..6cfea678f --- /dev/null +++ b/sample/tx/tx_struct_v2_test.c @@ -0,0 +1,479 @@ +/* + * Copyright (C) 2021 FISCO BCOS. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @file tx_struct_v2_test.c + * @author: lucasli + * @date 2023-11-29 + */ +#include "bcos-c-sdk/bcos_sdk_c_error.h" +#include "bcos-c-sdk/bcos_sdk_c_rpc.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_abi.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_keypair.h" +#include +#include +#include +#include +#include +#include + + +#ifdef _WIN32 +#include +#else +#include +#endif + +//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ + +// HelloWorld Source Code: + +// HelloWorld Source Code: +/** +pragma solidity>=0.4.24 <0.6.11; + +contract HelloWorld { + string name; + + constructor() public { + name = "Hello, World!"; + } + + function get() public view returns (string memory) { + return name; + } + + function set(string memory n) public { + name = n; + } +} +*/ +const char* g_hw_bin = + "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f72" + "6c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50" + "610107565b828054600181600116156101000203166002900490600052602060002090601f01602090048101928260" + "1f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100" + "d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b8082" + "11156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe60806040" + "5234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c1461" + "00f6575b600080fd5b6100f46004803603602081101561005157600080fd5b81019080803590602001906401000000" + "0081111561006e57600080fd5b82018360208201111561008057600080fd5b80359060200191846001830284011164" + "0100000000831117156100a257600080fd5b91908080601f0160208091040260200160405190810160405280939291" + "90818152602001838380828437600081840152601f19601f8201169050808301925050505050505091929192905050" + "50610179565b005b6100fe610193565b60405180806020018281038252838181518152602001915080519060200190" + "80838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f1680" + "1561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b8060" + "00908051906020019061018f929190610235565b5050565b6060600080546001816001161561010002031660029004" + "80601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203" + "1660029004801561022b5780601f106102005761010080835404028352916020019161022b565b8201919060005260" + "20600020905b81548152906001019060200180831161020e57829003601f168201915b5050505050905090565b8280" + "54600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102765780" + "5160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591" + "602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760" + "008160009055506001016102bb565b5090565b9056fea2646970667358221220b5943f43c48cc93c6d71cdcf27aee5" + "072566c88755ce9186e32ce83b24e8dc6c64736f6c634300060a0033"; + +const char* g_hw_sm_bin = + "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f72" + "6c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50" + "610107565b828054600181600116156101000203166002900490600052602060002090601f01602090048101928260" + "1f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100" + "d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b8082" + "11156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe60806040" + "5234801561001057600080fd5b50600436106100365760003560e01c8063299f7f9d1461003b5780633590b49f1461" + "00be575b600080fd5b610043610179565b604051808060200182810382528381815181526020019150805190602001" + "9080838360005b83811015610083578082015181840152602081019050610068565b50505050905090810190601f16" + "80156100b05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b61" + "0177600480360360208110156100d457600080fd5b81019080803590602001906401000000008111156100f1576000" + "80fd5b82018360208201111561010357600080fd5b8035906020019184600183028401116401000000008311171561" + "012557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380" + "828437600081840152601f19601f82011690508083019250505050505050919291929050505061021b565b005b6060" + "60008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190" + "818152602001828054600181600116156101000203166002900480156102115780601f106101e65761010080835404" + "0283529160200191610211565b820191906000526020600020905b8154815290600101906020018083116101f45782" + "9003601f168201915b5050505050905090565b8060009080519060200190610231929190610235565b5050565b8280" + "54600181600116156101000203166002900490600052602060002090601f016020900481019282601f106102765780" + "5160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591" + "602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760" + "008160009055506001016102bb565b5090565b9056fea26469706673582212209871cb2bcf390d53645807cbaedfe0" + "52d739ef9cff9d84787f74c4f379e1854664736f6c634300060a0033"; + +const char* g_hw_abi = + "[{\"inputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"inputs\":[]," + "\"name\":\"get\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}]" + ",\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":" + "\"string\",\"name\":\"n\",\"type\":\"string\"}],\"name\":\"set\",\"outputs\":[]," + "\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]"; + +/* +{ + "6d4ce63c": "get()", + "4ed3885e": "set(string)" +} + +{ + "299f7f9d": "get()", + "3590b49f": "set(string)" +} +*/ + +void usage(void) +{ + printf("Desc: tx struct v2 test sample\n"); + printf("Usage: tx_struct_v2_test \n"); + printf("Example:\n"); + printf(" ./tx_struct_v2_test ./config.ini group0\n"); + exit(0); +} + +int convertCharToHexNumber(char hexChar) { + if (hexChar >= '0' && hexChar <= '9') + return hexChar - '0'; + if (hexChar >= 'a' && hexChar <= 'f') + return hexChar - 'a' + 10; + if (hexChar >= 'A' && hexChar <= 'F') + return hexChar - 'A' + 10; + return -1; +} + +struct bcos_sdk_c_bytes* fromHexString(const char* hexedString) { + unsigned startIndex = (strlen(hexedString) >= 2 && hexedString[0] == '0' && hexedString[1] == 'x') ? 2 : 0; + struct bcos_sdk_c_bytes* bytesData = (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); + bytesData->buffer = (uint8_t*)malloc((strlen(hexedString) - startIndex + 1) / 2); + bytesData->length = 0; + + if (strlen(hexedString) % 2) { + int h = convertCharToHexNumber(hexedString[startIndex++]); + if (h == -1) { + // Handle error + free(bytesData->buffer); + free(bytesData); + return NULL; + } + bytesData->buffer[bytesData->length++] = (uint8_t)h; + } + for (unsigned i = startIndex; i < strlen(hexedString); i += 2) { + int highValue = convertCharToHexNumber(hexedString[i]); + int lowValue = convertCharToHexNumber(hexedString[i + 1]); + if (highValue == -1 || lowValue == -1) { + // Handle error + free(bytesData->buffer); + free(bytesData); + return NULL; + } + bytesData->buffer[bytesData->length++] = (uint8_t)((highValue << 4) + lowValue); + } + return bytesData; +} + +// contract address +char* contract_address = NULL; + +// callback for rpc interfaces +void on_deploy_resp_callback(struct bcos_sdk_c_struct_response* resp) +{ + if (resp->error != BCOS_SDK_C_SUCCESS) + { + printf("\t deploy contract failed, error: %d, message: %s\n", resp->error, resp->desc); + exit(-1); + } + + const char* cflag = "contractAddress\" : \""; + // find the "contractAddress": "0xxxxx" + char* p0 = strstr((char*)resp->data, cflag); + if (p0 == NULL) + { + printf("\t cannot find the \"contractAddress\" filed, resp: %s\n", (char*)resp->data); + exit(-1); + } + + char* p1 = (char*)p0 + strlen(cflag); + char* p2 = strstr(p1, "\""); + if (p2 == NULL) + { + printf("\t cannot find the \"contractAddress\" filed, resp: %s\n", (char*)resp->data); + exit(-1); + } + + contract_address = (char*)malloc(p2 - p1 + 1); + + memcpy(contract_address, p1, p2 - p1); + contract_address[p2 - p1] = '\0'; + + printf(" [TxStructV2Test] contractAddress ===>>>>: %s\n", contract_address); + printf(" [TxStructV2Test] transaction receipt ===>>>>: %s\n", (char*)resp->data); +} + +/* resp->data 的数据结构 +{ + "id" : 2, + "jsonrpc" : "2.0", + "result" : + { + "blockNumber" : 113, + "checksumContractAddress" : "", + "contractAddress" : "", + "extraData" : "ExtraData", + "from" : "0x69df04bec1c36551be6298f7e4c2f867592a4b37", + "gasUsed" : "13063", + "hash" : "0x7d816bbde4aef3bd4c084b0887982f2c50cb9a50975a4d07328ec5fd5dd4e6e6", + "input" : "0x3590b49f0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001748656c6c6f20464953434f2d42434f5320332e30212121000000000000000000", + "logEntries" : [], + "message" : "", + "output" : "0x", + "status" : 0, + "to" : "0xcd6787f79a8da1b4a607dd59c79c7c08209230c1", + "transactionHash" : "0x05424cb9f92e1f1cd9b7ebe7a7e86d628d71c67edc6cf9cadac1967d279f8017", + "version" : 0 + } +} +*/ +void on_send_tx_resp_callback(struct bcos_sdk_c_struct_response* resp) +{ + if (resp->error != BCOS_SDK_C_SUCCESS) + { + printf("\t send tx failed, error: %d, message: %s\n", resp->error, resp->desc); + exit(-1); + } + + printf(" ===>> send tx resp: %s\n", (char*)resp->data); +} + +void on_call_resp_callback(struct bcos_sdk_c_struct_response* resp) +{ + if (resp->error != BCOS_SDK_C_SUCCESS) + { + printf("\t call failed, error: %d, message: %s\n", resp->error, resp->desc); + exit(-1); + } + + printf(" ===>> call resp: %s\n", (char*)resp->data); +} + +int main(int argc, char** argv) +{ + if (argc < 3) + { + usage(); + } + + const char* config = argv[1]; + const char* group_id = argv[2]; + + printf("[TxStructV2Test] params ===>>>> \n"); + printf("\t # config: %s\n", config); + printf("\t # group_id: %s\n", group_id); + + // const char* version = bcos_sdk_version(); + // printf("\t # c-sdk version: \n%s", version); + // bcos_sdk_c_free((void*)version); + + // 1. create sdk object by config + void* sdk = bcos_sdk_create_by_config_file(config); + // check success or not + if (!bcos_sdk_is_last_opr_success()) + { + printf( + " bcos_sdk_create_by_config_file failed, error: %s\n", bcos_sdk_get_last_error_msg()); + exit(-1); + } + + printf(" [TxStructV2Test] start sdk ... \n"); + + // 2. start bcos c sdk + bcos_sdk_start(sdk); + if (!bcos_sdk_is_last_opr_success()) + { + printf(" [TxStructV2Test] bcos_sdk_start failed, error: %s\n", bcos_sdk_get_last_error_msg()); + exit(-1); + } + + int sm_crypto = 0; + int wasm = 0; + + // 3. get sm_crypto of the group_id + bcos_sdk_get_group_wasm_and_crypto(sdk, group_id, &wasm, &sm_crypto); + if (!bcos_sdk_is_last_opr_success()) + { + printf(" [CallHello] bcos_sdk_group_sm_crypto failed, error: %s\n", + bcos_sdk_get_last_error_msg()); + exit(-1); + } + + printf(" [TxStructV2Test] sm crypto: %d\n", sm_crypto); + // 4. get chain_id of the group_id + const char* chain_id = bcos_sdk_get_group_chain_id(sdk, group_id); + if (!bcos_sdk_is_last_opr_success()) + { + printf(" [TxStructV2Test] bcos_sdk_get_group_chain_id failed, error: %s\n", + bcos_sdk_get_last_error_msg()); + exit(-1); + } + + printf(" [TxStructV2Test] chain id: %s\n", chain_id); + // 5. get blocklimit of the group_id + int64_t block_limit = bcos_rpc_get_block_limit(sdk, group_id); + if (block_limit < 0) + { + printf(" [TxStructV2Test] group not exist, group: %s\n", group_id); + exit(-1); + } + + printf(" [TxStructV2Test] block limit: %d\n", (int32_t)block_limit); + // 6. load or create keypair for transaction sign + void* key_pair = bcos_sdk_create_keypair(sm_crypto); + if (!key_pair) + { + printf(" [TxStructV2Test] create keypair failed, error: %s\n", bcos_sdk_get_last_error_msg()); + exit(-1); + } + + // printf(" [TxStructV2Test] bcos_sdk_get_keypair_type: %d\n", + // bcos_sdk_get_keypair_type(key_pair)); + + // 7. get account address of the keypair + const char* address = bcos_sdk_get_keypair_address(key_pair); + printf(" [TxStructV2Test] new account, address: %s\n", address); + + const char* extra_data = "ExtraData"; + const char* value = "33"; + const char* gasPrice = "0"; + int64_t gasLimit = 0; + const char* maxFeePerGas = "11"; + const char* maxPriorityFeePerGas = "22"; + + // printf(" [TxStructV2Test] extra_data: %s, value: %s, gasPrice: %s, gasLimit: %ld, maxFeePerGas: %s, maxPriorityFeePerGas: %s\n", extra_data, value, gasPrice, gasLimit, maxFeePerGas, maxPriorityFeePerGas); + + // 8. deploy HelloWorld contract + struct bcos_sdk_c_transaction_data_v2* transaction_data_v2_deploy = bcos_sdk_create_transaction_data_struct_with_hex_input_v2( + group_id, chain_id, "", sm_crypto ? g_hw_sm_bin : g_hw_bin, "", block_limit, value, gasPrice, gasLimit, maxFeePerGas, maxPriorityFeePerGas); + const char* transaction_data_hash_deploy = + bcos_sdk_calc_transaction_data_struct_hash_v2(sm_crypto, transaction_data_v2_deploy); + const char* signed_hash_deploy = + bcos_sdk_sign_transaction_data_hash(key_pair, transaction_data_hash_deploy); + const char* signed_tx_deploy = bcos_sdk_create_encoded_transaction_v2( + transaction_data_v2_deploy, signed_hash_deploy, transaction_data_hash_deploy, 0, extra_data); + printf(" [TxStructV2Test] create deploy contract transaction success, signed_tx_deploy: %s\n", signed_tx_deploy); + + // call rpc interface, send transaction + bcos_rpc_send_transaction(sdk, group_id, "", signed_tx_deploy, 0, on_deploy_resp_callback, NULL); + // wait for async operation done, just for sample + sleep(5); + + printf(" [TxStructV2Test] set operation\n"); + // 9. HelloWorld set + // 9.1 abi encode params + const char* set_data = + bcos_sdk_abi_encode_method(g_hw_abi, "set", "[\"Hello FISCO-BCOS 3.0!!!\"]", sm_crypto); + // 9.2 create signed transaction + { + // 9.2.1 create transaction data with hex input + struct bcos_sdk_c_transaction_data_v2* transaction_data_v2 = bcos_sdk_create_transaction_data_struct_with_hex_input_v2( + group_id, chain_id, contract_address, set_data, g_hw_abi, block_limit, value, gasPrice, gasLimit, maxFeePerGas, maxPriorityFeePerGas); + // create transaction data with bytes input + struct bcos_sdk_c_bytes* input_bytes = fromHexString(set_data); + transaction_data_v2 = bcos_sdk_create_transaction_data_struct_with_bytes_v2( + group_id, chain_id, contract_address, input_bytes->buffer, input_bytes->length, g_hw_abi, block_limit, value, gasPrice, gasLimit, maxFeePerGas, maxPriorityFeePerGas); + + // 9.2.1.1 encode tx data to hex + const char* hex_tx_data = bcos_sdk_encode_transaction_data_struct_v2(transaction_data_v2); + printf(" [TxStructV2Test] tx_data_hex: %s\n", hex_tx_data); + // 9.2.1.2 decode hex tx data + struct bcos_sdk_c_transaction_data_v2* decode_tx_data = bcos_sdk_decode_transaction_data_struct_v2(hex_tx_data); + // 9.2.1.3 encode tx data to json + const char* json_tx_data = bcos_sdk_encode_transaction_data_struct_to_json_v2(decode_tx_data); + printf(" [TxStructV2Test] tx_data_json: %s\n", json_tx_data); + // 9.2.1.4 decode json to tx data struct + decode_tx_data = bcos_sdk_decode_transaction_data_struct_with_json_v2(json_tx_data); + + // 9.2.2 calc transaction data hash + const char* transaction_data_hash = + bcos_sdk_calc_transaction_data_struct_hash_v2(sm_crypto, decode_tx_data); + printf(" [TxStructV2Test] set tx hash: %s\n", transaction_data_hash); + transaction_data_hash = bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2(sm_crypto, hex_tx_data); + printf(" [TxStructV2Test] set tx hash with tx_data_hex: %s\n", transaction_data_hash); + + // 9.2.3 sign transaction hash + const char* signed_hash = + bcos_sdk_sign_transaction_data_hash(key_pair, transaction_data_hash); + + // 9.2.4 create signed transaction + const char* signed_tx = bcos_sdk_create_encoded_transaction_v2( + decode_tx_data, signed_hash, transaction_data_hash, 0, extra_data); + + // 9.2.4.1 create transaction struct + struct bcos_sdk_c_transaction_v2* transaction = bcos_sdk_create_transaction_struct_v2(decode_tx_data, + signed_hash, transaction_data_hash, 0, extra_data); + // 9.2.4.2 encode tx to hex + const char* hex_tx = bcos_sdk_encode_transaction_struct_v2(transaction); + printf(" [TxStructV2Test] tx_hex: %s\n", hex_tx); + // 9.2.4.3 decode hex to tx + struct bcos_sdk_c_transaction_v2* decode_tx = bcos_sdk_decode_transaction_struct_v2(hex_tx); + // 9.2.4.4 encode tx to json + const char* json_tx = bcos_sdk_encode_transaction_struct_to_json_v2(decode_tx); + printf(" [TxStructV2Test] tx_json: %s\n", json_tx); + // 9.2.4.5 decode json to tx + decode_tx = bcos_sdk_decode_transaction_struct_with_json_v2(json_tx); + const char* hex_tx2 = bcos_sdk_encode_transaction_struct_v2(decode_tx); + + printf(" [TxStructV2Test] signed_tx: %s\n", signed_tx); + printf(" [TxStructV2Test] hex_tx: %s\n", hex_tx); + printf(" [TxStructV2Test] hex_tx2: %s\n", hex_tx2); + + // 9.3 call rpc interface, sendTransaction + bcos_rpc_send_transaction(sdk, group_id, "", signed_tx, 0, on_send_tx_resp_callback, NULL); + + // wait for async operation done, just for sample + sleep(3); + + // free + if (input_bytes && input_bytes->buffer) + { + bcos_sdk_c_free(input_bytes->buffer); + } + bcos_sdk_destroy_transaction_data_struct_v2(transaction_data_v2); + bcos_sdk_destroy_transaction_data_struct_v2(decode_tx_data); + bcos_sdk_destroy_transaction_struct_v2(transaction); + bcos_sdk_destroy_transaction_struct_v2(decode_tx); + bcos_sdk_c_free((void*)transaction_data_hash); + bcos_sdk_c_free((void*)signed_hash); + bcos_sdk_c_free((void*)signed_tx); + bcos_sdk_c_free((void*)hex_tx_data); + bcos_sdk_c_free((void*)json_tx_data); + bcos_sdk_c_free((void*)hex_tx); + bcos_sdk_c_free((void*)json_tx); + } + + // wait for async operation done, just for sample + sleep(3); + + // free chain_id + bcos_sdk_c_free((void*)chain_id); + // free address + bcos_sdk_c_free((void*)address); + if (contract_address) + { + bcos_sdk_c_free((void*)contract_address); + } + + // stop sdk + bcos_sdk_stop(sdk); + // release sdk + bcos_sdk_destroy(sdk); + // release keypair + bcos_sdk_destroy_keypair(key_pair); + + return 0; +} From 4d32b6282d606609ec230fbcf288ddc3e7bc0b9c Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Fri, 15 Dec 2023 09:41:47 +0800 Subject: [PATCH 16/29] (build): fix boost uuid exception. (#194) --- CMakeLists.txt | 2 ++ test/testTransaction.cpp | 1 - vcpkg-configuration.json | 5 +++-- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cba6832f8..5ec2820e3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,6 +33,8 @@ if (WIN32) add_definitions(-D_WIN32_WINNT=0x0601) add_definitions(-DBOOST_USE_WINAPI_VERSION=BOOST_WINAPI_VERSION_WIN7) set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS "ON") +else () + add_definitions(-DBOOST_UUID_RANDOM_PROVIDER_FORCE_POSIX) endif () set(JAVA_SDK_JNI_CMAKE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/cmake" CACHE PATH "The cmake path for the library") diff --git a/test/testTransaction.cpp b/test/testTransaction.cpp index 45a553608..038d6cc42 100644 --- a/test/testTransaction.cpp +++ b/test/testTransaction.cpp @@ -34,7 +34,6 @@ struct TestTxFixture }; BOOST_FIXTURE_TEST_SUITE(TransactionTest, TestTxFixture) - const char* g_hw_abi = "[{\"inputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"inputs\":[]," "\"name\":\"get\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"\",\"type\":\"string\"}]" diff --git a/vcpkg-configuration.json b/vcpkg-configuration.json index 446f88340..799132798 100644 --- a/vcpkg-configuration.json +++ b/vcpkg-configuration.json @@ -3,13 +3,14 @@ { "kind": "git", "repository": "https://github.com/FISCO-BCOS/registry", - "baseline": "c15466a352b8daede8d2c447670a761ad736c132", + "baseline": "ffebe39f161b78791148924f18ed0c9d4d4bce71", "packages": [ "openssl", "hsm-crypto", "fisco-bcos-cpp-sdk", "wedprcrypto", - "secp256k1" + "secp256k1", + "boost-uuid" ] } ] From ad069eec4b7f6e32b8c63555b5b377ec494b205e Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Tue, 19 Dec 2023 18:32:45 +0800 Subject: [PATCH 17/29] (sdk): add bcos sdk static get protocol info interface. (#196) --- .../java/org/fisco/bcos/sdk/jni/BcosSDKJniObj.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/BcosSDKJniObj.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/BcosSDKJniObj.java index 4f48ae79c..e356a6ba2 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/BcosSDKJniObj.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/BcosSDKJniObj.java @@ -42,7 +42,7 @@ public static BcosSDKJniObj build(JniConfig jniConfig) throws JniException { } /** - * desctroy interface + * destroy interface * * @param nativePointer */ @@ -64,6 +64,18 @@ private void setNativePointer(long nativePointer) { this.nativePointer = nativePointer; } + public static int localProtocolInfo(long nativePointer) { + BcosSDKJniObj bcosSDKJniObj = new BcosSDKJniObj(); + bcosSDKJniObj.setNativePointer(nativePointer); + return bcosSDKJniObj.localProtocolInfo(); + } + + public static int negotiatedProtocolInfo(long nativePointer) { + BcosSDKJniObj bcosSDKJniObj = new BcosSDKJniObj(); + bcosSDKJniObj.setNativePointer(nativePointer); + return bcosSDKJniObj.negotiatedProtocolInfo(); + } + // ------------------------native method list------------------------------------------- public static native long create(JniConfig config); From 600b953df0a52ad184f7e741b52e32af88e43625 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Tue, 2 Jan 2024 20:46:05 +0800 Subject: [PATCH 18/29] (utilities): fix boost beast compile error in clang16, add decode tx logic. (#201) --- .gitignore | 2 +- bcos-c-sdk/bcos_sdk_c_uti_tx.cpp | 65 +++++++++++++ bcos-c-sdk/bcos_sdk_c_uti_tx.h | 22 +++++ .../main/c/jni/org_fisco_bcos_sdk_common.h | 23 ++++- .../org_fisco_bcos_sdk_jni_BcosSDKJniObj.cpp | 3 +- ...org_fisco_bcos_sdk_jni_amop_AmopJniObj.cpp | 20 ++-- ...isco_bcos_sdk_jni_event_EventSubJniObj.cpp | 6 +- .../org_fisco_bcos_sdk_jni_rpc_RpcJniObj.cpp | 66 ++++++------- ...isco_bcos_sdk_jni_rpc_RpcServiceJniObj.cpp | 4 +- ...dk_jni_utilities_keypair_KeyPairJniObj.cpp | 33 ++++--- ...utilities_receipt_ReceiptBuilderJniObj.cpp | 10 +- ...ni_utilities_signature_SignatureJniObj.cpp | 11 ++- ..._utilities_tx_TransactionBuilderJniObj.cpp | 94 +++++++++++++++---- ...ni_utilities_tx_TransactionBuilderJniObj.h | 37 +++++++- ...tilities_tx_TransactionBuilderV2JniObj.cpp | 46 ++++----- .../tx/TransactionBuilderJniObj.java | 19 ++++ test/testTransaction.cpp | 15 +++ vcpkg-configuration.json | 5 +- 18 files changed, 358 insertions(+), 123 deletions(-) diff --git a/.gitignore b/.gitignore index 0feed158d..5a642fc72 100644 --- a/.gitignore +++ b/.gitignore @@ -36,7 +36,7 @@ deps *.out *.app .cache -build +build** deps .gradle .vscode/ diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx.cpp b/bcos-c-sdk/bcos_sdk_c_uti_tx.cpp index e7c6a6c43..c0b4dc87e 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx.cpp +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx.cpp @@ -373,6 +373,71 @@ const char* bcos_sdk_create_signed_transaction_with_signed_data_ver_extra_data( return NULL; } +/** + * @brief Decode transaction hex string to Transaction object raw pointer + * @param transaction_bytes encoded transaction hex string + * @return Transaction object pointer + */ +void* bcos_sdk_decode_transaction(const char* transaction_bytes) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_bytes, nullptr); + try + { + TransactionBuilder builder{}; + auto transaction = builder.decodeTransaction(*fromHexString(transaction_bytes)); + return transaction.release(); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction") << LOG_DESC("exception") + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + return nullptr; +} + +/** + * @brief Decode transaction hex string to Transaction object json string + * @param transaction_bytes encoded transaction hex string + * @return Transaction object json string + */ +const char* bcos_sdk_decode_transaction_to_json_obj(const char* transaction_bytes) +{ + bcos_sdk_clear_last_error(); + BCOS_SDK_C_PARAMS_VERIFICATION(transaction_bytes, nullptr); + try + { + TransactionBuilder builder{}; + const auto transaction = + builder.decodeTransactionToJsonObj(*fromHexString(transaction_bytes)); + return strdup(transaction.c_str()); + } + catch (const std::exception& e) + { + std::string errorMsg = boost::diagnostic_information(e); + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction") << LOG_DESC("exception") + << LOG_KV("error", errorMsg); + bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); + } + return nullptr; +} + +/** + * @brief Destroy transaction object + * + * @param transaction transaction object pointer + */ +void bcos_sdk_destroy_transaction(void* transaction) +{ + if (transaction) + { + delete (bcostars::Transaction*)transaction; + transaction = nullptr; + } +} + /** * @brief * diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx.h b/bcos-c-sdk/bcos_sdk_c_uti_tx.h index d86372716..8cb3f55a8 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx.h +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx.h @@ -161,6 +161,28 @@ const char* bcos_sdk_create_signed_transaction_with_signed_data_ver_extra_data( void* transaction_data, const char* signature, const char* transaction_data_hash, int32_t attribute, const char* extra_data); + +/** + * @brief Decode transaction hex string to Transaction object raw pointer + * @param transaction_bytes encoded transaction hex string + * @return Transaction object pointer + */ +void* bcos_sdk_decode_transaction(const char* transaction_bytes); + +/** + * @brief Decode transaction hex string to Transaction object json string + * @param transaction_bytes encoded transaction hex string + * @return Transaction object json string + */ +const char* bcos_sdk_decode_transaction_to_json_obj(const char* transaction_bytes); + +/** + * @brief Destroy transaction object + * + * @param transaction transaction object pointer + */ +void bcos_sdk_destroy_transaction(void* transaction); + /** * @brief * diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_common.h b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_common.h index c154028db..acc0b026c 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_common.h +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_common.h @@ -9,10 +9,21 @@ extern "C" { #endif -#define GET_J_STRING_CONTENT(env, jstr) \ - (env)->GetStringUTFChars((jstr), NULL) +#define GET_J_STRING_CONTENT(env, jstr) (env)->GetStringUTFChars((jstr), NULL) #define GET_J_STRING_CONTENT_DEF(env, jstr, def) \ ((jstr) == NULL ? (def) : GET_J_STRING_CONTENT((env), (jstr))) +#define CHECK_OBJECT_NOT_NULL(env, obj, ret) \ + if (NULL == (obj)) \ + { \ + THROW_JNI_EXCEPTION((env), "illegal NULL " #obj " parameter"); \ + return (ret); \ + } +#define CHECK_OBJECT_NOT_NULL_RET_VOID(env, obj) \ + if (NULL == (obj)) \ + { \ + THROW_JNI_EXCEPTION((env), "illegal NULL " #obj " parameter"); \ + return; \ + } struct cb_context { @@ -27,20 +38,24 @@ jclass bcos_sdk_c_find_jclass(JNIEnv* env, const char* className); struct bcos_sdk_c_config* create_config_from_java_obj(JNIEnv* env, jobject jconfig); // check jstring not null -inline void checkJString(JNIEnv* env, jstring jstr) +inline bool checkJString(JNIEnv* env, jstring jstr) { if (jstr == NULL) { THROW_JNI_EXCEPTION(env, "illegal NULL string parameter"); + return false; } + return true; } // check jByteArray not null -inline void checkJByteArray(JNIEnv* env, jbyteArray jbyte_array) +inline bool checkJByteArray(JNIEnv* env, jbyteArray jbyte_array) { if (jbyte_array == NULL) { THROW_JNI_EXCEPTION(env, "illegal NULL byteArray parameter"); + return false; } + return true; } #ifdef __cplusplus diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.cpp index 2dde68587..20db8c175 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.cpp @@ -14,6 +14,7 @@ JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_create( JNIEnv* env, jclass self, jobject jconfig) { + CHECK_OBJECT_NOT_NULL(env, jconfig, 0); (void)self; // config struct bcos_sdk_c_config* config = create_config_from_java_obj(env, jconfig); @@ -163,7 +164,7 @@ JNIEXPORT jint JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_negotiatedProto JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_registerBlockNotifier( JNIEnv* env, jobject self, jstring jgroup, jobject jcallback) { - checkJString(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); void* sdk = bcos_sdk_get_native_pointer(env, self); if (!sdk) diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_amop_AmopJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_amop_AmopJniObj.cpp index 1dbaeceae..8062d204c 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_amop_AmopJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_amop_AmopJniObj.cpp @@ -181,7 +181,7 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_subscribeTopic__Ljava_lang_String_2Lorg_fisco_bcos_sdk_jni_amop_AmopRequestCallback_2( JNIEnv* env, jobject self, jstring jtopic, jobject jcallback) { - checkJString(env, jtopic); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jtopic); void* sdk = bcos_sdk_get_native_pointer(env, self); @@ -214,6 +214,7 @@ Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_subscribeTopic__Ljava_lang_String_2L JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_subscribeTopic__Ljava_util_Set_2( JNIEnv* env, jobject self, jobject jtopics) { + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jtopics); void* sdk = bcos_sdk_get_native_pointer(env, self); jclass setClass = env->GetObjectClass(jtopics); @@ -263,7 +264,7 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_subscribeTopic__Ljava_lang_String_2Lorg_fisco_bcos_sdk_jni_amop_AmopJniObjRequestCallback_2( JNIEnv* env, jobject self, jstring jtopic, jobject jcallback) { - checkJString(env, jtopic); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jtopic); void* sdk = bcos_sdk_get_native_pointer(env, self); const char* topic = env->GetStringUTFChars(jtopic, 0); @@ -295,6 +296,7 @@ Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_subscribeTopic__Ljava_lang_String_2L JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_unsubscribeTopic( JNIEnv* env, jobject self, jobject jtopics) { + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jtopics); void* sdk = bcos_sdk_get_native_pointer(env, self); jclass setClass = env->GetObjectClass(jtopics); @@ -366,8 +368,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_setCallback( JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_sendAmopMsg( JNIEnv* env, jobject self, jstring jtopic, jbyteArray jdata, jint jtimeout, jobject jcallback) { - checkJString(env, jtopic); - checkJByteArray(env, jdata); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jtopic); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jdata); void* sdk = bcos_sdk_get_native_pointer(env, self); // Note: The JNIEnv pointer, passed as the first argument to every native method, can only be @@ -404,8 +406,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_sendAmopMsg( JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_broadcastAmopMsg( JNIEnv* env, jobject self, jstring jtopic, jbyteArray jdata) { - checkJString(env, jtopic); - checkJByteArray(env, jdata); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jtopic); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jdata); void* sdk = bcos_sdk_get_native_pointer(env, self); const char* topic = env->GetStringUTFChars(jtopic, 0); @@ -426,9 +428,9 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_broadcastAmop JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_amop_AmopJniObj_sendResponse( JNIEnv* env, jobject self, jstring jendpoint, jstring jseq, jbyteArray jdata) { - checkJString(env, jendpoint); - checkJString(env, jseq); - checkJByteArray(env, jdata); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jendpoint); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jseq); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jdata); void* sdk = bcos_sdk_get_native_pointer(env, self); const char* endpoint = env->GetStringUTFChars(jendpoint, 0); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_event_EventSubJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_event_EventSubJniObj.cpp index 2ccf4a1f3..474885e22 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_event_EventSubJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_event_EventSubJniObj.cpp @@ -130,8 +130,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_event_EventSubJniObj_stop( JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_event_EventSubJniObj_subscribeEvent( JNIEnv* env, jobject self, jstring jgroup, jstring jparams, jobject jcallback) { - checkJString(env, jgroup); - checkJString(env, jparams); + CHECK_OBJECT_NOT_NULL(env, jgroup, NULL); + CHECK_OBJECT_NOT_NULL(env, jparams, NULL); void* sdk = bcos_sdk_get_native_pointer(env, self); const char* group = env->GetStringUTFChars(jgroup, 0); @@ -171,7 +171,7 @@ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_event_EventSubJniObj_subsc JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_event_EventSubJniObj_unsubscribeEvent( JNIEnv* env, jobject self, jstring jeventId) { - checkJString(env, jeventId); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jeventId); void* sdk = bcos_sdk_get_native_pointer(env, self); const char* eventid = env->GetStringUTFChars(jeventId, 0); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcJniObj.cpp index 4998a452e..cb5f1b546 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcJniObj.cpp @@ -151,7 +151,7 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_genericMethod__Ljava_lang_String_2Lorg_fisco_bcos_sdk_jni_rpc_RpcCallback_2( JNIEnv* env, jobject self, jstring jdata, jobject jcallback) { - checkJString(env, jdata); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jdata); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); @@ -184,8 +184,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_genericMethod__Ljava_lang_String_2Ljava_lang_String_2Lorg_fisco_bcos_sdk_jni_rpc_RpcCallback_2( JNIEnv* env, jobject self, jstring jgroup, jstring jdata, jobject jcallback) { - checkJString(env, jgroup); - checkJString(env, jdata); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jdata); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); @@ -223,8 +223,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_genericMethod__Ljava_lang_String_2Ljava_lang_String_2Ljava_lang_String_2Lorg_fisco_bcos_sdk_jni_rpc_RpcCallback_2( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jstring jdata, jobject jcallback) { - checkJString(env, jgroup); - checkJString(env, jdata); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jdata); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); @@ -265,9 +265,9 @@ Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_genericMethod__Ljava_lang_String_2Ljav JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_call(JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jstring jto, jstring jdata, jobject callback) { - checkJString(env, jgroup); - checkJString(env, jto); - checkJString(env, jdata); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jto); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jdata); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group @@ -309,8 +309,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_call(JNIEnv* en JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_sendTransaction(JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jstring jdata, jboolean jproof, jobject callback) { - checkJString(env, jgroup); - checkJString(env, jdata); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jdata); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); @@ -353,8 +353,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getTransaction( jobject self, jstring jgroup, jstring jnode, jstring jtx_hash, jboolean jproof, jobject callback) { - checkJString(env, jgroup); - checkJString(env, jtx_hash); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jtx_hash); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group @@ -396,8 +396,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getTransactionR jobject self, jstring jgroup, jstring jnode, jstring jtx_hash, jboolean jproof, jobject callback) { - checkJString(env, jgroup); - checkJString(env, jtx_hash); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jtx_hash); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group @@ -440,8 +440,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getBlockByHash( jobject self, jstring jgroup, jstring jnode, jstring jblock_hash, jboolean jonly_header, jboolean jonly_txhash, jobject callback) { - checkJString(env, jgroup); - checkJString(env, jblock_hash); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jblock_hash); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group @@ -485,7 +485,7 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getBlockByNumbe jobject self, jstring jgroup, jstring jnode, jlong jnumber, jboolean jonly_header, jboolean jonly_txhash, jobject callback) { - checkJString(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group @@ -527,7 +527,7 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getBlockByNumbe JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getBlockHashByNumber( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jlong jnumber, jobject callback) { - checkJString(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); @@ -566,7 +566,7 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getBlockHashByN JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getBlockLimit( JNIEnv* env, jobject self, jstring jgroup) { - checkJString(env, jgroup); + CHECK_OBJECT_NOT_NULL(env, jgroup, 0); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); @@ -586,7 +586,7 @@ JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getBlockLimit( JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getBlockNumber( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jobject callback) { - checkJString(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); @@ -623,8 +623,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getBlockNumber( JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getCode( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jstring jaddress, jobject callback) { - checkJString(env, jgroup); - checkJString(env, jaddress); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jaddress); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group @@ -662,7 +662,7 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getCode( JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getSealerList( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jobject callback) { - checkJString(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); @@ -698,7 +698,7 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getSealerList( JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getObserverList( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jobject callback) { - checkJString(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); @@ -734,7 +734,7 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getObserverList JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getPbftView( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jobject callback) { - checkJString(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); @@ -769,7 +769,7 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getPbftView( JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getPendingTxSize( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jobject callback) { - checkJString(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); @@ -805,7 +805,7 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getPendingTxSiz JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getSyncStatus( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jobject callback) { - checkJString(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); @@ -841,8 +841,8 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getSyncStatus( JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getSystemConfigByKey( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jstring jkey, jobject callback) { - checkJString(env, jgroup); - checkJString(env, jkey); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jkey); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); // group @@ -880,7 +880,7 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getSystemConfig JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getTotalTransactionCount( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jobject callback) { - checkJString(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); @@ -916,7 +916,7 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getTotalTransac JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getGroupPeers( JNIEnv* env, jobject self, jstring jgroup, jobject callback) { - checkJString(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); @@ -1003,7 +1003,7 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getGroupList( JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getGroupInfo( JNIEnv* env, jobject self, jstring jgroup, jobject callback) { - checkJString(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); @@ -1063,7 +1063,7 @@ JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getGroupInfoLis JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcJniObj_getGroupNodeInfo( JNIEnv* env, jobject self, jstring jgroup, jstring jnode, jobject callback) { - checkJString(env, jgroup); + CHECK_OBJECT_NOT_NULL_RET_VOID(env, jgroup); // rpc obj handler void* rpc = bcos_sdk_get_native_pointer(env, self); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcServiceJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcServiceJniObj.cpp index bc94f3ff0..85038d7d5 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcServiceJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcServiceJniObj.cpp @@ -121,8 +121,8 @@ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcServiceJniObj_sendT JNIEnv* env, jclass, jlong jsdk, jlong jkeypair, jstring jgroup, jstring jnode, jstring jto, jbyteArray jdata, jstring jabi, jint jatti, jstring jextra_data, jobject jcallback) { - checkJString(env, jgroup); - checkJByteArray(env, jdata); + CHECK_OBJECT_NOT_NULL(env, jgroup, NULL); + CHECK_OBJECT_NOT_NULL(env, jdata, NULL); void* sdk = reinterpret_cast(jsdk); void* keypair = reinterpret_cast(jkeypair); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj.cpp index 36845332a..513bb04ba 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj.cpp @@ -1,8 +1,8 @@ #include "org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj.h" #include "bcos-c-sdk/bcos_sdk_c_error.h" #include "bcos-c-sdk/bcos_sdk_c_uti_keypair.h" -#include "org_fisco_bcos_sdk_exception.h" #include "org_fisco_bcos_sdk_common.h" +#include "org_fisco_bcos_sdk_exception.h" #include /* @@ -30,10 +30,11 @@ Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_createJniKeyPair__I( * Method: createHsmKeyPair * Signature: (Ljava/lang/String;)J */ -JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_createHsmKeyPair__Ljava_lang_String_2 - (JNIEnv* env, jclass, jstring jhsm_lib_path) - { - checkJString(env, jhsm_lib_path); +JNIEXPORT jlong JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_createHsmKeyPair__Ljava_lang_String_2( + JNIEnv* env, jclass, jstring jhsm_lib_path) +{ + CHECK_OBJECT_NOT_NULL(env, jhsm_lib_path, 0); const char* hsm_lib_path = env->GetStringUTFChars(jhsm_lib_path, NULL); void* keypair = bcos_sdk_create_hsm_keypair(hsm_lib_path); @@ -45,7 +46,7 @@ JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJni } return reinterpret_cast(keypair); - } +} /* * Class: org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj @@ -56,6 +57,7 @@ JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_createJniKeyPair__I_3B( JNIEnv* env, jclass, jint jcrypto_type, jbyteArray jdata) { + CHECK_OBJECT_NOT_NULL(env, jdata, 0); int crypto_type = (int)jcrypto_type; jbyte* data = (jbyte*)env->GetByteArrayElements(jdata, 0); jsize len = env->GetArrayLength(jdata); @@ -74,10 +76,12 @@ Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_createJniKeyPair__I_ * Method: createHsmKeyPair * Signature: ([BLjava/lang/String;)J */ -JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_createHsmKeyPair___3BLjava_lang_String_2 - (JNIEnv* env, jclass, jbyteArray jdata, jstring jhsm_lib_path) +JNIEXPORT jlong JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_createHsmKeyPair___3BLjava_lang_String_2( + JNIEnv* env, jclass, jbyteArray jdata, jstring jhsm_lib_path) { - checkJString(env, jhsm_lib_path); + CHECK_OBJECT_NOT_NULL(env, jhsm_lib_path, 0); + CHECK_OBJECT_NOT_NULL(env, jdata, 0); jbyte* data = (jbyte*)env->GetByteArrayElements(jdata, 0); const char* hsm_lib_path = env->GetStringUTFChars(jhsm_lib_path, NULL); jsize len = env->GetArrayLength(jdata); @@ -98,14 +102,15 @@ JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJni * Method: useHsmKeyPair * Signature: (ILjava/lang/String;Ljava/lang/String;)J */ -JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_useHsmKeyPair - (JNIEnv* env, jclass, jint jkey_index, jstring jpass_word, jstring jhsm_lib_path) +JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_useHsmKeyPair( + JNIEnv* env, jclass, jint jkey_index, jstring jpass_word, jstring jhsm_lib_path) { - checkJString(env, jpass_word); - checkJString(env, jhsm_lib_path); + CHECK_OBJECT_NOT_NULL(env, jpass_word, 0); + CHECK_OBJECT_NOT_NULL(env, jhsm_lib_path, 0); const char* password = env->GetStringUTFChars(jpass_word, NULL); const char* hsm_lib_path = env->GetStringUTFChars(jhsm_lib_path, NULL); - void* keypair = bcos_sdk_use_hsm_keypair_by_keyindex_and_password((unsigned int)jkey_index, password, hsm_lib_path); + void* keypair = bcos_sdk_use_hsm_keypair_by_keyindex_and_password( + (unsigned int)jkey_index, password, hsm_lib_path); env->ReleaseStringUTFChars(jpass_word, password); env->ReleaseStringUTFChars(jhsm_lib_path, hsm_lib_path); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj.cpp index 1a3c1d350..59c606436 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj.cpp @@ -16,9 +16,9 @@ Java_org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj_createReceipt jclass, jstring jgas_used, jstring jcontract_address, jstring jout_put, jlong jblock_number) { - checkJString(env, jgas_used); - checkJString(env, jcontract_address); - checkJString(env, jout_put); + CHECK_OBJECT_NOT_NULL(env, jgas_used, 0); + CHECK_OBJECT_NOT_NULL(env, jcontract_address, 0); + CHECK_OBJECT_NOT_NULL(env, jout_put, 0); // group id const char* gas_used = env->GetStringUTFChars(jgas_used, NULL); @@ -53,7 +53,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj_createReceipt JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj_createReceiptDataWithJson (JNIEnv *env, jclass, jstring jjson) { - checkJString(env, jjson); + CHECK_OBJECT_NOT_NULL(env, jjson, 0); const char* json = env->GetStringUTFChars(jjson, NULL); void* receipt_data = bcos_sdk_create_receipt_data_with_json(json); @@ -114,7 +114,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj_encodeReceipt JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj_decodeReceiptDataToJsonObj (JNIEnv* env, jclass, jstring jreceipt_bytes) { - checkJString(env, jreceipt_bytes); + CHECK_OBJECT_NOT_NULL(env, jreceipt_bytes, NULL); const char* receipt_data = env->GetStringUTFChars(jreceipt_bytes, NULL); const char* receipt_data_json = bcos_sdk_decode_receipt_data(receipt_data); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj.cpp index c45e817e3..400e28bde 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj.cpp @@ -14,8 +14,8 @@ JNIEXPORT jbyteArray JNICALL Java_org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj_sign( JNIEnv* env, jclass, jlong jkeypair, jstring jtx_data_hash, jstring jhsm_lib_path) { - checkJString(env, jtx_data_hash); - checkJString(env, jhsm_lib_path); + CHECK_OBJECT_NOT_NULL(env, jtx_data_hash, NULL); + CHECK_OBJECT_NOT_NULL(env, jhsm_lib_path, NULL); const char* hsm_lib_path = env->GetStringUTFChars(jhsm_lib_path, NULL); const char* tx_data_hash = env->GetStringUTFChars(jtx_data_hash, NULL); void* keypair = reinterpret_cast(jkeypair); @@ -51,9 +51,10 @@ JNIEXPORT jboolean JNICALL Java_org_fisco_bcos_sdk_jni_utilities_signature_Signa JNIEnv* env, jclass, jint jcrypto_type, jbyteArray jpub_key, jstring jtx_data_hash, jstring jsigned_data, jstring jhsm_lib_path) { - checkJString(env, jtx_data_hash); - checkJString(env, jsigned_data); - checkJString(env, jhsm_lib_path); + CHECK_OBJECT_NOT_NULL(env, jpub_key, false); + CHECK_OBJECT_NOT_NULL(env, jtx_data_hash, false); + CHECK_OBJECT_NOT_NULL(env, jsigned_data, false); + CHECK_OBJECT_NOT_NULL(env, jhsm_lib_path, false); int crypto_type = (int)jcrypto_type; jbyte* public_key = (jbyte*)env->GetByteArrayElements(jpub_key, 0); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj.cpp index e9a1a20f8..16da58014 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj.cpp @@ -17,9 +17,9 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createTransact jclass, jstring jgroup_id, jstring jchain_id, jstring jto, jstring jdata, jstring jabi, jlong jblock_limit) { - checkJString(env, jgroup_id); - checkJString(env, jchain_id); - checkJString(env, jdata); + CHECK_OBJECT_NOT_NULL(env, jgroup_id, 0); + CHECK_OBJECT_NOT_NULL(env, jchain_id, 0); + CHECK_OBJECT_NOT_NULL(env, jdata, 0); // group id const char* group_id = env->GetStringUTFChars(jgroup_id, NULL); @@ -60,7 +60,7 @@ JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createTransactionDataWithJson( JNIEnv* env, jclass, jstring jjson) { - checkJString(env, jjson); + CHECK_OBJECT_NOT_NULL(env, jjson, 0); const char* json = env->GetStringUTFChars(jjson, NULL); void* transaction_data = bcos_sdk_create_transaction_data_with_json(json); @@ -125,7 +125,7 @@ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_decodeTransactionDataToJsonObj( JNIEnv* env, jclass, jstring jtransaction_bytes) { - checkJString(env, jtransaction_bytes); + CHECK_OBJECT_NOT_NULL(env, jtransaction_bytes, NULL); const char* transaction_data = env->GetStringUTFChars(jtransaction_bytes, NULL); const char* transaction_data_json = bcos_sdk_decode_transaction_data(transaction_data); @@ -174,6 +174,65 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_calcTransactio return jtransaction_data_hash; } +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj + * Method: decodeTransaction + * Signature: (Ljava/lang/String;)J + */ +JNIEXPORT jlong JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_decodeTransaction( + JNIEnv* env, jclass, jstring jtransaction_bytes) +{ + CHECK_OBJECT_NOT_NULL(env, jtransaction_bytes, 0); + const char* transaction = env->GetStringUTFChars(jtransaction_bytes, NULL); + void* transaction_data = bcos_sdk_decode_transaction(transaction); + env->ReleaseStringUTFChars(jtransaction_bytes, transaction); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + } + return reinterpret_cast(transaction_data); +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj + * Method: decodeTransactionToJsonObj + * Signature: (Ljava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_decodeTransactionToJsonObj( + JNIEnv* env, jclass, jstring jtransaction_bytes) +{ + CHECK_OBJECT_NOT_NULL(env, jtransaction_bytes, NULL); + const char* transaction = env->GetStringUTFChars(jtransaction_bytes, NULL); + const char* transaction_json = bcos_sdk_decode_transaction_to_json_obj(transaction); + env->ReleaseStringUTFChars(jtransaction_bytes, transaction); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + } + jstring jtransaction_json = env->NewStringUTF(transaction_json); + if (transaction_json) + { + free((void*)transaction_json); + transaction_json = NULL; + } + return jtransaction_json; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj + * Method: destroyTransaction + * Signature: (J)V + */ +JNIEXPORT void JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_destroyTransaction( + JNIEnv*, jclass, jlong jtransaction) +{ + void* transaction = reinterpret_cast(jtransaction); + bcos_sdk_destroy_transaction(transaction); +} + /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj * Method: signTransactionDataHash @@ -183,7 +242,7 @@ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_signTransactionDataHash( JNIEnv* env, jclass, jlong jkeypair, jstring jtransaction_data_hash) { - checkJString(env, jtransaction_data_hash); + CHECK_OBJECT_NOT_NULL(env, jtransaction_data_hash, NULL); void* keypair = reinterpret_cast(jkeypair); // transaction_data_hash @@ -219,8 +278,8 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createSignedTr JNIEnv* env, jclass, jlong jtransaction_data, jstring jtransaction_data_signed_data, jstring jtransaction_data_hash, jint jattr) { - checkJString(env, jtransaction_data_hash); - checkJString(env, jtransaction_data_signed_data); + CHECK_OBJECT_NOT_NULL(env, jtransaction_data_signed_data, NULL); + CHECK_OBJECT_NOT_NULL(env, jtransaction_data_hash, NULL); void* transaction_data = reinterpret_cast(jtransaction_data); const char* transaction_data_signed_data = @@ -261,9 +320,10 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createSignedTr JNIEnv* env, jclass, jlong jtransaction_data, jstring jtransaction_data_signed_data, jstring jtransaction_data_hash, jint jattr, jstring jextra_data) { - checkJString(env, jtransaction_data_hash); - checkJString(env, jtransaction_data_signed_data); - checkJString(env, jextra_data); + CHECK_OBJECT_NOT_NULL(env, jtransaction_data_signed_data, NULL); + CHECK_OBJECT_NOT_NULL(env, jtransaction_data_hash, NULL); + CHECK_OBJECT_NOT_NULL(env, jextra_data, NULL); + void* transaction_data = reinterpret_cast(jtransaction_data); const char* transaction_data_signed_data = @@ -307,9 +367,9 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createSignedTr JNIEnv* env, jclass, jlong jkeypair, jstring jgroup_id, jstring jchain_id, jstring jto, jstring jdata, jstring jabi, jlong jblock_limit, jint jattr) { - checkJString(env, jgroup_id); - checkJString(env, jchain_id); - checkJString(env, jdata); + CHECK_OBJECT_NOT_NULL(env, jgroup_id, NULL); + CHECK_OBJECT_NOT_NULL(env, jchain_id, NULL); + CHECK_OBJECT_NOT_NULL(env, jdata, NULL); // keypair void* keypair = reinterpret_cast(jkeypair); @@ -393,9 +453,9 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createSignedTr JNIEnv* env, jclass, jlong jkeypair, jstring jgroup_id, jstring jchain_id, jstring jto, jstring jdata, jstring jabi, jlong jblock_limit, jint jattr, jstring jextra_data) { - checkJString(env, jgroup_id); - checkJString(env, jchain_id); - checkJString(env, jdata); + CHECK_OBJECT_NOT_NULL(env, jgroup_id, NULL); + CHECK_OBJECT_NOT_NULL(env, jchain_id, NULL); + CHECK_OBJECT_NOT_NULL(env, jdata, NULL); // keypair void* keypair = reinterpret_cast(jkeypair); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj.h b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj.h index 9d584ddf5..cfff3a83f 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj.h +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj.h @@ -22,8 +22,9 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createTransact * Method: createTransactionDataWithJson * Signature: (Ljava/lang/String;)J */ -JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createTransactionDataWithJson - (JNIEnv *, jclass, jstring); +JNIEXPORT jlong JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createTransactionDataWithJson( + JNIEnv*, jclass, jstring); /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj @@ -48,8 +49,9 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_encodeTransact * Method: decodeTransactionDataToJsonObj * Signature: (Ljava/lang/String;)Ljava/lang/String; */ -JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_decodeTransactionDataToJsonObj - (JNIEnv *, jclass, jstring); +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_decodeTransactionDataToJsonObj( + JNIEnv*, jclass, jstring); /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj @@ -60,6 +62,33 @@ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_calcTransactionDataHash( JNIEnv*, jclass, jint, jlong); +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj + * Method: decodeTransaction + * Signature: (Ljava/lang/String;)J + */ +JNIEXPORT jlong JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_decodeTransaction( + JNIEnv*, jclass, jstring); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj + * Method: decodeTransactionToJsonObj + * Signature: (Ljava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_decodeTransactionToJsonObj( + JNIEnv*, jclass, jstring); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj + * Method: destroyTransaction + * Signature: (J)V + */ +JNIEXPORT void JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_destroyTransaction( + JNIEnv*, jclass, jlong); + /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj * Method: signTransactionDataHash diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp index d1579942b..ac54cf978 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp @@ -36,9 +36,9 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createTransa JNIEnv* env, jclass, jstring j_group_id, jstring j_chain_id, jstring j_to, jstring j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_gas_price, jlong j_gas_limit) { - checkJString(env, j_group_id); - checkJString(env, j_chain_id); - checkJString(env, j_input); + CHECK_OBJECT_NOT_NULL(env, j_group_id, 0); + CHECK_OBJECT_NOT_NULL(env, j_chain_id, 0); + CHECK_OBJECT_NOT_NULL(env, j_input, 0); const char* group_id = GET_J_STRING_CONTENT(env, j_group_id); const char* chain_id = GET_J_STRING_CONTENT(env, j_chain_id); @@ -81,9 +81,9 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createEIP155 jstring j_abi, jlong j_block_limit, jstring j_value, jlong j_gas_limit, jstring j_max_fee_per_gas, jstring j_max_priority_fee_per_gas) { - checkJString(env, j_group_id); - checkJString(env, j_chain_id); - checkJString(env, j_input); + CHECK_OBJECT_NOT_NULL(env, j_group_id, 0); + CHECK_OBJECT_NOT_NULL(env, j_chain_id, 0); + CHECK_OBJECT_NOT_NULL(env, j_input, 0); const char* group_id = GET_J_STRING_CONTENT(env, j_group_id); const char* chain_id = GET_J_STRING_CONTENT(env, j_chain_id); @@ -130,10 +130,10 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransact jlong j_block_limit, jstring j_value, jstring j_gasPrice, jlong j_gas_limit, jstring j_max_fee_per_gas, jstring j_max_priority_fee_per_gas) { - checkJString(env, j_group_id); - checkJString(env, j_chain_id); - checkJString(env, j_input); - checkJString(env, j_nonce); + CHECK_OBJECT_NOT_NULL(env, j_group_id, NULL); + CHECK_OBJECT_NOT_NULL(env, j_chain_id, NULL); + CHECK_OBJECT_NOT_NULL(env, j_input, NULL); + CHECK_OBJECT_NOT_NULL(env, j_nonce, NULL); const char* group_id = GET_J_STRING_CONTENT(env, j_group_id); const char* chain_id = GET_J_STRING_CONTENT(env, j_chain_id); @@ -194,7 +194,7 @@ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransactionDataHashWithJson( JNIEnv* env, jclass, jint j_crypto_type, jstring j_json) { - checkJString(env, j_json); + CHECK_OBJECT_NOT_NULL(env, j_json, NULL); const char* json = env->GetStringUTFChars(j_json, NULL); int crypto_type = (int)j_crypto_type; @@ -232,12 +232,12 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned jstring j_max_fee_per_gas, jstring j_max_priority_fee_per_gas, jint j_attribute, jstring j_extra_data) { - checkJString(env, j_signature); - checkJString(env, j_tx_hash); - checkJString(env, j_group_id); - checkJString(env, j_chain_id); - checkJString(env, j_input); - checkJString(env, j_nonce); + CHECK_OBJECT_NOT_NULL(env, j_signature, NULL); + CHECK_OBJECT_NOT_NULL(env, j_tx_hash, NULL); + CHECK_OBJECT_NOT_NULL(env, j_group_id, NULL); + CHECK_OBJECT_NOT_NULL(env, j_chain_id, NULL); + CHECK_OBJECT_NOT_NULL(env, j_input, NULL); + CHECK_OBJECT_NOT_NULL(env, j_nonce, NULL); int64_t block_limit = (int64_t)j_block_limit; int64_t gas_limit = (int64_t)j_gas_limit; @@ -307,9 +307,9 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned jstring j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_gas_price, jlong j_gas_limit, jint j_attribute, jstring j_extra_data) { - checkJString(env, j_group_id); - checkJString(env, j_chain_id); - checkJString(env, j_input); + CHECK_OBJECT_NOT_NULL(env, j_group_id, NULL); + CHECK_OBJECT_NOT_NULL(env, j_chain_id, NULL); + CHECK_OBJECT_NOT_NULL(env, j_input, NULL); void* keypair = reinterpret_cast(j_key_pair); int64_t block_limit = (int64_t)j_block_limit; @@ -392,9 +392,9 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned jstring j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_max_fee_per_gas, jstring j_max_priority_fee_per_gas, jlong j_gas_limit, jint j_attribute, jstring j_extra_data) { - checkJString(env, j_group_id); - checkJString(env, j_chain_id); - checkJString(env, j_input); + CHECK_OBJECT_NOT_NULL(env, j_group_id, NULL); + CHECK_OBJECT_NOT_NULL(env, j_chain_id, NULL); + CHECK_OBJECT_NOT_NULL(env, j_input, NULL); void* keypair = reinterpret_cast(j_key_pair); int64_t block_limit = (int64_t)j_block_limit; diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderJniObj.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderJniObj.java index 9c08eb509..d0bea21d9 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderJniObj.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderJniObj.java @@ -56,6 +56,25 @@ public static native String decodeTransactionDataToJsonObj(String transactionDat public static native String calcTransactionDataHash(int cryptoType, long transactionData) throws JniException; + /** + * Decode transaction hex string to Transaction object raw pointer + * + * @param transactionHex encoded transaction hex string + * @return Transaction object pointer + */ + public static native long decodeTransaction(String transactionHex) throws JniException; + + /** + * Decode transaction hex string to Transaction object json string + * + * @param transactionHex encoded transaction hex string + * @return Transaction object json string + */ + public static native String decodeTransactionToJsonObj(String transactionHex) throws JniException; + + /** @param transaction Transaction pointer */ + public static native void destroyTransaction(long transaction) throws JniException; + /** * @param keyPair KeyPair pointer * @param transactionDataHash transaction data hash hex string diff --git a/test/testTransaction.cpp b/test/testTransaction.cpp index 038d6cc42..f85afe626 100644 --- a/test/testTransaction.cpp +++ b/test/testTransaction.cpp @@ -24,6 +24,8 @@ #include "bcos-c-sdk/bcos_sdk_c_uti_keypair.h" #include "bcos-c-sdk/bcos_sdk_c_uti_tx.h" #include +#include + #include #include @@ -73,7 +75,20 @@ BOOST_AUTO_TEST_CASE(testTxData) success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == true); + void* txRaw = bcos_sdk_decode_transaction(signed_tx); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == true); + auto tx = (bcostars::Transaction*)txRaw; + BOOST_TEST(tx->extraData == extra_data); + BOOST_TEST(tx->data.version == 0); + BOOST_TEST(tx->data.blockLimit == block_limit); + BOOST_TEST(tx->data.groupID == group_id); + BOOST_TEST(tx->data.chainID == chain_id); + BOOST_TEST(tx->data.abi == g_hw_abi); + + bcos_sdk_destroy_transaction_data(transaction_data); + bcos_sdk_destroy_transaction(txRaw); bcos_sdk_c_free((void*)transaction_data_hash); bcos_sdk_c_free((void*)signed_hash); bcos_sdk_c_free((void*)signed_tx); diff --git a/vcpkg-configuration.json b/vcpkg-configuration.json index 799132798..22102b4ab 100644 --- a/vcpkg-configuration.json +++ b/vcpkg-configuration.json @@ -3,14 +3,15 @@ { "kind": "git", "repository": "https://github.com/FISCO-BCOS/registry", - "baseline": "ffebe39f161b78791148924f18ed0c9d4d4bce71", + "baseline": "3765dfb347d9fc820cfad9005b2f87d0755895ec", "packages": [ "openssl", "hsm-crypto", "fisco-bcos-cpp-sdk", "wedprcrypto", "secp256k1", - "boost-uuid" + "boost-uuid", + "boost-beast" ] } ] From 51ca6c37de966c5e6c0b9233fff2179ac420aba9 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Wed, 3 Jan 2024 21:05:21 +0800 Subject: [PATCH 19/29] (c-sdk,jni): change new interface input to bytes array, return default value when throw jni excetion. (#202) --- bcos-c-sdk/bcos_sdk_c_uti_tx_v2.cpp | 110 +++++++------- bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h | 69 +++++---- .../org_fisco_bcos_sdk_jni_BcosSDKJniObj.h | 24 +-- ...isco_bcos_sdk_jni_rpc_RpcServiceJniObj.cpp | 1 + ...dk_jni_utilities_keypair_KeyPairJniObj.cpp | 8 + ...utilities_receipt_ReceiptBuilderJniObj.cpp | 5 + ...ni_utilities_signature_SignatureJniObj.cpp | 2 + ..._utilities_tx_TransactionBuilderJniObj.cpp | 12 ++ ...tilities_tx_TransactionBuilderV2JniObj.cpp | 138 ++++++++++-------- ..._utilities_tx_TransactionBuilderV2JniObj.h | 58 +++++--- .../tx/TransactionBuilderV2JniObj.java | 16 +- 11 files changed, 266 insertions(+), 177 deletions(-) diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.cpp b/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.cpp index 3216e4921..07434dedc 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.cpp +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.cpp @@ -34,8 +34,8 @@ using namespace bcos::cppsdk::utilities; #include void* bcos_sdk_create_transaction_v2_data(const char* group_id, const char* chain_id, - const char* to, const char* input, const char* abi, int64_t block_limit, const char* value, - const char* gas_price, int64_t gas_limit) + const char* to, const unsigned char* input, long inputSize, const char* abi, + int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(group_id, NULL) @@ -47,9 +47,9 @@ void* bcos_sdk_create_transaction_v2_data(const char* group_id, const char* chai try { TransactionBuilderV2 builder; - auto bytesData = fromHexString(input); + auto bytesData = bytes(input, input + inputSize * sizeof(byte)); auto transactionData = - builder.createTransactionData(1, group_id, chain_id, to, "", *bytesData, abi, + builder.createTransactionData(1, group_id, chain_id, to, "", std::move(bytesData), abi, block_limit, value ? value : "", gas_price ? gas_price : "", gas_limit); return transactionData.release(); } @@ -71,8 +71,9 @@ void* bcos_sdk_create_transaction_v2_data(const char* group_id, const char* chai } void* bcos_sdk_create_eip1559_transaction_data(const char* group_id, const char* chain_id, - const char* to, const char* input, const char* abi, int64_t block_limit, const char* value, - int64_t gas_limit, const char* max_fee_per_gas, const char* max_priority_fee_per_gas) + const char* to, const unsigned char* input, long inputSize, const char* abi, + int64_t block_limit, const char* value, int64_t gas_limit, const char* max_fee_per_gas, + const char* max_priority_fee_per_gas) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(group_id, NULL) @@ -84,9 +85,9 @@ void* bcos_sdk_create_eip1559_transaction_data(const char* group_id, const char* try { TransactionBuilderV2 builder; - auto bytesData = fromHexString(input); + auto bytesData = bytes(input, input + inputSize * sizeof(byte)); auto transactionData = builder.createTransactionData(1, group_id, chain_id, to, "", - *bytesData, abi, block_limit, value ? value : "", "", gas_limit, + std::move(bytesData), abi, block_limit, value ? value : "", "", gas_limit, max_fee_per_gas ? max_fee_per_gas : "", max_priority_fee_per_gas ? max_priority_fee_per_gas : ""); return transactionData.release(); @@ -112,9 +113,9 @@ void* bcos_sdk_create_eip1559_transaction_data(const char* group_id, const char* const char* bcos_sdk_calc_transaction_data_hash_with_full_fields(int crypto_type, transaction_version version, const char* group_id, const char* chain_id, const char* to, - const char* nonce, const char* input, const char* abi, int64_t block_limit, const char* value, - const char* gas_price, int64_t gas_limit, const char* max_fee_per_gas, - const char* max_priority_fee_per_gas) + const char* nonce, const unsigned char* input, long inputSize, const char* abi, + int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit, + const char* max_fee_per_gas, const char* max_priority_fee_per_gas) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(group_id, NULL) @@ -126,12 +127,12 @@ const char* bcos_sdk_calc_transaction_data_hash_with_full_fields(int crypto_type try { - auto bytesData = fromHexString(input); + auto bytesData = bytes(input, input + inputSize * sizeof(byte)); TransactionBuilderV2 builder; auto transactionDataHash = builder.calculateTransactionDataHash( crypto_type == BCOS_C_SDK_ECDSA_TYPE ? CryptoType::Secp256K1 : CryptoType::SM2, version, - group_id, chain_id, to ? to : "", nonce, *bytesData, abi ? abi : "", block_limit, - value ? value : "", gas_price ? gas_price : "", gas_limit, + group_id, chain_id, to ? to : "", nonce, std::move(bytesData), abi ? abi : "", + block_limit, value ? value : "", gas_price ? gas_price : "", gas_limit, max_fee_per_gas ? max_fee_per_gas : "", max_priority_fee_per_gas ? max_priority_fee_per_gas : ""); return strdup(bcos::toHexStringWithPrefix(transactionDataHash).c_str()); @@ -180,12 +181,12 @@ const char* bcos_sdk_calc_transaction_data_hash_with_json(int crypto_type, const return nullptr; } -const char* bcos_sdk_create_signed_transaction_with_signature(const char* signature, - const char* transaction_hash, transaction_version version, const char* group_id, - const char* chain_id, const char* to, const char* nonce, const char* input, const char* abi, - int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit, - const char* max_fee_per_gas, const char* max_priority_fee_per_gas, int32_t attribute, - const char* extra_data) +const char* bcos_sdk_create_signed_transaction_with_signature(const unsigned char* signature, + long signSize, const char* transaction_hash, transaction_version version, const char* group_id, + const char* chain_id, const char* to, const char* nonce, const unsigned char* input, + long inputSize, const char* abi, int64_t block_limit, const char* value, const char* gas_price, + int64_t gas_limit, const char* max_fee_per_gas, const char* max_priority_fee_per_gas, + int32_t attribute, const char* extra_data) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(signature, NULL) @@ -199,14 +200,14 @@ const char* bcos_sdk_create_signed_transaction_with_signature(const char* signat try { TransactionBuilderV2 builder; - auto bytesData = fromHexString(input); + auto bytesData = bytes(input, input + inputSize * sizeof(byte)); crypto::HashType tx_hash(fromHex(std::string_view(transaction_hash))); - bytes sign = fromHex(std::string_view(signature)); + auto sign = bytes(signature, signature + signSize * sizeof(byte)); auto transaction = builder.createTransaction(std::move(sign), tx_hash, attribute, - (int32_t)version, group_id, chain_id, to ? to : "", nonce ? nonce : "", *bytesData, - abi ? abi : "", block_limit, value ? value : "", gas_price ? gas_price : "", gas_limit, - max_fee_per_gas ? max_fee_per_gas : "", + version, group_id, chain_id, to ? to : "", nonce ? nonce : "", + std::move(bytesData), abi ? abi : "", block_limit, value ? value : "", + gas_price ? gas_price : "", gas_limit, max_fee_per_gas ? max_fee_per_gas : "", max_priority_fee_per_gas ? max_priority_fee_per_gas : "", extra_data ? extra_data : ""); auto bytes = builder.encodeTransaction(*transaction); @@ -235,9 +236,9 @@ const char* bcos_sdk_create_signed_transaction_with_signature(const char* signat } void bcos_sdk_create_signed_transaction_with_full_fields(void* key_pair, const char* group_id, - const char* chain_id, const char* to, const char* input, const char* abi, int64_t block_limit, - const char* value, const char* gas_price, int64_t gas_limit, int32_t attribute, - const char* extra_data, char** tx_hash, char** signed_tx) + const char* chain_id, const char* to, const unsigned char* input, long inputSize, + const char* abi, int64_t block_limit, const char* value, const char* gas_price, + int64_t gas_limit, int32_t attribute, const char* extra_data, char** tx_hash, char** signed_tx) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(key_pair, ) @@ -252,11 +253,12 @@ void bcos_sdk_create_signed_transaction_with_full_fields(void* key_pair, const c try { TransactionBuilderV2 builder; - auto bytesData = fromHexString(input); - auto result = builder.createSignedTransaction(*((bcos::crypto::KeyPairInterface*)key_pair), - attribute, 1, group_id, chain_id, to ? to : "", "", *bytesData, abi ? abi : "", - block_limit, value ? value : "", gas_price ? gas_price : "", gas_limit, "", "", - extra_data ? extra_data : ""); + auto bytesData = bytes(input, input + inputSize * sizeof(byte)); + auto result = + builder.createSignedTransaction(*static_cast(key_pair), + attribute, 1, group_id, chain_id, to ? to : "", "", std::move(bytesData), + abi ? abi : "", block_limit, value ? value : "", gas_price ? gas_price : "", + gas_limit, "", "", extra_data ? extra_data : ""); *tx_hash = strdup(result.first.c_str()); *signed_tx = strdup(result.second.c_str()); } @@ -265,23 +267,25 @@ void bcos_sdk_create_signed_transaction_with_full_fields(void* key_pair, const c std::string errorMsg = boost::diagnostic_information(e); BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_signed_transaction_with_full_fields") << LOG_DESC("exception") << LOG_KV("group_id", group_id) - << LOG_KV("chain_id", chain_id) << LOG_KV("to", std::string(to ? to : "")) - << LOG_KV("input", input) << LOG_KV("abi", std::string(abi ? abi : "")) + << LOG_KV("chain_id", chain_id) + << LOG_KV("to", std::string_view(to ? to : "")) + << LOG_KV("inputSize", inputSize) + << LOG_KV("abi", std::string_view(abi ? abi : "")) << LOG_KV("block_limit", block_limit) - << LOG_KV("value", std::string(value ? value : "")) - << LOG_KV("gas_price", std::string(gas_price ? gas_price : "")) + << LOG_KV("value", std::string_view(value ? value : "")) + << LOG_KV("gas_price", std::string_view(gas_price ? gas_price : "")) << LOG_KV("gas_limit", gas_limit) << LOG_KV("attribute", attribute) - << LOG_KV("extra_data", std::string(extra_data ? extra_data : "")) + << LOG_KV("extra_data", std::string_view(extra_data ? extra_data : "")) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); } } void bcos_sdk_create_signed_eip1559_transaction_with_full_fields(void* key_pair, - const char* group_id, const char* chain_id, const char* to, const char* input, const char* abi, - int64_t block_limit, const char* value, int64_t gas_limit, const char* max_fee_per_gas, - const char* max_priority_fee_per_gas, int32_t attribute, const char* extra_data, char** tx_hash, - char** signed_tx) + const char* group_id, const char* chain_id, const char* to, const unsigned char* input, + long inputSize, const char* abi, int64_t block_limit, const char* value, int64_t gas_limit, + const char* max_fee_per_gas, const char* max_priority_fee_per_gas, int32_t attribute, + const char* extra_data, char** tx_hash, char** signed_tx) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(key_pair, ) @@ -296,10 +300,11 @@ void bcos_sdk_create_signed_eip1559_transaction_with_full_fields(void* key_pair, try { TransactionBuilderV2 builder; - auto bytesData = fromHexString(input); - auto result = builder.createSignedTransaction(*((bcos::crypto::KeyPairInterface*)key_pair), - attribute, 1, group_id, chain_id, to ? to : "", "", *bytesData, abi ? abi : "", - block_limit, value ? value : "", "", gas_limit, max_fee_per_gas ? max_fee_per_gas : "", + auto bytesData = bytes(input, input + inputSize * sizeof(byte)); + auto result = builder.createSignedTransaction( + *static_cast(key_pair), attribute, 1, group_id, + chain_id, to ? to : "", "", std::move(bytesData), abi ? abi : "", block_limit, + value ? value : "", "", gas_limit, max_fee_per_gas ? max_fee_per_gas : "", max_priority_fee_per_gas ? max_priority_fee_per_gas : "", extra_data ? extra_data : ""); *tx_hash = strdup(result.first.c_str()); *signed_tx = strdup(result.second.c_str()); @@ -310,14 +315,15 @@ void bcos_sdk_create_signed_eip1559_transaction_with_full_fields(void* key_pair, BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_signed_eip1559_transaction_with_full_fields") << LOG_DESC("exception") << LOG_KV("group_id", group_id) << LOG_KV("chain_id", chain_id) - << LOG_KV("to", std::string(to ? to : "")) << LOG_KV("input", input) - << LOG_KV("abi", std::string(abi ? abi : "")) << LOG_KV("block_limit", block_limit) - << LOG_KV("value", std::string(value ? value : "")) << LOG_KV("gas_limit", gas_limit) - << LOG_KV("max_fee_per_gas", std::string(max_fee_per_gas ? max_fee_per_gas : "")) + << LOG_KV("to", std::string_view(to ? to : "")) << LOG_KV("inputSize", inputSize) + << LOG_KV("abi", std::string_view(abi ? abi : "")) << LOG_KV("block_limit", block_limit) + << LOG_KV("value", std::string_view(value ? value : "")) + << LOG_KV("gas_limit", gas_limit) + << LOG_KV("max_fee_per_gas", std::string_view(max_fee_per_gas ? max_fee_per_gas : "")) << LOG_KV("max_priority_fee_per_gas", - std::string(max_priority_fee_per_gas ? max_priority_fee_per_gas : "")) + std::string_view(max_priority_fee_per_gas ? max_priority_fee_per_gas : "")) << LOG_KV("attribute", attribute) - << LOG_KV("extra_data", std::string(extra_data ? extra_data : "")) + << LOG_KV("extra_data", std::string_view(extra_data ? extra_data : "")) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); } diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h b/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h index 3812cb030..48e85bbc3 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h @@ -44,7 +44,8 @@ enum transaction_version * @param group_id group id * @param chain_id chain id * @param to contract address, if it is a contract creation transaction, it can be empty - * @param input encoded contract method and params + * @param input encoded contract method and params, bytes array + * @param inputSize encoded contract method and params size * @param abi contract abi, only create contract need * @param block_limit block limit * @param value transfer value @@ -53,8 +54,8 @@ enum transaction_version * @return void* transaction data pointer, you should release it after use */ void* bcos_sdk_create_transaction_v2_data(const char* group_id, const char* chain_id, - const char* to, const char* input, const char* abi, int64_t block_limit, const char* value, - const char* gas_price, int64_t gas_limit); + const char* to, const unsigned char* input, long inputSize, const char* abi, + int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit); /** * @brief create eip1559 transaction data with version 1 by default @@ -63,7 +64,8 @@ void* bcos_sdk_create_transaction_v2_data(const char* group_id, const char* chai * @param group_id group id * @param chain_id chain id * @param to contract address, if it is a contract creation transaction, it can be empty - * @param input encoded contract method and params + * @param input encoded contract method and params, bytes array + * @param inputSize encoded contract method and params size * @param abi contract abi, only create contract need * @param block_limit block limit * @param value transfer value @@ -73,8 +75,9 @@ void* bcos_sdk_create_transaction_v2_data(const char* group_id, const char* chai * @return void* transaction data pointer, you should release it after use */ void* bcos_sdk_create_eip1559_transaction_data(const char* group_id, const char* chain_id, - const char* to, const char* input, const char* abi, int64_t block_limit, const char* value, - int64_t gas_limit, const char* max_fee_per_gas, const char* max_priority_fee_per_gas); + const char* to, const unsigned char* input, long inputSize, const char* abi, + int64_t block_limit, const char* value, int64_t gas_limit, const char* max_fee_per_gas, + const char* max_priority_fee_per_gas); /** * @brief create transaction data with full fields @@ -87,7 +90,8 @@ void* bcos_sdk_create_eip1559_transaction_data(const char* group_id, const char* * @param chain_id chain id * @param to contract address, if it is a contract creation transaction, it can be empty * @param nonce nonce, random number to avoid duplicate transactions - * @param input encoded contract method and params + * @param input encoded contract method and params, bytes array + * @param inputSize encoded contract method and params size * @param abi contract abi, only create contract need * @param block_limit block limit * @param value transfer value @@ -97,11 +101,11 @@ void* bcos_sdk_create_eip1559_transaction_data(const char* group_id, const char* * @param max_priority_fee_per_gas max priority fee per gas * @return const char* transaction data hash hex string */ -const char* bcos_sdk_calc_transaction_data_hash_with_full_fields(int crypto_type, transaction_version version, - const char* group_id, const char* chain_id, const char* to, const char* nonce, - const char* input, const char* abi, int64_t block_limit, const char* value, - const char* gas_price, int64_t gas_limit, const char* max_fee_per_gas, - const char* max_priority_fee_per_gas); +const char* bcos_sdk_calc_transaction_data_hash_with_full_fields(int crypto_type, + transaction_version version, const char* group_id, const char* chain_id, const char* to, + const char* nonce, const unsigned char* input, long inputSize, const char* abi, + int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit, + const char* max_fee_per_gas, const char* max_priority_fee_per_gas); /** * @brief create transaction data with json string @@ -118,14 +122,17 @@ const char* bcos_sdk_calc_transaction_data_hash_with_json(int crypto_type, const * @brief create encoded transaction data with external signature * @note version 1 transaction only supported in FISCO BCOS 3.6.0 and later * - * @param signature signature hex string, if ECDSA, it is r||s||v, if SM2, it is r||s||pk + * @param signature signature bytes array, if ECDSA, it is r||s||v, if SM2, it is r||s||pk + * @param signSize signature bytes array size * @param transaction_hash transactionData hash hex string - * @param version tx version, only support 0 and 1 now, if version==1, then enable (value,gasPrice,gasLimit,maxFeePerGas,maxPriorityFeePerGas) fields + * @param version tx version, only support 0 and 1 now, if version==1, then enable + * (value,gasPrice,gasLimit,maxFeePerGas,maxPriorityFeePerGas) fields * @param group_id group id * @param chain_id chain id * @param to contract address, if it is a contract creation transaction, it can be empty * @param nonce nonce, random number to avoid duplicate transactions - * @param input encoded contract method and params + * @param input encoded contract method and params, bytes array + * @param inputSize encoded contract method and params size * @param abi contract abi, only create contract need * @param block_limit block limit * @param value transfer value @@ -137,11 +144,12 @@ const char* bcos_sdk_calc_transaction_data_hash_with_json(int crypto_type, const * @param extra_data extra data in transaction * @return const char* encoded transaction hex string */ -const char* bcos_sdk_create_signed_transaction_with_signature(const char* signature, - const char* transaction_hash, transaction_version version, const char* group_id, const char* chain_id, - const char* to, const char* nonce, const char* input, const char* abi, int64_t block_limit, - const char* value, const char* gas_price, int64_t gas_limit, const char* max_fee_per_gas, - const char* max_priority_fee_per_gas, int32_t attribute, const char* extra_data); +const char* bcos_sdk_create_signed_transaction_with_signature(const unsigned char* signature, + long signSize, const char* transaction_hash, transaction_version version, const char* group_id, + const char* chain_id, const char* to, const char* nonce, const unsigned char* input, + long inputSize, const char* abi, int64_t block_limit, const char* value, const char* gas_price, + int64_t gas_limit, const char* max_fee_per_gas, const char* max_priority_fee_per_gas, + int32_t attribute, const char* extra_data); /** * @brief create transaction with full fields, with version 1 by default @@ -151,8 +159,8 @@ const char* bcos_sdk_create_signed_transaction_with_signature(const char* signat * @param group_id group id * @param chain_id chain id * @param to contract address, if it is a contract creation transaction, it can be empty - * @param nonce nonce, random number to avoid duplicate transactions - * @param input encoded contract method and params + * @param input encoded contract method and params, bytes array + * @param inputSize encoded contract method and params size * @param abi contract abi, only create contract need * @param block_limit block limit * @param value transfer value @@ -164,9 +172,9 @@ const char* bcos_sdk_create_signed_transaction_with_signature(const char* signat * @param signed_tx output signed transaction hex string */ void bcos_sdk_create_signed_transaction_with_full_fields(void* key_pair, const char* group_id, - const char* chain_id, const char* to, const char* input, const char* abi, int64_t block_limit, - const char* value, const char* gas_price, int64_t gas_limit, int32_t attribute, - const char* extra_data, char** tx_hash, char** signed_tx); + const char* chain_id, const char* to, const unsigned char* input, long inputSize, + const char* abi, int64_t block_limit, const char* value, const char* gas_price, + int64_t gas_limit, int32_t attribute, const char* extra_data, char** tx_hash, char** signed_tx); /** * @brief create eip1559 transaction with full fields, with version 1 by default @@ -176,7 +184,8 @@ void bcos_sdk_create_signed_transaction_with_full_fields(void* key_pair, const c * @param group_id group id * @param chain_id chain id * @param to contract address, if it is a contract creation transaction, it can be empty - * @param input encoded contract method and params + * @param input encoded contract method and params, bytes array + * @param inputSize encoded contract method and params size * @param abi contract abi, only create contract need * @param block_limit block limit * @param value transfer value @@ -189,10 +198,10 @@ void bcos_sdk_create_signed_transaction_with_full_fields(void* key_pair, const c * @param signed_tx output signed transaction hex string */ void bcos_sdk_create_signed_eip1559_transaction_with_full_fields(void* key_pair, - const char* group_id, const char* chain_id, const char* to, const char* input, const char* abi, - int64_t block_limit, const char* value, int64_t gas_limit, const char* max_fee_per_gas, - const char* max_priority_fee_per_gas, int32_t attribute, const char* extra_data, char** tx_hash, - char** signed_tx); + const char* group_id, const char* chain_id, const char* to, const unsigned char* input, + long inputSize, const char* abi, int64_t block_limit, const char* value, int64_t gas_limit, + const char* max_fee_per_gas, const char* max_priority_fee_per_gas, int32_t attribute, + const char* extra_data, char** tx_hash, char** signed_tx); #ifdef __cplusplus } diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.h b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.h index 11835a621..09be572be 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.h +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_BcosSDKJniObj.h @@ -12,52 +12,56 @@ extern "C" { * Method: create * Signature: (Lorg/fisco/bcos/sdk/jni/common/JniConfig;)J */ -JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_create(JNIEnv*, jclass, jobject); +JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_create + (JNIEnv *, jclass, jobject); /* * Class: org_fisco_bcos_sdk_jni_BcosSDKJniObj * Method: start * Signature: ()V */ -JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_start(JNIEnv*, jobject); +JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_start + (JNIEnv *, jobject); /* * Class: org_fisco_bcos_sdk_jni_BcosSDKJniObj * Method: stop * Signature: ()V */ -JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_stop(JNIEnv*, jobject); +JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_stop + (JNIEnv *, jobject); /* * Class: org_fisco_bcos_sdk_jni_BcosSDKJniObj * Method: destroy * Signature: ()V */ -JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_destroy(JNIEnv*, jobject); +JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_destroy + (JNIEnv *, jobject); /* * Class: org_fisco_bcos_sdk_jni_BcosSDKJniObj * Method: localProtocolInfo * Signature: ()I */ -JNIEXPORT jint JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_localProtocolInfo( - JNIEnv*, jobject); +JNIEXPORT jint JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_localProtocolInfo + (JNIEnv *, jobject); /* * Class: org_fisco_bcos_sdk_jni_BcosSDKJniObj * Method: negotiatedProtocolInfo * Signature: ()I */ -JNIEXPORT jint JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_negotiatedProtocolInfo( - JNIEnv*, jobject); +JNIEXPORT jint JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_negotiatedProtocolInfo + (JNIEnv *, jobject); /* * Class: org_fisco_bcos_sdk_jni_BcosSDKJniObj * Method: registerBlockNotifier * Signature: (Ljava/lang/String;Lorg/fisco/bcos/sdk/jni/BlockNotifier;)V */ -JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_registerBlockNotifier( - JNIEnv*, jobject, jstring, jobject); +JNIEXPORT void JNICALL Java_org_fisco_bcos_sdk_jni_BcosSDKJniObj_registerBlockNotifier + (JNIEnv *, jobject, jstring, jobject); #ifdef __cplusplus } diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcServiceJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcServiceJniObj.cpp index 85038d7d5..10fab0dcd 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcServiceJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcServiceJniObj.cpp @@ -161,6 +161,7 @@ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_rpc_RpcServiceJniObj_sendT if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring jtx_hash = env->NewStringUTF(tx_hash); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj.cpp index 513bb04ba..63403d7a4 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj.cpp @@ -20,6 +20,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_createJniKeyPair__I( if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return 0; } return reinterpret_cast(keypair); @@ -43,6 +44,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_createHsmKeyPair__Lj if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return 0; } return reinterpret_cast(keypair); @@ -66,6 +68,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_createJniKeyPair__I_ if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return 0; } return reinterpret_cast(keypair); @@ -92,6 +95,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_createHsmKeyPair___3 if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return 0; } return reinterpret_cast(keypair); @@ -118,6 +122,7 @@ JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJni if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return 0; } return reinterpret_cast(keypair); @@ -138,6 +143,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_getJniKeyPairAddress if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring result = env->NewStringUTF(addr); @@ -160,6 +166,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_getJniKeyPairPubKey( if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring result = env->NewStringUTF(pub); @@ -182,6 +189,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_keypair_KeyPairJniObj_getJniKeyPairPrivate if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring result = env->NewStringUTF(pri); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj.cpp index 59c606436..8ed9eb8b3 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj.cpp @@ -39,6 +39,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj_createReceipt if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return 0; } return reinterpret_cast(receipt_data); @@ -61,6 +62,7 @@ JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBui if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return 0; } return reinterpret_cast(receipt_data); @@ -93,6 +95,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj_encodeReceipt if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring jencoded_receipt_data = env->NewStringUTF(encoded_receipt_data); @@ -122,6 +125,7 @@ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptB if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring jreceipt_data_json = env->NewStringUTF(receipt_data_json); @@ -149,6 +153,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_receipt_ReceiptBuilderJniObj_calcReceiptDa if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring jreceipt_data_hash = env->NewStringUTF(receipt_data_hash); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj.cpp index 400e28bde..4003b32ae 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_signature_SignatureJniObj.cpp @@ -29,6 +29,7 @@ JNIEXPORT jbyteArray JNICALL Java_org_fisco_bcos_sdk_jni_utilities_signature_Sig if (operation_status == -1) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jbyte* signed_data_r = (jbyte*)sign_result.r; @@ -73,6 +74,7 @@ JNIEXPORT jboolean JNICALL Java_org_fisco_bcos_sdk_jni_utilities_signature_Signa if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return JNI_FALSE; } return is_verify_success ? JNI_TRUE : JNI_FALSE; diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj.cpp index 16da58014..425d3ad8b 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj.cpp @@ -46,6 +46,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createTransact if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return 0; } return reinterpret_cast(transaction_data); @@ -68,6 +69,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createTransact if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return 0; } return reinterpret_cast(transaction_data); @@ -103,6 +105,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_encodeTransact if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring jencoded_transaction_data = env->NewStringUTF(encoded_transaction_data); @@ -134,6 +137,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_decodeTransact if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring jtransaction_data_json = env->NewStringUTF(transaction_data_json); @@ -161,6 +165,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_calcTransactio if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring jtransaction_data_hash = env->NewStringUTF(transaction_data_hash); @@ -190,6 +195,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_decodeTransact if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return 0; } return reinterpret_cast(transaction_data); } @@ -210,6 +216,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_decodeTransact if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring jtransaction_json = env->NewStringUTF(transaction_json); if (transaction_json) @@ -255,6 +262,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_signTransactio if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring jsigned_data = env->NewStringUTF(signed_data); @@ -297,6 +305,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createSignedTr if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring jsigned_tx = env->NewStringUTF(signed_tx); @@ -343,6 +352,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createSignedTr if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring jsigned_tx = env->NewStringUTF(signed_tx); @@ -395,6 +405,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createSignedTr if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring jtx_hash = env->NewStringUTF(tx_hash); @@ -483,6 +494,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderJniObj_createSignedTr if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring jtx_hash = env->NewStringUTF(tx_hash); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp index ac54cf978..538dd341a 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp @@ -29,11 +29,11 @@ * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: createTransactionData * Signature: - * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;J)J + * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;J)J */ JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createTransactionData( - JNIEnv* env, jclass, jstring j_group_id, jstring j_chain_id, jstring j_to, jstring j_input, + JNIEnv* env, jclass, jstring j_group_id, jstring j_chain_id, jstring j_to, jbyteArray j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_gas_price, jlong j_gas_limit) { CHECK_OBJECT_NOT_NULL(env, j_group_id, 0); @@ -43,27 +43,31 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createTransa const char* group_id = GET_J_STRING_CONTENT(env, j_group_id); const char* chain_id = GET_J_STRING_CONTENT(env, j_chain_id); const char* to = GET_J_STRING_CONTENT_DEF(env, j_to, NULL); - const char* input = GET_J_STRING_CONTENT(env, j_input); const char* abi = GET_J_STRING_CONTENT_DEF(env, j_abi, NULL); const char* value = GET_J_STRING_CONTENT_DEF(env, j_value, NULL); const char* gas_price = GET_J_STRING_CONTENT_DEF(env, j_gas_price, NULL); - int64_t block_limit = (int64_t)j_block_limit; - int64_t gas_limit = (int64_t)j_gas_limit; + int64_t block_limit = j_block_limit; + int64_t gas_limit = j_gas_limit; - void* transaction_data = bcos_sdk_create_transaction_v2_data( - group_id, chain_id, to, input, abi, block_limit, value, gas_price, gas_limit); + jbyte* inputBuffer = env->GetByteArrayElements(j_input, NULL); + jsize inputSize = env->GetArrayLength(j_input); + + void* transaction_data = bcos_sdk_create_transaction_v2_data(group_id, chain_id, to, + reinterpret_cast(inputBuffer), inputSize, abi, block_limit, value, + gas_price, gas_limit); env->ReleaseStringUTFChars(j_group_id, group_id); env->ReleaseStringUTFChars(j_chain_id, chain_id); env->ReleaseStringUTFChars(j_to, to); - env->ReleaseStringUTFChars(j_input, input); env->ReleaseStringUTFChars(j_abi, abi); env->ReleaseStringUTFChars(j_value, value); env->ReleaseStringUTFChars(j_gas_price, gas_price); + env->ReleaseByteArrayElements(j_input, inputBuffer, 0); if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return 0; } return reinterpret_cast(transaction_data); @@ -73,11 +77,11 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createTransa * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: createEIP1559TransactionData * Signature: - * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;JLjava/lang/String;Ljava/lang/String;)J + * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;JLjava/lang/String;Ljava/lang/String;)J */ JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createEIP1559TransactionData( - JNIEnv* env, jclass, jstring j_group_id, jstring j_chain_id, jstring j_to, jstring j_input, + JNIEnv* env, jclass, jstring j_group_id, jstring j_chain_id, jstring j_to, jbyteArray j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jlong j_gas_limit, jstring j_max_fee_per_gas, jstring j_max_priority_fee_per_gas) { @@ -88,30 +92,33 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createEIP155 const char* group_id = GET_J_STRING_CONTENT(env, j_group_id); const char* chain_id = GET_J_STRING_CONTENT(env, j_chain_id); const char* to = GET_J_STRING_CONTENT_DEF(env, j_to, NULL); - const char* input = GET_J_STRING_CONTENT(env, j_input); const char* abi = GET_J_STRING_CONTENT_DEF(env, j_abi, NULL); const char* value = GET_J_STRING_CONTENT_DEF(env, j_value, NULL); const char* max_fee_per_gas = GET_J_STRING_CONTENT_DEF(env, j_max_fee_per_gas, NULL); const char* max_priority_fee_per_gas = GET_J_STRING_CONTENT_DEF(env, j_max_priority_fee_per_gas, NULL); - int64_t block_limit = (int64_t)j_block_limit; - int64_t gas_limit = (int64_t)j_gas_limit; + int64_t block_limit = j_block_limit; + int64_t gas_limit = j_gas_limit; + jbyte* inputBuffer = env->GetByteArrayElements(j_input, NULL); + jsize inputSize = env->GetArrayLength(j_input); - void* transaction_data = bcos_sdk_create_eip1559_transaction_data(group_id, chain_id, to, input, - abi, block_limit, value, gas_limit, max_fee_per_gas, max_priority_fee_per_gas); + void* transaction_data = bcos_sdk_create_eip1559_transaction_data(group_id, chain_id, to, + reinterpret_cast(inputBuffer), inputSize, abi, block_limit, value, + gas_limit, max_fee_per_gas, max_priority_fee_per_gas); env->ReleaseStringUTFChars(j_group_id, group_id); env->ReleaseStringUTFChars(j_chain_id, chain_id); env->ReleaseStringUTFChars(j_to, to); - env->ReleaseStringUTFChars(j_input, input); env->ReleaseStringUTFChars(j_abi, abi); env->ReleaseStringUTFChars(j_value, value); env->ReleaseStringUTFChars(j_max_fee_per_gas, max_fee_per_gas); env->ReleaseStringUTFChars(j_max_priority_fee_per_gas, max_priority_fee_per_gas); + env->ReleaseByteArrayElements(j_input, inputBuffer, 0); if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return 0; } return reinterpret_cast(transaction_data); @@ -121,12 +128,12 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createEIP155 * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: calcTransactionDataHashWithFullFields * Signature: - * (ILorg/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;)Ljava/lang/String; + * (ILorg/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransactionDataHashWithFullFields( JNIEnv* env, jclass, jint j_crypto_type, jobject j_tx_version, jstring j_group_id, - jstring j_chain_id, jstring j_to, jstring j_nonce, jstring j_input, jstring j_abi, + jstring j_chain_id, jstring j_to, jstring j_nonce, jbyteArray j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_gasPrice, jlong j_gas_limit, jstring j_max_fee_per_gas, jstring j_max_priority_fee_per_gas) { @@ -139,39 +146,43 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransact const char* chain_id = GET_J_STRING_CONTENT(env, j_chain_id); const char* to = GET_J_STRING_CONTENT_DEF(env, j_to, NULL); const char* nonce = GET_J_STRING_CONTENT(env, j_nonce); - const char* input = GET_J_STRING_CONTENT(env, j_input); const char* abi = GET_J_STRING_CONTENT_DEF(env, j_abi, NULL); const char* value = GET_J_STRING_CONTENT_DEF(env, j_value, NULL); const char* gas_price = GET_J_STRING_CONTENT_DEF(env, j_gasPrice, NULL); const char* max_fee_per_gas = GET_J_STRING_CONTENT_DEF(env, j_max_fee_per_gas, NULL); const char* max_priority_fee_per_gas = GET_J_STRING_CONTENT_DEF(env, j_max_priority_fee_per_gas, NULL); - int crypto_type = (int)j_crypto_type; - int64_t block_limit = (int64_t)j_block_limit; - int64_t gas_limit = (int64_t)j_gas_limit; + jbyte* inputBuffer = env->GetByteArrayElements(j_input, NULL); + jsize inputSize = env->GetArrayLength(j_input); + + int crypto_type = j_crypto_type; + int64_t block_limit = j_block_limit; + int64_t gas_limit = j_gas_limit; jclass tx_version = env->GetObjectClass(j_tx_version); jmethodID j_methodId = env->GetMethodID(tx_version, "getValue", "()I"); - int32_t ver = (int32_t)env->CallIntMethod(j_tx_version, j_methodId); + int32_t ver = env->CallIntMethod(j_tx_version, j_methodId); const char* tx_hash = bcos_sdk_calc_transaction_data_hash_with_full_fields(crypto_type, - (transaction_version)ver, group_id, chain_id, to, nonce, input, abi, block_limit, value, + (transaction_version)ver, group_id, chain_id, to, nonce, + reinterpret_cast(inputBuffer), inputSize, abi, block_limit, value, gas_price, gas_limit, max_fee_per_gas, max_priority_fee_per_gas); env->ReleaseStringUTFChars(j_group_id, group_id); env->ReleaseStringUTFChars(j_chain_id, chain_id); env->ReleaseStringUTFChars(j_to, to); env->ReleaseStringUTFChars(j_nonce, nonce); - env->ReleaseStringUTFChars(j_input, input); env->ReleaseStringUTFChars(j_abi, abi); env->ReleaseStringUTFChars(j_value, value); env->ReleaseStringUTFChars(j_gasPrice, gas_price); env->ReleaseStringUTFChars(j_max_fee_per_gas, max_fee_per_gas); env->ReleaseStringUTFChars(j_max_priority_fee_per_gas, max_priority_fee_per_gas); + env->ReleaseByteArrayElements(j_input, inputBuffer, 0); if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring j_transaction_data_hash = env->NewStringUTF(tx_hash); @@ -205,6 +216,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransact if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring j_transaction_data_hash = env->NewStringUTF(tx_hash); @@ -222,12 +234,12 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransact * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: createSignedTransactionWithSignature * Signature: - * (Ljava/lang/String;Ljava/lang/String;Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;ILjava/lang/String;)Ljava/lang/String; + * ([BLjava/lang/String;Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;ILjava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedTransactionWithSignature( - JNIEnv* env, jclass, jstring j_signature, jstring j_tx_hash, jobject j_tx_version, - jstring j_group_id, jstring j_chain_id, jstring j_to, jstring j_nonce, jstring j_input, + JNIEnv* env, jclass, jbyteArray j_signature, jstring j_tx_hash, jobject j_tx_version, + jstring j_group_id, jstring j_chain_id, jstring j_to, jstring j_nonce, jbyteArray j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_gas_price, jlong j_gas_limit, jstring j_max_fee_per_gas, jstring j_max_priority_fee_per_gas, jint j_attribute, jstring j_extra_data) @@ -239,16 +251,14 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned CHECK_OBJECT_NOT_NULL(env, j_input, NULL); CHECK_OBJECT_NOT_NULL(env, j_nonce, NULL); - int64_t block_limit = (int64_t)j_block_limit; - int64_t gas_limit = (int64_t)j_gas_limit; - int32_t attribute = (int32_t)j_attribute; - const char* signature = GET_J_STRING_CONTENT(env, j_signature); + int64_t block_limit = j_block_limit; + int64_t gas_limit = j_gas_limit; + int32_t attribute = j_attribute; const char* tx_hash = GET_J_STRING_CONTENT(env, j_tx_hash); const char* group_id = GET_J_STRING_CONTENT(env, j_group_id); const char* chain_id = GET_J_STRING_CONTENT(env, j_chain_id); const char* to = GET_J_STRING_CONTENT_DEF(env, j_to, NULL); const char* nonce = GET_J_STRING_CONTENT(env, j_nonce); - const char* input = GET_J_STRING_CONTENT(env, j_input); const char* abi = GET_J_STRING_CONTENT_DEF(env, j_abi, NULL); const char* value = GET_J_STRING_CONTENT_DEF(env, j_value, NULL); const char* gas_price = GET_J_STRING_CONTENT_DEF(env, j_gas_price, NULL); @@ -256,32 +266,39 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned const char* max_priority_fee_per_gas = GET_J_STRING_CONTENT_DEF(env, j_max_priority_fee_per_gas, NULL); const char* extra_data = GET_J_STRING_CONTENT_DEF(env, j_extra_data, NULL); + jbyte* signBuffer = env->GetByteArrayElements(j_signature, NULL); + jsize signSize = env->GetArrayLength(j_signature); + jbyte* inputBuffer = env->GetByteArrayElements(j_input, NULL); + jsize inputSize = env->GetArrayLength(j_input); jclass tx_version = env->GetObjectClass(j_tx_version); jmethodID j_methodId = env->GetMethodID(tx_version, "getValue", "()I"); - int32_t ver = (int32_t)env->CallIntMethod(j_tx_version, j_methodId); + int32_t ver = env->CallIntMethod(j_tx_version, j_methodId); - const char* encodedTx = bcos_sdk_create_signed_transaction_with_signature(signature, tx_hash, - (transaction_version)ver, group_id, chain_id, to, nonce, input, abi, block_limit, value, + const char* encodedTx = bcos_sdk_create_signed_transaction_with_signature( + reinterpret_cast(signBuffer), signSize, tx_hash, + static_cast(ver), group_id, chain_id, to, nonce, + reinterpret_cast(inputBuffer), inputSize, abi, block_limit, value, gas_price, gas_limit, max_fee_per_gas, max_priority_fee_per_gas, attribute, extra_data); - env->ReleaseStringUTFChars(j_signature, signature); env->ReleaseStringUTFChars(j_tx_hash, tx_hash); env->ReleaseStringUTFChars(j_group_id, group_id); env->ReleaseStringUTFChars(j_chain_id, chain_id); env->ReleaseStringUTFChars(j_to, to); env->ReleaseStringUTFChars(j_nonce, nonce); - env->ReleaseStringUTFChars(j_input, input); env->ReleaseStringUTFChars(j_abi, abi); env->ReleaseStringUTFChars(j_value, value); env->ReleaseStringUTFChars(j_gas_price, gas_price); env->ReleaseStringUTFChars(j_max_fee_per_gas, max_fee_per_gas); env->ReleaseStringUTFChars(j_max_priority_fee_per_gas, max_priority_fee_per_gas); env->ReleaseStringUTFChars(j_extra_data, extra_data); + env->ReleaseByteArrayElements(j_input, inputBuffer, 0); + env->ReleaseByteArrayElements(j_signature, signBuffer, 0); if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring encoded_tx_hex = env->NewStringUTF(encodedTx); @@ -299,12 +316,12 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: createSignedTransactionWithFullFields * Signature: - * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; + * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; */ JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedTransactionWithFullFields( JNIEnv* env, jclass, jlong j_key_pair, jstring j_group_id, jstring j_chain_id, jstring j_to, - jstring j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_gas_price, + jbyteArray j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_gas_price, jlong j_gas_limit, jint j_attribute, jstring j_extra_data) { CHECK_OBJECT_NOT_NULL(env, j_group_id, NULL); @@ -312,26 +329,29 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned CHECK_OBJECT_NOT_NULL(env, j_input, NULL); void* keypair = reinterpret_cast(j_key_pair); - int64_t block_limit = (int64_t)j_block_limit; - int64_t gas_limit = (int64_t)j_gas_limit; - int32_t attribute = (int32_t)j_attribute; + int64_t block_limit = j_block_limit; + int64_t gas_limit = j_gas_limit; + int32_t attribute = j_attribute; const char* group_id = GET_J_STRING_CONTENT(env, j_group_id); const char* chain_id = GET_J_STRING_CONTENT(env, j_chain_id); const char* to = GET_J_STRING_CONTENT_DEF(env, j_to, NULL); - const char* input = GET_J_STRING_CONTENT(env, j_input); const char* abi = GET_J_STRING_CONTENT_DEF(env, j_abi, NULL); const char* value = GET_J_STRING_CONTENT_DEF(env, j_value, NULL); const char* gas_price = GET_J_STRING_CONTENT_DEF(env, j_gas_price, NULL); const char* extra_data = GET_J_STRING_CONTENT_DEF(env, j_extra_data, NULL); + jbyte* inputBuffer = env->GetByteArrayElements(j_input, NULL); + jsize inputSize = env->GetArrayLength(j_input); char* tx_hash = NULL; char* signed_tx = NULL; - bcos_sdk_create_signed_transaction_with_full_fields(keypair, group_id, chain_id, to, input, abi, - block_limit, value, gas_price, gas_limit, attribute, extra_data, &tx_hash, &signed_tx); + bcos_sdk_create_signed_transaction_with_full_fields(keypair, group_id, chain_id, to, + reinterpret_cast(inputBuffer), inputSize, abi, block_limit, value, + gas_price, gas_limit, attribute, extra_data, &tx_hash, &signed_tx); if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring j_tx_hash = env->NewStringUTF(tx_hash); @@ -356,11 +376,11 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned env->ReleaseStringUTFChars(j_group_id, group_id); env->ReleaseStringUTFChars(j_chain_id, chain_id); env->ReleaseStringUTFChars(j_to, to); - env->ReleaseStringUTFChars(j_input, input); env->ReleaseStringUTFChars(j_abi, abi); env->ReleaseStringUTFChars(j_value, value); env->ReleaseStringUTFChars(j_gas_price, gas_price); env->ReleaseStringUTFChars(j_extra_data, extra_data); + env->ReleaseByteArrayElements(j_input, inputBuffer, 0); env->DeleteLocalRef(j_tx_hash); env->DeleteLocalRef(j_signed_tx); @@ -384,42 +404,46 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: createSignedEIP1559TransactionWithFullFields * Signature: - * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; + * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; */ JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedEIP1559TransactionWithFullFields( JNIEnv* env, jclass, jlong j_key_pair, jstring j_group_id, jstring j_chain_id, jstring j_to, - jstring j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_max_fee_per_gas, - jstring j_max_priority_fee_per_gas, jlong j_gas_limit, jint j_attribute, jstring j_extra_data) + jbyteArray j_input, jstring j_abi, jlong j_block_limit, jstring j_value, + jstring j_max_fee_per_gas, jstring j_max_priority_fee_per_gas, jlong j_gas_limit, + jint j_attribute, jstring j_extra_data) { CHECK_OBJECT_NOT_NULL(env, j_group_id, NULL); CHECK_OBJECT_NOT_NULL(env, j_chain_id, NULL); CHECK_OBJECT_NOT_NULL(env, j_input, NULL); void* keypair = reinterpret_cast(j_key_pair); - int64_t block_limit = (int64_t)j_block_limit; - int64_t gas_limit = (int64_t)j_gas_limit; - int32_t attribute = (int32_t)j_attribute; + int64_t block_limit = j_block_limit; + int64_t gas_limit = j_gas_limit; + int32_t attribute = j_attribute; const char* group_id = GET_J_STRING_CONTENT(env, j_group_id); const char* chain_id = GET_J_STRING_CONTENT(env, j_chain_id); const char* to = GET_J_STRING_CONTENT_DEF(env, j_to, NULL); - const char* input = GET_J_STRING_CONTENT(env, j_input); const char* abi = GET_J_STRING_CONTENT_DEF(env, j_abi, NULL); const char* value = GET_J_STRING_CONTENT_DEF(env, j_value, NULL); const char* max_fee_per_gas = GET_J_STRING_CONTENT_DEF(env, j_max_fee_per_gas, NULL); const char* max_priority_fee_per_gas = GET_J_STRING_CONTENT_DEF(env, j_max_priority_fee_per_gas, NULL); const char* extra_data = GET_J_STRING_CONTENT_DEF(env, j_extra_data, NULL); + jbyte* inputBuffer = env->GetByteArrayElements(j_input, NULL); + jsize inputSize = env->GetArrayLength(j_input); char* tx_hash = NULL; char* signed_tx = NULL; bcos_sdk_create_signed_eip1559_transaction_with_full_fields(keypair, group_id, chain_id, to, - input, abi, block_limit, value, gas_limit, max_fee_per_gas, max_priority_fee_per_gas, - attribute, extra_data, &tx_hash, &signed_tx); + reinterpret_cast(inputBuffer), inputSize, abi, block_limit, value, + gas_limit, max_fee_per_gas, max_priority_fee_per_gas, attribute, extra_data, &tx_hash, + &signed_tx); if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return NULL; } jstring j_tx_hash = env->NewStringUTF(tx_hash); @@ -444,12 +468,12 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned env->ReleaseStringUTFChars(j_group_id, group_id); env->ReleaseStringUTFChars(j_chain_id, chain_id); env->ReleaseStringUTFChars(j_to, to); - env->ReleaseStringUTFChars(j_input, input); env->ReleaseStringUTFChars(j_abi, abi); env->ReleaseStringUTFChars(j_value, value); env->ReleaseStringUTFChars(j_max_fee_per_gas, max_fee_per_gas); env->ReleaseStringUTFChars(j_max_priority_fee_per_gas, max_priority_fee_per_gas); env->ReleaseStringUTFChars(j_extra_data, extra_data); + env->ReleaseByteArrayElements(j_input, inputBuffer, 0); env->DeleteLocalRef(j_tx_hash); env->DeleteLocalRef(j_signed_tx); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.h b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.h index 00643340e..8995ba6a6 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.h +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.h @@ -10,58 +10,76 @@ extern "C" { /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: createTransactionData - * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;J)J + * Signature: + * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;J)J */ -JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createTransactionData - (JNIEnv *, jclass, jstring, jstring, jstring, jstring, jstring, jlong, jstring, jstring, jlong); +JNIEXPORT jlong JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createTransactionData(JNIEnv*, + jclass, jstring, jstring, jstring, jbyteArray, jstring, jlong, jstring, jstring, jlong); /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: createEIP1559TransactionData - * Signature: (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;JLjava/lang/String;Ljava/lang/String;)J + * Signature: + * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;JLjava/lang/String;Ljava/lang/String;)J */ -JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createEIP1559TransactionData - (JNIEnv *, jclass, jstring, jstring, jstring, jstring, jstring, jlong, jstring, jlong, jstring, jstring); +JNIEXPORT jlong JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createEIP1559TransactionData( + JNIEnv*, jclass, jstring, jstring, jstring, jbyteArray, jstring, jlong, jstring, jlong, jstring, + jstring); /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: calcTransactionDataHashWithFullFields - * Signature: (ILorg/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;)Ljava/lang/String; + * Signature: + * (ILorg/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;)Ljava/lang/String; */ -JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransactionDataHashWithFullFields - (JNIEnv *, jclass, jint, jobject, jstring, jstring, jstring, jstring, jstring, jstring, jlong, jstring, jstring, jlong, jstring, jstring); +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransactionDataHashWithFullFields( + JNIEnv*, jclass, jint, jobject, jstring, jstring, jstring, jstring, jbyteArray, jstring, jlong, + jstring, jstring, jlong, jstring, jstring); /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: calcTransactionDataHashWithJson * Signature: (ILjava/lang/String;)Ljava/lang/String; */ -JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransactionDataHashWithJson - (JNIEnv *, jclass, jint, jstring); +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransactionDataHashWithJson( + JNIEnv*, jclass, jint, jstring); /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: createSignedTransactionWithSignature - * Signature: (Ljava/lang/String;Ljava/lang/String;Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;ILjava/lang/String;)Ljava/lang/String; + * Signature: + * ([BLjava/lang/String;Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;ILjava/lang/String;)Ljava/lang/String; */ -JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedTransactionWithSignature - (JNIEnv *, jclass, jstring, jstring, jobject, jstring, jstring, jstring, jstring, jstring, jstring, jlong, jstring, jstring, jlong, jstring, jstring, jint, jstring); +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedTransactionWithSignature( + JNIEnv*, jclass, jbyteArray, jstring, jobject, jstring, jstring, jstring, jstring, jbyteArray, + jstring, jlong, jstring, jstring, jlong, jstring, jstring, jint, jstring); /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: createSignedTransactionWithFullFields - * Signature: (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; + * Signature: + * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; */ -JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedTransactionWithFullFields - (JNIEnv *, jclass, jlong, jstring, jstring, jstring, jstring, jstring, jlong, jstring, jstring, jlong, jint, jstring); +JNIEXPORT jobject JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedTransactionWithFullFields( + JNIEnv*, jclass, jlong, jstring, jstring, jstring, jbyteArray, jstring, jlong, jstring, jstring, + jlong, jint, jstring); /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: createSignedEIP1559TransactionWithFullFields - * Signature: (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; + * Signature: + * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; */ -JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedEIP1559TransactionWithFullFields - (JNIEnv *, jclass, jlong, jstring, jstring, jstring, jstring, jstring, jlong, jstring, jstring, jstring, jlong, jint, jstring); +JNIEXPORT jobject JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedEIP1559TransactionWithFullFields( + JNIEnv*, jclass, jlong, jstring, jstring, jstring, jbyteArray, jstring, jlong, jstring, jstring, + jstring, jlong, jint, jstring); #ifdef __cplusplus } diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV2JniObj.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV2JniObj.java index ad5e49718..d490ab57b 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV2JniObj.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV2JniObj.java @@ -29,7 +29,7 @@ public static native long createTransactionData( String groupID, String chainID, String to, - String input, + byte[] input, String abi, long blockLimit, String value, @@ -58,7 +58,7 @@ public static native long createEIP1559TransactionData( String groupID, String chainID, String to, - String input, + byte[] input, String abi, long blockLimit, String value, @@ -95,7 +95,7 @@ public static native String calcTransactionDataHashWithFullFields( String chainID, String to, String nonce, - String input, + byte[] input, String abi, long blockLimit, String value, @@ -120,7 +120,7 @@ public static native String calcTransactionDataHashWithJson(int cryptoType, Stri /** * create encoded transaction data with external signature * - * @param signature signature hex string, if ECDSA, it is r||s||v, if SM2, it is r||s||pk + * @param signature signature bytes array, if ECDSA, it is r||s||v, if SM2, it is r||s||pk * @param txDataHash transactionData hash hex string * @param version tx version, only support 0 and 1 now, if version==1, then enable * (value,gasPrice,gasLimit,maxFeePerGas,maxPriorityFeePerGas) fields @@ -142,14 +142,14 @@ public static native String calcTransactionDataHashWithJson(int cryptoType, Stri * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later */ public static native String createSignedTransactionWithSignature( - String signature, + byte[] signature, String txDataHash, TransactionVersion version, String groupID, String chainID, String to, String nonce, - String input, + byte[] input, String abi, long blockLimit, String value, @@ -184,7 +184,7 @@ public static native TxPair createSignedTransactionWithFullFields( String groupID, String chainID, String to, - String input, + byte[] input, String abi, long blockLimit, String value, @@ -218,7 +218,7 @@ public static native TxPair createSignedEIP1559TransactionWithFullFields( String groupID, String chainID, String to, - String input, + byte[] input, String abi, long blockLimit, String value, From b51f1f19443d0ebf34224c179f70dc5bba0f3e56 Mon Sep 17 00:00:00 2001 From: lucasli <410567249@qq.com> Date: Fri, 5 Jan 2024 16:15:22 +0800 Subject: [PATCH 20/29] add API in jni for tx struct adapt to balance (#199) --- bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp | 90 +- bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h | 44 +- bindings/java/jni/scripts/java2jni.sh | 2 +- ...ties_tx_TransactionStructBuilderJniObj.cpp | 93 +- ...es_tx_TransactionStructBuilderV2JniObj.cpp | 724 ++++++++++++++ ...ties_tx_TransactionStructBuilderV2JniObj.h | 77 ++ .../jni/utilities/tx/TransactionDataV2.java | 121 +++ .../tx/TransactionStructBuilderV2JniObj.java | 76 ++ .../sdk/jni/utilities/tx/TransactionV2.java | 67 ++ .../bcos/sdk/jni/test/tx/TestTxStructV2.java | 239 +++++ sample/tx/tx_struct_test.c | 14 +- sample/tx/tx_struct_v2_test.c | 14 +- test/testTransactionStruct.cpp | 48 +- test/testTransactionStructV2.cpp | 902 ++++++++++++++++++ 14 files changed, 2372 insertions(+), 139 deletions(-) create mode 100644 bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj.cpp create mode 100644 bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj.h create mode 100644 bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2.java create mode 100644 bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderV2JniObj.java create mode 100644 bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionV2.java create mode 100644 bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV2.java create mode 100644 test/testTransactionStructV2.cpp diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp index fea933613..95233ec4a 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp @@ -534,9 +534,7 @@ struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct_with bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(group_id, NULL); BCOS_SDK_C_PARAMS_VERIFICATION(chain_id, NULL); - // BCOS_SDK_C_PARAMS_VERIFICATION(to, NULL); BCOS_SDK_C_PARAMS_VERIFICATION(input, NULL); - // BCOS_SDK_C_PARAMS_VERIFICATION(abi, NULL); BCOS_SDK_C_PARAMS_VERIFY_CONDITION((input[0] != '\0'), "input can not be empty string", NULL); BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must > 0", NULL); @@ -544,8 +542,6 @@ struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct_with { struct bcos_sdk_c_transaction_data* transaction_data_struct = (struct bcos_sdk_c_transaction_data*)malloc(sizeof(struct bcos_sdk_c_transaction_data)); - std::string toStr = to ? to : ""; - std::string abiStr = abi ? abi : ""; auto bytesInput = fromHexString(input); TransactionBuilder builder; std::string nonceStr = builder.generateRandomStr(); @@ -554,8 +550,8 @@ struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct_with transaction_data_struct->block_limit = block_limit; transaction_data_struct->group_id = my_strdup(group_id); transaction_data_struct->chain_id = my_strdup(chain_id); - transaction_data_struct->to = my_strdup(toStr.data()); - transaction_data_struct->abi = my_strdup(abiStr.data()); + transaction_data_struct->to = to ? my_strdup(to) : my_strdup(""); + transaction_data_struct->abi = abi ? my_strdup(abi) : my_strdup(""); transaction_data_struct->nonce = my_strdup(nonceStr.data()); transaction_data_struct->input = create_bytes_struct(bytesInput->size(), reinterpret_cast(bytesInput->data())); @@ -595,9 +591,7 @@ struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct_with bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(group_id, NULL); BCOS_SDK_C_PARAMS_VERIFICATION(chain_id, NULL); - // BCOS_SDK_C_PARAMS_VERIFICATION(to, NULL); BCOS_SDK_C_PARAMS_VERIFICATION(bytes_input, NULL); - // BCOS_SDK_C_PARAMS_VERIFICATION(abi, NULL); BCOS_SDK_C_PARAMS_VERIFY_CONDITION( (bytes_input_length > 0), "bytes input length must > 0", NULL); BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must > 0", NULL); @@ -606,8 +600,6 @@ struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct_with { struct bcos_sdk_c_transaction_data* transaction_data_struct = (struct bcos_sdk_c_transaction_data*)malloc(sizeof(struct bcos_sdk_c_transaction_data)); - std::string toStr = to ? to : ""; - std::string abiStr = abi ? abi : ""; TransactionBuilder builder; std::string nonceStr = builder.generateRandomStr(); @@ -615,8 +607,8 @@ struct bcos_sdk_c_transaction_data* bcos_sdk_create_transaction_data_struct_with transaction_data_struct->block_limit = block_limit; transaction_data_struct->group_id = my_strdup(group_id); transaction_data_struct->chain_id = my_strdup(chain_id); - transaction_data_struct->to = my_strdup(toStr.data()); - transaction_data_struct->abi = my_strdup(abiStr.data()); + transaction_data_struct->to = to ? my_strdup(to) : my_strdup(""); + transaction_data_struct->abi = abi ? my_strdup(abi) : my_strdup(""); transaction_data_struct->nonce = my_strdup(nonceStr.data()); transaction_data_struct->input = create_bytes_struct( bytes_input_length, const_cast(reinterpret_cast(bytes_input))); @@ -694,7 +686,7 @@ void bcos_sdk_destroy_transaction_data_struct(struct bcos_sdk_c_transaction_data * @param transaction_data: struct bcos_sdk_c_transaction_data* * @return const char* */ -const char* bcos_sdk_encode_transaction_data_struct( +const char* bcos_sdk_encode_transaction_data_struct_to_hex( struct bcos_sdk_c_transaction_data* transaction_data) { bcos_sdk_clear_last_error(); @@ -711,7 +703,7 @@ const char* bcos_sdk_encode_transaction_data_struct( catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_data_struct") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_data_struct_to_hex") << LOG_DESC("exception") << LOG_KV("transaction_data", transaction_data) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); @@ -754,7 +746,7 @@ const char* bcos_sdk_encode_transaction_data_struct_to_json( * @param transaction_data_hex_str * @return struct bcos_sdk_c_transaction_data* */ -struct bcos_sdk_c_transaction_data* bcos_sdk_decode_transaction_data_struct( +struct bcos_sdk_c_transaction_data* bcos_sdk_decode_transaction_data_struct_from_hex( const char* transaction_data_hex_str) { bcos_sdk_clear_last_error(); @@ -794,7 +786,7 @@ struct bcos_sdk_c_transaction_data* bcos_sdk_decode_transaction_data_struct( * nonce:string * @return struct bcos_sdk_c_transaction* */ -struct bcos_sdk_c_transaction_data* bcos_sdk_decode_transaction_data_struct_with_json( +struct bcos_sdk_c_transaction_data* bcos_sdk_decode_transaction_data_struct_from_json( const char* transaction_data_json_str) { bcos_sdk_clear_last_error(); @@ -813,7 +805,7 @@ struct bcos_sdk_c_transaction_data* bcos_sdk_decode_transaction_data_struct_with catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_data_struct_with_json") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_data_struct_from_json") << LOG_DESC("exception") << LOG_KV("transaction_data_json_str", transaction_data_json_str) << LOG_KV("error", errorMsg); @@ -886,7 +878,7 @@ const char* bcos_sdk_calc_transaction_data_struct_hash_with_hex( try { struct bcos_sdk_c_transaction_data* transaction_data_struct = - bcos_sdk_decode_transaction_data_struct(transaction_data_hex); + bcos_sdk_decode_transaction_data_struct_from_hex(transaction_data_hex); auto tars_transaction_data = convert_transaction_data_to_tars(transaction_data_struct); TransactionBuilder builder; auto transactionDataHash = builder.calculateTransactionDataHash( @@ -1073,7 +1065,7 @@ const char* bcos_sdk_create_encoded_transaction( * @param transaction: struct bcos_sdk_c_transaction* * @return const char* */ -const char* bcos_sdk_encode_transaction_struct(struct bcos_sdk_c_transaction* transaction) +const char* bcos_sdk_encode_transaction_struct_to_hex(struct bcos_sdk_c_transaction* transaction) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(transaction, NULL); @@ -1089,7 +1081,7 @@ const char* bcos_sdk_encode_transaction_struct(struct bcos_sdk_c_transaction* tr catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_struct") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_struct_to_hex") << LOG_DESC("exception") << LOG_KV("transaction", transaction) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); @@ -1118,7 +1110,7 @@ const char* bcos_sdk_encode_transaction_struct_to_json(struct bcos_sdk_c_transac catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_struct_to_json") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_struct_to_hex") << LOG_DESC("exception") << LOG_KV("transaction", transaction) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); @@ -1133,7 +1125,8 @@ const char* bcos_sdk_encode_transaction_struct_to_json(struct bcos_sdk_c_transac * @param transaction_hex_str * @return struct bcos_sdk_c_transaction* */ -struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct(const char* transaction_hex_str) +struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct_from_hex( + const char* transaction_hex_str) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(transaction_hex_str, NULL); @@ -1168,7 +1161,7 @@ struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct(const char* tr * @param transaction_json_str * @return struct bcos_sdk_c_transaction* */ -struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct_with_json( +struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct_from_json( const char* transaction_json_str) { bcos_sdk_clear_last_error(); @@ -1186,7 +1179,7 @@ struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct_with_json( catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_struct_with_json") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_struct_from_json") << LOG_DESC("exception") << LOG_KV("transaction_json_str", transaction_json_str) << LOG_KV("error", errorMsg); @@ -1208,25 +1201,16 @@ struct bcos_sdk_c_transaction_data_v2* bcos_sdk_create_transaction_data_struct_w BCOS_SDK_C_PARAMS_VERIFY_CONDITION((input[0] != '\0'), "input can not be empty string", NULL); BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must > 0", NULL); BCOS_SDK_C_PARAMS_VERIFICATION(value, NULL); - BCOS_SDK_C_PARAMS_VERIFY_CONDITION((value[0] != '\0'), "value can not be empty string", NULL); BCOS_SDK_C_PARAMS_VERIFICATION(gas_price, NULL); - BCOS_SDK_C_PARAMS_VERIFY_CONDITION( - (gas_price[0] != '\0'), "gas_price can not be empty string", NULL); BCOS_SDK_C_PARAMS_VERIFY_CONDITION((gas_limit >= 0), "gas limit must >= 0", NULL); BCOS_SDK_C_PARAMS_VERIFICATION(max_fee_per_gas, NULL); - BCOS_SDK_C_PARAMS_VERIFY_CONDITION( - (max_fee_per_gas[0] != '\0'), "max_fee_per_gas can not be empty string", NULL); BCOS_SDK_C_PARAMS_VERIFICATION(max_priority_fee_per_gas, NULL); - BCOS_SDK_C_PARAMS_VERIFY_CONDITION((max_priority_fee_per_gas[0] != '\0'), - "max_priority_fee_per_gas can not be empty string", NULL); try { struct bcos_sdk_c_transaction_data_v2* transaction_data_struct_v2 = (struct bcos_sdk_c_transaction_data_v2*)malloc( sizeof(struct bcos_sdk_c_transaction_data_v2)); - std::string toStr = to ? to : ""; - std::string abiStr = abi ? abi : ""; auto bytesInput = fromHexString(input); TransactionBuilder builder; std::string nonceStr = builder.generateRandomStr(); @@ -1235,8 +1219,8 @@ struct bcos_sdk_c_transaction_data_v2* bcos_sdk_create_transaction_data_struct_w transaction_data_struct_v2->block_limit = block_limit; transaction_data_struct_v2->group_id = my_strdup(group_id); transaction_data_struct_v2->chain_id = my_strdup(chain_id); - transaction_data_struct_v2->to = my_strdup(toStr.data()); - transaction_data_struct_v2->abi = my_strdup(abiStr.data()); + transaction_data_struct_v2->to = to ? my_strdup(to) : my_strdup(""); + transaction_data_struct_v2->abi = abi ? my_strdup(abi) : my_strdup(""); transaction_data_struct_v2->nonce = my_strdup(nonceStr.data()); transaction_data_struct_v2->input = create_bytes_struct(bytesInput->size(), reinterpret_cast(bytesInput->data())); @@ -1280,25 +1264,16 @@ struct bcos_sdk_c_transaction_data_v2* bcos_sdk_create_transaction_data_struct_w (bytes_input_length > 0), "bytes input length must > 0", NULL); BCOS_SDK_C_PARAMS_VERIFY_CONDITION((block_limit > 0), "block limit must > 0", NULL); BCOS_SDK_C_PARAMS_VERIFICATION(value, NULL); - BCOS_SDK_C_PARAMS_VERIFY_CONDITION((value[0] != '\0'), "value can not be empty string", NULL); BCOS_SDK_C_PARAMS_VERIFICATION(gas_price, NULL); - BCOS_SDK_C_PARAMS_VERIFY_CONDITION( - (gas_price[0] != '\0'), "gas_price can not be empty string", NULL); BCOS_SDK_C_PARAMS_VERIFY_CONDITION((gas_limit >= 0), "gas limit must >= 0", NULL); BCOS_SDK_C_PARAMS_VERIFICATION(max_fee_per_gas, NULL); - BCOS_SDK_C_PARAMS_VERIFY_CONDITION( - (max_fee_per_gas[0] != '\0'), "max_fee_per_gas can not be empty string", NULL); BCOS_SDK_C_PARAMS_VERIFICATION(max_priority_fee_per_gas, NULL); - BCOS_SDK_C_PARAMS_VERIFY_CONDITION((max_priority_fee_per_gas[0] != '\0'), - "max_priority_fee_per_gas can not be empty string", NULL); try { struct bcos_sdk_c_transaction_data_v2* transaction_data_struct_v2 = (struct bcos_sdk_c_transaction_data_v2*)malloc( sizeof(struct bcos_sdk_c_transaction_data_v2)); - std::string toStr = to ? to : ""; - std::string abiStr = abi ? abi : ""; TransactionBuilder builder; std::string nonceStr = builder.generateRandomStr(); @@ -1306,8 +1281,8 @@ struct bcos_sdk_c_transaction_data_v2* bcos_sdk_create_transaction_data_struct_w transaction_data_struct_v2->block_limit = block_limit; transaction_data_struct_v2->group_id = my_strdup(group_id); transaction_data_struct_v2->chain_id = my_strdup(chain_id); - transaction_data_struct_v2->to = my_strdup(toStr.data()); - transaction_data_struct_v2->abi = my_strdup(abiStr.data()); + transaction_data_struct_v2->to = to ? my_strdup(to) : my_strdup(""); + transaction_data_struct_v2->abi = abi ? my_strdup(abi) : my_strdup(""); transaction_data_struct_v2->nonce = my_strdup(nonceStr.data()); transaction_data_struct_v2->input = create_bytes_struct( bytes_input_length, const_cast(reinterpret_cast(bytes_input))); @@ -1404,7 +1379,7 @@ void bcos_sdk_destroy_transaction_data_struct_v2( bcos_sdk_c_free(transaction_data); } -const char* bcos_sdk_encode_transaction_data_struct_v2( +const char* bcos_sdk_encode_transaction_data_struct_to_hex_v2( struct bcos_sdk_c_transaction_data_v2* transaction_data) { bcos_sdk_clear_last_error(); @@ -1421,7 +1396,7 @@ const char* bcos_sdk_encode_transaction_data_struct_v2( catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_data_struct_v2") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_data_struct_to_hex_v2") << LOG_DESC("exception") << LOG_KV("transaction_data", transaction_data) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); @@ -1454,7 +1429,7 @@ const char* bcos_sdk_encode_transaction_data_struct_to_json_v2( return NULL; } -struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_v2( +struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_from_hex_v2( const char* transaction_data_hex_str) { bcos_sdk_clear_last_error(); @@ -1482,7 +1457,7 @@ struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_v return NULL; } -struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_with_json_v2( +struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_from_json_v2( const char* transaction_data_json_str) { bcos_sdk_clear_last_error(); @@ -1501,7 +1476,7 @@ struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_w catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_data_struct_with_json_v2") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_data_struct_from_json_v2") << LOG_DESC("exception") << LOG_KV("transaction_data_json_str", transaction_data_json_str) << LOG_KV("error", errorMsg); @@ -1560,7 +1535,7 @@ const char* bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2( try { struct bcos_sdk_c_transaction_data_v2* transaction_data_struct_v2 = - bcos_sdk_decode_transaction_data_struct_v2(transaction_data_hex); + bcos_sdk_decode_transaction_data_struct_from_hex_v2(transaction_data_hex); auto tars_transaction_data_v2 = convert_transaction_data_to_tars_v2(transaction_data_struct_v2); TransactionBuilder builder; @@ -1719,7 +1694,8 @@ const char* bcos_sdk_create_encoded_transaction_v2( return NULL; } -const char* bcos_sdk_encode_transaction_struct_v2(struct bcos_sdk_c_transaction_v2* transaction) +const char* bcos_sdk_encode_transaction_struct_to_hex_v2( + struct bcos_sdk_c_transaction_v2* transaction) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(transaction, NULL); @@ -1735,7 +1711,7 @@ const char* bcos_sdk_encode_transaction_struct_v2(struct bcos_sdk_c_transaction_ catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_struct_v2") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_struct_to_hex_v2") << LOG_DESC("exception") << LOG_KV("transaction", transaction) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); @@ -1768,7 +1744,7 @@ const char* bcos_sdk_encode_transaction_struct_to_json_v2( return NULL; } -struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_v2( +struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_from_hex_v2( const char* transaction_hex_str) { bcos_sdk_clear_last_error(); @@ -1798,7 +1774,7 @@ struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_v2( return NULL; } -struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_with_json_v2( +struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_from_json_v2( const char* transaction_json_str) { bcos_sdk_clear_last_error(); @@ -1816,7 +1792,7 @@ struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_with_json_v catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_struct_with_json_v2") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_struct_from_json_v2") << LOG_DESC("exception") << LOG_KV("transaction_json_str", transaction_json_str) << LOG_KV("error", errorMsg); diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h index 4075c51c3..1f76c9968 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h @@ -73,7 +73,7 @@ void bcos_sdk_destroy_transaction_data_struct(struct bcos_sdk_c_transaction_data * @param transaction_data: struct bcos_sdk_c_transaction_data* * @return const char* */ -const char* bcos_sdk_encode_transaction_data_struct( +const char* bcos_sdk_encode_transaction_data_struct_to_hex( struct bcos_sdk_c_transaction_data* transaction_data); /** @@ -89,7 +89,7 @@ const char* bcos_sdk_encode_transaction_data_struct_to_json( * @param transaction_data_hex_str * @return struct bcos_sdk_c_transaction_data* */ -struct bcos_sdk_c_transaction_data* bcos_sdk_decode_transaction_data_struct( +struct bcos_sdk_c_transaction_data* bcos_sdk_decode_transaction_data_struct_from_hex( const char* transaction_data_hex_str); /** @@ -104,7 +104,7 @@ struct bcos_sdk_c_transaction_data* bcos_sdk_decode_transaction_data_struct( * nonce:string * @return struct bcos_sdk_c_transaction_data* */ -struct bcos_sdk_c_transaction_data* bcos_sdk_decode_transaction_data_struct_with_json( +struct bcos_sdk_c_transaction_data* bcos_sdk_decode_transaction_data_struct_from_json( const char* transaction_data_json_str); /** @@ -166,7 +166,7 @@ const char* bcos_sdk_create_encoded_transaction( * @param transaction: struct bcos_sdk_c_transaction* * @return const char* */ -const char* bcos_sdk_encode_transaction_struct(struct bcos_sdk_c_transaction* transaction); +const char* bcos_sdk_encode_transaction_struct_to_hex(struct bcos_sdk_c_transaction* transaction); /** * @brief convert transaction into json format @@ -182,7 +182,8 @@ const char* bcos_sdk_encode_transaction_struct_to_json(struct bcos_sdk_c_transac * @param transaction_hex_str * @return struct bcos_sdk_c_transaction* */ -struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct(const char* transaction_hex_str); +struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct_from_hex( + const char* transaction_hex_str); /** * @brief @@ -190,27 +191,34 @@ struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct(const char* tr * @param transaction_json_str * @return struct bcos_sdk_c_transaction* */ -struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct_with_json( +struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct_from_json( const char* transaction_json_str); struct bcos_sdk_c_transaction_data_v2* bcos_sdk_create_transaction_data_struct_with_hex_input_v2( const char* group_id, const char* chain_id, const char* to, const char* input, const char* abi, - int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit, const char* max_fee_per_gas, const char* max_priority_fee_per_gas); + int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit, + const char* max_fee_per_gas, const char* max_priority_fee_per_gas); struct bcos_sdk_c_transaction_data_v2* bcos_sdk_create_transaction_data_struct_with_bytes_v2( const char* group_id, const char* chain_id, const char* to, const unsigned char* bytes_input, - uint32_t bytes_input_length, const char* abi, int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit, const char* max_fee_per_gas, const char* max_priority_fee_per_gas); + uint32_t bytes_input_length, const char* abi, int64_t block_limit, const char* value, + const char* gas_price, int64_t gas_limit, const char* max_fee_per_gas, + const char* max_priority_fee_per_gas); + -void bcos_sdk_destroy_transaction_data_struct_v2(struct bcos_sdk_c_transaction_data_v2* transaction_data); +void bcos_sdk_destroy_transaction_data_struct_v2( + struct bcos_sdk_c_transaction_data_v2* transaction_data); -const char* bcos_sdk_encode_transaction_data_struct_v2(struct bcos_sdk_c_transaction_data_v2* transaction_data); +const char* bcos_sdk_encode_transaction_data_struct_to_hex_v2( + struct bcos_sdk_c_transaction_data_v2* transaction_data); -const char* bcos_sdk_encode_transaction_data_struct_to_json_v2(struct bcos_sdk_c_transaction_data_v2* transaction_data); +const char* bcos_sdk_encode_transaction_data_struct_to_json_v2( + struct bcos_sdk_c_transaction_data_v2* transaction_data); -struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_v2( +struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_from_hex_v2( const char* transaction_data_hex_str); -struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_with_json_v2( +struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_from_json_v2( const char* transaction_data_json_str); const char* bcos_sdk_calc_transaction_data_struct_hash_v2( @@ -229,14 +237,16 @@ const char* bcos_sdk_create_encoded_transaction_v2( struct bcos_sdk_c_transaction_data_v2* transaction_data, const char* signature, const char* transaction_data_hash, int32_t attribute, const char* extra_data); -const char* bcos_sdk_encode_transaction_struct_v2(struct bcos_sdk_c_transaction_v2* transaction); +const char* bcos_sdk_encode_transaction_struct_to_hex_v2( + struct bcos_sdk_c_transaction_v2* transaction); -const char* bcos_sdk_encode_transaction_struct_to_json_v2(struct bcos_sdk_c_transaction_v2* transaction); +const char* bcos_sdk_encode_transaction_struct_to_json_v2( + struct bcos_sdk_c_transaction_v2* transaction); -struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_v2( +struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_from_hex_v2( const char* transaction_hex_str); -struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_with_json_v2( +struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_from_json_v2( const char* transaction_json_str); #ifdef __cplusplus diff --git a/bindings/java/jni/scripts/java2jni.sh b/bindings/java/jni/scripts/java2jni.sh index 35c0f91ec..fd00b93da 100644 --- a/bindings/java/jni/scripts/java2jni.sh +++ b/bindings/java/jni/scripts/java2jni.sh @@ -30,7 +30,7 @@ function convert_java_to_jni() { # convert_java_to_jni ${JAVA_SOURCE_PATH} # classes="rpc.RpcJniObj amop.AmopJniObj event.EventSubJniObj BcosSDKJniObj" -classes="BcosSDKJniObj utilities.keypair.KeyPairJniObj utilities.tx.TransactionBuilderJniObj utilities.tx.TransactionBuilderV2JniObj utilities.tx.TransactionStructBuilderJniObj utilities.receipt.ReceiptBuilderJniObj utilities.signature.SignatureJniObj" +classes="BcosSDKJniObj utilities.keypair.KeyPairJniObj utilities.tx.TransactionBuilderJniObj utilities.tx.TransactionBuilderV2JniObj utilities.tx.TransactionStructBuilderJniObj utilities.tx.TransactionStructBuilderV2JniObj utilities.receipt.ReceiptBuilderJniObj utilities.signature.SignatureJniObj" for class in ${classes} do diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp index e96679b59..b7d528df0 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp @@ -2,6 +2,7 @@ #include "bcos-c-sdk/bcos_sdk_c_error.h" #include "bcos-c-sdk/bcos_sdk_c_uti_keypair.h" #include "bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h" +#include "org_fisco_bcos_sdk_common.h" #include "org_fisco_bcos_sdk_exception.h" struct bcos_sdk_c_bytes* convert_to_bytes_struct(JNIEnv* env, jobject bytesObj) @@ -85,6 +86,13 @@ struct bcos_sdk_c_transaction_data* convert_to_tx_data_struct( { try { + if (transactionObject == NULL) + { + THROW_JNI_EXCEPTION( + env, "exception in convert_to_tx_data_struct, transactionObject is nullptr"); + return NULL; + } + bcos_sdk_c_transaction_data* tx_data_struct = (struct bcos_sdk_c_transaction_data*)malloc(sizeof(struct bcos_sdk_c_transaction_data)); jclass txDataClass = env->GetObjectClass(transactionObject); @@ -135,6 +143,7 @@ struct bcos_sdk_c_transaction_data* convert_to_tx_data_struct( if (inputStruct == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_struct"); + return NULL; } tx_data_struct->input = inputStruct; @@ -158,6 +167,13 @@ jobject convert_to_tx_data_jobject( { try { + if (transactionData == NULL) + { + THROW_JNI_EXCEPTION( + env, "exception in convert_to_tx_data_jobject, transactionData is nullptr"); + return NULL; + } + jclass txDataClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionData"); if (txDataClass == NULL) { @@ -199,6 +215,7 @@ jobject convert_to_tx_data_jobject( if (jInput == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_jobject"); + return NULL; } jfieldID inputField = env->GetFieldID( txDataClass, "input", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); @@ -219,6 +236,12 @@ jobject convert_to_tx_jobject(JNIEnv* env, const struct bcos_sdk_c_transaction* { try { + if (tx_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_jobject, tx_struct is nullptr"); + return NULL; + } + jclass txClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/Transaction"); if (txClass == NULL) { @@ -249,6 +272,7 @@ jobject convert_to_tx_jobject(JNIEnv* env, const struct bcos_sdk_c_transaction* if (javaTxDataObj == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_jobject"); + return NULL; } env->SetObjectField(javaTxObj, txDataField, javaTxDataObj); // DataHash @@ -256,6 +280,7 @@ jobject convert_to_tx_jobject(JNIEnv* env, const struct bcos_sdk_c_transaction* if (javaDataHashObj == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_jobject"); + return NULL; } env->SetObjectField(javaTxObj, dataHashField, javaDataHashObj); // Signature @@ -263,14 +288,11 @@ jobject convert_to_tx_jobject(JNIEnv* env, const struct bcos_sdk_c_transaction* if (javaSignatureObj == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_jobject"); + return NULL; } env->SetObjectField(javaTxObj, signatureField, javaSignatureObj); // Sender jobject javaSenderObj = convert_to_bytes_jobject(env, tx_struct->sender); - if (javaSenderObj == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_jobject"); - } env->SetObjectField(javaTxObj, senderField, javaSenderObj); // ImportTime env->SetLongField(javaTxObj, importTimeField, tx_struct->import_time); @@ -292,6 +314,12 @@ struct bcos_sdk_c_transaction* convert_to_tx_struct(JNIEnv* env, jobject jTxObj) { try { + if (jTxObj == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct, jTxObj is nullptr"); + return NULL; + } + jclass javaTxClass = env->GetObjectClass(jTxObj); jfieldID transactionDataField = env->GetFieldID(javaTxClass, "transactionData", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;"); @@ -314,6 +342,7 @@ struct bcos_sdk_c_transaction* convert_to_tx_struct(JNIEnv* env, jobject jTxObj) if (txDataStruct == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); + return NULL; } txStruct->transaction_data = txDataStruct; // DataHash @@ -322,6 +351,7 @@ struct bcos_sdk_c_transaction* convert_to_tx_struct(JNIEnv* env, jobject jTxObj) if (dataHashStruct == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_struct"); + return NULL; } txStruct->data_hash = dataHashStruct; // Signature @@ -330,15 +360,12 @@ struct bcos_sdk_c_transaction* convert_to_tx_struct(JNIEnv* env, jobject jTxObj) if (signatureStruct == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_struct"); + return NULL; } txStruct->signature = signatureStruct; // Sender jobject javaSenderObj = env->GetObjectField(jTxObj, senderField); struct bcos_sdk_c_bytes* senderStruct = convert_to_bytes_struct(env, javaSenderObj); - if (senderStruct == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_struct"); - } txStruct->sender = senderStruct; // ImportTime jlong importTimeValue = env->GetLongField(jTxObj, importTimeField); @@ -361,20 +388,6 @@ struct bcos_sdk_c_transaction* convert_to_tx_struct(JNIEnv* env, jobject jTxObj) } } -void destroy_bytes_jobject(JNIEnv* env, jobject jTxBytes) -{ - if (jTxBytes == NULL) - { - return; - } - - jclass jTxBytesClass = env->GetObjectClass(jTxBytes); - jfieldID jBufferFieldID = env->GetFieldID(jTxBytesClass, "buffer", "[B"); - jbyteArray jBufferArray = (jbyteArray)env->GetObjectField(jTxBytes, jBufferFieldID); - - env->DeleteLocalRef(jBufferArray); -} - /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj * Method: encodeTransactionDataStruct @@ -389,11 +402,13 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr if (tx_data_struct == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); + return nullptr; } - const char* tx_data_hex = bcos_sdk_encode_transaction_data_struct(tx_data_struct); + const char* tx_data_hex = bcos_sdk_encode_transaction_data_struct_to_hex(tx_data_struct); if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return nullptr; } jstring jTxDataHex = env->NewStringUTF(tx_data_hex); @@ -425,11 +440,13 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr if (tx_data_struct == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); + return nullptr; } const char* tx_data_json = bcos_sdk_encode_transaction_data_struct_to_json(tx_data_struct); if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return nullptr; } jstring jTxDataJson = env->NewStringUTF(tx_data_json); @@ -456,18 +473,21 @@ JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionDataStruct( JNIEnv* env, jclass, jstring jTxDataHexStr) { + checkJString(env, jTxDataHexStr); const char* tx_data_hex_str = env->GetStringUTFChars(jTxDataHexStr, nullptr); struct bcos_sdk_c_transaction_data* tx_data_struct = - bcos_sdk_decode_transaction_data_struct(tx_data_hex_str); + bcos_sdk_decode_transaction_data_struct_from_hex(tx_data_hex_str); if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return nullptr; } jobject jTxDataObj = convert_to_tx_data_jobject(env, tx_data_struct); if (jTxDataObj == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_jobject"); + return nullptr; } // release source @@ -494,6 +514,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_calcTran if (tx_data_struct == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); + return nullptr; } int crypto_type = jCrytpTyte; const char* tx_data_hash = @@ -501,6 +522,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_calcTran if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return nullptr; } jstring jTxDataHash = env->NewStringUTF(tx_data_hash); @@ -530,10 +552,15 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_createEn JNIEnv* env, jclass, jobject jTxDataObj, jstring jSignature, jstring jTxDataHash, jint jAttribute, jstring jExtraData) { + checkJString(env, jSignature); + checkJString(env, jTxDataHash); + checkJString(env, jExtraData); + struct bcos_sdk_c_transaction_data* tx_data_struct = convert_to_tx_data_struct(env, jTxDataObj); if (tx_data_struct == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); + return nullptr; } const char* signature = env->GetStringUTFChars(jSignature, NULL); const char* tx_data_hash = env->GetStringUTFChars(jTxDataHash, NULL); @@ -545,6 +572,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_createEn if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return nullptr; } jstring jTxStr = env->NewStringUTF(tx_str); @@ -579,11 +607,13 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr if (tx_struct == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct"); + return nullptr; } - const char* tx_hex = bcos_sdk_encode_transaction_struct(tx_struct); + const char* tx_hex = bcos_sdk_encode_transaction_struct_to_hex(tx_struct); if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return nullptr; } jstring jTxHex = env->NewStringUTF(tx_hex); @@ -615,11 +645,13 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr if (tx_struct == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct"); + return nullptr; } const char* tx_json = bcos_sdk_encode_transaction_struct_to_json(tx_struct); if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return nullptr; } jstring jTxJson = env->NewStringUTF(tx_json); @@ -646,14 +678,23 @@ JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionStruct( JNIEnv* env, jclass, jstring jTxHexStr) { + checkJString(env, jTxHexStr); + const char* tx_hex_str = env->GetStringUTFChars(jTxHexStr, nullptr); - struct bcos_sdk_c_transaction* tx_struct = bcos_sdk_decode_transaction_struct(tx_hex_str); + struct bcos_sdk_c_transaction* tx_struct = + bcos_sdk_decode_transaction_struct_from_hex(tx_hex_str); if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return nullptr; } jobject jTxObj = convert_to_tx_jobject(env, tx_struct); + if (jTxObj == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_jobject"); + return nullptr; + } // release source if (tx_struct) diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj.cpp new file mode 100644 index 000000000..c5a168c81 --- /dev/null +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj.cpp @@ -0,0 +1,724 @@ +#include "org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj.h" +#include "bcos-c-sdk/bcos_sdk_c_error.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_keypair.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h" +#include "org_fisco_bcos_sdk_common.h" +#include "org_fisco_bcos_sdk_exception.h" + + +struct bcos_sdk_c_bytes* convert_to_bytes_struct_v2(JNIEnv* env, jobject bytesObj) +{ + if (bytesObj == NULL) + { + return NULL; + } + + struct bcos_sdk_c_bytes* bytes = + (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); + jclass bytesClass = env->GetObjectClass(bytesObj); + jfieldID bufferField = env->GetFieldID(bytesClass, "buffer", "[B"); + jfieldID lengthField = env->GetFieldID(bytesClass, "length", "I"); + + jbyteArray bufferArray = (jbyteArray)env->GetObjectField(bytesObj, bufferField); + jbyte* bufferData = env->GetByteArrayElements(bufferArray, nullptr); + jsize bufferLength = env->GetArrayLength(bufferArray); + bytes->length = (uint32_t)env->GetIntField(bytesObj, lengthField); + bytes->buffer = (uint8_t*)malloc(bufferLength * sizeof(uint8_t)); + memcpy(bytes->buffer, bufferData, bufferLength); + + env->ReleaseByteArrayElements(bufferArray, bufferData, 0); + + return bytes; +} + +jobject convert_to_bytes_jobject_v2(JNIEnv* env, const struct bcos_sdk_c_bytes* bytes_struct) +{ + if (bytes_struct == NULL) + { + return NULL; + } + + jclass txBytesClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes"); + if (txBytesClass == NULL) + { + env->FatalError( + "No such class, className: " + "org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes"); + } + jmethodID txBytesMtd = env->GetMethodID(txBytesClass, "", "()V"); + if (txBytesMtd == NULL) + { + env->FatalError("No such constructor in TransactionBytes, constructor()"); + } + jfieldID bufferField = env->GetFieldID(txBytesClass, "buffer", "[B"); + jfieldID lengthField = env->GetFieldID(txBytesClass, "length", "I"); + + jobject jTxBytesObj = env->NewObject(txBytesClass, txBytesMtd); + // buffer + jbyteArray bufferArray = env->NewByteArray(bytes_struct->length); + env->SetByteArrayRegion( + bufferArray, 0, bytes_struct->length, reinterpret_cast(bytes_struct->buffer)); + env->SetObjectField(jTxBytesObj, bufferField, bufferArray); + // length + env->SetIntField(jTxBytesObj, lengthField, bytes_struct->length); + + env->DeleteLocalRef(bufferArray); + + return jTxBytesObj; +} + +struct bcos_sdk_c_transaction_data_v2* convert_to_tx_data_struct_v2( + JNIEnv* env, jobject transactionObject) +{ + if (transactionObject == NULL) + { + THROW_JNI_EXCEPTION( + env, "exception in convert_to_tx_data_struct_v2, transactionObject is nullptr"); + return NULL; + } + + bcos_sdk_c_transaction_data_v2* tx_data_struct_v2 = + (struct bcos_sdk_c_transaction_data_v2*)malloc( + sizeof(struct bcos_sdk_c_transaction_data_v2)); + jclass txDataClass = env->GetObjectClass(transactionObject); + + // version + jfieldID versionField = env->GetFieldID(txDataClass, "version", "I"); + tx_data_struct_v2->version = env->GetIntField(transactionObject, versionField); + + // blockLimit + jfieldID blockLimitField = env->GetFieldID(txDataClass, "blockLimit", "J"); + tx_data_struct_v2->block_limit = env->GetLongField(transactionObject, blockLimitField); + + // chainId + jfieldID chainIdField = env->GetFieldID(txDataClass, "chainId", "Ljava/lang/String;"); + jstring chainIdString = (jstring)env->GetObjectField(transactionObject, chainIdField); + const char* chainIdValue = env->GetStringUTFChars(chainIdString, nullptr); + tx_data_struct_v2->chain_id = strdup(chainIdValue); + + // groupId + jfieldID groupIdField = env->GetFieldID(txDataClass, "groupId", "Ljava/lang/String;"); + jstring groupIdString = (jstring)env->GetObjectField(transactionObject, groupIdField); + const char* groupIdValue = env->GetStringUTFChars(groupIdString, nullptr); + tx_data_struct_v2->group_id = strdup(groupIdValue); + + // nonce + jfieldID nonceField = env->GetFieldID(txDataClass, "nonce", "Ljava/lang/String;"); + jstring nonceString = (jstring)env->GetObjectField(transactionObject, nonceField); + const char* nonceValue = env->GetStringUTFChars(nonceString, nullptr); + tx_data_struct_v2->nonce = strdup(nonceValue); + + // to + jfieldID toField = env->GetFieldID(txDataClass, "to", "Ljava/lang/String;"); + jstring toString = (jstring)env->GetObjectField(transactionObject, toField); + const char* toValue = env->GetStringUTFChars(toString, nullptr); + tx_data_struct_v2->to = strdup(toValue); + + // abi + jfieldID abiField = env->GetFieldID(txDataClass, "abi", "Ljava/lang/String;"); + jstring abiString = (jstring)env->GetObjectField(transactionObject, abiField); + const char* abiValue = env->GetStringUTFChars(abiString, nullptr); + tx_data_struct_v2->abi = strdup(abiValue); + + // input + jfieldID inputField = env->GetFieldID( + txDataClass, "input", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jobject inputObject = env->GetObjectField(transactionObject, inputField); + struct bcos_sdk_c_bytes* inputStruct = convert_to_bytes_struct_v2(env, inputObject); + if (inputStruct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_struct_v2"); + return NULL; + } + tx_data_struct_v2->input = inputStruct; + + // value + jfieldID valueField = env->GetFieldID(txDataClass, "value", "Ljava/lang/String;"); + jstring valueString = (jstring)env->GetObjectField(transactionObject, valueField); + const char* valueChar = env->GetStringUTFChars(valueString, nullptr); + tx_data_struct_v2->value = strdup(valueChar); + + // gas_price + jfieldID gasPriceField = env->GetFieldID(txDataClass, "gasPrice", "Ljava/lang/String;"); + jstring gasPriceString = (jstring)env->GetObjectField(transactionObject, gasPriceField); + const char* gasPriceChar = env->GetStringUTFChars(gasPriceString, nullptr); + tx_data_struct_v2->gas_price = strdup(gasPriceChar); + + // gas_limit + jfieldID gasLimitField = env->GetFieldID(txDataClass, "gasLimit", "J"); + tx_data_struct_v2->gas_limit = env->GetLongField(transactionObject, gasLimitField); + + // max_fee_per_gas + jfieldID maxFeePerGasField = env->GetFieldID(txDataClass, "maxFeePerGas", "Ljava/lang/String;"); + jstring maxFeePerGasString = (jstring)env->GetObjectField(transactionObject, maxFeePerGasField); + const char* maxFeePerGasChar = env->GetStringUTFChars(maxFeePerGasString, nullptr); + tx_data_struct_v2->max_fee_per_gas = strdup(maxFeePerGasChar); + + // max_priority_fee_per_gas + jfieldID maxPriorityFeePerGasField = + env->GetFieldID(txDataClass, "maxPriorityFeePerGas", "Ljava/lang/String;"); + jstring maxPriorityFeePerGasString = + (jstring)env->GetObjectField(transactionObject, maxPriorityFeePerGasField); + const char* maxPriorityFeePerGasChar = + env->GetStringUTFChars(maxPriorityFeePerGasString, nullptr); + tx_data_struct_v2->max_priority_fee_per_gas = strdup(maxPriorityFeePerGasChar); + + // release source + env->ReleaseStringUTFChars(chainIdString, chainIdValue); + env->ReleaseStringUTFChars(groupIdString, groupIdValue); + env->ReleaseStringUTFChars(nonceString, nonceValue); + env->ReleaseStringUTFChars(toString, toValue); + env->ReleaseStringUTFChars(abiString, abiValue); + env->ReleaseStringUTFChars(valueString, valueChar); + env->ReleaseStringUTFChars(gasPriceString, gasPriceChar); + env->ReleaseStringUTFChars(maxFeePerGasString, maxFeePerGasChar); + env->ReleaseStringUTFChars(maxPriorityFeePerGasString, maxPriorityFeePerGasChar); + + return tx_data_struct_v2; +} + +jobject convert_to_tx_data_jobject_v2( + JNIEnv* env, const struct bcos_sdk_c_transaction_data_v2* transactionDataV2) +{ + if (transactionDataV2 == nullptr) + { + THROW_JNI_EXCEPTION( + env, "exception in convert_to_tx_data_jobject_v2, transactionDataV2 is nullptr"); + return NULL; + } + + jclass txDataClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2"); + if (txDataClass == NULL) + { + env->FatalError( + "No such class, className: " + "org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2"); + } + jmethodID txDataMtd = env->GetMethodID(txDataClass, "", "()V"); + if (txDataMtd == NULL) + { + env->FatalError("No such constructor in TransactionDataV2, constructor()"); + } + jobject jTxDataObj = env->NewObject(txDataClass, txDataMtd); + + // version + jfieldID versionField = env->GetFieldID(txDataClass, "version", "I"); + env->SetIntField(jTxDataObj, versionField, transactionDataV2->version); + // block_limit + jfieldID blockLimitField = env->GetFieldID(txDataClass, "blockLimit", "J"); + env->SetLongField(jTxDataObj, blockLimitField, transactionDataV2->block_limit); + // chain_id + jfieldID chainIdField = env->GetFieldID(txDataClass, "chainId", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, chainIdField, env->NewStringUTF(transactionDataV2->chain_id)); + // group_id + jfieldID groupIdField = env->GetFieldID(txDataClass, "groupId", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, groupIdField, env->NewStringUTF(transactionDataV2->group_id)); + // nonce + jfieldID nonceField = env->GetFieldID(txDataClass, "nonce", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, nonceField, env->NewStringUTF(transactionDataV2->nonce)); + // to + jfieldID toField = env->GetFieldID(txDataClass, "to", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, toField, env->NewStringUTF(transactionDataV2->to)); + // abi + jfieldID abiField = env->GetFieldID(txDataClass, "abi", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, abiField, env->NewStringUTF(transactionDataV2->abi)); + // input + jobject jInput = convert_to_bytes_jobject_v2(env, transactionDataV2->input); + if (jInput == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_jobject_v2"); + return NULL; + } + jfieldID inputField = env->GetFieldID( + txDataClass, "input", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + env->SetObjectField(jTxDataObj, inputField, jInput); + // value + jfieldID valueField = env->GetFieldID(txDataClass, "value", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, valueField, env->NewStringUTF(transactionDataV2->value)); + // gas_price + jfieldID gasPriceField = env->GetFieldID(txDataClass, "gasPrice", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, gasPriceField, env->NewStringUTF(transactionDataV2->gas_price)); + // gas_limit + jfieldID gasLimitField = env->GetFieldID(txDataClass, "gasLimit", "J"); + env->SetLongField(jTxDataObj, gasLimitField, transactionDataV2->gas_limit); + // max_fee_per_gas + jfieldID maxFeePerGasField = env->GetFieldID(txDataClass, "maxFeePerGas", "Ljava/lang/String;"); + env->SetObjectField( + jTxDataObj, maxFeePerGasField, env->NewStringUTF(transactionDataV2->max_fee_per_gas)); + // max_priority_fee_per_gas + jfieldID maxPriorityFeePerGasField = + env->GetFieldID(txDataClass, "maxPriorityFeePerGas", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, maxPriorityFeePerGasField, + env->NewStringUTF(transactionDataV2->max_priority_fee_per_gas)); + + // release + env->DeleteLocalRef(jInput); + + return jTxDataObj; +} + +jobject convert_to_tx_jobject_v2(JNIEnv* env, const struct bcos_sdk_c_transaction_v2* tx_struct_v2) +{ + if (tx_struct_v2 == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_jobject_v2, tx_struct_v2 is nullptr"); + return NULL; + } + + jclass txClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionV2"); + if (txClass == NULL) + { + env->FatalError( + "No such class, className: " + "org/fisco/bcos/sdk/jni/utilities/tx/TransactionV2"); + } + jmethodID txMtd = env->GetMethodID(txClass, "", "()V"); + if (txMtd == NULL) + { + env->FatalError("No such constructor in TransactionV2, constructor()"); + } + + jfieldID txDataField = env->GetFieldID( + txClass, "transactionData", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;"); + jfieldID dataHashField = env->GetFieldID( + txClass, "dataHash", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jfieldID signatureField = env->GetFieldID( + txClass, "signature", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jfieldID senderField = env->GetFieldID( + txClass, "sender", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jfieldID importTimeField = env->GetFieldID(txClass, "importTime", "J"); + jfieldID attributeField = env->GetFieldID(txClass, "attribute", "I"); + jfieldID extraDataField = env->GetFieldID(txClass, "extraData", "Ljava/lang/String;"); + + jobject javaTxObj = env->NewObject(txClass, txMtd); + // TransactionData + jobject javaTxDataObj = convert_to_tx_data_jobject_v2(env, tx_struct_v2->transaction_data); + if (javaTxDataObj == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_jobject_v2"); + return NULL; + } + env->SetObjectField(javaTxObj, txDataField, javaTxDataObj); + // DataHash + jobject javaDataHashObj = convert_to_bytes_jobject_v2(env, tx_struct_v2->data_hash); + if (javaDataHashObj == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_jobject_v2"); + return NULL; + } + env->SetObjectField(javaTxObj, dataHashField, javaDataHashObj); + // Signature + jobject javaSignatureObj = convert_to_bytes_jobject_v2(env, tx_struct_v2->signature); + if (javaSignatureObj == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_jobject_v2"); + return NULL; + } + env->SetObjectField(javaTxObj, signatureField, javaSignatureObj); + // Sender + jobject javaSenderObj = convert_to_bytes_jobject_v2(env, tx_struct_v2->sender); + env->SetObjectField(javaTxObj, senderField, javaSenderObj); + // ImportTime + env->SetLongField(javaTxObj, importTimeField, tx_struct_v2->import_time); + // Attribute + env->SetIntField(javaTxObj, attributeField, tx_struct_v2->attribute); + // ExtraData + jstring javaExtraData = env->NewStringUTF(tx_struct_v2->extra_data); + env->SetObjectField(javaTxObj, extraDataField, javaExtraData); + + return javaTxObj; +} + +struct bcos_sdk_c_transaction_v2* convert_to_tx_struct_v2(JNIEnv* env, jobject jTxObj) +{ + if (jTxObj == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct_v2, jTxObj is nullptr"); + return NULL; + } + + jclass javaTxClass = env->GetObjectClass(jTxObj); + jfieldID transactionDataField = env->GetFieldID( + javaTxClass, "transactionData", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;"); + jfieldID dataHashField = env->GetFieldID( + javaTxClass, "dataHash", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jfieldID signatureField = env->GetFieldID( + javaTxClass, "signature", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jfieldID senderField = env->GetFieldID( + javaTxClass, "sender", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); + jfieldID importTimeField = env->GetFieldID(javaTxClass, "importTime", "J"); + jfieldID attributeField = env->GetFieldID(javaTxClass, "attribute", "I"); + jfieldID extraDataField = env->GetFieldID(javaTxClass, "extraData", "Ljava/lang/String;"); + + struct bcos_sdk_c_transaction_v2* txStruct = + (struct bcos_sdk_c_transaction_v2*)malloc(sizeof(struct bcos_sdk_c_transaction_v2)); + // TransactionData + jobject javaTxDataObj = env->GetObjectField(jTxObj, transactionDataField); + struct bcos_sdk_c_transaction_data_v2* txDataStruct = + convert_to_tx_data_struct_v2(env, javaTxDataObj); + if (txDataStruct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct_v2"); + return NULL; + } + txStruct->transaction_data = txDataStruct; + // DataHash + jobject javaDataHashObj = env->GetObjectField(jTxObj, dataHashField); + struct bcos_sdk_c_bytes* dataHashStruct = convert_to_bytes_struct_v2(env, javaDataHashObj); + if (dataHashStruct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_struct_v2"); + return NULL; + } + txStruct->data_hash = dataHashStruct; + // Signature + jobject javaSignatureObj = env->GetObjectField(jTxObj, signatureField); + struct bcos_sdk_c_bytes* signatureStruct = convert_to_bytes_struct_v2(env, javaSignatureObj); + if (signatureStruct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_struct_v2"); + return NULL; + } + txStruct->signature = signatureStruct; + // Sender + jobject javaSenderObj = env->GetObjectField(jTxObj, senderField); + struct bcos_sdk_c_bytes* senderStruct = convert_to_bytes_struct_v2(env, javaSenderObj); + txStruct->sender = senderStruct; + // ImportTime + jlong importTimeValue = env->GetLongField(jTxObj, importTimeField); + txStruct->import_time = (int64_t)importTimeValue; + // Attribute + jint attributeValue = env->GetIntField(jTxObj, attributeField); + txStruct->attribute = (int32_t)attributeValue; + // ExtraData + jstring javaExtraData = (jstring)env->GetObjectField(jTxObj, extraDataField); + const char* extraDataValue = env->GetStringUTFChars(javaExtraData, NULL); + txStruct->extra_data = strdup(extraDataValue); + + env->ReleaseStringUTFChars(javaExtraData, extraDataValue); + + return txStruct; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj + * Method: encodeTransactionDataStructV2 + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_encodeTransactionDataStructV2( + JNIEnv* env, jclass, jobject jTransactionDataObj) +{ + struct bcos_sdk_c_transaction_data_v2* tx_data_struct_v2 = + convert_to_tx_data_struct_v2(env, jTransactionDataObj); + if (tx_data_struct_v2 == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct_v2"); + return nullptr; + } + const char* tx_data_hex = bcos_sdk_encode_transaction_data_struct_to_hex_v2(tx_data_struct_v2); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return nullptr; + } + + jstring jTxDataHex = env->NewStringUTF(tx_data_hex); + + if (tx_data_hex) + { + free((void*)tx_data_hex); + tx_data_hex = NULL; + } + if (tx_data_struct_v2) + { + bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct_v2); + } + + return jTxDataHex; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj + * Method: encodeTransactionDataStructToJsonV2 + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_encodeTransactionDataStructToJsonV2( + JNIEnv* env, jclass, jobject jTransactionDataObj) +{ + struct bcos_sdk_c_transaction_data_v2* tx_data_struct_v2 = + convert_to_tx_data_struct_v2(env, jTransactionDataObj); + if (tx_data_struct_v2 == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct_v2"); + return nullptr; + } + const char* tx_data_json = + bcos_sdk_encode_transaction_data_struct_to_json_v2(tx_data_struct_v2); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return nullptr; + } + + jstring jTxDataJson = env->NewStringUTF(tx_data_json); + + if (tx_data_json) + { + free((void*)tx_data_json); + tx_data_json = NULL; + } + if (tx_data_struct_v2) + { + bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct_v2); + } + + return jTxDataJson; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj + * Method: decodeTransactionDataStructV2 + * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2; + */ +JNIEXPORT jobject JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_decodeTransactionDataStructV2( + JNIEnv* env, jclass, jstring jTxDataHexStr) +{ + checkJString(env, jTxDataHexStr); + + const char* tx_data_hex_str = env->GetStringUTFChars(jTxDataHexStr, nullptr); + struct bcos_sdk_c_transaction_data_v2* tx_data_struct_v2 = + bcos_sdk_decode_transaction_data_struct_from_hex_v2(tx_data_hex_str); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return nullptr; + } + + jobject jTxDataObj = convert_to_tx_data_jobject_v2(env, tx_data_struct_v2); + if (jTxDataObj == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_jobject_v2"); + return nullptr; + } + + // release source + if (tx_data_struct_v2) + { + bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct_v2); + } + env->ReleaseStringUTFChars(jTxDataHexStr, tx_data_hex_str); + + return jTxDataObj; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj + * Method: calcTransactionDataStructHashV2 + * Signature: (ILorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_calcTransactionDataStructHashV2( + JNIEnv* env, jclass, jint jCrytpTyte, jobject jTransactionDataObj) +{ + struct bcos_sdk_c_transaction_data_v2* tx_data_struct_v2 = + convert_to_tx_data_struct_v2(env, jTransactionDataObj); + if (tx_data_struct_v2 == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct_v2"); + return nullptr; + } + int crypto_type = jCrytpTyte; + const char* tx_data_hash = + bcos_sdk_calc_transaction_data_struct_hash_v2(crypto_type, tx_data_struct_v2); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return nullptr; + } + + jstring jTxDataHash = env->NewStringUTF(tx_data_hash); + + // release source + if (tx_data_struct_v2) + { + bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct_v2); + } + if (tx_data_hash) + { + free((void*)tx_data_hash); + tx_data_hash = NULL; + } + + return jTxDataHash; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj + * Method: createEncodedTransactionV2 + * Signature: + * (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_createEncodedTransactionV2( + JNIEnv* env, jclass, jobject jTxDataObj, jstring jSignature, jstring jTxDataHash, + jint jAttribute, jstring jExtraData) +{ + checkJString(env, jSignature); + checkJString(env, jTxDataHash); + checkJString(env, jExtraData); + + struct bcos_sdk_c_transaction_data_v2* tx_data_struct_v2 = + convert_to_tx_data_struct_v2(env, jTxDataObj); + if (tx_data_struct_v2 == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct_v2"); + return nullptr; + } + const char* signature = env->GetStringUTFChars(jSignature, NULL); + const char* tx_data_hash = env->GetStringUTFChars(jTxDataHash, NULL); + int attribute = jAttribute; + const char* extra_data = env->GetStringUTFChars(jExtraData, NULL); + + const char* tx_str = bcos_sdk_create_encoded_transaction_v2( + tx_data_struct_v2, signature, tx_data_hash, attribute, extra_data); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return nullptr; + } + + jstring jTxStr = env->NewStringUTF(tx_str); + + // release resource + env->ReleaseStringUTFChars(jSignature, signature); + env->ReleaseStringUTFChars(jTxDataHash, tx_data_hash); + env->ReleaseStringUTFChars(jExtraData, extra_data); + if (tx_data_struct_v2) + { + bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct_v2); + } + if (tx_str) + { + free((void*)tx_str); + tx_str = NULL; + } + + return jTxStr; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj + * Method: encodeTransactionStructV2 + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionV2;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_encodeTransactionStructV2( + JNIEnv* env, jclass, jobject jTransactionObj) +{ + struct bcos_sdk_c_transaction_v2* tx_struct_v2 = convert_to_tx_struct_v2(env, jTransactionObj); + if (tx_struct_v2 == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct_v2"); + return nullptr; + } + + const char* tx_hex = bcos_sdk_encode_transaction_struct_to_hex_v2(tx_struct_v2); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return nullptr; + } + + jstring jTxHex = env->NewStringUTF(tx_hex); + + // release source + if (tx_hex) + { + free((void*)tx_hex); + tx_hex = NULL; + } + if (tx_struct_v2) + { + bcos_sdk_destroy_transaction_struct_v2(tx_struct_v2); + } + + return jTxHex; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj + * Method: encodeTransactionStructToJsonV2 + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionV2;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_encodeTransactionStructToJsonV2( + JNIEnv* env, jclass, jobject jTransactionObj) +{ + struct bcos_sdk_c_transaction_v2* tx_struct_v2 = convert_to_tx_struct_v2(env, jTransactionObj); + if (tx_struct_v2 == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct_v2"); + return nullptr; + } + + const char* tx_json = bcos_sdk_encode_transaction_struct_to_json_v2(tx_struct_v2); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return nullptr; + } + + jstring jTxJson = env->NewStringUTF(tx_json); + + if (tx_json) + { + free((void*)tx_json); + tx_json = NULL; + } + if (tx_struct_v2) + { + bcos_sdk_destroy_transaction_struct_v2(tx_struct_v2); + } + + return jTxJson; +} + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj + * Method: decodeTransactionStructV2 + * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionV2; + */ +JNIEXPORT jobject JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_decodeTransactionStructV2( + JNIEnv* env, jclass, jstring jTxHexStr) +{ + checkJString(env, jTxHexStr); + + const char* tx_hex_str = env->GetStringUTFChars(jTxHexStr, nullptr); + struct bcos_sdk_c_transaction_v2* tx_struct_v2 = + bcos_sdk_decode_transaction_struct_from_hex_v2(tx_hex_str); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return nullptr; + } + + jobject jTxObj = convert_to_tx_jobject_v2(env, tx_struct_v2); + if (jTxObj == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_jobject_v2"); + return nullptr; + } + + // release source + if (tx_struct_v2) + { + bcos_sdk_destroy_transaction_struct_v2(tx_struct_v2); + } + env->ReleaseStringUTFChars(jTxHexStr, tx_hex_str); + + return jTxObj; +} diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj.h b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj.h new file mode 100644 index 000000000..425cda94e --- /dev/null +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj.h @@ -0,0 +1,77 @@ +/* DO NOT EDIT THIS FILE - it is machine generated */ +#include +/* Header for class org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj */ + +#ifndef _Included_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj +#define _Included_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj +#ifdef __cplusplus +extern "C" { +#endif +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj + * Method: encodeTransactionDataStructV2 + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_encodeTransactionDataStructV2 + (JNIEnv *, jclass, jobject); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj + * Method: encodeTransactionDataStructToJsonV2 + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_encodeTransactionDataStructToJsonV2 + (JNIEnv *, jclass, jobject); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj + * Method: decodeTransactionDataStructV2 + * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2; + */ +JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_decodeTransactionDataStructV2 + (JNIEnv *, jclass, jstring); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj + * Method: calcTransactionDataStructHashV2 + * Signature: (ILorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_calcTransactionDataStructHashV2 + (JNIEnv *, jclass, jint, jobject); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj + * Method: createEncodedTransactionV2 + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_createEncodedTransactionV2 + (JNIEnv *, jclass, jobject, jstring, jstring, jint, jstring); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj + * Method: encodeTransactionStructV2 + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionV2;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_encodeTransactionStructV2 + (JNIEnv *, jclass, jobject); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj + * Method: encodeTransactionStructToJsonV2 + * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionV2;)Ljava/lang/String; + */ +JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_encodeTransactionStructToJsonV2 + (JNIEnv *, jclass, jobject); + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj + * Method: decodeTransactionStructV2 + * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionV2; + */ +JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_decodeTransactionStructV2 + (JNIEnv *, jclass, jstring); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2.java new file mode 100644 index 000000000..800ae8d86 --- /dev/null +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2.java @@ -0,0 +1,121 @@ +package org.fisco.bcos.sdk.jni.utilities.tx; + +public class TransactionDataV2 { + private int version; + private long blockLimit; + private String chainId; + private String groupId; + private String nonce; + private String to; + private String abi; + private TransactionBytes input; + private String value; + private String gasPrice; + private long gasLimit; + private String maxFeePerGas; + private String maxPriorityFeePerGas; + + public int getVersion() { + return version; + } + + public void setVersion(int version) { + this.version = version; + } + + public long getBlockLimit() { + return blockLimit; + } + + public void setBlockLimit(long blockLimit) { + this.blockLimit = blockLimit; + } + + public String getChainId() { + return chainId; + } + + public void setChainId(String chainId) { + this.chainId = chainId; + } + + public String getGroupId() { + return groupId; + } + + public void setGroupId(String groupId) { + this.groupId = groupId; + } + + public String getNonce() { + return nonce; + } + + public void setNonce(String nonce) { + this.nonce = nonce; + } + + public String getTo() { + return to; + } + + public void setTo(String to) { + this.to = to; + } + + public String getAbi() { + return abi; + } + + public void setAbi(String abi) { + this.abi = abi; + } + + public TransactionBytes getInput() { + return input; + } + + public void setInput(TransactionBytes input) { + this.input = input; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public String getGasPrice() { + return gasPrice; + } + + public void setGasPrice(String gasPrice) { + this.gasPrice = gasPrice; + } + + public long getGasLimit() { + return gasLimit; + } + + public void setGasLimit(long gasLimit) { + this.gasLimit = gasLimit; + } + + public String getMaxFeePerGas() { + return maxFeePerGas; + } + + public void setMaxFeePerGas(String maxFeePerGas) { + this.maxFeePerGas = maxFeePerGas; + } + + public String getMaxPriorityFeePerGas() { + return maxPriorityFeePerGas; + } + + public void setMaxPriorityFeePerGas(String maxPriorityFeePerGas) { + this.maxPriorityFeePerGas = maxPriorityFeePerGas; + } +} diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderV2JniObj.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderV2JniObj.java new file mode 100644 index 000000000..a5a2be374 --- /dev/null +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderV2JniObj.java @@ -0,0 +1,76 @@ +package org.fisco.bcos.sdk.jni.utilities.tx; + +import org.fisco.bcos.sdk.jni.common.JniException; +import org.fisco.bcos.sdk.jni.common.JniLibLoader; + +public class TransactionStructBuilderV2JniObj { + + static { + JniLibLoader.loadJniLibrary(); + } + + /** + * @param TransactionDataV2 Transaction Data struct + * @return Hexed Transaction Data + */ + public static native String encodeTransactionDataStructV2(TransactionDataV2 transactionData) + throws JniException; + + /** + * @param TransactionDataV2 Transaction Data struct + * @return Json Transaction Data + */ + public static native String encodeTransactionDataStructToJsonV2(TransactionDataV2 transactionData) + throws JniException; + + /** + * @param transactionDataHex transactionData hex string + * @return TxData + */ + public static native TransactionDataV2 decodeTransactionDataStructV2(String transactionDataHex) + throws JniException; + + /** + * @param cryptoType crypto type + * @param transactionData Transaction Data struct + * @return Hash hex string + */ + public static native String calcTransactionDataStructHashV2( + int cryptoType, TransactionDataV2 transactionData) throws JniException; + + /** + * @param transactionData + * @param signature + * @param transactionDataHash + * @param attribute + * @param extraData + * @return signedTransaction string + */ + public static native String createEncodedTransactionV2( + TransactionDataV2 transactionData, + String signature, + String transactionDataHash, + int attribute, + String extraData) + throws JniException; + + /** + * @param transaction Transaction struct + * @return Hexed Transaction + */ + public static native String encodeTransactionStructV2(TransactionV2 transaction) throws JniException; + + /** + * @param transaction Transaction struct + * @return Json Transaction + */ + public static native String encodeTransactionStructToJsonV2(TransactionV2 transaction) + throws JniException; + + /** + * @param transactionHex transaction hex string + * @return Tx + */ + public static native TransactionV2 decodeTransactionStructV2(String transactionHex) + throws JniException; +} diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionV2.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionV2.java new file mode 100644 index 000000000..4faa575f9 --- /dev/null +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionV2.java @@ -0,0 +1,67 @@ +package org.fisco.bcos.sdk.jni.utilities.tx; + +public class TransactionV2 { + private TransactionDataV2 transactionData; + private TransactionBytes dataHash; + private TransactionBytes signature; + private TransactionBytes sender; + private long importTime; + private int attribute; + private String extraData; + + public TransactionDataV2 getTransactionData() { + return transactionData; + } + + public void setTransactionData(TransactionDataV2 transactionData) { + this.transactionData = transactionData; + } + + public TransactionBytes getDataHash() { + return dataHash; + } + + public void setDataHash(TransactionBytes dataHash) { + this.dataHash = dataHash; + } + + public TransactionBytes getSignature() { + return signature; + } + + public void setSignature(TransactionBytes signature) { + this.signature = signature; + } + + public TransactionBytes getSender() { + return sender; + } + + public void setSender(TransactionBytes sender) { + this.sender = sender; + } + + public long getImportTime() { + return importTime; + } + + public void setImportTime(long importTime) { + this.importTime = importTime; + } + + public int getAttribute() { + return attribute; + } + + public void setAttribute(int attribute) { + this.attribute = attribute; + } + + public String getExtraData() { + return extraData; + } + + public void setExtraData(String extraData) { + this.extraData = extraData; + } +} diff --git a/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV2.java b/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV2.java new file mode 100644 index 000000000..6e7f0a9a6 --- /dev/null +++ b/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV2.java @@ -0,0 +1,239 @@ +package org.fisco.bcos.sdk.jni.test.tx; + +import org.fisco.bcos.sdk.jni.BcosSDKJniObj; +import org.fisco.bcos.sdk.jni.common.JniConfig; +import org.fisco.bcos.sdk.jni.common.JniException; +import org.fisco.bcos.sdk.jni.common.Response; +import org.fisco.bcos.sdk.jni.rpc.RpcCallback; +import org.fisco.bcos.sdk.jni.rpc.RpcJniObj; +import org.fisco.bcos.sdk.jni.test.Utility; +import org.fisco.bcos.sdk.jni.utilities.keypair.KeyPairJniObj; +import org.fisco.bcos.sdk.jni.utilities.tx.*; +import org.junit.Assert; + +import java.security.SecureRandom; +import java.util.Arrays; + +public class TestTxStructV2 { + + // ------------------------------------------------------------------------------ + // ------------------------------------------------------------------------------ + + // HelloWorld Source Code: + /** + * pragma solidity>=0.4.24 <0.6.11; + * + *

contract HelloWorld { string name; + * + *

constructor() public { name = "Hello, World!"; } + * + *

function get() public view returns (string memory) { return name; } + * + *

function set(string memory n) public { name = n; } } + */ + private static final String hwBIN = "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610235565b5050565b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561022b5780601f106102005761010080835404028352916020019161022b565b820191906000526020600020905b81548152906001019060200180831161020e57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea2646970667358221220b5943f43c48cc93c6d71cdcf27aee5072566c88755ce9186e32ce83b24e8dc6c64736f6c634300060a0033"; + + private static final String hwSmBIN = "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063299f7f9d1461003b5780633590b49f146100be575b600080fd5b610043610179565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610083578082015181840152602081019050610068565b50505050905090810190601f1680156100b05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610177600480360360208110156100d457600080fd5b81019080803590602001906401000000008111156100f157600080fd5b82018360208201111561010357600080fd5b8035906020019184600183028401116401000000008311171561012557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061021b565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102115780601f106101e657610100808354040283529160200191610211565b820191906000526020600020905b8154815290600101906020018083116101f457829003601f168201915b5050505050905090565b8060009080519060200190610231929190610235565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea26469706673582212209871cb2bcf390d53645807cbaedfe052d739ef9cff9d84787f74c4f379e1854664736f6c634300060a0033"; + + /* + { + "6d4ce63c": "get()", + "4ed3885e": "set(string)" + } + + { + "299f7f9d": "get()", + "3590b49f": "set(string)" + } + */ + + public static byte[] fromHex(String hexString) { + if (hexString.startsWith("0x")) { + hexString = hexString.substring(2); + } + + if (hexString.length() % 2 != 0) { + throw new IllegalArgumentException("Invalid hex string: " + hexString); + } + + int length = hexString.length() / 2; + byte[] bytes = new byte[length]; + + for (int i = 0; i < length; i++) { + String twoChars = hexString.substring(i * 2, i * 2 + 2); + bytes[i] = (byte) Integer.parseInt(twoChars, 16); + } + + return bytes; + } + + public static String generateNonce() { + byte[] nonceBytes = new byte[16]; + SecureRandom secureRandom = new SecureRandom(); + secureRandom.nextBytes(nonceBytes); + StringBuilder hex = new StringBuilder(); + for (byte b : nonceBytes) { + hex.append(String.format("%02x", b)); + } + return hex.toString(); + } + + public static String getBinary(boolean isSM) { + return isSM ? hwSmBIN : hwBIN; + } + + public static void Usage() { + System.out.println("Desc: test transaction struct V2 [HelloWorld set]"); + System.out.println("Usage: java -cp 'conf/:lib/*:apps/*' org.fisco.bcos.sdk.jni.test.tx.TestTxStructV2"); + System.exit(0); + } + + public static void main(String[] args) throws JniException, InterruptedException { + try { + if (args.length > 1) { + Usage(); + } + + String endpoint = "127.0.0.1:20200"; + String group = "group0"; + String node = ""; + JniConfig jniConfig = Utility.newJniConfig(Arrays.asList(endpoint)); + jniConfig.setDisableSsl(true); + BcosSDKJniObj bcosSDKJni = BcosSDKJniObj.build(jniConfig); + RpcJniObj rpcJniObj = RpcJniObj.build(bcosSDKJni.getNativePointer()); + System.out.println("build Rpc"); + rpcJniObj.start(); + + boolean smCrypto = false; + + long keyPair = KeyPairJniObj.createJniKeyPair(smCrypto ? 1 : 0); + String jniKeyPairAddress = KeyPairJniObj.getJniKeyPairAddress(keyPair); + System.out.printf(" [test Tx Struct V2] new account, address: %s\n", jniKeyPairAddress); + + String groupID = "group0"; + String chainID = "chain0"; + String data = getBinary(smCrypto); + long blockLimit = 1; + String value = "11"; + String gasPrice = "0"; + long gasLimit = 0; + String maxFeePerGas = "11"; + String maxPriorityFeePerGas = "22"; + + // construct TransactionDataV2 + TransactionDataV2 transactionDataStructV2 = new TransactionDataV2(); + transactionDataStructV2.setGroupId(groupID); + transactionDataStructV2.setChainId(chainID); + transactionDataStructV2.setTo(""); + transactionDataStructV2.setAbi(""); + transactionDataStructV2.setVersion(1); + transactionDataStructV2.setNonce(generateNonce()); + transactionDataStructV2.setBlockLimit(blockLimit); + transactionDataStructV2.setValue(value); + transactionDataStructV2.setGasPrice(gasPrice); + transactionDataStructV2.setGasLimit(gasLimit); + transactionDataStructV2.setMaxFeePerGas(maxFeePerGas); + transactionDataStructV2.setMaxPriorityFeePerGas(maxPriorityFeePerGas); + // input + TransactionBytes inputBytes = new TransactionBytes(); + byte[] bytesInput = fromHex(data); + inputBytes.setBuffer(bytesInput); + inputBytes.setLength(bytesInput.length); + transactionDataStructV2.setInput(inputBytes); + + // encode TxData to hex tx data + String txDataHex = TransactionStructBuilderV2JniObj.encodeTransactionDataStructV2(transactionDataStructV2); + // decode hex tx data to TxData + TransactionDataV2 decodeTransactionDataStructHex = TransactionStructBuilderV2JniObj.decodeTransactionDataStructV2(txDataHex); +// TransactionDataV2 decodeTransactionDataStructHex2 = TransactionStructBuilderV2JniObj.decodeTransactionDataStructV2(null); +// TransactionDataV2 decodeTransactionDataStructHex3 = TransactionStructBuilderV2JniObj.decodeTransactionDataStructV2(""); + + // assert + Assert.assertEquals(transactionDataStructV2.getChainId(), decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals(transactionDataStructV2.getGroupId(), decodeTransactionDataStructHex.getGroupId()); + Assert.assertEquals(transactionDataStructV2.getAbi(), decodeTransactionDataStructHex.getAbi()); + Assert.assertEquals(transactionDataStructV2.getBlockLimit(), decodeTransactionDataStructHex.getBlockLimit()); + Assert.assertEquals(transactionDataStructV2.getValue(), decodeTransactionDataStructHex.getValue()); + Assert.assertEquals(transactionDataStructV2.getGasPrice(), decodeTransactionDataStructHex.getGasPrice()); + Assert.assertEquals(transactionDataStructV2.getGasLimit(), decodeTransactionDataStructHex.getGasLimit()); + Assert.assertEquals(transactionDataStructV2.getMaxFeePerGas(), decodeTransactionDataStructHex.getMaxFeePerGas()); + Assert.assertEquals(transactionDataStructV2.getMaxPriorityFeePerGas(), decodeTransactionDataStructHex.getMaxPriorityFeePerGas()); + + // encode TxData to json tx data + String txDataJson = TransactionStructBuilderV2JniObj.encodeTransactionDataStructToJsonV2(transactionDataStructV2); + System.out.printf(" [test Tx Struct V2] txDataJson: %s\n", txDataJson); + + // calc tx data hash + String txDataHash = TransactionStructBuilderV2JniObj.calcTransactionDataStructHashV2(smCrypto ? 1 : 0, decodeTransactionDataStructHex); + System.out.printf(" [test Tx Struct V2] txDataHash: %s\n", txDataHash); + // signature tx data hash + String signature = TransactionBuilderJniObj.signTransactionDataHash(keyPair, txDataHash); + System.out.printf(" [test Tx Struct V2] signature: %s\n", signature); + + // construct tx + TransactionV2 transactionStructV2 = new TransactionV2(); + TransactionBytes dataHashBytes = new TransactionBytes(); + dataHashBytes.setBuffer(txDataHash.getBytes()); + dataHashBytes.setLength(txDataHash.getBytes().length); + TransactionBytes signatureBytes = new TransactionBytes(); + signatureBytes.setBuffer(signature.getBytes()); + signatureBytes.setLength(signature.getBytes().length); + transactionStructV2.setTransactionData(decodeTransactionDataStructHex); + transactionStructV2.setDataHash(dataHashBytes); + transactionStructV2.setSignature(signatureBytes); + transactionStructV2.setSender(null); + transactionStructV2.setImportTime(0); + transactionStructV2.setAttribute(0); + transactionStructV2.setExtraData(""); + // assert + Assert.assertEquals(transactionStructV2.getTransactionData().getBlockLimit(), decodeTransactionDataStructHex.getBlockLimit()); + Assert.assertEquals(transactionStructV2.getTransactionData().getGroupId(), decodeTransactionDataStructHex.getGroupId()); + Assert.assertEquals(transactionStructV2.getTransactionData().getChainId(), decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals(transactionStructV2.getTransactionData().getChainId(), decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals(transactionStructV2.getTransactionData().getAbi(), decodeTransactionDataStructHex.getAbi()); + Assert.assertArrayEquals(transactionStructV2.getDataHash().getBuffer(), txDataHash.getBytes()); + Assert.assertArrayEquals(transactionStructV2.getSignature().getBuffer(), signature.getBytes()); + + // encode Tx to hex tx + String txHex = TransactionStructBuilderV2JniObj.encodeTransactionStructV2(transactionStructV2); + // decode hex tx to Tx + TransactionV2 decodeTransactionStructHex = TransactionStructBuilderV2JniObj.decodeTransactionStructV2(txHex); + + // assert + Assert.assertEquals(transactionStructV2.getTransactionData().getBlockLimit(), decodeTransactionStructHex.getTransactionData().getBlockLimit()); + Assert.assertEquals(transactionStructV2.getTransactionData().getGroupId(), decodeTransactionStructHex.getTransactionData().getGroupId()); + Assert.assertEquals(transactionStructV2.getTransactionData().getChainId(), decodeTransactionStructHex.getTransactionData().getChainId()); + Assert.assertEquals(transactionStructV2.getTransactionData().getAbi(), decodeTransactionStructHex.getTransactionData().getAbi()); + Assert.assertEquals(transactionStructV2.getTransactionData().getValue(), decodeTransactionStructHex.getTransactionData().getValue()); + Assert.assertEquals(transactionStructV2.getTransactionData().getGasLimit(), decodeTransactionStructHex.getTransactionData().getGasLimit()); + Assert.assertEquals(transactionStructV2.getTransactionData().getGasPrice(), decodeTransactionStructHex.getTransactionData().getGasPrice()); + Assert.assertEquals(transactionStructV2.getTransactionData().getMaxFeePerGas(), decodeTransactionStructHex.getTransactionData().getMaxFeePerGas()); + Assert.assertEquals(transactionStructV2.getTransactionData().getMaxPriorityFeePerGas(), decodeTransactionStructHex.getTransactionData().getMaxPriorityFeePerGas()); + Assert.assertArrayEquals(transactionStructV2.getDataHash().getBuffer(), decodeTransactionStructHex.getDataHash().getBuffer()); + Assert.assertArrayEquals(transactionStructV2.getSignature().getBuffer(), decodeTransactionStructHex.getSignature().getBuffer()); + + // encode Tx to json tx + String txJson = TransactionStructBuilderV2JniObj.encodeTransactionStructToJsonV2(transactionStructV2); + System.out.printf(" [test Tx Struct V2] txJson: %s\n", txJson); + // create tx string + String txString = TransactionStructBuilderV2JniObj.createEncodedTransactionV2(decodeTransactionDataStructHex, signature, txDataHash, 0, ""); + // System.out.printf(" [test Tx Struct V2] txString: %s\n", txString); + + // rpc send tx + rpcJniObj.sendTransaction(group, node, txString, false, new RpcCallback() { + @Override + public void onResponse(Response response) { + System.out.println("response error code: ==>>> " + response.getErrorCode()); + String dataStr = new String(response.getData()); + System.out.println("response data: ==>>> " + dataStr); + } + }); + + Thread.sleep(2000); + rpcJniObj.stop(); + System.out.printf(" [test Tx Struct V2] finish !! \n"); + } catch (JniException e) { + System.out.println("test tx struct V2 failed, error: " + e.getMessage()); + } + } +} diff --git a/sample/tx/tx_struct_test.c b/sample/tx/tx_struct_test.c index 74143922d..911957ec0 100644 --- a/sample/tx/tx_struct_test.c +++ b/sample/tx/tx_struct_test.c @@ -380,15 +380,15 @@ int main(int argc, char** argv) group_id, chain_id, contract_address, input_bytes->buffer, input_bytes->length, g_hw_abi, block_limit); // 9.2.1.1 encode tx data to hex - const char* hex_tx_data = bcos_sdk_encode_transaction_data_struct(transaction_data); + const char* hex_tx_data = bcos_sdk_encode_transaction_data_struct_to_hex(transaction_data); printf(" [TxStructTest] tx_data_hex: %s\n", hex_tx_data); // 9.2.1.2 decode hex tx data - struct bcos_sdk_c_transaction_data* decode_tx_data = bcos_sdk_decode_transaction_data_struct(hex_tx_data); + struct bcos_sdk_c_transaction_data* decode_tx_data = bcos_sdk_decode_transaction_data_struct_from_hex(hex_tx_data); // 9.2.1.3 encode tx data to json const char* json_tx_data = bcos_sdk_encode_transaction_data_struct_to_json(transaction_data); printf(" [TxStructTest] tx_data_json: %s\n", json_tx_data); // 9.2.1.4 decode json to tx data struct - decode_tx_data = bcos_sdk_decode_transaction_data_struct_with_json(json_tx_data); + decode_tx_data = bcos_sdk_decode_transaction_data_struct_from_json(json_tx_data); // 9.2.2 calc transaction data hash const char* transaction_data_hash = @@ -409,16 +409,16 @@ int main(int argc, char** argv) struct bcos_sdk_c_transaction* transaction = bcos_sdk_create_transaction_struct(decode_tx_data, signed_hash, transaction_data_hash, 0, extra_data); // 9.2.4.2 encode tx to hex - const char* hex_tx = bcos_sdk_encode_transaction_struct(transaction); + const char* hex_tx = bcos_sdk_encode_transaction_struct_to_hex(transaction); printf(" [TxStructTest] tx_hex: %s\n", hex_tx); // 9.2.4.3 decode hex to tx - struct bcos_sdk_c_transaction* decode_tx = bcos_sdk_decode_transaction_struct(hex_tx); + struct bcos_sdk_c_transaction* decode_tx = bcos_sdk_decode_transaction_struct_from_hex(hex_tx); // 9.2.4.4 encode tx to json const char* json_tx = bcos_sdk_encode_transaction_struct_to_json(decode_tx); printf(" [TxStructTest] tx_json: %s\n", json_tx); // 9.2.4.5 decode json to tx - decode_tx = bcos_sdk_decode_transaction_struct_with_json(json_tx); - const char* hex_tx2 = bcos_sdk_encode_transaction_struct(decode_tx); + decode_tx = bcos_sdk_decode_transaction_struct_from_json(json_tx); + const char* hex_tx2 = bcos_sdk_encode_transaction_struct_to_hex(decode_tx); printf(" [TxStructTest] signed_tx: %s\n", signed_tx); printf(" [TxStructTest] hex_tx: %s\n", hex_tx); diff --git a/sample/tx/tx_struct_v2_test.c b/sample/tx/tx_struct_v2_test.c index 6cfea678f..d22a5fbc9 100644 --- a/sample/tx/tx_struct_v2_test.c +++ b/sample/tx/tx_struct_v2_test.c @@ -388,15 +388,15 @@ int main(int argc, char** argv) group_id, chain_id, contract_address, input_bytes->buffer, input_bytes->length, g_hw_abi, block_limit, value, gasPrice, gasLimit, maxFeePerGas, maxPriorityFeePerGas); // 9.2.1.1 encode tx data to hex - const char* hex_tx_data = bcos_sdk_encode_transaction_data_struct_v2(transaction_data_v2); + const char* hex_tx_data = bcos_sdk_encode_transaction_data_struct_to_hex_v2(transaction_data_v2); printf(" [TxStructV2Test] tx_data_hex: %s\n", hex_tx_data); // 9.2.1.2 decode hex tx data - struct bcos_sdk_c_transaction_data_v2* decode_tx_data = bcos_sdk_decode_transaction_data_struct_v2(hex_tx_data); + struct bcos_sdk_c_transaction_data_v2* decode_tx_data = bcos_sdk_decode_transaction_data_struct_from_hex_v2(hex_tx_data); // 9.2.1.3 encode tx data to json const char* json_tx_data = bcos_sdk_encode_transaction_data_struct_to_json_v2(decode_tx_data); printf(" [TxStructV2Test] tx_data_json: %s\n", json_tx_data); // 9.2.1.4 decode json to tx data struct - decode_tx_data = bcos_sdk_decode_transaction_data_struct_with_json_v2(json_tx_data); + decode_tx_data = bcos_sdk_decode_transaction_data_struct_from_json_v2(json_tx_data); // 9.2.2 calc transaction data hash const char* transaction_data_hash = @@ -417,16 +417,16 @@ int main(int argc, char** argv) struct bcos_sdk_c_transaction_v2* transaction = bcos_sdk_create_transaction_struct_v2(decode_tx_data, signed_hash, transaction_data_hash, 0, extra_data); // 9.2.4.2 encode tx to hex - const char* hex_tx = bcos_sdk_encode_transaction_struct_v2(transaction); + const char* hex_tx = bcos_sdk_encode_transaction_struct_to_hex_v2(transaction); printf(" [TxStructV2Test] tx_hex: %s\n", hex_tx); // 9.2.4.3 decode hex to tx - struct bcos_sdk_c_transaction_v2* decode_tx = bcos_sdk_decode_transaction_struct_v2(hex_tx); + struct bcos_sdk_c_transaction_v2* decode_tx = bcos_sdk_decode_transaction_struct_from_hex_v2(hex_tx); // 9.2.4.4 encode tx to json const char* json_tx = bcos_sdk_encode_transaction_struct_to_json_v2(decode_tx); printf(" [TxStructV2Test] tx_json: %s\n", json_tx); // 9.2.4.5 decode json to tx - decode_tx = bcos_sdk_decode_transaction_struct_with_json_v2(json_tx); - const char* hex_tx2 = bcos_sdk_encode_transaction_struct_v2(decode_tx); + decode_tx = bcos_sdk_decode_transaction_struct_from_json_v2(json_tx); + const char* hex_tx2 = bcos_sdk_encode_transaction_struct_to_hex_v2(decode_tx); printf(" [TxStructV2Test] signed_tx: %s\n", signed_tx); printf(" [TxStructV2Test] hex_tx: %s\n", hex_tx); diff --git a/test/testTransactionStruct.cpp b/test/testTransactionStruct.cpp index b4f668d61..5f59a5ac2 100644 --- a/test/testTransactionStruct.cpp +++ b/test/testTransactionStruct.cpp @@ -295,7 +295,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStruct) // 1. test tx data encode decode hex // encode to hex failed (transaction_data == NULL) - auto encodedTxDataHex = bcos_sdk_encode_transaction_data_struct(nullptr); + auto encodedTxDataHex = bcos_sdk_encode_transaction_data_struct_to_hex(nullptr); auto hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == false); @@ -303,14 +303,14 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStruct) BOOST_TEST(bcos_sdk_get_last_error() == -1); // encode to hex success - encodedTxDataHex = bcos_sdk_encode_transaction_data_struct(txDataWithHex); + encodedTxDataHex = bcos_sdk_encode_transaction_data_struct_to_hex(txDataWithHex); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == true); BOOST_TEST(encodedTxDataHex != nullptr); BOOST_TEST(bcos_sdk_get_last_error() == 0); - encodedTxDataHex = bcos_sdk_encode_transaction_data_struct(txDataWithBytes); + encodedTxDataHex = bcos_sdk_encode_transaction_data_struct_to_hex(txDataWithBytes); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == true); @@ -318,7 +318,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStruct) BOOST_TEST(bcos_sdk_get_last_error() == 0); // decode hex failed (transaction_data_hex_str == NULL) - auto decodedTxDataHex = bcos_sdk_decode_transaction_data_struct(nullptr); + auto decodedTxDataHex = bcos_sdk_decode_transaction_data_struct_from_hex(nullptr); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == false); @@ -326,7 +326,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStruct) BOOST_TEST(bcos_sdk_get_last_error() == -1); // decode hex failed (transaction_data_hex_str == "") - decodedTxDataHex = bcos_sdk_decode_transaction_data_struct(""); + decodedTxDataHex = bcos_sdk_decode_transaction_data_struct_from_hex(""); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == false); @@ -336,7 +336,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStruct) std::string("transaction_data_hex_str can not be empty string")); // decode hex failed (the length of transaction_data_hex_str is not even number) - decodedTxDataHex = bcos_sdk_decode_transaction_data_struct("0xabc"); + decodedTxDataHex = bcos_sdk_decode_transaction_data_struct_from_hex("0xabc"); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == false); @@ -346,7 +346,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStruct) std::string("the length of transaction_data_hex_str must be an even number")); // decode hex success - decodedTxDataHex = bcos_sdk_decode_transaction_data_struct(encodedTxDataHex); + decodedTxDataHex = bcos_sdk_decode_transaction_data_struct_from_hex(encodedTxDataHex); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == true); @@ -390,7 +390,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStruct) BOOST_TEST(bcos_sdk_get_last_error() == 0); // decode json failed (transaction_data_json_str == NULL) - auto decodedTxDataJson = bcos_sdk_decode_transaction_data_struct_with_json(nullptr); + auto decodedTxDataJson = bcos_sdk_decode_transaction_data_struct_from_json(nullptr); jsonSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(jsonSuccess == false); @@ -398,7 +398,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStruct) BOOST_TEST(bcos_sdk_get_last_error() == -1); // decode json failed (transaction_data_json_str == "") - decodedTxDataJson = bcos_sdk_decode_transaction_data_struct_with_json(""); + decodedTxDataJson = bcos_sdk_decode_transaction_data_struct_from_json(""); jsonSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(jsonSuccess == false); @@ -408,7 +408,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStruct) std::string("transaction_data_json_str can not be empty string")); // decode json success - decodedTxDataJson = bcos_sdk_decode_transaction_data_struct_with_json(encodedTxDataJson); + decodedTxDataJson = bcos_sdk_decode_transaction_data_struct_from_json(encodedTxDataJson); jsonSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(jsonSuccess == true); @@ -436,8 +436,8 @@ BOOST_AUTO_TEST_CASE(testCalculateTxDataHash) { auto txDataWithHex = createTxDataStructWithHexInput(); auto txDataWithByte = createTxDataStructWithByteInput(); - auto encodedtxDataWithHex = bcos_sdk_encode_transaction_data_struct(txDataWithHex); - auto encodedtxDataWithByte = bcos_sdk_encode_transaction_data_struct(txDataWithByte); + auto encodedtxDataWithHex = bcos_sdk_encode_transaction_data_struct_to_hex(txDataWithHex); + auto encodedtxDataWithByte = bcos_sdk_encode_transaction_data_struct_to_hex(txDataWithByte); // 1. test cal tx data hash with struct // calculate tx data hash failed (cryptoType not in 0,1) @@ -700,9 +700,9 @@ BOOST_AUTO_TEST_CASE(testCreateEncodedTx) BOOST_TEST(encodedTx != nullptr); BOOST_TEST(bcos_sdk_get_last_error() == 0); - // 2. test bcos_sdk_encode_transaction_struct + // 2. test bcos_sdk_encode_transaction_struct_to_hex_ // create encoded tx with tx_struct failed (transaction_struct == "") - auto encodedTxWithTxStruct = bcos_sdk_encode_transaction_struct(nullptr); + auto encodedTxWithTxStruct = bcos_sdk_encode_transaction_struct_to_hex(nullptr); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); @@ -710,7 +710,7 @@ BOOST_AUTO_TEST_CASE(testCreateEncodedTx) BOOST_TEST(bcos_sdk_get_last_error() == -1); // create encoded tx with tx_struct success - encodedTxWithTxStruct = bcos_sdk_encode_transaction_struct(txStruct); + encodedTxWithTxStruct = bcos_sdk_encode_transaction_struct_to_hex(txStruct); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == true); @@ -738,7 +738,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxStruct) // 1. test tx encode decode hex // encode tx to hex failed (transaction == NULL) - auto encodedTxHex = bcos_sdk_encode_transaction_struct(nullptr); + auto encodedTxHex = bcos_sdk_encode_transaction_struct_to_hex(nullptr); auto hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == false); @@ -746,7 +746,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxStruct) BOOST_TEST(bcos_sdk_get_last_error() == -1); // encode tx to hex success - encodedTxHex = bcos_sdk_encode_transaction_struct(txStruct); + encodedTxHex = bcos_sdk_encode_transaction_struct_to_hex(txStruct); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == true); @@ -754,7 +754,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxStruct) BOOST_TEST(bcos_sdk_get_last_error() == 0); // decode hex failed (transaction_hex_str == NULL) - auto decodedTxHex = bcos_sdk_decode_transaction_struct(nullptr); + auto decodedTxHex = bcos_sdk_decode_transaction_struct_from_hex(nullptr); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == false); @@ -762,7 +762,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxStruct) BOOST_TEST(bcos_sdk_get_last_error() == -1); // decode hex failed (transaction_hex_str == "") - decodedTxHex = bcos_sdk_decode_transaction_struct(""); + decodedTxHex = bcos_sdk_decode_transaction_struct_from_hex(""); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == false); @@ -772,7 +772,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxStruct) std::string("transaction_hex_str can not be empty string")); // decode hex failed (the length of transaction_hex_str is not even number) - decodedTxHex = bcos_sdk_decode_transaction_struct("0xabc"); + decodedTxHex = bcos_sdk_decode_transaction_struct_from_hex("0xabc"); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == false); @@ -782,7 +782,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxStruct) std::string("the length of transaction_hex_str must be an even number")); // decode hex success - decodedTxHex = bcos_sdk_decode_transaction_struct(encodedTxHex); + decodedTxHex = bcos_sdk_decode_transaction_struct_from_hex(encodedTxHex); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == true); BOOST_TEST(decodedTxHex != nullptr); @@ -808,7 +808,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxStruct) BOOST_TEST(bcos_sdk_get_last_error() == 0); // decode json failed (transaction_json_str == NULL) - auto decodedTxJson = bcos_sdk_decode_transaction_struct_with_json(nullptr); + auto decodedTxJson = bcos_sdk_decode_transaction_struct_from_json(nullptr); jsonSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(jsonSuccess == false); @@ -816,7 +816,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxStruct) BOOST_TEST(bcos_sdk_get_last_error() == -1); // decode json failed (transaction_json_str == "") - decodedTxJson = bcos_sdk_decode_transaction_struct_with_json(""); + decodedTxJson = bcos_sdk_decode_transaction_struct_from_json(""); jsonSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(jsonSuccess == false); @@ -826,7 +826,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxStruct) std::string("transaction_json_str can not be empty string")); // decode json success - decodedTxJson = bcos_sdk_decode_transaction_struct_with_json(encodedTxJson); + decodedTxJson = bcos_sdk_decode_transaction_struct_from_json(encodedTxJson); jsonSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(jsonSuccess == true); BOOST_TEST(decodedTxJson != nullptr); diff --git a/test/testTransactionStructV2.cpp b/test/testTransactionStructV2.cpp new file mode 100644 index 000000000..11bdc690e --- /dev/null +++ b/test/testTransactionStructV2.cpp @@ -0,0 +1,902 @@ +/* + * Copyright (C) 2021 FISCO BCOS. + * SPDX-License-Identifier: Apache-2.0 + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * @file testTransactionStructV2.cpp + * @author: lucasli + * @date 2023-12-13 + */ + +#include "bcos-c-sdk/bcos_sdk_c.h" +#include "bcos-c-sdk/bcos_sdk_c_error.h" +#include "bcos-c-sdk/bcos_sdk_c_rpc.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_abi.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_keypair.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_tx.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h" +#include +#include +#include + +using namespace bcos; + +struct TestTxStructV2Fixture +{ +}; + +BOOST_FIXTURE_TEST_SUITE(TransactionStructV2Test, TestTxStructV2Fixture) + +const auto input = + "0x3590b49f00000000000000000000000000000000000000000000000000000000000000200000000000000000" + "00000000000000000000000000000000000000000000001748656c6c6f20464953434f2d42434f5320332e3021" + "2121000000000000000000"; + +const char* abi_with_chinese = + "[{\"inputs\":[],\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"inputs\":[]," + "\"name\":\"get\",\"outputs\":[{\"internalType\":\"string\",\"name\":\"李伟光\",\"type\":" + "\"string\"}]" + ",\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"internalType\":" + "\"string\",\"name\":\"n\",\"type\":\"string\"}],\"name\":\"set\",\"outputs\":[]," + "\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]"; + +struct bcos_sdk_c_transaction_data_v2* createTxDataStructWithHexInputV2() +{ + auto group_id = ""; + auto chain_id = ""; + auto to = ""; + auto abi = ""; + int64_t block_limit = 1; + auto value = "1"; + auto gas_price = "0"; + int64_t gas_limit = 0; + auto max_fee_per_gas = "1"; + auto max_priority_fee_per_gas = "1"; + auto txDataWithHex = bcos_sdk_create_transaction_data_struct_with_hex_input_v2(group_id, + chain_id, to, input, abi, block_limit, value, gas_price, gas_limit, max_fee_per_gas, + max_priority_fee_per_gas); + + return txDataWithHex; +} + +struct bcos_sdk_c_transaction_data_v2* createTxDataStructWithByteInputV2() +{ + auto group_id = ""; + auto chain_id = ""; + auto to = ""; + auto abi = ""; + int64_t block_limit = 1; + auto value = "1"; + auto gas_price = "0"; + int64_t gas_limit = 0; + auto max_fee_per_gas = "1"; + auto max_priority_fee_per_gas = "1"; + auto bytesInput = fromHexString(input); + + auto txDataWithBytes = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, + to, bytesInput->data(), bytesInput->size(), abi, block_limit, value, gas_price, gas_limit, + max_fee_per_gas, max_priority_fee_per_gas); + + return txDataWithBytes; +} + +void compareTxStruct(struct bcos_sdk_c_transaction_v2* txStruct, + struct bcos_sdk_c_transaction_data_v2* txDataStruct, const char* signature, + const char* extraData, int64_t attribute) +{ + // transaction_data + BOOST_TEST(txStruct != nullptr); + BOOST_TEST(txStruct->transaction_data->version == txDataStruct->version); + BOOST_TEST(txStruct->transaction_data->block_limit == txDataStruct->block_limit); + BOOST_TEST(txStruct->transaction_data->chain_id == txDataStruct->chain_id); + BOOST_TEST(txStruct->transaction_data->version == txDataStruct->version); + BOOST_TEST(txStruct->transaction_data->group_id == txDataStruct->group_id); + BOOST_TEST(txStruct->transaction_data->nonce == txDataStruct->nonce); + BOOST_TEST(txStruct->transaction_data->to == txDataStruct->to); + BOOST_TEST(txStruct->transaction_data->abi == txDataStruct->abi); + BOOST_TEST(txStruct->transaction_data->value == txDataStruct->value); + BOOST_TEST(txStruct->transaction_data->gas_price == txDataStruct->gas_price); + BOOST_TEST(txStruct->transaction_data->gas_limit == txDataStruct->gas_limit); + BOOST_TEST(txStruct->transaction_data->max_fee_per_gas == txDataStruct->max_fee_per_gas); + BOOST_TEST(txStruct->transaction_data->max_priority_fee_per_gas == + txDataStruct->max_priority_fee_per_gas); + std::string txDataInputString( + reinterpret_cast(txDataStruct->input->buffer), txDataStruct->input->length); + std::string txInputString(reinterpret_cast(txStruct->transaction_data->input->buffer), + txStruct->transaction_data->input->length); + BOOST_TEST(txDataInputString == txInputString); + + // signature + std::string txStructSignatureString( + reinterpret_cast(txStruct->signature->buffer), txStruct->signature->length); + auto bytesSignature = fromHexString(signature); + std::string fromSignatureHexString(bytesSignature->begin(), bytesSignature->end()); + BOOST_TEST(fromSignatureHexString == txStructSignatureString); + + // attribute + BOOST_TEST(txStruct->attribute == attribute); + + // extra_data + BOOST_TEST(txStruct->extra_data == extraData); +} + +BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithHexInputV2) +{ + auto group_id = ""; + auto chain_id = ""; + auto to = ""; + auto abi = ""; + int64_t block_limit = 0; + auto value = "1"; + auto gas_price = "0"; + int64_t gas_limit = 0; + auto max_fee_per_gas = "1"; + auto max_priority_fee_per_gas = "1"; + + // create transaction_data_struct_v2 failed (block_limit = 0) + auto txData = bcos_sdk_create_transaction_data_struct_with_hex_input_v2(group_id, chain_id, to, + input, abi, block_limit, value, gas_price, gas_limit, max_fee_per_gas, + max_priority_fee_per_gas); + auto success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(txData == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("block limit must > 0")); + + // create transaction_data_struct_v2 failed (input = "") + block_limit = 1; + txData = bcos_sdk_create_transaction_data_struct_with_hex_input_v2(group_id, chain_id, to, "", + abi, block_limit, value, gas_price, gas_limit, max_fee_per_gas, max_priority_fee_per_gas); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(txData == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("input can not be empty string")); + + // create transaction_data_struct_v2 failed (gas_limit <0) + txData = bcos_sdk_create_transaction_data_struct_with_hex_input_v2(group_id, chain_id, to, + input, abi, block_limit, value, gas_price, -1, max_fee_per_gas, max_priority_fee_per_gas); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(txData == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("gas limit must >= 0")); + + // create transaction_data_struct_v2 success + txData = bcos_sdk_create_transaction_data_struct_with_hex_input_v2(group_id, chain_id, to, + input, abi, block_limit, value, gas_price, gas_limit, max_fee_per_gas, + max_priority_fee_per_gas); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == true); + BOOST_TEST(txData != nullptr); + BOOST_TEST(txData->group_id == group_id); + BOOST_TEST(txData->chain_id == chain_id); + BOOST_TEST(txData->to == to); + BOOST_TEST(txData->abi == abi); + BOOST_TEST(txData->block_limit == block_limit); + BOOST_TEST(txData->value == value); + BOOST_TEST(txData->gas_price == gas_price); + BOOST_TEST(txData->gas_limit == gas_limit); + BOOST_TEST(txData->max_fee_per_gas == max_fee_per_gas); + BOOST_TEST(txData->max_priority_fee_per_gas == max_priority_fee_per_gas); + auto bytesInput = fromHexString(input); + std::string fromInputHexString(bytesInput->begin(), bytesInput->end()); + std::string txDataInputString( + reinterpret_cast(txData->input->buffer), txData->input->length); + BOOST_TEST(txDataInputString == fromInputHexString); + + bcos_sdk_destroy_transaction_data_struct_v2(txData); +} + +BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithByteInputV2) +{ + auto bytesInput = fromHexString(input); + auto group_id = ""; + auto chain_id = ""; + auto to = ""; + auto abi = ""; + uint32_t bytes_input_length = 10; + int64_t block_limit = 0; + auto value = "1"; + auto gas_price = "0"; + int64_t gas_limit = 0; + auto max_fee_per_gas = "1"; + auto max_priority_fee_per_gas = "1"; + + // create transaction_data_struct_v2 failed (block_limit = 0) + auto txData = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, to, + bytesInput->data(), bytes_input_length, abi, block_limit, value, gas_price, gas_limit, + max_fee_per_gas, max_priority_fee_per_gas); + auto success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txData == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("block limit must > 0")); + + // create transaction_data_struct_v2 failed (bytes_input_length = 0) + bytes_input_length = 0; + block_limit = 1; + txData = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, to, + bytesInput->data(), bytes_input_length, abi, block_limit, value, gas_price, gas_limit, + max_fee_per_gas, max_priority_fee_per_gas); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txData == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("bytes input length must > 0")); + + // create transaction_data_struct_v2 failed (group_id = NULL) + group_id = nullptr; + txData = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, to, + bytesInput->data(), bytesInput->size(), abi, block_limit, value, gas_price, gas_limit, + max_fee_per_gas, max_priority_fee_per_gas); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txData == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // create transaction_data_struct_v2 success (group_id has Chinese characters) + group_id = "区块链群组1"; + txData = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, to, + bytesInput->data(), bytesInput->size(), abi, block_limit, value, gas_price, gas_limit, + max_fee_per_gas, max_priority_fee_per_gas); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == true); + BOOST_TEST(txData != nullptr); + BOOST_TEST(txData->group_id == group_id); + BOOST_TEST(txData->chain_id == chain_id); + BOOST_TEST(txData->to == to); + BOOST_TEST(txData->abi == abi); + BOOST_TEST(txData->block_limit == block_limit); + BOOST_TEST(txData->value == value); + BOOST_TEST(txData->gas_price == gas_price); + BOOST_TEST(txData->gas_limit == gas_limit); + BOOST_TEST(txData->max_fee_per_gas == max_fee_per_gas); + BOOST_TEST(txData->max_priority_fee_per_gas == max_priority_fee_per_gas); + std::string fromInputHexString1(bytesInput->begin(), bytesInput->end()); + std::string txDataInputString1( + reinterpret_cast(txData->input->buffer), txData->input->length); + BOOST_TEST(txDataInputString1 == fromInputHexString1); + + // create transaction_data_struct_v2 failed (chain_id = NULL) + chain_id = nullptr; + txData = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, to, + bytesInput->data(), bytesInput->size(), abi, block_limit, value, gas_price, gas_limit, + max_fee_per_gas, max_priority_fee_per_gas); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == false); + BOOST_TEST(txData == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // create transaction_data_struct_v2 success (chain_id has Chinese characters) + chain_id = "区块链1"; + txData = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, to, + bytesInput->data(), bytesInput->size(), abi, block_limit, value, gas_price, gas_limit, + max_fee_per_gas, max_priority_fee_per_gas); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == true); + BOOST_TEST(txData != nullptr); + BOOST_TEST(txData->group_id == group_id); + BOOST_TEST(txData->chain_id == chain_id); + BOOST_TEST(txData->to == to); + BOOST_TEST(txData->abi == abi); + BOOST_TEST(txData->block_limit == block_limit); + BOOST_TEST(txData->value == value); + BOOST_TEST(txData->gas_price == gas_price); + BOOST_TEST(txData->gas_limit == gas_limit); + BOOST_TEST(txData->max_fee_per_gas == max_fee_per_gas); + BOOST_TEST(txData->max_priority_fee_per_gas == max_priority_fee_per_gas); + std::string fromInputHexString2(bytesInput->begin(), bytesInput->end()); + std::string txDataInputString2( + reinterpret_cast(txData->input->buffer), txData->input->length); + BOOST_TEST(txDataInputString2 == fromInputHexString2); + + // create transaction_data_struct_v2 success (abi has Chinese characters) + chain_id = ""; + txData = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, to, + bytesInput->data(), bytesInput->size(), abi_with_chinese, block_limit, value, gas_price, + gas_limit, max_fee_per_gas, max_priority_fee_per_gas); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == true); + BOOST_TEST(txData != nullptr); + BOOST_TEST(txData->group_id == group_id); + BOOST_TEST(txData->chain_id == chain_id); + BOOST_TEST(txData->to == to); + BOOST_TEST(txData->abi == abi_with_chinese); + BOOST_TEST(txData->block_limit == block_limit); + BOOST_TEST(txData->value == value); + BOOST_TEST(txData->gas_price == gas_price); + BOOST_TEST(txData->gas_limit == gas_limit); + BOOST_TEST(txData->max_fee_per_gas == max_fee_per_gas); + BOOST_TEST(txData->max_priority_fee_per_gas == max_priority_fee_per_gas); + std::string fromInputHexString3(bytesInput->begin(), bytesInput->end()); + std::string txDataInputString3( + reinterpret_cast(txData->input->buffer), txData->input->length); + BOOST_TEST(txDataInputString3 == fromInputHexString3); + + // create transaction_data_struct_v2 failed (gas_limit <0) + txData = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, to, + bytesInput->data(), bytesInput->size(), abi, block_limit, value, gas_price, -1, + max_fee_per_gas, max_priority_fee_per_gas); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(txData == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("gas limit must >= 0")); + + // create transaction_data_struct_v2 success + group_id = ""; + txData = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, to, + bytesInput->data(), bytesInput->size(), abi, block_limit, value, gas_price, gas_limit, + max_fee_per_gas, max_priority_fee_per_gas); + success = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(success == true); + BOOST_TEST(txData != nullptr); + BOOST_TEST(txData->group_id == group_id); + BOOST_TEST(txData->chain_id == chain_id); + BOOST_TEST(txData->to == to); + BOOST_TEST(txData->abi == abi); + BOOST_TEST(txData->block_limit == block_limit); + BOOST_TEST(txData->value == value); + BOOST_TEST(txData->gas_price == gas_price); + BOOST_TEST(txData->gas_limit == gas_limit); + BOOST_TEST(txData->max_fee_per_gas == max_fee_per_gas); + BOOST_TEST(txData->max_priority_fee_per_gas == max_priority_fee_per_gas); + std::string fromInputHexString(bytesInput->begin(), bytesInput->end()); + std::string txDataInputString( + reinterpret_cast(txData->input->buffer), txData->input->length); + BOOST_TEST(txDataInputString == fromInputHexString); + + bcos_sdk_destroy_transaction_data_struct_v2(txData); +} + +BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStructV2) +{ + auto txDataWithHex = createTxDataStructWithHexInputV2(); + auto txDataWithBytes = createTxDataStructWithByteInputV2(); + + // 1. test tx data encode decode hex + // encode to hex failed (transaction_data == NULL) + auto encodedTxDataHex = bcos_sdk_encode_transaction_data_struct_to_hex_v2(nullptr); + auto hexSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(hexSuccess == false); + BOOST_TEST(encodedTxDataHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // encode to hex success + encodedTxDataHex = bcos_sdk_encode_transaction_data_struct_to_hex_v2(txDataWithHex); + hexSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(hexSuccess == true); + BOOST_TEST(encodedTxDataHex != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + encodedTxDataHex = bcos_sdk_encode_transaction_data_struct_to_hex_v2(txDataWithBytes); + hexSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(hexSuccess == true); + BOOST_TEST(encodedTxDataHex != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // decode hex failed (transaction_data_hex_str == NULL) + auto decodedTxDataHex = bcos_sdk_decode_transaction_data_struct_from_hex_v2(nullptr); + hexSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(hexSuccess == false); + BOOST_TEST(decodedTxDataHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // decode hex failed (transaction_data_hex_str == "") + decodedTxDataHex = bcos_sdk_decode_transaction_data_struct_from_hex_v2(""); + hexSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(hexSuccess == false); + BOOST_TEST(decodedTxDataHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("transaction_data_hex_str can not be empty string")); + + // decode hex failed (the length of transaction_data_hex_str is not even number) + decodedTxDataHex = bcos_sdk_decode_transaction_data_struct_from_hex_v2("0xabc"); + hexSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(hexSuccess == false); + BOOST_TEST(decodedTxDataHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("the length of transaction_data_hex_str must be an even number")); + + // decode hex success + decodedTxDataHex = bcos_sdk_decode_transaction_data_struct_from_hex_v2(encodedTxDataHex); + hexSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(hexSuccess == true); + BOOST_TEST(decodedTxDataHex != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + BOOST_TEST(decodedTxDataHex->version == txDataWithBytes->version); + BOOST_TEST(decodedTxDataHex->group_id == txDataWithBytes->group_id); + BOOST_TEST(decodedTxDataHex->chain_id == txDataWithBytes->chain_id); + BOOST_TEST(decodedTxDataHex->to == txDataWithBytes->to); + BOOST_TEST(decodedTxDataHex->abi == txDataWithBytes->abi); + BOOST_TEST(decodedTxDataHex->block_limit == txDataWithBytes->block_limit); + BOOST_TEST(decodedTxDataHex->nonce == txDataWithBytes->nonce); + BOOST_TEST(decodedTxDataHex->value == txDataWithBytes->value); + BOOST_TEST(decodedTxDataHex->gas_price == txDataWithBytes->gas_price); + BOOST_TEST(decodedTxDataHex->gas_limit == txDataWithBytes->gas_limit); + BOOST_TEST(decodedTxDataHex->max_fee_per_gas == txDataWithBytes->max_fee_per_gas); + BOOST_TEST( + decodedTxDataHex->max_priority_fee_per_gas == txDataWithBytes->max_priority_fee_per_gas); + std::string txDataInputString( + reinterpret_cast(txDataWithBytes->input->buffer), txDataWithBytes->input->length); + std::string decodedHexTxDataInputString( + reinterpret_cast(decodedTxDataHex->input->buffer), decodedTxDataHex->input->length); + BOOST_TEST(txDataInputString == decodedHexTxDataInputString); + + // 2. test tx data encode decode json + // encode to json failed (transaction_data == NULL) + auto encodedTxDataJson = bcos_sdk_encode_transaction_data_struct_to_json_v2(nullptr); + auto jsonSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(jsonSuccess == false); + BOOST_TEST(encodedTxDataJson == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // encode to json success + encodedTxDataJson = bcos_sdk_encode_transaction_data_struct_to_json_v2(txDataWithHex); + jsonSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(jsonSuccess == true); + BOOST_TEST(encodedTxDataJson != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + encodedTxDataJson = bcos_sdk_encode_transaction_data_struct_to_json_v2(txDataWithBytes); + jsonSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(jsonSuccess == true); + BOOST_TEST(encodedTxDataJson != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // decode json failed (transaction_data_json_str == NULL) + auto decodedTxDataJson = bcos_sdk_decode_transaction_data_struct_from_json_v2(nullptr); + jsonSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(jsonSuccess == false); + BOOST_TEST(decodedTxDataJson == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // decode json failed (transaction_data_json_str == "") + decodedTxDataJson = bcos_sdk_decode_transaction_data_struct_from_json_v2(""); + jsonSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(jsonSuccess == false); + BOOST_TEST(decodedTxDataJson == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("transaction_data_json_str can not be empty string")); + + // decode json success + decodedTxDataJson = bcos_sdk_decode_transaction_data_struct_from_json_v2(encodedTxDataJson); + jsonSuccess = bcos_sdk_is_last_opr_success(); + + BOOST_TEST(jsonSuccess == true); + BOOST_TEST(decodedTxDataJson != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + BOOST_TEST(decodedTxDataJson->version == txDataWithBytes->version); + BOOST_TEST(decodedTxDataJson->group_id == txDataWithBytes->group_id); + BOOST_TEST(decodedTxDataJson->chain_id == txDataWithBytes->chain_id); + BOOST_TEST(decodedTxDataJson->to == txDataWithBytes->to); + BOOST_TEST(decodedTxDataJson->abi == txDataWithBytes->abi); + BOOST_TEST(decodedTxDataJson->block_limit == txDataWithBytes->block_limit); + BOOST_TEST(decodedTxDataJson->nonce == txDataWithBytes->nonce); + BOOST_TEST(decodedTxDataHex->value == txDataWithBytes->value); + BOOST_TEST(decodedTxDataHex->gas_price == txDataWithBytes->gas_price); + BOOST_TEST(decodedTxDataHex->gas_limit == txDataWithBytes->gas_limit); + BOOST_TEST(decodedTxDataHex->max_fee_per_gas == txDataWithBytes->max_fee_per_gas); + BOOST_TEST( + decodedTxDataHex->max_priority_fee_per_gas == txDataWithBytes->max_priority_fee_per_gas); + std::string decodedJsonTxDataInputJson( + reinterpret_cast(decodedTxDataJson->input->buffer), + decodedTxDataJson->input->length); + BOOST_TEST(txDataInputString == decodedJsonTxDataInputJson); + + bcos_sdk_destroy_transaction_data_struct_v2(txDataWithHex); + bcos_sdk_destroy_transaction_data_struct_v2(txDataWithBytes); + bcos_sdk_destroy_transaction_data_struct_v2(decodedTxDataHex); + bcos_sdk_destroy_transaction_data_struct_v2(decodedTxDataJson); +} + +BOOST_AUTO_TEST_CASE(testCalculateTxDataHashV2) +{ + auto txDataWithHex = createTxDataStructWithHexInputV2(); + auto txDataWithByte = createTxDataStructWithByteInputV2(); + auto encodedtxDataWithHex = bcos_sdk_encode_transaction_data_struct_to_hex_v2(txDataWithHex); + auto encodedtxDataWithByte = bcos_sdk_encode_transaction_data_struct_to_hex_v2(txDataWithByte); + + // 1. test cal tx data hash with struct + // calculate tx data hash failed (cryptoType not in 0,1) + auto cryptoType = 2; + auto txDataHash = bcos_sdk_calc_transaction_data_struct_hash_v2(cryptoType, txDataWithHex); + auto success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(txDataHash == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("invalid crypto type, it must be BCOS_C_SDK_ECDSA_TYPE(ecdsa crypto " + "type) or BCOS_C_SDK_SM_TYPE(sm crypto type)")); + + // calculate tx data hash failed (cryptoType is negative number) + cryptoType = -11; + txDataHash = bcos_sdk_calc_transaction_data_struct_hash_v2(cryptoType, txDataWithHex); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(txDataHash == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("invalid crypto type, it must be BCOS_C_SDK_ECDSA_TYPE(ecdsa crypto " + "type) or BCOS_C_SDK_SM_TYPE(sm crypto type)")); + + // calculate tx data hash failed (transaction_data == NULL) + cryptoType = 1; + txDataHash = bcos_sdk_calc_transaction_data_struct_hash_v2(cryptoType, nullptr); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(txDataHash == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // calculate tx data hash success (txDataWithHex) + cryptoType = 1; + txDataHash = bcos_sdk_calc_transaction_data_struct_hash_v2(cryptoType, txDataWithHex); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == true); + BOOST_TEST(txDataHash != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // calculate tx data hash success (txDataWithByte) + txDataHash = bcos_sdk_calc_transaction_data_struct_hash_v2(cryptoType, txDataWithByte); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == true); + BOOST_TEST(txDataHash != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // 2. test cal tx data hash with tx_data_hex + // calculate tx data hash with hex failed (cryptoType not in 0,1) + cryptoType = 2; + auto txDataHashWithHex = + bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2(cryptoType, encodedtxDataWithHex); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(txDataHashWithHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("invalid crypto type, it must be BCOS_C_SDK_ECDSA_TYPE(ecdsa crypto " + "type) or BCOS_C_SDK_SM_TYPE(sm crypto type)")); + + // calculate tx data hash failed (cryptoType is negative number) + cryptoType = -11; + txDataHashWithHex = + bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2(cryptoType, encodedtxDataWithHex); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(txDataHashWithHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("invalid crypto type, it must be BCOS_C_SDK_ECDSA_TYPE(ecdsa crypto " + "type) or BCOS_C_SDK_SM_TYPE(sm crypto type)")); + + // calculate tx data hash with hex failed (transaction_data_hex == NULL) + cryptoType = 1; + txDataHashWithHex = bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2(cryptoType, nullptr); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(txDataHashWithHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // calculate tx data hash with hex failed (transaction_data_hex == "") + txDataHashWithHex = bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2(cryptoType, ""); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(txDataHashWithHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("transaction_data_hex can not be empty string")); + + // calculate tx data hash with hex success (encodedtxDataWithHex) + cryptoType = 1; + txDataHashWithHex = + bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2(cryptoType, encodedtxDataWithHex); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == true); + BOOST_TEST(txDataHashWithHex != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // calculate tx data hash with hex success (encodedtxDataWithByte) + txDataHashWithHex = + bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2(cryptoType, encodedtxDataWithByte); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == true); + BOOST_TEST(txDataHashWithHex != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // 3. compare hash + BOOST_TEST(txDataHash == txDataHashWithHex); + + bcos_sdk_destroy_transaction_data_struct_v2(txDataWithHex); + bcos_sdk_destroy_transaction_data_struct_v2(txDataWithByte); +} + +BOOST_AUTO_TEST_CASE(testCreateTxStructV2) +{ + auto txDataStruct = createTxDataStructWithHexInputV2(); + auto transactionDataHash = bcos_sdk_calc_transaction_data_struct_hash_v2(1, txDataStruct); + void* key_pair = bcos_sdk_create_keypair(1); + auto signature = bcos_sdk_sign_transaction_data_hash(key_pair, transactionDataHash); + auto extraData = ""; + int64_t attribute = 0; + + // create transaction_struct failed (transaction_data_struct_v2 = NULL) + auto txStruct = bcos_sdk_create_transaction_struct_v2( + nullptr, signature, transactionDataHash, attribute, extraData); + auto success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(txStruct == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // create transaction_struct failed (transaction_data_hash = NULL) + txStruct = bcos_sdk_create_transaction_struct_v2( + txDataStruct, signature, nullptr, attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(txStruct == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // create transaction_struct failed (signature = NULL) + txStruct = bcos_sdk_create_transaction_struct_v2( + txDataStruct, nullptr, transactionDataHash, attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(txStruct == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // create transaction_struct failed (transaction_data_hash = "") + txStruct = + bcos_sdk_create_transaction_struct_v2(txDataStruct, signature, "", attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(txStruct == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("transaction_data_hash can not be empty string")); + + // create transaction_struct failed (signature = "") + txStruct = bcos_sdk_create_transaction_struct_v2( + txDataStruct, "", transactionDataHash, attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(txStruct == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("signature can not be empty string")); + + // create transaction_struct success + txStruct = bcos_sdk_create_transaction_struct_v2( + txDataStruct, signature, transactionDataHash, attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == true); + + compareTxStruct(txStruct, txDataStruct, signature, extraData, attribute); + + bcos_sdk_destroy_transaction_data_struct_v2(txDataStruct); + bcos_sdk_destroy_transaction_struct_v2(txStruct); +} + +BOOST_AUTO_TEST_CASE(testCreateEncodedTxV2) +{ + auto txDataStruct = createTxDataStructWithHexInputV2(); + auto transactionDataHash = bcos_sdk_calc_transaction_data_struct_hash_v2(1, txDataStruct); + void* key_pair = bcos_sdk_create_keypair(1); + auto signature = bcos_sdk_sign_transaction_data_hash(key_pair, transactionDataHash); + auto extraData = ""; + int64_t attribute = 0; + auto txStruct = bcos_sdk_create_transaction_struct_v2( + txDataStruct, signature, transactionDataHash, attribute, extraData); + + // 1. test bcos_sdk_create_encoded_transaction_v2 + // create encoded tx failed (transaction_data_struct_v2 = NULL) + auto encodedTx = bcos_sdk_create_encoded_transaction_v2( + nullptr, signature, transactionDataHash, attribute, extraData); + auto success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(encodedTx == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // create encoded tx failed (transaction_data_hash = NULL) + encodedTx = bcos_sdk_create_encoded_transaction_v2( + txDataStruct, signature, nullptr, attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(encodedTx == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // create encoded tx failed (signature = NULL) + encodedTx = bcos_sdk_create_encoded_transaction_v2( + txDataStruct, nullptr, transactionDataHash, attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(encodedTx == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // create encoded tx failed (transaction_data_hash = "") + encodedTx = + bcos_sdk_create_encoded_transaction_v2(txDataStruct, signature, "", attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(encodedTx == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("transaction_data_hash can not be empty string")); + + // create encoded tx failed (signature = "") + encodedTx = bcos_sdk_create_encoded_transaction_v2( + txDataStruct, "", transactionDataHash, attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(encodedTx == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("signature can not be empty string")); + + // create encoded tx success + encodedTx = bcos_sdk_create_encoded_transaction_v2( + txDataStruct, signature, transactionDataHash, attribute, extraData); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == true); + BOOST_TEST(encodedTx != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // 2. test bcos_sdk_encode_transaction_struct_to_hex__v2 + // create encoded tx with tx_struct failed (transaction_struct == "") + auto encodedTxWithTxStruct = bcos_sdk_encode_transaction_struct_to_hex_v2(nullptr); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == false); + BOOST_TEST(encodedTxWithTxStruct == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // create encoded tx with tx_struct success + encodedTxWithTxStruct = bcos_sdk_encode_transaction_struct_to_hex_v2(txStruct); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == true); + BOOST_TEST(encodedTxWithTxStruct != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // 3. compare encodedTx encodedTxWithTxStruct + BOOST_TEST(encodedTx == encodedTxWithTxStruct); + + bcos_sdk_destroy_transaction_data_struct_v2(txDataStruct); + bcos_sdk_destroy_transaction_struct_v2(txStruct); +} + +BOOST_AUTO_TEST_CASE(testEncodeDecodeTxStructV2) +{ + auto txDataStruct = createTxDataStructWithHexInputV2(); + auto transactionDataHash = bcos_sdk_calc_transaction_data_struct_hash_v2(1, txDataStruct); + void* key_pair = bcos_sdk_create_keypair(1); + auto signature = bcos_sdk_sign_transaction_data_hash(key_pair, transactionDataHash); + auto extraData = ""; + int64_t attribute = 0; + + auto txStruct = bcos_sdk_create_transaction_struct_v2( + txDataStruct, signature, transactionDataHash, attribute, extraData); + + // 1. test tx encode decode hex + // encode tx to hex failed (transaction == NULL) + auto encodedTxHex = bcos_sdk_encode_transaction_struct_to_hex_v2(nullptr); + auto hexSuccess = bcos_sdk_is_last_opr_success(); + BOOST_TEST(hexSuccess == false); + BOOST_TEST(encodedTxHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // encode tx to hex success + encodedTxHex = bcos_sdk_encode_transaction_struct_to_hex_v2(txStruct); + hexSuccess = bcos_sdk_is_last_opr_success(); + BOOST_TEST(hexSuccess == true); + BOOST_TEST(encodedTxHex != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // decode hex failed (transaction_hex_str == NULL) + auto decodedTxHex = bcos_sdk_decode_transaction_struct_from_hex_v2(nullptr); + hexSuccess = bcos_sdk_is_last_opr_success(); + BOOST_TEST(hexSuccess == false); + BOOST_TEST(decodedTxHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // decode hex failed (transaction_hex_str == "") + decodedTxHex = bcos_sdk_decode_transaction_struct_from_hex_v2(""); + hexSuccess = bcos_sdk_is_last_opr_success(); + BOOST_TEST(hexSuccess == false); + BOOST_TEST(decodedTxHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("transaction_hex_str can not be empty string")); + + // decode hex failed (the length of transaction_hex_str is not even number) + decodedTxHex = bcos_sdk_decode_transaction_struct_from_hex_v2("0xabc"); + hexSuccess = bcos_sdk_is_last_opr_success(); + BOOST_TEST(hexSuccess == false); + BOOST_TEST(decodedTxHex == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("the length of transaction_hex_str must be an even number")); + + // decode hex success + decodedTxHex = bcos_sdk_decode_transaction_struct_from_hex_v2(encodedTxHex); + hexSuccess = bcos_sdk_is_last_opr_success(); + BOOST_TEST(hexSuccess == true); + BOOST_TEST(decodedTxHex != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + compareTxStruct(decodedTxHex, txDataStruct, signature, extraData, attribute); + + // 2. test tx encode decode json + // encode tx to json failed (transaction == NULL) + auto encodedTxJson = bcos_sdk_encode_transaction_struct_to_json_v2(nullptr); + auto jsonSuccess = bcos_sdk_is_last_opr_success(); + BOOST_TEST(jsonSuccess == false); + BOOST_TEST(encodedTxJson == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // encode tx to json success + encodedTxJson = bcos_sdk_encode_transaction_struct_to_json_v2(txStruct); + jsonSuccess = bcos_sdk_is_last_opr_success(); + BOOST_TEST(jsonSuccess == true); + BOOST_TEST(encodedTxJson != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + // decode json failed (transaction_json_str == NULL) + auto decodedTxJson = bcos_sdk_decode_transaction_struct_from_json_v2(nullptr); + jsonSuccess = bcos_sdk_is_last_opr_success(); + BOOST_TEST(jsonSuccess == false); + BOOST_TEST(decodedTxJson == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + + // decode json failed (transaction_json_str == "") + decodedTxJson = bcos_sdk_decode_transaction_struct_from_json_v2(""); + jsonSuccess = bcos_sdk_is_last_opr_success(); + BOOST_TEST(jsonSuccess == false); + BOOST_TEST(decodedTxJson == nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == -1); + BOOST_TEST(bcos_sdk_get_last_error_msg() == + std::string("transaction_json_str can not be empty string")); + + // decode json success + decodedTxJson = bcos_sdk_decode_transaction_struct_from_json_v2(encodedTxJson); + jsonSuccess = bcos_sdk_is_last_opr_success(); + BOOST_TEST(jsonSuccess == true); + BOOST_TEST(decodedTxJson != nullptr); + BOOST_TEST(bcos_sdk_get_last_error() == 0); + + compareTxStruct(decodedTxJson, txDataStruct, signature, extraData, attribute); + + bcos_sdk_destroy_transaction_data_struct_v2(txDataStruct); + bcos_sdk_destroy_transaction_struct_v2(txStruct); + bcos_sdk_destroy_transaction_struct_v2(decodedTxHex); + bcos_sdk_destroy_transaction_struct_v2(decodedTxJson); +} + +BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file From 04a037d95742d2133efca6140f7daa9b48f85829 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Thu, 11 Jan 2024 10:31:06 +0800 Subject: [PATCH 21/29] (csdk,jni): add nonce in transaction builder v2, fix fromHex bug. (#205) --- bcos-c-sdk/bcos_sdk_c_uti_tx_v2.cpp | 112 ++++++++++-------- bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h | 21 ++-- ...tilities_tx_TransactionBuilderV2JniObj.cpp | 39 +++--- ..._utilities_tx_TransactionBuilderV2JniObj.h | 23 ++-- .../tx/TransactionBuilderV2JniObj.java | 14 ++- vcpkg-configuration.json | 2 +- 6 files changed, 125 insertions(+), 86 deletions(-) diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.cpp b/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.cpp index 07434dedc..f6257bb62 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.cpp +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.cpp @@ -34,7 +34,7 @@ using namespace bcos::cppsdk::utilities; #include void* bcos_sdk_create_transaction_v2_data(const char* group_id, const char* chain_id, - const char* to, const unsigned char* input, long inputSize, const char* abi, + const char* to, const char* nonce, const unsigned char* input, long inputSize, const char* abi, int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit) { bcos_sdk_clear_last_error(); @@ -48,9 +48,9 @@ void* bcos_sdk_create_transaction_v2_data(const char* group_id, const char* chai { TransactionBuilderV2 builder; auto bytesData = bytes(input, input + inputSize * sizeof(byte)); - auto transactionData = - builder.createTransactionData(1, group_id, chain_id, to, "", std::move(bytesData), abi, - block_limit, value ? value : "", gas_price ? gas_price : "", gas_limit); + auto transactionData = builder.createTransactionData(1, group_id, chain_id, to, + nonce ? nonce : "", std::move(bytesData), abi, block_limit, value ? value : "", + gas_price ? gas_price : "", gas_limit); return transactionData.release(); } catch (const std::exception& e) @@ -58,11 +58,13 @@ void* bcos_sdk_create_transaction_v2_data(const char* group_id, const char* chai std::string errorMsg = boost::diagnostic_information(e); BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_data") << LOG_DESC("exception") << LOG_KV("group_id", group_id) << LOG_KV("chain_id", chain_id) - << LOG_KV("to", std::string(to ? to : "")) << LOG_KV("input", input) - << LOG_KV("abi", std::string(abi ? abi : "")) + << LOG_KV("to", std::string_view(to ? to : "")) + << LOG_KV("nonoce", std::string_view(nonce ? nonce : "")) + << LOG_KV("inputSize", inputSize) + << LOG_KV("abi", std::string_view(abi ? abi : "")) << LOG_KV("block_limit", block_limit) - << LOG_KV("value", std::string(value ? value : "")) - << LOG_KV("gas_price", std::string(gas_price ? gas_price : "")) + << LOG_KV("value", std::string_view(value ? value : "")) + << LOG_KV("gas_price", std::string_view(gas_price ? gas_price : "")) << LOG_KV("gas_limit", gas_limit) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); } @@ -71,7 +73,7 @@ void* bcos_sdk_create_transaction_v2_data(const char* group_id, const char* chai } void* bcos_sdk_create_eip1559_transaction_data(const char* group_id, const char* chain_id, - const char* to, const unsigned char* input, long inputSize, const char* abi, + const char* to, const char* nonce, const unsigned char* input, long inputSize, const char* abi, int64_t block_limit, const char* value, int64_t gas_limit, const char* max_fee_per_gas, const char* max_priority_fee_per_gas) { @@ -86,9 +88,9 @@ void* bcos_sdk_create_eip1559_transaction_data(const char* group_id, const char* { TransactionBuilderV2 builder; auto bytesData = bytes(input, input + inputSize * sizeof(byte)); - auto transactionData = builder.createTransactionData(1, group_id, chain_id, to, "", - std::move(bytesData), abi, block_limit, value ? value : "", "", gas_limit, - max_fee_per_gas ? max_fee_per_gas : "", + auto transactionData = builder.createTransactionData(1, group_id, chain_id, to, + nonce ? nonce : "", std::move(bytesData), abi, block_limit, value ? value : "", "", + gas_limit, max_fee_per_gas ? max_fee_per_gas : "", max_priority_fee_per_gas ? max_priority_fee_per_gas : ""); return transactionData.release(); } @@ -98,12 +100,15 @@ void* bcos_sdk_create_eip1559_transaction_data(const char* group_id, const char* BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_eip1559_transaction_data") << LOG_DESC("exception") << LOG_KV("group_id", group_id) << LOG_KV("chain_id", chain_id) - << LOG_KV("to", std::string(to ? to : "")) << LOG_KV("input", input) - << LOG_KV("abi", std::string(abi ? abi : "")) << LOG_KV("block_limit", block_limit) - << LOG_KV("value", std::string(value ? value : "")) << LOG_KV("gas_limit", gas_limit) - << LOG_KV("max_fee_per_gas", std::string(max_fee_per_gas ? max_fee_per_gas : "")) + << LOG_KV("to", std::string_view(to ? to : "")) + << LOG_KV("nonoce", std::string_view(nonce ? nonce : "")) + << LOG_KV("inputSize", inputSize) << LOG_KV("abi", std::string_view(abi ? abi : "")) + << LOG_KV("block_limit", block_limit) + << LOG_KV("value", std::string_view(value ? value : "")) + << LOG_KV("gas_limit", gas_limit) + << LOG_KV("max_fee_per_gas", std::string_view(max_fee_per_gas ? max_fee_per_gas : "")) << LOG_KV("max_priority_fee_per_gas", - std::string(max_priority_fee_per_gas ? max_priority_fee_per_gas : "")) + std::string_view(max_priority_fee_per_gas ? max_priority_fee_per_gas : "")) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); } @@ -143,15 +148,16 @@ const char* bcos_sdk_calc_transaction_data_hash_with_full_fields(int crypto_type BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_calc_transaction_data_hash_with_full_fields") << LOG_DESC("exception") << LOG_KV("group_id", group_id) << LOG_KV("chain_id", chain_id) - << LOG_KV("to", std::string(to ? to : "")) - << LOG_KV("nonce", std::string(nonce ? nonce : "")) << LOG_KV("input", input) - << LOG_KV("abi", std::string(abi ? abi : "")) << LOG_KV("block_limit", block_limit) - << LOG_KV("value", std::string(value ? value : "")) - << LOG_KV("gas_price", std::string(gas_price ? gas_price : "")) + << LOG_KV("to", std::string_view(to ? to : "")) + << LOG_KV("nonce", std::string_view(nonce ? nonce : "")) + << LOG_KV("inputSize", inputSize) << LOG_KV("abi", std::string_view(abi ? abi : "")) + << LOG_KV("block_limit", block_limit) + << LOG_KV("value", std::string_view(value ? value : "")) + << LOG_KV("gas_price", std::string_view(gas_price ? gas_price : "")) << LOG_KV("gas_limit", gas_limit) - << LOG_KV("max_fee_per_gas", std::string(max_fee_per_gas ? max_fee_per_gas : "")) + << LOG_KV("max_fee_per_gas", std::string_view(max_fee_per_gas ? max_fee_per_gas : "")) << LOG_KV("max_priority_fee_per_gas", - std::string(max_priority_fee_per_gas ? max_priority_fee_per_gas : "")) + std::string_view(max_priority_fee_per_gas ? max_priority_fee_per_gas : "")) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); } @@ -174,7 +180,7 @@ const char* bcos_sdk_calc_transaction_data_hash_with_json(int crypto_type, const { std::string errorMsg = boost::diagnostic_information(e); BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_calc_transaction_data_hash_with_json") - << LOG_DESC("exception") << LOG_KV("json", json) + << LOG_DESC("exception") << LOG_KV("json", std::string_view(json)) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); } @@ -201,13 +207,14 @@ const char* bcos_sdk_create_signed_transaction_with_signature(const unsigned cha { TransactionBuilderV2 builder; auto bytesData = bytes(input, input + inputSize * sizeof(byte)); - crypto::HashType tx_hash(fromHex(std::string_view(transaction_hash))); + auto hash = std::string_view(transaction_hash); + crypto::HashType tx_hash(fromHex(hash, hash.starts_with("0x") ? "0x" : "")); auto sign = bytes(signature, signature + signSize * sizeof(byte)); - auto transaction = builder.createTransaction(std::move(sign), tx_hash, attribute, - version, group_id, chain_id, to ? to : "", nonce ? nonce : "", - std::move(bytesData), abi ? abi : "", block_limit, value ? value : "", - gas_price ? gas_price : "", gas_limit, max_fee_per_gas ? max_fee_per_gas : "", + auto transaction = builder.createTransaction(std::move(sign), tx_hash, attribute, version, + group_id, chain_id, to ? to : "", nonce ? nonce : "", std::move(bytesData), + abi ? abi : "", block_limit, value ? value : "", gas_price ? gas_price : "", gas_limit, + max_fee_per_gas ? max_fee_per_gas : "", max_priority_fee_per_gas ? max_priority_fee_per_gas : "", extra_data ? extra_data : ""); auto bytes = builder.encodeTransaction(*transaction); @@ -218,26 +225,27 @@ const char* bcos_sdk_create_signed_transaction_with_signature(const unsigned cha { std::string errorMsg = boost::diagnostic_information(e); BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_signed_transaction_with_signature") - << LOG_DESC("exception") << LOG_KV("signature", signature) + << LOG_DESC("exception") << LOG_KV("signSize", signSize) << LOG_KV("transaction_hash", transaction_hash) << LOG_KV("group_id", group_id) << LOG_KV("chain_id", chain_id) - << LOG_KV("to", std::string(to ? to : "")) - << LOG_KV("nonce", std::string(nonce ? nonce : "")) - << LOG_KV("input", input) << LOG_KV("abi", std::string(abi ? abi : "")) + << LOG_KV("to", std::string_view(to ? to : "")) + << LOG_KV("nonce", std::string_view(nonce ? nonce : "")) + << LOG_KV("inputSize", inputSize) + << LOG_KV("abi", std::string_view(abi ? abi : "")) << LOG_KV("block_limit", block_limit) - << LOG_KV("value", std::string(value ? value : "")) - << LOG_KV("gas_price", std::string(gas_price ? gas_price : "")) + << LOG_KV("value", std::string_view(value ? value : "")) + << LOG_KV("gas_price", std::string_view(gas_price ? gas_price : "")) << LOG_KV("gas_limit", gas_limit) << LOG_KV("max_fee_per_gas", - std::string(max_fee_per_gas ? max_fee_per_gas : "")); + std::string_view(max_fee_per_gas ? max_fee_per_gas : "")); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); } return nullptr; } void bcos_sdk_create_signed_transaction_with_full_fields(void* key_pair, const char* group_id, - const char* chain_id, const char* to, const unsigned char* input, long inputSize, - const char* abi, int64_t block_limit, const char* value, const char* gas_price, + const char* chain_id, const char* to, const char* nonce, const unsigned char* input, + long inputSize, const char* abi, int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit, int32_t attribute, const char* extra_data, char** tx_hash, char** signed_tx) { bcos_sdk_clear_last_error(); @@ -256,9 +264,9 @@ void bcos_sdk_create_signed_transaction_with_full_fields(void* key_pair, const c auto bytesData = bytes(input, input + inputSize * sizeof(byte)); auto result = builder.createSignedTransaction(*static_cast(key_pair), - attribute, 1, group_id, chain_id, to ? to : "", "", std::move(bytesData), - abi ? abi : "", block_limit, value ? value : "", gas_price ? gas_price : "", - gas_limit, "", "", extra_data ? extra_data : ""); + attribute, 1, group_id, chain_id, to ? to : "", nonce ? nonce : "", + std::move(bytesData), abi ? abi : "", block_limit, value ? value : "", + gas_price ? gas_price : "", gas_limit, "", "", extra_data ? extra_data : ""); *tx_hash = strdup(result.first.c_str()); *signed_tx = strdup(result.second.c_str()); } @@ -269,6 +277,7 @@ void bcos_sdk_create_signed_transaction_with_full_fields(void* key_pair, const c << LOG_DESC("exception") << LOG_KV("group_id", group_id) << LOG_KV("chain_id", chain_id) << LOG_KV("to", std::string_view(to ? to : "")) + << LOG_KV("nonoce", std::string_view(nonce ? nonce : "")) << LOG_KV("inputSize", inputSize) << LOG_KV("abi", std::string_view(abi ? abi : "")) << LOG_KV("block_limit", block_limit) @@ -282,10 +291,11 @@ void bcos_sdk_create_signed_transaction_with_full_fields(void* key_pair, const c } void bcos_sdk_create_signed_eip1559_transaction_with_full_fields(void* key_pair, - const char* group_id, const char* chain_id, const char* to, const unsigned char* input, - long inputSize, const char* abi, int64_t block_limit, const char* value, int64_t gas_limit, - const char* max_fee_per_gas, const char* max_priority_fee_per_gas, int32_t attribute, - const char* extra_data, char** tx_hash, char** signed_tx) + const char* group_id, const char* chain_id, const char* to, const char* nonce, + const unsigned char* input, long inputSize, const char* abi, int64_t block_limit, + const char* value, int64_t gas_limit, const char* max_fee_per_gas, + const char* max_priority_fee_per_gas, int32_t attribute, const char* extra_data, char** tx_hash, + char** signed_tx) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(key_pair, ) @@ -303,8 +313,8 @@ void bcos_sdk_create_signed_eip1559_transaction_with_full_fields(void* key_pair, auto bytesData = bytes(input, input + inputSize * sizeof(byte)); auto result = builder.createSignedTransaction( *static_cast(key_pair), attribute, 1, group_id, - chain_id, to ? to : "", "", std::move(bytesData), abi ? abi : "", block_limit, - value ? value : "", "", gas_limit, max_fee_per_gas ? max_fee_per_gas : "", + chain_id, to ? to : "", nonce ? nonce : "", std::move(bytesData), abi ? abi : "", + block_limit, value ? value : "", "", gas_limit, max_fee_per_gas ? max_fee_per_gas : "", max_priority_fee_per_gas ? max_priority_fee_per_gas : "", extra_data ? extra_data : ""); *tx_hash = strdup(result.first.c_str()); *signed_tx = strdup(result.second.c_str()); @@ -315,8 +325,10 @@ void bcos_sdk_create_signed_eip1559_transaction_with_full_fields(void* key_pair, BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_signed_eip1559_transaction_with_full_fields") << LOG_DESC("exception") << LOG_KV("group_id", group_id) << LOG_KV("chain_id", chain_id) - << LOG_KV("to", std::string_view(to ? to : "")) << LOG_KV("inputSize", inputSize) - << LOG_KV("abi", std::string_view(abi ? abi : "")) << LOG_KV("block_limit", block_limit) + << LOG_KV("to", std::string_view(to ? to : "")) + << LOG_KV("nonoce", std::string_view(nonce ? nonce : "")) + << LOG_KV("inputSize", inputSize) << LOG_KV("abi", std::string_view(abi ? abi : "")) + << LOG_KV("block_limit", block_limit) << LOG_KV("value", std::string_view(value ? value : "")) << LOG_KV("gas_limit", gas_limit) << LOG_KV("max_fee_per_gas", std::string_view(max_fee_per_gas ? max_fee_per_gas : "")) diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h b/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h index 48e85bbc3..ac337da09 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h @@ -44,6 +44,7 @@ enum transaction_version * @param group_id group id * @param chain_id chain id * @param to contract address, if it is a contract creation transaction, it can be empty + * @param nonce random number to avoid duplicate transactions, if empty, then will generate one * @param input encoded contract method and params, bytes array * @param inputSize encoded contract method and params size * @param abi contract abi, only create contract need @@ -54,7 +55,7 @@ enum transaction_version * @return void* transaction data pointer, you should release it after use */ void* bcos_sdk_create_transaction_v2_data(const char* group_id, const char* chain_id, - const char* to, const unsigned char* input, long inputSize, const char* abi, + const char* to, const char* nonce, const unsigned char* input, long inputSize, const char* abi, int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit); /** @@ -64,6 +65,7 @@ void* bcos_sdk_create_transaction_v2_data(const char* group_id, const char* chai * @param group_id group id * @param chain_id chain id * @param to contract address, if it is a contract creation transaction, it can be empty + * @param nonce random number to avoid duplicate transactions, if empty, then will generate one * @param input encoded contract method and params, bytes array * @param inputSize encoded contract method and params size * @param abi contract abi, only create contract need @@ -75,7 +77,7 @@ void* bcos_sdk_create_transaction_v2_data(const char* group_id, const char* chai * @return void* transaction data pointer, you should release it after use */ void* bcos_sdk_create_eip1559_transaction_data(const char* group_id, const char* chain_id, - const char* to, const unsigned char* input, long inputSize, const char* abi, + const char* to, const char* nonce, const unsigned char* input, long inputSize, const char* abi, int64_t block_limit, const char* value, int64_t gas_limit, const char* max_fee_per_gas, const char* max_priority_fee_per_gas); @@ -159,6 +161,7 @@ const char* bcos_sdk_create_signed_transaction_with_signature(const unsigned cha * @param group_id group id * @param chain_id chain id * @param to contract address, if it is a contract creation transaction, it can be empty + * @param nonce random number to avoid duplicate transactions, if empty, then will generate one * @param input encoded contract method and params, bytes array * @param inputSize encoded contract method and params size * @param abi contract abi, only create contract need @@ -172,8 +175,8 @@ const char* bcos_sdk_create_signed_transaction_with_signature(const unsigned cha * @param signed_tx output signed transaction hex string */ void bcos_sdk_create_signed_transaction_with_full_fields(void* key_pair, const char* group_id, - const char* chain_id, const char* to, const unsigned char* input, long inputSize, - const char* abi, int64_t block_limit, const char* value, const char* gas_price, + const char* chain_id, const char* to, const char* nonce, const unsigned char* input, + long inputSize, const char* abi, int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit, int32_t attribute, const char* extra_data, char** tx_hash, char** signed_tx); /** @@ -184,6 +187,7 @@ void bcos_sdk_create_signed_transaction_with_full_fields(void* key_pair, const c * @param group_id group id * @param chain_id chain id * @param to contract address, if it is a contract creation transaction, it can be empty + * @param nonce random number to avoid duplicate transactions, if empty, then will generate one * @param input encoded contract method and params, bytes array * @param inputSize encoded contract method and params size * @param abi contract abi, only create contract need @@ -198,10 +202,11 @@ void bcos_sdk_create_signed_transaction_with_full_fields(void* key_pair, const c * @param signed_tx output signed transaction hex string */ void bcos_sdk_create_signed_eip1559_transaction_with_full_fields(void* key_pair, - const char* group_id, const char* chain_id, const char* to, const unsigned char* input, - long inputSize, const char* abi, int64_t block_limit, const char* value, int64_t gas_limit, - const char* max_fee_per_gas, const char* max_priority_fee_per_gas, int32_t attribute, - const char* extra_data, char** tx_hash, char** signed_tx); + const char* group_id, const char* chain_id, const char* to, const char* nonce, + const unsigned char* input, long inputSize, const char* abi, int64_t block_limit, + const char* value, int64_t gas_limit, const char* max_fee_per_gas, + const char* max_priority_fee_per_gas, int32_t attribute, const char* extra_data, char** tx_hash, + char** signed_tx); #ifdef __cplusplus } diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp index 538dd341a..ab940033e 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp @@ -29,12 +29,13 @@ * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: createTransactionData * Signature: - * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;J)J + * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;J)J */ JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createTransactionData( - JNIEnv* env, jclass, jstring j_group_id, jstring j_chain_id, jstring j_to, jbyteArray j_input, - jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_gas_price, jlong j_gas_limit) + JNIEnv* env, jclass, jstring j_group_id, jstring j_chain_id, jstring j_to, jstring j_nonce, + jbyteArray j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_gas_price, + jlong j_gas_limit) { CHECK_OBJECT_NOT_NULL(env, j_group_id, 0); CHECK_OBJECT_NOT_NULL(env, j_chain_id, 0); @@ -46,13 +47,14 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createTransa const char* abi = GET_J_STRING_CONTENT_DEF(env, j_abi, NULL); const char* value = GET_J_STRING_CONTENT_DEF(env, j_value, NULL); const char* gas_price = GET_J_STRING_CONTENT_DEF(env, j_gas_price, NULL); + const char* nonce = GET_J_STRING_CONTENT_DEF(env, j_nonce, NULL); int64_t block_limit = j_block_limit; int64_t gas_limit = j_gas_limit; jbyte* inputBuffer = env->GetByteArrayElements(j_input, NULL); jsize inputSize = env->GetArrayLength(j_input); - void* transaction_data = bcos_sdk_create_transaction_v2_data(group_id, chain_id, to, + void* transaction_data = bcos_sdk_create_transaction_v2_data(group_id, chain_id, to, nonce, reinterpret_cast(inputBuffer), inputSize, abi, block_limit, value, gas_price, gas_limit); @@ -62,6 +64,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createTransa env->ReleaseStringUTFChars(j_abi, abi); env->ReleaseStringUTFChars(j_value, value); env->ReleaseStringUTFChars(j_gas_price, gas_price); + env->ReleaseStringUTFChars(j_nonce, nonce); env->ReleaseByteArrayElements(j_input, inputBuffer, 0); if (!bcos_sdk_is_last_opr_success()) @@ -77,12 +80,12 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createTransa * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: createEIP1559TransactionData * Signature: - * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;JLjava/lang/String;Ljava/lang/String;)J + * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;JLjava/lang/String;Ljava/lang/String;)J */ JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createEIP1559TransactionData( - JNIEnv* env, jclass, jstring j_group_id, jstring j_chain_id, jstring j_to, jbyteArray j_input, - jstring j_abi, jlong j_block_limit, jstring j_value, jlong j_gas_limit, + JNIEnv* env, jclass, jstring j_group_id, jstring j_chain_id, jstring j_to, jstring j_nonce, + jbyteArray j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jlong j_gas_limit, jstring j_max_fee_per_gas, jstring j_max_priority_fee_per_gas) { CHECK_OBJECT_NOT_NULL(env, j_group_id, 0); @@ -92,6 +95,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createEIP155 const char* group_id = GET_J_STRING_CONTENT(env, j_group_id); const char* chain_id = GET_J_STRING_CONTENT(env, j_chain_id); const char* to = GET_J_STRING_CONTENT_DEF(env, j_to, NULL); + const char* nonce = GET_J_STRING_CONTENT_DEF(env, j_nonce, NULL); const char* abi = GET_J_STRING_CONTENT_DEF(env, j_abi, NULL); const char* value = GET_J_STRING_CONTENT_DEF(env, j_value, NULL); const char* max_fee_per_gas = GET_J_STRING_CONTENT_DEF(env, j_max_fee_per_gas, NULL); @@ -102,13 +106,14 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createEIP155 jbyte* inputBuffer = env->GetByteArrayElements(j_input, NULL); jsize inputSize = env->GetArrayLength(j_input); - void* transaction_data = bcos_sdk_create_eip1559_transaction_data(group_id, chain_id, to, + void* transaction_data = bcos_sdk_create_eip1559_transaction_data(group_id, chain_id, to, nonce, reinterpret_cast(inputBuffer), inputSize, abi, block_limit, value, gas_limit, max_fee_per_gas, max_priority_fee_per_gas); env->ReleaseStringUTFChars(j_group_id, group_id); env->ReleaseStringUTFChars(j_chain_id, chain_id); env->ReleaseStringUTFChars(j_to, to); + env->ReleaseStringUTFChars(j_nonce, nonce); env->ReleaseStringUTFChars(j_abi, abi); env->ReleaseStringUTFChars(j_value, value); env->ReleaseStringUTFChars(j_max_fee_per_gas, max_fee_per_gas); @@ -316,13 +321,13 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: createSignedTransactionWithFullFields * Signature: - * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; + * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; */ JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedTransactionWithFullFields( JNIEnv* env, jclass, jlong j_key_pair, jstring j_group_id, jstring j_chain_id, jstring j_to, - jbyteArray j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_gas_price, - jlong j_gas_limit, jint j_attribute, jstring j_extra_data) + jstring j_nonce, jbyteArray j_input, jstring j_abi, jlong j_block_limit, jstring j_value, + jstring j_gas_price, jlong j_gas_limit, jint j_attribute, jstring j_extra_data) { CHECK_OBJECT_NOT_NULL(env, j_group_id, NULL); CHECK_OBJECT_NOT_NULL(env, j_chain_id, NULL); @@ -335,6 +340,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned const char* group_id = GET_J_STRING_CONTENT(env, j_group_id); const char* chain_id = GET_J_STRING_CONTENT(env, j_chain_id); const char* to = GET_J_STRING_CONTENT_DEF(env, j_to, NULL); + const char* nonce = GET_J_STRING_CONTENT_DEF(env, j_nonce, NULL); const char* abi = GET_J_STRING_CONTENT_DEF(env, j_abi, NULL); const char* value = GET_J_STRING_CONTENT_DEF(env, j_value, NULL); const char* gas_price = GET_J_STRING_CONTENT_DEF(env, j_gas_price, NULL); @@ -344,7 +350,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned char* tx_hash = NULL; char* signed_tx = NULL; - bcos_sdk_create_signed_transaction_with_full_fields(keypair, group_id, chain_id, to, + bcos_sdk_create_signed_transaction_with_full_fields(keypair, group_id, chain_id, to, nonce, reinterpret_cast(inputBuffer), inputSize, abi, block_limit, value, gas_price, gas_limit, attribute, extra_data, &tx_hash, &signed_tx); @@ -376,6 +382,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned env->ReleaseStringUTFChars(j_group_id, group_id); env->ReleaseStringUTFChars(j_chain_id, chain_id); env->ReleaseStringUTFChars(j_to, to); + env->ReleaseStringUTFChars(j_nonce, nonce); env->ReleaseStringUTFChars(j_abi, abi); env->ReleaseStringUTFChars(j_value, value); env->ReleaseStringUTFChars(j_gas_price, gas_price); @@ -404,12 +411,12 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: createSignedEIP1559TransactionWithFullFields * Signature: - * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; + * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; */ JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedEIP1559TransactionWithFullFields( JNIEnv* env, jclass, jlong j_key_pair, jstring j_group_id, jstring j_chain_id, jstring j_to, - jbyteArray j_input, jstring j_abi, jlong j_block_limit, jstring j_value, + jstring j_nonce, jbyteArray j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_max_fee_per_gas, jstring j_max_priority_fee_per_gas, jlong j_gas_limit, jint j_attribute, jstring j_extra_data) { @@ -424,6 +431,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned const char* group_id = GET_J_STRING_CONTENT(env, j_group_id); const char* chain_id = GET_J_STRING_CONTENT(env, j_chain_id); const char* to = GET_J_STRING_CONTENT_DEF(env, j_to, NULL); + const char* nonce = GET_J_STRING_CONTENT_DEF(env, j_nonce, NULL); const char* abi = GET_J_STRING_CONTENT_DEF(env, j_abi, NULL); const char* value = GET_J_STRING_CONTENT_DEF(env, j_value, NULL); const char* max_fee_per_gas = GET_J_STRING_CONTENT_DEF(env, j_max_fee_per_gas, NULL); @@ -436,7 +444,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned char* tx_hash = NULL; char* signed_tx = NULL; bcos_sdk_create_signed_eip1559_transaction_with_full_fields(keypair, group_id, chain_id, to, - reinterpret_cast(inputBuffer), inputSize, abi, block_limit, value, + nonce, reinterpret_cast(inputBuffer), inputSize, abi, block_limit, value, gas_limit, max_fee_per_gas, max_priority_fee_per_gas, attribute, extra_data, &tx_hash, &signed_tx); @@ -468,6 +476,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned env->ReleaseStringUTFChars(j_group_id, group_id); env->ReleaseStringUTFChars(j_chain_id, chain_id); env->ReleaseStringUTFChars(j_to, to); + env->ReleaseStringUTFChars(j_nonce, nonce); env->ReleaseStringUTFChars(j_abi, abi); env->ReleaseStringUTFChars(j_value, value); env->ReleaseStringUTFChars(j_max_fee_per_gas, max_fee_per_gas); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.h b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.h index 8995ba6a6..4fd67599c 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.h +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.h @@ -11,22 +11,23 @@ extern "C" { * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: createTransactionData * Signature: - * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;J)J + * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;J)J */ JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createTransactionData(JNIEnv*, - jclass, jstring, jstring, jstring, jbyteArray, jstring, jlong, jstring, jstring, jlong); + jclass, jstring, jstring, jstring, jstring, jbyteArray, jstring, jlong, jstring, jstring, + jlong); /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: createEIP1559TransactionData * Signature: - * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;JLjava/lang/String;Ljava/lang/String;)J + * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;JLjava/lang/String;Ljava/lang/String;)J */ JNIEXPORT jlong JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createEIP1559TransactionData( - JNIEnv*, jclass, jstring, jstring, jstring, jbyteArray, jstring, jlong, jstring, jlong, jstring, - jstring); + JNIEnv*, jclass, jstring, jstring, jstring, jstring, jbyteArray, jstring, jlong, jstring, jlong, + jstring, jstring); /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj @@ -63,23 +64,23 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: createSignedTransactionWithFullFields * Signature: - * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; + * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; */ JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedTransactionWithFullFields( - JNIEnv*, jclass, jlong, jstring, jstring, jstring, jbyteArray, jstring, jlong, jstring, jstring, - jlong, jint, jstring); + JNIEnv*, jclass, jlong, jstring, jstring, jstring, jstring, jbyteArray, jstring, jlong, jstring, + jstring, jlong, jint, jstring); /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj * Method: createSignedEIP1559TransactionWithFullFields * Signature: - * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; + * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; */ JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedEIP1559TransactionWithFullFields( - JNIEnv*, jclass, jlong, jstring, jstring, jstring, jbyteArray, jstring, jlong, jstring, jstring, - jstring, jlong, jint, jstring); + JNIEnv*, jclass, jlong, jstring, jstring, jstring, jstring, jbyteArray, jstring, jlong, jstring, + jstring, jstring, jlong, jint, jstring); #ifdef __cplusplus } diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV2JniObj.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV2JniObj.java index d490ab57b..f2cbfff21 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV2JniObj.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV2JniObj.java @@ -15,6 +15,8 @@ public class TransactionBuilderV2JniObj extends TransactionBuilderJniObj { * @param groupID group id * @param chainID chain id * @param to contract address, if it is a contract creation transaction, it can be empty + * @param nonce random number to avoid duplicate transactions, if it is empty or null, then it + * will generate one * @param input encoded contract method and params * @param abi contract abi, only create contract need * @param blockLimit block limit @@ -29,6 +31,7 @@ public static native long createTransactionData( String groupID, String chainID, String to, + String nonce, byte[] input, String abi, long blockLimit, @@ -43,6 +46,8 @@ public static native long createTransactionData( * @param groupID group id * @param chainID chain id * @param to contract address, if it is a contract creation transaction, it can be empty + * @param nonce random number to avoid duplicate transactions, if it is empty or null, then it + * will generate one * @param input encoded contract method and params * @param abi contract abi, only create contract need * @param blockLimit block limit @@ -58,6 +63,7 @@ public static native long createEIP1559TransactionData( String groupID, String chainID, String to, + String nonce, byte[] input, String abi, long blockLimit, @@ -111,8 +117,8 @@ public static native String calcTransactionDataHashWithFullFields( * @param cryptoType 0: keccak256, 1: sm3 * @param json transaction data json string * @return const char* transaction data hash hex string - * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later * @throws JniException if lack of some required fields, or some fields are invalid + * @apiNote version 1 transaction only supported in FISCO BCOS 3.6.0 and later */ public static native String calcTransactionDataHashWithJson(int cryptoType, String json) throws JniException; @@ -168,6 +174,8 @@ public static native String createSignedTransactionWithSignature( * @param groupID group id * @param chainID chain id * @param to contract address, if it is a contract creation transaction, it can be empty + * @param nonce random number to avoid duplicate transactions, if it is empty or null, then it + * will generate one * @param input encoded contract method and params * @param abi contract abi, only create contract need * @param blockLimit block limit @@ -184,6 +192,7 @@ public static native TxPair createSignedTransactionWithFullFields( String groupID, String chainID, String to, + String nonce, byte[] input, String abi, long blockLimit, @@ -201,6 +210,8 @@ public static native TxPair createSignedTransactionWithFullFields( * @param groupID group id * @param chainID chain id * @param to contract address, if it is a contract creation transaction, it can be empty + * @param nonce random number to avoid duplicate transactions, if it is empty or null, then it + * will generate one * @param input encoded contract method and params * @param abi contract abi, only create contract need * @param blockLimit block limit @@ -218,6 +229,7 @@ public static native TxPair createSignedEIP1559TransactionWithFullFields( String groupID, String chainID, String to, + String nonce, byte[] input, String abi, long blockLimit, diff --git a/vcpkg-configuration.json b/vcpkg-configuration.json index 22102b4ab..6e8373d25 100644 --- a/vcpkg-configuration.json +++ b/vcpkg-configuration.json @@ -3,7 +3,7 @@ { "kind": "git", "repository": "https://github.com/FISCO-BCOS/registry", - "baseline": "3765dfb347d9fc820cfad9005b2f87d0755895ec", + "baseline": "61736e4c9b7926d3b11b22df0fd2946c5331b291", "packages": [ "openssl", "hsm-crypto", From efd80f3688a9f994fb8bcc3ccce7ba81ee536b8c Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Thu, 11 Jan 2024 11:35:34 +0800 Subject: [PATCH 22/29] (jni): perf jni code format, fix ci fetch vcpkg error. (#206) --- .github/workflows/workflow.yml | 8 - .../jni/utilities/tx/TransactionDataV2.java | 232 ++++----- .../tx/TransactionStructBuilderV2JniObj.java | 3 +- .../sdk/jni/utilities/tx/TransactionV2.java | 98 ++-- .../bcos/sdk/jni/test/tx/TestTxStructV2.java | 461 ++++++++++-------- 5 files changed, 430 insertions(+), 372 deletions(-) diff --git a/.github/workflows/workflow.yml b/.github/workflows/workflow.yml index 5e8652c64..cdfc877cb 100644 --- a/.github/workflows/workflow.yml +++ b/.github/workflows/workflow.yml @@ -42,8 +42,6 @@ jobs: - name: install macOS dependencies if: runner.os == 'macOS' run: brew install ccache lcov - - name: fetch vcpkg - run: cd /usr/local/share/vcpkg/ && git status && git reset --hard && git stash && git fetch && git pull && cd - - name: configure if: runner.os == 'macOS' run: | @@ -91,8 +89,6 @@ jobs: vcpkg-gcc-v1-notest-${{ runner.temp }}- - name: install Ubuntu dependencies run: sudo apt install -y git curl openssl build-essential cmake ccache lcov - # - name: fetch vcpkg - # run: cd /usr/local/share/vcpkg/ && git fetch --all && git checkout master && git pull && cd - - name: configure run: | export CC='gcc-10'; export CXX='g++-10' @@ -156,8 +152,6 @@ jobs: ln -s /usr/local/cmake/bin/cmake /usr/bin/cmake cmake --version git --version - # - name: fetch vcpkg - # run: cd /usr/local/share/vcpkg/ && git fetch --all && git checkout master && git pull && cd - - name: configure run: | export PATH="/usr/lib/ccache:/usr/local/opt/ccache/libexec:$PATH" @@ -210,8 +204,6 @@ jobs: vcpkg-msvc-v1-notest-${{ runner.temp }}- - name: Add MSbuild to PATH uses: microsoft/setup-msbuild@v1.1 - - name: fetch vcpkg - run: cd C:\vcpkg && git fetch && git reset --hard && git pull && cd - - name: configure if: runner.os == 'Windows' run: | diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2.java index 800ae8d86..290a1776a 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2.java @@ -1,121 +1,121 @@ package org.fisco.bcos.sdk.jni.utilities.tx; public class TransactionDataV2 { - private int version; - private long blockLimit; - private String chainId; - private String groupId; - private String nonce; - private String to; - private String abi; - private TransactionBytes input; - private String value; - private String gasPrice; - private long gasLimit; - private String maxFeePerGas; - private String maxPriorityFeePerGas; - - public int getVersion() { - return version; - } - - public void setVersion(int version) { - this.version = version; - } - - public long getBlockLimit() { - return blockLimit; - } - - public void setBlockLimit(long blockLimit) { - this.blockLimit = blockLimit; - } - - public String getChainId() { - return chainId; - } - - public void setChainId(String chainId) { - this.chainId = chainId; - } - - public String getGroupId() { - return groupId; - } - - public void setGroupId(String groupId) { - this.groupId = groupId; - } - - public String getNonce() { - return nonce; - } - - public void setNonce(String nonce) { - this.nonce = nonce; - } - - public String getTo() { - return to; - } - - public void setTo(String to) { - this.to = to; - } - - public String getAbi() { - return abi; - } - - public void setAbi(String abi) { - this.abi = abi; - } - - public TransactionBytes getInput() { - return input; - } - - public void setInput(TransactionBytes input) { - this.input = input; - } - - public String getValue() { - return value; - } - - public void setValue(String value) { - this.value = value; - } - - public String getGasPrice() { - return gasPrice; - } - - public void setGasPrice(String gasPrice) { - this.gasPrice = gasPrice; - } - - public long getGasLimit() { - return gasLimit; - } - - public void setGasLimit(long gasLimit) { - this.gasLimit = gasLimit; - } - - public String getMaxFeePerGas() { - return maxFeePerGas; - } - - public void setMaxFeePerGas(String maxFeePerGas) { - this.maxFeePerGas = maxFeePerGas; - } - - public String getMaxPriorityFeePerGas() { - return maxPriorityFeePerGas; - } + private int version; + private long blockLimit; + private String chainId; + private String groupId; + private String nonce; + private String to; + private String abi; + private TransactionBytes input; + private String value; + private String gasPrice; + private long gasLimit; + private String maxFeePerGas; + private String maxPriorityFeePerGas; + + public int getVersion() { + return version; + } + + public void setVersion(int version) { + this.version = version; + } + + public long getBlockLimit() { + return blockLimit; + } + + public void setBlockLimit(long blockLimit) { + this.blockLimit = blockLimit; + } + + public String getChainId() { + return chainId; + } + + public void setChainId(String chainId) { + this.chainId = chainId; + } + + public String getGroupId() { + return groupId; + } + + public void setGroupId(String groupId) { + this.groupId = groupId; + } + + public String getNonce() { + return nonce; + } + + public void setNonce(String nonce) { + this.nonce = nonce; + } + + public String getTo() { + return to; + } + + public void setTo(String to) { + this.to = to; + } + + public String getAbi() { + return abi; + } + + public void setAbi(String abi) { + this.abi = abi; + } + + public TransactionBytes getInput() { + return input; + } + + public void setInput(TransactionBytes input) { + this.input = input; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + public String getGasPrice() { + return gasPrice; + } + + public void setGasPrice(String gasPrice) { + this.gasPrice = gasPrice; + } + + public long getGasLimit() { + return gasLimit; + } + + public void setGasLimit(long gasLimit) { + this.gasLimit = gasLimit; + } + + public String getMaxFeePerGas() { + return maxFeePerGas; + } + + public void setMaxFeePerGas(String maxFeePerGas) { + this.maxFeePerGas = maxFeePerGas; + } + + public String getMaxPriorityFeePerGas() { + return maxPriorityFeePerGas; + } - public void setMaxPriorityFeePerGas(String maxPriorityFeePerGas) { - this.maxPriorityFeePerGas = maxPriorityFeePerGas; - } + public void setMaxPriorityFeePerGas(String maxPriorityFeePerGas) { + this.maxPriorityFeePerGas = maxPriorityFeePerGas; + } } diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderV2JniObj.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderV2JniObj.java index a5a2be374..c1945f3d4 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderV2JniObj.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderV2JniObj.java @@ -58,7 +58,8 @@ public static native String createEncodedTransactionV2( * @param transaction Transaction struct * @return Hexed Transaction */ - public static native String encodeTransactionStructV2(TransactionV2 transaction) throws JniException; + public static native String encodeTransactionStructV2(TransactionV2 transaction) + throws JniException; /** * @param transaction Transaction struct diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionV2.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionV2.java index 4faa575f9..ca37b2009 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionV2.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionV2.java @@ -1,67 +1,67 @@ package org.fisco.bcos.sdk.jni.utilities.tx; public class TransactionV2 { - private TransactionDataV2 transactionData; - private TransactionBytes dataHash; - private TransactionBytes signature; - private TransactionBytes sender; - private long importTime; - private int attribute; - private String extraData; + private TransactionDataV2 transactionData; + private TransactionBytes dataHash; + private TransactionBytes signature; + private TransactionBytes sender; + private long importTime; + private int attribute; + private String extraData; - public TransactionDataV2 getTransactionData() { - return transactionData; - } + public TransactionDataV2 getTransactionData() { + return transactionData; + } - public void setTransactionData(TransactionDataV2 transactionData) { - this.transactionData = transactionData; - } + public void setTransactionData(TransactionDataV2 transactionData) { + this.transactionData = transactionData; + } - public TransactionBytes getDataHash() { - return dataHash; - } + public TransactionBytes getDataHash() { + return dataHash; + } - public void setDataHash(TransactionBytes dataHash) { - this.dataHash = dataHash; - } + public void setDataHash(TransactionBytes dataHash) { + this.dataHash = dataHash; + } - public TransactionBytes getSignature() { - return signature; - } + public TransactionBytes getSignature() { + return signature; + } - public void setSignature(TransactionBytes signature) { - this.signature = signature; - } + public void setSignature(TransactionBytes signature) { + this.signature = signature; + } - public TransactionBytes getSender() { - return sender; - } + public TransactionBytes getSender() { + return sender; + } - public void setSender(TransactionBytes sender) { - this.sender = sender; - } + public void setSender(TransactionBytes sender) { + this.sender = sender; + } - public long getImportTime() { - return importTime; - } + public long getImportTime() { + return importTime; + } - public void setImportTime(long importTime) { - this.importTime = importTime; - } + public void setImportTime(long importTime) { + this.importTime = importTime; + } - public int getAttribute() { - return attribute; - } + public int getAttribute() { + return attribute; + } - public void setAttribute(int attribute) { - this.attribute = attribute; - } + public void setAttribute(int attribute) { + this.attribute = attribute; + } - public String getExtraData() { - return extraData; - } + public String getExtraData() { + return extraData; + } - public void setExtraData(String extraData) { - this.extraData = extraData; - } + public void setExtraData(String extraData) { + this.extraData = extraData; + } } diff --git a/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV2.java b/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV2.java index 6e7f0a9a6..4f304dede 100644 --- a/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV2.java +++ b/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV2.java @@ -1,5 +1,7 @@ package org.fisco.bcos.sdk.jni.test.tx; +import java.security.SecureRandom; +import java.util.Arrays; import org.fisco.bcos.sdk.jni.BcosSDKJniObj; import org.fisco.bcos.sdk.jni.common.JniConfig; import org.fisco.bcos.sdk.jni.common.JniException; @@ -11,29 +13,28 @@ import org.fisco.bcos.sdk.jni.utilities.tx.*; import org.junit.Assert; -import java.security.SecureRandom; -import java.util.Arrays; - public class TestTxStructV2 { - // ------------------------------------------------------------------------------ - // ------------------------------------------------------------------------------ - - // HelloWorld Source Code: - /** - * pragma solidity>=0.4.24 <0.6.11; - * - *

contract HelloWorld { string name; - * - *

constructor() public { name = "Hello, World!"; } - * - *

function get() public view returns (string memory) { return name; } - * - *

function set(string memory n) public { name = n; } } - */ - private static final String hwBIN = "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610235565b5050565b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561022b5780601f106102005761010080835404028352916020019161022b565b820191906000526020600020905b81548152906001019060200180831161020e57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea2646970667358221220b5943f43c48cc93c6d71cdcf27aee5072566c88755ce9186e32ce83b24e8dc6c64736f6c634300060a0033"; - - private static final String hwSmBIN = "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063299f7f9d1461003b5780633590b49f146100be575b600080fd5b610043610179565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610083578082015181840152602081019050610068565b50505050905090810190601f1680156100b05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610177600480360360208110156100d457600080fd5b81019080803590602001906401000000008111156100f157600080fd5b82018360208201111561010357600080fd5b8035906020019184600183028401116401000000008311171561012557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061021b565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102115780601f106101e657610100808354040283529160200191610211565b820191906000526020600020905b8154815290600101906020018083116101f457829003601f168201915b5050505050905090565b8060009080519060200190610231929190610235565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea26469706673582212209871cb2bcf390d53645807cbaedfe052d739ef9cff9d84787f74c4f379e1854664736f6c634300060a0033"; + // ------------------------------------------------------------------------------ + // ------------------------------------------------------------------------------ + + // HelloWorld Source Code: + /** + * pragma solidity>=0.4.24 <0.6.11; + * + *

contract HelloWorld { string name; + * + *

constructor() public { name = "Hello, World!"; } + * + *

function get() public view returns (string memory) { return name; } + * + *

function set(string memory n) public { name = n; } } + */ + private static final String hwBIN = + "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610235565b5050565b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561022b5780601f106102005761010080835404028352916020019161022b565b820191906000526020600020905b81548152906001019060200180831161020e57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea2646970667358221220b5943f43c48cc93c6d71cdcf27aee5072566c88755ce9186e32ce83b24e8dc6c64736f6c634300060a0033"; + + private static final String hwSmBIN = + "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063299f7f9d1461003b5780633590b49f146100be575b600080fd5b610043610179565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610083578082015181840152602081019050610068565b50505050905090810190601f1680156100b05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610177600480360360208110156100d457600080fd5b81019080803590602001906401000000008111156100f157600080fd5b82018360208201111561010357600080fd5b8035906020019184600183028401116401000000008311171561012557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061021b565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102115780601f106101e657610100808354040283529160200191610211565b820191906000526020600020905b8154815290600101906020018083116101f457829003601f168201915b5050505050905090565b8060009080519060200190610231929190610235565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea26469706673582212209871cb2bcf390d53645807cbaedfe052d739ef9cff9d84787f74c4f379e1854664736f6c634300060a0033"; /* { @@ -47,193 +48,257 @@ public class TestTxStructV2 { } */ - public static byte[] fromHex(String hexString) { - if (hexString.startsWith("0x")) { - hexString = hexString.substring(2); - } - - if (hexString.length() % 2 != 0) { - throw new IllegalArgumentException("Invalid hex string: " + hexString); - } + public static byte[] fromHex(String hexString) { + if (hexString.startsWith("0x")) { + hexString = hexString.substring(2); + } - int length = hexString.length() / 2; - byte[] bytes = new byte[length]; + if (hexString.length() % 2 != 0) { + throw new IllegalArgumentException("Invalid hex string: " + hexString); + } - for (int i = 0; i < length; i++) { - String twoChars = hexString.substring(i * 2, i * 2 + 2); - bytes[i] = (byte) Integer.parseInt(twoChars, 16); - } + int length = hexString.length() / 2; + byte[] bytes = new byte[length]; - return bytes; + for (int i = 0; i < length; i++) { + String twoChars = hexString.substring(i * 2, i * 2 + 2); + bytes[i] = (byte) Integer.parseInt(twoChars, 16); } - public static String generateNonce() { - byte[] nonceBytes = new byte[16]; - SecureRandom secureRandom = new SecureRandom(); - secureRandom.nextBytes(nonceBytes); - StringBuilder hex = new StringBuilder(); - for (byte b : nonceBytes) { - hex.append(String.format("%02x", b)); - } - return hex.toString(); - } + return bytes; + } - public static String getBinary(boolean isSM) { - return isSM ? hwSmBIN : hwBIN; + public static String generateNonce() { + byte[] nonceBytes = new byte[16]; + SecureRandom secureRandom = new SecureRandom(); + secureRandom.nextBytes(nonceBytes); + StringBuilder hex = new StringBuilder(); + for (byte b : nonceBytes) { + hex.append(String.format("%02x", b)); } + return hex.toString(); + } - public static void Usage() { - System.out.println("Desc: test transaction struct V2 [HelloWorld set]"); - System.out.println("Usage: java -cp 'conf/:lib/*:apps/*' org.fisco.bcos.sdk.jni.test.tx.TestTxStructV2"); - System.exit(0); - } + public static String getBinary(boolean isSM) { + return isSM ? hwSmBIN : hwBIN; + } + + public static void Usage() { + System.out.println("Desc: test transaction struct V2 [HelloWorld set]"); + System.out.println( + "Usage: java -cp 'conf/:lib/*:apps/*' org.fisco.bcos.sdk.jni.test.tx.TestTxStructV2"); + System.exit(0); + } - public static void main(String[] args) throws JniException, InterruptedException { - try { - if (args.length > 1) { - Usage(); + public static void main(String[] args) throws JniException, InterruptedException { + try { + if (args.length > 1) { + Usage(); + } + + String endpoint = "127.0.0.1:20200"; + String group = "group0"; + String node = ""; + JniConfig jniConfig = Utility.newJniConfig(Arrays.asList(endpoint)); + jniConfig.setDisableSsl(true); + BcosSDKJniObj bcosSDKJni = BcosSDKJniObj.build(jniConfig); + RpcJniObj rpcJniObj = RpcJniObj.build(bcosSDKJni.getNativePointer()); + System.out.println("build Rpc"); + rpcJniObj.start(); + + boolean smCrypto = false; + + long keyPair = KeyPairJniObj.createJniKeyPair(smCrypto ? 1 : 0); + String jniKeyPairAddress = KeyPairJniObj.getJniKeyPairAddress(keyPair); + System.out.printf(" [test Tx Struct V2] new account, address: %s\n", jniKeyPairAddress); + + String groupID = "group0"; + String chainID = "chain0"; + String data = getBinary(smCrypto); + long blockLimit = 1; + String value = "11"; + String gasPrice = "0"; + long gasLimit = 0; + String maxFeePerGas = "11"; + String maxPriorityFeePerGas = "22"; + + // construct TransactionDataV2 + TransactionDataV2 transactionDataStructV2 = new TransactionDataV2(); + transactionDataStructV2.setGroupId(groupID); + transactionDataStructV2.setChainId(chainID); + transactionDataStructV2.setTo(""); + transactionDataStructV2.setAbi(""); + transactionDataStructV2.setVersion(1); + transactionDataStructV2.setNonce(generateNonce()); + transactionDataStructV2.setBlockLimit(blockLimit); + transactionDataStructV2.setValue(value); + transactionDataStructV2.setGasPrice(gasPrice); + transactionDataStructV2.setGasLimit(gasLimit); + transactionDataStructV2.setMaxFeePerGas(maxFeePerGas); + transactionDataStructV2.setMaxPriorityFeePerGas(maxPriorityFeePerGas); + // input + TransactionBytes inputBytes = new TransactionBytes(); + byte[] bytesInput = fromHex(data); + inputBytes.setBuffer(bytesInput); + inputBytes.setLength(bytesInput.length); + transactionDataStructV2.setInput(inputBytes); + + // encode TxData to hex tx data + String txDataHex = + TransactionStructBuilderV2JniObj.encodeTransactionDataStructV2(transactionDataStructV2); + // decode hex tx data to TxData + TransactionDataV2 decodeTransactionDataStructHex = + TransactionStructBuilderV2JniObj.decodeTransactionDataStructV2(txDataHex); + // TransactionDataV2 decodeTransactionDataStructHex2 = + // TransactionStructBuilderV2JniObj.decodeTransactionDataStructV2(null); + // TransactionDataV2 decodeTransactionDataStructHex3 = + // TransactionStructBuilderV2JniObj.decodeTransactionDataStructV2(""); + + // assert + Assert.assertEquals( + transactionDataStructV2.getChainId(), decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals( + transactionDataStructV2.getGroupId(), decodeTransactionDataStructHex.getGroupId()); + Assert.assertEquals( + transactionDataStructV2.getAbi(), decodeTransactionDataStructHex.getAbi()); + Assert.assertEquals( + transactionDataStructV2.getBlockLimit(), decodeTransactionDataStructHex.getBlockLimit()); + Assert.assertEquals( + transactionDataStructV2.getValue(), decodeTransactionDataStructHex.getValue()); + Assert.assertEquals( + transactionDataStructV2.getGasPrice(), decodeTransactionDataStructHex.getGasPrice()); + Assert.assertEquals( + transactionDataStructV2.getGasLimit(), decodeTransactionDataStructHex.getGasLimit()); + Assert.assertEquals( + transactionDataStructV2.getMaxFeePerGas(), + decodeTransactionDataStructHex.getMaxFeePerGas()); + Assert.assertEquals( + transactionDataStructV2.getMaxPriorityFeePerGas(), + decodeTransactionDataStructHex.getMaxPriorityFeePerGas()); + + // encode TxData to json tx data + String txDataJson = + TransactionStructBuilderV2JniObj.encodeTransactionDataStructToJsonV2( + transactionDataStructV2); + System.out.printf(" [test Tx Struct V2] txDataJson: %s\n", txDataJson); + + // calc tx data hash + String txDataHash = + TransactionStructBuilderV2JniObj.calcTransactionDataStructHashV2( + smCrypto ? 1 : 0, decodeTransactionDataStructHex); + System.out.printf(" [test Tx Struct V2] txDataHash: %s\n", txDataHash); + // signature tx data hash + String signature = TransactionBuilderJniObj.signTransactionDataHash(keyPair, txDataHash); + System.out.printf(" [test Tx Struct V2] signature: %s\n", signature); + + // construct tx + TransactionV2 transactionStructV2 = new TransactionV2(); + TransactionBytes dataHashBytes = new TransactionBytes(); + dataHashBytes.setBuffer(txDataHash.getBytes()); + dataHashBytes.setLength(txDataHash.getBytes().length); + TransactionBytes signatureBytes = new TransactionBytes(); + signatureBytes.setBuffer(signature.getBytes()); + signatureBytes.setLength(signature.getBytes().length); + transactionStructV2.setTransactionData(decodeTransactionDataStructHex); + transactionStructV2.setDataHash(dataHashBytes); + transactionStructV2.setSignature(signatureBytes); + transactionStructV2.setSender(null); + transactionStructV2.setImportTime(0); + transactionStructV2.setAttribute(0); + transactionStructV2.setExtraData(""); + // assert + Assert.assertEquals( + transactionStructV2.getTransactionData().getBlockLimit(), + decodeTransactionDataStructHex.getBlockLimit()); + Assert.assertEquals( + transactionStructV2.getTransactionData().getGroupId(), + decodeTransactionDataStructHex.getGroupId()); + Assert.assertEquals( + transactionStructV2.getTransactionData().getChainId(), + decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals( + transactionStructV2.getTransactionData().getChainId(), + decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals( + transactionStructV2.getTransactionData().getAbi(), + decodeTransactionDataStructHex.getAbi()); + Assert.assertArrayEquals( + transactionStructV2.getDataHash().getBuffer(), txDataHash.getBytes()); + Assert.assertArrayEquals( + transactionStructV2.getSignature().getBuffer(), signature.getBytes()); + + // encode Tx to hex tx + String txHex = + TransactionStructBuilderV2JniObj.encodeTransactionStructV2(transactionStructV2); + // decode hex tx to Tx + TransactionV2 decodeTransactionStructHex = + TransactionStructBuilderV2JniObj.decodeTransactionStructV2(txHex); + + // assert + Assert.assertEquals( + transactionStructV2.getTransactionData().getBlockLimit(), + decodeTransactionStructHex.getTransactionData().getBlockLimit()); + Assert.assertEquals( + transactionStructV2.getTransactionData().getGroupId(), + decodeTransactionStructHex.getTransactionData().getGroupId()); + Assert.assertEquals( + transactionStructV2.getTransactionData().getChainId(), + decodeTransactionStructHex.getTransactionData().getChainId()); + Assert.assertEquals( + transactionStructV2.getTransactionData().getAbi(), + decodeTransactionStructHex.getTransactionData().getAbi()); + Assert.assertEquals( + transactionStructV2.getTransactionData().getValue(), + decodeTransactionStructHex.getTransactionData().getValue()); + Assert.assertEquals( + transactionStructV2.getTransactionData().getGasLimit(), + decodeTransactionStructHex.getTransactionData().getGasLimit()); + Assert.assertEquals( + transactionStructV2.getTransactionData().getGasPrice(), + decodeTransactionStructHex.getTransactionData().getGasPrice()); + Assert.assertEquals( + transactionStructV2.getTransactionData().getMaxFeePerGas(), + decodeTransactionStructHex.getTransactionData().getMaxFeePerGas()); + Assert.assertEquals( + transactionStructV2.getTransactionData().getMaxPriorityFeePerGas(), + decodeTransactionStructHex.getTransactionData().getMaxPriorityFeePerGas()); + Assert.assertArrayEquals( + transactionStructV2.getDataHash().getBuffer(), + decodeTransactionStructHex.getDataHash().getBuffer()); + Assert.assertArrayEquals( + transactionStructV2.getSignature().getBuffer(), + decodeTransactionStructHex.getSignature().getBuffer()); + + // encode Tx to json tx + String txJson = + TransactionStructBuilderV2JniObj.encodeTransactionStructToJsonV2(transactionStructV2); + System.out.printf(" [test Tx Struct V2] txJson: %s\n", txJson); + // create tx string + String txString = + TransactionStructBuilderV2JniObj.createEncodedTransactionV2( + decodeTransactionDataStructHex, signature, txDataHash, 0, ""); + // System.out.printf(" [test Tx Struct V2] txString: %s\n", txString); + + // rpc send tx + rpcJniObj.sendTransaction( + group, + node, + txString, + false, + new RpcCallback() { + @Override + public void onResponse(Response response) { + System.out.println("response error code: ==>>> " + response.getErrorCode()); + String dataStr = new String(response.getData()); + System.out.println("response data: ==>>> " + dataStr); } + }); - String endpoint = "127.0.0.1:20200"; - String group = "group0"; - String node = ""; - JniConfig jniConfig = Utility.newJniConfig(Arrays.asList(endpoint)); - jniConfig.setDisableSsl(true); - BcosSDKJniObj bcosSDKJni = BcosSDKJniObj.build(jniConfig); - RpcJniObj rpcJniObj = RpcJniObj.build(bcosSDKJni.getNativePointer()); - System.out.println("build Rpc"); - rpcJniObj.start(); - - boolean smCrypto = false; - - long keyPair = KeyPairJniObj.createJniKeyPair(smCrypto ? 1 : 0); - String jniKeyPairAddress = KeyPairJniObj.getJniKeyPairAddress(keyPair); - System.out.printf(" [test Tx Struct V2] new account, address: %s\n", jniKeyPairAddress); - - String groupID = "group0"; - String chainID = "chain0"; - String data = getBinary(smCrypto); - long blockLimit = 1; - String value = "11"; - String gasPrice = "0"; - long gasLimit = 0; - String maxFeePerGas = "11"; - String maxPriorityFeePerGas = "22"; - - // construct TransactionDataV2 - TransactionDataV2 transactionDataStructV2 = new TransactionDataV2(); - transactionDataStructV2.setGroupId(groupID); - transactionDataStructV2.setChainId(chainID); - transactionDataStructV2.setTo(""); - transactionDataStructV2.setAbi(""); - transactionDataStructV2.setVersion(1); - transactionDataStructV2.setNonce(generateNonce()); - transactionDataStructV2.setBlockLimit(blockLimit); - transactionDataStructV2.setValue(value); - transactionDataStructV2.setGasPrice(gasPrice); - transactionDataStructV2.setGasLimit(gasLimit); - transactionDataStructV2.setMaxFeePerGas(maxFeePerGas); - transactionDataStructV2.setMaxPriorityFeePerGas(maxPriorityFeePerGas); - // input - TransactionBytes inputBytes = new TransactionBytes(); - byte[] bytesInput = fromHex(data); - inputBytes.setBuffer(bytesInput); - inputBytes.setLength(bytesInput.length); - transactionDataStructV2.setInput(inputBytes); - - // encode TxData to hex tx data - String txDataHex = TransactionStructBuilderV2JniObj.encodeTransactionDataStructV2(transactionDataStructV2); - // decode hex tx data to TxData - TransactionDataV2 decodeTransactionDataStructHex = TransactionStructBuilderV2JniObj.decodeTransactionDataStructV2(txDataHex); -// TransactionDataV2 decodeTransactionDataStructHex2 = TransactionStructBuilderV2JniObj.decodeTransactionDataStructV2(null); -// TransactionDataV2 decodeTransactionDataStructHex3 = TransactionStructBuilderV2JniObj.decodeTransactionDataStructV2(""); - - // assert - Assert.assertEquals(transactionDataStructV2.getChainId(), decodeTransactionDataStructHex.getChainId()); - Assert.assertEquals(transactionDataStructV2.getGroupId(), decodeTransactionDataStructHex.getGroupId()); - Assert.assertEquals(transactionDataStructV2.getAbi(), decodeTransactionDataStructHex.getAbi()); - Assert.assertEquals(transactionDataStructV2.getBlockLimit(), decodeTransactionDataStructHex.getBlockLimit()); - Assert.assertEquals(transactionDataStructV2.getValue(), decodeTransactionDataStructHex.getValue()); - Assert.assertEquals(transactionDataStructV2.getGasPrice(), decodeTransactionDataStructHex.getGasPrice()); - Assert.assertEquals(transactionDataStructV2.getGasLimit(), decodeTransactionDataStructHex.getGasLimit()); - Assert.assertEquals(transactionDataStructV2.getMaxFeePerGas(), decodeTransactionDataStructHex.getMaxFeePerGas()); - Assert.assertEquals(transactionDataStructV2.getMaxPriorityFeePerGas(), decodeTransactionDataStructHex.getMaxPriorityFeePerGas()); - - // encode TxData to json tx data - String txDataJson = TransactionStructBuilderV2JniObj.encodeTransactionDataStructToJsonV2(transactionDataStructV2); - System.out.printf(" [test Tx Struct V2] txDataJson: %s\n", txDataJson); - - // calc tx data hash - String txDataHash = TransactionStructBuilderV2JniObj.calcTransactionDataStructHashV2(smCrypto ? 1 : 0, decodeTransactionDataStructHex); - System.out.printf(" [test Tx Struct V2] txDataHash: %s\n", txDataHash); - // signature tx data hash - String signature = TransactionBuilderJniObj.signTransactionDataHash(keyPair, txDataHash); - System.out.printf(" [test Tx Struct V2] signature: %s\n", signature); - - // construct tx - TransactionV2 transactionStructV2 = new TransactionV2(); - TransactionBytes dataHashBytes = new TransactionBytes(); - dataHashBytes.setBuffer(txDataHash.getBytes()); - dataHashBytes.setLength(txDataHash.getBytes().length); - TransactionBytes signatureBytes = new TransactionBytes(); - signatureBytes.setBuffer(signature.getBytes()); - signatureBytes.setLength(signature.getBytes().length); - transactionStructV2.setTransactionData(decodeTransactionDataStructHex); - transactionStructV2.setDataHash(dataHashBytes); - transactionStructV2.setSignature(signatureBytes); - transactionStructV2.setSender(null); - transactionStructV2.setImportTime(0); - transactionStructV2.setAttribute(0); - transactionStructV2.setExtraData(""); - // assert - Assert.assertEquals(transactionStructV2.getTransactionData().getBlockLimit(), decodeTransactionDataStructHex.getBlockLimit()); - Assert.assertEquals(transactionStructV2.getTransactionData().getGroupId(), decodeTransactionDataStructHex.getGroupId()); - Assert.assertEquals(transactionStructV2.getTransactionData().getChainId(), decodeTransactionDataStructHex.getChainId()); - Assert.assertEquals(transactionStructV2.getTransactionData().getChainId(), decodeTransactionDataStructHex.getChainId()); - Assert.assertEquals(transactionStructV2.getTransactionData().getAbi(), decodeTransactionDataStructHex.getAbi()); - Assert.assertArrayEquals(transactionStructV2.getDataHash().getBuffer(), txDataHash.getBytes()); - Assert.assertArrayEquals(transactionStructV2.getSignature().getBuffer(), signature.getBytes()); - - // encode Tx to hex tx - String txHex = TransactionStructBuilderV2JniObj.encodeTransactionStructV2(transactionStructV2); - // decode hex tx to Tx - TransactionV2 decodeTransactionStructHex = TransactionStructBuilderV2JniObj.decodeTransactionStructV2(txHex); - - // assert - Assert.assertEquals(transactionStructV2.getTransactionData().getBlockLimit(), decodeTransactionStructHex.getTransactionData().getBlockLimit()); - Assert.assertEquals(transactionStructV2.getTransactionData().getGroupId(), decodeTransactionStructHex.getTransactionData().getGroupId()); - Assert.assertEquals(transactionStructV2.getTransactionData().getChainId(), decodeTransactionStructHex.getTransactionData().getChainId()); - Assert.assertEquals(transactionStructV2.getTransactionData().getAbi(), decodeTransactionStructHex.getTransactionData().getAbi()); - Assert.assertEquals(transactionStructV2.getTransactionData().getValue(), decodeTransactionStructHex.getTransactionData().getValue()); - Assert.assertEquals(transactionStructV2.getTransactionData().getGasLimit(), decodeTransactionStructHex.getTransactionData().getGasLimit()); - Assert.assertEquals(transactionStructV2.getTransactionData().getGasPrice(), decodeTransactionStructHex.getTransactionData().getGasPrice()); - Assert.assertEquals(transactionStructV2.getTransactionData().getMaxFeePerGas(), decodeTransactionStructHex.getTransactionData().getMaxFeePerGas()); - Assert.assertEquals(transactionStructV2.getTransactionData().getMaxPriorityFeePerGas(), decodeTransactionStructHex.getTransactionData().getMaxPriorityFeePerGas()); - Assert.assertArrayEquals(transactionStructV2.getDataHash().getBuffer(), decodeTransactionStructHex.getDataHash().getBuffer()); - Assert.assertArrayEquals(transactionStructV2.getSignature().getBuffer(), decodeTransactionStructHex.getSignature().getBuffer()); - - // encode Tx to json tx - String txJson = TransactionStructBuilderV2JniObj.encodeTransactionStructToJsonV2(transactionStructV2); - System.out.printf(" [test Tx Struct V2] txJson: %s\n", txJson); - // create tx string - String txString = TransactionStructBuilderV2JniObj.createEncodedTransactionV2(decodeTransactionDataStructHex, signature, txDataHash, 0, ""); - // System.out.printf(" [test Tx Struct V2] txString: %s\n", txString); - - // rpc send tx - rpcJniObj.sendTransaction(group, node, txString, false, new RpcCallback() { - @Override - public void onResponse(Response response) { - System.out.println("response error code: ==>>> " + response.getErrorCode()); - String dataStr = new String(response.getData()); - System.out.println("response data: ==>>> " + dataStr); - } - }); - - Thread.sleep(2000); - rpcJniObj.stop(); - System.out.printf(" [test Tx Struct V2] finish !! \n"); - } catch (JniException e) { - System.out.println("test tx struct V2 failed, error: " + e.getMessage()); - } + Thread.sleep(2000); + rpcJniObj.stop(); + System.out.printf(" [test Tx Struct V2] finish !! \n"); + } catch (JniException e) { + System.out.println("test tx struct V2 failed, error: " + e.getMessage()); } + } } From d2bbf0301698843cc355f29d943a89ad631c5438 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Fri, 12 Jan 2024 10:34:03 +0800 Subject: [PATCH 23/29] (vcpkg): update vcpkg registry for tbb task group wait. (#207) --- .gitignore | 2 ++ vcpkg-configuration.json | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 5a642fc72..a7c063980 100644 --- a/.gitignore +++ b/.gitignore @@ -43,3 +43,5 @@ deps .idea/ .DS_Store vcpkg_installed** + +conf** diff --git a/vcpkg-configuration.json b/vcpkg-configuration.json index 6e8373d25..f99945394 100644 --- a/vcpkg-configuration.json +++ b/vcpkg-configuration.json @@ -3,7 +3,7 @@ { "kind": "git", "repository": "https://github.com/FISCO-BCOS/registry", - "baseline": "61736e4c9b7926d3b11b22df0fd2946c5331b291", + "baseline": "96d9ae6815e68fce43ec0e3d69a6d9f959a0b798", "packages": [ "openssl", "hsm-crypto", From 04ba0eda51a9f539bbc85ce6c976e7c0a0f712f1 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Wed, 24 Jan 2024 09:40:12 +0800 Subject: [PATCH 24/29] (sdk,vcpkg): update vcpkg, fix sdk destroy log. (#209) --- CMakeLists.txt | 1 + bcos-c-sdk/bcos_sdk_c.cpp | 20 +++++++++----------- vcpkg-configuration.json | 2 +- vcpkg.json | 6 ++++++ 4 files changed, 17 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5ec2820e3..1f5015ba5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -87,6 +87,7 @@ find_package(unofficial-secp256k1 CONFIG REQUIRED) find_package(jsoncpp CONFIG REQUIRED) find_package(wedprcrypto REQUIRED) find_package(HSM-CRYPTO REQUIRED) +find_package(ZLIB REQUIRED) find_package(fiscobcos REQUIRED) file(GLOB_RECURSE SOURCES "bcos-c-sdk/*.h" "bcos-c-sdk/*.cpp" "bcos-c-sdk/*.c") diff --git a/bcos-c-sdk/bcos_sdk_c.cpp b/bcos-c-sdk/bcos_sdk_c.cpp index 5c815e3ce..b9f8584a8 100644 --- a/bcos-c-sdk/bcos_sdk_c.cpp +++ b/bcos-c-sdk/bcos_sdk_c.cpp @@ -208,6 +208,7 @@ void bcos_sdk_start(void* sdk) try { + BCOS_LOG(INFO) << LOG_BADGE("bcos_sdk_start") << LOG_KV("sdk", sdk); ((bcos::cppsdk::Sdk*)sdk)->start(); } catch (const std::exception& e) @@ -217,8 +218,6 @@ void bcos_sdk_start(void* sdk) << LOG_KV("errorMsg", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); } - - BCOS_LOG(INFO) << LOG_BADGE("bcos_sdk_start") << LOG_KV("sdk", sdk); } /** @@ -231,10 +230,9 @@ void bcos_sdk_stop(void* sdk) bcos_sdk_clear_last_error(); if (sdk) { + BCOS_LOG(INFO) << LOG_BADGE("bcos_sdk_stop") << LOG_KV("sdk", sdk); ((bcos::cppsdk::Sdk*)sdk)->stop(); } - - BCOS_LOG(INFO) << LOG_BADGE("bcos_sdk_stop") << LOG_KV("sdk", sdk); } /** @@ -247,11 +245,10 @@ void bcos_sdk_destroy(void* sdk) bcos_sdk_clear_last_error(); if (sdk) { + BCOS_LOG(INFO) << LOG_BADGE("bcos_sdk_destroy") << LOG_KV("sdk", sdk); ((bcos::cppsdk::Sdk*)sdk)->stop(); delete (bcos::cppsdk::Sdk*)sdk; } - - BCOS_LOG(INFO) << LOG_BADGE("bcos_sdk_destroy") << LOG_KV("sdk", sdk); } /** @@ -267,14 +264,13 @@ void bcos_sdk_register_block_notifier(void* sdk, const char* group, void* contex BCOS_SDK_C_PARAMS_VERIFICATION(group, ); BCOS_SDK_C_PARAMS_VERIFICATION(callback, ); + BCOS_LOG(INFO) << LOG_BADGE("bcos_sdk_register_block_notifier") << LOG_KV("sdk", sdk) + << LOG_KV("group", group); auto service = ((bcos::cppsdk::Sdk*)sdk)->service(); service->registerBlockNumberNotifier( group, [context, callback](const std::string& _group, int64_t _blockNumber) { callback(_group.c_str(), _blockNumber, context); }); - - BCOS_LOG(INFO) << LOG_BADGE("bcos_sdk_register_block_notifier") << LOG_KV("sdk", sdk) - << LOG_KV("group", group); } /** @@ -320,14 +316,16 @@ const char* bcos_sdk_get_group_chain_id(void* sdk, const char* group) return strdup(chainID.c_str()); } -uint32_t bcos_sdk_get_local_protocol_info(void* sdk) { +uint32_t bcos_sdk_get_local_protocol_info(void* sdk) +{ bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(sdk, 0); return ((bcos::cppsdk::Sdk*)sdk)->localProtocolInfo(); } -uint32_t bcos_sdk_get_negotiated_protocol_info(void* sdk) { +uint32_t bcos_sdk_get_negotiated_protocol_info(void* sdk) +{ bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(sdk, 0); diff --git a/vcpkg-configuration.json b/vcpkg-configuration.json index f99945394..591959347 100644 --- a/vcpkg-configuration.json +++ b/vcpkg-configuration.json @@ -3,7 +3,7 @@ { "kind": "git", "repository": "https://github.com/FISCO-BCOS/registry", - "baseline": "96d9ae6815e68fce43ec0e3d69a6d9f959a0b798", + "baseline": "f8fde49506ea47504783f339aeeca6e269bbdb12", "packages": [ "openssl", "hsm-crypto", diff --git a/vcpkg.json b/vcpkg.json index 967d55446..86fe5efe4 100644 --- a/vcpkg.json +++ b/vcpkg.json @@ -32,6 +32,7 @@ "openssl", "secp256k1", "wedprcrypto", + "zlib", "fisco-bcos-cpp-sdk" ], "builtin-baseline": "51b14cd4e1230dd51c11ffeff6f7d53c61cc5297", @@ -103,6 +104,11 @@ { "name": "jsoncpp", "version": "1.9.5" + }, + { + "name": "zlib", + "version": "1.2.12", + "port-version": 1 } ] } \ No newline at end of file From 369584551eb095b4c1603f1397682d43c272a3c9 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Tue, 30 Jan 2024 11:43:30 +0800 Subject: [PATCH 25/29] (transaction): fix transaction struct coredump bug, use inherit TransactionData and builder interfaces. (#211) --- bcos-c-sdk/bcos_sdk_c_common.cpp | 1 + bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp | 42 +- bindings/java/jni/scripts/java2jni.sh | 2 +- .../org_fisco_bcos_sdk_jni_rpc_RpcJniObj.cpp | 2 +- ...ties_tx_TransactionStructBuilderJniObj.cpp | 1335 ++++++++++++----- ...lities_tx_TransactionStructBuilderJniObj.h | 18 + .../sdk/jni/utilities/tx/Transaction.java | 18 +- .../jni/utilities/tx/TransactionBytes.java | 22 - .../sdk/jni/utilities/tx/TransactionData.java | 20 +- .../jni/utilities/tx/TransactionDataV2.java | 84 +- .../tx/TransactionStructBuilderJniObj.java | 14 + .../tx/TransactionStructBuilderV2JniObj.java | 77 - .../sdk/jni/utilities/tx/TransactionV2.java | 67 - .../jni/utilities/tx/TransactionVersion.java | 29 +- .../bcos/sdk/jni/test/tx/TestTxStruct.java | 407 +++-- .../bcos/sdk/jni/test/tx/TestTxStructV2.java | 511 +++---- 16 files changed, 1512 insertions(+), 1137 deletions(-) delete mode 100644 bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes.java delete mode 100644 bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderV2JniObj.java delete mode 100644 bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionV2.java diff --git a/bcos-c-sdk/bcos_sdk_c_common.cpp b/bcos-c-sdk/bcos_sdk_c_common.cpp index fd7821923..2cb7d34b7 100644 --- a/bcos-c-sdk/bcos_sdk_c_common.cpp +++ b/bcos-c-sdk/bcos_sdk_c_common.cpp @@ -228,6 +228,7 @@ void bcos_sdk_c_handle_response( if (errorPtr && errorPtr->errorCode() != 0) { resp->error = errorPtr->errorCode(); + // not copy here because cpp sdk will release the errorPtr resp->desc = (char*)errorPtr->errorMessage().c_str(); resp->data = NULL; resp->size = 0; diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp index 95233ec4a..5d34da6b5 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp @@ -643,32 +643,32 @@ void bcos_sdk_destroy_transaction_data_struct(struct bcos_sdk_c_transaction_data return; } - if (transaction_data && transaction_data->chain_id) + if (transaction_data->chain_id) { bcos_sdk_c_free(transaction_data->chain_id); } - if (transaction_data && transaction_data->group_id) + if (transaction_data->group_id) { bcos_sdk_c_free(transaction_data->group_id); } - if (transaction_data && transaction_data->nonce) + if (transaction_data->nonce) { bcos_sdk_c_free(transaction_data->nonce); } - if (transaction_data && transaction_data->to) + if (transaction_data->to) { bcos_sdk_c_free(transaction_data->to); } - if (transaction_data && transaction_data->abi) + if (transaction_data->abi) { bcos_sdk_c_free((void*)transaction_data->abi); } - if (transaction_data && transaction_data->input) + if (transaction_data->input) { if (transaction_data->input->buffer) { @@ -974,12 +974,12 @@ void bcos_sdk_destroy_transaction_struct(struct bcos_sdk_c_transaction* transact return; } - if (transaction && transaction->transaction_data) + if (transaction->transaction_data) { bcos_sdk_destroy_transaction_data_struct(transaction->transaction_data); } - if (transaction && transaction->data_hash) + if (transaction->data_hash) { if (transaction->data_hash->buffer) { @@ -988,7 +988,7 @@ void bcos_sdk_destroy_transaction_struct(struct bcos_sdk_c_transaction* transact bcos_sdk_c_free(transaction->data_hash); } - if (transaction && transaction->signature) + if (transaction->signature) { if (transaction->signature->buffer) { @@ -997,7 +997,7 @@ void bcos_sdk_destroy_transaction_struct(struct bcos_sdk_c_transaction* transact bcos_sdk_c_free(transaction->signature); } - if (transaction && transaction->sender) + if (transaction->sender) { if (transaction->sender->buffer) { @@ -1006,7 +1006,7 @@ void bcos_sdk_destroy_transaction_struct(struct bcos_sdk_c_transaction* transact bcos_sdk_c_free(transaction->sender); } - if (transaction && transaction->extra_data) + if (transaction->extra_data) { bcos_sdk_c_free(transaction->extra_data); } @@ -1322,32 +1322,32 @@ void bcos_sdk_destroy_transaction_data_struct_v2( return; } - if (transaction_data && transaction_data->chain_id) + if (transaction_data->chain_id) { bcos_sdk_c_free(transaction_data->chain_id); } - if (transaction_data && transaction_data->group_id) + if (transaction_data->group_id) { bcos_sdk_c_free(transaction_data->group_id); } - if (transaction_data && transaction_data->nonce) + if (transaction_data->nonce) { bcos_sdk_c_free(transaction_data->nonce); } - if (transaction_data && transaction_data->to) + if (transaction_data->to) { bcos_sdk_c_free(transaction_data->to); } - if (transaction_data && transaction_data->abi) + if (transaction_data->abi) { bcos_sdk_c_free((void*)transaction_data->abi); } - if (transaction_data && transaction_data->input) + if (transaction_data->input) { if (transaction_data->input->buffer) { @@ -1356,22 +1356,22 @@ void bcos_sdk_destroy_transaction_data_struct_v2( bcos_sdk_c_free((void*)transaction_data->input); } - if (transaction_data && transaction_data->value) + if (transaction_data->value) { bcos_sdk_c_free(transaction_data->value); } - if (transaction_data && transaction_data->gas_price) + if (transaction_data->gas_price) { bcos_sdk_c_free(transaction_data->gas_price); } - if (transaction_data && transaction_data->max_fee_per_gas) + if (transaction_data->max_fee_per_gas) { bcos_sdk_c_free(transaction_data->max_fee_per_gas); } - if (transaction_data && transaction_data->max_priority_fee_per_gas) + if (transaction_data->max_priority_fee_per_gas) { bcos_sdk_c_free(transaction_data->max_priority_fee_per_gas); } diff --git a/bindings/java/jni/scripts/java2jni.sh b/bindings/java/jni/scripts/java2jni.sh index fd00b93da..35c0f91ec 100644 --- a/bindings/java/jni/scripts/java2jni.sh +++ b/bindings/java/jni/scripts/java2jni.sh @@ -30,7 +30,7 @@ function convert_java_to_jni() { # convert_java_to_jni ${JAVA_SOURCE_PATH} # classes="rpc.RpcJniObj amop.AmopJniObj event.EventSubJniObj BcosSDKJniObj" -classes="BcosSDKJniObj utilities.keypair.KeyPairJniObj utilities.tx.TransactionBuilderJniObj utilities.tx.TransactionBuilderV2JniObj utilities.tx.TransactionStructBuilderJniObj utilities.tx.TransactionStructBuilderV2JniObj utilities.receipt.ReceiptBuilderJniObj utilities.signature.SignatureJniObj" +classes="BcosSDKJniObj utilities.keypair.KeyPairJniObj utilities.tx.TransactionBuilderJniObj utilities.tx.TransactionBuilderV2JniObj utilities.tx.TransactionStructBuilderJniObj utilities.receipt.ReceiptBuilderJniObj utilities.signature.SignatureJniObj" for class in ${classes} do diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcJniObj.cpp index cb5f1b546..92c192aa6 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_rpc_RpcJniObj.cpp @@ -81,7 +81,7 @@ static void on_receive_rpc_response(struct bcos_sdk_c_struct_response* resp) // byte[] data jfieldID dataFieldID = env->GetFieldID(responseClass, "data", "[B"); - if (errorMsgFieldID == NULL) + if (dataFieldID == NULL) { env->ExceptionDescribe(); env->DeleteGlobalRef(jcallback); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp index b7d528df0..12f34235b 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp @@ -5,387 +5,752 @@ #include "org_fisco_bcos_sdk_common.h" #include "org_fisco_bcos_sdk_exception.h" -struct bcos_sdk_c_bytes* convert_to_bytes_struct(JNIEnv* env, jobject bytesObj) -{ - try - { - if (bytesObj == NULL) - { - return NULL; - } - struct bcos_sdk_c_bytes* bytes = - (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); - jclass bytesClass = env->GetObjectClass(bytesObj); - jfieldID bufferField = env->GetFieldID(bytesClass, "buffer", "[B"); - jfieldID lengthField = env->GetFieldID(bytesClass, "length", "I"); - - jbyteArray bufferArray = (jbyteArray)env->GetObjectField(bytesObj, bufferField); - jbyte* bufferData = env->GetByteArrayElements(bufferArray, nullptr); - jsize bufferLength = env->GetArrayLength(bufferArray); - bytes->length = (uint32_t)env->GetIntField(bytesObj, lengthField); - bytes->buffer = (uint8_t*)malloc(bufferLength * sizeof(uint8_t)); - memcpy(bytes->buffer, bufferData, bufferLength); - - env->ReleaseByteArrayElements(bufferArray, bufferData, 0); - - return bytes; - } - catch (...) +// tx data v2: java obj => c struct +struct bcos_sdk_c_transaction_data_v2* convert_to_tx_v2_data_struct( + JNIEnv* env, jobject transactionObject) +{ + if (transactionObject == NULL) { + THROW_JNI_EXCEPTION( + env, "exception in convert_to_tx_data_struct, transactionObject is nullptr"); return NULL; } -} -jobject convert_to_bytes_jobject(JNIEnv* env, const struct bcos_sdk_c_bytes* bytes_struct) -{ - try - { - if (bytes_struct == NULL) - { - return NULL; - } - - jclass txBytesClass = - env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes"); - if (txBytesClass == NULL) - { - env->FatalError( - "No such class, className: " - "org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes"); - } - jmethodID txBytesMtd = env->GetMethodID(txBytesClass, "", "()V"); - if (txBytesMtd == NULL) - { - env->FatalError("No such constructor in TransactionBytes, constructor()"); - } - jfieldID bufferField = env->GetFieldID(txBytesClass, "buffer", "[B"); - jfieldID lengthField = env->GetFieldID(txBytesClass, "length", "I"); - - jobject jTxBytesObj = env->NewObject(txBytesClass, txBytesMtd); - // buffer - jbyteArray bufferArray = env->NewByteArray(bytes_struct->length); - env->SetByteArrayRegion( - bufferArray, 0, bytes_struct->length, reinterpret_cast(bytes_struct->buffer)); - env->SetObjectField(jTxBytesObj, bufferField, bufferArray); - // length - env->SetIntField(jTxBytesObj, lengthField, bytes_struct->length); - - env->DeleteLocalRef(bufferArray); - - return jTxBytesObj; - } - catch (...) + jclass txDataClass = env->GetObjectClass(transactionObject); + + bcos_sdk_c_transaction_data_v2* tx_data_struct = (struct bcos_sdk_c_transaction_data_v2*)malloc( + sizeof(struct bcos_sdk_c_transaction_data_v2)); + // version + jfieldID versionField = env->GetFieldID(txDataClass, "version", "I"); + tx_data_struct->version = env->GetIntField(transactionObject, versionField); + + // blockLimit + jfieldID blockLimitField = env->GetFieldID(txDataClass, "blockLimit", "J"); + tx_data_struct->block_limit = env->GetLongField(transactionObject, blockLimitField); + + // chainId + jfieldID chainIdField = env->GetFieldID(txDataClass, "chainId", "Ljava/lang/String;"); + jstring chainIdString = (jstring)env->GetObjectField(transactionObject, chainIdField); + const char* chainIdValue = GET_J_STRING_CONTENT_DEF(env, chainIdString, ""); + tx_data_struct->chain_id = strdup(chainIdValue); + + // groupId + jfieldID groupIdField = env->GetFieldID(txDataClass, "groupId", "Ljava/lang/String;"); + jstring groupIdString = (jstring)env->GetObjectField(transactionObject, groupIdField); + const char* groupIdValue = GET_J_STRING_CONTENT_DEF(env, groupIdString, ""); + tx_data_struct->group_id = strdup(groupIdValue); + + // nonce + jfieldID nonceField = env->GetFieldID(txDataClass, "nonce", "Ljava/lang/String;"); + jstring nonceString = (jstring)env->GetObjectField(transactionObject, nonceField); + const char* nonceValue = GET_J_STRING_CONTENT_DEF(env, nonceString, ""); + tx_data_struct->nonce = strdup(nonceValue); + + // to + jfieldID toField = env->GetFieldID(txDataClass, "to", "Ljava/lang/String;"); + jstring toString = (jstring)env->GetObjectField(transactionObject, toField); + const char* toValue = GET_J_STRING_CONTENT_DEF(env, toString, ""); + tx_data_struct->to = strdup(toValue); + + // abi + jfieldID abiField = env->GetFieldID(txDataClass, "abi", "Ljava/lang/String;"); + jstring abiString = (jstring)env->GetObjectField(transactionObject, abiField); + const char* abiValue = GET_J_STRING_CONTENT_DEF(env, abiString, ""); + tx_data_struct->abi = strdup(abiValue); + + // input byte[] + jfieldID inputField = env->GetFieldID(txDataClass, "input", "[B"); + jbyteArray inputbytes = (jbyteArray)env->GetObjectField(transactionObject, inputField); + if (inputbytes == nullptr) { + THROW_JNI_EXCEPTION(env, "exception in input filed."); return NULL; } + struct bcos_sdk_c_bytes* bytes = + (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); + jbyte* bufferData = env->GetByteArrayElements(inputbytes, nullptr); + jsize bufferLength = env->GetArrayLength(inputbytes); + bytes->buffer = (uint8_t*)malloc(bufferLength * sizeof(uint8_t)); + memmove(bytes->buffer, bufferData, bufferLength); + bytes->length = bufferLength; + + tx_data_struct->input = bytes; + + + // value + jfieldID valueField = env->GetFieldID(txDataClass, "value", "Ljava/lang/String;"); + jstring valueString = (jstring)env->GetObjectField(transactionObject, valueField); + const char* valueChar = GET_J_STRING_CONTENT_DEF(env, valueString, ""); + tx_data_struct->value = strdup(valueChar); + + // gas_price + jfieldID gasPriceField = env->GetFieldID(txDataClass, "gasPrice", "Ljava/lang/String;"); + jstring gasPriceString = (jstring)env->GetObjectField(transactionObject, gasPriceField); + const char* gasPriceChar = GET_J_STRING_CONTENT_DEF(env, gasPriceString, ""); + tx_data_struct->gas_price = strdup(gasPriceChar); + + // gas_limit + jfieldID gasLimitField = env->GetFieldID(txDataClass, "gasLimit", "J"); + tx_data_struct->gas_limit = env->GetLongField(transactionObject, gasLimitField); + + // max_fee_per_gas + jfieldID maxFeePerGasField = env->GetFieldID(txDataClass, "maxFeePerGas", "Ljava/lang/String;"); + jstring maxFeePerGasString = (jstring)env->GetObjectField(transactionObject, maxFeePerGasField); + const char* maxFeePerGasChar = GET_J_STRING_CONTENT_DEF(env, maxFeePerGasString, ""); + tx_data_struct->max_fee_per_gas = strdup(maxFeePerGasChar); + + // max_priority_fee_per_gas + jfieldID maxPriorityFeePerGasField = + env->GetFieldID(txDataClass, "maxPriorityFeePerGas", "Ljava/lang/String;"); + jstring maxPriorityFeePerGasString = + (jstring)env->GetObjectField(transactionObject, maxPriorityFeePerGasField); + const char* maxPriorityFeePerGasChar = + GET_J_STRING_CONTENT_DEF(env, maxPriorityFeePerGasString, ""); + tx_data_struct->max_priority_fee_per_gas = strdup(maxPriorityFeePerGasChar); + + // release source + if (chainIdString) + env->ReleaseStringUTFChars(chainIdString, chainIdValue); + if (groupIdString) + env->ReleaseStringUTFChars(groupIdString, groupIdValue); + if (nonceString) + env->ReleaseStringUTFChars(nonceString, nonceValue); + if (toString) + env->ReleaseStringUTFChars(toString, toValue); + if (abiString) + env->ReleaseStringUTFChars(abiString, abiValue); + if (inputbytes) + env->ReleaseByteArrayElements(inputbytes, bufferData, 0); + if (valueString) + env->ReleaseStringUTFChars(valueString, valueChar); + if (gasPriceString) + env->ReleaseStringUTFChars(gasPriceString, gasPriceChar); + if (maxFeePerGasString) + env->ReleaseStringUTFChars(maxFeePerGasString, maxFeePerGasChar); + if (maxPriorityFeePerGasString) + env->ReleaseStringUTFChars(maxPriorityFeePerGasString, maxPriorityFeePerGasChar); + + return tx_data_struct; } +// tx data: java obj => c struct struct bcos_sdk_c_transaction_data* convert_to_tx_data_struct( JNIEnv* env, jobject transactionObject) { - try + if (transactionObject == NULL) { - if (transactionObject == NULL) - { - THROW_JNI_EXCEPTION( - env, "exception in convert_to_tx_data_struct, transactionObject is nullptr"); - return NULL; - } + THROW_JNI_EXCEPTION( + env, "exception in convert_to_tx_data_struct, transactionObject is nullptr"); + return NULL; + } - bcos_sdk_c_transaction_data* tx_data_struct = - (struct bcos_sdk_c_transaction_data*)malloc(sizeof(struct bcos_sdk_c_transaction_data)); - jclass txDataClass = env->GetObjectClass(transactionObject); - - // version - jfieldID versionField = env->GetFieldID(txDataClass, "version", "I"); - tx_data_struct->version = env->GetIntField(transactionObject, versionField); - - // blockLimit - jfieldID blockLimitField = env->GetFieldID(txDataClass, "blockLimit", "J"); - tx_data_struct->block_limit = env->GetLongField(transactionObject, blockLimitField); - - // chainId - jfieldID chainIdField = env->GetFieldID(txDataClass, "chainId", "Ljava/lang/String;"); - jstring chainIdString = (jstring)env->GetObjectField(transactionObject, chainIdField); - const char* chainIdValue = env->GetStringUTFChars(chainIdString, nullptr); - tx_data_struct->chain_id = strdup(chainIdValue); - - // groupId - jfieldID groupIdField = env->GetFieldID(txDataClass, "groupId", "Ljava/lang/String;"); - jstring groupIdString = (jstring)env->GetObjectField(transactionObject, groupIdField); - const char* groupIdValue = env->GetStringUTFChars(groupIdString, nullptr); - tx_data_struct->group_id = strdup(groupIdValue); - - // nonce - jfieldID nonceField = env->GetFieldID(txDataClass, "nonce", "Ljava/lang/String;"); - jstring nonceString = (jstring)env->GetObjectField(transactionObject, nonceField); - const char* nonceValue = env->GetStringUTFChars(nonceString, nullptr); - tx_data_struct->nonce = strdup(nonceValue); - - // to - jfieldID toField = env->GetFieldID(txDataClass, "to", "Ljava/lang/String;"); - jstring toString = (jstring)env->GetObjectField(transactionObject, toField); - const char* toValue = env->GetStringUTFChars(toString, nullptr); - tx_data_struct->to = strdup(toValue); - - // abi - jfieldID abiField = env->GetFieldID(txDataClass, "abi", "Ljava/lang/String;"); - jstring abiString = (jstring)env->GetObjectField(transactionObject, abiField); - const char* abiValue = env->GetStringUTFChars(abiString, nullptr); - tx_data_struct->abi = strdup(abiValue); - - // input - jfieldID inputField = env->GetFieldID( - txDataClass, "input", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jobject inputObject = env->GetObjectField(transactionObject, inputField); - struct bcos_sdk_c_bytes* inputStruct = convert_to_bytes_struct(env, inputObject); - if (inputStruct == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_struct"); - return NULL; - } - tx_data_struct->input = inputStruct; + bcos_sdk_c_transaction_data* tx_data_struct = + (struct bcos_sdk_c_transaction_data*)malloc(sizeof(struct bcos_sdk_c_transaction_data)); + jclass txDataClass = env->GetObjectClass(transactionObject); + + // version + jfieldID versionField = env->GetFieldID(txDataClass, "version", "I"); + tx_data_struct->version = env->GetIntField(transactionObject, versionField); + + // blockLimit + jfieldID blockLimitField = env->GetFieldID(txDataClass, "blockLimit", "J"); + tx_data_struct->block_limit = env->GetLongField(transactionObject, blockLimitField); + + // chainId + jfieldID chainIdField = env->GetFieldID(txDataClass, "chainId", "Ljava/lang/String;"); + jstring chainIdString = (jstring)env->GetObjectField(transactionObject, chainIdField); + const char* chainIdValue = GET_J_STRING_CONTENT_DEF(env, chainIdString, ""); + tx_data_struct->chain_id = strdup(chainIdValue); + + // groupId + jfieldID groupIdField = env->GetFieldID(txDataClass, "groupId", "Ljava/lang/String;"); + jstring groupIdString = (jstring)env->GetObjectField(transactionObject, groupIdField); + const char* groupIdValue = GET_J_STRING_CONTENT_DEF(env, groupIdString, ""); + tx_data_struct->group_id = strdup(groupIdValue); + + // nonce + jfieldID nonceField = env->GetFieldID(txDataClass, "nonce", "Ljava/lang/String;"); + jstring nonceString = (jstring)env->GetObjectField(transactionObject, nonceField); + const char* nonceValue = GET_J_STRING_CONTENT_DEF(env, nonceString, ""); + tx_data_struct->nonce = strdup(nonceValue); + + // to + jfieldID toField = env->GetFieldID(txDataClass, "to", "Ljava/lang/String;"); + jstring toString = (jstring)env->GetObjectField(transactionObject, toField); + const char* toValue = GET_J_STRING_CONTENT_DEF(env, toString, ""); + tx_data_struct->to = strdup(toValue); + + // abi + jfieldID abiField = env->GetFieldID(txDataClass, "abi", "Ljava/lang/String;"); + jstring abiString = (jstring)env->GetObjectField(transactionObject, abiField); + const char* abiValue = GET_J_STRING_CONTENT_DEF(env, abiString, ""); + tx_data_struct->abi = strdup(abiValue); + + // input byte[] + jfieldID inputField = env->GetFieldID(txDataClass, "input", "[B"); + jbyteArray inputbytes = (jbyteArray)env->GetObjectField(transactionObject, inputField); + if (inputbytes == nullptr) + { + THROW_JNI_EXCEPTION(env, "exception in input filed."); + return NULL; + } + struct bcos_sdk_c_bytes* bytes = + (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); + jbyte* bufferData = env->GetByteArrayElements(inputbytes, nullptr); + jsize bufferLength = env->GetArrayLength(inputbytes); + bytes->buffer = (uint8_t*)malloc(bufferLength * sizeof(uint8_t)); + memmove(bytes->buffer, bufferData, bufferLength); + bytes->length = bufferLength; + + tx_data_struct->input = bytes; - // release source + // release source + if (chainIdString) env->ReleaseStringUTFChars(chainIdString, chainIdValue); + if (groupIdString) env->ReleaseStringUTFChars(groupIdString, groupIdValue); + if (nonceString) env->ReleaseStringUTFChars(nonceString, nonceValue); + if (toString) env->ReleaseStringUTFChars(toString, toValue); + if (abiString) env->ReleaseStringUTFChars(abiString, abiValue); + if (inputbytes) + env->ReleaseByteArrayElements(inputbytes, bufferData, 0); - return tx_data_struct; - } - catch (...) - { - return NULL; - } + return tx_data_struct; } +// tx data: c struct => java obj jobject convert_to_tx_data_jobject( JNIEnv* env, const struct bcos_sdk_c_transaction_data* transactionData) { - try + if (transactionData == NULL) { - if (transactionData == NULL) - { - THROW_JNI_EXCEPTION( - env, "exception in convert_to_tx_data_jobject, transactionData is nullptr"); - return NULL; - } + THROW_JNI_EXCEPTION( + env, "exception in convert_to_tx_data_jobject, transactionData is nullptr"); + return NULL; + } - jclass txDataClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionData"); - if (txDataClass == NULL) - { - env->FatalError( - "No such class, className: " - "org/fisco/bcos/sdk/jni/utilities/tx/TransactionData"); - } - jmethodID txDataMtd = env->GetMethodID(txDataClass, "", "()V"); - if (txDataMtd == NULL) - { - env->FatalError("No such constructor in TransactionData, constructor()"); - } - jobject jTxDataObj = env->NewObject(txDataClass, txDataMtd); - - // version - jfieldID versionField = env->GetFieldID(txDataClass, "version", "I"); - env->SetIntField(jTxDataObj, versionField, transactionData->version); - // block_limit - jfieldID blockLimitField = env->GetFieldID(txDataClass, "blockLimit", "J"); - env->SetLongField(jTxDataObj, blockLimitField, transactionData->block_limit); - // chain_id - jfieldID chainIdField = env->GetFieldID(txDataClass, "chainId", "Ljava/lang/String;"); - env->SetObjectField(jTxDataObj, chainIdField, env->NewStringUTF(transactionData->chain_id)); - // group_id - jfieldID groupIdField = env->GetFieldID(txDataClass, "groupId", "Ljava/lang/String;"); - env->SetObjectField(jTxDataObj, groupIdField, env->NewStringUTF(transactionData->group_id)); - // nonce - jfieldID nonceField = env->GetFieldID(txDataClass, "nonce", "Ljava/lang/String;"); - env->SetObjectField(jTxDataObj, nonceField, env->NewStringUTF(transactionData->nonce)); - // to - jfieldID toField = env->GetFieldID(txDataClass, "to", "Ljava/lang/String;"); - env->SetObjectField(jTxDataObj, toField, env->NewStringUTF(transactionData->to)); - // abi - jfieldID abiField = env->GetFieldID(txDataClass, "abi", "Ljava/lang/String;"); - env->SetObjectField(jTxDataObj, abiField, env->NewStringUTF(transactionData->abi)); - - // input - jobject jInput = convert_to_bytes_jobject(env, transactionData->input); - if (jInput == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_jobject"); - return NULL; - } - jfieldID inputField = env->GetFieldID( - txDataClass, "input", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - env->SetObjectField(jTxDataObj, inputField, jInput); + jclass txDataClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionData"); + if (txDataClass == NULL) + { + env->FatalError( + "No such class, className: " + "org/fisco/bcos/sdk/jni/utilities/tx/TransactionData"); + } + jmethodID txDataMtd = env->GetMethodID(txDataClass, "", "()V"); + if (txDataMtd == NULL) + { + env->FatalError("No such constructor in TransactionData, constructor()"); + } + jobject jTxDataObj = env->NewObject(txDataClass, txDataMtd); + + // version + jfieldID versionField = env->GetFieldID(txDataClass, "version", "I"); + env->SetIntField(jTxDataObj, versionField, transactionData->version); + // block_limit + jfieldID blockLimitField = env->GetFieldID(txDataClass, "blockLimit", "J"); + env->SetLongField(jTxDataObj, blockLimitField, transactionData->block_limit); + // chain_id + jfieldID chainIdField = env->GetFieldID(txDataClass, "chainId", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, chainIdField, env->NewStringUTF(transactionData->chain_id)); + // group_id + jfieldID groupIdField = env->GetFieldID(txDataClass, "groupId", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, groupIdField, env->NewStringUTF(transactionData->group_id)); + // nonce + jfieldID nonceField = env->GetFieldID(txDataClass, "nonce", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, nonceField, env->NewStringUTF(transactionData->nonce)); + // to + jfieldID toField = env->GetFieldID(txDataClass, "to", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, toField, env->NewStringUTF(transactionData->to)); + // abi + jfieldID abiField = env->GetFieldID(txDataClass, "abi", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, abiField, env->NewStringUTF(transactionData->abi)); + + if (transactionData->input) + { + jfieldID inputField = env->GetFieldID(txDataClass, "input", "[B"); + jbyteArray bufferArray = + env->NewByteArray(static_cast(transactionData->input->length)); + env->SetByteArrayRegion(bufferArray, 0, static_cast(transactionData->input->length), + reinterpret_cast(transactionData->input->buffer)); + env->SetObjectField(jTxDataObj, inputField, bufferArray); + } + return jTxDataObj; +} - // release - env->DeleteLocalRef(jInput); +// tx data v2: c struct => java obj +jobject convert_to_tx_v2_data_jobject( + JNIEnv* env, const struct bcos_sdk_c_transaction_data_v2* transactionData) +{ + if (transactionData == NULL) + { + THROW_JNI_EXCEPTION( + env, "exception in convert_to_tx_data_jobject, transactionData is nullptr"); + return NULL; + } - return jTxDataObj; + jclass txDataClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2"); + if (txDataClass == NULL) + { + env->FatalError( + "No such class, className: " + "org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2"); } - catch (...) + jmethodID txDataMtd = env->GetMethodID(txDataClass, "", "()V"); + if (txDataMtd == NULL) { - return NULL; + env->FatalError("No such constructor in TransactionData, constructor()"); } + jobject jTxDataObj = env->NewObject(txDataClass, txDataMtd); + + // version + jfieldID versionField = env->GetFieldID(txDataClass, "version", "I"); + env->SetIntField(jTxDataObj, versionField, transactionData->version); + // block_limit + jfieldID blockLimitField = env->GetFieldID(txDataClass, "blockLimit", "J"); + env->SetLongField(jTxDataObj, blockLimitField, transactionData->block_limit); + // chain_id + jfieldID chainIdField = env->GetFieldID(txDataClass, "chainId", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, chainIdField, env->NewStringUTF(transactionData->chain_id)); + // group_id + jfieldID groupIdField = env->GetFieldID(txDataClass, "groupId", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, groupIdField, env->NewStringUTF(transactionData->group_id)); + // nonce + jfieldID nonceField = env->GetFieldID(txDataClass, "nonce", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, nonceField, env->NewStringUTF(transactionData->nonce)); + // to + jfieldID toField = env->GetFieldID(txDataClass, "to", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, toField, env->NewStringUTF(transactionData->to)); + // abi + jfieldID abiField = env->GetFieldID(txDataClass, "abi", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, abiField, env->NewStringUTF(transactionData->abi)); + // input + if (transactionData->input) + { + jfieldID inputField = env->GetFieldID(txDataClass, "input", "[B"); + jbyteArray bufferArray = + env->NewByteArray(static_cast(transactionData->input->length)); + env->SetByteArrayRegion(bufferArray, 0, static_cast(transactionData->input->length), + reinterpret_cast(transactionData->input->buffer)); + env->SetObjectField(jTxDataObj, inputField, bufferArray); + } + + // value + jfieldID valueField = env->GetFieldID(txDataClass, "value", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, valueField, env->NewStringUTF(transactionData->value)); + // gas_price + jfieldID gasPriceField = env->GetFieldID(txDataClass, "gasPrice", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, gasPriceField, env->NewStringUTF(transactionData->gas_price)); + // gas_limit + jfieldID gasLimitField = env->GetFieldID(txDataClass, "gasLimit", "J"); + env->SetLongField(jTxDataObj, gasLimitField, transactionData->gas_limit); + // max_fee_per_gas + jfieldID maxFeePerGasField = env->GetFieldID(txDataClass, "maxFeePerGas", "Ljava/lang/String;"); + env->SetObjectField( + jTxDataObj, maxFeePerGasField, env->NewStringUTF(transactionData->max_fee_per_gas)); + // max_priority_fee_per_gas + jfieldID maxPriorityFeePerGasField = + env->GetFieldID(txDataClass, "maxPriorityFeePerGas", "Ljava/lang/String;"); + env->SetObjectField(jTxDataObj, maxPriorityFeePerGasField, + env->NewStringUTF(transactionData->max_priority_fee_per_gas)); + + + return jTxDataObj; } +// tx: c struct => java obj jobject convert_to_tx_jobject(JNIEnv* env, const struct bcos_sdk_c_transaction* tx_struct) { - try + if (tx_struct == NULL) { - if (tx_struct == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_jobject, tx_struct is nullptr"); - return NULL; - } + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_jobject, tx_struct is nullptr"); + return NULL; + } - jclass txClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/Transaction"); - if (txClass == NULL) + jclass txClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/Transaction"); + if (txClass == NULL) + { + env->FatalError( + "No such class, className: " + "org/fisco/bcos/sdk/jni/utilities/tx/Transaction"); + } + jmethodID txMtd = env->GetMethodID(txClass, "", "()V"); + if (txMtd == NULL) + { + env->FatalError("No such constructor in Transaction, constructor()"); + } + jfieldID txDataField = env->GetFieldID( + txClass, "transactionData", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;"); + jfieldID dataHashField = env->GetFieldID(txClass, "dataHash", "[B"); + jfieldID signatureField = env->GetFieldID(txClass, "signature", "[B"); + jfieldID senderField = env->GetFieldID(txClass, "sender", "[B"); + jfieldID importTimeField = env->GetFieldID(txClass, "importTime", "J"); + jfieldID attributeField = env->GetFieldID(txClass, "attribute", "I"); + jfieldID extraDataField = env->GetFieldID(txClass, "extraData", "Ljava/lang/String;"); + + jobject javaTxObj = env->NewObject(txClass, txMtd); + // TransactionData + jobject javaTxDataObj = convert_to_tx_data_jobject(env, tx_struct->transaction_data); + if (javaTxDataObj == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in TransactionData field."); + return NULL; + } + env->SetObjectField(javaTxObj, txDataField, javaTxDataObj); + // DataHash byte[] + { + if (tx_struct->data_hash != NULL) { - env->FatalError( - "No such class, className: " - "org/fisco/bcos/sdk/jni/utilities/tx/Transaction"); + jbyteArray bufferArray = + env->NewByteArray(static_cast(tx_struct->data_hash->length)); + env->SetByteArrayRegion(bufferArray, 0, + static_cast(tx_struct->data_hash->length), + reinterpret_cast(tx_struct->data_hash->buffer)); + env->SetObjectField(javaTxObj, dataHashField, bufferArray); } - jmethodID txMtd = env->GetMethodID(txClass, "", "()V"); - if (txMtd == NULL) + } + // Signature byte[] + { + if (tx_struct->signature != NULL) { - env->FatalError("No such constructor in Transaction, constructor()"); + jbyteArray bufferArray = + env->NewByteArray(static_cast(tx_struct->signature->length)); + env->SetByteArrayRegion(bufferArray, 0, + static_cast(tx_struct->signature->length), + reinterpret_cast(tx_struct->signature->buffer)); + env->SetObjectField(javaTxObj, signatureField, bufferArray); } - jfieldID txDataField = env->GetFieldID( - txClass, "transactionData", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;"); - jfieldID dataHashField = env->GetFieldID( - txClass, "dataHash", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jfieldID signatureField = env->GetFieldID( - txClass, "signature", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jfieldID senderField = env->GetFieldID( - txClass, "sender", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jfieldID importTimeField = env->GetFieldID(txClass, "importTime", "J"); - jfieldID attributeField = env->GetFieldID(txClass, "attribute", "I"); - jfieldID extraDataField = env->GetFieldID(txClass, "extraData", "Ljava/lang/String;"); - - jobject javaTxObj = env->NewObject(txClass, txMtd); - // TransactionData - jobject javaTxDataObj = convert_to_tx_data_jobject(env, tx_struct->transaction_data); - if (javaTxDataObj == NULL) + } + // Sender byte[] + { + if (tx_struct->sender != NULL) { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_jobject"); - return NULL; + jbyteArray bufferArray = + env->NewByteArray(static_cast(tx_struct->sender->length)); + env->SetByteArrayRegion(bufferArray, 0, static_cast(tx_struct->sender->length), + reinterpret_cast(tx_struct->sender->buffer)); + env->SetObjectField(javaTxObj, senderField, bufferArray); } - env->SetObjectField(javaTxObj, txDataField, javaTxDataObj); - // DataHash - jobject javaDataHashObj = convert_to_bytes_jobject(env, tx_struct->data_hash); - if (javaDataHashObj == NULL) + } + // ImportTime + env->SetLongField(javaTxObj, importTimeField, tx_struct->import_time); + // Attribute + env->SetIntField(javaTxObj, attributeField, tx_struct->attribute); + // ExtraData + jstring javaExtraData = env->NewStringUTF(tx_struct->extra_data); + env->SetObjectField(javaTxObj, extraDataField, javaExtraData); + + return javaTxObj; +} + +// tx v2: c struct => java obj +jobject convert_to_tx_v2_jobject(JNIEnv* env, const struct bcos_sdk_c_transaction_v2* tx_struct) + +{ + if (tx_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_jobject, tx_struct is nullptr"); + return NULL; + } + + jclass txClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/Transaction"); + if (txClass == NULL) + { + env->FatalError( + "No such class, className: " + "org/fisco/bcos/sdk/jni/utilities/tx/Transaction"); + } + jmethodID txMtd = env->GetMethodID(txClass, "", "()V"); + if (txMtd == NULL) + { + env->FatalError("No such constructor in Transaction, constructor()"); + } + jfieldID txDataField = env->GetFieldID( + txClass, "transactionData", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;"); + jfieldID dataHashField = env->GetFieldID(txClass, "dataHash", "[B"); + jfieldID signatureField = env->GetFieldID(txClass, "signature", "[B"); + jfieldID senderField = env->GetFieldID(txClass, "sender", "[B"); + jfieldID importTimeField = env->GetFieldID(txClass, "importTime", "J"); + jfieldID attributeField = env->GetFieldID(txClass, "attribute", "I"); + jfieldID extraDataField = env->GetFieldID(txClass, "extraData", "Ljava/lang/String;"); + + jobject javaTxObj = env->NewObject(txClass, txMtd); + // TransactionData + jobject javaTxDataObj = convert_to_tx_v2_data_jobject(env, tx_struct->transaction_data); + if (javaTxDataObj == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in TransactionData field."); + return NULL; + } + env->SetObjectField(javaTxObj, txDataField, javaTxDataObj); + // DataHash byte[] + { + if (tx_struct->data_hash) { - THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_jobject"); - return NULL; + jbyteArray bufferArray = + env->NewByteArray(static_cast(tx_struct->data_hash->length)); + env->SetByteArrayRegion(bufferArray, 0, + static_cast(tx_struct->data_hash->length), + reinterpret_cast(tx_struct->data_hash->buffer)); + env->SetObjectField(javaTxObj, dataHashField, bufferArray); } - env->SetObjectField(javaTxObj, dataHashField, javaDataHashObj); - // Signature - jobject javaSignatureObj = convert_to_bytes_jobject(env, tx_struct->signature); - if (javaSignatureObj == NULL) + } + // Signature byte[] + { + if (tx_struct->signature) { - THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_jobject"); - return NULL; + jbyteArray bufferArray = + env->NewByteArray(static_cast(tx_struct->signature->length)); + env->SetByteArrayRegion(bufferArray, 0, + static_cast(tx_struct->signature->length), + reinterpret_cast(tx_struct->signature->buffer)); + env->SetObjectField(javaTxObj, signatureField, bufferArray); } - env->SetObjectField(javaTxObj, signatureField, javaSignatureObj); - // Sender - jobject javaSenderObj = convert_to_bytes_jobject(env, tx_struct->sender); - env->SetObjectField(javaTxObj, senderField, javaSenderObj); - // ImportTime - env->SetLongField(javaTxObj, importTimeField, tx_struct->import_time); - // Attribute - env->SetIntField(javaTxObj, attributeField, tx_struct->attribute); - // ExtraData - jstring javaExtraData = env->NewStringUTF(tx_struct->extra_data); - env->SetObjectField(javaTxObj, extraDataField, javaExtraData); - - return javaTxObj; } - catch (...) + // Sender byte[] { - return NULL; + if (tx_struct->sender) + { + jbyteArray bufferArray = + env->NewByteArray(static_cast(tx_struct->sender->length)); + env->SetByteArrayRegion(bufferArray, 0, static_cast(tx_struct->sender->length), + reinterpret_cast(tx_struct->sender->buffer)); + env->SetObjectField(javaTxObj, senderField, bufferArray); + } } + // ImportTime + env->SetLongField(javaTxObj, importTimeField, tx_struct->import_time); + // Attribute + env->SetIntField(javaTxObj, attributeField, tx_struct->attribute); + // ExtraData + jstring javaExtraData = env->NewStringUTF(tx_struct->extra_data); + env->SetObjectField(javaTxObj, extraDataField, javaExtraData); + + return javaTxObj; } +// tx: java obj => c struct struct bcos_sdk_c_transaction* convert_to_tx_struct(JNIEnv* env, jobject jTxObj) { - try + if (jTxObj == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct, Transaction is nullptr"); + return NULL; + } + + jclass javaTxClass = env->GetObjectClass(jTxObj); + jfieldID transactionDataField = env->GetFieldID( + javaTxClass, "transactionData", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;"); + jfieldID dataHashField = env->GetFieldID(javaTxClass, "dataHash", "[B"); + jfieldID signatureField = env->GetFieldID(javaTxClass, "signature", "[B"); + jfieldID senderField = env->GetFieldID(javaTxClass, "sender", "[B"); + jfieldID importTimeField = env->GetFieldID(javaTxClass, "importTime", "J"); + jfieldID attributeField = env->GetFieldID(javaTxClass, "attribute", "I"); + jfieldID extraDataField = env->GetFieldID(javaTxClass, "extraData", "Ljava/lang/String;"); + + struct bcos_sdk_c_transaction* txStruct = + (struct bcos_sdk_c_transaction*)malloc(sizeof(struct bcos_sdk_c_transaction)); + // TransactionData + jobject javaTxDataObj = env->GetObjectField(jTxObj, transactionDataField); + struct bcos_sdk_c_transaction_data* txDataStruct = + convert_to_tx_data_struct(env, javaTxDataObj); + if (txDataStruct == NULL) { - if (jTxObj == NULL) + THROW_JNI_EXCEPTION(env, "exception in txDataStruct field."); + return NULL; + } + txStruct->transaction_data = txDataStruct; + // DataHash byte[] + { + jbyteArray dataHashBytes = (jbyteArray)env->GetObjectField(jTxObj, dataHashField); + if (dataHashBytes == NULL) { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct, jTxObj is nullptr"); + THROW_JNI_EXCEPTION(env, "exception in DataHash field."); return NULL; } + struct bcos_sdk_c_bytes* dataHash = + (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); + jbyte* bufferData = env->GetByteArrayElements(dataHashBytes, nullptr); + jsize bufferLength = env->GetArrayLength(dataHashBytes); + dataHash->buffer = (uint8_t*)malloc(bufferLength * sizeof(uint8_t)); + memmove(dataHash->buffer, bufferData, bufferLength); + dataHash->length = bufferLength; + + // release temp dataHashBytes + env->ReleaseByteArrayElements(dataHashBytes, bufferData, 0); + txStruct->data_hash = dataHash; + } - jclass javaTxClass = env->GetObjectClass(jTxObj); - jfieldID transactionDataField = env->GetFieldID(javaTxClass, "transactionData", - "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;"); - jfieldID dataHashField = env->GetFieldID( - javaTxClass, "dataHash", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jfieldID signatureField = env->GetFieldID( - javaTxClass, "signature", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jfieldID senderField = env->GetFieldID( - javaTxClass, "sender", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jfieldID importTimeField = env->GetFieldID(javaTxClass, "importTime", "J"); - jfieldID attributeField = env->GetFieldID(javaTxClass, "attribute", "I"); - jfieldID extraDataField = env->GetFieldID(javaTxClass, "extraData", "Ljava/lang/String;"); - - struct bcos_sdk_c_transaction* txStruct = - (struct bcos_sdk_c_transaction*)malloc(sizeof(struct bcos_sdk_c_transaction)); - // TransactionData - jobject javaTxDataObj = env->GetObjectField(jTxObj, transactionDataField); - struct bcos_sdk_c_transaction_data* txDataStruct = - convert_to_tx_data_struct(env, javaTxDataObj); - if (txDataStruct == NULL) + // Signature byte[] + { + jbyteArray signatureBytes = (jbyteArray)env->GetObjectField(jTxObj, signatureField); + if (signatureBytes == NULL) { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); + THROW_JNI_EXCEPTION(env, "exception in Signature field."); return NULL; } - txStruct->transaction_data = txDataStruct; - // DataHash - jobject javaDataHashObj = env->GetObjectField(jTxObj, dataHashField); - struct bcos_sdk_c_bytes* dataHashStruct = convert_to_bytes_struct(env, javaDataHashObj); - if (dataHashStruct == NULL) + struct bcos_sdk_c_bytes* signature = + (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); + jbyte* bufferData = env->GetByteArrayElements(signatureBytes, nullptr); + jsize bufferLength = env->GetArrayLength(signatureBytes); + signature->buffer = (uint8_t*)malloc(bufferLength * sizeof(uint8_t)); + memmove(signature->buffer, bufferData, bufferLength); + signature->length = bufferLength; + + // release temp signatureBytes + env->ReleaseByteArrayElements(signatureBytes, bufferData, 0); + txStruct->signature = signature; + } + // Sender byte[] + { + jbyteArray senderBytes = (jbyteArray)env->GetObjectField(jTxObj, senderField); + if (senderBytes == NULL) { - THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_struct"); - return NULL; + // sender can be null + txStruct->sender = NULL; } - txStruct->data_hash = dataHashStruct; - // Signature - jobject javaSignatureObj = env->GetObjectField(jTxObj, signatureField); - struct bcos_sdk_c_bytes* signatureStruct = convert_to_bytes_struct(env, javaSignatureObj); - if (signatureStruct == NULL) + else { - THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_struct"); - return NULL; + struct bcos_sdk_c_bytes* sender = + (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); + jbyte* bufferData = env->GetByteArrayElements(senderBytes, nullptr); + jsize bufferLength = env->GetArrayLength(senderBytes); + sender->buffer = (uint8_t*)malloc(bufferLength * sizeof(uint8_t)); + memmove(sender->buffer, bufferData, bufferLength); + sender->length = bufferLength; + + // release temp senderBytes + env->ReleaseByteArrayElements(senderBytes, bufferData, 0); + txStruct->sender = sender; } - txStruct->signature = signatureStruct; - // Sender - jobject javaSenderObj = env->GetObjectField(jTxObj, senderField); - struct bcos_sdk_c_bytes* senderStruct = convert_to_bytes_struct(env, javaSenderObj); - txStruct->sender = senderStruct; - // ImportTime - jlong importTimeValue = env->GetLongField(jTxObj, importTimeField); - txStruct->import_time = (int64_t)importTimeValue; - // Attribute - jint attributeValue = env->GetIntField(jTxObj, attributeField); - txStruct->attribute = (int32_t)attributeValue; - // ExtraData - jstring javaExtraData = (jstring)env->GetObjectField(jTxObj, extraDataField); - const char* extraDataValue = env->GetStringUTFChars(javaExtraData, NULL); - txStruct->extra_data = strdup(extraDataValue); - - env->ReleaseStringUTFChars(javaExtraData, extraDataValue); + } + // ImportTime + jlong importTimeValue = env->GetLongField(jTxObj, importTimeField); + txStruct->import_time = (int64_t)importTimeValue; + // Attribute + jint attributeValue = env->GetIntField(jTxObj, attributeField); + txStruct->attribute = (int32_t)attributeValue; + // ExtraData + jstring javaExtraData = (jstring)env->GetObjectField(jTxObj, extraDataField); + const char* extraDataValue = GET_J_STRING_CONTENT_DEF(env, javaExtraData, ""); + txStruct->extra_data = strdup(extraDataValue); + + env->ReleaseStringUTFChars(javaExtraData, extraDataValue); + + return txStruct; +} - return txStruct; +// tx v2: java obj => c struct +struct bcos_sdk_c_transaction_v2* convert_to_tx_v2_struct(JNIEnv* env, jobject jTxObj) +{ + if (jTxObj == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct, Transaction is nullptr"); + return NULL; } - catch (...) + + jclass javaTxClass = env->GetObjectClass(jTxObj); + jfieldID transactionDataField = env->GetFieldID( + javaTxClass, "transactionData", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;"); + jfieldID dataHashField = env->GetFieldID(javaTxClass, "dataHash", "[B"); + jfieldID signatureField = env->GetFieldID(javaTxClass, "signature", "[B"); + jfieldID senderField = env->GetFieldID(javaTxClass, "sender", "[B"); + jfieldID importTimeField = env->GetFieldID(javaTxClass, "importTime", "J"); + jfieldID attributeField = env->GetFieldID(javaTxClass, "attribute", "I"); + jfieldID extraDataField = env->GetFieldID(javaTxClass, "extraData", "Ljava/lang/String;"); + + struct bcos_sdk_c_transaction_v2* txStruct = + (struct bcos_sdk_c_transaction_v2*)malloc(sizeof(struct bcos_sdk_c_transaction_v2)); + // TransactionData + jobject javaTxDataObj = env->GetObjectField(jTxObj, transactionDataField); + struct bcos_sdk_c_transaction_data_v2* txDataStruct = + convert_to_tx_v2_data_struct(env, javaTxDataObj); + if (txDataStruct == NULL) { + THROW_JNI_EXCEPTION(env, "exception in txDataStruct field."); return NULL; } + txStruct->transaction_data = txDataStruct; + // DataHash byte[] + { + jbyteArray dataHashBytes = (jbyteArray)env->GetObjectField(jTxObj, dataHashField); + if (dataHashBytes == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in DataHash field."); + return NULL; + } + struct bcos_sdk_c_bytes* dataHash = + (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); + jbyte* bufferData = env->GetByteArrayElements(dataHashBytes, nullptr); + jsize bufferLength = env->GetArrayLength(dataHashBytes); + dataHash->buffer = (uint8_t*)malloc(bufferLength * sizeof(uint8_t)); + memmove(dataHash->buffer, bufferData, bufferLength); + dataHash->length = bufferLength; + + // release temp dataHashBytes + env->ReleaseByteArrayElements(dataHashBytes, bufferData, 0); + txStruct->data_hash = dataHash; + } + + // Signature byte[] + { + jbyteArray signatureBytes = (jbyteArray)env->GetObjectField(jTxObj, signatureField); + if (signatureBytes == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in Signature field."); + return NULL; + } + struct bcos_sdk_c_bytes* signature = + (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); + jbyte* bufferData = env->GetByteArrayElements(signatureBytes, nullptr); + jsize bufferLength = env->GetArrayLength(signatureBytes); + signature->buffer = (uint8_t*)malloc(bufferLength * sizeof(uint8_t)); + memmove(signature->buffer, bufferData, bufferLength); + signature->length = bufferLength; + + // release temp signatureBytes + env->ReleaseByteArrayElements(signatureBytes, bufferData, 0); + txStruct->signature = signature; + } + // Sender byte[] + { + jbyteArray senderBytes = (jbyteArray)env->GetObjectField(jTxObj, senderField); + if (senderBytes == NULL) + { + // sender can be null + txStruct->sender = NULL; + } + else + { + struct bcos_sdk_c_bytes* sender = + (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); + jbyte* bufferData = env->GetByteArrayElements(senderBytes, nullptr); + jsize bufferLength = env->GetArrayLength(senderBytes); + sender->buffer = (uint8_t*)malloc(bufferLength * sizeof(uint8_t)); + memmove(sender->buffer, bufferData, bufferLength); + sender->length = bufferLength; + // release temp senderBytes + env->ReleaseByteArrayElements(senderBytes, bufferData, 0); + txStruct->sender = sender; + } + } + // ImportTime + jlong importTimeValue = env->GetLongField(jTxObj, importTimeField); + txStruct->import_time = (int64_t)importTimeValue; + // Attribute + jint attributeValue = env->GetIntField(jTxObj, attributeField); + txStruct->attribute = (int32_t)attributeValue; + // ExtraData + jstring javaExtraData = (jstring)env->GetObjectField(jTxObj, extraDataField); + const char* extraDataValue = GET_J_STRING_CONTENT_DEF(env, javaExtraData, ""); + txStruct->extra_data = strdup(extraDataValue); + + env->ReleaseStringUTFChars(javaExtraData, extraDataValue); + + return txStruct; } /* @@ -397,14 +762,43 @@ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTransactionDataStruct( JNIEnv* env, jclass, jobject jTransactionDataObj) { - struct bcos_sdk_c_transaction_data* tx_data_struct = - convert_to_tx_data_struct(env, jTransactionDataObj); - if (tx_data_struct == NULL) + if (jTransactionDataObj == NULL) { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); - return nullptr; + THROW_JNI_EXCEPTION(env, "transactionDataObject is nullptr"); + return NULL; + } + const char* tx_data_hex = nullptr; + if (env->IsInstanceOf(jTransactionDataObj, + env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2")) == JNI_TRUE) + { + bcos_sdk_c_transaction_data_v2* tx_data_struct = + convert_to_tx_v2_data_struct(env, jTransactionDataObj); + if (tx_data_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); + return nullptr; + } + tx_data_hex = bcos_sdk_encode_transaction_data_struct_to_hex_v2(tx_data_struct); + if (tx_data_struct) + { + bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct); + } + } + else + { + struct bcos_sdk_c_transaction_data* tx_data_struct = + convert_to_tx_data_struct(env, jTransactionDataObj); + if (tx_data_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); + return nullptr; + } + tx_data_hex = bcos_sdk_encode_transaction_data_struct_to_hex(tx_data_struct); + if (tx_data_struct) + { + bcos_sdk_destroy_transaction_data_struct(tx_data_struct); + } } - const char* tx_data_hex = bcos_sdk_encode_transaction_data_struct_to_hex(tx_data_struct); if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); @@ -418,10 +812,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr free((void*)tx_data_hex); tx_data_hex = NULL; } - if (tx_data_struct) - { - bcos_sdk_destroy_transaction_data_struct(tx_data_struct); - } + return jTxDataHex; } @@ -435,14 +826,43 @@ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTransactionDataStructToJson( JNIEnv* env, jclass, jobject jTransactionDataObj) { - struct bcos_sdk_c_transaction_data* tx_data_struct = - convert_to_tx_data_struct(env, jTransactionDataObj); - if (tx_data_struct == NULL) + if (jTransactionDataObj == NULL) { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); - return nullptr; + THROW_JNI_EXCEPTION(env, "transactionDataObject is nullptr"); + return NULL; + } + const char* tx_data_json = nullptr; + if (env->IsInstanceOf(jTransactionDataObj, + env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2")) == JNI_TRUE) + { + bcos_sdk_c_transaction_data_v2* tx_data_struct = + convert_to_tx_v2_data_struct(env, jTransactionDataObj); + if (tx_data_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); + return nullptr; + } + tx_data_json = bcos_sdk_encode_transaction_data_struct_to_json_v2(tx_data_struct); + if (tx_data_struct) + { + bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct); + } + } + else + { + struct bcos_sdk_c_transaction_data* tx_data_struct = + convert_to_tx_data_struct(env, jTransactionDataObj); + if (tx_data_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); + return nullptr; + } + tx_data_json = bcos_sdk_encode_transaction_data_struct_to_json(tx_data_struct); + if (tx_data_struct) + { + bcos_sdk_destroy_transaction_data_struct(tx_data_struct); + } } - const char* tx_data_json = bcos_sdk_encode_transaction_data_struct_to_json(tx_data_struct); if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); @@ -456,10 +876,6 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr free((void*)tx_data_json); tx_data_json = NULL; } - if (tx_data_struct) - { - bcos_sdk_destroy_transaction_data_struct(tx_data_struct); - } return jTxDataJson; } @@ -473,7 +889,7 @@ JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionDataStruct( JNIEnv* env, jclass, jstring jTxDataHexStr) { - checkJString(env, jTxDataHexStr); + CHECK_OBJECT_NOT_NULL(env, jTxDataHexStr, nullptr); const char* tx_data_hex_str = env->GetStringUTFChars(jTxDataHexStr, nullptr); struct bcos_sdk_c_transaction_data* tx_data_struct = bcos_sdk_decode_transaction_data_struct_from_hex(tx_data_hex_str); @@ -500,6 +916,43 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTr return jTxDataObj; } +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: decodeTransactionDataStructV2 + * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2; + */ +JNIEXPORT jobject JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionDataStructV2( + JNIEnv* env, jclass, jstring jTxDataHexStr) +{ + CHECK_OBJECT_NOT_NULL(env, jTxDataHexStr, nullptr); + + const char* tx_data_hex_str = env->GetStringUTFChars(jTxDataHexStr, nullptr); + struct bcos_sdk_c_transaction_data_v2* tx_data_struct_v2 = + bcos_sdk_decode_transaction_data_struct_from_hex_v2(tx_data_hex_str); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return nullptr; + } + + jobject jTxDataObj = convert_to_tx_v2_data_jobject(env, tx_data_struct_v2); + if (jTxDataObj == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_v2_data_jobject"); + return nullptr; + } + + // release source + if (tx_data_struct_v2) + { + bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct_v2); + } + env->ReleaseStringUTFChars(jTxDataHexStr, tx_data_hex_str); + + return jTxDataObj; +} + /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj * Method: calcTransactionDataStructHash @@ -509,16 +962,44 @@ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_calcTransactionDataStructHash( JNIEnv* env, jclass, jint jCrytpTyte, jobject jTransactionDataObj) { - struct bcos_sdk_c_transaction_data* tx_data_struct = - convert_to_tx_data_struct(env, jTransactionDataObj); - if (tx_data_struct == NULL) + if (jTransactionDataObj == NULL) { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); - return nullptr; + THROW_JNI_EXCEPTION(env, "transactionDataObject is nullptr"); + return NULL; } + const char* tx_data_hash = nullptr; int crypto_type = jCrytpTyte; - const char* tx_data_hash = - bcos_sdk_calc_transaction_data_struct_hash(crypto_type, tx_data_struct); + if (env->IsInstanceOf(jTransactionDataObj, + env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2")) == JNI_TRUE) + { + bcos_sdk_c_transaction_data_v2* tx_data_struct = + convert_to_tx_v2_data_struct(env, jTransactionDataObj); + if (tx_data_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); + return nullptr; + } + tx_data_hash = bcos_sdk_calc_transaction_data_struct_hash_v2(crypto_type, tx_data_struct); + if (tx_data_struct) + { + bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct); + } + } + else + { + struct bcos_sdk_c_transaction_data* tx_data_struct = + convert_to_tx_data_struct(env, jTransactionDataObj); + if (tx_data_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); + return nullptr; + } + tx_data_hash = bcos_sdk_calc_transaction_data_struct_hash(crypto_type, tx_data_struct); + if (tx_data_struct) + { + bcos_sdk_destroy_transaction_data_struct(tx_data_struct); + } + } if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); @@ -528,10 +1009,6 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_calcTran jstring jTxDataHash = env->NewStringUTF(tx_data_hash); // release source - if (tx_data_struct) - { - bcos_sdk_destroy_transaction_data_struct(tx_data_struct); - } if (tx_data_hash) { free((void*)tx_data_hash); @@ -552,23 +1029,51 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_createEn JNIEnv* env, jclass, jobject jTxDataObj, jstring jSignature, jstring jTxDataHash, jint jAttribute, jstring jExtraData) { - checkJString(env, jSignature); - checkJString(env, jTxDataHash); - checkJString(env, jExtraData); + CHECK_OBJECT_NOT_NULL(env, jTxDataObj, nullptr); + CHECK_OBJECT_NOT_NULL(env, jSignature, nullptr); + CHECK_OBJECT_NOT_NULL(env, jTxDataHash, nullptr); + CHECK_OBJECT_NOT_NULL(env, jExtraData, nullptr); - struct bcos_sdk_c_transaction_data* tx_data_struct = convert_to_tx_data_struct(env, jTxDataObj); - if (tx_data_struct == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); - return nullptr; - } + const char* tx_str = nullptr; const char* signature = env->GetStringUTFChars(jSignature, NULL); const char* tx_data_hash = env->GetStringUTFChars(jTxDataHash, NULL); int attribute = jAttribute; const char* extra_data = env->GetStringUTFChars(jExtraData, NULL); + if (env->IsInstanceOf(jTxDataObj, + env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2")) == JNI_TRUE) + { + bcos_sdk_c_transaction_data_v2* tx_data_struct = + convert_to_tx_v2_data_struct(env, jTxDataObj); + if (tx_data_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); + return nullptr; + } + tx_str = bcos_sdk_create_encoded_transaction_v2( + tx_data_struct, signature, tx_data_hash, attribute, extra_data); + if (tx_data_struct) + { + bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct); + } + } + else + { + struct bcos_sdk_c_transaction_data* tx_data_struct = + convert_to_tx_data_struct(env, jTxDataObj); + if (tx_data_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); + return nullptr; + } + tx_str = bcos_sdk_create_encoded_transaction( + tx_data_struct, signature, tx_data_hash, attribute, extra_data); + if (tx_data_struct) + { + bcos_sdk_destroy_transaction_data_struct(tx_data_struct); + } + } + - const char* tx_str = bcos_sdk_create_encoded_transaction( - tx_data_struct, signature, tx_data_hash, attribute, extra_data); if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); @@ -581,10 +1086,6 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_createEn env->ReleaseStringUTFChars(jSignature, signature); env->ReleaseStringUTFChars(jTxDataHash, tx_data_hash); env->ReleaseStringUTFChars(jExtraData, extra_data); - if (tx_data_struct) - { - bcos_sdk_destroy_transaction_data_struct(tx_data_struct); - } if (tx_str) { free((void*)tx_str); @@ -603,13 +1104,43 @@ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTransactionStruct( JNIEnv* env, jclass, jobject jTransactionObj) { - struct bcos_sdk_c_transaction* tx_struct = convert_to_tx_struct(env, jTransactionObj); - if (tx_struct == NULL) + CHECK_OBJECT_NOT_NULL(env, jTransactionObj, nullptr); + + jclass javaTxClass = env->GetObjectClass(jTransactionObj); + jfieldID transactionDataField = env->GetFieldID( + javaTxClass, "transactionData", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;"); + jobject javaTxDataObj = env->GetObjectField(jTransactionObj, transactionDataField); + CHECK_OBJECT_NOT_NULL(env, javaTxDataObj, nullptr); + const char* tx_hex = nullptr; + if (env->IsInstanceOf(javaTxDataObj, + env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2")) == JNI_TRUE) { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct"); - return nullptr; + bcos_sdk_c_transaction_v2* tx_struct = convert_to_tx_v2_struct(env, jTransactionObj); + if (tx_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct"); + return nullptr; + } + tx_hex = bcos_sdk_encode_transaction_struct_to_hex_v2(tx_struct); + if (tx_struct) + { + bcos_sdk_destroy_transaction_struct_v2(tx_struct); + } + } + else + { + struct bcos_sdk_c_transaction* tx_struct = convert_to_tx_struct(env, jTransactionObj); + if (tx_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct"); + return nullptr; + } + tx_hex = bcos_sdk_encode_transaction_struct_to_hex(tx_struct); + if (tx_struct) + { + bcos_sdk_destroy_transaction_struct(tx_struct); + } } - const char* tx_hex = bcos_sdk_encode_transaction_struct_to_hex(tx_struct); if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); @@ -624,10 +1155,6 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr free((void*)tx_hex); tx_hex = NULL; } - if (tx_struct) - { - bcos_sdk_destroy_transaction_struct(tx_struct); - } return jTxHex; } @@ -641,13 +1168,42 @@ JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTransactionStructToJson( JNIEnv* env, jclass, jobject jTransactionObj) { - struct bcos_sdk_c_transaction* tx_struct = convert_to_tx_struct(env, jTransactionObj); - if (tx_struct == NULL) + CHECK_OBJECT_NOT_NULL(env, jTransactionObj, nullptr); + jclass javaTxClass = env->GetObjectClass(jTransactionObj); + jfieldID transactionDataField = env->GetFieldID( + javaTxClass, "transactionData", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionData;"); + jobject javaTxDataObj = env->GetObjectField(jTransactionObj, transactionDataField); + CHECK_OBJECT_NOT_NULL(env, javaTxDataObj, nullptr); + const char* tx_json = nullptr; + if (env->IsInstanceOf(javaTxDataObj, + env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2")) == JNI_TRUE) { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct"); - return nullptr; + bcos_sdk_c_transaction_v2* tx_struct = convert_to_tx_v2_struct(env, jTransactionObj); + if (tx_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct"); + return nullptr; + } + tx_json = bcos_sdk_encode_transaction_struct_to_json_v2(tx_struct); + if (tx_struct) + { + bcos_sdk_destroy_transaction_struct_v2(tx_struct); + } + } + else + { + struct bcos_sdk_c_transaction* tx_struct = convert_to_tx_struct(env, jTransactionObj); + if (tx_struct == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct"); + return nullptr; + } + tx_json = bcos_sdk_encode_transaction_struct_to_json(tx_struct); + if (tx_struct) + { + bcos_sdk_destroy_transaction_struct(tx_struct); + } } - const char* tx_json = bcos_sdk_encode_transaction_struct_to_json(tx_struct); if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); @@ -661,10 +1217,6 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr free((void*)tx_json); tx_json = NULL; } - if (tx_struct) - { - bcos_sdk_destroy_transaction_struct(tx_struct); - } return jTxJson; } @@ -678,7 +1230,7 @@ JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionStruct( JNIEnv* env, jclass, jstring jTxHexStr) { - checkJString(env, jTxHexStr); + CHECK_OBJECT_NOT_NULL(env, jTxHexStr, nullptr); const char* tx_hex_str = env->GetStringUTFChars(jTxHexStr, nullptr); struct bcos_sdk_c_transaction* tx_struct = @@ -705,3 +1257,40 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTr return jTxObj; } + +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: decodeTransactionStructV2 + * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionV2; + */ +JNIEXPORT jobject JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionStructV2( + JNIEnv* env, jclass, jstring jTxHexStr) +{ + CHECK_OBJECT_NOT_NULL(env, jTxHexStr, nullptr); + + const char* tx_hex_str = env->GetStringUTFChars(jTxHexStr, nullptr); + struct bcos_sdk_c_transaction_v2* tx_struct_v2 = + bcos_sdk_decode_transaction_struct_from_hex_v2(tx_hex_str); + if (!bcos_sdk_is_last_opr_success()) + { + THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); + return nullptr; + } + + jobject jTxObj = convert_to_tx_v2_jobject(env, tx_struct_v2); + if (jTxObj == NULL) + { + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_v2_jobject"); + return nullptr; + } + + // release source + if (tx_struct_v2) + { + bcos_sdk_destroy_transaction_struct_v2(tx_struct_v2); + } + env->ReleaseStringUTFChars(jTxHexStr, tx_hex_str); + + return jTxObj; +} diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.h b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.h index ba591e8a8..82048c474 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.h +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.h @@ -34,6 +34,15 @@ JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionDataStruct( JNIEnv*, jclass, jstring); +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: decodeTransactionDataStructV2 + * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2; + */ +JNIEXPORT jobject JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionDataStructV2( + JNIEnv*, jclass, jstring); + /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj * Method: calcTransactionDataStructHash @@ -80,6 +89,15 @@ JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionStruct( JNIEnv*, jclass, jstring); +/* + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj + * Method: decodeTransactionStructV2 + * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/Transaction; + */ +JNIEXPORT jobject JNICALL +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionStructV2( + JNIEnv*, jclass, jstring); + #ifdef __cplusplus } #endif diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/Transaction.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/Transaction.java index be12e487c..c5a8adbf7 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/Transaction.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/Transaction.java @@ -2,9 +2,9 @@ public class Transaction { private TransactionData transactionData; - private TransactionBytes dataHash; - private TransactionBytes signature; - private TransactionBytes sender; + private byte[] dataHash; + private byte[] signature; + private byte[] sender; private long importTime; private int attribute; private String extraData; @@ -17,27 +17,27 @@ public void setTransactionData(TransactionData transactionData) { this.transactionData = transactionData; } - public TransactionBytes getDataHash() { + public byte[] getDataHash() { return dataHash; } - public void setDataHash(TransactionBytes dataHash) { + public void setDataHash(byte[] dataHash) { this.dataHash = dataHash; } - public TransactionBytes getSignature() { + public byte[] getSignature() { return signature; } - public void setSignature(TransactionBytes signature) { + public void setSignature(byte[] signature) { this.signature = signature; } - public TransactionBytes getSender() { + public byte[] getSender() { return sender; } - public void setSender(TransactionBytes sender) { + public void setSender(byte[] sender) { this.sender = sender; } diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes.java deleted file mode 100644 index 09e2b1495..000000000 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes.java +++ /dev/null @@ -1,22 +0,0 @@ -package org.fisco.bcos.sdk.jni.utilities.tx; - -public class TransactionBytes { - private byte[] buffer; - private int length; - - public byte[] getBuffer() { - return buffer; - } - - public void setBuffer(byte[] buffer) { - this.buffer = buffer; - } - - public int getLength() { - return length; - } - - public void setLength(int length) { - this.length = length; - } -} diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionData.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionData.java index 6baea446e..50f6af128 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionData.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionData.java @@ -1,14 +1,14 @@ package org.fisco.bcos.sdk.jni.utilities.tx; public class TransactionData { - private int version; - private long blockLimit; - private String chainId; - private String groupId; - private String nonce; - private String to; - private String abi; - private TransactionBytes input; + protected int version; + protected long blockLimit; + protected String chainId; + protected String groupId; + protected String nonce; + protected String to; + protected String abi; + protected byte[] input; public int getVersion() { return version; @@ -66,11 +66,11 @@ public void setAbi(String abi) { this.abi = abi; } - public TransactionBytes getInput() { + public byte[] getInput() { return input; } - public void setInput(TransactionBytes input) { + public void setInput(byte[] input) { this.input = input; } } diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2.java index 290a1776a..e83bb4796 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2.java @@ -1,83 +1,11 @@ package org.fisco.bcos.sdk.jni.utilities.tx; -public class TransactionDataV2 { - private int version; - private long blockLimit; - private String chainId; - private String groupId; - private String nonce; - private String to; - private String abi; - private TransactionBytes input; - private String value; - private String gasPrice; - private long gasLimit; - private String maxFeePerGas; - private String maxPriorityFeePerGas; - - public int getVersion() { - return version; - } - - public void setVersion(int version) { - this.version = version; - } - - public long getBlockLimit() { - return blockLimit; - } - - public void setBlockLimit(long blockLimit) { - this.blockLimit = blockLimit; - } - - public String getChainId() { - return chainId; - } - - public void setChainId(String chainId) { - this.chainId = chainId; - } - - public String getGroupId() { - return groupId; - } - - public void setGroupId(String groupId) { - this.groupId = groupId; - } - - public String getNonce() { - return nonce; - } - - public void setNonce(String nonce) { - this.nonce = nonce; - } - - public String getTo() { - return to; - } - - public void setTo(String to) { - this.to = to; - } - - public String getAbi() { - return abi; - } - - public void setAbi(String abi) { - this.abi = abi; - } - - public TransactionBytes getInput() { - return input; - } - - public void setInput(TransactionBytes input) { - this.input = input; - } +public class TransactionDataV2 extends TransactionData { + protected String value; + protected String gasPrice; + protected long gasLimit; + protected String maxFeePerGas; + protected String maxPriorityFeePerGas; public String getValue() { return value; diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderJniObj.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderJniObj.java index ec2c29e60..dfe34ac0e 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderJniObj.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderJniObj.java @@ -30,6 +30,13 @@ public static native String encodeTransactionDataStructToJson(TransactionData tr public static native TransactionData decodeTransactionDataStruct(String transactionDataHex) throws JniException; + /** + * @param transactionDataHex transactionData hex string + * @return TxData + */ + public static native TransactionDataV2 decodeTransactionDataStructV2(String transactionDataHex) + throws JniException; + /** * @param cryptoType crypto type * @param transactionData Transaction Data struct @@ -73,4 +80,11 @@ public static native String encodeTransactionStructToJson(Transaction transactio */ public static native Transaction decodeTransactionStruct(String transactionHex) throws JniException; + + /** + * @param transactionHex transaction hex string + * @return Tx + */ + public static native Transaction decodeTransactionStructV2(String transactionHex) + throws JniException; } diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderV2JniObj.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderV2JniObj.java deleted file mode 100644 index c1945f3d4..000000000 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderV2JniObj.java +++ /dev/null @@ -1,77 +0,0 @@ -package org.fisco.bcos.sdk.jni.utilities.tx; - -import org.fisco.bcos.sdk.jni.common.JniException; -import org.fisco.bcos.sdk.jni.common.JniLibLoader; - -public class TransactionStructBuilderV2JniObj { - - static { - JniLibLoader.loadJniLibrary(); - } - - /** - * @param TransactionDataV2 Transaction Data struct - * @return Hexed Transaction Data - */ - public static native String encodeTransactionDataStructV2(TransactionDataV2 transactionData) - throws JniException; - - /** - * @param TransactionDataV2 Transaction Data struct - * @return Json Transaction Data - */ - public static native String encodeTransactionDataStructToJsonV2(TransactionDataV2 transactionData) - throws JniException; - - /** - * @param transactionDataHex transactionData hex string - * @return TxData - */ - public static native TransactionDataV2 decodeTransactionDataStructV2(String transactionDataHex) - throws JniException; - - /** - * @param cryptoType crypto type - * @param transactionData Transaction Data struct - * @return Hash hex string - */ - public static native String calcTransactionDataStructHashV2( - int cryptoType, TransactionDataV2 transactionData) throws JniException; - - /** - * @param transactionData - * @param signature - * @param transactionDataHash - * @param attribute - * @param extraData - * @return signedTransaction string - */ - public static native String createEncodedTransactionV2( - TransactionDataV2 transactionData, - String signature, - String transactionDataHash, - int attribute, - String extraData) - throws JniException; - - /** - * @param transaction Transaction struct - * @return Hexed Transaction - */ - public static native String encodeTransactionStructV2(TransactionV2 transaction) - throws JniException; - - /** - * @param transaction Transaction struct - * @return Json Transaction - */ - public static native String encodeTransactionStructToJsonV2(TransactionV2 transaction) - throws JniException; - - /** - * @param transactionHex transaction hex string - * @return Tx - */ - public static native TransactionV2 decodeTransactionStructV2(String transactionHex) - throws JniException; -} diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionV2.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionV2.java deleted file mode 100644 index ca37b2009..000000000 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionV2.java +++ /dev/null @@ -1,67 +0,0 @@ -package org.fisco.bcos.sdk.jni.utilities.tx; - -public class TransactionV2 { - private TransactionDataV2 transactionData; - private TransactionBytes dataHash; - private TransactionBytes signature; - private TransactionBytes sender; - private long importTime; - private int attribute; - private String extraData; - - public TransactionDataV2 getTransactionData() { - return transactionData; - } - - public void setTransactionData(TransactionDataV2 transactionData) { - this.transactionData = transactionData; - } - - public TransactionBytes getDataHash() { - return dataHash; - } - - public void setDataHash(TransactionBytes dataHash) { - this.dataHash = dataHash; - } - - public TransactionBytes getSignature() { - return signature; - } - - public void setSignature(TransactionBytes signature) { - this.signature = signature; - } - - public TransactionBytes getSender() { - return sender; - } - - public void setSender(TransactionBytes sender) { - this.sender = sender; - } - - public long getImportTime() { - return importTime; - } - - public void setImportTime(long importTime) { - this.importTime = importTime; - } - - public int getAttribute() { - return attribute; - } - - public void setAttribute(int attribute) { - this.attribute = attribute; - } - - public String getExtraData() { - return extraData; - } - - public void setExtraData(String extraData) { - this.extraData = extraData; - } -} diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion.java index df0543acf..79d1d0a0b 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion.java @@ -1,16 +1,27 @@ package org.fisco.bcos.sdk.jni.utilities.tx; public enum TransactionVersion { - V0(0), - V1(1); + V0(0), + V1(1); - private int value; + private int value; - TransactionVersion(int value) { - this.value = value; - } + TransactionVersion(int value) { + this.value = value; + } - public int getValue() { - return value; - } + public int getValue() { + return value; + } + + public static TransactionVersion fromInt(int value) { + switch (value) { + case 0: + return V0; + case 1: + return V1; + default: + return V0; + } + } } diff --git a/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java b/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java index 81c8f77d0..e4fbf7120 100644 --- a/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java +++ b/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java @@ -2,6 +2,9 @@ import java.security.SecureRandom; import java.util.Arrays; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + import org.fisco.bcos.sdk.jni.BcosSDKJniObj; import org.fisco.bcos.sdk.jni.common.JniConfig; import org.fisco.bcos.sdk.jni.common.JniException; @@ -15,28 +18,28 @@ public class TestTxStruct { - // ------------------------------------------------------------------------------ - // ------------------------------------------------------------------------------ - - // HelloWorld Source Code: - - // HelloWorld Source Code: - /** - * pragma solidity>=0.4.24 <0.6.11; - * - *

contract HelloWorld { string name; - * - *

constructor() public { name = "Hello, World!"; } - * - *

function get() public view returns (string memory) { return name; } - * - *

function set(string memory n) public { name = n; } } - */ - private static final String hwBIN = - "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610235565b5050565b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561022b5780601f106102005761010080835404028352916020019161022b565b820191906000526020600020905b81548152906001019060200180831161020e57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea2646970667358221220b5943f43c48cc93c6d71cdcf27aee5072566c88755ce9186e32ce83b24e8dc6c64736f6c634300060a0033"; - - private static final String hwSmBIN = - "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063299f7f9d1461003b5780633590b49f146100be575b600080fd5b610043610179565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610083578082015181840152602081019050610068565b50505050905090810190601f1680156100b05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610177600480360360208110156100d457600080fd5b81019080803590602001906401000000008111156100f157600080fd5b82018360208201111561010357600080fd5b8035906020019184600183028401116401000000008311171561012557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061021b565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102115780601f106101e657610100808354040283529160200191610211565b820191906000526020600020905b8154815290600101906020018083116101f457829003601f168201915b5050505050905090565b8060009080519060200190610231929190610235565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea26469706673582212209871cb2bcf390d53645807cbaedfe052d739ef9cff9d84787f74c4f379e1854664736f6c634300060a0033"; + // ------------------------------------------------------------------------------ + // ------------------------------------------------------------------------------ + + // HelloWorld Source Code: + + // HelloWorld Source Code: + /** + * pragma solidity>=0.4.24 <0.6.11; + * + *

contract HelloWorld { string name; + * + *

constructor() public { name = "Hello, World!"; } + * + *

function get() public view returns (string memory) { return name; } + * + *

function set(string memory n) public { name = n; } } + */ + private static final String hwBIN = + "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610235565b5050565b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561022b5780601f106102005761010080835404028352916020019161022b565b820191906000526020600020905b81548152906001019060200180831161020e57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea2646970667358221220b5943f43c48cc93c6d71cdcf27aee5072566c88755ce9186e32ce83b24e8dc6c64736f6c634300060a0033"; + + private static final String hwSmBIN = + "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063299f7f9d1461003b5780633590b49f146100be575b600080fd5b610043610179565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610083578082015181840152602081019050610068565b50505050905090810190601f1680156100b05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610177600480360360208110156100d457600080fd5b81019080803590602001906401000000008111156100f157600080fd5b82018360208201111561010357600080fd5b8035906020019184600183028401116401000000008311171561012557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061021b565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102115780601f106101e657610100808354040283529160200191610211565b820191906000526020600020905b8154815290600101906020018083116101f457829003601f168201915b5050505050905090565b8060009080519060200190610231929190610235565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea26469706673582212209871cb2bcf390d53645807cbaedfe052d739ef9cff9d84787f74c4f379e1854664736f6c634300060a0033"; /* { @@ -50,200 +53,188 @@ public class TestTxStruct { } */ - public static byte[] fromHex(String hexString) { - if (hexString.startsWith("0x")) { - hexString = hexString.substring(2); - } + public static byte[] fromHex(String hexString) { + if (hexString.startsWith("0x")) { + hexString = hexString.substring(2); + } - if (hexString.length() % 2 != 0) { - throw new IllegalArgumentException("Invalid hex string: " + hexString); - } + if (hexString.length() % 2 != 0) { + throw new IllegalArgumentException("Invalid hex string: " + hexString); + } - int length = hexString.length() / 2; - byte[] bytes = new byte[length]; + int length = hexString.length() / 2; + byte[] bytes = new byte[length]; - for (int i = 0; i < length; i++) { - String twoChars = hexString.substring(i * 2, i * 2 + 2); - bytes[i] = (byte) Integer.parseInt(twoChars, 16); - } + for (int i = 0; i < length; i++) { + String twoChars = hexString.substring(i * 2, i * 2 + 2); + bytes[i] = (byte) Integer.parseInt(twoChars, 16); + } - return bytes; - } - - public static String generateNonce() { - byte[] nonceBytes = new byte[16]; - SecureRandom secureRandom = new SecureRandom(); - secureRandom.nextBytes(nonceBytes); - StringBuilder hex = new StringBuilder(); - for (byte b : nonceBytes) { - hex.append(String.format("%02x", b)); + return bytes; } - return hex.toString(); - } - public static String getBinary(boolean isSM) { - return isSM ? hwSmBIN : hwBIN; - } + public static String generateNonce() { + byte[] nonceBytes = new byte[16]; + SecureRandom secureRandom = new SecureRandom(); + secureRandom.nextBytes(nonceBytes); + StringBuilder hex = new StringBuilder(); + for (byte b : nonceBytes) { + hex.append(String.format("%02x", b)); + } + return hex.toString(); + } - public static void Usage() { - System.out.println("Desc: test transaction struct [HelloWorld set]"); - System.out.println( - "Usage: java -cp 'conf/:lib/*:apps/*' org.fisco.bcos.sdk.jni.test.tx.TestTxStruct"); - System.exit(0); - } + public static String getBinary(boolean isSM) { + return isSM ? hwSmBIN : hwBIN; + } - public static void main(String[] args) throws JniException, InterruptedException { - if (args.length > 1) { - Usage(); + public static void Usage() { + System.out.println("Desc: test transaction struct [HelloWorld set]"); + System.out.println( + "Usage: java -cp 'conf/:lib/*:apps/*' org.fisco.bcos.sdk.jni.test.tx.TestTxStruct"); + System.exit(0); } - String endpoint = "127.0.0.1:20200"; - String group = "group0"; - String node = ""; - JniConfig jniConfig = Utility.newJniConfig(Arrays.asList(endpoint)); - jniConfig.setDisableSsl(true); - BcosSDKJniObj bcosSDKJni = BcosSDKJniObj.build(jniConfig); - RpcJniObj rpcJniObj = RpcJniObj.build(bcosSDKJni.getNativePointer()); - System.out.println("build Rpc"); - rpcJniObj.start(); - - boolean smCrypto = false; - - long keyPair = KeyPairJniObj.createJniKeyPair(smCrypto ? 1 : 0); - String jniKeyPairAddress = KeyPairJniObj.getJniKeyPairAddress(keyPair); - - long blockLimit = 1111; - String groupID = "group0"; - String chainID = "chain0"; - String data = getBinary(smCrypto); - System.out.printf(" [test Tx Struct] new account, address: %s\n", jniKeyPairAddress); - - // construct TransactionData - TransactionData transactionDataStruct = new TransactionData(); - transactionDataStruct.setGroupId(groupID); - transactionDataStruct.setChainId(chainID); - transactionDataStruct.setTo(""); - transactionDataStruct.setAbi(""); - transactionDataStruct.setVersion(0); - transactionDataStruct.setNonce(generateNonce()); - transactionDataStruct.setBlockLimit(blockLimit); - // input - TransactionBytes inputBytes = new TransactionBytes(); - byte[] bytesInput = fromHex(data); - inputBytes.setBuffer(bytesInput); - inputBytes.setLength(bytesInput.length); - transactionDataStruct.setInput(inputBytes); - - // encode TxData to hex tx data - String txDataHex = - TransactionStructBuilderJniObj.encodeTransactionDataStruct(transactionDataStruct); - // decode hex tx data to TxData - TransactionData decodeTransactionDataStructHex = - TransactionStructBuilderJniObj.decodeTransactionDataStruct(txDataHex); - // assert - Assert.assertEquals( - transactionDataStruct.getChainId(), decodeTransactionDataStructHex.getChainId()); - Assert.assertEquals( - transactionDataStruct.getGroupId(), decodeTransactionDataStructHex.getGroupId()); - Assert.assertEquals(transactionDataStruct.getAbi(), decodeTransactionDataStructHex.getAbi()); - Assert.assertEquals( - transactionDataStruct.getBlockLimit(), decodeTransactionDataStructHex.getBlockLimit()); - - // encode TxData to json tx data - String txDataJson = - TransactionStructBuilderJniObj.encodeTransactionDataStructToJson(transactionDataStruct); - System.out.printf(" [test Tx Struct] txDataJson: %s\n", txDataJson); - - // calc tx data hash - String txDataHash = - TransactionStructBuilderJniObj.calcTransactionDataStructHash( - smCrypto ? 1 : 0, decodeTransactionDataStructHex); - System.out.printf(" [test Tx Struct] txDataHash: %s\n", txDataHash); - // signature tx data hash - String signature = TransactionBuilderJniObj.signTransactionDataHash(keyPair, txDataHash); - System.out.printf(" [test Tx Struct] signature: %s\n", signature); - - // construct tx - Transaction transactionStruct = new Transaction(); - TransactionBytes dataHashBytes = new TransactionBytes(); - dataHashBytes.setBuffer(txDataHash.getBytes()); - dataHashBytes.setLength(txDataHash.getBytes().length); - TransactionBytes signatureBytes = new TransactionBytes(); - signatureBytes.setBuffer(signature.getBytes()); - signatureBytes.setLength(signature.getBytes().length); - transactionStruct.setTransactionData(decodeTransactionDataStructHex); - transactionStruct.setDataHash(dataHashBytes); - transactionStruct.setSignature(signatureBytes); - transactionStruct.setSender(null); - transactionStruct.setImportTime(0); - transactionStruct.setAttribute(0); - transactionStruct.setExtraData(""); - // assert - Assert.assertEquals( - transactionStruct.getTransactionData().getBlockLimit(), - decodeTransactionDataStructHex.getBlockLimit()); - Assert.assertEquals( - transactionStruct.getTransactionData().getGroupId(), - decodeTransactionDataStructHex.getGroupId()); - Assert.assertEquals( - transactionStruct.getTransactionData().getChainId(), - decodeTransactionDataStructHex.getChainId()); - Assert.assertEquals( - transactionStruct.getTransactionData().getAbi(), decodeTransactionDataStructHex.getAbi()); - Assert.assertArrayEquals(transactionStruct.getDataHash().getBuffer(), txDataHash.getBytes()); - Assert.assertArrayEquals(transactionStruct.getSignature().getBuffer(), signature.getBytes()); - - // encode Tx to hex tx - String txHex = TransactionStructBuilderJniObj.encodeTransactionStruct(transactionStruct); - // decode hex tx to Tx - Transaction decodeTransactionStructHex = - TransactionStructBuilderJniObj.decodeTransactionStruct(txHex); - // assert - Assert.assertEquals( - transactionStruct.getTransactionData().getBlockLimit(), - decodeTransactionStructHex.getTransactionData().getBlockLimit()); - Assert.assertEquals( - transactionStruct.getTransactionData().getGroupId(), - decodeTransactionStructHex.getTransactionData().getGroupId()); - Assert.assertEquals( - transactionStruct.getTransactionData().getChainId(), - decodeTransactionStructHex.getTransactionData().getChainId()); - Assert.assertEquals( - transactionStruct.getTransactionData().getAbi(), - decodeTransactionStructHex.getTransactionData().getAbi()); - Assert.assertArrayEquals( - transactionStruct.getDataHash().getBuffer(), - decodeTransactionStructHex.getDataHash().getBuffer()); - Assert.assertArrayEquals( - transactionStruct.getSignature().getBuffer(), - decodeTransactionStructHex.getSignature().getBuffer()); - - // encode Tx to json tx - String txJson = TransactionStructBuilderJniObj.encodeTransactionStructToJson(transactionStruct); - System.out.printf(" [test Tx Struct] txJson: %s\n", txJson); - // create tx string - String txString = - TransactionStructBuilderJniObj.createEncodedTransaction( - decodeTransactionDataStructHex, signature, txDataHash, 0, ""); - // System.out.printf(" [test Tx Struct] txString: %s\n", txString); - - // rpc send tx - rpcJniObj.sendTransaction( - group, - node, - txString, - false, - new RpcCallback() { - @Override - public void onResponse(Response response) { - System.out.println("response error code: ==>>> " + response.getErrorCode()); - String dataStr = new String(response.getData()); - System.out.println("response data: ==>>> " + dataStr); - } - }); - - Thread.sleep(2000); - rpcJniObj.stop(); - System.out.printf(" [test Tx Struct] finish !! \n"); - } + public static void main(String[] args) throws JniException, InterruptedException, ExecutionException { + if (args.length > 1) { + Usage(); + } + + String endpoint = "127.0.0.1:20200"; + String group = "group0"; + String node = ""; + JniConfig jniConfig = Utility.newJniConfig(Arrays.asList(endpoint)); + jniConfig.setDisableSsl(true); + BcosSDKJniObj bcosSDKJni = BcosSDKJniObj.build(jniConfig); + RpcJniObj rpcJniObj = RpcJniObj.build(bcosSDKJni.getNativePointer()); + System.out.println("build Rpc"); + rpcJniObj.start(); + + boolean smCrypto = false; + + long keyPair = KeyPairJniObj.createJniKeyPair(smCrypto ? 1 : 0); + String jniKeyPairAddress = KeyPairJniObj.getJniKeyPairAddress(keyPair); + + long blockLimit = 1111; + String groupID = "group0"; + String chainID = "chain0"; + String data = getBinary(smCrypto); + System.out.printf(" [test Tx Struct] new account, address: %s\n", jniKeyPairAddress); + + // construct TransactionData + TransactionData transactionDataStruct = new TransactionData(); + transactionDataStruct.setGroupId(groupID); + transactionDataStruct.setChainId(chainID); + transactionDataStruct.setTo(""); + transactionDataStruct.setAbi(""); + transactionDataStruct.setVersion(0); + transactionDataStruct.setNonce(generateNonce()); + transactionDataStruct.setBlockLimit(blockLimit); + // input + byte[] bytesInput = fromHex(data); + transactionDataStruct.setInput(bytesInput); + + // encode TxData to hex tx data + String txDataHex = + TransactionStructBuilderJniObj.encodeTransactionDataStruct(transactionDataStruct); + // decode hex tx data to TxData + TransactionData decodeTransactionDataStructHex = + TransactionStructBuilderJniObj.decodeTransactionDataStruct(txDataHex); + // assert + Assert.assertEquals( + transactionDataStruct.getChainId(), decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals( + transactionDataStruct.getGroupId(), decodeTransactionDataStructHex.getGroupId()); + Assert.assertEquals(transactionDataStruct.getAbi(), decodeTransactionDataStructHex.getAbi()); + Assert.assertEquals( + transactionDataStruct.getBlockLimit(), decodeTransactionDataStructHex.getBlockLimit()); + + // encode TxData to json tx data + String txDataJson = + TransactionStructBuilderJniObj.encodeTransactionDataStructToJson(transactionDataStruct); + System.out.printf(" [test Tx Struct] txDataJson: %s\n", txDataJson); + + // calc tx data hash + String txDataHash = + TransactionStructBuilderJniObj.calcTransactionDataStructHash( + smCrypto ? 1 : 0, decodeTransactionDataStructHex); + System.out.printf(" [test Tx Struct] txDataHash: %s\n", txDataHash); + // signature tx data hash + String signature = TransactionBuilderJniObj.signTransactionDataHash(keyPair, txDataHash); + System.out.printf(" [test Tx Struct] signature: %s\n", signature); + + // construct tx + Transaction transactionStruct = new Transaction(); + transactionStruct.setTransactionData(decodeTransactionDataStructHex); + transactionStruct.setDataHash(fromHex(txDataHash)); + transactionStruct.setSignature(fromHex(signature)); + transactionStruct.setSender(null); + transactionStruct.setImportTime(0); + transactionStruct.setAttribute(0); + transactionStruct.setExtraData(""); + // assert + Assert.assertEquals( + transactionStruct.getTransactionData().getBlockLimit(), + decodeTransactionDataStructHex.getBlockLimit()); + Assert.assertEquals( + transactionStruct.getTransactionData().getGroupId(), + decodeTransactionDataStructHex.getGroupId()); + Assert.assertEquals( + transactionStruct.getTransactionData().getChainId(), + decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals( + transactionStruct.getTransactionData().getAbi(), decodeTransactionDataStructHex.getAbi()); + Assert.assertArrayEquals(transactionStruct.getDataHash(), fromHex(txDataHash)); + Assert.assertArrayEquals(transactionStruct.getSignature(), fromHex(signature)); + + // encode Tx to hex tx + String txHex = TransactionStructBuilderJniObj.encodeTransactionStruct(transactionStruct); + // decode hex tx to Tx + Transaction decodeTransactionStructHex = + TransactionStructBuilderJniObj.decodeTransactionStruct(txHex); + // assert + Assert.assertEquals( + transactionStruct.getTransactionData().getBlockLimit(), + decodeTransactionStructHex.getTransactionData().getBlockLimit()); + Assert.assertEquals( + transactionStruct.getTransactionData().getGroupId(), + decodeTransactionStructHex.getTransactionData().getGroupId()); + Assert.assertEquals( + transactionStruct.getTransactionData().getChainId(), + decodeTransactionStructHex.getTransactionData().getChainId()); + Assert.assertEquals( + transactionStruct.getTransactionData().getAbi(), + decodeTransactionStructHex.getTransactionData().getAbi()); + Assert.assertArrayEquals( + transactionStruct.getDataHash(), + decodeTransactionStructHex.getDataHash()); + Assert.assertArrayEquals( + transactionStruct.getSignature(), + decodeTransactionStructHex.getSignature()); + + // encode Tx to json tx + String txJson = TransactionStructBuilderJniObj.encodeTransactionStructToJson(transactionStruct); + System.out.printf(" [test Tx Struct] txJson: %s\n", txJson); + // create tx string + String txString = + TransactionStructBuilderJniObj.createEncodedTransaction( + decodeTransactionDataStructHex, signature, txDataHash, 0, ""); + // System.out.printf(" [test Tx Struct] txString: %s\n", txString); + + CompletableFuture future = new CompletableFuture<>(); + // rpc send tx + rpcJniObj.sendTransaction( + group, + node, + txString, + false, + future::complete); + + Response response = future.get(); + System.out.println("response error code: ==>>> " + response.getErrorCode()); + String dataStr = new String(response.getData()); + System.out.println("response data: ==>>> " + dataStr); + rpcJniObj.stop(); + System.out.println(" [test Tx Struct] finish !!"); + } } diff --git a/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV2.java b/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV2.java index 4f304dede..16db16885 100644 --- a/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV2.java +++ b/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV2.java @@ -2,6 +2,9 @@ import java.security.SecureRandom; import java.util.Arrays; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; + import org.fisco.bcos.sdk.jni.BcosSDKJniObj; import org.fisco.bcos.sdk.jni.common.JniConfig; import org.fisco.bcos.sdk.jni.common.JniException; @@ -15,26 +18,26 @@ public class TestTxStructV2 { - // ------------------------------------------------------------------------------ - // ------------------------------------------------------------------------------ - - // HelloWorld Source Code: - /** - * pragma solidity>=0.4.24 <0.6.11; - * - *

contract HelloWorld { string name; - * - *

constructor() public { name = "Hello, World!"; } - * - *

function get() public view returns (string memory) { return name; } - * - *

function set(string memory n) public { name = n; } } - */ - private static final String hwBIN = - "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610235565b5050565b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561022b5780601f106102005761010080835404028352916020019161022b565b820191906000526020600020905b81548152906001019060200180831161020e57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea2646970667358221220b5943f43c48cc93c6d71cdcf27aee5072566c88755ce9186e32ce83b24e8dc6c64736f6c634300060a0033"; - - private static final String hwSmBIN = - "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063299f7f9d1461003b5780633590b49f146100be575b600080fd5b610043610179565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610083578082015181840152602081019050610068565b50505050905090810190601f1680156100b05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610177600480360360208110156100d457600080fd5b81019080803590602001906401000000008111156100f157600080fd5b82018360208201111561010357600080fd5b8035906020019184600183028401116401000000008311171561012557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061021b565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102115780601f106101e657610100808354040283529160200191610211565b820191906000526020600020905b8154815290600101906020018083116101f457829003601f168201915b5050505050905090565b8060009080519060200190610231929190610235565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea26469706673582212209871cb2bcf390d53645807cbaedfe052d739ef9cff9d84787f74c4f379e1854664736f6c634300060a0033"; + // ------------------------------------------------------------------------------ + // ------------------------------------------------------------------------------ + + // HelloWorld Source Code: + /** + * pragma solidity>=0.4.24 <0.6.11; + * + *

contract HelloWorld { string name; + * + *

constructor() public { name = "Hello, World!"; } + * + *

function get() public view returns (string memory) { return name; } + * + *

function set(string memory n) public { name = n; } } + */ + private static final String hwBIN = + "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610235565b5050565b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561022b5780601f106102005761010080835404028352916020019161022b565b820191906000526020600020905b81548152906001019060200180831161020e57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea2646970667358221220b5943f43c48cc93c6d71cdcf27aee5072566c88755ce9186e32ce83b24e8dc6c64736f6c634300060a0033"; + + private static final String hwSmBIN = + "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063299f7f9d1461003b5780633590b49f146100be575b600080fd5b610043610179565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610083578082015181840152602081019050610068565b50505050905090810190601f1680156100b05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610177600480360360208110156100d457600080fd5b81019080803590602001906401000000008111156100f157600080fd5b82018360208201111561010357600080fd5b8035906020019184600183028401116401000000008311171561012557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061021b565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102115780601f106101e657610100808354040283529160200191610211565b820191906000526020600020905b8154815290600101906020018083116101f457829003601f168201915b5050505050905090565b8060009080519060200190610231929190610235565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea26469706673582212209871cb2bcf390d53645807cbaedfe052d739ef9cff9d84787f74c4f379e1854664736f6c634300060a0033"; /* { @@ -48,257 +51,243 @@ public class TestTxStructV2 { } */ - public static byte[] fromHex(String hexString) { - if (hexString.startsWith("0x")) { - hexString = hexString.substring(2); + public static byte[] fromHex(String hexString) { + if (hexString.startsWith("0x")) { + hexString = hexString.substring(2); + } + + if (hexString.length() % 2 != 0) { + throw new IllegalArgumentException("Invalid hex string: " + hexString); + } + + int length = hexString.length() / 2; + byte[] bytes = new byte[length]; + + for (int i = 0; i < length; i++) { + String twoChars = hexString.substring(i * 2, i * 2 + 2); + bytes[i] = (byte) Integer.parseInt(twoChars, 16); + } + + return bytes; } - if (hexString.length() % 2 != 0) { - throw new IllegalArgumentException("Invalid hex string: " + hexString); + public static String generateNonce() { + byte[] nonceBytes = new byte[16]; + SecureRandom secureRandom = new SecureRandom(); + secureRandom.nextBytes(nonceBytes); + StringBuilder hex = new StringBuilder(); + for (byte b : nonceBytes) { + hex.append(String.format("%02x", b)); + } + return hex.toString(); } - int length = hexString.length() / 2; - byte[] bytes = new byte[length]; + public static String getBinary(boolean isSM) { + return isSM ? hwSmBIN : hwBIN; + } - for (int i = 0; i < length; i++) { - String twoChars = hexString.substring(i * 2, i * 2 + 2); - bytes[i] = (byte) Integer.parseInt(twoChars, 16); + public static void Usage() { + System.out.println("Desc: test transaction struct V2 [HelloWorld set]"); + System.out.println( + "Usage: java -cp 'conf/:lib/*:apps/*' org.fisco.bcos.sdk.jni.test.tx.TestTxStructV2"); + System.exit(0); } - return bytes; - } + public static void main(String[] args) throws JniException, InterruptedException, ExecutionException { + if (args.length > 1) { + Usage(); + } - public static String generateNonce() { - byte[] nonceBytes = new byte[16]; - SecureRandom secureRandom = new SecureRandom(); - secureRandom.nextBytes(nonceBytes); - StringBuilder hex = new StringBuilder(); - for (byte b : nonceBytes) { - hex.append(String.format("%02x", b)); - } - return hex.toString(); - } + String endpoint = "127.0.0.1:20200"; + String group = "group0"; + String node = ""; + JniConfig jniConfig = Utility.newJniConfig(Arrays.asList(endpoint)); + jniConfig.setDisableSsl(true); + BcosSDKJniObj bcosSDKJni = BcosSDKJniObj.build(jniConfig); + RpcJniObj rpcJniObj = RpcJniObj.build(bcosSDKJni.getNativePointer()); + System.out.println("build Rpc"); + rpcJniObj.start(); + long blockLimit = rpcJniObj.getBlockLimit(group); - public static String getBinary(boolean isSM) { - return isSM ? hwSmBIN : hwBIN; - } + boolean smCrypto = false; - public static void Usage() { - System.out.println("Desc: test transaction struct V2 [HelloWorld set]"); - System.out.println( - "Usage: java -cp 'conf/:lib/*:apps/*' org.fisco.bcos.sdk.jni.test.tx.TestTxStructV2"); - System.exit(0); - } + long keyPair = KeyPairJniObj.createJniKeyPair(smCrypto ? 1 : 0); + String jniKeyPairAddress = KeyPairJniObj.getJniKeyPairAddress(keyPair); + System.out.printf(" [test Tx Struct V2] new account, address: %s\n", jniKeyPairAddress); - public static void main(String[] args) throws JniException, InterruptedException { - try { - if (args.length > 1) { - Usage(); - } - - String endpoint = "127.0.0.1:20200"; - String group = "group0"; - String node = ""; - JniConfig jniConfig = Utility.newJniConfig(Arrays.asList(endpoint)); - jniConfig.setDisableSsl(true); - BcosSDKJniObj bcosSDKJni = BcosSDKJniObj.build(jniConfig); - RpcJniObj rpcJniObj = RpcJniObj.build(bcosSDKJni.getNativePointer()); - System.out.println("build Rpc"); - rpcJniObj.start(); - - boolean smCrypto = false; - - long keyPair = KeyPairJniObj.createJniKeyPair(smCrypto ? 1 : 0); - String jniKeyPairAddress = KeyPairJniObj.getJniKeyPairAddress(keyPair); - System.out.printf(" [test Tx Struct V2] new account, address: %s\n", jniKeyPairAddress); - - String groupID = "group0"; - String chainID = "chain0"; - String data = getBinary(smCrypto); - long blockLimit = 1; - String value = "11"; - String gasPrice = "0"; - long gasLimit = 0; - String maxFeePerGas = "11"; - String maxPriorityFeePerGas = "22"; - - // construct TransactionDataV2 - TransactionDataV2 transactionDataStructV2 = new TransactionDataV2(); - transactionDataStructV2.setGroupId(groupID); - transactionDataStructV2.setChainId(chainID); - transactionDataStructV2.setTo(""); - transactionDataStructV2.setAbi(""); - transactionDataStructV2.setVersion(1); - transactionDataStructV2.setNonce(generateNonce()); - transactionDataStructV2.setBlockLimit(blockLimit); - transactionDataStructV2.setValue(value); - transactionDataStructV2.setGasPrice(gasPrice); - transactionDataStructV2.setGasLimit(gasLimit); - transactionDataStructV2.setMaxFeePerGas(maxFeePerGas); - transactionDataStructV2.setMaxPriorityFeePerGas(maxPriorityFeePerGas); - // input - TransactionBytes inputBytes = new TransactionBytes(); - byte[] bytesInput = fromHex(data); - inputBytes.setBuffer(bytesInput); - inputBytes.setLength(bytesInput.length); - transactionDataStructV2.setInput(inputBytes); - - // encode TxData to hex tx data - String txDataHex = - TransactionStructBuilderV2JniObj.encodeTransactionDataStructV2(transactionDataStructV2); - // decode hex tx data to TxData - TransactionDataV2 decodeTransactionDataStructHex = - TransactionStructBuilderV2JniObj.decodeTransactionDataStructV2(txDataHex); - // TransactionDataV2 decodeTransactionDataStructHex2 = - // TransactionStructBuilderV2JniObj.decodeTransactionDataStructV2(null); - // TransactionDataV2 decodeTransactionDataStructHex3 = - // TransactionStructBuilderV2JniObj.decodeTransactionDataStructV2(""); - - // assert - Assert.assertEquals( - transactionDataStructV2.getChainId(), decodeTransactionDataStructHex.getChainId()); - Assert.assertEquals( - transactionDataStructV2.getGroupId(), decodeTransactionDataStructHex.getGroupId()); - Assert.assertEquals( - transactionDataStructV2.getAbi(), decodeTransactionDataStructHex.getAbi()); - Assert.assertEquals( - transactionDataStructV2.getBlockLimit(), decodeTransactionDataStructHex.getBlockLimit()); - Assert.assertEquals( - transactionDataStructV2.getValue(), decodeTransactionDataStructHex.getValue()); - Assert.assertEquals( - transactionDataStructV2.getGasPrice(), decodeTransactionDataStructHex.getGasPrice()); - Assert.assertEquals( - transactionDataStructV2.getGasLimit(), decodeTransactionDataStructHex.getGasLimit()); - Assert.assertEquals( - transactionDataStructV2.getMaxFeePerGas(), - decodeTransactionDataStructHex.getMaxFeePerGas()); - Assert.assertEquals( - transactionDataStructV2.getMaxPriorityFeePerGas(), - decodeTransactionDataStructHex.getMaxPriorityFeePerGas()); - - // encode TxData to json tx data - String txDataJson = - TransactionStructBuilderV2JniObj.encodeTransactionDataStructToJsonV2( - transactionDataStructV2); - System.out.printf(" [test Tx Struct V2] txDataJson: %s\n", txDataJson); - - // calc tx data hash - String txDataHash = - TransactionStructBuilderV2JniObj.calcTransactionDataStructHashV2( - smCrypto ? 1 : 0, decodeTransactionDataStructHex); - System.out.printf(" [test Tx Struct V2] txDataHash: %s\n", txDataHash); - // signature tx data hash - String signature = TransactionBuilderJniObj.signTransactionDataHash(keyPair, txDataHash); - System.out.printf(" [test Tx Struct V2] signature: %s\n", signature); - - // construct tx - TransactionV2 transactionStructV2 = new TransactionV2(); - TransactionBytes dataHashBytes = new TransactionBytes(); - dataHashBytes.setBuffer(txDataHash.getBytes()); - dataHashBytes.setLength(txDataHash.getBytes().length); - TransactionBytes signatureBytes = new TransactionBytes(); - signatureBytes.setBuffer(signature.getBytes()); - signatureBytes.setLength(signature.getBytes().length); - transactionStructV2.setTransactionData(decodeTransactionDataStructHex); - transactionStructV2.setDataHash(dataHashBytes); - transactionStructV2.setSignature(signatureBytes); - transactionStructV2.setSender(null); - transactionStructV2.setImportTime(0); - transactionStructV2.setAttribute(0); - transactionStructV2.setExtraData(""); - // assert - Assert.assertEquals( - transactionStructV2.getTransactionData().getBlockLimit(), - decodeTransactionDataStructHex.getBlockLimit()); - Assert.assertEquals( - transactionStructV2.getTransactionData().getGroupId(), - decodeTransactionDataStructHex.getGroupId()); - Assert.assertEquals( - transactionStructV2.getTransactionData().getChainId(), - decodeTransactionDataStructHex.getChainId()); - Assert.assertEquals( - transactionStructV2.getTransactionData().getChainId(), - decodeTransactionDataStructHex.getChainId()); - Assert.assertEquals( - transactionStructV2.getTransactionData().getAbi(), - decodeTransactionDataStructHex.getAbi()); - Assert.assertArrayEquals( - transactionStructV2.getDataHash().getBuffer(), txDataHash.getBytes()); - Assert.assertArrayEquals( - transactionStructV2.getSignature().getBuffer(), signature.getBytes()); - - // encode Tx to hex tx - String txHex = - TransactionStructBuilderV2JniObj.encodeTransactionStructV2(transactionStructV2); - // decode hex tx to Tx - TransactionV2 decodeTransactionStructHex = - TransactionStructBuilderV2JniObj.decodeTransactionStructV2(txHex); - - // assert - Assert.assertEquals( - transactionStructV2.getTransactionData().getBlockLimit(), - decodeTransactionStructHex.getTransactionData().getBlockLimit()); - Assert.assertEquals( - transactionStructV2.getTransactionData().getGroupId(), - decodeTransactionStructHex.getTransactionData().getGroupId()); - Assert.assertEquals( - transactionStructV2.getTransactionData().getChainId(), - decodeTransactionStructHex.getTransactionData().getChainId()); - Assert.assertEquals( - transactionStructV2.getTransactionData().getAbi(), - decodeTransactionStructHex.getTransactionData().getAbi()); - Assert.assertEquals( - transactionStructV2.getTransactionData().getValue(), - decodeTransactionStructHex.getTransactionData().getValue()); - Assert.assertEquals( - transactionStructV2.getTransactionData().getGasLimit(), - decodeTransactionStructHex.getTransactionData().getGasLimit()); - Assert.assertEquals( - transactionStructV2.getTransactionData().getGasPrice(), - decodeTransactionStructHex.getTransactionData().getGasPrice()); - Assert.assertEquals( - transactionStructV2.getTransactionData().getMaxFeePerGas(), - decodeTransactionStructHex.getTransactionData().getMaxFeePerGas()); - Assert.assertEquals( - transactionStructV2.getTransactionData().getMaxPriorityFeePerGas(), - decodeTransactionStructHex.getTransactionData().getMaxPriorityFeePerGas()); - Assert.assertArrayEquals( - transactionStructV2.getDataHash().getBuffer(), - decodeTransactionStructHex.getDataHash().getBuffer()); - Assert.assertArrayEquals( - transactionStructV2.getSignature().getBuffer(), - decodeTransactionStructHex.getSignature().getBuffer()); - - // encode Tx to json tx - String txJson = - TransactionStructBuilderV2JniObj.encodeTransactionStructToJsonV2(transactionStructV2); - System.out.printf(" [test Tx Struct V2] txJson: %s\n", txJson); - // create tx string - String txString = - TransactionStructBuilderV2JniObj.createEncodedTransactionV2( - decodeTransactionDataStructHex, signature, txDataHash, 0, ""); - // System.out.printf(" [test Tx Struct V2] txString: %s\n", txString); - - // rpc send tx - rpcJniObj.sendTransaction( - group, - node, - txString, - false, - new RpcCallback() { - @Override - public void onResponse(Response response) { - System.out.println("response error code: ==>>> " + response.getErrorCode()); - String dataStr = new String(response.getData()); - System.out.println("response data: ==>>> " + dataStr); - } - }); - - Thread.sleep(2000); - rpcJniObj.stop(); - System.out.printf(" [test Tx Struct V2] finish !! \n"); - } catch (JniException e) { - System.out.println("test tx struct V2 failed, error: " + e.getMessage()); + String groupID = "group0"; + String chainID = "chain0"; + String data = getBinary(smCrypto); + String value = "0x11"; + String gasPrice = "0x10"; + long gasLimit = 0; + String maxFeePerGas = "0x11"; + String maxPriorityFeePerGas = "0x22"; + + // construct TransactionDataV2 + TransactionDataV2 transactionDataStructV2 = new TransactionDataV2(); + transactionDataStructV2.setGroupId(groupID); + transactionDataStructV2.setChainId(chainID); + transactionDataStructV2.setTo(""); + transactionDataStructV2.setAbi(""); + transactionDataStructV2.setVersion(1); + transactionDataStructV2.setNonce(generateNonce()); + transactionDataStructV2.setBlockLimit(blockLimit); + transactionDataStructV2.setValue(value); + transactionDataStructV2.setGasPrice(gasPrice); + transactionDataStructV2.setGasLimit(gasLimit); + transactionDataStructV2.setMaxFeePerGas(maxFeePerGas); + transactionDataStructV2.setMaxPriorityFeePerGas(maxPriorityFeePerGas); + // input + byte[] bytesInput = fromHex(data); + transactionDataStructV2.setInput(bytesInput); + + // encode TxData to hex tx data + String txDataHex = + TransactionStructBuilderJniObj.encodeTransactionDataStruct(transactionDataStructV2); + // decode hex tx data to TxData + TransactionDataV2 decodeTransactionDataStructHex = + TransactionStructBuilderJniObj.decodeTransactionDataStructV2(txDataHex); + // TransactionDataV2 decodeTransactionDataStructHex2 = + // TransactionStructBuilderV2JniObj.decodeTransactionDataStructV2(null); + // TransactionDataV2 decodeTransactionDataStructHex3 = + // TransactionStructBuilderV2JniObj.decodeTransactionDataStructV2(""); + + // assert + Assert.assertEquals( + transactionDataStructV2.getChainId(), decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals( + transactionDataStructV2.getGroupId(), decodeTransactionDataStructHex.getGroupId()); + Assert.assertEquals( + transactionDataStructV2.getAbi(), decodeTransactionDataStructHex.getAbi()); + Assert.assertEquals( + transactionDataStructV2.getBlockLimit(), decodeTransactionDataStructHex.getBlockLimit()); + Assert.assertEquals( + transactionDataStructV2.getValue(), decodeTransactionDataStructHex.getValue()); + Assert.assertEquals( + transactionDataStructV2.getGasPrice(), decodeTransactionDataStructHex.getGasPrice()); + Assert.assertEquals( + transactionDataStructV2.getGasLimit(), decodeTransactionDataStructHex.getGasLimit()); + Assert.assertEquals( + transactionDataStructV2.getMaxFeePerGas(), + decodeTransactionDataStructHex.getMaxFeePerGas()); + Assert.assertEquals( + transactionDataStructV2.getMaxPriorityFeePerGas(), + decodeTransactionDataStructHex.getMaxPriorityFeePerGas()); + + // encode TxData to json tx data + String txDataJson = + TransactionStructBuilderJniObj.encodeTransactionDataStructToJson( + transactionDataStructV2); + System.out.printf(" [test Tx Struct V2] txDataJson: %s\n", txDataJson); + + // calc tx data hash + String txDataHash = + TransactionStructBuilderJniObj.calcTransactionDataStructHash( + smCrypto ? 1 : 0, decodeTransactionDataStructHex); + System.out.printf(" [test Tx Struct V2] txDataHash: %s\n", txDataHash); + // signature tx data hash + String signature = TransactionBuilderJniObj.signTransactionDataHash(keyPair, txDataHash); + System.out.printf(" [test Tx Struct V2] signature: %s\n", signature); + + // construct tx + Transaction transactionStructV2 = new Transaction(); + transactionStructV2.setTransactionData(decodeTransactionDataStructHex); + transactionStructV2.setDataHash(fromHex(txDataHash)); + transactionStructV2.setSignature(fromHex(signature)); + transactionStructV2.setSender(null); + transactionStructV2.setImportTime(0); + transactionStructV2.setAttribute(0); + transactionStructV2.setExtraData(""); + // assert + Assert.assertEquals( + transactionStructV2.getTransactionData().getBlockLimit(), + decodeTransactionDataStructHex.getBlockLimit()); + Assert.assertEquals( + transactionStructV2.getTransactionData().getGroupId(), + decodeTransactionDataStructHex.getGroupId()); + Assert.assertEquals( + transactionStructV2.getTransactionData().getChainId(), + decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals( + transactionStructV2.getTransactionData().getChainId(), + decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals( + transactionStructV2.getTransactionData().getAbi(), + decodeTransactionDataStructHex.getAbi()); + Assert.assertArrayEquals( + transactionStructV2.getDataHash(), fromHex(txDataHash)); + Assert.assertArrayEquals( + transactionStructV2.getSignature(), fromHex(signature)); + + // encode Tx to hex tx + String txHex = + TransactionStructBuilderJniObj.encodeTransactionStruct(transactionStructV2); + // decode hex tx to Tx + Transaction decodeTransactionStructHex = + TransactionStructBuilderJniObj.decodeTransactionStructV2(txHex); + + // assert + Assert.assertEquals( + transactionStructV2.getTransactionData().getBlockLimit(), + decodeTransactionStructHex.getTransactionData().getBlockLimit()); + Assert.assertEquals( + transactionStructV2.getTransactionData().getGroupId(), + decodeTransactionStructHex.getTransactionData().getGroupId()); + Assert.assertEquals( + transactionStructV2.getTransactionData().getChainId(), + decodeTransactionStructHex.getTransactionData().getChainId()); + Assert.assertEquals( + transactionStructV2.getTransactionData().getAbi(), + decodeTransactionStructHex.getTransactionData().getAbi()); + TransactionDataV2 transactionData = (TransactionDataV2) transactionStructV2.getTransactionData(); + TransactionDataV2 decodeTransactionData = (TransactionDataV2) decodeTransactionStructHex.getTransactionData(); + Assert.assertEquals( + transactionData.getValue(), + decodeTransactionData.getValue()); + Assert.assertEquals( + transactionData.getGasLimit(), + decodeTransactionData.getGasLimit()); + Assert.assertEquals( + transactionData.getGasPrice(), + decodeTransactionData.getGasPrice()); + Assert.assertEquals( + transactionData.getMaxFeePerGas(), + decodeTransactionData.getMaxFeePerGas()); + Assert.assertEquals( + transactionData.getMaxPriorityFeePerGas(), + decodeTransactionData.getMaxPriorityFeePerGas()); + Assert.assertArrayEquals( + transactionStructV2.getDataHash(), + decodeTransactionStructHex.getDataHash()); + Assert.assertArrayEquals( + transactionStructV2.getSignature(), + decodeTransactionStructHex.getSignature()); + + // encode Tx to json tx + String txJson = + TransactionStructBuilderJniObj.encodeTransactionStructToJson(transactionStructV2); + System.out.printf(" [test Tx Struct V2] txJson: %s\n", txJson); + // create tx string + String txString = + TransactionStructBuilderJniObj.createEncodedTransaction( + decodeTransactionDataStructHex, signature, txDataHash, 0, ""); + // System.out.printf(" [test Tx Struct V2] txString: %s\n", txString); + CompletableFuture future = new CompletableFuture<>(); + + // rpc send tx + rpcJniObj.sendTransaction( + group, + node, + txString, + false, + future::complete); + + Response response = future.get(); + System.out.println("response error code: ==>>> " + response.getErrorCode()); + String dataStr = new String(response.getData()); + System.out.println("response data: ==>>> " + dataStr); + rpcJniObj.stop(); + System.out.println(" [test Tx Struct V2] finish !! "); } - } } From 805a3b991dee97b9967f38e9ac64168dc4c3fd4d Mon Sep 17 00:00:00 2001 From: lucasli <410567249@qq.com> Date: Tue, 30 Jan 2024 20:29:30 +0800 Subject: [PATCH 26/29] fix core when extra_data is nullptr (#213) --- bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp | 4 ++-- test/testTransactionStruct.cpp | 8 +++++++- test/testTransactionStructV2.cpp | 7 +++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp index 5d34da6b5..bd2817932 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp @@ -929,7 +929,7 @@ struct bcos_sdk_c_transaction* bcos_sdk_create_transaction_struct( transaction_struct->sender = NULL; transaction_struct->import_time = 0; transaction_struct->attribute = attribute; - transaction_struct->extra_data = my_strdup(extra_data); + transaction_struct->extra_data = extra_data ? my_strdup(extra_data) : my_strdup(""); // signature auto signatureWithoutHex = fromHexString(signature); struct bcos_sdk_c_bytes* signature_bytes = @@ -1578,7 +1578,7 @@ struct bcos_sdk_c_transaction_v2* bcos_sdk_create_transaction_struct_v2( transaction_struct_v2->sender = NULL; transaction_struct_v2->import_time = 0; transaction_struct_v2->attribute = attribute; - transaction_struct_v2->extra_data = my_strdup(extra_data); + transaction_struct_v2->extra_data = extra_data ? my_strdup(extra_data) : my_strdup(""); // signature auto signatureWithoutHex = fromHexString(signature); struct bcos_sdk_c_bytes* signature_bytes = diff --git a/test/testTransactionStruct.cpp b/test/testTransactionStruct.cpp index 5f59a5ac2..4d212a048 100644 --- a/test/testTransactionStruct.cpp +++ b/test/testTransactionStruct.cpp @@ -619,12 +619,18 @@ BOOST_AUTO_TEST_CASE(testCreateTxStruct) BOOST_TEST(bcos_sdk_get_last_error() == -1); BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("signature can not be empty string")); + // create transaction_struct success (extraData = NULL) + txStruct = bcos_sdk_create_transaction_struct( + txDataStruct, signature, transactionDataHash, attribute, nullptr); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == true); + compareTxStruct(txStruct, txDataStruct, signature, "", attribute); + // create transaction_struct success txStruct = bcos_sdk_create_transaction_struct( txDataStruct, signature, transactionDataHash, attribute, extraData); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == true); - compareTxStruct(txStruct, txDataStruct, signature, extraData, attribute); bcos_sdk_destroy_transaction_data_struct(txDataStruct); diff --git a/test/testTransactionStructV2.cpp b/test/testTransactionStructV2.cpp index 11bdc690e..283825ba5 100644 --- a/test/testTransactionStructV2.cpp +++ b/test/testTransactionStructV2.cpp @@ -695,6 +695,13 @@ BOOST_AUTO_TEST_CASE(testCreateTxStructV2) BOOST_TEST(bcos_sdk_get_last_error() == -1); BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("signature can not be empty string")); + // create transaction_struct success (extraData = NULL) + txStruct = bcos_sdk_create_transaction_struct_v2( + txDataStruct, signature, transactionDataHash, attribute, nullptr); + success = bcos_sdk_is_last_opr_success(); + BOOST_TEST(success == true); + compareTxStruct(txStruct, txDataStruct, signature, "", attribute); + // create transaction_struct success txStruct = bcos_sdk_create_transaction_struct_v2( txDataStruct, signature, transactionDataHash, attribute, extraData); From d17b3b481d2f655d05cb438baa27d455ab15501d Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Tue, 30 Jan 2024 22:47:45 +0800 Subject: [PATCH 27/29] (vcpkg): update vcpkg registry for hostname resolve. (#212) --- vcpkg-configuration.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vcpkg-configuration.json b/vcpkg-configuration.json index 591959347..19978ab0c 100644 --- a/vcpkg-configuration.json +++ b/vcpkg-configuration.json @@ -3,7 +3,7 @@ { "kind": "git", "repository": "https://github.com/FISCO-BCOS/registry", - "baseline": "f8fde49506ea47504783f339aeeca6e269bbdb12", + "baseline": "cf9b1d3f9133ac203abe2ae22a887f4efc2fdccb", "packages": [ "openssl", "hsm-crypto", From 2fd08319f65a68f58f751fbbf7a3639a5febfcc8 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Thu, 1 Feb 2024 21:29:58 +0800 Subject: [PATCH 28/29] (tx): change transaction v2 to v1. (#214) --- bcos-c-sdk/bcos_sdk_c_common.cpp | 4 + bcos-c-sdk/bcos_sdk_c_common.h | 6 +- bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp | 360 +++++---- bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h | 48 +- ...uti_tx_v2.cpp => bcos_sdk_c_uti_tx_v1.cpp} | 22 +- ...k_c_uti_tx_v2.h => bcos_sdk_c_uti_tx_v1.h} | 10 +- bindings/java/jni/scripts/java2jni.sh | 2 +- ...ilities_tx_TransactionBuilderV1JniObj.cpp} | 36 +- ...utilities_tx_TransactionBuilderV1JniObj.h} | 34 +- ...ties_tx_TransactionStructBuilderJniObj.cpp | 128 ++-- ...lities_tx_TransactionStructBuilderJniObj.h | 10 +- ...es_tx_TransactionStructBuilderV2JniObj.cpp | 724 ------------------ ...ties_tx_TransactionStructBuilderV2JniObj.h | 77 -- .../sdk/jni/utilities/tx/Transaction.java | 2 +- ...j.java => TransactionBuilderV1JniObj.java} | 2 +- ...tionDataV2.java => TransactionDataV1.java} | 2 +- .../tx/TransactionStructBuilderJniObj.java | 8 +- .../jni/utilities/tx/TransactionVersion.java | 36 +- .../bcos/sdk/jni/test/tx/TestTxStruct.java | 388 +++++----- .../bcos/sdk/jni/test/tx/TestTxStructV1.java | 272 +++++++ .../bcos/sdk/jni/test/tx/TestTxStructV2.java | 293 ------- sample/tx/CMakeLists.txt | 4 +- ...x_struct_v2_test.c => tx_struct_v1_test.c} | 168 ++-- ...ructV2.cpp => testTransactionStructV1.cpp} | 252 +++--- vcpkg-configuration.json | 2 +- 25 files changed, 1044 insertions(+), 1846 deletions(-) rename bcos-c-sdk/{bcos_sdk_c_uti_tx_v2.cpp => bcos_sdk_c_uti_tx_v1.cpp} (97%) rename bcos-c-sdk/{bcos_sdk_c_uti_tx_v2.h => bcos_sdk_c_uti_tx_v1.h} (97%) rename bindings/java/jni/src/main/c/jni/{org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp => org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj.cpp} (96%) rename bindings/java/jni/src/main/c/jni/{org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.h => org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj.h} (86%) delete mode 100644 bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj.cpp delete mode 100644 bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj.h rename bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/{TransactionBuilderV2JniObj.java => TransactionBuilderV1JniObj.java} (99%) rename bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/{TransactionDataV2.java => TransactionDataV1.java} (94%) create mode 100644 bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV1.java delete mode 100644 bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV2.java rename sample/tx/{tx_struct_v2_test.c => tx_struct_v1_test.c} (74%) rename test/{testTransactionStructV2.cpp => testTransactionStructV1.cpp} (85%) diff --git a/bcos-c-sdk/bcos_sdk_c_common.cpp b/bcos-c-sdk/bcos_sdk_c_common.cpp index 2cb7d34b7..e9683ea1c 100644 --- a/bcos-c-sdk/bcos_sdk_c_common.cpp +++ b/bcos-c-sdk/bcos_sdk_c_common.cpp @@ -59,6 +59,10 @@ struct bcos_sdk_c_config* bcos_sdk_c_config_create_empty() char* my_strdup(const char* s) { + if (s == NULL) + { + return NULL; + } size_t len = strlen(s) + 1; char* result = (char*)malloc(len); if (result == (char*)0) diff --git a/bcos-c-sdk/bcos_sdk_c_common.h b/bcos-c-sdk/bcos_sdk_c_common.h index f381f2182..cd9beefd0 100644 --- a/bcos-c-sdk/bcos_sdk_c_common.h +++ b/bcos-c-sdk/bcos_sdk_c_common.h @@ -209,7 +209,7 @@ struct bcos_sdk_c_transaction_data struct bcos_sdk_c_bytes* input; }; -struct bcos_sdk_c_transaction_data_v2 +struct bcos_sdk_c_transaction_data_v1 { int32_t version; int64_t block_limit; @@ -241,9 +241,9 @@ struct bcos_sdk_c_transaction char* extra_data; }; -struct bcos_sdk_c_transaction_v2 +struct bcos_sdk_c_transaction_v1 { - struct bcos_sdk_c_transaction_data_v2* transaction_data; + struct bcos_sdk_c_transaction_data_v1* transaction_data; struct bcos_sdk_c_bytes* data_hash; struct bcos_sdk_c_bytes* signature; struct bcos_sdk_c_bytes* sender; diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp index bd2817932..fa37c723d 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.cpp @@ -126,38 +126,38 @@ struct bcos_sdk_c_transaction_data* transaction_data_copy( return NULL; } -struct bcos_sdk_c_transaction_data_v2* transaction_data_copy_v2( - const bcos_sdk_c_transaction_data_v2* tx_data_src) +struct bcos_sdk_c_transaction_data_v1* transaction_data_copy_v1( + const bcos_sdk_c_transaction_data_v1* tx_data_src) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(tx_data_src, NULL); try { - struct bcos_sdk_c_transaction_data_v2* transaction_data_struct_v2 = - (struct bcos_sdk_c_transaction_data_v2*)malloc( - sizeof(struct bcos_sdk_c_transaction_data_v2)); - transaction_data_struct_v2->version = tx_data_src->version; - transaction_data_struct_v2->block_limit = tx_data_src->block_limit; - transaction_data_struct_v2->chain_id = my_strdup(tx_data_src->chain_id); - transaction_data_struct_v2->group_id = my_strdup(tx_data_src->group_id); - transaction_data_struct_v2->nonce = my_strdup(tx_data_src->nonce); - transaction_data_struct_v2->to = my_strdup(tx_data_src->to); - transaction_data_struct_v2->abi = my_strdup(tx_data_src->abi); - transaction_data_struct_v2->input = bytes_struct_copy(tx_data_src->input); - transaction_data_struct_v2->value = my_strdup(tx_data_src->value); - transaction_data_struct_v2->gas_price = my_strdup(tx_data_src->gas_price); - transaction_data_struct_v2->gas_limit = tx_data_src->gas_limit; - transaction_data_struct_v2->max_fee_per_gas = my_strdup(tx_data_src->max_fee_per_gas); - transaction_data_struct_v2->max_priority_fee_per_gas = + struct bcos_sdk_c_transaction_data_v1* transaction_data_struct_v1 = + (struct bcos_sdk_c_transaction_data_v1*)malloc( + sizeof(struct bcos_sdk_c_transaction_data_v1)); + transaction_data_struct_v1->version = tx_data_src->version; + transaction_data_struct_v1->block_limit = tx_data_src->block_limit; + transaction_data_struct_v1->chain_id = my_strdup(tx_data_src->chain_id); + transaction_data_struct_v1->group_id = my_strdup(tx_data_src->group_id); + transaction_data_struct_v1->nonce = my_strdup(tx_data_src->nonce); + transaction_data_struct_v1->to = my_strdup(tx_data_src->to); + transaction_data_struct_v1->abi = my_strdup(tx_data_src->abi); + transaction_data_struct_v1->input = bytes_struct_copy(tx_data_src->input); + transaction_data_struct_v1->value = my_strdup(tx_data_src->value); + transaction_data_struct_v1->gas_price = my_strdup(tx_data_src->gas_price); + transaction_data_struct_v1->gas_limit = tx_data_src->gas_limit; + transaction_data_struct_v1->max_fee_per_gas = my_strdup(tx_data_src->max_fee_per_gas); + transaction_data_struct_v1->max_priority_fee_per_gas = my_strdup(tx_data_src->max_priority_fee_per_gas); - return transaction_data_struct_v2; + return transaction_data_struct_v1; } catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("transaction_data_copy_v2") << LOG_DESC("exception") + BCOS_LOG(WARNING) << LOG_BADGE("transaction_data_copy_v1") << LOG_DESC("exception") << LOG_KV("tx_data_src", tx_data_src) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); } @@ -208,44 +208,42 @@ struct bcos_sdk_c_transaction_data* convert_tars_transaction_data_to_struct( return NULL; } -struct bcos_sdk_c_transaction_data_v2* convert_tars_transaction_data_to_struct_v2( +struct bcos_sdk_c_transaction_data_v1* convert_tars_transaction_data_to_struct_v1( bcostars::TransactionDataUniquePtr tars_transaction_data) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(tars_transaction_data, NULL); - BCOS_SDK_C_PARAMS_VERIFY_CONDITION( - (tars_transaction_data->version == 1), "version of tars tx data V2 must be 1", NULL); try { - struct bcos_sdk_c_transaction_data_v2* transaction_data_struct_v2 = - (struct bcos_sdk_c_transaction_data_v2*)malloc( - sizeof(struct bcos_sdk_c_transaction_data_v2)); + struct bcos_sdk_c_transaction_data_v1* transaction_data_struct_v1 = + (struct bcos_sdk_c_transaction_data_v1*)malloc( + sizeof(struct bcos_sdk_c_transaction_data_v1)); struct bcos_sdk_c_bytes* input_bytes = create_bytes_struct( tars_transaction_data->input.size(), tars_transaction_data->input.data()); - transaction_data_struct_v2->input = input_bytes; - transaction_data_struct_v2->version = tars_transaction_data->version; - transaction_data_struct_v2->block_limit = tars_transaction_data->blockLimit; - transaction_data_struct_v2->chain_id = my_strdup(tars_transaction_data->chainID.data()); - transaction_data_struct_v2->group_id = my_strdup(tars_transaction_data->groupID.data()); - transaction_data_struct_v2->nonce = my_strdup(tars_transaction_data->nonce.data()); - transaction_data_struct_v2->to = my_strdup(tars_transaction_data->to.data()); - transaction_data_struct_v2->abi = my_strdup(tars_transaction_data->abi.data()); - transaction_data_struct_v2->value = my_strdup(tars_transaction_data->value.data()); - transaction_data_struct_v2->gas_price = my_strdup(tars_transaction_data->gasPrice.data()); - transaction_data_struct_v2->gas_limit = tars_transaction_data->gasLimit; - transaction_data_struct_v2->max_fee_per_gas = + transaction_data_struct_v1->input = input_bytes; + transaction_data_struct_v1->version = tars_transaction_data->version; + transaction_data_struct_v1->block_limit = tars_transaction_data->blockLimit; + transaction_data_struct_v1->chain_id = my_strdup(tars_transaction_data->chainID.data()); + transaction_data_struct_v1->group_id = my_strdup(tars_transaction_data->groupID.data()); + transaction_data_struct_v1->nonce = my_strdup(tars_transaction_data->nonce.data()); + transaction_data_struct_v1->to = my_strdup(tars_transaction_data->to.data()); + transaction_data_struct_v1->abi = my_strdup(tars_transaction_data->abi.data()); + transaction_data_struct_v1->value = my_strdup(tars_transaction_data->value.data()); + transaction_data_struct_v1->gas_price = my_strdup(tars_transaction_data->gasPrice.data()); + transaction_data_struct_v1->gas_limit = tars_transaction_data->gasLimit; + transaction_data_struct_v1->max_fee_per_gas = my_strdup(tars_transaction_data->maxFeePerGas.data()); - transaction_data_struct_v2->max_priority_fee_per_gas = + transaction_data_struct_v1->max_priority_fee_per_gas = my_strdup(tars_transaction_data->maxPriorityFeePerGas.data()); - return transaction_data_struct_v2; + return transaction_data_struct_v1; } catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("convert_tars_transaction_data_to_struct_v2") + BCOS_LOG(WARNING) << LOG_BADGE("convert_tars_transaction_data_to_struct_v1") << LOG_DESC("exception") << LOG_KV("transaction data", tars_transaction_data) << LOG_KV("error", errorMsg); @@ -294,8 +292,8 @@ bcostars::TransactionDataUniquePtr convert_transaction_data_to_tars( return NULL; } -bcostars::TransactionDataUniquePtr convert_transaction_data_to_tars_v2( - struct bcos_sdk_c_transaction_data_v2* transaction_data) +bcostars::TransactionDataUniquePtr convert_transaction_data_to_tars_v1( + struct bcos_sdk_c_transaction_data_v1* transaction_data) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); @@ -303,36 +301,36 @@ bcostars::TransactionDataUniquePtr convert_transaction_data_to_tars_v2( BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data->chain_id, NULL); BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data->input, NULL); BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data->value, NULL); - BCOS_SDK_C_PARAMS_VERIFY_CONDITION( - (transaction_data->version == 1), "version of tx data struct V2 must be 1", NULL); try { - auto tars_transaction_data_v2 = std::make_unique(); + auto tars_transaction_data_v1 = std::make_unique(); + tars_transaction_data_v1->input.reserve(transaction_data->input->length); for (size_t i = 0; i < transaction_data->input->length; ++i) { - tars_transaction_data_v2->input.push_back(transaction_data->input->buffer[i]); + tars_transaction_data_v1->input.push_back( + static_cast(transaction_data->input->buffer[i])); } - tars_transaction_data_v2->version = (tars::Int32)transaction_data->version; - tars_transaction_data_v2->blockLimit = (tars::Int64)transaction_data->block_limit; - tars_transaction_data_v2->chainID = std::string(transaction_data->chain_id); - tars_transaction_data_v2->groupID = std::string(transaction_data->group_id); - tars_transaction_data_v2->nonce = std::string(transaction_data->nonce); - tars_transaction_data_v2->to = std::string(transaction_data->to); - tars_transaction_data_v2->abi = std::string(transaction_data->abi); - tars_transaction_data_v2->value = std::string(transaction_data->value); - tars_transaction_data_v2->gasPrice = std::string(transaction_data->gas_price); - tars_transaction_data_v2->gasLimit = (tars::Int64)transaction_data->gas_limit; - tars_transaction_data_v2->maxFeePerGas = std::string(transaction_data->max_fee_per_gas); - tars_transaction_data_v2->maxPriorityFeePerGas = + tars_transaction_data_v1->version = (tars::Int32)transaction_data->version; + tars_transaction_data_v1->blockLimit = (tars::Int64)transaction_data->block_limit; + tars_transaction_data_v1->chainID = std::string(transaction_data->chain_id); + tars_transaction_data_v1->groupID = std::string(transaction_data->group_id); + tars_transaction_data_v1->nonce = std::string(transaction_data->nonce); + tars_transaction_data_v1->to = std::string(transaction_data->to); + tars_transaction_data_v1->abi = std::string(transaction_data->abi); + tars_transaction_data_v1->value = std::string(transaction_data->value); + tars_transaction_data_v1->gasPrice = std::string(transaction_data->gas_price); + tars_transaction_data_v1->gasLimit = (tars::Int64)transaction_data->gas_limit; + tars_transaction_data_v1->maxFeePerGas = std::string(transaction_data->max_fee_per_gas); + tars_transaction_data_v1->maxPriorityFeePerGas = std::string(transaction_data->max_priority_fee_per_gas); - return tars_transaction_data_v2; + return tars_transaction_data_v1; } catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("convert_transaction_data_to_tars_v2") + BCOS_LOG(WARNING) << LOG_BADGE("convert_transaction_data_to_tars_v1") << LOG_DESC("exception") << LOG_KV("transaction data", transaction_data) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); @@ -387,8 +385,8 @@ bcostars::TransactionUniquePtr convert_transaction_to_tars( return NULL; } -bcostars::TransactionUniquePtr convert_transaction_to_tars_v2( - struct bcos_sdk_c_transaction_v2* transaction) +bcostars::TransactionUniquePtr convert_transaction_to_tars_v1( + struct bcos_sdk_c_transaction_v1* transaction) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(transaction, NULL); @@ -399,7 +397,7 @@ bcostars::TransactionUniquePtr convert_transaction_to_tars_v2( { auto tars_transaction = std::make_unique(); auto TransactionDataUniquePtr = - convert_transaction_data_to_tars_v2(transaction->transaction_data); + convert_transaction_data_to_tars_v1(transaction->transaction_data); tars_transaction->data = *TransactionDataUniquePtr; for (size_t i = 0; i < transaction->data_hash->length; ++i) { @@ -425,7 +423,7 @@ bcostars::TransactionUniquePtr convert_transaction_to_tars_v2( catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("convert_transaction_to_tars_v2") << LOG_DESC("exception") + BCOS_LOG(WARNING) << LOG_BADGE("convert_transaction_to_tars_v1") << LOG_DESC("exception") << LOG_KV("transaction", transaction) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); } @@ -474,7 +472,7 @@ struct bcos_sdk_c_transaction* convert_tars_transaction_to_struct( return NULL; } -struct bcos_sdk_c_transaction_v2* convert_tars_transaction_to_struct_v2( +struct bcos_sdk_c_transaction_v1* convert_tars_transaction_to_struct_v1( bcostars::TransactionUniquePtr tars_transaction) { bcos_sdk_clear_last_error(); @@ -482,8 +480,8 @@ struct bcos_sdk_c_transaction_v2* convert_tars_transaction_to_struct_v2( try { - struct bcos_sdk_c_transaction_v2* transaction_struct_v2 = - (struct bcos_sdk_c_transaction_v2*)malloc(sizeof(struct bcos_sdk_c_transaction_v2)); + struct bcos_sdk_c_transaction_v1* transaction_struct_v1 = + (struct bcos_sdk_c_transaction_v1*)malloc(sizeof(struct bcos_sdk_c_transaction_v1)); struct bcos_sdk_c_bytes* data_hash_bytes = create_bytes_struct( tars_transaction->dataHash.size(), tars_transaction->dataHash.data()); struct bcos_sdk_c_bytes* signature_bytes = create_bytes_struct( @@ -493,20 +491,20 @@ struct bcos_sdk_c_transaction_v2* convert_tars_transaction_to_struct_v2( auto transactionDataUniquePtr = std::make_unique(tars_transaction->data); - transaction_struct_v2->transaction_data = - convert_tars_transaction_data_to_struct_v2(std::move(transactionDataUniquePtr)); - transaction_struct_v2->data_hash = data_hash_bytes; - transaction_struct_v2->signature = signature_bytes; - transaction_struct_v2->sender = sender_bytes; - transaction_struct_v2->import_time = tars_transaction->importTime; - transaction_struct_v2->attribute = tars_transaction->attribute; - transaction_struct_v2->extra_data = my_strdup(tars_transaction->extraData.data()); - return transaction_struct_v2; + transaction_struct_v1->transaction_data = + convert_tars_transaction_data_to_struct_v1(std::move(transactionDataUniquePtr)); + transaction_struct_v1->data_hash = data_hash_bytes; + transaction_struct_v1->signature = signature_bytes; + transaction_struct_v1->sender = sender_bytes; + transaction_struct_v1->import_time = tars_transaction->importTime; + transaction_struct_v1->attribute = tars_transaction->attribute; + transaction_struct_v1->extra_data = my_strdup(tars_transaction->extraData.data()); + return transaction_struct_v1; } catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("convert_tars_transaction_to_struct_v2") + BCOS_LOG(WARNING) << LOG_BADGE("convert_tars_transaction_to_struct_v1") << LOG_DESC("exception") << LOG_KV("transaction", tars_transaction) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); @@ -1189,7 +1187,7 @@ struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct_from_json( return NULL; } -struct bcos_sdk_c_transaction_data_v2* bcos_sdk_create_transaction_data_struct_with_hex_input_v2( +struct bcos_sdk_c_transaction_data_v1* bcos_sdk_create_transaction_data_struct_with_hex_input_v1( const char* group_id, const char* chain_id, const char* to, const char* input, const char* abi, int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit, const char* max_fee_per_gas, const char* max_priority_fee_per_gas) @@ -1208,34 +1206,34 @@ struct bcos_sdk_c_transaction_data_v2* bcos_sdk_create_transaction_data_struct_w try { - struct bcos_sdk_c_transaction_data_v2* transaction_data_struct_v2 = - (struct bcos_sdk_c_transaction_data_v2*)malloc( - sizeof(struct bcos_sdk_c_transaction_data_v2)); + struct bcos_sdk_c_transaction_data_v1* transaction_data_struct_v1 = + (struct bcos_sdk_c_transaction_data_v1*)malloc( + sizeof(struct bcos_sdk_c_transaction_data_v1)); auto bytesInput = fromHexString(input); TransactionBuilder builder; std::string nonceStr = builder.generateRandomStr(); - transaction_data_struct_v2->version = 1; - transaction_data_struct_v2->block_limit = block_limit; - transaction_data_struct_v2->group_id = my_strdup(group_id); - transaction_data_struct_v2->chain_id = my_strdup(chain_id); - transaction_data_struct_v2->to = to ? my_strdup(to) : my_strdup(""); - transaction_data_struct_v2->abi = abi ? my_strdup(abi) : my_strdup(""); - transaction_data_struct_v2->nonce = my_strdup(nonceStr.data()); - transaction_data_struct_v2->input = + transaction_data_struct_v1->version = 1; + transaction_data_struct_v1->block_limit = block_limit; + transaction_data_struct_v1->group_id = my_strdup(group_id); + transaction_data_struct_v1->chain_id = my_strdup(chain_id); + transaction_data_struct_v1->to = to ? my_strdup(to) : my_strdup(""); + transaction_data_struct_v1->abi = abi ? my_strdup(abi) : my_strdup(""); + transaction_data_struct_v1->nonce = my_strdup(nonceStr.data()); + transaction_data_struct_v1->input = create_bytes_struct(bytesInput->size(), reinterpret_cast(bytesInput->data())); - transaction_data_struct_v2->value = my_strdup(value); - transaction_data_struct_v2->gas_price = my_strdup(gas_price); - transaction_data_struct_v2->gas_limit = gas_limit; - transaction_data_struct_v2->max_fee_per_gas = my_strdup(max_fee_per_gas); - transaction_data_struct_v2->max_priority_fee_per_gas = my_strdup(max_priority_fee_per_gas); + transaction_data_struct_v1->value = my_strdup(value); + transaction_data_struct_v1->gas_price = my_strdup(gas_price); + transaction_data_struct_v1->gas_limit = gas_limit; + transaction_data_struct_v1->max_fee_per_gas = my_strdup(max_fee_per_gas); + transaction_data_struct_v1->max_priority_fee_per_gas = my_strdup(max_priority_fee_per_gas); - return transaction_data_struct_v2; + return transaction_data_struct_v1; } catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_data_struct_with_hex_input_v2") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_data_struct_with_hex_input_v1") << LOG_DESC("exception") << LOG_KV("group_id", group_id) << LOG_KV("chain_id", chain_id) << LOG_KV("to", std::string(to ? to : "")) << LOG_KV("input", input) << LOG_KV("abi", std::string(abi ? abi : "")) @@ -1250,7 +1248,7 @@ struct bcos_sdk_c_transaction_data_v2* bcos_sdk_create_transaction_data_struct_w return NULL; } -struct bcos_sdk_c_transaction_data_v2* bcos_sdk_create_transaction_data_struct_with_bytes_v2( +struct bcos_sdk_c_transaction_data_v1* bcos_sdk_create_transaction_data_struct_with_bytes_v1( const char* group_id, const char* chain_id, const char* to, const unsigned char* bytes_input, uint32_t bytes_input_length, const char* abi, int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit, const char* max_fee_per_gas, @@ -1271,33 +1269,33 @@ struct bcos_sdk_c_transaction_data_v2* bcos_sdk_create_transaction_data_struct_w try { - struct bcos_sdk_c_transaction_data_v2* transaction_data_struct_v2 = - (struct bcos_sdk_c_transaction_data_v2*)malloc( - sizeof(struct bcos_sdk_c_transaction_data_v2)); + struct bcos_sdk_c_transaction_data_v1* transaction_data_struct_v1 = + (struct bcos_sdk_c_transaction_data_v1*)malloc( + sizeof(struct bcos_sdk_c_transaction_data_v1)); TransactionBuilder builder; std::string nonceStr = builder.generateRandomStr(); - transaction_data_struct_v2->version = 1; - transaction_data_struct_v2->block_limit = block_limit; - transaction_data_struct_v2->group_id = my_strdup(group_id); - transaction_data_struct_v2->chain_id = my_strdup(chain_id); - transaction_data_struct_v2->to = to ? my_strdup(to) : my_strdup(""); - transaction_data_struct_v2->abi = abi ? my_strdup(abi) : my_strdup(""); - transaction_data_struct_v2->nonce = my_strdup(nonceStr.data()); - transaction_data_struct_v2->input = create_bytes_struct( + transaction_data_struct_v1->version = 1; + transaction_data_struct_v1->block_limit = block_limit; + transaction_data_struct_v1->group_id = my_strdup(group_id); + transaction_data_struct_v1->chain_id = my_strdup(chain_id); + transaction_data_struct_v1->to = to ? my_strdup(to) : my_strdup(""); + transaction_data_struct_v1->abi = abi ? my_strdup(abi) : my_strdup(""); + transaction_data_struct_v1->nonce = my_strdup(nonceStr.data()); + transaction_data_struct_v1->input = create_bytes_struct( bytes_input_length, const_cast(reinterpret_cast(bytes_input))); - transaction_data_struct_v2->value = my_strdup(value); - transaction_data_struct_v2->gas_price = my_strdup(gas_price); - transaction_data_struct_v2->gas_limit = gas_limit; - transaction_data_struct_v2->max_fee_per_gas = my_strdup(max_fee_per_gas); - transaction_data_struct_v2->max_priority_fee_per_gas = my_strdup(max_priority_fee_per_gas); + transaction_data_struct_v1->value = my_strdup(value); + transaction_data_struct_v1->gas_price = my_strdup(gas_price); + transaction_data_struct_v1->gas_limit = gas_limit; + transaction_data_struct_v1->max_fee_per_gas = my_strdup(max_fee_per_gas); + transaction_data_struct_v1->max_priority_fee_per_gas = my_strdup(max_priority_fee_per_gas); - return transaction_data_struct_v2; + return transaction_data_struct_v1; } catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_data_struct_with_bytes_v2") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_data_struct_with_bytes_v1") << LOG_DESC("exception") << LOG_KV("group_id", group_id) << LOG_KV("chain_id", chain_id) << LOG_KV("to", std::string(to ? to : "")) << LOG_KV("bytes_input", bytes_input) @@ -1314,8 +1312,8 @@ struct bcos_sdk_c_transaction_data_v2* bcos_sdk_create_transaction_data_struct_w return NULL; } -void bcos_sdk_destroy_transaction_data_struct_v2( - struct bcos_sdk_c_transaction_data_v2* transaction_data) +void bcos_sdk_destroy_transaction_data_struct_v1( + struct bcos_sdk_c_transaction_data_v1* transaction_data) { if (transaction_data == NULL) { @@ -1379,15 +1377,15 @@ void bcos_sdk_destroy_transaction_data_struct_v2( bcos_sdk_c_free(transaction_data); } -const char* bcos_sdk_encode_transaction_data_struct_to_hex_v2( - struct bcos_sdk_c_transaction_data_v2* transaction_data) +const char* bcos_sdk_encode_transaction_data_struct_to_hex_v1( + struct bcos_sdk_c_transaction_data_v1* transaction_data) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); try { - auto tars_transaction_data = convert_transaction_data_to_tars_v2(transaction_data); + auto tars_transaction_data = convert_transaction_data_to_tars_v1(transaction_data); TransactionBuilder builder; auto encodedTransactionData = builder.encodeTransactionData(*tars_transaction_data); auto hex_tx_data_str = bcos::toHexString(*encodedTransactionData); @@ -1396,7 +1394,7 @@ const char* bcos_sdk_encode_transaction_data_struct_to_hex_v2( catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_data_struct_to_hex_v2") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_data_struct_to_hex_v1") << LOG_DESC("exception") << LOG_KV("transaction_data", transaction_data) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); @@ -1405,22 +1403,22 @@ const char* bcos_sdk_encode_transaction_data_struct_to_hex_v2( return NULL; } -const char* bcos_sdk_encode_transaction_data_struct_to_json_v2( - struct bcos_sdk_c_transaction_data_v2* transaction_data) +const char* bcos_sdk_encode_transaction_data_struct_to_json_v1( + struct bcos_sdk_c_transaction_data_v1* transaction_data) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); try { - auto tars_transaction_data_v2 = convert_transaction_data_to_tars_v2(transaction_data); - auto json_str = tars_transaction_data_v2->writeToJsonString(); + auto tars_transaction_data_v1 = convert_transaction_data_to_tars_v1(transaction_data); + auto json_str = tars_transaction_data_v1->writeToJsonString(); return strdup(json_str.c_str()); } catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_data_struct_to_json_v2") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_data_struct_to_json_v1") << LOG_DESC("exception") << LOG_KV("transaction_data", transaction_data) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); @@ -1429,7 +1427,7 @@ const char* bcos_sdk_encode_transaction_data_struct_to_json_v2( return NULL; } -struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_from_hex_v2( +struct bcos_sdk_c_transaction_data_v1* bcos_sdk_decode_transaction_data_struct_from_hex_v1( const char* transaction_data_hex_str) { bcos_sdk_clear_last_error(); @@ -1443,13 +1441,13 @@ struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_f { TransactionBuilder builder; auto tx_data_bytes = fromHexString(transaction_data_hex_str); - auto tars_tx_data_v2 = builder.decodeTransactionData(*tx_data_bytes); - return convert_tars_transaction_data_to_struct_v2(std::move(tars_tx_data_v2)); + auto tars_tx_data_v1 = builder.decodeTransactionData(*tx_data_bytes); + return convert_tars_transaction_data_to_struct_v1(std::move(tars_tx_data_v1)); } catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_data_struct_v2") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_data_struct_v1") << LOG_DESC("exception") << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); } @@ -1457,7 +1455,7 @@ struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_f return NULL; } -struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_from_json_v2( +struct bcos_sdk_c_transaction_data_v1* bcos_sdk_decode_transaction_data_struct_from_json_v1( const char* transaction_data_json_str) { bcos_sdk_clear_last_error(); @@ -1471,12 +1469,12 @@ struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_f auto transactionData = builder.createTransactionDataWithJson(std::string(transaction_data_json_str)); - return convert_tars_transaction_data_to_struct_v2(std::move(transactionData)); + return convert_tars_transaction_data_to_struct_v1(std::move(transactionData)); } catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_data_struct_from_json_v2") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_data_struct_from_json_v1") << LOG_DESC("exception") << LOG_KV("transaction_data_json_str", transaction_data_json_str) << LOG_KV("error", errorMsg); @@ -1486,8 +1484,8 @@ struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_f return NULL; } -const char* bcos_sdk_calc_transaction_data_struct_hash_v2( - int crypto_type, struct bcos_sdk_c_transaction_data_v2* transaction_data) +const char* bcos_sdk_calc_transaction_data_struct_hash_v1( + int crypto_type, struct bcos_sdk_c_transaction_data_v1* transaction_data) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(transaction_data, NULL); @@ -1499,17 +1497,17 @@ const char* bcos_sdk_calc_transaction_data_struct_hash_v2( try { - auto tars_transaction_data_v2 = convert_transaction_data_to_tars_v2(transaction_data); + auto tars_transaction_data_v1 = convert_transaction_data_to_tars_v1(transaction_data); TransactionBuilder builder; auto transactionDataHash = builder.calculateTransactionDataHash( crypto_type == BCOS_C_SDK_ECDSA_TYPE ? CryptoType::Secp256K1 : CryptoType::SM2, - *tars_transaction_data_v2); + *tars_transaction_data_v1); return strdup(bcos::toHexStringWithPrefix(transactionDataHash).c_str()); } catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_calc_transaction_data_struct_hash_v2") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_calc_transaction_data_struct_hash_v1") << LOG_DESC("exception") << LOG_KV("crypto_type", crypto_type) << LOG_KV("transaction_data", transaction_data) << LOG_KV("error", errorMsg); @@ -1519,7 +1517,7 @@ const char* bcos_sdk_calc_transaction_data_struct_hash_v2( return NULL; } -const char* bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2( +const char* bcos_sdk_calc_transaction_data_struct_hash_with_hex_v1( int crypto_type, const char* transaction_data_hex) { bcos_sdk_clear_last_error(); @@ -1534,20 +1532,20 @@ const char* bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2( try { - struct bcos_sdk_c_transaction_data_v2* transaction_data_struct_v2 = - bcos_sdk_decode_transaction_data_struct_from_hex_v2(transaction_data_hex); - auto tars_transaction_data_v2 = - convert_transaction_data_to_tars_v2(transaction_data_struct_v2); + struct bcos_sdk_c_transaction_data_v1* transaction_data_struct_v1 = + bcos_sdk_decode_transaction_data_struct_from_hex_v1(transaction_data_hex); + auto tars_transaction_data_v1 = + convert_transaction_data_to_tars_v1(transaction_data_struct_v1); TransactionBuilder builder; auto transactionDataHash = builder.calculateTransactionDataHash( crypto_type == BCOS_C_SDK_ECDSA_TYPE ? CryptoType::Secp256K1 : CryptoType::SM2, - *tars_transaction_data_v2); + *tars_transaction_data_v1); return strdup(bcos::toHexStringWithPrefix(transactionDataHash).c_str()); } catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_calc_transaction_data_struct_hash_with_hex_v1") << LOG_DESC("exception") << LOG_KV("crypto_type", crypto_type) << LOG_KV("transaction_data_hex", transaction_data_hex) << LOG_KV("error", errorMsg); @@ -1557,8 +1555,8 @@ const char* bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2( return NULL; } -struct bcos_sdk_c_transaction_v2* bcos_sdk_create_transaction_struct_v2( - struct bcos_sdk_c_transaction_data_v2* transaction_data, const char* signature, +struct bcos_sdk_c_transaction_v1* bcos_sdk_create_transaction_struct_v1( + struct bcos_sdk_c_transaction_data_v1* transaction_data, const char* signature, const char* transaction_data_hash, int32_t attribute, const char* extra_data) { bcos_sdk_clear_last_error(); @@ -1572,13 +1570,13 @@ struct bcos_sdk_c_transaction_v2* bcos_sdk_create_transaction_struct_v2( try { - struct bcos_sdk_c_transaction_v2* transaction_struct_v2 = - (struct bcos_sdk_c_transaction_v2*)malloc(sizeof(struct bcos_sdk_c_transaction_v2)); - transaction_struct_v2->transaction_data = transaction_data_copy_v2(transaction_data); - transaction_struct_v2->sender = NULL; - transaction_struct_v2->import_time = 0; - transaction_struct_v2->attribute = attribute; - transaction_struct_v2->extra_data = extra_data ? my_strdup(extra_data) : my_strdup(""); + struct bcos_sdk_c_transaction_v1* transaction_struct_v1 = + (struct bcos_sdk_c_transaction_v1*)malloc(sizeof(struct bcos_sdk_c_transaction_v1)); + transaction_struct_v1->transaction_data = transaction_data_copy_v1(transaction_data); + transaction_struct_v1->sender = NULL; + transaction_struct_v1->import_time = 0; + transaction_struct_v1->attribute = attribute; + transaction_struct_v1->extra_data = my_strdup(extra_data); // signature auto signatureWithoutHex = fromHexString(signature); struct bcos_sdk_c_bytes* signature_bytes = @@ -1586,7 +1584,7 @@ struct bcos_sdk_c_transaction_v2* bcos_sdk_create_transaction_struct_v2( signature_bytes->length = signatureWithoutHex->size(); signature_bytes->buffer = (uint8_t*)malloc(signatureWithoutHex->size()); memcpy(signature_bytes->buffer, signatureWithoutHex->data(), signatureWithoutHex->size()); - transaction_struct_v2->signature = signature_bytes; + transaction_struct_v1->signature = signature_bytes; // data_hash auto dataHashArray = bcos::crypto::HashType(transaction_data_hash); struct bcos_sdk_c_bytes* data_hash_bytes = @@ -1594,14 +1592,14 @@ struct bcos_sdk_c_transaction_v2* bcos_sdk_create_transaction_struct_v2( data_hash_bytes->length = dataHashArray.size(); data_hash_bytes->buffer = (uint8_t*)malloc(dataHashArray.size()); memcpy(data_hash_bytes->buffer, dataHashArray.data(), dataHashArray.size()); - transaction_struct_v2->data_hash = data_hash_bytes; + transaction_struct_v1->data_hash = data_hash_bytes; - return transaction_struct_v2; + return transaction_struct_v1; } catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_struct_v2") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_transaction_struct_v1") << LOG_DESC("exception") << LOG_KV("signature", signature) << LOG_KV("transaction_data_hash", transaction_data_hash) << LOG_KV("attribute", attribute) << LOG_KV("extra_data", extra_data) @@ -1612,7 +1610,7 @@ struct bcos_sdk_c_transaction_v2* bcos_sdk_create_transaction_struct_v2( return NULL; } -void bcos_sdk_destroy_transaction_struct_v2(struct bcos_sdk_c_transaction_v2* transaction) +void bcos_sdk_destroy_transaction_struct_v1(struct bcos_sdk_c_transaction_v1* transaction) { if (transaction == NULL) { @@ -1621,7 +1619,7 @@ void bcos_sdk_destroy_transaction_struct_v2(struct bcos_sdk_c_transaction_v2* tr if (transaction && transaction->transaction_data) { - bcos_sdk_destroy_transaction_data_struct_v2(transaction->transaction_data); + bcos_sdk_destroy_transaction_data_struct_v1(transaction->transaction_data); } if (transaction && transaction->data_hash) @@ -1659,8 +1657,8 @@ void bcos_sdk_destroy_transaction_struct_v2(struct bcos_sdk_c_transaction_v2* tr bcos_sdk_c_free(transaction); } -const char* bcos_sdk_create_encoded_transaction_v2( - struct bcos_sdk_c_transaction_data_v2* transaction_data, const char* signature, +const char* bcos_sdk_create_encoded_transaction_v1( + struct bcos_sdk_c_transaction_data_v1* transaction_data, const char* signature, const char* transaction_data_hash, int32_t attribute, const char* extra_data) { bcos_sdk_clear_last_error(); @@ -1675,8 +1673,8 @@ const char* bcos_sdk_create_encoded_transaction_v2( try { TransactionBuilder builder; - auto tars_tx_data_v2 = convert_transaction_data_to_tars_v2(transaction_data); - auto signedBytes = builder.createSignedTransaction(*tars_tx_data_v2, + auto tars_tx_data_v1 = convert_transaction_data_to_tars_v1(transaction_data); + auto signedBytes = builder.createSignedTransaction(*tars_tx_data_v1, *fromHexString(signature), bcos::crypto::HashType(transaction_data_hash), attribute, extra_data ? std::string(extra_data) : std::string()); return strdup(bcos::toHexStringWithPrefix(*signedBytes).c_str()); @@ -1684,7 +1682,7 @@ const char* bcos_sdk_create_encoded_transaction_v2( catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_encoded_transaction_v2") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_create_encoded_transaction_v1") << LOG_DESC("exception") << LOG_KV("signature", signature) << LOG_KV("transaction_data_hash", transaction_data_hash) << LOG_KV("attribute", attribute) << LOG_KV("extra_data", extra_data) @@ -1694,24 +1692,24 @@ const char* bcos_sdk_create_encoded_transaction_v2( return NULL; } -const char* bcos_sdk_encode_transaction_struct_to_hex_v2( - struct bcos_sdk_c_transaction_v2* transaction) +const char* bcos_sdk_encode_transaction_struct_to_hex_v1( + struct bcos_sdk_c_transaction_v1* transaction) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(transaction, NULL); try { - auto tars_transaction_v2 = convert_transaction_to_tars_v2(transaction); + auto tars_transaction_v1 = convert_transaction_to_tars_v1(transaction); TransactionBuilder builder; - auto encodedTransaction = builder.encodeTransaction(*tars_transaction_v2); + auto encodedTransaction = builder.encodeTransaction(*tars_transaction_v1); auto hex_tx_str = toHexStringWithPrefix(*encodedTransaction); return strdup(hex_tx_str.c_str()); } catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_struct_to_hex_v2") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_struct_to_hex_v1") << LOG_DESC("exception") << LOG_KV("transaction", transaction) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); @@ -1720,22 +1718,22 @@ const char* bcos_sdk_encode_transaction_struct_to_hex_v2( return NULL; } -const char* bcos_sdk_encode_transaction_struct_to_json_v2( - struct bcos_sdk_c_transaction_v2* transaction) +const char* bcos_sdk_encode_transaction_struct_to_json_v1( + struct bcos_sdk_c_transaction_v1* transaction) { bcos_sdk_clear_last_error(); BCOS_SDK_C_PARAMS_VERIFICATION(transaction, NULL); try { - auto tars_transaction_v2 = convert_transaction_to_tars_v2(transaction); - auto json_str = tars_transaction_v2->writeToJsonString(); + auto tars_transaction_v1 = convert_transaction_to_tars_v1(transaction); + auto json_str = tars_transaction_v1->writeToJsonString(); return strdup(json_str.c_str()); } catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_struct_to_json_v2") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_encode_transaction_struct_to_json_v1") << LOG_DESC("exception") << LOG_KV("transaction", transaction) << LOG_KV("error", errorMsg); bcos_sdk_set_last_error_msg(-1, errorMsg.c_str()); @@ -1744,7 +1742,7 @@ const char* bcos_sdk_encode_transaction_struct_to_json_v2( return NULL; } -struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_from_hex_v2( +struct bcos_sdk_c_transaction_v1* bcos_sdk_decode_transaction_struct_from_hex_v1( const char* transaction_hex_str) { bcos_sdk_clear_last_error(); @@ -1759,7 +1757,7 @@ struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_from_hex_v2 TransactionBuilder builder; auto tx_bytes = fromHexString(transaction_hex_str); auto tars_tx = builder.decodeTransaction(*tx_bytes); - return convert_tars_transaction_to_struct_v2(std::move(tars_tx)); + return convert_tars_transaction_to_struct_v1(std::move(tars_tx)); } catch (const std::exception& e) { @@ -1774,7 +1772,7 @@ struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_from_hex_v2 return NULL; } -struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_from_json_v2( +struct bcos_sdk_c_transaction_v1* bcos_sdk_decode_transaction_struct_from_json_v1( const char* transaction_json_str) { bcos_sdk_clear_last_error(); @@ -1787,12 +1785,12 @@ struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_from_json_v TransactionBuilder builder; auto transaction = builder.createTransactionWithJson(std::string(transaction_json_str)); - return convert_tars_transaction_to_struct_v2(std::move(transaction)); + return convert_tars_transaction_to_struct_v1(std::move(transaction)); } catch (const std::exception& e) { std::string errorMsg = boost::diagnostic_information(e); - BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_struct_from_json_v2") + BCOS_LOG(WARNING) << LOG_BADGE("bcos_sdk_decode_transaction_struct_from_json_v1") << LOG_DESC("exception") << LOG_KV("transaction_json_str", transaction_json_str) << LOG_KV("error", errorMsg); diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h index 1f76c9968..f45bce564 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h @@ -194,59 +194,59 @@ struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct_from_hex( struct bcos_sdk_c_transaction* bcos_sdk_decode_transaction_struct_from_json( const char* transaction_json_str); -struct bcos_sdk_c_transaction_data_v2* bcos_sdk_create_transaction_data_struct_with_hex_input_v2( +struct bcos_sdk_c_transaction_data_v1* bcos_sdk_create_transaction_data_struct_with_hex_input_v1( const char* group_id, const char* chain_id, const char* to, const char* input, const char* abi, int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit, const char* max_fee_per_gas, const char* max_priority_fee_per_gas); -struct bcos_sdk_c_transaction_data_v2* bcos_sdk_create_transaction_data_struct_with_bytes_v2( +struct bcos_sdk_c_transaction_data_v1* bcos_sdk_create_transaction_data_struct_with_bytes_v1( const char* group_id, const char* chain_id, const char* to, const unsigned char* bytes_input, uint32_t bytes_input_length, const char* abi, int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit, const char* max_fee_per_gas, const char* max_priority_fee_per_gas); -void bcos_sdk_destroy_transaction_data_struct_v2( - struct bcos_sdk_c_transaction_data_v2* transaction_data); +void bcos_sdk_destroy_transaction_data_struct_v1( + struct bcos_sdk_c_transaction_data_v1* transaction_data); -const char* bcos_sdk_encode_transaction_data_struct_to_hex_v2( - struct bcos_sdk_c_transaction_data_v2* transaction_data); +const char* bcos_sdk_encode_transaction_data_struct_to_hex_v1( + struct bcos_sdk_c_transaction_data_v1* transaction_data); -const char* bcos_sdk_encode_transaction_data_struct_to_json_v2( - struct bcos_sdk_c_transaction_data_v2* transaction_data); +const char* bcos_sdk_encode_transaction_data_struct_to_json_v1( + struct bcos_sdk_c_transaction_data_v1* transaction_data); -struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_from_hex_v2( +struct bcos_sdk_c_transaction_data_v1* bcos_sdk_decode_transaction_data_struct_from_hex_v1( const char* transaction_data_hex_str); -struct bcos_sdk_c_transaction_data_v2* bcos_sdk_decode_transaction_data_struct_from_json_v2( +struct bcos_sdk_c_transaction_data_v1* bcos_sdk_decode_transaction_data_struct_from_json_v1( const char* transaction_data_json_str); -const char* bcos_sdk_calc_transaction_data_struct_hash_v2( - int crypto_type, struct bcos_sdk_c_transaction_data_v2* transaction_data); +const char* bcos_sdk_calc_transaction_data_struct_hash_v1( + int crypto_type, struct bcos_sdk_c_transaction_data_v1* transaction_data); -const char* bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2( +const char* bcos_sdk_calc_transaction_data_struct_hash_with_hex_v1( int crypto_type, const char* transaction_data_hex); -struct bcos_sdk_c_transaction_v2* bcos_sdk_create_transaction_struct_v2( - struct bcos_sdk_c_transaction_data_v2* transaction_data, const char* signature, +struct bcos_sdk_c_transaction_v1* bcos_sdk_create_transaction_struct_v1( + struct bcos_sdk_c_transaction_data_v1* transaction_data, const char* signature, const char* transaction_data_hash, int32_t attribute, const char* extra_data); -void bcos_sdk_destroy_transaction_struct_v2(struct bcos_sdk_c_transaction_v2* transaction); +void bcos_sdk_destroy_transaction_struct_v1(struct bcos_sdk_c_transaction_v1* transaction); -const char* bcos_sdk_create_encoded_transaction_v2( - struct bcos_sdk_c_transaction_data_v2* transaction_data, const char* signature, +const char* bcos_sdk_create_encoded_transaction_v1( + struct bcos_sdk_c_transaction_data_v1* transaction_data, const char* signature, const char* transaction_data_hash, int32_t attribute, const char* extra_data); -const char* bcos_sdk_encode_transaction_struct_to_hex_v2( - struct bcos_sdk_c_transaction_v2* transaction); +const char* bcos_sdk_encode_transaction_struct_to_hex_v1( + struct bcos_sdk_c_transaction_v1* transaction); -const char* bcos_sdk_encode_transaction_struct_to_json_v2( - struct bcos_sdk_c_transaction_v2* transaction); +const char* bcos_sdk_encode_transaction_struct_to_json_v1( + struct bcos_sdk_c_transaction_v1* transaction); -struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_from_hex_v2( +struct bcos_sdk_c_transaction_v1* bcos_sdk_decode_transaction_struct_from_hex_v1( const char* transaction_hex_str); -struct bcos_sdk_c_transaction_v2* bcos_sdk_decode_transaction_struct_from_json_v2( +struct bcos_sdk_c_transaction_v1* bcos_sdk_decode_transaction_struct_from_json_v1( const char* transaction_json_str); #ifdef __cplusplus diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.cpp b/bcos-c-sdk/bcos_sdk_c_uti_tx_v1.cpp similarity index 97% rename from bcos-c-sdk/bcos_sdk_c_uti_tx_v2.cpp rename to bcos-c-sdk/bcos_sdk_c_uti_tx_v1.cpp index f6257bb62..85ba13674 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.cpp +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_v1.cpp @@ -13,17 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * @file bcos_sdk_c_uti_tx_v2.cpp + * @file bcos_sdk_c_uti_tx_v1.cpp * @author: kyonGuo * @date 2023/11/28 */ -#include "bcos_sdk_c_uti_tx_v2.h" +#include "bcos_sdk_c_uti_tx_v1.h" #include "bcos_sdk_c_error.h" #include #include #include -#include +#include using namespace bcos; using namespace bcos::cppsdk; @@ -33,7 +33,7 @@ using namespace bcos::cppsdk::utilities; #include #include -void* bcos_sdk_create_transaction_v2_data(const char* group_id, const char* chain_id, +void* bcos_sdk_create_transaction_v1_data(const char* group_id, const char* chain_id, const char* to, const char* nonce, const unsigned char* input, long inputSize, const char* abi, int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit) { @@ -46,7 +46,7 @@ void* bcos_sdk_create_transaction_v2_data(const char* group_id, const char* chai try { - TransactionBuilderV2 builder; + TransactionBuilderV1 builder; auto bytesData = bytes(input, input + inputSize * sizeof(byte)); auto transactionData = builder.createTransactionData(1, group_id, chain_id, to, nonce ? nonce : "", std::move(bytesData), abi, block_limit, value ? value : "", @@ -86,7 +86,7 @@ void* bcos_sdk_create_eip1559_transaction_data(const char* group_id, const char* try { - TransactionBuilderV2 builder; + TransactionBuilderV1 builder; auto bytesData = bytes(input, input + inputSize * sizeof(byte)); auto transactionData = builder.createTransactionData(1, group_id, chain_id, to, nonce ? nonce : "", std::move(bytesData), abi, block_limit, value ? value : "", "", @@ -133,7 +133,7 @@ const char* bcos_sdk_calc_transaction_data_hash_with_full_fields(int crypto_type try { auto bytesData = bytes(input, input + inputSize * sizeof(byte)); - TransactionBuilderV2 builder; + TransactionBuilderV1 builder; auto transactionDataHash = builder.calculateTransactionDataHash( crypto_type == BCOS_C_SDK_ECDSA_TYPE ? CryptoType::Secp256K1 : CryptoType::SM2, version, group_id, chain_id, to ? to : "", nonce, std::move(bytesData), abi ? abi : "", @@ -171,7 +171,7 @@ const char* bcos_sdk_calc_transaction_data_hash_with_json(int crypto_type, const try { - TransactionBuilderV2 builder; + TransactionBuilderV1 builder; auto transactionDataHash = builder.calculateTransactionDataHashWithJson( crypto_type == BCOS_C_SDK_ECDSA_TYPE ? CryptoType::Secp256K1 : CryptoType::SM2, json); return strdup(bcos::toHexStringWithPrefix(transactionDataHash).c_str()); @@ -205,7 +205,7 @@ const char* bcos_sdk_create_signed_transaction_with_signature(const unsigned cha try { - TransactionBuilderV2 builder; + TransactionBuilderV1 builder; auto bytesData = bytes(input, input + inputSize * sizeof(byte)); auto hash = std::string_view(transaction_hash); crypto::HashType tx_hash(fromHex(hash, hash.starts_with("0x") ? "0x" : "")); @@ -260,7 +260,7 @@ void bcos_sdk_create_signed_transaction_with_full_fields(void* key_pair, const c try { - TransactionBuilderV2 builder; + TransactionBuilderV1 builder; auto bytesData = bytes(input, input + inputSize * sizeof(byte)); auto result = builder.createSignedTransaction(*static_cast(key_pair), @@ -309,7 +309,7 @@ void bcos_sdk_create_signed_eip1559_transaction_with_full_fields(void* key_pair, try { - TransactionBuilderV2 builder; + TransactionBuilderV1 builder; auto bytesData = bytes(input, input + inputSize * sizeof(byte)); auto result = builder.createSignedTransaction( *static_cast(key_pair), attribute, 1, group_id, diff --git a/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h b/bcos-c-sdk/bcos_sdk_c_uti_tx_v1.h similarity index 97% rename from bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h rename to bcos-c-sdk/bcos_sdk_c_uti_tx_v1.h index ac337da09..20e3994f6 100644 --- a/bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h +++ b/bcos-c-sdk/bcos_sdk_c_uti_tx_v1.h @@ -13,13 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * @file bcos_sdk_c_uti_tx_v2.h + * @file bcos_sdk_c_uti_tx_v1.h * @author: kyonGuo * @date 2023/11/28 */ -#ifndef BCOS_C_SDK_BCOS_SDK_C_UTI_TX_V2_H -#define BCOS_C_SDK_BCOS_SDK_C_UTI_TX_V2_H +#ifndef BCOS_C_SDK_BCOS_SDK_C_UTI_TX_V1_H +#define BCOS_C_SDK_BCOS_SDK_C_UTI_TX_V1_H #include "bcos_sdk_c_common.h" @@ -54,7 +54,7 @@ enum transaction_version * @param gas_limit gas limit * @return void* transaction data pointer, you should release it after use */ -void* bcos_sdk_create_transaction_v2_data(const char* group_id, const char* chain_id, +void* bcos_sdk_create_transaction_v1_data(const char* group_id, const char* chain_id, const char* to, const char* nonce, const unsigned char* input, long inputSize, const char* abi, int64_t block_limit, const char* value, const char* gas_price, int64_t gas_limit); @@ -211,4 +211,4 @@ void bcos_sdk_create_signed_eip1559_transaction_with_full_fields(void* key_pair, #ifdef __cplusplus } #endif -#endif // BCOS_C_SDK_BCOS_SDK_C_UTI_TX_V2_H +#endif // BCOS_C_SDK_BCOS_SDK_C_UTI_TX_V1_H diff --git a/bindings/java/jni/scripts/java2jni.sh b/bindings/java/jni/scripts/java2jni.sh index 35c0f91ec..55f7117f0 100644 --- a/bindings/java/jni/scripts/java2jni.sh +++ b/bindings/java/jni/scripts/java2jni.sh @@ -30,7 +30,7 @@ function convert_java_to_jni() { # convert_java_to_jni ${JAVA_SOURCE_PATH} # classes="rpc.RpcJniObj amop.AmopJniObj event.EventSubJniObj BcosSDKJniObj" -classes="BcosSDKJniObj utilities.keypair.KeyPairJniObj utilities.tx.TransactionBuilderJniObj utilities.tx.TransactionBuilderV2JniObj utilities.tx.TransactionStructBuilderJniObj utilities.receipt.ReceiptBuilderJniObj utilities.signature.SignatureJniObj" +classes="BcosSDKJniObj utilities.keypair.KeyPairJniObj utilities.tx.TransactionBuilderJniObj utilities.tx.TransactionBuilderV1JniObj utilities.tx.TransactionStructBuilderJniObj utilities.receipt.ReceiptBuilderJniObj utilities.signature.SignatureJniObj" for class in ${classes} do diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj.cpp similarity index 96% rename from bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp rename to bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj.cpp index ab940033e..d8e9283ae 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj.cpp @@ -13,26 +13,26 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * @file org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.cpp + * @file org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj.cpp * @author: kyonGuo * @date 2023/11/29 */ -#include "org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.h" +#include "org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj.h" +#include "bcos-c-sdk/bcos_sdk_c_uti_tx_v1.h" #include "bcos-c-sdk/bcos_sdk_c_error.h" #include "bcos-c-sdk/bcos_sdk_c_uti_keypair.h" -#include "bcos-c-sdk/bcos_sdk_c_uti_tx_v2.h" #include "org_fisco_bcos_sdk_common.h" #include /* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj * Method: createTransactionData * Signature: * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;J)J */ JNIEXPORT jlong JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createTransactionData( +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj_createTransactionData( JNIEnv* env, jclass, jstring j_group_id, jstring j_chain_id, jstring j_to, jstring j_nonce, jbyteArray j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_gas_price, jlong j_gas_limit) @@ -54,7 +54,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createTransa jbyte* inputBuffer = env->GetByteArrayElements(j_input, NULL); jsize inputSize = env->GetArrayLength(j_input); - void* transaction_data = bcos_sdk_create_transaction_v2_data(group_id, chain_id, to, nonce, + void* transaction_data = bcos_sdk_create_transaction_v1_data(group_id, chain_id, to, nonce, reinterpret_cast(inputBuffer), inputSize, abi, block_limit, value, gas_price, gas_limit); @@ -77,13 +77,13 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createTransa } /* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj * Method: createEIP1559TransactionData * Signature: * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;JLjava/lang/String;Ljava/lang/String;)J */ JNIEXPORT jlong JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createEIP1559TransactionData( +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj_createEIP1559TransactionData( JNIEnv* env, jclass, jstring j_group_id, jstring j_chain_id, jstring j_to, jstring j_nonce, jbyteArray j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jlong j_gas_limit, jstring j_max_fee_per_gas, jstring j_max_priority_fee_per_gas) @@ -130,13 +130,13 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createEIP155 } /* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj * Method: calcTransactionDataHashWithFullFields * Signature: * (ILorg/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransactionDataHashWithFullFields( +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj_calcTransactionDataHashWithFullFields( JNIEnv* env, jclass, jint j_crypto_type, jobject j_tx_version, jstring j_group_id, jstring j_chain_id, jstring j_to, jstring j_nonce, jbyteArray j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_gasPrice, jlong j_gas_limit, @@ -202,12 +202,12 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransact } /* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj * Method: calcTransactionDataHashWithJson * Signature: (ILjava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransactionDataHashWithJson( +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj_calcTransactionDataHashWithJson( JNIEnv* env, jclass, jint j_crypto_type, jstring j_json) { CHECK_OBJECT_NOT_NULL(env, j_json, NULL); @@ -236,13 +236,13 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransact } /* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj * Method: createSignedTransactionWithSignature * Signature: * ([BLjava/lang/String;Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;ILjava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedTransactionWithSignature( +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj_createSignedTransactionWithSignature( JNIEnv* env, jclass, jbyteArray j_signature, jstring j_tx_hash, jobject j_tx_version, jstring j_group_id, jstring j_chain_id, jstring j_to, jstring j_nonce, jbyteArray j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_gas_price, jlong j_gas_limit, @@ -318,13 +318,13 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned } /* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj * Method: createSignedTransactionWithFullFields * Signature: * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; */ JNIEXPORT jobject JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedTransactionWithFullFields( +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj_createSignedTransactionWithFullFields( JNIEnv* env, jclass, jlong j_key_pair, jstring j_group_id, jstring j_chain_id, jstring j_to, jstring j_nonce, jbyteArray j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_gas_price, jlong j_gas_limit, jint j_attribute, jstring j_extra_data) @@ -408,13 +408,13 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSigned } /* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj * Method: createSignedEIP1559TransactionWithFullFields * Signature: * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; */ JNIEXPORT jobject JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedEIP1559TransactionWithFullFields( +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj_createSignedEIP1559TransactionWithFullFields( JNIEnv* env, jclass, jlong j_key_pair, jstring j_group_id, jstring j_chain_id, jstring j_to, jstring j_nonce, jbyteArray j_input, jstring j_abi, jlong j_block_limit, jstring j_value, jstring j_max_fee_per_gas, jstring j_max_priority_fee_per_gas, jlong j_gas_limit, diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.h b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj.h similarity index 86% rename from bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.h rename to bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj.h index 4fd67599c..bd74d5402 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj.h +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj.h @@ -1,84 +1,84 @@ /* DO NOT EDIT THIS FILE - it is machine generated */ #include -/* Header for class org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj */ +/* Header for class org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj */ -#ifndef _Included_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj -#define _Included_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj +#ifndef _Included_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj +#define _Included_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj #ifdef __cplusplus extern "C" { #endif /* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj * Method: createTransactionData * Signature: * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;J)J */ JNIEXPORT jlong JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createTransactionData(JNIEnv*, +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj_createTransactionData(JNIEnv*, jclass, jstring, jstring, jstring, jstring, jbyteArray, jstring, jlong, jstring, jstring, jlong); /* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj * Method: createEIP1559TransactionData * Signature: * (Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;JLjava/lang/String;Ljava/lang/String;)J */ JNIEXPORT jlong JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createEIP1559TransactionData( +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj_createEIP1559TransactionData( JNIEnv*, jclass, jstring, jstring, jstring, jstring, jbyteArray, jstring, jlong, jstring, jlong, jstring, jstring); /* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj * Method: calcTransactionDataHashWithFullFields * Signature: * (ILorg/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransactionDataHashWithFullFields( +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj_calcTransactionDataHashWithFullFields( JNIEnv*, jclass, jint, jobject, jstring, jstring, jstring, jstring, jbyteArray, jstring, jlong, jstring, jstring, jlong, jstring, jstring); /* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj * Method: calcTransactionDataHashWithJson * Signature: (ILjava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_calcTransactionDataHashWithJson( +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj_calcTransactionDataHashWithJson( JNIEnv*, jclass, jint, jstring); /* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj * Method: createSignedTransactionWithSignature * Signature: * ([BLjava/lang/String;Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;JLjava/lang/String;Ljava/lang/String;ILjava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedTransactionWithSignature( +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj_createSignedTransactionWithSignature( JNIEnv*, jclass, jbyteArray, jstring, jobject, jstring, jstring, jstring, jstring, jbyteArray, jstring, jlong, jstring, jstring, jlong, jstring, jstring, jint, jstring); /* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj * Method: createSignedTransactionWithFullFields * Signature: * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; */ JNIEXPORT jobject JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedTransactionWithFullFields( +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj_createSignedTransactionWithFullFields( JNIEnv*, jclass, jlong, jstring, jstring, jstring, jstring, jbyteArray, jstring, jlong, jstring, jstring, jlong, jint, jstring); /* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj + * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj * Method: createSignedEIP1559TransactionWithFullFields * Signature: * (JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;[BLjava/lang/String;JLjava/lang/String;Ljava/lang/String;Ljava/lang/String;JILjava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TxPair; */ JNIEXPORT jobject JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV2JniObj_createSignedEIP1559TransactionWithFullFields( +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionBuilderV1JniObj_createSignedEIP1559TransactionWithFullFields( JNIEnv*, jclass, jlong, jstring, jstring, jstring, jstring, jbyteArray, jstring, jlong, jstring, jstring, jstring, jlong, jint, jstring); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp index 12f34235b..ed5582b4e 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp @@ -6,8 +6,8 @@ #include "org_fisco_bcos_sdk_exception.h" -// tx data v2: java obj => c struct -struct bcos_sdk_c_transaction_data_v2* convert_to_tx_v2_data_struct( +// tx data v1: java obj => c struct +struct bcos_sdk_c_transaction_data_v1* convert_to_tx_v1_data_struct( JNIEnv* env, jobject transactionObject) { if (transactionObject == NULL) @@ -19,8 +19,8 @@ struct bcos_sdk_c_transaction_data_v2* convert_to_tx_v2_data_struct( jclass txDataClass = env->GetObjectClass(transactionObject); - bcos_sdk_c_transaction_data_v2* tx_data_struct = (struct bcos_sdk_c_transaction_data_v2*)malloc( - sizeof(struct bcos_sdk_c_transaction_data_v2)); + bcos_sdk_c_transaction_data_v1* tx_data_struct = (struct bcos_sdk_c_transaction_data_v1*)malloc( + sizeof(struct bcos_sdk_c_transaction_data_v1)); // version jfieldID versionField = env->GetFieldID(txDataClass, "version", "I"); tx_data_struct->version = env->GetIntField(transactionObject, versionField); @@ -281,9 +281,9 @@ jobject convert_to_tx_data_jobject( return jTxDataObj; } -// tx data v2: c struct => java obj -jobject convert_to_tx_v2_data_jobject( - JNIEnv* env, const struct bcos_sdk_c_transaction_data_v2* transactionData) +// tx data v1: c struct => java obj +jobject convert_to_tx_v1_data_jobject( + JNIEnv* env, const struct bcos_sdk_c_transaction_data_v1* transactionData) { if (transactionData == NULL) { @@ -292,12 +292,12 @@ jobject convert_to_tx_v2_data_jobject( return NULL; } - jclass txDataClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2"); + jclass txDataClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDatav1"); if (txDataClass == NULL) { env->FatalError( "No such class, className: " - "org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2"); + "org/fisco/bcos/sdk/jni/utilities/tx/TransactionDatav1"); } jmethodID txDataMtd = env->GetMethodID(txDataClass, "", "()V"); if (txDataMtd == NULL) @@ -446,8 +446,8 @@ jobject convert_to_tx_jobject(JNIEnv* env, const struct bcos_sdk_c_transaction* return javaTxObj; } -// tx v2: c struct => java obj -jobject convert_to_tx_v2_jobject(JNIEnv* env, const struct bcos_sdk_c_transaction_v2* tx_struct) +// tx v1: c struct => java obj +jobject convert_to_tx_v1_jobject(JNIEnv* env, const struct bcos_sdk_c_transaction_v1* tx_struct) { if (tx_struct == NULL) @@ -479,7 +479,7 @@ jobject convert_to_tx_v2_jobject(JNIEnv* env, const struct bcos_sdk_c_transactio jobject javaTxObj = env->NewObject(txClass, txMtd); // TransactionData - jobject javaTxDataObj = convert_to_tx_v2_data_jobject(env, tx_struct->transaction_data); + jobject javaTxDataObj = convert_to_tx_v1_data_jobject(env, tx_struct->transaction_data); if (javaTxDataObj == NULL) { THROW_JNI_EXCEPTION(env, "exception in TransactionData field."); @@ -643,8 +643,8 @@ struct bcos_sdk_c_transaction* convert_to_tx_struct(JNIEnv* env, jobject jTxObj) return txStruct; } -// tx v2: java obj => c struct -struct bcos_sdk_c_transaction_v2* convert_to_tx_v2_struct(JNIEnv* env, jobject jTxObj) +// tx v1: java obj => c struct +struct bcos_sdk_c_transaction_v1* convert_to_tx_v1_struct(JNIEnv* env, jobject jTxObj) { if (jTxObj == NULL) { @@ -662,12 +662,12 @@ struct bcos_sdk_c_transaction_v2* convert_to_tx_v2_struct(JNIEnv* env, jobject j jfieldID attributeField = env->GetFieldID(javaTxClass, "attribute", "I"); jfieldID extraDataField = env->GetFieldID(javaTxClass, "extraData", "Ljava/lang/String;"); - struct bcos_sdk_c_transaction_v2* txStruct = - (struct bcos_sdk_c_transaction_v2*)malloc(sizeof(struct bcos_sdk_c_transaction_v2)); + struct bcos_sdk_c_transaction_v1* txStruct = + (struct bcos_sdk_c_transaction_v1*)malloc(sizeof(struct bcos_sdk_c_transaction_v1)); // TransactionData jobject javaTxDataObj = env->GetObjectField(jTxObj, transactionDataField); - struct bcos_sdk_c_transaction_data_v2* txDataStruct = - convert_to_tx_v2_data_struct(env, javaTxDataObj); + struct bcos_sdk_c_transaction_data_v1* txDataStruct = + convert_to_tx_v1_data_struct(env, javaTxDataObj); if (txDataStruct == NULL) { THROW_JNI_EXCEPTION(env, "exception in txDataStruct field."); @@ -769,19 +769,19 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr } const char* tx_data_hex = nullptr; if (env->IsInstanceOf(jTransactionDataObj, - env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2")) == JNI_TRUE) + env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDatav1")) == JNI_TRUE) { - bcos_sdk_c_transaction_data_v2* tx_data_struct = - convert_to_tx_v2_data_struct(env, jTransactionDataObj); + bcos_sdk_c_transaction_data_v1* tx_data_struct = + convert_to_tx_v1_data_struct(env, jTransactionDataObj); if (tx_data_struct == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); return nullptr; } - tx_data_hex = bcos_sdk_encode_transaction_data_struct_to_hex_v2(tx_data_struct); + tx_data_hex = bcos_sdk_encode_transaction_data_struct_to_hex_v1(tx_data_struct); if (tx_data_struct) { - bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct); + bcos_sdk_destroy_transaction_data_struct_v1(tx_data_struct); } } else @@ -833,19 +833,19 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr } const char* tx_data_json = nullptr; if (env->IsInstanceOf(jTransactionDataObj, - env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2")) == JNI_TRUE) + env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDatav1")) == JNI_TRUE) { - bcos_sdk_c_transaction_data_v2* tx_data_struct = - convert_to_tx_v2_data_struct(env, jTransactionDataObj); + bcos_sdk_c_transaction_data_v1* tx_data_struct = + convert_to_tx_v1_data_struct(env, jTransactionDataObj); if (tx_data_struct == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); return nullptr; } - tx_data_json = bcos_sdk_encode_transaction_data_struct_to_json_v2(tx_data_struct); + tx_data_json = bcos_sdk_encode_transaction_data_struct_to_json_v1(tx_data_struct); if (tx_data_struct) { - bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct); + bcos_sdk_destroy_transaction_data_struct_v1(tx_data_struct); } } else @@ -918,35 +918,35 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTr /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj - * Method: decodeTransactionDataStructV2 - * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2; + * Method: decodeTransactionDataStructv1 + * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDatav1; */ JNIEXPORT jobject JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionDataStructV2( +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionDataStructV1( JNIEnv* env, jclass, jstring jTxDataHexStr) { CHECK_OBJECT_NOT_NULL(env, jTxDataHexStr, nullptr); const char* tx_data_hex_str = env->GetStringUTFChars(jTxDataHexStr, nullptr); - struct bcos_sdk_c_transaction_data_v2* tx_data_struct_v2 = - bcos_sdk_decode_transaction_data_struct_from_hex_v2(tx_data_hex_str); + struct bcos_sdk_c_transaction_data_v1* tx_data_struct_v1 = + bcos_sdk_decode_transaction_data_struct_from_hex_v1(tx_data_hex_str); if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); return nullptr; } - jobject jTxDataObj = convert_to_tx_v2_data_jobject(env, tx_data_struct_v2); + jobject jTxDataObj = convert_to_tx_v1_data_jobject(env, tx_data_struct_v1); if (jTxDataObj == NULL) { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_v2_data_jobject"); + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_v1_data_jobject"); return nullptr; } // release source - if (tx_data_struct_v2) + if (tx_data_struct_v1) { - bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct_v2); + bcos_sdk_destroy_transaction_data_struct_v1(tx_data_struct_v1); } env->ReleaseStringUTFChars(jTxDataHexStr, tx_data_hex_str); @@ -970,19 +970,19 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_calcTran const char* tx_data_hash = nullptr; int crypto_type = jCrytpTyte; if (env->IsInstanceOf(jTransactionDataObj, - env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2")) == JNI_TRUE) + env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDatav1")) == JNI_TRUE) { - bcos_sdk_c_transaction_data_v2* tx_data_struct = - convert_to_tx_v2_data_struct(env, jTransactionDataObj); + bcos_sdk_c_transaction_data_v1* tx_data_struct = + convert_to_tx_v1_data_struct(env, jTransactionDataObj); if (tx_data_struct == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); return nullptr; } - tx_data_hash = bcos_sdk_calc_transaction_data_struct_hash_v2(crypto_type, tx_data_struct); + tx_data_hash = bcos_sdk_calc_transaction_data_struct_hash_v1(crypto_type, tx_data_struct); if (tx_data_struct) { - bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct); + bcos_sdk_destroy_transaction_data_struct_v1(tx_data_struct); } } else @@ -1040,20 +1040,20 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_createEn int attribute = jAttribute; const char* extra_data = env->GetStringUTFChars(jExtraData, NULL); if (env->IsInstanceOf(jTxDataObj, - env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2")) == JNI_TRUE) + env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDatav1")) == JNI_TRUE) { - bcos_sdk_c_transaction_data_v2* tx_data_struct = - convert_to_tx_v2_data_struct(env, jTxDataObj); + bcos_sdk_c_transaction_data_v1* tx_data_struct = + convert_to_tx_v1_data_struct(env, jTxDataObj); if (tx_data_struct == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct"); return nullptr; } - tx_str = bcos_sdk_create_encoded_transaction_v2( + tx_str = bcos_sdk_create_encoded_transaction_v1( tx_data_struct, signature, tx_data_hash, attribute, extra_data); if (tx_data_struct) { - bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct); + bcos_sdk_destroy_transaction_data_struct_v1(tx_data_struct); } } else @@ -1113,18 +1113,18 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr CHECK_OBJECT_NOT_NULL(env, javaTxDataObj, nullptr); const char* tx_hex = nullptr; if (env->IsInstanceOf(javaTxDataObj, - env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2")) == JNI_TRUE) + env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDatav1")) == JNI_TRUE) { - bcos_sdk_c_transaction_v2* tx_struct = convert_to_tx_v2_struct(env, jTransactionObj); + bcos_sdk_c_transaction_v1* tx_struct = convert_to_tx_v1_struct(env, jTransactionObj); if (tx_struct == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct"); return nullptr; } - tx_hex = bcos_sdk_encode_transaction_struct_to_hex_v2(tx_struct); + tx_hex = bcos_sdk_encode_transaction_struct_to_hex_v1(tx_struct); if (tx_struct) { - bcos_sdk_destroy_transaction_struct_v2(tx_struct); + bcos_sdk_destroy_transaction_struct_v1(tx_struct); } } else @@ -1176,18 +1176,18 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr CHECK_OBJECT_NOT_NULL(env, javaTxDataObj, nullptr); const char* tx_json = nullptr; if (env->IsInstanceOf(javaTxDataObj, - env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2")) == JNI_TRUE) + env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDatav1")) == JNI_TRUE) { - bcos_sdk_c_transaction_v2* tx_struct = convert_to_tx_v2_struct(env, jTransactionObj); + bcos_sdk_c_transaction_v1* tx_struct = convert_to_tx_v1_struct(env, jTransactionObj); if (tx_struct == NULL) { THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct"); return nullptr; } - tx_json = bcos_sdk_encode_transaction_struct_to_json_v2(tx_struct); + tx_json = bcos_sdk_encode_transaction_struct_to_json_v1(tx_struct); if (tx_struct) { - bcos_sdk_destroy_transaction_struct_v2(tx_struct); + bcos_sdk_destroy_transaction_struct_v1(tx_struct); } } else @@ -1260,35 +1260,35 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTr /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj - * Method: decodeTransactionStructV2 - * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionV2; + * Method: decodeTransactionStructv1 + * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/Transactionv1; */ JNIEXPORT jobject JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionStructV2( +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionStructV1( JNIEnv* env, jclass, jstring jTxHexStr) { CHECK_OBJECT_NOT_NULL(env, jTxHexStr, nullptr); const char* tx_hex_str = env->GetStringUTFChars(jTxHexStr, nullptr); - struct bcos_sdk_c_transaction_v2* tx_struct_v2 = - bcos_sdk_decode_transaction_struct_from_hex_v2(tx_hex_str); + struct bcos_sdk_c_transaction_v1* tx_struct_v1 = + bcos_sdk_decode_transaction_struct_from_hex_v1(tx_hex_str); if (!bcos_sdk_is_last_opr_success()) { THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); return nullptr; } - jobject jTxObj = convert_to_tx_v2_jobject(env, tx_struct_v2); + jobject jTxObj = convert_to_tx_v1_jobject(env, tx_struct_v1); if (jTxObj == NULL) { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_v2_jobject"); + THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_v1_jobject"); return nullptr; } // release source - if (tx_struct_v2) + if (tx_struct_v1) { - bcos_sdk_destroy_transaction_struct_v2(tx_struct_v2); + bcos_sdk_destroy_transaction_struct_v1(tx_struct_v1); } env->ReleaseStringUTFChars(jTxHexStr, tx_hex_str); diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.h b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.h index 82048c474..0ade4cbdc 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.h +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.h @@ -36,11 +36,11 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTr /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj - * Method: decodeTransactionDataStructV2 - * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2; + * Method: decodeTransactionDataStructV1 + * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV1; */ JNIEXPORT jobject JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionDataStructV2( +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionDataStructV1( JNIEnv*, jclass, jstring); /* @@ -91,11 +91,11 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTr /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj - * Method: decodeTransactionStructV2 + * Method: decodeTransactionStructV1 * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/Transaction; */ JNIEXPORT jobject JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionStructV2( +Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionStructV1( JNIEnv*, jclass, jstring); #ifdef __cplusplus diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj.cpp deleted file mode 100644 index c5a168c81..000000000 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj.cpp +++ /dev/null @@ -1,724 +0,0 @@ -#include "org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj.h" -#include "bcos-c-sdk/bcos_sdk_c_error.h" -#include "bcos-c-sdk/bcos_sdk_c_uti_keypair.h" -#include "bcos-c-sdk/bcos_sdk_c_uti_tx_struct.h" -#include "org_fisco_bcos_sdk_common.h" -#include "org_fisco_bcos_sdk_exception.h" - - -struct bcos_sdk_c_bytes* convert_to_bytes_struct_v2(JNIEnv* env, jobject bytesObj) -{ - if (bytesObj == NULL) - { - return NULL; - } - - struct bcos_sdk_c_bytes* bytes = - (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); - jclass bytesClass = env->GetObjectClass(bytesObj); - jfieldID bufferField = env->GetFieldID(bytesClass, "buffer", "[B"); - jfieldID lengthField = env->GetFieldID(bytesClass, "length", "I"); - - jbyteArray bufferArray = (jbyteArray)env->GetObjectField(bytesObj, bufferField); - jbyte* bufferData = env->GetByteArrayElements(bufferArray, nullptr); - jsize bufferLength = env->GetArrayLength(bufferArray); - bytes->length = (uint32_t)env->GetIntField(bytesObj, lengthField); - bytes->buffer = (uint8_t*)malloc(bufferLength * sizeof(uint8_t)); - memcpy(bytes->buffer, bufferData, bufferLength); - - env->ReleaseByteArrayElements(bufferArray, bufferData, 0); - - return bytes; -} - -jobject convert_to_bytes_jobject_v2(JNIEnv* env, const struct bcos_sdk_c_bytes* bytes_struct) -{ - if (bytes_struct == NULL) - { - return NULL; - } - - jclass txBytesClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes"); - if (txBytesClass == NULL) - { - env->FatalError( - "No such class, className: " - "org/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes"); - } - jmethodID txBytesMtd = env->GetMethodID(txBytesClass, "", "()V"); - if (txBytesMtd == NULL) - { - env->FatalError("No such constructor in TransactionBytes, constructor()"); - } - jfieldID bufferField = env->GetFieldID(txBytesClass, "buffer", "[B"); - jfieldID lengthField = env->GetFieldID(txBytesClass, "length", "I"); - - jobject jTxBytesObj = env->NewObject(txBytesClass, txBytesMtd); - // buffer - jbyteArray bufferArray = env->NewByteArray(bytes_struct->length); - env->SetByteArrayRegion( - bufferArray, 0, bytes_struct->length, reinterpret_cast(bytes_struct->buffer)); - env->SetObjectField(jTxBytesObj, bufferField, bufferArray); - // length - env->SetIntField(jTxBytesObj, lengthField, bytes_struct->length); - - env->DeleteLocalRef(bufferArray); - - return jTxBytesObj; -} - -struct bcos_sdk_c_transaction_data_v2* convert_to_tx_data_struct_v2( - JNIEnv* env, jobject transactionObject) -{ - if (transactionObject == NULL) - { - THROW_JNI_EXCEPTION( - env, "exception in convert_to_tx_data_struct_v2, transactionObject is nullptr"); - return NULL; - } - - bcos_sdk_c_transaction_data_v2* tx_data_struct_v2 = - (struct bcos_sdk_c_transaction_data_v2*)malloc( - sizeof(struct bcos_sdk_c_transaction_data_v2)); - jclass txDataClass = env->GetObjectClass(transactionObject); - - // version - jfieldID versionField = env->GetFieldID(txDataClass, "version", "I"); - tx_data_struct_v2->version = env->GetIntField(transactionObject, versionField); - - // blockLimit - jfieldID blockLimitField = env->GetFieldID(txDataClass, "blockLimit", "J"); - tx_data_struct_v2->block_limit = env->GetLongField(transactionObject, blockLimitField); - - // chainId - jfieldID chainIdField = env->GetFieldID(txDataClass, "chainId", "Ljava/lang/String;"); - jstring chainIdString = (jstring)env->GetObjectField(transactionObject, chainIdField); - const char* chainIdValue = env->GetStringUTFChars(chainIdString, nullptr); - tx_data_struct_v2->chain_id = strdup(chainIdValue); - - // groupId - jfieldID groupIdField = env->GetFieldID(txDataClass, "groupId", "Ljava/lang/String;"); - jstring groupIdString = (jstring)env->GetObjectField(transactionObject, groupIdField); - const char* groupIdValue = env->GetStringUTFChars(groupIdString, nullptr); - tx_data_struct_v2->group_id = strdup(groupIdValue); - - // nonce - jfieldID nonceField = env->GetFieldID(txDataClass, "nonce", "Ljava/lang/String;"); - jstring nonceString = (jstring)env->GetObjectField(transactionObject, nonceField); - const char* nonceValue = env->GetStringUTFChars(nonceString, nullptr); - tx_data_struct_v2->nonce = strdup(nonceValue); - - // to - jfieldID toField = env->GetFieldID(txDataClass, "to", "Ljava/lang/String;"); - jstring toString = (jstring)env->GetObjectField(transactionObject, toField); - const char* toValue = env->GetStringUTFChars(toString, nullptr); - tx_data_struct_v2->to = strdup(toValue); - - // abi - jfieldID abiField = env->GetFieldID(txDataClass, "abi", "Ljava/lang/String;"); - jstring abiString = (jstring)env->GetObjectField(transactionObject, abiField); - const char* abiValue = env->GetStringUTFChars(abiString, nullptr); - tx_data_struct_v2->abi = strdup(abiValue); - - // input - jfieldID inputField = env->GetFieldID( - txDataClass, "input", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jobject inputObject = env->GetObjectField(transactionObject, inputField); - struct bcos_sdk_c_bytes* inputStruct = convert_to_bytes_struct_v2(env, inputObject); - if (inputStruct == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_struct_v2"); - return NULL; - } - tx_data_struct_v2->input = inputStruct; - - // value - jfieldID valueField = env->GetFieldID(txDataClass, "value", "Ljava/lang/String;"); - jstring valueString = (jstring)env->GetObjectField(transactionObject, valueField); - const char* valueChar = env->GetStringUTFChars(valueString, nullptr); - tx_data_struct_v2->value = strdup(valueChar); - - // gas_price - jfieldID gasPriceField = env->GetFieldID(txDataClass, "gasPrice", "Ljava/lang/String;"); - jstring gasPriceString = (jstring)env->GetObjectField(transactionObject, gasPriceField); - const char* gasPriceChar = env->GetStringUTFChars(gasPriceString, nullptr); - tx_data_struct_v2->gas_price = strdup(gasPriceChar); - - // gas_limit - jfieldID gasLimitField = env->GetFieldID(txDataClass, "gasLimit", "J"); - tx_data_struct_v2->gas_limit = env->GetLongField(transactionObject, gasLimitField); - - // max_fee_per_gas - jfieldID maxFeePerGasField = env->GetFieldID(txDataClass, "maxFeePerGas", "Ljava/lang/String;"); - jstring maxFeePerGasString = (jstring)env->GetObjectField(transactionObject, maxFeePerGasField); - const char* maxFeePerGasChar = env->GetStringUTFChars(maxFeePerGasString, nullptr); - tx_data_struct_v2->max_fee_per_gas = strdup(maxFeePerGasChar); - - // max_priority_fee_per_gas - jfieldID maxPriorityFeePerGasField = - env->GetFieldID(txDataClass, "maxPriorityFeePerGas", "Ljava/lang/String;"); - jstring maxPriorityFeePerGasString = - (jstring)env->GetObjectField(transactionObject, maxPriorityFeePerGasField); - const char* maxPriorityFeePerGasChar = - env->GetStringUTFChars(maxPriorityFeePerGasString, nullptr); - tx_data_struct_v2->max_priority_fee_per_gas = strdup(maxPriorityFeePerGasChar); - - // release source - env->ReleaseStringUTFChars(chainIdString, chainIdValue); - env->ReleaseStringUTFChars(groupIdString, groupIdValue); - env->ReleaseStringUTFChars(nonceString, nonceValue); - env->ReleaseStringUTFChars(toString, toValue); - env->ReleaseStringUTFChars(abiString, abiValue); - env->ReleaseStringUTFChars(valueString, valueChar); - env->ReleaseStringUTFChars(gasPriceString, gasPriceChar); - env->ReleaseStringUTFChars(maxFeePerGasString, maxFeePerGasChar); - env->ReleaseStringUTFChars(maxPriorityFeePerGasString, maxPriorityFeePerGasChar); - - return tx_data_struct_v2; -} - -jobject convert_to_tx_data_jobject_v2( - JNIEnv* env, const struct bcos_sdk_c_transaction_data_v2* transactionDataV2) -{ - if (transactionDataV2 == nullptr) - { - THROW_JNI_EXCEPTION( - env, "exception in convert_to_tx_data_jobject_v2, transactionDataV2 is nullptr"); - return NULL; - } - - jclass txDataClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2"); - if (txDataClass == NULL) - { - env->FatalError( - "No such class, className: " - "org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2"); - } - jmethodID txDataMtd = env->GetMethodID(txDataClass, "", "()V"); - if (txDataMtd == NULL) - { - env->FatalError("No such constructor in TransactionDataV2, constructor()"); - } - jobject jTxDataObj = env->NewObject(txDataClass, txDataMtd); - - // version - jfieldID versionField = env->GetFieldID(txDataClass, "version", "I"); - env->SetIntField(jTxDataObj, versionField, transactionDataV2->version); - // block_limit - jfieldID blockLimitField = env->GetFieldID(txDataClass, "blockLimit", "J"); - env->SetLongField(jTxDataObj, blockLimitField, transactionDataV2->block_limit); - // chain_id - jfieldID chainIdField = env->GetFieldID(txDataClass, "chainId", "Ljava/lang/String;"); - env->SetObjectField(jTxDataObj, chainIdField, env->NewStringUTF(transactionDataV2->chain_id)); - // group_id - jfieldID groupIdField = env->GetFieldID(txDataClass, "groupId", "Ljava/lang/String;"); - env->SetObjectField(jTxDataObj, groupIdField, env->NewStringUTF(transactionDataV2->group_id)); - // nonce - jfieldID nonceField = env->GetFieldID(txDataClass, "nonce", "Ljava/lang/String;"); - env->SetObjectField(jTxDataObj, nonceField, env->NewStringUTF(transactionDataV2->nonce)); - // to - jfieldID toField = env->GetFieldID(txDataClass, "to", "Ljava/lang/String;"); - env->SetObjectField(jTxDataObj, toField, env->NewStringUTF(transactionDataV2->to)); - // abi - jfieldID abiField = env->GetFieldID(txDataClass, "abi", "Ljava/lang/String;"); - env->SetObjectField(jTxDataObj, abiField, env->NewStringUTF(transactionDataV2->abi)); - // input - jobject jInput = convert_to_bytes_jobject_v2(env, transactionDataV2->input); - if (jInput == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_jobject_v2"); - return NULL; - } - jfieldID inputField = env->GetFieldID( - txDataClass, "input", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - env->SetObjectField(jTxDataObj, inputField, jInput); - // value - jfieldID valueField = env->GetFieldID(txDataClass, "value", "Ljava/lang/String;"); - env->SetObjectField(jTxDataObj, valueField, env->NewStringUTF(transactionDataV2->value)); - // gas_price - jfieldID gasPriceField = env->GetFieldID(txDataClass, "gasPrice", "Ljava/lang/String;"); - env->SetObjectField(jTxDataObj, gasPriceField, env->NewStringUTF(transactionDataV2->gas_price)); - // gas_limit - jfieldID gasLimitField = env->GetFieldID(txDataClass, "gasLimit", "J"); - env->SetLongField(jTxDataObj, gasLimitField, transactionDataV2->gas_limit); - // max_fee_per_gas - jfieldID maxFeePerGasField = env->GetFieldID(txDataClass, "maxFeePerGas", "Ljava/lang/String;"); - env->SetObjectField( - jTxDataObj, maxFeePerGasField, env->NewStringUTF(transactionDataV2->max_fee_per_gas)); - // max_priority_fee_per_gas - jfieldID maxPriorityFeePerGasField = - env->GetFieldID(txDataClass, "maxPriorityFeePerGas", "Ljava/lang/String;"); - env->SetObjectField(jTxDataObj, maxPriorityFeePerGasField, - env->NewStringUTF(transactionDataV2->max_priority_fee_per_gas)); - - // release - env->DeleteLocalRef(jInput); - - return jTxDataObj; -} - -jobject convert_to_tx_jobject_v2(JNIEnv* env, const struct bcos_sdk_c_transaction_v2* tx_struct_v2) -{ - if (tx_struct_v2 == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_jobject_v2, tx_struct_v2 is nullptr"); - return NULL; - } - - jclass txClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionV2"); - if (txClass == NULL) - { - env->FatalError( - "No such class, className: " - "org/fisco/bcos/sdk/jni/utilities/tx/TransactionV2"); - } - jmethodID txMtd = env->GetMethodID(txClass, "", "()V"); - if (txMtd == NULL) - { - env->FatalError("No such constructor in TransactionV2, constructor()"); - } - - jfieldID txDataField = env->GetFieldID( - txClass, "transactionData", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;"); - jfieldID dataHashField = env->GetFieldID( - txClass, "dataHash", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jfieldID signatureField = env->GetFieldID( - txClass, "signature", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jfieldID senderField = env->GetFieldID( - txClass, "sender", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jfieldID importTimeField = env->GetFieldID(txClass, "importTime", "J"); - jfieldID attributeField = env->GetFieldID(txClass, "attribute", "I"); - jfieldID extraDataField = env->GetFieldID(txClass, "extraData", "Ljava/lang/String;"); - - jobject javaTxObj = env->NewObject(txClass, txMtd); - // TransactionData - jobject javaTxDataObj = convert_to_tx_data_jobject_v2(env, tx_struct_v2->transaction_data); - if (javaTxDataObj == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_jobject_v2"); - return NULL; - } - env->SetObjectField(javaTxObj, txDataField, javaTxDataObj); - // DataHash - jobject javaDataHashObj = convert_to_bytes_jobject_v2(env, tx_struct_v2->data_hash); - if (javaDataHashObj == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_jobject_v2"); - return NULL; - } - env->SetObjectField(javaTxObj, dataHashField, javaDataHashObj); - // Signature - jobject javaSignatureObj = convert_to_bytes_jobject_v2(env, tx_struct_v2->signature); - if (javaSignatureObj == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_jobject_v2"); - return NULL; - } - env->SetObjectField(javaTxObj, signatureField, javaSignatureObj); - // Sender - jobject javaSenderObj = convert_to_bytes_jobject_v2(env, tx_struct_v2->sender); - env->SetObjectField(javaTxObj, senderField, javaSenderObj); - // ImportTime - env->SetLongField(javaTxObj, importTimeField, tx_struct_v2->import_time); - // Attribute - env->SetIntField(javaTxObj, attributeField, tx_struct_v2->attribute); - // ExtraData - jstring javaExtraData = env->NewStringUTF(tx_struct_v2->extra_data); - env->SetObjectField(javaTxObj, extraDataField, javaExtraData); - - return javaTxObj; -} - -struct bcos_sdk_c_transaction_v2* convert_to_tx_struct_v2(JNIEnv* env, jobject jTxObj) -{ - if (jTxObj == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct_v2, jTxObj is nullptr"); - return NULL; - } - - jclass javaTxClass = env->GetObjectClass(jTxObj); - jfieldID transactionDataField = env->GetFieldID( - javaTxClass, "transactionData", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;"); - jfieldID dataHashField = env->GetFieldID( - javaTxClass, "dataHash", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jfieldID signatureField = env->GetFieldID( - javaTxClass, "signature", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jfieldID senderField = env->GetFieldID( - javaTxClass, "sender", "Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionBytes;"); - jfieldID importTimeField = env->GetFieldID(javaTxClass, "importTime", "J"); - jfieldID attributeField = env->GetFieldID(javaTxClass, "attribute", "I"); - jfieldID extraDataField = env->GetFieldID(javaTxClass, "extraData", "Ljava/lang/String;"); - - struct bcos_sdk_c_transaction_v2* txStruct = - (struct bcos_sdk_c_transaction_v2*)malloc(sizeof(struct bcos_sdk_c_transaction_v2)); - // TransactionData - jobject javaTxDataObj = env->GetObjectField(jTxObj, transactionDataField); - struct bcos_sdk_c_transaction_data_v2* txDataStruct = - convert_to_tx_data_struct_v2(env, javaTxDataObj); - if (txDataStruct == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct_v2"); - return NULL; - } - txStruct->transaction_data = txDataStruct; - // DataHash - jobject javaDataHashObj = env->GetObjectField(jTxObj, dataHashField); - struct bcos_sdk_c_bytes* dataHashStruct = convert_to_bytes_struct_v2(env, javaDataHashObj); - if (dataHashStruct == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_struct_v2"); - return NULL; - } - txStruct->data_hash = dataHashStruct; - // Signature - jobject javaSignatureObj = env->GetObjectField(jTxObj, signatureField); - struct bcos_sdk_c_bytes* signatureStruct = convert_to_bytes_struct_v2(env, javaSignatureObj); - if (signatureStruct == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_bytes_struct_v2"); - return NULL; - } - txStruct->signature = signatureStruct; - // Sender - jobject javaSenderObj = env->GetObjectField(jTxObj, senderField); - struct bcos_sdk_c_bytes* senderStruct = convert_to_bytes_struct_v2(env, javaSenderObj); - txStruct->sender = senderStruct; - // ImportTime - jlong importTimeValue = env->GetLongField(jTxObj, importTimeField); - txStruct->import_time = (int64_t)importTimeValue; - // Attribute - jint attributeValue = env->GetIntField(jTxObj, attributeField); - txStruct->attribute = (int32_t)attributeValue; - // ExtraData - jstring javaExtraData = (jstring)env->GetObjectField(jTxObj, extraDataField); - const char* extraDataValue = env->GetStringUTFChars(javaExtraData, NULL); - txStruct->extra_data = strdup(extraDataValue); - - env->ReleaseStringUTFChars(javaExtraData, extraDataValue); - - return txStruct; -} - -/* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj - * Method: encodeTransactionDataStructV2 - * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;)Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_encodeTransactionDataStructV2( - JNIEnv* env, jclass, jobject jTransactionDataObj) -{ - struct bcos_sdk_c_transaction_data_v2* tx_data_struct_v2 = - convert_to_tx_data_struct_v2(env, jTransactionDataObj); - if (tx_data_struct_v2 == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct_v2"); - return nullptr; - } - const char* tx_data_hex = bcos_sdk_encode_transaction_data_struct_to_hex_v2(tx_data_struct_v2); - if (!bcos_sdk_is_last_opr_success()) - { - THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); - return nullptr; - } - - jstring jTxDataHex = env->NewStringUTF(tx_data_hex); - - if (tx_data_hex) - { - free((void*)tx_data_hex); - tx_data_hex = NULL; - } - if (tx_data_struct_v2) - { - bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct_v2); - } - - return jTxDataHex; -} - -/* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj - * Method: encodeTransactionDataStructToJsonV2 - * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;)Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_encodeTransactionDataStructToJsonV2( - JNIEnv* env, jclass, jobject jTransactionDataObj) -{ - struct bcos_sdk_c_transaction_data_v2* tx_data_struct_v2 = - convert_to_tx_data_struct_v2(env, jTransactionDataObj); - if (tx_data_struct_v2 == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct_v2"); - return nullptr; - } - const char* tx_data_json = - bcos_sdk_encode_transaction_data_struct_to_json_v2(tx_data_struct_v2); - if (!bcos_sdk_is_last_opr_success()) - { - THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); - return nullptr; - } - - jstring jTxDataJson = env->NewStringUTF(tx_data_json); - - if (tx_data_json) - { - free((void*)tx_data_json); - tx_data_json = NULL; - } - if (tx_data_struct_v2) - { - bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct_v2); - } - - return jTxDataJson; -} - -/* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj - * Method: decodeTransactionDataStructV2 - * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2; - */ -JNIEXPORT jobject JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_decodeTransactionDataStructV2( - JNIEnv* env, jclass, jstring jTxDataHexStr) -{ - checkJString(env, jTxDataHexStr); - - const char* tx_data_hex_str = env->GetStringUTFChars(jTxDataHexStr, nullptr); - struct bcos_sdk_c_transaction_data_v2* tx_data_struct_v2 = - bcos_sdk_decode_transaction_data_struct_from_hex_v2(tx_data_hex_str); - if (!bcos_sdk_is_last_opr_success()) - { - THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); - return nullptr; - } - - jobject jTxDataObj = convert_to_tx_data_jobject_v2(env, tx_data_struct_v2); - if (jTxDataObj == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_jobject_v2"); - return nullptr; - } - - // release source - if (tx_data_struct_v2) - { - bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct_v2); - } - env->ReleaseStringUTFChars(jTxDataHexStr, tx_data_hex_str); - - return jTxDataObj; -} - -/* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj - * Method: calcTransactionDataStructHashV2 - * Signature: (ILorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;)Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_calcTransactionDataStructHashV2( - JNIEnv* env, jclass, jint jCrytpTyte, jobject jTransactionDataObj) -{ - struct bcos_sdk_c_transaction_data_v2* tx_data_struct_v2 = - convert_to_tx_data_struct_v2(env, jTransactionDataObj); - if (tx_data_struct_v2 == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct_v2"); - return nullptr; - } - int crypto_type = jCrytpTyte; - const char* tx_data_hash = - bcos_sdk_calc_transaction_data_struct_hash_v2(crypto_type, tx_data_struct_v2); - if (!bcos_sdk_is_last_opr_success()) - { - THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); - return nullptr; - } - - jstring jTxDataHash = env->NewStringUTF(tx_data_hash); - - // release source - if (tx_data_struct_v2) - { - bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct_v2); - } - if (tx_data_hash) - { - free((void*)tx_data_hash); - tx_data_hash = NULL; - } - - return jTxDataHash; -} - -/* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj - * Method: createEncodedTransactionV2 - * Signature: - * (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;)Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_createEncodedTransactionV2( - JNIEnv* env, jclass, jobject jTxDataObj, jstring jSignature, jstring jTxDataHash, - jint jAttribute, jstring jExtraData) -{ - checkJString(env, jSignature); - checkJString(env, jTxDataHash); - checkJString(env, jExtraData); - - struct bcos_sdk_c_transaction_data_v2* tx_data_struct_v2 = - convert_to_tx_data_struct_v2(env, jTxDataObj); - if (tx_data_struct_v2 == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_data_struct_v2"); - return nullptr; - } - const char* signature = env->GetStringUTFChars(jSignature, NULL); - const char* tx_data_hash = env->GetStringUTFChars(jTxDataHash, NULL); - int attribute = jAttribute; - const char* extra_data = env->GetStringUTFChars(jExtraData, NULL); - - const char* tx_str = bcos_sdk_create_encoded_transaction_v2( - tx_data_struct_v2, signature, tx_data_hash, attribute, extra_data); - if (!bcos_sdk_is_last_opr_success()) - { - THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); - return nullptr; - } - - jstring jTxStr = env->NewStringUTF(tx_str); - - // release resource - env->ReleaseStringUTFChars(jSignature, signature); - env->ReleaseStringUTFChars(jTxDataHash, tx_data_hash); - env->ReleaseStringUTFChars(jExtraData, extra_data); - if (tx_data_struct_v2) - { - bcos_sdk_destroy_transaction_data_struct_v2(tx_data_struct_v2); - } - if (tx_str) - { - free((void*)tx_str); - tx_str = NULL; - } - - return jTxStr; -} - -/* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj - * Method: encodeTransactionStructV2 - * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionV2;)Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_encodeTransactionStructV2( - JNIEnv* env, jclass, jobject jTransactionObj) -{ - struct bcos_sdk_c_transaction_v2* tx_struct_v2 = convert_to_tx_struct_v2(env, jTransactionObj); - if (tx_struct_v2 == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct_v2"); - return nullptr; - } - - const char* tx_hex = bcos_sdk_encode_transaction_struct_to_hex_v2(tx_struct_v2); - if (!bcos_sdk_is_last_opr_success()) - { - THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); - return nullptr; - } - - jstring jTxHex = env->NewStringUTF(tx_hex); - - // release source - if (tx_hex) - { - free((void*)tx_hex); - tx_hex = NULL; - } - if (tx_struct_v2) - { - bcos_sdk_destroy_transaction_struct_v2(tx_struct_v2); - } - - return jTxHex; -} - -/* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj - * Method: encodeTransactionStructToJsonV2 - * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionV2;)Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_encodeTransactionStructToJsonV2( - JNIEnv* env, jclass, jobject jTransactionObj) -{ - struct bcos_sdk_c_transaction_v2* tx_struct_v2 = convert_to_tx_struct_v2(env, jTransactionObj); - if (tx_struct_v2 == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_struct_v2"); - return nullptr; - } - - const char* tx_json = bcos_sdk_encode_transaction_struct_to_json_v2(tx_struct_v2); - if (!bcos_sdk_is_last_opr_success()) - { - THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); - return nullptr; - } - - jstring jTxJson = env->NewStringUTF(tx_json); - - if (tx_json) - { - free((void*)tx_json); - tx_json = NULL; - } - if (tx_struct_v2) - { - bcos_sdk_destroy_transaction_struct_v2(tx_struct_v2); - } - - return jTxJson; -} - -/* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj - * Method: decodeTransactionStructV2 - * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionV2; - */ -JNIEXPORT jobject JNICALL -Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_decodeTransactionStructV2( - JNIEnv* env, jclass, jstring jTxHexStr) -{ - checkJString(env, jTxHexStr); - - const char* tx_hex_str = env->GetStringUTFChars(jTxHexStr, nullptr); - struct bcos_sdk_c_transaction_v2* tx_struct_v2 = - bcos_sdk_decode_transaction_struct_from_hex_v2(tx_hex_str); - if (!bcos_sdk_is_last_opr_success()) - { - THROW_JNI_EXCEPTION(env, bcos_sdk_get_last_error_msg()); - return nullptr; - } - - jobject jTxObj = convert_to_tx_jobject_v2(env, tx_struct_v2); - if (jTxObj == NULL) - { - THROW_JNI_EXCEPTION(env, "exception in convert_to_tx_jobject_v2"); - return nullptr; - } - - // release source - if (tx_struct_v2) - { - bcos_sdk_destroy_transaction_struct_v2(tx_struct_v2); - } - env->ReleaseStringUTFChars(jTxHexStr, tx_hex_str); - - return jTxObj; -} diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj.h b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj.h deleted file mode 100644 index 425cda94e..000000000 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj.h +++ /dev/null @@ -1,77 +0,0 @@ -/* DO NOT EDIT THIS FILE - it is machine generated */ -#include -/* Header for class org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj */ - -#ifndef _Included_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj -#define _Included_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj -#ifdef __cplusplus -extern "C" { -#endif -/* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj - * Method: encodeTransactionDataStructV2 - * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;)Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_encodeTransactionDataStructV2 - (JNIEnv *, jclass, jobject); - -/* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj - * Method: encodeTransactionDataStructToJsonV2 - * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;)Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_encodeTransactionDataStructToJsonV2 - (JNIEnv *, jclass, jobject); - -/* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj - * Method: decodeTransactionDataStructV2 - * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2; - */ -JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_decodeTransactionDataStructV2 - (JNIEnv *, jclass, jstring); - -/* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj - * Method: calcTransactionDataStructHashV2 - * Signature: (ILorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;)Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_calcTransactionDataStructHashV2 - (JNIEnv *, jclass, jint, jobject); - -/* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj - * Method: createEncodedTransactionV2 - * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2;Ljava/lang/String;Ljava/lang/String;ILjava/lang/String;)Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_createEncodedTransactionV2 - (JNIEnv *, jclass, jobject, jstring, jstring, jint, jstring); - -/* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj - * Method: encodeTransactionStructV2 - * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionV2;)Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_encodeTransactionStructV2 - (JNIEnv *, jclass, jobject); - -/* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj - * Method: encodeTransactionStructToJsonV2 - * Signature: (Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionV2;)Ljava/lang/String; - */ -JNIEXPORT jstring JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_encodeTransactionStructToJsonV2 - (JNIEnv *, jclass, jobject); - -/* - * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj - * Method: decodeTransactionStructV2 - * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionV2; - */ -JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderV2JniObj_decodeTransactionStructV2 - (JNIEnv *, jclass, jstring); - -#ifdef __cplusplus -} -#endif -#endif diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/Transaction.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/Transaction.java index c5a8adbf7..a0ad0c9e2 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/Transaction.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/Transaction.java @@ -4,7 +4,7 @@ public class Transaction { private TransactionData transactionData; private byte[] dataHash; private byte[] signature; - private byte[] sender; + private byte[] sender; private long importTime; private int attribute; private String extraData; diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV2JniObj.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV1JniObj.java similarity index 99% rename from bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV2JniObj.java rename to bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV1JniObj.java index f2cbfff21..88e0499c0 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV2JniObj.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionBuilderV1JniObj.java @@ -3,7 +3,7 @@ import org.fisco.bcos.sdk.jni.common.JniException; import org.fisco.bcos.sdk.jni.common.JniLibLoader; -public class TransactionBuilderV2JniObj extends TransactionBuilderJniObj { +public class TransactionBuilderV1JniObj extends TransactionBuilderJniObj { static { JniLibLoader.loadJniLibrary(); diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV1.java similarity index 94% rename from bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2.java rename to bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV1.java index e83bb4796..7d3d5b55f 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV2.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV1.java @@ -1,6 +1,6 @@ package org.fisco.bcos.sdk.jni.utilities.tx; -public class TransactionDataV2 extends TransactionData { +public class TransactionDataV1 extends TransactionData { protected String value; protected String gasPrice; protected long gasLimit; diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderJniObj.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderJniObj.java index dfe34ac0e..400320b61 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderJniObj.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionStructBuilderJniObj.java @@ -34,8 +34,8 @@ public static native TransactionData decodeTransactionDataStruct(String transact * @param transactionDataHex transactionData hex string * @return TxData */ - public static native TransactionDataV2 decodeTransactionDataStructV2(String transactionDataHex) - throws JniException; + public static native TransactionDataV1 decodeTransactionDataStructV1(String transactionDataHex) + throws JniException; /** * @param cryptoType crypto type @@ -85,6 +85,6 @@ public static native Transaction decodeTransactionStruct(String transactionHex) * @param transactionHex transaction hex string * @return Tx */ - public static native Transaction decodeTransactionStructV2(String transactionHex) - throws JniException; + public static native Transaction decodeTransactionStructV1(String transactionHex) + throws JniException; } diff --git a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion.java b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion.java index 79d1d0a0b..ba96756f3 100644 --- a/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion.java +++ b/bindings/java/jni/src/main/java/org/fisco/bcos/sdk/jni/utilities/tx/TransactionVersion.java @@ -1,27 +1,27 @@ package org.fisco.bcos.sdk.jni.utilities.tx; public enum TransactionVersion { - V0(0), - V1(1); + V0(0), + V1(1); - private int value; + private int value; - TransactionVersion(int value) { - this.value = value; - } + TransactionVersion(int value) { + this.value = value; + } - public int getValue() { - return value; - } + public int getValue() { + return value; + } - public static TransactionVersion fromInt(int value) { - switch (value) { - case 0: - return V0; - case 1: - return V1; - default: - return V0; - } + public static TransactionVersion fromInt(int value) { + switch (value) { + case 0: + return V0; + case 1: + return V1; + default: + return V0; } + } } diff --git a/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java b/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java index e4fbf7120..78b605c5a 100644 --- a/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java +++ b/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStruct.java @@ -4,12 +4,10 @@ import java.util.Arrays; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutionException; - import org.fisco.bcos.sdk.jni.BcosSDKJniObj; import org.fisco.bcos.sdk.jni.common.JniConfig; import org.fisco.bcos.sdk.jni.common.JniException; import org.fisco.bcos.sdk.jni.common.Response; -import org.fisco.bcos.sdk.jni.rpc.RpcCallback; import org.fisco.bcos.sdk.jni.rpc.RpcJniObj; import org.fisco.bcos.sdk.jni.test.Utility; import org.fisco.bcos.sdk.jni.utilities.keypair.KeyPairJniObj; @@ -18,28 +16,28 @@ public class TestTxStruct { - // ------------------------------------------------------------------------------ - // ------------------------------------------------------------------------------ - - // HelloWorld Source Code: - - // HelloWorld Source Code: - /** - * pragma solidity>=0.4.24 <0.6.11; - * - *

contract HelloWorld { string name; - * - *

constructor() public { name = "Hello, World!"; } - * - *

function get() public view returns (string memory) { return name; } - * - *

function set(string memory n) public { name = n; } } - */ - private static final String hwBIN = - "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610235565b5050565b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561022b5780601f106102005761010080835404028352916020019161022b565b820191906000526020600020905b81548152906001019060200180831161020e57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea2646970667358221220b5943f43c48cc93c6d71cdcf27aee5072566c88755ce9186e32ce83b24e8dc6c64736f6c634300060a0033"; - - private static final String hwSmBIN = - "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063299f7f9d1461003b5780633590b49f146100be575b600080fd5b610043610179565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610083578082015181840152602081019050610068565b50505050905090810190601f1680156100b05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610177600480360360208110156100d457600080fd5b81019080803590602001906401000000008111156100f157600080fd5b82018360208201111561010357600080fd5b8035906020019184600183028401116401000000008311171561012557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061021b565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102115780601f106101e657610100808354040283529160200191610211565b820191906000526020600020905b8154815290600101906020018083116101f457829003601f168201915b5050505050905090565b8060009080519060200190610231929190610235565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea26469706673582212209871cb2bcf390d53645807cbaedfe052d739ef9cff9d84787f74c4f379e1854664736f6c634300060a0033"; + // ------------------------------------------------------------------------------ + // ------------------------------------------------------------------------------ + + // HelloWorld Source Code: + + // HelloWorld Source Code: + /** + * pragma solidity>=0.4.24 <0.6.11; + * + *

contract HelloWorld { string name; + * + *

constructor() public { name = "Hello, World!"; } + * + *

function get() public view returns (string memory) { return name; } + * + *

function set(string memory n) public { name = n; } } + */ + private static final String hwBIN = + "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610235565b5050565b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561022b5780601f106102005761010080835404028352916020019161022b565b820191906000526020600020905b81548152906001019060200180831161020e57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea2646970667358221220b5943f43c48cc93c6d71cdcf27aee5072566c88755ce9186e32ce83b24e8dc6c64736f6c634300060a0033"; + + private static final String hwSmBIN = + "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063299f7f9d1461003b5780633590b49f146100be575b600080fd5b610043610179565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610083578082015181840152602081019050610068565b50505050905090810190601f1680156100b05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610177600480360360208110156100d457600080fd5b81019080803590602001906401000000008111156100f157600080fd5b82018360208201111561010357600080fd5b8035906020019184600183028401116401000000008311171561012557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061021b565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102115780601f106101e657610100808354040283529160200191610211565b820191906000526020600020905b8154815290600101906020018083116101f457829003601f168201915b5050505050905090565b8060009080519060200190610231929190610235565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea26469706673582212209871cb2bcf390d53645807cbaedfe052d739ef9cff9d84787f74c4f379e1854664736f6c634300060a0033"; /* { @@ -53,188 +51,182 @@ public class TestTxStruct { } */ - public static byte[] fromHex(String hexString) { - if (hexString.startsWith("0x")) { - hexString = hexString.substring(2); - } - - if (hexString.length() % 2 != 0) { - throw new IllegalArgumentException("Invalid hex string: " + hexString); - } + public static byte[] fromHex(String hexString) { + if (hexString.startsWith("0x")) { + hexString = hexString.substring(2); + } - int length = hexString.length() / 2; - byte[] bytes = new byte[length]; + if (hexString.length() % 2 != 0) { + throw new IllegalArgumentException("Invalid hex string: " + hexString); + } - for (int i = 0; i < length; i++) { - String twoChars = hexString.substring(i * 2, i * 2 + 2); - bytes[i] = (byte) Integer.parseInt(twoChars, 16); - } + int length = hexString.length() / 2; + byte[] bytes = new byte[length]; - return bytes; + for (int i = 0; i < length; i++) { + String twoChars = hexString.substring(i * 2, i * 2 + 2); + bytes[i] = (byte) Integer.parseInt(twoChars, 16); } - public static String generateNonce() { - byte[] nonceBytes = new byte[16]; - SecureRandom secureRandom = new SecureRandom(); - secureRandom.nextBytes(nonceBytes); - StringBuilder hex = new StringBuilder(); - for (byte b : nonceBytes) { - hex.append(String.format("%02x", b)); - } - return hex.toString(); - } + return bytes; + } - public static String getBinary(boolean isSM) { - return isSM ? hwSmBIN : hwBIN; + public static String generateNonce() { + byte[] nonceBytes = new byte[16]; + SecureRandom secureRandom = new SecureRandom(); + secureRandom.nextBytes(nonceBytes); + StringBuilder hex = new StringBuilder(); + for (byte b : nonceBytes) { + hex.append(String.format("%02x", b)); } + return hex.toString(); + } - public static void Usage() { - System.out.println("Desc: test transaction struct [HelloWorld set]"); - System.out.println( - "Usage: java -cp 'conf/:lib/*:apps/*' org.fisco.bcos.sdk.jni.test.tx.TestTxStruct"); - System.exit(0); - } + public static String getBinary(boolean isSM) { + return isSM ? hwSmBIN : hwBIN; + } + + public static void Usage() { + System.out.println("Desc: test transaction struct [HelloWorld set]"); + System.out.println( + "Usage: java -cp 'conf/:lib/*:apps/*' org.fisco.bcos.sdk.jni.test.tx.TestTxStruct"); + System.exit(0); + } - public static void main(String[] args) throws JniException, InterruptedException, ExecutionException { - if (args.length > 1) { - Usage(); - } - - String endpoint = "127.0.0.1:20200"; - String group = "group0"; - String node = ""; - JniConfig jniConfig = Utility.newJniConfig(Arrays.asList(endpoint)); - jniConfig.setDisableSsl(true); - BcosSDKJniObj bcosSDKJni = BcosSDKJniObj.build(jniConfig); - RpcJniObj rpcJniObj = RpcJniObj.build(bcosSDKJni.getNativePointer()); - System.out.println("build Rpc"); - rpcJniObj.start(); - - boolean smCrypto = false; - - long keyPair = KeyPairJniObj.createJniKeyPair(smCrypto ? 1 : 0); - String jniKeyPairAddress = KeyPairJniObj.getJniKeyPairAddress(keyPair); - - long blockLimit = 1111; - String groupID = "group0"; - String chainID = "chain0"; - String data = getBinary(smCrypto); - System.out.printf(" [test Tx Struct] new account, address: %s\n", jniKeyPairAddress); - - // construct TransactionData - TransactionData transactionDataStruct = new TransactionData(); - transactionDataStruct.setGroupId(groupID); - transactionDataStruct.setChainId(chainID); - transactionDataStruct.setTo(""); - transactionDataStruct.setAbi(""); - transactionDataStruct.setVersion(0); - transactionDataStruct.setNonce(generateNonce()); - transactionDataStruct.setBlockLimit(blockLimit); - // input - byte[] bytesInput = fromHex(data); - transactionDataStruct.setInput(bytesInput); - - // encode TxData to hex tx data - String txDataHex = - TransactionStructBuilderJniObj.encodeTransactionDataStruct(transactionDataStruct); - // decode hex tx data to TxData - TransactionData decodeTransactionDataStructHex = - TransactionStructBuilderJniObj.decodeTransactionDataStruct(txDataHex); - // assert - Assert.assertEquals( - transactionDataStruct.getChainId(), decodeTransactionDataStructHex.getChainId()); - Assert.assertEquals( - transactionDataStruct.getGroupId(), decodeTransactionDataStructHex.getGroupId()); - Assert.assertEquals(transactionDataStruct.getAbi(), decodeTransactionDataStructHex.getAbi()); - Assert.assertEquals( - transactionDataStruct.getBlockLimit(), decodeTransactionDataStructHex.getBlockLimit()); - - // encode TxData to json tx data - String txDataJson = - TransactionStructBuilderJniObj.encodeTransactionDataStructToJson(transactionDataStruct); - System.out.printf(" [test Tx Struct] txDataJson: %s\n", txDataJson); - - // calc tx data hash - String txDataHash = - TransactionStructBuilderJniObj.calcTransactionDataStructHash( - smCrypto ? 1 : 0, decodeTransactionDataStructHex); - System.out.printf(" [test Tx Struct] txDataHash: %s\n", txDataHash); - // signature tx data hash - String signature = TransactionBuilderJniObj.signTransactionDataHash(keyPair, txDataHash); - System.out.printf(" [test Tx Struct] signature: %s\n", signature); - - // construct tx - Transaction transactionStruct = new Transaction(); - transactionStruct.setTransactionData(decodeTransactionDataStructHex); - transactionStruct.setDataHash(fromHex(txDataHash)); - transactionStruct.setSignature(fromHex(signature)); - transactionStruct.setSender(null); - transactionStruct.setImportTime(0); - transactionStruct.setAttribute(0); - transactionStruct.setExtraData(""); - // assert - Assert.assertEquals( - transactionStruct.getTransactionData().getBlockLimit(), - decodeTransactionDataStructHex.getBlockLimit()); - Assert.assertEquals( - transactionStruct.getTransactionData().getGroupId(), - decodeTransactionDataStructHex.getGroupId()); - Assert.assertEquals( - transactionStruct.getTransactionData().getChainId(), - decodeTransactionDataStructHex.getChainId()); - Assert.assertEquals( - transactionStruct.getTransactionData().getAbi(), decodeTransactionDataStructHex.getAbi()); - Assert.assertArrayEquals(transactionStruct.getDataHash(), fromHex(txDataHash)); - Assert.assertArrayEquals(transactionStruct.getSignature(), fromHex(signature)); - - // encode Tx to hex tx - String txHex = TransactionStructBuilderJniObj.encodeTransactionStruct(transactionStruct); - // decode hex tx to Tx - Transaction decodeTransactionStructHex = - TransactionStructBuilderJniObj.decodeTransactionStruct(txHex); - // assert - Assert.assertEquals( - transactionStruct.getTransactionData().getBlockLimit(), - decodeTransactionStructHex.getTransactionData().getBlockLimit()); - Assert.assertEquals( - transactionStruct.getTransactionData().getGroupId(), - decodeTransactionStructHex.getTransactionData().getGroupId()); - Assert.assertEquals( - transactionStruct.getTransactionData().getChainId(), - decodeTransactionStructHex.getTransactionData().getChainId()); - Assert.assertEquals( - transactionStruct.getTransactionData().getAbi(), - decodeTransactionStructHex.getTransactionData().getAbi()); - Assert.assertArrayEquals( - transactionStruct.getDataHash(), - decodeTransactionStructHex.getDataHash()); - Assert.assertArrayEquals( - transactionStruct.getSignature(), - decodeTransactionStructHex.getSignature()); - - // encode Tx to json tx - String txJson = TransactionStructBuilderJniObj.encodeTransactionStructToJson(transactionStruct); - System.out.printf(" [test Tx Struct] txJson: %s\n", txJson); - // create tx string - String txString = - TransactionStructBuilderJniObj.createEncodedTransaction( - decodeTransactionDataStructHex, signature, txDataHash, 0, ""); - // System.out.printf(" [test Tx Struct] txString: %s\n", txString); - - CompletableFuture future = new CompletableFuture<>(); - // rpc send tx - rpcJniObj.sendTransaction( - group, - node, - txString, - false, - future::complete); - - Response response = future.get(); - System.out.println("response error code: ==>>> " + response.getErrorCode()); - String dataStr = new String(response.getData()); - System.out.println("response data: ==>>> " + dataStr); - rpcJniObj.stop(); - System.out.println(" [test Tx Struct] finish !!"); + public static void main(String[] args) + throws JniException, InterruptedException, ExecutionException { + if (args.length > 1) { + Usage(); } + + String endpoint = "127.0.0.1:20200"; + String group = "group0"; + String node = ""; + JniConfig jniConfig = Utility.newJniConfig(Arrays.asList(endpoint)); + jniConfig.setDisableSsl(true); + BcosSDKJniObj bcosSDKJni = BcosSDKJniObj.build(jniConfig); + RpcJniObj rpcJniObj = RpcJniObj.build(bcosSDKJni.getNativePointer()); + System.out.println("build Rpc"); + rpcJniObj.start(); + + boolean smCrypto = false; + + long keyPair = KeyPairJniObj.createJniKeyPair(smCrypto ? 1 : 0); + String jniKeyPairAddress = KeyPairJniObj.getJniKeyPairAddress(keyPair); + + long blockLimit = 1111; + String groupID = "group0"; + String chainID = "chain0"; + String data = getBinary(smCrypto); + System.out.printf(" [test Tx Struct] new account, address: %s\n", jniKeyPairAddress); + + // construct TransactionData + TransactionData transactionDataStruct = new TransactionData(); + transactionDataStruct.setGroupId(groupID); + transactionDataStruct.setChainId(chainID); + transactionDataStruct.setTo(""); + transactionDataStruct.setAbi(""); + transactionDataStruct.setVersion(0); + transactionDataStruct.setNonce(generateNonce()); + transactionDataStruct.setBlockLimit(blockLimit); + // input + byte[] bytesInput = fromHex(data); + transactionDataStruct.setInput(bytesInput); + + // encode TxData to hex tx data + String txDataHex = + TransactionStructBuilderJniObj.encodeTransactionDataStruct(transactionDataStruct); + // decode hex tx data to TxData + TransactionData decodeTransactionDataStructHex = + TransactionStructBuilderJniObj.decodeTransactionDataStruct(txDataHex); + // assert + Assert.assertEquals( + transactionDataStruct.getChainId(), decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals( + transactionDataStruct.getGroupId(), decodeTransactionDataStructHex.getGroupId()); + Assert.assertEquals(transactionDataStruct.getAbi(), decodeTransactionDataStructHex.getAbi()); + Assert.assertEquals( + transactionDataStruct.getBlockLimit(), decodeTransactionDataStructHex.getBlockLimit()); + + // encode TxData to json tx data + String txDataJson = + TransactionStructBuilderJniObj.encodeTransactionDataStructToJson(transactionDataStruct); + System.out.printf(" [test Tx Struct] txDataJson: %s\n", txDataJson); + + // calc tx data hash + String txDataHash = + TransactionStructBuilderJniObj.calcTransactionDataStructHash( + smCrypto ? 1 : 0, decodeTransactionDataStructHex); + System.out.printf(" [test Tx Struct] txDataHash: %s\n", txDataHash); + // signature tx data hash + String signature = TransactionBuilderJniObj.signTransactionDataHash(keyPair, txDataHash); + System.out.printf(" [test Tx Struct] signature: %s\n", signature); + + // construct tx + Transaction transactionStruct = new Transaction(); + transactionStruct.setTransactionData(decodeTransactionDataStructHex); + transactionStruct.setDataHash(fromHex(txDataHash)); + transactionStruct.setSignature(fromHex(signature)); + transactionStruct.setSender(null); + transactionStruct.setImportTime(0); + transactionStruct.setAttribute(0); + transactionStruct.setExtraData(""); + // assert + Assert.assertEquals( + transactionStruct.getTransactionData().getBlockLimit(), + decodeTransactionDataStructHex.getBlockLimit()); + Assert.assertEquals( + transactionStruct.getTransactionData().getGroupId(), + decodeTransactionDataStructHex.getGroupId()); + Assert.assertEquals( + transactionStruct.getTransactionData().getChainId(), + decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals( + transactionStruct.getTransactionData().getAbi(), decodeTransactionDataStructHex.getAbi()); + Assert.assertArrayEquals(transactionStruct.getDataHash(), fromHex(txDataHash)); + Assert.assertArrayEquals(transactionStruct.getSignature(), fromHex(signature)); + + // encode Tx to hex tx + String txHex = TransactionStructBuilderJniObj.encodeTransactionStruct(transactionStruct); + // decode hex tx to Tx + Transaction decodeTransactionStructHex = + TransactionStructBuilderJniObj.decodeTransactionStruct(txHex); + // assert + Assert.assertEquals( + transactionStruct.getTransactionData().getBlockLimit(), + decodeTransactionStructHex.getTransactionData().getBlockLimit()); + Assert.assertEquals( + transactionStruct.getTransactionData().getGroupId(), + decodeTransactionStructHex.getTransactionData().getGroupId()); + Assert.assertEquals( + transactionStruct.getTransactionData().getChainId(), + decodeTransactionStructHex.getTransactionData().getChainId()); + Assert.assertEquals( + transactionStruct.getTransactionData().getAbi(), + decodeTransactionStructHex.getTransactionData().getAbi()); + Assert.assertArrayEquals( + transactionStruct.getDataHash(), decodeTransactionStructHex.getDataHash()); + Assert.assertArrayEquals( + transactionStruct.getSignature(), decodeTransactionStructHex.getSignature()); + + // encode Tx to json tx + String txJson = TransactionStructBuilderJniObj.encodeTransactionStructToJson(transactionStruct); + System.out.printf(" [test Tx Struct] txJson: %s\n", txJson); + // create tx string + String txString = + TransactionStructBuilderJniObj.createEncodedTransaction( + decodeTransactionDataStructHex, signature, txDataHash, 0, ""); + // System.out.printf(" [test Tx Struct] txString: %s\n", txString); + + CompletableFuture future = new CompletableFuture<>(); + // rpc send tx + rpcJniObj.sendTransaction(group, node, txString, false, future::complete); + + Response response = future.get(); + System.out.println("response error code: ==>>> " + response.getErrorCode()); + String dataStr = new String(response.getData()); + System.out.println("response data: ==>>> " + dataStr); + rpcJniObj.stop(); + System.out.println(" [test Tx Struct] finish !!"); + } } diff --git a/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV1.java b/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV1.java new file mode 100644 index 000000000..5a09b7854 --- /dev/null +++ b/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV1.java @@ -0,0 +1,272 @@ +package org.fisco.bcos.sdk.jni.test.tx; + +import java.security.SecureRandom; +import java.util.Arrays; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import org.fisco.bcos.sdk.jni.BcosSDKJniObj; +import org.fisco.bcos.sdk.jni.common.JniConfig; +import org.fisco.bcos.sdk.jni.common.JniException; +import org.fisco.bcos.sdk.jni.common.Response; +import org.fisco.bcos.sdk.jni.rpc.RpcJniObj; +import org.fisco.bcos.sdk.jni.test.Utility; +import org.fisco.bcos.sdk.jni.utilities.keypair.KeyPairJniObj; +import org.fisco.bcos.sdk.jni.utilities.tx.*; +import org.junit.Assert; + +public class TestTxStructV1 { + + // ------------------------------------------------------------------------------ + // ------------------------------------------------------------------------------ + + // HelloWorld Source Code: + /** + * pragma solidity>=0.4.24 <0.6.11; + * + *

contract HelloWorld { string name; + * + *

constructor() public { name = "Hello, World!"; } + * + *

function get() public view returns (string memory) { return name; } + * + *

function set(string memory n) public { name = n; } } + */ + private static final String hwBIN = + "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610235565b5050565b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561022b5780601f106102005761010080835404028352916020019161022b565b820191906000526020600020905b81548152906001019060200180831161020e57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea2646970667358221220b5943f43c48cc93c6d71cdcf27aee5072566c88755ce9186e32ce83b24e8dc6c64736f6c634300060a0033"; + + private static final String hwSmBIN = + "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063299f7f9d1461003b5780633590b49f146100be575b600080fd5b610043610179565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610083578082015181840152602081019050610068565b50505050905090810190601f1680156100b05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610177600480360360208110156100d457600080fd5b81019080803590602001906401000000008111156100f157600080fd5b82018360208201111561010357600080fd5b8035906020019184600183028401116401000000008311171561012557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061021b565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102115780601f106101e657610100808354040283529160200191610211565b820191906000526020600020905b8154815290600101906020018083116101f457829003601f168201915b5050505050905090565b8060009080519060200190610231929190610235565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea26469706673582212209871cb2bcf390d53645807cbaedfe052d739ef9cff9d84787f74c4f379e1854664736f6c634300060a0033"; + + /* + { + "6d4ce63c": "get()", + "4ed3885e": "set(string)" + } + + { + "299f7f9d": "get()", + "3590b49f": "set(string)" + } + */ + + public static byte[] fromHex(String hexString) { + if (hexString.startsWith("0x")) { + hexString = hexString.substring(2); + } + + if (hexString.length() % 2 != 0) { + throw new IllegalArgumentException("Invalid hex string: " + hexString); + } + + int length = hexString.length() / 2; + byte[] bytes = new byte[length]; + + for (int i = 0; i < length; i++) { + String twoChars = hexString.substring(i * 2, i * 2 + 2); + bytes[i] = (byte) Integer.parseInt(twoChars, 16); + } + + return bytes; + } + + public static String generateNonce() { + byte[] nonceBytes = new byte[16]; + SecureRandom secureRandom = new SecureRandom(); + secureRandom.nextBytes(nonceBytes); + StringBuilder hex = new StringBuilder(); + for (byte b : nonceBytes) { + hex.append(String.format("%02x", b)); + } + return hex.toString(); + } + + public static String getBinary(boolean isSM) { + return isSM ? hwSmBIN : hwBIN; + } + + public static void Usage() { + System.out.println("Desc: test transaction struct V1 [HelloWorld set]"); + System.out.println( + "Usage: java -cp 'conf/:lib/*:apps/*' org.fisco.bcos.sdk.jni.test.tx.TestTxStructV1"); + System.exit(0); + } + + public static void main(String[] args) + throws JniException, InterruptedException, ExecutionException { + if (args.length > 1) { + Usage(); + } + + String endpoint = "127.0.0.1:20200"; + String group = "group0"; + String node = ""; + JniConfig jniConfig = Utility.newJniConfig(Arrays.asList(endpoint)); + jniConfig.setDisableSsl(true); + BcosSDKJniObj bcosSDKJni = BcosSDKJniObj.build(jniConfig); + RpcJniObj rpcJniObj = RpcJniObj.build(bcosSDKJni.getNativePointer()); + System.out.println("build Rpc"); + rpcJniObj.start(); + long blockLimit = rpcJniObj.getBlockLimit(group); + + boolean smCrypto = false; + + long keyPair = KeyPairJniObj.createJniKeyPair(smCrypto ? 1 : 0); + String jniKeyPairAddress = KeyPairJniObj.getJniKeyPairAddress(keyPair); + System.out.printf(" [test Tx Struct V1] new account, address: %s\n", jniKeyPairAddress); + + String groupID = "group0"; + String chainID = "chain0"; + String data = getBinary(smCrypto); + String value = "0x11"; + String gasPrice = "0x10"; + long gasLimit = 0; + String maxFeePerGas = "0x11"; + String maxPriorityFeePerGas = "0x22"; + + // construct TransactionDataV1 + TransactionDataV1 transactionDataStructV1 = new TransactionDataV1(); + transactionDataStructV1.setGroupId(groupID); + transactionDataStructV1.setChainId(chainID); + transactionDataStructV1.setTo(""); + transactionDataStructV1.setAbi(""); + transactionDataStructV1.setVersion(1); + transactionDataStructV1.setNonce(generateNonce()); + transactionDataStructV1.setBlockLimit(blockLimit); + transactionDataStructV1.setValue(value); + transactionDataStructV1.setGasPrice(gasPrice); + transactionDataStructV1.setGasLimit(gasLimit); + transactionDataStructV1.setMaxFeePerGas(maxFeePerGas); + transactionDataStructV1.setMaxPriorityFeePerGas(maxPriorityFeePerGas); + // input + byte[] bytesInput = fromHex(data); + transactionDataStructV1.setInput(bytesInput); + + // encode TxData to hex tx data + String txDataHex = + TransactionStructBuilderJniObj.encodeTransactionDataStruct(transactionDataStructV1); + // decode hex tx data to TxData + TransactionDataV1 decodeTransactionDataStructHex = + TransactionStructBuilderJniObj.decodeTransactionDataStructV1(txDataHex); + // TransactionDataV1 decodeTransactionDataStructHex2 = + // TransactionStructBuilderV1JniObj.decodeTransactionDataStructV1(null); + // TransactionDataV1 decodeTransactionDataStructHex3 = + // TransactionStructBuilderV1JniObj.decodeTransactionDataStructV1(""); + + // assert + Assert.assertEquals( + transactionDataStructV1.getChainId(), decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals( + transactionDataStructV1.getGroupId(), decodeTransactionDataStructHex.getGroupId()); + Assert.assertEquals(transactionDataStructV1.getAbi(), decodeTransactionDataStructHex.getAbi()); + Assert.assertEquals( + transactionDataStructV1.getBlockLimit(), decodeTransactionDataStructHex.getBlockLimit()); + Assert.assertEquals( + transactionDataStructV1.getValue(), decodeTransactionDataStructHex.getValue()); + Assert.assertEquals( + transactionDataStructV1.getGasPrice(), decodeTransactionDataStructHex.getGasPrice()); + Assert.assertEquals( + transactionDataStructV1.getGasLimit(), decodeTransactionDataStructHex.getGasLimit()); + Assert.assertEquals( + transactionDataStructV1.getMaxFeePerGas(), + decodeTransactionDataStructHex.getMaxFeePerGas()); + Assert.assertEquals( + transactionDataStructV1.getMaxPriorityFeePerGas(), + decodeTransactionDataStructHex.getMaxPriorityFeePerGas()); + + // encode TxData to json tx data + String txDataJson = + TransactionStructBuilderJniObj.encodeTransactionDataStructToJson(transactionDataStructV1); + System.out.printf(" [test Tx Struct V1] txDataJson: %s\n", txDataJson); + + // calc tx data hash + String txDataHash = + TransactionStructBuilderJniObj.calcTransactionDataStructHash( + smCrypto ? 1 : 0, decodeTransactionDataStructHex); + System.out.printf(" [test Tx Struct V1] txDataHash: %s\n", txDataHash); + // signature tx data hash + String signature = TransactionBuilderJniObj.signTransactionDataHash(keyPair, txDataHash); + System.out.printf(" [test Tx Struct V1] signature: %s\n", signature); + + // construct tx + Transaction transactionStructV1 = new Transaction(); + transactionStructV1.setTransactionData(decodeTransactionDataStructHex); + transactionStructV1.setDataHash(fromHex(txDataHash)); + transactionStructV1.setSignature(fromHex(signature)); + transactionStructV1.setSender(null); + transactionStructV1.setImportTime(0); + transactionStructV1.setAttribute(0); + transactionStructV1.setExtraData(""); + // assert + Assert.assertEquals( + transactionStructV1.getTransactionData().getBlockLimit(), + decodeTransactionDataStructHex.getBlockLimit()); + Assert.assertEquals( + transactionStructV1.getTransactionData().getGroupId(), + decodeTransactionDataStructHex.getGroupId()); + Assert.assertEquals( + transactionStructV1.getTransactionData().getChainId(), + decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals( + transactionStructV1.getTransactionData().getChainId(), + decodeTransactionDataStructHex.getChainId()); + Assert.assertEquals( + transactionStructV1.getTransactionData().getAbi(), decodeTransactionDataStructHex.getAbi()); + Assert.assertArrayEquals(transactionStructV1.getDataHash(), fromHex(txDataHash)); + Assert.assertArrayEquals(transactionStructV1.getSignature(), fromHex(signature)); + + // encode Tx to hex tx + String txHex = TransactionStructBuilderJniObj.encodeTransactionStruct(transactionStructV1); + // decode hex tx to Tx + Transaction decodeTransactionStructHex = + TransactionStructBuilderJniObj.decodeTransactionStructV1(txHex); + + // assert + Assert.assertEquals( + transactionStructV1.getTransactionData().getBlockLimit(), + decodeTransactionStructHex.getTransactionData().getBlockLimit()); + Assert.assertEquals( + transactionStructV1.getTransactionData().getGroupId(), + decodeTransactionStructHex.getTransactionData().getGroupId()); + Assert.assertEquals( + transactionStructV1.getTransactionData().getChainId(), + decodeTransactionStructHex.getTransactionData().getChainId()); + Assert.assertEquals( + transactionStructV1.getTransactionData().getAbi(), + decodeTransactionStructHex.getTransactionData().getAbi()); + TransactionDataV1 transactionData = + (TransactionDataV1) transactionStructV1.getTransactionData(); + TransactionDataV1 decodeTransactionData = + (TransactionDataV1) decodeTransactionStructHex.getTransactionData(); + Assert.assertEquals(transactionData.getValue(), decodeTransactionData.getValue()); + Assert.assertEquals(transactionData.getGasLimit(), decodeTransactionData.getGasLimit()); + Assert.assertEquals(transactionData.getGasPrice(), decodeTransactionData.getGasPrice()); + Assert.assertEquals(transactionData.getMaxFeePerGas(), decodeTransactionData.getMaxFeePerGas()); + Assert.assertEquals( + transactionData.getMaxPriorityFeePerGas(), decodeTransactionData.getMaxPriorityFeePerGas()); + Assert.assertArrayEquals( + transactionStructV1.getDataHash(), decodeTransactionStructHex.getDataHash()); + Assert.assertArrayEquals( + transactionStructV1.getSignature(), decodeTransactionStructHex.getSignature()); + + // encode Tx to json tx + String txJson = + TransactionStructBuilderJniObj.encodeTransactionStructToJson(transactionStructV1); + System.out.printf(" [test Tx Struct V1] txJson: %s\n", txJson); + // create tx string + String txString = + TransactionStructBuilderJniObj.createEncodedTransaction( + decodeTransactionDataStructHex, signature, txDataHash, 0, ""); + // System.out.printf(" [test Tx Struct V1] txString: %s\n", txString); + CompletableFuture future = new CompletableFuture<>(); + + // rpc send tx + rpcJniObj.sendTransaction(group, node, txString, false, future::complete); + + Response response = future.get(); + System.out.println("response error code: ==>>> " + response.getErrorCode()); + String dataStr = new String(response.getData()); + System.out.println("response data: ==>>> " + dataStr); + rpcJniObj.stop(); + System.out.println(" [test Tx Struct V1] finish !! "); + } +} diff --git a/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV2.java b/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV2.java deleted file mode 100644 index 16db16885..000000000 --- a/bindings/java/jni/src/test/java/org/fisco/bcos/sdk/jni/test/tx/TestTxStructV2.java +++ /dev/null @@ -1,293 +0,0 @@ -package org.fisco.bcos.sdk.jni.test.tx; - -import java.security.SecureRandom; -import java.util.Arrays; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.ExecutionException; - -import org.fisco.bcos.sdk.jni.BcosSDKJniObj; -import org.fisco.bcos.sdk.jni.common.JniConfig; -import org.fisco.bcos.sdk.jni.common.JniException; -import org.fisco.bcos.sdk.jni.common.Response; -import org.fisco.bcos.sdk.jni.rpc.RpcCallback; -import org.fisco.bcos.sdk.jni.rpc.RpcJniObj; -import org.fisco.bcos.sdk.jni.test.Utility; -import org.fisco.bcos.sdk.jni.utilities.keypair.KeyPairJniObj; -import org.fisco.bcos.sdk.jni.utilities.tx.*; -import org.junit.Assert; - -public class TestTxStructV2 { - - // ------------------------------------------------------------------------------ - // ------------------------------------------------------------------------------ - - // HelloWorld Source Code: - /** - * pragma solidity>=0.4.24 <0.6.11; - * - *

contract HelloWorld { string name; - * - *

constructor() public { name = "Hello, World!"; } - * - *

function get() public view returns (string memory) { return name; } - * - *

function set(string memory n) public { name = n; } } - */ - private static final String hwBIN = - "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c80634ed3885e1461003b5780636d4ce63c146100f6575b600080fd5b6100f46004803603602081101561005157600080fd5b810190808035906020019064010000000081111561006e57600080fd5b82018360208201111561008057600080fd5b803590602001918460018302840111640100000000831117156100a257600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f820116905080830192505050505050509192919290505050610179565b005b6100fe610193565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561013e578082015181840152602081019050610123565b50505050905090810190601f16801561016b5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b806000908051906020019061018f929190610235565b5050565b606060008054600181600116156101000203166002900480601f01602080910402602001604051908101604052809291908181526020018280546001816001161561010002031660029004801561022b5780601f106102005761010080835404028352916020019161022b565b820191906000526020600020905b81548152906001019060200180831161020e57829003601f168201915b5050505050905090565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea2646970667358221220b5943f43c48cc93c6d71cdcf27aee5072566c88755ce9186e32ce83b24e8dc6c64736f6c634300060a0033"; - - private static final String hwSmBIN = - "608060405234801561001057600080fd5b506040518060400160405280600d81526020017f48656c6c6f2c20576f726c6421000000000000000000000000000000000000008152506000908051906020019061005c929190610062565b50610107565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100a357805160ff19168380011785556100d1565b828001600101855582156100d1579182015b828111156100d05782518255916020019190600101906100b5565b5b5090506100de91906100e2565b5090565b61010491905b808211156101005760008160009055506001016100e8565b5090565b90565b610310806101166000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c8063299f7f9d1461003b5780633590b49f146100be575b600080fd5b610043610179565b6040518080602001828103825283818151815260200191508051906020019080838360005b83811015610083578082015181840152602081019050610068565b50505050905090810190601f1680156100b05780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b610177600480360360208110156100d457600080fd5b81019080803590602001906401000000008111156100f157600080fd5b82018360208201111561010357600080fd5b8035906020019184600183028401116401000000008311171561012557600080fd5b91908080601f016020809104026020016040519081016040528093929190818152602001838380828437600081840152601f19601f82011690508083019250505050505050919291929050505061021b565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156102115780601f106101e657610100808354040283529160200191610211565b820191906000526020600020905b8154815290600101906020018083116101f457829003601f168201915b5050505050905090565b8060009080519060200190610231929190610235565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061027657805160ff19168380011785556102a4565b828001600101855582156102a4579182015b828111156102a3578251825591602001919060010190610288565b5b5090506102b191906102b5565b5090565b6102d791905b808211156102d35760008160009055506001016102bb565b5090565b9056fea26469706673582212209871cb2bcf390d53645807cbaedfe052d739ef9cff9d84787f74c4f379e1854664736f6c634300060a0033"; - - /* - { - "6d4ce63c": "get()", - "4ed3885e": "set(string)" - } - - { - "299f7f9d": "get()", - "3590b49f": "set(string)" - } - */ - - public static byte[] fromHex(String hexString) { - if (hexString.startsWith("0x")) { - hexString = hexString.substring(2); - } - - if (hexString.length() % 2 != 0) { - throw new IllegalArgumentException("Invalid hex string: " + hexString); - } - - int length = hexString.length() / 2; - byte[] bytes = new byte[length]; - - for (int i = 0; i < length; i++) { - String twoChars = hexString.substring(i * 2, i * 2 + 2); - bytes[i] = (byte) Integer.parseInt(twoChars, 16); - } - - return bytes; - } - - public static String generateNonce() { - byte[] nonceBytes = new byte[16]; - SecureRandom secureRandom = new SecureRandom(); - secureRandom.nextBytes(nonceBytes); - StringBuilder hex = new StringBuilder(); - for (byte b : nonceBytes) { - hex.append(String.format("%02x", b)); - } - return hex.toString(); - } - - public static String getBinary(boolean isSM) { - return isSM ? hwSmBIN : hwBIN; - } - - public static void Usage() { - System.out.println("Desc: test transaction struct V2 [HelloWorld set]"); - System.out.println( - "Usage: java -cp 'conf/:lib/*:apps/*' org.fisco.bcos.sdk.jni.test.tx.TestTxStructV2"); - System.exit(0); - } - - public static void main(String[] args) throws JniException, InterruptedException, ExecutionException { - if (args.length > 1) { - Usage(); - } - - String endpoint = "127.0.0.1:20200"; - String group = "group0"; - String node = ""; - JniConfig jniConfig = Utility.newJniConfig(Arrays.asList(endpoint)); - jniConfig.setDisableSsl(true); - BcosSDKJniObj bcosSDKJni = BcosSDKJniObj.build(jniConfig); - RpcJniObj rpcJniObj = RpcJniObj.build(bcosSDKJni.getNativePointer()); - System.out.println("build Rpc"); - rpcJniObj.start(); - long blockLimit = rpcJniObj.getBlockLimit(group); - - boolean smCrypto = false; - - long keyPair = KeyPairJniObj.createJniKeyPair(smCrypto ? 1 : 0); - String jniKeyPairAddress = KeyPairJniObj.getJniKeyPairAddress(keyPair); - System.out.printf(" [test Tx Struct V2] new account, address: %s\n", jniKeyPairAddress); - - String groupID = "group0"; - String chainID = "chain0"; - String data = getBinary(smCrypto); - String value = "0x11"; - String gasPrice = "0x10"; - long gasLimit = 0; - String maxFeePerGas = "0x11"; - String maxPriorityFeePerGas = "0x22"; - - // construct TransactionDataV2 - TransactionDataV2 transactionDataStructV2 = new TransactionDataV2(); - transactionDataStructV2.setGroupId(groupID); - transactionDataStructV2.setChainId(chainID); - transactionDataStructV2.setTo(""); - transactionDataStructV2.setAbi(""); - transactionDataStructV2.setVersion(1); - transactionDataStructV2.setNonce(generateNonce()); - transactionDataStructV2.setBlockLimit(blockLimit); - transactionDataStructV2.setValue(value); - transactionDataStructV2.setGasPrice(gasPrice); - transactionDataStructV2.setGasLimit(gasLimit); - transactionDataStructV2.setMaxFeePerGas(maxFeePerGas); - transactionDataStructV2.setMaxPriorityFeePerGas(maxPriorityFeePerGas); - // input - byte[] bytesInput = fromHex(data); - transactionDataStructV2.setInput(bytesInput); - - // encode TxData to hex tx data - String txDataHex = - TransactionStructBuilderJniObj.encodeTransactionDataStruct(transactionDataStructV2); - // decode hex tx data to TxData - TransactionDataV2 decodeTransactionDataStructHex = - TransactionStructBuilderJniObj.decodeTransactionDataStructV2(txDataHex); - // TransactionDataV2 decodeTransactionDataStructHex2 = - // TransactionStructBuilderV2JniObj.decodeTransactionDataStructV2(null); - // TransactionDataV2 decodeTransactionDataStructHex3 = - // TransactionStructBuilderV2JniObj.decodeTransactionDataStructV2(""); - - // assert - Assert.assertEquals( - transactionDataStructV2.getChainId(), decodeTransactionDataStructHex.getChainId()); - Assert.assertEquals( - transactionDataStructV2.getGroupId(), decodeTransactionDataStructHex.getGroupId()); - Assert.assertEquals( - transactionDataStructV2.getAbi(), decodeTransactionDataStructHex.getAbi()); - Assert.assertEquals( - transactionDataStructV2.getBlockLimit(), decodeTransactionDataStructHex.getBlockLimit()); - Assert.assertEquals( - transactionDataStructV2.getValue(), decodeTransactionDataStructHex.getValue()); - Assert.assertEquals( - transactionDataStructV2.getGasPrice(), decodeTransactionDataStructHex.getGasPrice()); - Assert.assertEquals( - transactionDataStructV2.getGasLimit(), decodeTransactionDataStructHex.getGasLimit()); - Assert.assertEquals( - transactionDataStructV2.getMaxFeePerGas(), - decodeTransactionDataStructHex.getMaxFeePerGas()); - Assert.assertEquals( - transactionDataStructV2.getMaxPriorityFeePerGas(), - decodeTransactionDataStructHex.getMaxPriorityFeePerGas()); - - // encode TxData to json tx data - String txDataJson = - TransactionStructBuilderJniObj.encodeTransactionDataStructToJson( - transactionDataStructV2); - System.out.printf(" [test Tx Struct V2] txDataJson: %s\n", txDataJson); - - // calc tx data hash - String txDataHash = - TransactionStructBuilderJniObj.calcTransactionDataStructHash( - smCrypto ? 1 : 0, decodeTransactionDataStructHex); - System.out.printf(" [test Tx Struct V2] txDataHash: %s\n", txDataHash); - // signature tx data hash - String signature = TransactionBuilderJniObj.signTransactionDataHash(keyPair, txDataHash); - System.out.printf(" [test Tx Struct V2] signature: %s\n", signature); - - // construct tx - Transaction transactionStructV2 = new Transaction(); - transactionStructV2.setTransactionData(decodeTransactionDataStructHex); - transactionStructV2.setDataHash(fromHex(txDataHash)); - transactionStructV2.setSignature(fromHex(signature)); - transactionStructV2.setSender(null); - transactionStructV2.setImportTime(0); - transactionStructV2.setAttribute(0); - transactionStructV2.setExtraData(""); - // assert - Assert.assertEquals( - transactionStructV2.getTransactionData().getBlockLimit(), - decodeTransactionDataStructHex.getBlockLimit()); - Assert.assertEquals( - transactionStructV2.getTransactionData().getGroupId(), - decodeTransactionDataStructHex.getGroupId()); - Assert.assertEquals( - transactionStructV2.getTransactionData().getChainId(), - decodeTransactionDataStructHex.getChainId()); - Assert.assertEquals( - transactionStructV2.getTransactionData().getChainId(), - decodeTransactionDataStructHex.getChainId()); - Assert.assertEquals( - transactionStructV2.getTransactionData().getAbi(), - decodeTransactionDataStructHex.getAbi()); - Assert.assertArrayEquals( - transactionStructV2.getDataHash(), fromHex(txDataHash)); - Assert.assertArrayEquals( - transactionStructV2.getSignature(), fromHex(signature)); - - // encode Tx to hex tx - String txHex = - TransactionStructBuilderJniObj.encodeTransactionStruct(transactionStructV2); - // decode hex tx to Tx - Transaction decodeTransactionStructHex = - TransactionStructBuilderJniObj.decodeTransactionStructV2(txHex); - - // assert - Assert.assertEquals( - transactionStructV2.getTransactionData().getBlockLimit(), - decodeTransactionStructHex.getTransactionData().getBlockLimit()); - Assert.assertEquals( - transactionStructV2.getTransactionData().getGroupId(), - decodeTransactionStructHex.getTransactionData().getGroupId()); - Assert.assertEquals( - transactionStructV2.getTransactionData().getChainId(), - decodeTransactionStructHex.getTransactionData().getChainId()); - Assert.assertEquals( - transactionStructV2.getTransactionData().getAbi(), - decodeTransactionStructHex.getTransactionData().getAbi()); - TransactionDataV2 transactionData = (TransactionDataV2) transactionStructV2.getTransactionData(); - TransactionDataV2 decodeTransactionData = (TransactionDataV2) decodeTransactionStructHex.getTransactionData(); - Assert.assertEquals( - transactionData.getValue(), - decodeTransactionData.getValue()); - Assert.assertEquals( - transactionData.getGasLimit(), - decodeTransactionData.getGasLimit()); - Assert.assertEquals( - transactionData.getGasPrice(), - decodeTransactionData.getGasPrice()); - Assert.assertEquals( - transactionData.getMaxFeePerGas(), - decodeTransactionData.getMaxFeePerGas()); - Assert.assertEquals( - transactionData.getMaxPriorityFeePerGas(), - decodeTransactionData.getMaxPriorityFeePerGas()); - Assert.assertArrayEquals( - transactionStructV2.getDataHash(), - decodeTransactionStructHex.getDataHash()); - Assert.assertArrayEquals( - transactionStructV2.getSignature(), - decodeTransactionStructHex.getSignature()); - - // encode Tx to json tx - String txJson = - TransactionStructBuilderJniObj.encodeTransactionStructToJson(transactionStructV2); - System.out.printf(" [test Tx Struct V2] txJson: %s\n", txJson); - // create tx string - String txString = - TransactionStructBuilderJniObj.createEncodedTransaction( - decodeTransactionDataStructHex, signature, txDataHash, 0, ""); - // System.out.printf(" [test Tx Struct V2] txString: %s\n", txString); - CompletableFuture future = new CompletableFuture<>(); - - // rpc send tx - rpcJniObj.sendTransaction( - group, - node, - txString, - false, - future::complete); - - Response response = future.get(); - System.out.println("response error code: ==>>> " + response.getErrorCode()); - String dataStr = new String(response.getData()); - System.out.println("response data: ==>>> " + dataStr); - rpcJniObj.stop(); - System.out.println(" [test Tx Struct V2] finish !! "); - } -} diff --git a/sample/tx/CMakeLists.txt b/sample/tx/CMakeLists.txt index f6a8783e0..5776ef247 100644 --- a/sample/tx/CMakeLists.txt +++ b/sample/tx/CMakeLists.txt @@ -7,8 +7,8 @@ target_link_libraries(hello_sample_hsm PUBLIC ${BCOS_C_SDK_STATIC_TARGET}) add_executable(tx_struct_test tx_struct_test.c) target_link_libraries(tx_struct_test PUBLIC ${BCOS_C_SDK_STATIC_TARGET}) -add_executable(tx_struct_v2_test tx_struct_v2_test.c) -target_link_libraries(tx_struct_v2_test PUBLIC ${BCOS_C_SDK_STATIC_TARGET}) +add_executable(tx_struct_v1_test tx_struct_v1_test.c) +target_link_libraries(tx_struct_v1_test PUBLIC ${BCOS_C_SDK_STATIC_TARGET}) # add_executable(tx_sign_perf tx_sign_perf.cpp) # target_link_libraries(tx_sign_perf PUBLIC ${BCOS_C_SDK_STATIC_TARGET}) \ No newline at end of file diff --git a/sample/tx/tx_struct_v2_test.c b/sample/tx/tx_struct_v1_test.c similarity index 74% rename from sample/tx/tx_struct_v2_test.c rename to sample/tx/tx_struct_v1_test.c index d22a5fbc9..869f85bf0 100644 --- a/sample/tx/tx_struct_v2_test.c +++ b/sample/tx/tx_struct_v1_test.c @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * @file tx_struct_v2_test.c + * @file tx_struct_v1_test.c * @author: lucasli * @date 2023-11-29 */ @@ -131,14 +131,15 @@ const char* g_hw_abi = void usage(void) { - printf("Desc: tx struct v2 test sample\n"); - printf("Usage: tx_struct_v2_test \n"); + printf("Desc: tx struct v1 test sample\n"); + printf("Usage: tx_struct_v1_test \n"); printf("Example:\n"); - printf(" ./tx_struct_v2_test ./config.ini group0\n"); + printf(" ./tx_struct_v1_test ./config.ini group0\n"); exit(0); } -int convertCharToHexNumber(char hexChar) { +int convertCharToHexNumber(char hexChar) +{ if (hexChar >= '0' && hexChar <= '9') return hexChar - '0'; if (hexChar >= 'a' && hexChar <= 'f') @@ -148,15 +149,20 @@ int convertCharToHexNumber(char hexChar) { return -1; } -struct bcos_sdk_c_bytes* fromHexString(const char* hexedString) { - unsigned startIndex = (strlen(hexedString) >= 2 && hexedString[0] == '0' && hexedString[1] == 'x') ? 2 : 0; - struct bcos_sdk_c_bytes* bytesData = (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); +struct bcos_sdk_c_bytes* fromHexString(const char* hexedString) +{ + unsigned startIndex = + (strlen(hexedString) >= 2 && hexedString[0] == '0' && hexedString[1] == 'x') ? 2 : 0; + struct bcos_sdk_c_bytes* bytesData = + (struct bcos_sdk_c_bytes*)malloc(sizeof(struct bcos_sdk_c_bytes)); bytesData->buffer = (uint8_t*)malloc((strlen(hexedString) - startIndex + 1) / 2); bytesData->length = 0; - if (strlen(hexedString) % 2) { + if (strlen(hexedString) % 2) + { int h = convertCharToHexNumber(hexedString[startIndex++]); - if (h == -1) { + if (h == -1) + { // Handle error free(bytesData->buffer); free(bytesData); @@ -164,10 +170,12 @@ struct bcos_sdk_c_bytes* fromHexString(const char* hexedString) { } bytesData->buffer[bytesData->length++] = (uint8_t)h; } - for (unsigned i = startIndex; i < strlen(hexedString); i += 2) { + for (unsigned i = startIndex; i < strlen(hexedString); i += 2) + { int highValue = convertCharToHexNumber(hexedString[i]); int lowValue = convertCharToHexNumber(hexedString[i + 1]); - if (highValue == -1 || lowValue == -1) { + if (highValue == -1 || lowValue == -1) + { // Handle error free(bytesData->buffer); free(bytesData); @@ -212,15 +220,15 @@ void on_deploy_resp_callback(struct bcos_sdk_c_struct_response* resp) memcpy(contract_address, p1, p2 - p1); contract_address[p2 - p1] = '\0'; - printf(" [TxStructV2Test] contractAddress ===>>>>: %s\n", contract_address); - printf(" [TxStructV2Test] transaction receipt ===>>>>: %s\n", (char*)resp->data); + printf(" [TxStructv1Test] contractAddress ===>>>>: %s\n", contract_address); + printf(" [TxStructv1Test] transaction receipt ===>>>>: %s\n", (char*)resp->data); } /* resp->data 的数据结构 { "id" : 2, "jsonrpc" : "2.0", - "result" : + "result" : { "blockNumber" : 113, "checksumContractAddress" : "", @@ -229,7 +237,8 @@ void on_deploy_resp_callback(struct bcos_sdk_c_struct_response* resp) "from" : "0x69df04bec1c36551be6298f7e4c2f867592a4b37", "gasUsed" : "13063", "hash" : "0x7d816bbde4aef3bd4c084b0887982f2c50cb9a50975a4d07328ec5fd5dd4e6e6", - "input" : "0x3590b49f0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001748656c6c6f20464953434f2d42434f5320332e30212121000000000000000000", + "input" : +"0x3590b49f0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000001748656c6c6f20464953434f2d42434f5320332e30212121000000000000000000", "logEntries" : [], "message" : "", "output" : "0x", @@ -272,7 +281,7 @@ int main(int argc, char** argv) const char* config = argv[1]; const char* group_id = argv[2]; - printf("[TxStructV2Test] params ===>>>> \n"); + printf("[TxStructv1Test] params ===>>>> \n"); printf("\t # config: %s\n", config); printf("\t # group_id: %s\n", group_id); @@ -290,13 +299,14 @@ int main(int argc, char** argv) exit(-1); } - printf(" [TxStructV2Test] start sdk ... \n"); + printf(" [TxStructv1Test] start sdk ... \n"); // 2. start bcos c sdk bcos_sdk_start(sdk); if (!bcos_sdk_is_last_opr_success()) { - printf(" [TxStructV2Test] bcos_sdk_start failed, error: %s\n", bcos_sdk_get_last_error_msg()); + printf( + " [TxStructv1Test] bcos_sdk_start failed, error: %s\n", bcos_sdk_get_last_error_msg()); exit(-1); } @@ -312,40 +322,41 @@ int main(int argc, char** argv) exit(-1); } - printf(" [TxStructV2Test] sm crypto: %d\n", sm_crypto); + printf(" [TxStructv1Test] sm crypto: %d\n", sm_crypto); // 4. get chain_id of the group_id const char* chain_id = bcos_sdk_get_group_chain_id(sdk, group_id); if (!bcos_sdk_is_last_opr_success()) { - printf(" [TxStructV2Test] bcos_sdk_get_group_chain_id failed, error: %s\n", + printf(" [TxStructv1Test] bcos_sdk_get_group_chain_id failed, error: %s\n", bcos_sdk_get_last_error_msg()); exit(-1); } - printf(" [TxStructV2Test] chain id: %s\n", chain_id); + printf(" [TxStructv1Test] chain id: %s\n", chain_id); // 5. get blocklimit of the group_id int64_t block_limit = bcos_rpc_get_block_limit(sdk, group_id); if (block_limit < 0) { - printf(" [TxStructV2Test] group not exist, group: %s\n", group_id); + printf(" [TxStructv1Test] group not exist, group: %s\n", group_id); exit(-1); } - printf(" [TxStructV2Test] block limit: %d\n", (int32_t)block_limit); + printf(" [TxStructv1Test] block limit: %d\n", (int32_t)block_limit); // 6. load or create keypair for transaction sign void* key_pair = bcos_sdk_create_keypair(sm_crypto); if (!key_pair) { - printf(" [TxStructV2Test] create keypair failed, error: %s\n", bcos_sdk_get_last_error_msg()); + printf( + " [TxStructv1Test] create keypair failed, error: %s\n", bcos_sdk_get_last_error_msg()); exit(-1); } - // printf(" [TxStructV2Test] bcos_sdk_get_keypair_type: %d\n", + // printf(" [TxStructv1Test] bcos_sdk_get_keypair_type: %d\n", // bcos_sdk_get_keypair_type(key_pair)); // 7. get account address of the keypair const char* address = bcos_sdk_get_keypair_address(key_pair); - printf(" [TxStructV2Test] new account, address: %s\n", address); + printf(" [TxStructv1Test] new account, address: %s\n", address); const char* extra_data = "ExtraData"; const char* value = "33"; @@ -354,25 +365,32 @@ int main(int argc, char** argv) const char* maxFeePerGas = "11"; const char* maxPriorityFeePerGas = "22"; - // printf(" [TxStructV2Test] extra_data: %s, value: %s, gasPrice: %s, gasLimit: %ld, maxFeePerGas: %s, maxPriorityFeePerGas: %s\n", extra_data, value, gasPrice, gasLimit, maxFeePerGas, maxPriorityFeePerGas); - + // printf(" [TxStructv1Test] extra_data: %s, value: %s, gasPrice: %s, gasLimit: %ld, + // maxFeePerGas: %s, maxPriorityFeePerGas: %s\n", extra_data, value, gasPrice, gasLimit, + // maxFeePerGas, maxPriorityFeePerGas); + // 8. deploy HelloWorld contract - struct bcos_sdk_c_transaction_data_v2* transaction_data_v2_deploy = bcos_sdk_create_transaction_data_struct_with_hex_input_v2( - group_id, chain_id, "", sm_crypto ? g_hw_sm_bin : g_hw_bin, "", block_limit, value, gasPrice, gasLimit, maxFeePerGas, maxPriorityFeePerGas); + struct bcos_sdk_c_transaction_data_v1* transaction_data_v1_deploy = + bcos_sdk_create_transaction_data_struct_with_hex_input_v1(group_id, chain_id, "", + sm_crypto ? g_hw_sm_bin : g_hw_bin, "", block_limit, value, gasPrice, gasLimit, + maxFeePerGas, maxPriorityFeePerGas); const char* transaction_data_hash_deploy = - bcos_sdk_calc_transaction_data_struct_hash_v2(sm_crypto, transaction_data_v2_deploy); + bcos_sdk_calc_transaction_data_struct_hash_v1(sm_crypto, transaction_data_v1_deploy); const char* signed_hash_deploy = bcos_sdk_sign_transaction_data_hash(key_pair, transaction_data_hash_deploy); - const char* signed_tx_deploy = bcos_sdk_create_encoded_transaction_v2( - transaction_data_v2_deploy, signed_hash_deploy, transaction_data_hash_deploy, 0, extra_data); - printf(" [TxStructV2Test] create deploy contract transaction success, signed_tx_deploy: %s\n", signed_tx_deploy); - + const char* signed_tx_deploy = + bcos_sdk_create_encoded_transaction_v1(transaction_data_v1_deploy, signed_hash_deploy, + transaction_data_hash_deploy, 0, extra_data); + printf(" [TxStructv1Test] create deploy contract transaction success, signed_tx_deploy: %s\n", + signed_tx_deploy); + // call rpc interface, send transaction - bcos_rpc_send_transaction(sdk, group_id, "", signed_tx_deploy, 0, on_deploy_resp_callback, NULL); + bcos_rpc_send_transaction( + sdk, group_id, "", signed_tx_deploy, 0, on_deploy_resp_callback, NULL); // wait for async operation done, just for sample sleep(5); - printf(" [TxStructV2Test] set operation\n"); + printf(" [TxStructv1Test] set operation\n"); // 9. HelloWorld set // 9.1 abi encode params const char* set_data = @@ -380,57 +398,65 @@ int main(int argc, char** argv) // 9.2 create signed transaction { // 9.2.1 create transaction data with hex input - struct bcos_sdk_c_transaction_data_v2* transaction_data_v2 = bcos_sdk_create_transaction_data_struct_with_hex_input_v2( - group_id, chain_id, contract_address, set_data, g_hw_abi, block_limit, value, gasPrice, gasLimit, maxFeePerGas, maxPriorityFeePerGas); + struct bcos_sdk_c_transaction_data_v1* transaction_data_v1 = + bcos_sdk_create_transaction_data_struct_with_hex_input_v1(group_id, chain_id, + contract_address, set_data, g_hw_abi, block_limit, value, gasPrice, gasLimit, + maxFeePerGas, maxPriorityFeePerGas); // create transaction data with bytes input struct bcos_sdk_c_bytes* input_bytes = fromHexString(set_data); - transaction_data_v2 = bcos_sdk_create_transaction_data_struct_with_bytes_v2( - group_id, chain_id, contract_address, input_bytes->buffer, input_bytes->length, g_hw_abi, block_limit, value, gasPrice, gasLimit, maxFeePerGas, maxPriorityFeePerGas); + transaction_data_v1 = bcos_sdk_create_transaction_data_struct_with_bytes_v1(group_id, + chain_id, contract_address, input_bytes->buffer, input_bytes->length, g_hw_abi, + block_limit, value, gasPrice, gasLimit, maxFeePerGas, maxPriorityFeePerGas); // 9.2.1.1 encode tx data to hex - const char* hex_tx_data = bcos_sdk_encode_transaction_data_struct_to_hex_v2(transaction_data_v2); - printf(" [TxStructV2Test] tx_data_hex: %s\n", hex_tx_data); + const char* hex_tx_data = + bcos_sdk_encode_transaction_data_struct_to_hex_v1(transaction_data_v1); + printf(" [TxStructv1Test] tx_data_hex: %s\n", hex_tx_data); // 9.2.1.2 decode hex tx data - struct bcos_sdk_c_transaction_data_v2* decode_tx_data = bcos_sdk_decode_transaction_data_struct_from_hex_v2(hex_tx_data); + struct bcos_sdk_c_transaction_data_v1* decode_tx_data = + bcos_sdk_decode_transaction_data_struct_from_hex_v1(hex_tx_data); // 9.2.1.3 encode tx data to json - const char* json_tx_data = bcos_sdk_encode_transaction_data_struct_to_json_v2(decode_tx_data); - printf(" [TxStructV2Test] tx_data_json: %s\n", json_tx_data); + const char* json_tx_data = + bcos_sdk_encode_transaction_data_struct_to_json_v1(decode_tx_data); + printf(" [TxStructv1Test] tx_data_json: %s\n", json_tx_data); // 9.2.1.4 decode json to tx data struct - decode_tx_data = bcos_sdk_decode_transaction_data_struct_from_json_v2(json_tx_data); + decode_tx_data = bcos_sdk_decode_transaction_data_struct_from_json_v1(json_tx_data); // 9.2.2 calc transaction data hash const char* transaction_data_hash = - bcos_sdk_calc_transaction_data_struct_hash_v2(sm_crypto, decode_tx_data); - printf(" [TxStructV2Test] set tx hash: %s\n", transaction_data_hash); - transaction_data_hash = bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2(sm_crypto, hex_tx_data); - printf(" [TxStructV2Test] set tx hash with tx_data_hex: %s\n", transaction_data_hash); + bcos_sdk_calc_transaction_data_struct_hash_v1(sm_crypto, decode_tx_data); + printf(" [TxStructv1Test] set tx hash: %s\n", transaction_data_hash); + transaction_data_hash = + bcos_sdk_calc_transaction_data_struct_hash_with_hex_v1(sm_crypto, hex_tx_data); + printf(" [TxStructv1Test] set tx hash with tx_data_hex: %s\n", transaction_data_hash); // 9.2.3 sign transaction hash const char* signed_hash = bcos_sdk_sign_transaction_data_hash(key_pair, transaction_data_hash); - + // 9.2.4 create signed transaction - const char* signed_tx = bcos_sdk_create_encoded_transaction_v2( + const char* signed_tx = bcos_sdk_create_encoded_transaction_v1( decode_tx_data, signed_hash, transaction_data_hash, 0, extra_data); - + // 9.2.4.1 create transaction struct - struct bcos_sdk_c_transaction_v2* transaction = bcos_sdk_create_transaction_struct_v2(decode_tx_data, - signed_hash, transaction_data_hash, 0, extra_data); + struct bcos_sdk_c_transaction_v1* transaction = bcos_sdk_create_transaction_struct_v1( + decode_tx_data, signed_hash, transaction_data_hash, 0, extra_data); // 9.2.4.2 encode tx to hex - const char* hex_tx = bcos_sdk_encode_transaction_struct_to_hex_v2(transaction); - printf(" [TxStructV2Test] tx_hex: %s\n", hex_tx); + const char* hex_tx = bcos_sdk_encode_transaction_struct_to_hex_v1(transaction); + printf(" [TxStructv1Test] tx_hex: %s\n", hex_tx); // 9.2.4.3 decode hex to tx - struct bcos_sdk_c_transaction_v2* decode_tx = bcos_sdk_decode_transaction_struct_from_hex_v2(hex_tx); + struct bcos_sdk_c_transaction_v1* decode_tx = + bcos_sdk_decode_transaction_struct_from_hex_v1(hex_tx); // 9.2.4.4 encode tx to json - const char* json_tx = bcos_sdk_encode_transaction_struct_to_json_v2(decode_tx); - printf(" [TxStructV2Test] tx_json: %s\n", json_tx); + const char* json_tx = bcos_sdk_encode_transaction_struct_to_json_v1(decode_tx); + printf(" [TxStructv1Test] tx_json: %s\n", json_tx); // 9.2.4.5 decode json to tx - decode_tx = bcos_sdk_decode_transaction_struct_from_json_v2(json_tx); - const char* hex_tx2 = bcos_sdk_encode_transaction_struct_to_hex_v2(decode_tx); + decode_tx = bcos_sdk_decode_transaction_struct_from_json_v1(json_tx); + const char* hex_tx2 = bcos_sdk_encode_transaction_struct_to_hex_v1(decode_tx); - printf(" [TxStructV2Test] signed_tx: %s\n", signed_tx); - printf(" [TxStructV2Test] hex_tx: %s\n", hex_tx); - printf(" [TxStructV2Test] hex_tx2: %s\n", hex_tx2); + printf(" [TxStructv1Test] signed_tx: %s\n", signed_tx); + printf(" [TxStructv1Test] hex_tx: %s\n", hex_tx); + printf(" [TxStructv1Test] hex_tx2: %s\n", hex_tx2); // 9.3 call rpc interface, sendTransaction bcos_rpc_send_transaction(sdk, group_id, "", signed_tx, 0, on_send_tx_resp_callback, NULL); @@ -443,10 +469,10 @@ int main(int argc, char** argv) { bcos_sdk_c_free(input_bytes->buffer); } - bcos_sdk_destroy_transaction_data_struct_v2(transaction_data_v2); - bcos_sdk_destroy_transaction_data_struct_v2(decode_tx_data); - bcos_sdk_destroy_transaction_struct_v2(transaction); - bcos_sdk_destroy_transaction_struct_v2(decode_tx); + bcos_sdk_destroy_transaction_data_struct_v1(transaction_data_v1); + bcos_sdk_destroy_transaction_data_struct_v1(decode_tx_data); + bcos_sdk_destroy_transaction_struct_v1(transaction); + bcos_sdk_destroy_transaction_struct_v1(decode_tx); bcos_sdk_c_free((void*)transaction_data_hash); bcos_sdk_c_free((void*)signed_hash); bcos_sdk_c_free((void*)signed_tx); diff --git a/test/testTransactionStructV2.cpp b/test/testTransactionStructV1.cpp similarity index 85% rename from test/testTransactionStructV2.cpp rename to test/testTransactionStructV1.cpp index 283825ba5..c059bc7f6 100644 --- a/test/testTransactionStructV2.cpp +++ b/test/testTransactionStructV1.cpp @@ -13,7 +13,7 @@ * See the License for the specific language governing permissions and * limitations under the License. * - * @file testTransactionStructV2.cpp + * @file testTransactionStructv1.cpp * @author: lucasli * @date 2023-12-13 */ @@ -31,11 +31,11 @@ using namespace bcos; -struct TestTxStructV2Fixture +struct TestTxStructv1Fixture { }; -BOOST_FIXTURE_TEST_SUITE(TransactionStructV2Test, TestTxStructV2Fixture) +BOOST_FIXTURE_TEST_SUITE(TransactionStructv1Test, TestTxStructv1Fixture) const auto input = "0x3590b49f00000000000000000000000000000000000000000000000000000000000000200000000000000000" @@ -50,7 +50,7 @@ const char* abi_with_chinese = "\"string\",\"name\":\"n\",\"type\":\"string\"}],\"name\":\"set\",\"outputs\":[]," "\"stateMutability\":\"nonpayable\",\"type\":\"function\"}]"; -struct bcos_sdk_c_transaction_data_v2* createTxDataStructWithHexInputV2() +struct bcos_sdk_c_transaction_data_v1* createTxDataStructWithHexInputv1() { auto group_id = ""; auto chain_id = ""; @@ -62,14 +62,14 @@ struct bcos_sdk_c_transaction_data_v2* createTxDataStructWithHexInputV2() int64_t gas_limit = 0; auto max_fee_per_gas = "1"; auto max_priority_fee_per_gas = "1"; - auto txDataWithHex = bcos_sdk_create_transaction_data_struct_with_hex_input_v2(group_id, + auto txDataWithHex = bcos_sdk_create_transaction_data_struct_with_hex_input_v1(group_id, chain_id, to, input, abi, block_limit, value, gas_price, gas_limit, max_fee_per_gas, max_priority_fee_per_gas); return txDataWithHex; } -struct bcos_sdk_c_transaction_data_v2* createTxDataStructWithByteInputV2() +struct bcos_sdk_c_transaction_data_v1* createTxDataStructWithByteInputv1() { auto group_id = ""; auto chain_id = ""; @@ -83,15 +83,15 @@ struct bcos_sdk_c_transaction_data_v2* createTxDataStructWithByteInputV2() auto max_priority_fee_per_gas = "1"; auto bytesInput = fromHexString(input); - auto txDataWithBytes = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, + auto txDataWithBytes = bcos_sdk_create_transaction_data_struct_with_bytes_v1(group_id, chain_id, to, bytesInput->data(), bytesInput->size(), abi, block_limit, value, gas_price, gas_limit, max_fee_per_gas, max_priority_fee_per_gas); return txDataWithBytes; } -void compareTxStruct(struct bcos_sdk_c_transaction_v2* txStruct, - struct bcos_sdk_c_transaction_data_v2* txDataStruct, const char* signature, +void compareTxStruct(struct bcos_sdk_c_transaction_v1* txStruct, + struct bcos_sdk_c_transaction_data_v1* txDataStruct, const char* signature, const char* extraData, int64_t attribute) { // transaction_data @@ -130,7 +130,7 @@ void compareTxStruct(struct bcos_sdk_c_transaction_v2* txStruct, BOOST_TEST(txStruct->extra_data == extraData); } -BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithHexInputV2) +BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithHexInputv1) { auto group_id = ""; auto chain_id = ""; @@ -143,8 +143,8 @@ BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithHexInputV2) auto max_fee_per_gas = "1"; auto max_priority_fee_per_gas = "1"; - // create transaction_data_struct_v2 failed (block_limit = 0) - auto txData = bcos_sdk_create_transaction_data_struct_with_hex_input_v2(group_id, chain_id, to, + // create transaction_data_struct_v1 failed (block_limit = 0) + auto txData = bcos_sdk_create_transaction_data_struct_with_hex_input_v1(group_id, chain_id, to, input, abi, block_limit, value, gas_price, gas_limit, max_fee_per_gas, max_priority_fee_per_gas); auto success = bcos_sdk_is_last_opr_success(); @@ -153,9 +153,9 @@ BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithHexInputV2) BOOST_TEST(bcos_sdk_get_last_error() == -1); BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("block limit must > 0")); - // create transaction_data_struct_v2 failed (input = "") + // create transaction_data_struct_v1 failed (input = "") block_limit = 1; - txData = bcos_sdk_create_transaction_data_struct_with_hex_input_v2(group_id, chain_id, to, "", + txData = bcos_sdk_create_transaction_data_struct_with_hex_input_v1(group_id, chain_id, to, "", abi, block_limit, value, gas_price, gas_limit, max_fee_per_gas, max_priority_fee_per_gas); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); @@ -163,8 +163,8 @@ BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithHexInputV2) BOOST_TEST(bcos_sdk_get_last_error() == -1); BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("input can not be empty string")); - // create transaction_data_struct_v2 failed (gas_limit <0) - txData = bcos_sdk_create_transaction_data_struct_with_hex_input_v2(group_id, chain_id, to, + // create transaction_data_struct_v1 failed (gas_limit <0) + txData = bcos_sdk_create_transaction_data_struct_with_hex_input_v1(group_id, chain_id, to, input, abi, block_limit, value, gas_price, -1, max_fee_per_gas, max_priority_fee_per_gas); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); @@ -172,8 +172,8 @@ BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithHexInputV2) BOOST_TEST(bcos_sdk_get_last_error() == -1); BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("gas limit must >= 0")); - // create transaction_data_struct_v2 success - txData = bcos_sdk_create_transaction_data_struct_with_hex_input_v2(group_id, chain_id, to, + // create transaction_data_struct_v1 success + txData = bcos_sdk_create_transaction_data_struct_with_hex_input_v1(group_id, chain_id, to, input, abi, block_limit, value, gas_price, gas_limit, max_fee_per_gas, max_priority_fee_per_gas); success = bcos_sdk_is_last_opr_success(); @@ -195,10 +195,10 @@ BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithHexInputV2) reinterpret_cast(txData->input->buffer), txData->input->length); BOOST_TEST(txDataInputString == fromInputHexString); - bcos_sdk_destroy_transaction_data_struct_v2(txData); + bcos_sdk_destroy_transaction_data_struct_v1(txData); } -BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithByteInputV2) +BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithByteInputv1) { auto bytesInput = fromHexString(input); auto group_id = ""; @@ -213,8 +213,8 @@ BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithByteInputV2) auto max_fee_per_gas = "1"; auto max_priority_fee_per_gas = "1"; - // create transaction_data_struct_v2 failed (block_limit = 0) - auto txData = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, to, + // create transaction_data_struct_v1 failed (block_limit = 0) + auto txData = bcos_sdk_create_transaction_data_struct_with_bytes_v1(group_id, chain_id, to, bytesInput->data(), bytes_input_length, abi, block_limit, value, gas_price, gas_limit, max_fee_per_gas, max_priority_fee_per_gas); auto success = bcos_sdk_is_last_opr_success(); @@ -224,10 +224,10 @@ BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithByteInputV2) BOOST_TEST(bcos_sdk_get_last_error() == -1); BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("block limit must > 0")); - // create transaction_data_struct_v2 failed (bytes_input_length = 0) + // create transaction_data_struct_v1 failed (bytes_input_length = 0) bytes_input_length = 0; block_limit = 1; - txData = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, to, + txData = bcos_sdk_create_transaction_data_struct_with_bytes_v1(group_id, chain_id, to, bytesInput->data(), bytes_input_length, abi, block_limit, value, gas_price, gas_limit, max_fee_per_gas, max_priority_fee_per_gas); success = bcos_sdk_is_last_opr_success(); @@ -237,9 +237,9 @@ BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithByteInputV2) BOOST_TEST(bcos_sdk_get_last_error() == -1); BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("bytes input length must > 0")); - // create transaction_data_struct_v2 failed (group_id = NULL) + // create transaction_data_struct_v1 failed (group_id = NULL) group_id = nullptr; - txData = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, to, + txData = bcos_sdk_create_transaction_data_struct_with_bytes_v1(group_id, chain_id, to, bytesInput->data(), bytesInput->size(), abi, block_limit, value, gas_price, gas_limit, max_fee_per_gas, max_priority_fee_per_gas); success = bcos_sdk_is_last_opr_success(); @@ -248,9 +248,9 @@ BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithByteInputV2) BOOST_TEST(txData == nullptr); BOOST_TEST(bcos_sdk_get_last_error() == -1); - // create transaction_data_struct_v2 success (group_id has Chinese characters) + // create transaction_data_struct_v1 success (group_id has Chinese characters) group_id = "区块链群组1"; - txData = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, to, + txData = bcos_sdk_create_transaction_data_struct_with_bytes_v1(group_id, chain_id, to, bytesInput->data(), bytesInput->size(), abi, block_limit, value, gas_price, gas_limit, max_fee_per_gas, max_priority_fee_per_gas); success = bcos_sdk_is_last_opr_success(); @@ -272,9 +272,9 @@ BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithByteInputV2) reinterpret_cast(txData->input->buffer), txData->input->length); BOOST_TEST(txDataInputString1 == fromInputHexString1); - // create transaction_data_struct_v2 failed (chain_id = NULL) + // create transaction_data_struct_v1 failed (chain_id = NULL) chain_id = nullptr; - txData = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, to, + txData = bcos_sdk_create_transaction_data_struct_with_bytes_v1(group_id, chain_id, to, bytesInput->data(), bytesInput->size(), abi, block_limit, value, gas_price, gas_limit, max_fee_per_gas, max_priority_fee_per_gas); success = bcos_sdk_is_last_opr_success(); @@ -283,9 +283,9 @@ BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithByteInputV2) BOOST_TEST(txData == nullptr); BOOST_TEST(bcos_sdk_get_last_error() == -1); - // create transaction_data_struct_v2 success (chain_id has Chinese characters) + // create transaction_data_struct_v1 success (chain_id has Chinese characters) chain_id = "区块链1"; - txData = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, to, + txData = bcos_sdk_create_transaction_data_struct_with_bytes_v1(group_id, chain_id, to, bytesInput->data(), bytesInput->size(), abi, block_limit, value, gas_price, gas_limit, max_fee_per_gas, max_priority_fee_per_gas); success = bcos_sdk_is_last_opr_success(); @@ -307,9 +307,9 @@ BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithByteInputV2) reinterpret_cast(txData->input->buffer), txData->input->length); BOOST_TEST(txDataInputString2 == fromInputHexString2); - // create transaction_data_struct_v2 success (abi has Chinese characters) + // create transaction_data_struct_v1 success (abi has Chinese characters) chain_id = ""; - txData = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, to, + txData = bcos_sdk_create_transaction_data_struct_with_bytes_v1(group_id, chain_id, to, bytesInput->data(), bytesInput->size(), abi_with_chinese, block_limit, value, gas_price, gas_limit, max_fee_per_gas, max_priority_fee_per_gas); success = bcos_sdk_is_last_opr_success(); @@ -331,8 +331,8 @@ BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithByteInputV2) reinterpret_cast(txData->input->buffer), txData->input->length); BOOST_TEST(txDataInputString3 == fromInputHexString3); - // create transaction_data_struct_v2 failed (gas_limit <0) - txData = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, to, + // create transaction_data_struct_v1 failed (gas_limit <0) + txData = bcos_sdk_create_transaction_data_struct_with_bytes_v1(group_id, chain_id, to, bytesInput->data(), bytesInput->size(), abi, block_limit, value, gas_price, -1, max_fee_per_gas, max_priority_fee_per_gas); success = bcos_sdk_is_last_opr_success(); @@ -341,9 +341,9 @@ BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithByteInputV2) BOOST_TEST(bcos_sdk_get_last_error() == -1); BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("gas limit must >= 0")); - // create transaction_data_struct_v2 success + // create transaction_data_struct_v1 success group_id = ""; - txData = bcos_sdk_create_transaction_data_struct_with_bytes_v2(group_id, chain_id, to, + txData = bcos_sdk_create_transaction_data_struct_with_bytes_v1(group_id, chain_id, to, bytesInput->data(), bytesInput->size(), abi, block_limit, value, gas_price, gas_limit, max_fee_per_gas, max_priority_fee_per_gas); success = bcos_sdk_is_last_opr_success(); @@ -365,17 +365,17 @@ BOOST_AUTO_TEST_CASE(testCreateTxDataStructWithByteInputV2) reinterpret_cast(txData->input->buffer), txData->input->length); BOOST_TEST(txDataInputString == fromInputHexString); - bcos_sdk_destroy_transaction_data_struct_v2(txData); + bcos_sdk_destroy_transaction_data_struct_v1(txData); } -BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStructV2) +BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStructv1) { - auto txDataWithHex = createTxDataStructWithHexInputV2(); - auto txDataWithBytes = createTxDataStructWithByteInputV2(); + auto txDataWithHex = createTxDataStructWithHexInputv1(); + auto txDataWithBytes = createTxDataStructWithByteInputv1(); // 1. test tx data encode decode hex // encode to hex failed (transaction_data == NULL) - auto encodedTxDataHex = bcos_sdk_encode_transaction_data_struct_to_hex_v2(nullptr); + auto encodedTxDataHex = bcos_sdk_encode_transaction_data_struct_to_hex_v1(nullptr); auto hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == false); @@ -383,14 +383,14 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStructV2) BOOST_TEST(bcos_sdk_get_last_error() == -1); // encode to hex success - encodedTxDataHex = bcos_sdk_encode_transaction_data_struct_to_hex_v2(txDataWithHex); + encodedTxDataHex = bcos_sdk_encode_transaction_data_struct_to_hex_v1(txDataWithHex); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == true); BOOST_TEST(encodedTxDataHex != nullptr); BOOST_TEST(bcos_sdk_get_last_error() == 0); - encodedTxDataHex = bcos_sdk_encode_transaction_data_struct_to_hex_v2(txDataWithBytes); + encodedTxDataHex = bcos_sdk_encode_transaction_data_struct_to_hex_v1(txDataWithBytes); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == true); @@ -398,7 +398,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStructV2) BOOST_TEST(bcos_sdk_get_last_error() == 0); // decode hex failed (transaction_data_hex_str == NULL) - auto decodedTxDataHex = bcos_sdk_decode_transaction_data_struct_from_hex_v2(nullptr); + auto decodedTxDataHex = bcos_sdk_decode_transaction_data_struct_from_hex_v1(nullptr); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == false); @@ -406,7 +406,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStructV2) BOOST_TEST(bcos_sdk_get_last_error() == -1); // decode hex failed (transaction_data_hex_str == "") - decodedTxDataHex = bcos_sdk_decode_transaction_data_struct_from_hex_v2(""); + decodedTxDataHex = bcos_sdk_decode_transaction_data_struct_from_hex_v1(""); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == false); @@ -416,7 +416,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStructV2) std::string("transaction_data_hex_str can not be empty string")); // decode hex failed (the length of transaction_data_hex_str is not even number) - decodedTxDataHex = bcos_sdk_decode_transaction_data_struct_from_hex_v2("0xabc"); + decodedTxDataHex = bcos_sdk_decode_transaction_data_struct_from_hex_v1("0xabc"); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == false); @@ -426,7 +426,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStructV2) std::string("the length of transaction_data_hex_str must be an even number")); // decode hex success - decodedTxDataHex = bcos_sdk_decode_transaction_data_struct_from_hex_v2(encodedTxDataHex); + decodedTxDataHex = bcos_sdk_decode_transaction_data_struct_from_hex_v1(encodedTxDataHex); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == true); @@ -453,7 +453,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStructV2) // 2. test tx data encode decode json // encode to json failed (transaction_data == NULL) - auto encodedTxDataJson = bcos_sdk_encode_transaction_data_struct_to_json_v2(nullptr); + auto encodedTxDataJson = bcos_sdk_encode_transaction_data_struct_to_json_v1(nullptr); auto jsonSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(jsonSuccess == false); @@ -461,14 +461,14 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStructV2) BOOST_TEST(bcos_sdk_get_last_error() == -1); // encode to json success - encodedTxDataJson = bcos_sdk_encode_transaction_data_struct_to_json_v2(txDataWithHex); + encodedTxDataJson = bcos_sdk_encode_transaction_data_struct_to_json_v1(txDataWithHex); jsonSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(jsonSuccess == true); BOOST_TEST(encodedTxDataJson != nullptr); BOOST_TEST(bcos_sdk_get_last_error() == 0); - encodedTxDataJson = bcos_sdk_encode_transaction_data_struct_to_json_v2(txDataWithBytes); + encodedTxDataJson = bcos_sdk_encode_transaction_data_struct_to_json_v1(txDataWithBytes); jsonSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(jsonSuccess == true); @@ -476,7 +476,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStructV2) BOOST_TEST(bcos_sdk_get_last_error() == 0); // decode json failed (transaction_data_json_str == NULL) - auto decodedTxDataJson = bcos_sdk_decode_transaction_data_struct_from_json_v2(nullptr); + auto decodedTxDataJson = bcos_sdk_decode_transaction_data_struct_from_json_v1(nullptr); jsonSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(jsonSuccess == false); @@ -484,7 +484,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStructV2) BOOST_TEST(bcos_sdk_get_last_error() == -1); // decode json failed (transaction_data_json_str == "") - decodedTxDataJson = bcos_sdk_decode_transaction_data_struct_from_json_v2(""); + decodedTxDataJson = bcos_sdk_decode_transaction_data_struct_from_json_v1(""); jsonSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(jsonSuccess == false); @@ -494,7 +494,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStructV2) std::string("transaction_data_json_str can not be empty string")); // decode json success - decodedTxDataJson = bcos_sdk_decode_transaction_data_struct_from_json_v2(encodedTxDataJson); + decodedTxDataJson = bcos_sdk_decode_transaction_data_struct_from_json_v1(encodedTxDataJson); jsonSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(jsonSuccess == true); @@ -518,23 +518,23 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxDataStructV2) decodedTxDataJson->input->length); BOOST_TEST(txDataInputString == decodedJsonTxDataInputJson); - bcos_sdk_destroy_transaction_data_struct_v2(txDataWithHex); - bcos_sdk_destroy_transaction_data_struct_v2(txDataWithBytes); - bcos_sdk_destroy_transaction_data_struct_v2(decodedTxDataHex); - bcos_sdk_destroy_transaction_data_struct_v2(decodedTxDataJson); + bcos_sdk_destroy_transaction_data_struct_v1(txDataWithHex); + bcos_sdk_destroy_transaction_data_struct_v1(txDataWithBytes); + bcos_sdk_destroy_transaction_data_struct_v1(decodedTxDataHex); + bcos_sdk_destroy_transaction_data_struct_v1(decodedTxDataJson); } -BOOST_AUTO_TEST_CASE(testCalculateTxDataHashV2) +BOOST_AUTO_TEST_CASE(testCalculateTxDataHashv1) { - auto txDataWithHex = createTxDataStructWithHexInputV2(); - auto txDataWithByte = createTxDataStructWithByteInputV2(); - auto encodedtxDataWithHex = bcos_sdk_encode_transaction_data_struct_to_hex_v2(txDataWithHex); - auto encodedtxDataWithByte = bcos_sdk_encode_transaction_data_struct_to_hex_v2(txDataWithByte); + auto txDataWithHex = createTxDataStructWithHexInputv1(); + auto txDataWithByte = createTxDataStructWithByteInputv1(); + auto encodedtxDataWithHex = bcos_sdk_encode_transaction_data_struct_to_hex_v1(txDataWithHex); + auto encodedtxDataWithByte = bcos_sdk_encode_transaction_data_struct_to_hex_v1(txDataWithByte); // 1. test cal tx data hash with struct // calculate tx data hash failed (cryptoType not in 0,1) auto cryptoType = 2; - auto txDataHash = bcos_sdk_calc_transaction_data_struct_hash_v2(cryptoType, txDataWithHex); + auto txDataHash = bcos_sdk_calc_transaction_data_struct_hash_v1(cryptoType, txDataWithHex); auto success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); BOOST_TEST(txDataHash == nullptr); @@ -545,7 +545,7 @@ BOOST_AUTO_TEST_CASE(testCalculateTxDataHashV2) // calculate tx data hash failed (cryptoType is negative number) cryptoType = -11; - txDataHash = bcos_sdk_calc_transaction_data_struct_hash_v2(cryptoType, txDataWithHex); + txDataHash = bcos_sdk_calc_transaction_data_struct_hash_v1(cryptoType, txDataWithHex); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); BOOST_TEST(txDataHash == nullptr); @@ -556,7 +556,7 @@ BOOST_AUTO_TEST_CASE(testCalculateTxDataHashV2) // calculate tx data hash failed (transaction_data == NULL) cryptoType = 1; - txDataHash = bcos_sdk_calc_transaction_data_struct_hash_v2(cryptoType, nullptr); + txDataHash = bcos_sdk_calc_transaction_data_struct_hash_v1(cryptoType, nullptr); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); BOOST_TEST(txDataHash == nullptr); @@ -564,14 +564,14 @@ BOOST_AUTO_TEST_CASE(testCalculateTxDataHashV2) // calculate tx data hash success (txDataWithHex) cryptoType = 1; - txDataHash = bcos_sdk_calc_transaction_data_struct_hash_v2(cryptoType, txDataWithHex); + txDataHash = bcos_sdk_calc_transaction_data_struct_hash_v1(cryptoType, txDataWithHex); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == true); BOOST_TEST(txDataHash != nullptr); BOOST_TEST(bcos_sdk_get_last_error() == 0); // calculate tx data hash success (txDataWithByte) - txDataHash = bcos_sdk_calc_transaction_data_struct_hash_v2(cryptoType, txDataWithByte); + txDataHash = bcos_sdk_calc_transaction_data_struct_hash_v1(cryptoType, txDataWithByte); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == true); BOOST_TEST(txDataHash != nullptr); @@ -581,7 +581,7 @@ BOOST_AUTO_TEST_CASE(testCalculateTxDataHashV2) // calculate tx data hash with hex failed (cryptoType not in 0,1) cryptoType = 2; auto txDataHashWithHex = - bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2(cryptoType, encodedtxDataWithHex); + bcos_sdk_calc_transaction_data_struct_hash_with_hex_v1(cryptoType, encodedtxDataWithHex); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); BOOST_TEST(txDataHashWithHex == nullptr); @@ -593,7 +593,7 @@ BOOST_AUTO_TEST_CASE(testCalculateTxDataHashV2) // calculate tx data hash failed (cryptoType is negative number) cryptoType = -11; txDataHashWithHex = - bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2(cryptoType, encodedtxDataWithHex); + bcos_sdk_calc_transaction_data_struct_hash_with_hex_v1(cryptoType, encodedtxDataWithHex); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); BOOST_TEST(txDataHashWithHex == nullptr); @@ -604,14 +604,14 @@ BOOST_AUTO_TEST_CASE(testCalculateTxDataHashV2) // calculate tx data hash with hex failed (transaction_data_hex == NULL) cryptoType = 1; - txDataHashWithHex = bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2(cryptoType, nullptr); + txDataHashWithHex = bcos_sdk_calc_transaction_data_struct_hash_with_hex_v1(cryptoType, nullptr); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); BOOST_TEST(txDataHashWithHex == nullptr); BOOST_TEST(bcos_sdk_get_last_error() == -1); // calculate tx data hash with hex failed (transaction_data_hex == "") - txDataHashWithHex = bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2(cryptoType, ""); + txDataHashWithHex = bcos_sdk_calc_transaction_data_struct_hash_with_hex_v1(cryptoType, ""); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); BOOST_TEST(txDataHashWithHex == nullptr); @@ -622,7 +622,7 @@ BOOST_AUTO_TEST_CASE(testCalculateTxDataHashV2) // calculate tx data hash with hex success (encodedtxDataWithHex) cryptoType = 1; txDataHashWithHex = - bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2(cryptoType, encodedtxDataWithHex); + bcos_sdk_calc_transaction_data_struct_hash_with_hex_v1(cryptoType, encodedtxDataWithHex); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == true); BOOST_TEST(txDataHashWithHex != nullptr); @@ -630,7 +630,7 @@ BOOST_AUTO_TEST_CASE(testCalculateTxDataHashV2) // calculate tx data hash with hex success (encodedtxDataWithByte) txDataHashWithHex = - bcos_sdk_calc_transaction_data_struct_hash_with_hex_v2(cryptoType, encodedtxDataWithByte); + bcos_sdk_calc_transaction_data_struct_hash_with_hex_v1(cryptoType, encodedtxDataWithByte); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == true); BOOST_TEST(txDataHashWithHex != nullptr); @@ -639,21 +639,21 @@ BOOST_AUTO_TEST_CASE(testCalculateTxDataHashV2) // 3. compare hash BOOST_TEST(txDataHash == txDataHashWithHex); - bcos_sdk_destroy_transaction_data_struct_v2(txDataWithHex); - bcos_sdk_destroy_transaction_data_struct_v2(txDataWithByte); + bcos_sdk_destroy_transaction_data_struct_v1(txDataWithHex); + bcos_sdk_destroy_transaction_data_struct_v1(txDataWithByte); } -BOOST_AUTO_TEST_CASE(testCreateTxStructV2) +BOOST_AUTO_TEST_CASE(testCreateTxStructv1) { - auto txDataStruct = createTxDataStructWithHexInputV2(); - auto transactionDataHash = bcos_sdk_calc_transaction_data_struct_hash_v2(1, txDataStruct); + auto txDataStruct = createTxDataStructWithHexInputv1(); + auto transactionDataHash = bcos_sdk_calc_transaction_data_struct_hash_v1(1, txDataStruct); void* key_pair = bcos_sdk_create_keypair(1); auto signature = bcos_sdk_sign_transaction_data_hash(key_pair, transactionDataHash); auto extraData = ""; int64_t attribute = 0; - // create transaction_struct failed (transaction_data_struct_v2 = NULL) - auto txStruct = bcos_sdk_create_transaction_struct_v2( + // create transaction_struct failed (transaction_data_struct_v1 = NULL) + auto txStruct = bcos_sdk_create_transaction_struct_v1( nullptr, signature, transactionDataHash, attribute, extraData); auto success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); @@ -661,7 +661,7 @@ BOOST_AUTO_TEST_CASE(testCreateTxStructV2) BOOST_TEST(bcos_sdk_get_last_error() == -1); // create transaction_struct failed (transaction_data_hash = NULL) - txStruct = bcos_sdk_create_transaction_struct_v2( + txStruct = bcos_sdk_create_transaction_struct_v1( txDataStruct, signature, nullptr, attribute, extraData); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); @@ -669,7 +669,7 @@ BOOST_AUTO_TEST_CASE(testCreateTxStructV2) BOOST_TEST(bcos_sdk_get_last_error() == -1); // create transaction_struct failed (signature = NULL) - txStruct = bcos_sdk_create_transaction_struct_v2( + txStruct = bcos_sdk_create_transaction_struct_v1( txDataStruct, nullptr, transactionDataHash, attribute, extraData); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); @@ -678,7 +678,7 @@ BOOST_AUTO_TEST_CASE(testCreateTxStructV2) // create transaction_struct failed (transaction_data_hash = "") txStruct = - bcos_sdk_create_transaction_struct_v2(txDataStruct, signature, "", attribute, extraData); + bcos_sdk_create_transaction_struct_v1(txDataStruct, signature, "", attribute, extraData); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); BOOST_TEST(txStruct == nullptr); @@ -687,7 +687,7 @@ BOOST_AUTO_TEST_CASE(testCreateTxStructV2) std::string("transaction_data_hash can not be empty string")); // create transaction_struct failed (signature = "") - txStruct = bcos_sdk_create_transaction_struct_v2( + txStruct = bcos_sdk_create_transaction_struct_v1( txDataStruct, "", transactionDataHash, attribute, extraData); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); @@ -696,38 +696,38 @@ BOOST_AUTO_TEST_CASE(testCreateTxStructV2) BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("signature can not be empty string")); // create transaction_struct success (extraData = NULL) - txStruct = bcos_sdk_create_transaction_struct_v2( + txStruct = bcos_sdk_create_transaction_struct_v1( txDataStruct, signature, transactionDataHash, attribute, nullptr); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == true); compareTxStruct(txStruct, txDataStruct, signature, "", attribute); // create transaction_struct success - txStruct = bcos_sdk_create_transaction_struct_v2( + txStruct = bcos_sdk_create_transaction_struct_v1( txDataStruct, signature, transactionDataHash, attribute, extraData); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == true); compareTxStruct(txStruct, txDataStruct, signature, extraData, attribute); - bcos_sdk_destroy_transaction_data_struct_v2(txDataStruct); - bcos_sdk_destroy_transaction_struct_v2(txStruct); + bcos_sdk_destroy_transaction_data_struct_v1(txDataStruct); + bcos_sdk_destroy_transaction_struct_v1(txStruct); } -BOOST_AUTO_TEST_CASE(testCreateEncodedTxV2) +BOOST_AUTO_TEST_CASE(testCreateEncodedTxv1) { - auto txDataStruct = createTxDataStructWithHexInputV2(); - auto transactionDataHash = bcos_sdk_calc_transaction_data_struct_hash_v2(1, txDataStruct); + auto txDataStruct = createTxDataStructWithHexInputv1(); + auto transactionDataHash = bcos_sdk_calc_transaction_data_struct_hash_v1(1, txDataStruct); void* key_pair = bcos_sdk_create_keypair(1); auto signature = bcos_sdk_sign_transaction_data_hash(key_pair, transactionDataHash); auto extraData = ""; int64_t attribute = 0; - auto txStruct = bcos_sdk_create_transaction_struct_v2( + auto txStruct = bcos_sdk_create_transaction_struct_v1( txDataStruct, signature, transactionDataHash, attribute, extraData); - // 1. test bcos_sdk_create_encoded_transaction_v2 - // create encoded tx failed (transaction_data_struct_v2 = NULL) - auto encodedTx = bcos_sdk_create_encoded_transaction_v2( + // 1. test bcos_sdk_create_encoded_transaction_v1 + // create encoded tx failed (transaction_data_struct_v1 = NULL) + auto encodedTx = bcos_sdk_create_encoded_transaction_v1( nullptr, signature, transactionDataHash, attribute, extraData); auto success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); @@ -735,7 +735,7 @@ BOOST_AUTO_TEST_CASE(testCreateEncodedTxV2) BOOST_TEST(bcos_sdk_get_last_error() == -1); // create encoded tx failed (transaction_data_hash = NULL) - encodedTx = bcos_sdk_create_encoded_transaction_v2( + encodedTx = bcos_sdk_create_encoded_transaction_v1( txDataStruct, signature, nullptr, attribute, extraData); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); @@ -743,7 +743,7 @@ BOOST_AUTO_TEST_CASE(testCreateEncodedTxV2) BOOST_TEST(bcos_sdk_get_last_error() == -1); // create encoded tx failed (signature = NULL) - encodedTx = bcos_sdk_create_encoded_transaction_v2( + encodedTx = bcos_sdk_create_encoded_transaction_v1( txDataStruct, nullptr, transactionDataHash, attribute, extraData); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); @@ -752,7 +752,7 @@ BOOST_AUTO_TEST_CASE(testCreateEncodedTxV2) // create encoded tx failed (transaction_data_hash = "") encodedTx = - bcos_sdk_create_encoded_transaction_v2(txDataStruct, signature, "", attribute, extraData); + bcos_sdk_create_encoded_transaction_v1(txDataStruct, signature, "", attribute, extraData); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); BOOST_TEST(encodedTx == nullptr); @@ -761,7 +761,7 @@ BOOST_AUTO_TEST_CASE(testCreateEncodedTxV2) std::string("transaction_data_hash can not be empty string")); // create encoded tx failed (signature = "") - encodedTx = bcos_sdk_create_encoded_transaction_v2( + encodedTx = bcos_sdk_create_encoded_transaction_v1( txDataStruct, "", transactionDataHash, attribute, extraData); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); @@ -770,23 +770,23 @@ BOOST_AUTO_TEST_CASE(testCreateEncodedTxV2) BOOST_TEST(bcos_sdk_get_last_error_msg() == std::string("signature can not be empty string")); // create encoded tx success - encodedTx = bcos_sdk_create_encoded_transaction_v2( + encodedTx = bcos_sdk_create_encoded_transaction_v1( txDataStruct, signature, transactionDataHash, attribute, extraData); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == true); BOOST_TEST(encodedTx != nullptr); BOOST_TEST(bcos_sdk_get_last_error() == 0); - // 2. test bcos_sdk_encode_transaction_struct_to_hex__v2 + // 2. test bcos_sdk_encode_transaction_struct_to_hex__v1 // create encoded tx with tx_struct failed (transaction_struct == "") - auto encodedTxWithTxStruct = bcos_sdk_encode_transaction_struct_to_hex_v2(nullptr); + auto encodedTxWithTxStruct = bcos_sdk_encode_transaction_struct_to_hex_v1(nullptr); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == false); BOOST_TEST(encodedTxWithTxStruct == nullptr); BOOST_TEST(bcos_sdk_get_last_error() == -1); // create encoded tx with tx_struct success - encodedTxWithTxStruct = bcos_sdk_encode_transaction_struct_to_hex_v2(txStruct); + encodedTxWithTxStruct = bcos_sdk_encode_transaction_struct_to_hex_v1(txStruct); success = bcos_sdk_is_last_opr_success(); BOOST_TEST(success == true); BOOST_TEST(encodedTxWithTxStruct != nullptr); @@ -795,46 +795,46 @@ BOOST_AUTO_TEST_CASE(testCreateEncodedTxV2) // 3. compare encodedTx encodedTxWithTxStruct BOOST_TEST(encodedTx == encodedTxWithTxStruct); - bcos_sdk_destroy_transaction_data_struct_v2(txDataStruct); - bcos_sdk_destroy_transaction_struct_v2(txStruct); + bcos_sdk_destroy_transaction_data_struct_v1(txDataStruct); + bcos_sdk_destroy_transaction_struct_v1(txStruct); } -BOOST_AUTO_TEST_CASE(testEncodeDecodeTxStructV2) +BOOST_AUTO_TEST_CASE(testEncodeDecodeTxStructv1) { - auto txDataStruct = createTxDataStructWithHexInputV2(); - auto transactionDataHash = bcos_sdk_calc_transaction_data_struct_hash_v2(1, txDataStruct); + auto txDataStruct = createTxDataStructWithHexInputv1(); + auto transactionDataHash = bcos_sdk_calc_transaction_data_struct_hash_v1(1, txDataStruct); void* key_pair = bcos_sdk_create_keypair(1); auto signature = bcos_sdk_sign_transaction_data_hash(key_pair, transactionDataHash); auto extraData = ""; int64_t attribute = 0; - auto txStruct = bcos_sdk_create_transaction_struct_v2( + auto txStruct = bcos_sdk_create_transaction_struct_v1( txDataStruct, signature, transactionDataHash, attribute, extraData); // 1. test tx encode decode hex // encode tx to hex failed (transaction == NULL) - auto encodedTxHex = bcos_sdk_encode_transaction_struct_to_hex_v2(nullptr); + auto encodedTxHex = bcos_sdk_encode_transaction_struct_to_hex_v1(nullptr); auto hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == false); BOOST_TEST(encodedTxHex == nullptr); BOOST_TEST(bcos_sdk_get_last_error() == -1); // encode tx to hex success - encodedTxHex = bcos_sdk_encode_transaction_struct_to_hex_v2(txStruct); + encodedTxHex = bcos_sdk_encode_transaction_struct_to_hex_v1(txStruct); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == true); BOOST_TEST(encodedTxHex != nullptr); BOOST_TEST(bcos_sdk_get_last_error() == 0); // decode hex failed (transaction_hex_str == NULL) - auto decodedTxHex = bcos_sdk_decode_transaction_struct_from_hex_v2(nullptr); + auto decodedTxHex = bcos_sdk_decode_transaction_struct_from_hex_v1(nullptr); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == false); BOOST_TEST(decodedTxHex == nullptr); BOOST_TEST(bcos_sdk_get_last_error() == -1); // decode hex failed (transaction_hex_str == "") - decodedTxHex = bcos_sdk_decode_transaction_struct_from_hex_v2(""); + decodedTxHex = bcos_sdk_decode_transaction_struct_from_hex_v1(""); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == false); BOOST_TEST(decodedTxHex == nullptr); @@ -843,7 +843,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxStructV2) std::string("transaction_hex_str can not be empty string")); // decode hex failed (the length of transaction_hex_str is not even number) - decodedTxHex = bcos_sdk_decode_transaction_struct_from_hex_v2("0xabc"); + decodedTxHex = bcos_sdk_decode_transaction_struct_from_hex_v1("0xabc"); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == false); BOOST_TEST(decodedTxHex == nullptr); @@ -852,7 +852,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxStructV2) std::string("the length of transaction_hex_str must be an even number")); // decode hex success - decodedTxHex = bcos_sdk_decode_transaction_struct_from_hex_v2(encodedTxHex); + decodedTxHex = bcos_sdk_decode_transaction_struct_from_hex_v1(encodedTxHex); hexSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(hexSuccess == true); BOOST_TEST(decodedTxHex != nullptr); @@ -862,28 +862,28 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxStructV2) // 2. test tx encode decode json // encode tx to json failed (transaction == NULL) - auto encodedTxJson = bcos_sdk_encode_transaction_struct_to_json_v2(nullptr); + auto encodedTxJson = bcos_sdk_encode_transaction_struct_to_json_v1(nullptr); auto jsonSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(jsonSuccess == false); BOOST_TEST(encodedTxJson == nullptr); BOOST_TEST(bcos_sdk_get_last_error() == -1); // encode tx to json success - encodedTxJson = bcos_sdk_encode_transaction_struct_to_json_v2(txStruct); + encodedTxJson = bcos_sdk_encode_transaction_struct_to_json_v1(txStruct); jsonSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(jsonSuccess == true); BOOST_TEST(encodedTxJson != nullptr); BOOST_TEST(bcos_sdk_get_last_error() == 0); // decode json failed (transaction_json_str == NULL) - auto decodedTxJson = bcos_sdk_decode_transaction_struct_from_json_v2(nullptr); + auto decodedTxJson = bcos_sdk_decode_transaction_struct_from_json_v1(nullptr); jsonSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(jsonSuccess == false); BOOST_TEST(decodedTxJson == nullptr); BOOST_TEST(bcos_sdk_get_last_error() == -1); // decode json failed (transaction_json_str == "") - decodedTxJson = bcos_sdk_decode_transaction_struct_from_json_v2(""); + decodedTxJson = bcos_sdk_decode_transaction_struct_from_json_v1(""); jsonSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(jsonSuccess == false); BOOST_TEST(decodedTxJson == nullptr); @@ -892,7 +892,7 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxStructV2) std::string("transaction_json_str can not be empty string")); // decode json success - decodedTxJson = bcos_sdk_decode_transaction_struct_from_json_v2(encodedTxJson); + decodedTxJson = bcos_sdk_decode_transaction_struct_from_json_v1(encodedTxJson); jsonSuccess = bcos_sdk_is_last_opr_success(); BOOST_TEST(jsonSuccess == true); BOOST_TEST(decodedTxJson != nullptr); @@ -900,10 +900,10 @@ BOOST_AUTO_TEST_CASE(testEncodeDecodeTxStructV2) compareTxStruct(decodedTxJson, txDataStruct, signature, extraData, attribute); - bcos_sdk_destroy_transaction_data_struct_v2(txDataStruct); - bcos_sdk_destroy_transaction_struct_v2(txStruct); - bcos_sdk_destroy_transaction_struct_v2(decodedTxHex); - bcos_sdk_destroy_transaction_struct_v2(decodedTxJson); + bcos_sdk_destroy_transaction_data_struct_v1(txDataStruct); + bcos_sdk_destroy_transaction_struct_v1(txStruct); + bcos_sdk_destroy_transaction_struct_v1(decodedTxHex); + bcos_sdk_destroy_transaction_struct_v1(decodedTxJson); } BOOST_AUTO_TEST_SUITE_END() \ No newline at end of file diff --git a/vcpkg-configuration.json b/vcpkg-configuration.json index 19978ab0c..f271077e4 100644 --- a/vcpkg-configuration.json +++ b/vcpkg-configuration.json @@ -3,7 +3,7 @@ { "kind": "git", "repository": "https://github.com/FISCO-BCOS/registry", - "baseline": "cf9b1d3f9133ac203abe2ae22a887f4efc2fdccb", + "baseline": "87805d2845731a26b5e9a80beff1cdf83331279e", "packages": [ "openssl", "hsm-crypto", From ba85c26e22539acc39ca5e657fe1288d13c37437 Mon Sep 17 00:00:00 2001 From: Kyon <32325790+kyonRay@users.noreply.github.com> Date: Mon, 5 Feb 2024 08:35:30 +0800 Subject: [PATCH 29/29] (jni): fix jni find class bug. (#215) --- ...ities_tx_TransactionStructBuilderJniObj.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp index ed5582b4e..a3ad6e6ba 100644 --- a/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp +++ b/bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj.cpp @@ -292,12 +292,12 @@ jobject convert_to_tx_v1_data_jobject( return NULL; } - jclass txDataClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDatav1"); + jclass txDataClass = env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV1"); if (txDataClass == NULL) { env->FatalError( "No such class, className: " - "org/fisco/bcos/sdk/jni/utilities/tx/TransactionDatav1"); + "org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV1"); } jmethodID txDataMtd = env->GetMethodID(txDataClass, "", "()V"); if (txDataMtd == NULL) @@ -769,7 +769,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr } const char* tx_data_hex = nullptr; if (env->IsInstanceOf(jTransactionDataObj, - env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDatav1")) == JNI_TRUE) + env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV1")) == JNI_TRUE) { bcos_sdk_c_transaction_data_v1* tx_data_struct = convert_to_tx_v1_data_struct(env, jTransactionDataObj); @@ -833,7 +833,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr } const char* tx_data_json = nullptr; if (env->IsInstanceOf(jTransactionDataObj, - env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDatav1")) == JNI_TRUE) + env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV1")) == JNI_TRUE) { bcos_sdk_c_transaction_data_v1* tx_data_struct = convert_to_tx_v1_data_struct(env, jTransactionDataObj); @@ -919,7 +919,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTr /* * Class: org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj * Method: decodeTransactionDataStructv1 - * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDatav1; + * Signature: (Ljava/lang/String;)Lorg/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV1; */ JNIEXPORT jobject JNICALL Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_decodeTransactionDataStructV1( @@ -970,7 +970,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_calcTran const char* tx_data_hash = nullptr; int crypto_type = jCrytpTyte; if (env->IsInstanceOf(jTransactionDataObj, - env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDatav1")) == JNI_TRUE) + env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV1")) == JNI_TRUE) { bcos_sdk_c_transaction_data_v1* tx_data_struct = convert_to_tx_v1_data_struct(env, jTransactionDataObj); @@ -1040,7 +1040,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_createEn int attribute = jAttribute; const char* extra_data = env->GetStringUTFChars(jExtraData, NULL); if (env->IsInstanceOf(jTxDataObj, - env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDatav1")) == JNI_TRUE) + env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV1")) == JNI_TRUE) { bcos_sdk_c_transaction_data_v1* tx_data_struct = convert_to_tx_v1_data_struct(env, jTxDataObj); @@ -1113,7 +1113,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr CHECK_OBJECT_NOT_NULL(env, javaTxDataObj, nullptr); const char* tx_hex = nullptr; if (env->IsInstanceOf(javaTxDataObj, - env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDatav1")) == JNI_TRUE) + env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV1")) == JNI_TRUE) { bcos_sdk_c_transaction_v1* tx_struct = convert_to_tx_v1_struct(env, jTransactionObj); if (tx_struct == NULL) @@ -1176,7 +1176,7 @@ Java_org_fisco_bcos_sdk_jni_utilities_tx_TransactionStructBuilderJniObj_encodeTr CHECK_OBJECT_NOT_NULL(env, javaTxDataObj, nullptr); const char* tx_json = nullptr; if (env->IsInstanceOf(javaTxDataObj, - env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDatav1")) == JNI_TRUE) + env->FindClass("org/fisco/bcos/sdk/jni/utilities/tx/TransactionDataV1")) == JNI_TRUE) { bcos_sdk_c_transaction_v1* tx_struct = convert_to_tx_v1_struct(env, jTransactionObj); if (tx_struct == NULL)