Skip to content

Commit 25db643

Browse files
Merge pull request lightning#11 from rustyrussell/more-complex-gossip
BOLT lightning#7: More complex proposal, using three separate message types.
2 parents 9137aed + e96412a commit 25db643

File tree

2 files changed

+281
-0
lines changed

2 files changed

+281
-0
lines changed

Diff for: 02-peer-protocol.md

+10
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ This message indicates that the funding transaction has reached the `minimum-dep
231231
* [8:temporary-channel-id]
232232
* [8:channel-id]
233233
* [33:next-per-commitment-point]
234+
* [33:announcement-signature]
234235

235236
The `channel-id` is the unique description of the funding transaction.
236237
It is constructed with the most significant 3 bytes as the block
@@ -241,6 +242,8 @@ index which pays to the channel.
241242
This `channel-id` is used in all messages referring to the channel
242243
from this point onwards.
243244

245+
`announcement-signature` is the optional signature for `channel_announcement` as described in [BOLT #7](07-routing-gossip.md#the-channel_announcement-message).
246+
244247
#### Requirements
245248

246249
The sender MUST wait until the funding transaction has reached
@@ -256,6 +259,13 @@ transaction, derived as specified in
256259
If the recipient has already sent `funding_locked` it MUST fail the
257260
channel if `channel-id` does not match the `channel-id` it sent.
258261

262+
The sender MUST set `announcement-signature` to the signature for the
263+
channel_announcement message, or all zeroes if it does not want the
264+
channel announced.
265+
266+
The recipient SHOULD queue the `channel_announcement` message for its
267+
peers if it has sent and received a non-zero `announcement-signature`.
268+
259269
#### Future
260270

261271
We could add an SPV proof, and route block hashes in separate

Diff for: 07-routing-gossip.md

+271
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,271 @@
1+
# BOLT #7: P2P Node and Channel Discovery
2+
3+
This specification describes simple node discovery, channel discovery and channel update mechanisms which do not rely on a third-party to disseminate the information.
4+
5+
Node and channel discovery serve two different purposes:
6+
7+
- Channel discovery allows the creation and maintenance of a local view of the network's topology such that the node can discover routes to desired destination.
8+
- Node discovery allows nodes to broadcast their ID, host and port, such that other nodes can open connections and establish payment channels.
9+
10+
Peers in the network exchange `channel_announcement` messages that contain information about new channels between two nodes. They can also exchange `node_announcement` messages which supply additional information about nodes, and `channel_update` messages which update information about a channel.
11+
12+
There can only be one valid `channel_announcement` for any channel,
13+
but multiple `node_announcement` messages are possible (to update node
14+
information), and at least two `channel_update` messages are expected.
15+
16+
## The `channel_announcement` message
17+
18+
This message contains ownership information about a channel. It ties each
19+
on-chain bitcoin key to the lightning node key, and vice-versa.
20+
21+
The channel is not really usable until at least one side has announced
22+
its fee levels and expiry using `channel_update`.
23+
24+
1. type: 256 (`MSG_CHANNEL_ANNOUNCEMENT`)
25+
2. data:
26+
* [4:blockheight]
27+
* [3:blockindex]
28+
* [1:outputindex]
29+
* [33:node-id-1]
30+
* [33:node-id-2]
31+
* [33:bitcoin-key-1]
32+
* [33:bitcoin-key-2]
33+
* [64:bitcoin-signature-1]
34+
* [64:bitcoin-signature-2]
35+
* [64:node-signature-1]
36+
* [64:node-signature-2]
37+
38+
### Requirements
39+
40+
The creating node MUST set `blockheight` to the block number
41+
containing the confirmed funding transaction, `blockindex` to the
42+
transaction number within the block, and `outputindex` to the output
43+
number which funds the channel. The corresponding output MUST be a
44+
P2WSH as described in [BOLT #3](03-transactions.md#funding-transaction-output).
45+
46+
The creating node MUST set `node-id-1` and `node-id-2` to the public
47+
keys of the two nodes who are operating the channel, such that
48+
`node-id-1` is the numerically-lesser of the two DER encoded keys.
49+
ascending numerical order, and MUST set `bitcoin-key-1` and
50+
`bitcoin-key-2` to `funding-pubkey`s of `node-id-1` and `node-id-2`
51+
respectively.
52+
53+
The creating node MUST set `bitcoin-signature-1` to the signature of
54+
the double-SHA256 of `node-id-1` using `bitcoin-key-1`, and set
55+
`bitcoin-signature-2` to the signature of the double-SHA256 of
56+
`node-id-2` using `bitcoin-key-2`
57+
58+
The creating node MUST set `node-signature-1` to the signature of the
59+
double-SHA256 of every data field up to and including
60+
`bitcoin-signature-2` using `node-id-1`, and `node-signature-2` to the
61+
signature of the double-SHA256 of every data field up to and including
62+
`bitcoin-signature-2` using `node-id-2`.
63+
64+
The receiving node MUST ignore the message if the output `outputindex`
65+
in block `blockheight` transaction number `blockindex` does not
66+
correspond to a P2WSH using `bitcoin-key-1` and `bitcoin-key-2` as
67+
specified in [BOLT #3](03-transactions.md#funding-transaction-output).
68+
The receiving node MUST ignore the message if this output is spent.
69+
70+
Otherwise, the receiving node SHOULD fail the connection if
71+
`bitcoin-signature-1`, `bitcoin-signature-2`, `node-signature-1` or
72+
`node-signature-2` are invalid or not correct.
73+
74+
Otherwise, if `node-id-1` or `node-id-2` are blacklisted, it SHOULD
75+
ignore the message.
76+
77+
Otherwise, if the transaction referred to was not previously announced
78+
as a channel, the receiving node SHOULD queue the message for
79+
rebroadcasting. If it has previously received a valid
80+
`channel_announcement` for the same transaction in the same block, but
81+
different `node-id-1` or `node-id-2`, it SHOULD blacklist the
82+
previous message's `node-id-1` and `node-id-2` as well as this
83+
`node-id-1` and `node-id-2` and forget channels connected to them,
84+
otherwise it SHOULD store this `channel_announcement`.
85+
86+
The receiving node SHOULD forget a channel once its funding output has
87+
been spent or reorganized out.
88+
89+
## Rationale
90+
91+
Requiring both nodes to sign indicates they are both willing to route
92+
other payments via this node (ie. take part of the public network).
93+
Requiring the bitcoin signatures proves they control the channel.
94+
95+
The blacklisting of conflicting nodes means that we disallow multiple
96+
different announcements: no node should ever do this, as it implies
97+
that keys have leaked.
98+
99+
While channels shouldn't be advertised before they are sufficiently
100+
deep, the requirement against rebroadcasting only applies if the
101+
transaction hasn't moved to a different block.
102+
103+
## The `node_announcement` message
104+
105+
This allows a node to indicate extra data associated with it, in
106+
addition to its public key. To avoid trivial denial of service attacks,
107+
nodes for which a channel is not already known are ignored.
108+
109+
1. type: 257 (`MSG_NODE_ANNOUNCEMENT`)
110+
2. data:
111+
* [64:signature]
112+
* [4:timestamp]
113+
* [16:ipv6]
114+
* [2:port]
115+
* [33:node-id]
116+
* [3:rgb-color]
117+
* [2:pad]
118+
* [32:alias]
119+
120+
The `timestamp` allows ordering in the case of multiple announcements;
121+
the `ipv6` and `port` allow the node to announce its willingness to
122+
accept incoming network connections, the `rgb-color` and `alias` allow
123+
intelligence services to give their nodes cool monikers like IRATEMONK
124+
and WISTFULTOLL and use the color black.
125+
126+
### Requirements
127+
128+
The creating node MUST set `timestamp` to be greater than any previous
129+
`node_announcement` it has created. It MAY base it on a UNIX
130+
timestamp. It MUST set the `ipv6` and `port` fields to all zeroes, or
131+
a non-zero `port` and `ipv6` set to a valid IPv6 address or an IPv4-Mapped IPv6 Address format as defined in [RFC 4291 section 2.5.5.2](https://tools.ietf.org/html/rfc4291#section-2.5.5.2). It MUST set `signature` to the signature of
132+
the double-SHA256 of the entire remaining packet after `signature` using the
133+
key given by `node-id`. It MUST set `pad` to zero. It MAY set `alias` and `rgb-color` to customize their node's appearance in maps and graphs, where the first byte of `rgb` is the red value, the second byte is the green value and the last byte is the blue value. It MUST set `alias` to a valid UTF-8 string of up to 21 bytes in length, with all `alias` bytes following equal to zero.
134+
135+
The receiving node SHOULD fail the connection if `signature` is
136+
invalid or incorrect for the entire message including unknown fields
137+
following `alias`, and MUST NOT further process the message. The
138+
receiving node SHOULD ignore `ipv6` if `port` is zero. It SHOULD fail
139+
the connection if the final byte of `alias` is not zero. It MUST ignore
140+
the contents of `pad`.
141+
142+
The receiving node SHOULD ignore the message if `node-id` is not
143+
previously known from a `channel_announcement` message, or if
144+
`timestamp` is not greater than the last-received
145+
`node_announcement` from this `node-id`. Otherwise, if the
146+
`timestamp` is greater than the last-received `node_announcement` from
147+
this `node-id` the receiving node SHOULD queue the message for
148+
rebroadcasting.
149+
150+
The receiving node MAY use `rgb` and `alias` to reference nodes in interfaces, but SHOULD insinuate their self-signed origin.
151+
152+
### Rationale
153+
154+
RFC 4291 section 2.5.5.2 described IPv4 addresses like so:
155+
156+
```
157+
| 80 bits | 16 | 32 bits |
158+
+--------------------------------------+--------------------------+
159+
|0000..............................0000|FFFF| IPv4 address |
160+
+--------------------------------------+----+---------------------+
161+
```
162+
163+
## The `channel_update` message
164+
165+
After a channel has been initially announced, each side independently
166+
announces its fees and minimum expiry for HTLCs. It uses the 8-byte
167+
channel shortid which matches the `channel_announcement` and one byte
168+
to indicate which end this is. It can do this multiple times, if
169+
it wants to change fees.
170+
171+
1. type: 258 (`MSG_CHANNEL_UPDATE`)
172+
2. data:
173+
* [64:signature]
174+
* [4:blockheight]
175+
* [3:blockindex]
176+
* [1:outputindex]
177+
* [4:timestamp]
178+
* [1:side]
179+
* [1:pad]
180+
* [2:expiry]
181+
* [4:htlc-minimum-msat]
182+
* [4:fee-base-msat]
183+
* [4:fee-proportional-millionths]
184+
185+
### Requirements
186+
187+
The creating node MUST set `signature` to the signature of the
188+
double-SHA256 of the entire remaining packet after `signature` using its own `node-id`.
189+
190+
The creating node MUST set `blockheight`, `blockindex` and `outputindex` to
191+
match those in the already-sent `channel_announcement` message, and MUST set `side` to 0 if the creating node is `node-id-1` in that message, otherwise 1.
192+
193+
The creating node MUST set `timestamp` to greater than zero, and MUST set it to greater than any previously-sent `channel_update` for this channel, and MUST set `pad` to zero.
194+
195+
It MUST set `expiry` to the number of blocks it will subtract from an incoming HTLC's `expiry`. It MUST set `htlc-minimum-msat` to the minimum HTLC value it will accept, in millisatoshi. It MUST set `fee-base-msat` to the base fee it will charge for any HTLC, in millisatoshi, and `fee-proportional-millionths` to the amount it will charge per millionth of a satoshi.
196+
197+
The receiving node SHOULD fail the connection if `side` is not 0 or 1.
198+
It MUST ignore the contents of `pad`. The receiving node SHOULD fail
199+
the connection if `signature` is invalid or incorrect for the entire
200+
message including unknown fields following `signature`, and MUST NOT
201+
further process the message. The receiving node SHOULD ignore `ipv6`
202+
if `port` is zero. It SHOULD ignore the message if `blockheight`,
203+
`blockindex` and `outputindex` do not correspond to a previously
204+
known, unspent channel from `channel_announcement` or if `timestamp`
205+
is not greater than than the last-received `channel_announcement` for
206+
this channel and `side`. Otherwise, if the `timestamp` is equal to
207+
the last-received `channel_announcement` and the fields other than
208+
`signature` differ, the node MAY blacklist this node-id and forget all
209+
channels associated with it. Otherwise the receiving node SHOULD
210+
queue the message for rebroadcasting.
211+
212+
## Rebroadcasting
213+
214+
Nodes receiving a new `channel_announcement` or a `channel_update` or
215+
`node_update` with an updated timestamp update their local view of the network's topology accordingly.
216+
217+
Once the announcement has been processed it is added to a list of outgoing announcements (perhaps replacing older updates) to the processing node's peers, which will be flushed at regular intervals.
218+
This store and delayed forward broadcast is called a _staggered broadcast_
219+
220+
If, after applying the changes from the announcement, there are no channels associated with the announcing node, then the receiving node MAY purge the announcing node from the set of known nodes.
221+
Otherwise the receiving node updates the metadata and stores the signature associated with the announcement.
222+
This will later allow the receiving node to rebuild the announcement for its peers.
223+
224+
After processing the announcement the receiving node adds the announcement to a list of outgoing announcements.
225+
226+
### Requirements
227+
228+
Each node SHOULD flush outgoing announcements once every 60 seconds, independently of the arrival times of announcements, resulting in a staggered announcement and deduplication of announcements.
229+
230+
Nodes MAY re-announce their channels regularly, however this is discouraged in order to keep the resource requirements low.
231+
232+
Nodes SHOULD send all `channel_announcement` messages followed by the
233+
latest `node_announcement` and `channel_update` messages upon
234+
connection establishment.
235+
236+
### Rationale
237+
238+
Batching announcements form a natural ratelimit with low overhead.
239+
240+
The sending of all announcements on reconnection is naive, but simple,
241+
and allows bootstrap for new nodes as well as updating for nodes which
242+
have been offline for some time.
243+
244+
## HTLC Fees
245+
246+
The node creating `channel_update` SHOULD accept HTLCs which pay a fee equal or greater than:
247+
248+
fee-base-msat + htlc-amount-msat * fee-proportional-millionths / 1000000
249+
250+
The node creating `channel_update` SHOULD accept HTLCs which pay an
251+
older fee for some time after sending `channel_update` to allow for
252+
propagation delay.
253+
254+
## Recommendations for Routing
255+
256+
As the fee is proportional, it must be calculated backwards from the
257+
destination to the source: only the amount required at the final
258+
destination is known initially.
259+
260+
When calculating a route for an HTLC, the `expiry` and the fee both
261+
need to be considered: the `expiry` contributes to the time that funds
262+
will be unavailable on worst-case failure. The tradeoff between these
263+
two is unclear, as it depends on the reliability of nodes.
264+
265+
Other more advanced considerations involve diversity of routes to
266+
avoid single points of failure and detection, and channel balance
267+
of local channels.
268+
269+
## References
270+
271+
- [RFC 4291](https://tools.ietf.org/html/rfc4291)

0 commit comments

Comments
 (0)