02-peer-protocol.md (138832B)
1 # BOLT #2: Peer Protocol for Channel Management 2 3 The peer channel protocol has three phases: establishment, normal 4 operation, and closing. 5 6 # Table of Contents 7 8 * [Channel](#channel) 9 * [Definition of `channel_id`](#definition-of-channel_id) 10 * [Interactive Transaction Construction](#interactive-transaction-construction) 11 * [Set-Up and Vocabulary](#set-up-and-vocabulary) 12 * [Fee Responsibility](#fee-responsibility) 13 * [Overview](#overview) 14 * [The `tx_add_input` Message](#the-tx_add_input-message) 15 * [The `tx_add_output` Message](#the-tx_add_output-message) 16 * [The `tx_remove_input` and `tx_remove_output` Messages](#the-tx_remove_input-and-tx_remove_output-messages) 17 * [The `tx_complete` Message](#the-tx_complete-message) 18 * [The `tx_signatures` Message](#the-tx_signatures-message) 19 * [The `tx_init_rbf` Message](#the-tx_init_rbf-message) 20 * [The `tx_ack_rbf` Message](#the-tx_ack_rbf-message) 21 * [The `tx_abort` Message](#the-tx_abort-message) 22 * [Channel Establishment v1](#channel-establishment-v1) 23 * [The `open_channel` Message](#the-open_channel-message) 24 * [The `accept_channel` Message](#the-accept_channel-message) 25 * [The `funding_created` Message](#the-funding_created-message) 26 * [The `funding_signed` Message](#the-funding_signed-message) 27 * [The `channel_ready` Message](#the-channel_ready-message) 28 * [Channel Establishment v2](#channel-establishment-v2) 29 * [The `open_channel2` Message](#the-open_channel2-message) 30 * [The `accept_channel2` Message](#the-accept_channel2-message) 31 * [Funding Composition](#funding-composition) 32 * [The `commitment_signed` Message](#the-commitment_signed-message) 33 * [Sharing funding signatures: `tx_signatures`](#sharing-funding-signatures-tx_signatures) 34 * [Fee bumping: `tx_init_rbf` and `tx_ack_rbf`](#fee-bumping-tx_init_rbf-and-tx_ack_rbf) 35 * [Channel Quiescence](#channel-quiescence) 36 * [Channel Close](#channel-close) 37 * [Closing Initiation: `shutdown`](#closing-initiation-shutdown) 38 * [Closing Negotiation: `closing_complete` and `closing_sig`](#closing-negotiation-closing_complete-and-closing_sig) 39 * [Legacy Closing Negotiation: `closing_signed`](#legacy-closing-negotiation-closing_signed) 40 * [Normal Operation](#normal-operation) 41 * [Forwarding HTLCs](#forwarding-htlcs) 42 * [`cltv_expiry_delta` Selection](#cltv_expiry_delta-selection) 43 * [Adding an HTLC: `update_add_htlc`](#adding-an-htlc-update_add_htlc) 44 * [Removing an HTLC: `update_fulfill_htlc`, `update_fail_htlc`, and `update_fail_malformed_htlc`](#removing-an-htlc-update_fulfill_htlc-update_fail_htlc-and-update_fail_malformed_htlc) 45 * [Committing Updates So Far: `commitment_signed`](#committing-updates-so-far-commitment_signed) 46 * [Completing the Transition to the Updated State: `revoke_and_ack`](#completing-the-transition-to-the-updated-state-revoke_and_ack) 47 * [Updating Fees: `update_fee`](#updating-fees-update_fee) 48 * [Message Retransmission: `channel_reestablish` message](#message-retransmission) 49 * [Authors](#authors) 50 51 # Channel 52 53 ## Definition of `channel_id` 54 55 Some messages use a `channel_id` to identify the channel. It's 56 derived from the funding transaction by combining the `funding_txid` 57 and the `funding_output_index`, using big-endian exclusive-OR 58 (i.e. `funding_output_index` alters the last 2 bytes). 59 60 Prior to channel establishment, a `temporary_channel_id` is used, 61 which is a random nonce. 62 63 Note that as duplicate `temporary_channel_id`s may exist from different 64 peers, APIs which reference channels by their channel id before the funding 65 transaction is created are inherently unsafe. The only protocol-provided 66 identifier for a channel before funding_created has been exchanged is the 67 (source_node_id, destination_node_id, temporary_channel_id) tuple. Note that 68 any such APIs which reference channels by their channel id before the funding 69 transaction is confirmed are also not persistent - until you know the script 70 pubkey corresponding to the funding output nothing prevents duplicative channel 71 ids. 72 73 ### `channel_id`, v2 74 75 For channels established using the v2 protocol, the `channel_id` is the 76 `SHA256(lesser-revocation-basepoint || greater-revocation-basepoint)`, 77 where the lesser and greater is based off the order of the basepoint. 78 79 When sending `open_channel2`, the peer's revocation basepoint is unknown. 80 A `temporary_channel_id` must be computed by using a zeroed out basepoint 81 for the non-initiator. 82 83 When sending `accept_channel2`, the `temporary_channel_id` from `open_channel2` 84 must be used, to allow the initiator to match the response to its request. 85 86 #### Rationale 87 88 The revocation basepoints must be remembered by both peers for correct 89 operation anyway. They're known after the first exchange of messages, 90 obviating the need for a `temporary_channel_id` in subsequent messages. 91 By mixing information from both sides, they avoid `channel_id` collisions, 92 and they remove the dependency on the funding txid. 93 94 ## Interactive Transaction Construction 95 96 Interactive transaction construction allows two peers to collaboratively 97 build a transaction for broadcast. This protocol is the foundation 98 for dual-funded channels establishment (v2). 99 100 ### Set-Up and Vocabulary 101 102 There are two parties to a transaction construction: an *initiator* 103 and a *non-initiator*. 104 The *initiator* is the peer which initiates the protocol, e.g. 105 for channel establishment v2 the *initiator* would be the peer which 106 sends `open_channel2`. 107 108 The protocol makes the following assumptions: 109 110 - The `feerate` for the transaction is known. 111 - The `dust_limit` for the transaction is known. 112 - The `nLocktime` for the transaction is known. 113 - The `nVersion` for the transaction is known. 114 115 ### Fee Responsibility 116 117 The *initiator* is responsible for paying the fees for the following fields, 118 to be referred to as the `common fields`. 119 120 - version 121 - segwit marker + flag 122 - input count 123 - output count 124 - locktime 125 126 The rest of the transaction bytes' fees are the responsibility of 127 the peer who contributed that input or output via `tx_add_input` or 128 `tx_add_output`, at the agreed upon `feerate`. 129 130 ### Overview 131 132 The *initiator* initiates the interactive transaction construction 133 protocol with `tx_add_input`. The *non-initiator* responds with any 134 of `tx_add_input`, `tx_add_output`, `tx_remove_input`, `tx_remove_output`, or 135 `tx_complete`. The protocol continues with the synchronous exchange 136 of interactive transaction protocol messages until both nodes have sent 137 and received a consecutive `tx_complete`. This is a turn-based protocol. 138 139 Once peers have exchanged consecutive `tx_complete`s, the 140 interactive transaction construction protocol is considered concluded. 141 Both peers should construct the transaction and fail the negotiation 142 if an error is discovered. 143 144 This protocol is expressly designed to allow for parallel, multi-party 145 sessions to collectively construct a single transaction. This preserves 146 the ability to open multiple channels in a single transaction. While 147 `serial_id`s are generally chosen randomly, to maintain consistent transaction 148 ordering across all peer sessions, it is simplest to reuse received 149 `serial_id`s when forwarding them to other peers, inverting the bottom bit as 150 necessary to satisfy the parity requirement. 151 152 Here are a few example exchanges. 153 154 #### *initiator* only 155 156 A, the *initiator*, has two inputs and an output (the funding output). 157 B, the *non-initiator* has nothing to contribute. 158 159 +-------+ +-------+ 160 | |--(1)- tx_add_input -->| | 161 | |<-(2)- tx_complete ----| | 162 | |--(3)- tx_add_input -->| | 163 | A |<-(4)- tx_complete ----| B | 164 | |--(5)- tx_add_output ->| | 165 | |<-(6)- tx_complete ----| | 166 | |--(7)- tx_complete --->| | 167 +-------+ +-------+ 168 169 #### *initiator* and *non-initiator* 170 171 A, the *initiator*, contributes 2 inputs and an output that they 172 then remove. B, the *non-initiator*, contributes 1 input and an output, 173 but waits until A adds a second input before contributing. 174 175 Note that if A does not send a second input, the negotiation will end without 176 B's contributions. 177 178 +-------+ +-------+ 179 | |--(1)- tx_add_input ---->| | 180 | |<-(2)- tx_complete ------| | 181 | |--(3)- tx_add_output --->| | 182 | |<-(4)- tx_complete ------| | 183 | |--(5)- tx_add_input ---->| | 184 | A |<-(6)- tx_add_input -----| B | 185 | |--(7)- tx_remove_output >| | 186 | |<-(8)- tx_add_output ----| | 187 | |--(9)- tx_complete ----->| | 188 | |<-(10) tx_complete ------| | 189 +-------+ +-------+ 190 191 ### The `tx_add_input` Message 192 193 This message contains a transaction input. 194 195 1. type: 66 (`tx_add_input`) 196 2. data: 197 * [`channel_id`:`channel_id`] 198 * [`u64`:`serial_id`] 199 * [`u16`:`prevtx_len`] 200 * [`prevtx_len*byte`:`prevtx`] 201 * [`u32`:`prevtx_vout`] 202 * [`u32`:`sequence`] 203 204 #### Requirements 205 206 The sending node: 207 - MUST add all sent inputs to the transaction 208 - MUST use a unique `serial_id` for each input currently added to the 209 transaction 210 - MUST set `sequence` to be less than or equal to 4294967293 (`0xFFFFFFFD`) 211 - MUST NOT re-transmit inputs it has received from the peer 212 - if is the *initiator*: 213 - MUST send even `serial_id`s 214 - if is the *non-initiator*: 215 - MUST send odd `serial_id`s 216 217 The receiving node: 218 - MUST add all received inputs to the transaction 219 - MUST fail the negotiation if: 220 - `sequence` is set to `0xFFFFFFFE` or `0xFFFFFFFF` 221 - the `prevtx` and `prevtx_vout` are identical to a previously added 222 (and not removed) input's 223 - `prevtx` is not a valid transaction 224 - `prevtx_vout` is greater or equal to the number of outputs on `prevtx` 225 - the `scriptPubKey` of the `prevtx_vout` output of `prevtx` is not exactly a 1-byte push opcode (for the numeric values `0` to `16`) followed by a data push between 2 and 40 bytes 226 - the `serial_id` is already included in the transaction 227 - the `serial_id` has the wrong parity 228 - if has received 4096 `tx_add_input` messages during this negotiation 229 230 #### Rationale 231 232 Each node must know the set of the transaction inputs. The *non-initiator* 233 MAY omit this message. 234 235 `serial_id` is a randomly chosen number which uniquely identifies this input. 236 Inputs in the constructed transaction MUST be sorted by `serial_id`. 237 238 `prevtx` is the serialized transaction that contains the output 239 this input spends. Used to verify that the input is non-malleable. 240 241 `prevtx_vout` is the index of the output being spent. 242 243 `sequence` is the sequence number of this input: it must signal 244 replaceability, and the same value should be used across implementations 245 to avoid on-chain fingerprinting. 246 247 #### Liquidity griefing 248 249 When sending `tx_add_input`, senders have no guarantee that the remote node 250 will complete the protocol in a timely manner. Malicious remote nodes could 251 delay messages or stop responding, which can result in a partially created 252 transaction that cannot be broadcast by the honest node. If the honest node 253 is locking the corresponding UTXO exclusively for this remote node, this can 254 be exploited to lock up the honest node's liquidity. 255 256 It is thus recommended that implementations keep UTXOs unlocked and actively 257 reuse them in concurrent sessions, which guarantees that transactions created 258 with honest nodes double-spend pending transactions with malicious nodes at 259 no additional cost for the honest node. 260 261 Unfortunately, this will also create conflicts between concurrent sessions 262 with honest nodes. This is a reasonable trade-off though because: 263 264 * on-chain funding attempts are relatively infrequent operations 265 * honest nodes should complete the protocol quickly, reducing the risk of 266 conflicts 267 * failed attempts can simply be retried at no cost 268 269 ### The `tx_add_output` Message 270 271 This message adds a transaction output. 272 273 1. type: 67 (`tx_add_output`) 274 2. data: 275 * [`channel_id`:`channel_id`] 276 * [`u64`:`serial_id`] 277 * [`u64`:`sats`] 278 * [`u16`:`scriptlen`] 279 * [`scriptlen*byte`:`script`] 280 281 #### Requirements 282 283 Either node: 284 - MAY omit this message 285 286 The sending node: 287 - MUST add all sent outputs to the transaction 288 - if is the *initiator*: 289 - MUST send even `serial_id`s 290 - if is the *non-initiator*: 291 - MUST send odd `serial_id`s 292 293 The receiving node: 294 - MUST add all received outputs to the transaction 295 - MUST accept P2WSH, P2WPKH, P2TR `script`s 296 - MAY fail the negotiation if `script` is non-standard 297 - MUST fail the negotiation if: 298 - the `serial_id` is already included in the transaction 299 - the `serial_id` has the wrong parity 300 - it has received 4096 `tx_add_output` messages during this negotiation 301 - the `sats` amount is less than the `dust_limit` 302 - the `sats` amount is greater than 2,100,000,000,000,000 (`MAX_MONEY`) 303 304 #### Rationale 305 306 Each node must know the set of the transaction outputs. 307 308 `serial_id` is a randomly chosen number which uniquely identifies this output. 309 Outputs in the constructed transaction MUST be sorted by `serial_id`. 310 311 `sats` is the satoshi value of the output. 312 313 `script` is the scriptPubKey for the output (with its length omitted). 314 `script`s are not required to follow standardness rules: non-standard 315 scripts such as `OP_RETURN` may be accepted, but the corresponding 316 transaction may fail to relay across the network. 317 318 ### The `tx_remove_input` and `tx_remove_output` Messages 319 320 This message removes an input from the transaction. 321 322 1. type: 68 (`tx_remove_input`) 323 2. data: 324 * [`channel_id`:`channel_id`] 325 * [`u64`:`serial_id`] 326 327 This message removes an output from the transaction. 328 329 1. type: 69 (`tx_remove_output`) 330 2. data: 331 * [`channel_id`:`channel_id`] 332 * [`u64`:`serial_id`] 333 334 #### Requirements 335 336 The sending node: 337 - MUST NOT send a `tx_remove` with a `serial_id` it did not add 338 to the transaction or has already been removed 339 340 The receiving node: 341 - MUST remove the indicated input or output from the transaction 342 - MUST fail the negotiation if: 343 - the input or output identified by the `serial_id` was not added by the 344 sender 345 - the `serial_id` does not correspond to a currently added input (or output) 346 347 ### The `tx_complete` Message 348 349 This message signals the conclusion of a peer's transaction 350 contributions. 351 352 1. type: 70 (`tx_complete`) 353 2. data: 354 * [`channel_id`:`channel_id`] 355 356 #### Requirements 357 358 The nodes: 359 - MUST send this message in succession to conclude this protocol 360 361 The receiving node: 362 - MUST use the negotiated inputs and outputs to construct a transaction 363 - MUST fail the negotiation if: 364 - the peer's total input satoshis is less than their outputs. One MUST 365 account for the peer's portion of the funding output when verifying 366 compliance with this requirement. 367 - the peer's paid feerate does not meet or exceed the agreed `feerate` 368 (based on the `minimum fee`). 369 - if is the *non-initiator*: 370 - the *initiator*'s fees do not cover the `common` fields 371 - there are more than 252 inputs 372 - there are more than 252 outputs 373 - the estimated weight of the tx is greater than 400,000 (`MAX_STANDARD_TX_WEIGHT`) 374 375 #### Rationale 376 377 To signal the conclusion of exchange of transaction inputs and outputs. 378 379 Upon successful exchange of `tx_complete` messages, both nodes 380 should construct the transaction and proceed to the next portion of the 381 protocol. For channel establishment v2, exchanging commitment transactions. 382 383 For the `minimum fee` calculation see [BOLT #3](03-transactions.md#calculating-fees-for-collaborative-transaction-construction). 384 385 The maximum inputs and outputs are capped at 252. This effectively fixes 386 the byte size of the input and output counts on the transaction to one (1). 387 388 ### The `tx_signatures` Message 389 390 1. type: 71 (`tx_signatures`) 391 2. data: 392 * [`channel_id`:`channel_id`] 393 * [`sha256`:`txid`] 394 * [`u16`:`num_witnesses`] 395 * [`num_witnesses*witness`:`witnesses`] 396 397 1. subtype: `witness` 398 2. data: 399 * [`u16`:`len`] 400 * [`len*byte`:`witness_data`] 401 402 #### Requirements 403 404 The sending node: 405 - if it has the lowest total satoshis contributed, as defined by total 406 `tx_add_input` values, or both peers have contributed equal amounts 407 but it has the lowest `node_id` (sorted lexicographically): 408 - MUST transmit their `tx_signatures` first 409 - MUST order the `witnesses` by the `serial_id` of the input they 410 correspond to 411 - `num_witnesses`s MUST equal the number of inputs they added 412 - MUST use the `SIGHASH_ALL` (0x01) flag on each signature 413 414 The receiving node: 415 - MUST fail the negotiation if: 416 - the message contains an empty `witness` 417 - the number of `witnesses` does not equal the number of inputs 418 added by the sending node 419 - the `txid` does not match the txid of the transaction 420 - the `witnesses` are non-standard 421 - a signature uses a flag that is not `SIGHASH_ALL` (0x01) 422 - SHOULD apply the `witnesses` to the transaction and broadcast it 423 - MUST reply with their `tx_signatures` if not already transmitted 424 425 #### Rationale 426 427 A strict ordering is used to decide which peer sends `tx_signatures` first. 428 This prevents deadlocks where each peer is waiting for the other peer to 429 send `tx_signatures`, and enables multiparty tx collaboration. 430 431 The `witness_data` is encoded as per bitcoin's wire protocol (a CompactSize number 432 of elements, with each element a CompactSize length and that many bytes following). 433 434 While the `minimum fee` is calculated and verified at `tx_complete` conclusion, 435 it is possible for the fee for the exchanged witness data to be underpaid. 436 It is the responsibility of the sending peer to correctly account for the 437 required fee. 438 439 ### The `tx_init_rbf` Message 440 441 This message initiates a replacement of the transaction after it's been 442 completed. 443 444 1. type: 72 (`tx_init_rbf`) 445 2. data: 446 * [`channel_id`:`channel_id`] 447 * [`u32`:`locktime`] 448 * [`u32`:`feerate`] 449 * [`tx_init_rbf_tlvs`:`tlvs`] 450 451 1. `tlv_stream`: `tx_init_rbf_tlvs` 452 2. types: 453 1. type: 0 (`funding_output_contribution`) 454 2. data: 455 * [`s64`:`satoshis`] 456 1. type: 2 (`require_confirmed_inputs`) 457 458 #### Requirements 459 460 The sender: 461 - MUST set `feerate` greater than or equal to 25/24 times the `feerate` 462 of the previously constructed transaction, rounded down. 463 - If it contributes to the transaction's funding output: 464 - MUST set `funding_output_contribution` 465 - If it requires the receiving node to only use confirmed inputs: 466 - MUST set `require_confirmed_inputs` 467 468 The recipient: 469 - MUST respond either with `tx_abort` or with `tx_ack_rbf` 470 - MUST respond with `tx_abort` if: 471 - the `feerate` is not greater than or equal to 25/24 times `feerate` 472 of the last successfully constructed transaction 473 - MAY send `tx_abort` for any reason 474 - MUST fail the negotiation if: 475 - `require_confirmed_inputs` is set but it cannot provide confirmed inputs 476 477 #### Rationale 478 479 `feerate` is the feerate this transaction will pay. It must be at least 480 1/24 greater than the last used `feerate`, rounded down to the nearest 481 satoshi to ensure there is progress. 482 483 E.g. if the last `feerate` was 520, the next sent `feerate` must be 541 484 (520 * 25 / 24 = 541.667, rounded down to 541). 485 486 If the previous transaction confirms in the middle of an RBF attempt, 487 the attempt MUST be abandoned. 488 489 `funding_output_contribution` is the amount of satoshis that this peer 490 will contribute to the funding output of the transaction, when there is 491 such an output. Note that it may be different from the contribution 492 made in the previously completed transaction. If omitted, the sender is 493 not contributing to the funding output. 494 495 ### The `tx_ack_rbf` Message 496 497 1. type: 73 (`tx_ack_rbf`) 498 2. data: 499 * [`channel_id`:`channel_id`] 500 * [`tx_ack_rbf_tlvs`:`tlvs`] 501 502 503 1. `tlv_stream`: `tx_ack_rbf_tlvs` 504 2. types: 505 1. type: 0 (`funding_output_contribution`) 506 2. data: 507 * [`s64`:`satoshis`] 508 1. type: 2 (`require_confirmed_inputs`) 509 510 #### Requirements 511 512 The sender: 513 - If it contributes to the transaction's funding output: 514 - MUST set `funding_output_contribution` 515 - If it requires the receiving node to only use confirmed inputs: 516 - MUST set `require_confirmed_inputs` 517 518 The recipient: 519 - MUST respond with `tx_abort` or with a `tx_add_input` message, 520 restarting the interactive tx collaboration protocol. 521 - MUST fail the negotiation if: 522 - `require_confirmed_inputs` is set but it cannot provide confirmed inputs 523 524 #### Rationale 525 526 `funding_output_contribution` is the amount of satoshis that this peer 527 will contribute to the funding output of the transaction, when there is 528 such an output. Note that it may be different from the contribution 529 made in the previously completed transaction. If omitted, the sender is 530 not contributing to the funding output. 531 532 It's recommended that a peer, rather than fail the RBF negotiation due to 533 a large feerate change, instead stop contributing to the funding output, 534 and decline to participate further in the transaction (by not contributing, 535 they may obtain incoming liquidity at no cost). 536 537 ### The `tx_abort` Message 538 539 1. type: 74 (`tx_abort`) 540 2. data: 541 * [`channel_id`:`channel_id`] 542 * [`u16`:`len`] 543 * [`len*byte`:`data`] 544 545 #### Requirements 546 547 A sending node: 548 - MUST NOT have already transmitted `tx_signatures` 549 - SHOULD forget the current negotiation and reset their state. 550 - MAY send an empty `data` field. 551 - when failure was caused by an invalid signature check: 552 - SHOULD include the raw, hex-encoded transaction in reply to a 553 `tx_signatures` or `commitment_signed` message. 554 555 A receiving node: 556 - if they have already sent `tx_signatures` to the peer: 557 - MUST NOT forget the channel until any inputs to the negotiated tx 558 have been spent. 559 - if they have not sent `tx_signatures`: 560 - SHOULD forget the current negotiation and reset their state. 561 - if they have not sent `tx_abort`: 562 - MUST echo back `tx_abort` 563 - if `data` is not composed solely of printable ASCII characters (For 564 reference: the printable character set includes byte values 32 through 565 126, inclusive): 566 - SHOULD NOT print out `data` verbatim. 567 568 #### Rationale 569 570 A receiving node, if they've already sent their `tx_signatures` has no guarantee 571 that the transaction won't be signed and published by their peer. They must remember 572 the transaction and channel (if appropriate) until the transaction is no longer 573 eligible to be spent (i.e. any input has been spent in a different transaction). 574 575 The `tx_abort` message allows for the cancellation of an in progress negotiation, 576 and a return to the initial starting state. It is distinct from the `error` 577 message, which triggers a channel close. 578 579 Echoing back `tx_abort` allows the peer to ack that they've seen the abort message, 580 permitting the originating peer to terminate the in-flight process without 581 worrying about stale messages. 582 583 ## Channel Establishment v1 584 585 After authenticating and initializing a connection ([BOLT #8](08-transport.md) 586 and [BOLT #1](01-messaging.md#the-init-message), respectively), channel establishment may begin. 587 588 There are two pathways for establishing a channel, a legacy version presented here, 589 and a second version ([below](#channel-establishment-v2)). Which channel 590 establishment protocols are available for use is negotiated in the `init` message. 591 592 This consists of the funding node (funder) sending an `open_channel` message, 593 followed by the responding node (fundee) sending `accept_channel`. With the 594 channel parameters locked in, the funder is able to create the funding 595 transaction and both versions of the commitment transaction, as described in 596 [BOLT #3](03-transactions.md#bolt-3-bitcoin-transaction-and-script-formats). 597 The funder then sends the outpoint of the funding output with the `funding_created` 598 message, along with the signature for the fundee's version of the commitment 599 transaction. Once the fundee learns the funding outpoint, it's able to 600 generate the signature for the funder's version of the commitment transaction and send it 601 over using the `funding_signed` message. 602 603 Once the channel funder receives the `funding_signed` message, it 604 must broadcast the funding transaction to the Bitcoin network. After 605 the `funding_signed` message is sent/received, both sides should wait 606 for the funding transaction to enter the blockchain and reach the 607 specified depth (number of confirmations). After both sides have sent 608 the `channel_ready` message, the channel is established and can begin 609 normal operation. The `channel_ready` message includes information 610 that will be used to construct channel authentication proofs. 611 612 613 +-------+ +-------+ 614 | |--(1)--- open_channel ----->| | 615 | |<-(2)-- accept_channel -----| | 616 | | | | 617 | A |--(3)-- funding_created --->| B | 618 | |<-(4)-- funding_signed -----| | 619 | | | | 620 | |--(5)--- channel_ready ---->| | 621 | |<-(6)--- channel_ready -----| | 622 +-------+ +-------+ 623 624 - where node A is 'funder' and node B is 'fundee' 625 626 If this fails at any stage, or if one node decides the channel terms 627 offered by the other node are not suitable, the channel establishment 628 fails. 629 630 Note that multiple channels can operate in parallel, as all channel 631 messages are identified by either a `temporary_channel_id` (before the 632 funding transaction is created) or a `channel_id` (derived from the 633 funding transaction). 634 635 ### The `open_channel` Message 636 637 This message contains information about a node and indicates its 638 desire to set up a new channel. This is the first step toward creating 639 the funding transaction and both versions of the commitment transaction. 640 641 1. type: 32 (`open_channel`) 642 2. data: 643 * [`chain_hash`:`chain_hash`] 644 * [`32*byte`:`temporary_channel_id`] 645 * [`u64`:`funding_satoshis`] 646 * [`u64`:`push_msat`] 647 * [`u64`:`dust_limit_satoshis`] 648 * [`u64`:`max_htlc_value_in_flight_msat`] 649 * [`u64`:`channel_reserve_satoshis`] 650 * [`u64`:`htlc_minimum_msat`] 651 * [`u32`:`feerate_per_kw`] 652 * [`u16`:`to_self_delay`] 653 * [`u16`:`max_accepted_htlcs`] 654 * [`point`:`funding_pubkey`] 655 * [`point`:`revocation_basepoint`] 656 * [`point`:`payment_basepoint`] 657 * [`point`:`delayed_payment_basepoint`] 658 * [`point`:`htlc_basepoint`] 659 * [`point`:`first_per_commitment_point`] 660 * [`byte`:`channel_flags`] 661 * [`open_channel_tlvs`:`tlvs`] 662 663 1. `tlv_stream`: `open_channel_tlvs` 664 2. types: 665 1. type: 0 (`upfront_shutdown_script`) 666 2. data: 667 * [`...*byte`:`shutdown_scriptpubkey`] 668 1. type: 1 (`channel_type`) 669 2. data: 670 * [`...*byte`:`type`] 671 672 The `chain_hash` value denotes the exact blockchain that the opened channel will 673 reside within. This is usually the genesis hash of the respective blockchain. 674 The existence of the `chain_hash` allows nodes to open channels 675 across many distinct blockchains as well as have channels within multiple 676 blockchains opened to the same peer (if it supports the target chains). 677 678 The `temporary_channel_id` is used to identify this channel on a per-peer basis until the 679 funding transaction is established, at which point it is replaced 680 by the `channel_id`, which is derived from the funding transaction. 681 682 `funding_satoshis` is the amount the sender is putting into the 683 channel. `push_msat` is an amount of initial funds that the sender is 684 unconditionally giving to the receiver. `dust_limit_satoshis` is the 685 threshold below which outputs should not be generated for this node's 686 commitment or HTLC transactions (i.e. HTLCs below this amount plus 687 HTLC transaction fees are not enforceable on-chain). This reflects the 688 reality that tiny outputs are not considered standard transactions and 689 will not propagate through the Bitcoin network. `channel_reserve_satoshis` 690 is the minimum amount that the other node is to keep as a direct 691 payment. `htlc_minimum_msat` indicates the smallest value HTLC this 692 node will accept. 693 694 `max_htlc_value_in_flight_msat` is a cap on total value of outstanding 695 HTLCs offered by the remote node, which allows the local node to limit its 696 exposure to HTLCs; similarly, `max_accepted_htlcs` limits the number of 697 outstanding HTLCs the remote node can offer. 698 699 `feerate_per_kw` indicates the initial fee rate in satoshi per 1000-weight 700 (i.e. 1/4 the more normally-used 'satoshi per 1000 vbytes') that this 701 side will pay for commitment and HTLC transactions, as described in 702 [BOLT #3](03-transactions.md#fee-calculation) (this can be adjusted 703 later with an `update_fee` message). 704 705 `to_self_delay` is the number of blocks that the other node's to-self 706 outputs must be delayed, using `OP_CHECKSEQUENCEVERIFY` delays; this 707 is how long it will have to wait in case of breakdown before redeeming 708 its own funds. 709 710 `funding_pubkey` is the public key in the 2-of-2 multisig script of 711 the funding transaction output. 712 713 The various `_basepoint` fields are used to derive unique 714 keys as described in [BOLT #3](03-transactions.md#key-derivation) for each commitment 715 transaction. Varying these keys ensures that the transaction ID of 716 each commitment transaction is unpredictable to an external observer, 717 even if one commitment transaction is seen; this property is very 718 useful for preserving privacy when outsourcing penalty transactions to 719 third parties. 720 721 `first_per_commitment_point` is the per-commitment point to be used 722 for the first commitment transaction, 723 724 Only the least-significant bit of `channel_flags` is currently 725 defined: `announce_channel`. This indicates whether the initiator of 726 the funding flow wishes to advertise this channel publicly to the 727 network, as detailed within [BOLT #7](07-routing-gossip.md#bolt-7-p2p-node-and-channel-discovery). 728 729 The `shutdown_scriptpubkey` allows the sending node to commit to where 730 funds will go on mutual close, which the remote node should enforce 731 even if a node is compromised later. 732 733 The `option_support_large_channel` is a feature used to let everyone 734 know this node will accept `funding_satoshis` greater than or equal to 2^24. 735 Since it's broadcast in the `node_announcement` message other nodes can use it to identify peers 736 willing to accept large channel even before exchanging the `init` message with them. 737 738 #### Defined Channel Types 739 740 Channel types are an explicit enumeration: for convenience of future 741 definitions they reuse even feature bits, but they are not an 742 arbitrary combination (they represent the persistent features which 743 affect the channel operation). 744 745 The currently defined basic types are: 746 - `option_static_remotekey` (bit 12) 747 - `option_anchors` and `option_static_remotekey` (bits 22 and 12) 748 749 Each basic type has the following variations allowed: 750 - `option_scid_alias` (bit 46) 751 - `option_zeroconf` (bit 50) 752 753 #### Requirements 754 755 The sending node: 756 - MUST ensure the `chain_hash` value identifies the chain it wishes to open the channel within. 757 - MUST ensure `temporary_channel_id` is unique from any other channel ID with the same peer. 758 - if both nodes advertised `option_support_large_channel`: 759 - MAY set `funding_satoshis` greater than or equal to 2^24 satoshi. 760 - otherwise: 761 - MUST set `funding_satoshis` to less than 2^24 satoshi. 762 - MUST set `push_msat` to equal or less than 1000 * `funding_satoshis`. 763 - MUST set `funding_pubkey`, `revocation_basepoint`, `htlc_basepoint`, `payment_basepoint`, and `delayed_payment_basepoint` to valid secp256k1 pubkeys in compressed format. 764 - MUST set `first_per_commitment_point` to the per-commitment point to be used for the initial commitment transaction, derived as specified in [BOLT #3](03-transactions.md#per-commitment-secret-requirements). 765 - MUST set `channel_reserve_satoshis` greater than or equal to `dust_limit_satoshis`. 766 - MUST set undefined bits in `channel_flags` to 0. 767 - if both nodes advertised the `option_upfront_shutdown_script` feature: 768 - MUST include `upfront_shutdown_script` with either a valid `shutdown_scriptpubkey` as required by `shutdown` `scriptpubkey`, or a zero-length `shutdown_scriptpubkey` (ie. `0x0000`). 769 - otherwise: 770 - MAY include `upfront_shutdown_script`. 771 - if it includes `open_channel_tlvs`: 772 - MUST include `upfront_shutdown_script`. 773 - MUST set `channel_type`: 774 - MUST set it to a defined type representing the type it wants. 775 - MUST use the smallest bitmap possible to represent the channel type. 776 - SHOULD NOT set it to a type containing a feature which was not negotiated. 777 - if `announce_channel` is `true` (not `0`): 778 - MUST NOT send `channel_type` with the `option_scid_alias` bit set. 779 780 The sending node SHOULD: 781 - set `to_self_delay` sufficient to ensure the sender can irreversibly spend a commitment transaction output, in case of misbehavior by the receiver. 782 - set `feerate_per_kw` to at least the rate it estimates would cause the transaction to be immediately included in a block. 783 - set `dust_limit_satoshis` to a sufficient value to allow commitment transactions to propagate through the Bitcoin network. 784 - set `htlc_minimum_msat` to the minimum value HTLC it's willing to accept from this peer. 785 786 The receiving node MUST: 787 - ignore undefined bits in `channel_flags`. 788 - if the message doesn't include a `channel_type`: 789 - fail the channel. 790 - if the connection has been re-established after receiving a previous 791 `open_channel`, BUT before receiving a `funding_created` message: 792 - accept a new `open_channel` message. 793 - discard the previous `open_channel` message. 794 - if `option_dual_fund` has been negotiated: 795 - fail the channel. 796 797 The receiving node MAY fail the channel if: 798 - `announce_channel` is `false` (`0`), yet it wishes to publicly announce the channel. 799 - `funding_satoshis` is too small. 800 - it considers `htlc_minimum_msat` too large. 801 - it considers `max_htlc_value_in_flight_msat` too small. 802 - it considers `channel_reserve_satoshis` too large. 803 - it considers `max_accepted_htlcs` too small. 804 - it considers `dust_limit_satoshis` too large. 805 806 The receiving node MUST fail the channel if: 807 - the `chain_hash` value is set to a hash of a chain that is unknown to the receiver. 808 - `push_msat` is greater than `funding_satoshis` * 1000. 809 - `to_self_delay` is unreasonably large. 810 - `max_accepted_htlcs` is greater than 483. 811 - it considers `feerate_per_kw` too small for timely processing or unreasonably large. 812 - `funding_pubkey`, `revocation_basepoint`, `htlc_basepoint`, `payment_basepoint`, or `delayed_payment_basepoint` 813 are not valid secp256k1 pubkeys in compressed format. 814 - `dust_limit_satoshis` is greater than `channel_reserve_satoshis`. 815 - `dust_limit_satoshis` is smaller than `354 satoshis` (see [BOLT 3](03-transactions.md#dust-limits)). 816 - the funder's amount for the initial commitment transaction is not sufficient for full [fee payment](03-transactions.md#fee-payment). 817 - both `to_local` and `to_remote` amounts for the initial commitment transaction are less than or equal to `channel_reserve_satoshis` (see [BOLT 3](03-transactions.md#commitment-transaction-outputs)). 818 - `funding_satoshis` is greater than or equal to 2^24 and the receiver does not support `option_support_large_channel`. 819 - the `channel_type` is not suitable. 820 - the `channel_type` includes `option_zeroconf` and it does not trust the sender to open an unconfirmed channel. 821 822 The receiving node MUST NOT: 823 - consider funds received, using `push_msat`, to be received until the funding transaction has reached sufficient depth. 824 825 #### Rationale 826 827 The requirement for `funding_satoshis` to be less than 2^24 satoshi was a temporary self-imposed limit while implementations were not yet considered stable, it can be lifted by advertising `option_support_large_channel`. 828 829 The *channel reserve* is specified by the peer's `channel_reserve_satoshis`: 1% of the channel total is suggested. Each side of a channel maintains this reserve so it always has something to lose if it were to try to broadcast an old, revoked commitment transaction. Initially, this reserve may not be met, as only one side has funds; but the protocol ensures that there is always progress toward meeting this reserve, and once met, it is maintained. 830 831 The sender can unconditionally give initial funds to the receiver using a non-zero `push_msat`, but even in this case we ensure that the funder has sufficient remaining funds to pay fees and that one side has some amount it can spend (which also implies there is at least one non-dust output). Note that, like any other on-chain transaction, this payment is not certain until the funding transaction has been confirmed sufficiently (with a danger of double-spend until this occurs) and may require a separate method to prove payment via on-chain confirmation. 832 833 The `feerate_per_kw` is generally only of concern to the sender (who pays the fees), but there is also the fee rate paid by HTLC transactions; thus, unreasonably large fee rates can also penalize the recipient. 834 835 Separating the `htlc_basepoint` from the `payment_basepoint` improves security: a node needs the secret associated with the `htlc_basepoint` to produce HTLC signatures for the protocol, but the secret for the `payment_basepoint` can be in cold storage. 836 837 The requirement that `channel_reserve_satoshis` is not considered dust 838 according to `dust_limit_satoshis` eliminates cases where all outputs 839 would be eliminated as dust. The similar requirements in 840 `accept_channel` ensure that both sides' `channel_reserve_satoshis` 841 are above both `dust_limit_satoshis`. 842 843 The receiver should not accept large `dust_limit_satoshis`, as this could be 844 used in griefing attacks, where the peer publishes its commitment with a lot 845 of dust htlcs, which effectively become miner fees. 846 847 Details for how to handle a channel failure can be found in [BOLT 5:Failing a Channel](05-onchain.md#failing-a-channel). 848 849 ### The `accept_channel` Message 850 851 This message contains information about a node and indicates its 852 acceptance of the new channel. This is the second step toward creating the 853 funding transaction and both versions of the commitment transaction. 854 855 1. type: 33 (`accept_channel`) 856 2. data: 857 * [`32*byte`:`temporary_channel_id`] 858 * [`u64`:`dust_limit_satoshis`] 859 * [`u64`:`max_htlc_value_in_flight_msat`] 860 * [`u64`:`channel_reserve_satoshis`] 861 * [`u64`:`htlc_minimum_msat`] 862 * [`u32`:`minimum_depth`] 863 * [`u16`:`to_self_delay`] 864 * [`u16`:`max_accepted_htlcs`] 865 * [`point`:`funding_pubkey`] 866 * [`point`:`revocation_basepoint`] 867 * [`point`:`payment_basepoint`] 868 * [`point`:`delayed_payment_basepoint`] 869 * [`point`:`htlc_basepoint`] 870 * [`point`:`first_per_commitment_point`] 871 * [`accept_channel_tlvs`:`tlvs`] 872 873 1. `tlv_stream`: `accept_channel_tlvs` 874 2. types: 875 1. type: 0 (`upfront_shutdown_script`) 876 2. data: 877 * [`...*byte`:`shutdown_scriptpubkey`] 878 1. type: 1 (`channel_type`) 879 2. data: 880 * [`...*byte`:`type`] 881 882 #### Requirements 883 884 The `temporary_channel_id` MUST be the same as the `temporary_channel_id` in 885 the `open_channel` message. 886 887 The sender: 888 - if `channel_type` includes `option_zeroconf`: 889 - MUST set `minimum_depth` to zero. 890 - otherwise: 891 - SHOULD set `minimum_depth` to a number of blocks it considers reasonable to avoid double-spending of the funding transaction. 892 - MUST set `channel_reserve_satoshis` greater than or equal to `dust_limit_satoshis` from the `open_channel` message. 893 - MUST set `dust_limit_satoshis` less than or equal to `channel_reserve_satoshis` from the `open_channel` message. 894 - MUST set `channel_type` to the `channel_type` from `open_channel`. 895 896 The receiver: 897 - if `minimum_depth` is unreasonably large: 898 - MAY fail the channel. 899 - if `channel_reserve_satoshis` is less than `dust_limit_satoshis` within the `open_channel` message: 900 - MUST fail the channel. 901 - if `channel_reserve_satoshis` from the `open_channel` message is less than `dust_limit_satoshis`: 902 - MUST fail the channel. 903 - if the message doesn't include a `channel_type`: 904 - MUST fail the channel. 905 - if `channel_type` does not match the `channel_type` from `open_channel`: 906 - MUST fail the channel. 907 908 Other fields have the same requirements as their counterparts in `open_channel`. 909 910 ### The `funding_created` Message 911 912 This message describes the outpoint which the funder has created for 913 the initial commitment transactions. After receiving the peer's 914 signature, via `funding_signed`, it will broadcast the funding transaction. 915 916 1. type: 34 (`funding_created`) 917 2. data: 918 * [`32*byte`:`temporary_channel_id`] 919 * [`sha256`:`funding_txid`] 920 * [`u16`:`funding_output_index`] 921 * [`signature`:`signature`] 922 923 #### Requirements 924 925 The sender MUST set: 926 - `temporary_channel_id` the same as the `temporary_channel_id` in the `open_channel` message. 927 - `funding_txid` to the transaction ID of a non-malleable transaction, 928 - and MUST NOT broadcast this transaction. 929 - `funding_output_index` to the output number of that transaction that corresponds the funding transaction output, as defined in [BOLT #3](03-transactions.md#funding-transaction-output). 930 - `signature` to the valid signature using its `funding_pubkey` for the initial commitment transaction, as defined in [BOLT #3](03-transactions.md#commitment-transaction). 931 932 The sender: 933 - when creating the funding transaction: 934 - SHOULD use only BIP141 (Segregated Witness) inputs. 935 - SHOULD ensure the funding transaction confirms in the next 2016 blocks. 936 937 The recipient: 938 - if `signature` is incorrect OR non-compliant with LOW-S-standard rule<sup>[LOWS](https://github.com/bitcoin/bitcoin/pull/6769)</sup>: 939 - MUST send a `warning` and close the connection, or send an 940 `error` and fail the channel. 941 942 #### Rationale 943 944 The `funding_output_index` can only be 2 bytes, since that's how it's packed into the `channel_id` and used throughout the gossip protocol. The limit of 65535 outputs should not be overly burdensome. 945 946 A transaction with all Segregated Witness inputs is not malleable, hence the funding transaction recommendation. 947 948 The funder may use CPFP on a change output to ensure that the funding transaction confirms before 2016 blocks, 949 otherwise the fundee may forget that channel. 950 951 ### The `funding_signed` Message 952 953 This message gives the funder the signature it needs for the first 954 commitment transaction, so it can broadcast the transaction knowing that funds 955 can be redeemed, if need be. 956 957 This message introduces the `channel_id` to identify the channel. It's derived from the funding transaction by combining the `funding_txid` and the `funding_output_index`, using big-endian exclusive-OR (i.e. `funding_output_index` alters the last 2 bytes). 958 959 1. type: 35 (`funding_signed`) 960 2. data: 961 * [`channel_id`:`channel_id`] 962 * [`signature`:`signature`] 963 964 #### Requirements 965 966 Both peers: 967 - MUST use the negotiated `channel_type` for all commitment transactions. 968 969 The sender MUST set: 970 - `channel_id` by exclusive-OR of the `funding_txid` and the `funding_output_index` from the `funding_created` message. 971 - `signature` to the valid signature, using its `funding_pubkey` for the initial commitment transaction, as defined in [BOLT #3](03-transactions.md#commitment-transaction). 972 973 The recipient: 974 - if `signature` is incorrect OR non-compliant with LOW-S-standard rule<sup>[LOWS](https://github.com/bitcoin/bitcoin/pull/6769)</sup>: 975 - MUST send a `warning` and close the connection, or send an 976 `error` and fail the channel. 977 - MUST NOT broadcast the funding transaction before receipt of a valid `funding_signed`. 978 - on receipt of a valid `funding_signed`: 979 - SHOULD broadcast the funding transaction. 980 981 #### Rationale 982 983 We generate the commitment transaction at this point, using the `channel_type` 984 that was communicated in the `open_channel` and `accept_channel` messages. 985 This `channel_type` determines the channel commitment format for the total 986 lifetime of the channel. 987 988 ### The `channel_ready` Message 989 990 This message (which used to be called `funding_locked`) indicates that the funding transaction has sufficient confirms for channel use. Once both nodes have sent this, the channel enters normal operating mode. 991 992 Note that the opener is free to send this message at any time (since it presumably trusts itself), but the 993 accepter would usually wait until the funding has reached the `minimum_depth` asked for in `accept_channel`. 994 995 1. type: 36 (`channel_ready`) 996 2. data: 997 * [`channel_id`:`channel_id`] 998 * [`point`:`second_per_commitment_point`] 999 * [`channel_ready_tlvs`:`tlvs`] 1000 1001 1. `tlv_stream`: `channel_ready_tlvs` 1002 2. types: 1003 1. type: 1 (`short_channel_id`) 1004 2. data: 1005 * [`short_channel_id`:`alias`] 1006 1007 #### Requirements 1008 1009 The sender: 1010 - MUST NOT send `channel_ready` unless outpoint of given by `funding_txid` and 1011 `funding_output_index` in the `funding_created` message pays exactly `funding_satoshis` to the scriptpubkey specified in [BOLT #3](03-transactions.md#funding-transaction-output). 1012 - if it is not the node opening the channel: 1013 - SHOULD wait until the funding transaction has reached `minimum_depth` before 1014 sending this message. 1015 - MUST set `second_per_commitment_point` to the per-commitment point to be used 1016 for commitment transaction #1, derived as specified in 1017 [BOLT #3](03-transactions.md#per-commitment-secret-requirements). 1018 - if `option_scid_alias` was negotiated: 1019 - MUST set `short_channel_id` `alias`. 1020 - otherwise: 1021 - MAY set `short_channel_id` `alias`. 1022 - if it sets `alias`: 1023 - if the `announce_channel` bit was set in `open_channel`: 1024 - SHOULD initially set `alias` to value not related to the real `short_channel_id`. 1025 - otherwise: 1026 - MUST set `alias` to a value not related to the real `short_channel_id`. 1027 - MUST NOT send the same `alias` for multiple peers or use an alias which 1028 collides with a `short_channel_id` of a channel on the same node. 1029 - MUST always recognize the `alias` as a `short_channel_id` for incoming HTLCs to this channel. 1030 - if `channel_type` has `option_scid_alias` set: 1031 - MUST NOT allow incoming HTLCs to this channel using the real `short_channel_id` 1032 - MAY send multiple `channel_ready` messages to the same peer with different `alias` values. 1033 - otherwise: 1034 - MUST wait until the funding transaction has reached `minimum_depth` before sending this message. 1035 1036 1037 The sender: 1038 1039 A non-funding node (fundee): 1040 - SHOULD forget the channel if it does not see the correct funding 1041 transaction after a timeout of 2016 blocks. 1042 1043 The receiver: 1044 - MAY use any of the `alias` it received, in BOLT 11 `r` fields. 1045 - if `channel_type` has `option_scid_alias` set: 1046 - MUST NOT use the real `short_channel_id` in BOLT 11 `r` fields. 1047 1048 From the point of waiting for `channel_ready` onward, either node MAY 1049 send an `error` and fail the channel if it does not receive a required response from the 1050 other node after a reasonable timeout. 1051 1052 #### Rationale 1053 1054 The non-funder can simply forget the channel ever existed, since no 1055 funds are at risk. If the fundee were to remember the channel forever, this 1056 would create a Denial of Service risk; therefore, forgetting it is recommended 1057 (even if the promise of `push_msat` is significant). 1058 1059 If the fundee forgets the channel before it was confirmed, the funder will need 1060 to broadcast the commitment transaction to get his funds back and open a new 1061 channel. To avoid this, the funder should ensure the funding transaction 1062 confirms in the next 2016 blocks. 1063 1064 The `alias` here is required for two distinct use cases. The first one is 1065 for routing payments through channels that are not confirmed yet (since 1066 the real `short_channel_id` is unknown until confirmation). The second one 1067 is to provide one or more aliases to use for private channels (even once 1068 a real `short_channel_id` is available). 1069 1070 While a node can send multiple `alias`, it must remember all of the 1071 ones it has sent so it can use them should they be requested by 1072 incoming HTLCs. The recipient only need remember one, for use in 1073 `r` route hints in BOLT 11 invoices. 1074 1075 If an RBF negotiation is in progress when a `channel_ready` message is 1076 exchanged, the negotiation must be abandoned. 1077 1078 ## Channel Establishment v2 1079 1080 This is a revision of the channel establishment protocol. 1081 It changes the previous protocol to allow the `accept_channel2` peer 1082 (the *accepter*/*non-initiator*) to contribute inputs to the funding 1083 transaction, via the interactive transaction construction protocol. 1084 1085 +-------+ +-------+ 1086 | |--(1)--- open_channel2 ----->| | 1087 | |<-(2)--- accept_channel2 -----| | 1088 | | | | 1089 --->| | <tx collaboration> | | 1090 | | | | | 1091 | | |--(3)-- commitment_signed -->| | 1092 | | |<-(4)-- commitment_signed ---| | 1093 | | A | | B | 1094 | | |<-(5)-- tx_signatures -------| | 1095 | | |--(6)-- tx_signatures ------>| | 1096 | | | | | 1097 | | |--(a)--- tx_init_rbf -------->| | 1098 ----| |<-(b)--- tx_ack_rbf ----------| | 1099 | | | | 1100 | | <tx rbf collaboration> | | 1101 | | | | 1102 | |--(c)-- commitment_signed -->| | 1103 | |<-(d)-- commitment_signed ---| | 1104 | | | | 1105 | |<-(e)-- tx_signatures -------| | 1106 | |--(f)-- tx_signatures ------>| | 1107 | | | | 1108 | |--(7)--- channel_ready ----->| | 1109 | |<-(8)--- channel_ready ------| | 1110 +-------+ +-------+ 1111 1112 - where node A is *opener*/*initiator* and node B is 1113 *accepter*/*non-initiator* 1114 1115 ### The `open_channel2` Message 1116 1117 This message initiates the v2 channel establishment workflow. 1118 1119 1. type: 64 (`open_channel2`) 1120 2. data: 1121 * [`chain_hash`:`chain_hash`] 1122 * [`channel_id`:`temporary_channel_id`] 1123 * [`u32`:`funding_feerate_perkw`] 1124 * [`u32`:`commitment_feerate_perkw`] 1125 * [`u64`:`funding_satoshis`] 1126 * [`u64`:`dust_limit_satoshis`] 1127 * [`u64`:`max_htlc_value_in_flight_msat`] 1128 * [`u64`:`htlc_minimum_msat`] 1129 * [`u16`:`to_self_delay`] 1130 * [`u16`:`max_accepted_htlcs`] 1131 * [`u32`:`locktime`] 1132 * [`point`:`funding_pubkey`] 1133 * [`point`:`revocation_basepoint`] 1134 * [`point`:`payment_basepoint`] 1135 * [`point`:`delayed_payment_basepoint`] 1136 * [`point`:`htlc_basepoint`] 1137 * [`point`:`first_per_commitment_point`] 1138 * [`point`:`second_per_commitment_point`] 1139 * [`byte`:`channel_flags`] 1140 * [`opening_tlvs`:`tlvs`] 1141 1142 1. `tlv_stream`: `opening_tlvs` 1143 2. types: 1144 1. type: 0 (`upfront_shutdown_script`) 1145 2. data: 1146 * [`...*byte`:`shutdown_scriptpubkey`] 1147 1. type: 1 (`channel_type`) 1148 2. data: 1149 * [`...*byte`:`type`] 1150 1. type: 2 (`require_confirmed_inputs`) 1151 1152 Rationale and Requirements are the same as for [`open_channel`](#the-open_channel-message), 1153 with the following additions. 1154 1155 #### Requirements 1156 1157 If nodes have negotiated `option_dual_fund`: 1158 - the opening node: 1159 - MUST NOT send `open_channel` 1160 1161 The sending node: 1162 - MUST set `channel_type` 1163 - MUST set `funding_feerate_perkw` to the feerate for this transaction 1164 - If it requires the receiving node to only use confirmed inputs: 1165 - MUST set `require_confirmed_inputs` 1166 1167 The receiving node: 1168 - MAY fail the negotiation if: 1169 - the `locktime` is unacceptable 1170 - the `funding_feerate_perkw` is unacceptable 1171 - MUST fail the negotiation if: 1172 - `require_confirmed_inputs` is set but it cannot provide confirmed inputs 1173 - `channel_type` is not set 1174 1175 #### Rationale 1176 1177 `temporary_channel_id` MUST be derived using a zeroed out basepoint for the 1178 peer's revocation basepoint. This allows the peer to return channel-assignable 1179 errors before the *accepter*'s revocation basepoint is known. 1180 1181 `funding_feerate_perkw` indicates the fee rate that the opening node will 1182 pay for the funding transaction in satoshi per 1000-weight, as described 1183 in [BOLT-3, Appendix F](03-transactions.md#appendix-f-dual-funded-transaction-test-vectors). 1184 1185 `locktime` is the locktime for the funding transaction. 1186 1187 The receiving node, if the `locktime` or `funding_feerate_perkw` is considered 1188 out of an acceptable range, may fail the negotiation. However, it is 1189 recommended that the *accepter* permits the channel open to proceed 1190 without their participation in the channel's funding. 1191 1192 Note that `open_channel`'s `channel_reserve_satoshi` has been omitted. 1193 Instead, the channel reserve is fixed at 1% of the total channel balance 1194 (`open_channel2`.`funding_satoshis` + `accept_channel2`.`funding_satoshis`) 1195 rounded down to the nearest whole satoshi or the `dust_limit_satoshis`, 1196 whichever is greater. 1197 1198 Note that `push_msat` has been omitted. 1199 1200 `second_per_commitment_point` is now sent here (as well as in `channel_ready`) 1201 as a convenience for implementations. 1202 1203 The sending node may require the other participant to only use confirmed inputs. 1204 This ensures that the sending node doesn't end up paying the fees of a low 1205 feerate unconfirmed ancestor of one of the other participant's inputs. 1206 1207 ### The `accept_channel2` Message 1208 1209 This message contains information about a node and indicates its 1210 acceptance of the new channel. 1211 1212 1. type: 65 (`accept_channel2`) 1213 2. data: 1214 * [`channel_id`:`temporary_channel_id`] 1215 * [`u64`:`funding_satoshis`] 1216 * [`u64`:`dust_limit_satoshis`] 1217 * [`u64`:`max_htlc_value_in_flight_msat`] 1218 * [`u64`:`htlc_minimum_msat`] 1219 * [`u32`:`minimum_depth`] 1220 * [`u16`:`to_self_delay`] 1221 * [`u16`:`max_accepted_htlcs`] 1222 * [`point`:`funding_pubkey`] 1223 * [`point`:`revocation_basepoint`] 1224 * [`point`:`payment_basepoint`] 1225 * [`point`:`delayed_payment_basepoint`] 1226 * [`point`:`htlc_basepoint`] 1227 * [`point`:`first_per_commitment_point`] 1228 * [`point`:`second_per_commitment_point`] 1229 * [`accept_tlvs`:`tlvs`] 1230 1231 1. `tlv_stream`: `accept_tlvs` 1232 2. types: 1233 1. type: 0 (`upfront_shutdown_script`) 1234 2. data: 1235 * [`...*byte`:`shutdown_scriptpubkey`] 1236 1. type: 1 (`channel_type`) 1237 2. data: 1238 * [`...*byte`:`type`] 1239 1. type: 2 (`require_confirmed_inputs`) 1240 1241 Rationale and Requirements are the same as listed above, 1242 for [`accept_channel`](#the-accept_channel-message) with the following 1243 additions. 1244 1245 #### Requirements 1246 1247 The accepting node: 1248 - MUST use the `temporary_channel_id` of the `open_channel2` message. 1249 - MUST set `channel_type` to the `channel_type` from `open_channel2`. 1250 - MAY respond with a `funding_satoshis` value of zero. 1251 - If it requires the opening node to only use confirmed inputs: 1252 - MUST set `require_confirmed_inputs`. 1253 1254 The receiving node: 1255 - MUST fail the negotiation if: 1256 - `require_confirmed_inputs` is set but it cannot provide confirmed inputs. 1257 - `channel_type` is not set. 1258 1259 #### Rationale 1260 1261 The `funding_satoshis` is the amount of bitcoin in satoshis 1262 the *accepter* will be contributing to the channel's funding transaction. 1263 1264 Note that `accept_channel`'s `channel_reserve_satoshi` has been omitted. 1265 Instead, the channel reserve is fixed at 1% of the total channel balance 1266 (`open_channel2`.`funding_satoshis` + `accept_channel2`.`funding_satoshis`) 1267 rounded down to the nearest whole satoshi or the `dust_limit_satoshis`, 1268 whichever is greater. 1269 1270 ### Funding Composition 1271 1272 Funding composition for channel establishment v2 makes use of the 1273 [Interactive Transaction Construction](#interactive-transaction-construction) 1274 protocol, with the following additional caveats. 1275 1276 #### The `tx_add_input` Message 1277 1278 ##### Requirements 1279 1280 The sending node: 1281 - if the receiver set `require_confirmed_inputs` in `open_channel2`, 1282 `accept_channel2`, `tx_init_rbf` or `tx_ack_rbf`: 1283 - MUST NOT send a `tx_add_input` that contains an unconfirmed input 1284 1285 #### The `tx_add_output` Message 1286 1287 ##### Requirements 1288 1289 The sending node: 1290 - if is the *opener*: 1291 - MUST send at least one `tx_add_output`, which contains the 1292 channel's funding output 1293 1294 ##### Rationale 1295 1296 The channel funding output must be added by the *opener*, who pays its fees. 1297 1298 #### The `tx_complete` Message 1299 1300 Upon receipt of consecutive `tx_complete`s, the receiving node: 1301 - if is the *accepter*: 1302 - MUST fail the negotiation if: 1303 - no funding output was received 1304 - the value of the funding output is not equal to the sum of 1305 `open_channel2`.`funding_satoshis` and `accept_channel2`. 1306 `funding_satoshis` 1307 - the value of the funding output is less than the `dust_limit` 1308 - if this is an RBF attempt: 1309 - MUST fail the negotiation if: 1310 - the transaction's total fees is less than the last 1311 successfully negotiated transaction's fees 1312 - the transaction does not share at least one input with 1313 each previous funding transaction 1314 - if it has sent `require_confirmed_inputs` in `open_channel2`, 1315 `accept_channel2`, `tx_init_rbf` or `tx_ack_rbf`: 1316 - MUST fail the negotiation if: 1317 - one of the inputs added by the other peer is unconfirmed 1318 1319 ### The `commitment_signed` Message 1320 1321 This message is exchanged by both peers. It contains the signatures for 1322 the first commitment transaction, which uses a format determined by the 1323 `channel_type` sent in `open_channel2` and `accept_channel2`. 1324 1325 Rationale and Requirements are the same as listed below, 1326 for [`commitment_signed`](#committing-updates-so-far-commitment_signed) with the following additions. 1327 1328 #### Requirements 1329 1330 The sending node: 1331 - MUST send zero HTLCs. 1332 - MUST remember the details of this funding transaction. 1333 1334 The receiving node: 1335 - if the message has one or more HTLCs: 1336 - MUST fail the negotiation 1337 - if it has not already transmitted its `commitment_signed`: 1338 - MUST send `commitment_signed` 1339 - Otherwise: 1340 - MUST send `tx_signatures` if it should sign first, as specified 1341 in the [`tx_signatures` requirements](#the-tx_signatures-message) 1342 1343 #### Rationale 1344 1345 The first commitment transaction has no HTLCs. 1346 1347 Once peers are ready to exchange commitment signatures, they must remember 1348 the details of the funding transaction to allow resuming the signatures 1349 exchange if a disconnection happens. 1350 1351 ### Sharing funding signatures: `tx_signatures` 1352 1353 After a valid `commitment_signed` has been received 1354 from the peer and a `commitment_signed` has been sent, a peer: 1355 - MUST transmit `tx_signatures` with their signatures for the funding 1356 transaction, following the order specified in the 1357 [`tx_signatures` requirements](#the-tx_signatures-message) 1358 1359 #### Requirements 1360 1361 The sending node: 1362 - MUST verify it has received a valid commitment signature from its peer 1363 - MUST remember the details of this funding transaction 1364 - if it has NOT received a valid `commitment_signed` message: 1365 - MUST NOT send a `tx_signatures` message 1366 1367 The receiving node: 1368 - if has already sent or received a `channel_ready` message for this 1369 channel: 1370 - MUST ignore this message 1371 - if the `witness` weight lowers the effective `feerate` 1372 below the *opener*'s feerate for the funding transaction and the effective 1373 `feerate` is determined by the receiving node to be insufficient for 1374 getting the transaction confirmed in a timely manner: 1375 - SHOULD broadcast their commitment transaction, closing the channel 1376 - SHOULD double-spend their channel inputs when there is a productive 1377 opportunity to do so; effectively canceling this channel open 1378 - SHOULD apply `witnesses` to the funding transaction and broadcast it 1379 1380 #### Rationale 1381 1382 A peer sends their `tx_signatures` after receiving a valid `commitment_signed` 1383 message, following the order specified in the [`tx_signatures` section](#the-tx_signatures-message). 1384 1385 In the case where a peer provides valid witness data that causes their paid 1386 feerate to fall beneath the `open_channel2.funding_feerate_perkw`, the channel 1387 should be considered failed and the channel should be double-spent when 1388 there is a productive opportunity to do so. This should disincentivize 1389 peers from underpaying fees. 1390 1391 ### Fee bumping: `tx_init_rbf` and `tx_ack_rbf` 1392 1393 After the funding transaction has been broadcast, it can be replaced by 1394 a transaction paying more fees to make the channel confirm faster. 1395 1396 #### Requirements 1397 1398 The sender of `tx_init_rbf`: 1399 - MUST be the *initiator* 1400 - MUST NOT have sent or received a `channel_ready` message. 1401 1402 The recipient: 1403 - MUST fail the negotiation if they have already sent or received 1404 `channel_ready` 1405 - MAY fail the negotiation for any reason 1406 1407 #### Rationale 1408 1409 If a valid `channel_ready` message is received in the middle of an 1410 RBF attempt, the attempt MUST be abandoned. 1411 1412 Peers can use different values in `tx_init_rbf.funding_output_contribution` 1413 and `tx_ack_rbf.funding_output_contribution` from the amounts transmitted 1414 in `open_channel2` and `accept_channel2`: they are allowed to change how 1415 much they wish to commit to the funding output. 1416 1417 It's recommended that a peer, rather than fail the RBF negotiation due to 1418 a large feerate change, instead sets their `sats` to zero, and decline to 1419 participate further in the channel funding: by not contributing, they 1420 may obtain incoming liquidity at no cost. 1421 1422 ## Channel Quiescence 1423 1424 Various fundamental changes, in particular protocol upgrades, are 1425 easiest on channels where both commitment transactions match, and no 1426 pending updates are in flight. We define a protocol to quiesce the 1427 channel by indicating that "SomeThing Fundamental is Underway". 1428 1429 ### `stfu` 1430 1431 1. type: 2 (`stfu`) 1432 2. data: 1433 * [`channel_id`:`channel_id`] 1434 * [`u8`:`initiator`] 1435 1436 ### Requirements 1437 1438 The sender of `stfu`: 1439 - MUST NOT send `stfu` unless `option_quiesce` is negotiated. 1440 - MUST NOT send `stfu` if any of the sender's htlc additions, htlc removals 1441 or fee updates are pending for either peer. 1442 - MUST NOT send `stfu` twice. 1443 - if it is replying to an `stfu`: 1444 - MUST set `initiator` to 0 1445 - otherwise: 1446 - MUST set `initiator` to 1 1447 - MUST set `channel_id` to the id of the channel to quiesce. 1448 - MUST now consider the channel to be quiescing. 1449 - MUST NOT send an update message after `stfu`. 1450 1451 The receiver of `stfu`: 1452 - if it has sent `stfu` then: 1453 - MUST now consider the channel to be quiescent 1454 - otherwise: 1455 - SHOULD NOT send any more update messages. 1456 - MUST reply with `stfu` once it can do so. 1457 1458 Both nodes: 1459 - MUST disconnect after 60 seconds of quiescence if the HTLCs are pending. 1460 1461 Upon disconnection: 1462 - the channel is no longer considered quiescent. 1463 1464 Dependent Protocols: 1465 - MUST specify all states that terminate quiescence. 1466 - NOTE: this prevents batching executions of protocols that depend on 1467 quiescence. 1468 1469 ### Rationale 1470 1471 The normal use would be to cease sending updates, then wait for all 1472 the current updates to be acknowledged by both peers, then start 1473 quiescence. For some protocols, choosing the initiator matters, 1474 so this flag is sent. 1475 1476 If both sides send `stfu` simultaneously, they will both set 1477 `initiator` to `1`, in which case the "initiator" is arbitrarily 1478 considered to be the channel funder (the sender of `open_channel`). 1479 The quiescence effect is exactly the same as if one had replied to the 1480 other. 1481 1482 Dependent protocols have to specify termination conditions to prevent the need 1483 for disconnection to resume channel traffic. An explicit resume message was 1484 [considered but rejected](https://github.com/rustyrussell/lightning-rfc/pull/14) 1485 since it introduces a number of edge cases that make bilateral consensus of 1486 channel state significantly more complex to maintain. This introduces the 1487 derivative property that it is impossible to batch multiple downstream protocols 1488 in the same quiescence session. 1489 1490 ## Channel Close 1491 1492 Nodes can negotiate a mutual close of the connection, which unlike a 1493 unilateral close, allows them to access their funds immediately and 1494 can be negotiated with lower fees. 1495 1496 Closing happens in two stages: 1497 1. one side indicates it wants to clear the channel (and thus will accept no new HTLCs) 1498 2. once all HTLCs are resolved, the final channel close negotiation begins. 1499 1500 +-------+ +-------+ 1501 | | shutdown(scriptA1) | | 1502 | |--------------------------------------------------------->| | 1503 | | shutdown(scriptB1) | | 1504 | |<---------------------------------------------------------| | 1505 | | | | 1506 | | <complete all pending HTLCs> | | 1507 | A | .... | B | 1508 | | | | 1509 | | closing_complete(scriptA1, scriptB1, 1000 sat) | | 1510 | |--------------------------------------------------------->| | 1511 | | closing_complete(scriptB1, scriptA1, 750 sat) | | 1512 | |<---------------------------------------------------------| | 1513 | | closing_sig(scriptA1, scriptB1, 1000 sat) | | 1514 | |<---------------------------------------------------------| | 1515 | | closing_sig(scriptB1, scriptA1, 750 sat) | | 1516 | |--------------------------------------------------------->| | 1517 | | | | 1518 | | <A updates their script> | | 1519 | | | | 1520 | | closing_complete(scriptA2, scriptB1, 1100 sat) | | 1521 | |--------------------------------------------------------->| | 1522 | | closing_sig(scriptA2, scriptB1, 1100 sat) | | 1523 | |<---------------------------------------------------------| | 1524 | | | | 1525 | | .... | | 1526 | | | | 1527 | | <B RBFs their previous tx> | | 1528 | | | | 1529 | | closing_complete(scriptB1, scriptA2, 850 sat) | | 1530 | |<---------------------------------------------------------| | 1531 | | closing_sig(scriptB1, scriptA2, 850 sat) | | 1532 | |--------------------------------------------------------->| | 1533 | | | | 1534 | | .... | | 1535 | | | | 1536 | | <A and B concurrently update their script> | | (*) Note that this is a very rare race condition 1537 | | | | 1538 | | closing_complete(scriptA3, scriptB1, 1250 sat) | | 1539 | |-----------------------------> | | 1540 | | closing_complete(scriptB2, scriptA2, 900 sat) | | 1541 | | <--------------------------------------| | 1542 | | closing_complete | | 1543 | | --------------------------->| | 1544 | | warning | | (*) B sends a warning because A is not using scriptB2 1545 | | <--------------------------------------| | 1546 | | closing_complete | | 1547 | |<------------------ | | 1548 | | warning | | (*) A sends a warning because B is not using scriptA3 1549 | |--------------------------------------------------------->| | 1550 | | warning | | 1551 | |<------------------ | | 1552 | | | | 1553 | | <disconnect> | | (*) A and B reconnect to resolve the race condition 1554 | | | | 1555 | | channel_reestablish | | 1556 | |--------------------------------------------------------->| | 1557 | | channel_reestablish | | 1558 | |<---------------------------------------------------------| | 1559 | | shutdown(scriptA3) | | 1560 | |--------------------------------------------------------->| | 1561 | | shutdown(scriptB2) | | 1562 | |<---------------------------------------------------------| | 1563 | | | | 1564 | | <A retries with B's latest script> | | 1565 | | | | 1566 | | closing_complete(scriptA3, scriptB2, 1250 sat) | | 1567 | |--------------------------------------------------------->| | 1568 | | closing_sig(scriptA3, scriptB2, 1250 sat) | | 1569 | |<---------------------------------------------------------| | 1570 | | | | 1571 | | <B retries with A's latest script> | | 1572 | | | | 1573 | | closing_complete(scriptB2, scriptA3, 900 sat) | | 1574 | |<---------------------------------------------------------| | 1575 | | closing_sig(scriptB2, scriptA3, 900 sat) | | 1576 | |--------------------------------------------------------->| | 1577 | | | | 1578 +-------+ +-------+ 1579 1580 ### Closing Initiation: `shutdown` 1581 1582 Either node (or both) can send a `shutdown` message to initiate closing, 1583 along with the `scriptpubkey` it wants to be paid to. 1584 1585 1. type: 38 (`shutdown`) 1586 2. data: 1587 * [`channel_id`:`channel_id`] 1588 * [`u16`:`len`] 1589 * [`len*byte`:`scriptpubkey`] 1590 1591 #### Requirements 1592 1593 A sending node: 1594 - if it hasn't sent a `funding_created` (if it is a funder) or a `funding_signed` (if it is a fundee): 1595 - MUST NOT send a `shutdown` 1596 - MAY send a `shutdown` before a `channel_ready`, i.e. before the funding transaction has reached `minimum_depth`. 1597 - if there are updates pending on the receiving node's commitment transaction: 1598 - MUST NOT send a `shutdown`. 1599 - MUST NOT send multiple `shutdown` messages. 1600 - MUST NOT send an `update_add_htlc` after a `shutdown`. 1601 - if no HTLCs remain in either commitment transaction (including dust HTLCs) 1602 and neither side has a pending `revoke_and_ack` to send: 1603 - MUST NOT send any `update` message after that point. 1604 - SHOULD fail to route any HTLC added after it has sent `shutdown`. 1605 - if it sent a non-zero-length `shutdown_scriptpubkey` in `open_channel` or `accept_channel`: 1606 - MUST send the same value in `scriptpubkey`. 1607 - MUST set `scriptpubkey` in one of the following forms: 1608 1609 1. `OP_0` `20` 20-bytes (version 0 pay to witness pubkey hash), OR 1610 2. `OP_0` `32` 32-bytes (version 0 pay to witness script hash), OR 1611 3. if (and only if) `option_shutdown_anysegwit` is negotiated: 1612 * `OP_1` through `OP_16` inclusive, followed by a single push of 2 to 40 bytes 1613 (witness program versions 1 through 16) 1614 4. if (and only if) `option_simple_close` is negotiated: 1615 * `OP_RETURN` followed by one of: 1616 * `6` to `75` inclusive followed by exactly that many bytes 1617 * `76` followed by `76` to `80` followed by exactly that many bytes 1618 1619 A receiving node: 1620 - if it hasn't received a `funding_signed` (if it is a funder) or a `funding_created` (if it is a fundee): 1621 - SHOULD send an `error` and fail the channel. 1622 - if the `scriptpubkey` is not in one of the above forms: 1623 - SHOULD send a `warning`. 1624 - if it hasn't sent a `channel_ready` yet: 1625 - MAY reply to a `shutdown` message with a `shutdown` 1626 - once there are no outstanding updates on the peer, UNLESS it has already sent a `shutdown`: 1627 - MUST reply to a `shutdown` message with a `shutdown` 1628 - if both nodes advertised the `option_upfront_shutdown_script` feature, and the receiving node received a non-zero-length `shutdown_scriptpubkey` in `open_channel` or `accept_channel`, and that `shutdown_scriptpubkey` is not equal to `scriptpubkey`: 1629 - MAY send a `warning`. 1630 - MUST fail the connection. 1631 1632 #### Rationale 1633 1634 If channel state is always "clean" (no pending changes) when a 1635 shutdown starts, the question of how to behave if it wasn't is avoided: 1636 the sender always sends a `commitment_signed` first. 1637 1638 As shutdown implies a desire to terminate, it implies that no new 1639 HTLCs will be added or accepted. Once any HTLCs are cleared, there are no commitments 1640 for which a revocation is owed, and all updates are included on both commitment 1641 transactions, the peer may immediately begin closing negotiation, so we ban further 1642 updates to the commitment transaction (in particular, `update_fee` would be 1643 possible otherwise). However, while there are HTLCs on the commitment transaction, 1644 the initiator may find it desirable to increase the feerate as there may be pending 1645 HTLCs on the commitment which could timeout. 1646 1647 The `scriptpubkey` forms include only standard segwit forms accepted by 1648 the Bitcoin network, which ensures the resulting transaction will 1649 propagate to miners. However old nodes may send non-segwit scripts, which 1650 may be accepted for backwards-compatibility (with a caveat to force-close 1651 if this output doesn't meet dust relay requirements). 1652 1653 The `option_upfront_shutdown_script` feature means that the node 1654 wanted to pre-commit to `shutdown_scriptpubkey` in case it was 1655 compromised somehow. This is a weak commitment (a malevolent 1656 implementation tends to ignore specifications like this one!), but it 1657 provides an incremental improvement in security by requiring the cooperation 1658 of the receiving node to change the `scriptpubkey`. 1659 1660 The `shutdown` response requirement implies that the node sends `commitment_signed` to commit any outstanding changes before replying; however, it could theoretically reconnect instead, which would simply erase all outstanding uncommitted changes. 1661 1662 `OP_RETURN` is only standard if followed by PUSH opcodes, and the total script 1663 is 83 bytes or less. We are slightly stricter, to only allow a single PUSH, but 1664 there are two forms in script: one which pushes up to 75 bytes, and a longer 1665 one (`OP_PUSHDATA1`) which is needed for 76-80 bytes. 1666 1667 1668 ### Closing Negotiation: `closing_complete` and `closing_sig` 1669 1670 Once shutdown is complete, the channel is empty of HTLCs, there are no commitments 1671 for which a revocation is owed, and all updates are included on both commitments, 1672 the final current commitment transactions will have no HTLCs. 1673 1674 If `option_simple_close` is not negotiated, see [Legacy Closing 1675 Negotiation](#legacy-closing-negotiation-closing_signed) below. 1676 1677 Each peer creates their own closing transaction where they pay the fee, and sends 1678 `closing_complete` to the other peer with the transaction details. The other peer 1679 simply signs that transaction and sends back `closing_sig`. Each peer will thus 1680 independently send `closing_complete` and receive `closing_sig`, resulting in two 1681 independent (but conflicting) closing transactions being created. 1682 1683 The lesser-paid peer (if either is) can opt to omit their own output from the 1684 closing transaction. 1685 1686 This process can be repeated multiple times by sending `closing_complete` again, 1687 which allows increasing the fees and changing the output script. 1688 1689 1. type: 40 (`closing_complete`) 1690 2. data: 1691 * [`channel_id`:`channel_id`] 1692 * [`u16`:`closer_scriptpubkey_len`] 1693 * [`closer_scriptpubkey_len*byte`:`closer_scriptpubkey`] 1694 * [`u16`:`closee_scriptpubkey_len`] 1695 * [`closee_scriptpubkey_len*byte`:`closee_scriptpubkey`] 1696 * [`u64`:`fee_satoshis`] 1697 * [`u32`:`locktime`] 1698 * [`closing_tlvs`:`tlvs`] 1699 1700 1. type: 41 (`closing_sig`) 1701 2. data: 1702 * [`channel_id`:`channel_id`] 1703 * [`u16`:`closer_scriptpubkey_len`] 1704 * [`closer_scriptpubkey_len*byte`:`closer_scriptpubkey`] 1705 * [`u16`:`closee_scriptpubkey_len`] 1706 * [`closee_scriptpubkey_len*byte`:`closee_scriptpubkey`] 1707 * [`u64`:`fee_satoshis`] 1708 * [`u32`:`locktime`] 1709 * [`closing_tlvs`:`tlvs`] 1710 1711 1. `tlv_stream`: `closing_tlvs` 1712 2. types: 1713 1. type: 1 (`closer_output_only`) 1714 2. data: 1715 * [`signature`:`sig`] 1716 1. type: 2 (`closee_output_only`) 1717 2. data: 1718 * [`signature`:`sig`] 1719 1. type: 3 (`closer_and_closee_outputs`) 1720 2. data: 1721 * [`signature`:`sig`] 1722 1723 #### Requirements 1724 1725 Note: the details and requirements for the transaction being signed are in [BOLT 3](03-transactions.md#closing-transaction). 1726 1727 An output is *dust* if the amount is less than the [Bitcoin Core Dust Thresholds](03-transactions.md#dust-limits). 1728 1729 Note: These requirements only apply if `option_simple_close` is 1730 negotiated, otherwise the requirements are in [Legacy Closing 1731 Negotiation](#legacy-closing-negotiation-closing_signed). 1732 1733 Both nodes: 1734 - After a `shutdown` has been sent and received, AND no HTLCs remain in either commitment transaction: 1735 - SHOULD send a `closing_complete` message. 1736 1737 The sender of `closing_complete` (aka. "the closer"): 1738 - MUST set `fee_satoshis` to a fee less than or equal to its outstanding balance, rounded down to whole satoshis. 1739 - MUST set `fee_satoshis` so that at least one output is not dust. 1740 - MUST set `closer_scriptpubkey` to its desired output script. 1741 - MUST set `closee_scriptpubkey` to the last script it received from its peer (from `closing_complete` or from the initial `shutdown`). 1742 - MUST set `locktime` to the desired `nLockTime` of the closing transaction. 1743 - If the local outstanding balance (in millisatoshi) is less than the remote outstanding balance: 1744 - MUST NOT set `closer_output_only`. 1745 - MUST set `closee_output_only` if the local output amount is dust. 1746 - MAY set `closee_output_only` if it considers the local output amount uneconomical AND its `closer_scriptpubkey` is not `OP_RETURN`. 1747 - Otherwise (not lesser amount, cannot remove its own output): 1748 - MUST NOT set `closee_output_only`. 1749 - If it considers the local output amount uneconomical: 1750 - MAY send a `closer_scriptpubkey` that is a valid `OP_RETURN` script. 1751 - If it does, the output value MUST be set to zero so that all funds go to fees, as specified in [BOLT #3](03-transactions.md#closing-transaction). 1752 - If the closee's output amount is dust: 1753 - MUST set `closer_output_only`. 1754 - MUST NOT set `closer_and_closee_outputs`. 1755 - Otherwise: 1756 - MUST set both `closer_output_only` and `closer_and_closee_outputs`. 1757 - MUST generate its closing transaction as specified in [BOLT #3](03-transactions.md#closing-transaction). 1758 - MUST set `signature` fields as valid signature using its `funding_pubkey` of: 1759 - `closer_output_only`: closing transaction with only the local ("closer") output. 1760 - `closee_output_only`: closing transaction with only the remote ("closee") output. 1761 - `closer_and_closee_outputs`: closing transaction with both the closer and closee outputs. 1762 - If it wants to send another `closing_complete` (e.g. with a different `fee_satoshis` or `closer_scriptpubkey`): 1763 - MUST wait until it has received `closing_sig` first. 1764 - SHOULD close the connection if it doesn't receive `closing_sig`. 1765 1766 The receiver of `closing_complete` (aka. "the closee"): 1767 - If `fee_satoshis` is greater than the closer's outstanding balance: 1768 - MUST either send a `warning` and close the connection, or send an `error` and fail the channel. 1769 - If `closee_scriptpubkey` does not match the last script it sent (from `closing_complete` or from the initial `shutdown`): 1770 - SHOULD ignore `closing_complete`. 1771 - SHOULD send a `warning`. 1772 - SHOULD close the connection. 1773 - If `closer_scriptpubkey` is invalid (as detailed in the [`shutdown` requirements](#closing-initiation-shutdown)): 1774 - SHOULD ignore `closing_complete`. 1775 - SHOULD send a `warning`. 1776 - SHOULD close the connection. 1777 - If `closer_scriptpubkey` is a valid `OP_RETURN` script: 1778 - MUST set the closer's output amount to zero so that all funds go to fees, as specified in [BOLT #3](03-transactions.md#closing-transaction). 1779 - MUST generate the remote closing transaction as specified in [BOLT #3](03-transactions.md#closing-transaction). 1780 - Select a signature for validation: 1781 - If the local output amount is dust: 1782 - MUST use `closer_output_only`. 1783 - Otherwise, if it considers the local output amount uneconomical AND its `closee_scriptpubkey` is not `OP_RETURN`: 1784 - MUST use `closer_output_only`. 1785 - Otherwise, if `closer_and_closee_outputs` is present: 1786 - MUST use `closer_and_closee_outputs`. 1787 - Otherwise: 1788 - MUST use `closee_output_only`. 1789 - If the selected signature field does not exist: 1790 - MUST either send a `warning` and close the connection, or send an `error` and fail the channel. 1791 - If the signature field is not valid for the corresponding closing transaction specified in [BOLT #3](03-transactions.md#closing-transaction): 1792 - MUST either send a `warning` and close the connection, or send an `error` and fail the channel. 1793 - If the signature field is non-compliant with LOW-S-standard rule<sup>[LOWS](https://github.com/bitcoin/bitcoin/pull/6769)</sup>: 1794 - MUST either send a `warning` and close the connection, or send an `error` and fail the channel. 1795 - MUST sign and broadcast the corresponding closing transaction. 1796 - MUST send `closing_sig` with a single valid signature in the same TLV field as the `closing_complete`. 1797 - MUST use `closer_scriptpubkey` for its own future `closing_complete` messages. 1798 1799 The receiver of `closing_sig`: 1800 - If `closer_scriptpubkey`, `closee_scriptpubkey`, `fee_satoshis` or `locktime` don't match what was sent in `closing_complete`: 1801 - MUST either send a `warning` and close the connection, or send an `error` and fail the channel. 1802 - If `tlvs` does not contain exactly one signature: 1803 - MUST either send a `warning` and close the connection, or send an `error` and fail the channel. 1804 - If `tlvs` does not contain one of the TLV fields sent in `closing_complete`: 1805 - MUST either send a `warning` and close the connection, or send an `error` and fail the channel. 1806 - If the signature field is not valid for the corresponding closing transaction specified in [BOLT #3](03-transactions.md#closing-transaction): 1807 - MUST either send a `warning` and close the connection, or send an `error` and fail the channel. 1808 - If the signature field is non-compliant with LOW-S-standard rule<sup>[LOWS](https://github.com/bitcoin/bitcoin/pull/6769)</sup>: 1809 - MUST either send a `warning` and close the connection, or send an `error` and fail the channel. 1810 - otherwise: 1811 - MUST broadcast the corresponding closing transaction. 1812 - MAY send another `closing_complete` (e.g. with a different `fee_satoshis` or `closer_scriptpubkey`). 1813 1814 ### Rationale 1815 1816 The close protocol is designed to avoid any failure scenarios caused by fee disagreement, 1817 since each side offers to pay its own desired fee. 1818 1819 If one side has less funds than the other, it may choose to omit its own output, and in this case 1820 dust MUST be omitted, to ensure that the resulting transaction can be broadcast. 1821 1822 The corner case where fees are so high that both outputs are dust is addressed in two ways: paying 1823 a low fee to avoid the problem, or using an `OP_RETURN` (which is never "dust"). If one side 1824 chooses to use an `OP_RETURN` output, its amount must be 0 to ensure that the resulting transaction 1825 can be broadcast. 1826 1827 Note that there is usually no reason to pay a high fee for rapid processing, since an urgent child 1828 could pay the fee on the closing transactions' behalf. If rapid processing is desired and CPFP is 1829 not an option, the closer can RBF its previous closing transactions by sending `closing_complete` 1830 again. 1831 1832 Sending a new `closing_complete` message overrides previous ones, so you can negotiate again (even 1833 changing the output address if `upfront_shutdown_script` was not negotiated). This creates a rare 1834 race condition if both nodes send `closing_complete` to change their `closer_scriptpubkey` at the 1835 same time: when that happens, the `closing_complete` they receive will be using their previous 1836 output script, so they shouldn't sign the corresponding transaction. When that happens, we simply 1837 reconnect, which provides the opportunity for both nodes to send their latest output script in 1838 `shutdown` and restart the signing flow. We include the closer and closee scripts in `closing_sig` 1839 to allow our peer to detect a script mismatch and correctly ignore the signatures, which also 1840 helps debugging race conditions. 1841 1842 If the closer proposes a transaction which will not relay (an output is dust, or the fee rate it 1843 proposes is too low), it doesn't harm the closee to sign the transaction. 1844 1845 Similarly, if the closer proposes a high fee, it doesn't harm the closee to sign the transaction, 1846 as the closer is paying. 1847 1848 There's a slight game where each side would prefer the other side pay the fee, and proposes a 1849 minimal fee. If neither side proposes a fee which will relay, the negotiation can occur again, 1850 or the final commitment transaction can be spent. In practice, the opener has an incentive to 1851 offer a reasonable closing fee, as they would pay the fee for the commitment transaction, which 1852 also costs more to spend. 1853 1854 ### Legacy Closing Negotiation: `closing_signed` 1855 1856 Once shutdown is complete, the channel is empty of HTLCs, there are no commitments 1857 for which a revocation is owed, and all updates are included on both commitments, 1858 the final current commitment transactions will have no HTLCs, and closing fee 1859 negotiation begins: if `option_simple_close` is negotiated, the section above applies, 1860 otherwise this legacy section applies. 1861 1862 The funder chooses a fee it thinks is fair, and 1863 signs the closing transaction with the `scriptpubkey` fields from the 1864 `shutdown` messages (along with its chosen fee) and sends the signature; 1865 the other node then replies similarly, using a fee it thinks is fair. This 1866 exchange continues until both agree on the same fee or when one side fails 1867 the channel. 1868 1869 In the modern method, the funder sends its permissible fee range, and the 1870 non-funder has to pick a fee in this range. If the non-funder chooses the same 1871 value, negotiation is complete after two messages, otherwise the funder will 1872 reply with the same value (completing after three messages). 1873 1874 1. type: 39 (`closing_signed`) 1875 2. data: 1876 * [`channel_id`:`channel_id`] 1877 * [`u64`:`fee_satoshis`] 1878 * [`signature`:`signature`] 1879 * [`closing_signed_tlvs`:`tlvs`] 1880 1881 1. `tlv_stream`: `closing_signed_tlvs` 1882 2. types: 1883 1. type: 1 (`fee_range`) 1884 2. data: 1885 * [`u64`:`min_fee_satoshis`] 1886 * [`u64`:`max_fee_satoshis`] 1887 1888 #### Requirements 1889 1890 Note: These requirements only apply if `option_simple_close` is NOT 1891 negotiated, otherwise the requirements [Closing Negotiation: 1892 `closing_complete` and `closing_sig`](#closing-negotiation-closing_complete-and-closing_sig) apply. 1893 1894 The funding node: 1895 - after `shutdown` has been received, AND no HTLCs remain in either commitment transaction: 1896 - SHOULD send a `closing_signed` message. 1897 1898 The sending node: 1899 - SHOULD set the initial `fee_satoshis` according to its estimate of cost of 1900 inclusion in a block. 1901 - SHOULD set `fee_range` according to the minimum and maximum fees it is 1902 prepared to pay for a close transaction. 1903 - if it doesn't receive a `closing_signed` response after a reasonable amount of time: 1904 - MUST fail the channel 1905 - if it is not the funder: 1906 - SHOULD set `max_fee_satoshis` to at least the `max_fee_satoshis` received 1907 - SHOULD set `min_fee_satoshis` to a fairly low value 1908 - MUST set `signature` to the Bitcoin signature of the close transaction, 1909 as specified in [BOLT #3](03-transactions.md#closing-transaction). 1910 1911 The receiving node: 1912 - if the `signature` is not valid for either variant of closing transaction 1913 specified in [BOLT #3](03-transactions.md#closing-transaction) OR non-compliant with LOW-S-standard rule<sup>[LOWS](https://github.com/bitcoin/bitcoin/pull/6769)</sup>: 1914 - MUST send a `warning` and close the connection, or send an 1915 `error` and fail the channel. 1916 - if `fee_satoshis` is equal to its previously sent `fee_satoshis`: 1917 - SHOULD sign and broadcast the final closing transaction. 1918 - MAY close the connection. 1919 - if `fee_satoshis` matches its previously sent `fee_range`: 1920 - SHOULD use `fee_satoshis` to sign and broadcast the final closing transaction 1921 - SHOULD reply with a `closing_signed` with the same `fee_satoshis` value if it is different from its previously sent `fee_satoshis` 1922 - MAY close the connection. 1923 - if the message contains a `fee_range`: 1924 - if there is no overlap between that and its own `fee_range`: 1925 - SHOULD send a warning 1926 - MUST fail the channel if it doesn't receive a satisfying `fee_range` after a reasonable amount of time 1927 - otherwise: 1928 - if it is the funder: 1929 - if `fee_satoshis` is not in the overlap between the sent and received `fee_range`: 1930 - MUST fail the channel 1931 - otherwise: 1932 - MUST reply with the same `fee_satoshis`. 1933 - otherwise (it is not the funder): 1934 - if it has already sent a `closing_signed`: 1935 - if `fee_satoshis` is not the same as the value it sent: 1936 - MUST fail the channel 1937 - otherwise: 1938 - MUST propose a `fee_satoshis` in the overlap between received and (about-to-be) sent `fee_range`. 1939 - otherwise, if `fee_satoshis` is not strictly between its last-sent `fee_satoshis` 1940 and its previously-received `fee_satoshis`, UNLESS it has since reconnected: 1941 - SHOULD send a `warning` and close the connection, or send an 1942 `error` and fail the channel. 1943 - otherwise, if the receiver agrees with the fee: 1944 - SHOULD reply with a `closing_signed` with the same `fee_satoshis` value. 1945 - otherwise: 1946 - MUST propose a value "strictly between" the received `fee_satoshis` 1947 and its previously-sent `fee_satoshis`. 1948 1949 The receiving node: 1950 - if one of the outputs in the closing transaction is below the dust limit for its `scriptpubkey` (see [BOLT 3](03-transactions.md#dust-limits)): 1951 - MUST fail the channel 1952 1953 #### Rationale 1954 1955 When `fee_range` is not provided, the "strictly between" requirement ensures 1956 that forward progress is made, even if only by a single satoshi at a time. 1957 To avoid keeping state and to handle the corner case, where fees have shifted 1958 between disconnection and reconnection, negotiation restarts on reconnection. 1959 1960 Note there is limited risk if the closing transaction is 1961 delayed, but it will be broadcast very soon; so there is usually no 1962 reason to pay a premium for rapid processing. 1963 1964 Note that the non-funder is not paying the fee, so there is no reason for it 1965 to have a maximum feerate. It may want a minimum feerate, however, to ensure 1966 that the transaction propagates. It can always use CPFP later to speed up 1967 confirmation if necessary, so that minimum should be low. 1968 1969 It may happen that the closing transaction doesn't meet bitcoin's default relay 1970 policies (e.g. when using a non-segwit shutdown script for an output below 546 1971 satoshis, which is possible if `dust_limit_satoshis` is below 546 satoshis). 1972 No funds are at risk when that happens, but the channel must be force-closed as 1973 the closing transaction will likely never reach miners. 1974 1975 ## Normal Operation 1976 1977 Once both nodes have exchanged `channel_ready` (and optionally [`announcement_signatures`](07-routing-gossip.md#the-announcement_signatures-message)), the channel can be used to make payments via Hashed Time Locked Contracts. 1978 1979 Changes are sent in batches: one or more `update_` messages are sent before a 1980 `commitment_signed` message, as in the following diagram: 1981 1982 +-------+ +-------+ 1983 | |--(1)---- update_add_htlc ---->| | 1984 | |--(2)---- update_add_htlc ---->| | 1985 | |<-(3)---- update_add_htlc -----| | 1986 | | | | 1987 | |--(4)--- commitment_signed --->| | 1988 | A |<-(5)---- revoke_and_ack ------| B | 1989 | | | | 1990 | |<-(6)--- commitment_signed ----| | 1991 | |--(7)---- revoke_and_ack ----->| | 1992 | | | | 1993 | |--(8)--- commitment_signed --->| | 1994 | |<-(9)---- revoke_and_ack ------| | 1995 +-------+ +-------+ 1996 1997 Counter-intuitively, these updates apply to the *other node's* 1998 commitment transaction; the node only adds those updates to its own 1999 commitment transaction when the remote node acknowledges it has 2000 applied them via `revoke_and_ack`. 2001 2002 Thus each update traverses through the following states: 2003 2004 1. pending on the receiver 2005 2. in the receiver's latest commitment transaction 2006 3. ... and the receiver's previous commitment transaction has been revoked, 2007 and the update is pending on the sender 2008 4. ... and in the sender's latest commitment transaction 2009 5. ... and the sender's previous commitment transaction has been revoked 2010 2011 2012 As the two nodes' updates are independent, the two commitment 2013 transactions may be out of sync indefinitely. This is not concerning: 2014 what matters is whether both sides have irrevocably committed to a 2015 particular update or not (the final state, above). 2016 2017 ### Forwarding HTLCs 2018 2019 In general, a node offers HTLCs for two reasons: to initiate a payment of its own, 2020 or to forward another node's payment. In the forwarding case, care must 2021 be taken to ensure the *outgoing* HTLC cannot be redeemed unless the *incoming* 2022 HTLC can be redeemed. The following requirements ensure this is always true. 2023 2024 The respective **addition/removal** of an HTLC is considered *irrevocably committed* when: 2025 2026 1. The commitment transaction **with/without** it is committed to by both nodes, and any 2027 previous commitment transaction **without/with** it has been revoked, OR 2028 2. The commitment transaction **with/without** it has been irreversibly committed to 2029 the blockchain. 2030 2031 #### Requirements 2032 2033 A node: 2034 - until an incoming HTLC has been irrevocably committed: 2035 - MUST NOT offer the corresponding outgoing HTLC (`update_add_htlc`) in response to that incoming HTLC. 2036 - until the removal of an outgoing HTLC is irrevocably committed, OR until the outgoing on-chain HTLC output has been spent via the HTLC-timeout transaction (with sufficient depth): 2037 - MUST NOT fail the incoming HTLC (`update_fail_htlc`) that corresponds 2038 to that outgoing HTLC. 2039 - once the `cltv_expiry` of an incoming HTLC has been reached, OR if `cltv_expiry` minus `current_height` is less than `cltv_expiry_delta` for the corresponding outgoing HTLC: 2040 - MUST fail that incoming HTLC (`update_fail_htlc`). 2041 - if an incoming HTLC's `cltv_expiry` is unreasonably far in the future: 2042 - SHOULD fail that incoming HTLC (`update_fail_htlc`). 2043 - upon receiving an `update_fulfill_htlc` for an outgoing HTLC, OR upon discovering the `payment_preimage` from an on-chain HTLC spend: 2044 - MUST fulfill the incoming HTLC that corresponds to that outgoing HTLC. 2045 2046 #### Rationale 2047 2048 In general, one side of the exchange needs to be dealt with before the other. 2049 Fulfilling an HTLC is different: knowledge of the preimage is, by definition, 2050 irrevocable and the incoming HTLC should be fulfilled as soon as possible to 2051 reduce latency. 2052 2053 An HTLC with an unreasonably long expiry is a denial-of-service vector and 2054 therefore is not allowed. Note that the exact value of "unreasonable" is currently unclear 2055 and may depend on network topology. 2056 2057 ### `cltv_expiry_delta` Selection 2058 2059 Once an HTLC has timed out, it can either be fulfilled or timed-out; 2060 care must be taken around this transition, both for offered and received HTLCs. 2061 2062 Consider the following scenario, where A sends an HTLC to B, who 2063 forwards to C, who delivers the goods as soon as the payment is 2064 received. 2065 2066 1. C needs to be sure that the HTLC from B cannot time out, even if B becomes 2067 unresponsive; i.e. C can fulfill the incoming HTLC on-chain before B can 2068 time it out on-chain. 2069 2070 2. B needs to be sure that if C fulfills the HTLC from B, it can fulfill the 2071 incoming HTLC from A; i.e. B can get the preimage from C and fulfill the incoming 2072 HTLC on-chain before A can time it out on-chain. 2073 2074 The critical settings here are the `cltv_expiry_delta` in 2075 [BOLT #7](07-routing-gossip.md#the-channel_update-message) and the 2076 related `min_final_cltv_expiry_delta` in [BOLT #11](11-payment-encoding.md#tagged-fields). 2077 `cltv_expiry_delta` is the minimum difference in HTLC CLTV timeouts, in 2078 the forwarding case (B). `min_final_cltv_expiry_delta` is the minimum difference 2079 between HTLC CLTV timeout and the current block height, for the 2080 terminal case (C). 2081 2082 Note that a node is at risk if it accepts an HTLC in one channel and 2083 offers an HTLC in another channel with too small of a difference between 2084 the CLTV timeouts. For this reason, the `cltv_expiry_delta` for the 2085 *outgoing* channel is used as the delta across a node. 2086 2087 The worst-case number of blocks between outgoing and 2088 incoming HTLC resolution can be derived, given a few assumptions: 2089 2090 * a worst-case reorganization depth `R` blocks 2091 * a grace-period `G` blocks after HTLC timeout before giving up on 2092 an unresponsive peer and dropping to chain 2093 * a number of blocks `S` between transaction broadcast and the 2094 transaction being included in a block 2095 2096 The worst case is for a forwarding node (B) that takes the longest 2097 possible time to spot the outgoing HTLC fulfillment and also takes 2098 the longest possible time to redeem it on-chain: 2099 2100 1. The B->C HTLC times out at block `N`, and B waits `G` blocks until 2101 it gives up waiting for C. B or C commits to the blockchain, 2102 and B spends HTLC, which takes `S` blocks to be included. 2103 2. Bad case: C wins the race (just) and fulfills the HTLC, B only sees 2104 that transaction when it sees block `N+G+S+1`. 2105 3. Worst case: There's reorganization `R` deep in which C wins and 2106 fulfills. B only sees transaction at `N+G+S+R`. 2107 4. B now needs to fulfill the incoming A->B HTLC, but A is unresponsive: B waits `G` more 2108 blocks before giving up waiting for A. A or B commits to the blockchain. 2109 5. Bad case: B sees A's commitment transaction in block `N+G+S+R+G+1` and has 2110 to spend the HTLC output, which takes `S` blocks to be mined. 2111 6. Worst case: there's another reorganization `R` deep which A uses to 2112 spend the commitment transaction, so B sees A's commitment 2113 transaction in block `N+G+S+R+G+R` and has to spend the HTLC output, which 2114 takes `S` blocks to be mined. 2115 7. B's HTLC spend needs to be at least `R` deep before it times out, 2116 otherwise another reorganization could allow A to timeout the 2117 transaction. 2118 2119 Thus, the worst case is `3R+2G+2S`, assuming `R` is at least 1. Note that the 2120 chances of three reorganizations in which the other node wins all of them is 2121 low for `R` of 2 or more. Since high fees are used (and HTLC spends can use 2122 almost arbitrary fees), `S` should be small during normal operation; although, 2123 given that block times are irregular, empty blocks still occur, fees may vary 2124 greatly, and the fees cannot be bumped on HTLC transactions, `S=12` should be 2125 considered a minimum. `S` is also the parameter that may vary the most under 2126 attack, so a higher value may be desirable when non-negligible amounts are at 2127 risk. The grace period `G` can be low (1 or 2), as nodes are required to timeout 2128 or fulfill as soon as possible; but if `G` is too low it increases the risk of 2129 unnecessary channel closure due to networking delays. 2130 2131 There are four values that need be derived: 2132 2133 1. the `cltv_expiry_delta` for channels, `3R+2G+2S`: if in doubt, a 2134 `cltv_expiry_delta` of at least 34 is reasonable (R=2, G=2, S=12). 2135 2136 2. the deadline for offered HTLCs: the deadline after which the channel has to 2137 be failed and timed out on-chain. This is `G` blocks after the HTLC's 2138 `cltv_expiry`: 1 or 2 blocks is reasonable. 2139 2140 3. the deadline for received HTLCs this node has fulfilled: the deadline after 2141 which the channel has to be failed and the HTLC fulfilled on-chain before 2142 its `cltv_expiry`. See steps 4-7 above, which imply a deadline of `2R+G+S` 2143 blocks before `cltv_expiry`: 18 blocks is reasonable. 2144 2145 4. the minimum `cltv_expiry` accepted for terminal payments: the 2146 worst case for the terminal node C is `2R+G+S` blocks (as, again, steps 2147 1-3 above don't apply). The default in [BOLT #11](11-payment-encoding.md) is 2148 18, which matches this calculation. 2149 2150 #### Requirements 2151 2152 An offering node: 2153 - MUST estimate a timeout deadline for each HTLC it offers. 2154 - MUST NOT offer an HTLC with a timeout deadline before its `cltv_expiry`. 2155 - if an HTLC which it offered is in either node's current 2156 commitment transaction, AND is past this timeout deadline: 2157 - SHOULD send an `error` to the receiving peer (if connected). 2158 - MUST fail the channel. 2159 2160 A fulfilling node: 2161 - for each HTLC it is attempting to fulfill: 2162 - MUST estimate a fulfillment deadline. 2163 - MUST fail (and not forward) an HTLC whose fulfillment deadline is already past. 2164 - if an HTLC it has fulfilled is in either node's current commitment 2165 transaction, AND is past this fulfillment deadline: 2166 - SHOULD send an `error` to the offering peer (if connected). 2167 - MUST fail the channel. 2168 2169 ### Bounding exposure to trimmed in-flight HTLCs: `max_dust_htlc_exposure_msat` 2170 2171 When an HTLC in a channel is below the "trimmed" threshold in [BOLT3 #3](03-transactions.md), 2172 the HTLC cannot be claimed on-chain, instead being turned into additional miner 2173 fees if either party unilaterally closes the channel. Because the threshold is 2174 per-HTLC, the total exposure to such HTLCs may be substantial if there are many 2175 dust HTLCs committed when the channel is force-closed. 2176 2177 This can be exploited in griefing attacks or even in miner-extractable-value attacks, 2178 if the malicious entity wins <sup>[mining capabilities](https://lists.linuxfoundation.org/pipermail/lightning-dev/2020-May/002714.html)</sup>. 2179 2180 The total exposure is given by the following back-of-the-envelope computation: 2181 2182 remote `max_accepted_htlcs` * (`HTLC-success-kiloweight` * `feerate_per_kw` + remote `dust_limit_satoshis`) 2183 + local `max_accepted_htlcs` * (`HTLC-timeout-kiloweight` * `feerate_per_kw` + remote `dust_limit_satoshis`) 2184 2185 To mitigate this scenario, a `max_dust_htlc_exposure_msat` threshold can be 2186 applied when sending, forwarding and receiving HTLCs. 2187 2188 A node: 2189 - when receiving an HTLC: 2190 - if the HTLC's `amount_msat` is smaller than the remote `dust_limit_satoshis` plus the HTLC-timeout fee at `feerate_per_kw`: 2191 - if the `amount_msat` plus the dust balance of the remote transaction is greater than `max_dust_htlc_exposure_msat`: 2192 - SHOULD fail this HTLC once it's committed 2193 - SHOULD NOT reveal a preimage for this HTLC 2194 - if the HTLC's `amount_msat` is smaller than the local `dust_limit_satoshis` plus the HTLC-success fee at `feerate_per_kw`: 2195 - if the `amount_msat` plus the dust balance of the local transaction is greater than `max_dust_htlc_exposure_msat`: 2196 - SHOULD fail this HTLC once it's committed 2197 - SHOULD NOT reveal a preimage for this HTLC 2198 - when offering an HTLC: 2199 - if the HTLC's `amount_msat` is smaller than the remote `dust_limit_satoshis` plus the HTLC-success fee at `feerate_per_kw`: 2200 - if the `amount_msat` plus the dust balance of the remote transaction is greater than `max_dust_htlc_exposure_msat`: 2201 - SHOULD NOT send this HTLC 2202 - SHOULD fail the corresponding incoming HTLC (if any) 2203 - if the HTLC's `amount_msat` is inferior to the holder's `dust_limit_satoshis` plus the HTLC-timeout fee at the `feerate_per_kw`: 2204 - if the `amount_msat` plus the dust balance of the local transaction is greater than `max_dust_htlc_exposure_msat`: 2205 - SHOULD NOT send this HTLC 2206 - SHOULD fail the corresponding incoming HTLC (if any) 2207 2208 The `max_dust_htlc_exposure_msat` is an upper bound on the trimmed balance from 2209 dust exposure. The exact value used is a matter of node policy. 2210 2211 For channels that don't use `option_anchors`, an increase of 2212 the `feerate_per_kw` may trim multiple htlcs from commitment transactions, 2213 which could create a large increase in dust exposure. 2214 2215 ### Adding an HTLC: `update_add_htlc` 2216 2217 Either node can send `update_add_htlc` to offer an HTLC to the other, 2218 which is redeemable in return for a payment preimage. Amounts are in 2219 millisatoshi, though on-chain enforcement is only possible for whole 2220 satoshi amounts greater than the dust limit (in commitment transactions these are rounded down as 2221 specified in [BOLT #3](03-transactions.md)). 2222 2223 The format of the `onion_routing_packet` portion, which indicates where the payment 2224 is destined, is described in [BOLT #4](04-onion-routing.md). 2225 2226 1. type: 128 (`update_add_htlc`) 2227 2. data: 2228 * [`channel_id`:`channel_id`] 2229 * [`u64`:`id`] 2230 * [`u64`:`amount_msat`] 2231 * [`sha256`:`payment_hash`] 2232 * [`u32`:`cltv_expiry`] 2233 * [`1366*byte`:`onion_routing_packet`] 2234 2235 1. `tlv_stream`: `update_add_htlc_tlvs` 2236 2. types: 2237 1. type: 0 (`blinded_path`) 2238 2. data: 2239 * [`point`:`path_key`] 2240 2241 #### Requirements 2242 2243 A sending node: 2244 - if it is _responsible_ for paying the Bitcoin fee: 2245 - MUST NOT offer `amount_msat` if, after adding that HTLC to its commitment 2246 transaction, it cannot pay the fee for either the local or remote commitment 2247 transaction at the current `feerate_per_kw` while maintaining its channel 2248 reserve (see [Updating Fees](#updating-fees-update_fee)). 2249 - if `option_anchors` applies to this commitment transaction and the sending 2250 node is the funder: 2251 - MUST be able to additionally pay for `to_local_anchor` and 2252 `to_remote_anchor` above its reserve. 2253 - SHOULD NOT offer `amount_msat` if, after adding that HTLC to its commitment 2254 transaction, its remaining balance doesn't allow it to pay the commitment 2255 transaction fee when receiving or sending a future additional non-dust HTLC 2256 while maintaining its channel reserve. It is recommended that this "fee spike 2257 buffer" can handle twice the current `feerate_per_kw` to ensure predictability 2258 between implementations. 2259 - if it is _not responsible_ for paying the Bitcoin fee: 2260 - SHOULD NOT offer `amount_msat` if, once the remote node adds that HTLC to 2261 its commitment transaction, it cannot pay the fee for the updated local or 2262 remote transaction at the current `feerate_per_kw` while maintaining its 2263 channel reserve. 2264 - MUST offer `amount_msat` greater than 0. 2265 - MUST NOT offer `amount_msat` below the receiving node's `htlc_minimum_msat` 2266 - MUST set `cltv_expiry` less than 500000000. 2267 - if result would be offering more than the remote's 2268 `max_accepted_htlcs` HTLCs, in the remote commitment transaction: 2269 - MUST NOT add an HTLC. 2270 - if the total value of offered HTLCs would exceed the remote's 2271 `max_htlc_value_in_flight_msat`: 2272 - MUST NOT add an HTLC. 2273 - for the first HTLC it offers: 2274 - MUST set `id` to 0. 2275 - MUST increase the value of `id` by 1 for each successive offer. 2276 - if it is relaying a payment inside a blinded route: 2277 - MUST set `path_key` (see [Route Blinding](04-onion-routing.md#route-blinding)) 2278 2279 `id` MUST NOT be reset to 0 after the update is complete (i.e. after `revoke_and_ack` has 2280 been received). It MUST continue incrementing instead. 2281 2282 A receiving node: 2283 - receiving an `amount_msat` equal to 0, OR less than its own `htlc_minimum_msat`: 2284 - SHOULD send a `warning` and close the connection, or send an 2285 `error` and fail the channel. 2286 - receiving an `amount_msat` that the sending node cannot afford at the current `feerate_per_kw` (while maintaining its channel reserve and any `to_local_anchor` and `to_remote_anchor` costs): 2287 - SHOULD send a `warning` and close the connection, or send an 2288 `error` and fail the channel. 2289 - if a sending node adds more than receiver `max_accepted_htlcs` HTLCs to 2290 its local commitment transaction, OR adds more than receiver `max_htlc_value_in_flight_msat` worth of offered HTLCs to its local commitment transaction: 2291 - SHOULD send a `warning` and close the connection, or send an 2292 `error` and fail the channel. 2293 - if sending node sets `cltv_expiry` to greater or equal to 500000000: 2294 - SHOULD send a `warning` and close the connection, or send an 2295 `error` and fail the channel. 2296 - MUST allow multiple HTLCs with the same `payment_hash`. 2297 - if the sender did not previously acknowledge the commitment of that HTLC: 2298 - MUST ignore a repeated `id` value after a reconnection. 2299 - if other `id` violations occur: 2300 - MAY send a `warning` and close the connection, or send an 2301 `error` and fail the channel. 2302 - MUST decrypt `onion_routing_packet` as described in [Onion Decryption](04-onion-routing.md#onion-decryption) to extract a `payload`. 2303 - MUST use `path_key` (if specified). 2304 - MUST use `payment_hash` as `associated_data`. 2305 - If decryption fails, the result is not a valid `payload` TLV, or it contains unknown even types: 2306 - MUST respond with an error as detailed in [Failure Messages](04-onion-routing.md#failure-messages) 2307 - Otherwise: 2308 - MUST follow the requirements for the reader of `payload` in [Payload Format](04-onion-routing.md#payload-format) 2309 2310 The `onion_routing_packet` contains an obfuscated list of hops and instructions for each hop along the path. 2311 It commits to the HTLC by setting the `payment_hash` as associated data, i.e. includes the `payment_hash` in the computation of HMACs. 2312 This prevents replay attacks that would reuse a previous `onion_routing_packet` with a different `payment_hash`. 2313 2314 #### Rationale 2315 2316 Invalid amounts are a clear protocol violation and indicate a breakdown. 2317 2318 If a node did not accept multiple HTLCs with the same payment hash, an 2319 attacker could probe to see if a node had an existing HTLC. This 2320 requirement, to deal with duplicates, leads to the use of a separate 2321 identifier; it's assumed a 64-bit counter never wraps. 2322 2323 Retransmissions of unacknowledged updates are explicitly allowed for 2324 reconnection purposes; allowing them at other times simplifies the 2325 recipient code (though strict checking may help debugging). 2326 2327 `max_accepted_htlcs` is limited to 483 to ensure that, even if both 2328 sides send the maximum number of HTLCs, the `commitment_signed` message will 2329 still be under the maximum message size. It also ensures that 2330 a single penalty transaction can spend the entire commitment transaction, 2331 as calculated in [BOLT #5](05-onchain.md#penalty-transaction-weight-calculation). 2332 2333 `cltv_expiry` values equal to or greater than 500000000 would indicate a time in 2334 seconds, and the protocol only supports an expiry in blocks. 2335 2336 The node _responsible_ for paying the Bitcoin fee should maintain a "fee 2337 spike buffer" on top of its reserve to accommodate a future fee increase. 2338 Without this buffer, the node _responsible_ for paying the Bitcoin fee may 2339 reach a state where it is unable to send or receive any non-dust HTLC while 2340 maintaining its channel reserve (because of the increased weight of the 2341 commitment transaction), resulting in a degraded channel. See [#728](https://github.com/lightningnetwork/lightning-rfc/issues/728) 2342 for more details. 2343 2344 ### Removing an HTLC: `update_fulfill_htlc`, `update_fail_htlc`, and `update_fail_malformed_htlc` 2345 2346 For simplicity, a node can only remove HTLCs added by the other node. 2347 There are four reasons for removing an HTLC: the payment preimage is supplied, 2348 it has timed out, it has failed to route, or it is malformed. 2349 2350 To supply the preimage: 2351 2352 1. type: 130 (`update_fulfill_htlc`) 2353 2. data: 2354 * [`channel_id`:`channel_id`] 2355 * [`u64`:`id`] 2356 * [`32*byte`:`payment_preimage`] 2357 1. `tlv_stream`: `update_fulfill_htlc_tlvs` 2358 2. types: 2359 1. type: 1 (`attribution_data`) 2360 2. data: 2361 * [`20*u32`:`htlc_hold_times`] 2362 * [`210*sha256[..4]`:`truncated_hmacs`] 2363 2364 For a timed out or route-failed HTLC: 2365 2366 1. type: 131 (`update_fail_htlc`) 2367 2. data: 2368 * [`channel_id`:`channel_id`] 2369 * [`u64`:`id`] 2370 * [`u16`:`len`] 2371 * [`len*byte`:`reason`] 2372 1. `tlv_stream`: `update_fail_htlc_tlvs` 2373 2. types: 2374 1. type: 1 (`attribution_data`) 2375 2. data: 2376 * [`20*u32`:`htlc_hold_times`] 2377 * [`210*sha256[..4]`:`truncated_hmacs`] 2378 2379 The `reason` field is an opaque encrypted blob for the benefit of the 2380 original HTLC initiator, as defined in [BOLT #4](04-onion-routing.md); 2381 however, there's a special malformed failure variant for the case where 2382 the peer couldn't parse it: in this case the current node instead takes action, encrypting 2383 it into a `update_fail_htlc` for relaying. 2384 2385 For an unparsable HTLC: 2386 2387 1. type: 135 (`update_fail_malformed_htlc`) 2388 2. data: 2389 * [`channel_id`:`channel_id`] 2390 * [`u64`:`id`] 2391 * [`sha256`:`sha256_of_onion`] 2392 * [`u16`:`failure_code`] 2393 2394 #### Requirements 2395 2396 A node: 2397 - SHOULD remove an HTLC as soon as it can. 2398 - SHOULD fail an HTLC which has timed out. 2399 - until the corresponding HTLC is irrevocably committed in both sides' 2400 commitment transactions: 2401 - MUST NOT send an `update_fulfill_htlc`, `update_fail_htlc`, or 2402 `update_fail_malformed_htlc`. 2403 - When failing an incoming HTLC: 2404 - If `current_path_key` is set in the onion payload and it is not the 2405 final node: 2406 - MUST send an `update_fail_htlc` error using the `invalid_onion_blinding` 2407 failure code for any local or downstream errors. 2408 - SHOULD use the `sha256_of_onion` of the onion it received. 2409 - MAY use an all zero `sha256_of_onion`. 2410 - SHOULD add a random delay before sending `update_fail_htlc`. 2411 - If `path_key` is set in the incoming `update_add_htlc`: 2412 - MUST send an `update_fail_malformed_htlc` error using the 2413 `invalid_onion_blinding` failure code for any local or downstream errors. 2414 - SHOULD use the `sha256_of_onion` of the onion it received. 2415 - MAY use an all zero `sha256_of_onion`. 2416 - When supporting `option_attribution_data`: 2417 - if `path_key` is not set in the incoming `update_add_htlc`: 2418 - MUST initialize `attribution_data` and include it in `update_fail_htlc` and `update_fulfill_htlc`. See [BOLT04](04-onion-routing.md). 2419 2420 A receiving node: 2421 - if the `id` does not correspond to an HTLC in its current commitment transaction: 2422 - MUST send a `warning` and close the connection, or send an 2423 `error` and fail the channel. 2424 - if the `payment_preimage` value in `update_fulfill_htlc` 2425 doesn't SHA256 hash to the corresponding HTLC `payment_hash`: 2426 - MUST send a `warning` and close the connection, or send an 2427 `error` and fail the channel. 2428 - if the `BADONION` bit in `failure_code` is not set for 2429 `update_fail_malformed_htlc`: 2430 - MUST send a `warning` and close the connection, or send an 2431 `error` and fail the channel. 2432 - if the `sha256_of_onion` in `update_fail_malformed_htlc` doesn't match the 2433 onion it sent and is not all zero: 2434 - MAY retry or choose an alternate error response. 2435 - otherwise, a receiving node which has an outgoing HTLC canceled by `update_fail_malformed_htlc`: 2436 - MUST return an error in the `update_fail_htlc` sent to the link which 2437 originally sent the HTLC, using the `failure_code` given and setting the 2438 data to `sha256_of_onion`. 2439 2440 #### Rationale 2441 2442 A node that doesn't time out HTLCs risks channel failure (see 2443 [`cltv_expiry_delta` Selection](#cltv_expiry_delta-selection)). 2444 2445 A node that sends `update_fulfill_htlc`, before the sender, is also 2446 committed to the HTLC and risks losing funds. 2447 2448 If the onion is malformed, the upstream node won't be able to extract 2449 the shared key to generate a response — hence the special failure message, which 2450 makes this node do it. 2451 2452 The node can check that the SHA256 that the upstream is complaining about 2453 does match the onion it sent, which may allow it to detect random bit 2454 errors. However, without re-checking the actual encrypted packet sent, 2455 it won't know whether the error was its own or the remote's; so 2456 such detection is left as an option. 2457 2458 Nodes inside a blinded route must use `invalid_onion_blinding` to avoid 2459 leaking information to senders trying to probe the blinded route. 2460 2461 ### Committing Updates So Far: `commitment_signed` 2462 2463 When a node has changes for the remote commitment, it can apply them, 2464 sign the resulting transaction (as defined in [BOLT #3](03-transactions.md)), and send a 2465 `commitment_signed` message. 2466 2467 1. type: 132 (`commitment_signed`) 2468 2. data: 2469 * [`channel_id`:`channel_id`] 2470 * [`signature`:`signature`] 2471 * [`u16`:`num_htlcs`] 2472 * [`num_htlcs*signature`:`htlc_signature`] 2473 2474 #### Requirements 2475 2476 A sending node: 2477 - MUST NOT send a `commitment_signed` message that does not include any 2478 updates. 2479 - MAY send a `commitment_signed` message that only 2480 alters the fee. 2481 - MAY send a `commitment_signed` message that doesn't 2482 change the commitment transaction aside from the new revocation number 2483 (due to dust, identical HTLC replacement, or insignificant or multiple 2484 fee changes). 2485 - MUST include one `htlc_signature` for every HTLC transaction corresponding 2486 to the ordering of the commitment transaction (see [BOLT #3](03-transactions.md#transaction-input-and-output-ordering)). 2487 - if it has not recently received a message from the remote node: 2488 - SHOULD use `ping` and await the reply `pong` before sending `commitment_signed`. 2489 2490 A receiving node: 2491 - once all pending updates are applied: 2492 - if `signature` is not valid for its local commitment transaction OR non-compliant with LOW-S-standard rule <sup>[LOWS](https://github.com/bitcoin/bitcoin/pull/6769)</sup>: 2493 - MUST send a `warning` and close the connection, or send an 2494 `error` and fail the channel. 2495 - if `num_htlcs` is not equal to the number of HTLC outputs in the local 2496 commitment transaction: 2497 - MUST send a `warning` and close the connection, or send an 2498 `error` and fail the channel. 2499 - if any `htlc_signature` is not valid for the corresponding HTLC transaction OR non-compliant with LOW-S-standard rule <sup>[LOWS](https://github.com/bitcoin/bitcoin/pull/6769)</sup>: 2500 - MUST send a `warning` and close the connection, or send an 2501 `error` and fail the channel. 2502 - MUST respond with a `revoke_and_ack` message. 2503 2504 #### Rationale 2505 2506 There's little point offering spam updates: it implies a bug. 2507 2508 The `num_htlcs` field is redundant, but makes the packet length check fully self-contained. 2509 2510 The recommendation to require recent messages recognizes the reality 2511 that networks are unreliable: nodes might not realize their peers are 2512 offline until after sending `commitment_signed`. Once 2513 `commitment_signed` is sent, the sender considers itself bound to 2514 those HTLCs, and cannot fail the related incoming HTLCs until the 2515 output HTLCs are fully resolved. 2516 2517 Note that the `htlc_signature` implicitly enforces the time-lock mechanism in 2518 the case of offered HTLCs being timed out or received HTLCs being spent. This 2519 is done to reduce fees by creating smaller scripts compared to explicitly 2520 stating time-locks on HTLC outputs. 2521 2522 The `option_anchors` allows HTLC transactions to "bring their own fees" by 2523 attaching other inputs and outputs, hence the modified signature flags. 2524 2525 ### Completing the Transition to the Updated State: `revoke_and_ack` 2526 2527 Once the recipient of `commitment_signed` checks the signature and knows 2528 it has a valid new commitment transaction, it replies with the commitment 2529 preimage for the previous commitment transaction in a `revoke_and_ack` 2530 message. 2531 2532 This message also implicitly serves as an acknowledgment of receipt 2533 of the `commitment_signed`, so this is a logical time for the `commitment_signed` sender 2534 to apply (to its own commitment) any pending updates it sent before 2535 that `commitment_signed`. 2536 2537 The description of key derivation is in [BOLT #3](03-transactions.md#key-derivation). 2538 2539 1. type: 133 (`revoke_and_ack`) 2540 2. data: 2541 * [`channel_id`:`channel_id`] 2542 * [`32*byte`:`per_commitment_secret`] 2543 * [`point`:`next_per_commitment_point`] 2544 2545 #### Requirements 2546 2547 A sending node: 2548 - MUST set `per_commitment_secret` to the secret used to generate keys for 2549 the previous commitment transaction. 2550 - MUST set `next_per_commitment_point` to the values for its next commitment 2551 transaction. 2552 2553 A receiving node: 2554 - if `per_commitment_secret` is not a valid secret key or does not generate the previous `per_commitment_point`: 2555 - MUST send an `error` and fail the channel. 2556 - if the `per_commitment_secret` was not generated by the protocol in [BOLT #3](03-transactions.md#per-commitment-secret-requirements): 2557 - MAY send a `warning` and close the connection, or send an 2558 `error` and fail the channel. 2559 2560 A node: 2561 - MUST NOT broadcast old (revoked) commitment transactions, 2562 - Note: doing so will allow the other node to seize all channel funds. 2563 - SHOULD NOT sign commitment transactions, unless it's about to broadcast 2564 them (due to a failed connection), 2565 - Note: this is to reduce the above risk. 2566 2567 ### Updating Fees: `update_fee` 2568 2569 An `update_fee` message is sent by the node which is paying the 2570 Bitcoin fee. Like any update, it's first committed to the receiver's 2571 commitment transaction and then (once acknowledged) committed to the 2572 sender's. Unlike an HTLC, `update_fee` is never closed but simply 2573 replaced. 2574 2575 There is a possibility of a race, as the recipient can add new HTLCs 2576 before it receives the `update_fee`. Under this circumstance, the sender may 2577 not be able to afford the fee on its own commitment transaction, once the `update_fee` 2578 is finally acknowledged by the recipient. In this case, the fee will be less 2579 than the fee rate, as described in [BOLT #3](03-transactions.md#fee-payment). 2580 2581 The exact calculation used for deriving the fee from the fee rate is 2582 given in [BOLT #3](03-transactions.md#fee-calculation). 2583 2584 1. type: 134 (`update_fee`) 2585 2. data: 2586 * [`channel_id`:`channel_id`] 2587 * [`u32`:`feerate_per_kw`] 2588 2589 #### Requirements 2590 2591 The node _responsible_ for paying the Bitcoin fee: 2592 - SHOULD send `update_fee` to ensure the current fee rate is sufficient (by a 2593 significant margin) for timely processing of the commitment transaction. 2594 2595 The node _not responsible_ for paying the Bitcoin fee: 2596 - MUST NOT send `update_fee`. 2597 2598 A sending node: 2599 - if `option_anchors` was not negotiated: 2600 - if the `update_fee` increases `feerate_per_kw`: 2601 - if the dust balance of the remote transaction at the updated `feerate_per_kw` is greater than `max_dust_htlc_exposure_msat`: 2602 - MAY NOT send `update_fee` 2603 - MAY fail the channel 2604 - if the dust balance of the local transaction at the updated `feerate_per_kw` is greater than `max_dust_htlc_exposure_msat`: 2605 - MAY NOT send `update_fee` 2606 - MAY fail the channel 2607 2608 A receiving node: 2609 - if the `update_fee` is too low for timely processing, OR is unreasonably large: 2610 - MUST send a `warning` and close the connection, or send an 2611 `error` and fail the channel. 2612 - if the sender is not responsible for paying the Bitcoin fee: 2613 - MUST send a `warning` and close the connection, or send an 2614 `error` and fail the channel. 2615 - if the sender cannot afford the new fee rate on the receiving node's 2616 current commitment transaction: 2617 - SHOULD send a `warning` and close the connection, or send an 2618 `error` and fail the channel. 2619 - but MAY delay this check until the `update_fee` is committed. 2620 - if `option_anchors` was not negotiated: 2621 - if the `update_fee` increases `feerate_per_kw`: 2622 - if the dust balance of the remote transaction at the updated `feerate_per_kw` is greater then `max_dust_htlc_exposure_msat`: 2623 - MAY fail the channel 2624 - if the dust balance of the local transaction at the updated `feerate_per_kw` is greater than `max_dust_htlc_exposure_msat`: 2625 - MAY fail the channel 2626 2627 #### Rationale 2628 2629 Bitcoin fees are required for unilateral closes to be effective. 2630 With `option_anchors`, `feerate_per_kw` is not as critical anymore to guarantee 2631 confirmation as it was in the legacy commitment format, but it still needs to 2632 be enough to be able to enter the mempool (satisfy min relay fee and mempool 2633 min fee). 2634 2635 For the legacy commitment format, there is no general method for the 2636 broadcasting node to use child-pays-for-parent to increase its effective fee. 2637 2638 Given the variance in fees, and the fact that the transaction may be 2639 spent in the future, it's a good idea for the fee payer to keep a good 2640 margin (say 5x the expected fee requirement) for legacy commitment txes; but, due to differing methods of 2641 fee estimation, an exact value is not specified. 2642 2643 Since the fees are currently one-sided (the party which requested the 2644 channel creation always pays the fees for the commitment transaction), 2645 it's simplest to only allow it to set fee levels; however, as the same 2646 fee rate applies to HTLC transactions, the receiving node must also 2647 care about the reasonableness of the fee. 2648 2649 If on-chain fees increase while commitments contain many HTLCs that will 2650 be trimmed at the updated feerate, this could overflow the configured 2651 `max_dust_htlc_exposure_msat`. Whether to close the channel preemptively 2652 or not is left as a matter of node policy. 2653 2654 ## Message Retransmission 2655 2656 Because communication transports are unreliable, and may need to be 2657 re-established from time to time, the design of the transport has been 2658 explicitly separated from the protocol. 2659 2660 Nonetheless, it's assumed our transport is ordered and reliable. 2661 Reconnection introduces doubt as to what has been received, so there are 2662 explicit acknowledgments at that point. 2663 2664 This is fairly straightforward in the case of channel establishment 2665 and close, where messages have an explicit order, but during normal 2666 operation, acknowledgments of updates are delayed until the 2667 `commitment_signed` / `revoke_and_ack` exchange; so it cannot be assumed 2668 that the updates have been received. This also means that the receiving 2669 node only needs to store updates upon receipt of `commitment_signed`. 2670 2671 Note that messages described in [BOLT #7](07-routing-gossip.md) are 2672 independent of particular channels; their transmission requirements 2673 are covered there, and besides being transmitted after `init` (as all 2674 messages are), they are independent of requirements here. 2675 2676 1. type: 136 (`channel_reestablish`) 2677 2. data: 2678 * [`channel_id`:`channel_id`] 2679 * [`u64`:`next_commitment_number`] 2680 * [`u64`:`next_revocation_number`] 2681 * [`32*byte`:`your_last_per_commitment_secret`] 2682 * [`point`:`my_current_per_commitment_point`] 2683 2684 1. `tlv_stream`: `channel_reestablish_tlvs` 2685 2. types: 2686 1. type: 0 (`next_funding`) 2687 2. data: 2688 * [`sha256`:`next_funding_txid`] 2689 2690 `next_commitment_number`: A commitment number is a 48-bit 2691 incrementing counter for each commitment transaction; counters 2692 are independent for each peer in the channel and start at 0. 2693 They're only explicitly relayed to the other node in the case of 2694 re-establishment, otherwise they are implicit. 2695 2696 ### Requirements 2697 2698 A funding node: 2699 - upon disconnection: 2700 - if it has broadcast the funding transaction: 2701 - MUST remember the channel for reconnection. 2702 - otherwise: 2703 - SHOULD NOT remember the channel for reconnection. 2704 2705 A non-funding node: 2706 - upon disconnection: 2707 - if it has sent the `funding_signed` message: 2708 - MUST remember the channel for reconnection. 2709 - otherwise: 2710 - SHOULD NOT remember the channel for reconnection. 2711 2712 A node: 2713 - MUST handle continuation of a previous channel on a new encrypted transport. 2714 - upon disconnection: 2715 - MUST reverse any uncommitted updates sent by the other side (i.e. all 2716 messages beginning with `update_` for which no `commitment_signed` has 2717 been received). 2718 - Note: a node MAY have already used the `payment_preimage` value from 2719 the `update_fulfill_htlc`, so the effects of `update_fulfill_htlc` are not 2720 completely reversed. 2721 - upon reconnection: 2722 - if a channel is in an error state: 2723 - SHOULD retransmit the error packet and ignore any other packets for 2724 that channel. 2725 - otherwise: 2726 - MUST transmit `channel_reestablish` for each channel. 2727 - MUST wait to receive the other node's `channel_reestablish` 2728 message before sending any other messages for that channel. 2729 2730 The sending node: 2731 - MUST set `next_commitment_number` to the commitment number of the 2732 next `commitment_signed` it expects to receive. 2733 - MUST set `next_revocation_number` to the commitment number of the 2734 next `revoke_and_ack` message it expects to receive. 2735 - MUST set `my_current_per_commitment_point` to a valid point. 2736 - if `next_revocation_number` equals 0: 2737 - MUST set `your_last_per_commitment_secret` to all zeroes 2738 - otherwise: 2739 - MUST set `your_last_per_commitment_secret` to the last `per_commitment_secret` it received 2740 - if it has sent `commitment_signed` for an interactive transaction construction but 2741 it has not received `tx_signatures`: 2742 - MUST set `next_funding_txid` to the txid of that interactive transaction. 2743 - otherwise: 2744 - MUST NOT set `next_funding_txid`. 2745 2746 A node: 2747 - if `next_commitment_number` is 1 in both the `channel_reestablish` it 2748 sent and received: 2749 - MUST retransmit `channel_ready`. 2750 - otherwise: 2751 - MUST NOT retransmit `channel_ready`, but MAY send `channel_ready` with 2752 a different `short_channel_id` `alias` field. 2753 - upon reconnection: 2754 - MUST ignore any redundant `channel_ready` it receives. 2755 - if `next_commitment_number` is equal to the commitment number of 2756 the last `commitment_signed` message the receiving node has sent: 2757 - MUST reuse the same commitment number for its next `commitment_signed`. 2758 - otherwise: 2759 - if `next_commitment_number` is not 1 greater than the 2760 commitment number of the last `commitment_signed` message the receiving 2761 node has sent: 2762 - SHOULD send an `error` and fail the channel. 2763 - if it has not sent `commitment_signed`, AND `next_commitment_number` 2764 is not equal to 1: 2765 - SHOULD send an `error` and fail the channel. 2766 - if `next_revocation_number` is equal to the commitment number of 2767 the last `revoke_and_ack` the receiving node sent, AND the receiving node 2768 hasn't already received a `closing_signed`: 2769 - MUST re-send the `revoke_and_ack`. 2770 - if it has previously sent a `commitment_signed` that needs to be 2771 retransmitted: 2772 - MUST retransmit `revoke_and_ack` and `commitment_signed` in the same 2773 relative order as initially transmitted. 2774 - otherwise: 2775 - if `next_revocation_number` is not equal to 1 greater than the 2776 commitment number of the last `revoke_and_ack` the receiving node has sent: 2777 - SHOULD send an `error` and fail the channel. 2778 - if it has not sent `revoke_and_ack`, AND `next_revocation_number` 2779 is not equal to 0: 2780 - SHOULD send an `error` and fail the channel. 2781 2782 A receiving node: 2783 - MUST ignore `my_current_per_commitment_point`, but MAY require it to be a valid point. 2784 - if `next_revocation_number` is greater than expected above, AND 2785 `your_last_per_commitment_secret` is correct for that 2786 `next_revocation_number` minus 1: 2787 - MUST NOT broadcast its commitment transaction. 2788 - SHOULD send an `error` to request the peer to fail the channel. 2789 - otherwise: 2790 - if `your_last_per_commitment_secret` does not match the expected values: 2791 - SHOULD send an `error` and fail the channel. 2792 2793 A receiving node: 2794 - if `next_funding_txid` is set: 2795 - if `next_funding_txid` matches the latest interactive funding transaction: 2796 - if it has not received `tx_signatures` for that funding transaction: 2797 - MUST retransmit its `commitment_signed` for that funding transaction. 2798 - if it has already received `commitment_signed` and it should sign first, 2799 as specified in the [`tx_signatures` requirements](#the-tx_signatures-message): 2800 - MUST send its `tx_signatures` for that funding transaction. 2801 - if it has already received `tx_signatures` for that funding transaction: 2802 - MUST send its `tx_signatures` for that funding transaction. 2803 - otherwise: 2804 - MUST send `tx_abort` to let the sending node know that they can forget 2805 this funding transaction. 2806 2807 A node: 2808 - MUST NOT assume that previously-transmitted messages were lost, 2809 - if it has sent a previous `commitment_signed` message: 2810 - MUST handle the case where the corresponding commitment transaction is 2811 broadcast at any time by the other side, 2812 - Note: this is particularly important if the node does not simply 2813 retransmit the exact `update_` messages as previously sent. 2814 - upon reconnection: 2815 - if it has sent a previous `shutdown`: 2816 - MUST retransmit `shutdown`. 2817 2818 ### Rationale 2819 2820 The requirements above ensure that the opening phase is nearly 2821 atomic: if it doesn't complete, it starts again. The only exception 2822 is if the `funding_signed` message is sent but not received. In 2823 this case, the funder will forget the channel, and presumably open 2824 a new one upon reconnection; meanwhile, the other node will eventually forget 2825 the original channel, due to never receiving `channel_ready` or seeing 2826 the funding transaction on-chain. 2827 2828 There's no acknowledgment for `error`, so if a reconnect occurs it's 2829 polite to retransmit before disconnecting again; however, it's not a MUST, 2830 because there are also occasions where a node can simply forget the 2831 channel altogether. 2832 2833 `closing_signed` also has no acknowledgment so must be retransmitted 2834 upon reconnection (though negotiation restarts on reconnection, so it needs 2835 not be an exact retransmission). 2836 The only acknowledgment for `shutdown` is `closing_signed`, so one or the other 2837 needs to be retransmitted. 2838 2839 The handling of updates is similarly atomic: if the commit is not 2840 acknowledged (or wasn't sent) the updates are re-sent. However, it's not 2841 insisted they be identical: they could be in a different order, 2842 involve different fees, or even be missing HTLCs which are now too old 2843 to be added. Requiring they be identical would effectively mean a 2844 write to disk by the sender upon each transmission, whereas the scheme 2845 here encourages a single persistent write to disk for each 2846 `commitment_signed` sent or received. But if you need to retransmit both a 2847 `commitment_signed` and a `revoke_and_ack`, the relative order of these two 2848 must be preserved, otherwise it will lead to a channel closure. 2849 2850 A re-transmittal of `revoke_and_ack` should never be asked for after a 2851 `closing_signed` has been received, since that would imply a shutdown has been 2852 completed — which can only occur after the `revoke_and_ack` has been received 2853 by the remote node. 2854 2855 Note that the `next_commitment_number` starts at 1, since 2856 commitment number 0 is created during opening. 2857 `next_revocation_number` will be 0 until the 2858 `commitment_signed` for commitment number 1 is send and then 2859 the revocation for commitment number 0 is received. 2860 2861 `channel_ready` is implicitly acknowledged by the start of normal 2862 operation, which is known to have begun after a `commitment_signed` has been 2863 received — hence, the test for a `next_commitment_number` greater 2864 than 1. 2865 2866 A previous draft insisted that the funder "MUST remember ...if it has 2867 broadcast the funding transaction, otherwise it MUST NOT": this was in 2868 fact an impossible requirement. A node must either firstly commit to 2869 disk and secondly broadcast the transaction or vice versa. The new 2870 language reflects this reality: it's surely better to remember a 2871 channel which hasn't been broadcast than to forget one which has! 2872 Similarly, for the fundee's `funding_signed` message: it's better to 2873 remember a channel that never opens (and times out) than to let the 2874 funder open it while the fundee has forgotten it. 2875 2876 A node, which has somehow fallen 2877 behind (e.g. has been restored from old backup), can detect that it has fallen 2878 behind. A fallen-behind node must know it cannot broadcast its current 2879 commitment transaction — which would lead to total loss of funds — as the 2880 remote node can prove it knows the revocation preimage. The `error` returned by 2881 the fallen-behind node should make the other node drop its current commitment 2882 transaction to the chain. The other node should wait for that `error` to give 2883 the fallen-behind node an opportunity to fix its state first (e.g by restarting 2884 with a different backup). 2885 2886 `next_funding_txid` allows peers to finalize the signing steps of an 2887 interactive transaction construction, or safely abort that transaction 2888 if it was not signed by one of the peers, who has thus already removed 2889 it from its state. 2890 2891 # Authors 2892 2893 [ FIXME: Insert Author List ] 2894 2895  2896 <br> 2897 This work is licensed under a [Creative Commons Attribution 4.0 International License](http://creativecommons.org/licenses/by/4.0/).