Skip to content

Commit

Permalink
[net processing] Warn user if the median outbound time offset is larg…
Browse files Browse the repository at this point in the history
…er than 70min
  • Loading branch information
dergoegge committed Jan 2, 2024
1 parent aa1d9b4 commit e7df61b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/net_processing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
#include <util/strencodings.h>
#include <util/trace.h>
#include <validation.h>
#include <warnings.h>

#include <algorithm>
#include <atomic>
Expand Down Expand Up @@ -3615,6 +3616,12 @@ void PeerManagerImpl::ProcessMessage(CNode& pfrom, const std::string& msg_type,
AddTimeData(pfrom.addr, peer->m_time_offset);

m_outbound_time_offsets.Add(peer->m_time_offset);

auto median = m_outbound_time_offsets.Median();
if (median > m_opts.time_offset_warn_threshold.count() ||
median < -m_opts.time_offset_warn_threshold.count()) {
SetMedianTimeOffsetWarning();
}
}

// If the peer is old enough to have the old alert system, send it the final alert.
Expand Down
3 changes: 3 additions & 0 deletions src/net_processing.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ static const bool DEFAULT_PEERBLOCKFILTERS = false;
static const int DISCOURAGEMENT_THRESHOLD{100};
/** Maximum number of outstanding CMPCTBLOCK requests for the same block. */
static const unsigned int MAX_CMPCTBLOCKS_INFLIGHT_PER_BLOCK = 3;
static constexpr std::chrono::seconds DEFAULT_TIME_OFFSET_WARN_THRESHOLD{70min};

struct CNodeStateStats {
int nSyncHeight = -1;
Expand Down Expand Up @@ -67,6 +68,8 @@ class PeerManager : public CValidationInterface, public NetEventsInterface
//! Whether or not the internal RNG behaves deterministically (this is
//! a test-only option).
bool deterministic_rng{false};
//! Threshold for the network adjusted time offset warning.
std::chrono::seconds time_offset_warn_threshold{DEFAULT_TIME_OFFSET_WARN_THRESHOLD};
};

static std::unique_ptr<PeerManager> make(CConnman& connman, AddrMan& addrman,
Expand Down
2 changes: 2 additions & 0 deletions src/node/peerman_args.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ void ApplyArgsManOptions(const ArgsManager& argsman, PeerManager::Options& optio
if (auto value{argsman.GetBoolArg("-capturemessages")}) options.capture_messages = *value;

if (auto value{argsman.GetBoolArg("-blocksonly")}) options.ignore_incoming_txs = *value;

if (auto value{argsman.GetIntArg("-maxtimeadjustment")}) options.time_offset_warn_threshold = std::chrono::seconds{*value};
}

} // namespace node
Expand Down
12 changes: 12 additions & 0 deletions src/warnings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
static GlobalMutex g_warnings_mutex;
static bilingual_str g_misc_warnings GUARDED_BY(g_warnings_mutex);
static bool fLargeWorkInvalidChainFound GUARDED_BY(g_warnings_mutex) = false;
static bool g_timeoffset_warning GUARDED_BY(g_warnings_mutex){};

void SetMiscWarning(const bilingual_str& warning)
{
Expand All @@ -28,6 +29,12 @@ void SetfLargeWorkInvalidChainFound(bool flag)
fLargeWorkInvalidChainFound = flag;
}

void SetMedianTimeOffsetWarning()
{
LOCK(g_warnings_mutex);
g_timeoffset_warning = true;
}

bilingual_str GetWarnings(bool verbose)
{
bilingual_str warnings_concise;
Expand All @@ -52,6 +59,11 @@ bilingual_str GetWarnings(bool verbose)
warnings_verbose.emplace_back(warnings_concise);
}

if (g_timeoffset_warning) {
warnings_concise = Untranslated("Please check that your computer's date and time are correct");
warnings_verbose.emplace_back(warnings_concise);
}

if (verbose) {
return Join(warnings_verbose, Untranslated("<hr />"));
}
Expand Down
1 change: 1 addition & 0 deletions src/warnings.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct bilingual_str;

void SetMiscWarning(const bilingual_str& warning);
void SetfLargeWorkInvalidChainFound(bool flag);
void SetMedianTimeOffsetWarning();
/** Format a string that describes several potential problems detected by the core.
* @param[in] verbose bool
* - if true, get all warnings separated by <hr />
Expand Down

0 comments on commit e7df61b

Please sign in to comment.