diff --git a/src/app/App.cpp b/src/app/App.cpp index fcf6aa50..ae75c135 100644 --- a/src/app/App.cpp +++ b/src/app/App.cpp @@ -2,6 +2,7 @@ bool offline_chain_mode = false; bool g_upgrade_flag = false; +bool g_use_sys_disk = false; extern std::string config_file_path; crust::Log *p_log = crust::Log::get_instance(); @@ -42,6 +43,10 @@ int SGX_CDECL main(int argc, char *argv[]) \nSWorker version: %s\n", VERSION, SWORKER_VERSION); return 0; } + else if (strcmp(argv[i], "--use-sysdisk") == 0) + { + g_use_sys_disk = true; + } else if (strcmp(argv[i], "--offline") == 0) { offline_chain_mode = true; @@ -60,8 +65,7 @@ int SGX_CDECL main(int argc, char *argv[]) // Check if configure path has been indicated if (!is_set_config) { - p_log->err("Please indicate configure file path!\n"); - goto show_help; + p_log->info("-c argument is not provided, default config path: %s.json will be used.\n", config_file_path.c_str()); } // Main branch @@ -75,8 +79,9 @@ int SGX_CDECL main(int argc, char *argv[]) printf(" option: \n"); printf(" -h, --help: help information. \n"); printf(" -c, --config: required, indicate configure file path, followed by configure file path. Like: '--config Config.json'\n"); - printf(" If no file provided, default path is etc/Config.json. \n"); + printf(" If no file provided, default path is %s. \n", config_file_path.c_str()); printf(" -v, --version: show whole version and sworker version. \n"); + printf(" --use-sysdisk: use system disk as data disk(be careful to using this argument leading to unexpected error). \n"); printf(" --offline: add this flag, program will not interact with the chain. \n"); printf(" --debug: add this flag, program will output debug logs. \n"); printf(" --upgrade: used to upgrade.\n"); diff --git a/src/app/config/Config.cpp b/src/app/config/Config.cpp index 4e500417..ea64ae1b 100644 --- a/src/app/config/Config.cpp +++ b/src/app/config/Config.cpp @@ -4,9 +4,10 @@ using namespace std; Config *Config::config = NULL; crust::Log *p_log = crust::Log::get_instance(); -std::string config_file_path; +std::string config_file_path = CRUST_INST_DIR "/etc/Config.json"; extern bool offline_chain_mode; +extern bool g_use_sys_disk; /** * @desination: Single instance class function to get instance @@ -69,7 +70,7 @@ bool Config::init(std::string path) for (int i = 0; i < data_paths.size(); i++) { std::string d_path = data_paths[i].ToString(); - this->data_paths.insert(d_path); + this->data_paths.push_back(d_path); if (CRUST_SUCCESS != (crust_status = create_directory(d_path))) { p_log->err("Create path:'%s' failed! Error code:%lx\n", d_path.c_str(), crust_status); @@ -155,17 +156,7 @@ void Config::show(void) printf(" 'base path' : '%s',\n", this->base_path.c_str()); printf(" 'db path' : '%s',\n", this->db_path.c_str()); printf(" 'data path' : [\n"); - std::vector data_paths; - std::set tmp_paths = this->get_data_paths(); - data_paths.insert(data_paths.end(), tmp_paths.begin(), tmp_paths.end()); - std::sort(data_paths.begin(), data_paths.end(), [](std::string s1, std::string s2) { - if (s1.length() != s2.length()) - { - return s1.length() < s2.length(); - } - return s1.compare(s2) < 0; - }); - for (auto it = data_paths.begin(); it != data_paths.end(); ) + for (auto it = this->data_paths.begin(); it != this->data_paths.end(); ) { printf(" \"%s\"", (*it).c_str()); ++it == data_paths.end() ? printf("\n") : printf(",\n"); @@ -211,7 +202,7 @@ std::string Config::get_config_path() bool Config::unique_paths() { // Get system disk fsid - if (!offline_chain_mode) + if (!offline_chain_mode && !g_use_sys_disk) { struct statfs sys_st; if (statfs(this->base_path.c_str(), &sys_st) != -1) @@ -220,8 +211,9 @@ bool Config::unique_paths() } } - std::map sid_m; - std::set data_paths = this->data_paths; + std::set sids_s; + std::set data_paths(this->data_paths.begin(), this->data_paths.end()); + this->data_paths.clear(); for (auto path : data_paths) { struct statfs st; @@ -229,28 +221,37 @@ bool Config::unique_paths() { std::string fsid = hexstring_safe(&st.f_fsid, sizeof(st.f_fsid)); // Compare to check if current disk is system disk - if (this->sys_fsid.compare(fsid) == 0) - { - this->data_paths.erase(path); - } - else + if (this->sys_fsid.compare(fsid) != 0) { // Remove duplicated disk - if (sid_m.find(fsid) == sid_m.end()) + if (sids_s.find(fsid) == sids_s.end()) { - sid_m[fsid] = path; - } - else - { - this->data_paths.erase(path); + this->data_paths.push_back(path); } } } } + this->sort_data_paths(); return 0 != this->data_paths.size(); } +/** + * @description: Sort data paths + */ +void Config::sort_data_paths() +{ + this->data_paths_mutex.lock(); + std::sort(this->data_paths.begin(), this->data_paths.end(), [](std::string s1, std::string s2) { + if (s1.length() != s2.length()) + { + return s1.size() < s2.size(); + } + return s1.compare(s2) < 0; + }); + this->data_paths_mutex.unlock(); +} + /** * @description: Check if given data path is valid * @param path -> Reference to given data path @@ -323,7 +324,7 @@ bool Config::is_valid_or_normal_disk(const std::string &path) * @description: Get data paths * @return: Data paths */ -std::set Config::get_data_paths() +std::vector Config::get_data_paths() { SafeLock sl(this->data_paths_mutex); sl.lock(); @@ -363,6 +364,7 @@ bool Config::config_file_add_data_paths(const json::JSON &paths) } SafeLock sl(this->data_paths_mutex); sl.lock(); + std::set paths_s(this->data_paths.begin(), this->data_paths.end()); bool is_valid = false; for (auto path : paths.ArrayRange()) { @@ -370,14 +372,20 @@ bool Config::config_file_add_data_paths(const json::JSON &paths) if (this->is_valid_data_path(pstr, false)) { config_json["data_path"].append(path); - this->data_paths.insert(pstr); - is_valid = true; + if (paths_s.find(pstr) == paths_s.end()) + { + this->data_paths.push_back(pstr); + paths_s.insert(pstr); + is_valid = true; + } } } + sl.unlock(); if (! is_valid) { return false; } + this->sort_data_paths(); std::string config_str = config_json.dump(); replace(config_str, "\\\\", "\\"); crust_status = save_file(config_file_path.c_str(), reinterpret_cast(config_str.c_str()), config_str.size()); diff --git a/src/app/config/Config.h b/src/app/config/Config.h index d01c30db..8e690299 100644 --- a/src/app/config/Config.h +++ b/src/app/config/Config.h @@ -63,16 +63,17 @@ class Config bool unique_paths(); bool is_valid_or_normal_disk(const std::string &path); bool is_valid_data_path(const std::string &path, bool lock = true); - std::set get_data_paths(); + std::vector get_data_paths(); bool config_file_add_data_paths(const json::JSON &paths); private: Config() {} Config(const Config &); bool init(std::string path); + void sort_data_paths(); Config& operator = (const Config &); std::string sys_fsid; - std::set data_paths; /* data path */ + std::vector data_paths; /* data path */ std::mutex data_paths_mutex; }; diff --git a/src/app/ocalls/StoreOCalls.cpp b/src/app/ocalls/StoreOCalls.cpp index 5dabbbc8..3a92adf9 100644 --- a/src/app/ocalls/StoreOCalls.cpp +++ b/src/app/ocalls/StoreOCalls.cpp @@ -64,17 +64,20 @@ crust_status_t ocall_save_ipfs_block(const char *path, const uint8_t *data, size // Choose disk std::string cid(path, CID_LENGTH); - uint32_t index = 0; - read_rand(reinterpret_cast(&index), sizeof(index)); - index = index % FILE_DISK_LIMIT; - uint32_t ci = index + 2; // Current index, jump the first "Qm" + uint32_t start_index = 0; + read_rand(reinterpret_cast(&start_index), sizeof(start_index)); + start_index = start_index % FILE_DISK_LIMIT; + uint32_t end_index = (CID_LENGTH - 2) / 2; + uint32_t ci = start_index; // Current index uint32_t oi = ci; // Origin index uint32_t loop_num = FILE_DISK_LIMIT; + const char *p_index_path = path + 2; do { - uint32_t di = path[ci] % disk_json.size(); + uint32_t ii = ci * 2; + uint32_t di = (p_index_path[ii] + p_index_path[ii+1]) % disk_json.size(); size_t reserved = disk_json[di][WL_DISK_AVAILABLE].ToInt() * 1024 * 1024 * 1024; - if (reserved > data_size) + if (reserved > data_size * 4) { std::string disk_path = disk_json[di][WL_DISK_PATH].ToString(); std::string uuid_str = EnclaveData::get_instance()->get_uuid(disk_path); @@ -92,10 +95,10 @@ crust_status_t ocall_save_ipfs_block(const char *path, const uint8_t *data, size ci = (ci + 1) % loop_num; if (ci == oi) { - ci = FILE_DISK_LIMIT + 2; - loop_num = CID_LENGTH + 1; + ci = FILE_DISK_LIMIT; + loop_num = end_index + 1; } - } while (ci != CID_LENGTH); + } while (ci < end_index); return CRUST_STORAGE_NO_ENOUGH_SPACE; }