Skip to content
LearnBitcoin

ResearchBitcoin Dev Archive lightning-dev list

type,len,value standard

7 messages, Nov 15 to 16, 2018. Started by ZmnSCPxj.

ZmnSCPxj (3), Conner Fromknecht (2), Christian Decker, Rusty Russell

Permalink First message in the Wayback Machine

ZmnSCPxj permalink original

Good morning list,

An item added discussed in the summit was the proposed "type,len,value", which is added to the end of messages and other intercommunication structures (invoices and so on). This would allow some transition to future additional fields while maintaining backward compatibility.

I believe these were brought up:

1.  For a sequence of `type,len,value`, each `type` must be unique. -- accepted.
2.  For a sequence of `type,len,value`, the `type`s must be in ascending order -- not explicitly accepted or rejected.  It would be easier to check uniqueness (the previous rule we accepted) here for a naive parser (keep track of some "minimum allowed type" that initializes at zero, check current type >= this, update to current type + 1) if `type`s are in ascending order.

Now for bikeshedding:

1, `type` - one byte or two? 2. `type` - maybe some other name, since we already use `type` for messages? How about, `key` instead? 3. `type` - does "it's OK to be odd" apply? i.e. if an even `type` that is not known is found, crash and burn. But intent of this system is for future expansion for optional fields, so...? 4. `len` - measures bytes of `value`, obviously since if the receiver does not know the `type` then it cannot know what unit is used for the `value`. 5. `len` - one byte or two? I believe we tend to use two bytes for various lengths. 6. BOLT - I propose making a separate BOLT for `type,len,value`, which other messages and so on simply refer to.

Regards, ZmnSCPxj

Conner Fromknecht in reply to ZmnSCPxj permalink original

Hi ZmnSCPxj,

Thanks for writing this up! I had started an email, but you beat me to it :)

Quoted text (2 lines)
> 1.  For a sequence of `type,len,value`, each `type` must be unique. --
> accepted.

To add to this, it seemed that there was some agreement that repeated fields should be serialized under a single root key, since a receiver can't know if a field is allowed to have duplicates if they don't understand the field.

Quoted text (5 lines)
> For a sequence of `type,len,value`, the `type`s must be in ascending order
> -- not explicitly accepted or rejected.  It would be easier to check
> uniqueness > (the previous rule we accepted) here for a naive parser (keep
> track of some "minimum allowed type" that initializes at zero, check current
> type >= this, update to current type + 1) if `type`s are in ascending order.

Yep ascending makes sense to me, for the reasons you stated.

Quoted text (1 line)
> 1, `type` - one byte or two?

I'd lean towards one, if a message has 256 optional fields, it might be time to consider a new message type altogether.

Quoted text (3 lines)
> 3. `type` - does "it's OK to be odd" apply?  i.e. if an even `type` that is
> not known is found, crash and burn.  But intent of this system is for future
> expansion for optional fields, so...?
Perhaps this depends on context:
 - for gossip messages, I think the primary concern is not breaking signature
     validation, and that these would need to remain optional for backwards
     compatibility.
 - for link-level messages, we have a little more control. I imagined the fields
   would be gated by feature bit negotiation, and deviating from
   unsupported/required would result in being disconnected.
Quoted text (2 lines)
> 5. `len` - one byte or two? I believe we tend to use two bytes for various
> lengths.

Maybe varint? One byte is not enough for all lengths, but two seems excessive for uint8 or even uint32.

Quoted text (2 lines)
> 6.  BOLT - I propose making a separate BOLT for `type,len,value`, which other
> messages and so on simply refer to.

Indeed, are you thinking we'd use this to add new fields proposed in 1.1?

In addition to the above, do we also want to flesh out what sub-TLV structures would look like? Or perhaps that isn't necessary, if we can continue adding more root-level keys.

--Conner On Wed, Nov 14, 2018 at 8:54 PM ZmnSCPxj via Lightning-dev <lightning-dev at lists.linuxfoundation.org> wrote:

Quoted text (27 lines)
>
> Good morning list,
>
> An item added discussed in the summit was the proposed "type,len,value", which is added to the end of messages and other intercommunication structures (invoices and so on).
> This would allow some transition to future additional fields while maintaining backward compatibility.
>
> I believe these were brought up:
>
> 1.  For a sequence of `type,len,value`, each `type` must be unique. -- accepted.
> 2.  For a sequence of `type,len,value`, the `type`s must be in ascending order -- not explicitly accepted or rejected.  It would be easier to check uniqueness (the previous rule we accepted) here for a naive parser (keep track of some "minimum allowed type" that initializes at zero, check current type >= this, update to current type + 1) if `type`s are in ascending order.
>
> Now for bikeshedding:
>
> 1, `type` - one byte or two?
> 2. `type` - maybe some other name, since we already use `type` for messages?  How about, `key` instead?
> 3. `type` - does "it's OK to be odd" apply?  i.e. if an even `type` that is not known is found, crash and burn.  But intent of this system is for future expansion for optional fields, so...?
> 4. `len` - measures bytes of `value`, obviously since if the receiver does not know the `type` then it cannot know what unit is used for the `value`.
> 5. `len` - one byte or two? I believe we tend to use two bytes for various lengths.
> 6.  BOLT - I propose making a separate BOLT for `type,len,value`, which other messages and so on simply refer to.
>
> Regards,
> ZmnSCPxj
>
> _______________________________________________
> Lightning-dev mailing list
> Lightning-dev at lists.linuxfoundation.org
> https://www.learnbitcoin.com/archive/lists/lightning-dev
ZmnSCPxj in reply to Conner Fromknecht permalink original

Good morning Conner,

Quoted text (7 lines)
> > For a sequence of `type,len,value`, the `type`s must be in ascending order
> > -- not explicitly accepted or rejected. It would be easier to check
> > uniqueness > (the previous rule we accepted) here for a naive parser (keep
> > track of some "minimum allowed type" that initializes at zero, check current
> > type >= this, update to current type + 1) if `type`s are in ascending order.
>
> Yep ascending makes sense to me, for the reasons you stated.

We could bikeshed this and point out that descending would make just as much sense and would work just as well, especially if we want to waste precious electrons on debating this.

Quoted text (5 lines)
>
> > 1, `type` - one byte or two?
>
> I'd lean towards one, if a message has 256 optional fields, it might be time to
> consider a new message type altogether.

This rationale seems sensible.

Quoted text (15 lines)
>
> > 3.  `type` - does "it's OK to be odd" apply? i.e. if an even `type` that is
> >     not known is found, crash and burn. But intent of this system is for future
> >     expansion for optional fields, so...?
> >
>
> Perhaps this depends on context:
>
> -   for gossip messages, I think the primary concern is not breaking signature
>     validation, and that these would need to remain optional for backwards
>     compatibility.
>
> -   for link-level messages, we have a little more control. I imagined the fields
>     would be gated by feature bit negotiation, and deviating from
>     unsupported/required would result in being disconnected.

Then I suppose gossip messages could always just use odd type/key, there would still be 128 of those with a single byte, and at least we would have some sort of consistency, which could help if our software factors out some consistency checks for all t-l-v.

Quoted text (8 lines)
>
>
> > 5.  `len` - one byte or two? I believe we tend to use two bytes for various
> >     lengths.
> >
>
> Maybe varint? One byte is not enough for all lengths, but two seems excessive
> for uint8 or even uint32.

Given that messages are currently only up to 65536 bytes total, is that not a bit much?

Quoted text (6 lines)
>
> > 6.  BOLT - I propose making a separate BOLT for `type,len,value`, which other
> >     messages and so on simply refer to.
> >
>
> Indeed, are you thinking we'd use this to add new fields proposed in 1.1?

Yes. It would also be good to have a single place to describe the new scheme. I also imagine that we would do the sensible thing and create a new type/module/class/whatever for t-l-v data structures (a mapping from u16 to binary blobs). Having a separate BOLT document would help us spec and verify this in a reasonably separate manner.

Quoted text (4 lines)
>
> In addition to the above, do we also want to flesh out what sub-TLV structures
> would look like? Or perhaps that isn't necessary, if we can continue adding more
> root-level keys.

No, I imagine at the t-l-v level we see only binary blobs for the value, and separate parts of our software would understand what the actual meanings of those blobs are.

Regards, ZmnSCPxj

ZmnSCPxj in reply to ZmnSCPxj permalink original

Good morning Conner et al,

Quoted text (8 lines)
> > > 5.  `len` - one byte or two? I believe we tend to use two bytes for various
> > >     lengths.
> > >
> >
> > Maybe varint? One byte is not enough for all lengths, but two seems excessive
> > for uint8 or even uint32.
>
> Given that messages are currently only up to 65536 bytes total, is that not a bit much?

Sorry, I misunderstood.

This is varint, correct? http://learnmeabitcoin.com/glossary/varint

If so, I think this is good idea. It seems we do not have varint currently in Lightning (at least the parts I am familiar with). I suppose the t-l-v being in a different BOLT would let us make some section or part for describing `varint`.

Regards, ZmnSCPxj

Christian Decker in reply to Conner Fromknecht permalink original

Conner Fromknecht <conner at lightning.engineering> writes:

Quoted text (7 lines)
>> For a sequence of `type,len,value`, the `type`s must be in ascending order
>> -- not explicitly accepted or rejected.  It would be easier to check
>> uniqueness > (the previous rule we accepted) here for a naive parser (keep
>> track of some "minimum allowed type" that initializes at zero, check current
>> type >= this, update to current type + 1) if `type`s are in ascending order.
>
> Yep ascending makes sense to me, for the reasons you stated.

Definitely a good idea, especially because it results in a canonical serialization format, which is important to ensure signatures over messages can be verified even when reserializing parsed messages.

Conner Fromknecht in reply to ZmnSCPxj permalink original

Hi ZmnSCPxj,

Precisely, something like that is what I had in mind.

Since the max message size is 65KB, one option could be to only allow
the varint to
be max 2 bytes where:
 - if the 8th bit is not set, the lowest 7 bits of the first bytes
translate to 0 - 127
 - if the 8th bit is set, this implies that the second byte is also
treated as part of the
   length, and the total value is 0x7f & first byte + second byte << 7

This would be fairly straightforward to implement, at the cost of limiting a particular field to 2^15 bytes. I wonder, is this too restrictive?

At the same time, we could offer a varint that could extend up to the three bytes. The third byte would only come in to play if the length of the field is greater than 2^14 - 1. The argument could be made that for values of this size, one extra byte is irrelevant compared to the size of these larger fields.

Cheers, Conner

On Thu, Nov 15, 2018 at 1:45 AM ZmnSCPxj <ZmnSCPxj at protonmail.com> wrote:

Quoted text (22 lines)
>
> Good morning Conner et al,
>
> > > > 5.  `len` - one byte or two? I believe we tend to use two bytes for various
> > > >     lengths.
> > > >
> > >
> > > Maybe varint? One byte is not enough for all lengths, but two seems excessive
> > > for uint8 or even uint32.
> >
> > Given that messages are currently only up to 65536 bytes total, is that not a bit much?
>
> Sorry, I misunderstood.
>
> This is varint, correct? http://learnmeabitcoin.com/glossary/varint
>
> If so, I think this is good idea.
> It seems we do not have varint currently in Lightning (at least the parts I am familiar with).
> I suppose the t-l-v being in a different BOLT would let us make some section or part for describing `varint`.
>
> Regards,
> ZmnSCPxj
Rusty Russell in reply to Conner Fromknecht permalink original

Conner Fromknecht <conner at lightning.engineering> writes:

Quoted text (10 lines)
> Hi ZmnSCPxj,
>
> Thanks for writing this up! I had started an email, but you beat me to it :)
>
>> 1.  For a sequence of `type,len,value`, each `type` must be unique. --
>> accepted.
>
> To add to this, it seemed that there was some agreement that repeated fields
> should be serialized under a single root key, since a receiver can't know if a
> field is allowed to have duplicates if they don't understand the field.

If a receiver doesn't understand it, it doesn't have to enforce any rules on it *at all*. So it depends on individual cases.

Quoted text (7 lines)
>> For a sequence of `type,len,value`, the `type`s must be in ascending order
>> -- not explicitly accepted or rejected.  It would be easier to check
>> uniqueness > (the previous rule we accepted) here for a naive parser (keep
>> track of some "minimum allowed type" that initializes at zero, check current
>> type >= this, update to current type + 1) if `type`s are in ascending order.
>
> Yep ascending makes sense to me, for the reasons you stated.

Again, writer MUST, with no constraint on reader.

Quoted text (4 lines)
>> 1, `type` - one byte or two?
>
> I'd lean towards one, if a message has 256 optional fields, it might be time to
> consider a new message type altogether.

Exactly.

Quoted text (8 lines)
>> 3. `type` - does "it's OK to be odd" apply?  i.e. if an even `type` that is
>> not known is found, crash and burn.  But intent of this system is for future
>> expansion for optional fields, so...?
>
> Perhaps this depends on context:
>  - for gossip messages, I think the primary concern is not breaking signature
>      validation, and that these would need to remain optional for backwards
>      compatibility.

Yes, "not OK" is message dependent. It doesn't break parsing (if you want that, use a new message type), but it does break *usage*; "you can't use this information".

TLV makes sense when there are many many options, at cost of a couple of bytes per option. We agreed to consider it for new additions, but sometimes a simple option bitmap will be a win.

Quoted text (3 lines)
>  - for link-level messages, we have a little more control. I imagined the fields
>    would be gated by feature bit negotiation, and deviating from
>    unsupported/required would result in being disconnected.

It's nice if you can simplify your code by always sending it, even if the remote doesn't understand it.

Quoted text (5 lines)
>> 5. `len` - one byte or two? I believe we tend to use two bytes for various
>> lengths.
>
> Maybe varint? One byte is not enough for all lengths, but two seems excessive
> for uint8 or even uint32.

Yes, it's a nice compromise IMHO.

Quoted text (4 lines)
>> 6.  BOLT - I propose making a separate BOLT for `type,len,value`, which other
>> messages and so on simply refer to.
>
> Indeed, are you thinking we'd use this to add new fields proposed in 1.1?

Well, it's a very individual message thing. We would specify it in general, then each place where it's used (eg. gossip) would have their own types.

Quoted text (3 lines)
> In addition to the above, do we also want to flesh out what sub-TLV structures
> would look like? Or perhaps that isn't necessary, if we can continue adding more
> root-level keys.

I think that the general TLV definition just belongs in BOLT #1, since it's so short.

Cheers, Rusty.