Skip to content

Commit 6656b70

Browse files
committed
Bolt 1: Specify that extensions to existing messages must use TLV
The spec already prepared a hook to add additional information to existing messages (additional bytes at the end of a message must be ignored). Since we're using TLV in many places, it makes sense to use that optional additional space at the end of each message to allow an optional tlv stream. This requires making a few previously optional fields mandatory: - channel_reestablish commitment points: it makes sense to always include those regardless of whether `option_dataloss_protect` or `option_static_remotekey` are set. - option_upfront_shutdown_script: if you're not using one, just set the length to 0. That field is moved to a TLV record because luckily, the resulting bytes are the same. This provides more flexibility to later remove the requirement of making this field mandatory. No need to change the `channel_update`'s `htlc_maximum_msat` because the `message_flags` encode its presence/absence. It can still be either included or omitted without causing issues to the extension stream.
1 parent f38f559 commit 6656b70

File tree

2 files changed

+69
-35
lines changed

2 files changed

+69
-35
lines changed

01-messaging.md

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ All data fields are unsigned big-endian unless otherwise specified.
2222
* [The `ping` and `pong` Messages](#the-ping-and-pong-messages)
2323
* [Appendix A: BigSize Test Vectors](#appendix-a-bigsize-test-vectors)
2424
* [Appendix B: Type-Length-Value Test Vectors](#appendix-b-type-length-value-test-vectors)
25+
* [Appendix C: Message Extension](#appendix-c-message-extension)
2526
* [Acknowledgments](#acknowledgments)
2627
* [References](#references)
2728
* [Authors](#authors)
@@ -37,20 +38,12 @@ After decryption, all Lightning messages are of the form:
3738
1. `type`: a 2-byte big-endian field indicating the type of message
3839
2. `payload`: a variable-length payload that comprises the remainder of
3940
the message and that conforms to a format matching the `type`
41+
3. `extension`: an optional [TLV stream](#type-length-value-format)
4042

4143
The `type` field indicates how to interpret the `payload` field.
4244
The format for each individual type is defined by a specification in this repository.
4345
The type follows the _it's ok to be odd_ rule, so nodes MAY send _odd_-numbered types without ascertaining that the recipient understands it.
4446

45-
A sending node:
46-
- MUST NOT send an evenly-typed message not listed here without prior negotiation.
47-
48-
A receiving node:
49-
- upon receiving a message of _odd_, unknown type:
50-
- MUST ignore the received message.
51-
- upon receiving a message of _even_, unknown type:
52-
- MUST fail the channels.
53-
5447
The messages are grouped logically into five groups, ordered by the most significant bit that is set:
5548

5649
- Setup & Control (types `0`-`31`): messages related to connection setup, control, supported features, and error reporting (described below)
@@ -61,10 +54,9 @@ The messages are grouped logically into five groups, ordered by the most signifi
6154

6255
The size of the message is required by the transport layer to fit into a 2-byte unsigned int; therefore, the maximum possible size is 65535 bytes.
6356

64-
A node:
65-
- MUST ignore any additional data within a message beyond the length that it expects for that type.
66-
- upon receiving a known message with insufficient length for the contents:
67-
- MUST fail the channels.
57+
A sending node:
58+
- MUST NOT send an evenly-typed message not listed here without prior negotiation.
59+
- MUST NOT send evenly-typed TLV records in the `extension` without prior negotiation.
6860
- that negotiates an option in this specification:
6961
- MUST include all the fields annotated with that option.
7062
- When defining custom messages:
@@ -75,6 +67,18 @@ A node:
7567
- SHOULD pick an even `type` identifiers when regular nodes should reject
7668
the message and close the connection.
7769

70+
A receiving node:
71+
- upon receiving a message of _odd_, unknown type:
72+
- MUST ignore the received message.
73+
- upon receiving a message of _even_, unknown type:
74+
- MUST fail the channels.
75+
- upon receiving a known message with insufficient length for the contents:
76+
- MUST fail the channels.
77+
- upon receiving a message with an `extension`:
78+
- MAY ignore the `extension`.
79+
- Otherwise, if the `extension` is invalid:
80+
- MUST fail the channels.
81+
7882
### Rationale
7983

8084
By default `SHA2` and Bitcoin public keys are both encoded as
@@ -85,8 +89,9 @@ Length is limited to 65535 bytes by the cryptographic wrapping, and
8589
messages in the protocol are never more than that length anyway.
8690

8791
The _it's ok to be odd_ rule allows for future optional extensions
88-
without negotiation or special coding in clients. The "ignore
89-
additional data" rule similarly allows for future expansion.
92+
without negotiation or special coding in clients. The _extension_ field
93+
similarly allows for future expansion by letting senders include additional
94+
TLV data.
9095

9196
Implementations may prefer to have message data aligned on an 8-byte
9297
boundary (the largest natural alignment requirement of any type here);
@@ -896,6 +901,27 @@ failure:
896901
1. Invalid stream: 0xffffffffffffffffff 00 00 00
897902
2. Reason: valid TLV records but invalid ordering
898903

904+
## Appendix C: Message Extension
905+
906+
This section contains examples of valid and invalid extensions on the `init`
907+
message. The base `init` message (without extensions) for these examples is
908+
`0x001000000000` (all features turned off).
909+
910+
The following `init` messages are valid:
911+
912+
- `0x001000000000`: no extension provided
913+
- `0x00100000000001012a030104`: the extension contains two _odd_ TLV records (with types `0x01` and `0x03`)
914+
915+
The following `init` messages are invalid:
916+
917+
- `0x00100000000001`: the extension is present but truncated
918+
- `0x00100000000002012a`: the extension contains unknown _even_ TLV records (assuming that TLV type `0x02` is unknown)
919+
- `0x001000000000010101010102`: the extension TLV stream is invalid (duplicate TLV record type `0x01`)
920+
921+
Note that when messages are signed, the _extension_ is part of the signed bytes.
922+
Nodes should store the _extension_ bytes even if they don't understand them to
923+
be able to correctly verify signatures.
924+
899925
## Acknowledgments
900926

901927
[ TODO: (roasbeef); fin ]

02-peer-protocol.md

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,13 @@ the funding transaction and both versions of the commitment transaction.
123123
* [`point`:`htlc_basepoint`]
124124
* [`point`:`first_per_commitment_point`]
125125
* [`byte`:`channel_flags`]
126-
* [`u16`:`shutdown_len`] (`option_upfront_shutdown_script`)
127-
* [`shutdown_len*byte`:`shutdown_scriptpubkey`] (`option_upfront_shutdown_script`)
126+
* [`open_channel_tlvs`:`tlvs`]
127+
128+
1. tlvs: `open_channel_tlvs`
129+
2. types:
130+
1. type: 0 (`upfront_shutdown_script`)
131+
2. data:
132+
* [`...*byte`:`shutdown_scriptpubkey`]
128133

129134
The `chain_hash` value denotes the exact blockchain that the opened channel will
130135
reside within. This is usually the genesis hash of the respective blockchain.
@@ -207,9 +212,11 @@ The sending node:
207212
- MUST set `channel_reserve_satoshis` greater than or equal to `dust_limit_satoshis`.
208213
- MUST set undefined bits in `channel_flags` to 0.
209214
- if both nodes advertised the `option_upfront_shutdown_script` feature:
210-
- MUST include either a valid `shutdown_scriptpubkey` as required by `shutdown` `scriptpubkey`, or a zero-length `shutdown_scriptpubkey`.
215+
- MUST include `upfront_shutdown_script` with either a valid `shutdown_scriptpubkey` as required by `shutdown` `scriptpubkey`, or a zero-length `shutdown_scriptpubkey`.
211216
- otherwise:
212-
- MAY include a`shutdown_scriptpubkey`.
217+
- MAY include `upfront_shutdown_script`.
218+
- if it includes `open_channel_tlvs`:
219+
- MUST include `upfront_shutdown_script`.
213220

214221
The sending node SHOULD:
215222
- set `to_self_delay` sufficient to ensure the sender can irreversibly spend a commitment transaction output, in case of misbehavior by the receiver.
@@ -291,8 +298,13 @@ funding transaction and both versions of the commitment transaction.
291298
* [`point`:`delayed_payment_basepoint`]
292299
* [`point`:`htlc_basepoint`]
293300
* [`point`:`first_per_commitment_point`]
294-
* [`u16`:`shutdown_len`] (`option_upfront_shutdown_script`)
295-
* [`shutdown_len*byte`:`shutdown_scriptpubkey`] (`option_upfront_shutdown_script`)
301+
* [`accept_channel_tlvs`:`tlvs`]
302+
303+
1. tlvs: `accept_channel_tlvs`
304+
2. types:
305+
1. type: 0 (`upfront_shutdown_script`)
306+
2. data:
307+
* [`...*byte`:`shutdown_scriptpubkey`]
296308

297309
#### Requirements
298310

@@ -1127,16 +1139,15 @@ messages are), they are independent of requirements here.
11271139
* [`channel_id`:`channel_id`]
11281140
* [`u64`:`next_commitment_number`]
11291141
* [`u64`:`next_revocation_number`]
1130-
* [`32*byte`:`your_last_per_commitment_secret`] (option_data_loss_protect,option_static_remotekey)
1131-
* [`point`:`my_current_per_commitment_point`] (option_data_loss_protect,option_static_remotekey)
1142+
* [`32*byte`:`your_last_per_commitment_secret`]
1143+
* [`point`:`my_current_per_commitment_point`]
11321144

11331145
`next_commitment_number`: A commitment number is a 48-bit
11341146
incrementing counter for each commitment transaction; counters
11351147
are independent for each peer in the channel and start at 0.
11361148
They're only explicitly relayed to the other node in the case of
11371149
re-establishment, otherwise they are implicit.
11381150

1139-
11401151
### Requirements
11411152

11421153
A funding node:
@@ -1178,16 +1189,14 @@ The sending node:
11781189
next `revoke_and_ack` message it expects to receive.
11791190
- if `option_static_remotekey` applies to the commitment transaction:
11801191
- MUST set `my_current_per_commitment_point` to a valid point.
1181-
- otherwise, if it supports `option_data_loss_protect`:
1192+
- otherwise:
11821193
- MUST set `my_current_per_commitment_point` to its commitment point for
1183-
the last signed commitment it received from its channel peer (i.e. the commitment_point
1194+
the last signed commitment it received from its channel peer (i.e. the commitment_point
11841195
corresponding to the commitment transaction the sender would use to unilaterally close).
1185-
- if `option_static_remotekey` applies to the commitment transaction, or the sending node supports `option_data_loss_protect`:
1186-
- if `next_revocation_number` equals 0:
1187-
- MUST set `your_last_per_commitment_secret` to all zeroes
1188-
- otherwise:
1189-
- MUST set `your_last_per_commitment_secret` to the last `per_commitment_secret`
1190-
it received
1196+
- if `next_revocation_number` equals 0:
1197+
- MUST set `your_last_per_commitment_secret` to all zeroes
1198+
- otherwise:
1199+
- MUST set `your_last_per_commitment_secret` to the last `per_commitment_secret` it received
11911200

11921201
A node:
11931202
- if `next_commitment_number` is 1 in both the `channel_reestablish` it
@@ -1228,10 +1237,9 @@ A node:
12281237
- MUST NOT broadcast its commitment transaction.
12291238
- SHOULD fail the channel.
12301239
- otherwise:
1231-
- if `your_last_per_commitment_secret` does not match the expected values:
1240+
- if `your_last_per_commitment_secret` does not match the expected values:
12321241
- SHOULD fail the channel.
1233-
- otherwise, if it supports `option_data_loss_protect`, AND the `option_data_loss_protect`
1234-
fields are present:
1242+
- otherwise, if it supports `option_data_loss_protect`:
12351243
- if `next_revocation_number` is greater than expected above, AND
12361244
`your_last_per_commitment_secret` is correct for that
12371245
`next_revocation_number` minus 1:

0 commit comments

Comments
 (0)