From e3944976985d59f93667a8f34116071be024ac7a Mon Sep 17 00:00:00 2001 From: Ravin Perera <33562092+ravinsp@users.noreply.github.com> Date: Wed, 17 Feb 2021 12:21:51 +0530 Subject: [PATCH] Used uint32 for roundtime and timeout fields. (#248) --- src/comm/comm_session.cpp | 10 +++---- src/conf.cpp | 10 +++---- src/conf.hpp | 6 ++-- src/consensus.cpp | 2 +- src/consensus.hpp | 4 +-- src/hpfs/hpfs_mount.hpp | 2 +- src/hpfs/hpfs_serve.cpp | 2 +- src/hpfs/hpfs_sync.cpp | 2 +- src/hpfs/hpfs_sync.hpp | 2 +- src/msg/fbuf/p2pmsg_content.fbs | 4 +-- src/msg/fbuf/p2pmsg_content_generated.h | 38 ++++++++++++------------- src/p2p/p2p.hpp | 4 +-- src/p2p/peer_comm_session.hpp | 2 +- src/unl.cpp | 10 +++---- src/unl.hpp | 2 +- test/local-cluster/rundir.sh | 2 +- 16 files changed, 51 insertions(+), 51 deletions(-) diff --git a/src/comm/comm_session.cpp b/src/comm/comm_session.cpp index f66cd516..25431242 100644 --- a/src/comm/comm_session.cpp +++ b/src/comm/comm_session.cpp @@ -9,8 +9,8 @@ namespace comm { constexpr uint32_t INTERVALMS = 60000; - constexpr uint16_t UNVERIFIED_INACTIVE_TIMEOUT = 5; // Time threshold for unverified inactive connections in seconds. - constexpr uint16_t MAX_IN_MSG_QUEUE_SIZE = 64; // Maximum in message queue size, The size passed is rounded to next number in binary sequence 1(1),11(3),111(7),1111(15),11111(31).... + constexpr uint32_t UNVERIFIED_INACTIVE_TIMEOUT = 5000; // Time threshold ms for unverified inactive connections. + constexpr uint16_t MAX_IN_MSG_QUEUE_SIZE = 64; // Maximum in message queue size, The size passed is rounded to next number in binary sequence 1(1),11(3),111(7),1111(15),11111(31).... comm_session::comm_session( std::string_view host_address, hpws::client &&hpws_client, const bool is_inbound, const uint64_t (&metric_thresholds)[5]) @@ -302,13 +302,13 @@ namespace comm */ void comm_session::check_last_activity_rules() { - const uint16_t timeout_seconds = (challenge_status == CHALLENGE_STATUS::CHALLENGE_VERIFIED ? thresholds[SESSION_THRESHOLDS::IDLE_CONNECTION_TIMEOUT].threshold_limit : UNVERIFIED_INACTIVE_TIMEOUT); + const uint32_t timeout = (challenge_status == CHALLENGE_STATUS::CHALLENGE_VERIFIED ? thresholds[SESSION_THRESHOLDS::IDLE_CONNECTION_TIMEOUT].threshold_limit : UNVERIFIED_INACTIVE_TIMEOUT); // Timeout zero means unlimited. - if (timeout_seconds == 0) + if (timeout == 0) return; - if (util::get_epoch_milliseconds() - last_activity_timestamp >= (timeout_seconds * 1000)) + if (util::get_epoch_milliseconds() - last_activity_timestamp >= timeout) { LOG_DEBUG << "Closing " << display_name() << " connection due to inactivity."; mark_for_closure(); diff --git a/src/conf.cpp b/src/conf.cpp index f4544a26..ce6984e4 100644 --- a/src/conf.cpp +++ b/src/conf.cpp @@ -149,7 +149,7 @@ namespace conf cfg.mesh.port = 22860; cfg.mesh.msg_forwarding = true; - cfg.mesh.idle_timeout = 120; + cfg.mesh.idle_timeout = 120000; cfg.mesh.peer_discovery.enabled = true; cfg.mesh.peer_discovery.interval = 30000; @@ -347,6 +347,7 @@ namespace conf const jsoncons::ojson &mesh = d["mesh"]; cfg.mesh.port = mesh["port"].as(); cfg.mesh.listen = mesh["listen"].as(); + cfg.mesh.idle_timeout = mesh["idle_timeout"].as(); // Storing peers in unordered map keyed by the concatenated address:port and also saving address and port // seperately to retrieve easily when handling peer connections. @@ -387,7 +388,6 @@ namespace conf cfg.mesh.max_bad_msgs_per_min = mesh["max_bad_msgs_per_min"].as(); cfg.mesh.max_bad_msgsigs_per_min = mesh["max_bad_msgsigs_per_min"].as(); cfg.mesh.max_dup_msgs_per_min = mesh["max_dup_msgs_per_min"].as(); - cfg.mesh.idle_timeout = mesh["idle_timeout"].as(); jpath = "mesh.peer_discovery"; cfg.mesh.peer_discovery.interval = mesh["peer_discovery"]["interval"].as(); @@ -409,12 +409,12 @@ namespace conf const jsoncons::ojson &user = d["user"]; cfg.user.port = user["port"].as(); cfg.user.listen = user["listen"].as(); + cfg.user.idle_timeout = user["idle_timeout"].as(); cfg.user.max_connections = user["max_connections"].as(); cfg.user.max_in_connections_per_host = user["max_in_connections_per_host"].as(); cfg.user.max_bytes_per_msg = user["max_bytes_per_msg"].as(); cfg.user.max_bytes_per_min = user["max_bytes_per_min"].as(); cfg.user.max_bad_msgs_per_min = user["max_bad_msgs_per_min"].as(); - cfg.user.idle_timeout = user["idle_timeout"].as(); cfg.user.concurrent_read_reqeuests = user["concurrent_read_reqeuests"].as(); } catch (const std::exception &e) @@ -498,6 +498,7 @@ namespace conf jsoncons::ojson mesh_config; mesh_config.insert_or_assign("port", cfg.mesh.port); mesh_config.insert_or_assign("listen", cfg.mesh.listen); + mesh_config.insert_or_assign("idle_timeout", cfg.mesh.idle_timeout); jsoncons::ojson peers(jsoncons::json_array_arg); for (const auto &peer : cfg.mesh.known_peers) @@ -515,7 +516,6 @@ namespace conf mesh_config.insert_or_assign("max_bad_msgs_per_min", cfg.mesh.max_bad_msgs_per_min); mesh_config.insert_or_assign("max_bad_msgsigs_per_min", cfg.mesh.max_bad_msgsigs_per_min); mesh_config.insert_or_assign("max_dup_msgs_per_min", cfg.mesh.max_dup_msgs_per_min); - mesh_config.insert_or_assign("idle_timeout", cfg.mesh.idle_timeout); jsoncons::ojson peer_discovery_config; peer_discovery_config.insert_or_assign("enabled", cfg.mesh.peer_discovery.enabled); @@ -938,7 +938,7 @@ namespace conf contract.bin_path = jdoc["bin_path"].as(); contract.bin_args = jdoc["bin_args"].as(); - contract.roundtime = jdoc["roundtime"].as(); + contract.roundtime = jdoc["roundtime"].as(); if (contract.roundtime == 0) { std::cerr << "Round time cannot be zero.\n"; diff --git a/src/conf.hpp b/src/conf.hpp index 009ef8b1..b93ba022 100644 --- a/src/conf.hpp +++ b/src/conf.hpp @@ -104,7 +104,7 @@ namespace conf std::set unl; // Unique node list (list of binary public keys) std::string bin_path; // Full path to the contract binary std::string bin_args; // CLI arguments to pass to the contract binary - std::atomic roundtime = 0; // Consensus round time in ms + std::atomic roundtime = 0; // Consensus round time in ms bool is_consensus_public = false; // If true, consensus are broadcasted to non-unl nodes as well. bool is_npl_public = false; // If true, npl messages are broadcasted to non-unl nodes as well. appbill_config appbill; @@ -118,7 +118,7 @@ namespace conf { uint16_t port = 0; // Listening port for public user connections bool listen = true; // Whether to listen for incoming user connections. - uint16_t idle_timeout = 0; // Idle connection timeout for user connections in seconds. + uint32_t idle_timeout = 0; // Idle connection timeout ms for user connections. uint64_t max_bytes_per_msg = 0; // User message max size in bytes uint64_t max_bytes_per_min = 0; // User message rate (characters(bytes) per minute) uint64_t max_bad_msgs_per_min = 0; // User bad messages per minute @@ -137,6 +137,7 @@ namespace conf { uint16_t port = 0; // Listening port for peer connections bool listen = true; // Whether to listen for incoming peer connections. + uint32_t idle_timeout = 0; // Idle connection timeout ms for peer connections. std::vector known_peers; // Vector of peers with ip_port, timestamp, capacity. bool msg_forwarding = false; // Whether peer message forwarding is on/off. uint16_t max_connections = 0; // Max peer connections. @@ -147,7 +148,6 @@ namespace conf uint64_t max_bad_msgs_per_min = 0; // Peer bad messages per minute. uint64_t max_bad_msgsigs_per_min = 0; // Peer bad signatures per minute. uint64_t max_dup_msgs_per_min = 0; // Peer max duplicate messages per minute. - uint16_t idle_timeout = 0; // Idle connection timeout for peer connections in seconds. peer_discovery_config peer_discovery; // Peer discovery configs. }; diff --git a/src/consensus.cpp b/src/consensus.cpp index aa9badfe..470291b0 100644 --- a/src/consensus.cpp +++ b/src/consensus.cpp @@ -1070,7 +1070,7 @@ namespace consensus if (perform_detection) { LOG_DEBUG << "Detecting roundtime..."; - const uint16_t majority_roundtime = unl::get_majority_roundtime(); + const uint32_t majority_roundtime = unl::get_majority_roundtime(); if (majority_roundtime == 0 || conf::cfg.contract.roundtime == majority_roundtime) return; diff --git a/src/consensus.hpp b/src/consensus.hpp index 3e1d408a..4849601d 100644 --- a/src/consensus.hpp +++ b/src/consensus.hpp @@ -69,8 +69,8 @@ namespace consensus uint8_t stage = 1; uint64_t round_start_time = 0; - uint16_t stage_time = 0; // Time allocated to a consensus stage. - uint16_t stage_reset_wait_threshold = 0; // Minimum stage wait time to reset the stage. + uint32_t stage_time = 0; // Time allocated to a consensus stage. + uint32_t stage_reset_wait_threshold = 0; // Minimum stage wait time to reset the stage. uint64_t round_boundry_offset = 0; // Time window boundry offset based on contract id. uint16_t unreliable_votes_attempts = 0; // No. of times we failed to get reliable votes continously. diff --git a/src/hpfs/hpfs_mount.hpp b/src/hpfs/hpfs_mount.hpp index e5ad642d..73a7332c 100644 --- a/src/hpfs/hpfs_mount.hpp +++ b/src/hpfs/hpfs_mount.hpp @@ -26,7 +26,7 @@ namespace hpfs } }; - inline uint16_t get_request_resubmit_timeout() + inline uint32_t get_request_resubmit_timeout() { return conf::cfg.contract.roundtime; } diff --git a/src/hpfs/hpfs_serve.cpp b/src/hpfs/hpfs_serve.cpp index 48f937d7..aa8317fe 100644 --- a/src/hpfs/hpfs_serve.cpp +++ b/src/hpfs/hpfs_serve.cpp @@ -70,7 +70,7 @@ namespace hpfs prev_requests_processed = !hpfs_requests.empty(); const uint64_t time_start = util::get_epoch_milliseconds(); const std::string lcl = ledger::ctx.get_lcl(); - const uint16_t request_batch_timeout = hpfs::get_request_resubmit_timeout() * 0.9; + const uint32_t request_batch_timeout = hpfs::get_request_resubmit_timeout() * 0.9; if (hpfs_requests.empty()) continue; diff --git a/src/hpfs/hpfs_sync.cpp b/src/hpfs/hpfs_sync.cpp index 5f84b113..3cc148d5 100644 --- a/src/hpfs/hpfs_sync.cpp +++ b/src/hpfs/hpfs_sync.cpp @@ -287,7 +287,7 @@ namespace hpfs candidate_hpfs_responses.clear(); // No. of milliseconds to wait before resubmitting a request. - const uint16_t request_resubmit_timeout = hpfs::get_request_resubmit_timeout(); + const uint32_t request_resubmit_timeout = hpfs::get_request_resubmit_timeout(); // Check for long-awaited responses and re-request them. for (auto &[hash, request] : submitted_requests) diff --git a/src/hpfs/hpfs_sync.hpp b/src/hpfs/hpfs_sync.hpp index 659db0ad..ee2cc0d9 100644 --- a/src/hpfs/hpfs_sync.hpp +++ b/src/hpfs/hpfs_sync.hpp @@ -28,7 +28,7 @@ namespace hpfs // No. of millisconds that this item has been waiting in pending state. // Used by pending_responses list to increase waiting time and resubmit request. - uint16_t waiting_time = 0; + uint32_t waiting_time = 0; }; struct sync_target diff --git a/src/msg/fbuf/p2pmsg_content.fbs b/src/msg/fbuf/p2pmsg_content.fbs index a5a3847e..5c1f63b9 100644 --- a/src/msg/fbuf/p2pmsg_content.fbs +++ b/src/msg/fbuf/p2pmsg_content.fbs @@ -6,7 +6,7 @@ namespace msg.fbuf.p2pmsg; table Peer_Challenge_Message { contract_id:string; - roundtime:uint16; + roundtime:uint32; challenge:string; } @@ -53,7 +53,7 @@ table NonUnl_Proposal_Message { table Proposal_Message { //Proposal type message schema stage:uint8; time:uint64; - roundtime:uint16; + roundtime:uint32; nonce: [ubyte]; users:[ByteArray]; input_hashes:[ByteArray]; diff --git a/src/msg/fbuf/p2pmsg_content_generated.h b/src/msg/fbuf/p2pmsg_content_generated.h index a6a4451c..9c080da1 100644 --- a/src/msg/fbuf/p2pmsg_content_generated.h +++ b/src/msg/fbuf/p2pmsg_content_generated.h @@ -307,11 +307,11 @@ struct Peer_Challenge_Message FLATBUFFERS_FINAL_CLASS : private flatbuffers::Tab flatbuffers::String *mutable_contract_id() { return GetPointer(VT_CONTRACT_ID); } - uint16_t roundtime() const { - return GetField(VT_ROUNDTIME, 0); + uint32_t roundtime() const { + return GetField(VT_ROUNDTIME, 0); } - bool mutate_roundtime(uint16_t _roundtime) { - return SetField(VT_ROUNDTIME, _roundtime, 0); + bool mutate_roundtime(uint32_t _roundtime) { + return SetField(VT_ROUNDTIME, _roundtime, 0); } const flatbuffers::String *challenge() const { return GetPointer(VT_CHALLENGE); @@ -323,7 +323,7 @@ struct Peer_Challenge_Message FLATBUFFERS_FINAL_CLASS : private flatbuffers::Tab return VerifyTableStart(verifier) && VerifyOffset(verifier, VT_CONTRACT_ID) && verifier.VerifyString(contract_id()) && - VerifyField(verifier, VT_ROUNDTIME) && + VerifyField(verifier, VT_ROUNDTIME) && VerifyOffset(verifier, VT_CHALLENGE) && verifier.VerifyString(challenge()) && verifier.EndTable(); @@ -337,8 +337,8 @@ struct Peer_Challenge_MessageBuilder { void add_contract_id(flatbuffers::Offset contract_id) { fbb_.AddOffset(Peer_Challenge_Message::VT_CONTRACT_ID, contract_id); } - void add_roundtime(uint16_t roundtime) { - fbb_.AddElement(Peer_Challenge_Message::VT_ROUNDTIME, roundtime, 0); + void add_roundtime(uint32_t roundtime) { + fbb_.AddElement(Peer_Challenge_Message::VT_ROUNDTIME, roundtime, 0); } void add_challenge(flatbuffers::Offset challenge) { fbb_.AddOffset(Peer_Challenge_Message::VT_CHALLENGE, challenge); @@ -358,19 +358,19 @@ struct Peer_Challenge_MessageBuilder { inline flatbuffers::Offset CreatePeer_Challenge_Message( flatbuffers::FlatBufferBuilder &_fbb, flatbuffers::Offset contract_id = 0, - uint16_t roundtime = 0, + uint32_t roundtime = 0, flatbuffers::Offset challenge = 0) { Peer_Challenge_MessageBuilder builder_(_fbb); builder_.add_challenge(challenge); - builder_.add_contract_id(contract_id); builder_.add_roundtime(roundtime); + builder_.add_contract_id(contract_id); return builder_.Finish(); } inline flatbuffers::Offset CreatePeer_Challenge_MessageDirect( flatbuffers::FlatBufferBuilder &_fbb, const char *contract_id = nullptr, - uint16_t roundtime = 0, + uint32_t roundtime = 0, const char *challenge = nullptr) { auto contract_id__ = contract_id ? _fbb.CreateString(contract_id) : 0; auto challenge__ = challenge ? _fbb.CreateString(challenge) : 0; @@ -843,11 +843,11 @@ struct Proposal_Message FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { bool mutate_time(uint64_t _time) { return SetField(VT_TIME, _time, 0); } - uint16_t roundtime() const { - return GetField(VT_ROUNDTIME, 0); + uint32_t roundtime() const { + return GetField(VT_ROUNDTIME, 0); } - bool mutate_roundtime(uint16_t _roundtime) { - return SetField(VT_ROUNDTIME, _roundtime, 0); + bool mutate_roundtime(uint32_t _roundtime) { + return SetField(VT_ROUNDTIME, _roundtime, 0); } const flatbuffers::Vector *nonce() const { return GetPointer *>(VT_NONCE); @@ -895,7 +895,7 @@ struct Proposal_Message FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { return VerifyTableStart(verifier) && VerifyField(verifier, VT_STAGE) && VerifyField(verifier, VT_TIME) && - VerifyField(verifier, VT_ROUNDTIME) && + VerifyField(verifier, VT_ROUNDTIME) && VerifyOffset(verifier, VT_NONCE) && verifier.VerifyVector(nonce()) && VerifyOffset(verifier, VT_USERS) && @@ -926,8 +926,8 @@ struct Proposal_MessageBuilder { void add_time(uint64_t time) { fbb_.AddElement(Proposal_Message::VT_TIME, time, 0); } - void add_roundtime(uint16_t roundtime) { - fbb_.AddElement(Proposal_Message::VT_ROUNDTIME, roundtime, 0); + void add_roundtime(uint32_t roundtime) { + fbb_.AddElement(Proposal_Message::VT_ROUNDTIME, roundtime, 0); } void add_nonce(flatbuffers::Offset> nonce) { fbb_.AddOffset(Proposal_Message::VT_NONCE, nonce); @@ -966,7 +966,7 @@ inline flatbuffers::Offset CreateProposal_Message( flatbuffers::FlatBufferBuilder &_fbb, uint8_t stage = 0, uint64_t time = 0, - uint16_t roundtime = 0, + uint32_t roundtime = 0, flatbuffers::Offset> nonce = 0, flatbuffers::Offset>> users = 0, flatbuffers::Offset>> input_hashes = 0, @@ -992,7 +992,7 @@ inline flatbuffers::Offset CreateProposal_MessageDirect( flatbuffers::FlatBufferBuilder &_fbb, uint8_t stage = 0, uint64_t time = 0, - uint16_t roundtime = 0, + uint32_t roundtime = 0, const std::vector *nonce = nullptr, const std::vector> *users = nullptr, const std::vector> *input_hashes = nullptr, diff --git a/src/p2p/p2p.hpp b/src/p2p/p2p.hpp index aba7433d..b232673a 100644 --- a/src/p2p/p2p.hpp +++ b/src/p2p/p2p.hpp @@ -26,7 +26,7 @@ namespace p2p uint64_t recv_timestamp = 0; // The timestamp when we received the proposal. (used for network statistics) uint64_t time = 0; // The descreet concensus time value that is voted on. uint8_t stage = 0; // The round-stage that this proposal belongs to. - uint16_t roundtime = 0; // Roundtime of the proposer. + uint32_t roundtime = 0; // Roundtime of the proposer. std::string nonce; // Random nonce that is used to reduce lcl predictability. std::string lcl; util::h32 state_hash; // Contract state hash. @@ -57,7 +57,7 @@ namespace p2p struct peer_challenge { std::string contract_id; - uint16_t roundtime = 0; + uint32_t roundtime = 0; std::string challenge; }; diff --git a/src/p2p/peer_comm_session.hpp b/src/p2p/peer_comm_session.hpp index 7b9241c9..37cd7b3e 100644 --- a/src/p2p/peer_comm_session.hpp +++ b/src/p2p/peer_comm_session.hpp @@ -24,7 +24,7 @@ namespace p2p std::optional known_ipport; // A known ip/port information that matches with our peer list configuration. bool need_consensus_msg_forwarding = false; // Holds whether this node requires consensus message forwarding. bool is_unl = false; // Whether this session's pubkey is in unl list. - uint16_t reported_roundtime = 0; // Initial roundtime reported by this peer on peer challenge. + uint32_t reported_roundtime = 0; // Initial roundtime reported by this peer on peer challenge. }; } // namespace p2p diff --git a/src/unl.cpp b/src/unl.cpp index 0c61d2a1..0716d89f 100644 --- a/src/unl.cpp +++ b/src/unl.cpp @@ -9,7 +9,7 @@ */ namespace unl { - std::map list; // List of binary pubkeys of UNL and their latest reported roundtime. + std::map list; // List of binary pubkeys of UNL and their latest reported roundtime. std::string json_list; // Stringified json array of UNL. (To be fed into the contract args) std::shared_mutex unl_mutex; @@ -98,13 +98,13 @@ namespace unl /** * Returns the majority roundtime reported among the unl. */ - uint16_t get_majority_roundtime() + uint32_t get_majority_roundtime() { std::unique_lock lock(unl_mutex); // Vote and find majority roundtime within the unl. // Fill any 0 roundtimes with information from peer connections. - std::map roundtime_votes; + std::map roundtime_votes; { std::scoped_lock lock(p2p::ctx.peer_connections_mutex); @@ -119,7 +119,7 @@ namespace unl itr->second = peer_itr->second->reported_roundtime; } - const uint16_t roundtime = itr->second; + const uint32_t roundtime = itr->second; if (roundtime > 0) roundtime_votes[roundtime]++; } @@ -127,7 +127,7 @@ namespace unl // Find the majority vote. uint32_t highest_votes = 0; - uint16_t majority_roundtime = 0; + uint32_t majority_roundtime = 0; for (const auto [roundtime, num_votes] : roundtime_votes) { if (num_votes > highest_votes) diff --git a/src/unl.hpp b/src/unl.hpp index 2d81c96f..386d05bf 100644 --- a/src/unl.hpp +++ b/src/unl.hpp @@ -18,7 +18,7 @@ namespace unl int init(); void update_unl_changes_from_patch(); void update_roundtime_stats(const std::list &proposals); - uint16_t get_majority_roundtime(); + uint32_t get_majority_roundtime(); bool update_unl_list(const std::set &new_list); const std::string prepare_json_list(const std::set &new_list); diff --git a/test/local-cluster/rundir.sh b/test/local-cluster/rundir.sh index 068404e9..4a7ebdfe 100755 --- a/test/local-cluster/rundir.sh +++ b/test/local-cluster/rundir.sh @@ -2,7 +2,7 @@ # Runs the specified contract directory with hpcore docker image. # This script assumes you already have the hpcore docker image and 'hpnet' virtual docker network. -# Usage: ./rundir.sh 1 +# Usage: ./rundir.sh # Validate the node count arg. if [ -z "$1" ]; then