# Quick peer gossip catchup protocol

**URL:** <https://delvingbitcoin.org/t/quick-peer-gossip-catchup-protocol/251>\
**Category:** Protocol Design\
**Tags:** lightning\
**Created:** [December 5, 2023, 3:22am UTC](https://delvingbitcoin.org/t/quick-peer-gossip-catchup-protocol/251 "2023-12-05T03:22:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rustyrussell](https://delvingbitcoin.org/user_avatar/delvingbitcoin.org/rustyrussell/32/154_2.png) [@rustyrussell](https://delvingbitcoin.org/u/rustyrussell)\
**Post date:** [December 5, 2023, 3:22am UTC](https://delvingbitcoin.org/t/quick-peer-gossip-catchup-protocol/251/1 "2023-12-05T03:22:20Z")

</div>

Hi all! I’ve been reworking CLN’s gossip daemon, and it is clear that we can fairly easy save a little information on a per-peer basis and send them all the gossip which we’ve received since they disconnected (minus a little bit, probably).

I’m thinking of it as a new feature “option\_serves\_gossip\_catchup”. The peer would send the gossip\_timestamp\_filter with an odd TLV field indicating it wanted gossip since last time, though I suspect we’ll want a new message to say “cannot serve from last time” if (say) the peer doesn’t have a channel, or dataloss; in this case just stream gossip from now on, if they want the entire gossip then they simply re-send gossip\_timestamp\_filter with 0 as time.

Implementing this for us simply means tracking the last gossip\_store offset we sent, and adjusting it when we compact (on restart). This requires some work and storage per peer, so we may want to restrict it to those which have channels with us.

As an addition, we could prioritize “important” gossip (new channels of significance), though that’s harder to specify.

---

<div class="post-metadata">

**Author:** ![MattCorallo](https://delvingbitcoin.org/letter_avatar_proxy/v4/letter/m/e47c2d/32.png) [@MattCorallo](https://delvingbitcoin.org/u/MattCorallo)\
**Post date:** [December 5, 2023, 5:49am UTC](https://delvingbitcoin.org/t/quick-peer-gossip-catchup-protocol/251/2 "2023-12-05T05:49:50Z")

</div>

Can we accomplish this without any per-peer storage? Probably something like “the last time I was online, running, and received gossip was block header/timestamp X”. From there any peer that does receive-time tracking should be able to go back into their gossip history and check for everything newer than said time (minus a fudge factor).

Of course that assumes nodes keep track of when they received all gossip messages. Currently we only track that for channel announcements, not updates. TBH I’d really rather we Just Do Minisketch and move on rather than trying to keep applying bandaids to the current gossip.

---

<div class="post-metadata">

**Author:** ![rustyrussell](https://delvingbitcoin.org/user_avatar/delvingbitcoin.org/rustyrussell/32/154_2.png) [@rustyrussell](https://delvingbitcoin.org/u/rustyrussell)\
**Post date:** [December 6, 2023, 5:06am UTC](https://delvingbitcoin.org/t/quick-peer-gossip-catchup-protocol/251/3 "2023-12-06T05:06:18Z")

</div>

> TBH I’d really rather we Just Do Minisketch and move on rather than trying to keep applying bandaids to the current gossip.

Sure, but you need gossip v1.5 for minisketch. And you still might want this for catchup, if you only have a few peers (i.e. one peer!).

Meanwhile, I thought “no, your proposal is just like gossip\_timestamp\_filter which we’re moving away from”, but actually, /it’s different/, being based on the gossip time, not the timestamps.

If you record the timestamp when you commit the gossip, assuming 60 second relay, you’re pretty close. If the peer reports when they last received gossip you can subtract 120 seconds and probably do a pretty good job.

It turns out, we even have a field in our gossip\_store header “timestamp”, which is redundant (it used to be a copy of the update timestamp, used for easy timestamp filter when we supported that). We use that without a format change.

We’d still need to keep some offset markers to avoid having to sweep the entire file when asked for a specific time, but that’s actually extremely implementable…

---

<div class="post-metadata">

**Author:** ![cdecker](https://delvingbitcoin.org/user_avatar/delvingbitcoin.org/cdecker/32/99_2.png) [@cdecker](https://delvingbitcoin.org/u/cdecker)\
**Post date:** [December 6, 2023, 1:23pm UTC](https://delvingbitcoin.org/t/quick-peer-gossip-catchup-protocol/251/4 "2023-12-06T13:23:54Z")

</div>

Fwiw this sounds very close to what `lnsync` does. It assigns an arbitatry ordering to the messages and allows clients to seek through that ordering. Since the ordering only has to make sense to the serving node, and any misordering can only result in a bit too much being sent across this seems like a trivial and backwards compatible solution to quickly sync up.

While the idea of canonical ordering by the serving node works quite nicely, we can notice that we also have a partial order among messages, with the `channel_announcement` being the outlier, since it cannot be uniquely ordered among the other messages that may pertain to the channel or the endpoints.

- `channel_update` sort by timestamp, and allow querying a range based on these timestamps
- `node_announcement` sort by timestamp, and allow querying a range based on these timestamps
- `channel_announcement` does not have a timestamp, and ought to be included if the querying node is asking for a timestamp range that include ANY update to the channel

As you can see the only potential over-sharing is in the form of the `channel_announcement` but I don’t think that is solvable, since there is no unique position in the ordering that’d guarantee inclusion iff a matching `channel_update` is included.

This allows us to avoid per-peer storage: on startup we load the timestamp when we were last online, we then subtract a buffer from that (1/2h?) and we tell our peers that timestamp when reconnecting. This means we don’t store anything per-peer, rather we only store a timestamp up to which we believe we are up to date, and then ask for incremental diffs from that point onwards.

---

<div class="post-metadata">

**Author:** ![rustyrussell](https://delvingbitcoin.org/user_avatar/delvingbitcoin.org/rustyrussell/32/154_2.png) [@rustyrussell](https://delvingbitcoin.org/u/rustyrussell)\
**Post date:** [December 6, 2023, 8:01pm UTC](https://delvingbitcoin.org/t/quick-peer-gossip-catchup-protocol/251/5 "2023-12-06T20:01:08Z")

</div>

This is, in fact, what gossip\_timestamp\_filter does:

```auto
1. type: 265 (`gossip_timestamp_filter`) (`gossip_queries`)
2. data:
    * [`chain_hash`:`chain_hash`]
    * [`u32`:`first_timestamp`]
    * [`u32`:`timestamp_range`]

```

The “timestamp” for channel\_announcement is defined as " the `timestamp` of a corresponding `channel_update`." (rather than “all corresponding channel\_update” which is what you specified).

It turns out, however, that everyone uses “none”, “all” or “recent”, where recent is 10 minutes ago (CLN), now (LND and Eclair), 1 hour ago (LDK). To avoid scanning the entire gossip\_store (or keeping more metadata), we now keep a single “first record with timestamp \> 2 hours ago” offset and scan from there, unless they set first\_update to 0. This basically turns the field into a trinary.

But this is _still_ not what a single-peer-gossiper wants! The timestamp of the record is a very rough guide to “have you seen this before”. A better guide is when the _peer_ received the gossip: presumably it started streaming it within 60 seconds. It’s also much easier for us to implement: for speed we can keep the received timestamps at every ~1MB of gossip store, since they will be monotonic.
