Moved h32 hash type from hpfs to util. (#204)

This commit is contained in:
Ravin Perera
2020-12-23 21:58:49 +05:30
committed by GitHub
parent c25ebe66f4
commit f97d7de530
19 changed files with 98 additions and 98 deletions

View File

@@ -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

View File

@@ -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<std::string, usr::raw_user_input> raw_inputs;

View File

@@ -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<std::string, uint32_t> users;
std::map<std::string, uint32_t> inputs;
std::map<std::string, uint32_t> output_hash;
std::map<hpfs::h32, uint32_t> state;
std::map<util::h32, uint32_t> state;
std::map<std::string, uint32_t> unl;
std::map<std::string, uint32_t> unl_additions;
std::map<std::string, uint32_t> 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 <typename T>
void increment(std::map<T, uint32_t> &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);

View File

@@ -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<h32> &hashes, const std::string_view mount_dir, const std::string_view vpath)
int get_file_block_hashes(std::vector<util::h32> &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);

View File

@@ -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<h32> &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<util::h32> &hashes, const std::string_view mount_dir, const std::string_view vpath);
int get_dir_children_hashes(std::vector<child_hash_node> &hash_nodes, const std::string_view mount_dir, const std::string_view dir_vpath);
} // namespace hpfs

View File

@@ -33,9 +33,9 @@ namespace msg::fbuf
/**
* Returns hash from Flat Buffer vector of bytes.
*/
hpfs::h32 flatbuff_bytes_to_hash(const flatbuffers::Vector<uint8_t> *buffer)
util::h32 flatbuff_bytes_to_hash(const flatbuffers::Vector<uint8_t> *buffer)
{
return *reinterpret_cast<const hpfs::h32 *>(buffer->data());
return *reinterpret_cast<const util::h32 *>(buffer->data());
}
/**
@@ -87,9 +87,9 @@ namespace msg::fbuf
* Returns Flatbuffer bytes vector from hash.
*/
const flatbuffers::Offset<flatbuffers::Vector<uint8_t>>
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<const uint8_t *>(&hash), sizeof(hpfs::h32));
return builder.CreateVector(reinterpret_cast<const uint8_t *>(&hash), sizeof(util::h32));
}
/**

View File

@@ -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<uint8_t> *buffer);
util::h32 flatbuff_bytes_to_hash(const flatbuffers::Vector<uint8_t> *buffer);
const std::set<std::string>
flatbuf_bytearrayvector_to_stringlist(const flatbuffers::Vector<flatbuffers::Offset<ByteArray>> *fbvec);
@@ -36,7 +36,7 @@ namespace msg::fbuf
sv_to_flatbuff_str(flatbuffers::FlatBufferBuilder &builder, std::string_view sv);
const flatbuffers::Offset<flatbuffers::Vector<uint8_t>>
hash_to_flatbuff_bytes(flatbuffers::FlatBufferBuilder &builder, hpfs::h32 hash);
hash_to_flatbuff_bytes(flatbuffers::FlatBufferBuilder &builder, util::h32 hash);
const flatbuffers::Offset<flatbuffers::Vector<flatbuffers::Offset<ByteArray>>>
stringlist_to_flatbuf_bytearrayvector(flatbuffers::FlatBufferBuilder &builder, const std::set<std::string> &set);

View File

@@ -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<hpfs::child_hash_node> &hash_nodes, hpfs::h32 expected_hash, std::string_view lcl)
std::vector<hpfs::child_hash_node> &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<hpfs::h32> &hashmap, std::size_t file_length, hpfs::h32 expected_hash, std::string_view lcl)
std::vector<util::h32> &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<const char *>(hashmap.data()), hashmap.size() * sizeof(hpfs::h32));
std::string_view hashmap_sv(reinterpret_cast<const char *>(hashmap.data()), hashmap.size() * sizeof(util::h32));
const flatbuffers::Offset<File_HashMap_Response> resp =
CreateFile_HashMap_Response(

View File

@@ -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<hpfs::child_hash_node> &hash_nodes, hpfs::h32 expected_hash, std::string_view lcl);
std::vector<hpfs::child_hash_node> &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<hpfs::h32> &hashmap, std::size_t file_length, hpfs::h32 expected_hash, std::string_view lcl);
std::vector<util::h32> &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);

View File

@@ -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<std::string> users;
std::set<std::string> 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

View File

@@ -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;

View File

@@ -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)

View File

@@ -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;

View File

@@ -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<hpfs::h32> block_hashes;
std::vector<util::h32> 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<uint8_t> &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<hpfs::h32> block_hashes;
std::vector<util::h32> 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<hpfs::h32> &hashes, size_t &file_length,
const std::string_view vpath, const hpfs::h32 expected_hash)
int get_data_block_hashes(std::vector<util::h32> &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<hpfs::child_hash_node> &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)
{

View File

@@ -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<uint8_t> &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<hpfs::h32> &hashes, size_t &file_length,
const std::string_view vpath, const hpfs::h32 expected_hash);
int get_data_block_hashes(std::vector<util::h32> &hashes, size_t &file_length,
const std::string_view vpath, const util::h32 expected_hash);
int get_fs_entry_hashes(std::vector<hpfs::child_hash_node> &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

View File

@@ -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<const hpfs::h32 *>(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<const util::h32 *>(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<std::string, p2p::state_fs_hash_entry> &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<const char *>(&request.expected_hash), sizeof(hpfs::h32));
.append(reinterpret_cast<const char *>(&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<hpfs::h32> existing_hashes;
std::vector<util::h32> 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();

View File

@@ -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<std::pair<std::string, std::string>> 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<std::string, p2p::state_fs_hash_entry> &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<std::string, p2p::state_fs_hash_entry> &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);

View File

@@ -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<const char *>(this), sizeof(hpfs::h32));
return std::string_view(reinterpret_cast<const char *>(this), sizeof(h32));
}
h32 &h32::operator=(std::string_view sv)
@@ -71,4 +71,4 @@ namespace hpfs
return res;
}
} // namespace hpfs
} // namespace util

View File

@@ -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