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

Implement api_send_transfer #65

Merged
merged 4 commits into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions accelerator/apis.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,45 @@ int api_find_transactions_obj_by_tag(const iota_client_service_t* const service,
ta_find_transactions_obj_res_free(&res);
return ret;
}

int api_send_transfer(const iota_client_service_t* const service,
const char* const obj, char** json_result) {
int ret = 0;
char hash_trytes[NUM_TRYTES_HASH + 1];
ta_send_transfer_req_t* req = ta_send_transfer_req_new();
ta_send_transfer_res_t* res = ta_send_transfer_res_new();
ta_get_transaction_object_res_t* txn_obj_res =
ta_get_transaction_object_res_new();

if (req == NULL || res == NULL || txn_obj_res == NULL) {
goto done;
}

ret = ta_send_transfer_req_deserialize(obj, req);
if (ret) {
goto done;
}

ret = ta_send_transfer(service, req, res);
if (ret) {
goto done;
}

// return transaction object
flex_trits_to_trytes((tryte_t*)hash_trytes, NUM_TRYTES_HASH,
hash243_queue_peek(res->hash), NUM_TRITS_HASH,
NUM_TRITS_HASH);
hash_trytes[NUM_TRYTES_HASH] = '\0';
ret = ta_get_transaction_object(service, hash_trytes, txn_obj_res);
if (ret) {
goto done;
}

ret = ta_get_transaction_object_res_serialize(json_result, txn_obj_res);

done:
ta_send_transfer_req_free(&req);
ta_send_transfer_res_free(&res);
ta_get_transaction_object_res_free(&txn_obj_res);
return ret;
}
2 changes: 2 additions & 0 deletions accelerator/common_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ int ta_attach_to_tangle(const attach_to_tangle_req_t* const req,
bundle_transactions_t* bundle = NULL;
iota_transaction_t tx;
flex_trit_t* elt = NULL;
pow_init();

// create bundle
bundle_transactions_new(&bundle);
Expand Down Expand Up @@ -134,6 +135,7 @@ int ta_attach_to_tangle(const attach_to_tangle_req_t* const req,
}

done:
pow_destroy();
bundle_transactions_free(&bundle);
return ret;
}
Expand Down
25 changes: 25 additions & 0 deletions accelerator/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,31 @@ int main(int, char const**) {
res << json_result;
});

/**
* @method {post} /transaction send transfer
*
* @return {String} transaction object
*/
mux.handle("/transaction")
.post([&](served::response& res, const served::request& req) {
char* json_result;

if (req.header("content-type") != "application/json") {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does operator != perform byte-by-byte character comparison?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to spec 21.4.8.3 operator !=
The above comparison fit this situation:

template<class charT, class traits, class Allocator>
   bool operator!=(const basic_string<charT,traits,Allocator>& lhs,
                            const charT* rhs);
Requires: rhs points to an array of at least traits::length(rhs) + 1elements of charT.
Returns: lhs.compare(rhs) != 0

And compare performs byte-by-byte character comparison, in [1] it uses the phrase lexicographical comparison([2]).

references:

cJSON* json_obj = cJSON_CreateObject();
cJSON_AddStringToObject(json_obj, "message",
"Invalid request header");
json_result = cJSON_PrintUnformatted(json_obj);

res.set_status(SC_BAD_REQUEST);
cJSON_Delete(json_obj);
} else {
api_send_transfer(&service, req.body().c_str(), &json_result);
}

res.set_header("content-type", "application/json");
res << json_result;
});

/**
* @method {get} / Client bad request
*
Expand Down
21 changes: 0 additions & 21 deletions serializer/serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,27 +271,6 @@ int ta_send_transfer_req_deserialize(const char* const obj,
return ret;
}

int ta_send_transfer_res_serialize(char** obj,
const ta_send_transfer_res_t* const res) {
cJSON* json_root = cJSON_CreateObject();
if (json_root == NULL) {
return -1;
}

char trytes_out[NUM_TRYTES_HASH + 1];
flex_trits_to_trytes((tryte_t*)trytes_out, NUM_TRYTES_HASH, res->hash->hash,
NUM_TRITS_HASH, NUM_TRITS_HASH);
trytes_out[NUM_TRYTES_HASH] = '\0';

cJSON_AddStringToObject(json_root, "hash", trytes_out);
*obj = cJSON_PrintUnformatted(json_root);
if (*obj == NULL) {
return -1;
}
cJSON_Delete(json_root);
return 0;
}

int ta_get_transaction_object_res_serialize(
char** obj, const ta_get_transaction_object_res_t* const res) {
int ret = 0;
Expand Down
2 changes: 0 additions & 2 deletions serializer/serializer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ int ta_generate_address_res_serialize(
int ta_get_tips_res_serialize(char** obj, const ta_get_tips_res_t* const res);
int ta_send_transfer_req_deserialize(const char* const obj,
ta_send_transfer_req_t* req);
int ta_send_transfer_res_serialize(char** obj,
const ta_send_transfer_res_t* const res);
int ta_get_transaction_object_res_serialize(
char** obj, const ta_get_transaction_object_res_t* const res);
int ta_find_transactions_res_serialize(
Expand Down
17 changes: 0 additions & 17 deletions tests/test_serializer.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,22 +72,6 @@ void test_deserialize_ta_send_transfer(void) {
ta_send_transfer_req_free(&req);
}

void test_serialize_ta_send_transfer(void) {
const char* json = "{\"hash\":\"" TRYTES_81_1 "\"}";
char* json_result;
flex_trit_t hash_trits_1[FLEX_TRIT_SIZE_243];
ta_send_transfer_res_t* res = ta_send_transfer_res_new();

flex_trits_from_trytes(hash_trits_1, NUM_TRITS_HASH,
(const tryte_t*)TRYTES_81_1, NUM_TRYTES_HASH,
NUM_TRYTES_HASH);
hash243_queue_push(&res->hash, hash_trits_1);
ta_send_transfer_res_serialize(&json_result, res);
TEST_ASSERT_EQUAL_STRING(json, json_result);
ta_send_transfer_res_free(&res);
free(json_result);
}

void test_serialize_ta_get_transaction_object(void) {
const char* json =
"{\"hash\":\"" TRYTES_81_1 "\","
Expand Down Expand Up @@ -273,7 +257,6 @@ int main(void) {
RUN_TEST(test_serialize_ta_get_tips);
RUN_TEST(test_serialize_ta_generate_address);
RUN_TEST(test_deserialize_ta_send_transfer);
RUN_TEST(test_serialize_ta_send_transfer);
RUN_TEST(test_serialize_ta_get_transaction_object);
RUN_TEST(test_serialize_ta_find_transactions_by_tag);
RUN_TEST(test_serialize_ta_find_transactions_obj_by_tag);
Expand Down