Skip to content

Commit 20a5267

Browse files
committed
sphinx: increase NumMaxHops to accomodate for tlv payloads
1 parent 7964cdf commit 20a5267

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

path.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -304,11 +304,15 @@ func (hp *HopPayload) HopData() (*HopData, error) {
304304
return &hd, nil
305305
}
306306

307-
// NumMaxHops is the maximum path length. This should be set to an estimate of
308-
// the upper limit of the diameter of the node graph.
309-
//
310-
// TODO(roasbeef): adjust due to var-payloads?
311-
const NumMaxHops = 20
307+
// NumMaxHops is the maximum path length. There is a maximum of 1300 bytes in
308+
// the routing info block. Legacy hop payloads are always 65 bytes, while tlv
309+
// payloads are at least 47 bytes (tlvlen 1, amt 2, timelock 2, nextchan 10,
310+
// hmac 32) for the intermediate hops and 37 bytes (tlvlen 1, amt 2, timelock 2,
311+
// hmac 32) for the exit hop. The maximum path length can therefore only be
312+
// reached by using tlv payloads only. With that, the maximum number of
313+
// intermediate hops is: Floor((1300 - 37) / 47) = 26. Including the exit hop,
314+
// the maximum path length is 27 hops.
315+
const NumMaxHops = 27
312316

313317
// PaymentPath represents a series of hops within the Lightning Network
314318
// starting at a sender and terminating at a receiver. Each hop contains a set

sphinx_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ var (
8686
"2cf4f0731da13b8546d1d6d4f8d75b9fce6c2341a71b0ea6f780" +
8787
"df54bfdb0dd5cd9855179f602f917265f21f9190c70217774a6f" +
8888
"baaa7d63ad64199f4664813b955cff954949076dcf"
89+
90+
testLegacyRouteNumHops = 20
8991
)
9092

9193
func newTestRoute(numHops int) ([]*Router, *PaymentPath, *[]HopData, *OnionPacket, error) {
@@ -214,7 +216,7 @@ func TestBolt4Packet(t *testing.T) {
214216
}
215217

216218
func TestSphinxCorrectness(t *testing.T) {
217-
nodes, _, hopDatas, fwdMsg, err := newTestRoute(NumMaxHops)
219+
nodes, _, hopDatas, fwdMsg, err := newTestRoute(testLegacyRouteNumHops)
218220
if err != nil {
219221
t.Fatalf("unable to create random onion packet: %v", err)
220222
}
@@ -307,7 +309,7 @@ func TestSphinxSingleHop(t *testing.T) {
307309
func TestSphinxNodeRelpay(t *testing.T) {
308310
// We'd like to ensure that the sphinx node itself rejects all replayed
309311
// packets which share the same shared secret.
310-
nodes, _, _, fwdMsg, err := newTestRoute(NumMaxHops)
312+
nodes, _, _, fwdMsg, err := newTestRoute(testLegacyRouteNumHops)
311313
if err != nil {
312314
t.Fatalf("unable to create test route: %v", err)
313315
}
@@ -332,7 +334,7 @@ func TestSphinxNodeRelpay(t *testing.T) {
332334
func TestSphinxNodeRelpaySameBatch(t *testing.T) {
333335
// We'd like to ensure that the sphinx node itself rejects all replayed
334336
// packets which share the same shared secret.
335-
nodes, _, _, fwdMsg, err := newTestRoute(NumMaxHops)
337+
nodes, _, _, fwdMsg, err := newTestRoute(testLegacyRouteNumHops)
336338
if err != nil {
337339
t.Fatalf("unable to create test route: %v", err)
338340
}
@@ -378,7 +380,7 @@ func TestSphinxNodeRelpaySameBatch(t *testing.T) {
378380
func TestSphinxNodeRelpayLaterBatch(t *testing.T) {
379381
// We'd like to ensure that the sphinx node itself rejects all replayed
380382
// packets which share the same shared secret.
381-
nodes, _, _, fwdMsg, err := newTestRoute(NumMaxHops)
383+
nodes, _, _, fwdMsg, err := newTestRoute(testLegacyRouteNumHops)
382384
if err != nil {
383385
t.Fatalf("unable to create test route: %v", err)
384386
}
@@ -423,7 +425,7 @@ func TestSphinxNodeRelpayLaterBatch(t *testing.T) {
423425
func TestSphinxNodeReplayBatchIdempotency(t *testing.T) {
424426
// We'd like to ensure that the sphinx node itself rejects all replayed
425427
// packets which share the same shared secret.
426-
nodes, _, _, fwdMsg, err := newTestRoute(NumMaxHops)
428+
nodes, _, _, fwdMsg, err := newTestRoute(testLegacyRouteNumHops)
427429
if err != nil {
428430
t.Fatalf("unable to create test route: %v", err)
429431
}

0 commit comments

Comments
 (0)