Skip to content

Commit

Permalink
lnwire: modify PackRecords to prepend new records
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Roasbeef authored and guggero committed Apr 25, 2024
1 parent 7f38712 commit ffa19c0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lnwire/extra_bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
22 changes: 22 additions & 0 deletions lnwire/extra_bytes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}

0 comments on commit ffa19c0

Please sign in to comment.