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

Commit 6c43471

Browse files
author
Wu Yu Wei
authored
Merge pull request #161 from DLTcollab/develop
Pre-release v0.5.0
2 parents 59c8f48 + 29ec953 commit 6c43471

30 files changed

+1201
-86
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ bazel*
7676
cclient/bazel*
7777
cclient/cmake-*
7878

79+
coverage
80+
7981
# Atom conf
8082
.clang_complete
8183

BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
load("@bazel_tools//tools/build_defs/pkg:pkg.bzl", "pkg_tar")
2+
3+
pkg_tar(
4+
name = "pkg_coverage",
5+
srcs = glob(["coverage/*"]),
6+
package_dir = "./coverage",
7+
)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Tangle-accelerator
22

3-
[![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)
3+
[![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)
44

55
`Tangle-accelerator` is a caching proxy server for [IOTA](https://www.iota.org/), which
66
can cache API requests and rewrite their responses as needed to be routed through full

accelerator/BUILD

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ cc_library(
2828
":ta_errors",
2929
"//serializer",
3030
"@entangled//common/trinary:trit_tryte",
31-
"@entangled//common/trinary:tryte_ascii",
3231
"@entangled//mam/api",
3332
],
3433
)
@@ -55,6 +54,7 @@ cc_library(
5554
hdrs = ["config.h"],
5655
visibility = ["//visibility:public"],
5756
deps = [
57+
":message",
5858
":ta_errors",
5959
"//utils:cache",
6060
"//utils:pow",
@@ -68,3 +68,9 @@ cc_library(
6868
hdrs = ["errors.h"],
6969
visibility = ["//visibility:public"],
7070
)
71+
72+
cc_library(
73+
name = "message",
74+
srcs = ["message.c"],
75+
hdrs = ["message.h"],
76+
)

accelerator/apis.c

Lines changed: 78 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,5 @@
11
#include "apis.h"
22

3-
// TODO: Generate actual pre shared keys
4-
static mam_psk_t const psk = {
5-
.id = {1, 0, -1, -1, 0, -1, -1, 0, 0, 1, -1, 0, 1, 0, 0, 1, 1,
6-
1, -1, 1, 1, 0, 1, 1, 0, 0, -1, 1, -1, -1, -1, -1, -1, -1,
7-
-1, 1, -1, -1, 0, -1, -1, 1, 0, -1, -1, -1, 1, 1, 1, 0, 0,
8-
-1, 1, -1, -1, -1, 0, -1, 1, -1, -1, -1, 1, 1, -1, 1, 0, 0,
9-
1, 1, 1, -1, -1, 0, 0, -1, -1, 1, 0, -1, 1},
10-
.key = {-1, 1, -1, -1, 1, -1, -1, 0, 0, 0, -1, -1, 1, 1, 1, -1, -1,
11-
-1, 0, 0, 0, 0, -1, -1, 1, 1, 1, 0, -1, -1, -1, 0, 0, 0,
12-
-1, -1, 1, -1, 0, 0, 1, 0, 0, -1, 1, 1, 0, -1, 0, 0, 1,
13-
-1, 1, 0, 1, 0, 0, -1, 1, 1, -1, 1, 0, -1, 0, -1, 1, -1,
14-
-1, -1, 0, -1, -1, 0, -1, -1, 0, 0, -1, -1, 1, -1, 0, 0, -1,
15-
-1, -1, -1, 0, -1, -1, -1, 1, -1, -1, 1, 1, 1, 1, 1, 0, 1,
16-
0, 1, -1, 0, 0, 1, 0, 1, 0, 0, 1, 0, -1, 0, 1, 1, 0,
17-
0, -1, -1, 1, 1, 0, 0, 1, -1, 1, 1, 1, 0, 1, 1, 1, 0,
18-
0, -1, -1, -1, -1, 1, 1, 1, 0, 0, -1, 0, 1, -1, 1, 1, 1,
19-
0, 0, 1, -1, -1, 0, -1, 1, -1, 1, 0, 0, 1, -1, 0, 1, -1,
20-
0, 0, 1, 1, 1, 1, 1, 0, 0, 1, -1, 1, -1, 1, 0, 1, 1,
21-
1, -1, 0, 0, -1, 1, 1, 0, -1, -1, 0, 0, -1, 1, 0, 1, -1,
22-
0, 0, -1, 1, -1, 1, 1, 1, -1, 0, 1, 1, 0, 0, -1, -1, -1,
23-
0, 0, 1, 0, 1, 0, -1, 1, -1, 0, 1, 0, -1, 1, 1, -1, -1,
24-
0, 0, -1, 0, -1}};
25-
263
status_t api_get_tips(const iota_client_service_t* const service,
274
char** json_result) {
285
status_t ret = SC_OK;
@@ -54,7 +31,7 @@ status_t api_get_tips_pair(const iota_config_t* const tangle,
5431
goto done;
5532
}
5633

57-
ret = cclient_get_txn_to_approve(service, tangle->depth, res);
34+
ret = cclient_get_txn_to_approve(service, tangle->milestone_depth, res);
5835
if (ret) {
5936
goto done;
6037
}
@@ -178,7 +155,6 @@ status_t api_receive_mam_message(const iota_client_service_t* const service,
178155
}
179156

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

200+
status_t api_mam_send_message(const iota_config_t* const tangle,
201+
const iota_client_service_t* const service,
202+
char const* const payload, char** json_result) {
203+
status_t ret = SC_OK;
204+
retcode_t rc = RC_OK;
205+
mam_api_t mam;
206+
const bool last_packet = true;
207+
bundle_transactions_t* bundle = NULL;
208+
bundle_transactions_new(&bundle);
209+
tryte_t channel_id[MAM_CHANNEL_ID_SIZE];
210+
trit_t msg_id[MAM_MSG_ID_SIZE];
211+
send_mam_req_t* req = send_mam_req_new();
212+
send_mam_res_t* res = send_mam_res_new();
213+
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) {
222+
ret = SC_MAM_FAILED_INIT;
223+
goto done;
224+
}
225+
226+
ret = send_mam_req_deserialize(payload, req);
227+
if (ret) {
228+
goto done;
229+
}
230+
231+
// Create mam channel
232+
if (mam_channel_t_set_size(mam.channels) == 0) {
233+
mam_api_create_channel(&mam, tangle->mss_depth, channel_id);
234+
} else {
235+
mam_channel_t* channel = &mam.channels->value;
236+
trits_to_trytes(trits_begin(mam_channel_id(channel)), channel_id,
237+
NUM_TRITS_ADDRESS);
238+
}
239+
240+
// Write header and packet
241+
if (mam_api_bundle_write_header_on_channel(&mam, channel_id, NULL, NULL, 0,
242+
bundle, msg_id) != RC_OK) {
243+
ret = SC_MAM_FAILED_WRITE;
244+
goto done;
245+
}
246+
if (mam_api_bundle_write_packet(&mam, msg_id, req->payload_trytes,
247+
req->payload_trytes_size, 0, last_packet,
248+
bundle) != RC_OK) {
249+
ret = SC_MAM_FAILED_WRITE;
250+
goto done;
251+
}
252+
send_mam_res_set_channel_id(res, channel_id);
253+
254+
// Sending bundle
255+
if (ta_send_bundle(tangle, service, bundle) != SC_OK) {
256+
ret = SC_MAM_FAILED_RESPONSE;
257+
goto done;
258+
}
259+
send_mam_res_set_bundle_hash(
260+
res, transaction_bundle((iota_transaction_t*)utarray_front(bundle)));
261+
262+
ret = send_mam_res_serialize(json_result, res);
263+
264+
done:
265+
// Save and destroying MAM API
266+
if (ret != SC_MAM_FAILED_INIT) {
267+
if (mam_api_save(&mam, tangle->mam_file) || mam_api_destroy(&mam)) {
268+
ret = SC_MAM_FAILED_DESTROYED;
269+
}
270+
}
271+
bundle_transactions_free(&bundle);
272+
send_mam_req_free(&req);
273+
send_mam_res_free(&res);
274+
return ret;
275+
}
276+
224277
status_t api_send_transfer(const iota_config_t* const tangle,
225278
const iota_client_service_t* const service,
226279
const char* const obj, char** json_result) {

accelerator/apis.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
#include "accelerator/errors.h"
66
#include "cclient/types/types.h"
77
#include "common/trinary/trit_tryte.h"
8-
#include "common/trinary/tryte_ascii.h"
98
#include "mam/api/api.h"
109
#include "mam/mam/mam_channel_t_set.h"
1110
#include "serializer/serializer.h"
@@ -92,6 +91,26 @@ status_t api_receive_mam_message(const iota_client_service_t* const service,
9291
const char* const bundle_hash,
9392
char** json_result);
9493

94+
/**
95+
* @brief Send a MAM message with given Payload.
96+
*
97+
* Send a MAM message from given Payload(ascii message).
98+
* There is no need to decode the ascii payload to tryte, since the
99+
* api_mam_send_message() will take this job.
100+
*
101+
* @param[in] tangle IOTA API parameter configurations
102+
* @param[in] service IRI node end point service
103+
* @param[in] payload message to send undecoded ascii string.
104+
* @param[out] json_result Result containing channel id and bundle hash
105+
*
106+
* @return
107+
* - SC_OK on success
108+
* - non-zero on error
109+
*/
110+
status_t api_mam_send_message(const iota_config_t* const tangle,
111+
const iota_client_service_t* const service,
112+
char const* const payload, char** json_result);
113+
95114
/**
96115
* @brief Send transfer to tangle.
97116
*

accelerator/common_core.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,8 @@ status_t ta_send_trytes(const iota_config_t* const tangle,
169169
}
170170

171171
// get transaction to approve
172-
ret = cclient_get_txn_to_approve(service, tangle->depth, get_txn_res);
172+
ret =
173+
cclient_get_txn_to_approve(service, tangle->milestone_depth, get_txn_res);
173174
if (ret) {
174175
goto done;
175176
}
@@ -520,3 +521,27 @@ status_t ta_get_bundle(const iota_client_service_t* const service,
520521
find_transactions_req_free(&find_tx_req);
521522
return ret;
522523
}
524+
525+
status_t ta_send_bundle(const iota_config_t* const tangle,
526+
const iota_client_service_t* const service,
527+
bundle_transactions_t* const bundle) {
528+
Kerl kerl;
529+
kerl_init(&kerl);
530+
bundle_finalize(bundle, &kerl);
531+
transaction_array_t* out_tx_objs = transaction_array_new();
532+
hash8019_array_p raw_trytes = hash8019_array_new();
533+
iota_transaction_t* curr_tx = NULL;
534+
flex_trit_t trits_8019[FLEX_TRIT_SIZE_8019];
535+
536+
BUNDLE_FOREACH(bundle, curr_tx) {
537+
transaction_serialize_on_flex_trits(curr_tx, trits_8019);
538+
hash_array_push(raw_trytes, trits_8019);
539+
}
540+
541+
ta_send_trytes(tangle, service, raw_trytes);
542+
543+
hash_array_free(raw_trytes);
544+
transaction_array_free(out_tx_objs);
545+
546+
return SC_OK;
547+
}

accelerator/common_core.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,24 @@ status_t ta_get_bundle(const iota_client_service_t* const service,
195195
tryte_t const* const bundle_hash,
196196
bundle_transactions_t* const bundle);
197197

198+
/**
199+
* @brief Send bundle object.
200+
*
201+
* Send the unpacked bundle which contains transactions. MAM functions should
202+
* send message with this function.
203+
*
204+
* @param[in] service IRI node end point service
205+
* @param[in] bundle bundle object to send
206+
* @param[out] bundle Result containing bundle object in bundle_transactions_t
207+
*
208+
* @return
209+
* - SC_OK on success
210+
* - non-zero on error
211+
*/
212+
status_t ta_send_bundle(const iota_config_t* const tangle,
213+
const iota_client_service_t* const service,
214+
bundle_transactions_t* const bundle);
215+
198216
#ifdef __cplusplus
199217
}
200218
#endif

0 commit comments

Comments
 (0)