Skip to content

Commit

Permalink
cmd/commands: add validation for MPP parameters
Browse files Browse the repository at this point in the history
Adds validation to ensure that the combination of max_parts and
max_shard_size_msat parameters can accommodate the full payment amount
before attempting the payment. This prevents payments from entering a path
finding loop that would eventually timeout when parameters are incompatible.
  • Loading branch information
anibilthare committed Nov 1, 2024
1 parent acbb33b commit 2de5713
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion lnrpc/routerrpc/router_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -800,6 +800,30 @@ func (r *RouterBackend) UnmarshallRoute(rpcroute *lnrpc.Route) (
return route, nil
}

// maybeValidateMPPParams validates that the MPP parameters are compatible with the
// payment amount. It returns an error if the parameters don't allow the full
// payment amount to be sent.
// maybeValidateMPPParams could be enhanced with additional checks

Check failure on line 806 in lnrpc/routerrpc/router_backend.go

View workflow job for this annotation

GitHub Actions / lint code

Comment should end in a period (godot)
func maybeValidateMPPParams(amt lnwire.MilliSatoshi, maxParts uint32,
maxShardSize lnwire.MilliSatoshi) error {

// Early return if MPP is not being used
if maxShardSize == 0 {
return nil
}

// Calculate maximum possible amount
maxPossibleAmount := lnwire.MilliSatoshi(maxParts) * maxShardSize

if amt > maxPossibleAmount {
return fmt.Errorf("payment amount %v exceeds maximum possible "+
"amount %v with max_parts=%v and max_shard_size_msat=%v",
amt, maxPossibleAmount, maxParts, maxShardSize)
}

return nil
}

// extractIntentFromSendRequest attempts to parse the SendRequest details
// required to dispatch a client from the information presented by an RPC
// client.
Expand Down Expand Up @@ -1174,7 +1198,11 @@ func (r *RouterBackend) extractIntentFromSendRequest(

payIntent.DestFeatures = features
}

err = maybeValidateMPPParams(
payIntent.Amount, maxParts, lnwire.MilliSatoshi(rpcPayReq.MaxShardSizeMsat))
if err != nil {
return nil, fmt.Errorf("invalid MPP parameters: %v", err)

Check failure on line 1204 in lnrpc/routerrpc/router_backend.go

View workflow job for this annotation

GitHub Actions / lint code

non-wrapping format verb for fmt.Errorf. Use `%w` to format errors (errorlint)
}
// Do bounds checking with the block padding so the router isn't
// left with a zombie payment in case the user messes up.
err = routing.ValidateCLTVLimit(
Expand Down

0 comments on commit 2de5713

Please sign in to comment.