|
| 1 | +package routing |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/binary" |
| 6 | + "encoding/hex" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/btcsuite/btcd/btcec/v2" |
| 10 | + sphinx "github.com/lightningnetwork/lightning-onion" |
| 11 | + "github.com/lightningnetwork/lnd/routing/route" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +// TestIntermediatePayloadSize tests the payload size functions of the |
| 16 | +// PrivateEdge and the BlindedEdge. |
| 17 | +func TestIntermediatePayloadSize(t *testing.T) { |
| 18 | + t.Parallel() |
| 19 | + |
| 20 | + testPrivKeyBytes, _ := hex.DecodeString("e126f68f7eafcc8b74f54d269fe" + |
| 21 | + "206be715000f94dac067d1c04a8ca3b2db734") |
| 22 | + _, blindedPoint := btcec.PrivKeyFromBytes(testPrivKeyBytes) |
| 23 | + |
| 24 | + testCases := []struct { |
| 25 | + name string |
| 26 | + hop route.Hop |
| 27 | + nextHop uint64 |
| 28 | + edge AdditionalEdge |
| 29 | + }{ |
| 30 | + { |
| 31 | + name: "Legacy payload private edge", |
| 32 | + hop: route.Hop{ |
| 33 | + AmtToForward: 1000, |
| 34 | + OutgoingTimeLock: 600000, |
| 35 | + ChannelID: 3432483437438, |
| 36 | + LegacyPayload: true, |
| 37 | + }, |
| 38 | + nextHop: 1, |
| 39 | + edge: &PrivateEdge{}, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "Tlv payload private edge", |
| 43 | + hop: route.Hop{ |
| 44 | + AmtToForward: 1000, |
| 45 | + OutgoingTimeLock: 600000, |
| 46 | + ChannelID: 3432483437438, |
| 47 | + LegacyPayload: false, |
| 48 | + }, |
| 49 | + nextHop: 1, |
| 50 | + edge: &PrivateEdge{}, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "Blinded edge", |
| 54 | + hop: route.Hop{ |
| 55 | + EncryptedData: []byte{12, 13}, |
| 56 | + }, |
| 57 | + edge: &BlindedEdge{ |
| 58 | + cipherText: []byte{12, 13}, |
| 59 | + }, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: "Blinded edge - introduction point", |
| 63 | + hop: route.Hop{ |
| 64 | + EncryptedData: []byte{12, 13}, |
| 65 | + BlindingPoint: blindedPoint, |
| 66 | + }, |
| 67 | + edge: &BlindedEdge{ |
| 68 | + cipherText: []byte{12, 13}, |
| 69 | + blindingPoint: blindedPoint, |
| 70 | + }, |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + for _, testCase := range testCases { |
| 75 | + testCase := testCase |
| 76 | + |
| 77 | + t.Run(testCase.name, func(t *testing.T) { |
| 78 | + t.Parallel() |
| 79 | + |
| 80 | + payLoad, err := createHopPayload( |
| 81 | + testCase.hop, testCase.nextHop, false, |
| 82 | + ) |
| 83 | + require.NoErrorf(t, err, "failed to create hop payload") |
| 84 | + |
| 85 | + expectedPayloadSize := testCase.edge. |
| 86 | + IntermediatePayloadSize( |
| 87 | + testCase.hop.AmtToForward, |
| 88 | + testCase.hop.OutgoingTimeLock, |
| 89 | + testCase.hop.LegacyPayload, |
| 90 | + testCase.nextHop, |
| 91 | + ) |
| 92 | + |
| 93 | + require.Equal( |
| 94 | + t, expectedPayloadSize, |
| 95 | + uint64(payLoad.NumBytes()), |
| 96 | + ) |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +// createHopPayload creates the hop payload of the sphinx package to facilitate |
| 102 | +// the testing of the payload size. |
| 103 | +func createHopPayload(hop route.Hop, nextHop uint64, |
| 104 | + finalHop bool) (sphinx.HopPayload, error) { |
| 105 | + |
| 106 | + // If this is the legacy payload, then we can just include the |
| 107 | + // hop data as normal. |
| 108 | + if hop.LegacyPayload { |
| 109 | + // Before we encode this value, we'll pack the next hop |
| 110 | + // into the NextAddress field of the hop info to ensure |
| 111 | + // we point to the right now. |
| 112 | + hopData := sphinx.HopData{ |
| 113 | + ForwardAmount: uint64(hop.AmtToForward), |
| 114 | + OutgoingCltv: hop.OutgoingTimeLock, |
| 115 | + } |
| 116 | + binary.BigEndian.PutUint64( |
| 117 | + hopData.NextAddress[:], nextHop, |
| 118 | + ) |
| 119 | + |
| 120 | + return sphinx.NewLegacyHopPayload(&hopData) |
| 121 | + } |
| 122 | + |
| 123 | + // For non-legacy payloads, we'll need to pack the |
| 124 | + // routing information, along with any extra TLV |
| 125 | + // information into the new per-hop payload format. |
| 126 | + // We'll also pass in the chan ID of the hop this |
| 127 | + // channel should be forwarded to so we can construct a |
| 128 | + // valid payload. |
| 129 | + var b bytes.Buffer |
| 130 | + err := hop.PackHopPayload(&b, nextHop, finalHop) |
| 131 | + if err != nil { |
| 132 | + return sphinx.HopPayload{}, err |
| 133 | + } |
| 134 | + |
| 135 | + return sphinx.NewTLVHopPayload(b.Bytes()) |
| 136 | +} |
0 commit comments