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

Commit 616856a

Browse files
committed
fix(core): integrate conflicting IOTA conf and fix incorrect TA conf
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
1 parent 0d1a082 commit 616856a

File tree

4 files changed

+34
-61
lines changed

4 files changed

+34
-61
lines changed

accelerator/config.c

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
5252
#endif
5353
char* strtol_p = NULL;
5454
long int strtol_temp;
55+
uint8_t idx;
5556
switch (key) {
5657
// TA configuration
5758
case TA_HOST_CLI:
@@ -67,57 +68,33 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
6768
break;
6869
case TA_THREAD_COUNT_CLI:
6970
strtol_temp = strtol(value, &strtol_p, 10);
70-
if (strtol_p != value && errno != ERANGE && strtol_temp >= INT_MIN && strtol_temp <= INT_MAX) {
71-
ta_conf->thread_count = (int)strtol_temp;
71+
if (strtol_p != value && errno != ERANGE && strtol_temp >= 0 && strtol_temp <= UCHAR_MAX) {
72+
ta_conf->thread_count = (uint8_t)strtol_temp;
7273
} else {
7374
ta_log_error("Malformed input or illegal input character\n");
7475
}
7576
break;
7677

7778
// IRI configuration
7879
case IRI_HOST_CLI:
79-
iota_service->http.host = value;
80-
break;
81-
case IRI_PORT_CLI:
82-
strtol_temp = strtol(value, &strtol_p, 10);
83-
if (strtol_p != value && errno != ERANGE && strtol_temp >= INT_MIN && strtol_temp <= INT_MAX) {
84-
iota_service->http.port = (int)strtol_temp;
85-
} else {
86-
ta_log_error("Malformed input or illegal input character\n");
80+
idx = 0;
81+
for (char* p = strtok(value, ","); p && idx < MAX_IRI_LIST_ELEMENTS; p = strtok(NULL, ","), idx++) {
82+
ta_conf->iota_host_list[idx] = p;
8783
}
84+
iota_service->http.host = ta_conf->iota_host_list[0];
8885
break;
89-
case IRI_ADDRESS_CLI:
90-
for (int i = 0; i < MAX_IRI_LIST_ELEMENTS; i++) {
91-
if (!ta_conf->host_list[i]) {
92-
char *host, *port;
93-
host = strtok(value, ":");
94-
port = strtok(NULL, "");
95-
if (!port) {
96-
ta_log_error("Malformed input or illegal input character\n");
97-
break;
98-
}
99-
100-
if (i == 0) {
101-
iota_service->http.host = host;
102-
strtol_temp = strtol(port, NULL, 10);
103-
if (strtol_p != value && errno != ERANGE && strtol_temp >= INT_MIN && strtol_temp <= INT_MAX) {
104-
iota_service->http.port = (int)strtol_temp;
105-
} else {
106-
ta_log_error("Malformed input or illegal input character\n");
107-
}
108-
}
109-
ta_conf->host_list[i] = host;
110-
strtol_temp = strtol(port, NULL, 10);
111-
if (strtol_p != value && errno != ERANGE && strtol_temp >= INT_MIN && strtol_temp <= INT_MAX) {
112-
ta_conf->port_list[i] = (int)strtol_temp;
113-
} else {
114-
ta_log_error("Malformed input or illegal input character\n");
115-
}
116-
break;
86+
case IRI_PORT_CLI:
87+
idx = 0;
88+
for (char* p = strtok(value, ","); p && idx < MAX_IRI_LIST_ELEMENTS; p = strtok(NULL, ","), idx++) {
89+
strtol_temp = strtol(p, &strtol_p, 10);
90+
if (strtol_p != p && errno != ERANGE && strtol_temp >= 0 && strtol_temp <= USHRT_MAX) {
91+
ta_conf->iota_port_list[idx] = (uint16_t)strtol_temp;
92+
} else {
93+
ta_log_error("Malformed input or illegal input character\n");
11794
}
11895
}
96+
iota_service->http.port = ta_conf->iota_port_list[0];
11997
break;
120-
12198
case HEALTH_TRACK_PERIOD:
12299
strtol_temp = strtol(value, NULL, 10);
123100
if (strtol_p != value && errno != ERANGE && strtol_temp >= INT_MIN && strtol_temp <= INT_MAX) {
@@ -207,6 +184,7 @@ status_t cli_core_set(ta_core_t* const core, int key, char* const value) {
207184
break;
208185
}
209186
}
187+
210188
return SC_OK;
211189
}
212190

@@ -228,8 +206,10 @@ status_t ta_core_default_init(ta_core_t* const core) {
228206
ta_conf->version = TA_VERSION;
229207
ta_conf->host = TA_HOST;
230208
ta_conf->port = TA_PORT;
231-
memset(ta_conf->host_list, 0, sizeof(ta_conf->host_list));
232-
memset(ta_conf->port_list, 0, sizeof(ta_conf->port_list));
209+
memset(ta_conf->iota_host_list, 0, sizeof(ta_conf->iota_host_list));
210+
for (int i = 0; i < MAX_IRI_LIST_ELEMENTS; i++) {
211+
ta_conf->iota_port_list[i] = IRI_PORT;
212+
}
233213
ta_conf->thread_count = TA_THREAD_COUNT;
234214
ta_conf->proxy_passthrough = false;
235215
ta_conf->health_track_period = IRI_HEALTH_TRACK_PERIOD;

accelerator/config.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ extern "C" {
6565

6666
/** struct type of accelerator configuration */
6767
typedef struct ta_config_s {
68-
char* version; /**< Binding version of tangle-accelerator */
69-
char* host; /**< Binding address of tangle-accelerator */
70-
int port; /**< Binding port of tangle-accelerator */
71-
char* host_list[MAX_IRI_LIST_ELEMENTS]; /**< List of binding host of tangle-accelerator */
72-
int port_list[MAX_IRI_LIST_ELEMENTS]; /**< List of binding port of tangle-accelerator */
73-
int health_track_period; /**< The period for checking IRI host connection status */
68+
char* version; /**< Binding version of tangle-accelerator */
69+
char* host; /**< Binding address of tangle-accelerator */
70+
int port; /**< Binding port of tangle-accelerator */
71+
char* iota_host_list[MAX_IRI_LIST_ELEMENTS]; /**< List of binding hosts of IOTA services */
72+
uint16_t iota_port_list[MAX_IRI_LIST_ELEMENTS]; /**< List of binding ports of IOTA services */
73+
int health_track_period; /**< The period for checking IRI host connection status */
7474
#ifdef MQTT_ENABLE
7575
char* mqtt_host; /**< Address of MQTT broker host */
7676
char* mqtt_topic_root; /**< The topic root of MQTT topic */

accelerator/core/core.c

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -574,23 +574,16 @@ status_t ta_get_iri_status(const iota_client_service_t* const service) {
574574

575575
status_t ta_update_iri_conneciton(ta_config_t* const ta_conf, iota_client_service_t* const service) {
576576
status_t ret = SC_OK;
577-
for (int i = 0; i < MAX_IRI_LIST_ELEMENTS && ta_conf->host_list[i]; i++) {
577+
for (int i = 0; i < MAX_IRI_LIST_ELEMENTS && ta_conf->iota_host_list[i]; i++) {
578578
// update new IRI host
579-
ta_conf->host = ta_conf->host_list[i];
580-
ta_conf->port = ta_conf->port_list[i];
581-
service->http.host = ta_conf->host_list[i];
582-
service->http.port = ta_conf->port_list[i];
583-
ta_log_info("Try tp connect to %s:%d\n", ta_conf->host, ta_conf->port);
584-
585-
if (iota_client_core_init(service)) {
586-
ta_log_error("Initializing IRI connection failed!\n");
587-
return RC_CCLIENT_UNIMPLEMENTED;
588-
}
589-
iota_client_extended_init();
579+
580+
service->http.host = ta_conf->iota_host_list[i];
581+
service->http.port = ta_conf->iota_port_list[i];
582+
ta_log_info("Try to connect to %s:%d\n", service->http.host, service->http.port);
590583

591584
// Run from the first one until found a good one.
592585
if (ta_get_iri_status(service) == SC_OK) {
593-
ta_log_info("Connect to %s:%d\n", ta_conf->host, ta_conf->port);
586+
ta_log_info("Connect to %s:%d\n", service->http.host, service->http.port);
594587
goto done;
595588
}
596589
}

accelerator/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void health_track(void* arg) {
3636
if (ret == SC_OK) {
3737
ret = broadcast_buffered_txn(core);
3838
if (ret) {
39-
ta_log_error("Broadcast buffered transactions fialed. %s\n", ta_error_to_string(ret));
39+
ta_log_error("Broadcast buffered transactions failed. %s\n", ta_error_to_string(ret));
4040
}
4141
}
4242

0 commit comments

Comments
 (0)