Skip to content

Allow zero amount paths with MinHTLC policies #10029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/release-notes/release-notes-0.20.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
https://github.com/lightningnetwork/lnd/pull/9815). This ensures the original
announcement remains unchanged until the new one is fully signed and
validated.
- [Fixed creation of invoices with zero amount and blinded paths](
https://github.com/lightningnetwork/lnd/pull/10029).

# New Features

Expand Down Expand Up @@ -170,5 +172,6 @@ reader of a payment request.
* Erick Cestari
* Funyug
* Mohamed Awnallah
* Maurice Poirrier
* Pins
* Ziggie
15 changes: 11 additions & 4 deletions routing/blindedpath/blinded_path.go
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,13 @@ func collectRelayInfo(cfg *BuildBlindedPathCfg, path *candidatePath) (
}
}

if policy.MinHTLCMsat > cfg.ValueMsat {
// If the payment amount is 0, means the sender is deciding the
// amount, so we don't need to check the min HTLC value while
// building the path. Payment would fail if there is no
// route with the min HTLC value from the sender's perspective.
isZeroAmount := cfg.ValueMsat == 0

if !isZeroAmount && policy.MinHTLCMsat > cfg.ValueMsat {
return nil, 0, 0, fmt.Errorf("%w: minHTLC of hop "+
"policy larger than payment amt: sentAmt(%v), "+
"minHTLC(%v)", errInvalidBlindedPath,
Expand All @@ -450,12 +456,13 @@ func collectRelayInfo(cfg *BuildBlindedPathCfg, path *candidatePath) (
return nil, 0, 0, err
}

// We only use the new buffered policy if the new minHTLC value
// does not violate the sender amount.
// We only use the new buffered policy if:
// 1) Sender amount is 0, or
// 2) The new minHTLC value does not violate the sender amount.
//
// NOTE: We don't check this for maxHTLC, because the payment
// amount can always be splitted using MPP.
if bufferPolicy.MinHTLCMsat <= cfg.ValueMsat {
if isZeroAmount || bufferPolicy.MinHTLCMsat <= cfg.ValueMsat {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we explicitly need to set bufferPolicy when the value is 0 here. If we leave it as is, and when the policy's minHtlc is zero (assuming zero can be a valid value), the bufferPolicy will return the new minHtlc as 0, which will equal cfg.ValueMsat in this case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh I see, the AddPolicyBuffer expects a value greater than zero as the multiplier for minHtlc, so if the policy passed has zero for minHtlc, it errors out.

policy = bufferPolicy
}

Expand Down
107 changes: 107 additions & 0 deletions routing/blindedpath/blinded_path_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1059,3 +1059,110 @@ func decryptAndDecodeHopData(t *testing.T, priv *btcec.PrivateKey,

return routeData, nextEphem
}

// TestBuildBlindedPathZeroAmount tests that blinded paths can be built
// successfully when ValueMsat is 0 (zero amount), even when channels have
// MinHTLC policies > 0. This is important for building blinded paths invoices
// for zero amount payments.
func TestBuildBlindedPathZeroAmount(t *testing.T) {
// Alice chooses the following path to herself for blinded path
// construction:
// Carol -> Bob -> Alice.
var (
_, pkC = btcec.PrivKeyFromBytes([]byte{1})
_, pkB = btcec.PrivKeyFromBytes([]byte{2})
_, pkA = btcec.PrivKeyFromBytes([]byte{3})

carol = route.NewVertex(pkC)
bob = route.NewVertex(pkB)
alice = route.NewVertex(pkA)

chanCB = uint64(1)
chanBA = uint64(2)
)

realRoute := &route.Route{
SourcePubKey: carol,
Hops: []*route.Hop{
{
PubKeyBytes: bob,
ChannelID: chanCB,
},
{
PubKeyBytes: alice,
ChannelID: chanBA,
},
},
}

realPolicies := map[uint64]*models.ChannelEdgePolicy{
chanCB: {
ChannelID: chanCB,
ToNode: bob,
MinHTLC: 1000, // MinHTLC > 0
MaxHTLC: lnwire.MaxMilliSatoshi,
FeeBaseMSat: 100,
FeeProportionalMillionths: 500,
TimeLockDelta: 144,
},
chanBA: {
ChannelID: chanBA,
ToNode: alice,
MinHTLC: 500, // MinHTLC > 0
MaxHTLC: lnwire.MaxMilliSatoshi,
FeeBaseMSat: 50,
FeeProportionalMillionths: 250,
TimeLockDelta: 72,
},
}

paths, err := BuildBlindedPaymentPaths(&BuildBlindedPathCfg{
FindRoutes: func(_ lnwire.MilliSatoshi) ([]*route.Route,
error) {

return []*route.Route{realRoute}, nil
},
FetchChannelEdgesByID: func(chanID uint64) (
*models.ChannelEdgeInfo, *models.ChannelEdgePolicy,
*models.ChannelEdgePolicy, error) {

return nil, realPolicies[chanID], nil, nil
},
BestHeight: func() (uint32, error) {
return 1000, nil
},
AddPolicyBuffer: func(policy *BlindedHopPolicy) (
*BlindedHopPolicy, error) {
// We don't mind about maxHTLCs
return AddPolicyBuffer(policy, 1.5, 0.0)
},
PathID: []byte{1, 2, 3},
ValueMsat: 0,
MinFinalCLTVExpiryDelta: 12,
BlocksUntilExpiry: 200,
})

require.NoError(t, err)
require.Len(t, paths, 1, "Should return exactly one blinded path")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: additional context strings can be omitted.


path := paths[0]
require.Len(
t,
path.Hops, 3,
"Path should have 3 hops (intro + 2 relay hops)",
)

hop := path.Hops[0]
require.True(
t,
hop.BlindedNodePub.IsEqual(pkC),
"First hop should be Carol (introduction node)",
)

require.EqualValues(
t,
path.HTLCMinMsat,
uint64(1500),
"Path MinHTLC should be 1500 with 1.5x policy buffer applied",
)
}
Loading