lnwire: add new ChannelType field as TLV record to Open/AcceptChannel

In this commit, we add a new ChannelType field as a new TLV record to
the OpenChannel message. During this change, we make a few tweaks to the
generic TLV encode/decode methods for the ExtraOpaqueData struct to have
it work on the level of tlv.RecordProducer instead of tlv.Record, as
this reduces line noise a bit.

We also partially undo existing logic that would attempt to "prepend"
any new TLV records to the end of the ExtraOpaqueData if one was already
present within the struct. This is based on the assumption that if we've
read a message from disk to order to re-send/transmit it, then the
ExtraOpaqueData is fully populated so we'll write that as is. Otherwise,
a message is being encoded for the first time, and we expect all fields
that are known TLV fields to be specified within the struct itself.

This change required the unit tests to be modified slightly, as we'll
always encode a fresh set of TLV records if none was already specified
within the struct.
This commit is contained in:
Olaoluwa Osuntokun
2021-03-03 19:37:00 -08:00
parent 988d01de0d
commit 57b7a668c0
8 changed files with 116 additions and 108 deletions

View File

@@ -2,6 +2,7 @@ package lnwire
import (
"bytes"
"fmt"
"io"
"io/ioutil"
@@ -50,7 +51,17 @@ func (e *ExtraOpaqueData) Decode(r io.Reader) error {
// PackRecords attempts to encode the set of tlv records into the target
// ExtraOpaqueData instance. The records will be encoded as a raw TLV stream
// and stored within the backing slice pointer.
func (e *ExtraOpaqueData) PackRecords(records ...tlv.Record) error {
func (e *ExtraOpaqueData) PackRecords(recordProducers ...tlv.RecordProducer) error {
// First, assemble all the records passed in in series.
records := make([]tlv.Record, 0, len(recordProducers))
for _, producer := range recordProducers {
records = append(records, producer.Record())
}
// Ensure that the set of records are sorted before we encode them into
// the stream, to ensure they're canonical.
tlv.SortRecords(records)
tlvStream, err := tlv.NewStream(records...)
if err != nil {
return err
@@ -70,9 +81,15 @@ func (e *ExtraOpaqueData) PackRecords(records ...tlv.Record) error {
// it were a tlv stream. The set of raw parsed types is returned, and any
// passed records (if found in the stream) will be parsed into the proper
// tlv.Record.
func (e *ExtraOpaqueData) ExtractRecords(records ...tlv.Record) (
func (e *ExtraOpaqueData) ExtractRecords(recordProducers ...tlv.RecordProducer) (
tlv.TypeMap, error) {
// First, assemble all the records passed in in series.
records := make([]tlv.Record, 0, len(recordProducers))
for _, producer := range recordProducers {
records = append(records, producer.Record())
}
extraBytesReader := bytes.NewReader(*e)
tlvStream, err := tlv.NewStream(records...)
@@ -82,3 +99,19 @@ func (e *ExtraOpaqueData) ExtractRecords(records ...tlv.Record) (
return tlvStream.DecodeWithParsedTypes(extraBytesReader)
}
// EncodeMessageExtraData encodes the given recordProducers into the given
// extraData.
func EncodeMessageExtraData(extraData *ExtraOpaqueData,
recordProducers ...tlv.RecordProducer) error {
// Treat extraData as a mutable reference.
if extraData == nil {
return fmt.Errorf("extra data cannot be nil")
}
// Pack in the series of TLV records into this message. The order we
// pass them in doesn't matter, as the method will ensure that things
// are all properly sorted.
return extraData.PackRecords(recordProducers...)
}