lnwire: add TLV extension to channel_reestablish for dynamic commitments

This commit is contained in:
Keagan McClelland 2023-10-13 18:32:52 -06:00 committed by Olaoluwa Osuntokun
parent e5f7ed8ba1
commit 3e84d22eeb

View File

@ -5,9 +5,24 @@ import (
"io"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/lightningnetwork/lnd/fn"
"github.com/lightningnetwork/lnd/tlv"
)
const (
CRDynHeight tlv.Type = 20
)
// DynHeight is a newtype wrapper to get the proper RecordProducer instance
// to smoothly integrate with the ChannelReestablish Message instance.
type DynHeight uint64
// Record implements the RecordProducer interface, allowing a full tlv.Record
// object to be constructed from a DynHeight.
func (d *DynHeight) Record() tlv.Record {
return tlv.MakePrimitiveRecord(CRDynHeight, (*uint64)(d))
}
// ChannelReestablish is a message sent between peers that have an existing
// open channel upon connection reestablishment. This message allows both sides
// to report their local state, and their current knowledge of the state of the
@ -70,6 +85,11 @@ type ChannelReestablish struct {
// TODO(roasbeef): rename to verification nonce
LocalNonce *Musig2Nonce
// DynHeight is an optional field that stores the dynamic commitment
// negotiation height that is incremented upon successful completion of
// a dynamic commitment negotiation
DynHeight fn.Option[DynHeight]
// ExtraData is the set of data that was appended to this message to
// fill out the full maximum transport message size. These fields can
// be used to specify optional data such as custom TLV fields.
@ -121,6 +141,10 @@ func (a *ChannelReestablish) Encode(w *bytes.Buffer, pver uint32) error {
if a.LocalNonce != nil {
recordProducers = append(recordProducers, a.LocalNonce)
}
a.DynHeight.WhenSome(func(h DynHeight) {
recordProducers = append(recordProducers, &h)
})
err := EncodeMessageExtraData(&a.ExtraData, recordProducers...)
if err != nil {
return err
@ -180,8 +204,9 @@ func (a *ChannelReestablish) Decode(r io.Reader, pver uint32) error {
}
var localNonce Musig2Nonce
var dynHeight DynHeight
typeMap, err := tlvRecords.ExtractRecords(
&localNonce,
&localNonce, &dynHeight,
)
if err != nil {
return err
@ -190,6 +215,9 @@ func (a *ChannelReestablish) Decode(r io.Reader, pver uint32) error {
if val, ok := typeMap[NonceRecordType]; ok && val == nil {
a.LocalNonce = &localNonce
}
if val, ok := typeMap[CRDynHeight]; ok && val == nil {
a.DynHeight = fn.Some(dynHeight)
}
if len(tlvRecords) != 0 {
a.ExtraData = tlvRecords