Merge pull request #3021 from TheBlueMatt/2024-04-drop-blocked-completed-updates
[rust-lightning] / lightning-rapid-gossip-sync / README.md
1 # lightning-rapid-gossip-sync
2
3 This crate exposes functionality for rapid gossip graph syncing, aimed primarily at mobile clients.
4 Its server counterpart is the
5 [rapid-gossip-sync-server](https://github.com/lightningdevkit/rapid-gossip-sync-server) repository.
6
7 ## Mechanism
8
9 The (presumed) server sends a compressed gossip response containing gossip data. The gossip data is
10 formatted compactly, omitting signatures and opportunistically incremental where previous channel
11 updates are known.
12
13 Essentially, the serialization structure is as follows:
14
15 1. Fixed prefix bytes `76, 68, 75, 1` (the first three bytes are ASCII for `LDK`)
16     - The purpose of this prefix is to identify the serialization format, should other rapid gossip
17       sync formats arise in the future
18     - The fourth byte is the protocol version in case our format gets updated
19 2. Chain hash (32 bytes)
20 3. Latest seen timestamp (`u32`)
21 4. An unsigned int indicating the number of node IDs to follow
22 5. An array of compressed node ID pubkeys (all pubkeys are presumed to be standard
23    compressed 33-byte-serializations)
24 6. An unsigned int indicating the number of channel announcement messages to follow
25 7. An array of significantly stripped down customized channel announcements
26 8. An unsigned int indicating the number of channel update messages to follow
27 9. A series of default values used for non-incremental channel updates
28     - The values are defined as follows:
29         1. `default_cltv_expiry_delta`
30         2. `default_htlc_minimum_msat`
31         3. `default_fee_base_msat`
32         4. `default_fee_proportional_millionths`
33         5. `default_htlc_maximum_msat` (`u64`, and if the default is no maximum, `u64::MAX`)
34     - The defaults are calculated by the server based on the frequency among non-incremental
35       updates within a given delta set
36 10. An array of customized channel updates
37
38 You will also notice that `NodeAnnouncement` messages are omitted altogether as the node IDs are
39 implicitly extracted from the channel announcements and updates.
40
41 The data is then applied to the current network graph, artificially dated to the timestamp of the
42 latest seen message less one week, be it an announcement or an update, from the server's
43 perspective. The network graph should not be pruned until the graph sync completes.
44
45 ### Custom Channel Announcement
46
47 To achieve compactness and avoid data repetition, we're sending a significantly stripped down
48 version of the channel announcement message, which contains only the following data:
49
50 1. `channel_features`: `u16` + `n`, where `n` is the number of bytes indicated by the first `u16`
51 2. `short_channel_id`: `CompactSize` (incremental `CompactSize` deltas starting from 0)
52 3. `node_id_1_index`: `CompactSize` (index of node id within the previously sent sequence)
53 4. `node_id_2_index`: `CompactSize` (index of node id within the previously sent sequence)
54
55 ### Custom Channel Update
56
57 For the purpose of rapid syncing, we have deviated from the channel update format specified in
58 BOLT 7 significantly. Our custom channel updates are structured as follows:
59
60 1. `short_channel_id`: `CompactSize` (incremental `CompactSize` deltas starting at 0)
61 2. `custom_channel_flags`: `u8`
62 3. `update_data`
63
64 Specifically, our custom channel flags break down like this:
65
66 | 128                 | 64 | 32 | 16 | 8 | 4 | 2                | 1         |
67 |---------------------|----|----|----|---|---|------------------|-----------|
68 | Incremental update? |    |    |    |   |   | Disable channel? | Direction |
69
70 If the most significant bit is set to `1`, indicating an incremental update, the intermediate bit
71 flags assume the following meaning:
72
73 | 64                              | 32                              | 16                          | 8                                         | 4                               |
74 |---------------------------------|---------------------------------|-----------------------------|-------------------------------------------|---------------------------------|
75 | `cltv_expiry_delta` has changed | `htlc_minimum_msat` has changed | `fee_base_msat` has changed | `fee_proportional_millionths` has changed | `htlc_maximum_msat` has changed |
76
77 If the most significant bit is set to `0`, the meaning is almost identical, except instead of a
78 change, the flags now represent a deviation from the defaults sent at the beginning of the update
79 sequence.
80
81 In both cases, `update_data` only contains the fields that are indicated by the channel flags to be
82 non-default or to have mutated.
83
84 ## Delta Calculation
85
86 The way a server is meant to calculate this rapid gossip sync data is by taking the latest time
87 any change, be it either an announcement or an update, was seen. That timestamp is included in each
88 rapid sync message, so all the client needs to do is cache one variable.
89
90 If a particular channel update had never occurred before, the full update is sent. If a channel has
91 had updates prior to the provided timestamp, the latest update prior to the timestamp is taken as a
92 reference, and the delta is calculated against it.
93
94 Depending on whether the rapid sync message is calculated on the fly or a snapshotted version is
95 returned, intermediate changes between the latest update seen by the client and the latest update
96 broadcast on the network may be taken into account when calculating the delta.
97
98 ## Performance
99
100 Given the primary purpose of this utility is a faster graph sync, we thought it might be helpful to
101 provide some examples of various delta sets. These examples were calculated as of May 19th  2022
102 with a network graph comprised of 80,000 channel announcements and 160,000 directed channel updates.
103
104 | Full sync                   |        |
105 |-----------------------------|--------|
106 | Message Length              | 4.7 MB |
107 | Gzipped Message Length      | 2.0 MB |
108 | Client-side Processing Time | 1.4 s  |
109
110 | Week-old sync               |        |
111 |-----------------------------|--------|
112 | Message Length              | 2.7 MB |
113 | Gzipped Message Length      | 862 kB |
114 | Client-side Processing Time | 907 ms |
115
116 | Day-old sync                |         |
117 |-----------------------------|---------|
118 | Message Length              | 191 kB  |
119 | Gzipped Message Length      | 92.8 kB |
120 | Client-side Processing Time | 196 ms  |