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

Commit

Permalink
Merge pull request #161 from DLTcollab/develop
Browse files Browse the repository at this point in the history
Pre-release v0.5.0
  • Loading branch information
Wu Yu Wei committed Apr 30, 2019
2 parents 59c8f48 + 29ec953 commit 6c43471
Show file tree
Hide file tree
Showing 30 changed files with 1,201 additions and 86 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ bazel*
cclient/bazel*
cclient/cmake-*

coverage

# Atom conf
.clang_complete

Expand Down
7 changes: 7 additions & 0 deletions BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")

pkg_tar(
name = "pkg_coverage",
srcs = glob(["coverage/*"]),
package_dir = "./coverage",
)
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Tangle-accelerator

[![Build Status](https://badge.buildkite.com/46ec07b122bde13f984c241fe8b38e64698c5c0d816ee6c7e4.svg)](https://buildkite.com/dltcollab/dcurl-test) [![Gitter](https://img.shields.io/gitter/room/DLTcollab/tangle-accelerator.svg)](https://gitter.im/DLTcollab/tangle-accelerator) ![GitHub release](https://img.shields.io/github/release-pre/DLTcollab/tangle-accelerator.svg)
[![Build Status](https://badge.buildkite.com/0deb4c46f2f69363e4d326014843b92853733f243f379c70b5.svg)](https://buildkite.com/dltcollab/tangle-accelerator-test) [![Gitter](https://img.shields.io/gitter/room/DLTcollab/tangle-accelerator.svg)](https://gitter.im/DLTcollab/tangle-accelerator) [![GitHub release](https://img.shields.io/github/release-pre/DLTcollab/tangle-accelerator.svg)](https://github.com/DLTcollab/tangle-accelerator/releases)

`Tangle-accelerator` is a caching proxy server for [IOTA](https://www.iota.org/), which
can cache API requests and rewrite their responses as needed to be routed through full
Expand Down
8 changes: 7 additions & 1 deletion accelerator/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ cc_library(
":ta_errors",
"//serializer",
"@entangled//common/trinary:trit_tryte",
"@entangled//common/trinary:tryte_ascii",
"@entangled//mam/api",
],
)
Expand All @@ -55,6 +54,7 @@ cc_library(
hdrs = ["config.h"],
visibility = ["//visibility:public"],
deps = [
":message",
":ta_errors",
"//utils:cache",
"//utils:pow",
Expand All @@ -68,3 +68,9 @@ cc_library(
hdrs = ["errors.h"],
visibility = ["//visibility:public"],
)

cc_library(
name = "message",
srcs = ["message.c"],
hdrs = ["message.h"],
)
103 changes: 78 additions & 25 deletions accelerator/apis.c
Original file line number Diff line number Diff line change
@@ -1,28 +1,5 @@
#include "apis.h"

// TODO: Generate actual pre shared keys
static mam_psk_t const psk = {
.id = {1, 0, -1, -1, 0, -1, -1, 0, 0, 1, -1, 0, 1, 0, 0, 1, 1,
1, -1, 1, 1, 0, 1, 1, 0, 0, -1, 1, -1, -1, -1, -1, -1, -1,
-1, 1, -1, -1, 0, -1, -1, 1, 0, -1, -1, -1, 1, 1, 1, 0, 0,
-1, 1, -1, -1, -1, 0, -1, 1, -1, -1, -1, 1, 1, -1, 1, 0, 0,
1, 1, 1, -1, -1, 0, 0, -1, -1, 1, 0, -1, 1},
.key = {-1, 1, -1, -1, 1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1, -1, -1,
-1, 0, 0, 0, 0, -1, -1, 1, 1, 1, 0, -1, -1, -1, 0, 0, 0,
-1, -1, 1, -1, 0, 0, 1, 0, 0, -1, 1, 1, 0, -1, 0, 0, 1,
-1, 1, 0, 1, 0, 0, -1, 1, 1, -1, 1, 0, -1, 0, -1, 1, -1,
-1, -1, 0, -1, -1, 0, -1, -1, 0, 0, -1, -1, 1, -1, 0, 0, -1,
-1, -1, -1, 0, -1, -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, 0, 1,
0, 1, -1, 0, 0, 1, 0, 1, 0, 0, 1, 0, -1, 0, 1, 1, 0,
0, -1, -1, 1, 1, 0, 0, 1, -1, 1, 1, 1, 0, 1, 1, 1, 0,
0, -1, -1, -1, -1, 1, 1, 1, 0, 0, -1, 0, 1, -1, 1, 1, 1,
0, 0, 1, -1, -1, 0, -1, 1, -1, 1, 0, 0, 1, -1, 0, 1, -1,
0, 0, 1, 1, 1, 1, 1, 0, 0, 1, -1, 1, -1, 1, 0, 1, 1,
1, -1, 0, 0, -1, 1, 1, 0, -1, -1, 0, 0, -1, 1, 0, 1, -1,
0, 0, -1, 1, -1, 1, 1, 1, -1, 0, 1, 1, 0, 0, -1, -1, -1,
0, 0, 1, 0, 1, 0, -1, 1, -1, 0, 1, 0, -1, 1, 1, -1, -1,
0, 0, -1, 0, -1}};

status_t api_get_tips(const iota_client_service_t* const service,
char** json_result) {
status_t ret = SC_OK;
Expand Down Expand Up @@ -54,7 +31,7 @@ status_t api_get_tips_pair(const iota_config_t* const tangle,
goto done;
}

ret = cclient_get_txn_to_approve(service, tangle->depth, res);
ret = cclient_get_txn_to_approve(service, tangle->milestone_depth, res);
if (ret) {
goto done;
}
Expand Down Expand Up @@ -178,7 +155,6 @@ status_t api_receive_mam_message(const iota_client_service_t* const service,
}

// Set first transaction's address as chid, if no `chid` specified
mam_psk_t_set_add(&mam.psks, &psk);
iota_transaction_t* curr_tx = (iota_transaction_t*)utarray_eltptr(bundle, 0);
none_chid_trytes = (tryte_t*)malloc(sizeof(tryte_t) * NUM_TRYTES_ADDRESS);
flex_trits_to_trytes(none_chid_trytes, NUM_TRYTES_ADDRESS,
Expand Down Expand Up @@ -221,6 +197,83 @@ status_t api_receive_mam_message(const iota_client_service_t* const service,
return ret;
}

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;
bundle_transactions_new(&bundle);
tryte_t channel_id[MAM_CHANNEL_ID_SIZE];
trit_t msg_id[MAM_MSG_ID_SIZE];
send_mam_req_t* req = send_mam_req_new();
send_mam_res_t* res = send_mam_res_new();

// 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;
}

ret = send_mam_req_deserialize(payload, req);
if (ret) {
goto done;
}

// Create mam channel
if (mam_channel_t_set_size(mam.channels) == 0) {
mam_api_create_channel(&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);
}

// Write header and packet
if (mam_api_bundle_write_header_on_channel(&mam, channel_id, NULL, NULL, 0,
bundle, msg_id) != RC_OK) {
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) {
ret = SC_MAM_FAILED_WRITE;
goto done;
}
send_mam_res_set_channel_id(res, channel_id);

// Sending bundle
if (ta_send_bundle(tangle, service, bundle) != SC_OK) {
ret = SC_MAM_FAILED_RESPONSE;
goto done;
}
send_mam_res_set_bundle_hash(
res, transaction_bundle((iota_transaction_t*)utarray_front(bundle)));

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) || mam_api_destroy(&mam)) {
ret = SC_MAM_FAILED_DESTROYED;
}
}
bundle_transactions_free(&bundle);
send_mam_req_free(&req);
send_mam_res_free(&res);
return ret;
}

status_t api_send_transfer(const iota_config_t* const tangle,
const iota_client_service_t* const service,
const char* const obj, char** json_result) {
Expand Down
21 changes: 20 additions & 1 deletion accelerator/apis.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "accelerator/errors.h"
#include "cclient/types/types.h"
#include "common/trinary/trit_tryte.h"
#include "common/trinary/tryte_ascii.h"
#include "mam/api/api.h"
#include "mam/mam/mam_channel_t_set.h"
#include "serializer/serializer.h"
Expand Down Expand Up @@ -92,6 +91,26 @@ status_t api_receive_mam_message(const iota_client_service_t* const service,
const char* const bundle_hash,
char** json_result);

/**
* @brief Send a MAM message with given Payload.
*
* Send a MAM message from given Payload(ascii message).
* There is no need to decode the ascii payload to tryte, since the
* api_mam_send_message() will take this job.
*
* @param[in] tangle IOTA API parameter configurations
* @param[in] service IRI node end point service
* @param[in] payload message to send undecoded ascii string.
* @param[out] json_result Result containing channel id and bundle hash
*
* @return
* - SC_OK on success
* - non-zero on error
*/
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);

/**
* @brief Send transfer to tangle.
*
Expand Down
27 changes: 26 additions & 1 deletion accelerator/common_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ status_t ta_send_trytes(const iota_config_t* const tangle,
}

// get transaction to approve
ret = cclient_get_txn_to_approve(service, tangle->depth, get_txn_res);
ret =
cclient_get_txn_to_approve(service, tangle->milestone_depth, get_txn_res);
if (ret) {
goto done;
}
Expand Down Expand Up @@ -520,3 +521,27 @@ status_t ta_get_bundle(const iota_client_service_t* const service,
find_transactions_req_free(&find_tx_req);
return ret;
}

status_t ta_send_bundle(const iota_config_t* const tangle,
const iota_client_service_t* const service,
bundle_transactions_t* const bundle) {
Kerl kerl;
kerl_init(&kerl);
bundle_finalize(bundle, &kerl);
transaction_array_t* out_tx_objs = transaction_array_new();
hash8019_array_p raw_trytes = hash8019_array_new();
iota_transaction_t* curr_tx = NULL;
flex_trit_t trits_8019[FLEX_TRIT_SIZE_8019];

BUNDLE_FOREACH(bundle, curr_tx) {
transaction_serialize_on_flex_trits(curr_tx, trits_8019);
hash_array_push(raw_trytes, trits_8019);
}

ta_send_trytes(tangle, service, raw_trytes);

hash_array_free(raw_trytes);
transaction_array_free(out_tx_objs);

return SC_OK;
}
18 changes: 18 additions & 0 deletions accelerator/common_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,24 @@ status_t ta_get_bundle(const iota_client_service_t* const service,
tryte_t const* const bundle_hash,
bundle_transactions_t* const bundle);

/**
* @brief Send bundle object.
*
* Send the unpacked bundle which contains transactions. MAM functions should
* send message with this function.
*
* @param[in] service IRI node end point service
* @param[in] bundle bundle object to send
* @param[out] bundle Result containing bundle object in bundle_transactions_t
*
* @return
* - SC_OK on success
* - non-zero on error
*/
status_t ta_send_bundle(const iota_config_t* const tangle,
const iota_client_service_t* const service,
bundle_transactions_t* const bundle);

#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit 6c43471

Please sign in to comment.