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

Commit 5ac73cd

Browse files
author
Yu Wei Wu
committed
feat(api): Refactor send mam API
Add a new argument "order" to specific channel order. Functions are now from map target and add test case for it. Type of send mam request is also refactored.
1 parent ba75888 commit 5ac73cd

File tree

10 files changed

+139
-81
lines changed

10 files changed

+139
-81
lines changed

accelerator/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ cc_library(
2626
deps = [
2727
":common_core",
2828
":ta_errors",
29+
"//map:mode",
2930
"//serializer",
3031
"@entangled//common/model:bundle",
3132
"@entangled//common/trinary:trit_tryte",

accelerator/apis.c

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "apis.h"
2+
#include "map/mode.h"
23

34
status_t api_get_tips(const iota_client_service_t* const service, char** json_result) {
45
status_t ret = SC_OK;
@@ -232,7 +233,6 @@ status_t api_receive_mam_message(const iota_client_service_t* const service, con
232233
status_t api_mam_send_message(const iota_config_t* const tangle, const iota_client_service_t* const service,
233234
char const* const payload, char** json_result) {
234235
status_t ret = SC_OK;
235-
retcode_t rc = RC_OK;
236236
mam_api_t mam;
237237
const bool last_packet = true;
238238
bundle_transactions_t* bundle = NULL;
@@ -242,13 +242,8 @@ status_t api_mam_send_message(const iota_config_t* const tangle, const iota_clie
242242
ta_send_mam_req_t* req = send_mam_req_new();
243243
ta_send_mam_res_t* res = send_mam_res_new();
244244

245-
// Loading and creating MAM API
246-
if ((rc = mam_api_load(tangle->mam_file, &mam, NULL, NULL)) == RC_UTILS_FAILED_TO_OPEN_FILE) {
247-
if (mam_api_init(&mam, (tryte_t*)SEED)) {
248-
ret = SC_MAM_FAILED_INIT;
249-
goto done;
250-
}
251-
} else if (rc != RC_OK) {
245+
// Creating MAM API
246+
if (mam_api_init(&mam, (tryte_t*)SEED)) {
252247
ret = SC_MAM_FAILED_INIT;
253248
goto done;
254249
}
@@ -258,21 +253,21 @@ status_t api_mam_send_message(const iota_config_t* const tangle, const iota_clie
258253
goto done;
259254
}
260255

261-
// Create mam channel
262-
if (mam_channel_t_set_size(mam.channels) == 0) {
263-
mam_api_channel_create(&mam, tangle->mss_depth, channel_id);
264-
} else {
265-
mam_channel_t* channel = &mam.channels->value;
266-
trits_to_trytes(trits_begin(mam_channel_id(channel)), channel_id, NUM_TRITS_ADDRESS);
256+
// Creating channel
257+
mam.channel_ord = req->channel_ord;
258+
if (map_channel_create(&mam, channel_id)) {
259+
ret = SC_MAM_NULL;
260+
goto done;
267261
}
268262

269-
// Write header and packet
270-
if (mam_api_bundle_write_header_on_channel(&mam, channel_id, NULL, NULL, bundle, msg_id) != RC_OK) {
263+
// Writing header to bundle
264+
if (map_write_header_on_channel(&mam, channel_id, bundle, msg_id)) {
271265
ret = SC_MAM_FAILED_WRITE;
272266
goto done;
273267
}
274-
if (mam_api_bundle_write_packet(&mam, msg_id, req->payload_trytes, req->payload_trytes_size, 0, last_packet,
275-
bundle) != RC_OK) {
268+
269+
// Writing packet to bundle
270+
if (map_write_packet(&mam, bundle, req->payload, msg_id, last_packet)) {
276271
ret = SC_MAM_FAILED_WRITE;
277272
goto done;
278273
}
@@ -288,11 +283,9 @@ status_t api_mam_send_message(const iota_config_t* const tangle, const iota_clie
288283
ret = send_mam_res_serialize(json_result, res);
289284

290285
done:
291-
// Save and destroying MAM API
292-
if (ret != SC_MAM_FAILED_INIT) {
293-
if (mam_api_save(&mam, tangle->mam_file, NULL, NULL) || mam_api_destroy(&mam)) {
294-
ret = SC_MAM_FAILED_DESTROYED;
295-
}
286+
// Destroying MAM API
287+
if (mam_api_destroy(&mam)) {
288+
ret = SC_MAM_FAILED_DESTROYED;
296289
}
297290
bundle_transactions_free(&bundle);
298291
send_mam_req_free(&req);

accelerator/config.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,6 @@ status_t ta_config_default_init(ta_config_t* const info, iota_config_t* const ta
8686

8787
log_info(logger_id, "Initializing IRI configuration\n");
8888
tangle->milestone_depth = MILESTONE_DEPTH;
89-
mkstemp(mss_tmp);
90-
strncpy(tangle->mam_file, mss_tmp, FSIZE);
91-
tangle->mss_depth = MSS_DEPTH;
9289
tangle->mwm = MWM;
9390
tangle->seed = SEED;
9491

accelerator/config.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ extern "C" {
2626
#define IRI_PORT 14265
2727
#define MILESTONE_DEPTH 3
2828
#define FSIZE 11
29-
#define MSS_DEPTH 4
3029
#define MWM 14
3130
#define SEED \
3231
"AMRWQP9BUMJALJHBXUCHOD9HFFD9LGTGEAWMJWWXSDVOF9PI9YGJAPBQLQUOMNYEQCZPGCTHGV" \
@@ -49,8 +48,6 @@ typedef struct ta_info_s {
4948
/** struct type of iota configuration */
5049
typedef struct ta_config_s {
5150
uint8_t milestone_depth; /**< Depth of API argument */
52-
char mam_file[FSIZE]; /** Save file for mam struct like MSS, skn... */
53-
uint8_t mss_depth; /**< Depth of MSS layer merkle tree */
5451
uint8_t mwm; /**< Minimum weight magnitude of API argument */
5552
/** Seed to generate address. This does not do any signature yet. */
5653
const char* seed;

request/ta_send_mam.c

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,38 +3,23 @@
33
ta_send_mam_req_t* send_mam_req_new() {
44
ta_send_mam_req_t* req = (ta_send_mam_req_t*)malloc(sizeof(ta_send_mam_req_t));
55
if (req) {
6-
req->payload_trytes = NULL;
6+
req->payload = NULL;
7+
req->channel_ord = 0;
78
}
89

910
return req;
1011
}
1112

12-
status_t send_mam_req_set_payload(ta_send_mam_req_t* req, const tryte_t* payload) {
13-
status_t ret = SC_OK;
14-
size_t payload_size = sizeof(payload) / sizeof(tryte_t);
15-
16-
if ((!payload) || (payload_size == 0) || ((payload_size * 3) > SIZE_MAX)) {
17-
return SC_MAM_OOM;
18-
}
19-
20-
req->payload_trytes = (tryte_t*)malloc(payload_size * sizeof(tryte_t));
21-
if (!req->payload_trytes) {
22-
return SC_MAM_OOM;
23-
}
24-
memcpy(req->payload_trytes, payload, payload_size);
25-
req->payload_trytes_size = payload_size;
26-
27-
return ret;
28-
}
29-
3013
void send_mam_req_free(ta_send_mam_req_t** req) {
3114
if (!req || !(*req)) {
3215
return;
3316
}
34-
if ((*req)->payload_trytes) {
35-
free((*req)->payload_trytes);
36-
(*req)->payload_trytes = NULL;
17+
18+
if ((*req)->payload) {
19+
free((*req)->payload);
20+
(*req)->payload = NULL;
3721
}
22+
3823
free(*req);
3924
*req = NULL;
4025
}

request/ta_send_mam.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ extern "C" {
1616

1717
/** struct of ta_send_transfer_req_t */
1818
typedef struct send_mam_req_s {
19-
tryte_t* payload_trytes;
20-
size_t payload_trytes_size;
19+
char* payload;
20+
uint8_t channel_ord;
2121
} ta_send_mam_req_t;
2222

2323
/**
@@ -29,16 +29,6 @@ typedef struct send_mam_req_s {
2929
*/
3030
ta_send_mam_req_t* send_mam_req_new();
3131

32-
/**
33-
* Set payload of ta_send_mam_req_t object
34-
*
35-
* @param req Data type of ta_send_transfer_req_t
36-
* @param payload Tryte data going to send with mam
37-
*
38-
* @return NULL
39-
*/
40-
status_t send_mam_req_set_payload(ta_send_mam_req_t* req, const tryte_t* payload);
41-
4232
/**
4333
* Free memory of ta_send_mam_req_t
4434
*

serializer/serializer.c

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -552,26 +552,32 @@ status_t send_mam_req_deserialize(const char* const obj, ta_send_mam_req_t* req)
552552

553553
json_result = cJSON_GetObjectItemCaseSensitive(json_obj, "message");
554554
if (json_result != NULL) {
555-
size_t payload_size = strlen(json_result->valuestring) * 2;
556-
tryte_t* payload_trytes = (tryte_t*)malloc(payload_size * sizeof(tryte_t));
557-
ascii_to_trytes(json_result->valuestring, payload_trytes);
555+
size_t payload_size = strlen(json_result->valuestring);
556+
char* payload = (char*)malloc((payload_size + 1) * sizeof(char));
558557

559-
// in case the payload is unicode, char more than 128 will result to an
558+
// In case the payload is unicode, char more than 128 will result to an
560559
// error status_t code
561-
size_t payload_ascii_len = payload_size > 1;
562-
for (int i = 0; i < payload_ascii_len; i++) {
563-
if (json_result->valuestring[i] & (unsigned)128) {
560+
for (int i = 0; i < payload_size; i++) {
561+
if (json_result->valuestring[i] & 0x80) {
564562
ret = SC_SERIALIZER_JSON_PARSE_ASCII;
565563
goto done;
566564
}
567565
}
568566

569-
req->payload_trytes = payload_trytes;
570-
req->payload_trytes_size = payload_size;
567+
snprintf(payload, payload_size + 1, "%s", json_result->valuestring);
568+
req->payload = payload;
571569
} else {
572570
ret = SC_SERIALIZER_NULL;
573571
}
574572

573+
json_result = cJSON_GetObjectItemCaseSensitive(json_obj, "order");
574+
if ((json_result != NULL) && cJSON_IsNumber(json_result)) {
575+
req->channel_ord = json_result->valueint;
576+
} else {
577+
// 'value' does not exist or invalid, set to 0
578+
req->channel_ord = 0;
579+
}
580+
575581
done:
576582
cJSON_Delete(json_obj);
577583
return ret;

tests/BUILD

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ cc_test(
8282
],
8383
)
8484

85+
cc_test(
86+
name = "test_map_mode",
87+
srcs = [
88+
"test_map_mode.c",
89+
],
90+
deps = [
91+
":test_define",
92+
"//map:mode",
93+
],
94+
)
95+
8596
cc_library(
8697
name = "test_define",
8798
hdrs = ["test_define.h"],

tests/test_map_mode.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "map/mode.h"
2+
#include "test_define.h"
3+
4+
#define CHID "UANFAVTSAXZMYUWRECNAOJDAQVTTORVGJCCISMZYAFFU9EYLBMZKEJ9VNXVFFGUTCHONEYVWVUTBTDJLO"
5+
#define NEW_CHID "ONMTPDICUWBGEGODWKGBGMLNAZFXNHCJITSSTBTGMXCXBXJFBPOPXFPOJTXKOOSAJOZAYANZZBFKYHJ9N"
6+
#define EPID "KI99YKKLFALYRUVRXKKRJCPVFISPMNCQQSMB9BGUWIHZTYFQOBZWYSVRNKVFJLSPPLPSFNBNREJWOR99U"
7+
8+
void test_channel_create(void) {
9+
mam_api_t mam;
10+
tryte_t channel_id[MAM_CHANNEL_ID_TRYTE_SIZE + 1];
11+
mam_api_init(&mam, (tryte_t *)TRYTES_81_1);
12+
13+
map_channel_create(&mam, channel_id);
14+
channel_id[MAM_CHANNEL_ID_TRYTE_SIZE] = '\0';
15+
TEST_ASSERT_EQUAL_STRING(CHID, channel_id);
16+
17+
mam_api_destroy(&mam);
18+
}
19+
20+
void test_announce_channel(void) {
21+
mam_api_t mam;
22+
bundle_transactions_t *bundle = NULL;
23+
bundle_transactions_new(&bundle);
24+
tryte_t channel_id[MAM_CHANNEL_ID_TRYTE_SIZE + 1];
25+
trit_t msg_id[MAM_MSG_ID_SIZE];
26+
mam_api_init(&mam, (tryte_t *)TRYTES_81_1);
27+
28+
map_channel_create(&mam, channel_id);
29+
map_announce_channel(&mam, channel_id, bundle, msg_id, channel_id);
30+
channel_id[MAM_CHANNEL_ID_TRYTE_SIZE] = '\0';
31+
TEST_ASSERT_EQUAL_STRING(NEW_CHID, channel_id);
32+
33+
bundle_transactions_free(&bundle);
34+
mam_api_destroy(&mam);
35+
}
36+
37+
void test_announce_endpoint(void) {
38+
mam_api_t mam;
39+
bundle_transactions_t *bundle = NULL;
40+
bundle_transactions_new(&bundle);
41+
tryte_t channel_id[MAM_CHANNEL_ID_TRYTE_SIZE + 1];
42+
trit_t msg_id[MAM_MSG_ID_SIZE];
43+
mam_api_init(&mam, (tryte_t *)TRYTES_81_1);
44+
45+
map_channel_create(&mam, channel_id);
46+
// Channel_id is actually the new endpoint id
47+
map_announce_endpoint(&mam, channel_id, bundle, msg_id, channel_id);
48+
channel_id[MAM_CHANNEL_ID_TRYTE_SIZE] = '\0';
49+
TEST_ASSERT_EQUAL_STRING(EPID, channel_id);
50+
51+
bundle_transactions_free(&bundle);
52+
mam_api_destroy(&mam);
53+
}
54+
55+
void test_write_message(void) {
56+
mam_api_t mam;
57+
bundle_transactions_t *bundle = NULL;
58+
bundle_transactions_new(&bundle);
59+
tryte_t channel_id[MAM_CHANNEL_ID_TRYTE_SIZE + 1];
60+
trit_t msg_id[MAM_MSG_ID_SIZE];
61+
mam_api_init(&mam, (tryte_t *)TRYTES_81_1);
62+
retcode_t ret = RC_ERROR;
63+
64+
map_channel_create(&mam, channel_id);
65+
ret = map_write_header_on_channel(&mam, channel_id, bundle, msg_id);
66+
TEST_ASSERT_EQUAL(RC_OK, ret);
67+
68+
ret = map_write_packet(&mam, bundle, TEST_PAYLOAD, msg_id, true);
69+
TEST_ASSERT_EQUAL(RC_OK, ret);
70+
71+
bundle_transactions_free(&bundle);
72+
mam_api_destroy(&mam);
73+
}
74+
75+
int main(void) {
76+
UNITY_BEGIN();
77+
RUN_TEST(test_channel_create);
78+
RUN_TEST(test_announce_channel);
79+
RUN_TEST(test_announce_endpoint);
80+
RUN_TEST(test_write_message);
81+
return UNITY_END();
82+
}

tests/test_serializer.c

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,19 +236,15 @@ void test_deserialize_send_mam_message_response(void) {
236236
}
237237

238238
void test_deserialize_send_mam_message(void) {
239-
const char* json = "{\"message\":\"" TEST_PAYLOAD "\"}";
239+
const char* json = "{\"message\":\"" TEST_PAYLOAD
240+
"\","
241+
"\"order\":2}";
240242
ta_send_mam_req_t* req = send_mam_req_new();
241243

242244
send_mam_req_deserialize(json, req);
245+
TEST_ASSERT_EQUAL_STRING(TEST_PAYLOAD, req->payload);
246+
TEST_ASSERT_EQUAL_INT(2, req->channel_ord);
243247

244-
size_t payload_size = strlen(TEST_PAYLOAD) * 2;
245-
tryte_t* payload_trytes = (tryte_t*)malloc(payload_size * sizeof(tryte_t));
246-
ascii_to_trytes(TEST_PAYLOAD, payload_trytes);
247-
248-
TEST_ASSERT_EQUAL_UINT(payload_size, req->payload_trytes_size);
249-
TEST_ASSERT_EQUAL_MEMORY(payload_trytes, req->payload_trytes, payload_size);
250-
251-
free(payload_trytes);
252248
send_mam_req_free(&req);
253249
}
254250

0 commit comments

Comments
 (0)