From fb6c29cc3de772b7005117ee4f2aa3ab6ae6d6bf Mon Sep 17 00:00:00 2001 From: jkrvivian Date: Fri, 15 Mar 2019 13:53:17 +0800 Subject: [PATCH 01/29] fix(core): Move HTTP response code to status_t --- accelerator/errors.h | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/accelerator/errors.h b/accelerator/errors.h index 672aa09f..ec47f273 100644 --- a/accelerator/errors.h +++ b/accelerator/errors.h @@ -47,16 +47,15 @@ extern "C" { #define SC_ERROR_MASK 0x07 /** @} */ -/** @name http error code */ -/** @{ */ -#define SC_BAD_REQUEST 400 -#define SC_NOT_FOUND 404 -/** @} */ - /* status code */ typedef enum { SC_OK = 0, /**< No error */ + SC_HTTP_OK = 200, /**< HTTP response OK */ + SC_BAD_REQUEST = 400, /**< HTTP response, error when parsing request */ + SC_NOT_FOUND = 404, /**< HTTP request not found */ + SC_INTERNAL_SERVICE_ERROR = 500, /**< HTTP response, other errors in TA */ + SC_TA_OOM = 0x01 | SC_MODULE_TA | SC_SEVERITY_FATAL, /**< Fail to create TA object */ SC_TA_NULL = 0x02 | SC_MODULE_TA | SC_SEVERITY_FATAL, From cb3d9264bdf44b0559228b5f9b40485a32e1ecae Mon Sep 17 00:00:00 2001 From: jkrvivian Date: Fri, 15 Mar 2019 13:57:47 +0800 Subject: [PATCH 02/29] feat(core): Response error code for invalid result --- accelerator/server.cc | 62 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/accelerator/server.cc b/accelerator/server.cc index 485d8e70..e9a39d7e 100644 --- a/accelerator/server.cc +++ b/accelerator/server.cc @@ -14,6 +14,27 @@ void set_options_method_header(served::response& res) { res.set_header("Access-Control-Max-Age", "86400"); } +status_t set_response_content(status_t ret, char** json_result) { + status_t http_ret; + if (ret == SC_OK) { + return SC_HTTP_OK; + } + + cJSON* json_obj = cJSON_CreateObject(); + if (ret == SC_CCLIENT_NOT_FOUND) { + http_ret = SC_NOT_FOUND; + cJSON_AddStringToObject(json_obj, "message", "Request not found"); + } else if (ret == SC_SERIALIZER_JSON_PARSE) { + http_ret = SC_BAD_REQUEST; + cJSON_AddStringToObject(json_obj, "message", "Invalid request header"); + } else { + http_ret = SC_INTERNAL_SERVICE_ERROR; + cJSON_AddStringToObject(json_obj, "message", "Internal service error"); + } + *json_result = cJSON_PrintUnformatted(json_obj); + return http_ret; +} + int main(int, char const**) { served::multiplexer mux; mux.use_after(served::plugin::access_log); @@ -62,12 +83,15 @@ int main(int, char const**) { set_options_method_header(res); }) .get([&](served::response& res, const served::request& req) { + status_t ret = SC_OK; char* json_result; - api_find_transactions_by_tag(&service, req.params["tag"].c_str(), - &json_result); + ret = api_find_transactions_by_tag(&service, req.params["tag"].c_str(), + &json_result); + ret = set_response_content(ret, &json_result); res.set_header("Content-Type", "application/json"); res.set_header("Access-Control-Allow-Origin", "*"); + res.set_status(ret); res << json_result; }); @@ -84,12 +108,15 @@ int main(int, char const**) { set_options_method_header(res); }) .get([&](served::response& res, const served::request& req) { + status_t ret = SC_OK; char* json_result; - api_get_transaction_object(&service, req.params["tx"].c_str(), - &json_result); + ret = api_get_transaction_object(&service, req.params["tx"].c_str(), + &json_result); + ret = set_response_content(ret, &json_result); res.set_header("Content-Type", "application/json"); res.set_header("Access-Control-Allow-Origin", "*"); + res.set_status(ret); res << json_result; }); @@ -106,12 +133,15 @@ int main(int, char const**) { set_options_method_header(res); }) .get([&](served::response& res, const served::request& req) { + status_t ret = SC_OK; char* json_result; - api_find_transactions_obj_by_tag(&service, req.params["tag"].c_str(), - &json_result); + ret = api_find_transactions_obj_by_tag( + &service, req.params["tag"].c_str(), &json_result); + ret = set_response_content(ret, &json_result); res.set_header("Content-Type", "application/json"); res.set_header("Access-Control-Allow-Origin", "*"); + res.set_status(ret); res << json_result; }); @@ -126,11 +156,14 @@ int main(int, char const**) { set_options_method_header(res); }) .get([&](served::response& res, const served::request& req) { + status_t ret = SC_OK; char* json_result; - api_get_tips_pair(&service, &json_result); + ret = api_get_tips_pair(&service, &json_result); + ret = set_response_content(ret, &json_result); res.set_header("Content-Type", "application/json"); res.set_header("Access-Control-Allow-Origin", "*"); + res.set_status(ret); res << json_result; }); @@ -145,11 +178,14 @@ int main(int, char const**) { set_options_method_header(res); }) .get([&](served::response& res, const served::request& req) { + status_t ret = SC_OK; char* json_result; - api_get_tips(&service, &json_result); + ret = api_get_tips(&service, &json_result); + ret = set_response_content(ret, &json_result); res.set_header("Content-Type", "application/json"); res.set_header("Access-Control-Allow-Origin", "*"); + res.set_status(ret); res << json_result; }); @@ -164,11 +200,14 @@ int main(int, char const**) { set_options_method_header(res); }) .get([&](served::response& res, const served::request& req) { + status_t ret = SC_OK; char* json_result; - api_generate_address(&service, &json_result); + ret = api_generate_address(&service, &json_result); + ret = set_response_content(ret, &json_result); res.set_header("Content-Type", "application/json"); res.set_header("Access-Control-Allow-Origin", "*"); + res.set_status(ret); res << json_result; }); @@ -183,6 +222,7 @@ int main(int, char const**) { set_options_method_header(res); }) .post([&](served::response& res, const served::request& req) { + status_t ret = SC_OK; char* json_result; if (req.header("content-type").find("application/json") == @@ -195,7 +235,9 @@ int main(int, char const**) { res.set_status(SC_BAD_REQUEST); cJSON_Delete(json_obj); } else { - api_send_transfer(&service, req.body().c_str(), &json_result); + ret = api_send_transfer(&service, req.body().c_str(), &json_result); + ret = set_response_content(ret, &json_result); + res.set_status(ret); } res.set_header("Content-Type", "application/json"); From 92c1f8742d64d5222d129b63450d1a8965834ed9 Mon Sep 17 00:00:00 2001 From: jkrvivian Date: Fri, 15 Mar 2019 17:20:41 +0800 Subject: [PATCH 03/29] fix(core): Fix severity shift error and reorder error code Same type of error is given the same error code now(the last 3 bits). --- accelerator/errors.h | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/accelerator/errors.h b/accelerator/errors.h index ec47f273..764f6b13 100644 --- a/accelerator/errors.h +++ b/accelerator/errors.h @@ -24,10 +24,10 @@ extern "C" { #define SC_SEVERITY_MASK 0x18 #define SC_SEVERITY_SHIFT 3 -#define SC_SEVERITY_MINOR (0x0 << SC_SEVERITY_MASK) -#define SC_SEVERITY_MODERATE (0x01 << SC_SEVERITY_MASK) -#define SC_SEVERITY_MAJOR (0x02 << SC_SEVERITY_MASK) -#define SC_SEVERITY_FATAL (0x03 << SC_SEVERITY_MASK) +#define SC_SEVERITY_MINOR (0x0 << SC_SEVERITY_SHIFT) +#define SC_SEVERITY_MODERATE (0x01 << SC_SEVERITY_SHIFT) +#define SC_SEVERITY_MAJOR (0x02 << SC_SEVERITY_SHIFT) +#define SC_SEVERITY_FATAL (0x03 << SC_SEVERITY_SHIFT) /** @} */ /** @name module code */ @@ -64,27 +64,27 @@ typedef enum { // CClient module SC_CCLIENT_OOM = 0x01 | SC_MODULE_CCLIENT | SC_SEVERITY_FATAL, /**< Fail to create cclient object */ - SC_CCLIENT_NOT_FOUND = 0x02 | SC_MODULE_CCLIENT | SC_SEVERITY_FATAL, + SC_CCLIENT_NOT_FOUND = 0x03 | SC_MODULE_CCLIENT | SC_SEVERITY_FATAL, /**< Empty result from cclient */ - SC_CCLIENT_FAILED_RESPONSE = 0x03 | SC_MODULE_CCLIENT | SC_SEVERITY_FATAL, + SC_CCLIENT_FAILED_RESPONSE = 0x04 | SC_MODULE_CCLIENT | SC_SEVERITY_FATAL, /**< Error in cclient response */ - SC_CCLIENT_INVALID_FLEX_TRITS = 0x04 | SC_MODULE_CCLIENT | SC_SEVERITY_MAJOR, + SC_CCLIENT_INVALID_FLEX_TRITS = 0x05 | SC_MODULE_CCLIENT | SC_SEVERITY_MAJOR, /**< flex_trits conversion error */ - SC_CCLIENT_HASH = 0x05 | SC_MODULE_CCLIENT | SC_SEVERITY_MAJOR, + SC_CCLIENT_HASH = 0x06 | SC_MODULE_CCLIENT | SC_SEVERITY_MAJOR, /**< hash container operation error */ // Serializer module SC_SERIALIZER_JSON_CREATE = 0x01 | SC_MODULE_SERIALIZER | SC_SEVERITY_FATAL, /**< Fail to create JSON object in serializer */ - SC_SERIALIZER_JSON_PARSE = 0x02 | SC_MODULE_SERIALIZER | SC_SEVERITY_FATAL, - /**< Fail to parse JSON object in serializer */ - SC_SERIALIZER_NULL = 0x03 | SC_MODULE_SERIALIZER | SC_SEVERITY_FATAL, + SC_SERIALIZER_NULL = 0x02 | SC_MODULE_SERIALIZER | SC_SEVERITY_FATAL, /**< NULL object in serializer */ + SC_SERIALIZER_JSON_PARSE = 0x07 | SC_MODULE_SERIALIZER | SC_SEVERITY_FATAL, + /**< Fail to parse JSON object in serializer */ // Cache module - SC_CACHE_NULL = 0x01 | SC_MODULE_CACHE | SC_SEVERITY_FATAL, + SC_CACHE_NULL = 0x02 | SC_MODULE_CACHE | SC_SEVERITY_FATAL, /**< NULL parameters in cache */ - SC_CACHE_FAILED_RESPONSE = 0x02 | SC_MODULE_CACHE | SC_SEVERITY_FATAL, + SC_CACHE_FAILED_RESPONSE = 0x04 | SC_MODULE_CACHE | SC_SEVERITY_FATAL, /**< Fail in cache operations */ // MAM module @@ -92,7 +92,9 @@ typedef enum { /**< Fail to create mam object */ SC_MAM_NULL = 0x02 | SC_MODULE_MAM | SC_SEVERITY_FATAL, /**< NULL object in mam */ - SC_MAM_FAILED_RESPONSE = 0x03 | SC_MODULE_MAM | SC_SEVERITY_FATAL, + SC_MAM_NOT_FOUND = 0x03 | SC_MODULE_MAM | SC_SEVERITY_FATAL, + /**< Empty result from mam */ + SC_MAM_FAILED_RESPONSE = 0x04 | SC_MODULE_MAM | SC_SEVERITY_FATAL, /**< Error in mam response */ } status_t; From 08a55477cd9ef629888b763ab1dff78e0ca0111d Mon Sep 17 00:00:00 2001 From: jkrvivian Date: Fri, 15 Mar 2019 17:25:17 +0800 Subject: [PATCH 04/29] fix(core): Response error code when MAM failed --- accelerator/apis.c | 4 ++++ accelerator/server.cc | 17 ++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/accelerator/apis.c b/accelerator/apis.c index 1c254420..15ca4369 100644 --- a/accelerator/apis.c +++ b/accelerator/apis.c @@ -184,6 +184,10 @@ status_t api_receive_mam_message(const iota_client_service_t* const service, char* payload = calloc(payload_size * 2 + 1, sizeof(char)); trytes_to_ascii(payload_trytes, payload_size, payload); + if (payload == NULL) { + ret = SC_MAM_NOT_FOUND; + goto done; + } *json_result = payload; payload = NULL; diff --git a/accelerator/server.cc b/accelerator/server.cc index e9a39d7e..75203622 100644 --- a/accelerator/server.cc +++ b/accelerator/server.cc @@ -21,10 +21,10 @@ status_t set_response_content(status_t ret, char** json_result) { } cJSON* json_obj = cJSON_CreateObject(); - if (ret == SC_CCLIENT_NOT_FOUND) { + if ((ret & SC_ERROR_MASK) == 0x03) { http_ret = SC_NOT_FOUND; cJSON_AddStringToObject(json_obj, "message", "Request not found"); - } else if (ret == SC_SERIALIZER_JSON_PARSE) { + } else if ((ret & SC_ERROR_MASK) == 0x07) { http_ret = SC_BAD_REQUEST; cJSON_AddStringToObject(json_obj, "message", "Invalid request header"); } else { @@ -56,18 +56,17 @@ int main(int, char const**) { set_options_method_header(res); }) .get([&](served::response& res, const served::request& req) { + status_t ret = SC_OK; char* json_result = NULL; - api_receive_mam_message(&service, req.params["bundle"].c_str(), - &json_result); + ret = api_receive_mam_message(&service, req.params["bundle"].c_str(), + &json_result); + ret = set_response_content(ret, &json_result); res.set_header("Content-Type", "application/json"); res.set_header("Access-Control-Allow-Origin", "*"); - if (!json_result) { - res.set_status(SC_NOT_FOUND); - } else { - res << json_result; - } + res.set_status(ret); + res << json_result; }); /** From 6357bba37eb556f4245ce9627bca5bd4372152f2 Mon Sep 17 00:00:00 2001 From: jkrvivian Date: Fri, 15 Mar 2019 21:33:24 +0800 Subject: [PATCH 05/29] fix(core): Rename HTTP status code --- accelerator/errors.h | 9 +++++---- accelerator/server.cc | 10 +++++----- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/accelerator/errors.h b/accelerator/errors.h index 764f6b13..7c6b52ba 100644 --- a/accelerator/errors.h +++ b/accelerator/errors.h @@ -51,10 +51,11 @@ extern "C" { typedef enum { SC_OK = 0, /**< No error */ - SC_HTTP_OK = 200, /**< HTTP response OK */ - SC_BAD_REQUEST = 400, /**< HTTP response, error when parsing request */ - SC_NOT_FOUND = 404, /**< HTTP request not found */ - SC_INTERNAL_SERVICE_ERROR = 500, /**< HTTP response, other errors in TA */ + SC_HTTP_OK = 200, /**< HTTP response OK */ + SC_HTTP_BAD_REQUEST = 400, /**< HTTP response, error when parsing request */ + SC_HTTP_NOT_FOUND = 404, /**< HTTP request not found */ + SC_HTTP_INTERNAL_SERVICE_ERROR = 500, + /**< HTTP response, other errors in TA */ SC_TA_OOM = 0x01 | SC_MODULE_TA | SC_SEVERITY_FATAL, /**< Fail to create TA object */ diff --git a/accelerator/server.cc b/accelerator/server.cc index 75203622..cb653167 100644 --- a/accelerator/server.cc +++ b/accelerator/server.cc @@ -22,13 +22,13 @@ status_t set_response_content(status_t ret, char** json_result) { cJSON* json_obj = cJSON_CreateObject(); if ((ret & SC_ERROR_MASK) == 0x03) { - http_ret = SC_NOT_FOUND; + http_ret = SC_HTTP_NOT_FOUND; cJSON_AddStringToObject(json_obj, "message", "Request not found"); } else if ((ret & SC_ERROR_MASK) == 0x07) { - http_ret = SC_BAD_REQUEST; + http_ret = SC_HTTP_BAD_REQUEST; cJSON_AddStringToObject(json_obj, "message", "Invalid request header"); } else { - http_ret = SC_INTERNAL_SERVICE_ERROR; + http_ret = SC_HTTP_INTERNAL_SERVICE_ERROR; cJSON_AddStringToObject(json_obj, "message", "Internal service error"); } *json_result = cJSON_PrintUnformatted(json_obj); @@ -231,7 +231,7 @@ int main(int, char const**) { "Invalid request header"); json_result = cJSON_PrintUnformatted(json_obj); - res.set_status(SC_BAD_REQUEST); + res.set_status(SC_HTTP_BAD_REQUEST); cJSON_Delete(json_obj); } else { ret = api_send_transfer(&service, req.body().c_str(), &json_result); @@ -260,7 +260,7 @@ int main(int, char const**) { cJSON_AddStringToObject(json_obj, "message", "Invalid path"); const char* json = cJSON_PrintUnformatted(json_obj); - res.set_status(SC_BAD_REQUEST); + res.set_status(SC_HTTP_BAD_REQUEST); res.set_header("Content-Type", "application/json"); res.set_header("Access-Control-Allow-Origin", "*"); res << json; From 70928dd3b03ab4efbf516cd1d6a897f192ced9fb Mon Sep 17 00:00:00 2001 From: Yu Wei Wu Date: Mon, 18 Mar 2019 13:09:21 +0800 Subject: [PATCH 06/29] fix(core): Return error if transaction not found Return error code if find_transaction_object can't find any result since default response would be null trytes instead. --- accelerator/common_core.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/accelerator/common_core.c b/accelerator/common_core.c index 1e4364c4..e6b4b796 100644 --- a/accelerator/common_core.c +++ b/accelerator/common_core.c @@ -389,6 +389,9 @@ status_t ta_get_transaction_object(const iota_client_service_t* const service, (tryte_t*)cache_value, NUM_TRYTES_SERIALIZED_TRANSACTION, tx_trits, NUM_TRITS_SERIALIZED_TRANSACTION, NUM_TRITS_SERIALIZED_TRANSACTION); cache_set(cache, req, cache_value); + } else { + ret = SC_CCLIENT_NOT_FOUND; + goto done; } } From 02f52e943ccf42e477eee4f520175f4a1d0d6220 Mon Sep 17 00:00:00 2001 From: Yu Wei Wu Date: Mon, 18 Mar 2019 13:18:58 +0800 Subject: [PATCH 07/29] feat(test): Add code coverage configs Add bazel coverage configurations. Report generation is include in buildkite CI and would be uploaded to artifact. --- .bazelrc | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.bazelrc b/.bazelrc index dc09b0aa..fb99d6e2 100644 --- a/.bazelrc +++ b/.bazelrc @@ -1,4 +1,9 @@ test --copt='-ggdb3' +coverage -s +coverage --experimental_cc_coverage +coverage --combined_report=lcov +coverage --coverage_report_generator=@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main +coverage --instrumentation_filter="-/tests[/:]" # Address Sanitizer:--config asan build:asan --strip=never From 7f44d2b44c8e7c940a7c2d484ed803d499f18d33 Mon Sep 17 00:00:00 2001 From: Jim Huang Date: Mon, 18 Mar 2019 22:37:42 +0800 Subject: [PATCH 08/29] Fix company info --- LICENSE | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index f842c60f..2f88229f 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ The MIT License (MIT) -Copyright (C) 2018-2019 BiiLabs, Co. Ltd. and Contributors +Copyright (C) 2018-2019 BiiLabs Co., Ltd. and Contributors All Rights Reserved. Permission is hereby granted, free of charge, to any person obtaining a copy From 5672d82b78e9f9f85e1c95ad6e5e00d9e6817dd1 Mon Sep 17 00:00:00 2001 From: Yu Wei Wu Date: Mon, 18 Mar 2019 20:00:04 +0800 Subject: [PATCH 09/29] feat(server): Add Server name in header Add "tangle-accelerator" as server name and version to the header. Versioning is also added to config header file. Future releases require update this macro. --- accelerator/config.h | 1 + accelerator/errors.h | 6 ++++ accelerator/server.cc | 64 +++++++++++++++++++++---------------------- 3 files changed, 39 insertions(+), 32 deletions(-) diff --git a/accelerator/config.h b/accelerator/config.h index df7475c2..f60cf384 100644 --- a/accelerator/config.h +++ b/accelerator/config.h @@ -12,6 +12,7 @@ extern "C" { /** @name tangle-accelerator config */ /** @{ */ +#define TA_VERSION "tangle-accelerator/0.3.0" #define TA_HOST "localhost" /**< Binding address of tangle-acclerator */ #define TA_PORT "8000" /**< Binding port of tangle-acclerator */ #define TA_THREAD_COUNT 10 /**< Thread count of tangle-acclerator instance */ diff --git a/accelerator/errors.h b/accelerator/errors.h index 7c6b52ba..afa86e22 100644 --- a/accelerator/errors.h +++ b/accelerator/errors.h @@ -99,6 +99,12 @@ typedef enum { /**< Error in mam response */ } status_t; +typedef enum { + HTTP_METHOD_GET = 0, /**< HTTP GET method */ + HTTP_METHOD_POST = 1, /**< HTTP POST method */ + HTTP_METHOD_OPTIONS = 2, /**< HTTP OPTIONS method */ +} http_method_t; + #ifdef __cplusplus } #endif diff --git a/accelerator/server.cc b/accelerator/server.cc index cb653167..7582b557 100644 --- a/accelerator/server.cc +++ b/accelerator/server.cc @@ -6,12 +6,21 @@ #include "accelerator/errors.h" #include "cJSON.h" -void set_options_method_header(served::response& res) { +void set_method_header(served::response& res, http_method_t method) { + res.set_header("Server", TA_VERSION); res.set_header("Access-Control-Allow-Origin", "*"); - res.set_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); - res.set_header("Access-Control-Allow-Headers", - "Origin, Content-Type, Accept"); - res.set_header("Access-Control-Max-Age", "86400"); + + switch (method) { + case HTTP_METHOD_OPTIONS: + res.set_header("Access-Control-Allow-Methods", "GET, POST, OPTIONS"); + res.set_header("Access-Control-Allow-Headers", + "Origin, Content-Type, Accept"); + res.set_header("Access-Control-Max-Age", "86400"); + break; + default: + res.set_header("Content-Type", "application/json"); + break; + } } status_t set_response_content(status_t ret, char** json_result) { @@ -53,7 +62,7 @@ int main(int, char const**) { mux.handle("/mam/{bundle:[A-Z9]{81}}") .method(served::method::OPTIONS, [&](served::response& res, const served::request& req) { - set_options_method_header(res); + set_method_header(res, HTTP_METHOD_OPTIONS); }) .get([&](served::response& res, const served::request& req) { status_t ret = SC_OK; @@ -63,8 +72,7 @@ int main(int, char const**) { &json_result); ret = set_response_content(ret, &json_result); - res.set_header("Content-Type", "application/json"); - res.set_header("Access-Control-Allow-Origin", "*"); + set_method_header(res, HTTP_METHOD_GET); res.set_status(ret); res << json_result; }); @@ -79,7 +87,7 @@ int main(int, char const**) { mux.handle("/tag/{tag:[A-Z9]{1,27}}/hashes") .method(served::method::OPTIONS, [&](served::response& res, const served::request& req) { - set_options_method_header(res); + set_method_header(res, HTTP_METHOD_OPTIONS); }) .get([&](served::response& res, const served::request& req) { status_t ret = SC_OK; @@ -88,8 +96,7 @@ int main(int, char const**) { ret = api_find_transactions_by_tag(&service, req.params["tag"].c_str(), &json_result); ret = set_response_content(ret, &json_result); - res.set_header("Content-Type", "application/json"); - res.set_header("Access-Control-Allow-Origin", "*"); + set_method_header(res, HTTP_METHOD_GET); res.set_status(ret); res << json_result; }); @@ -104,7 +111,7 @@ int main(int, char const**) { mux.handle("/transaction/{tx:[A-Z9]{81}}") .method(served::method::OPTIONS, [&](served::response& res, const served::request& req) { - set_options_method_header(res); + set_method_header(res, HTTP_METHOD_OPTIONS); }) .get([&](served::response& res, const served::request& req) { status_t ret = SC_OK; @@ -113,8 +120,7 @@ int main(int, char const**) { ret = api_get_transaction_object(&service, req.params["tx"].c_str(), &json_result); ret = set_response_content(ret, &json_result); - res.set_header("Content-Type", "application/json"); - res.set_header("Access-Control-Allow-Origin", "*"); + set_method_header(res, HTTP_METHOD_GET); res.set_status(ret); res << json_result; }); @@ -129,7 +135,7 @@ int main(int, char const**) { mux.handle("/tag/{tag:[A-Z9]{1,27}}") .method(served::method::OPTIONS, [&](served::response& res, const served::request& req) { - set_options_method_header(res); + set_method_header(res, HTTP_METHOD_OPTIONS); }) .get([&](served::response& res, const served::request& req) { status_t ret = SC_OK; @@ -138,8 +144,7 @@ int main(int, char const**) { ret = api_find_transactions_obj_by_tag( &service, req.params["tag"].c_str(), &json_result); ret = set_response_content(ret, &json_result); - res.set_header("Content-Type", "application/json"); - res.set_header("Access-Control-Allow-Origin", "*"); + set_method_header(res, HTTP_METHOD_GET); res.set_status(ret); res << json_result; }); @@ -152,7 +157,7 @@ int main(int, char const**) { mux.handle("/tips/pair") .method(served::method::OPTIONS, [&](served::response& res, const served::request& req) { - set_options_method_header(res); + set_method_header(res, HTTP_METHOD_OPTIONS); }) .get([&](served::response& res, const served::request& req) { status_t ret = SC_OK; @@ -160,8 +165,7 @@ int main(int, char const**) { ret = api_get_tips_pair(&service, &json_result); ret = set_response_content(ret, &json_result); - res.set_header("Content-Type", "application/json"); - res.set_header("Access-Control-Allow-Origin", "*"); + set_method_header(res, HTTP_METHOD_GET); res.set_status(ret); res << json_result; }); @@ -174,7 +178,7 @@ int main(int, char const**) { mux.handle("/tips") .method(served::method::OPTIONS, [&](served::response& res, const served::request& req) { - set_options_method_header(res); + set_method_header(res, HTTP_METHOD_OPTIONS); }) .get([&](served::response& res, const served::request& req) { status_t ret = SC_OK; @@ -182,8 +186,7 @@ int main(int, char const**) { ret = api_get_tips(&service, &json_result); ret = set_response_content(ret, &json_result); - res.set_header("Content-Type", "application/json"); - res.set_header("Access-Control-Allow-Origin", "*"); + set_method_header(res, HTTP_METHOD_GET); res.set_status(ret); res << json_result; }); @@ -196,7 +199,7 @@ int main(int, char const**) { mux.handle("/address") .method(served::method::OPTIONS, [&](served::response& res, const served::request& req) { - set_options_method_header(res); + set_method_header(res, HTTP_METHOD_OPTIONS); }) .get([&](served::response& res, const served::request& req) { status_t ret = SC_OK; @@ -204,8 +207,7 @@ int main(int, char const**) { ret = api_generate_address(&service, &json_result); ret = set_response_content(ret, &json_result); - res.set_header("Content-Type", "application/json"); - res.set_header("Access-Control-Allow-Origin", "*"); + set_method_header(res, HTTP_METHOD_GET); res.set_status(ret); res << json_result; }); @@ -218,7 +220,7 @@ int main(int, char const**) { mux.handle("/transaction") .method(served::method::OPTIONS, [&](served::response& res, const served::request& req) { - set_options_method_header(res); + set_method_header(res, HTTP_METHOD_OPTIONS); }) .post([&](served::response& res, const served::request& req) { status_t ret = SC_OK; @@ -239,8 +241,7 @@ int main(int, char const**) { res.set_status(ret); } - res.set_header("Content-Type", "application/json"); - res.set_header("Access-Control-Allow-Origin", "*"); + set_method_header(res, HTTP_METHOD_POST); res << json_result; }); @@ -253,7 +254,7 @@ int main(int, char const**) { mux.handle("{*}") .method(served::method::OPTIONS, [&](served::response& res, const served::request& req) { - set_options_method_header(res); + set_method_header(res, HTTP_METHOD_OPTIONS); }) .get([](served::response& res, const served::request&) { cJSON* json_obj = cJSON_CreateObject(); @@ -261,8 +262,7 @@ int main(int, char const**) { const char* json = cJSON_PrintUnformatted(json_obj); res.set_status(SC_HTTP_BAD_REQUEST); - res.set_header("Content-Type", "application/json"); - res.set_header("Access-Control-Allow-Origin", "*"); + set_method_header(res, HTTP_METHOD_GET); res << json; cJSON_Delete(json_obj); From 67b05476910af3d8740909ef46b2b4b2eb511e8a Mon Sep 17 00:00:00 2001 From: Yu Wei Wu Date: Wed, 20 Mar 2019 12:36:35 +0800 Subject: [PATCH 10/29] feat(docker): Add docker image rule Add docker image support for easier installation. User can now use bazel rules to build a docker image. The image is also uploaded to docker hub for faster deployment with default settings. --- README.md | 14 ++++++++++++++ WORKSPACE | 9 +++++++++ accelerator/BUILD | 7 +++++++ 3 files changed, 30 insertions(+) diff --git a/README.md b/README.md index 4627ac1d..bff81559 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,20 @@ Before running tangle-accelerator, please edit binding address/port of accelerat $ make && bazel run //accelerator ``` +### Build from docker + +If you prefer building a docker image, tangle-accelerator also provides build rules for it. Note that you still have to edit configurations in `accelerator/config.h`. + +``` +$ make && bazel run //accelerator:ta_image +``` + +There's also an easier option to pull image from docker hub then simply run with default configs. Please do remember a redis-server is still required in this way. + +``` +$ docker run -d --net=host --name tangle-accelerator wusyong/tangel-accelerator:latest +``` + ## Developing The codebase of this repository follows [Google's C++ guidelines](https://google.github.io/styleguide/cppguide.html): diff --git a/WORKSPACE b/WORKSPACE index e4e9d051..b8dfb207 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -18,6 +18,15 @@ git_repository( remote = "https://github.com/meltwater/served.git", ) +git_repository( + name = "io_bazel_rules_docker", + remote = "https://github.com/bazelbuild/rules_docker.git", + tag = "v0.6.0", +) + load("@rules_iota//:defs.bzl", "iota_deps") +load("@io_bazel_rules_docker//cc:image.bzl", _cc_image_repos = "repositories") iota_deps() + +_cc_image_repos() diff --git a/accelerator/BUILD b/accelerator/BUILD index 497cd5ce..da1e32e5 100644 --- a/accelerator/BUILD +++ b/accelerator/BUILD @@ -1,5 +1,7 @@ package(default_visibility = ["//visibility:public"]) +load("@io_bazel_rules_docker//cc:image.bzl", "cc_image") + cc_binary( name = "accelerator", srcs = ["server.cc"], @@ -11,6 +13,11 @@ cc_binary( ], ) +cc_image( + name = "ta_image", + binary = ":accelerator", +) + cc_library( name = "apis", srcs = ["apis.c"], From d04fdb6b14c69b922524aa137fd4c3bfa405a263 Mon Sep 17 00:00:00 2001 From: Yu Wei Wu Date: Thu, 21 Mar 2019 10:21:17 +0800 Subject: [PATCH 11/29] feat(config): Refactor configuration variables Refactor configuration variables for future cli and file config feature. This also enable logger helper to record initializations and destroys. --- accelerator/BUILD | 8 +++-- accelerator/common_core.h | 4 --- accelerator/config.c | 52 +++++++++++++++++++++++++++ accelerator/config.h | 74 ++++++++++++++++++++++++++++++--------- accelerator/server.cc | 58 ++++++++++++++++-------------- 5 files changed, 148 insertions(+), 48 deletions(-) create mode 100644 accelerator/config.c diff --git a/accelerator/BUILD b/accelerator/BUILD index 497cd5ce..8da6ed8d 100644 --- a/accelerator/BUILD +++ b/accelerator/BUILD @@ -39,8 +39,6 @@ cc_library( "//utils:cache", "//utils:pow", "@com_github_uthash//:uthash", - "@entangled//cclient/api", - "@entangled//cclient/types", "@entangled//common/model:bundle", "@entangled//utils:time", ], @@ -48,8 +46,14 @@ cc_library( cc_library( name = "ta_config", + srcs = ["config.c"], hdrs = ["config.h"], visibility = ["//visibility:public"], + deps = [ + ":ta_errors", + "@entangled//cclient/api", + "@entangled//cclient/types", + ], ) cc_library( diff --git a/accelerator/common_core.h b/accelerator/common_core.h index 12530a60..e8f8e99c 100644 --- a/accelerator/common_core.h +++ b/accelerator/common_core.h @@ -2,10 +2,6 @@ #define ACCELERATOR_COMMON_CORE_H_ #include "accelerator/config.h" -#include "accelerator/errors.h" -#include "cclient/api/core/core_api.h" -#include "cclient/api/extended/extended_api.h" -#include "cclient/types/types.h" #include "common/model/bundle.h" #include "common/model/transfer.h" #include "request/request.h" diff --git a/accelerator/config.c b/accelerator/config.c new file mode 100644 index 00000000..b57add4e --- /dev/null +++ b/accelerator/config.c @@ -0,0 +1,52 @@ +#include "config.h" +#include "utils/logger_helper.h" + +#define CONFIG_LOGGER_ID "config" + +static logger_id_t logger_id; + +status_t ta_config_init(ta_config_t* const info, iota_config_t* const config, + iota_client_service_t* const service) { + status_t ret = SC_OK; + if (info == NULL || config == NULL || service == NULL) { + return SC_TA_NULL; + } + + logger_id = logger_helper_enable(CONFIG_LOGGER_ID, LOGGER_DEBUG, true); + log_info(logger_id, "[%s:%d] enable logger %s.\n", __func__, __LINE__, + CONFIG_LOGGER_ID); + + log_info(logger_id, "Initializing TA information\n"); + info->version = TA_VERSION; + info->host = TA_HOST; + info->port = TA_PORT; + info->thread_count = TA_THREAD_COUNT; + + log_info(logger_id, "Initializing IRI configuration\n"); + config->depth = DEPTH; + config->mwm = MWM; + config->seed = SEED; + + log_info(logger_id, "Initializing IRI connection\n"); + service->http.path = "/"; + service->http.content_type = "application/json"; + service->http.accept = "application/json"; + service->http.host = IRI_HOST; + service->http.port = IRI_PORT; + service->http.api_version = 1; + service->serializer_type = SR_JSON; + if (iota_client_core_init(service)) { + log_critical(logger_id, "Initializing IRI connection failed!\n"); + ret = SC_TA_OOM; + } + iota_client_extended_init(); + + return ret; +} + +void ta_config_destroy(iota_client_service_t* const service) { + log_info(logger_id, "Destroying IRI connection\n"); + iota_client_extended_destroy(); + iota_client_core_destroy(service); + logger_helper_release(logger_id); +} diff --git a/accelerator/config.h b/accelerator/config.h index f60cf384..34676a02 100644 --- a/accelerator/config.h +++ b/accelerator/config.h @@ -1,34 +1,31 @@ #ifndef ACCELERATOR_CONFIG_H_ #define ACCELERATOR_CONFIG_H_ +#include "accelerator/errors.h" +#include "cclient/api/core/core_api.h" +#include "cclient/api/extended/extended_api.h" +#include "cclient/types/types.h" + #ifdef __cplusplus extern "C" { #endif /** * @file config.h - * @brief Configuration of tangle-acclerator + * @brief Configuration of tangle-accelerator */ -/** @name tangle-accelerator config */ -/** @{ */ #define TA_VERSION "tangle-accelerator/0.3.0" -#define TA_HOST "localhost" /**< Binding address of tangle-acclerator */ -#define TA_PORT "8000" /**< Binding port of tangle-acclerator */ -#define TA_THREAD_COUNT 10 /**< Thread count of tangle-acclerator instance */ -/** @} */ - -/** @name IRI connection config */ -/** @{ */ -#define IRI_HOST "localhost" /**< Address of IRI */ -#define IRI_PORT 14265 /**< Port of IRI */ -#define DEPTH 3 /**< Depth of API argument */ -#define MWM 14 /**< Maximum weight magnitude of API argument */ -/** Seed to generate address. This does not do any signature yet. */ +#define TA_HOST "localhost" +#define TA_PORT "8000" +#define TA_THREAD_COUNT 10 +#define IRI_HOST "localhost" +#define IRI_PORT 14265 +#define DEPTH 3 +#define MWM 14 #define SEED \ "AMRWQP9BUMJALJHBXUCHOD9HFFD9LGTGEAWMJWWXSDVOF9PI9YGJAPBQLQUOMNYEQCZPGCTHGV" \ "NNAPGHA" -/** @} */ /** @name Redis connection config */ /** @{ */ @@ -36,6 +33,51 @@ extern "C" { #define REDIS_PORT 6379 /**< poer of Redis server */ /** @} */ +/** struct type of accelerator configuration */ +typedef struct ta_info_s { + char* version; /**< Binding version of tangle-accelerator */ + char* host; /**< Binding address of tangle-accelerator */ + char* port; /**< Binding port of tangle-accelerator */ + uint8_t thread_count; /**< Thread count of tangle-accelerator instance */ +} ta_config_t; + +/** struct type of iota configuration */ +typedef struct ta_config_s { + uint8_t depth; /**< Depth of API argument */ + uint8_t mwm; /**< Minimum weight magnitude of API argument */ + /** Seed to generate address. This does not do any signature yet. */ + const char* seed; +} iota_config_t; + +/** struct type of accelerator core */ +typedef struct ta_core_s { + ta_config_t info; /**< accelerator configiuration structure */ + iota_config_t config; /**< iota configuration structure */ + iota_client_service_t service; /**< iota connection structure */ +} ta_core_t; + +/** + * Initializes configurations with default values + * Should be called first + * + * @param info[in] Tangle-accelerator configuration variables + * @param config[in] iota configuration variables + * @param service[in] IRI connection configuration variables + * + * @return + * - SC_OK on success + * - non-zero on error + */ +status_t ta_config_init(ta_config_t* const info, iota_config_t* const config, + iota_client_service_t* const service); + +/** + * Free memory of configuration variables + * + * @param service[in] IRI connection configuration variables + */ +void ta_config_destroy(iota_client_service_t* const service); + #ifdef __cplusplus } #endif diff --git a/accelerator/server.cc b/accelerator/server.cc index 7582b557..65f46c37 100644 --- a/accelerator/server.cc +++ b/accelerator/server.cc @@ -5,9 +5,15 @@ #include "accelerator/config.h" #include "accelerator/errors.h" #include "cJSON.h" +#include "utils/logger_helper.h" + +#define MAIN_LOGGER_ID "main" + +static ta_core_t ta_core; +static logger_id_t logger_id; void set_method_header(served::response& res, http_method_t method) { - res.set_header("Server", TA_VERSION); + res.set_header("Server", ta_core.info.version); res.set_header("Access-Control-Allow-Origin", "*"); switch (method) { @@ -48,16 +54,12 @@ int main(int, char const**) { served::multiplexer mux; mux.use_after(served::plugin::access_log); - iota_client_service_t service; - service.http.path = "/"; - service.http.content_type = "application/json"; - service.http.accept = "application/json"; - service.http.host = IRI_HOST; - service.http.port = IRI_PORT; - service.http.api_version = 1; - service.serializer_type = SR_JSON; - iota_client_core_init(&service); - iota_client_extended_init(); + if (logger_helper_init() != RC_OK) { + return EXIT_FAILURE; + } + logger_id = logger_helper_enable(MAIN_LOGGER_ID, LOGGER_DEBUG, true); + + ta_config_init(&ta_core.info, &ta_core.config, &ta_core.service); mux.handle("/mam/{bundle:[A-Z9]{81}}") .method(served::method::OPTIONS, @@ -68,8 +70,8 @@ int main(int, char const**) { status_t ret = SC_OK; char* json_result = NULL; - ret = api_receive_mam_message(&service, req.params["bundle"].c_str(), - &json_result); + ret = api_receive_mam_message( + &ta_core.service, req.params["bundle"].c_str(), &json_result); ret = set_response_content(ret, &json_result); set_method_header(res, HTTP_METHOD_GET); @@ -93,8 +95,8 @@ int main(int, char const**) { status_t ret = SC_OK; char* json_result; - ret = api_find_transactions_by_tag(&service, req.params["tag"].c_str(), - &json_result); + ret = api_find_transactions_by_tag( + &ta_core.service, req.params["tag"].c_str(), &json_result); ret = set_response_content(ret, &json_result); set_method_header(res, HTTP_METHOD_GET); res.set_status(ret); @@ -117,8 +119,8 @@ int main(int, char const**) { status_t ret = SC_OK; char* json_result; - ret = api_get_transaction_object(&service, req.params["tx"].c_str(), - &json_result); + ret = api_get_transaction_object( + &ta_core.service, req.params["tx"].c_str(), &json_result); ret = set_response_content(ret, &json_result); set_method_header(res, HTTP_METHOD_GET); res.set_status(ret); @@ -142,7 +144,7 @@ int main(int, char const**) { char* json_result; ret = api_find_transactions_obj_by_tag( - &service, req.params["tag"].c_str(), &json_result); + &ta_core.service, req.params["tag"].c_str(), &json_result); ret = set_response_content(ret, &json_result); set_method_header(res, HTTP_METHOD_GET); res.set_status(ret); @@ -163,7 +165,7 @@ int main(int, char const**) { status_t ret = SC_OK; char* json_result; - ret = api_get_tips_pair(&service, &json_result); + ret = api_get_tips_pair(&ta_core.service, &json_result); ret = set_response_content(ret, &json_result); set_method_header(res, HTTP_METHOD_GET); res.set_status(ret); @@ -184,7 +186,7 @@ int main(int, char const**) { status_t ret = SC_OK; char* json_result; - ret = api_get_tips(&service, &json_result); + ret = api_get_tips(&ta_core.service, &json_result); ret = set_response_content(ret, &json_result); set_method_header(res, HTTP_METHOD_GET); res.set_status(ret); @@ -205,7 +207,7 @@ int main(int, char const**) { status_t ret = SC_OK; char* json_result; - ret = api_generate_address(&service, &json_result); + ret = api_generate_address(&ta_core.service, &json_result); ret = set_response_content(ret, &json_result); set_method_header(res, HTTP_METHOD_GET); res.set_status(ret); @@ -236,7 +238,8 @@ int main(int, char const**) { res.set_status(SC_HTTP_BAD_REQUEST); cJSON_Delete(json_obj); } else { - ret = api_send_transfer(&service, req.body().c_str(), &json_result); + ret = api_send_transfer(&ta_core.service, req.body().c_str(), + &json_result); ret = set_response_content(ret, &json_result); res.set_status(ret); } @@ -269,10 +272,13 @@ int main(int, char const**) { }); std::cout << "Starting..." << std::endl; - served::net::server server(TA_HOST, TA_PORT, mux); - server.run(TA_THREAD_COUNT); + served::net::server server(ta_core.info.host, ta_core.info.port, mux); + server.run(ta_core.info.thread_count); - iota_client_extended_destroy(); - iota_client_core_destroy(&service); + ta_config_destroy(&ta_core.service); + logger_helper_release(logger_id); + if (logger_helper_destroy() != RC_OK) { + return EXIT_FAILURE; + } return 0; } From bc2ee0a894367cba76dd59d5eed62a3360b0647a Mon Sep 17 00:00:00 2001 From: Yu Wei Wu Date: Thu, 21 Mar 2019 10:21:39 +0800 Subject: [PATCH 12/29] fix(config): Fix all functions related to configs --- accelerator/apis.c | 15 +++++++++------ accelerator/apis.h | 11 ++++++++--- accelerator/common_core.c | 21 +++++++++++++-------- accelerator/common_core.h | 12 ++++++++++-- accelerator/server.cc | 10 ++++++---- 5 files changed, 46 insertions(+), 23 deletions(-) diff --git a/accelerator/apis.c b/accelerator/apis.c index 15ca4369..2823a8e1 100644 --- a/accelerator/apis.c +++ b/accelerator/apis.c @@ -44,7 +44,8 @@ status_t api_get_tips(const iota_client_service_t* const service, return ret; } -status_t api_get_tips_pair(const iota_client_service_t* const service, +status_t api_get_tips_pair(const iota_config_t* const config, + const iota_client_service_t* const service, char** json_result) { status_t ret = SC_OK; ta_get_tips_res_t* res = ta_get_tips_res_new(); @@ -53,7 +54,7 @@ status_t api_get_tips_pair(const iota_client_service_t* const service, goto done; } - ret = cclient_get_txn_to_approve(service, res); + ret = cclient_get_txn_to_approve(service, config->depth, res); if (ret) { goto done; } @@ -65,7 +66,8 @@ status_t api_get_tips_pair(const iota_client_service_t* const service, return ret; } -status_t api_generate_address(const iota_client_service_t* const service, +status_t api_generate_address(const iota_config_t* const config, + const iota_client_service_t* const service, char** json_result) { status_t ret = SC_OK; ta_generate_address_res_t* res = ta_generate_address_res_new(); @@ -74,7 +76,7 @@ status_t api_generate_address(const iota_client_service_t* const service, goto done; } - ret = ta_generate_address(service, res); + ret = ta_generate_address(config, service, res); if (ret) { goto done; } @@ -203,7 +205,8 @@ status_t api_receive_mam_message(const iota_client_service_t* const service, return ret; } -status_t api_send_transfer(const iota_client_service_t* const service, +status_t api_send_transfer(const iota_config_t* const config, + const iota_client_service_t* const service, const char* const obj, char** json_result) { status_t ret = SC_OK; char hash_trytes[NUM_TRYTES_HASH + 1]; @@ -222,7 +225,7 @@ status_t api_send_transfer(const iota_client_service_t* const service, goto done; } - ret = ta_send_transfer(service, req, res); + ret = ta_send_transfer(config, service, req, res); if (ret) { goto done; } diff --git a/accelerator/apis.h b/accelerator/apis.h index c7bdf22f..c8d32adc 100644 --- a/accelerator/apis.h +++ b/accelerator/apis.h @@ -28,6 +28,7 @@ extern "C" { * Generate and return an unused address from the seed. An unused address means * the address does not have any transaction with it yet. * + * @param[in] config IOTA API parameter configurations * @param[in] service IRI node end point service * @param[out] json_result Result containing an unused address in json format * @@ -35,7 +36,8 @@ extern "C" { * - SC_OK on success * - non-zero on error */ -status_t api_generate_address(const iota_client_service_t* const service, +status_t api_generate_address(const iota_config_t* const config, + const iota_client_service_t* const service, char** json_result); /** @@ -44,6 +46,7 @@ status_t api_generate_address(const iota_client_service_t* const service, * Get a tips pair as trunk/branch transactions for transaction construction. * The result is char array in json format: * + * @param[in] config IOTA API parameter configurations * @param[in] service IRI node end point service * @param[out] json_result Result containing a tips pair in json format * @@ -51,7 +54,8 @@ status_t api_generate_address(const iota_client_service_t* const service, * - SC_OK on success * - non-zero on error */ -status_t api_get_tips_pair(const iota_client_service_t* const service, +status_t api_get_tips_pair(const iota_config_t* const config, + const iota_client_service_t* const service, char** json_result); /** @@ -101,7 +105,8 @@ status_t api_receive_mam_message(const iota_client_service_t* const service, * - SC_OK on success * - non-zero on error */ -status_t api_send_transfer(const iota_client_service_t* const service, +status_t api_send_transfer(const iota_config_t* const config, + const iota_client_service_t* const service, const char* const obj, char** json_result); /** diff --git a/accelerator/common_core.c b/accelerator/common_core.c index e6b4b796..97df5861 100644 --- a/accelerator/common_core.c +++ b/accelerator/common_core.c @@ -4,6 +4,7 @@ #include "utils/pow.h" status_t cclient_get_txn_to_approve(const iota_client_service_t* const service, + uint8_t const depth, ta_get_tips_res_t* res) { if (res == NULL) { return SC_TA_NULL; @@ -19,7 +20,7 @@ status_t cclient_get_txn_to_approve(const iota_client_service_t* const service, goto done; } // The depth at which Random Walk starts. Mininal is 3, and max is 15. - get_transactions_to_approve_req_set_depth(get_txn_req, DEPTH); + get_transactions_to_approve_req_set_depth(get_txn_req, depth); ret = iota_client_get_transactions_to_approve(service, get_txn_req, get_txn_res); @@ -162,6 +163,7 @@ status_t ta_attach_to_tangle(const attach_to_tangle_req_t* const req, } status_t ta_send_trytes(const iota_client_service_t* const service, + uint8_t const depth, uint8_t const mwm, hash8019_array_p trytes) { status_t ret = SC_OK; ta_get_tips_res_t* get_txn_res = ta_get_tips_res_new(); @@ -173,7 +175,7 @@ status_t ta_send_trytes(const iota_client_service_t* const service, } // get transaction to approve - ret = cclient_get_txn_to_approve(service, get_txn_res); + ret = cclient_get_txn_to_approve(service, depth, get_txn_res); if (ret) { goto done; } @@ -186,7 +188,7 @@ status_t ta_send_trytes(const iota_client_service_t* const service, memcpy(attach_req->branch, hash243_stack_peek(get_txn_res->tips), FLEX_TRIT_SIZE_243); hash243_stack_pop(&get_txn_res->tips); - attach_req->mwm = MWM; + attach_req->mwm = mwm; attach_req->trytes = trytes; ret = ta_attach_to_tangle(attach_req, attach_res); if (ret) { @@ -208,7 +210,8 @@ status_t ta_send_trytes(const iota_client_service_t* const service, return ret; } -status_t ta_generate_address(const iota_client_service_t* const service, +status_t ta_generate_address(const iota_config_t* const config, + const iota_client_service_t* const service, ta_generate_address_res_t* res) { if (res == NULL) { return SC_TA_NULL; @@ -217,8 +220,9 @@ status_t ta_generate_address(const iota_client_service_t* const service, status_t ret = SC_OK; hash243_queue_t out_address = NULL; flex_trit_t seed_trits[FLEX_TRIT_SIZE_243]; - flex_trits_from_trytes(seed_trits, NUM_TRITS_HASH, (const tryte_t*)SEED, - NUM_TRYTES_HASH, NUM_TRYTES_HASH); + flex_trits_from_trytes(seed_trits, NUM_TRITS_HASH, + (const tryte_t*)config->seed, NUM_TRYTES_HASH, + NUM_TRYTES_HASH); address_opt_t opt = {.security = 3, .start = 0, .total = 0}; ret = iota_client_get_new_address(service, seed_trits, opt, &out_address); @@ -231,7 +235,8 @@ status_t ta_generate_address(const iota_client_service_t* const service, return ret; } -status_t ta_send_transfer(const iota_client_service_t* const service, +status_t ta_send_transfer(const iota_config_t* const config, + const iota_client_service_t* const service, const ta_send_transfer_req_t* const req, ta_send_transfer_res_t* res) { if (req == NULL || res == NULL) { @@ -270,7 +275,7 @@ status_t ta_send_transfer(const iota_client_service_t* const service, free(serialized_txn); } - ret = ta_send_trytes(service, raw_tx); + ret = ta_send_trytes(service, config->depth, config->mwm, raw_tx); if (ret) { goto done; } diff --git a/accelerator/common_core.h b/accelerator/common_core.h index e8f8e99c..4fb8c333 100644 --- a/accelerator/common_core.h +++ b/accelerator/common_core.h @@ -31,6 +31,7 @@ extern "C" { * The result is a 243 long flex trits hash stack. * * @param[in] service IRI node end point service + * @param[in] depth Depth of get transaction to approve * @param[out] res Result containing a tips pair in ta_get_tips_res_t * * @return @@ -38,6 +39,7 @@ extern "C" { * - non-zero on error */ status_t cclient_get_txn_to_approve(const iota_client_service_t* const service, + uint8_t const depth, ta_get_tips_res_t* res); /** @@ -70,7 +72,8 @@ status_t cclient_get_tips(const iota_client_service_t* const service, * - SC_OK on success * - non-zero on error */ -status_t ta_generate_address(const iota_client_service_t* const service, +status_t ta_generate_address(const iota_config_t* const config, + const iota_client_service_t* const service, ta_generate_address_res_t* res); /** @@ -80,6 +83,7 @@ status_t ta_generate_address(const iota_client_service_t* const service, * fields include address, value, tag, and message. This API would also try to * find the transactions after bundle sent. * + * @param[in] config IOTA API parameter configurations * @param[in] service IRI node end point service * @param[in] req Request containing address value, message, tag in * ta_send_transfer_req_t @@ -89,7 +93,8 @@ status_t ta_generate_address(const iota_client_service_t* const service, * - SC_OK on success * - non-zero on error */ -status_t ta_send_transfer(const iota_client_service_t* const service, +status_t ta_send_transfer(const iota_config_t* const config, + const iota_client_service_t* const service, const ta_send_transfer_req_t* const req, ta_send_transfer_res_t* res); @@ -101,6 +106,8 @@ status_t ta_send_transfer(const iota_client_service_t* const service, * transaction to tangle. * * @param[in] service IRI node end point service + * @param[in] dpeth Depth of get transaction to approve + * @param[in] mwm Minimum weight magnitude * @param[in] trytes Trytes that will be attached to tangle * * @return @@ -108,6 +115,7 @@ status_t ta_send_transfer(const iota_client_service_t* const service, * - non-zero on error */ status_t ta_send_trytes(const iota_client_service_t* const service, + uint8_t const depth, uint8_t const mwm, hash8019_array_p trytes); /** diff --git a/accelerator/server.cc b/accelerator/server.cc index 65f46c37..37bb6769 100644 --- a/accelerator/server.cc +++ b/accelerator/server.cc @@ -165,7 +165,8 @@ int main(int, char const**) { status_t ret = SC_OK; char* json_result; - ret = api_get_tips_pair(&ta_core.service, &json_result); + ret = + api_get_tips_pair(&ta_core.config, &ta_core.service, &json_result); ret = set_response_content(ret, &json_result); set_method_header(res, HTTP_METHOD_GET); res.set_status(ret); @@ -207,7 +208,8 @@ int main(int, char const**) { status_t ret = SC_OK; char* json_result; - ret = api_generate_address(&ta_core.service, &json_result); + ret = api_generate_address(&ta_core.config, &ta_core.service, + &json_result); ret = set_response_content(ret, &json_result); set_method_header(res, HTTP_METHOD_GET); res.set_status(ret); @@ -238,8 +240,8 @@ int main(int, char const**) { res.set_status(SC_HTTP_BAD_REQUEST); cJSON_Delete(json_obj); } else { - ret = api_send_transfer(&ta_core.service, req.body().c_str(), - &json_result); + ret = api_send_transfer(&ta_core.config, &ta_core.service, + req.body().c_str(), &json_result); ret = set_response_content(ret, &json_result); res.set_status(ret); } From 1a91bf7fd90275d8976a5937e34d38c40b0454d0 Mon Sep 17 00:00:00 2001 From: Yu Wei Wu Date: Thu, 21 Mar 2019 10:53:45 +0800 Subject: [PATCH 13/29] fix(config): Fix all test cases related to configs --- tests/driver.c | 35 +++++++++++++++++------------------ tests/iota_api_mock.cc | 4 ++-- tests/iota_api_mock.hh | 5 +++-- tests/test_common.cc | 10 ++++++---- 4 files changed, 28 insertions(+), 26 deletions(-) diff --git a/tests/driver.c b/tests/driver.c index 9580701d..d46374a8 100644 --- a/tests/driver.c +++ b/tests/driver.c @@ -2,7 +2,7 @@ #include "accelerator/apis.h" #include "test_define.h" -iota_client_service_t service; +static ta_core_t ta_core; struct timespec start_time, end_time; #if defined(ENABLE_STAT) @@ -29,7 +29,8 @@ void test_generate_address(void) { for (size_t count = 0; count < TEST_COUNT; count++) { clock_gettime(CLOCK_REALTIME, &start_time); - TEST_ASSERT_FALSE(api_generate_address(&service, &json_result)); + TEST_ASSERT_FALSE( + api_generate_address(&ta_core.config, &ta_core.service, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); #if defined(ENABLE_STAT) printf("%lf\n", diff_time(start_time, end_time)); @@ -46,7 +47,8 @@ void test_get_tips_pair(void) { for (size_t count = 0; count < TEST_COUNT; count++) { clock_gettime(CLOCK_REALTIME, &start_time); - TEST_ASSERT_FALSE(api_get_tips_pair(&service, &json_result)); + TEST_ASSERT_FALSE( + api_get_tips_pair(&ta_core.config, &ta_core.service, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); #if defined(ENABLE_STAT) printf("%lf\n", diff_time(start_time, end_time)); @@ -63,7 +65,7 @@ void test_get_tips(void) { for (size_t count = 0; count < TEST_COUNT; count++) { clock_gettime(CLOCK_REALTIME, &start_time); - TEST_ASSERT_FALSE(api_get_tips(&service, &json_result)); + TEST_ASSERT_FALSE(api_get_tips(&ta_core.service, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); #if defined(ENABLE_STAT) printf("%lf\n", diff_time(start_time, end_time)); @@ -85,7 +87,8 @@ void test_send_transfer(void) { for (size_t count = 0; count < TEST_COUNT; count++) { clock_gettime(CLOCK_REALTIME, &start_time); - TEST_ASSERT_FALSE(api_send_transfer(&service, json, &json_result)); + TEST_ASSERT_FALSE(api_send_transfer(&ta_core.config, &ta_core.service, json, + &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); #if defined(ENABLE_STAT) printf("%lf\n", diff_time(start_time, end_time)); @@ -103,8 +106,8 @@ void test_get_transaction_object(void) { clock_gettime(CLOCK_REALTIME, &start_time); for (size_t count = 0; count < TEST_COUNT; count++) { clock_gettime(CLOCK_REALTIME, &start_time); - TEST_ASSERT_FALSE( - api_get_transaction_object(&service, TRYTES_81_1, &json_result)); + TEST_ASSERT_FALSE(api_get_transaction_object(&ta_core.service, TRYTES_81_1, + &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); #if defined(ENABLE_STAT) printf("%lf\n", diff_time(start_time, end_time)); @@ -122,7 +125,7 @@ void test_find_transactions_by_tag(void) { for (size_t count = 0; count < TEST_COUNT; count++) { clock_gettime(CLOCK_REALTIME, &start_time); TEST_ASSERT_FALSE( - api_find_transactions_by_tag(&service, TAG_MSG, &json_result)); + api_find_transactions_by_tag(&ta_core.service, TAG_MSG, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); #if defined(ENABLE_STAT) printf("%lf\n", diff_time(start_time, end_time)); @@ -139,8 +142,8 @@ void test_find_transactions_obj_by_tag(void) { for (size_t count = 0; count < TEST_COUNT; count++) { clock_gettime(CLOCK_REALTIME, &start_time); - TEST_ASSERT_FALSE( - api_find_transactions_obj_by_tag(&service, TAG_MSG, &json_result)); + TEST_ASSERT_FALSE(api_find_transactions_obj_by_tag(&ta_core.service, + TAG_MSG, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); #if defined(ENABLE_STAT) printf("%lf\n", diff_time(start_time, end_time)); @@ -158,7 +161,7 @@ void test_receive_mam_message(void) { for (size_t count = 0; count < TEST_COUNT; count++) { clock_gettime(CLOCK_REALTIME, &start_time); TEST_ASSERT_FALSE( - api_receive_mam_message(&service, BUNDLE_HASH, &json_result)); + api_receive_mam_message(&ta_core.service, BUNDLE_HASH, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); #if defined(ENABLE_STAT) printf("%lf\n", diff_time(start_time, end_time)); @@ -171,12 +174,8 @@ void test_receive_mam_message(void) { int main(void) { UNITY_BEGIN(); - service.http.path = "/"; - service.http.host = IRI_HOST; - service.http.port = IRI_PORT; - service.http.api_version = 1; - service.serializer_type = SR_JSON; - iota_client_core_init(&service); + + ta_config_init(&ta_core.info, &ta_core.config, &ta_core.service); printf("Total samples for each API test: %d\n", TEST_COUNT); RUN_TEST(test_generate_address); @@ -187,6 +186,6 @@ int main(void) { RUN_TEST(test_find_transactions_by_tag); RUN_TEST(test_find_transactions_obj_by_tag); RUN_TEST(test_receive_mam_message); - iota_client_core_destroy(&service); + ta_config_destroy(&ta_core.service); return UNITY_END(); } diff --git a/tests/iota_api_mock.cc b/tests/iota_api_mock.cc index ae3ea4c1..ab9bd303 100644 --- a/tests/iota_api_mock.cc +++ b/tests/iota_api_mock.cc @@ -78,6 +78,6 @@ retcode_t iota_client_get_trytes(const iota_client_service_t* const service, } status_t ta_send_trytes(const iota_client_service_t* const service, - hash8019_array_p trytes) { - return APIMockObj.ta_send_trytes(service, trytes); + uint8_t depth, uint8_t mwm, hash8019_array_p trytes) { + return APIMockObj.ta_send_trytes(service, depth, mwm, trytes); } diff --git a/tests/iota_api_mock.hh b/tests/iota_api_mock.hh index ac59aef9..4d5e8b57 100644 --- a/tests/iota_api_mock.hh +++ b/tests/iota_api_mock.hh @@ -43,6 +43,7 @@ class IotaAPI { return RC_OK; } virtual status_t ta_send_trytes(const iota_client_service_t* const service, + uint8_t depth, uint8_t mwm, hash8019_array_p trytes) { return SC_OK; } @@ -79,7 +80,7 @@ class APIMock : public IotaAPI { MOCK_METHOD3(iota_client_get_trytes, retcode_t(const iota_client_service_t* const service, get_trytes_req_t* const req, get_trytes_res_t* res)); - MOCK_METHOD2(ta_send_trytes, + MOCK_METHOD4(ta_send_trytes, status_t(const iota_client_service_t* const service, - hash8019_array_p trytes)); + uint8_t depth, uint8_t mwm, hash8019_array_p trytes)); }; diff --git a/tests/test_common.cc b/tests/test_common.cc index 9e276721..3dd07cf8 100644 --- a/tests/test_common.cc +++ b/tests/test_common.cc @@ -8,6 +8,7 @@ using ::testing::AtLeast; using ::testing::ElementsAreArray; APIMock APIMockObj; +iota_config_t config; iota_client_service_t service; TEST(GetTxnToApproveTest, TrunkBranchHashTest) { @@ -24,7 +25,7 @@ TEST(GetTxnToApproveTest, TrunkBranchHashTest) { EXPECT_CALL(APIMockObj, iota_client_get_transactions_to_approve(_, _, _)) .Times(AtLeast(1)); - EXPECT_EQ(cclient_get_txn_to_approve(&service, res), 0); + EXPECT_EQ(cclient_get_txn_to_approve(&service, 3, res), 0); EXPECT_FALSE(memcmp(res->tips->hash, hash_trits_1, sizeof(flex_trit_t) * FLEX_TRIT_SIZE_243)); hash243_stack_pop(&res->tips); @@ -72,6 +73,7 @@ TEST(FindTxnTest, TxnHashTest) { } TEST(GenAdressTest, GetNewAddressTest) { + config.seed = SEED; hash243_queue_entry_t* q_iter = NULL; ta_generate_address_res_t* res = ta_generate_address_res_new(); flex_trit_t hash_trits_1[FLEX_TRIT_SIZE_243]; @@ -82,7 +84,7 @@ TEST(GenAdressTest, GetNewAddressTest) { EXPECT_CALL(APIMockObj, iota_client_get_new_address(_, _, _, _)) .Times(AtLeast(1)); - EXPECT_EQ(ta_generate_address(&service, res), 0); + EXPECT_EQ(ta_generate_address(&config, &service, res), 0); CDL_FOREACH(res->addresses, q_iter) { EXPECT_FALSE(memcmp(q_iter->hash, hash_trits_1, sizeof(flex_trit_t) * FLEX_TRIT_SIZE_243)); @@ -154,11 +156,11 @@ TEST(SendTransferTest, SendTransferTest) { flex_trits_slice(req->message, req->msg_len, msg_trits, req->msg_len, 0, req->msg_len); - EXPECT_CALL(APIMockObj, ta_send_trytes(_, _)).Times(AtLeast(1)); + EXPECT_CALL(APIMockObj, ta_send_trytes(_, _, _, _)).Times(AtLeast(1)); EXPECT_CALL(APIMockObj, iota_client_find_transactions(_, _, _)) .Times(AtLeast(1)); - EXPECT_EQ(ta_send_transfer(&service, req, res), 0); + EXPECT_EQ(ta_send_transfer(&config, &service, req, res), 0); txn_hash = hash243_queue_peek(res->hash); EXPECT_FALSE( memcmp(txn_hash, hash_trits_1, sizeof(flex_trit_t) * FLEX_TRIT_SIZE_243)); From 7d431e5973bbd420f63a8bb6241aa9e2be9619fa Mon Sep 17 00:00:00 2001 From: Yu Wei Wu Date: Thu, 21 Mar 2019 20:33:20 +0800 Subject: [PATCH 14/29] fix(config): Rename iota_config_t variable --- accelerator/apis.c | 12 ++++++------ accelerator/apis.h | 11 ++++++----- accelerator/common_core.c | 8 ++++---- accelerator/common_core.h | 7 ++++--- accelerator/config.c | 12 ++++++------ accelerator/config.h | 6 +++--- accelerator/server.cc | 8 ++++---- tests/driver.c | 8 ++++---- tests/test_common.cc | 8 ++++---- 9 files changed, 41 insertions(+), 39 deletions(-) diff --git a/accelerator/apis.c b/accelerator/apis.c index 2823a8e1..44fe2fe8 100644 --- a/accelerator/apis.c +++ b/accelerator/apis.c @@ -44,7 +44,7 @@ status_t api_get_tips(const iota_client_service_t* const service, return ret; } -status_t api_get_tips_pair(const iota_config_t* const config, +status_t api_get_tips_pair(const iota_config_t* const tangle, const iota_client_service_t* const service, char** json_result) { status_t ret = SC_OK; @@ -54,7 +54,7 @@ status_t api_get_tips_pair(const iota_config_t* const config, goto done; } - ret = cclient_get_txn_to_approve(service, config->depth, res); + ret = cclient_get_txn_to_approve(service, tangle->depth, res); if (ret) { goto done; } @@ -66,7 +66,7 @@ status_t api_get_tips_pair(const iota_config_t* const config, return ret; } -status_t api_generate_address(const iota_config_t* const config, +status_t api_generate_address(const iota_config_t* const tangle, const iota_client_service_t* const service, char** json_result) { status_t ret = SC_OK; @@ -76,7 +76,7 @@ status_t api_generate_address(const iota_config_t* const config, goto done; } - ret = ta_generate_address(config, service, res); + ret = ta_generate_address(tangle, service, res); if (ret) { goto done; } @@ -205,7 +205,7 @@ status_t api_receive_mam_message(const iota_client_service_t* const service, return ret; } -status_t api_send_transfer(const iota_config_t* const config, +status_t api_send_transfer(const iota_config_t* const tangle, const iota_client_service_t* const service, const char* const obj, char** json_result) { status_t ret = SC_OK; @@ -225,7 +225,7 @@ status_t api_send_transfer(const iota_config_t* const config, goto done; } - ret = ta_send_transfer(config, service, req, res); + ret = ta_send_transfer(tangle, service, req, res); if (ret) { goto done; } diff --git a/accelerator/apis.h b/accelerator/apis.h index c8d32adc..2280ff41 100644 --- a/accelerator/apis.h +++ b/accelerator/apis.h @@ -28,7 +28,7 @@ extern "C" { * Generate and return an unused address from the seed. An unused address means * the address does not have any transaction with it yet. * - * @param[in] config IOTA API parameter configurations + * @param[in] tangle IOTA API parameter configurations * @param[in] service IRI node end point service * @param[out] json_result Result containing an unused address in json format * @@ -36,7 +36,7 @@ extern "C" { * - SC_OK on success * - non-zero on error */ -status_t api_generate_address(const iota_config_t* const config, +status_t api_generate_address(const iota_config_t* const tangle, const iota_client_service_t* const service, char** json_result); @@ -46,7 +46,7 @@ status_t api_generate_address(const iota_config_t* const config, * Get a tips pair as trunk/branch transactions for transaction construction. * The result is char array in json format: * - * @param[in] config IOTA API parameter configurations + * @param[in] tangle IOTA API parameter configurations * @param[in] service IRI node end point service * @param[out] json_result Result containing a tips pair in json format * @@ -54,7 +54,7 @@ status_t api_generate_address(const iota_config_t* const config, * - SC_OK on success * - non-zero on error */ -status_t api_get_tips_pair(const iota_config_t* const config, +status_t api_get_tips_pair(const iota_config_t* const tangle, const iota_client_service_t* const service, char** json_result); @@ -97,6 +97,7 @@ status_t api_receive_mam_message(const iota_client_service_t* const service, * fields include address, value, tag, and message. This API would also try to * find the transactions after bundle sent. * + * @param[in] tangle IOTA API parameter configurations * @param[in] service IRI node end point service * @param[in] obj Input data in JSON * @param[out] json_result Result containing transaction objects in json format @@ -105,7 +106,7 @@ status_t api_receive_mam_message(const iota_client_service_t* const service, * - SC_OK on success * - non-zero on error */ -status_t api_send_transfer(const iota_config_t* const config, +status_t api_send_transfer(const iota_config_t* const tangle, const iota_client_service_t* const service, const char* const obj, char** json_result); diff --git a/accelerator/common_core.c b/accelerator/common_core.c index 97df5861..d1debe98 100644 --- a/accelerator/common_core.c +++ b/accelerator/common_core.c @@ -210,7 +210,7 @@ status_t ta_send_trytes(const iota_client_service_t* const service, return ret; } -status_t ta_generate_address(const iota_config_t* const config, +status_t ta_generate_address(const iota_config_t* const tangle, const iota_client_service_t* const service, ta_generate_address_res_t* res) { if (res == NULL) { @@ -221,7 +221,7 @@ status_t ta_generate_address(const iota_config_t* const config, hash243_queue_t out_address = NULL; flex_trit_t seed_trits[FLEX_TRIT_SIZE_243]; flex_trits_from_trytes(seed_trits, NUM_TRITS_HASH, - (const tryte_t*)config->seed, NUM_TRYTES_HASH, + (const tryte_t*)tangle->seed, NUM_TRYTES_HASH, NUM_TRYTES_HASH); address_opt_t opt = {.security = 3, .start = 0, .total = 0}; @@ -235,7 +235,7 @@ status_t ta_generate_address(const iota_config_t* const config, return ret; } -status_t ta_send_transfer(const iota_config_t* const config, +status_t ta_send_transfer(const iota_config_t* const tangle, const iota_client_service_t* const service, const ta_send_transfer_req_t* const req, ta_send_transfer_res_t* res) { @@ -275,7 +275,7 @@ status_t ta_send_transfer(const iota_config_t* const config, free(serialized_txn); } - ret = ta_send_trytes(service, config->depth, config->mwm, raw_tx); + ret = ta_send_trytes(service, tangle->depth, tangle->mwm, raw_tx); if (ret) { goto done; } diff --git a/accelerator/common_core.h b/accelerator/common_core.h index 4fb8c333..cdb5d0cb 100644 --- a/accelerator/common_core.h +++ b/accelerator/common_core.h @@ -64,6 +64,7 @@ status_t cclient_get_tips(const iota_client_service_t* const service, * Generate and return an unused address from the seed. An unused address means * the address does not have any transaction with it yet. * + * @param[in] tangle IOTA API parameter configurations * @param[in] service IRI node end point service * @param[out] res Result containing an unused address in * ta_generate_address_res_t @@ -72,7 +73,7 @@ status_t cclient_get_tips(const iota_client_service_t* const service, * - SC_OK on success * - non-zero on error */ -status_t ta_generate_address(const iota_config_t* const config, +status_t ta_generate_address(const iota_config_t* const tangle, const iota_client_service_t* const service, ta_generate_address_res_t* res); @@ -83,7 +84,7 @@ status_t ta_generate_address(const iota_config_t* const config, * fields include address, value, tag, and message. This API would also try to * find the transactions after bundle sent. * - * @param[in] config IOTA API parameter configurations + * @param[in] tangle IOTA API parameter configurations * @param[in] service IRI node end point service * @param[in] req Request containing address value, message, tag in * ta_send_transfer_req_t @@ -93,7 +94,7 @@ status_t ta_generate_address(const iota_config_t* const config, * - SC_OK on success * - non-zero on error */ -status_t ta_send_transfer(const iota_config_t* const config, +status_t ta_send_transfer(const iota_config_t* const tangle, const iota_client_service_t* const service, const ta_send_transfer_req_t* const req, ta_send_transfer_res_t* res); diff --git a/accelerator/config.c b/accelerator/config.c index b57add4e..b99f5a4f 100644 --- a/accelerator/config.c +++ b/accelerator/config.c @@ -1,14 +1,14 @@ #include "config.h" #include "utils/logger_helper.h" -#define CONFIG_LOGGER_ID "config" +#define CONFIG_LOGGER_ID "TA" static logger_id_t logger_id; -status_t ta_config_init(ta_config_t* const info, iota_config_t* const config, +status_t ta_config_init(ta_config_t* const info, iota_config_t* const tangle, iota_client_service_t* const service) { status_t ret = SC_OK; - if (info == NULL || config == NULL || service == NULL) { + if (info == NULL || tangle == NULL || service == NULL) { return SC_TA_NULL; } @@ -23,9 +23,9 @@ status_t ta_config_init(ta_config_t* const info, iota_config_t* const config, info->thread_count = TA_THREAD_COUNT; log_info(logger_id, "Initializing IRI configuration\n"); - config->depth = DEPTH; - config->mwm = MWM; - config->seed = SEED; + tangle->depth = DEPTH; + tangle->mwm = MWM; + tangle->seed = SEED; log_info(logger_id, "Initializing IRI connection\n"); service->http.path = "/"; diff --git a/accelerator/config.h b/accelerator/config.h index 34676a02..4a27fdea 100644 --- a/accelerator/config.h +++ b/accelerator/config.h @@ -52,7 +52,7 @@ typedef struct ta_config_s { /** struct type of accelerator core */ typedef struct ta_core_s { ta_config_t info; /**< accelerator configiuration structure */ - iota_config_t config; /**< iota configuration structure */ + iota_config_t tangle; /**< iota configuration structure */ iota_client_service_t service; /**< iota connection structure */ } ta_core_t; @@ -61,14 +61,14 @@ typedef struct ta_core_s { * Should be called first * * @param info[in] Tangle-accelerator configuration variables - * @param config[in] iota configuration variables + * @param tangle[in] iota configuration variables * @param service[in] IRI connection configuration variables * * @return * - SC_OK on success * - non-zero on error */ -status_t ta_config_init(ta_config_t* const info, iota_config_t* const config, +status_t ta_config_init(ta_config_t* const info, iota_config_t* const tangle, iota_client_service_t* const service); /** diff --git a/accelerator/server.cc b/accelerator/server.cc index 37bb6769..7ee9173b 100644 --- a/accelerator/server.cc +++ b/accelerator/server.cc @@ -59,7 +59,7 @@ int main(int, char const**) { } logger_id = logger_helper_enable(MAIN_LOGGER_ID, LOGGER_DEBUG, true); - ta_config_init(&ta_core.info, &ta_core.config, &ta_core.service); + ta_config_init(&ta_core.info, &ta_core.tangle, &ta_core.service); mux.handle("/mam/{bundle:[A-Z9]{81}}") .method(served::method::OPTIONS, @@ -166,7 +166,7 @@ int main(int, char const**) { char* json_result; ret = - api_get_tips_pair(&ta_core.config, &ta_core.service, &json_result); + api_get_tips_pair(&ta_core.tangle, &ta_core.service, &json_result); ret = set_response_content(ret, &json_result); set_method_header(res, HTTP_METHOD_GET); res.set_status(ret); @@ -208,7 +208,7 @@ int main(int, char const**) { status_t ret = SC_OK; char* json_result; - ret = api_generate_address(&ta_core.config, &ta_core.service, + ret = api_generate_address(&ta_core.tangle, &ta_core.service, &json_result); ret = set_response_content(ret, &json_result); set_method_header(res, HTTP_METHOD_GET); @@ -240,7 +240,7 @@ int main(int, char const**) { res.set_status(SC_HTTP_BAD_REQUEST); cJSON_Delete(json_obj); } else { - ret = api_send_transfer(&ta_core.config, &ta_core.service, + ret = api_send_transfer(&ta_core.tangle, &ta_core.service, req.body().c_str(), &json_result); ret = set_response_content(ret, &json_result); res.set_status(ret); diff --git a/tests/driver.c b/tests/driver.c index d46374a8..a84c4e6b 100644 --- a/tests/driver.c +++ b/tests/driver.c @@ -30,7 +30,7 @@ void test_generate_address(void) { for (size_t count = 0; count < TEST_COUNT; count++) { clock_gettime(CLOCK_REALTIME, &start_time); TEST_ASSERT_FALSE( - api_generate_address(&ta_core.config, &ta_core.service, &json_result)); + api_generate_address(&ta_core.tangle, &ta_core.service, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); #if defined(ENABLE_STAT) printf("%lf\n", diff_time(start_time, end_time)); @@ -48,7 +48,7 @@ void test_get_tips_pair(void) { for (size_t count = 0; count < TEST_COUNT; count++) { clock_gettime(CLOCK_REALTIME, &start_time); TEST_ASSERT_FALSE( - api_get_tips_pair(&ta_core.config, &ta_core.service, &json_result)); + api_get_tips_pair(&ta_core.tangle, &ta_core.service, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); #if defined(ENABLE_STAT) printf("%lf\n", diff_time(start_time, end_time)); @@ -87,7 +87,7 @@ void test_send_transfer(void) { for (size_t count = 0; count < TEST_COUNT; count++) { clock_gettime(CLOCK_REALTIME, &start_time); - TEST_ASSERT_FALSE(api_send_transfer(&ta_core.config, &ta_core.service, json, + TEST_ASSERT_FALSE(api_send_transfer(&ta_core.tangle, &ta_core.service, json, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); #if defined(ENABLE_STAT) @@ -175,7 +175,7 @@ void test_receive_mam_message(void) { int main(void) { UNITY_BEGIN(); - ta_config_init(&ta_core.info, &ta_core.config, &ta_core.service); + ta_config_init(&ta_core.info, &ta_core.tangle, &ta_core.service); printf("Total samples for each API test: %d\n", TEST_COUNT); RUN_TEST(test_generate_address); diff --git a/tests/test_common.cc b/tests/test_common.cc index 3dd07cf8..b9e7cb3c 100644 --- a/tests/test_common.cc +++ b/tests/test_common.cc @@ -8,7 +8,7 @@ using ::testing::AtLeast; using ::testing::ElementsAreArray; APIMock APIMockObj; -iota_config_t config; +iota_config_t tangle; iota_client_service_t service; TEST(GetTxnToApproveTest, TrunkBranchHashTest) { @@ -73,7 +73,7 @@ TEST(FindTxnTest, TxnHashTest) { } TEST(GenAdressTest, GetNewAddressTest) { - config.seed = SEED; + tangle.seed = SEED; hash243_queue_entry_t* q_iter = NULL; ta_generate_address_res_t* res = ta_generate_address_res_new(); flex_trit_t hash_trits_1[FLEX_TRIT_SIZE_243]; @@ -84,7 +84,7 @@ TEST(GenAdressTest, GetNewAddressTest) { EXPECT_CALL(APIMockObj, iota_client_get_new_address(_, _, _, _)) .Times(AtLeast(1)); - EXPECT_EQ(ta_generate_address(&config, &service, res), 0); + EXPECT_EQ(ta_generate_address(&tangle, &service, res), 0); CDL_FOREACH(res->addresses, q_iter) { EXPECT_FALSE(memcmp(q_iter->hash, hash_trits_1, sizeof(flex_trit_t) * FLEX_TRIT_SIZE_243)); @@ -160,7 +160,7 @@ TEST(SendTransferTest, SendTransferTest) { EXPECT_CALL(APIMockObj, iota_client_find_transactions(_, _, _)) .Times(AtLeast(1)); - EXPECT_EQ(ta_send_transfer(&config, &service, req, res), 0); + EXPECT_EQ(ta_send_transfer(&tangle, &service, req, res), 0); txn_hash = hash243_queue_peek(res->hash); EXPECT_FALSE( memcmp(txn_hash, hash_trits_1, sizeof(flex_trit_t) * FLEX_TRIT_SIZE_243)); From cd5202f5b705eedeb66df17f86a915bc833b96b5 Mon Sep 17 00:00:00 2001 From: Yu Wei Wu Date: Fri, 22 Mar 2019 23:11:20 +0800 Subject: [PATCH 15/29] fix(common): Fix send_trytes parameters --- accelerator/common_core.c | 10 +++++----- accelerator/common_core.h | 7 +++---- tests/iota_api_mock.cc | 7 ++++--- tests/iota_api_mock.hh | 11 ++++++----- tests/test_common.cc | 2 +- 5 files changed, 19 insertions(+), 18 deletions(-) diff --git a/accelerator/common_core.c b/accelerator/common_core.c index d1debe98..c5c37331 100644 --- a/accelerator/common_core.c +++ b/accelerator/common_core.c @@ -162,8 +162,8 @@ status_t ta_attach_to_tangle(const attach_to_tangle_req_t* const req, return ret; } -status_t ta_send_trytes(const iota_client_service_t* const service, - uint8_t const depth, uint8_t const mwm, +status_t ta_send_trytes(const iota_config_t* const tangle, + const iota_client_service_t* const service, hash8019_array_p trytes) { status_t ret = SC_OK; ta_get_tips_res_t* get_txn_res = ta_get_tips_res_new(); @@ -175,7 +175,7 @@ status_t ta_send_trytes(const iota_client_service_t* const service, } // get transaction to approve - ret = cclient_get_txn_to_approve(service, depth, get_txn_res); + ret = cclient_get_txn_to_approve(service, tangle->depth, get_txn_res); if (ret) { goto done; } @@ -188,7 +188,7 @@ status_t ta_send_trytes(const iota_client_service_t* const service, memcpy(attach_req->branch, hash243_stack_peek(get_txn_res->tips), FLEX_TRIT_SIZE_243); hash243_stack_pop(&get_txn_res->tips); - attach_req->mwm = mwm; + attach_req->mwm = tangle->mwm; attach_req->trytes = trytes; ret = ta_attach_to_tangle(attach_req, attach_res); if (ret) { @@ -275,7 +275,7 @@ status_t ta_send_transfer(const iota_config_t* const tangle, free(serialized_txn); } - ret = ta_send_trytes(service, tangle->depth, tangle->mwm, raw_tx); + ret = ta_send_trytes(tangle, service, raw_tx); if (ret) { goto done; } diff --git a/accelerator/common_core.h b/accelerator/common_core.h index cdb5d0cb..c372272d 100644 --- a/accelerator/common_core.h +++ b/accelerator/common_core.h @@ -106,17 +106,16 @@ status_t ta_send_transfer(const iota_config_t* const tangle, * bundle and do PoW in `ta_attach_to_tangle` and store and broadcast * transaction to tangle. * + * @param[in] tangle IOTA API parameter configurations * @param[in] service IRI node end point service - * @param[in] dpeth Depth of get transaction to approve - * @param[in] mwm Minimum weight magnitude * @param[in] trytes Trytes that will be attached to tangle * * @return * - SC_OK on success * - non-zero on error */ -status_t ta_send_trytes(const iota_client_service_t* const service, - uint8_t const depth, uint8_t const mwm, +status_t ta_send_trytes(const iota_config_t* const tangle, + const iota_client_service_t* const service, hash8019_array_p trytes); /** diff --git a/tests/iota_api_mock.cc b/tests/iota_api_mock.cc index ab9bd303..70acc1a4 100644 --- a/tests/iota_api_mock.cc +++ b/tests/iota_api_mock.cc @@ -77,7 +77,8 @@ retcode_t iota_client_get_trytes(const iota_client_service_t* const service, return APIMockObj.iota_client_get_trytes(service, req, res); } -status_t ta_send_trytes(const iota_client_service_t* const service, - uint8_t depth, uint8_t mwm, hash8019_array_p trytes) { - return APIMockObj.ta_send_trytes(service, depth, mwm, trytes); +status_t ta_send_trytes(const iota_config_t* const tangle, + const iota_client_service_t* const service, + hash8019_array_p trytes) { + return APIMockObj.ta_send_trytes(tangle, service, trytes); } diff --git a/tests/iota_api_mock.hh b/tests/iota_api_mock.hh index 4d5e8b57..bb8cd88a 100644 --- a/tests/iota_api_mock.hh +++ b/tests/iota_api_mock.hh @@ -42,8 +42,8 @@ class IotaAPI { get_trytes_res_t* res) { return RC_OK; } - virtual status_t ta_send_trytes(const iota_client_service_t* const service, - uint8_t depth, uint8_t mwm, + virtual status_t ta_send_trytes(const iota_config_t* const tangle, + const iota_client_service_t* const service, hash8019_array_p trytes) { return SC_OK; } @@ -80,7 +80,8 @@ class APIMock : public IotaAPI { MOCK_METHOD3(iota_client_get_trytes, retcode_t(const iota_client_service_t* const service, get_trytes_req_t* const req, get_trytes_res_t* res)); - MOCK_METHOD4(ta_send_trytes, - status_t(const iota_client_service_t* const service, - uint8_t depth, uint8_t mwm, hash8019_array_p trytes)); + MOCK_METHOD3(ta_send_trytes, + status_t(const iota_config_t* const tangle, + const iota_client_service_t* const service, + hash8019_array_p trytes)); }; diff --git a/tests/test_common.cc b/tests/test_common.cc index b9e7cb3c..68d7de27 100644 --- a/tests/test_common.cc +++ b/tests/test_common.cc @@ -156,7 +156,7 @@ TEST(SendTransferTest, SendTransferTest) { flex_trits_slice(req->message, req->msg_len, msg_trits, req->msg_len, 0, req->msg_len); - EXPECT_CALL(APIMockObj, ta_send_trytes(_, _, _, _)).Times(AtLeast(1)); + EXPECT_CALL(APIMockObj, ta_send_trytes(_, _, _)).Times(AtLeast(1)); EXPECT_CALL(APIMockObj, iota_client_find_transactions(_, _, _)) .Times(AtLeast(1)); From 22627b5c0de99ff5b0413b9af468047347d38734 Mon Sep 17 00:00:00 2001 From: jkrvivian Date: Sat, 23 Mar 2019 17:25:18 +0800 Subject: [PATCH 16/29] fix(test): Fix memory leak when ENABLE_STAT is enabled --- tests/driver.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/driver.c b/tests/driver.c index 9580701d..c562a5d0 100644 --- a/tests/driver.c +++ b/tests/driver.c @@ -31,13 +31,13 @@ void test_generate_address(void) { clock_gettime(CLOCK_REALTIME, &start_time); TEST_ASSERT_FALSE(api_generate_address(&service, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); + free(json_result); #if defined(ENABLE_STAT) printf("%lf\n", diff_time(start_time, end_time)); #endif sum += diff_time(start_time, end_time); } printf("Average time of generate_address: %lf\n", sum / TEST_COUNT); - free(json_result); } void test_get_tips_pair(void) { @@ -48,13 +48,13 @@ void test_get_tips_pair(void) { clock_gettime(CLOCK_REALTIME, &start_time); TEST_ASSERT_FALSE(api_get_tips_pair(&service, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); + free(json_result); #if defined(ENABLE_STAT) printf("%lf\n", diff_time(start_time, end_time)); #endif sum += diff_time(start_time, end_time); } printf("Average time of get_tips_pair: %lf\n", sum / TEST_COUNT); - free(json_result); } void test_get_tips(void) { @@ -65,13 +65,13 @@ void test_get_tips(void) { clock_gettime(CLOCK_REALTIME, &start_time); TEST_ASSERT_FALSE(api_get_tips(&service, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); + free(json_result); #if defined(ENABLE_STAT) printf("%lf\n", diff_time(start_time, end_time)); #endif sum += diff_time(start_time, end_time); } printf("Average time of get_tips: %lf\n", sum / TEST_COUNT); - free(json_result); } void test_send_transfer(void) { @@ -87,13 +87,13 @@ void test_send_transfer(void) { clock_gettime(CLOCK_REALTIME, &start_time); TEST_ASSERT_FALSE(api_send_transfer(&service, json, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); + free(json_result); #if defined(ENABLE_STAT) printf("%lf\n", diff_time(start_time, end_time)); #endif sum += diff_time(start_time, end_time); } printf("Average time of send_transfer: %lf\n", sum / TEST_COUNT); - free(json_result); } void test_get_transaction_object(void) { @@ -106,13 +106,13 @@ void test_get_transaction_object(void) { TEST_ASSERT_FALSE( api_get_transaction_object(&service, TRYTES_81_1, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); + free(json_result); #if defined(ENABLE_STAT) printf("%lf\n", diff_time(start_time, end_time)); #endif sum += diff_time(start_time, end_time); } printf("Average time of get_transaction_object: %lf\n", sum / TEST_COUNT); - free(json_result); } void test_find_transactions_by_tag(void) { @@ -124,13 +124,13 @@ void test_find_transactions_by_tag(void) { TEST_ASSERT_FALSE( api_find_transactions_by_tag(&service, TAG_MSG, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); + free(json_result); #if defined(ENABLE_STAT) printf("%lf\n", diff_time(start_time, end_time)); #endif sum += diff_time(start_time, end_time); } printf("Average time of find_transactions_by_tag: %lf\n", sum / TEST_COUNT); - free(json_result); } void test_find_transactions_obj_by_tag(void) { @@ -142,13 +142,13 @@ void test_find_transactions_obj_by_tag(void) { TEST_ASSERT_FALSE( api_find_transactions_obj_by_tag(&service, TAG_MSG, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); + free(json_result); #if defined(ENABLE_STAT) printf("%lf\n", diff_time(start_time, end_time)); #endif sum += diff_time(start_time, end_time); } printf("Average time of find_tx_obj_by_tag: %lf\n", sum / TEST_COUNT); - free(json_result); } void test_receive_mam_message(void) { @@ -160,13 +160,13 @@ void test_receive_mam_message(void) { TEST_ASSERT_FALSE( api_receive_mam_message(&service, BUNDLE_HASH, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); + free(json_result); #if defined(ENABLE_STAT) printf("%lf\n", diff_time(start_time, end_time)); #endif sum += diff_time(start_time, end_time); } printf("Average time of receive_mam_message: %lf\n", sum / TEST_COUNT); - free(json_result); } int main(void) { From 0acae9864a28697cd82c39c8ba868cdbc1b1e20c Mon Sep 17 00:00:00 2001 From: Yu Wei Wu Date: Tue, 26 Mar 2019 13:41:20 +0800 Subject: [PATCH 17/29] feat(config): Move pow init/destroy to cofig --- accelerator/BUILD | 2 +- accelerator/common_core.c | 3 --- accelerator/config.c | 4 ++++ accelerator/config.h | 1 + utils/BUILD | 1 - 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/accelerator/BUILD b/accelerator/BUILD index 3168a9b0..3af2503e 100644 --- a/accelerator/BUILD +++ b/accelerator/BUILD @@ -44,7 +44,6 @@ cc_library( "//request", "//response", "//utils:cache", - "//utils:pow", "@com_github_uthash//:uthash", "@entangled//common/model:bundle", "@entangled//utils:time", @@ -58,6 +57,7 @@ cc_library( visibility = ["//visibility:public"], deps = [ ":ta_errors", + "//utils:pow", "@entangled//cclient/api", "@entangled//cclient/types", ], diff --git a/accelerator/common_core.c b/accelerator/common_core.c index c5c37331..45746ea1 100644 --- a/accelerator/common_core.c +++ b/accelerator/common_core.c @@ -1,7 +1,6 @@ #include "common_core.h" #include #include "utils/cache.h" -#include "utils/pow.h" status_t cclient_get_txn_to_approve(const iota_client_service_t* const service, uint8_t const depth, @@ -115,7 +114,6 @@ status_t ta_attach_to_tangle(const attach_to_tangle_req_t* const req, char cache_key[NUM_TRYTES_HASH] = {0}; char cache_value[NUM_TRYTES_SERIALIZED_TRANSACTION] = {0}; cache_t* cache = cache_init(); - pow_init(); // create bundle bundle_transactions_new(&bundle); @@ -157,7 +155,6 @@ status_t ta_attach_to_tangle(const attach_to_tangle_req_t* const req, done: cache_stop(&cache); - pow_destroy(); bundle_transactions_free(&bundle); return ret; } diff --git a/accelerator/config.c b/accelerator/config.c index b99f5a4f..daac92a1 100644 --- a/accelerator/config.c +++ b/accelerator/config.c @@ -41,6 +41,8 @@ status_t ta_config_init(ta_config_t* const info, iota_config_t* const tangle, } iota_client_extended_init(); + pow_init(); + return ret; } @@ -48,5 +50,7 @@ void ta_config_destroy(iota_client_service_t* const service) { log_info(logger_id, "Destroying IRI connection\n"); iota_client_extended_destroy(); iota_client_core_destroy(service); + + pow_destroy(); logger_helper_release(logger_id); } diff --git a/accelerator/config.h b/accelerator/config.h index 4a27fdea..a61c8adc 100644 --- a/accelerator/config.h +++ b/accelerator/config.h @@ -5,6 +5,7 @@ #include "cclient/api/core/core_api.h" #include "cclient/api/extended/extended_api.h" #include "cclient/types/types.h" +#include "utils/pow.h" #ifdef __cplusplus extern "C" { diff --git a/utils/BUILD b/utils/BUILD index 857158c9..de8bf999 100644 --- a/utils/BUILD +++ b/utils/BUILD @@ -17,7 +17,6 @@ cc_library( srcs = ["pow.c"], hdrs = ["pow.h"], deps = [ - "//accelerator:ta_config", "//accelerator:ta_errors", "//third_party:dcurl", "@entangled//cclient/types", From 00d4f18b01130a4f5bfae0bbcca2c2f7202cad8f Mon Sep 17 00:00:00 2001 From: jkrvivian Date: Mon, 25 Mar 2019 21:03:41 +0800 Subject: [PATCH 18/29] fix(test): Replace tag and hash for find transactions related apis When ENABLE_STAT is enabled, every api will be tested for TEST_COUNT times. send_transfer will issue TEST_COUNT transactions with tag defined in test_define.h which find transaction related apis also use to get transaction info. But after a few more times of execution, the size of retrieved transactions will get larger than the valid storage of redis cache server, then driver test would crash. This fix defines FIND_TAG_MSG and TRYTES_81_3 for retriving and another TAG_MSG for send_transfer that we can confirm the retrieved transaction info would not get larger. --- tests/driver.c | 10 +++++----- tests/test_define.h | 4 ++++ 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/driver.c b/tests/driver.c index 6e93480b..af77973a 100644 --- a/tests/driver.c +++ b/tests/driver.c @@ -106,7 +106,7 @@ void test_get_transaction_object(void) { clock_gettime(CLOCK_REALTIME, &start_time); for (size_t count = 0; count < TEST_COUNT; count++) { clock_gettime(CLOCK_REALTIME, &start_time); - TEST_ASSERT_FALSE(api_get_transaction_object(&ta_core.service, TRYTES_81_1, + TEST_ASSERT_FALSE(api_get_transaction_object(&ta_core.service, TRYTES_81_3, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); free(json_result); @@ -124,8 +124,8 @@ void test_find_transactions_by_tag(void) { for (size_t count = 0; count < TEST_COUNT; count++) { clock_gettime(CLOCK_REALTIME, &start_time); - TEST_ASSERT_FALSE( - api_find_transactions_by_tag(&ta_core.service, TAG_MSG, &json_result)); + TEST_ASSERT_FALSE(api_find_transactions_by_tag(&ta_core.service, + FIND_TAG_MSG, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); free(json_result); #if defined(ENABLE_STAT) @@ -142,8 +142,8 @@ void test_find_transactions_obj_by_tag(void) { for (size_t count = 0; count < TEST_COUNT; count++) { clock_gettime(CLOCK_REALTIME, &start_time); - TEST_ASSERT_FALSE(api_find_transactions_obj_by_tag(&ta_core.service, - TAG_MSG, &json_result)); + TEST_ASSERT_FALSE(api_find_transactions_obj_by_tag( + &ta_core.service, FIND_TAG_MSG, &json_result)); clock_gettime(CLOCK_REALTIME, &end_time); free(json_result); #if defined(ENABLE_STAT) diff --git a/tests/test_define.h b/tests/test_define.h index 86674d14..c5d2e1a1 100644 --- a/tests/test_define.h +++ b/tests/test_define.h @@ -17,10 +17,14 @@ extern "C" { #define TRYTES_81_2 \ "RVORZ9SIIP9RCYMREUIXXVPQIPHVCNPQ9HZWYKFWYWZRE9JQKG9REPKIASHUUECPSQO9JT9XNM" \ "VKWYGVA" +#define TRYTES_81_3 \ + "FPPBU9AOXZHSJEWDCPWGCPKGOVIUWWPDTIIXDJUKYYNNPTWCGDWFBZLKVCLOKGYEHJUMABSTA9" \ + "OP99999" #define BUNDLE_HASH \ "LVXEVZABVCIFEDSCONKEVEYBSIRMXGHLJDKSKQHTKZC9ULEAPSLKOOWCCZJGWSIISDDSEVUQHV" \ "GPFOSIW" #define TAG_MSG "TANGLEACCELERATOR9999999999" +#define FIND_TAG_MSG "TAFINDTXN999999999999999999" #define TAG_MSG_LEN 27 #define VALUE 100 #define TIMESTAMP 1546436542 From b8a6b60bc5c43e03d5b2596caa4cdccd30935222 Mon Sep 17 00:00:00 2001 From: Yu Wei Wu Date: Tue, 26 Mar 2019 14:58:50 +0800 Subject: [PATCH 19/29] feat(utils): Refactor cache interface Refactor cache interface to match the need of configuration. --- accelerator/common_core.c | 16 ++++++++-------- tests/test_cache.c | 15 +++++---------- utils/backend_redis.c | 38 +++++++++++++++++--------------------- utils/cache.h | 26 ++++++++++---------------- 4 files changed, 40 insertions(+), 55 deletions(-) diff --git a/accelerator/common_core.c b/accelerator/common_core.c index 45746ea1..6a9f8b00 100644 --- a/accelerator/common_core.c +++ b/accelerator/common_core.c @@ -113,7 +113,7 @@ status_t ta_attach_to_tangle(const attach_to_tangle_req_t* const req, flex_trit_t* elt = NULL; char cache_key[NUM_TRYTES_HASH] = {0}; char cache_value[NUM_TRYTES_SERIALIZED_TRANSACTION] = {0}; - cache_t* cache = cache_init(); + cache_init(); // create bundle bundle_transactions_new(&bundle); @@ -127,7 +127,7 @@ status_t ta_attach_to_tangle(const attach_to_tangle_req_t* const req, flex_trits_to_trytes( (tryte_t*)cache_value, NUM_TRYTES_SERIALIZED_TRANSACTION, elt, NUM_TRITS_SERIALIZED_TRANSACTION, NUM_TRITS_SERIALIZED_TRANSACTION); - ret = cache_set(cache, cache_key, cache_value); + ret = cache_set(cache_key, cache_value); if (ret) { goto done; } @@ -154,7 +154,7 @@ status_t ta_attach_to_tangle(const attach_to_tangle_req_t* const req, } done: - cache_stop(&cache); + cache_stop(); bundle_transactions_free(&bundle); return ret; } @@ -355,16 +355,16 @@ status_t ta_get_transaction_object(const iota_client_service_t* const service, char cache_value[FLEX_TRIT_SIZE_8019] = {0}; // get raw transaction data of transaction hashes - cache_t* cache = cache_init(); + cache_init(); get_trytes_req_t* get_trytes_req = get_trytes_req_new(); get_trytes_res_t* get_trytes_res = get_trytes_res_new(); - if (cache == NULL || get_trytes_req == NULL || get_trytes_res == NULL) { + if (get_trytes_req == NULL || get_trytes_res == NULL) { ret = SC_CCLIENT_OOM; goto done; } // get in cache first, then get from full node if no result in cache - ret = cache_get(cache, req, cache_value); + ret = cache_get(req, cache_value); if (ret == SC_OK) { flex_trits_from_trytes( tx_trits, NUM_TRITS_SERIALIZED_TRANSACTION, (const tryte_t*)cache_value, @@ -390,7 +390,7 @@ status_t ta_get_transaction_object(const iota_client_service_t* const service, flex_trits_to_trytes( (tryte_t*)cache_value, NUM_TRYTES_SERIALIZED_TRANSACTION, tx_trits, NUM_TRITS_SERIALIZED_TRANSACTION, NUM_TRITS_SERIALIZED_TRANSACTION); - cache_set(cache, req, cache_value); + cache_set(req, cache_value); } else { ret = SC_CCLIENT_NOT_FOUND; goto done; @@ -406,7 +406,7 @@ status_t ta_get_transaction_object(const iota_client_service_t* const service, transaction_set_hash(res->txn, hash_trits); done: - cache_stop(&cache); + cache_stop(); get_trytes_req_free(&get_trytes_req); get_trytes_res_free(&get_trytes_res); return ret; diff --git a/tests/test_cache.c b/tests/test_cache.c index 9b79c027..a54f6399 100644 --- a/tests/test_cache.c +++ b/tests/test_cache.c @@ -2,35 +2,30 @@ #include "utils/cache.h" void test_cache_del(void) { - cache_t* cache = cache_init(); const char* key = TRYTES_81_1; - cache_del(cache, key); - cache_stop(&cache); + cache_del(key); } void test_cache_get(void) { - cache_t* cache = cache_init(); const char* key = TRYTES_81_1; char res[TRYTES_2673_LEN + 1] = {0}; - cache_get(cache, key, res); + cache_get(key, res); res[TRYTES_2673_LEN] = '\0'; TEST_ASSERT_EQUAL_STRING(res, TRYTES_2673_1); - cache_stop(&cache); } void test_cache_set(void) { - cache_t* cache = cache_init(); const char* key = TRYTES_81_1; const char* value = TRYTES_2673_1; - cache_set(cache, key, value); - cache_stop(&cache); + cache_set(key, value); } int main(void) { UNITY_BEGIN(); - + cache_init(); RUN_TEST(test_cache_set); RUN_TEST(test_cache_get); RUN_TEST(test_cache_del); + cache_stop(); return UNITY_END(); } diff --git a/utils/backend_redis.c b/utils/backend_redis.c index a57aea99..c3fc9c02 100644 --- a/utils/backend_redis.c +++ b/utils/backend_redis.c @@ -6,7 +6,9 @@ typedef struct { redisContext* rc; } connection_private; -#define CONN(c) ((connection_private*)(c->conn)) +#define CONN(c) ((connection_private*)(c.conn)) + +static cache_t cache; /* * Private functions @@ -68,39 +70,33 @@ static status_t redis_set(redisContext* c, const char* const key, * Public functions */ -cache_t* cache_init() { - cache_t* cache = (cache_t*)malloc(sizeof(cache_t)); - if (cache != NULL) { - cache->conn = (connection_private*)malloc(sizeof(connection_private)); - CONN(cache)->rc = redisConnect(REDIS_HOST, REDIS_PORT); - return cache; +bool cache_init() { + cache.conn = (connection_private*)malloc(sizeof(connection_private)); + CONN(cache)->rc = redisConnect(REDIS_HOST, REDIS_PORT); + if (CONN(cache)->rc) { + return true; } - return NULL; + return false; } -void cache_stop(cache_t** cache) { - if (*cache) { - redisFree(CONN((*cache))->rc); +void cache_stop() { + if (CONN(cache)->rc) { + redisFree(CONN(cache)->rc); - if (CONN((*cache))) { - free(CONN((*cache))); + if (CONN(cache)) { + free(CONN(cache)); } - - free(*cache); - *cache = NULL; } } -status_t cache_del(const cache_t* const cache, const char* const key) { +status_t cache_del(const char* const key) { return redis_del(CONN(cache)->rc, key); } -status_t cache_get(const cache_t* const cache, const char* const key, - char* res) { +status_t cache_get(const char* const key, char* res) { return redis_get(CONN(cache)->rc, key, res); } -status_t cache_set(const cache_t* const cache, const char* const key, - const char* const value) { +status_t cache_set(const char* const key, const char* const value) { return redis_set(CONN(cache)->rc, key, value); } diff --git a/utils/cache.h b/utils/cache.h index 198bc9b7..f0ba9253 100644 --- a/utils/cache.h +++ b/utils/cache.h @@ -25,45 +25,40 @@ typedef struct { * Initiate cache module * * @return - * - struct of cache_t on success - * - NULL on error + * - True on success + * - False on error */ -cache_t* cache_init(); +bool cache_init(); /** * Stop interacting with cache module * - * @param cache Data type for Cache module - * * @return NULL */ -void cache_stop(cache_t** cache); +void cache_stop(); /** * Delete certain key-value store from cache * - * @param[in] cache Data type for Cache module * @param[in] key Key string to search * * @return * - SC_OK on success - * - 1 on error + * - non-zero on error */ -status_t cache_del(const cache_t* const cache, const char* const key); +status_t cache_del(const char* const key); /** * Get key-value store from in-memory cache * - * @param[in] cache Data type for Cache module * @param[in] key Key string to search * @param[out] res Result of GET key * * @return * - SC_OK on success - * - 1 on error + * - non-zero on error */ -status_t cache_get(const cache_t* const cache, const char* const key, - char* res); +status_t cache_get(const char* const key, char* res); /** * Set key-value store into in-memory cache @@ -74,10 +69,9 @@ status_t cache_get(const cache_t* const cache, const char* const key, * * @return * - SC_OK on success - * - 1 on error + * - non-zero on error */ -status_t cache_set(const cache_t* const cache, const char* const key, - const char* const value); +status_t cache_set(const char* const key, const char* const value); #ifdef __cplusplus } From 0cfb0ac6a10903e54cbddc3daeee637fe0f3d074 Mon Sep 17 00:00:00 2001 From: Yu Wei Wu Date: Tue, 26 Mar 2019 15:35:43 +0800 Subject: [PATCH 20/29] feat(config): Move cache init/destroy to config --- accelerator/BUILD | 2 +- accelerator/common_core.c | 5 ----- accelerator/config.c | 3 +++ accelerator/config.h | 1 + tests/BUILD | 2 +- tests/test_cache.c | 2 +- tests/test_common.cc | 2 ++ tests/test_define.h | 2 +- utils/BUILD | 1 - utils/backend_redis.c | 5 ++--- utils/cache.h | 9 ++++++++- 11 files changed, 20 insertions(+), 14 deletions(-) diff --git a/accelerator/BUILD b/accelerator/BUILD index 3af2503e..e959d048 100644 --- a/accelerator/BUILD +++ b/accelerator/BUILD @@ -43,7 +43,6 @@ cc_library( ":ta_errors", "//request", "//response", - "//utils:cache", "@com_github_uthash//:uthash", "@entangled//common/model:bundle", "@entangled//utils:time", @@ -57,6 +56,7 @@ cc_library( visibility = ["//visibility:public"], deps = [ ":ta_errors", + "//utils:cache", "//utils:pow", "@entangled//cclient/api", "@entangled//cclient/types", diff --git a/accelerator/common_core.c b/accelerator/common_core.c index 6a9f8b00..2b7c7efa 100644 --- a/accelerator/common_core.c +++ b/accelerator/common_core.c @@ -1,6 +1,5 @@ #include "common_core.h" #include -#include "utils/cache.h" status_t cclient_get_txn_to_approve(const iota_client_service_t* const service, uint8_t const depth, @@ -113,7 +112,6 @@ status_t ta_attach_to_tangle(const attach_to_tangle_req_t* const req, flex_trit_t* elt = NULL; char cache_key[NUM_TRYTES_HASH] = {0}; char cache_value[NUM_TRYTES_SERIALIZED_TRANSACTION] = {0}; - cache_init(); // create bundle bundle_transactions_new(&bundle); @@ -154,7 +152,6 @@ status_t ta_attach_to_tangle(const attach_to_tangle_req_t* const req, } done: - cache_stop(); bundle_transactions_free(&bundle); return ret; } @@ -355,7 +352,6 @@ status_t ta_get_transaction_object(const iota_client_service_t* const service, char cache_value[FLEX_TRIT_SIZE_8019] = {0}; // get raw transaction data of transaction hashes - cache_init(); get_trytes_req_t* get_trytes_req = get_trytes_req_new(); get_trytes_res_t* get_trytes_res = get_trytes_res_new(); if (get_trytes_req == NULL || get_trytes_res == NULL) { @@ -406,7 +402,6 @@ status_t ta_get_transaction_object(const iota_client_service_t* const service, transaction_set_hash(res->txn, hash_trits); done: - cache_stop(); get_trytes_req_free(&get_trytes_req); get_trytes_res_free(&get_trytes_res); return ret; diff --git a/accelerator/config.c b/accelerator/config.c index daac92a1..c22ac709 100644 --- a/accelerator/config.c +++ b/accelerator/config.c @@ -43,6 +43,8 @@ status_t ta_config_init(ta_config_t* const info, iota_config_t* const tangle, pow_init(); + cache_init(REDIS_HOST, REDIS_PORT); + return ret; } @@ -52,5 +54,6 @@ void ta_config_destroy(iota_client_service_t* const service) { iota_client_core_destroy(service); pow_destroy(); + cache_stop(); logger_helper_release(logger_id); } diff --git a/accelerator/config.h b/accelerator/config.h index a61c8adc..0348f7d9 100644 --- a/accelerator/config.h +++ b/accelerator/config.h @@ -5,6 +5,7 @@ #include "cclient/api/core/core_api.h" #include "cclient/api/extended/extended_api.h" #include "cclient/types/types.h" +#include "utils/cache.h" #include "utils/pow.h" #ifdef __cplusplus diff --git a/tests/BUILD b/tests/BUILD index abc6e349..95c38883 100644 --- a/tests/BUILD +++ b/tests/BUILD @@ -86,7 +86,7 @@ cc_library( name = "test_define", hdrs = ["test_define.h"], deps = [ - "@entangled//cclient/types", + "//accelerator:ta_config", "@unity", ], ) diff --git a/tests/test_cache.c b/tests/test_cache.c index a54f6399..daaf6c88 100644 --- a/tests/test_cache.c +++ b/tests/test_cache.c @@ -22,7 +22,7 @@ void test_cache_set(void) { int main(void) { UNITY_BEGIN(); - cache_init(); + cache_init(REDIS_HOST, REDIS_PORT); RUN_TEST(test_cache_set); RUN_TEST(test_cache_get); RUN_TEST(test_cache_del); diff --git a/tests/test_common.cc b/tests/test_common.cc index 68d7de27..b5e44f3c 100644 --- a/tests/test_common.cc +++ b/tests/test_common.cc @@ -194,6 +194,8 @@ TEST(GetBundleTest, RetreiveBundleTest) { } int main(int argc, char** argv) { + // GTest manage to cleanup after testing, so only need to initialize here + cache_init(REDIS_HOST, REDIS_PORT); ::testing::GTEST_FLAG(throw_on_failure) = true; ::testing::InitGoogleMock(&argc, argv); return RUN_ALL_TESTS(); diff --git a/tests/test_define.h b/tests/test_define.h index 86674d14..2a10db2e 100644 --- a/tests/test_define.h +++ b/tests/test_define.h @@ -2,7 +2,7 @@ #define TESTS_TEST_DEFINE_H_ #include -#include "cclient/types/types.h" +#include "accelerator/config.h" #ifdef __cplusplus extern "C" { diff --git a/utils/BUILD b/utils/BUILD index de8bf999..eac468e1 100644 --- a/utils/BUILD +++ b/utils/BUILD @@ -5,7 +5,6 @@ cc_library( srcs = ["backend_redis.c"], hdrs = ["cache.h"], deps = [ - "//accelerator:ta_config", "//accelerator:ta_errors", "//third_party:hiredis", "@entangled//cclient/types", diff --git a/utils/backend_redis.c b/utils/backend_redis.c index c3fc9c02..49acce38 100644 --- a/utils/backend_redis.c +++ b/utils/backend_redis.c @@ -1,4 +1,3 @@ -#include "accelerator/config.h" #include "cache.h" #include "third_party/hiredis/hiredis.h" @@ -70,9 +69,9 @@ static status_t redis_set(redisContext* c, const char* const key, * Public functions */ -bool cache_init() { +bool cache_init(const char* host, int port) { cache.conn = (connection_private*)malloc(sizeof(connection_private)); - CONN(cache)->rc = redisConnect(REDIS_HOST, REDIS_PORT); + CONN(cache)->rc = redisConnect(host, port); if (CONN(cache)->rc) { return true; } diff --git a/utils/cache.h b/utils/cache.h index f0ba9253..52744c30 100644 --- a/utils/cache.h +++ b/utils/cache.h @@ -8,6 +8,10 @@ #include "accelerator/errors.h" #include "cclient/types/types.h" +#ifdef __cplusplus +extern "C" { +#endif + /** * @file cache.h * @brief Implementation of cache interface @@ -24,11 +28,14 @@ typedef struct { /** * Initiate cache module * + * @param[in] host cache server host + * @param[in] port cache server port + * * @return * - True on success * - False on error */ -bool cache_init(); +bool cache_init(const char* host, int port); /** * Stop interacting with cache module From d7fa966ed1127721bf3a9a5485474663f4d16b73 Mon Sep 17 00:00:00 2001 From: Yu Wei Wu Date: Tue, 26 Mar 2019 21:03:47 +0800 Subject: [PATCH 21/29] fix(config): Add logger for utils initializing --- accelerator/config.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/accelerator/config.c b/accelerator/config.c index c22ac709..97c78eaf 100644 --- a/accelerator/config.c +++ b/accelerator/config.c @@ -41,8 +41,10 @@ status_t ta_config_init(ta_config_t* const info, iota_config_t* const tangle, } iota_client_extended_init(); + log_info(logger_id, "Initializing PoW implementation context\n"); pow_init(); + log_info(logger_id, "Initializing cache connection\n"); cache_init(REDIS_HOST, REDIS_PORT); return ret; From 7407a39f3ae52e552c9b7b67d09ecd7892ea2af6 Mon Sep 17 00:00:00 2001 From: jkrvivian Date: Mon, 25 Mar 2019 21:39:30 +0800 Subject: [PATCH 22/29] feat(test): Implement time utility functions --- tests/driver.c | 78 +++++++++++++++++++------------------------------- 1 file changed, 29 insertions(+), 49 deletions(-) diff --git a/tests/driver.c b/tests/driver.c index af77973a..e59d7607 100644 --- a/tests/driver.c +++ b/tests/driver.c @@ -23,20 +23,29 @@ double diff_time(struct timespec start, struct timespec end) { return (diff.tv_sec + diff.tv_nsec / 1000000000.0); } +void test_time_start(struct timespec* start) { + clock_gettime(CLOCK_REALTIME, start); +} + +void test_time_end(struct timespec* start, struct timespec* end, double* sum) { + clock_gettime(CLOCK_REALTIME, end); + double difference = diff_time(*start, *end); +#if defined(ENABLE_STAT) + printf("%lf\n", difference); +#endif + *sum += difference; +} + void test_generate_address(void) { char* json_result; double sum = 0; for (size_t count = 0; count < TEST_COUNT; count++) { - clock_gettime(CLOCK_REALTIME, &start_time); + test_time_start(&start_time); TEST_ASSERT_FALSE( api_generate_address(&ta_core.tangle, &ta_core.service, &json_result)); - clock_gettime(CLOCK_REALTIME, &end_time); + test_time_end(&start_time, &end_time, &sum); free(json_result); -#if defined(ENABLE_STAT) - printf("%lf\n", diff_time(start_time, end_time)); -#endif - sum += diff_time(start_time, end_time); } printf("Average time of generate_address: %lf\n", sum / TEST_COUNT); } @@ -46,15 +55,11 @@ void test_get_tips_pair(void) { double sum = 0; for (size_t count = 0; count < TEST_COUNT; count++) { - clock_gettime(CLOCK_REALTIME, &start_time); + test_time_start(&start_time); TEST_ASSERT_FALSE( api_get_tips_pair(&ta_core.tangle, &ta_core.service, &json_result)); - clock_gettime(CLOCK_REALTIME, &end_time); + test_time_end(&start_time, &end_time, &sum); free(json_result); -#if defined(ENABLE_STAT) - printf("%lf\n", diff_time(start_time, end_time)); -#endif - sum += diff_time(start_time, end_time); } printf("Average time of get_tips_pair: %lf\n", sum / TEST_COUNT); } @@ -64,14 +69,10 @@ void test_get_tips(void) { double sum = 0; for (size_t count = 0; count < TEST_COUNT; count++) { - clock_gettime(CLOCK_REALTIME, &start_time); + test_time_start(&start_time); TEST_ASSERT_FALSE(api_get_tips(&ta_core.service, &json_result)); - clock_gettime(CLOCK_REALTIME, &end_time); + test_time_end(&start_time, &end_time, &sum); free(json_result); -#if defined(ENABLE_STAT) - printf("%lf\n", diff_time(start_time, end_time)); -#endif - sum += diff_time(start_time, end_time); } printf("Average time of get_tips: %lf\n", sum / TEST_COUNT); } @@ -86,15 +87,11 @@ void test_send_transfer(void) { double sum = 0; for (size_t count = 0; count < TEST_COUNT; count++) { - clock_gettime(CLOCK_REALTIME, &start_time); + test_time_start(&start_time); TEST_ASSERT_FALSE(api_send_transfer(&ta_core.tangle, &ta_core.service, json, &json_result)); - clock_gettime(CLOCK_REALTIME, &end_time); + test_time_end(&start_time, &end_time, &sum); free(json_result); -#if defined(ENABLE_STAT) - printf("%lf\n", diff_time(start_time, end_time)); -#endif - sum += diff_time(start_time, end_time); } printf("Average time of send_transfer: %lf\n", sum / TEST_COUNT); } @@ -103,17 +100,12 @@ void test_get_transaction_object(void) { char* json_result; double sum = 0; - clock_gettime(CLOCK_REALTIME, &start_time); for (size_t count = 0; count < TEST_COUNT; count++) { - clock_gettime(CLOCK_REALTIME, &start_time); + test_time_start(&start_time); TEST_ASSERT_FALSE(api_get_transaction_object(&ta_core.service, TRYTES_81_3, &json_result)); - clock_gettime(CLOCK_REALTIME, &end_time); + test_time_end(&start_time, &end_time, &sum); free(json_result); -#if defined(ENABLE_STAT) - printf("%lf\n", diff_time(start_time, end_time)); -#endif - sum += diff_time(start_time, end_time); } printf("Average time of get_transaction_object: %lf\n", sum / TEST_COUNT); } @@ -123,15 +115,11 @@ void test_find_transactions_by_tag(void) { double sum = 0; for (size_t count = 0; count < TEST_COUNT; count++) { - clock_gettime(CLOCK_REALTIME, &start_time); + test_time_start(&start_time); TEST_ASSERT_FALSE(api_find_transactions_by_tag(&ta_core.service, FIND_TAG_MSG, &json_result)); - clock_gettime(CLOCK_REALTIME, &end_time); + test_time_end(&start_time, &end_time, &sum); free(json_result); -#if defined(ENABLE_STAT) - printf("%lf\n", diff_time(start_time, end_time)); -#endif - sum += diff_time(start_time, end_time); } printf("Average time of find_transactions_by_tag: %lf\n", sum / TEST_COUNT); } @@ -141,15 +129,11 @@ void test_find_transactions_obj_by_tag(void) { double sum = 0; for (size_t count = 0; count < TEST_COUNT; count++) { - clock_gettime(CLOCK_REALTIME, &start_time); + test_time_start(&start_time); TEST_ASSERT_FALSE(api_find_transactions_obj_by_tag( &ta_core.service, FIND_TAG_MSG, &json_result)); - clock_gettime(CLOCK_REALTIME, &end_time); + test_time_end(&start_time, &end_time, &sum); free(json_result); -#if defined(ENABLE_STAT) - printf("%lf\n", diff_time(start_time, end_time)); -#endif - sum += diff_time(start_time, end_time); } printf("Average time of find_tx_obj_by_tag: %lf\n", sum / TEST_COUNT); } @@ -159,15 +143,11 @@ void test_receive_mam_message(void) { double sum = 0; for (size_t count = 0; count < TEST_COUNT; count++) { - clock_gettime(CLOCK_REALTIME, &start_time); + test_time_start(&start_time); TEST_ASSERT_FALSE( api_receive_mam_message(&ta_core.service, BUNDLE_HASH, &json_result)); - clock_gettime(CLOCK_REALTIME, &end_time); + test_time_end(&start_time, &end_time, &sum); free(json_result); -#if defined(ENABLE_STAT) - printf("%lf\n", diff_time(start_time, end_time)); -#endif - sum += diff_time(start_time, end_time); } printf("Average time of receive_mam_message: %lf\n", sum / TEST_COUNT); } From 15e5e433778c2308116dd0fd589cae6717a245d0 Mon Sep 17 00:00:00 2001 From: YangHau Date: Wed, 27 Mar 2019 01:29:07 +0800 Subject: [PATCH 23/29] feat: Synchronize with entangled for MAM APIs In order to implement api_receive_mam_message(), some functions implemented in the later commits are necessary. --- WORKSPACE | 4 ++-- accelerator/common_core.c | 6 ++---- response/ta_get_transaction_object.c | 6 ++++-- tests/iota_api_mock.cc | 2 +- tests/iota_api_mock.hh | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/WORKSPACE b/WORKSPACE index b8dfb207..9a4d5050 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -2,13 +2,13 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository", "new_git_r git_repository( name = "rules_iota", - commit = "b15744b9ea520717752c866d5afc769c3b6b68f3", + commit = "1cb59eea62fd1d071de213a9aa46e61e8273472d", remote = "https://github.com/iotaledger/rules_iota.git", ) git_repository( name = "entangled", - commit = "8d847ffcecd50f8f3760bfee07d7ed33ecc067bf", + commit = "4960865730640d23e75ffbce84d3f74264cfcd28", remote = "https://github.com/iotaledger/entangled.git", ) diff --git a/accelerator/common_core.c b/accelerator/common_core.c index 2b7c7efa..1cdc6b43 100644 --- a/accelerator/common_core.c +++ b/accelerator/common_core.c @@ -198,7 +198,6 @@ status_t ta_send_trytes(const iota_config_t* const tangle, done: ta_get_tips_res_free(&get_txn_res); - attach_req->trytes = NULL; attach_to_tangle_req_free(&attach_req); attach_to_tangle_res_free(&attach_res); return ret; @@ -467,7 +466,7 @@ static int idx_sort(void const* lhs, void const* rhs) { } static void get_first_bundle_from_transactions( - transaction_array_t const transactions, + transaction_array_t const* transactions, bundle_transactions_t* const bundle) { iota_transaction_t* tail = NULL; iota_transaction_t* curr_tx = NULL; @@ -493,9 +492,8 @@ status_t ta_get_bundle(const iota_client_service_t* const service, tryte_t const* const bundle_hash, bundle_transactions_t* const bundle) { status_t ret = SC_OK; - iota_transaction_t* curr_tx = NULL; flex_trit_t bundle_hash_flex[FLEX_TRIT_SIZE_243]; - transaction_array_t tx_objs = transaction_array_new(); + transaction_array_t* tx_objs = transaction_array_new(); find_transactions_req_t* find_tx_req = find_transactions_req_new(); if (find_tx_req == NULL) { ret = SC_CCLIENT_OOM; diff --git a/response/ta_get_transaction_object.c b/response/ta_get_transaction_object.c index 56969e49..fb435fc6 100644 --- a/response/ta_get_transaction_object.c +++ b/response/ta_get_transaction_object.c @@ -11,11 +11,13 @@ ta_get_transaction_object_res_t* ta_get_transaction_object_res_new() { } void ta_get_transaction_object_res_free(ta_get_transaction_object_res_t** res) { - if (!res || !(*res) || !(*res)->txn) { + if (!res || !(*res)) { return; } - transaction_free((*res)->txn); + if ((*res)->txn) { + transaction_free((*res)->txn); + } free(*res); *res = NULL; } diff --git a/tests/iota_api_mock.cc b/tests/iota_api_mock.cc index 70acc1a4..b6e3c37d 100644 --- a/tests/iota_api_mock.cc +++ b/tests/iota_api_mock.cc @@ -41,7 +41,7 @@ retcode_t iota_client_find_transactions( retcode_t iota_client_find_transaction_objects( const iota_client_service_t* const service, - const find_transactions_req_t* const req, transaction_array_t tx_objs) { + const find_transactions_req_t* const req, transaction_array_t* tx_objs) { flex_trit_t tx_trits[FLEX_TRIT_SIZE_8019]; iota_transaction_t tx; diff --git a/tests/iota_api_mock.hh b/tests/iota_api_mock.hh index bb8cd88a..e8e3ffe0 100644 --- a/tests/iota_api_mock.hh +++ b/tests/iota_api_mock.hh @@ -29,7 +29,7 @@ class IotaAPI { } virtual retcode_t iota_client_find_transaction_objects( const iota_client_service_t* const service, - const find_transactions_req_t* const req, transaction_array_t tx_objs) { + const find_transactions_req_t* const req, transaction_array_t* tx_objs) { return RC_OK; } virtual retcode_t iota_client_get_new_address( @@ -71,7 +71,7 @@ class APIMock : public IotaAPI { MOCK_METHOD3(iota_client_find_transaction_objects, retcode_t(const iota_client_service_t* const service, const find_transactions_req_t* const req, - transaction_array_t tx_objs)); + transaction_array_t* tx_objs)); MOCK_METHOD4(iota_client_get_new_address, retcode_t(iota_client_service_t const* const serv, flex_trit_t const* const seed, From 3f000713142e6f743103ad3644f9fd37f0e98050 Mon Sep 17 00:00:00 2001 From: YangHau Date: Tue, 26 Mar 2019 20:02:26 +0800 Subject: [PATCH 24/29] refactor(mam): Implement api_receive_mam_message() Reimplement a new api_receive_mam_message() to fit the needs of MAMv2. Current version of api_receive_mam_message() without arg `chid`. if `chid` --- accelerator/apis.c | 55 +++++++++++++++++++++++---------------- accelerator/apis.h | 6 +++-- accelerator/common_core.c | 6 +++-- accelerator/errors.h | 8 +++++- tests/driver.c | 11 ++++---- tests/test_define.h | 7 +++++ 6 files changed, 60 insertions(+), 33 deletions(-) diff --git a/accelerator/apis.c b/accelerator/apis.c index 44fe2fe8..125ebf92 100644 --- a/accelerator/apis.c +++ b/accelerator/apis.c @@ -154,54 +154,63 @@ status_t api_find_transactions_obj_by_tag( } status_t api_receive_mam_message(const iota_client_service_t* const service, - const char* const obj, char** json_result) { - status_t ret = SC_OK; + const char* const bundle_hash, + char** json_result) { mam_api_t mam; - + status_t ret = SC_OK; tryte_t* payload_trytes = NULL; + tryte_t* none_chid_trytes = NULL; size_t payload_size = 0; bundle_transactions_t* bundle = NULL; bundle_transactions_new(&bundle); bool is_last_packet; // Creating MAM API - if (mam_api_init(&mam, (tryte_t*)SEED)) { - ret = SC_MAM_OOM; + if (mam_api_init(&mam, (tryte_t*)SEED) != RC_OK) { + ret = SC_MAM_FAILED_INIT; goto done; } - // Get bundle which is find_transactions_by_bundle - ret = ta_get_bundle(service, (tryte_t*)obj, bundle); - if (ret) { + ret = ta_get_bundle(service, (tryte_t*)bundle_hash, bundle); + if (ret != SC_OK) { goto done; } - // Read MAM message from bundle + // Set first transaction's address as chid, if no `chid` specified mam_psk_t_set_add(&mam.psks, &psk); + iota_transaction_t* curr_tx = (iota_transaction_t*)utarray_eltptr(bundle, 0); + none_chid_trytes = (tryte_t*)malloc(sizeof(tryte_t) * NUM_TRYTES_ADDRESS); + flex_trits_to_trytes(none_chid_trytes, NUM_TRYTES_ADDRESS, + transaction_address(curr_tx), NUM_TRITS_ADDRESS, + NUM_TRITS_ADDRESS); + mam_api_add_trusted_channel_pk(&mam, none_chid_trytes); + if (mam_api_bundle_read(&mam, bundle, &payload_trytes, &payload_size, &is_last_packet) == RC_OK) { if (payload_trytes == NULL || payload_size == 0) { - ret = SC_MAM_NULL; + ret = SC_MAM_NO_PAYLOAD; + goto done; } else { - char* payload = calloc(payload_size * 2 + 1, sizeof(char)); - - trytes_to_ascii(payload_trytes, payload_size, payload); - if (payload == NULL) { - ret = SC_MAM_NOT_FOUND; - goto done; - } - *json_result = payload; - - payload = NULL; - free(payload_trytes); + *(json_result) = calloc(payload_size * 2 + 1, sizeof(char)); + trytes_to_ascii(payload_trytes, payload_size, *(json_result)); } } else { - ret = SC_MAM_FAILED_RESPONSE; + ret = SC_MAM_NOT_FOUND; + goto done; } + // Cleanup done: - mam_api_destroy(&mam); + // Destroying MAM API + if (ret != SC_MAM_FAILED_INIT) { + if (mam_api_destroy(&mam) != RC_OK) { + ret = SC_MAM_FAILED_DESTROYED; + } + } + free(none_chid_trytes); + free(payload_trytes); bundle_transactions_free(&bundle); + return ret; } diff --git a/accelerator/apis.h b/accelerator/apis.h index 2280ff41..c9be5fe2 100644 --- a/accelerator/apis.h +++ b/accelerator/apis.h @@ -7,6 +7,7 @@ #include "common/trinary/trit_tryte.h" #include "common/trinary/tryte_ascii.h" #include "mam/api/api.h" +#include "mam/mam/mam_channel_t_set.h" #include "serializer/serializer.h" #ifdef __cplusplus @@ -80,7 +81,7 @@ status_t api_get_tips(const iota_client_service_t* const service, * Receive a MAM message from given bundle hash. * * @param[in] service IRI node end point service - * @param[out] obj bundle hash in trytes + * @param[in] bundle_hash bundle hash decoded in trytes string * @param[out] json_result Result containing an unused address in json format * * @return @@ -88,7 +89,8 @@ status_t api_get_tips(const iota_client_service_t* const service, * - non-zero on error */ status_t api_receive_mam_message(const iota_client_service_t* const service, - const char* const obj, char** json_result); + const char* const bundle_hash, + char** json_result); /** * @brief Send transfer to tangle. diff --git a/accelerator/common_core.c b/accelerator/common_core.c index 1cdc6b43..1cedbc68 100644 --- a/accelerator/common_core.c +++ b/accelerator/common_core.c @@ -175,7 +175,6 @@ status_t ta_send_trytes(const iota_config_t* const tangle, } // attach to tangle - memcpy(attach_req->trunk, hash243_stack_peek(get_txn_res->tips), FLEX_TRIT_SIZE_243); hash243_stack_pop(&get_txn_res->tips); @@ -183,7 +182,10 @@ status_t ta_send_trytes(const iota_config_t* const tangle, FLEX_TRIT_SIZE_243); hash243_stack_pop(&get_txn_res->tips); attach_req->mwm = tangle->mwm; - attach_req->trytes = trytes; + + flex_trit_t* elt = NULL; + HASH_ARRAY_FOREACH(trytes, elt) { hash_array_push(attach_req->trytes, elt); } + ret = ta_attach_to_tangle(attach_req, attach_res); if (ret) { goto done; diff --git a/accelerator/errors.h b/accelerator/errors.h index afa86e22..e191e750 100644 --- a/accelerator/errors.h +++ b/accelerator/errors.h @@ -95,8 +95,14 @@ typedef enum { /**< NULL object in mam */ SC_MAM_NOT_FOUND = 0x03 | SC_MODULE_MAM | SC_SEVERITY_FATAL, /**< Empty result from mam */ - SC_MAM_FAILED_RESPONSE = 0x04 | SC_MODULE_MAM | SC_SEVERITY_FATAL, + SC_MAM_FAILED_INIT = 0x04 | SC_MODULE_MAM | SC_SEVERITY_FATAL, + /**< Error in mam initialization */ + SC_MAM_FAILED_RESPONSE = 0x05 | SC_MODULE_MAM | SC_SEVERITY_FATAL, /**< Error in mam response */ + SC_MAM_FAILED_DESTROYED = 0x06 | SC_MODULE_MAM | SC_SEVERITY_FATAL, + /**< Error in mam destroy */ + SC_MAM_NO_PAYLOAD = 0x07 | SC_MODULE_MAM | SC_SEVERITY_FATAL, + /**< No payload or no chid */ } status_t; typedef enum { diff --git a/tests/driver.c b/tests/driver.c index e59d7607..c7e2183e 100644 --- a/tests/driver.c +++ b/tests/driver.c @@ -141,12 +141,13 @@ void test_find_transactions_obj_by_tag(void) { void test_receive_mam_message(void) { char* json_result; double sum = 0; - + status_t ret = SC_OK; for (size_t count = 0; count < TEST_COUNT; count++) { - test_time_start(&start_time); - TEST_ASSERT_FALSE( - api_receive_mam_message(&ta_core.service, BUNDLE_HASH, &json_result)); - test_time_end(&start_time, &end_time, &sum); + clock_gettime(CLOCK_REALTIME, &start_time); + ret = api_receive_mam_message(&ta_core.service, TEST_BUNDLE_HASH, + &json_result); + TEST_ASSERT_EQUAL_INT32(ret, SC_OK); + clock_gettime(CLOCK_REALTIME, &end_time); free(json_result); } printf("Average time of receive_mam_message: %lf\n", sum / TEST_COUNT); diff --git a/tests/test_define.h b/tests/test_define.h index 6c75aefd..cd9995a2 100644 --- a/tests/test_define.h +++ b/tests/test_define.h @@ -106,6 +106,13 @@ extern "C" { "999999999999999999999999999999999999999999999999999999999999999999999999" \ "999999999999999999999999999" +#define TEST_BUNDLE_HASH \ + "QBFXQETKSHDYPFUDO9ILVCAVQIXOHXKCECZYFLPBNVIX9JUXQZJE9URQEEUWPWYZOIACTCGZX9" \ + "IDIODCA " +#define TEST_CHID \ + "WYEVIWJN9DF9SBQHBUWYUECD9KD9BQHQXHOGQDTVKKYBRQUFQYGOFOTHREGVSKSSEVXMFOEHWN" \ + "KHLHDKQ" + #ifdef __cplusplus } #endif From e0b0bc5cbf00d07069653b0a9c0b4c064c06b6dd Mon Sep 17 00:00:00 2001 From: YangHau Date: Wed, 27 Mar 2019 21:36:01 +0800 Subject: [PATCH 25/29] fix(pow): Fix memory leaks in pow.c If there were some error happened, the older version would return immediately which would cause memory leaks. Therfore, I use `goto` instead of `return` immediately. --- utils/pow.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/utils/pow.c b/utils/pow.c index f5f27334..373ad324 100644 --- a/utils/pow.c +++ b/utils/pow.c @@ -39,12 +39,17 @@ flex_trit_t* ta_pow_flex(const flex_trit_t* const trits_in, const uint8_t mwm) { status_t ta_pow(const bundle_transactions_t* bundle, const flex_trit_t* const trunk, const flex_trit_t* const branch, const uint8_t mwm) { + status_t ret = SC_OK; iota_transaction_t* tx; flex_trit_t* ctrunk = (flex_trit_t*)calloc(FLEX_TRIT_SIZE_243, sizeof(flex_trit_t)); size_t cur_idx = 0; tx = (iota_transaction_t*)utarray_front(bundle); + if (tx == NULL) { + ret = SC_TA_NULL; + goto done; + } cur_idx = transaction_last_index(tx) + 1; memcpy(ctrunk, trunk, FLEX_TRIT_SIZE_243); @@ -60,13 +65,15 @@ status_t ta_pow(const bundle_transactions_t* bundle, flex_trit_t* tx_trits = transaction_serialize(tx); if (tx_trits == NULL) { - return SC_CCLIENT_INVALID_FLEX_TRITS; + ret = SC_CCLIENT_INVALID_FLEX_TRITS; + goto done; } // get nonce flex_trit_t* nonce = ta_pow_flex(tx_trits, mwm); if (nonce == NULL) { - return SC_TA_OOM; + ret = SC_TA_OOM; + goto done; } transaction_set_nonce(tx, nonce); @@ -76,6 +83,7 @@ status_t ta_pow(const bundle_transactions_t* bundle, free(tx_trits); } while (cur_idx != 0); +done: free(ctrunk); - return SC_OK; + return ret; } From 544f3a4490145190f433ec513fee25a76f12b0c7 Mon Sep 17 00:00:00 2001 From: Yu Wei Wu Date: Thu, 28 Mar 2019 10:37:17 +0800 Subject: [PATCH 26/29] fix(README): Add CI and chat badges Version badge will be added in release PR. --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index bff81559..8ad35b3f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Tangle-accelerator +[![Build Status](https://badge.buildkite.com/46ec07b122bde13f984c241fe8b38e64698c5c0d816ee6c7e4.svg)](https://buildkite.com/dltcollab/dcurl-test) ![Gitter](https://img.shields.io/gitter/room/DLTcollab/tangle-accelerator.svg) + `Tangle-accelerator` is a caching proxy server for [IOTA](https://www.iota.org/), which can cache API requests and rewrite their responses as needed to be routed through full nodes. Thus, one instance of `Tangle-accelerator` can serve thousands of Tangle requests From 4e4c2668e672e97e8beb13a204e36eb0277e005b Mon Sep 17 00:00:00 2001 From: Wu Yu Wei Date: Thu, 28 Mar 2019 21:18:00 +0800 Subject: [PATCH 27/29] docs: Update badges --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8ad35b3f..2a5cd048 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # Tangle-accelerator -[![Build Status](https://badge.buildkite.com/46ec07b122bde13f984c241fe8b38e64698c5c0d816ee6c7e4.svg)](https://buildkite.com/dltcollab/dcurl-test) ![Gitter](https://img.shields.io/gitter/room/DLTcollab/tangle-accelerator.svg) +[![Build Status](https://badge.buildkite.com/46ec07b122bde13f984c241fe8b38e64698c5c0d816ee6c7e4.svg)](https://buildkite.com/dltcollab/dcurl-test) [![Gitter](https://img.shields.io/gitter/room/DLTcollab/tangle-accelerator.svg)](https://gitter.im/DLTcollab/tangle-accelerator) ![GitHub release](https://img.shields.io/github/release-pre/DLTcollab/tangle-accelerator.svg) `Tangle-accelerator` is a caching proxy server for [IOTA](https://www.iota.org/), which can cache API requests and rewrite their responses as needed to be routed through full From 8f62edf32771b78e4619a3918da95f55e1c7b49d Mon Sep 17 00:00:00 2001 From: Yu Wei Wu Date: Thu, 28 Mar 2019 21:25:00 +0800 Subject: [PATCH 28/29] feat(dcurl): Update dcurl version Update dcurl version to v0.2.0 and edit Makefile to force dcurl pin to certain version in the release. --- Makefile | 1 - third_party/dcurl | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 3ea81037..01e845cc 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,6 @@ all: $(DEPS) $(DCURL_LIB): $(DCURL_DIR) git submodule update --init $^ - git submodule update --remote $^ $(MAKE) -C $^ config @echo $(info Modify $^/build/local.mk for your environments.) diff --git a/third_party/dcurl b/third_party/dcurl index e5bbddde..778dcdbf 160000 --- a/third_party/dcurl +++ b/third_party/dcurl @@ -1 +1 @@ -Subproject commit e5bbdddeec33504bbfd2c649b2c4c85d233f0fd6 +Subproject commit 778dcdbf40967ff957415e81b5a207567cca5985 From f18a22382fdaef3e357aa59fec82d7ab0df652fc Mon Sep 17 00:00:00 2001 From: Yu Wei Wu Date: Fri, 29 Mar 2019 23:44:58 +0800 Subject: [PATCH 29/29] feat(serializer): Add receive_mam_message serialize --- accelerator/apis.c | 13 ++++++++++--- serializer/serializer.c | 21 +++++++++++++++++++++ serializer/serializer.h | 11 +++++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/accelerator/apis.c b/accelerator/apis.c index 125ebf92..b1707583 100644 --- a/accelerator/apis.c +++ b/accelerator/apis.c @@ -160,6 +160,7 @@ status_t api_receive_mam_message(const iota_client_service_t* const service, status_t ret = SC_OK; tryte_t* payload_trytes = NULL; tryte_t* none_chid_trytes = NULL; + char* payload = NULL; size_t payload_size = 0; bundle_transactions_t* bundle = NULL; bundle_transactions_new(&bundle); @@ -191,15 +192,20 @@ status_t api_receive_mam_message(const iota_client_service_t* const service, ret = SC_MAM_NO_PAYLOAD; goto done; } else { - *(json_result) = calloc(payload_size * 2 + 1, sizeof(char)); - trytes_to_ascii(payload_trytes, payload_size, *(json_result)); + payload = calloc(payload_size * 2 + 1, sizeof(char)); + if (payload == NULL) { + ret = SC_TA_NULL; + goto done; + } + trytes_to_ascii(payload_trytes, payload_size, payload); } } else { ret = SC_MAM_NOT_FOUND; goto done; } - // Cleanup + ret = receive_mam_message_serialize(json_result, &payload); + done: // Destroying MAM API if (ret != SC_MAM_FAILED_INIT) { @@ -209,6 +215,7 @@ status_t api_receive_mam_message(const iota_client_service_t* const service, } free(none_chid_trytes); free(payload_trytes); + free(payload); bundle_transactions_free(&bundle); return ret; diff --git a/serializer/serializer.c b/serializer/serializer.c index 6dd26114..fb38b94d 100644 --- a/serializer/serializer.c +++ b/serializer/serializer.c @@ -386,3 +386,24 @@ status_t ta_find_transactions_obj_res_serialize( cJSON_Delete(json_root); return ret; } + +status_t receive_mam_message_serialize(char** obj, const char** res) { + status_t ret = SC_OK; + cJSON* json_root = cJSON_CreateObject(); + if (json_root == NULL) { + ret = SC_SERIALIZER_JSON_CREATE; + goto done; + } + + cJSON_AddStringToObject(json_root, "message", *res); + + *obj = cJSON_PrintUnformatted(json_root); + if (*obj == NULL) { + ret = SC_SERIALIZER_JSON_PARSE; + goto done; + } + +done: + cJSON_Delete(json_root); + return ret; +} diff --git a/serializer/serializer.h b/serializer/serializer.h index abc578f4..bca7d18e 100644 --- a/serializer/serializer.h +++ b/serializer/serializer.h @@ -111,6 +111,17 @@ status_t ta_find_transactions_res_serialize( status_t ta_find_transactions_obj_res_serialize( char** obj, const ta_find_transactions_obj_res_t* const res); +/** + * @brief Serialize mam message + * + * @param[out] obj message formed in JSON + * @param[in] res Response of payload message + * + * @return + * - SC_OK on success + * - non-zero on error + */ +status_t receive_mam_message_serialize(char** obj, const char** res); #ifdef __cplusplus } #endif