Skip to content

Commit 99b3c57

Browse files
committed
routing: assume TLV onion during route construction
1 parent 738253f commit 99b3c57

File tree

2 files changed

+19
-37
lines changed

2 files changed

+19
-37
lines changed

routing/pathfind.go

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ func newRoute(sourceVertex route.Vertex,
161161
fee int64
162162
totalAmtMsatBlinded lnwire.MilliSatoshi
163163
outgoingTimeLock uint32
164-
tlvPayload bool
165164
customRecords record.CustomSet
166165
mpp *record.MPP
167166
metadata []byte
@@ -180,13 +179,6 @@ func newRoute(sourceVertex route.Vertex,
180179
return edge.ToNodeFeatures.HasFeature(feature)
181180
}
182181

183-
// We start by assuming the node doesn't support TLV. We'll now
184-
// inspect the node's feature vector to see if we can promote
185-
// the hop. We assume already that the feature vector's
186-
// transitive dependencies have already been validated by path
187-
// finding or some other means.
188-
tlvPayload = supports(lnwire.TLVOnionPayloadOptional)
189-
190182
if i == len(pathEdges)-1 {
191183
// If this is the last hop, then the hop payload will
192184
// contain the exact amount. In BOLT #4: Onion Routing
@@ -204,12 +196,7 @@ func newRoute(sourceVertex route.Vertex,
204196
totalTimeLock += uint32(finalHop.cltvDelta)
205197
outgoingTimeLock = totalTimeLock
206198

207-
// Attach any custom records to the final hop if the
208-
// receiver supports TLV.
209-
if !tlvPayload && finalHop.records != nil {
210-
return nil, errors.New("cannot attach " +
211-
"custom records")
212-
}
199+
// Attach any custom records to the final hop.
213200
customRecords = finalHop.records
214201

215202
// If we're attaching a payment addr but the receiver
@@ -275,7 +262,6 @@ func newRoute(sourceVertex route.Vertex,
275262
ChannelID: edge.ChannelID,
276263
AmtToForward: amtToForward,
277264
OutgoingTimeLock: outgoingTimeLock,
278-
LegacyPayload: !tlvPayload,
279265
CustomRecords: customRecords,
280266
MPP: mpp,
281267
Metadata: metadata,

routing/pathfind_test.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package routing
33
import (
44
"bytes"
55
"crypto/sha256"
6-
"encoding/binary"
76
"encoding/hex"
87
"encoding/json"
98
"errors"
@@ -25,6 +24,7 @@ import (
2524
"github.com/lightningnetwork/lnd/channeldb"
2625
"github.com/lightningnetwork/lnd/channeldb/models"
2726
"github.com/lightningnetwork/lnd/htlcswitch"
27+
switchhop "github.com/lightningnetwork/lnd/htlcswitch/hop"
2828
"github.com/lightningnetwork/lnd/kvdb"
2929
"github.com/lightningnetwork/lnd/lnwire"
3030
"github.com/lightningnetwork/lnd/record"
@@ -1030,7 +1030,8 @@ var basicGraphPathFindingTests = []basicGraphPathFindingTestCase{
10301030
// Basic route with fee limit.
10311031
{target: "sophon", paymentAmt: 100, feeLimit: 50,
10321032
expectFailureNoPath: true,
1033-
}}
1033+
},
1034+
}
10341035

10351036
func runBasicGraphPathFinding(t *testing.T, useCache bool) {
10361037
testGraphInstance, err := parseTestGraph(t, useCache, basicGraphFilePath)
@@ -1123,32 +1124,27 @@ func testBasicGraphPathFindingCase(t *testing.T, graphInstance *testGraphInstanc
11231124

11241125
// Hops should point to the next hop
11251126
for i := 0; i < len(expectedHops)-1; i++ {
1126-
var expectedHop [8]byte
1127-
binary.BigEndian.PutUint64(expectedHop[:], route.Hops[i+1].ChannelID)
1128-
1129-
hopData, err := sphinxPath[i].HopPayload.HopData()
1130-
if err != nil {
1131-
t.Fatalf("unable to make hop data: %v", err)
1132-
}
1127+
payload, _, err := switchhop.ParseTLVPayload(
1128+
bytes.NewReader(sphinxPath[i].HopPayload.Payload),
1129+
)
1130+
require.NoError(t, err)
11331131

1134-
if !bytes.Equal(hopData.NextAddress[:], expectedHop[:]) {
1135-
t.Fatalf("first hop has incorrect next hop: expected %x, got %x",
1136-
expectedHop[:], hopData.NextAddress[:])
1137-
}
1132+
require.Equal(
1133+
t, route.Hops[i+1].ChannelID,
1134+
payload.FwdInfo.NextHop.ToUint64(),
1135+
)
11381136
}
11391137

1140-
// The final hop should have a next hop value of all zeroes in order
1141-
// to indicate it's the exit hop.
1142-
var exitHop [8]byte
11431138
lastHopIndex := len(expectedHops) - 1
11441139

1145-
hopData, err := sphinxPath[lastHopIndex].HopPayload.HopData()
1146-
require.NoError(t, err, "unable to create hop data")
1140+
payload, _, err := switchhop.ParseTLVPayload(
1141+
bytes.NewReader(sphinxPath[lastHopIndex].HopPayload.Payload),
1142+
)
1143+
require.NoError(t, err)
11471144

1148-
if !bytes.Equal(hopData.NextAddress[:], exitHop[:]) {
1149-
t.Fatalf("first hop has incorrect next hop: expected %x, got %x",
1150-
exitHop[:], hopData.NextAddress)
1151-
}
1145+
// The final hop should have a next hop value of all zeroes in order
1146+
// to indicate it's the exit hop.
1147+
require.Zero(t, payload.FwdInfo.NextHop.ToUint64())
11521148

11531149
var expectedTotalFee lnwire.MilliSatoshi
11541150
for i := 0; i < expectedHopCount; i++ {

0 commit comments

Comments
 (0)