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

Commit

Permalink
fix(serializer): Fix wrong msg lengh limitation
Browse files Browse the repository at this point in the history
The length of message should less than `NUM_TRYTES_MESSAGE` (2187)
in trytes. The field `msg_len` is the length in `tryte_t`.

The input argument size of `flex_trits_to_trytes` and
`flex_trits_from_trytes` should be the size encoding in `trit_t`
and encoding in `tryte_t`, respectively.

For the same data, the data size in `trit_t` would be three times
longer than encoding in `tryte_t`. However, the data length
in `flex_trit_t` depends on how many `flex_trit_t` in one bytes
which depends on the different build time options.
  • Loading branch information
howjmay committed May 5, 2020
1 parent 8cb4610 commit 00b039f
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 77 deletions.
9 changes: 2 additions & 7 deletions accelerator/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,9 @@ status_t ta_send_transfer(const ta_config_t* const info, const iota_config_t* co
goto done;
}

// TODO Maybe we can replace the variable type of message from flex_trit_t to
// tryte_t
tryte_t msg_tryte[NUM_TRYTES_SERIALIZED_TRANSACTION];
flex_trits_to_trytes(msg_tryte, req->msg_len / 3, req->message, req->msg_len, req->msg_len);
transfer_t transfer = {.value = 0, .timestamp = current_timestamp_ms(), .msg_len = req->msg_len};

transfer_t transfer = {.value = 0, .timestamp = current_timestamp_ms(), .msg_len = req->msg_len / 3};

if (transfer_message_set_trytes(&transfer, msg_tryte, transfer.msg_len) != RC_OK) {
if (transfer_message_set_trytes(&transfer, req->message, transfer.msg_len) != RC_OK) {
ret = SC_CCLIENT_OOM;
ta_log_error("%s\n", ta_error_to_string(ret));
goto done;
Expand Down
1 change: 1 addition & 0 deletions accelerator/core/request/ta_send_transfer.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ta_send_transfer_req_t* ta_send_transfer_req_new() {
if (req != NULL) {
req->tag = NULL;
req->address = NULL;
memset(req->message, 0, sizeof(tryte_t) * NUM_TRYTES_MESSAGE);
return req;
}
return NULL;
Expand Down
7 changes: 4 additions & 3 deletions accelerator/core/request/ta_send_transfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define REQUEST_TA_SEND_TRANSFER_H_

#include <stdlib.h>
#include "common/macros.h"
#include "common/trinary/flex_trit.h"
#include "utils/containers/hash/hash243_queue.h"
#include "utils/containers/hash/hash81_queue.h"
Expand All @@ -32,9 +33,9 @@ typedef struct {
hash243_queue_t address;
/** @name message metadata */
/* @{ */
/** Transfer message is a 6561 long flex trits hash array. */
flex_trit_t message[FLEX_TRIT_SIZE_6561];
/** message length */
/** Transfer message is a 2187 long trytes array. */
tryte_t message[NUM_TRYTES_MESSAGE];
/** message trytes length */
int msg_len;
/* @} */
} ta_send_transfer_req_t;
Expand Down
27 changes: 14 additions & 13 deletions accelerator/core/serializer/serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,21 +531,22 @@ status_t ta_send_transfer_req_deserialize(const char* const obj, ta_send_transfe
}

if (raw_message) {
msg_len = msg_len * 2;
req->msg_len = msg_len * 3;
tryte_t trytes_buffer[msg_len];
req->msg_len = msg_len * 2;
if (req->msg_len > NUM_TRYTES_MESSAGE) {
ret = SC_SERIALIZER_MESSAGE_OVERRUN;
ta_log_error("%s\n", ta_error_to_string(ret));
goto done;
}

ascii_to_trytes(json_result->valuestring, trytes_buffer);
flex_trits_from_trytes(req->message, req->msg_len, trytes_buffer, msg_len, msg_len);
ascii_to_trytes(json_result->valuestring, req->message);
} else {
req->msg_len = msg_len * 3;
flex_trits_from_trytes(req->message, req->msg_len, (const tryte_t*)json_result->valuestring, msg_len, msg_len);
}

if (req->msg_len > NUM_TRYTES_MESSAGE) {
ret = SC_SERIALIZER_MESSAGE_OVERRUN;
ta_log_error("%s\n", ta_error_to_string(ret));
goto done;
req->msg_len = msg_len;
if (req->msg_len > NUM_TRYTES_MESSAGE) {
ret = SC_SERIALIZER_MESSAGE_OVERRUN;
ta_log_error("%s\n", ta_error_to_string(ret));
goto done;
}
memcpy(req->message, json_result->valuestring, req->msg_len);
}

} else {
Expand Down
3 changes: 3 additions & 0 deletions common/macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ typedef enum mam_protocol_e { MAM_V1 } mam_protocol_t;
#define __must_be_array(a) BUILD_BUG_ON_ZERO(__same_type((a), &(a)[0]))
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))

// How many different letters are used in tryte alphabet
#define TRINARY_ALPHABET_LEN 27

// TODO The temporary default timeout in cache server is 1 week. We should investigate the performance of redis to
// design a better data structure and appropriate timeout period. And we should study the methodology to partially
// release cached data.
Expand Down
11 changes: 11 additions & 0 deletions tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,14 @@ cc_library(
"@unity",
],
)

cc_library(
name = "common",
srcs = ["common.c"],
hdrs = ["common.h"],
visibility = ["//visibility:public"],
deps = [
"//accelerator:ta_config",
"//common",
],
)
18 changes: 4 additions & 14 deletions tests/api/BUILD
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
cc_library(
name = "common",
srcs = ["common.c"],
hdrs = ["common.h"],
deps = [
"//accelerator:ta_config",
"//common",
],
)

cc_test(
name = "driver",
srcs = [
Expand All @@ -16,8 +6,8 @@ cc_test(
deps = [
"//accelerator/core:apis",
"//accelerator/core:proxy_apis",
"//tests:common",
"//tests:test_define",
"//tests/api:common",
],
)

Expand All @@ -30,8 +20,8 @@ cc_binary(
deps = [
"//accelerator/core:apis",
"//accelerator/core:proxy_apis",
"//tests:common",
"//tests:test_define",
"//tests/api:common",
],
)

Expand All @@ -42,8 +32,8 @@ cc_test(
],
deps = [
"//accelerator/core",
"//tests:common",
"//tests:test_define",
"//tests/api:common",
],
)

Expand All @@ -53,10 +43,10 @@ cc_test(
"mam_test.c",
],
deps = [
":common",
"//accelerator/core:apis",
"//accelerator/core:proxy_apis",
"//common",
"//tests:common",
"//tests:test_define",
],
)
19 changes: 12 additions & 7 deletions tests/api/driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <time.h>
#include "accelerator/core/apis.h"
#include "accelerator/core/proxy_apis.h"
#include "common.h"
#include "tests/common.h"
#include "tests/test_define.h"

static driver_test_cases_t test_case;
Expand Down Expand Up @@ -91,13 +91,17 @@ void test_get_tips(void) {
}

void test_send_transfer(void) {
const char* json =
"{\"value\":100"
",\"tag\":\"" TEST_TAG
const char* json_template =
"{\"value\":100,"
"\"message_format\":\"trytes\","
"\"message\":\"%s\",\"tag\":\"" TEST_TAG
"\","
"\"address\":\"" TRYTES_81_1
"\","
"\"message\":\"" TEST_TRANSFER_MESSAGE "\"}";
"\"address\":\"" TRYTES_81_1 "\"}";
tryte_t test_transfer_message[TEST_TRANSFER_MESSAGE_LEN + 1] = {};
gen_rand_trytes(TEST_TRANSFER_MESSAGE_LEN, test_transfer_message);
const int len = strlen(json_template) + TEST_TRANSFER_MESSAGE_LEN;
char* json = (char*)malloc(sizeof(char) * len);
snprintf(json, len, json_template, test_transfer_message);
char* json_result;
double sum = 0;

Expand Down Expand Up @@ -125,6 +129,7 @@ void test_send_transfer(void) {
#endif
free(json_result);
}
free(json);
printf("Average time of send_transfer: %lf\n", sum / TEST_COUNT);
}

Expand Down
49 changes: 28 additions & 21 deletions tests/api/driver_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/

#include "accelerator/core/core.h"
#include "common.h"
#include "tests/common.h"
#include "tests/test_define.h"

static ta_core_t ta_core;
Expand All @@ -24,12 +24,9 @@ status_t prepare_transfer(const iota_config_t* const iconf, const iota_client_se
goto done;
}

tryte_t msg_tryte[NUM_TRYTES_SERIALIZED_TRANSACTION];
flex_trits_to_trytes(msg_tryte, req->msg_len / 3, req->message, req->msg_len, req->msg_len);
transfer_t transfer = {.value = 0, .timestamp = current_timestamp_ms(), .msg_len = req->msg_len};

transfer_t transfer = {.value = 0, .timestamp = current_timestamp_ms(), .msg_len = req->msg_len / 3};

if (transfer_message_set_trytes(&transfer, msg_tryte, transfer.msg_len) != RC_OK) {
if (transfer_message_set_trytes(&transfer, req->message, transfer.msg_len) != RC_OK) {
ret = SC_CCLIENT_OOM;
printf("%s\n", "SC_CCLIENT_OOM");
goto done;
Expand Down Expand Up @@ -70,13 +67,17 @@ status_t prepare_transfer(const iota_config_t* const iconf, const iota_client_se

void test_broadcast_buffered_txn(void) {
// Generate transaction trytes, and don't send them
const char* json =
"{\"value\":100"
",\"tag\":\"" TEST_TAG
"\","
"\"address\":\"" TEST_ADDRESS
const char* json_template =
"{\"value\":100,"
"\"message_format\":\"trytes\","
"\"message\":\"%s\",\"tag\":\"" TEST_TAG
"\","
"\"message\":\"" TEST_TRANSFER_MESSAGE "\"}";
"\"address\":\"" TRYTES_81_1 "\"}";
tryte_t test_transfer_message[TEST_TRANSFER_MESSAGE_LEN + 1] = {};
gen_rand_trytes(TEST_TRANSFER_MESSAGE_LEN, test_transfer_message);
const int len = strlen(json_template) + TEST_TRANSFER_MESSAGE_LEN;
char* json = (char*)malloc(sizeof(char) * len);
snprintf(json, len, json_template, test_transfer_message);

char uuid[UUID_STR_LEN];
int list_len = -1;
Expand Down Expand Up @@ -111,17 +112,22 @@ void test_broadcast_buffered_txn(void) {

ta_send_transfer_req_free(&req);
hash_array_free(raw_txn_array);
free(json);
}

void test_fetch_txn_with_uuid(void) {
// Generate transaction trytes, and don't send them
const char* json =
"{\"value\":100"
",\"tag\":\"" TEST_TAG
const char* json_template =
"{\"value\":100,"
"\"message_format\":\"trytes\","
"\"message\":\"%s\",\"tag\":\"" TEST_TAG
"\","
"\"address\":\"" TEST_ADDRESS
"\","
"\"message\":\"" TEST_TRANSFER_MESSAGE "\"}";
"\"address\":\"" TEST_ADDRESS "\"}";
tryte_t test_transfer_message[TEST_TRANSFER_MESSAGE_LEN + 1] = {};
gen_rand_trytes(TEST_TRANSFER_MESSAGE_LEN, test_transfer_message);
const int len = strlen(json_template) + TEST_TRANSFER_MESSAGE_LEN;
char* json = (char*)malloc(sizeof(char) * len);
snprintf(json, len, json_template, test_transfer_message);

char uuid[UUID_STR_LEN];
ta_send_transfer_req_t* req = ta_send_transfer_req_new();
Expand Down Expand Up @@ -153,17 +159,18 @@ void test_fetch_txn_with_uuid(void) {
flex_trits_to_trytes(trytes, NUM_TRYTES_HASH, transaction_address(res->txn), NUM_TRITS_HASH, NUM_TRITS_HASH);
TEST_ASSERT_EQUAL_STRING(TEST_ADDRESS, (char*)trytes);
flex_trits_to_trytes(trytes, NUM_TRYTES_MESSAGE, transaction_message(res->txn), NUM_TRITS_MESSAGE, NUM_TRITS_MESSAGE);
char txn_msg_ascii[NUM_TRYTES_MESSAGE / 2 + 1] = {};
trytes_to_ascii(trytes, NUM_TRYTES_MESSAGE, txn_msg_ascii);
TEST_ASSERT_EQUAL_STRING(TEST_TRANSFER_MESSAGE, txn_msg_ascii);
TEST_ASSERT_EQUAL_MEMORY(test_transfer_message, trytes, TEST_TRANSFER_MESSAGE_LEN);

ta_send_transfer_req_free(&req);
hash_array_free(raw_txn_array);
ta_fetch_txn_with_uuid_res_free(&res);
free(transaction_flex_trits);
free(json);
}

int main(int argc, char* argv[]) {
srand(time(NULL));

// Initialize logger
if (ta_logger_init() != SC_OK) {
return EXIT_FAILURE;
Expand Down
2 changes: 1 addition & 1 deletion tests/api/mam_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#include <unistd.h>
#include "accelerator/core/apis.h"
#include "accelerator/core/mam_core.h"
#include "common.h"
#include "tests/common.h"
#include "tests/test_define.h"

static ta_core_t ta_core;
Expand Down
8 changes: 8 additions & 0 deletions tests/api/common.c → tests/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,11 @@ void test_time_end(struct timespec* start, struct timespec* end, double* sum) {
#endif
*sum += difference;
}

void gen_rand_trytes(int len, tryte_t* trytes) {
const char tryte_alphabet[] = "NOPQRSTUVWXYZ9ABCDEFGHIJKLM";

for (int i = 0; i < len; i++) {
trytes[i] = tryte_alphabet[rand() % TRINARY_ALPHABET_LEN];
}
}
11 changes: 10 additions & 1 deletion tests/api/common.h → tests/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,13 @@ static inline void test_time_start(struct timespec* start) { clock_gettime(CLOCK
* @param start[out] The total elapsing time.
*
*/
void test_time_end(struct timespec* start, struct timespec* end, double* sum);
void test_time_end(struct timespec* start, struct timespec* end, double* sum);

/**
* Generate a random trytes combination with given length
*
* @param len[in] The length of generated trytes
* @param trytes[out] Output trytes combination
*
*/
void gen_rand_trytes(int len, tryte_t* trytes);
4 changes: 3 additions & 1 deletion tests/test_define.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ extern "C" {
"THIS9IS9AN9ADDRESS9USED9BY9TAGNLEACCELERATOR999999999999999999999999999999" \
"9999999"
#define TEST_TAG "TANGLEACCELERATOR9999999999"
#define TEST_TRANSFER_MESSAGE "THIS9IS9MESSAGE9FROM9TAGNLEACCELERATOR"
#define TEST_TRANSFER_MESSAGE_LEN 2186
#define TEST_TRANSFER_MESSAGE_RAW_MESSAGE_LEN 1093
#define TEST_TRANSFER_MESSAGE_OVERRUN_RAW_MESSAGE_LEN 1095
#define TEST_TAG_LEN 27
#define VALUE 100
#define TIMESTAMP 1546436542
Expand Down
1 change: 1 addition & 0 deletions tests/unit-test/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ cc_test(
],
deps = [
"//accelerator/core/serializer",
"//tests:common",
"//tests:test_define",
],
)
Expand Down
Loading

0 comments on commit 00b039f

Please sign in to comment.