Skip to content

Commit

Permalink
routing: assume TLV onion during route construction
Browse files Browse the repository at this point in the history
  • Loading branch information
ellemouton committed Jun 24, 2024
1 parent 738253f commit 99b3c57
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 37 deletions.
16 changes: 1 addition & 15 deletions routing/pathfind.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,6 @@ func newRoute(sourceVertex route.Vertex,
fee int64
totalAmtMsatBlinded lnwire.MilliSatoshi
outgoingTimeLock uint32
tlvPayload bool
customRecords record.CustomSet
mpp *record.MPP
metadata []byte
Expand All @@ -180,13 +179,6 @@ func newRoute(sourceVertex route.Vertex,
return edge.ToNodeFeatures.HasFeature(feature)
}

// We start by assuming the node doesn't support TLV. We'll now
// inspect the node's feature vector to see if we can promote
// the hop. We assume already that the feature vector's
// transitive dependencies have already been validated by path
// finding or some other means.
tlvPayload = supports(lnwire.TLVOnionPayloadOptional)

if i == len(pathEdges)-1 {
// If this is the last hop, then the hop payload will
// contain the exact amount. In BOLT #4: Onion Routing
Expand All @@ -204,12 +196,7 @@ func newRoute(sourceVertex route.Vertex,
totalTimeLock += uint32(finalHop.cltvDelta)
outgoingTimeLock = totalTimeLock

// Attach any custom records to the final hop if the
// receiver supports TLV.
if !tlvPayload && finalHop.records != nil {
return nil, errors.New("cannot attach " +
"custom records")
}
// Attach any custom records to the final hop.
customRecords = finalHop.records

// If we're attaching a payment addr but the receiver
Expand Down Expand Up @@ -275,7 +262,6 @@ func newRoute(sourceVertex route.Vertex,
ChannelID: edge.ChannelID,
AmtToForward: amtToForward,
OutgoingTimeLock: outgoingTimeLock,
LegacyPayload: !tlvPayload,
CustomRecords: customRecords,
MPP: mpp,
Metadata: metadata,
Expand Down
40 changes: 18 additions & 22 deletions routing/pathfind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package routing
import (
"bytes"
"crypto/sha256"
"encoding/binary"
"encoding/hex"
"encoding/json"
"errors"
Expand All @@ -25,6 +24,7 @@ import (
"github.com/lightningnetwork/lnd/channeldb"
"github.com/lightningnetwork/lnd/channeldb/models"
"github.com/lightningnetwork/lnd/htlcswitch"
switchhop "github.com/lightningnetwork/lnd/htlcswitch/hop"
"github.com/lightningnetwork/lnd/kvdb"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/record"
Expand Down Expand Up @@ -1030,7 +1030,8 @@ var basicGraphPathFindingTests = []basicGraphPathFindingTestCase{
// Basic route with fee limit.
{target: "sophon", paymentAmt: 100, feeLimit: 50,
expectFailureNoPath: true,
}}
},
}

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

// Hops should point to the next hop
for i := 0; i < len(expectedHops)-1; i++ {
var expectedHop [8]byte
binary.BigEndian.PutUint64(expectedHop[:], route.Hops[i+1].ChannelID)

hopData, err := sphinxPath[i].HopPayload.HopData()
if err != nil {
t.Fatalf("unable to make hop data: %v", err)
}
payload, _, err := switchhop.ParseTLVPayload(
bytes.NewReader(sphinxPath[i].HopPayload.Payload),
)
require.NoError(t, err)

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

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

hopData, err := sphinxPath[lastHopIndex].HopPayload.HopData()
require.NoError(t, err, "unable to create hop data")
payload, _, err := switchhop.ParseTLVPayload(
bytes.NewReader(sphinxPath[lastHopIndex].HopPayload.Payload),
)
require.NoError(t, err)

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

var expectedTotalFee lnwire.MilliSatoshi
for i := 0; i < expectedHopCount; i++ {
Expand Down

0 comments on commit 99b3c57

Please sign in to comment.