From b14614f8f204b199214349fc11eac4971d01e611 Mon Sep 17 00:00:00 2001 From: Yang Zhao Date: Wed, 25 Nov 2020 15:48:39 +0800 Subject: [PATCH 1/2] fix get chain header --- src/app/chain/Chain.cpp | 29 ++++++++++++++--------------- src/app/chain/Chain.h | 2 +- src/app/http/ApiHandler.h | 6 +++--- src/app/process/Process.cpp | 6 +++--- src/app/process/WorkReport.cpp | 32 +++++++++++++++----------------- 5 files changed, 36 insertions(+), 39 deletions(-) diff --git a/src/app/chain/Chain.cpp b/src/app/chain/Chain.cpp index 2836bbbb..ec570ebe 100644 --- a/src/app/chain/Chain.cpp +++ b/src/app/chain/Chain.cpp @@ -51,20 +51,19 @@ Chain::~Chain() } /** - * @description: get laster block header from chain - * @return: the point of block header + * @description: get latest block header from chain + * @return: true or false */ -BlockHeader *Chain::get_block_header(void) +bool Chain::get_block_header(BlockHeader& block_header) { std::string path = this->url + "/block/header"; http::response res = pri_chain_client->Get(path.c_str()); if ((int)res.result() == 200) { json::JSON block_header_json = json::JSON::Load(res.body()); - BlockHeader *block_header = new BlockHeader(); - block_header->hash = block_header_json["hash"].ToString().erase(0,2); - block_header->number = block_header_json["number"].ToInt(); - return block_header; + block_header.hash = block_header_json["hash"].ToString().erase(0,2); + block_header.number = block_header_json["number"].ToInt(); + return true; } if (res.body().size() != 0) @@ -76,7 +75,7 @@ BlockHeader *Chain::get_block_header(void) p_log->err("%s\n", "return body is null"); } - return NULL; + return false; } @@ -171,20 +170,20 @@ bool Chain::wait_for_running(void) while (true) { - crust::BlockHeader *block_header = this->get_block_header(); - if (block_header == NULL) + crust::BlockHeader block_header; + if (!this->get_block_header(block_header)) { p_log->info("Waiting for chain to run...\n"); sleep(3); } - if (block_header->number >= start_block_height) + if (block_header.number >= start_block_height) { break; } else { - p_log->info("Wait for the chain to execute after %lu blocks, now is %lu ...\n", start_block_height, block_header->number); + p_log->info("Wait for the chain to execute after %lu blocks, now is %lu ...\n", start_block_height, block_header.number); sleep(3); } } @@ -197,10 +196,10 @@ bool Chain::wait_for_running(void) } else { - crust::BlockHeader *block_header = this->get_block_header(); - if (block_header != NULL) + crust::BlockHeader block_header; + if (this->get_block_header(block_header)) { - p_log->info("Wait for chain synchronization to complete, currently synchronized to the %lu block\n", block_header->number); + p_log->info("Wait for chain synchronization to complete, currently synchronized to the %lu block\n", block_header.number); } else { diff --git a/src/app/chain/Chain.h b/src/app/chain/Chain.h index 70a54f03..ca73f3b3 100644 --- a/src/app/chain/Chain.h +++ b/src/app/chain/Chain.h @@ -26,7 +26,7 @@ class Chain public: static Chain *chain; static Chain *get_instance(); - BlockHeader *get_block_header(void); + bool get_block_header(BlockHeader& block_header); std::string get_block_hash(size_t block_number); bool post_sworker_identity(std::string identity); bool post_sworker_work_report(std::string work_report); diff --git a/src/app/http/ApiHandler.h b/src/app/http/ApiHandler.h index 9f743be2..09d2ba4c 100644 --- a/src/app/http/ApiHandler.h +++ b/src/app/http/ApiHandler.h @@ -293,14 +293,14 @@ void ApiHandler::http_handler(beast::string_view /*doc_root*/, sgx_status_t sgx_status = SGX_SUCCESS; crust_status_t crust_status = CRUST_SUCCESS; - crust::BlockHeader *block_header = crust::Chain::get_instance()->get_block_header(); - if (block_header == NULL) + crust::BlockHeader block_header; + if (!crust::Chain::get_instance()->get_block_header(block_header)) { ret_info = "Chain is not running!Get block header failed!"; res.result(402); p_log->err("%s\n", ret_info.c_str()); } - else if (SGX_SUCCESS != (sgx_status = Ecall_enable_upgrade(global_eid, &crust_status, block_header->number))) + else if (SGX_SUCCESS != (sgx_status = Ecall_enable_upgrade(global_eid, &crust_status, block_header.number))) { ret_info = "Invoke SGX API failed!"; res.result(403); diff --git a/src/app/process/Process.cpp b/src/app/process/Process.cpp index fd62569d..ee934a9e 100644 --- a/src/app/process/Process.cpp +++ b/src/app/process/Process.cpp @@ -565,12 +565,12 @@ int process_run() if (UPGRADE_STATUS_END == ed->get_upgrade_status()) { p_log->info("Start generating upgrade data...\n"); - crust::BlockHeader *block_header = crust::Chain::get_instance()->get_block_header(); - if (block_header == NULL) + crust::BlockHeader block_header; + if (!crust::Chain::get_instance()->get_block_header(block_header)) { p_log->err("Get block header failed!Please check your crust-api and crust chain!\n"); } - else if (SGX_SUCCESS != (sgx_status = Ecall_gen_upgrade_data(global_eid, &crust_status, block_header->number))) + else if (SGX_SUCCESS != (sgx_status = Ecall_gen_upgrade_data(global_eid, &crust_status, block_header.number))) { p_log->err("Generate upgrade metadata failed! Invoke SGX API failed! Error code:%lx.Try next turn!\n", sgx_status); } diff --git a/src/app/process/WorkReport.cpp b/src/app/process/WorkReport.cpp index c76b09c7..c60805d5 100644 --- a/src/app/process/WorkReport.cpp +++ b/src/app/process/WorkReport.cpp @@ -37,14 +37,14 @@ void work_report_loop(void) // Generate target block height if (!offline_chain_mode) { - crust::BlockHeader *block_header = p_chain->get_block_header(); - if (block_header == NULL) + crust::BlockHeader block_header; + if (!p_chain->get_block_header(block_header)) { p_log->warn("Cannot get block header! Set target block height %d\n", target_block_height); } else { - target_block_height = (block_header->number / REPORT_BLOCK_HEIGHT_BASE + 1) * REPORT_BLOCK_HEIGHT_BASE; + target_block_height = (block_header.number / REPORT_BLOCK_HEIGHT_BASE + 1) * REPORT_BLOCK_HEIGHT_BASE; p_log->info("Set target block height %d\n", target_block_height); } } @@ -56,7 +56,7 @@ void work_report_loop(void) break; } - crust::BlockHeader *block_header = NULL; + crust::BlockHeader block_header; // Avoid A competing work-report with B if (UPGRADE_STATUS_STOP_WORKREPORT == ed->get_upgrade_status()) @@ -74,19 +74,18 @@ void work_report_loop(void) // ----- Report work report ----- // if (!offline_chain_mode) { - block_header = p_chain->get_block_header(); - if (block_header == NULL) + if (!p_chain->get_block_header(block_header)) { p_log->warn("Cannot get block header!\n"); goto loop; } - if (block_header->number < target_block_height) + if (block_header.number < target_block_height) { goto loop; } - size_t cut_wait_time = (block_header->number - (block_header->number / REPORT_BLOCK_HEIGHT_BASE) * REPORT_BLOCK_HEIGHT_BASE) * BLOCK_INTERVAL; + size_t cut_wait_time = (block_header.number - (block_header.number / REPORT_BLOCK_HEIGHT_BASE) * REPORT_BLOCK_HEIGHT_BASE) * BLOCK_INTERVAL; size_t wait_time = get_random_wait_time(id_json["pub_key"].ToString()); if (cut_wait_time >= wait_time) @@ -99,32 +98,31 @@ void work_report_loop(void) } wait_time = std::max(wait_time, (size_t)REPORT_INTERVAL_BLCOK_NUMBER_LOWER_LIMIT); - p_log->info("It is estimated that the workload will be reported at the %lu block\n", block_header->number + (wait_time / BLOCK_INTERVAL) + 1); - block_header->number = (block_header->number / REPORT_BLOCK_HEIGHT_BASE) * REPORT_BLOCK_HEIGHT_BASE; + p_log->info("It is estimated that the workload will be reported at the %lu block\n", block_header.number + (wait_time / BLOCK_INTERVAL) + 1); + block_header.number = (block_header.number / REPORT_BLOCK_HEIGHT_BASE) * REPORT_BLOCK_HEIGHT_BASE; sleep(wait_time); // Get confirmed block hash - block_header->hash = p_chain->get_block_hash(block_header->number); - if (block_header->hash == "" || block_header->hash == "0000000000000000000000000000000000000000000000000000000000000000") + block_header.hash = p_chain->get_block_hash(block_header.number); + if (block_header.hash == "" || block_header.hash == "0000000000000000000000000000000000000000000000000000000000000000") { p_log->warn("Get block hash failed"); goto loop; } - target_block_height = block_header->number + REPORT_BLOCK_HEIGHT_BASE; + target_block_height = block_header.number + REPORT_BLOCK_HEIGHT_BASE; } else { - block_header = new crust::BlockHeader(); - block_header->hash = "1000000000000000000000000000000000000000000000000000000000000001"; - block_header->number = offline_base_height; + block_header.hash = "1000000000000000000000000000000000000000000000000000000000000001"; + block_header.number = offline_base_height; offline_base_height += REPORT_BLOCK_HEIGHT_BASE; sleep(60); } // Get signed validation report if (SGX_SUCCESS != Ecall_gen_and_upload_work_report(global_eid, &crust_status, - block_header->hash.c_str(), block_header->number)) + block_header.hash.c_str(), block_header.number)) { p_log->err("Get signed work report failed!Message:Invoke SGX API failed!\n"); } From 15d5815d9522d0fd27367d44700ad62a3cd8c639 Mon Sep 17 00:00:00 2001 From: Yang Zhao Date: Wed, 25 Nov 2020 16:06:30 +0800 Subject: [PATCH 2/2] fix get url end point --- src/app/http/ApiHandler.cpp | 2 +- src/app/http/ApiHandler.h | 30 +++++++++++++++--------------- src/app/http/HttpClient.cpp | 16 ++++++++-------- src/app/http/WebServer.cpp | 8 ++++---- src/app/ocalls/OCalls.cpp | 4 ++-- src/app/utils/Common.cpp | 24 ++++++++++++------------ src/app/utils/Common.h | 2 +- 7 files changed, 43 insertions(+), 43 deletions(-) diff --git a/src/app/http/ApiHandler.cpp b/src/app/http/ApiHandler.cpp index 279594f4..2a50cf6b 100644 --- a/src/app/http/ApiHandler.cpp +++ b/src/app/http/ApiHandler.cpp @@ -92,7 +92,7 @@ std::string ApiHandler::websocket_handler(std::string &/*path*/, std::string &/* { //Config *p_config = Config::get_instance(); json::JSON res; - //UrlEndPoint *url_end_point = get_url_end_point(p_config->base_url); + //UrlEndPoint url_end_point = get_url_end_point(p_config->base_url); res["status"] = 300; res["body"] = "Websocket doesn't provide service now!"; diff --git a/src/app/http/ApiHandler.h b/src/app/http/ApiHandler.h index 09d2ba4c..50c002cc 100644 --- a/src/app/http/ApiHandler.h +++ b/src/app/http/ApiHandler.h @@ -87,7 +87,7 @@ void ApiHandler::http_handler(beast::string_view /*doc_root*/, { Config *p_config = Config::get_instance(); crust::Log *p_log = crust::Log::get_instance(); - UrlEndPoint *urlendpoint = get_url_end_point(p_config->base_url); + UrlEndPoint urlendpoint = get_url_end_point(p_config->base_url); EnclaveData *ed = EnclaveData::get_instance(); std::string cur_path; // Upgrade block service set @@ -131,14 +131,14 @@ void ApiHandler::http_handler(beast::string_view /*doc_root*/, { p_log->debug("Http request:%s\n", req_route.c_str()); } - if (memcmp(req_route.c_str(), urlendpoint->base.c_str(), urlendpoint->base.size()) != 0) + if (memcmp(req_route.c_str(), urlendpoint.base.c_str(), urlendpoint.base.size()) != 0) { return send(bad_request("Illegal request-target")); } std::map params = get_params(req_route); // Choose service according to upgrade status - std::string route_tag = req_route.substr(req_route.find(urlendpoint->base) + urlendpoint->base.size(), req_route.size()); + std::string route_tag = req_route.substr(req_route.find(urlendpoint.base) + urlendpoint.base.size(), req_route.size()); if (UPGRADE_STATUS_EXIT == ed->get_upgrade_status()) { p_log->warn("This process will exit!\n"); @@ -191,7 +191,7 @@ void ApiHandler::http_handler(beast::string_view /*doc_root*/, res.set(http::field::content_type, "application/text"); // ----- Get workload ----- // - cur_path = urlendpoint->base + "/workload"; + cur_path = urlendpoint.base + "/workload"; if (req_route.size() == cur_path.size() && req_route.compare(cur_path) == 0) { sgx_status_t sgx_status = SGX_SUCCESS; @@ -253,7 +253,7 @@ void ApiHandler::http_handler(beast::string_view /*doc_root*/, } // ----- Get enclave thread information ----- // - cur_path = urlendpoint->base + "/enclave/thread_info"; + cur_path = urlendpoint.base + "/enclave/thread_info"; if (req_route.size() == cur_path.size() && req_route.compare(cur_path) == 0) { res.body() = get_running_ecalls_info(); @@ -261,7 +261,7 @@ void ApiHandler::http_handler(beast::string_view /*doc_root*/, } // ----- Get enclave id information ----- // - cur_path = urlendpoint->base + "/enclave/id_info"; + cur_path = urlendpoint.base + "/enclave/id_info"; if (req_route.size() == cur_path.size() && req_route.compare(cur_path) == 0) { Ecall_id_get_info(global_eid); @@ -274,7 +274,7 @@ void ApiHandler::http_handler(beast::string_view /*doc_root*/, } // ----- Inform upgrade ----- // - cur_path = urlendpoint->base + "/upgrade/start"; + cur_path = urlendpoint.base + "/upgrade/start"; if (req_route.size() == cur_path.size() && req_route.compare(cur_path) == 0) { res.result(200); @@ -349,7 +349,7 @@ void ApiHandler::http_handler(beast::string_view /*doc_root*/, } // ----- Get metadata ----- // - cur_path = urlendpoint->base + "/upgrade/metadata"; + cur_path = urlendpoint.base + "/upgrade/metadata"; if (req_route.size() == cur_path.size() && req_route.compare(cur_path) == 0) { res.result(200); @@ -371,7 +371,7 @@ void ApiHandler::http_handler(beast::string_view /*doc_root*/, // ----- Inform that new version is already ----- // // Use to inform current sworker upgrade result - cur_path = urlendpoint->base + "/upgrade/complete"; + cur_path = urlendpoint.base + "/upgrade/complete"; if (req_route.size() == cur_path.size() && req_route.compare(cur_path) == 0) { res.result(200); @@ -436,7 +436,7 @@ void ApiHandler::http_handler(beast::string_view /*doc_root*/, // ----- Set debug flag ----- // - cur_path = urlendpoint->base + "/debug"; + cur_path = urlendpoint.base + "/debug"; if (req_route.size() == cur_path.size() && req_route.compare(cur_path) == 0) { // Check input parameters @@ -459,7 +459,7 @@ void ApiHandler::http_handler(beast::string_view /*doc_root*/, } // --- Srd change API --- // - cur_path = urlendpoint->base + "/srd/change"; + cur_path = urlendpoint.base + "/srd/change"; if (req_route.size() == cur_path.size() && req_route.compare(cur_path) == 0) { res.result(200); @@ -512,7 +512,7 @@ void ApiHandler::http_handler(beast::string_view /*doc_root*/, } // --- Confirm new file --- // - cur_path = urlendpoint->base + "/storage/confirm"; + cur_path = urlendpoint.base + "/storage/confirm"; if (req_route.size() == cur_path.size() && req_route.compare(cur_path) == 0) { res.result(200); @@ -537,7 +537,7 @@ void ApiHandler::http_handler(beast::string_view /*doc_root*/, } // --- Delete meaningful file --- // - cur_path = urlendpoint->base + "/storage/delete"; + cur_path = urlendpoint.base + "/storage/delete"; if (req_route.size() == cur_path.size() && req_route.compare(cur_path) == 0) { res.result(200); @@ -562,7 +562,7 @@ void ApiHandler::http_handler(beast::string_view /*doc_root*/, } // ----- Storage seal file block ----- // - cur_path = urlendpoint->base + "/storage/seal"; + cur_path = urlendpoint.base + "/storage/seal"; if (req_route.size() == cur_path.size() && req_route.compare(cur_path) == 0) { res.result(200); @@ -654,7 +654,7 @@ void ApiHandler::http_handler(beast::string_view /*doc_root*/, } // ----- Storage unseal file block ----- // - cur_path = urlendpoint->base + "/storage/unseal"; + cur_path = urlendpoint.base + "/storage/unseal"; if (req_route.size() == cur_path.size() && req_route.compare(cur_path) == 0) { res.result(200); diff --git a/src/app/http/HttpClient.cpp b/src/app/http/HttpClient.cpp index 1bb139d2..261a6cdd 100644 --- a/src/app/http/HttpClient.cpp +++ b/src/app/http/HttpClient.cpp @@ -158,10 +158,10 @@ http::response HttpClient::request_sync_ssl(http::verb method try { - UrlEndPoint *url_end_point = get_url_end_point(url); - auto const host = url_end_point->ip.c_str(); - auto port = std::to_string(url_end_point->port).c_str(); - auto const path = url_end_point->base.c_str(); + UrlEndPoint url_end_point = get_url_end_point(url); + auto const host = url_end_point.ip.c_str(); + auto port = std::to_string(url_end_point.port).c_str(); + auto const path = url_end_point.base.c_str(); int version = 10; if (std::strncmp(port, "-1", 2) == 0) { @@ -274,10 +274,10 @@ http::response HttpClient::request_sync(http::verb method, st try { - UrlEndPoint *url_end_point = get_url_end_point(url); - auto const host = url_end_point->ip.c_str(); - auto const port = std::to_string(url_end_point->port).c_str(); - auto const path = url_end_point->base.c_str(); + UrlEndPoint url_end_point = get_url_end_point(url); + auto const host = url_end_point.ip.c_str(); + auto const port = std::to_string(url_end_point.port).c_str(); + auto const path = url_end_point.base.c_str(); int version = 10; // The io_context is required for all I/O diff --git a/src/app/http/WebServer.cpp b/src/app/http/WebServer.cpp index 2a9c7cc0..a67e3362 100644 --- a/src/app/http/WebServer.cpp +++ b/src/app/http/WebServer.cpp @@ -844,10 +844,10 @@ void start_webservice(void) { // Check command line arguments. Config *p_config = Config::get_instance(); - UrlEndPoint *url_end_point = get_url_end_point(p_config->base_url); - auto const address = net::ip::make_address(url_end_point->ip); - auto const port = static_cast(url_end_point->port); - auto const doc_root = std::make_shared(url_end_point->base); + UrlEndPoint url_end_point = get_url_end_point(p_config->base_url); + auto const address = net::ip::make_address(url_end_point.ip); + auto const port = static_cast(url_end_point.port); + auto const doc_root = std::make_shared(url_end_point.base); auto const threads = std::max(1, WEBSOCKET_THREAD_NUM); // The io_context is required for all I/O diff --git a/src/app/ocalls/OCalls.cpp b/src/app/ocalls/OCalls.cpp index 842bc73c..8f5ee60c 100644 --- a/src/app/ocalls/OCalls.cpp +++ b/src/app/ocalls/OCalls.cpp @@ -411,8 +411,8 @@ crust_status_t ocall_validate_init() wssclient = new WebsocketClient(); Config *p_config = Config::get_instance(); - UrlEndPoint *urlendpoint = get_url_end_point(p_config->karst_url); - if (! wssclient->websocket_init(urlendpoint->ip, std::to_string(urlendpoint->port), urlendpoint->base)) + UrlEndPoint urlendpoint = get_url_end_point(p_config->karst_url); + if (! wssclient->websocket_init(urlendpoint.ip, std::to_string(urlendpoint.port), urlendpoint.base)) { return CRUST_VALIDATE_INIT_WSS_FAILED; } diff --git a/src/app/utils/Common.cpp b/src/app/utils/Common.cpp index 895d1af4..d3d326e2 100644 --- a/src/app/utils/Common.cpp +++ b/src/app/utils/Common.cpp @@ -9,9 +9,9 @@ crust::Log *p_log = crust::Log::get_instance(); * @param url -> base url, like: http://127.0.0.1:56666/api/v1 * @return: Url end point */ -UrlEndPoint *get_url_end_point(std::string url) +UrlEndPoint get_url_end_point(std::string url) { - UrlEndPoint *url_end_point = new UrlEndPoint(); + UrlEndPoint url_end_point; std::string proto_type; size_t spos = 0, epos; @@ -30,38 +30,38 @@ UrlEndPoint *get_url_end_point(std::string url) epos = url.find("/", spos); if (epos == url.npos) { - url_end_point->ip = url.substr(spos, url.length()); + url_end_point.ip = url.substr(spos, url.length()); goto parse_end; } - url_end_point->ip = url.substr(spos, epos - spos); - url_end_point->base = url.substr(epos, url.size()); + url_end_point.ip = url.substr(spos, epos - spos); + url_end_point.base = url.substr(epos, url.size()); p_log->warn("Parse url warn: Port not indicate, will assign port by protocol.\n"); if (proto_type.compare("https") == 0) { - url_end_point->port = 443; + url_end_point.port = 443; } else if (proto_type.compare("http") == 0) { - url_end_point->port = 80; + url_end_point.port = 80; } else { p_log->warn("Parse url warn: Cannot assign port by protocal!\n"); } - p_log->info("Parse url warn: Set port to:%d\n", url_end_point->port); + p_log->info("Parse url warn: Set port to:%d\n", url_end_point.port); } else { - url_end_point->ip = url.substr(spos, epos - spos); + url_end_point.ip = url.substr(spos, epos - spos); spos = epos + 1; epos = url.find("/", spos); if (epos == url.npos) { - url_end_point->port = std::atoi(url.substr(spos, epos - spos).c_str()); + url_end_point.port = std::atoi(url.substr(spos, epos - spos).c_str()); goto parse_end; } - url_end_point->port = std::atoi(url.substr(spos, epos - spos).c_str()); - url_end_point->base = url.substr(epos, url.size()); + url_end_point.port = std::atoi(url.substr(spos, epos - spos).c_str()); + url_end_point.base = url.substr(epos, url.size()); } parse_end: diff --git a/src/app/utils/Common.h b/src/app/utils/Common.h index ea05dbf8..a5e79295 100644 --- a/src/app/utils/Common.h +++ b/src/app/utils/Common.h @@ -28,7 +28,7 @@ extern "C" int port = -1; }; - UrlEndPoint *get_url_end_point(std::string url); + UrlEndPoint get_url_end_point(std::string url); void remove_chars_from_string(std::string &str, const char *chars_to_remove); MerkleTree *deserialize_merkle_tree_from_json(json::JSON tree_json); json::JSON serialize_merkletree_to_json(MerkleTree *root);