mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-27 14:11:04 +02:00
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:
@@ -86,6 +86,14 @@ func TestExtraOpaqueDataEncodeDecode(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
type recordProducer struct {
|
||||
record tlv.Record
|
||||
}
|
||||
|
||||
func (r *recordProducer) Record() tlv.Record {
|
||||
return r.record
|
||||
}
|
||||
|
||||
// TestExtraOpaqueDataPackUnpackRecords tests that we're able to pack a set of
|
||||
// tlv.Records into a stream, and unpack them on the other side to obtain the
|
||||
// same set of records.
|
||||
@@ -102,23 +110,23 @@ func TestExtraOpaqueDataPackUnpackRecords(t *testing.T) {
|
||||
hop1 uint32 = 99
|
||||
hop2 uint32
|
||||
)
|
||||
testRecords := []tlv.Record{
|
||||
tlv.MakePrimitiveRecord(type1, &channelType1),
|
||||
tlv.MakePrimitiveRecord(type2, &hop1),
|
||||
testRecordsProducers := []tlv.RecordProducer{
|
||||
&recordProducer{tlv.MakePrimitiveRecord(type1, &channelType1)},
|
||||
&recordProducer{tlv.MakePrimitiveRecord(type2, &hop1)},
|
||||
}
|
||||
|
||||
// Now that we have our set of sample records and types, we'll encode
|
||||
// them into the passed ExtraOpaqueData instance.
|
||||
var extraBytes ExtraOpaqueData
|
||||
if err := extraBytes.PackRecords(testRecords...); err != nil {
|
||||
if err := extraBytes.PackRecords(testRecordsProducers...); err != nil {
|
||||
t.Fatalf("unable to pack records: %v", err)
|
||||
}
|
||||
|
||||
// We'll now simulate decoding these types _back_ into records on the
|
||||
// other side.
|
||||
newRecords := []tlv.Record{
|
||||
tlv.MakePrimitiveRecord(type1, &channelType2),
|
||||
tlv.MakePrimitiveRecord(type2, &hop2),
|
||||
newRecords := []tlv.RecordProducer{
|
||||
&recordProducer{tlv.MakePrimitiveRecord(type1, &channelType2)},
|
||||
&recordProducer{tlv.MakePrimitiveRecord(type2, &hop2)},
|
||||
}
|
||||
typeMap, err := extraBytes.ExtractRecords(newRecords...)
|
||||
if err != nil {
|
||||
|
Reference in New Issue
Block a user