Skip to content
This repository has been archived by the owner on Dec 26, 2022. It is now read-only.

Commit

Permalink
feat(mam): Return UUID as the repsonse of send MAM message
Browse files Browse the repository at this point in the history
Tangle-accelerator would respond an UUID for every `send_mam_message()`
request. The requests would be buffered, and they will be published
with another background thread which is in `periodical_task.[c, h]`
  • Loading branch information
howjmay committed Sep 21, 2020
1 parent 091aa58 commit 7c4e8f0
Show file tree
Hide file tree
Showing 22 changed files with 631 additions and 326 deletions.
1 change: 1 addition & 0 deletions accelerator/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ cc_binary(
":ta_config",
":ta_cli",
"//connectivity/http",
"//accelerator/core:periodical_task",
"@org_iota_common//utils/handles:signal",
] + select({
":mqtt_enable": [
Expand Down
4 changes: 2 additions & 2 deletions accelerator/cli_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ typedef enum ta_cli_arg_value_e {
HEALTH_TRACK_PERIOD,
NO_GTTA,
BUFFER_LIST,
DONE_LIST,
COMPLETE_LIST,
HTTP_THREADS_CLI,
CACHE_CAPACITY,

Expand Down Expand Up @@ -95,7 +95,7 @@ static struct ta_cli_argument_s {
"The period for checking IOTA full node host connection status"},
{"no-gtta", no_argument, NULL, NO_GTTA, "Disable getTransactionToConfirm (gTTA) when sending transaction"},
{"buffer_list", required_argument, NULL, BUFFER_LIST, "Set the value of `buffer_list_name`"},
{"done_list", required_argument, NULL, DONE_LIST, "Set the value of `done_list_name`"},
{"complete_list", required_argument, NULL, COMPLETE_LIST, "Specify the name of complete list`"},
{"cache_capacity", required_argument, NULL, CACHE_CAPACITY, "Set the maximum capacity of caching server"},
{"quiet", no_argument, NULL, QUIET, "Disable logger"},
{"runtime_cli", no_argument, NULL, RUNTIME_CLI, "Enable runtime command line"},
Expand Down
11 changes: 6 additions & 5 deletions accelerator/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
ta_log_error("Malformed input character\n");
}
break;
case CACHE_MAX_CAPACITY:
case CACHE_CAPACITY:
strtol_temp = strtol(value, NULL, 10);
if (strtol_p != value && errno != ERANGE && strtol_temp >= INT_MIN && strtol_temp <= INT_MAX) {
if (strtol_temp <= 0) {
Expand Down Expand Up @@ -265,8 +265,8 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
case BUFFER_LIST:
cache->buffer_list_name = value;
break;
case DONE_LIST:
cache->done_list_name = value;
case COMPLETE_LIST:
cache->complete_list_name = value;
break;

// Command line options configuration
Expand Down Expand Up @@ -342,8 +342,9 @@ status_t ta_core_default_init(ta_core_t* const core) {
cache->host = REDIS_HOST;
cache->port = REDIS_PORT;
cache->state = false;
cache->buffer_list_name = BUFFER_LIST_NAME;
cache->done_list_name = DONE_LIST_NAME;
cache->complete_list_name = COMPLETE_LIST_NAME;
cache->mam_buffer_list_name = MAM_BUFFER_LIST_NAME;
cache->mam_complete_list_name = MAM_COMPLETE_LIST_NAME;
cache->capacity = CACHE_MAX_CAPACITY;

ta_log_info("Initializing IOTA full node configuration\n");
Expand Down
22 changes: 13 additions & 9 deletions accelerator/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ extern "C" {
#define DB_HOST "localhost"
#define MAM_FILE_PREFIX "/tmp/mam_bin_XXXXXX"
#define BUFFER_LIST_NAME "txn_buff_list"
#define DONE_LIST_NAME "done_txn_buff_list"
#define COMPLETE_LIST_NAME "complete_txn_buff_list"
#define MAM_BUFFER_LIST_NAME "mam_buff_list"
#define MAM_COMPLETE_LIST_NAME "complete_mam_buff_list"
#define CACHE_MAX_CAPACITY \
170 * 1024 * 1024 /**< Default cache server maximum capacity. It is set to 170MB by default. */
#define HEALTH_TRACK_PERIOD 1800 /**< Check every half hour in default */
Expand Down Expand Up @@ -113,14 +115,16 @@ typedef struct iota_config_s {

/** struct type of accelerator cache */
typedef struct ta_cache_s {
char* host; /**< Binding address of redis server */
uint64_t timeout; /**< Timeout for keys in cache server */
char* buffer_list_name; /**< Name of the list to buffer transactions */
char* done_list_name; /**< Name of the list to store successfully broadcast transactions from buffer */
uint16_t port; /**< Binding port of redis server */
bool state; /**< Set it true to turn on cache server */
long int capacity; /**< The maximum capacity of cache server */
pthread_rwlock_t* rwlock; /**< Read/Write lock to avoid data racing in buffering */
char* host; /**< Binding address of redis server */
uint64_t timeout; /**< Timeout for keys in cache server */
char* buffer_list_name; /**< Name of the list to buffer transactions */
char* complete_list_name; /**< Name of the list to store successfully broadcast transactions from buffer */
char* mam_buffer_list_name; /**< Name of the list to buffer MAM requests UUID */
char* mam_complete_list_name; /**< Name of the list to successfullay published MAM requests */
uint16_t port; /**< Binding port of redis server */
bool state; /**< Set it true to turn on cache server */
long int capacity; /**< The maximum capacity of cache server */
pthread_rwlock_t* rwlock; /**< Read/Write lock to avoid data racing in buffering */
} ta_cache_t;

/** struct type of accelerator core */
Expand Down
16 changes: 16 additions & 0 deletions accelerator/core/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,19 @@ cc_library(
"@org_iota_common//utils:time",
],
)

cc_library(
name = "periodical_task",
srcs = ["periodical_task.c"],
hdrs = ["periodical_task.h"],
linkopts = [
"-lpthread",
],
visibility = ["//visibility:public"],
deps = [
":core",
":mam_core",
"//common:ta_errors",
"//common:ta_logger",
],
)
33 changes: 25 additions & 8 deletions accelerator/core/apis.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,32 +214,49 @@ status_t api_recv_mam_message(const iota_config_t* const iconf, const iota_clien
return ret;
}

status_t api_send_mam_message(const ta_config_t* const info, const iota_config_t* const iconf,
const iota_client_service_t* const service, char const* const obj, char** json_result) {
status_t api_send_mam_message(const ta_cache_t* const cache, char const* const obj, char** json_result) {
status_t ret = SC_OK;
ta_send_mam_req_t* req = send_mam_req_new();
ta_send_mam_res_t* res = send_mam_res_new();

if (send_mam_message_req_deserialize(obj, req)) {
ret = SC_MAM_FAILED_INIT;
ret = send_mam_message_req_deserialize(obj, req);
if (ret) {
ta_log_error("%s\n", ta_error_to_string(ret));
goto done;
}

// Generate UUID
char uuid[UUID_STR_LEN];
uuid_t bin_uuid;
uuid_generate_random(bin_uuid);
uuid_unparse(bin_uuid, uuid);
if (!uuid[0]) {
ta_log_error("%s\n", "Failed to generate UUID");
goto done;
}

// TODO Generate the address that TA can quickly generate from the SEED with given parameters.

// Buffer send_mam_req_t object for publishing it later
ret = cache_set(uuid, UUID_STR_LEN - 1, obj, strlen(obj), cache->timeout);
if (ret != SC_OK) {
ta_log_error("%s\n", ta_error_to_string(ret));
goto done;
}

ret = ta_send_mam_message(info, iconf, service, req, res);
// Buffer the UUID to the list
ret = cache_list_push(cache->mam_buffer_list_name, strlen(cache->mam_buffer_list_name), uuid, UUID_STR_LEN - 1);
if (ret != SC_OK) {
ta_log_error("%s\n", ta_error_to_string(ret));
goto done;
}

ret = send_mam_message_res_serialize(res, json_result);
ret = send_mam_message_res_serialize(NULL, uuid, json_result);
if (ret != SC_OK) {
ta_log_error("%s\n", ta_error_to_string(ret));
}

done:
send_mam_req_free(&req);
send_mam_res_free(&res);
return ret;
}

Expand Down
3 changes: 1 addition & 2 deletions accelerator/core/apis.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ status_t api_recv_mam_message(const iota_config_t* const iconf, const iota_clien
* - SC_OK on success
* - non-zero on error
*/
status_t api_send_mam_message(const ta_config_t* const info, const iota_config_t* const iconf,
const iota_client_service_t* const service, char const* const obj, char** json_result);
status_t api_send_mam_message(const ta_cache_t* const cache, char const* const obj, char** json_result);

/**
* @brief Send transfer to tangle.
Expand Down
Loading

0 comments on commit 7c4e8f0

Please sign in to comment.