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

Commit 59c8f48

Browse files
authored
Merge pull request #129 from DLTcollab/develop
Pre-release v0.4.0
2 parents ca28a76 + 18dabe8 commit 59c8f48

29 files changed

+591
-318
lines changed

.bazelrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
test --copt='-ggdb3'
2+
coverage -s
3+
coverage --experimental_cc_coverage
4+
coverage --combined_report=lcov
5+
coverage --coverage_report_generator=@bazel_tools//tools/test/CoverageOutputGenerator/java/com/google/devtools/coverageoutputgenerator:Main
6+
coverage --instrumentation_filter="-/tests[/:]"
27

38
# Address Sanitizer:--config asan
49
build:asan --strip=never

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (C) 2018-2019 BiiLabs, Co. Ltd. and Contributors
3+
Copyright (C) 2018-2019 BiiLabs Co., Ltd. and Contributors
44
All Rights Reserved.
55

66
Permission is hereby granted, free of charge, to any person obtaining a copy

Makefile

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ all: $(DEPS)
1010

1111
$(DCURL_LIB): $(DCURL_DIR)
1212
git submodule update --init $^
13-
git submodule update --remote $^
1413
$(MAKE) -C $^ config
1514
@echo
1615
$(info Modify $^/build/local.mk for your environments.)

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
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)
4+
35
`Tangle-accelerator` is a caching proxy server for [IOTA](https://www.iota.org/), which
46
can cache API requests and rewrite their responses as needed to be routed through full
57
nodes. Thus, one instance of `Tangle-accelerator` can serve thousands of Tangle requests
@@ -69,6 +71,20 @@ Before running tangle-accelerator, please edit binding address/port of accelerat
6971
$ make && bazel run //accelerator
7072
```
7173

74+
### Build from docker
75+
76+
If you prefer building a docker image, tangle-accelerator also provides build rules for it. Note that you still have to edit configurations in `accelerator/config.h`.
77+
78+
```
79+
$ make && bazel run //accelerator:ta_image
80+
```
81+
82+
There's also an easier option to pull image from docker hub then simply run with default configs. Please do remember a redis-server is still required in this way.
83+
84+
```
85+
$ docker run -d --net=host --name tangle-accelerator wusyong/tangel-accelerator:latest
86+
```
87+
7288
## Developing
7389

7490
The codebase of this repository follows [Google's C++ guidelines](https://google.github.io/styleguide/cppguide.html):

WORKSPACE

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ load("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository", "new_git_r
22

33
git_repository(
44
name = "rules_iota",
5-
commit = "b15744b9ea520717752c866d5afc769c3b6b68f3",
5+
commit = "1cb59eea62fd1d071de213a9aa46e61e8273472d",
66
remote = "https://github.com/iotaledger/rules_iota.git",
77
)
88

99
git_repository(
1010
name = "entangled",
11-
commit = "8d847ffcecd50f8f3760bfee07d7ed33ecc067bf",
11+
commit = "4960865730640d23e75ffbce84d3f74264cfcd28",
1212
remote = "https://github.com/iotaledger/entangled.git",
1313
)
1414

@@ -18,6 +18,15 @@ git_repository(
1818
remote = "https://github.com/meltwater/served.git",
1919
)
2020

21+
git_repository(
22+
name = "io_bazel_rules_docker",
23+
remote = "https://github.com/bazelbuild/rules_docker.git",
24+
tag = "v0.6.0",
25+
)
26+
2127
load("@rules_iota//:defs.bzl", "iota_deps")
28+
load("@io_bazel_rules_docker//cc:image.bzl", _cc_image_repos = "repositories")
2229

2330
iota_deps()
31+
32+
_cc_image_repos()

accelerator/BUILD

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package(default_visibility = ["//visibility:public"])
22

3+
load("@io_bazel_rules_docker//cc:image.bzl", "cc_image")
4+
35
cc_binary(
46
name = "accelerator",
57
srcs = ["server.cc"],
@@ -11,6 +13,11 @@ cc_binary(
1113
],
1214
)
1315

16+
cc_image(
17+
name = "ta_image",
18+
binary = ":accelerator",
19+
)
20+
1421
cc_library(
1522
name = "apis",
1623
srcs = ["apis.c"],
@@ -36,20 +43,24 @@ cc_library(
3643
":ta_errors",
3744
"//request",
3845
"//response",
39-
"//utils:cache",
40-
"//utils:pow",
4146
"@com_github_uthash//:uthash",
42-
"@entangled//cclient/api",
43-
"@entangled//cclient/types",
4447
"@entangled//common/model:bundle",
4548
"@entangled//utils:time",
4649
],
4750
)
4851

4952
cc_library(
5053
name = "ta_config",
54+
srcs = ["config.c"],
5155
hdrs = ["config.h"],
5256
visibility = ["//visibility:public"],
57+
deps = [
58+
":ta_errors",
59+
"//utils:cache",
60+
"//utils:pow",
61+
"@entangled//cclient/api",
62+
"@entangled//cclient/types",
63+
],
5364
)
5465

5566
cc_library(

accelerator/apis.c

Lines changed: 47 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ status_t api_get_tips(const iota_client_service_t* const service,
4444
return ret;
4545
}
4646

47-
status_t api_get_tips_pair(const iota_client_service_t* const service,
47+
status_t api_get_tips_pair(const iota_config_t* const tangle,
48+
const iota_client_service_t* const service,
4849
char** json_result) {
4950
status_t ret = SC_OK;
5051
ta_get_tips_res_t* res = ta_get_tips_res_new();
@@ -53,7 +54,7 @@ status_t api_get_tips_pair(const iota_client_service_t* const service,
5354
goto done;
5455
}
5556

56-
ret = cclient_get_txn_to_approve(service, res);
57+
ret = cclient_get_txn_to_approve(service, tangle->depth, res);
5758
if (ret) {
5859
goto done;
5960
}
@@ -65,7 +66,8 @@ status_t api_get_tips_pair(const iota_client_service_t* const service,
6566
return ret;
6667
}
6768

68-
status_t api_generate_address(const iota_client_service_t* const service,
69+
status_t api_generate_address(const iota_config_t* const tangle,
70+
const iota_client_service_t* const service,
6971
char** json_result) {
7072
status_t ret = SC_OK;
7173
ta_generate_address_res_t* res = ta_generate_address_res_new();
@@ -74,7 +76,7 @@ status_t api_generate_address(const iota_client_service_t* const service,
7476
goto done;
7577
}
7678

77-
ret = ta_generate_address(service, res);
79+
ret = ta_generate_address(tangle, service, res);
7880
if (ret) {
7981
goto done;
8082
}
@@ -152,54 +154,75 @@ status_t api_find_transactions_obj_by_tag(
152154
}
153155

154156
status_t api_receive_mam_message(const iota_client_service_t* const service,
155-
const char* const obj, char** json_result) {
156-
status_t ret = SC_OK;
157+
const char* const bundle_hash,
158+
char** json_result) {
157159
mam_api_t mam;
158-
160+
status_t ret = SC_OK;
159161
tryte_t* payload_trytes = NULL;
162+
tryte_t* none_chid_trytes = NULL;
163+
char* payload = NULL;
160164
size_t payload_size = 0;
161165
bundle_transactions_t* bundle = NULL;
162166
bundle_transactions_new(&bundle);
163167
bool is_last_packet;
164168

165169
// Creating MAM API
166-
if (mam_api_init(&mam, (tryte_t*)SEED)) {
167-
ret = SC_MAM_OOM;
170+
if (mam_api_init(&mam, (tryte_t*)SEED) != RC_OK) {
171+
ret = SC_MAM_FAILED_INIT;
168172
goto done;
169173
}
170174

171-
// Get bundle which is find_transactions_by_bundle
172-
ret = ta_get_bundle(service, (tryte_t*)obj, bundle);
173-
if (ret) {
175+
ret = ta_get_bundle(service, (tryte_t*)bundle_hash, bundle);
176+
if (ret != SC_OK) {
174177
goto done;
175178
}
176179

177-
// Read MAM message from bundle
180+
// Set first transaction's address as chid, if no `chid` specified
178181
mam_psk_t_set_add(&mam.psks, &psk);
182+
iota_transaction_t* curr_tx = (iota_transaction_t*)utarray_eltptr(bundle, 0);
183+
none_chid_trytes = (tryte_t*)malloc(sizeof(tryte_t) * NUM_TRYTES_ADDRESS);
184+
flex_trits_to_trytes(none_chid_trytes, NUM_TRYTES_ADDRESS,
185+
transaction_address(curr_tx), NUM_TRITS_ADDRESS,
186+
NUM_TRITS_ADDRESS);
187+
mam_api_add_trusted_channel_pk(&mam, none_chid_trytes);
188+
179189
if (mam_api_bundle_read(&mam, bundle, &payload_trytes, &payload_size,
180190
&is_last_packet) == RC_OK) {
181191
if (payload_trytes == NULL || payload_size == 0) {
182-
ret = SC_MAM_NULL;
192+
ret = SC_MAM_NO_PAYLOAD;
193+
goto done;
183194
} else {
184-
char* payload = calloc(payload_size * 2 + 1, sizeof(char));
185-
195+
payload = calloc(payload_size * 2 + 1, sizeof(char));
196+
if (payload == NULL) {
197+
ret = SC_TA_NULL;
198+
goto done;
199+
}
186200
trytes_to_ascii(payload_trytes, payload_size, payload);
187-
*json_result = payload;
188-
189-
payload = NULL;
190-
free(payload_trytes);
191201
}
192202
} else {
193-
ret = SC_MAM_FAILED_RESPONSE;
203+
ret = SC_MAM_NOT_FOUND;
204+
goto done;
194205
}
195206

207+
ret = receive_mam_message_serialize(json_result, &payload);
208+
196209
done:
197-
mam_api_destroy(&mam);
210+
// Destroying MAM API
211+
if (ret != SC_MAM_FAILED_INIT) {
212+
if (mam_api_destroy(&mam) != RC_OK) {
213+
ret = SC_MAM_FAILED_DESTROYED;
214+
}
215+
}
216+
free(none_chid_trytes);
217+
free(payload_trytes);
218+
free(payload);
198219
bundle_transactions_free(&bundle);
220+
199221
return ret;
200222
}
201223

202-
status_t api_send_transfer(const iota_client_service_t* const service,
224+
status_t api_send_transfer(const iota_config_t* const tangle,
225+
const iota_client_service_t* const service,
203226
const char* const obj, char** json_result) {
204227
status_t ret = SC_OK;
205228
char hash_trytes[NUM_TRYTES_HASH + 1];
@@ -218,7 +241,7 @@ status_t api_send_transfer(const iota_client_service_t* const service,
218241
goto done;
219242
}
220243

221-
ret = ta_send_transfer(service, req, res);
244+
ret = ta_send_transfer(tangle, service, req, res);
222245
if (ret) {
223246
goto done;
224247
}

accelerator/apis.h

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "common/trinary/trit_tryte.h"
88
#include "common/trinary/tryte_ascii.h"
99
#include "mam/api/api.h"
10+
#include "mam/mam/mam_channel_t_set.h"
1011
#include "serializer/serializer.h"
1112

1213
#ifdef __cplusplus
@@ -28,14 +29,16 @@ extern "C" {
2829
* Generate and return an unused address from the seed. An unused address means
2930
* the address does not have any transaction with it yet.
3031
*
32+
* @param[in] tangle IOTA API parameter configurations
3133
* @param[in] service IRI node end point service
3234
* @param[out] json_result Result containing an unused address in json format
3335
*
3436
* @return
3537
* - SC_OK on success
3638
* - non-zero on error
3739
*/
38-
status_t api_generate_address(const iota_client_service_t* const service,
40+
status_t api_generate_address(const iota_config_t* const tangle,
41+
const iota_client_service_t* const service,
3942
char** json_result);
4043

4144
/**
@@ -44,14 +47,16 @@ status_t api_generate_address(const iota_client_service_t* const service,
4447
* Get a tips pair as trunk/branch transactions for transaction construction.
4548
* The result is char array in json format:
4649
*
50+
* @param[in] tangle IOTA API parameter configurations
4751
* @param[in] service IRI node end point service
4852
* @param[out] json_result Result containing a tips pair in json format
4953
*
5054
* @return
5155
* - SC_OK on success
5256
* - non-zero on error
5357
*/
54-
status_t api_get_tips_pair(const iota_client_service_t* const service,
58+
status_t api_get_tips_pair(const iota_config_t* const tangle,
59+
const iota_client_service_t* const service,
5560
char** json_result);
5661

5762
/**
@@ -76,15 +81,16 @@ status_t api_get_tips(const iota_client_service_t* const service,
7681
* Receive a MAM message from given bundle hash.
7782
*
7883
* @param[in] service IRI node end point service
79-
* @param[out] obj bundle hash in trytes
84+
* @param[in] bundle_hash bundle hash decoded in trytes string
8085
* @param[out] json_result Result containing an unused address in json format
8186
*
8287
* @return
8388
* - SC_OK on success
8489
* - non-zero on error
8590
*/
8691
status_t api_receive_mam_message(const iota_client_service_t* const service,
87-
const char* const obj, char** json_result);
92+
const char* const bundle_hash,
93+
char** json_result);
8894

8995
/**
9096
* @brief Send transfer to tangle.
@@ -93,6 +99,7 @@ status_t api_receive_mam_message(const iota_client_service_t* const service,
9399
* fields include address, value, tag, and message. This API would also try to
94100
* find the transactions after bundle sent.
95101
*
102+
* @param[in] tangle IOTA API parameter configurations
96103
* @param[in] service IRI node end point service
97104
* @param[in] obj Input data in JSON
98105
* @param[out] json_result Result containing transaction objects in json format
@@ -101,7 +108,8 @@ status_t api_receive_mam_message(const iota_client_service_t* const service,
101108
* - SC_OK on success
102109
* - non-zero on error
103110
*/
104-
status_t api_send_transfer(const iota_client_service_t* const service,
111+
status_t api_send_transfer(const iota_config_t* const tangle,
112+
const iota_client_service_t* const service,
105113
const char* const obj, char** json_result);
106114

107115
/**

0 commit comments

Comments
 (0)