Skip to content

Commit bbd5d4f

Browse files
authored
Empty configure will trigger starting error (#349)
* Add default ipfs url * No ipfs url config trigger start error * Delete srd temp data if moving it failed * Change Config class * Check if configure is empty * Print configured srd_ratio and remaining srd task * Delete srd tmp folder when save file failed * Free p_sealed_data in srd
1 parent 28dd122 commit bbd5d4f

File tree

7 files changed

+192
-25
lines changed

7 files changed

+192
-25
lines changed

src/app/config/Config.cpp

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,51 +13,116 @@ Config *Config::get_instance()
1313
{
1414
if (Config::config == NULL)
1515
{
16-
Config::config = new Config(config_file_path);
16+
Config::config = new Config();
17+
if (!Config::config->init(config_file_path))
18+
{
19+
delete Config::config;
20+
return NULL;
21+
}
1722
}
1823

1924
return Config::config;
2025
}
2126

2227
/**
23-
* @description: constructor
24-
* @param path -> configurations file path
28+
* @description: Initialze config
29+
* @param path -> Configure file path
30+
* @return: Initialize result
2531
*/
26-
Config::Config(std::string path)
32+
bool Config::init(std::string path)
2733
{
2834
/* Read user configurations from file */
2935
std::ifstream config_ifs(path);
3036
std::string config_str((std::istreambuf_iterator<char>(config_ifs)), std::istreambuf_iterator<char>());
3137

32-
/* Fill configurations */
38+
// Fill configurations
3339
json::JSON config_value = json::JSON::Load(config_str);
3440

41+
crust::Log *p_log = crust::Log::get_instance();
3542
// Base configurations
3643
this->base_path = config_value["base_path"].ToString();
37-
this->srd_path = config_value["data_path"].ToString() + "/srd";
38-
this->file_path = config_value["data_path"].ToString() + "/file";
39-
this->temp_path = config_value["data_path"].ToString() + "/../sworker_temp";
44+
if (this->base_path.compare("") == 0)
45+
{
46+
p_log->err("Please configure 'base_path'!\n");
47+
return false;
48+
}
4049
this->db_path = this->base_path + "/db";
50+
51+
// Set data_path related
52+
std::string data_path = config_value["data_path"].ToString();
53+
if (data_path.compare("") == 0)
54+
{
55+
p_log->err("Please configure 'data_path'!\n");
56+
return false;
57+
}
58+
this->srd_path = data_path + "/srd";
59+
this->file_path = data_path + "/file";
60+
this->temp_path = data_path + "/../sworker_temp";
61+
62+
// Set base url
4163
this->base_url = config_value["base_url"].ToString();
64+
if (this->base_url.compare("") == 0)
65+
{
66+
p_log->err("Please configure 'base_url'!\n");
67+
return false;
68+
}
4269

70+
// Set srd related
4371
this->srd_thread_num = std::min(omp_get_num_procs() * 2, SRD_THREAD_MAX_NUM);
4472
this->srd_ratio = SRD_RATIO_DEFAULT;
4573

46-
// storage configurations
74+
// Set storage configure related
4775
this->ipfs_url = config_value["ipfs_url"].ToString();
76+
if (this->ipfs_url.compare("") == 0)
77+
{
78+
p_log->err("Please configure 'ipfs_url'!\n");
79+
return false;
80+
}
4881

49-
// crust chain configurations
82+
// ----- Crust chain configure ----- //
83+
// Base url
5084
this->chain_api_base_url = config_value["chain"]["base_url"].ToString();
85+
if (this->chain_api_base_url.compare("") == 0)
86+
{
87+
p_log->err("Please configure 'chain base_url'!\n");
88+
return false;
89+
}
90+
// Address
5191
this->chain_address = config_value["chain"]["address"].ToString();
92+
if (this->chain_address.compare("") == 0)
93+
{
94+
p_log->err("Please configure 'chain address'!\n");
95+
return false;
96+
}
97+
// Account id
5298
this->chain_account_id = config_value["chain"]["account_id"].ToString();
99+
if (this->chain_account_id.compare("") == 0)
100+
{
101+
p_log->err("Please configure 'chain account_id'!\n");
102+
return false;
103+
}
53104
if (this->chain_account_id.find("0x") != this->chain_account_id.npos)
54105
{
55106
this->chain_account_id.erase(0, 2);
56107
}
108+
// Password
57109
this->chain_password = config_value["chain"]["password"].ToString();
110+
if (this->chain_password.compare("") == 0)
111+
{
112+
p_log->err("Please configure 'chain password'!\n");
113+
return false;
114+
}
115+
// Backup
58116
std::string backup_temp = config_value["chain"]["backup"].ToString();
59117
remove_chars_from_string(backup_temp, "\\");
60118
this->chain_backup = backup_temp;
119+
if (this->chain_backup.compare("") == 0)
120+
{
121+
p_log->err("Please configure 'chain backup'!\n");
122+
return false;
123+
}
124+
125+
return true;
61126
}
62127

63128
/**

src/app/config/Config.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,9 @@ class Config
6666
double get_srd_ratio();
6767

6868
private:
69-
Config(std::string path);
69+
Config() {}
7070
Config(const Config &);
71+
bool init(std::string path);
7172
Config& operator = (const Config &);
7273
double srd_ratio;
7374
std::mutex srd_ratio_mutex;

src/app/http/WebServer.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -814,7 +814,10 @@ void WebServer::set_api_handler(ApiHandler *api_handler)
814814

815815
void stop_webservice(void)
816816
{
817-
p_ioc->stop();
817+
if (p_ioc != NULL)
818+
{
819+
p_ioc->stop();
820+
}
818821
}
819822

820823
void start_webservice(void)

src/app/process/EnclaveData.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ std::string EnclaveData::gen_workload()
324324
{
325325
sgx_status_t sgx_status = SGX_SUCCESS;
326326
// Get srd info
327-
if (SGX_SUCCESS != Ecall_get_workload(global_eid))
327+
if (SGX_SUCCESS != (sgx_status = Ecall_get_workload(global_eid)))
328328
{
329329
p_log->warn("Get workload failed! Error code:%lx\n", sgx_status);
330330
}
@@ -345,7 +345,7 @@ std::string EnclaveData::gen_workload()
345345
.append("}");
346346
wl_json[WL_SRD] = srd_info;
347347
// Get file info
348-
json::JSON file_info = wl_json["files"];
348+
json::JSON file_info = wl_json[WL_FILES];
349349
json::JSON n_file_info;
350350
char buf[128];
351351
int space_num = 0;
@@ -361,7 +361,7 @@ std::string EnclaveData::gen_workload()
361361
n_file_info[it->first] = std::string(buf);
362362
}
363363

364-
wl_json["files"] = n_file_info;
364+
wl_json[WL_FILES] = n_file_info;
365365
std::string wl_str = wl_json.dump();
366366
replace(wl_str, "\"{", "{");
367367
replace(wl_str, ": \" ", ": ");

src/app/process/Process.cpp

Lines changed: 72 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ bool start_task(task_func_t func);
99
bool restore_tasks();
1010

1111
// Global EID shared by multiple threads
12-
sgx_enclave_id_t global_eid;
12+
sgx_enclave_id_t global_eid = 0;
1313
// Pointor to configure instance
1414
Config *p_config = NULL;
1515
// Map to record specific task
@@ -447,6 +447,10 @@ int process_run()
447447
std::string srd_ratio_str;
448448
EnclaveData *ed = EnclaveData::get_instance();
449449
crust::DataBase *db = NULL;
450+
bool is_restart = false;
451+
bool has_recover_option = false;
452+
json::JSON wl_json;
453+
std::string wl_str;
450454
p_log->info("WorkerPID = %d\n", worker_pid);
451455

452456
// Init conifigure
@@ -549,7 +553,8 @@ int process_run()
549553
goto cleanup;
550554
}
551555

552-
p_log->info("Workload information:\n%s\n", ed->gen_workload().c_str());
556+
is_restart = true;
557+
wl_str = ed->gen_workload();
553558

554559
if (!offline_chain_mode)
555560
{
@@ -597,7 +602,6 @@ int process_run()
597602
p_log->info("Mrenclave is '%s'\n", id_info["mrenclave"].ToString().c_str());
598603
}
599604
}
600-
p_log->info("Restore enclave data successfully, sworker is running now.\n");
601605
}
602606
}
603607
db = crust::DataBase::get_instance();
@@ -609,7 +613,15 @@ int process_run()
609613
double srd_ratio = 0.0;
610614
sstream >> srd_ratio;
611615
p_config->set_srd_ratio(srd_ratio);
612-
p_log->info("Recover user defined srd ratio:%s successfully!\n", float_to_string(srd_ratio).c_str());
616+
if (is_restart)
617+
{
618+
if (wl_json.size() <= 0)
619+
{
620+
wl_json = json::JSON::Load(wl_str);
621+
}
622+
wl_json[WL_SRD][WL_SRD_RATIO] = srd_ratio;
623+
}
624+
has_recover_option = true;
613625
}
614626

615627
// Get srd remaining task
@@ -619,6 +631,15 @@ int process_run()
619631
size_t srd_task_remain = 0;
620632
sstream >> srd_task_remain;
621633
srd_task = std::max(srd_task, srd_task_remain);
634+
if (is_restart)
635+
{
636+
if (wl_json.size() <= 0)
637+
{
638+
wl_json = json::JSON::Load(wl_str);
639+
}
640+
wl_json[WL_SRD][WL_SRD_REMAINING_TASK] = srd_task;
641+
}
642+
has_recover_option = true;
622643
}
623644

624645
// Restore or add srd task
@@ -645,6 +666,49 @@ int process_run()
645666
}
646667
}
647668

669+
// Print recovered workload
670+
if (is_restart)
671+
{
672+
if (has_recover_option)
673+
{
674+
std::string srd_info;
675+
srd_info.append("{\n")
676+
.append("\"" WL_SRD_COMPLETE "\" : ").append(std::to_string(wl_json[WL_SRD][WL_SRD_COMPLETE].ToInt())).append(",\n")
677+
.append("\"" WL_SRD_REMAINING_TASK "\" : ").append(std::to_string(wl_json[WL_SRD][WL_SRD_REMAINING_TASK].ToInt())).append(",\n")
678+
.append("\"" WL_SRD_RATIO "\" : ").append(float_to_string(wl_json[WL_SRD][WL_SRD_RATIO].ToFloat())).append(",\n")
679+
.append("\"" WL_DISK_AVAILABLE_FOR_SRD "\" : ").append(std::to_string(wl_json[WL_SRD][WL_DISK_AVAILABLE_FOR_SRD].ToInt())).append(",\n")
680+
.append("\"" WL_DISK_AVAILABLE "\" : ").append(std::to_string(wl_json[WL_SRD][WL_DISK_AVAILABLE].ToInt())).append(",\n")
681+
.append("\"" WL_DISK_VOLUME "\" : ").append(std::to_string(wl_json[WL_SRD][WL_DISK_VOLUME].ToInt())).append("\n")
682+
.append("}");
683+
// Get file info
684+
json::JSON file_info = wl_json[WL_FILES];
685+
json::JSON n_file_info;
686+
char buf[128];
687+
int space_num = 0;
688+
for (auto it = file_info.ObjectRange().begin(); it != file_info.ObjectRange().end(); it++)
689+
{
690+
space_num = std::max(space_num, (int)it->first.size());
691+
}
692+
for (auto it = file_info.ObjectRange().begin(); it != file_info.ObjectRange().end(); it++)
693+
{
694+
memset(buf, 0, sizeof(buf));
695+
sprintf(buf, "%s{ \"num\" : %-6ld, \"size\" : %ld }",
696+
std::string(space_num - it->first.size(), ' ').c_str(), it->second["num"].ToInt(), it->second["size"].ToInt());
697+
n_file_info[it->first] = std::string(buf);
698+
}
699+
wl_json[WL_SRD] = srd_info;
700+
wl_json[WL_FILES] = n_file_info;
701+
wl_str = wl_json.dump();
702+
replace(wl_str, "\"{", "{");
703+
replace(wl_str, ": \" ", ": ");
704+
replace(wl_str, "}\"", "}");
705+
replace(wl_str, "\\n", "\n");
706+
remove_char(wl_str, '\\');
707+
}
708+
p_log->info("Workload information:\n%s\n", wl_str.c_str());
709+
p_log->info("Restore enclave data successfully, sworker is running now.\n");
710+
}
711+
648712
// Restore sealed file information
649713
ed->restore_sealed_file_info();
650714

@@ -749,7 +813,10 @@ int process_run()
749813
// Destroy enclave
750814
// TODO: Fix me, why destory enclave leads to coredump
751815
p_log->info("Destroy enclave for exit...\n");
752-
sgx_destroy_enclave(global_eid);
816+
if (global_eid != 0)
817+
{
818+
sgx_destroy_enclave(global_eid);
819+
}
753820

754821
return return_status;
755822
}

src/enclave/srd/Srd.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,18 @@ void srd_increase()
159159
hashs[i * HASH_LENGTH + j] = out_hash256[j];
160160
}
161161

162-
save_file(tmp_dir.c_str(), i, out_hash256, (unsigned char *)p_sealed_data, SRD_RAND_DATA_LENGTH);
162+
crust_status = save_file(tmp_dir.c_str(), i, out_hash256, (unsigned char *)p_sealed_data, SRD_RAND_DATA_LENGTH);
163+
if (CRUST_SUCCESS != crust_status)
164+
{
165+
log_err("Save srd file(%s) failed!\n", tmp_dir.c_str());
166+
ocall_delete_folder_or_file(&crust_status, tmp_dir.c_str(), STORE_TYPE_TEMP);
167+
if (CRUST_SUCCESS != crust_status)
168+
{
169+
log_warn("Delete temp directory %s failed!\n", tmp_dir.c_str());
170+
}
171+
free(p_sealed_data);
172+
return;
173+
}
163174

164175
free(p_sealed_data);
165176
p_sealed_data = NULL;
@@ -169,12 +180,32 @@ void srd_increase()
169180
sgx_sha256_hash_t g_out_hash256;
170181
sgx_sha256_msg(hashs, SRD_RAND_DATA_NUM * HASH_LENGTH, &g_out_hash256);
171182

172-
save_m_hashs_file(tmp_dir.c_str(), hashs, SRD_RAND_DATA_NUM * HASH_LENGTH);
183+
crust_status = save_m_hashs_file(tmp_dir.c_str(), hashs, SRD_RAND_DATA_NUM * HASH_LENGTH);
173184
free(hashs);
185+
if (CRUST_SUCCESS != crust_status)
186+
{
187+
log_err("Save srd(%s) metadata failed!\n", tmp_dir.c_str());
188+
ocall_delete_folder_or_file(&crust_status, tmp_dir.c_str(), STORE_TYPE_TEMP);
189+
if (CRUST_SUCCESS != crust_status)
190+
{
191+
log_warn("Delete temp directory %s failed!\n", tmp_dir.c_str());
192+
}
193+
return;
194+
}
174195

175196
// Change G path name
176197
std::string new_g_path = hexstring_safe(&g_out_hash256, HASH_LENGTH);
177198
ocall_rename_dir(&crust_status, tmp_dir.c_str(), new_g_path.c_str(), STORE_TYPE_TEMP, STORE_TYPE_SRD);
199+
if (CRUST_SUCCESS != crust_status)
200+
{
201+
log_err("Move directory %s to %s failed!\n", tmp_dir.c_str(), new_g_path.c_str());
202+
ocall_delete_folder_or_file(&crust_status, tmp_dir.c_str(), STORE_TYPE_TEMP);
203+
if (CRUST_SUCCESS != crust_status)
204+
{
205+
log_warn("Delete temp directory %s failed!\n", tmp_dir.c_str());
206+
}
207+
return;
208+
}
178209

179210
// Get g hash
180211
uint8_t *p_hash_u = (uint8_t *)enc_malloc(HASH_LENGTH);

src/enclave/validator/Validator.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -435,15 +435,15 @@ void validate_meaningful_file_real()
435435
{
436436
// Get current validate files size
437437
sgx_thread_mutex_lock(&g_validate_files_m_iter_mutex);
438-
size_t tmp_validate_files_m = g_validate_files_m.size();
438+
size_t tmp_validate_files_m_num = g_validate_files_m.size();
439439
sgx_thread_mutex_unlock(&g_validate_files_m_iter_mutex);
440440
// Increase validated files number
441441
sgx_thread_mutex_lock(&g_validated_files_num_mutex);
442442
if (service_unavailable)
443443
{
444-
if (g_validated_files_num < tmp_validate_files_m)
444+
if (g_validated_files_num < tmp_validate_files_m_num)
445445
{
446-
g_validated_files_num = tmp_validate_files_m;
446+
g_validated_files_num = tmp_validate_files_m_num;
447447
wl->set_report_file_flag(false);
448448
log_err("IPFS is offline! Please start it.\n");
449449
}

0 commit comments

Comments
 (0)