Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<feat>(utilities): fix boost beast compile error in clang16, add decode tx logic. #201

Merged
merged 2 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ deps
*.out
*.app
.cache
build
build**
deps
.gradle
.vscode/
Expand Down
65 changes: 65 additions & 0 deletions bcos-c-sdk/bcos_sdk_c_uti_tx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,71 @@
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;

Check warning on line 398 in bcos-c-sdk/bcos_sdk_c_uti_tx.cpp

View check run for this annotation

Codecov / codecov/patch

bcos-c-sdk/bcos_sdk_c_uti_tx.cpp#L398

Added line #L398 was not covered by tests
}

/**
* @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)

Check warning on line 406 in bcos-c-sdk/bcos_sdk_c_uti_tx.cpp

View check run for this annotation

Codecov / codecov/patch

bcos-c-sdk/bcos_sdk_c_uti_tx.cpp#L406

Added line #L406 was not covered by tests
{
bcos_sdk_clear_last_error();

Check warning on line 408 in bcos-c-sdk/bcos_sdk_c_uti_tx.cpp

View check run for this annotation

Codecov / codecov/patch

bcos-c-sdk/bcos_sdk_c_uti_tx.cpp#L408

Added line #L408 was not covered by tests
BCOS_SDK_C_PARAMS_VERIFICATION(transaction_bytes, nullptr);
try

Check warning on line 410 in bcos-c-sdk/bcos_sdk_c_uti_tx.cpp

View check run for this annotation

Codecov / codecov/patch

bcos-c-sdk/bcos_sdk_c_uti_tx.cpp#L410

Added line #L410 was not covered by tests
{
TransactionBuilder builder{};
const auto transaction =
builder.decodeTransactionToJsonObj(*fromHexString(transaction_bytes));
return strdup(transaction.c_str());

Check warning on line 415 in bcos-c-sdk/bcos_sdk_c_uti_tx.cpp

View check run for this annotation

Codecov / codecov/patch

bcos-c-sdk/bcos_sdk_c_uti_tx.cpp#L415

Added line #L415 was not covered by tests
}
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;

Check warning on line 424 in bcos-c-sdk/bcos_sdk_c_uti_tx.cpp

View check run for this annotation

Codecov / codecov/patch

bcos-c-sdk/bcos_sdk_c_uti_tx.cpp#L424

Added line #L424 was not covered by tests
}

/**
* @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
*
Expand Down
22 changes: 22 additions & 0 deletions bcos-c-sdk/bcos_sdk_c_uti_tx.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
23 changes: 19 additions & 4 deletions bindings/java/jni/src/main/c/jni/org_fisco_bcos_sdk_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);

Expand Down
Loading
Loading