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

Commit

Permalink
fix(core): integrate conflicting IOTA conf and fix incorrect TA conf
Browse files Browse the repository at this point in the history
Remove CLI option `iri_address` that will overwrite options `iri_host` and `iri_port`.

Make options  `iri_host` and  `iri_port` support multiple addresses separated by comma.
For example `./accelerator --iri_host  host1,host2, ...`

Correct the misused TA binding port and address.
Correct error casting and range checking of CLI options.

Close #583 #584
  • Loading branch information
YingHan-Chen committed Apr 23, 2020
1 parent 0d1a082 commit 616856a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 61 deletions.
62 changes: 21 additions & 41 deletions accelerator/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
#endif
char* strtol_p = NULL;
long int strtol_temp;
uint8_t idx;
switch (key) {
// TA configuration
case TA_HOST_CLI:
Expand All @@ -67,57 +68,33 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
break;
case TA_THREAD_COUNT_CLI:
strtol_temp = strtol(value, &strtol_p, 10);
if (strtol_p != value && errno != ERANGE && strtol_temp >= INT_MIN && strtol_temp <= INT_MAX) {
ta_conf->thread_count = (int)strtol_temp;
if (strtol_p != value && errno != ERANGE && strtol_temp >= 0 && strtol_temp <= UCHAR_MAX) {
ta_conf->thread_count = (uint8_t)strtol_temp;
} else {
ta_log_error("Malformed input or illegal input character\n");
}
break;

// IRI configuration
case IRI_HOST_CLI:
iota_service->http.host = value;
break;
case IRI_PORT_CLI:
strtol_temp = strtol(value, &strtol_p, 10);
if (strtol_p != value && errno != ERANGE && strtol_temp >= INT_MIN && strtol_temp <= INT_MAX) {
iota_service->http.port = (int)strtol_temp;
} else {
ta_log_error("Malformed input or illegal input character\n");
idx = 0;
for (char* p = strtok(value, ","); p && idx < MAX_IRI_LIST_ELEMENTS; p = strtok(NULL, ","), idx++) {
ta_conf->iota_host_list[idx] = p;
}
iota_service->http.host = ta_conf->iota_host_list[0];
break;
case IRI_ADDRESS_CLI:
for (int i = 0; i < MAX_IRI_LIST_ELEMENTS; i++) {
if (!ta_conf->host_list[i]) {
char *host, *port;
host = strtok(value, ":");
port = strtok(NULL, "");
if (!port) {
ta_log_error("Malformed input or illegal input character\n");
break;
}

if (i == 0) {
iota_service->http.host = host;
strtol_temp = strtol(port, NULL, 10);
if (strtol_p != value && errno != ERANGE && strtol_temp >= INT_MIN && strtol_temp <= INT_MAX) {
iota_service->http.port = (int)strtol_temp;
} else {
ta_log_error("Malformed input or illegal input character\n");
}
}
ta_conf->host_list[i] = host;
strtol_temp = strtol(port, NULL, 10);
if (strtol_p != value && errno != ERANGE && strtol_temp >= INT_MIN && strtol_temp <= INT_MAX) {
ta_conf->port_list[i] = (int)strtol_temp;
} else {
ta_log_error("Malformed input or illegal input character\n");
}
break;
case IRI_PORT_CLI:
idx = 0;
for (char* p = strtok(value, ","); p && idx < MAX_IRI_LIST_ELEMENTS; p = strtok(NULL, ","), idx++) {
strtol_temp = strtol(p, &strtol_p, 10);
if (strtol_p != p && errno != ERANGE && strtol_temp >= 0 && strtol_temp <= USHRT_MAX) {
ta_conf->iota_port_list[idx] = (uint16_t)strtol_temp;
} else {
ta_log_error("Malformed input or illegal input character\n");
}
}
iota_service->http.port = ta_conf->iota_port_list[0];
break;

case HEALTH_TRACK_PERIOD:
strtol_temp = strtol(value, NULL, 10);
if (strtol_p != value && errno != ERANGE && strtol_temp >= INT_MIN && strtol_temp <= INT_MAX) {
Expand Down Expand Up @@ -207,6 +184,7 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
break;
}
}

return SC_OK;
}

Expand All @@ -228,8 +206,10 @@ status_t ta_core_default_init(ta_core_t* const core) {
ta_conf->version = TA_VERSION;
ta_conf->host = TA_HOST;
ta_conf->port = TA_PORT;
memset(ta_conf->host_list, 0, sizeof(ta_conf->host_list));
memset(ta_conf->port_list, 0, sizeof(ta_conf->port_list));
memset(ta_conf->iota_host_list, 0, sizeof(ta_conf->iota_host_list));
for (int i = 0; i < MAX_IRI_LIST_ELEMENTS; i++) {
ta_conf->iota_port_list[i] = IRI_PORT;
}
ta_conf->thread_count = TA_THREAD_COUNT;
ta_conf->proxy_passthrough = false;
ta_conf->health_track_period = IRI_HEALTH_TRACK_PERIOD;
Expand Down
12 changes: 6 additions & 6 deletions accelerator/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ extern "C" {

/** struct type of accelerator configuration */
typedef struct ta_config_s {
char* version; /**< Binding version of tangle-accelerator */
char* host; /**< Binding address of tangle-accelerator */
int port; /**< Binding port of tangle-accelerator */
char* host_list[MAX_IRI_LIST_ELEMENTS]; /**< List of binding host of tangle-accelerator */
int port_list[MAX_IRI_LIST_ELEMENTS]; /**< List of binding port of tangle-accelerator */
int health_track_period; /**< The period for checking IRI host connection status */
char* version; /**< Binding version of tangle-accelerator */
char* host; /**< Binding address of tangle-accelerator */
int port; /**< Binding port of tangle-accelerator */
char* iota_host_list[MAX_IRI_LIST_ELEMENTS]; /**< List of binding hosts of IOTA services */
uint16_t iota_port_list[MAX_IRI_LIST_ELEMENTS]; /**< List of binding ports of IOTA services */
int health_track_period; /**< The period for checking IRI host connection status */
#ifdef MQTT_ENABLE
char* mqtt_host; /**< Address of MQTT broker host */
char* mqtt_topic_root; /**< The topic root of MQTT topic */
Expand Down
19 changes: 6 additions & 13 deletions accelerator/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -574,23 +574,16 @@ status_t ta_get_iri_status(const iota_client_service_t* const service) {

status_t ta_update_iri_conneciton(ta_config_t* const ta_conf, iota_client_service_t* const service) {
status_t ret = SC_OK;
for (int i = 0; i < MAX_IRI_LIST_ELEMENTS && ta_conf->host_list[i]; i++) {
for (int i = 0; i < MAX_IRI_LIST_ELEMENTS && ta_conf->iota_host_list[i]; i++) {
// update new IRI host
ta_conf->host = ta_conf->host_list[i];
ta_conf->port = ta_conf->port_list[i];
service->http.host = ta_conf->host_list[i];
service->http.port = ta_conf->port_list[i];
ta_log_info("Try tp connect to %s:%d\n", ta_conf->host, ta_conf->port);

if (iota_client_core_init(service)) {
ta_log_error("Initializing IRI connection failed!\n");
return RC_CCLIENT_UNIMPLEMENTED;
}
iota_client_extended_init();

service->http.host = ta_conf->iota_host_list[i];
service->http.port = ta_conf->iota_port_list[i];
ta_log_info("Try to connect to %s:%d\n", service->http.host, service->http.port);

// Run from the first one until found a good one.
if (ta_get_iri_status(service) == SC_OK) {
ta_log_info("Connect to %s:%d\n", ta_conf->host, ta_conf->port);
ta_log_info("Connect to %s:%d\n", service->http.host, service->http.port);
goto done;
}
}
Expand Down
2 changes: 1 addition & 1 deletion accelerator/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ void health_track(void* arg) {
if (ret == SC_OK) {
ret = broadcast_buffered_txn(core);
if (ret) {
ta_log_error("Broadcast buffered transactions fialed. %s\n", ta_error_to_string(ret));
ta_log_error("Broadcast buffered transactions failed. %s\n", ta_error_to_string(ret));
}
}

Expand Down

0 comments on commit 616856a

Please sign in to comment.