mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-09-05 17:05:50 +02:00
record: add Padding field to BlindedRouteData
When we start creating blinded paths to ourselves, we will want to be able to pad the data for each hop so that the `encrypted_recipient_data` for each hop is the same. We add a `PadBy` method that allows a caller to add a certain number of bytes to the padding field. Note that adding n bytes won't always mean that the encoded payload will increase by size n since there will be overhead for the type and lenght fields for the new TLV field. This will also be the case when the number of bytes added results in a BigSize bucket jump for TLV length field. The responsibility of ensuring that the final payloads are the same size is left to the caller who may need to call PadBy iteratively to achieve the goal. I decided to leave this to the caller since doing this at the actual TLV level will be quite intrusive & I think it is uneccessary to touch that code for this unique use case.
This commit is contained in:
@@ -171,6 +171,74 @@ func TestBlindedDataFinalHopEncoding(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestBlindedRouteDataPadding tests the PadBy method of BlindedRouteData.
|
||||
func TestBlindedRouteDataPadding(t *testing.T) {
|
||||
newBlindedRouteData := func() *BlindedRouteData {
|
||||
channelID := lnwire.NewShortChanIDFromInt(1)
|
||||
info := PaymentRelayInfo{
|
||||
FeeRate: 2,
|
||||
CltvExpiryDelta: 3,
|
||||
BaseFee: 30,
|
||||
}
|
||||
|
||||
constraints := &PaymentConstraints{
|
||||
MaxCltvExpiry: 4,
|
||||
HtlcMinimumMsat: 100,
|
||||
}
|
||||
|
||||
return NewNonFinalBlindedRouteData(
|
||||
channelID, pubkey(t), info, constraints, nil,
|
||||
)
|
||||
}
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
paddingSize int
|
||||
expectedSizeIncrease uint64
|
||||
}{
|
||||
{
|
||||
// Calling PadBy with an n value of 0 in the case where
|
||||
// there is not yet a padding field will result in a
|
||||
// zero length TLV entry being added. This will add 2
|
||||
// bytes for the type and length fields.
|
||||
name: "no extra padding",
|
||||
expectedSizeIncrease: 2,
|
||||
},
|
||||
{
|
||||
name: "small padding (length " +
|
||||
"field of 1 byte)",
|
||||
paddingSize: 200,
|
||||
expectedSizeIncrease: 202,
|
||||
},
|
||||
{
|
||||
name: "medium padding (length field " +
|
||||
"of 3 bytes)",
|
||||
paddingSize: 256,
|
||||
expectedSizeIncrease: 260,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
data := newBlindedRouteData()
|
||||
|
||||
prePaddingEncoding, err := EncodeBlindedRouteData(data)
|
||||
require.NoError(t, err)
|
||||
|
||||
data.PadBy(test.paddingSize)
|
||||
|
||||
postPaddingEncoding, err := EncodeBlindedRouteData(data)
|
||||
require.NoError(t, err)
|
||||
|
||||
require.EqualValues(
|
||||
t, test.expectedSizeIncrease,
|
||||
len(postPaddingEncoding)-
|
||||
len(prePaddingEncoding),
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestBlindedRouteVectors tests encoding/decoding of the test vectors for
|
||||
// blinded route data provided in the specification.
|
||||
//
|
||||
@@ -184,6 +252,7 @@ func TestBlindingSpecTestVectors(t *testing.T) {
|
||||
tests := []struct {
|
||||
encoded string
|
||||
expectedPaymentData *BlindedRouteData
|
||||
expectedPadding int
|
||||
}{
|
||||
{
|
||||
encoded: "011a0000000000000000000000000000000000000000000000000000020800000000000006c10a0800240000009627100c06000b69e505dc0e00fd023103123456",
|
||||
@@ -208,6 +277,7 @@ func TestBlindingSpecTestVectors(t *testing.T) {
|
||||
lnwire.Features,
|
||||
),
|
||||
),
|
||||
expectedPadding: 26,
|
||||
},
|
||||
{
|
||||
encoded: "020800000000000004510821031b84c5567b126440995d3ed5aaba0565d71e1834604819ff9c17f5e9d5dd078f0a0800300000006401f40c06000b69c105dc0e00",
|
||||
@@ -228,7 +298,8 @@ func TestBlindingSpecTestVectors(t *testing.T) {
|
||||
lnwire.NewFeatureVector(
|
||||
lnwire.NewRawFeatureVector(),
|
||||
lnwire.Features,
|
||||
)),
|
||||
),
|
||||
),
|
||||
},
|
||||
}
|
||||
|
||||
@@ -242,6 +313,12 @@ func TestBlindingSpecTestVectors(t *testing.T) {
|
||||
decodedRoute, err := DecodeBlindedRouteData(buff)
|
||||
require.NoError(t, err)
|
||||
|
||||
if test.expectedPadding != 0 {
|
||||
test.expectedPaymentData.PadBy(
|
||||
test.expectedPadding,
|
||||
)
|
||||
}
|
||||
|
||||
require.Equal(
|
||||
t, test.expectedPaymentData, decodedRoute,
|
||||
)
|
||||
|
Reference in New Issue
Block a user