From 1858e1966fa5604fe5b24a377ff95fdc2c3ca7ed Mon Sep 17 00:00:00 2001 From: Olaoluwa Osuntokun Date: Mon, 8 Apr 2024 19:50:05 -0700 Subject: [PATCH] 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. --- lnwire/extra_bytes.go | 2 +- lnwire/extra_bytes_test.go | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/lnwire/extra_bytes.go b/lnwire/extra_bytes.go index 0ebf48f57..c8e18c182 100644 --- a/lnwire/extra_bytes.go +++ b/lnwire/extra_bytes.go @@ -71,7 +71,7 @@ func (e *ExtraOpaqueData) PackRecords(recordProducers ...tlv.RecordProducer) err return err } - *e = ExtraOpaqueData(extraBytesWriter.Bytes()) + *e = append(extraBytesWriter.Bytes(), *e...) return nil } diff --git a/lnwire/extra_bytes_test.go b/lnwire/extra_bytes_test.go index fd9f28841..bc1de8c57 100644 --- a/lnwire/extra_bytes_test.go +++ b/lnwire/extra_bytes_test.go @@ -151,3 +151,25 @@ func TestExtraOpaqueDataPackUnpackRecords(t *testing.T) { t.Fatalf("type2 not found in typeMap") } } + +// TestPackRecordsPrepend tests that if an ExtraOpaqueData instance already a +// set of opaque bytes, then any records passed in are prepended to the +// existing bytes. +func TestPackRecordsPrepend(t *testing.T) { + t.Parallel() + + chanTypeRecord := tlv.NewPrimitiveRecord[tlv.TlvType1](uint8(2)) + + // Create some opaque data that is already pre-populated with some + // bytes. + existingBytes := bytes.Repeat([]byte{1}, 10) + extraBytes := ExtraOpaqueData(existingBytes) + + // Now we'll attempt to pack the records into the existing bytes. + err := extraBytes.PackRecords(&chanTypeRecord) + require.NoError(t, err) + + // After we've packed the records, the existing bytes should be at the + // very end. + require.True(t, bytes.HasSuffix(extraBytes[:], existingBytes)) +}