diff --git a/CMakeLists.txt b/CMakeLists.txt index 13910348..e7a8c707 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -29,13 +29,13 @@ add_executable(hpcore src/util/ttl_set.cpp src/util/buffer_store.cpp src/util/merkle_hash_tree.cpp + src/util/h32.cpp src/unl.cpp src/crypto.cpp src/conf.cpp src/hplog.cpp src/sc.cpp src/bill/corebill.cpp - src/hpfs/h32.cpp src/hpfs/hpfs.cpp src/comm/comm_session.cpp src/msg/fbuf/common_helpers.cpp diff --git a/src/consensus.cpp b/src/consensus.cpp index a9a2a4a6..9c926518 100644 --- a/src/consensus.cpp +++ b/src/consensus.cpp @@ -11,7 +11,7 @@ #include "hplog.hpp" #include "crypto.hpp" #include "sc.hpp" -#include "hpfs/h32.hpp" +#include "util/h32.hpp" #include "hpfs/hpfs.hpp" #include "state/state_common.hpp" #include "state/state_sync.hpp" @@ -112,7 +112,7 @@ namespace consensus // Get current lcl and state. std::string lcl = ledger::ctx.get_lcl(); const uint64_t lcl_seq_no = ledger::ctx.get_seq_no(); - hpfs::h32 state = state_common::ctx.get_state(); + util::h32 state = state_common::ctx.get_state(); std::string unl_hash = unl::get_hash(); if (ctx.stage == 0) @@ -187,7 +187,7 @@ namespace consensus // Check our state with majority state. bool is_state_desync = false; - hpfs::h32 majority_state = hpfs::h32_empty; + util::h32 majority_state = util::h32_empty; check_state_votes(is_state_desync, majority_state, votes); // Start state sync if we are out-of-sync with majority state. @@ -530,7 +530,7 @@ namespace consensus return 0; } - p2p::proposal create_stage0_proposal(std::string_view lcl, hpfs::h32 state, std::string_view unl_hash) + p2p::proposal create_stage0_proposal(std::string_view lcl, util::h32 state, std::string_view unl_hash) { // This is the proposal that stage 0 votes on. // We report our own values in stage 0. @@ -559,7 +559,7 @@ namespace consensus return p; } - p2p::proposal create_stage123_proposal(vote_counter &votes, std::string_view lcl, const size_t unl_count, const hpfs::h32 state, std::string_view unl_hash) + p2p::proposal create_stage123_proposal(vote_counter &votes, std::string_view lcl, const size_t unl_count, const util::h32 state, std::string_view unl_hash) { // The proposal to be emited at the end of this stage. p2p::proposal p; @@ -765,7 +765,7 @@ namespace consensus * Check state against the winning and canonical state * @param votes The voting table. */ - void check_state_votes(bool &is_desync, hpfs::h32 &majority_state, vote_counter &votes) + void check_state_votes(bool &is_desync, util::h32 &majority_state, vote_counter &votes) { for (const auto &[pubkey, cp] : ctx.candidate_proposals) { @@ -816,7 +816,7 @@ namespace consensus * Update the ledger and execute the contract after consensus. * @param cons_prop The proposal that reached consensus. */ - int update_ledger_and_execute_contract(const p2p::proposal &cons_prop, std::string &new_lcl, hpfs::h32 &new_state) + int update_ledger_and_execute_contract(const p2p::proposal &cons_prop, std::string &new_lcl, util::h32 &new_state) { // Map to temporarily store the raw inputs along with the hash. std::unordered_map raw_inputs; diff --git a/src/consensus.hpp b/src/consensus.hpp index 7ca5df15..00cae2c6 100644 --- a/src/consensus.hpp +++ b/src/consensus.hpp @@ -8,7 +8,7 @@ #include "sc.hpp" #include "p2p/p2p.hpp" #include "usr/user_input.hpp" -#include "hpfs/h32.hpp" +#include "util/h32.hpp" #include "sc.hpp" namespace consensus @@ -95,7 +95,7 @@ namespace consensus std::map users; std::map inputs; std::map output_hash; - std::map state; + std::map state; std::map unl; std::map unl_additions; std::map unl_removals; @@ -125,15 +125,15 @@ namespace consensus int verify_and_populate_candidate_user_inputs(const uint64_t lcl_seq_no); - p2p::proposal create_stage0_proposal(std::string_view lcl, hpfs::h32 state, std::string_view unl_hash); + p2p::proposal create_stage0_proposal(std::string_view lcl, util::h32 state, std::string_view unl_hash); - p2p::proposal create_stage123_proposal(vote_counter &votes, std::string_view lcl, const size_t unl_count, const hpfs::h32 state, std::string_view unl_hash); + p2p::proposal create_stage123_proposal(vote_counter &votes, std::string_view lcl, const size_t unl_count, const util::h32 state, std::string_view unl_hash); void broadcast_proposal(const p2p::proposal &p); bool check_lcl_votes(bool &is_desync, std::string &majority_lcl, vote_counter &votes, std::string_view lcl, const size_t unl_count); - void check_state_votes(bool &is_desync, hpfs::h32 &majority_state, vote_counter &votes); + void check_state_votes(bool &is_desync, util::h32 &majority_state, vote_counter &votes); void check_unl_votes(bool &is_desync, std::string &majority_unl, vote_counter &votes, std::string_view unl_hash); @@ -143,7 +143,7 @@ namespace consensus uint64_t get_stage_time_resolution(const uint64_t time); - int update_ledger_and_execute_contract(const p2p::proposal &proposal, std::string &new_lcl, hpfs::h32 &new_state); + int update_ledger_and_execute_contract(const p2p::proposal &proposal, std::string &new_lcl, util::h32 &new_state); int dispatch_user_outputs(const p2p::proposal &cons_prop, const uint64_t lcl_seq_no, std::string_view lcl); @@ -154,7 +154,7 @@ namespace consensus template void increment(std::map &counter, const T &candidate); - int get_initial_state_hash(hpfs::h32 &hash); + int get_initial_state_hash(util::h32 &hash); bool push_control_message(const std::string &control_msg); diff --git a/src/hpfs/hpfs.cpp b/src/hpfs/hpfs.cpp index 693d6f56..f5d63f1b 100644 --- a/src/hpfs/hpfs.cpp +++ b/src/hpfs/hpfs.cpp @@ -1,8 +1,8 @@ #include "hpfs.hpp" -#include "h32.hpp" #include "../conf.hpp" #include "../hplog.hpp" #include "../util/util.hpp" +#include "../util/h32.hpp" namespace hpfs { @@ -202,7 +202,7 @@ namespace hpfs * Populates the hash of the specified vpath. * @return 1 on success. 0 if vpath not found. -1 on error. */ - int get_hash(h32 &hash, const std::string_view mount_dir, const std::string_view vpath) + int get_hash(util::h32 &hash, const std::string_view mount_dir, const std::string_view vpath) { const std::string path = std::string(mount_dir).append(vpath).append(HPFS_HMAP_HASH); const int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); @@ -217,7 +217,7 @@ namespace hpfs return -1; } - const int res = read(fd, &hash, sizeof(h32)); + const int res = read(fd, &hash, sizeof(util::h32)); close(fd); if (res == -1) { @@ -231,7 +231,7 @@ namespace hpfs * Populates the list of file block hashes for the specified vpath. * @return 1 on success. 0 if vpath not found. -1 on error. */ - int get_file_block_hashes(std::vector &hashes, const std::string_view mount_dir, const std::string_view vpath) + int get_file_block_hashes(std::vector &hashes, const std::string_view mount_dir, const std::string_view vpath) { const std::string path = std::string(mount_dir).append(vpath).append(HPFS_HMAP_CHILDREN); const int fd = open(path.c_str(), O_RDONLY | O_CLOEXEC); @@ -254,7 +254,7 @@ namespace hpfs return -1; } - const int children_count = st.st_size / sizeof(h32); + const int children_count = st.st_size / sizeof(util::h32); hashes.resize(children_count); const int res = read(fd, hashes.data(), st.st_size); diff --git a/src/hpfs/hpfs.hpp b/src/hpfs/hpfs.hpp index 332b7d03..c11acc1c 100644 --- a/src/hpfs/hpfs.hpp +++ b/src/hpfs/hpfs.hpp @@ -2,7 +2,7 @@ #define _HP_HPFS_HPFS_ #include "../pchheader.hpp" -#include "h32.hpp" +#include "../util/h32.hpp" namespace hpfs { @@ -10,7 +10,7 @@ namespace hpfs { bool is_file = false; char name[256]; - h32 hash; + util::h32 hash; child_hash_node() { @@ -26,8 +26,8 @@ namespace hpfs const bool hash_map_enabled, const bool auto_start_session, const uint16_t timeout = 4000); int start_fs_session(std::string_view mount_dir); int stop_fs_session(std::string_view mount_dir); - int get_hash(h32 &hash, const std::string_view mount_dir, const std::string_view vpath); - int get_file_block_hashes(std::vector &hashes, const std::string_view mount_dir, const std::string_view vpath); + int get_hash(util::h32 &hash, const std::string_view mount_dir, const std::string_view vpath); + int get_file_block_hashes(std::vector &hashes, const std::string_view mount_dir, const std::string_view vpath); int get_dir_children_hashes(std::vector &hash_nodes, const std::string_view mount_dir, const std::string_view dir_vpath); } // namespace hpfs diff --git a/src/msg/fbuf/common_helpers.cpp b/src/msg/fbuf/common_helpers.cpp index 46a550f9..34440476 100644 --- a/src/msg/fbuf/common_helpers.cpp +++ b/src/msg/fbuf/common_helpers.cpp @@ -33,9 +33,9 @@ namespace msg::fbuf /** * Returns hash from Flat Buffer vector of bytes. */ - hpfs::h32 flatbuff_bytes_to_hash(const flatbuffers::Vector *buffer) + util::h32 flatbuff_bytes_to_hash(const flatbuffers::Vector *buffer) { - return *reinterpret_cast(buffer->data()); + return *reinterpret_cast(buffer->data()); } /** @@ -87,9 +87,9 @@ namespace msg::fbuf * Returns Flatbuffer bytes vector from hash. */ const flatbuffers::Offset> - hash_to_flatbuff_bytes(flatbuffers::FlatBufferBuilder &builder, const hpfs::h32 hash) + hash_to_flatbuff_bytes(flatbuffers::FlatBufferBuilder &builder, const util::h32 hash) { - return builder.CreateVector(reinterpret_cast(&hash), sizeof(hpfs::h32)); + return builder.CreateVector(reinterpret_cast(&hash), sizeof(util::h32)); } /** diff --git a/src/msg/fbuf/common_helpers.hpp b/src/msg/fbuf/common_helpers.hpp index 2c461d9b..1653a40f 100644 --- a/src/msg/fbuf/common_helpers.hpp +++ b/src/msg/fbuf/common_helpers.hpp @@ -2,7 +2,7 @@ #define _HP_MSG_FBUF_COMMON_HELPERS_ #include "../../pchheader.hpp" -#include "../../hpfs/h32.hpp" +#include "../../util/h32.hpp" #include "common_schema_generated.h" namespace msg::fbuf @@ -19,7 +19,7 @@ namespace msg::fbuf std::string_view flatbuff_str_to_sv(const flatbuffers::String *buffer); - hpfs::h32 flatbuff_bytes_to_hash(const flatbuffers::Vector *buffer); + util::h32 flatbuff_bytes_to_hash(const flatbuffers::Vector *buffer); const std::set flatbuf_bytearrayvector_to_stringlist(const flatbuffers::Vector> *fbvec); @@ -36,7 +36,7 @@ namespace msg::fbuf sv_to_flatbuff_str(flatbuffers::FlatBufferBuilder &builder, std::string_view sv); const flatbuffers::Offset> - hash_to_flatbuff_bytes(flatbuffers::FlatBufferBuilder &builder, hpfs::h32 hash); + hash_to_flatbuff_bytes(flatbuffers::FlatBufferBuilder &builder, util::h32 hash); const flatbuffers::Offset>> stringlist_to_flatbuf_bytearrayvector(flatbuffers::FlatBufferBuilder &builder, const std::set &set); diff --git a/src/msg/fbuf/p2pmsg_helpers.cpp b/src/msg/fbuf/p2pmsg_helpers.cpp index 8af15342..f037b624 100644 --- a/src/msg/fbuf/p2pmsg_helpers.cpp +++ b/src/msg/fbuf/p2pmsg_helpers.cpp @@ -3,7 +3,7 @@ #include "../../crypto.hpp" #include "../../util/util.hpp" #include "../../hplog.hpp" -#include "../../hpfs/h32.hpp" +#include "../../util/h32.hpp" #include "../../hpfs/hpfs.hpp" #include "../../unl.hpp" #include "p2pmsg_container_generated.h" @@ -508,7 +508,7 @@ namespace msg::fbuf::p2pmsg */ void create_msg_from_fsentry_response( flatbuffers::FlatBufferBuilder &container_builder, const std::string_view path, - std::vector &hash_nodes, hpfs::h32 expected_hash, std::string_view lcl) + std::vector &hash_nodes, util::h32 expected_hash, std::string_view lcl) { flatbuffers::FlatBufferBuilder builder(1024); @@ -540,12 +540,12 @@ namespace msg::fbuf::p2pmsg */ void create_msg_from_filehashmap_response( flatbuffers::FlatBufferBuilder &container_builder, std::string_view path, - std::vector &hashmap, std::size_t file_length, hpfs::h32 expected_hash, std::string_view lcl) + std::vector &hashmap, std::size_t file_length, util::h32 expected_hash, std::string_view lcl) { // todo:get a average propsal message size and allocate content builder based on that. flatbuffers::FlatBufferBuilder builder(1024); - std::string_view hashmap_sv(reinterpret_cast(hashmap.data()), hashmap.size() * sizeof(hpfs::h32)); + std::string_view hashmap_sv(reinterpret_cast(hashmap.data()), hashmap.size() * sizeof(util::h32)); const flatbuffers::Offset resp = CreateFile_HashMap_Response( diff --git a/src/msg/fbuf/p2pmsg_helpers.hpp b/src/msg/fbuf/p2pmsg_helpers.hpp index a1d0147a..557bb56d 100644 --- a/src/msg/fbuf/p2pmsg_helpers.hpp +++ b/src/msg/fbuf/p2pmsg_helpers.hpp @@ -3,7 +3,7 @@ #include "../../pchheader.hpp" #include "../../p2p/p2p.hpp" -#include "../../hpfs/h32.hpp" +#include "../../util/h32.hpp" #include "../../hpfs/hpfs.hpp" #include "p2pmsg_container_generated.h" #include "p2pmsg_content_generated.h" @@ -67,11 +67,11 @@ namespace msg::fbuf::p2pmsg void create_msg_from_fsentry_response( flatbuffers::FlatBufferBuilder &container_builder, const std::string_view path, - std::vector &hash_nodes, hpfs::h32 expected_hash, std::string_view lcl); + std::vector &hash_nodes, util::h32 expected_hash, std::string_view lcl); void create_msg_from_filehashmap_response( flatbuffers::FlatBufferBuilder &container_builder, std::string_view path, - std::vector &hashmap, std::size_t file_length, hpfs::h32 expected_hash, std::string_view lcl); + std::vector &hashmap, std::size_t file_length, util::h32 expected_hash, std::string_view lcl); void create_msg_from_block_response(flatbuffers::FlatBufferBuilder &container_builder, p2p::block_response &block_resp, std::string_view lcl); diff --git a/src/p2p/p2p.hpp b/src/p2p/p2p.hpp index 482bda37..1364f08a 100644 --- a/src/p2p/p2p.hpp +++ b/src/p2p/p2p.hpp @@ -3,7 +3,7 @@ #include "../pchheader.hpp" #include "../usr/user_input.hpp" -#include "../hpfs/h32.hpp" +#include "../util/h32.hpp" #include "../conf.hpp" #include "../msg/fbuf/p2pmsg_container_generated.h" #include "peer_comm_server.hpp" @@ -53,7 +53,7 @@ namespace p2p std::string nonce; // Random nonce that is used to reduce lcl predictability. std::string lcl; std::string unl_hash; // Hash of the current unl list. - hpfs::h32 state; + util::h32 state; std::set users; std::set input_hashes; std::string output_hash; @@ -130,7 +130,7 @@ namespace p2p std::string parent_path; // The requested file or dir path. bool is_file = false; // Whether the path is a file or dir. int32_t block_id = 0; // Block id of the file if we are requesting for file block. Otherwise -1. - hpfs::h32 expected_hash; // The expected hash of the requested result. + util::h32 expected_hash; // The expected hash of the requested result. }; // Represents state file system entry. @@ -138,7 +138,7 @@ namespace p2p { std::string name; // Name of the file/dir. bool is_file = false; // Whether this is a file or dir. - hpfs::h32 hash; // Hash of the file or dir. + util::h32 hash; // Hash of the file or dir. }; // Represents a file block data resposne. @@ -147,7 +147,7 @@ namespace p2p std::string path; // Path of the file. uint32_t block_id = 0; // Id of the block where the data belongs to. std::string_view data; // The block data. - hpfs::h32 hash; // Hash of the bloc data. + util::h32 hash; // Hash of the bloc data. }; struct message_collection diff --git a/src/sc.hpp b/src/sc.hpp index f06c0aec..360e0e8b 100644 --- a/src/sc.hpp +++ b/src/sc.hpp @@ -3,7 +3,7 @@ #include "pchheader.hpp" #include "usr/usr.hpp" -#include "hpfs/h32.hpp" +#include "util/h32.hpp" #include "util/util.hpp" #include "util/buffer_store.hpp" #include "p2p/p2p.hpp" @@ -81,7 +81,7 @@ namespace sc std::string lcl; // State hash after execution will be copied to this (not applicable to read only mode). - hpfs::h32 post_execution_state_hash = hpfs::h32_empty; + util::h32 post_execution_state_hash = util::h32_empty; // Collected unl addition and removal change sets. Holds the changeset until they are subjected to the consensus. p2p::contract_unl_changeset unl_changeset; diff --git a/src/state/state_common.cpp b/src/state/state_common.cpp index feb2e5f9..07b97151 100644 --- a/src/state/state_common.cpp +++ b/src/state/state_common.cpp @@ -11,7 +11,7 @@ namespace state_common */ int init() { - hpfs::h32 initial_state; + util::h32 initial_state; if (hpfs::start_fs_session(conf::ctx.state_rw_dir) == -1 || hpfs::get_hash(initial_state, conf::ctx.state_rw_dir, "/") == -1 || hpfs::stop_fs_session(conf::ctx.state_rw_dir) == -1) diff --git a/src/state/state_common.hpp b/src/state/state_common.hpp index 0bf5a151..34dcc5c2 100644 --- a/src/state/state_common.hpp +++ b/src/state/state_common.hpp @@ -3,7 +3,7 @@ #include "../pchheader.hpp" #include "../conf.hpp" -#include "../hpfs/h32.hpp" +#include "../util/h32.hpp" namespace state_common { @@ -17,17 +17,17 @@ namespace state_common struct state_context { private: - hpfs::h32 state; + util::h32 state; std::shared_mutex state_mutex; public: - hpfs::h32 get_state() + util::h32 get_state() { std::shared_lock lock(state_mutex); return state; } - void set_state(hpfs::h32 new_state) + void set_state(util::h32 new_state) { std::unique_lock lock(state_mutex); state = new_state; diff --git a/src/state/state_serve.cpp b/src/state/state_serve.cpp index d702f683..08cec240 100644 --- a/src/state/state_serve.cpp +++ b/src/state/state_serve.cpp @@ -1,6 +1,6 @@ #include "../pchheader.hpp" #include "../hpfs/hpfs.hpp" -#include "../hpfs/h32.hpp" +#include "../util/h32.hpp" #include "../util/util.hpp" #include "../p2p/p2p.hpp" #include "../msg/fbuf/p2pmsg_content_generated.h" @@ -175,7 +175,7 @@ namespace state_serve // File state request means we have to reply with the file block hash map. if (sr.is_file) { - std::vector block_hashes; + std::vector block_hashes; std::size_t file_length = 0; const int result = get_data_block_hashes(block_hashes, file_length, sr.parent_path, sr.expected_hash); @@ -222,10 +222,10 @@ namespace state_serve * @return 1 if block data was succefully fetched. 0 if vpath or block does not exist. -1 on error. */ int get_data_block(std::vector &block, const std::string_view vpath, - const uint32_t block_id, const hpfs::h32 expected_hash) + const uint32_t block_id, const util::h32 expected_hash) { // Check whether the existing block hash matches expected hash. - std::vector block_hashes; + std::vector block_hashes; int result = hpfs::get_file_block_hashes(block_hashes, conf::ctx.state_serve_dir, vpath); if (result == 1) { @@ -298,11 +298,11 @@ namespace state_serve * Retrieves the specified file block hashes if expected hash matches. * @return 1 if block hashes were successfuly fetched. 0 if vpath does not exist. -1 on error. */ - int get_data_block_hashes(std::vector &hashes, size_t &file_length, - const std::string_view vpath, const hpfs::h32 expected_hash) + int get_data_block_hashes(std::vector &hashes, size_t &file_length, + const std::string_view vpath, const util::h32 expected_hash) { // Check whether the existing file hash matches expected hash. - hpfs::h32 file_hash = hpfs::h32_empty; + util::h32 file_hash = util::h32_empty; int result = hpfs::get_hash(file_hash, conf::ctx.state_serve_dir, vpath); if (result == 1) { @@ -339,10 +339,10 @@ namespace state_serve * @return 1 if fs entry hashes were successfuly fetched. 0 if vpath does not exist. -1 on error. */ int get_fs_entry_hashes(std::vector &hash_nodes, - const std::string_view vpath, const hpfs::h32 expected_hash) + const std::string_view vpath, const util::h32 expected_hash) { // Check whether the existing dir hash matches expected hash. - hpfs::h32 dir_hash = hpfs::h32_empty; + util::h32 dir_hash = util::h32_empty; int result = hpfs::get_hash(dir_hash, conf::ctx.state_serve_dir, vpath); if (result == 1) { diff --git a/src/state/state_serve.hpp b/src/state/state_serve.hpp index 4a602407..296153bc 100644 --- a/src/state/state_serve.hpp +++ b/src/state/state_serve.hpp @@ -1,7 +1,7 @@ #ifndef _HP_STATE_STATE_SERVE_ #define _HP_STATE_STATE_SERVE_ -#include "../hpfs/h32.hpp" +#include "../util/h32.hpp" #include "../hpfs/hpfs.hpp" #include "../p2p/p2p.hpp" #include "../msg/fbuf/p2pmsg_content_generated.h" @@ -17,13 +17,13 @@ namespace state_serve int create_state_response(flatbuffers::FlatBufferBuilder &fbuf, const p2p::state_request &sr, std::string_view lcl); int get_data_block(std::vector &vec, const std::string_view vpath, - const uint32_t block_id, const hpfs::h32 expected_hash); + const uint32_t block_id, const util::h32 expected_hash); - int get_data_block_hashes(std::vector &hashes, size_t &file_length, - const std::string_view vpath, const hpfs::h32 expected_hash); + int get_data_block_hashes(std::vector &hashes, size_t &file_length, + const std::string_view vpath, const util::h32 expected_hash); int get_fs_entry_hashes(std::vector &hash_nodes, - const std::string_view vpath, const hpfs::h32 expected_hash); + const std::string_view vpath, const util::h32 expected_hash); } // namespace state_sync #endif \ No newline at end of file diff --git a/src/state/state_sync.cpp b/src/state/state_sync.cpp index 395f907f..1c3cd44a 100644 --- a/src/state/state_sync.cpp +++ b/src/state/state_sync.cpp @@ -7,7 +7,7 @@ #include "../hplog.hpp" #include "../util/util.hpp" #include "../hpfs/hpfs.hpp" -#include "../hpfs/h32.hpp" +#include "../util/h32.hpp" #include "state_sync.hpp" #include "state_common.hpp" @@ -35,7 +35,7 @@ namespace state_sync int init() { REQUEST_RESUBMIT_TIMEOUT = state_common::get_request_resubmit_timeout(); - ctx.target_state = hpfs::h32_empty; + ctx.target_state = util::h32_empty; ctx.state_sync_thread = std::thread(state_syncer_loop); ctx.hpfs_mount_dir = conf::ctx.state_rw_dir; init_success = true; @@ -57,7 +57,7 @@ namespace state_sync * @param target_state The target state which we should sync towards. * @param completion_callback The callback function to call upon state sync completion. */ - void set_target(const hpfs::h32 target_state) + void set_target(const util::h32 target_state) { std::unique_lock lock(ctx.target_state_mutex); @@ -95,7 +95,7 @@ namespace state_sync { while (!ctx.is_shutting_down) { - hpfs::h32 new_state = hpfs::h32_empty; + util::h32 new_state = util::h32_empty; const int result = request_loop(ctx.target_state, new_state); ctx.pending_requests.clear(); @@ -129,14 +129,14 @@ namespace state_sync } std::unique_lock lock(ctx.target_state_mutex); - ctx.target_state = hpfs::h32_empty; + ctx.target_state = util::h32_empty; ctx.is_syncing = false; } LOG_INFO << "State sync: Worker stopped."; } - int request_loop(const hpfs::h32 current_target, hpfs::h32 &updated_state) + int request_loop(const util::h32 current_target, util::h32 &updated_state) { std::string lcl = ledger::ctx.get_lcl(); @@ -219,8 +219,8 @@ namespace state_sync const msg::fbuf::p2pmsg::File_HashMap_Response *file_resp = resp_msg->state_response_as_File_HashMap_Response(); // File block hashes we received from the peer. - const hpfs::h32 *peer_hashes = reinterpret_cast(file_resp->hash_map()->data()); - const size_t peer_hash_count = file_resp->hash_map()->size() / sizeof(hpfs::h32); + const util::h32 *peer_hashes = reinterpret_cast(file_resp->hash_map()->data()); + const size_t peer_hash_count = file_resp->hash_map()->size() / sizeof(util::h32); // Validate received hashmap against the hash. if (!validate_file_hashmap_hash(vpath, hash, peer_hashes, peer_hash_count)) @@ -323,7 +323,7 @@ namespace state_sync */ bool validate_fs_entry_hash(std::string_view vpath, std::string_view hash, const std::unordered_map &fs_entry_map) { - hpfs::h32 content_hash; + util::h32 content_hash; // Initilal hash is vpath hash. content_hash = crypto::get_hash(vpath); @@ -345,9 +345,9 @@ namespace state_sync * @param hash_count Size of the hash list. * @returns true if hash is valid, otherwise false. */ - bool validate_file_hashmap_hash(std::string_view vpath, std::string_view hash, const hpfs::h32 *hashes, const size_t hash_count) + bool validate_file_hashmap_hash(std::string_view vpath, std::string_view hash, const util::h32 *hashes, const size_t hash_count) { - hpfs::h32 content_hash = hpfs::h32_empty; + util::h32 content_hash = util::h32_empty; // Initilal hash is vpath hash. content_hash = crypto::get_hash(vpath); @@ -379,7 +379,7 @@ namespace state_sync /** * Indicates whether to break out of state request processing loop. */ - bool should_stop_request_loop(const hpfs::h32 current_target) + bool should_stop_request_loop(const util::h32 current_target) { if (ctx.is_shutting_down) return true; @@ -398,7 +398,7 @@ namespace state_sync * @param target_pubkey The peer pubkey the request was submitted to. */ void request_state_from_peer(const std::string &path, const bool is_file, const int32_t block_id, - const hpfs::h32 expected_hash, std::string_view lcl, std::string &target_pubkey) + const util::h32 expected_hash, std::string_view lcl, std::string &target_pubkey) { p2p::state_request sr; sr.parent_path = path; @@ -417,7 +417,7 @@ namespace state_sync void submit_request(const backlog_item &request, std::string_view lcl) { const std::string key = std::string(request.path) - .append(reinterpret_cast(&request.expected_hash), sizeof(hpfs::h32)); + .append(reinterpret_cast(&request.expected_hash), sizeof(util::h32)); ctx.submitted_requests.try_emplace(key, request); const bool is_file = request.type != BACKLOG_ITEM_TYPE::DIR; @@ -513,13 +513,13 @@ namespace state_sync * @param file_length Size of the file. * @returns 0 on success, otherwise -1. */ - int handle_file_hashmap_response(std::string_view vpath, const hpfs::h32 *hashes, const size_t hash_count, const uint64_t file_length) + int handle_file_hashmap_response(std::string_view vpath, const util::h32 *hashes, const size_t hash_count, const uint64_t file_length) { // Get the file path of the block hashes we have received. LOG_DEBUG << "State sync: Processing file block hashes response for " << vpath; // File block hashes on our side (file might not exist on our side). - std::vector existing_hashes; + std::vector existing_hashes; if (hpfs::get_file_block_hashes(existing_hashes, ctx.hpfs_mount_dir, vpath) == -1 && errno != ENOENT) return -1; const size_t existing_hash_count = existing_hashes.size(); diff --git a/src/state/state_sync.hpp b/src/state/state_sync.hpp index 3d2c0fb8..7f90295d 100644 --- a/src/state/state_sync.hpp +++ b/src/state/state_sync.hpp @@ -4,7 +4,7 @@ #include "../pchheader.hpp" #include "../p2p/p2p.hpp" #include "../msg/fbuf/p2pmsg_content_generated.h" -#include "../hpfs/h32.hpp" +#include "../util/h32.hpp" #include "../crypto.hpp" namespace state_sync @@ -23,7 +23,7 @@ namespace state_sync BACKLOG_ITEM_TYPE type = BACKLOG_ITEM_TYPE::DIR; std::string path; int32_t block_id = -1; // Only relevant if type=BLOCK - hpfs::h32 expected_hash; + util::h32 expected_hash; // No. of millisconds that this item has been waiting in pending state. // Used by pending_responses list to increase waiting time and resubmit request. @@ -33,7 +33,7 @@ namespace state_sync struct sync_context { // The current target state we are syncing towards. - hpfs::h32 target_state; + util::h32 target_state; // List of sender pubkeys and state responses(flatbuffer messages) to be processed. std::list> candidate_state_responses; @@ -59,28 +59,28 @@ namespace state_sync void deinit(); - void set_target(const hpfs::h32 target_state); + void set_target(const util::h32 target_state); void state_syncer_loop(); - int request_loop(const hpfs::h32 current_target, hpfs::h32 &updated_state); + int request_loop(const util::h32 current_target, util::h32 &updated_state); bool validate_fs_entry_hash(std::string_view vpath, std::string_view hash, const std::unordered_map &fs_entry_map); - bool validate_file_hashmap_hash(std::string_view vpath, std::string_view hash, const hpfs::h32 *hashes, const size_t hash_count); + bool validate_file_hashmap_hash(std::string_view vpath, std::string_view hash, const util::h32 *hashes, const size_t hash_count); bool validate_file_block_hash(std::string_view hash, const uint32_t block_id, std::string_view buf); - bool should_stop_request_loop(const hpfs::h32 current_target); + bool should_stop_request_loop(const util::h32 current_target); void request_state_from_peer(const std::string &path, const bool is_file, const int32_t block_id, - const hpfs::h32 expected_hash, std::string_view lcl, std::string &target_pubkey); + const util::h32 expected_hash, std::string_view lcl, std::string &target_pubkey); void submit_request(const backlog_item &request, std::string_view lcl); int handle_fs_entry_response(std::string_view vpath, std::unordered_map &fs_entry_map); - int handle_file_hashmap_response(std::string_view vpath, const hpfs::h32 *hashes, const size_t hash_count, const uint64_t file_length); + int handle_file_hashmap_response(std::string_view vpath, const util::h32 *hashes, const size_t hash_count, const uint64_t file_length); int handle_file_block_response(std::string_view vpath, const uint32_t block_id, std::string_view buf); diff --git a/src/hpfs/h32.cpp b/src/util/h32.cpp similarity index 97% rename from src/hpfs/h32.cpp rename to src/util/h32.cpp index d0dfdd0a..6be61886 100644 --- a/src/hpfs/h32.cpp +++ b/src/util/h32.cpp @@ -3,7 +3,7 @@ /** * Based on https://github.com/codetsunami/file-ptracer/blob/master/merkle.cpp */ -namespace hpfs +namespace util { /** * Helper functions for working with 32 byte hash type h32. @@ -31,7 +31,7 @@ namespace hpfs std::string_view h32::to_string_view() const { - return std::string_view(reinterpret_cast(this), sizeof(hpfs::h32)); + return std::string_view(reinterpret_cast(this), sizeof(h32)); } h32 &h32::operator=(std::string_view sv) @@ -71,4 +71,4 @@ namespace hpfs return res; } -} // namespace hpfs \ No newline at end of file +} // namespace util \ No newline at end of file diff --git a/src/hpfs/h32.hpp b/src/util/h32.hpp similarity index 79% rename from src/hpfs/h32.hpp rename to src/util/h32.hpp index a9cbab54..3409b08d 100644 --- a/src/hpfs/h32.hpp +++ b/src/util/h32.hpp @@ -1,12 +1,12 @@ -#ifndef _HP_HPFS_H32_ -#define _HP_HPFS_H32_ +#ifndef _HP_UTIL_H32_ +#define _HP_UTIL_H32_ #include "../pchheader.hpp" -namespace hpfs +namespace util { - // blake2b hash is 32 bytes which we store as 4 quad words + // blake3b hash is 32 bytes which we store as 4 quad words // Originally from https://github.com/codetsunami/file-ptracer/blob/master/merkle.cpp struct h32 { @@ -29,13 +29,13 @@ namespace hpfs std::ostream &operator<<(std::ostream &output, const h32 &h); // Helper class to support std::map/std::unordered_map custom hashing function. - // This is needed to use B2H as the std map container key. + // This is needed to use h32 as the std map container key. class h32_std_key_hasher { public: size_t operator()(const h32 h) const; }; -} // namespace hpfs +} // namespace util #endif \ No newline at end of file