|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 BiiLabs Co., Ltd. and Contributors |
| 3 | + * All Rights Reserved. |
| 4 | + * This is free software; you can redistribute it and/or modify it under the |
| 5 | + * terms of the MIT license. A copy of the license can be found in the file |
| 6 | + * "LICENSE" at the root of this distribution. |
| 7 | + */ |
| 8 | +#include <getopt.h> |
| 9 | +#include <sys/time.h>" |
| 10 | +#include "accelerator/config.h" |
| 11 | +#include "cclient/api/core/core_api.h" |
| 12 | +#include "cclient/api/extended/extended_api.h" |
| 13 | +#include "common/model/bundle.h" |
| 14 | +#include "storage/ta_storage.h" |
| 15 | + |
| 16 | +#define PRESISTENT_PENDING_SECOND 1800 /**< 30 mins */ |
| 17 | +#define DELAY_INTERVAL 300 /**< 5 mins */ |
| 18 | + |
| 19 | +#define logger_id scylladb_logger_id |
| 20 | + |
| 21 | +static status_t init_iota_client_service(iota_client_service_t* const serv) { |
| 22 | + if (serv == NULL) { |
| 23 | + ta_log_error("Invalid NULL pointer\n"); |
| 24 | + return SC_TA_NULL; |
| 25 | + } |
| 26 | + serv->http.path = "/"; |
| 27 | + serv->http.content_type = "application/json"; |
| 28 | + serv->http.accept = "application/json"; |
| 29 | + serv->http.api_version = 1; |
| 30 | + serv->http.ca_pem = NULL; |
| 31 | + serv->serializer_type = SR_JSON; |
| 32 | + if (iota_client_core_init(serv) != RC_OK) { |
| 33 | + ta_log_error("Failed to connect to IRI.\n"); |
| 34 | + return SC_TA_OOM; |
| 35 | + } |
| 36 | + return SC_OK; |
| 37 | +} |
| 38 | + |
| 39 | +static status_t handle_pending_txn(iota_client_service_t* iota_service, db_client_service_t* db_service, |
| 40 | + db_identity_t* obj) { |
| 41 | + status_t ret = SC_OK; |
| 42 | + hash243_queue_t req_txn = NULL; |
| 43 | + get_inclusion_states_res_t* res = get_inclusion_states_res_new(); |
| 44 | + flex_trit_t trit_hash[NUM_TRITS_HASH]; |
| 45 | + char tryte_hash[NUM_TRYTES_HASH]; |
| 46 | + flex_trits_from_trytes(trit_hash, NUM_TRITS_HASH, (const tryte_t*)db_ret_identity_hash(obj), NUM_TRYTES_HASH, |
| 47 | + NUM_TRYTES_HASH); |
| 48 | + |
| 49 | + hash243_queue_push(&req_txn, trit_hash); |
| 50 | + |
| 51 | + if (iota_client_get_latest_inclusion(iota_service, req_txn, res) != RC_OK || |
| 52 | + get_inclusion_states_res_states_count(res) != 1) { |
| 53 | + ret = SC_CCLIENT_FAILED_RESPONSE; |
| 54 | + ta_log_error("Failed to get inclustion status\n"); |
| 55 | + db_show_identity_info(obj); |
| 56 | + goto exit; |
| 57 | + } |
| 58 | + if (get_inclusion_states_res_states_at(res, 0)) { |
| 59 | + // confirmed transaction |
| 60 | + ta_log_info("Find confirmed transaction\n"); |
| 61 | + db_set_identity_status(obj, CONFIRMED_TXN); |
| 62 | + ret = db_insert_identity_table(db_service, obj); |
| 63 | + if (ret != SC_OK) { |
| 64 | + ta_log_error("Failed to insert identity table\n"); |
| 65 | + db_show_identity_info(obj); |
| 66 | + goto exit; |
| 67 | + } |
| 68 | + } else if (db_ret_identity_time_elapsed(obj) > PRESISTENT_PENDING_SECOND) { |
| 69 | + // reattach |
| 70 | + ta_log_info("Reattach pending transaction\n"); |
| 71 | + db_show_identity_info(obj); |
| 72 | + bundle_transactions_t* res_bundle_txn; |
| 73 | + bundle_transactions_new(&res_bundle_txn); |
| 74 | + |
| 75 | + if (iota_client_replay_bundle(iota_service, trit_hash, MILESTONE_DEPTH, MWM, NULL, res_bundle_txn) != RC_OK) { |
| 76 | + ta_log_error("Failed to reattach to Tangle\n"); |
| 77 | + db_show_identity_info(obj); |
| 78 | + ret = SC_CCLIENT_FAILED_RESPONSE; |
| 79 | + goto reattach_done; |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * < get the second transaction in the bundle, |
| 84 | + * the first transaction is the original transaction before reattachment |
| 85 | + */ |
| 86 | + iota_transaction_t* txn = bundle_at(res_bundle_txn, 1); |
| 87 | + flex_trits_to_trytes((tryte_t*)tryte_hash, NUM_TRYTES_HASH, transaction_hash(txn), NUM_TRITS_HASH, NUM_TRITS_HASH); |
| 88 | + |
| 89 | + db_set_identity_hash(obj, (cass_byte_t*)tryte_hash, NUM_TRYTES_HASH); |
| 90 | + db_set_identity_timestamp(obj, time(NULL)); |
| 91 | + |
| 92 | + ret = db_insert_identity_table(db_service, obj); |
| 93 | + if (ret != SC_OK) { |
| 94 | + ta_log_error("Failed to insert identity table\n"); |
| 95 | + goto exit; |
| 96 | + } |
| 97 | + |
| 98 | + reattach_done: |
| 99 | + bundle_transactions_free(&res_bundle_txn); |
| 100 | + } |
| 101 | + |
| 102 | +exit: |
| 103 | + hash243_queue_free(&req_txn); |
| 104 | + get_inclusion_states_res_free(&res); |
| 105 | + |
| 106 | + return ret; |
| 107 | +} |
| 108 | + |
| 109 | +int main(int argc, char** argv) { |
| 110 | + int optIdx; |
| 111 | + db_client_service_t db_service; |
| 112 | + iota_client_service_t iota_service; |
| 113 | + db_service.host = strdup("localhost"); |
| 114 | + iota_service.http.host = "localhost"; |
| 115 | + iota_service.http.port = 14265; |
| 116 | + |
| 117 | + const struct option longOpt[] = {{"iri_host", required_argument, NULL, 'h'}, |
| 118 | + {"iri_port", required_argument, NULL, 'p'}, |
| 119 | + {"db_host", required_argument, NULL, 'd'}, |
| 120 | + {NULL, 0, NULL, 0}}; |
| 121 | + |
| 122 | + /* Parse the command line options */ |
| 123 | + /* TODO: Support macOS since getopt_long() is GNU extension */ |
| 124 | + while (1) { |
| 125 | + int cmdOpt = getopt_long(argc, argv, "b:", longOpt, &optIdx); |
| 126 | + if (cmdOpt == -1) break; |
| 127 | + |
| 128 | + /* Invalid option */ |
| 129 | + if (cmdOpt == '?') break; |
| 130 | + |
| 131 | + if (cmdOpt == 'h') { |
| 132 | + iota_service.http.host = optarg; |
| 133 | + } |
| 134 | + if (cmdOpt == 'p') { |
| 135 | + iota_service.http.port = atoi(optarg); |
| 136 | + } |
| 137 | + if (cmdOpt == 'd') { |
| 138 | + free(db_service.host); |
| 139 | + db_service.host = strdup(optarg); |
| 140 | + } |
| 141 | + } |
| 142 | + if (ta_logger_init() != SC_OK) { |
| 143 | + ta_log_error("logger init fail\n"); |
| 144 | + return EXIT_FAILURE; |
| 145 | + } |
| 146 | + scylladb_logger_init(); |
| 147 | + if (db_client_service_init(&db_service, DB_USAGE_REATTACH) != SC_OK) { |
| 148 | + ta_log_error("Failed to init db client service\n"); |
| 149 | + return EXIT_FAILURE; |
| 150 | + } |
| 151 | + if (init_iota_client_service(&iota_service) != SC_OK) { |
| 152 | + ta_log_error("Failed to init iota client service\n"); |
| 153 | + return EXIT_FAILURE; |
| 154 | + } |
| 155 | + while (1) { |
| 156 | + db_identity_array_t* id_array = db_identity_array_new(); |
| 157 | + db_get_identity_objs_by_status(&db_service, PENDING_TXN, id_array); |
| 158 | + db_identity_t* itr; |
| 159 | + IDENTITY_TABLE_ARRAY_FOREACH(id_array, itr) { |
| 160 | + if (handle_pending_txn(&iota_service, &db_service, itr) != SC_OK) { |
| 161 | + ta_log_warning("Failed to handle pending transaction\n"); |
| 162 | + db_show_identity_info(itr); |
| 163 | + } |
| 164 | + } |
| 165 | + db_identity_array_free(&id_array); |
| 166 | + sleep(DELAY_INTERVAL); |
| 167 | + } |
| 168 | + |
| 169 | + db_client_service_free(&db_service); |
| 170 | + iota_client_core_destroy(&iota_service); |
| 171 | + scylladb_logger_release(); |
| 172 | + return 0; |
| 173 | +} |
0 commit comments