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

Commit

Permalink
feat(api): Refactor send mam API
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Yu Wei Wu committed Jun 20, 2019
1 parent ba75888 commit 5ac73cd
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 81 deletions.
1 change: 1 addition & 0 deletions accelerator/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ cc_library(
deps = [
":common_core",
":ta_errors",
"//map:mode",
"//serializer",
"@entangled//common/model:bundle",
"@entangled//common/trinary:trit_tryte",
Expand Down
39 changes: 16 additions & 23 deletions accelerator/apis.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "apis.h"
#include "map/mode.h"

status_t api_get_tips(const iota_client_service_t* const service, char** json_result) {
status_t ret = SC_OK;
Expand Down Expand Up @@ -232,7 +233,6 @@ status_t api_receive_mam_message(const iota_client_service_t* const service, con
status_t api_mam_send_message(const iota_config_t* const tangle, const iota_client_service_t* const service,
char const* const payload, char** json_result) {
status_t ret = SC_OK;
retcode_t rc = RC_OK;
mam_api_t mam;
const bool last_packet = true;
bundle_transactions_t* bundle = NULL;
Expand All @@ -242,13 +242,8 @@ status_t api_mam_send_message(const iota_config_t* const tangle, const iota_clie
ta_send_mam_req_t* req = send_mam_req_new();
ta_send_mam_res_t* res = send_mam_res_new();

// Loading and creating MAM API
if ((rc = mam_api_load(tangle->mam_file, &mam, NULL, NULL)) == RC_UTILS_FAILED_TO_OPEN_FILE) {
if (mam_api_init(&mam, (tryte_t*)SEED)) {
ret = SC_MAM_FAILED_INIT;
goto done;
}
} else if (rc != RC_OK) {
// Creating MAM API
if (mam_api_init(&mam, (tryte_t*)SEED)) {
ret = SC_MAM_FAILED_INIT;
goto done;
}
Expand All @@ -258,21 +253,21 @@ status_t api_mam_send_message(const iota_config_t* const tangle, const iota_clie
goto done;
}

// Create mam channel
if (mam_channel_t_set_size(mam.channels) == 0) {
mam_api_channel_create(&mam, tangle->mss_depth, channel_id);
} else {
mam_channel_t* channel = &mam.channels->value;
trits_to_trytes(trits_begin(mam_channel_id(channel)), channel_id, NUM_TRITS_ADDRESS);
// Creating channel
mam.channel_ord = req->channel_ord;
if (map_channel_create(&mam, channel_id)) {
ret = SC_MAM_NULL;
goto done;
}

// Write header and packet
if (mam_api_bundle_write_header_on_channel(&mam, channel_id, NULL, NULL, bundle, msg_id) != RC_OK) {
// Writing header to bundle
if (map_write_header_on_channel(&mam, channel_id, bundle, msg_id)) {
ret = SC_MAM_FAILED_WRITE;
goto done;
}
if (mam_api_bundle_write_packet(&mam, msg_id, req->payload_trytes, req->payload_trytes_size, 0, last_packet,
bundle) != RC_OK) {

// Writing packet to bundle
if (map_write_packet(&mam, bundle, req->payload, msg_id, last_packet)) {
ret = SC_MAM_FAILED_WRITE;
goto done;
}
Expand All @@ -288,11 +283,9 @@ status_t api_mam_send_message(const iota_config_t* const tangle, const iota_clie
ret = send_mam_res_serialize(json_result, res);

done:
// Save and destroying MAM API
if (ret != SC_MAM_FAILED_INIT) {
if (mam_api_save(&mam, tangle->mam_file, NULL, NULL) || mam_api_destroy(&mam)) {
ret = SC_MAM_FAILED_DESTROYED;
}
// Destroying MAM API
if (mam_api_destroy(&mam)) {
ret = SC_MAM_FAILED_DESTROYED;
}
bundle_transactions_free(&bundle);
send_mam_req_free(&req);
Expand Down
3 changes: 0 additions & 3 deletions accelerator/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ status_t ta_config_default_init(ta_config_t* const info, iota_config_t* const ta

log_info(logger_id, "Initializing IRI configuration\n");
tangle->milestone_depth = MILESTONE_DEPTH;
mkstemp(mss_tmp);
strncpy(tangle->mam_file, mss_tmp, FSIZE);
tangle->mss_depth = MSS_DEPTH;
tangle->mwm = MWM;
tangle->seed = SEED;

Expand Down
3 changes: 0 additions & 3 deletions accelerator/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ extern "C" {
#define IRI_PORT 14265
#define MILESTONE_DEPTH 3
#define FSIZE 11
#define MSS_DEPTH 4
#define MWM 14
#define SEED \
"AMRWQP9BUMJALJHBXUCHOD9HFFD9LGTGEAWMJWWXSDVOF9PI9YGJAPBQLQUOMNYEQCZPGCTHGV" \
Expand All @@ -49,8 +48,6 @@ typedef struct ta_info_s {
/** struct type of iota configuration */
typedef struct ta_config_s {
uint8_t milestone_depth; /**< Depth of API argument */
char mam_file[FSIZE]; /** Save file for mam struct like MSS, skn... */
uint8_t mss_depth; /**< Depth of MSS layer merkle tree */
uint8_t mwm; /**< Minimum weight magnitude of API argument */
/** Seed to generate address. This does not do any signature yet. */
const char* seed;
Expand Down
29 changes: 7 additions & 22 deletions request/ta_send_mam.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,38 +3,23 @@
ta_send_mam_req_t* send_mam_req_new() {
ta_send_mam_req_t* req = (ta_send_mam_req_t*)malloc(sizeof(ta_send_mam_req_t));
if (req) {
req->payload_trytes = NULL;
req->payload = NULL;
req->channel_ord = 0;
}

return req;
}

status_t send_mam_req_set_payload(ta_send_mam_req_t* req, const tryte_t* payload) {
status_t ret = SC_OK;
size_t payload_size = sizeof(payload) / sizeof(tryte_t);

if ((!payload) || (payload_size == 0) || ((payload_size * 3) > SIZE_MAX)) {
return SC_MAM_OOM;
}

req->payload_trytes = (tryte_t*)malloc(payload_size * sizeof(tryte_t));
if (!req->payload_trytes) {
return SC_MAM_OOM;
}
memcpy(req->payload_trytes, payload, payload_size);
req->payload_trytes_size = payload_size;

return ret;
}

void send_mam_req_free(ta_send_mam_req_t** req) {
if (!req || !(*req)) {
return;
}
if ((*req)->payload_trytes) {
free((*req)->payload_trytes);
(*req)->payload_trytes = NULL;

if ((*req)->payload) {
free((*req)->payload);
(*req)->payload = NULL;
}

free(*req);
*req = NULL;
}
14 changes: 2 additions & 12 deletions request/ta_send_mam.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ extern "C" {

/** struct of ta_send_transfer_req_t */
typedef struct send_mam_req_s {
tryte_t* payload_trytes;
size_t payload_trytes_size;
char* payload;
uint8_t channel_ord;
} ta_send_mam_req_t;

/**
Expand All @@ -29,16 +29,6 @@ typedef struct send_mam_req_s {
*/
ta_send_mam_req_t* send_mam_req_new();

/**
* Set payload of ta_send_mam_req_t object
*
* @param req Data type of ta_send_transfer_req_t
* @param payload Tryte data going to send with mam
*
* @return NULL
*/
status_t send_mam_req_set_payload(ta_send_mam_req_t* req, const tryte_t* payload);

/**
* Free memory of ta_send_mam_req_t
*
Expand Down
24 changes: 15 additions & 9 deletions serializer/serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -552,26 +552,32 @@ status_t send_mam_req_deserialize(const char* const obj, ta_send_mam_req_t* req)

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

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

req->payload_trytes = payload_trytes;
req->payload_trytes_size = payload_size;
snprintf(payload, payload_size + 1, "%s", json_result->valuestring);
req->payload = payload;
} else {
ret = SC_SERIALIZER_NULL;
}

json_result = cJSON_GetObjectItemCaseSensitive(json_obj, "order");
if ((json_result != NULL) && cJSON_IsNumber(json_result)) {
req->channel_ord = json_result->valueint;
} else {
// 'value' does not exist or invalid, set to 0
req->channel_ord = 0;
}

done:
cJSON_Delete(json_obj);
return ret;
Expand Down
11 changes: 11 additions & 0 deletions tests/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,17 @@ cc_test(
],
)

cc_test(
name = "test_map_mode",
srcs = [
"test_map_mode.c",
],
deps = [
":test_define",
"//map:mode",
],
)

cc_library(
name = "test_define",
hdrs = ["test_define.h"],
Expand Down
82 changes: 82 additions & 0 deletions tests/test_map_mode.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include "map/mode.h"
#include "test_define.h"

#define CHID "UANFAVTSAXZMYUWRECNAOJDAQVTTORVGJCCISMZYAFFU9EYLBMZKEJ9VNXVFFGUTCHONEYVWVUTBTDJLO"
#define NEW_CHID "ONMTPDICUWBGEGODWKGBGMLNAZFXNHCJITSSTBTGMXCXBXJFBPOPXFPOJTXKOOSAJOZAYANZZBFKYHJ9N"
#define EPID "KI99YKKLFALYRUVRXKKRJCPVFISPMNCQQSMB9BGUWIHZTYFQOBZWYSVRNKVFJLSPPLPSFNBNREJWOR99U"

void test_channel_create(void) {
mam_api_t mam;
tryte_t channel_id[MAM_CHANNEL_ID_TRYTE_SIZE + 1];
mam_api_init(&mam, (tryte_t *)TRYTES_81_1);

map_channel_create(&mam, channel_id);
channel_id[MAM_CHANNEL_ID_TRYTE_SIZE] = '\0';
TEST_ASSERT_EQUAL_STRING(CHID, channel_id);

mam_api_destroy(&mam);
}

void test_announce_channel(void) {
mam_api_t mam;
bundle_transactions_t *bundle = NULL;
bundle_transactions_new(&bundle);
tryte_t channel_id[MAM_CHANNEL_ID_TRYTE_SIZE + 1];
trit_t msg_id[MAM_MSG_ID_SIZE];
mam_api_init(&mam, (tryte_t *)TRYTES_81_1);

map_channel_create(&mam, channel_id);
map_announce_channel(&mam, channel_id, bundle, msg_id, channel_id);
channel_id[MAM_CHANNEL_ID_TRYTE_SIZE] = '\0';
TEST_ASSERT_EQUAL_STRING(NEW_CHID, channel_id);

bundle_transactions_free(&bundle);
mam_api_destroy(&mam);
}

void test_announce_endpoint(void) {
mam_api_t mam;
bundle_transactions_t *bundle = NULL;
bundle_transactions_new(&bundle);
tryte_t channel_id[MAM_CHANNEL_ID_TRYTE_SIZE + 1];
trit_t msg_id[MAM_MSG_ID_SIZE];
mam_api_init(&mam, (tryte_t *)TRYTES_81_1);

map_channel_create(&mam, channel_id);
// Channel_id is actually the new endpoint id
map_announce_endpoint(&mam, channel_id, bundle, msg_id, channel_id);
channel_id[MAM_CHANNEL_ID_TRYTE_SIZE] = '\0';
TEST_ASSERT_EQUAL_STRING(EPID, channel_id);

bundle_transactions_free(&bundle);
mam_api_destroy(&mam);
}

void test_write_message(void) {
mam_api_t mam;
bundle_transactions_t *bundle = NULL;
bundle_transactions_new(&bundle);
tryte_t channel_id[MAM_CHANNEL_ID_TRYTE_SIZE + 1];
trit_t msg_id[MAM_MSG_ID_SIZE];
mam_api_init(&mam, (tryte_t *)TRYTES_81_1);
retcode_t ret = RC_ERROR;

map_channel_create(&mam, channel_id);
ret = map_write_header_on_channel(&mam, channel_id, bundle, msg_id);
TEST_ASSERT_EQUAL(RC_OK, ret);

ret = map_write_packet(&mam, bundle, TEST_PAYLOAD, msg_id, true);
TEST_ASSERT_EQUAL(RC_OK, ret);

bundle_transactions_free(&bundle);
mam_api_destroy(&mam);
}

int main(void) {
UNITY_BEGIN();
RUN_TEST(test_channel_create);
RUN_TEST(test_announce_channel);
RUN_TEST(test_announce_endpoint);
RUN_TEST(test_write_message);
return UNITY_END();
}
14 changes: 5 additions & 9 deletions tests/test_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,19 +236,15 @@ void test_deserialize_send_mam_message_response(void) {
}

void test_deserialize_send_mam_message(void) {
const char* json = "{\"message\":\"" TEST_PAYLOAD "\"}";
const char* json = "{\"message\":\"" TEST_PAYLOAD
"\","
"\"order\":2}";
ta_send_mam_req_t* req = send_mam_req_new();

send_mam_req_deserialize(json, req);
TEST_ASSERT_EQUAL_STRING(TEST_PAYLOAD, req->payload);
TEST_ASSERT_EQUAL_INT(2, req->channel_ord);

size_t payload_size = strlen(TEST_PAYLOAD) * 2;
tryte_t* payload_trytes = (tryte_t*)malloc(payload_size * sizeof(tryte_t));
ascii_to_trytes(TEST_PAYLOAD, payload_trytes);

TEST_ASSERT_EQUAL_UINT(payload_size, req->payload_trytes_size);
TEST_ASSERT_EQUAL_MEMORY(payload_trytes, req->payload_trytes, payload_size);

free(payload_trytes);
send_mam_req_free(&req);
}

Expand Down

0 comments on commit 5ac73cd

Please sign in to comment.