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

Commit dccf8ec

Browse files
author
Yu Wei Wu
committed
fix(MAM): Add MAM save
MAM API can be serialized and saved into files. This PR create a temporarily file to save MSS after first time. Send MAM message can be faster in second call and after in this way.
1 parent 4b05f39 commit dccf8ec

File tree

3 files changed

+16
-4
lines changed

3 files changed

+16
-4
lines changed

accelerator/apis.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ status_t api_mam_send_message(const iota_config_t* const tangle,
201201
const iota_client_service_t* const service,
202202
char const* const payload, char** json_result) {
203203
status_t ret = SC_OK;
204+
retcode_t rc = RC_OK;
204205
mam_api_t mam;
205206
const bool last_packet = true;
206207
bundle_transactions_t* bundle = NULL;
@@ -210,8 +211,14 @@ status_t api_mam_send_message(const iota_config_t* const tangle,
210211
send_mam_req_t* req = send_mam_req_new();
211212
send_mam_res_t* res = send_mam_res_new();
212213

213-
// Creating MAM API
214-
if (mam_api_init(&mam, (tryte_t*)SEED)) {
214+
// Loading and creating MAM API
215+
if ((rc = mam_api_load(tangle->mam_file, &mam)) ==
216+
RC_UTILS_FAILED_TO_OPEN_FILE) {
217+
if (mam_api_init(&mam, (tryte_t*)SEED)) {
218+
ret = SC_MAM_FAILED_INIT;
219+
goto done;
220+
}
221+
} else if (rc != RC_OK) {
215222
ret = SC_MAM_FAILED_INIT;
216223
goto done;
217224
}
@@ -255,9 +262,9 @@ status_t api_mam_send_message(const iota_config_t* const tangle,
255262
ret = send_mam_res_serialize(json_result, res);
256263

257264
done:
258-
// Destroying MAM API
265+
// Save and destroying MAM API
259266
if (ret != SC_MAM_FAILED_INIT) {
260-
if (mam_api_destroy(&mam) != RC_OK) {
267+
if (mam_api_save(&mam, tangle->mam_file) || mam_api_destroy(&mam)) {
261268
ret = SC_MAM_FAILED_DESTROYED;
262269
}
263270
}

accelerator/config.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ status_t ta_config_default_init(ta_config_t* const info,
7272
ta_cache_t* const cache,
7373
iota_client_service_t* const service) {
7474
status_t ret = SC_OK;
75+
char mss_tmp[] = "/tmp/XXXXXX";
7576
if (info == NULL || tangle == NULL || cache == NULL || service == NULL) {
7677
return SC_TA_NULL;
7778
}
@@ -92,6 +93,8 @@ status_t ta_config_default_init(ta_config_t* const info,
9293

9394
log_info(logger_id, "Initializing IRI configuration\n");
9495
tangle->milestone_depth = MILESTONE_DEPTH;
96+
mkstemp(mss_tmp);
97+
strncpy(tangle->mam_file, mss_tmp, FSIZE);
9598
tangle->mss_depth = MSS_DEPTH;
9699
tangle->mwm = MWM;
97100
tangle->seed = SEED;

accelerator/config.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ extern "C" {
2727
#define IRI_HOST "localhost"
2828
#define IRI_PORT 14265
2929
#define MILESTONE_DEPTH 3
30+
#define FSIZE 11
3031
#define MSS_DEPTH 4
3132
#define MWM 14
3233
#define SEED \
@@ -50,6 +51,7 @@ typedef struct ta_info_s {
5051
/** struct type of iota configuration */
5152
typedef struct ta_config_s {
5253
uint8_t milestone_depth; /**< Depth of API argument */
54+
char mam_file[FSIZE]; /** Save file for mam struct like MSS, skn... */
5355
uint8_t mss_depth; /**< Depth of MSS layer merkle tree */
5456
uint8_t mwm; /**< Minimum weight magnitude of API argument */
5557
/** Seed to generate address. This does not do any signature yet. */

0 commit comments

Comments
 (0)