From 4fc6903280ad5a7ac75018b7c03176a4d955eff8 Mon Sep 17 00:00:00 2001 From: Ian Holsman Date: Tue, 26 May 2020 22:28:16 -0400 Subject: [PATCH 1/4] debug tool, helpful for diagnosing abi issues --- CMakeLists.txt | 12 +++++++ tools/CMakeLists.txt | 3 ++ tools/hex_to_json.c | 81 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 96 insertions(+) create mode 100644 tools/hex_to_json.c diff --git a/CMakeLists.txt b/CMakeLists.txt index c14e851..b462e95 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -36,11 +36,21 @@ find_package(Threads) add_library(abieos STATIC src/abi.cpp src/crypto.cpp include/eosio/fpconv.c) target_include_directories(abieos PUBLIC include external/rapidjson/include) +add_library(abieos_static STATIC src/abi.cpp src/abieos.cpp src/crypto.cpp include/eosio/fpconv.c) +target_include_directories(abieos_static PUBLIC include external/outcome/single-header external/rapidjson/include external/date/include) +set_target_properties(abieos_static PROPERTIES POSITION_INDEPENDENT_CODE ON) + add_library(abieos_module MODULE src/abieos.cpp src/abi.cpp src/crypto.cpp include/eosio/fpconv.c) target_include_directories(abieos_module PUBLIC include external/rapidjson/include) target_link_libraries(abieos_module ${CMAKE_THREAD_LIBS_INIT}) set_target_properties(abieos_module PROPERTIES OUTPUT_NAME "abieos") +if (APPLE) + add_library(abieos_shared SHARED src/abieos.cpp) + target_link_libraries(abieos_shared abieos) + set_target_properties(abieos_shared PROPERTIES OUTPUT_NAME "abieos") +endif() + enable_testing() add_executable(test_abieos src/test.cpp src/abieos.cpp) @@ -79,3 +89,5 @@ endif() if (NOT ABIEOS_ONLY_LIBRARY) add_subdirectory(tools) endif() + +install(TARGETS abieos_static DESTINATION .) diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index cf87dd8..ca77b73 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -5,5 +5,8 @@ find_package(Threads) add_executable(name name.cpp) target_link_libraries(name abieos ${CMAKE_THREAD_LIBS_INIT}) +add_executable(hex_to_json hex_to_json.c) +target_link_libraries(hex_to_json abieos_static ${CMAKE_THREAD_LIBS_INIT}) + add_custom_command( TARGET name POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $ ${CMAKE_CURRENT_BINARY_DIR}/name2num ) add_custom_command( TARGET name POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $ ${CMAKE_CURRENT_BINARY_DIR}/num2name ) diff --git a/tools/hex_to_json.c b/tools/hex_to_json.c new file mode 100644 index 0000000..1526c2c --- /dev/null +++ b/tools/hex_to_json.c @@ -0,0 +1,81 @@ +// +// Created by Ian Holsman on 5/26/20. +// +#include "../src/abieos.h" +#include +#include + +void usage(char* name) { + fprintf(stderr, "Usage: %s abi_file.json hex-type hex-string\n", name); + fputs( "This example takes a abieos encoded hex string, and returns the corresponding json\n", stderr); + fputs("Example:\n",stderr); + fputs("$ ./hex_to_json transaction.abi.json transaction AE0D635CDCAC90A6DCFA000000000100A6823403EA3055000000572D3CCDCD0100AEAA4AC15CFD4500000000A8ED32323B00AEAA4AC15CFD4500000060D234CD3DA06806000000000004454F53000000001A746865206772617373686F70706572206C69657320686561767900\n",stderr); +} +int main(int argc, char* argv[]) { + const char* contract_str = "eosio"; + if (argc != 4) { + usage(argv[0]); + return 1; + } + char* abi_filename = argv[1]; + char* hex_type = argv[2]; + char* hex_string = argv[3]; + + char* source = NULL; + FILE* abi_file = fopen(abi_filename, "r"); + if (abi_file != NULL) { + /* Go to the end of the file. */ + if (fseek(abi_file, 0L, SEEK_END) == 0) { + /* Get the size of the file. */ + long bufsize = ftell(abi_file); + if (bufsize == -1) { /* Error */ + } + + /* Allocate our buffer to that size. */ + source = malloc(sizeof(char) * (bufsize + 1)); + + /* Go back to the start of the file. */ + if (fseek(abi_file, 0L, SEEK_SET) != 0) { /* Error */ + } + + /* Read the entire file into memory. */ + size_t newLen = fread(source, sizeof(char), bufsize, abi_file); + if (ferror(abi_file) != 0) { + fputs("Error reading file\n", stderr); + } else { + source[newLen++] = '\0'; /* Just to be safe. */ + } + } + fclose(abi_file); + } else { + fprintf(stderr, "Unable to open abi_file.json %s\n", abi_filename); + return -1; + } + abieos_context* context = abieos_create(); + if (context == NULL) { + fprintf(stderr, "Unable to create context\n"); + } else { + uint64_t contract_name = abieos_string_to_name(context, contract_str); + if (contract_name == 0) { + fprintf(stderr, "Error: abieos_string_to_name %s\n", abieos_get_error(context)); + } else { + abieos_bool result = abieos_set_abi(context, contract_name, source); + if (!result) { + fprintf(stderr, "Error: abieos_set_abi %s\n", abieos_get_error(context)); + } else { + const char* json = abieos_hex_to_json(context, contract_name, hex_type, hex_string); + if (!json) { + fprintf(stderr, "Error: abieos_hex_to_json %s\n", abieos_get_error(context)); + + } else { + fputs(json, stdout); + } + } + } + abieos_destroy(context); + } + + free(source); + + return 0; +} From fe340534709fc70e7affec145f9c4923b1f9f5f9 Mon Sep 17 00:00:00 2001 From: Ian Holsman Date: Tue, 26 May 2020 22:44:23 -0400 Subject: [PATCH 2/4] Update hex_to_json.c --- tools/hex_to_json.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/tools/hex_to_json.c b/tools/hex_to_json.c index 1526c2c..fbfccce 100644 --- a/tools/hex_to_json.c +++ b/tools/hex_to_json.c @@ -1,6 +1,3 @@ -// -// Created by Ian Holsman on 5/26/20. -// #include "../src/abieos.h" #include #include From a019ce4b220be62a692205d1711c3aba84797bc6 Mon Sep 17 00:00:00 2001 From: Ian Holsman Date: Wed, 27 May 2020 16:52:00 -0400 Subject: [PATCH 3/4] make it do reverse as well. --- tools/CMakeLists.txt | 7 ++- tools/hex_to_json.c | 123 ++++++++++++++++++++++++++++--------------- 2 files changed, 85 insertions(+), 45 deletions(-) diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index ca77b73..f668ba1 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -5,8 +5,11 @@ find_package(Threads) add_executable(name name.cpp) target_link_libraries(name abieos ${CMAKE_THREAD_LIBS_INIT}) +add_custom_command( TARGET name POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $ ${CMAKE_CURRENT_BINARY_DIR}/name2num ) +add_custom_command( TARGET name POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $ ${CMAKE_CURRENT_BINARY_DIR}/num2name ) + add_executable(hex_to_json hex_to_json.c) target_link_libraries(hex_to_json abieos_static ${CMAKE_THREAD_LIBS_INIT}) -add_custom_command( TARGET name POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $ ${CMAKE_CURRENT_BINARY_DIR}/name2num ) -add_custom_command( TARGET name POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $ ${CMAKE_CURRENT_BINARY_DIR}/num2name ) +add_custom_command( TARGET hex_to_json POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $ ${CMAKE_CURRENT_BINARY_DIR}/json2hex ) +add_custom_command( TARGET hex_to_json POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $ ${CMAKE_CURRENT_BINARY_DIR}/hex2json ) diff --git a/tools/hex_to_json.c b/tools/hex_to_json.c index 1526c2c..bf3477c 100644 --- a/tools/hex_to_json.c +++ b/tools/hex_to_json.c @@ -1,80 +1,117 @@ // // Created by Ian Holsman on 5/26/20. // -#include "../src/abieos.h" #include #include +#include +#include +#include +#include "../src/abieos.h" -void usage(char* name) { - fprintf(stderr, "Usage: %s abi_file.json hex-type hex-string\n", name); - fputs( "This example takes a abieos encoded hex string, and returns the corresponding json\n", stderr); - fputs("Example:\n",stderr); - fputs("$ ./hex_to_json transaction.abi.json transaction AE0D635CDCAC90A6DCFA000000000100A6823403EA3055000000572D3CCDCD0100AEAA4AC15CFD4500000000A8ED32323B00AEAA4AC15CFD4500000060D234CD3DA06806000000000004454F53000000001A746865206772617373686F70706572206C69657320686561767900\n",stderr); +void usage(char* name, bool reverse ) { + if (reverse) { + fprintf(stderr, "Usage: %s abi_file.json type json\n", name); + fputs("This example takes a json value, and returns the corresponding abieos encoded hex string\n", stderr); + fputs("Example:\n", stderr); + fprintf(stderr, + "$ ./%s transaction.abi.json transaction '{your json here}'\n", name); + } else { + fprintf(stderr, "Usage: %s abi_file.json hex-type hex-string\n", name); + fputs("This example takes a abieos encoded hex string, and returns the corresponding json\n", stderr); + fputs("Example:\n", stderr); + fprintf(stderr, + "$ ./%s transaction.abi.json transaction AE0D635CDCAC90A6DCFA000000000100A6823403EA3055000000572D3CCDCD0100AEAA4AC15CFD4500000000A8ED32323B00AEAA4AC15CFD4500000060D234CD3DA06806000000000004454F53000000001A746865206772617373686F70706572206C69657320686561767900\n", + name); + } } + + int main(int argc, char* argv[]) { const char* contract_str = "eosio"; + bool reverse = false; + char* program_name = basename(argv[0]); + + if (strcasecmp(program_name, "json2hex") == 0) { + reverse = true; + } + if (argc != 4) { - usage(argv[0]); + usage(program_name, reverse); return 1; } char* abi_filename = argv[1]; char* hex_type = argv[2]; - char* hex_string = argv[3]; + char* hex_json_string = argv[3]; char* source = NULL; FILE* abi_file = fopen(abi_filename, "r"); - if (abi_file != NULL) { - /* Go to the end of the file. */ - if (fseek(abi_file, 0L, SEEK_END) == 0) { - /* Get the size of the file. */ - long bufsize = ftell(abi_file); - if (bufsize == -1) { /* Error */ - } + if (abi_file == NULL) { + fprintf(stderr, "unable to open abi_file.json %s\n", abi_filename); + return -1; + } + /* Go to the end of the file. */ + if (fseek(abi_file, 0L, SEEK_END) == 0) { + /* Get the size of the file. */ + long bufsize = ftell(abi_file); + if (bufsize == -1) { + /* Error */ + fprintf(stderr, "ftell error\n"); + return -1; + } - /* Allocate our buffer to that size. */ - source = malloc(sizeof(char) * (bufsize + 1)); + /* Allocate our buffer to that size. */ + source = malloc(sizeof(char) * (bufsize + 1)); - /* Go back to the start of the file. */ - if (fseek(abi_file, 0L, SEEK_SET) != 0) { /* Error */ - } + /* Go back to the start of the file. */ + if (fseek(abi_file, 0L, SEEK_SET) != 0) { /* Error */ + } - /* Read the entire file into memory. */ - size_t newLen = fread(source, sizeof(char), bufsize, abi_file); - if (ferror(abi_file) != 0) { - fputs("Error reading file\n", stderr); - } else { - source[newLen++] = '\0'; /* Just to be safe. */ - } + /* Read the entire file into memory. */ + size_t newLen = fread(source, sizeof(char), bufsize, abi_file); + if (ferror(abi_file) != 0) { + fputs("Error reading file\n", stderr); + } else { + source[newLen++] = '\0'; /* Just to be safe. */ } - fclose(abi_file); - } else { - fprintf(stderr, "Unable to open abi_file.json %s\n", abi_filename); - return -1; } + fclose(abi_file); + abieos_context* context = abieos_create(); if (context == NULL) { fprintf(stderr, "Unable to create context\n"); + free(source); + return -1; + } + + uint64_t contract_name = abieos_string_to_name(context, contract_str); + if (contract_name == 0) { + fprintf(stderr, "Error: abieos_string_to_name %s\n", abieos_get_error(context)); } else { - uint64_t contract_name = abieos_string_to_name(context, contract_str); - if (contract_name == 0) { - fprintf(stderr, "Error: abieos_string_to_name %s\n", abieos_get_error(context)); + abieos_bool result = abieos_set_abi(context, contract_name, source); + if (!result) { + fprintf(stderr, "Error: abieos_set_abi %s\n", abieos_get_error(context)); } else { - abieos_bool result = abieos_set_abi(context, contract_name, source); - if (!result) { - fprintf(stderr, "Error: abieos_set_abi %s\n", abieos_get_error(context)); + if (reverse) { + abieos_bool j2b_result = abieos_json_to_bin(context, contract_name, hex_type, hex_json_string); + if (j2b_result) { + const char*hex = abieos_get_bin_hex(context); + fprintf(stdout, "%s\n",hex); + } else { + fprintf(stderr, "Error: %s %s\n", program_name, abieos_get_error(context)); + } + } else { - const char* json = abieos_hex_to_json(context, contract_name, hex_type, hex_string); + const char* json = abieos_hex_to_json(context, contract_name, hex_type, hex_json_string); if (!json) { - fprintf(stderr, "Error: abieos_hex_to_json %s\n", abieos_get_error(context)); - + fprintf(stderr, "Error: %s %s\n", program_name, abieos_get_error(context)); } else { - fputs(json, stdout); + fprintf(stdout, "%s\n",json); } } + } - abieos_destroy(context); } - + abieos_destroy(context); free(source); return 0; From 48b8314c920ceca5dfa5e6b27ff44f7ad29d6f29 Mon Sep 17 00:00:00 2001 From: Ian Holsman Date: Thu, 28 May 2020 09:53:02 -0400 Subject: [PATCH 4/4] make 'abieos' build target include abieos.cpp. remove apple & abieos_static targets --- CMakeLists.txt | 14 ++------------ tools/CMakeLists.txt | 2 +- 2 files changed, 3 insertions(+), 13 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index b462e95..19ab3d7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -33,24 +33,14 @@ endif() find_package(Threads) -add_library(abieos STATIC src/abi.cpp src/crypto.cpp include/eosio/fpconv.c) +add_library(abieos STATIC src/abi.cpp src/crypto.cpp include/eosio/fpconv.c src/abieos.cpp) target_include_directories(abieos PUBLIC include external/rapidjson/include) -add_library(abieos_static STATIC src/abi.cpp src/abieos.cpp src/crypto.cpp include/eosio/fpconv.c) -target_include_directories(abieos_static PUBLIC include external/outcome/single-header external/rapidjson/include external/date/include) -set_target_properties(abieos_static PROPERTIES POSITION_INDEPENDENT_CODE ON) - add_library(abieos_module MODULE src/abieos.cpp src/abi.cpp src/crypto.cpp include/eosio/fpconv.c) target_include_directories(abieos_module PUBLIC include external/rapidjson/include) target_link_libraries(abieos_module ${CMAKE_THREAD_LIBS_INIT}) set_target_properties(abieos_module PROPERTIES OUTPUT_NAME "abieos") -if (APPLE) - add_library(abieos_shared SHARED src/abieos.cpp) - target_link_libraries(abieos_shared abieos) - set_target_properties(abieos_shared PROPERTIES OUTPUT_NAME "abieos") -endif() - enable_testing() add_executable(test_abieos src/test.cpp src/abieos.cpp) @@ -90,4 +80,4 @@ if (NOT ABIEOS_ONLY_LIBRARY) add_subdirectory(tools) endif() -install(TARGETS abieos_static DESTINATION .) +install(TARGETS abieos DESTINATION .) diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index f668ba1..4baf89e 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -9,7 +9,7 @@ add_custom_command( TARGET name POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_sy add_custom_command( TARGET name POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $ ${CMAKE_CURRENT_BINARY_DIR}/num2name ) add_executable(hex_to_json hex_to_json.c) -target_link_libraries(hex_to_json abieos_static ${CMAKE_THREAD_LIBS_INIT}) +target_link_libraries(hex_to_json abieos ${CMAKE_THREAD_LIBS_INIT}) add_custom_command( TARGET hex_to_json POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $ ${CMAKE_CURRENT_BINARY_DIR}/json2hex ) add_custom_command( TARGET hex_to_json POST_BUILD COMMAND ${CMAKE_COMMAND} -E create_symlink $ ${CMAKE_CURRENT_BINARY_DIR}/hex2json )