8
8
9
9
#include "apis.h"
10
10
#include "map/mode.h"
11
+ #include "utils/handles/lock.h"
11
12
12
13
#define APIS_LOGGER "apis"
13
14
14
15
static logger_id_t apis_logger_id ;
16
+ lock_handle_t cjson_lock ;
15
17
16
18
void apis_logger_init () { apis_logger_id = logger_helper_enable (APIS_LOGGER , LOGGER_DEBUG , true); }
17
19
@@ -38,6 +40,20 @@ status_t api_get_ta_info(char** json_result, ta_config_t* const info, iota_confi
38
40
return ret ;
39
41
}
40
42
43
+ status_t apis_lock_init () {
44
+ if (lock_handle_init (& cjson_lock )) {
45
+ return SC_CONF_LOCK_INIT ;
46
+ }
47
+ return SC_OK ;
48
+ }
49
+
50
+ status_t apis_lock_destroy () {
51
+ if (lock_handle_destroy (& cjson_lock )) {
52
+ return SC_CONF_LOCK_DESTROY ;
53
+ }
54
+ return SC_OK ;
55
+ }
56
+
41
57
status_t api_get_tips (const iota_client_service_t * const service , char * * json_result ) {
42
58
status_t ret = SC_OK ;
43
59
get_tips_res_t * res = get_tips_res_new ();
@@ -48,11 +64,14 @@ status_t api_get_tips(const iota_client_service_t* const service, char** json_re
48
64
goto done ;
49
65
}
50
66
67
+ lock_handle_lock (& cjson_lock );
51
68
if (iota_client_get_tips (service , res ) != RC_OK ) {
69
+ lock_handle_unlock (& cjson_lock );
52
70
ret = SC_CCLIENT_FAILED_RESPONSE ;
53
71
log_error (apis_logger_id , "[%s:%d:%s]\n" , __func__ , __LINE__ , "SC_CCLIENT_FAILED_RESPONSE" );
54
72
goto done ;
55
73
}
74
+ lock_handle_unlock (& cjson_lock );
56
75
57
76
ret = ta_get_tips_res_serialize (res , json_result );
58
77
if (ret != SC_OK ) {
@@ -80,11 +99,14 @@ status_t api_get_tips_pair(const iota_config_t* const tangle, const iota_client_
80
99
}
81
100
82
101
get_transactions_to_approve_req_set_depth (req , tangle -> milestone_depth );
102
+ lock_handle_lock (& cjson_lock );
83
103
if (iota_client_get_transactions_to_approve (service , req , res ) != RC_OK ) {
104
+ lock_handle_unlock (& cjson_lock );
84
105
ret = SC_CCLIENT_FAILED_RESPONSE ;
85
106
log_error (apis_logger_id , "[%s:%d:%s]\n" , __func__ , __LINE__ , "SC_CCLIENT_FAILED_RESPONSE" );
86
107
goto done ;
87
108
}
109
+ lock_handle_unlock (& cjson_lock );
88
110
89
111
if (service -> serializer .vtable .get_transactions_to_approve_serialize_response (res , res_buff ) != RC_OK ) {
90
112
ret = SC_CCLIENT_JSON_PARSE ;
@@ -117,10 +139,13 @@ status_t api_generate_address(const iota_config_t* const tangle, const iota_clie
117
139
goto done ;
118
140
}
119
141
142
+ lock_handle_lock (& cjson_lock );
120
143
ret = ta_generate_address (tangle , service , res );
121
144
if (ret ) {
145
+ lock_handle_unlock (& cjson_lock );
122
146
goto done ;
123
147
}
148
+ lock_handle_unlock (& cjson_lock );
124
149
125
150
ret = ta_generate_address_res_serialize (res , json_result );
126
151
@@ -139,17 +164,23 @@ status_t api_find_transactions(const iota_client_service_t* const service, const
139
164
log_error (apis_logger_id , "[%s:%d:%s]\n" , __func__ , __LINE__ , "SC_TA_OOM" );
140
165
goto done ;
141
166
}
167
+ lock_handle_lock (& cjson_lock );
142
168
if (service -> serializer .vtable .find_transactions_deserialize_request (obj , req ) != RC_OK ) {
169
+ lock_handle_unlock (& cjson_lock );
143
170
ret = SC_CCLIENT_JSON_PARSE ;
144
171
log_error (apis_logger_id , "[%s:%d:%s]\n" , __func__ , __LINE__ , "SC_CCLIENT_JSON_PARSE" );
145
172
goto done ;
146
173
}
174
+ lock_handle_unlock (& cjson_lock );
147
175
176
+ lock_handle_lock (& cjson_lock );
148
177
if (iota_client_find_transactions (service , req , res ) != RC_OK ) {
178
+ lock_handle_unlock (& cjson_lock );
149
179
ret = SC_CCLIENT_FAILED_RESPONSE ;
150
180
log_error (apis_logger_id , "[%s:%d:%s]\n" , __func__ , __LINE__ , "SC_CCLIENT_FAILED_RESPONSE" );
151
181
goto done ;
152
182
}
183
+ lock_handle_unlock (& cjson_lock );
153
184
154
185
if (service -> serializer .vtable .find_transactions_serialize_response (res , res_buff ) != RC_OK ) {
155
186
ret = SC_CCLIENT_JSON_PARSE ;
@@ -187,11 +218,14 @@ status_t api_find_transaction_object_single(const iota_client_service_t* const s
187
218
flex_trits_from_trytes (txn_hash , NUM_TRITS_HASH , (const tryte_t * )obj , NUM_TRYTES_HASH , NUM_TRYTES_HASH );
188
219
hash243_queue_push (& req -> hashes , txn_hash );
189
220
221
+ lock_handle_lock (& cjson_lock );
190
222
ret = ta_find_transaction_objects (service , req , res );
191
223
if (ret ) {
224
+ lock_handle_unlock (& cjson_lock );
192
225
log_error (apis_logger_id , "[%s:%d]\n" , __func__ , __LINE__ );
193
226
goto done ;
194
227
}
228
+ lock_handle_unlock (& cjson_lock );
195
229
196
230
ret = ta_find_transaction_object_single_res_serialize (res , json_result );
197
231
@@ -212,17 +246,23 @@ status_t api_find_transaction_objects(const iota_client_service_t* const service
212
246
goto done ;
213
247
}
214
248
249
+ lock_handle_lock (& cjson_lock );
215
250
ret = ta_find_transaction_objects_req_deserialize (obj , req );
216
251
if (ret != SC_OK ) {
252
+ lock_handle_unlock (& cjson_lock );
217
253
log_error (apis_logger_id , "[%s:%d]\n" , __func__ , __LINE__ );
218
254
goto done ;
219
255
}
256
+ lock_handle_unlock (& cjson_lock );
220
257
258
+ lock_handle_lock (& cjson_lock );
221
259
ret = ta_find_transaction_objects (service , req , res );
222
260
if (ret ) {
261
+ lock_handle_unlock (& cjson_lock );
223
262
log_error (apis_logger_id , "[%s:%d]\n" , __func__ , __LINE__ );
224
263
goto done ;
225
264
}
265
+ lock_handle_unlock (& cjson_lock );
226
266
227
267
ret = ta_find_transaction_objects_res_serialize (res , json_result );
228
268
@@ -251,11 +291,14 @@ status_t api_find_transactions_by_tag(const iota_client_service_t* const service
251
291
goto done ;
252
292
}
253
293
294
+ lock_handle_lock (& cjson_lock );
254
295
if (iota_client_find_transactions (service , req , res ) != RC_OK ) {
296
+ lock_handle_unlock (& cjson_lock );
255
297
ret = SC_CCLIENT_FAILED_RESPONSE ;
256
298
log_error (apis_logger_id , "[%s:%d]\n" , __func__ , __LINE__ );
257
299
goto done ;
258
300
}
301
+ lock_handle_unlock (& cjson_lock );
259
302
260
303
ret = ta_find_transactions_by_tag_res_serialize ((ta_find_transactions_by_tag_res_t * )res , json_result );
261
304
@@ -284,11 +327,14 @@ status_t api_find_transactions_obj_by_tag(const iota_client_service_t* const ser
284
327
goto done ;
285
328
}
286
329
330
+ lock_handle_lock (& cjson_lock );
287
331
ret = ta_find_transactions_obj_by_tag (service , req , res );
288
332
if (ret ) {
333
+ lock_handle_unlock (& cjson_lock );
289
334
log_error (apis_logger_id , "[%s:%d]\n" , __func__ , __LINE__ );
290
335
goto done ;
291
336
}
337
+ lock_handle_unlock (& cjson_lock );
292
338
293
339
ret = ta_find_transaction_objects_res_serialize (res , json_result );
294
340
@@ -355,11 +401,14 @@ status_t api_mam_send_message(const iota_config_t* const tangle, const iota_clie
355
401
ta_send_mam_req_t * req = send_mam_req_new ();
356
402
ta_send_mam_res_t * res = send_mam_res_new ();
357
403
404
+ lock_handle_lock (& cjson_lock );
358
405
if (send_mam_req_deserialize (payload , req )) {
406
+ lock_handle_unlock (& cjson_lock );
359
407
ret = SC_MAM_FAILED_INIT ;
360
408
log_error (apis_logger_id , "[%s:%d:%s]\n" , __func__ , __LINE__ , "SC_MAM_FAILED_INIT" );
361
409
goto done ;
362
410
}
411
+ lock_handle_unlock (& cjson_lock );
363
412
364
413
// Creating MAM API
365
414
prng = (req -> prng [0 ]) ? req -> prng : SEED ;
@@ -404,11 +453,14 @@ status_t api_mam_send_message(const iota_config_t* const tangle, const iota_clie
404
453
send_mam_res_set_channel_id (res , channel_id );
405
454
406
455
// Sending bundle
456
+ lock_handle_lock (& cjson_lock );
407
457
if (ta_send_bundle (tangle , service , bundle ) != SC_OK ) {
458
+ lock_handle_unlock (& cjson_lock );
408
459
ret = SC_MAM_FAILED_RESPONSE ;
409
460
log_error (apis_logger_id , "[%s:%d:%s]\n" , __func__ , __LINE__ , "SC_MAM_FAILED_RESPONSE" );
410
461
goto done ;
411
462
}
463
+ lock_handle_unlock (& cjson_lock );
412
464
send_mam_res_set_bundle_hash (res , transaction_bundle ((iota_transaction_t * )utarray_front (bundle )));
413
465
414
466
ret = send_mam_res_serialize (res , json_result );
@@ -441,23 +493,30 @@ status_t api_send_transfer(const iota_config_t* const tangle, const iota_client_
441
493
goto done ;
442
494
}
443
495
496
+ lock_handle_lock (& cjson_lock );
444
497
ret = ta_send_transfer_req_deserialize (obj , req );
445
498
if (ret ) {
499
+ lock_handle_unlock (& cjson_lock );
446
500
goto done ;
447
501
}
448
502
449
503
ret = ta_send_transfer (tangle , service , req , res );
450
504
if (ret ) {
505
+ lock_handle_unlock (& cjson_lock );
451
506
goto done ;
452
507
}
508
+ lock_handle_unlock (& cjson_lock );
453
509
454
510
// return transaction object
455
511
hash243_queue_push (& txn_obj_req -> hashes , hash243_queue_peek (res -> hash ));
456
512
513
+ lock_handle_lock (& cjson_lock );
457
514
ret = ta_find_transaction_objects (service , txn_obj_req , res_txn_array );
458
515
if (ret ) {
516
+ lock_handle_unlock (& cjson_lock );
459
517
goto done ;
460
518
}
519
+ lock_handle_unlock (& cjson_lock );
461
520
462
521
ret = ta_send_transfer_res_serialize (res_txn_array , json_result );
463
522
@@ -480,15 +539,19 @@ status_t api_send_trytes(const iota_config_t* const tangle, const iota_client_se
480
539
goto done ;
481
540
}
482
541
542
+ lock_handle_lock (& cjson_lock );
483
543
ret = ta_send_trytes_req_deserialize (obj , trytes );
484
544
if (ret != SC_OK ) {
545
+ lock_handle_unlock (& cjson_lock );
485
546
goto done ;
486
547
}
487
548
488
549
ret = ta_send_trytes (tangle , service , trytes );
489
550
if (ret != SC_OK ) {
551
+ lock_handle_unlock (& cjson_lock );
490
552
goto done ;
491
553
}
554
+ lock_handle_unlock (& cjson_lock );
492
555
493
556
ret = ta_send_trytes_res_serialize (trytes , json_result );
494
557
0 commit comments