[net processing] Use std::chrono for type-safe time offsets
This commit is contained in:
4 files changed
+9
-6
No files matched your search
@@ -392,7 +392,7 @@ struct Peer {
|
|||||||
|
|
||||||
/** Time offset computed during the version handshake based on the
|
/** Time offset computed during the version handshake based on the
|
||||||
* timestamp the peer sent in the version message. */
|
* timestamp the peer sent in the version message. */
|
||||||
std::atomic<int64_t> m_time_offset{0};
|
std::atomic<std::chrono::seconds> m_time_offset{0s};
|
||||||
|
|
||||||
explicit Peer(NodeId id, ServiceFlags our_services)
|
explicit Peer(NodeId id, ServiceFlags our_services)
|
||||||
: m_id{id}
|
: m_id{id}
|
||||||
@@ -3671,11 +3671,11 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
|
|||||||
peer->m_starting_height, addrMe.ToStringAddrPort(), fRelay, pfrom.GetId(),
|
peer->m_starting_height, addrMe.ToStringAddrPort(), fRelay, pfrom.GetId(),
|
||||||
remoteAddr, (mapped_as ? strprintf(", mapped_as=%d", mapped_as) : ""));
|
remoteAddr, (mapped_as ? strprintf(", mapped_as=%d", mapped_as) : ""));
|
||||||
|
|
||||||
peer->m_time_offset = nTime - GetTime();
|
peer->m_time_offset = NodeSeconds{std::chrono::seconds{nTime}} - Now<NodeSeconds>();
|
||||||
if (!pfrom.IsInboundConn()) {
|
if (!pfrom.IsInboundConn()) {
|
||||||
// Don't use timedata samples from inbound peers to make it
|
// Don't use timedata samples from inbound peers to make it
|
||||||
// harder for others to tamper with our adjusted time.
|
// harder for others to tamper with our adjusted time.
|
||||||
AddTimeData(pfrom.addr, peer->m_time_offset);
|
AddTimeData(pfrom.addr, Ticks<std::chrono::seconds>(peer->m_time_offset.load()));
|
||||||
}
|
}
|
||||||
|
|
||||||
// If the peer is old enough to have the old alert system, send it the final alert.
|
// If the peer is old enough to have the old alert system, send it the final alert.
|
||||||
|
|||||||
@@ -9,6 +9,8 @@
|
|||||||
#include <net.h>
|
#include <net.h>
|
||||||
#include <validationinterface.h>
|
#include <validationinterface.h>
|
||||||
|
|
||||||
|
#include <chrono>
|
||||||
|
|
||||||
class AddrMan;
|
class AddrMan;
|
||||||
class CChainParams;
|
class CChainParams;
|
||||||
class CTxMemPool;
|
class CTxMemPool;
|
||||||
@@ -41,7 +43,7 @@ struct CNodeStateStats {
|
|||||||
bool m_addr_relay_enabled{false};
|
bool m_addr_relay_enabled{false};
|
||||||
ServiceFlags their_services;
|
ServiceFlags their_services;
|
||||||
int64_t presync_height{-1};
|
int64_t presync_height{-1};
|
||||||
int64_t time_offset{0};
|
std::chrono::seconds time_offset{0};
|
||||||
};
|
};
|
||||||
|
|
||||||
class PeerManager : public CValidationInterface, public NetEventsInterface
|
class PeerManager : public CValidationInterface, public NetEventsInterface
|
||||||
|
|||||||
@@ -23,6 +23,7 @@
|
|||||||
#include <rpc/server.h>
|
#include <rpc/server.h>
|
||||||
#include <util/strencodings.h>
|
#include <util/strencodings.h>
|
||||||
#include <util/string.h>
|
#include <util/string.h>
|
||||||
|
#include <util/time.h>
|
||||||
#include <util/threadnames.h>
|
#include <util/threadnames.h>
|
||||||
|
|
||||||
#include <univalue.h>
|
#include <univalue.h>
|
||||||
@@ -1227,7 +1228,7 @@ void RPCConsole::updateDetailWidget()
|
|||||||
// This check fails for example if the lock was busy and
|
// This check fails for example if the lock was busy and
|
||||||
// nodeStateStats couldn't be fetched.
|
// nodeStateStats couldn't be fetched.
|
||||||
if (stats->fNodeStateStatsAvailable) {
|
if (stats->fNodeStateStatsAvailable) {
|
||||||
ui->timeoffset->setText(GUIUtil::formatTimeOffset(stats->nodeStateStats.time_offset));
|
ui->timeoffset->setText(GUIUtil::formatTimeOffset(Ticks<std::chrono::seconds>(stats->nodeStateStats.time_offset)));
|
||||||
ui->peerServices->setText(GUIUtil::formatServicesStr(stats->nodeStateStats.their_services));
|
ui->peerServices->setText(GUIUtil::formatServicesStr(stats->nodeStateStats.their_services));
|
||||||
// Sync height is init to -1
|
// Sync height is init to -1
|
||||||
if (stats->nodeStateStats.nSyncHeight > -1) {
|
if (stats->nodeStateStats.nSyncHeight > -1) {
|
||||||
|
|||||||
+1
-1
@@ -239,7 +239,7 @@ static RPCHelpMan getpeerinfo()
|
|||||||
obj.pushKV("bytessent", stats.nSendBytes);
|
obj.pushKV("bytessent", stats.nSendBytes);
|
||||||
obj.pushKV("bytesrecv", stats.nRecvBytes);
|
obj.pushKV("bytesrecv", stats.nRecvBytes);
|
||||||
obj.pushKV("conntime", count_seconds(stats.m_connected));
|
obj.pushKV("conntime", count_seconds(stats.m_connected));
|
||||||
obj.pushKV("timeoffset", statestats.time_offset);
|
obj.pushKV("timeoffset", Ticks<std::chrono::seconds>(statestats.time_offset));
|
||||||
if (stats.m_last_ping_time > 0us) {
|
if (stats.m_last_ping_time > 0us) {
|
||||||
obj.pushKV("pingtime", Ticks<SecondsDouble>(stats.m_last_ping_time));
|
obj.pushKV("pingtime", Ticks<SecondsDouble>(stats.m_last_ping_time));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user