From fef5fe8555d26a544fa540796f7e8b54c6cd20bb Mon Sep 17 00:00:00 2001 From: clemahieu Date: Tue, 28 Feb 2023 16:12:04 +0000 Subject: [PATCH] Add typedefs for timer units. (#4162) Updating variable types in usages to use new typesdefs. --- nano/lib/blocks.cpp | 4 ++-- nano/lib/blocks.hpp | 5 +++-- nano/lib/timer.hpp | 13 +++++++++++-- nano/node/transport/socket.cpp | 2 +- nano/node/transport/socket.hpp | 3 ++- nano/rpc_test/rpc.cpp | 16 ++++++++-------- nano/secure/common.cpp | 2 +- nano/secure/common.hpp | 11 ++++++----- 8 files changed, 34 insertions(+), 22 deletions(-) diff --git a/nano/lib/blocks.cpp b/nano/lib/blocks.cpp index 6e404755ab..ab5f9183f0 100644 --- a/nano/lib/blocks.cpp +++ b/nano/lib/blocks.cpp @@ -1760,7 +1760,7 @@ std::string nano::state_subtype (nano::block_details const details_a) } } -nano::block_sideband::block_sideband (nano::account const & account_a, nano::block_hash const & successor_a, nano::amount const & balance_a, uint64_t const height_a, uint64_t const timestamp_a, nano::block_details const & details_a, nano::epoch const source_epoch_a) : +nano::block_sideband::block_sideband (nano::account const & account_a, nano::block_hash const & successor_a, nano::amount const & balance_a, uint64_t const height_a, nano::seconds_t const timestamp_a, nano::block_details const & details_a, nano::epoch const source_epoch_a) : successor (successor_a), account (account_a), balance (balance_a), @@ -1771,7 +1771,7 @@ nano::block_sideband::block_sideband (nano::account const & account_a, nano::blo { } -nano::block_sideband::block_sideband (nano::account const & account_a, nano::block_hash const & successor_a, nano::amount const & balance_a, uint64_t const height_a, uint64_t const timestamp_a, nano::epoch const epoch_a, bool const is_send, bool const is_receive, bool const is_epoch, nano::epoch const source_epoch_a) : +nano::block_sideband::block_sideband (nano::account const & account_a, nano::block_hash const & successor_a, nano::amount const & balance_a, uint64_t const height_a, nano::seconds_t const timestamp_a, nano::epoch const epoch_a, bool const is_send, bool const is_receive, bool const is_epoch, nano::epoch const source_epoch_a) : successor (successor_a), account (account_a), balance (balance_a), diff --git a/nano/lib/blocks.hpp b/nano/lib/blocks.hpp index a4320dc4f1..abfb6521ca 100644 --- a/nano/lib/blocks.hpp +++ b/nano/lib/blocks.hpp @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -58,8 +59,8 @@ class block_sideband final { public: block_sideband () = default; - block_sideband (nano::account const &, nano::block_hash const &, nano::amount const &, uint64_t const, uint64_t const, nano::block_details const &, nano::epoch const source_epoch_a); - block_sideband (nano::account const &, nano::block_hash const &, nano::amount const &, uint64_t const, uint64_t const, nano::epoch const epoch_a, bool const is_send, bool const is_receive, bool const is_epoch, nano::epoch const source_epoch_a); + block_sideband (nano::account const &, nano::block_hash const &, nano::amount const &, uint64_t const, nano::seconds_t const local_timestamp, nano::block_details const &, nano::epoch const source_epoch_a); + block_sideband (nano::account const &, nano::block_hash const &, nano::amount const &, uint64_t const, nano::seconds_t const local_timestamp, nano::epoch const epoch_a, bool const is_send, bool const is_receive, bool const is_epoch, nano::epoch const source_epoch_a); void serialize (nano::stream &, nano::block_type) const; bool deserialize (nano::stream &, nano::block_type); static size_t size (nano::block_type); diff --git a/nano/lib/timer.hpp b/nano/lib/timer.hpp index 29e0acd2c5..166a212e9f 100644 --- a/nano/lib/timer.hpp +++ b/nano/lib/timer.hpp @@ -98,13 +98,22 @@ class timer void update_ticks (); }; -inline uint64_t milliseconds_since_epoch () +using millis_t = uint64_t; + +inline millis_t milliseconds_since_epoch () { return std::chrono::duration_cast (std::chrono::system_clock::now ().time_since_epoch ()).count (); } -inline uint64_t seconds_since_epoch () +using seconds_t = uint64_t; + +inline seconds_t seconds_since_epoch () { return std::chrono::duration_cast (std::chrono::system_clock::now ().time_since_epoch ()).count (); } + +inline nano::millis_t time_difference (nano::millis_t start, nano::millis_t end) +{ + return end > start ? (end - start) : 0; } +} \ No newline at end of file diff --git a/nano/node/transport/socket.cpp b/nano/node/transport/socket.cpp index 9ebb1eb158..aee4ee353d 100644 --- a/nano/node/transport/socket.cpp +++ b/nano/node/transport/socket.cpp @@ -208,7 +208,7 @@ void nano::transport::socket::checkup () return; } - uint64_t now (nano::seconds_since_epoch ()); + nano::seconds_t now = nano::seconds_since_epoch (); auto condition_to_disconnect{ false }; // if this is a server socket, and no data is received for silent_connection_tolerance_time seconds then disconnect diff --git a/nano/node/transport/socket.hpp b/nano/node/transport/socket.hpp index e2676d560c..f92f13313f 100644 --- a/nano/node/transport/socket.hpp +++ b/nano/node/transport/socket.hpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -142,7 +143,7 @@ class socket : public std::enable_shared_from_this /** the timestamp (in seconds since epoch) of the last time there was successful receive on the socket * successful receive includes graceful closing of the socket by the peer (the read succeeds but returns 0 bytes) */ - std::atomic last_receive_time_or_init; + std::atomic last_receive_time_or_init; /** Flag that is set when cleanup decides to close the socket due to timeout. * NOTE: Currently used by tcp_server::timeout() but I suspect that this and tcp_server::timeout() are not needed. diff --git a/nano/rpc_test/rpc.cpp b/nano/rpc_test/rpc.cpp index 812603a4b7..d247645dff 100644 --- a/nano/rpc_test/rpc.cpp +++ b/nano/rpc_test/rpc.cpp @@ -4421,7 +4421,7 @@ TEST (rpc, account_info) .work (*node1->work_generate_blocking (latest)) .build (); ASSERT_EQ (nano::process_result::progress, node1->process (*send).code); - auto time (nano::seconds_since_epoch ()); + auto time = nano::seconds_since_epoch (); { auto transaction = node1->store.tx_begin_write (); node1->store.confirmation_height.put (transaction, nano::dev::genesis_key.pub, { 1, nano::dev::genesis->hash () }); @@ -5086,7 +5086,7 @@ TEST (rpc, ledger) .work (*node->work_generate_blocking (key.pub)) .build (); ASSERT_EQ (nano::process_result::progress, node->process (*open).code); - auto time (nano::seconds_since_epoch ()); + auto time = nano::seconds_since_epoch (); auto const rpc_ctx = add_rpc (system, node); boost::property_tree::ptree request; request.put ("action", "ledger"); @@ -5686,7 +5686,7 @@ TEST (rpc, wallet_ledger) .work (*node1->work_generate_blocking (key.pub)) .build (); ASSERT_EQ (nano::process_result::progress, node1->process (*open).code); - auto time (nano::seconds_since_epoch ()); + auto time = nano::seconds_since_epoch (); auto const rpc_ctx = add_rpc (system, node1); boost::property_tree::ptree request; request.put ("action", "wallet_ledger"); @@ -6142,7 +6142,7 @@ TEST (rpc, unchecked_get) { auto response (wait_response (system, rpc_ctx, request)); ASSERT_EQ (1, response.count ("contents")); - auto timestamp (response.get ("modified_timestamp")); + auto timestamp (response.get ("modified_timestamp")); ASSERT_LE (timestamp, nano::seconds_since_epoch ()); } request.put ("json_block", true); @@ -6150,7 +6150,7 @@ TEST (rpc, unchecked_get) auto response (wait_response (system, rpc_ctx, request)); auto & contents (response.get_child ("contents")); ASSERT_EQ ("state", contents.get ("type")); - auto timestamp (response.get ("modified_timestamp")); + auto timestamp (response.get ("modified_timestamp")); ASSERT_LE (timestamp, nano::seconds_since_epoch ()); } } @@ -6297,14 +6297,14 @@ TEST (rpc, DISABLED_wallet_history) node_config.enable_voting = false; auto node = add_ipc_enabled_node (system, node_config); system.wallet (0)->insert_adhoc (nano::dev::genesis_key.prv); - auto timestamp1 (nano::seconds_since_epoch ()); + auto timestamp1 = nano::seconds_since_epoch (); auto send (system.wallet (0)->send_action (nano::dev::genesis_key.pub, nano::dev::genesis_key.pub, node->config.receive_minimum.number ())); ASSERT_NE (nullptr, send); - auto timestamp2 (nano::seconds_since_epoch ()); + auto timestamp2 = nano::seconds_since_epoch (); auto receive (system.wallet (0)->receive_action (send->hash (), nano::dev::genesis_key.pub, node->config.receive_minimum.number (), send->link ().as_account ())); ASSERT_NE (nullptr, receive); nano::keypair key; - auto timestamp3 (nano::seconds_since_epoch ()); + auto timestamp3 = nano::seconds_since_epoch (); auto send2 (system.wallet (0)->send_action (nano::dev::genesis_key.pub, key.pub, node->config.receive_minimum.number ())); ASSERT_NE (nullptr, send2); system.deadline_set (10s); diff --git a/nano/secure/common.cpp b/nano/secure/common.cpp index 88aa0caea4..0231279dc4 100644 --- a/nano/secure/common.cpp +++ b/nano/secure/common.cpp @@ -221,7 +221,7 @@ nano::keypair::keypair (std::string const & prv_a) ed25519_publickey (prv.bytes.data (), pub.bytes.data ()); } -nano::account_info::account_info (nano::block_hash const & head_a, nano::account const & representative_a, nano::block_hash const & open_block_a, nano::amount const & balance_a, uint64_t modified_a, uint64_t block_count_a, nano::epoch epoch_a) : +nano::account_info::account_info (nano::block_hash const & head_a, nano::account const & representative_a, nano::block_hash const & open_block_a, nano::amount const & balance_a, nano::seconds_t modified_a, uint64_t block_count_a, nano::epoch epoch_a) : head (head_a), representative (representative_a), open_block (open_block_a), diff --git a/nano/secure/common.hpp b/nano/secure/common.hpp index 5acaf05cc7..f1d52ff7cf 100644 --- a/nano/secure/common.hpp +++ b/nano/secure/common.hpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include @@ -103,7 +104,7 @@ class account_info final { public: account_info () = default; - account_info (nano::block_hash const &, nano::account const &, nano::block_hash const &, nano::amount const &, uint64_t, uint64_t, epoch); + account_info (nano::block_hash const &, nano::account const &, nano::block_hash const &, nano::amount const &, nano::seconds_t modified, uint64_t, epoch); bool deserialize (nano::stream &); bool operator== (nano::account_info const &) const; bool operator!= (nano::account_info const &) const; @@ -114,7 +115,7 @@ class account_info final nano::block_hash open_block{ 0 }; nano::amount balance{ 0 }; /** Seconds since posix epoch */ - uint64_t modified{ 0 }; + nano::seconds_t modified{ 0 }; uint64_t block_count{ 0 }; nano::epoch epoch_m{ nano::epoch::epoch_0 }; }; @@ -203,7 +204,7 @@ class unchecked_info final unchecked_info (std::shared_ptr const &); void serialize (nano::stream &) const; bool deserialize (nano::stream &); - uint64_t modified () const; + nano::seconds_t modified () const; std::shared_ptr block; private: @@ -255,7 +256,7 @@ class vote final vote () = default; vote (nano::vote const &); vote (bool &, nano::stream &); - vote (nano::account const &, nano::raw_key const &, uint64_t timestamp, uint8_t duration, std::vector const &); + vote (nano::account const &, nano::raw_key const &, nano::millis_t timestamp, uint8_t duration, std::vector const &); std::string hashes_string () const; nano::block_hash hash () const; nano::block_hash full_hash () const; @@ -276,7 +277,7 @@ class vote final uint8_t duration_bits () const; std::chrono::milliseconds duration () const; static uint64_t constexpr timestamp_mask = { 0xffff'ffff'ffff'fff0ULL }; - static uint64_t constexpr timestamp_max = { 0xffff'ffff'ffff'fff0ULL }; + static nano::seconds_t constexpr timestamp_max = { 0xffff'ffff'ffff'fff0ULL }; static uint64_t constexpr timestamp_min = { 0x0000'0000'0000'0010ULL }; static uint8_t constexpr duration_max = { 0x0fu };