Skip to content

Commit

Permalink
Add typedefs for timer units. (#4162)
Browse files Browse the repository at this point in the history
Updating variable types in usages to use new typesdefs.
  • Loading branch information
clemahieu committed Feb 28, 2023
1 parent 3a496cb commit fef5fe8
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 22 deletions.
4 changes: 2 additions & 2 deletions nano/lib/blocks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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),
Expand Down
5 changes: 3 additions & 2 deletions nano/lib/blocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <nano/lib/numbers.hpp>
#include <nano/lib/optional_ptr.hpp>
#include <nano/lib/stream.hpp>
#include <nano/lib/timer.hpp>
#include <nano/lib/utility.hpp>
#include <nano/lib/work.hpp>

Expand Down Expand Up @@ -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);
Expand Down
13 changes: 11 additions & 2 deletions nano/lib/timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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::milliseconds> (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::seconds> (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;
}
}
2 changes: 1 addition & 1 deletion nano/node/transport/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion nano/node/transport/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <nano/boost/asio/ip/tcp.hpp>
#include <nano/boost/asio/strand.hpp>
#include <nano/lib/asio.hpp>
#include <nano/lib/timer.hpp>

#include <chrono>
#include <map>
Expand Down Expand Up @@ -142,7 +143,7 @@ class socket : public std::enable_shared_from_this<nano::transport::socket>
/** 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<uint64_t> last_receive_time_or_init;
std::atomic<nano::seconds_t> 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.
Expand Down
16 changes: 8 additions & 8 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 () });
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -6142,15 +6142,15 @@ TEST (rpc, unchecked_get)
{
auto response (wait_response (system, rpc_ctx, request));
ASSERT_EQ (1, response.count ("contents"));
auto timestamp (response.get<uint64_t> ("modified_timestamp"));
auto timestamp (response.get<nano::seconds_t> ("modified_timestamp"));
ASSERT_LE (timestamp, nano::seconds_since_epoch ());
}
request.put ("json_block", true);
{
auto response (wait_response (system, rpc_ctx, request));
auto & contents (response.get_child ("contents"));
ASSERT_EQ ("state", contents.get<std::string> ("type"));
auto timestamp (response.get<uint64_t> ("modified_timestamp"));
auto timestamp (response.get<nano::seconds_t> ("modified_timestamp"));
ASSERT_LE (timestamp, nano::seconds_since_epoch ());
}
}
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion nano/secure/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
11 changes: 6 additions & 5 deletions nano/secure/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <nano/lib/numbers.hpp>
#include <nano/lib/rep_weights.hpp>
#include <nano/lib/stats.hpp>
#include <nano/lib/timer.hpp>
#include <nano/lib/utility.hpp>

#include <boost/iterator/transform_iterator.hpp>
Expand Down Expand Up @@ -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;
Expand All @@ -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 };
};
Expand Down Expand Up @@ -203,7 +204,7 @@ class unchecked_info final
unchecked_info (std::shared_ptr<nano::block> const &);
void serialize (nano::stream &) const;
bool deserialize (nano::stream &);
uint64_t modified () const;
nano::seconds_t modified () const;
std::shared_ptr<nano::block> block;

private:
Expand Down Expand Up @@ -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<nano::block_hash> const &);
vote (nano::account const &, nano::raw_key const &, nano::millis_t timestamp, uint8_t duration, std::vector<nano::block_hash> const &);
std::string hashes_string () const;
nano::block_hash hash () const;
nano::block_hash full_hash () const;
Expand All @@ -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 };

Expand Down

0 comments on commit fef5fe8

Please sign in to comment.