Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix the problem that the pointer is not released #292

Merged
merged 2 commits into from
Nov 25, 2020
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
29 changes: 14 additions & 15 deletions src/app/chain/Chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<http::string_body> 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)
Expand All @@ -76,7 +75,7 @@ BlockHeader *Chain::get_block_header(void)
p_log->err("%s\n", "return body is null");
}

return NULL;
return false;
}


Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion src/app/chain/Chain.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/app/http/ApiHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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!";

Expand Down
36 changes: 18 additions & 18 deletions src/app/http/ApiHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<std::string, std::string> 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");
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -253,15 +253,15 @@ 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();
goto getcleanup;
}

// ----- 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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
16 changes: 8 additions & 8 deletions src/app/http/HttpClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ http::response<http::string_body> 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)
{
Expand Down Expand Up @@ -274,10 +274,10 @@ http::response<http::string_body> 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
Expand Down
8 changes: 4 additions & 4 deletions src/app/http/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<unsigned short>(url_end_point->port);
auto const doc_root = std::make_shared<std::string>(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<unsigned short>(url_end_point.port);
auto const doc_root = std::make_shared<std::string>(url_end_point.base);
auto const threads = std::max<int>(1, WEBSOCKET_THREAD_NUM);

// The io_context is required for all I/O
Expand Down
4 changes: 2 additions & 2 deletions src/app/ocalls/OCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/app/process/Process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading