Skip to content

Commit 0dffdea

Browse files
committed
paymentsdb: verify total amount for last hop in the blinded path
1 parent 26e8bcd commit 0dffdea

File tree

3 files changed

+52
-0
lines changed

3 files changed

+52
-0
lines changed

payments/db/errors.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,12 @@ var (
8484
ErrMixedBlindedAndNonBlindedPayments = errors.New("mixed blinded and " +
8585
"non-blinded payments")
8686

87+
// ErrBlindedPaymentMissingTotalAmount is returned if we try to
88+
// register a blinded payment attempt where the final hop doesn't set
89+
// the total amount.
90+
ErrBlindedPaymentMissingTotalAmount = errors.New("blinded payment " +
91+
"final hop must set total amount")
92+
8793
// ErrMPPPaymentAddrMismatch is returned if we try to register an MPP
8894
// shard where the payment address doesn't match existing shards.
8995
ErrMPPPaymentAddrMismatch = errors.New("payment address mismatch")

payments/db/payment.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,13 @@ func verifyAttempt(payment *MPPayment, attempt *HTLCAttemptInfo) error {
744744
// in the split payment is correct.
745745
isBlinded := len(attempt.Route.FinalHop().EncryptedData) != 0
746746

747+
// For blinded payments, the last hop must set the total amount.
748+
if isBlinded {
749+
if attempt.Route.FinalHop().TotalAmtMsat == 0 {
750+
return ErrBlindedPaymentMissingTotalAmount
751+
}
752+
}
753+
747754
// Make sure any existing shards match the new one with regards
748755
// to MPP options.
749756
mpp := attempt.Route.FinalHop().MPP

payments/db/payment_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,6 +1388,45 @@ func TestVerifyAttemptBlindedValidation(t *testing.T) {
13881388
require.NoError(t, verifyAttempt(payment, &matching))
13891389
}
13901390

1391+
// TestVerifyAttemptBlindedMissingTotalAmount tests that we return an error if
1392+
// we try to register a blinded payment attempt where the final hop doesn't set
1393+
// the total amount.
1394+
func TestVerifyAttemptBlindedMissingTotalAmount(t *testing.T) {
1395+
t.Parallel()
1396+
1397+
total := lnwire.MilliSatoshi(5000)
1398+
1399+
// Payment with no existing attempts.
1400+
payment := makePayment(total)
1401+
1402+
// Attempt with encrypted data (blinded payment) but missing total
1403+
// amount.
1404+
attemptMissingTotal := makeLastHopAttemptInfo(
1405+
1,
1406+
lastHopArgs{
1407+
amt: 2500,
1408+
total: 0,
1409+
encrypted: []byte{1, 2, 3},
1410+
},
1411+
)
1412+
require.ErrorIs(
1413+
t,
1414+
verifyAttempt(payment, &attemptMissingTotal),
1415+
ErrBlindedPaymentMissingTotalAmount,
1416+
)
1417+
1418+
// Attempt with encrypted data and valid total amount should succeed.
1419+
attemptWithTotal := makeLastHopAttemptInfo(
1420+
2,
1421+
lastHopArgs{
1422+
amt: 2500,
1423+
total: total,
1424+
encrypted: []byte{4, 5, 6},
1425+
},
1426+
)
1427+
require.NoError(t, verifyAttempt(payment, &attemptWithTotal))
1428+
}
1429+
13911430
// TestVerifyAttemptBlindedMixedWithNonBlinded tests that we return an error if
13921431
// we try to register a non-MPP attempt for a blinded payment.
13931432
func TestVerifyAttemptBlindedMixedWithNonBlinded(t *testing.T) {

0 commit comments

Comments
 (0)