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

Commit

Permalink
fix(api): Add prng as parameter of send mam API
Browse files Browse the repository at this point in the history
This is a temporary option to let application like tangle ID to be able to
create unique channel and keep it stateless. This will be deprecated
once JS binding of mam is done.
  • Loading branch information
Yu Wei Wu committed Jun 21, 2019
1 parent 66c83a5 commit 45847de
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 10 deletions.
28 changes: 21 additions & 7 deletions accelerator/apis.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,25 +237,37 @@ status_t api_mam_send_message(const iota_config_t* const tangle, const iota_clie
const bool last_packet = true;
bundle_transactions_t* bundle = NULL;
bundle_transactions_new(&bundle);
tryte_t* prng = NULL;
tryte_t channel_id[MAM_CHANNEL_ID_TRYTE_SIZE];
trit_t msg_id[MAM_MSG_ID_SIZE];
ta_send_mam_req_t* req = send_mam_req_new();
ta_send_mam_res_t* res = send_mam_res_new();

// Creating MAM API
if (mam_api_init(&mam, (tryte_t*)SEED)) {
if (send_mam_req_deserialize(payload, req)) {
ret = SC_MAM_FAILED_INIT;
goto done;
}

ret = send_mam_req_deserialize(payload, req);
if (ret) {
// Creating MAM API
prng = (req->prng[0]) ? req->prng : SEED;
if (mam_api_init(&mam, prng) != RC_OK) {
ret = SC_MAM_FAILED_INIT;
goto done;
}

// Creating channel
// TODO: TA only supports mss depth under 2. Larger than this would result in wasting too much time generate keys.
// With depth of 2, user can send a bundle with 3 transaction messages filled (last one is for header).
size_t payload_size = strlen(req->payload);
size_t payload_depth = 1;
if (payload_size > (NUM_TRYTES_SIGNATURE / 2) * 3) {
ret = SC_MAM_NO_PAYLOAD;
goto done;
} else if (payload_size > (NUM_TRYTES_SIGNATURE / 2)) {
payload_depth = 2;
}
mam.channel_ord = req->channel_ord;
if (map_channel_create(&mam, channel_id)) {
if (map_channel_create(&mam, channel_id, payload_depth)) {
ret = SC_MAM_NULL;
goto done;
}
Expand Down Expand Up @@ -284,8 +296,10 @@ status_t api_mam_send_message(const iota_config_t* const tangle, const iota_clie

done:
// Destroying MAM API
if (mam_api_destroy(&mam)) {
ret = SC_MAM_FAILED_DESTROYED;
if (ret != SC_MAM_FAILED_INIT) {
if (mam_api_destroy(&mam)) {
ret = SC_MAM_FAILED_DESTROYED;
}
}
bundle_transactions_free(&bundle);
send_mam_req_free(&req);
Expand Down
1 change: 1 addition & 0 deletions request/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ cc_library(
deps = [
"//accelerator:ta_errors",
"@entangled//common:errors",
"@entangled//common/model:transaction",
"@entangled//common/trinary:tryte",
"@entangled//utils/containers/hash:hash243_queue",
"@entangled//utils/containers/hash:hash81_queue",
Expand Down
1 change: 1 addition & 0 deletions request/ta_send_mam.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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->prng[0] = 0;
req->payload = NULL;
req->channel_ord = 0;
}
Expand Down
2 changes: 2 additions & 0 deletions request/ta_send_mam.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdlib.h>
#include <string.h>
#include "accelerator/errors.h"
#include "common/model/transaction.h"
#include "common/trinary/tryte.h"

#ifdef __cplusplus
Expand All @@ -16,6 +17,7 @@ extern "C" {

/** struct of ta_send_transfer_req_t */
typedef struct send_mam_req_s {
tryte_t prng[NUM_TRYTES_ADDRESS + 1];
char* payload;
uint8_t channel_ord;
} ta_send_mam_req_t;
Expand Down
11 changes: 11 additions & 0 deletions serializer/serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,17 @@ status_t send_mam_req_deserialize(const char* const obj, ta_send_mam_req_t* req)
goto done;
}

json_result = cJSON_GetObjectItemCaseSensitive(json_obj, "prng");
if (json_result != NULL) {
size_t prng_size = strlen(json_result->valuestring);

if (prng_size != NUM_TRYTES_HASH) {
ret = SC_SERIALIZER_INVALID_REQ;
goto done;
}
snprintf(req->prng, prng_size + 1, "%s", json_result->valuestring);
}

json_result = cJSON_GetObjectItemCaseSensitive(json_obj, "message");
if (json_result != NULL) {
size_t payload_size = strlen(json_result->valuestring);
Expand Down
5 changes: 2 additions & 3 deletions tests/test_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,11 @@ void test_deserialize_send_mam_message_response(void) {
}

void test_deserialize_send_mam_message(void) {
const char* json = "{\"message\":\"" TEST_PAYLOAD
"\","
"\"order\":2}";
const char* json = "{\"prng\":\"" TRYTES_81_1 "\",\"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(TRYTES_81_1, req->prng);
TEST_ASSERT_EQUAL_STRING(TEST_PAYLOAD, req->payload);
TEST_ASSERT_EQUAL_INT(2, req->channel_ord);

Expand Down

0 comments on commit 45847de

Please sign in to comment.