mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-06-19 05:11:25 +02:00
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:
parent
c0c511c686
commit
1858e1966f
@ -71,7 +71,7 @@ func (e *ExtraOpaqueData) PackRecords(recordProducers ...tlv.RecordProducer) err
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
*e = ExtraOpaqueData(extraBytesWriter.Bytes())
|
*e = append(extraBytesWriter.Bytes(), *e...)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -151,3 +151,25 @@ func TestExtraOpaqueDataPackUnpackRecords(t *testing.T) {
|
|||||||
t.Fatalf("type2 not found in typeMap")
|
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))
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user