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