mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-29 15:11:09 +02:00
lntypes+routing: add generic Min/Max functions
This commit is contained in:
26
lntypes/comparison.go
Normal file
26
lntypes/comparison.go
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
package lntypes
|
||||||
|
|
||||||
|
import "golang.org/x/exp/constraints"
|
||||||
|
|
||||||
|
// Number defines a type constraint for numbers.
|
||||||
|
type Number interface {
|
||||||
|
constraints.Integer | constraints.Float
|
||||||
|
}
|
||||||
|
|
||||||
|
// Max returns the greater of the two inputs.
|
||||||
|
func Max[N Number](op1 N, op2 N) N {
|
||||||
|
if op1 > op2 {
|
||||||
|
return op1
|
||||||
|
}
|
||||||
|
|
||||||
|
return op2
|
||||||
|
}
|
||||||
|
|
||||||
|
// Min returns the lesser of the two inputs.
|
||||||
|
func Min[N Number](op1 N, op2 N) N {
|
||||||
|
if op1 < op2 {
|
||||||
|
return op1
|
||||||
|
}
|
||||||
|
|
||||||
|
return op2
|
||||||
|
}
|
25
lntypes/comparison_test.go
Normal file
25
lntypes/comparison_test.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package lntypes
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestMin tests getting correct minimal numbers.
|
||||||
|
func TestMin(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
require.Equal(t, 1, Min(1, 1))
|
||||||
|
require.Equal(t, 1, Min(1, 2))
|
||||||
|
require.Equal(t, 1.5, Min(1.5, 2.5))
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestMax tests getting correct maximal numbers.
|
||||||
|
func TestMax(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
require.Equal(t, 1, Max(1, 1))
|
||||||
|
require.Equal(t, 2, Max(1, 2))
|
||||||
|
require.Equal(t, 2.5, Max(1.5, 2.5))
|
||||||
|
}
|
@@ -3,6 +3,7 @@ package routing
|
|||||||
import (
|
import (
|
||||||
"github.com/btcsuite/btcd/btcutil"
|
"github.com/btcsuite/btcd/btcutil"
|
||||||
"github.com/lightningnetwork/lnd/channeldb"
|
"github.com/lightningnetwork/lnd/channeldb"
|
||||||
|
"github.com/lightningnetwork/lnd/lntypes"
|
||||||
"github.com/lightningnetwork/lnd/lnwire"
|
"github.com/lightningnetwork/lnd/lnwire"
|
||||||
"github.com/lightningnetwork/lnd/routing/route"
|
"github.com/lightningnetwork/lnd/routing/route"
|
||||||
)
|
)
|
||||||
@@ -223,9 +224,9 @@ func (u *edgeUnifier) getEdgeNetwork(amt lnwire.MilliSatoshi) *unifiedEdge {
|
|||||||
|
|
||||||
// Track the maximum time lock of all channels that are
|
// Track the maximum time lock of all channels that are
|
||||||
// candidate for non-strict forwarding at the routing node.
|
// candidate for non-strict forwarding at the routing node.
|
||||||
if edge.policy.TimeLockDelta > maxTimelock {
|
maxTimelock = lntypes.Max(
|
||||||
maxTimelock = edge.policy.TimeLockDelta
|
maxTimelock, edge.policy.TimeLockDelta,
|
||||||
}
|
)
|
||||||
|
|
||||||
// Use the policy that results in the highest fee for this
|
// Use the policy that results in the highest fee for this
|
||||||
// specific amount.
|
// specific amount.
|
||||||
@@ -264,9 +265,7 @@ func (u *edgeUnifier) getEdgeNetwork(amt lnwire.MilliSatoshi) *unifiedEdge {
|
|||||||
func (u *edgeUnifier) minAmt() lnwire.MilliSatoshi {
|
func (u *edgeUnifier) minAmt() lnwire.MilliSatoshi {
|
||||||
min := lnwire.MaxMilliSatoshi
|
min := lnwire.MaxMilliSatoshi
|
||||||
for _, edge := range u.edges {
|
for _, edge := range u.edges {
|
||||||
if edge.policy.MinHTLC < min {
|
min = lntypes.Min(min, edge.policy.MinHTLC)
|
||||||
min = edge.policy.MinHTLC
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return min
|
return min
|
||||||
|
Reference in New Issue
Block a user