Skip to content

Commit 55361a1

Browse files
committed
[net processing] Use std::chrono for type-safe time offsets
1 parent 038fd97 commit 55361a1

File tree

4 files changed

+9
-6
lines changed

4 files changed

+9
-6
lines changed

src/net_processing.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ struct Peer {
392392

393393
/** Time offset computed during the version handshake based on the
394394
* timestamp the peer sent in the version message. */
395-
std::atomic<int64_t> m_time_offset{0};
395+
std::atomic<std::chrono::seconds> m_time_offset{0s};
396396

397397
explicit Peer(NodeId id, ServiceFlags our_services)
398398
: m_id{id}
@@ -3671,11 +3671,11 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
36713671
peer->m_starting_height, addrMe.ToStringAddrPort(), fRelay, pfrom.GetId(),
36723672
remoteAddr, (mapped_as ? strprintf(", mapped_as=%d", mapped_as) : ""));
36733673

3674-
peer->m_time_offset = nTime - GetTime();
3674+
peer->m_time_offset = NodeSeconds{std::chrono::seconds{nTime}} - Now<NodeSeconds>();
36753675
if (!pfrom.IsInboundConn()) {
36763676
// Don't use timedata samples from inbound peers to make it
36773677
// harder for others to tamper with our adjusted time.
3678-
AddTimeData(pfrom.addr, peer->m_time_offset);
3678+
AddTimeData(pfrom.addr, Ticks<std::chrono::seconds>(peer->m_time_offset.load()));
36793679
}
36803680

36813681
// If the peer is old enough to have the old alert system, send it the final alert.

src/net_processing.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include <net.h>
1010
#include <validationinterface.h>
1111

12+
#include <chrono>
13+
1214
class AddrMan;
1315
class CChainParams;
1416
class CTxMemPool;
@@ -41,7 +43,7 @@ struct CNodeStateStats {
4143
bool m_addr_relay_enabled{false};
4244
ServiceFlags their_services;
4345
int64_t presync_height{-1};
44-
int64_t time_offset{0};
46+
std::chrono::seconds time_offset{0};
4547
};
4648

4749
class PeerManager : public CValidationInterface, public NetEventsInterface

src/qt/rpcconsole.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <rpc/server.h>
2424
#include <util/strencodings.h>
2525
#include <util/string.h>
26+
#include <util/time.h>
2627
#include <util/threadnames.h>
2728

2829
#include <univalue.h>
@@ -1227,7 +1228,7 @@ void RPCConsole::updateDetailWidget()
12271228
// This check fails for example if the lock was busy and
12281229
// nodeStateStats couldn't be fetched.
12291230
if (stats->fNodeStateStatsAvailable) {
1230-
ui->timeoffset->setText(GUIUtil::formatTimeOffset(stats->nodeStateStats.time_offset));
1231+
ui->timeoffset->setText(GUIUtil::formatTimeOffset(Ticks<std::chrono::seconds>(stats->nodeStateStats.time_offset)));
12311232
ui->peerServices->setText(GUIUtil::formatServicesStr(stats->nodeStateStats.their_services));
12321233
// Sync height is init to -1
12331234
if (stats->nodeStateStats.nSyncHeight > -1) {

src/rpc/net.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ static RPCHelpMan getpeerinfo()
239239
obj.pushKV("bytessent", stats.nSendBytes);
240240
obj.pushKV("bytesrecv", stats.nRecvBytes);
241241
obj.pushKV("conntime", count_seconds(stats.m_connected));
242-
obj.pushKV("timeoffset", statestats.time_offset);
242+
obj.pushKV("timeoffset", Ticks<std::chrono::seconds>(statestats.time_offset));
243243
if (stats.m_last_ping_time > 0us) {
244244
obj.pushKV("pingtime", Ticks<SecondsDouble>(stats.m_last_ping_time));
245245
}

0 commit comments

Comments
 (0)