07-routing-gossip.md (56223B)
1 # BOLT #7: P2P Node and Channel Discovery 2 3 This specification describes simple node discovery, channel discovery, and channel update mechanisms that do not rely on a third-party to disseminate the information. 4 5 Node and channel discovery serve two different purposes: 6 7 - Node discovery allows nodes to broadcast their ID, host, and port, so that other nodes can open connections and establish payment channels with them. 8 - Channel discovery allows the creation and maintenance of a local view of the network's topology, so that a node can discover routes to desired destinations. 9 10 To support channel and node discovery, three *gossip messages* are supported: 11 12 - For node discovery, peers exchange `node_announcement` 13 messages, which supply additional information about the nodes. There may be 14 multiple `node_announcement` messages, in order to update the node information. 15 16 - For channel discovery, peers in the network exchange 17 `channel_announcement` messages containing information regarding new 18 channels between the two nodes. They can also exchange `channel_update` 19 messages, which update information about a channel. There can only be 20 one valid `channel_announcement` for any channel, but at least two 21 `channel_update` messages are expected. 22 23 # Table of Contents 24 25 * [Definition of `short_channel_id`](#definition-of-short_channel_id) 26 * [The `announcement_signatures` Message](#the-announcement_signatures-message) 27 * [The `channel_announcement` Message](#the-channel_announcement-message) 28 * [The `node_announcement` Message](#the-node_announcement-message) 29 * [The `channel_update` Message](#the-channel_update-message) 30 * [Query Messages](#query-messages) 31 * [Rebroadcasting](#rebroadcasting) 32 * [HTLC Fees](#htlc-fees) 33 * [Pruning the Network View](#pruning-the-network-view) 34 * [Recommendations for Routing](#recommendations-for-routing) 35 * [References](#references) 36 37 ## Definition of `short_channel_id` 38 39 The `short_channel_id` is the unique description of the funding transaction. 40 It is constructed as follows: 41 1. the most significant 3 bytes: indicating the block height 42 2. the next 3 bytes: indicating the transaction index within the block 43 3. the least significant 2 bytes: indicating the output index that pays to the channel. 44 45 The standard human readable format for `short_channel_id` is created 46 by printing the above components, in the order: 47 block height, transaction index, and output index. 48 Each component is printed as a decimal number, 49 and separated from each other by the small letter `x`. 50 For example, a `short_channel_id` might be written as `539268x845x1`, 51 indicating a channel on the output 1 of the transaction at index 845 52 of the block at height 539268. 53 54 ### Rationale 55 56 The `short_channel_id` human readable format is designed 57 so that double-clicking or double-tapping it will select the entire ID 58 on most systems. 59 Humans prefer decimal when reading numbers, 60 so the ID components are written in decimal. 61 The small letter `x` is used since on most fonts, 62 the `x` is visibly smaller than decimal digits, 63 making it easy to visibly group each component of the ID. 64 65 ## The `announcement_signatures` Message 66 67 This is a direct message between the two endpoints of a channel and serves as an opt-in mechanism to allow the announcement of the channel to the rest of the network. 68 It contains the necessary signatures, by the sender, to construct the `channel_announcement` message. 69 70 1. type: 259 (`announcement_signatures`) 71 2. data: 72 * [`channel_id`:`channel_id`] 73 * [`short_channel_id`:`short_channel_id`] 74 * [`signature`:`node_signature`] 75 * [`signature`:`bitcoin_signature`] 76 77 The willingness of the initiating node to announce the channel is signaled during channel opening by setting the `announce_channel` bit in `channel_flags` (see [BOLT #2](02-peer-protocol.md#the-open_channel-message)). 78 79 ### Requirements 80 81 The `announcement_signatures` message is created by constructing a `channel_announcement` message, 82 corresponding to the newly confirmed channel funding transaction, and signing it with the secrets 83 matching an endpoint's `node_id` and `bitcoin_key`. 84 85 A node: 86 - If the `open_channel` message has the `announce_channel` bit set AND a `shutdown` message has not been sent: 87 - After `channel_ready` has been sent and received AND the funding transaction has enough confirmations to ensure that it won't be reorganized: 88 - MUST send `announcement_signatures` for the funding transaction. 89 - After `splice_locked` has been sent and received AND the splice transaction has enough confirmations to ensure that it won't be reorganized: 90 - MUST send `announcement_signatures` for the matching splice transaction. 91 - Otherwise: 92 - MUST NOT send the `announcement_signatures` message. 93 - Upon reconnection (once the above timing requirements have been met): 94 - If it has NOT previously received `announcement_signatures` for the funding transaction: 95 - MUST send its own `announcement_signatures` message. 96 - If it receives `announcement_signatures` for the funding transaction: 97 - MUST respond with its own `announcement_signatures` message. 98 - If it has NOT previously received `announcement_signatures` for a splice transaction: 99 - MUST SET the `announcement_signatures` bit in the `retransmit_flags` of `my_current_funding_locked`. 100 - If the `announcement_signatures` bit is set in the *remote* `retransmit_flags`: 101 - MUST retransmit its `announcement_signatures` message. 102 103 A recipient node: 104 - If the `short_channel_id` doesn't match one of its funding transactions: 105 - SHOULD send a `warning`. 106 - If the `node_signature` OR the `bitcoin_signature` is NOT correct: 107 - MAY send a `warning` and close the connection, or send an `error` and fail the channel. 108 - If it has sent AND received a valid `announcement_signatures` message: 109 - If the funding transaction has at least 6 confirmations: 110 - SHOULD queue the `channel_announcement` message for its peers. 111 - If it has not sent `channel_ready`: 112 - SHOULD defer handling the `announcement_signatures` until after it has sent `channel_ready`. 113 - If it has not sent `splice_locked` for the transaction matching this `short_channel_id`: 114 - SHOULD defer handling the `announcement_signatures` until after it has sent `splice_locked`. 115 116 ### Rationale 117 118 Channels must not be announced before the funding transaction has enough 119 confirmations, because a blockchain reorganization would otherwise invalidate 120 the `short_channel_id`. 121 122 When splicing is used, a `channel_announcement` is generated for every splice 123 transaction once both sides have sent `splice_locked`. This lets the network 124 know that the transaction spending a currently active channel is a splice and 125 not a closing transaction, and this channel can still be used with its updated 126 `short_channel_id`. 127 128 ## The `channel_announcement` Message 129 130 This gossip message contains ownership information regarding a channel. It ties 131 each on-chain Bitcoin key to the associated Lightning node key, and vice-versa. 132 The channel is not practically usable until at least one side has announced 133 its fee levels and expiry, using `channel_update`. 134 135 Proving the existence of a channel between `node_1` and `node_2` requires: 136 137 1. proving that the funding transaction pays to `bitcoin_key_1` and 138 `bitcoin_key_2` 139 2. proving that `node_1` owns `bitcoin_key_1` 140 3. proving that `node_2` owns `bitcoin_key_2` 141 142 Assuming that all nodes know the unspent transaction outputs, the first proof is 143 accomplished by a node finding the output given by the `short_channel_id` and 144 verifying that it is indeed a P2WSH funding transaction output for those keys 145 specified in [BOLT #3](03-transactions.md#funding-transaction-output). 146 147 The last two proofs are accomplished through explicit signatures: 148 `bitcoin_signature_1` and `bitcoin_signature_2` are generated for each 149 `bitcoin_key` and each of the corresponding `node_id`s are signed. 150 151 It's also necessary to prove that `node_1` and `node_2` both agree on the 152 announcement message: this is accomplished by having a signature from each 153 `node_id` (`node_signature_1` and `node_signature_2`) signing the message. 154 155 1. type: 256 (`channel_announcement`) 156 2. data: 157 * [`signature`:`node_signature_1`] 158 * [`signature`:`node_signature_2`] 159 * [`signature`:`bitcoin_signature_1`] 160 * [`signature`:`bitcoin_signature_2`] 161 * [`u16`:`len`] 162 * [`len*byte`:`features`] 163 * [`chain_hash`:`chain_hash`] 164 * [`short_channel_id`:`short_channel_id`] 165 * [`point`:`node_id_1`] 166 * [`point`:`node_id_2`] 167 * [`point`:`bitcoin_key_1`] 168 * [`point`:`bitcoin_key_2`] 169 170 ### Requirements 171 172 The origin node: 173 - MUST set `chain_hash` to the 32-byte hash that uniquely identifies the chain 174 that the channel was opened within: 175 - for the _Bitcoin blockchain_: 176 - MUST set `chain_hash` value (encoded in hex) equal to `6fe28c0ab6f1b372c1a6a246ae63f74f931e8365e15a089c68d6190000000000`. 177 - When announcing a channel creation: 178 - MUST set `short_channel_id` to refer to the confirmed funding transaction, 179 as specified in [BOLT #2](02-peer-protocol.md#the-channel_ready-message). 180 - When announcing a splice transaction: 181 - MUST set `short_channel_id` to refer to the confirmed splice transaction 182 for which `splice_locked` has been sent and received, as specified in 183 [BOLT #2](02-peer-protocol.md#the-splice_locked-message). 184 - SHOULD keep relaying payments that use the `short_channel_id`s of its 185 previous `channel_announcement`s. 186 - SHOULD send a new `channel_update` using the `short_channel_id` that 187 matches the latest `channel_announcement`. 188 - Note: the corresponding output MUST be a P2WSH, as described in [BOLT #3](03-transactions.md#funding-transaction-output). 189 - MUST set `node_id_1` and `node_id_2` to the public keys of the two nodes 190 operating the channel, such that `node_id_1` is the lexicographically-lesser of the 191 two compressed keys sorted in ascending lexicographic order. 192 - MUST set `bitcoin_key_1` and `bitcoin_key_2` to `node_id_1` and `node_id_2`'s 193 respective `funding_pubkey`s. 194 - MUST compute the double-SHA256 hash `h` of the message, beginning at offset 195 256, up to the end of the message. 196 - Note: the hash skips the 4 signatures but hashes the rest of the message, 197 including any future fields appended to the end. 198 - MUST set `node_signature_1` and `node_signature_2` to valid 199 signatures of the hash `h` (using `node_id_1` and `node_id_2`'s respective 200 secrets). 201 - MUST set `bitcoin_signature_1` and `bitcoin_signature_2` to valid 202 signatures of the hash `h` (using `bitcoin_key_1` and `bitcoin_key_2`'s 203 respective secrets). 204 - MUST set `features` based on what features were negotiated for this channel, according to [BOLT #9](09-features.md#assigned-features-flags) 205 - MUST set `len` to the minimum length required to hold the `features` bits 206 it sets. 207 - If the funding transaction has less than 6 confirmations: 208 - MUST NOT send `channel_announcement`. 209 210 The receiving node: 211 - MUST verify the integrity AND authenticity of the message by verifying the 212 signatures. 213 - if `node_id_1` is not lexicographically less than `node_id_2`: 214 - SHOULD send a `warning`. 215 - MAY close the connection. 216 - MUST ignore the message. 217 - if there is an unknown even bit in the `features` field: 218 - MUST NOT attempt to route messages through the channel. 219 - if the `short_channel_id`'s output does NOT correspond to a P2WSH (using 220 `bitcoin_key_1` and `bitcoin_key_2`, as specified in 221 [BOLT #3](03-transactions.md#funding-transaction-output)) OR the output is 222 spent: 223 - MUST ignore the message. 224 - if the specified `chain_hash` is unknown to the receiver: 225 - MUST ignore the message. 226 - if the `short_channel_id`'s output does NOT have at least 6 confirmations: 227 - MAY accept the message if the output is close to 6 confirmations, in case 228 the receiving node hasn't received the latest block(s) yet. 229 - otherwise: 230 - SHOULD ignore the message. 231 - otherwise: 232 - if `bitcoin_signature_1`, `bitcoin_signature_2`, `node_signature_1` OR 233 `node_signature_2` are invalid OR NOT correct: 234 - SHOULD send a `warning`. 235 - MAY close the connection. 236 - MUST ignore the message. 237 - otherwise: 238 - if `node_id_1` OR `node_id_2` are blacklisted: 239 - SHOULD ignore the message. 240 - otherwise: 241 - if the transaction referred to was NOT previously announced as a 242 channel: 243 - SHOULD queue the message for rebroadcasting. 244 - MAY choose NOT to for messages longer than the minimum expected 245 length. 246 - if it has previously received a valid `channel_announcement`, for the 247 same transaction, in the same block, but for a different `node_id_1` or 248 `node_id_2`: 249 - SHOULD blacklist the previous message's `node_id_1` and `node_id_2`, 250 as well as this `node_id_1` and `node_id_2` AND forget any channels 251 connected to them. 252 - otherwise: 253 - SHOULD store this `channel_announcement`. 254 - once its funding output has been spent OR reorganized out: 255 - SHOULD forget a channel after a 72-block delay. 256 - SHOULD NOT rebroadcast this `channel_announcement` to its peers. 257 258 ### Rationale 259 260 Both nodes are required to sign to indicate they are willing to route other 261 payments via this channel (i.e. be part of the public network); requiring their 262 Bitcoin signatures proves that they control the channel. 263 264 The blacklisting of conflicting nodes disallows multiple different 265 announcements. Such conflicting announcements should never be broadcast by any 266 node, as this implies that keys have leaked. 267 268 While channels should not be advertised before they are sufficiently deep, the 269 requirement against rebroadcasting only applies if the transaction has not moved 270 to a different block. 271 272 In order to avoid storing excessively large messages, yet still allow for 273 reasonable future expansion, nodes are permitted to restrict rebroadcasting 274 (perhaps statistically). 275 276 New channel features are possible in the future: backwards compatible (or 277 optional) features will have _odd_ feature bits, while incompatible features 278 will have _even_ feature bits 279 (["It's OK to be odd!"](00-introduction.md#glossary-and-terminology-guide)). 280 281 A delay of 72 blocks is used when forgetting a channel after detecting that it 282 has been spent: this can allow a new `channel_announcement` to propagate to 283 indicate that this channel was spliced and not closed. Thanks to this delay, 284 payments can still be relayed on the channel while the splice transaction is 285 waiting for enough confirmations. 286 287 ## The `node_announcement` Message 288 289 This gossip message allows a node to indicate extra data associated with it, in 290 addition to its public key. To avoid trivial denial of service attacks, 291 nodes not associated with an already known channel are ignored. 292 293 1. type: 257 (`node_announcement`) 294 2. data: 295 * [`signature`:`signature`] 296 * [`u16`:`flen`] 297 * [`flen*byte`:`features`] 298 * [`u32`:`timestamp`] 299 * [`point`:`node_id`] 300 * [`3*byte`:`rgb_color`] 301 * [`32*byte`:`alias`] 302 * [`u16`:`addrlen`] 303 * [`addrlen*byte`:`addresses`] 304 305 `timestamp` allows for the ordering of messages, in the case of multiple 306 announcements. `rgb_color` and `alias` allow intelligence services to assign 307 nodes colors like black and cool monikers like 'IRATEMONK' and 'WISTFULTOLL'. 308 309 `addresses` allows a node to announce its willingness to accept incoming network 310 connections: it contains a series of `address descriptor`s for connecting to the 311 node. The first byte describes the address type and is followed by the 312 appropriate number of bytes for that type. 313 314 The following `address descriptor` types are defined: 315 316 * `1`: ipv4; data = `[4:ipv4_addr][2:port]` (length 6) 317 * `2`: ipv6; data = `[16:ipv6_addr][2:port]` (length 18) 318 * `3`: Deprecated (length 12). Used to contain Tor v2 onion services. 319 * `4`: Tor v3 onion service; data = `[35:onion_addr][2:port]` (length 37) 320 * version 3 ([prop224](https://gitweb.torproject.org/torspec.git/tree/proposals/224-rend-spec-ng.txt)) 321 onion service addresses; Encodes: 322 `[32:32_byte_ed25519_pubkey] || [2:checksum] || [1:version]`, where 323 `checksum = sha3(".onion checksum" || pubkey || version)[:2]`. 324 * `5`: DNS hostname; data = `[1:hostname_len][hostname_len:hostname][2:port]` (length up to 258) 325 * `hostname` bytes MUST be ASCII characters. 326 * Non-ASCII characters MUST be encoded using Punycode: 327 https://en.wikipedia.org/wiki/Punycode 328 329 ### Requirements 330 331 The origin node: 332 - MUST set `timestamp` to be greater than that of any previous 333 `node_announcement` it has previously created. 334 - MAY base it on a UNIX timestamp. 335 - MUST set `signature` to the signature of the double-SHA256 of the entire 336 remaining packet after `signature` (using the key given by `node_id`). 337 - MAY set `alias` AND `rgb_color` to customize its appearance in maps and 338 graphs. 339 - Note: the first byte of `rgb_color` is the red value, the second byte is the 340 green value, and the last byte is the blue value. 341 - MUST set `alias` to a valid UTF-8 string, with any `alias` trailing-bytes 342 equal to 0. 343 - SHOULD fill `addresses` with an address descriptor for each public network 344 address that expects incoming connections. 345 - MUST set `addrlen` to the number of bytes in `addresses`. 346 - MUST place address descriptors in ascending order. 347 - SHOULD NOT place any zero-typed address descriptors anywhere. 348 - SHOULD use placement only for aligning fields that follow `addresses`. 349 - MUST NOT create an address descriptor with `port` equal to 0. 350 - SHOULD ensure `ipv4_addr` AND `ipv6_addr` are routable addresses. 351 - MUST set `features` according to [BOLT #9](09-features.md#assigned-features-flags) 352 - SHOULD set `flen` to the minimum length required to hold the `features` 353 bits it sets. 354 - SHOULD not announce a Tor v2 onion service. 355 - MUST NOT announce more than one `type 5` DNS hostname. 356 357 The receiving node: 358 - if `node_id` is NOT a valid compressed public key: 359 - SHOULD send a `warning`. 360 - MAY close the connection. 361 - MUST NOT process the message further. 362 - if `signature` is NOT a valid signature (using `node_id` of the 363 double-SHA256 of the entire message following the `signature` field, including 364 any future fields appended to the end): 365 - SHOULD send a `warning`. 366 - MAY close the connection. 367 - MUST NOT process the message further. 368 - if `features` field contains _unknown even bits_: 369 - SHOULD NOT connect to the node. 370 - Unless paying a [BOLT #11](11-payment-encoding.md) invoice which does not 371 have the same bit(s) set, MUST NOT attempt to send payments _to_ the node. 372 - MUST NOT route a payment _through_ the node. 373 - SHOULD ignore the first `address descriptor` that does NOT match the types 374 defined above. 375 - if `addrlen` is insufficient to hold the address descriptors of the 376 known types: 377 - SHOULD send a `warning`. 378 - MAY close the connection. 379 - if `port` is equal to 0: 380 - SHOULD ignore that address descriptor. 381 - if `node_id` is NOT previously known from a `channel_announcement` message, 382 OR if `timestamp` is NOT greater than the last-received `node_announcement` 383 from this `node_id`: 384 - SHOULD ignore the message. 385 - otherwise: 386 - if `timestamp` is greater than the last-received `node_announcement` from 387 this `node_id`: 388 - SHOULD queue the message for rebroadcasting. 389 - MAY choose NOT to queue messages longer than the minimum expected length. 390 - MAY use `rgb_color` AND `alias` to reference nodes in interfaces. 391 - SHOULD insinuate their self-signed origins. 392 - SHOULD ignore Tor v2 onion services. 393 - if more than one `type 5` address is announced: 394 - SHOULD ignore the additional data. 395 - MUST not forward the `node_announcement`. 396 397 ### Rationale 398 399 New node features are possible in the future: backwards compatible (or 400 optional) ones will have _odd_ `feature` _bits_, incompatible ones will have 401 _even_ `feature` _bits_. These will be propagated normally; incompatible 402 feature bits here refer to the nodes, not the `node_announcement` message 403 itself. 404 405 New address types may be added in the future; as address descriptors have 406 to be ordered in ascending order, unknown ones can be safely ignored. 407 Additional fields beyond `addresses` may also be added in the future—with 408 optional padding within `addresses`, if they require certain alignment. 409 410 ### Security Considerations for Node Aliases 411 412 Node aliases are user-defined and provide a potential avenue for injection 413 attacks, both during the process of rendering and during persistence. 414 415 Node aliases should always be sanitized before being displayed in 416 HTML/Javascript contexts or any other dynamically interpreted rendering 417 frameworks. Similarly, consider using prepared statements, input validation, 418 and escaping to protect against injection vulnerabilities and persistence 419 engines that support SQL or other dynamically interpreted querying languages. 420 421 * [Stored and Reflected XSS Prevention](https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet) 422 * [DOM-based XSS Prevention](https://www.owasp.org/index.php/DOM_based_XSS_Prevention_Cheat_Sheet) 423 * [SQL Injection Prevention](https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet) 424 425 Don't be like the school of [Little Bobby Tables](https://xkcd.com/327/). 426 427 ## The `channel_update` Message 428 429 After a channel has been initially announced, each side independently 430 announces the fees and minimum expiry delta it requires to relay HTLCs 431 through this channel. Each uses the 8-byte channel shortid that matches the 432 `channel_announcement` and the 1-bit `channel_flags` field to indicate which end of the 433 channel it's on (origin or final). A node can do this multiple times, in 434 order to change fees. 435 436 Note that the `channel_update` gossip message is only useful in the context 437 of *relaying* payments, not *sending* payments. When making a payment 438 `A` -> `B` -> `C` -> `D`, only the `channel_update`s related to channels 439 `B` -> `C` (announced by `B`) and `C` -> `D` (announced by `C`) will 440 come into play. When building the route, amounts and expiries for HTLCs need 441 to be calculated backward from the destination to the source. The exact initial 442 value for `amount_msat` and the minimal value for `cltv_expiry`, to be used for 443 the last HTLC in the route, are provided in the payment request 444 (see [BOLT #11](11-payment-encoding.md#tagged-fields)). 445 446 1. type: 258 (`channel_update`) 447 2. data: 448 * [`signature`:`signature`] 449 * [`chain_hash`:`chain_hash`] 450 * [`short_channel_id`:`short_channel_id`] 451 * [`u32`:`timestamp`] 452 * [`byte`:`message_flags`] 453 * [`byte`:`channel_flags`] 454 * [`u16`:`cltv_expiry_delta`] 455 * [`u64`:`htlc_minimum_msat`] 456 * [`u32`:`fee_base_msat`] 457 * [`u32`:`fee_proportional_millionths`] 458 * [`u64`:`htlc_maximum_msat`] 459 460 The `channel_flags` bitfield is used to indicate the direction of the channel: it 461 identifies the node that this update originated from and signals various options 462 concerning the channel. The following table specifies the meaning of its 463 individual bits: 464 465 | Bit Position | Name | Meaning | 466 | ------------- | ----------- | -------------------------------- | 467 | 0 | `direction` | Direction this update refers to. | 468 | 1 | `disable` | Disable the channel. | 469 470 The `message_flags` bitfield is used to provide additional details about the message: 471 472 | Bit Position | Name | 473 | ------------- | ---------------| 474 | 0 | `must_be_one` | 475 | 1 | `dont_forward` | 476 477 The `node_id` for the signature verification is taken from the corresponding 478 `channel_announcement`: `node_id_1` if the least-significant bit of flags is 0 479 or `node_id_2` otherwise. 480 481 ### Requirements 482 483 The origin node: 484 - MUST NOT send a created `channel_update` before `channel_ready` has been received. 485 - MAY create a `channel_update` to communicate the channel parameters to the 486 channel peer, even though the channel has not yet been announced (i.e. 487 the `announce_channel` bit was not set or the `channel_update` is sent before 488 the peers exchanged [announcement signatures](#the-announcement_signatures-message)). 489 - MUST set the `short_channel_id` to either an `alias` it has 490 received from the peer, or the real channel `short_channel_id`. 491 - MUST set `dont_forward` to 1 in `message_flags` 492 - MUST NOT forward such a `channel_update` to other peers, for privacy 493 reasons. 494 - Note: such a `channel_update`, one not preceded by a 495 `channel_announcement`, is invalid to any other peer and would be discarded. 496 - MUST set `signature` to the signature of the double-SHA256 of the entire 497 remaining packet after `signature`, using its own `node_id`. 498 - MUST set `chain_hash` AND `short_channel_id` to match the 32-byte hash AND 499 8-byte channel ID that uniquely identifies the channel specified in the 500 `channel_announcement` message. 501 - if the origin node is `node_id_1` in the message: 502 - MUST set the `direction` bit of `channel_flags` to 0. 503 - otherwise: 504 - MUST set the `direction` bit of `channel_flags` to 1. 505 - MUST set `htlc_maximum_msat` to the maximum value it will send through this channel for a single HTLC. 506 - MUST set this to less than or equal to the channel capacity. 507 - MUST set this to less than or equal to `max_htlc_value_in_flight_msat` it received from the peer. 508 - MUST set this to greater than or equal to `htlc_minimum_msat`. 509 - MUST set `must_be_one` in `message_flags` to 1. 510 - MUST set bits in `channel_flags` and `message_flags` that are not assigned a meaning to 0. 511 - MAY create and send a `channel_update` with the `disable` bit set to 1, to 512 signal a channel's temporary unavailability (e.g. due to a loss of 513 connectivity) OR permanent unavailability (e.g. prior to an on-chain 514 settlement). 515 - MAY send a subsequent `channel_update` with the `disable` bit set to 0 to 516 re-enable the channel. 517 - MUST set `timestamp` to greater than 0, AND to greater than any 518 previously-sent `channel_update` for this `short_channel_id`. 519 - SHOULD base `timestamp` on a UNIX timestamp. 520 - MUST set `cltv_expiry_delta` to the number of blocks it will subtract from 521 an incoming HTLC's `cltv_expiry`. 522 - MUST set `htlc_minimum_msat` to the minimum HTLC value (in millisatoshi) 523 that the channel peer will accept. 524 - MUST set `htlc_minimum_msat` to less than or equal to `htlc_maximum_msat`. 525 - MUST set `fee_base_msat` to the base fee (in millisatoshi) it will charge 526 for any HTLC. 527 - MUST set `fee_proportional_millionths` to the amount (in millionths of a 528 satoshi) it will charge per transferred satoshi. 529 - SHOULD NOT create redundant `channel_update`s 530 - If it creates a new `channel_update` with updated channel parameters: 531 - SHOULD keep accepting the previous channel parameters for 10 minutes 532 533 The receiving node: 534 - if the `short_channel_id` does NOT match a previous `channel_announcement`: 535 - MUST ignore `channel_update`s that do NOT correspond to one of its own 536 channels. 537 - if the channel output has been spent: 538 - MUST ignore `channel_update`s, unless they have the `disable` bit set to 1. 539 - SHOULD NOT rebroadcast `channel_update`s to its peers, unless they have the 540 `disable` bit set to 1. 541 - SHOULD accept `channel_update`s for its own channels (even if non-public), 542 in order to learn the associated origin nodes' forwarding parameters. 543 - if `signature` is not a valid signature, using `node_id` of the 544 double-SHA256 of the entire message following the `signature` field (including 545 unknown fields following `fee_proportional_millionths`): 546 - SHOULD send a `warning` and close the connection. 547 - MUST NOT process the message further. 548 - if the specified `chain_hash` value is unknown (meaning it isn't active on 549 the specified chain): 550 - MUST ignore the channel update. 551 - if the `timestamp` is equal to the last-received `channel_update` for this 552 `short_channel_id` AND `node_id`: 553 - if the fields below `timestamp` differ: 554 - MAY blacklist this `node_id`. 555 - MAY forget all channels associated with it. 556 - if the fields below `timestamp` are equal: 557 - SHOULD ignore this message 558 - if `timestamp` is lower than that of the last-received 559 `channel_update` for this `short_channel_id` AND for `node_id`: 560 - SHOULD ignore the message. 561 - otherwise: 562 - if the `timestamp` is unreasonably far in the future: 563 - MAY discard the `channel_update`. 564 - otherwise: 565 - SHOULD queue the message for rebroadcasting. 566 - MAY choose NOT to for messages longer than the minimum expected length. 567 - if `htlc_maximum_msat` < `htlc_minimum_msat`: 568 - SHOULD ignore this channel during route considerations. 569 - if `htlc_maximum_msat` is greater than channel capacity: 570 - MAY blacklist this `node_id` 571 - SHOULD ignore this channel during route considerations. 572 - otherwise: 573 - SHOULD consider the `htlc_maximum_msat` when routing. 574 575 ### Rationale 576 577 The `timestamp` field is used by nodes for pruning `channel_update`s that are 578 either too far in the future or have not been updated in two weeks; so it 579 makes sense to have it be a UNIX timestamp (i.e. seconds since UTC 580 1970-01-01). This cannot be a hard requirement, however, given the possible case 581 of two `channel_update`s within a single second. 582 583 It is assumed that more than one `channel_update` message changing the channel 584 parameters in the same second may be a DoS attempt, and therefore, the node responsible 585 for signing such messages may be blacklisted. However, a node may send a same 586 `channel_update` message with a different signature (changing the nonce in signature 587 signing), and hence fields apart from signature are checked to see if the channel 588 parameters have changed for the same timestamp. It is also important to note that 589 ECDSA signatures are malleable. So, an intermediate node who received the `channel_update` 590 message can rebroadcast it just by changing the `s` component of signature with `-s`. 591 This should however not result in the blacklist of the `node_id` from where 592 the message originated. 593 594 The recommendation against redundant `channel_update`s minimizes spamming the network, 595 however it is sometimes inevitable. For example, a channel with a 596 peer which is unreachable will eventually cause a `channel_update` to 597 indicate that the channel is disabled, with another update re-enabling 598 the channel when the peer reestablishes contact. Because gossip 599 messages are batched and replace previous ones, the result may be a 600 single seemingly-redundant update. 601 602 When a node creates a new `channel_update` to change its channel parameters, 603 it will take some time to propagate through the network and payers may use 604 older parameters. It is recommended to keep accepting older parameters for 605 at least 10 minutes to improve payment latency and reliability. 606 607 The `must_be_one` field in `message_flags` was previously used to indicate 608 the presence of the `htlc_maximum_msat` field. This field must now always 609 be present, so `must_be_one` is a constant value, and ignored by receivers. 610 611 ## Query Messages 612 613 Understanding of messages used to be indicated with the `gossip_queries` 614 feature bit; now these messages are universally supported, that feature has 615 now been slightly repurposed. Not offering this feature means a node is not 616 worth querying for gossip: either they do not store the entire gossip map, or 617 they are only connected to a single peer (this one). 618 619 There are several messages which contain a long array of 620 `short_channel_id`s (called `encoded_short_ids`) so we include an encoding byte 621 which allows for different encoding schemes to be defined in the future, if they 622 provide benefit. 623 624 Encoding types: 625 * `0`: uncompressed array of `short_channel_id` types, in ascending order. 626 * `1`: Previously used for zlib compression, this encoding MUST NOT be used. 627 628 This encoding is also used for arrays of other types (timestamps, flags, ...), 629 and specified with an `encoded_` prefix. For example, `encoded_timestamps` is 630 an array of timestamps with a `0` prefix. 631 632 Query messages can be extended with optional fields that can help reduce the number of messages needed to synchronize routing tables by enabling: 633 634 - timestamp-based filtering of `channel_update` messages: only ask for `channel_update` messages that are newer than the ones you already have. 635 - checksum-based filtering of `channel_update` messages: only ask for `channel_update` messages that carry different information from the ones you already have. 636 637 Nodes can signal that they support extended gossip queries with the `gossip_queries_ex` feature bit. 638 639 ### The `query_short_channel_ids`/`reply_short_channel_ids_end` Messages 640 641 1. type: 261 (`query_short_channel_ids`) 642 2. data: 643 * [`chain_hash`:`chain_hash`] 644 * [`u16`:`len`] 645 * [`len*byte`:`encoded_short_ids`] 646 * [`query_short_channel_ids_tlvs`:`tlvs`] 647 648 1. `tlv_stream`: `query_short_channel_ids_tlvs` 649 2. types: 650 1. type: 1 (`query_flags`) 651 2. data: 652 * [`byte`:`encoding_type`] 653 * [`...*byte`:`encoded_query_flags`] 654 655 `encoded_query_flags` is an array of bitfields, one bigsize per bitfield, one bitfield for each `short_channel_id`. Bits have the following meaning: 656 657 | Bit Position | Meaning | 658 | ------------- | ---------------------------------------- | 659 | 0 | Sender wants `channel_announcement` | 660 | 1 | Sender wants `channel_update` for node 1 | 661 | 2 | Sender wants `channel_update` for node 2 | 662 | 3 | Sender wants `node_announcement` for node 1 | 663 | 4 | Sender wants `node_announcement` for node 2 | 664 665 Query flags must be minimally encoded, which means that one flag will be encoded with a single byte. 666 667 1. type: 262 (`reply_short_channel_ids_end`) 668 2. data: 669 * [`chain_hash`:`chain_hash`] 670 * [`byte`:`full_information`] 671 672 This is a general mechanism which lets a node query for the 673 `channel_announcement` and `channel_update` messages for specific channels 674 (identified via `short_channel_id`s). This is usually used either because 675 a node sees a `channel_update` for which it has no `channel_announcement` or 676 because it has obtained previously unknown `short_channel_id`s 677 from `reply_channel_range`. 678 679 #### Requirements 680 681 The sender: 682 - SHOULD NOT send this to a peer which does not offer `gossip_queries`. 683 - MUST NOT send `query_short_channel_ids` if it has sent a previous `query_short_channel_ids` to this peer and not received `reply_short_channel_ids_end`. 684 - MUST set `chain_hash` to the 32-byte hash that uniquely identifies the chain 685 that the `short_channel_id`s refer to. 686 - MUST set the first byte of `encoded_short_ids` to the encoding type. 687 - MUST encode a whole number of `short_channel_id`s to `encoded_short_ids` 688 - MAY send this if it receives a `channel_update` for a 689 `short_channel_id` for which it has no `channel_announcement`. 690 - SHOULD NOT send this if the channel referred to is not an unspent output. 691 - MAY include an optional `query_flags`. If so: 692 - MUST set `encoding_type`, as for `encoded_short_ids`. 693 - Each query flag is a minimally-encoded bigsize. 694 - MUST encode one query flag per `short_channel_id`. 695 696 The receiver: 697 - if the first byte of `encoded_short_ids` is not a known encoding type: 698 - MAY send a `warning`. 699 - MAY close the connection. 700 - if `encoded_short_ids` does not decode into a whole number of `short_channel_id`: 701 - MAY send a `warning`. 702 - MAY close the connection. 703 - if it has not sent `reply_short_channel_ids_end` to a previously received `query_short_channel_ids` from this sender: 704 - MAY send a `warning`. 705 - MAY close the connection. 706 - if the incoming message includes `query_short_channel_ids_tlvs`: 707 - if `encoding_type` is not a known encoding type: 708 - MAY send a `warning`. 709 - MAY close the connection. 710 - if `encoded_query_flags` does not decode to exactly one flag per `short_channel_id`: 711 - MAY send a `warning`. 712 - MAY close the connection. 713 - MUST respond to each known `short_channel_id`: 714 - if the incoming message does not include `encoded_query_flags`: 715 - with a `channel_announcement` and the latest `channel_update` for each end 716 - MUST follow with any `node_announcement`s for each `channel_announcement` 717 - otherwise: 718 - We define `query_flag` for the Nth `short_channel_id` in 719 `encoded_short_ids` to be the Nth bigsize of the decoded 720 `encoded_query_flags`. 721 - if bit 0 of `query_flag` is set: 722 - MUST reply with a `channel_announcement` 723 - if bit 1 of `query_flag` is set and it has received a `channel_update` from `node_id_1`: 724 - MUST reply with the latest `channel_update` for `node_id_1` 725 - if bit 2 of `query_flag` is set and it has received a `channel_update` from `node_id_2`: 726 - MUST reply with the latest `channel_update` for `node_id_2` 727 - if bit 3 of `query_flag` is set and it has received a `node_announcement` from `node_id_1`: 728 - MUST reply with the latest `node_announcement` for `node_id_1` 729 - if bit 4 of `query_flag` is set and it has received a `node_announcement` from `node_id_2`: 730 - MUST reply with the latest `node_announcement` for `node_id_2` 731 - SHOULD NOT wait for the next outgoing gossip flush to send these. 732 - SHOULD avoid sending duplicate `node_announcements` in response to a single `query_short_channel_ids`. 733 - MUST follow these responses with `reply_short_channel_ids_end`. 734 - if does not maintain up-to-date channel information for `chain_hash`: 735 - MUST set `full_information` to 0. 736 - otherwise: 737 - SHOULD set `full_information` to 1. 738 739 #### Rationale 740 741 Future nodes may not have complete information; they certainly won't have 742 complete information on unknown `chain_hash` chains. While this `full_information` 743 field (previously and confusingly called `complete`) cannot be trusted, a 0 does indicate that the sender should search 744 elsewhere for additional data. 745 746 The explicit `reply_short_channel_ids_end` message means that the receiver can 747 indicate it doesn't know anything, and the sender doesn't need to rely on 748 timeouts. It also causes a natural ratelimiting of queries. 749 750 ### The `query_channel_range` and `reply_channel_range` Messages 751 752 1. type: 263 (`query_channel_range`) 753 2. data: 754 * [`chain_hash`:`chain_hash`] 755 * [`u32`:`first_blocknum`] 756 * [`u32`:`number_of_blocks`] 757 * [`query_channel_range_tlvs`:`tlvs`] 758 759 1. `tlv_stream`: `query_channel_range_tlvs` 760 2. types: 761 1. type: 1 (`query_option`) 762 2. data: 763 * [`bigsize`:`query_option_flags`] 764 765 `query_option_flags` is a bitfield represented as a minimally-encoded bigsize. Bits have the following meaning: 766 767 | Bit Position | Meaning | 768 | ------------- | ----------------------- | 769 | 0 | Sender wants timestamps | 770 | 1 | Sender wants checksums | 771 772 Though it is possible, it would not be very useful to ask for checksums without asking for timestamps too: the receiving node may have an older `channel_update` with a different checksum, asking for it would be useless. And if a `channel_update` checksum is actually 0 (which is quite unlikely) it will not be queried. 773 774 1. type: 264 (`reply_channel_range`) 775 2. data: 776 * [`chain_hash`:`chain_hash`] 777 * [`u32`:`first_blocknum`] 778 * [`u32`:`number_of_blocks`] 779 * [`byte`:`sync_complete`] 780 * [`u16`:`len`] 781 * [`len*byte`:`encoded_short_ids`] 782 * [`reply_channel_range_tlvs`:`tlvs`] 783 784 1. `tlv_stream`: `reply_channel_range_tlvs` 785 2. types: 786 1. type: 1 (`timestamps_tlv`) 787 2. data: 788 * [`byte`:`encoding_type`] 789 * [`...*byte`:`encoded_timestamps`] 790 1. type: 3 (`checksums_tlv`) 791 2. data: 792 * [`...*channel_update_checksums`:`checksums`] 793 794 For a single `channel_update`, timestamps are encoded as: 795 796 1. subtype: `channel_update_timestamps` 797 2. data: 798 * [`u32`:`timestamp_node_id_1`] 799 * [`u32`:`timestamp_node_id_2`] 800 801 Where: 802 * `timestamp_node_id_1` is the timestamp of the `channel_update` for `node_id_1`, or 0 if there was no `channel_update` from that node. 803 * `timestamp_node_id_2` is the timestamp of the `channel_update` for `node_id_2`, or 0 if there was no `channel_update` from that node. 804 805 For a single `channel_update`, checksums are encoded as: 806 807 1. subtype: `channel_update_checksums` 808 2. data: 809 * [`u32`:`checksum_node_id_1`] 810 * [`u32`:`checksum_node_id_2`] 811 812 Where: 813 * `checksum_node_id_1` is the checksum of the `channel_update` for `node_id_1`, or 0 if there was no `channel_update` from that node. 814 * `checksum_node_id_2` is the checksum of the `channel_update` for `node_id_2`, or 0 if there was no `channel_update` from that node. 815 816 The checksum of a `channel_update` is the CRC32C checksum as specified in [RFC3720](https://tools.ietf.org/html/rfc3720#appendix-B.4) of this `channel_update` without its `signature` and `timestamp` fields. 817 818 This allows querying for channels within specific blocks. 819 820 #### Requirements 821 822 The sender of `query_channel_range`: 823 - SHOULD NOT send this to a peer which does not offer `gossip_queries`. 824 - MUST NOT send this if it has sent a previous `query_channel_range` to this peer and not received all `reply_channel_range` replies. 825 - MUST set `chain_hash` to the 32-byte hash that uniquely identifies the chain 826 that it wants the `reply_channel_range` to refer to 827 - MUST set `first_blocknum` to the first block it wants to know channels for 828 - MUST set `number_of_blocks` to 1 or greater. 829 - MAY append an additional `query_channel_range_tlv`, which specifies the type of extended information it would like to receive. 830 831 The receiver of `query_channel_range`: 832 - if it has not sent all `reply_channel_range` to a previously received `query_channel_range` from this sender: 833 - MAY send a `warning`. 834 - MAY close the connection. 835 - MUST respond with one or more `reply_channel_range`: 836 - MUST set with `chain_hash` equal to that of `query_channel_range`, 837 - MUST limit `number_of_blocks` to the maximum number of blocks whose 838 results could fit in `encoded_short_ids` 839 - MAY split block contents across multiple `reply_channel_range`. 840 - the first `reply_channel_range` message: 841 - MUST set `first_blocknum` less than or equal to the `first_blocknum` in `query_channel_range` 842 - MUST set `first_blocknum` plus `number_of_blocks` greater than `first_blocknum` in `query_channel_range`. 843 - successive `reply_channel_range` message: 844 - MUST have `first_blocknum` equal or greater than the previous `first_blocknum`. 845 - MUST set `sync_complete` to `false` if this is not the final `reply_channel_range`. 846 - the final `reply_channel_range` message: 847 - MUST have `first_blocknum` plus `number_of_blocks` equal or greater than the `query_channel_range` `first_blocknum` plus `number_of_blocks`. 848 - MUST set `sync_complete` to `true`. 849 850 If the incoming message includes `query_option`, the receiver MAY append additional information to its reply: 851 - if bit 0 in `query_option_flags` is set, the receiver MAY append a `timestamps_tlv` that contains `channel_update` timestamps for all `short_channel_id`s in `encoded_short_ids` 852 - if bit 1 in `query_option_flags` is set, the receiver MAY append a `checksums_tlv` that contains `channel_update` checksums for all `short_channel_id`s in `encoded_short_ids` 853 854 #### Rationale 855 856 A single response might be too large for a single packet, so multiple replies 857 may be required. We want to allow a peer to store canned results for (say) 858 1000-block ranges, so replies can exceed the requested range. However, we 859 require that each reply be relevant (overlapping the requested range). 860 861 By insisting that replies be in increasing order, the receiver can easily 862 determine if replies are done: simply check if `first_blocknum` plus 863 `number_of_blocks` equals or exceeds the `first_blocknum` plus 864 `number_of_blocks` it asked for. 865 866 The addition of timestamp and checksum fields allow a peer to omit querying for redundant updates. 867 868 ### The `gossip_timestamp_filter` Message 869 870 1. type: 265 (`gossip_timestamp_filter`) 871 2. data: 872 * [`chain_hash`:`chain_hash`] 873 * [`u32`:`first_timestamp`] 874 * [`u32`:`timestamp_range`] 875 876 This message allows a node to constrain future gossip messages to 877 a specific range. A node which wants any gossip messages has 878 to send this, otherwise no gossip messages would be received. 879 880 Note that this filter replaces any previous one, so it can be used 881 multiple times to change the gossip from a peer. 882 883 #### Requirements 884 885 The sender: 886 - MUST set `chain_hash` to the 32-byte hash that uniquely identifies the chain 887 that it wants the gossip to refer to. 888 - If the receiver does not offer `gossip_queries`: 889 - SHOULD set `first_timestamp` to 0xFFFFFFFF and `timestamp_range` to 0. 890 891 The receiver: 892 - SHOULD send all gossip messages whose `timestamp` is greater or 893 equal to `first_timestamp`, and less than `first_timestamp` plus 894 `timestamp_range`. 895 - MAY wait for the next outgoing gossip flush to send these. 896 - SHOULD send gossip messages as it generates them regardless of `timestamp`. 897 - Otherwise (relayed gossip): 898 - SHOULD restrict future gossip messages to those whose `timestamp` 899 is greater or equal to `first_timestamp`, and less than 900 `first_timestamp` plus `timestamp_range`. 901 - If a `channel_announcement` has no corresponding `channel_update`s: 902 - MUST NOT send the `channel_announcement`. 903 - If the funding output of the `channel_announcement` has been spent: 904 - SHOULD NOT send the `channel_announcement`. 905 - Otherwise: 906 - MUST consider the `timestamp` of the `channel_announcement` to be the `timestamp` of a corresponding `channel_update`. 907 - MUST consider whether to send the `channel_announcement` after receiving the first corresponding `channel_update`. 908 - If a `channel_announcement` is sent: 909 - MUST send the `channel_announcement` prior to any corresponding `channel_update`s and `node_announcement`s. 910 911 #### Rationale 912 913 Since `channel_announcement` doesn't have a timestamp, we generate a likely 914 one. If there's no `channel_update` then it is not sent at all, which is most 915 likely in the case of pruned channels. 916 917 Otherwise the `channel_announcement` is usually followed immediately by a 918 `channel_update`. Ideally we would specify that the first (oldest) `channel_update`'s 919 timestamp is to be used as the time of the `channel_announcement`, but new nodes on 920 the network will not have this, and further would require the first `channel_update` 921 timestamp to be stored. Instead, we allow any update to be used, which 922 is simple to implement. 923 924 In the case where the `channel_announcement` is nonetheless missed, 925 `query_short_channel_ids` can be used to retrieve it. 926 927 Nodes can use `timestamp_filter` to reduce their gossip load when they 928 have many peers (eg. setting `first_timestamp` to `0xFFFFFFFF` after the 929 first few peers, in the assumption that propagation is adequate). 930 This assumption of adequate propagation does not apply for gossip messages 931 generated directly by the node itself, so they should ignore filters. 932 933 ### Requirements 934 935 A node: 936 - MUST NOT relay any gossip messages it did not generate itself, unless explicitly requested. 937 938 ## Rebroadcasting 939 940 ### Requirements 941 942 A receiving node: 943 - upon receiving a new `channel_announcement` or a `channel_update` or 944 `node_announcement` with an updated `timestamp`: 945 - SHOULD update its local view of the network's topology accordingly. 946 - after applying the changes from the announcement: 947 - if there are no channels associated with the corresponding origin node: 948 - MAY purge the origin node from its set of known nodes. 949 - otherwise: 950 - SHOULD update the appropriate metadata AND store the signature 951 associated with the announcement. 952 - Note: this will later allow the node to rebuild the announcement 953 for its peers. 954 955 A node: 956 - MUST not send gossip it did not generate itself, until it receives `gossip_timestamp_filter`. 957 - SHOULD flush outgoing gossip messages once every 60 seconds, independently of 958 the arrival times of the messages. 959 - Note: this results in staggered announcements that are unique (not 960 duplicated). 961 - SHOULD NOT forward gossip messages to peers who sent `networks` in `init` 962 and did not specify the `chain_hash` of this gossip message. 963 - MAY re-announce its channels regularly. 964 - Note: this is discouraged, in order to keep the resource requirements low. 965 966 ### Rationale 967 968 Once the gossip message has been processed, it's added to a list of outgoing 969 messages, destined for the processing node's peers, replacing any older 970 updates from the origin node. This list of gossip messages will be flushed at 971 regular intervals; such a store-and-delayed-forward broadcast is called a 972 _staggered broadcast_. Also, such batching forms a natural rate 973 limit with low overhead. 974 975 ## HTLC Fees 976 977 ### Requirements 978 979 The origin node: 980 - SHOULD accept HTLCs that pay a fee equal to or greater than: 981 - fee_base_msat + ( amount_to_forward * fee_proportional_millionths / 1000000 ) 982 - SHOULD accept HTLCs that pay an older fee, for some reasonable time after 983 sending `channel_update`. 984 - Note: this allows for any propagation delay. 985 986 ## Pruning the Network View 987 988 ### Requirements 989 990 A node: 991 - SHOULD monitor the funding transactions in the blockchain, to identify 992 channels that are being closed. 993 - if the funding output of a channel is spent and received 72 block confirmations: 994 - SHOULD be removed from the local network view AND be considered closed. 995 - if the announced node no longer has any associated open channels: 996 - MAY prune nodes added through `node_announcement` messages from their 997 local view. 998 - Note: this is a direct result of the dependency of a `node_announcement` 999 being preceded by a `channel_announcement`. 1000 1001 ### Recommendation on Pruning Stale Entries 1002 1003 #### Requirements 1004 1005 A node: 1006 - if the `timestamp` of the latest `channel_update` in either direction is 1007 older than two weeks (1209600 seconds): 1008 - MAY prune the channel. 1009 - MAY ignore the channel. 1010 - Note: this is an individual node policy and MUST NOT be enforced by 1011 forwarding peers, e.g. by closing channels when receiving outdated gossip 1012 messages. 1013 1014 #### Rationale 1015 1016 Several scenarios may result in channels becoming unusable and its endpoints 1017 becoming unable to send updates for these channels. For example, this occurs if 1018 both endpoints lose access to their private keys and can neither sign 1019 `channel_update`s nor close the channel on-chain. In this case, the channels are 1020 unlikely to be part of a computed route, since they would be partitioned off 1021 from the rest of the network; however, they would remain in the local network 1022 view would be forwarded to other peers indefinitely. 1023 1024 The oldest `channel_update` is used to prune the channel since both sides need 1025 to be active in order for the channel to be usable. Doing so prunes channels 1026 even if one side continues to send fresh `channel_update`s but the other node 1027 has disappeared. 1028 1029 ## Recommendations for Routing 1030 1031 When calculating a route for an HTLC, both the `cltv_expiry_delta` and the fee 1032 need to be considered: the `cltv_expiry_delta` contributes to the time that 1033 funds will be unavailable in the event of a worst-case failure. The relationship 1034 between these two attributes is unclear, as it depends on the reliability of the 1035 nodes involved. 1036 1037 If a route is computed by simply routing to the intended recipient and summing 1038 the `cltv_expiry_delta`s, then it's possible for intermediate nodes to guess 1039 their position in the route. Knowing the CLTV of the HTLC, the surrounding 1040 network topology, and the `cltv_expiry_delta`s gives an attacker a way to guess 1041 the intended recipient. Therefore, it's highly desirable to add a random offset 1042 to the CLTV that the intended recipient will receive, which bumps all CLTVs 1043 along the route. 1044 1045 In order to create a plausible offset, the origin node MAY start a limited 1046 random walk on the graph, starting from the intended recipient and summing the 1047 `cltv_expiry_delta`s, and use the resulting sum as the offset. 1048 This effectively creates a _shadow route extension_ to the actual route and 1049 provides better protection against this attack vector than simply picking a 1050 random offset would. 1051 1052 Other more advanced considerations involve diversification of route selection, 1053 to avoid single points of failure and detection, and balancing of local 1054 channels. 1055 1056 ### Routing Example 1057 1058 Consider four nodes: 1059 1060 1061 ``` 1062 B 1063 / \ 1064 / \ 1065 A C 1066 \ / 1067 \ / 1068 D 1069 ``` 1070 1071 Each advertises the following `cltv_expiry_delta` on its end of every 1072 channel: 1073 1074 1. A: 10 blocks 1075 2. B: 20 blocks 1076 3. C: 30 blocks 1077 4. D: 40 blocks 1078 1079 C also uses a `min_final_cltv_expiry_delta` of 18 (the default) when requesting 1080 payments. 1081 1082 Also, each node has a set fee scheme that it uses for each of its 1083 channels: 1084 1085 1. A: 100 base + 1000 millionths 1086 2. B: 200 base + 2000 millionths 1087 3. C: 300 base + 3000 millionths 1088 4. D: 400 base + 4000 millionths 1089 1090 The network will see eight `channel_update` messages: 1091 1092 1. A->B: `cltv_expiry_delta` = 10, `fee_base_msat` = 100, `fee_proportional_millionths` = 1000 1093 1. A->D: `cltv_expiry_delta` = 10, `fee_base_msat` = 100, `fee_proportional_millionths` = 1000 1094 1. B->A: `cltv_expiry_delta` = 20, `fee_base_msat` = 200, `fee_proportional_millionths` = 2000 1095 1. D->A: `cltv_expiry_delta` = 40, `fee_base_msat` = 400, `fee_proportional_millionths` = 4000 1096 1. B->C: `cltv_expiry_delta` = 20, `fee_base_msat` = 200, `fee_proportional_millionths` = 2000 1097 1. D->C: `cltv_expiry_delta` = 40, `fee_base_msat` = 400, `fee_proportional_millionths` = 4000 1098 1. C->B: `cltv_expiry_delta` = 30, `fee_base_msat` = 300, `fee_proportional_millionths` = 3000 1099 1. C->D: `cltv_expiry_delta` = 30, `fee_base_msat` = 300, `fee_proportional_millionths` = 3000 1100 1101 **B->C.** If B were to send 4,999,999 millisatoshi directly to C, it would 1102 neither charge itself a fee nor add its own `cltv_expiry_delta`, so it would 1103 use C's requested `min_final_cltv_expiry_delta` of 18. Presumably it would also add a 1104 _shadow route_ to give an extra CLTV of 42. Additionally, it could add extra 1105 CLTV deltas at other hops, as these values represent a minimum, but chooses not 1106 to do so here, for the sake of simplicity: 1107 1108 * `amount_msat`: 4999999 1109 * `cltv_expiry`: current-block-height + 18 + 42 1110 * `onion_routing_packet`: 1111 * `amt_to_forward` = 4999999 1112 * `outgoing_cltv_value` = current-block-height + 18 + 42 1113 1114 **A->B->C.** If A were to send 4,999,999 millisatoshi to C via B, it needs to 1115 pay B the fee it specified in the B->C `channel_update`, calculated as 1116 per [HTLC Fees](#htlc-fees): 1117 1118 fee_base_msat + ( amount_to_forward * fee_proportional_millionths / 1000000 ) 1119 1120 200 + ( 4999999 * 2000 / 1000000 ) = 10199 1121 1122 Similarly, it would need to add B->C's `channel_update` `cltv_expiry_delta` (20), C's 1123 requested `min_final_cltv_expiry_delta` (18), and the cost for the _shadow route_ (42). 1124 Thus, A->B's `update_add_htlc` message would be: 1125 1126 * `amount_msat`: 5010198 1127 * `cltv_expiry`: current-block-height + 20 + 18 + 42 1128 * `onion_routing_packet`: 1129 * `amt_to_forward` = 4999999 1130 * `outgoing_cltv_value` = current-block-height + 18 + 42 1131 1132 B->C's `update_add_htlc` would be the same as B->C's direct payment above. 1133 1134 **A->D->C.** Finally, if for some reason A chose the more expensive route via D, 1135 A->D's `update_add_htlc` message would be: 1136 1137 * `amount_msat`: 5020398 1138 * `cltv_expiry`: current-block-height + 40 + 18 + 42 1139 * `onion_routing_packet`: 1140 * `amt_to_forward` = 4999999 1141 * `outgoing_cltv_value` = current-block-height + 18 + 42 1142 1143 And D->C's `update_add_htlc` would again be the same as B->C's direct payment 1144 above. 1145 1146 1147  1148 <br> 1149 This work is licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/).