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

Commit

Permalink
fix(MAM): Add MAM save
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Yu Wei Wu committed Apr 24, 2019
1 parent 4b05f39 commit dccf8ec
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
15 changes: 11 additions & 4 deletions accelerator/apis.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ 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 @@ -210,8 +211,14 @@ status_t api_mam_send_message(const iota_config_t* const tangle,
send_mam_req_t* req = send_mam_req_new();
send_mam_res_t* res = send_mam_res_new();

// Creating MAM API
if (mam_api_init(&mam, (tryte_t*)SEED)) {
// Loading and creating MAM API
if ((rc = mam_api_load(tangle->mam_file, &mam)) ==
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) {
ret = SC_MAM_FAILED_INIT;
goto done;
}
Expand Down Expand Up @@ -255,9 +262,9 @@ status_t api_mam_send_message(const iota_config_t* const tangle,
ret = send_mam_res_serialize(json_result, res);

done:
// Destroying MAM API
// Save and destroying MAM API
if (ret != SC_MAM_FAILED_INIT) {
if (mam_api_destroy(&mam) != RC_OK) {
if (mam_api_save(&mam, tangle->mam_file) || mam_api_destroy(&mam)) {
ret = SC_MAM_FAILED_DESTROYED;
}
}
Expand Down
3 changes: 3 additions & 0 deletions accelerator/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ status_t ta_config_default_init(ta_config_t* const info,
ta_cache_t* const cache,
iota_client_service_t* const service) {
status_t ret = SC_OK;
char mss_tmp[] = "/tmp/XXXXXX";
if (info == NULL || tangle == NULL || cache == NULL || service == NULL) {
return SC_TA_NULL;
}
Expand All @@ -92,6 +93,8 @@ status_t ta_config_default_init(ta_config_t* const info,

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
2 changes: 2 additions & 0 deletions accelerator/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ extern "C" {
#define IRI_HOST "localhost"
#define IRI_PORT 14265
#define MILESTONE_DEPTH 3
#define FSIZE 11
#define MSS_DEPTH 4
#define MWM 14
#define SEED \
Expand All @@ -50,6 +51,7 @@ 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. */
Expand Down

0 comments on commit dccf8ec

Please sign in to comment.