From aaebf9d9d7e331ae0b288c0b57e3e218661499f0 Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Fri, 23 Sep 2022 15:51:26 -0300 Subject: [PATCH] Remove epee/time_helper It's included in a bunch of places, but only actually half-used in one, which is easily replaced with `` and tools::friendly_duration. --- contrib/epee/include/epee/time_helper.h | 35 ------------------------- contrib/epee/src/CMakeLists.txt | 1 - contrib/epee/src/time_helper.cpp | 31 ---------------------- src/cryptonote_core/blockchain.cpp | 12 ++++----- src/p2p/net_node.inl | 24 +++++++++++++---- src/p2p/p2p_protocol_defs.cpp | 8 +++--- src/p2p/p2p_protocol_defs.h | 1 - 7 files changed, 29 insertions(+), 83 deletions(-) delete mode 100644 contrib/epee/include/epee/time_helper.h delete mode 100644 contrib/epee/src/time_helper.cpp diff --git a/contrib/epee/include/epee/time_helper.h b/contrib/epee/include/epee/time_helper.h deleted file mode 100644 index f16dcd0b7..000000000 --- a/contrib/epee/include/epee/time_helper.h +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright (c) 2006-2013, Andrey N. Sabelnikov, www.sabelnikov.net -// All rights reserved. -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are met: -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above copyright -// notice, this list of conditions and the following disclaimer in the -// documentation and/or other materials provided with the distribution. -// * Neither the name of the Andrey N. Sabelnikov nor the -// names of its contributors may be used to endorse or promote products -// derived from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND -// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED -// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER BE LIABLE FOR ANY -// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES -// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; -// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND -// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS -// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// - -#pragma once -#include -#include - -namespace epee::misc_utils -{ - std::string get_internet_time_str(const time_t& time_); - std::string get_time_interval_string(time_t seconds); -} diff --git a/contrib/epee/src/CMakeLists.txt b/contrib/epee/src/CMakeLists.txt index 44d9ba609..06043bff4 100644 --- a/contrib/epee/src/CMakeLists.txt +++ b/contrib/epee/src/CMakeLists.txt @@ -38,7 +38,6 @@ add_library(epee parserse_base_utils.cpp portable_storage.cpp string_tools.cpp - time_helper.cpp wipeable_string.cpp ) diff --git a/contrib/epee/src/time_helper.cpp b/contrib/epee/src/time_helper.cpp deleted file mode 100644 index 1ac7f9623..000000000 --- a/contrib/epee/src/time_helper.cpp +++ /dev/null @@ -1,31 +0,0 @@ -#include "epee/time_helper.h" -#include "epee/pragma_comp_defs.h" - -namespace epee::misc_utils -{ - std::string get_internet_time_str(const time_t& time_) - { - char tmpbuf[200] = {0}; - tm* pt = NULL; -PRAGMA_WARNING_PUSH -PRAGMA_WARNING_DISABLE_VS(4996) - pt = gmtime(&time_); -PRAGMA_WARNING_POP - strftime( tmpbuf, 199, "%a, %d %b %Y %H:%M:%S GMT", pt ); - return tmpbuf; - } - - std::string get_time_interval_string(time_t seconds) - { -PRAGMA_WARNING_PUSH -PRAGMA_WARNING_DISABLE_VS(4244) - int days = seconds/(60*60*24); - seconds %= 60*60*24; - int hours = seconds /(60*60); - seconds %= (60*60); - int minutes = seconds/60; - seconds %= 60; -PRAGMA_WARNING_POP - return "d" + std::to_string(days) + ".h" + std::to_string(hours) + ".m" + std::to_string(minutes) + ".s" + std::to_string(seconds); - } -} diff --git a/src/cryptonote_core/blockchain.cpp b/src/cryptonote_core/blockchain.cpp index 7db11ed7a..a3657cb89 100644 --- a/src/cryptonote_core/blockchain.cpp +++ b/src/cryptonote_core/blockchain.cpp @@ -55,7 +55,6 @@ #include "cryptonote_config.h" #include "cryptonote_basic/miner.h" #include "epee/int-util.h" -#include "epee/time_helper.h" #include "common/threadpool.h" #include "common/boost_serialization_helper.h" #include "epee/warnings.h" @@ -512,11 +511,9 @@ bool Blockchain::init(BlockchainDB* db, sqlite3 *ons_db, std::shared_ptrget_top_block_timestamp(); - uint64_t timestamp_diff = time(NULL) - top_block_timestamp; - - // genesis block has no timestamp, could probably change it to have timestamp of 1341378000... - if(!top_block_timestamp) - timestamp_diff = time(NULL) - 1341378000; + // genesis block has no timestamp, so use block 1's timestamp if we get that: + if (!top_block_timestamp) + top_block_timestamp = 1525306361; // create general purpose async service queue @@ -528,7 +525,8 @@ bool Blockchain::init(BlockchainDB* db, sqlite3 *ons_db, std::shared_ptrheight() - 1, epee::misc_utils::get_time_interval_string(timestamp_diff)); + log::info(logcat, "Blockchain initialized. last block: {}, {} time ago", m_db->height() - 1, + tools::friendly_duration(std::chrono::system_clock::now() - std::chrono::system_clock::from_time_t(top_block_timestamp))); rtxn_guard.stop(); uint64_t num_popped_blocks = 0; diff --git a/src/p2p/net_node.inl b/src/p2p/net_node.inl index e0921a388..fe5ca0813 100644 --- a/src/p2p/net_node.inl +++ b/src/p2p/net_node.inl @@ -32,6 +32,7 @@ // IP blocking adapted from Boolberry #include +#include #include #include #include @@ -42,10 +43,10 @@ #include #include +#include "common/string_util.h" #include "cryptonote_config.h" #include "version.h" #include "epee/string_tools.h" -#include "epee/time_helper.h" #include "common/file.h" #include "common/fs-format.h" #include "common/pruning.h" @@ -1072,6 +1073,12 @@ namespace nodetool return connected; } + static std::string format_stamp_ago(int64_t stamp) { + if (stamp) + return tools::friendly_duration(std::chrono::system_clock::now() - std::chrono::system_clock::from_time_t(stamp)); + return "never"s; + } + template bool node_server::try_to_connect_and_handshake_with_new_peer(const epee::net_utils::network_address& na, bool just_take_peerlist, uint64_t last_seen_stamp, PeerType peer_type, uint64_t first_seen_stamp) { @@ -1091,7 +1098,8 @@ namespace nodetool } - log::debug(logcat, "Connecting to {}(peer_type={}, last_seen: {})...", na.str(), peer_type, (last_seen_stamp ? epee::misc_utils::get_time_interval_string(time(NULL) - last_seen_stamp):"never")); + log::debug(logcat, "Connecting to {}(peer_type={}, last_seen: {})...", + na.str(), peer_type, format_stamp_ago(last_seen_stamp)); auto con = zone.m_connect(zone, na); if(!con) @@ -1155,7 +1163,7 @@ namespace nodetool if (zone.m_connect == nullptr) return false; - log::info(logcat, "Connecting to {}(last_seen: {})...", na.str(), (last_seen_stamp ? epee::misc_utils::get_time_interval_string(time(NULL) - last_seen_stamp):"never")); + log::info(logcat, "Connecting to {}(last_seen: {})...", na.str(), format_stamp_ago(last_seen_stamp)); auto con = zone.m_connect(zone, na); if (!con) { @@ -1232,7 +1240,8 @@ namespace nodetool continue; } - log::debug(logcat, "Selected peer: {} {} first_seen: {}", peerid_to_string(pe.id), pe.adr.str(), epee::misc_utils::get_time_interval_string(time(NULL) - pe.first_seen)); + log::debug(logcat, "Selected peer: {} {} first_seen: {}", + peerid_to_string(pe.id), pe.adr.str(), format_stamp_ago(pe.first_seen)); if(!try_to_connect_and_handshake_with_new_peer(pe.adr, false, 0, anchor, pe.first_seen)) { log::debug(logcat, "Handshake failed"); @@ -1364,7 +1373,12 @@ namespace nodetool if(is_addr_recently_failed(pe.adr)) continue; - log::debug(logcat, "Selected peer: {} {}, pruning seed {} [peer_list={}] last_seen: {}", peerid_to_string(pe.id), pe.adr.str(), epee::string_tools::to_string_hex(pe.pruning_seed), (use_white_list ? white : gray), (pe.last_seen ? epee::misc_utils::get_time_interval_string(time(NULL) - pe.last_seen) : "never")); + log::debug(logcat, "Selected peer: {} {}, pruning seed {} [peer_list={}] last_seen: {}", + peerid_to_string(pe.id), + pe.adr.str(), + epee::string_tools::to_string_hex(pe.pruning_seed), + (use_white_list ? white : gray), + format_stamp_ago(pe.last_seen)); if(!try_to_connect_and_handshake_with_new_peer(pe.adr, false, pe.last_seen, use_white_list ? white : gray)) { log::debug(logcat, "Handshake failed"); diff --git a/src/p2p/p2p_protocol_defs.cpp b/src/p2p/p2p_protocol_defs.cpp index 8f223f107..d2cb6a07f 100644 --- a/src/p2p/p2p_protocol_defs.cpp +++ b/src/p2p/p2p_protocol_defs.cpp @@ -1,20 +1,22 @@ #include "p2p_protocol_defs.h" +#include "common/string_util.h" #include "epee/string_tools.h" -#include "epee/time_helper.h" #include "net/tor_address.h" // needed for serialization #include "net/i2p_address.h" // needed for serialization #include +#include namespace nodetool { std::string print_peerlist_to_string(const std::vector& pl) { - time_t now = time(nullptr); + auto now = std::chrono::system_clock::now(); std::string result; for (const auto& pe : pl) { result += "{:016x}\t{}\tpruning seed {}\tlast_seen {}"_format( pe.id, pe.adr.str(), pe.pruning_seed, - (pe.last_seen == 0 ? std::string("never") : epee::misc_utils::get_time_interval_string(now - pe.last_seen))); + pe.last_seen == 0 ? "never"s : tools::friendly_duration(now - std::chrono::system_clock::from_time_t(pe.last_seen)) + ); } return result; } diff --git a/src/p2p/p2p_protocol_defs.h b/src/p2p/p2p_protocol_defs.h index 7a9d21bcc..b4cd620c6 100644 --- a/src/p2p/p2p_protocol_defs.h +++ b/src/p2p/p2p_protocol_defs.h @@ -37,7 +37,6 @@ #include "net/tor_address.h" // needed for serialization #include "net/i2p_address.h" // needed for serialization #include "epee/string_tools.h" -#include "epee/time_helper.h" #include "cryptonote_config.h" #include "crypto/crypto.h"