|
23 | 23 | #include <netbase.h> |
24 | 24 | #include <netmessagemaker.h> |
25 | 25 | #include <node/blockstorage.h> |
| 26 | +#include <node/timeoffsets.h> |
26 | 27 | #include <node/txreconciliation.h> |
27 | 28 | #include <policy/fees.h> |
28 | 29 | #include <policy/policy.h> |
|
34 | 35 | #include <scheduler.h> |
35 | 36 | #include <streams.h> |
36 | 37 | #include <sync.h> |
37 | | -#include <timedata.h> |
38 | 38 | #include <tinyformat.h> |
39 | 39 | #include <txmempool.h> |
40 | 40 | #include <txorphanage.h> |
@@ -390,6 +390,10 @@ struct Peer { |
390 | 390 | /** Whether this peer wants invs or headers (when possible) for block announcements */ |
391 | 391 | bool m_prefers_headers GUARDED_BY(NetEventsInterface::g_msgproc_mutex){false}; |
392 | 392 |
|
| 393 | + /** Time offset computed during the version handshake based on the |
| 394 | + * timestamp the peer sent in the version message. */ |
| 395 | + std::atomic<std::chrono::seconds> m_time_offset{0s}; |
| 396 | + |
393 | 397 | explicit Peer(NodeId id, ServiceFlags our_services) |
394 | 398 | : m_id{id} |
395 | 399 | , m_our_services{our_services} |
@@ -513,7 +517,7 @@ class PeerManagerImpl final : public PeerManager |
513 | 517 | std::optional<std::string> FetchBlock(NodeId peer_id, const CBlockIndex& block_index) override |
514 | 518 | EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex); |
515 | 519 | bool GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) const override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex); |
516 | | - bool IgnoresIncomingTxs() override { return m_opts.ignore_incoming_txs; } |
| 520 | + PeerManagerInfo GetInfo() const override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex); |
517 | 521 | void SendPings() override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex); |
518 | 522 | void RelayTransaction(const uint256& txid, const uint256& wtxid) override EXCLUSIVE_LOCKS_REQUIRED(!m_peer_mutex); |
519 | 523 | void SetBestBlock(int height, std::chrono::seconds time) override |
@@ -788,6 +792,8 @@ class PeerManagerImpl final : public PeerManager |
788 | 792 | /** Next time to check for stale tip */ |
789 | 793 | std::chrono::seconds m_stale_tip_check_time GUARDED_BY(cs_main){0s}; |
790 | 794 |
|
| 795 | + TimeOffsets m_outbound_time_offsets; |
| 796 | + |
791 | 797 | const Options m_opts; |
792 | 798 |
|
793 | 799 | bool RejectIncomingTxs(const CNode& peer) const; |
@@ -1864,10 +1870,19 @@ bool PeerManagerImpl::GetNodeStateStats(NodeId nodeid, CNodeStateStats& stats) c |
1864 | 1870 | stats.presync_height = peer->m_headers_sync->GetPresyncHeight(); |
1865 | 1871 | } |
1866 | 1872 | } |
| 1873 | + stats.time_offset = peer->m_time_offset; |
1867 | 1874 |
|
1868 | 1875 | return true; |
1869 | 1876 | } |
1870 | 1877 |
|
| 1878 | +PeerManagerInfo PeerManagerImpl::GetInfo() const |
| 1879 | +{ |
| 1880 | + return PeerManagerInfo{ |
| 1881 | + .median_outbound_time_offset = m_outbound_time_offsets.Median(), |
| 1882 | + .ignores_incoming_txs = m_opts.ignore_incoming_txs, |
| 1883 | + }; |
| 1884 | +} |
| 1885 | + |
1871 | 1886 | void PeerManagerImpl::AddToCompactExtraTransactions(const CTransactionRef& tx) |
1872 | 1887 | { |
1873 | 1888 | if (m_opts.max_extra_txs <= 0) |
@@ -3861,12 +3876,12 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type, |
3861 | 3876 | peer->m_starting_height, addrMe.ToStringAddrPort(), fRelay, pfrom.GetId(), |
3862 | 3877 | remoteAddr, (mapped_as ? strprintf(", mapped_as=%d", mapped_as) : "")); |
3863 | 3878 |
|
3864 | | - int64_t nTimeOffset = nTime - GetTime(); |
3865 | | - pfrom.nTimeOffset = nTimeOffset; |
| 3879 | + peer->m_time_offset = NodeSeconds{std::chrono::seconds{nTime}} - Now<NodeSeconds>(); |
3866 | 3880 | if (!pfrom.IsInboundConn()) { |
3867 | 3881 | // Don't use timedata samples from inbound peers to make it |
3868 | | - // harder for others to tamper with our adjusted time. |
3869 | | - AddTimeData(pfrom.addr, nTimeOffset); |
| 3882 | + // harder for others to create false warnings about our clock being out of sync. |
| 3883 | + m_outbound_time_offsets.Add(peer->m_time_offset); |
| 3884 | + m_outbound_time_offsets.WarnIfOutOfSync(); |
3870 | 3885 | } |
3871 | 3886 |
|
3872 | 3887 | // If the peer is old enough to have the old alert system, send it the final alert. |
|
0 commit comments