Opportunistic clock sync from verified advert timestamps#2423
Opportunistic clock sync from verified advert timestamps#2423pelgraine wants to merge 3 commits intomeshcore-dev:devfrom
Conversation
GPS-less repeaters that lose their clock on power cycle can now automatically sync from signature-verified advert timestamps. Opt-in via shouldSyncClockFromMesh() override (default: false). Only fires when local clock is clearly wrong (before Jan 2025). Closes meshcore-dev#1329
|
I took a stab at peer-based time synchronization in #1443 , but there were genuine issues with the approach of relying on peer-based timed (such as clock poisoning, future dated clocks on ESP32, etcetera). There is also no way of knowing if some other's advert is on the right time. My PR used a statistical approach (Median Absolute Deviation by sampling many packets, while estimated taking time of flight into account. There are actually quite some genuine concerns by relying on peers to synchronize time, as also voice by reviewers. Also, with the continuous move towards reducing the number of flood adverts the reliance on flood packets for time keeping has also become less reliable.As such I closed the PR. This PR doesn't have any safeguards, doesn't use statistics, doesn't take time of flight into account and has the same issues (and more) trusting peer time. This PR simply takes any packet that comes around that has a time between Jan 2025 and Jan 2026 and updated their time to match the packet. If I were to send a flood advert using a timestamp set to April 1st, 2030, your repeater would happily adopt it. |
| if (shouldSyncClockFromMesh()) { | ||
| uint32_t local_time = _rtc->getCurrentTime(); | ||
| if (local_time < 1735689600UL // local clock before Jan 2025 (clearly wrong) | ||
| && timestamp > 1735689600UL // advert after Jan 2025 |
There was a problem hiding this comment.
You cannot assume a packet with a time between Jan 2025 and Jan 2036 has the right time, it can be off by years.
Closes #1329. Related: #2132, #1332, #1882.
Problem
GPS-less repeaters lose their clock on every power cycle, reverting to the firmware default (~May 2024). This means their adverts carry stale timestamps until an admin manually syncs the clock via serial, the mobile app, or a remote management command. For inaccessible repeaters (rooftops, hilltops, solar-powered sites that reboot daily), manual sync is impractical.
Solution
Add an opt-in mechanism for nodes to sync their local RTC from signature-verified advert timestamps. When a node's clock is clearly wrong (before Jan 2025) and it receives a valid advert with a sane timestamp (Jan 2025 – Jan 2036), it adopts that timestamp.
The sync only fires when:
shouldSyncClockFromMesh()(defaults tofalse)Once the clock is set past Jan 2025, the sync stops firing — it won't overwrite a valid clock with subsequent adverts.
Changes
src/Mesh.h— New virtual method:src/Mesh.cpp— 14 lines added after advert signature verification (inside thePAYLOAD_TYPE_ADVERThandler), beforeonAdvertRecv()call. Checks the opt-in gate, validates both local and incoming timestamps, and callssetCurrentTime()if appropriate.examples/simple_repeater/MyMesh.h— Single-line override:What this does NOT do
Testing
clock synced from advert: <epoch>Tested