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.
This commit is contained in:
Olaoluwa Osuntokun 2024-04-08 19:50:05 -07:00 committed by Oliver Gugger
parent c0c511c686
commit 1858e1966f
No known key found for this signature in database
GPG Key ID: 8E4256593F177720
2 changed files with 23 additions and 1 deletions

View File

@ -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
}

View File

@ -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))
}