-
Notifications
You must be signed in to change notification settings - Fork 497
/
Copy path02-peer-protocol.md
2623 lines (2136 loc) · 118 KB
/
02-peer-protocol.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# BOLT #2: Peer Protocol for Channel Management
The peer channel protocol has three phases: establishment, normal
operation, and closing.
# Table of Contents
* [Channel](#channel)
* [Definition of `channel_id`](#definition-of-channel_id)
* [Interactive Transaction Construction](#interactive-transaction-construction)
* [Set-Up and Vocabulary](#set-up-and-vocabulary)
* [Fee Responsibility](#fee-responsibility)
* [Overview](#overview)
* [The `tx_add_input` Message](#the-tx_add_input-message)
* [The `tx_add_output` Message](#the-tx_add_output-message)
* [The `tx_remove_input` and `tx_remove_output` Messages](#the-tx_remove_input-and-tx_remove_output-messages)
* [The `tx_complete` Message](#the-tx_complete-message)
* [The `tx_signatures` Message](#the-tx_signatures-message)
* [The `tx_init_rbf` Message](#the-tx_init_rbf-message)
* [The `tx_ack_rbf` Message](#the-tx_ack_rbf-message)
* [The `tx_abort` Message](#the-tx_abort-message)
* [Channel Establishment v1](#channel-establishment-v1)
* [The `open_channel` Message](#the-open_channel-message)
* [The `accept_channel` Message](#the-accept_channel-message)
* [The `funding_created` Message](#the-funding_created-message)
* [The `funding_signed` Message](#the-funding_signed-message)
* [The `channel_ready` Message](#the-channel_ready-message)
* [Channel Establishment v2](#channel-establishment-v2)
* [The `open_channel2` Message](#the-open_channel2-message)
* [The `accept_channel2` Message](#the-accept_channel2-message)
* [Funding Composition](#funding-composition)
* [The `commitment_signed` Message](#the-commitment_signed-message)
* [Sharing funding signatures: `tx_signatures`](#sharing-funding-signatures-tx_signatures)
* [Fee bumping: `tx_init_rbf` and `tx_ack_rbf`](#fee-bumping-tx_init_rbf-and-tx_ack_rbf)
* [Channel Quiescence](#channel-quiescence)
* [Channel Close](#channel-close)
* [Closing Initiation: `shutdown`](#closing-initiation-shutdown)
* [Closing Negotiation: `closing_signed`](#closing-negotiation-closing_signed)
* [Normal Operation](#normal-operation)
* [Forwarding HTLCs](#forwarding-htlcs)
* [`cltv_expiry_delta` Selection](#cltv_expiry_delta-selection)
* [Adding an HTLC: `update_add_htlc`](#adding-an-htlc-update_add_htlc)
* [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)
* [Committing Updates So Far: `commitment_signed`](#committing-updates-so-far-commitment_signed)
* [Completing the Transition to the Updated State: `revoke_and_ack`](#completing-the-transition-to-the-updated-state-revoke_and_ack)
* [Updating Fees: `update_fee`](#updating-fees-update_fee)
* [Message Retransmission: `channel_reestablish` message](#message-retransmission)
* [Authors](#authors)
# Channel
## Definition of `channel_id`
Some messages use a `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).
Prior to channel establishment, a `temporary_channel_id` is used,
which is a random nonce.
Note that as duplicate `temporary_channel_id`s may exist from different
peers, APIs which reference channels by their channel id before the funding
transaction is created are inherently unsafe. The only protocol-provided
identifier for a channel before funding_created has been exchanged is the
(source_node_id, destination_node_id, temporary_channel_id) tuple. Note that
any such APIs which reference channels by their channel id before the funding
transaction is confirmed are also not persistent - until you know the script
pubkey corresponding to the funding output nothing prevents duplicative channel
ids.
### `channel_id`, v2
For channels established using the v2 protocol, the `channel_id` is the
`SHA256(lesser-revocation-basepoint || greater-revocation-basepoint)`,
where the lesser and greater is based off the order of the basepoint.
When sending `open_channel2`, the peer's revocation basepoint is unknown.
A `temporary_channel_id` must be computed by using a zeroed out basepoint
for the non-initiator.
When sending `accept_channel2`, the `temporary_channel_id` from `open_channel2`
must be used, to allow the initiator to match the response to its request.
#### Rationale
The revocation basepoints must be remembered by both peers for correct
operation anyway. They're known after the first exchange of messages,
obviating the need for a `temporary_channel_id` in subsequent messages.
By mixing information from both sides, they avoid `channel_id` collisions,
and they remove the dependency on the funding txid.
## Interactive Transaction Construction
Interactive transaction construction allows two peers to collaboratively
build a transaction for broadcast. This protocol is the foundation
for dual-funded channels establishment (v2).
### Set-Up and Vocabulary
There are two parties to a transaction construction: an *initiator*
and a *non-initiator*.
The *initiator* is the peer which initiates the protocol, e.g.
for channel establishment v2 the *initiator* would be the peer which
sends `open_channel2`.
The protocol makes the following assumptions:
- The `feerate` for the transaction is known.
- The `dust_limit` for the transaction is known.
- The `nLocktime` for the transaction is known.
- The `nVersion` for the transaction is known.
### Fee Responsibility
The *initiator* is responsible for paying the fees for the following fields,
to be referred to as the `common fields`.
- version
- segwit marker + flag
- input count
- output count
- locktime
The rest of the transaction bytes' fees are the responsibility of
the peer who contributed that input or output via `tx_add_input` or
`tx_add_output`, at the agreed upon `feerate`.
### Overview
The *initiator* initiates the interactive transaction construction
protocol with `tx_add_input`. The *non-initiator* responds with any
of `tx_add_input`, `tx_add_output`, `tx_remove_input`, `tx_remove_output`, or
`tx_complete`. The protocol continues with the synchronous exchange
of interactive transaction protocol messages until both nodes have sent
and received a consecutive `tx_complete`. This is a turn-based protocol.
Once peers have exchanged consecutive `tx_complete`s, the
interactive transaction construction protocol is considered concluded.
Both peers should construct the transaction and fail the negotiation
if an error is discovered.
This protocol is expressly designed to allow for parallel, multi-party
sessions to collectively construct a single transaction. This preserves
the ability to open multiple channels in a single transaction. While
`serial_id`s are generally chosen randomly, to maintain consistent transaction
ordering across all peer sessions, it is simplest to reuse received
`serial_id`s when forwarding them to other peers, inverting the bottom bit as
necessary to satisfy the parity requirement.
Here are a few example exchanges.
#### *initiator* only
A, the *initiator*, has two inputs and an output (the funding output).
B, the *non-initiator* has nothing to contribute.
+-------+ +-------+
| |--(1)- tx_add_input -->| |
| |<-(2)- tx_complete ----| |
| |--(3)- tx_add_input -->| |
| A |<-(4)- tx_complete ----| B |
| |--(5)- tx_add_output ->| |
| |<-(6)- tx_complete ----| |
| |--(7)- tx_complete --->| |
+-------+ +-------+
#### *initiator* and *non-initiator*
A, the *initiator*, contributes 2 inputs and an output that they
then remove. B, the *non-initiator*, contributes 1 input and an output,
but waits until A adds a second input before contributing.
Note that if A does not send a second input, the negotiation will end without
B's contributions.
+-------+ +-------+
| |--(1)- tx_add_input ---->| |
| |<-(2)- tx_complete ------| |
| |--(3)- tx_add_output --->| |
| |<-(4)- tx_complete ------| |
| |--(5)- tx_add_input ---->| |
| A |<-(6)- tx_add_input -----| B |
| |--(7)- tx_remove_output >| |
| |<-(8)- tx_add_output ----| |
| |--(9)- tx_complete ----->| |
| |<-(10) tx_complete ------| |
+-------+ +-------+
### The `tx_add_input` Message
This message contains a transaction input.
1. type: 66 (`tx_add_input`)
2. data:
* [`channel_id`:`channel_id`]
* [`u64`:`serial_id`]
* [`u16`:`prevtx_len`]
* [`prevtx_len*byte`:`prevtx`]
* [`u32`:`prevtx_vout`]
* [`u32`:`sequence`]
#### Requirements
The sending node:
- MUST add all sent inputs to the transaction
- MUST use a unique `serial_id` for each input currently added to the
transaction
- MUST set `sequence` to be less than or equal to 4294967293 (`0xFFFFFFFD`)
- MUST NOT re-transmit inputs it has received from the peer
- if is the *initiator*:
- MUST send even `serial_id`s
- if is the *non-initiator*:
- MUST send odd `serial_id`s
The receiving node:
- MUST add all received inputs to the transaction
- MUST fail the negotiation if:
- `sequence` is set to `0xFFFFFFFE` or `0xFFFFFFFF`
- the `prevtx` and `prevtx_vout` are identical to a previously added
(and not removed) input's
- `prevtx` is not a valid transaction
- `prevtx_vout` is greater or equal to the number of outputs on `prevtx`
- 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
- the `serial_id` is already included in the transaction
- the `serial_id` has the wrong parity
- if has received 4096 `tx_add_input` messages during this negotiation
#### Rationale
Each node must know the set of the transaction inputs. The *non-initiator*
MAY omit this message.
`serial_id` is a randomly chosen number which uniquely identifies this input.
Inputs in the constructed transaction MUST be sorted by `serial_id`.
`prevtx` is the serialized transaction that contains the output
this input spends. Used to verify that the input is non-malleable.
`prevtx_vout` is the index of the output being spent.
`sequence` is the sequence number of this input: it must signal
replaceability, and the same value should be used across implementations
to avoid on-chain fingerprinting.
#### Liquidity griefing
When sending `tx_add_input`, senders have no guarantee that the remote node
will complete the protocol in a timely manner. Malicious remote nodes could
delay messages or stop responding, which can result in a partially created
transaction that cannot be broadcast by the honest node. If the honest node
is locking the corresponding UTXO exclusively for this remote node, this can
be exploited to lock up the honest node's liquidity.
It is thus recommended that implementations keep UTXOs unlocked and actively
reuse them in concurrent sessions, which guarantees that transactions created
with honest nodes double-spend pending transactions with malicious nodes at
no additional cost for the honest node.
Unfortunately, this will also create conflicts between concurrent sessions
with honest nodes. This is a reasonable trade-off though because:
* on-chain funding attempts are relatively infrequent operations
* honest nodes should complete the protocol quickly, reducing the risk of
conflicts
* failed attempts can simply be retried at no cost
### The `tx_add_output` Message
This message adds a transaction output.
1. type: 67 (`tx_add_output`)
2. data:
* [`channel_id`:`channel_id`]
* [`u64`:`serial_id`]
* [`u64`:`sats`]
* [`u16`:`scriptlen`]
* [`scriptlen*byte`:`script`]
#### Requirements
Either node:
- MAY omit this message
The sending node:
- MUST add all sent outputs to the transaction
- if is the *initiator*:
- MUST send even `serial_id`s
- if is the *non-initiator*:
- MUST send odd `serial_id`s
The receiving node:
- MUST add all received outputs to the transaction
- MUST accept P2WSH, P2WPKH, P2TR `script`s
- MAY fail the negotiation if `script` is non-standard
- MUST fail the negotiation if:
- the `serial_id` is already included in the transaction
- the `serial_id` has the wrong parity
- it has received 4096 `tx_add_output` messages during this negotiation
- the `sats` amount is less than the `dust_limit`
- the `sats` amount is greater than 2,100,000,000,000,000 (`MAX_MONEY`)
#### Rationale
Each node must know the set of the transaction outputs.
`serial_id` is a randomly chosen number which uniquely identifies this output.
Outputs in the constructed transaction MUST be sorted by `serial_id`.
`sats` is the satoshi value of the output.
`script` is the scriptPubKey for the output (with its length omitted).
`script`s are not required to follow standardness rules: non-standard
scripts such as `OP_RETURN` may be accepted, but the corresponding
transaction may fail to relay across the network.
### The `tx_remove_input` and `tx_remove_output` Messages
This message removes an input from the transaction.
1. type: 68 (`tx_remove_input`)
2. data:
* [`channel_id`:`channel_id`]
* [`u64`:`serial_id`]
This message removes an output from the transaction.
1. type: 69 (`tx_remove_output`)
2. data:
* [`channel_id`:`channel_id`]
* [`u64`:`serial_id`]
#### Requirements
The sending node:
- MUST NOT send a `tx_remove` with a `serial_id` it did not add
to the transaction or has already been removed
The receiving node:
- MUST remove the indicated input or output from the transaction
- MUST fail the negotiation if:
- the input or output identified by the `serial_id` was not added by the
sender
- the `serial_id` does not correspond to a currently added input (or output)
### The `tx_complete` Message
This message signals the conclusion of a peer's transaction
contributions.
1. type: 70 (`tx_complete`)
2. data:
* [`channel_id`:`channel_id`]
#### Requirements
The nodes:
- MUST send this message in succession to conclude this protocol
The receiving node:
- MUST use the negotiated inputs and outputs to construct a transaction
- MUST fail the negotiation if:
- the peer's total input satoshis is less than their outputs. One MUST
account for the peer's portion of the funding output when verifying
compliance with this requirement.
- the peer's paid feerate does not meet or exceed the agreed `feerate`
(based on the `minimum fee`).
- if is the *non-initiator*:
- the *initiator*'s fees do not cover the `common` fields
- there are more than 252 inputs
- there are more than 252 outputs
- the estimated weight of the tx is greater than 400,000 (`MAX_STANDARD_TX_WEIGHT`)
#### Rationale
To signal the conclusion of exchange of transaction inputs and outputs.
Upon successful exchange of `tx_complete` messages, both nodes
should construct the transaction and proceed to the next portion of the
protocol. For channel establishment v2, exchanging commitment transactions.
For the `minimum fee` calculation see [BOLT #3](03-transactions.md#calculating-fees-for-collaborative-transaction-construction).
The maximum inputs and outputs are capped at 252. This effectively fixes
the byte size of the input and output counts on the transaction to one (1).
### The `tx_signatures` Message
1. type: 71 (`tx_signatures`)
2. data:
* [`channel_id`:`channel_id`]
* [`sha256`:`txid`]
* [`u16`:`num_witnesses`]
* [`num_witnesses*witness`:`witnesses`]
1. subtype: `witness`
2. data:
* [`u16`:`len`]
* [`len*byte`:`witness_data`]
#### Requirements
The sending node:
- if it has the lowest total satoshis contributed, as defined by total
`tx_add_input` values, or both peers have contributed equal amounts
but it has the lowest `node_id` (sorted lexicographically):
- MUST transmit their `tx_signatures` first
- MUST order the `witnesses` by the `serial_id` of the input they
correspond to
- `num_witnesses`s MUST equal the number of inputs they added
- MUST use the `SIGHASH_ALL` (0x01) flag on each signature
The receiving node:
- MUST fail the negotiation if:
- the message contains an empty `witness`
- the number of `witnesses` does not equal the number of inputs
added by the sending node
- the `txid` does not match the txid of the transaction
- the `witnesses` are non-standard
- a signature uses a flag that is not `SIGHASH_ALL` (0x01)
- SHOULD apply the `witnesses` to the transaction and broadcast it
- MUST reply with their `tx_signatures` if not already transmitted
#### Rationale
A strict ordering is used to decide which peer sends `tx_signatures` first.
This prevents deadlocks where each peer is waiting for the other peer to
send `tx_signatures`, and enables multiparty tx collaboration.
The `witness_data` is encoded as per bitcoin's wire protocol (a CompactSize number
of elements, with each element a CompactSize length and that many bytes following).
While the `minimum fee` is calculated and verified at `tx_complete` conclusion,
it is possible for the fee for the exchanged witness data to be underpaid.
It is the responsibility of the sending peer to correctly account for the
required fee.
### The `tx_init_rbf` Message
This message initiates a replacement of the transaction after it's been
completed.
1. type: 72 (`tx_init_rbf`)
2. data:
* [`channel_id`:`channel_id`]
* [`u32`:`locktime`]
* [`u32`:`feerate`]
* [`tx_init_rbf_tlvs`:`tlvs`]
1. `tlv_stream`: `tx_init_rbf_tlvs`
2. types:
1. type: 0 (`funding_output_contribution`)
2. data:
* [`s64`:`satoshis`]
1. type: 2 (`require_confirmed_inputs`)
#### Requirements
The sender:
- MUST set `feerate` greater than or equal to 25/24 times the `feerate`
of the previously constructed transaction, rounded down.
- If it contributes to the transaction's funding output:
- MUST set `funding_output_contribution`
- If it requires the receiving node to only use confirmed inputs:
- MUST set `require_confirmed_inputs`
The recipient:
- MUST respond either with `tx_abort` or with `tx_ack_rbf`
- MUST respond with `tx_abort` if:
- the `feerate` is not greater than or equal to 25/24 times `feerate`
of the last successfully constructed transaction
- MAY send `tx_abort` for any reason
- MUST fail the negotiation if:
- `require_confirmed_inputs` is set but it cannot provide confirmed inputs
#### Rationale
`feerate` is the feerate this transaction will pay. It must be at least
1/24 greater than the last used `feerate`, rounded down to the nearest
satoshi to ensure there is progress.
E.g. if the last `feerate` was 520, the next sent `feerate` must be 541
(520 * 25 / 24 = 541.667, rounded down to 541).
If the previous transaction confirms in the middle of an RBF attempt,
the attempt MUST be abandoned.
`funding_output_contribution` is the amount of satoshis that this peer
will contribute to the funding output of the transaction, when there is
such an output. Note that it may be different from the contribution
made in the previously completed transaction. If omitted, the sender is
not contributing to the funding output.
### The `tx_ack_rbf` Message
1. type: 73 (`tx_ack_rbf`)
2. data:
* [`channel_id`:`channel_id`]
* [`tx_ack_rbf_tlvs`:`tlvs`]
1. `tlv_stream`: `tx_ack_rbf_tlvs`
2. types:
1. type: 0 (`funding_output_contribution`)
2. data:
* [`s64`:`satoshis`]
1. type: 2 (`require_confirmed_inputs`)
#### Requirements
The sender:
- If it contributes to the transaction's funding output:
- MUST set `funding_output_contribution`
- If it requires the receiving node to only use confirmed inputs:
- MUST set `require_confirmed_inputs`
The recipient:
- MUST respond with `tx_abort` or with a `tx_add_input` message,
restarting the interactive tx collaboration protocol.
- MUST fail the negotiation if:
- `require_confirmed_inputs` is set but it cannot provide confirmed inputs
#### Rationale
`funding_output_contribution` is the amount of satoshis that this peer
will contribute to the funding output of the transaction, when there is
such an output. Note that it may be different from the contribution
made in the previously completed transaction. If omitted, the sender is
not contributing to the funding output.
It's recommended that a peer, rather than fail the RBF negotiation due to
a large feerate change, instead stop contributing to the funding output,
and decline to participate further in the transaction (by not contributing,
they may obtain incoming liquidity at no cost).
### The `tx_abort` Message
1. type: 74 (`tx_abort`)
2. data:
* [`channel_id`:`channel_id`]
* [`u16`:`len`]
* [`len*byte`:`data`]
#### Requirements
A sending node:
- MUST NOT have already transmitted `tx_signatures`
- SHOULD forget the current negotiation and reset their state.
- MAY send an empty `data` field.
- when failure was caused by an invalid signature check:
- SHOULD include the raw, hex-encoded transaction in reply to a
`tx_signatures` or `commitment_signed` message.
A receiving node:
- if they have already sent `tx_signatures` to the peer:
- MUST NOT forget the channel until any inputs to the negotiated tx
have been spent.
- if they have not sent `tx_signatures`:
- SHOULD forget the current negotiation and reset their state.
- if they have not sent `tx_abort`:
- MUST echo back `tx_abort`
- if `data` is not composed solely of printable ASCII characters (For
reference: the printable character set includes byte values 32 through
126, inclusive):
- SHOULD NOT print out `data` verbatim.
#### Rationale
A receiving node, if they've already sent their `tx_signatures` has no guarantee
that the transaction won't be signed and published by their peer. They must remember
the transaction and channel (if appropriate) until the transaction is no longer
eligible to be spent (i.e. any input has been spent in a different transaction).
The `tx_abort` message allows for the cancellation of an in progress negotiation,
and a return to the initial starting state. It is distinct from the `error`
message, which triggers a channel close.
Echoing back `tx_abort` allows the peer to ack that they've seen the abort message,
permitting the originating peer to terminate the in-flight process without
worrying about stale messages.
## Channel Establishment v1
After authenticating and initializing a connection ([BOLT #8](08-transport.md)
and [BOLT #1](01-messaging.md#the-init-message), respectively), channel establishment may begin.
There are two pathways for establishing a channel, a legacy version presented here,
and a second version ([below](#channel-establishment-v2)). Which channel
establishment protocols are available for use is negotiated in the `init` message.
This consists of the funding node (funder) sending an `open_channel` message,
followed by the responding node (fundee) sending `accept_channel`. With the
channel parameters locked in, the funder is able to create the funding
transaction and both versions of the commitment transaction, as described in
[BOLT #3](03-transactions.md#bolt-3-bitcoin-transaction-and-script-formats).
The funder then sends the outpoint of the funding output with the `funding_created`
message, along with the signature for the fundee's version of the commitment
transaction. Once the fundee learns the funding outpoint, it's able to
generate the signature for the funder's version of the commitment transaction and send it
over using the `funding_signed` message.
Once the channel funder receives the `funding_signed` message, it
must broadcast the funding transaction to the Bitcoin network. After
the `funding_signed` message is sent/received, both sides should wait
for the funding transaction to enter the blockchain and reach the
specified depth (number of confirmations). After both sides have sent
the `channel_ready` message, the channel is established and can begin
normal operation. The `channel_ready` message includes information
that will be used to construct channel authentication proofs.
+-------+ +-------+
| |--(1)--- open_channel ----->| |
| |<-(2)-- accept_channel -----| |
| | | |
| A |--(3)-- funding_created --->| B |
| |<-(4)-- funding_signed -----| |
| | | |
| |--(5)--- channel_ready ---->| |
| |<-(6)--- channel_ready -----| |
+-------+ +-------+
- where node A is 'funder' and node B is 'fundee'
If this fails at any stage, or if one node decides the channel terms
offered by the other node are not suitable, the channel establishment
fails.
Note that multiple channels can operate in parallel, as all channel
messages are identified by either a `temporary_channel_id` (before the
funding transaction is created) or a `channel_id` (derived from the
funding transaction).
### The `open_channel` Message
This message contains information about a node and indicates its
desire to set up a new channel. This is the first step toward creating
the funding transaction and both versions of the commitment transaction.
1. type: 32 (`open_channel`)
2. data:
* [`chain_hash`:`chain_hash`]
* [`32*byte`:`temporary_channel_id`]
* [`u64`:`funding_satoshis`]
* [`u64`:`push_msat`]
* [`u64`:`dust_limit_satoshis`]
* [`u64`:`max_htlc_value_in_flight_msat`]
* [`u64`:`channel_reserve_satoshis`]
* [`u64`:`htlc_minimum_msat`]
* [`u32`:`feerate_per_kw`]
* [`u16`:`to_self_delay`]
* [`u16`:`max_accepted_htlcs`]
* [`point`:`funding_pubkey`]
* [`point`:`revocation_basepoint`]
* [`point`:`payment_basepoint`]
* [`point`:`delayed_payment_basepoint`]
* [`point`:`htlc_basepoint`]
* [`point`:`first_per_commitment_point`]
* [`byte`:`channel_flags`]
* [`open_channel_tlvs`:`tlvs`]
1. `tlv_stream`: `open_channel_tlvs`
2. types:
1. type: 0 (`upfront_shutdown_script`)
2. data:
* [`...*byte`:`shutdown_scriptpubkey`]
1. type: 1 (`channel_type`)
2. data:
* [`...*byte`:`type`]
The `chain_hash` value denotes the exact blockchain that the opened channel will
reside within. This is usually the genesis hash of the respective blockchain.
The existence of the `chain_hash` allows nodes to open channels
across many distinct blockchains as well as have channels within multiple
blockchains opened to the same peer (if it supports the target chains).
The `temporary_channel_id` is used to identify this channel on a per-peer basis until the
funding transaction is established, at which point it is replaced
by the `channel_id`, which is derived from the funding transaction.
`funding_satoshis` is the amount the sender is putting into the
channel. `push_msat` is an amount of initial funds that the sender is
unconditionally giving to the receiver. `dust_limit_satoshis` is the
threshold below which outputs should not be generated for this node's
commitment or HTLC transactions (i.e. HTLCs below this amount plus
HTLC transaction fees are not enforceable on-chain). This reflects the
reality that tiny outputs are not considered standard transactions and
will not propagate through the Bitcoin network. `channel_reserve_satoshis`
is the minimum amount that the other node is to keep as a direct
payment. `htlc_minimum_msat` indicates the smallest value HTLC this
node will accept.
`max_htlc_value_in_flight_msat` is a cap on total value of outstanding
HTLCs offered by the remote node, which allows the local node to limit its
exposure to HTLCs; similarly, `max_accepted_htlcs` limits the number of
outstanding HTLCs the remote node can offer.
`feerate_per_kw` indicates the initial fee rate in satoshi per 1000-weight
(i.e. 1/4 the more normally-used 'satoshi per 1000 vbytes') that this
side will pay for commitment and HTLC transactions, as described in
[BOLT #3](03-transactions.md#fee-calculation) (this can be adjusted
later with an `update_fee` message).
`to_self_delay` is the number of blocks that the other node's to-self
outputs must be delayed, using `OP_CHECKSEQUENCEVERIFY` delays; this
is how long it will have to wait in case of breakdown before redeeming
its own funds.
`funding_pubkey` is the public key in the 2-of-2 multisig script of
the funding transaction output.
The various `_basepoint` fields are used to derive unique
keys as described in [BOLT #3](03-transactions.md#key-derivation) for each commitment
transaction. Varying these keys ensures that the transaction ID of
each commitment transaction is unpredictable to an external observer,
even if one commitment transaction is seen; this property is very
useful for preserving privacy when outsourcing penalty transactions to
third parties.
`first_per_commitment_point` is the per-commitment point to be used
for the first commitment transaction,
Only the least-significant bit of `channel_flags` is currently
defined: `announce_channel`. This indicates whether the initiator of
the funding flow wishes to advertise this channel publicly to the
network, as detailed within [BOLT #7](07-routing-gossip.md#bolt-7-p2p-node-and-channel-discovery).
The `shutdown_scriptpubkey` allows the sending node to commit to where
funds will go on mutual close, which the remote node should enforce
even if a node is compromised later.
The `option_support_large_channel` is a feature used to let everyone
know this node will accept `funding_satoshis` greater than or equal to 2^24.
Since it's broadcast in the `node_announcement` message other nodes can use it to identify peers
willing to accept large channel even before exchanging the `init` message with them.
#### Defined Channel Types
Channel types are an explicit enumeration: for convenience of future
definitions they reuse even feature bits, but they are not an
arbitrary combination (they represent the persistent features which
affect the channel operation).
The currently defined basic types are:
- `option_static_remotekey` (bit 12)
- `option_anchors` and `option_static_remotekey` (bits 22 and 12)
Each basic type has the following variations allowed:
- `option_scid_alias` (bit 46)
- `option_zeroconf` (bit 50)
#### Requirements
The sending node:
- MUST ensure the `chain_hash` value identifies the chain it wishes to open the channel within.
- MUST ensure `temporary_channel_id` is unique from any other channel ID with the same peer.
- if both nodes advertised `option_support_large_channel`:
- MAY set `funding_satoshis` greater than or equal to 2^24 satoshi.
- otherwise:
- MUST set `funding_satoshis` to less than 2^24 satoshi.
- MUST set `push_msat` to equal or less than 1000 * `funding_satoshis`.
- MUST set `funding_pubkey`, `revocation_basepoint`, `htlc_basepoint`, `payment_basepoint`, and `delayed_payment_basepoint` to valid secp256k1 pubkeys in compressed format.
- 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).
- MUST set `channel_reserve_satoshis` greater than or equal to `dust_limit_satoshis`.
- MUST set undefined bits in `channel_flags` to 0.
- if both nodes advertised the `option_upfront_shutdown_script` feature:
- MUST include `upfront_shutdown_script` with either a valid `shutdown_scriptpubkey` as required by `shutdown` `scriptpubkey`, or a zero-length `shutdown_scriptpubkey` (ie. `0x0000`).
- otherwise:
- MAY include `upfront_shutdown_script`.
- if it includes `open_channel_tlvs`:
- MUST include `upfront_shutdown_script`.
- if `option_channel_type` is negotiated:
- MUST set `channel_type`
- if it includes `channel_type`:
- MUST set it to a defined type representing the type it wants.
- MUST use the smallest bitmap possible to represent the channel type.
- SHOULD NOT set it to a type containing a feature which was not negotiated.
- if `announce_channel` is `true` (not `0`):
- MUST NOT send `channel_type` with the `option_scid_alias` bit set.
The sending node SHOULD:
- set `to_self_delay` sufficient to ensure the sender can irreversibly spend a commitment transaction output, in case of misbehavior by the receiver.
- set `feerate_per_kw` to at least the rate it estimates would cause the transaction to be immediately included in a block.
- set `dust_limit_satoshis` to a sufficient value to allow commitment transactions to propagate through the Bitcoin network.
- set `htlc_minimum_msat` to the minimum value HTLC it's willing to accept from this peer.
The receiving node MUST:
- ignore undefined bits in `channel_flags`.
- if the connection has been re-established after receiving a previous
`open_channel`, BUT before receiving a `funding_created` message:
- accept a new `open_channel` message.
- discard the previous `open_channel` message.
- if `option_dual_fund` has been negotiated:
- fail the channel.
The receiving node MAY fail the channel if:
- `option_channel_type` was negotiated but the message doesn't include a `channel_type`
- `announce_channel` is `false` (`0`), yet it wishes to publicly announce the channel.
- `funding_satoshis` is too small.
- it considers `htlc_minimum_msat` too large.
- it considers `max_htlc_value_in_flight_msat` too small.
- it considers `channel_reserve_satoshis` too large.
- it considers `max_accepted_htlcs` too small.
- it considers `dust_limit_satoshis` too large.
The receiving node MUST fail the channel if:
- the `chain_hash` value is set to a hash of a chain that is unknown to the receiver.
- `push_msat` is greater than `funding_satoshis` * 1000.
- `to_self_delay` is unreasonably large.
- `max_accepted_htlcs` is greater than 483.
- it considers `feerate_per_kw` too small for timely processing or unreasonably large.
- `funding_pubkey`, `revocation_basepoint`, `htlc_basepoint`, `payment_basepoint`, or `delayed_payment_basepoint`
are not valid secp256k1 pubkeys in compressed format.
- `dust_limit_satoshis` is greater than `channel_reserve_satoshis`.
- `dust_limit_satoshis` is smaller than `354 satoshis` (see [BOLT 3](03-transactions.md#dust-limits)).
- the funder's amount for the initial commitment transaction is not sufficient for full [fee payment](03-transactions.md#fee-payment).
- 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)).
- `funding_satoshis` is greater than or equal to 2^24 and the receiver does not support `option_support_large_channel`.
- It supports `channel_type` and `channel_type` was set:
- if `type` is not suitable.
- if `type` includes `option_zeroconf` and it does not trust the sender to open an unconfirmed channel.
The receiving node MUST NOT:
- consider funds received, using `push_msat`, to be received until the funding transaction has reached sufficient depth.
#### Rationale
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`.
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.
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.
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.
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.
The requirement that `channel_reserve_satoshis` is not considered dust
according to `dust_limit_satoshis` eliminates cases where all outputs
would be eliminated as dust. The similar requirements in
`accept_channel` ensure that both sides' `channel_reserve_satoshis`
are above both `dust_limit_satoshis`.
The receiver should not accept large `dust_limit_satoshis`, as this could be
used in griefing attacks, where the peer publishes its commitment with a lot
of dust htlcs, which effectively become miner fees.
Details for how to handle a channel failure can be found in [BOLT 5:Failing a Channel](05-onchain.md#failing-a-channel).
### The `accept_channel` Message
This message contains information about a node and indicates its
acceptance of the new channel. This is the second step toward creating the
funding transaction and both versions of the commitment transaction.
1. type: 33 (`accept_channel`)
2. data:
* [`32*byte`:`temporary_channel_id`]
* [`u64`:`dust_limit_satoshis`]
* [`u64`:`max_htlc_value_in_flight_msat`]
* [`u64`:`channel_reserve_satoshis`]
* [`u64`:`htlc_minimum_msat`]
* [`u32`:`minimum_depth`]
* [`u16`:`to_self_delay`]
* [`u16`:`max_accepted_htlcs`]
* [`point`:`funding_pubkey`]
* [`point`:`revocation_basepoint`]
* [`point`:`payment_basepoint`]
* [`point`:`delayed_payment_basepoint`]
* [`point`:`htlc_basepoint`]
* [`point`:`first_per_commitment_point`]
* [`accept_channel_tlvs`:`tlvs`]
1. `tlv_stream`: `accept_channel_tlvs`
2. types:
1. type: 0 (`upfront_shutdown_script`)
2. data:
* [`...*byte`:`shutdown_scriptpubkey`]
1. type: 1 (`channel_type`)
2. data:
* [`...*byte`:`type`]
#### Requirements
The `temporary_channel_id` MUST be the same as the `temporary_channel_id` in
the `open_channel` message.
The sender:
- if `channel_type` includes `option_zeroconf`:
- MUST set `minimum_depth` to zero.
- otherwise:
- SHOULD set `minimum_depth` to a number of blocks it considers reasonable to avoid double-spending of the funding transaction.
- MUST set `channel_reserve_satoshis` greater than or equal to `dust_limit_satoshis` from the `open_channel` message.
- MUST set `dust_limit_satoshis` less than or equal to `channel_reserve_satoshis` from the `open_channel` message.
- if `option_channel_type` was negotiated:
- MUST set `channel_type` to the `channel_type` from `open_channel`
The receiver:
- if `minimum_depth` is unreasonably large:
- MAY fail the channel.
- if `channel_reserve_satoshis` is less than `dust_limit_satoshis` within the `open_channel` message:
- MUST fail the channel.
- if `channel_reserve_satoshis` from the `open_channel` message is less than `dust_limit_satoshis`:
- MUST fail the channel.
- if `channel_type` is set, and `channel_type` was set in `open_channel`, and they are not equal types:
- MUST fail the channel.
- if `option_channel_type` was negotiated but the message doesn't include a `channel_type`:
- MAY fail the channel.
Other fields have the same requirements as their counterparts in `open_channel`.
### The `funding_created` Message
This message describes the outpoint which the funder has created for
the initial commitment transactions. After receiving the peer's
signature, via `funding_signed`, it will broadcast the funding transaction.
1. type: 34 (`funding_created`)
2. data:
* [`32*byte`:`temporary_channel_id`]
* [`sha256`:`funding_txid`]
* [`u16`:`funding_output_index`]
* [`signature`:`signature`]
#### Requirements
The sender MUST set:
- `temporary_channel_id` the same as the `temporary_channel_id` in the `open_channel` message.
- `funding_txid` to the transaction ID of a non-malleable transaction,
- and MUST NOT broadcast this transaction.
- `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).
- `signature` to the valid signature using its `funding_pubkey` for the initial commitment transaction, as defined in [BOLT #3](03-transactions.md#commitment-transaction).
The sender:
- when creating the funding transaction:
- SHOULD use only BIP141 (Segregated Witness) inputs.
- SHOULD ensure the funding transaction confirms in the next 2016 blocks.
The recipient:
- if `signature` is incorrect OR non-compliant with LOW-S-standard rule<sup>[LOWS](https://github.com/bitcoin/bitcoin/pull/6769)</sup>:
- MUST send a `warning` and close the connection, or send an
`error` and fail the channel.
#### Rationale
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.
A transaction with all Segregated Witness inputs is not malleable, hence the funding transaction recommendation.
The funder may use CPFP on a change output to ensure that the funding transaction confirms before 2016 blocks,
otherwise the fundee may forget that channel.
### The `funding_signed` Message
This message gives the funder the signature it needs for the first
commitment transaction, so it can broadcast the transaction knowing that funds
can be redeemed, if need be.
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).
1. type: 35 (`funding_signed`)
2. data:
* [`channel_id`:`channel_id`]
* [`signature`:`signature`]
#### Requirements
Both peers:
- if `channel_type` was present in both `open_channel` and `accept_channel`:
- This is the `channel_type` (they must be equal, required above)
- otherwise:
- if `option_anchors` was negotiated:
- the `channel_type` is `option_anchors` and `option_static_remotekey` (bits 22 and 12)
- otherwise:
- the `channel_type` is `option_static_remotekey` (bit 12)
- MUST use that `channel_type` for all commitment transactions.
The sender MUST set:
- `channel_id` by exclusive-OR of the `funding_txid` and the `funding_output_index` from the `funding_created` message.
- `signature` to the valid signature, using its `funding_pubkey` for the initial commitment transaction, as defined in [BOLT #3](03-transactions.md#commitment-transaction).
The recipient:
- if `signature` is incorrect OR non-compliant with LOW-S-standard rule<sup>[LOWS](https://github.com/bitcoin/bitcoin/pull/6769)</sup>:
- MUST send a `warning` and close the connection, or send an
`error` and fail the channel.
- MUST NOT broadcast the funding transaction before receipt of a valid `funding_signed`.
- on receipt of a valid `funding_signed`:
- SHOULD broadcast the funding transaction.
#### Rationale
We decide on `option_static_remotekey` or `option_anchors` at this point
when we first have to generate the commitment transaction. The feature
bits that were communicated in the `init` message exchange for the current
connection determine the channel commitment format for the total lifetime
of the channel. Even if a later reconnection does not negotiate this
parameter, this channel will continue to use `option_static_remotekey` or
`option_anchors`; we don't support "downgrading".
`option_anchors` is considered superior to `option_static_remotekey`,