forked from iotaledger/entangled
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_inclusion_states.c
111 lines (90 loc) · 3.42 KB
/
get_inclusion_states.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* Copyright (c) 2019 IOTA Stiftung
* https://github.com/iotaledger/entangled
*
* Refer to the LICENSE file for licensing information
*/
#include "cclient/serialization/json/get_inclusion_states.h"
#include "cclient/serialization/json/helpers.h"
#include "cclient/serialization/json/logger.h"
retcode_t json_get_inclusion_states_serialize_request(serializer_t const *const s,
get_inclusion_states_req_t const *const req, char_buffer_t *out) {
retcode_t ret = RC_OK;
char const *json_text = NULL;
log_debug(json_logger_id, "[%s:%d]\n", __func__, __LINE__);
cJSON *json_root = cJSON_CreateObject();
if (json_root == NULL) {
log_critical(json_logger_id, "[%s:%d] %s\n", __func__, __LINE__, STR_CCLIENT_JSON_CREATE);
return RC_CCLIENT_JSON_CREATE;
}
cJSON_AddItemToObject(json_root, "command", cJSON_CreateString("getInclusionStates"));
ret = hash243_queue_to_json_array(req->hashes, json_root, "transactions");
if (ret != RC_OK) {
goto done;
}
ret = hash243_queue_to_json_array(req->tips, json_root, "tips");
if (ret != RC_OK) {
goto done;
}
json_text = cJSON_PrintUnformatted(json_root);
if (json_text) {
ret = char_buffer_set(out, json_text);
cJSON_free((void *)json_text);
}
done:
cJSON_Delete(json_root);
return ret;
}
retcode_t json_get_inclusion_states_deserialize_request(serializer_t const *const s, char const *const obj,
get_inclusion_states_req_t *const req) {
retcode_t ret = RC_OK;
cJSON *json_obj = cJSON_Parse(obj);
cJSON *json_item = NULL;
log_debug(json_logger_id, "[%s:%d] %s\n", __func__, __LINE__, obj);
JSON_CHECK_ERROR(json_obj, json_item, json_logger_id);
ret = json_array_to_hash243_queue(json_obj, "transactions", &req->hashes);
if (ret != RC_OK) {
goto done;
}
if (cJSON_HasObjectItem(json_obj, "tips")) {
ret = json_array_to_hash243_queue(json_obj, "tips", &req->tips);
}
done:
cJSON_Delete(json_obj);
return ret;
}
retcode_t json_get_inclusion_states_serialize_response(serializer_t const *const s,
get_inclusion_states_res_t const *const res,
char_buffer_t *out) {
retcode_t ret = RC_OK;
char const *json_text = NULL;
log_debug(json_logger_id, "[%s:%d]\n", __func__, __LINE__);
cJSON *json_root = cJSON_CreateObject();
if (json_root == NULL) {
log_critical(json_logger_id, "[%s:%d] %s\n", __func__, __LINE__, STR_CCLIENT_JSON_CREATE);
return RC_CCLIENT_JSON_CREATE;
}
ret = utarray_to_json_boolean_array(res->states, json_root, "states");
if (ret != RC_OK) {
goto done;
}
json_text = cJSON_PrintUnformatted(json_root);
if (json_text) {
ret = char_buffer_set(out, json_text);
cJSON_free((void *)json_text);
}
done:
cJSON_Delete(json_root);
return ret;
}
retcode_t json_get_inclusion_states_deserialize_response(serializer_t const *const s, char const *const obj,
get_inclusion_states_res_t *const res) {
retcode_t ret = RC_OK;
cJSON *json_obj = cJSON_Parse(obj);
cJSON *json_item = NULL;
log_debug(json_logger_id, "[%s:%d] %s\n", __func__, __LINE__, obj);
JSON_CHECK_ERROR(json_obj, json_item, json_logger_id);
ret = json_boolean_array_to_utarray(json_obj, "states", res->states);
cJSON_Delete(json_obj);
return ret;
}