Skip to content

Commit cfd4314

Browse files
Roasbeefguggero
authored andcommitted
lnwire: modify PackRecords to prepend new records
As is, we can't use PackRecords when some data may already be stored in the ExtraData field. To allow this use case, we add a failing test, then modify `PackRecords` to append the existing raw bytes (prepend the new records). Note that this doesn't 100% solve the problem, as for the stream to be cannonical we need unique IDs/types and also for them to be in ascending order.
1 parent 01ef2b1 commit cfd4314

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

lnwire/extra_bytes.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (e *ExtraOpaqueData) PackRecords(recordProducers ...tlv.RecordProducer) err
7171
return err
7272
}
7373

74-
*e = ExtraOpaqueData(extraBytesWriter.Bytes())
74+
*e = append(extraBytesWriter.Bytes(), *e...)
7575

7676
return nil
7777
}

lnwire/extra_bytes_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,25 @@ func TestExtraOpaqueDataPackUnpackRecords(t *testing.T) {
151151
t.Fatalf("type2 not found in typeMap")
152152
}
153153
}
154+
155+
// TestPackRecordsPrepend tests that if an ExtraOpaqueData instance already a
156+
// set of opaque bytes, then any records passed in are prepended to the
157+
// existing bytes.
158+
func TestPackRecordsPrepend(t *testing.T) {
159+
t.Parallel()
160+
161+
chanTypeRecord := tlv.NewPrimitiveRecord[tlv.TlvType1](uint8(2))
162+
163+
// Create some opaque data that is already pre-populated with some
164+
// bytes.
165+
existingBytes := bytes.Repeat([]byte{1}, 10)
166+
extraBytes := ExtraOpaqueData(existingBytes)
167+
168+
// Now we'll attempt to pack the records into the existing bytes.
169+
err := extraBytes.PackRecords(&chanTypeRecord)
170+
require.NoError(t, err)
171+
172+
// After we've packed the records, the existing bytes should be at the
173+
// very end.
174+
require.True(t, bytes.HasSuffix(extraBytes[:], existingBytes))
175+
}

0 commit comments

Comments
 (0)