Skip to content

Commit

Permalink
Merge pull request #9451 from Juneezee/minmax
Browse files Browse the repository at this point in the history
refactor: replace min/max helpers with built-in min/max
  • Loading branch information
guggero authored Feb 10, 2025
2 parents 6bf6603 + 82a221b commit 6f312d4
Show file tree
Hide file tree
Showing 12 changed files with 15 additions and 137 deletions.
4 changes: 1 addition & 3 deletions autopilot/simple_graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,7 @@ func NewSimpleGraph(g ChannelGraph) (*SimpleGraph, error) {
func maxVal(mapping map[int]uint32) uint32 {
maxValue := uint32(0)
for _, value := range mapping {
if maxValue < value {
maxValue = value
}
maxValue = max(maxValue, value)
}
return maxValue
}
Expand Down
5 changes: 1 addition & 4 deletions contractcourt/commit_sweep_resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/binary"
"fmt"
"io"
"math"
"sync"

"github.com/btcsuite/btcd/btcutil"
Expand Down Expand Up @@ -391,9 +390,7 @@ func (c *commitSweepResolver) Launch() error {
// expires after.
unlockHeight := confHeight + c.commitResolution.MaturityDelay
if c.hasCLTV() {
unlockHeight = uint32(math.Max(
float64(unlockHeight), float64(c.leaseExpiry),
))
unlockHeight = max(unlockHeight, c.leaseExpiry)
}

// Update report now that we learned the confirmation height.
Expand Down
6 changes: 1 addition & 5 deletions contractcourt/htlc_lease_resolver.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package contractcourt

import (
"math"

"github.com/btcsuite/btcd/wire"
"github.com/lightningnetwork/lnd/chainntnfs"
"github.com/lightningnetwork/lnd/channeldb"
Expand Down Expand Up @@ -42,9 +40,7 @@ func (h *htlcLeaseResolver) deriveWaitHeight(csvDelay uint32,

waitHeight := uint32(commitSpend.SpendingHeight) + csvDelay - 1
if h.hasCLTV() {
waitHeight = uint32(math.Max(
float64(waitHeight), float64(h.leaseExpiry),
))
waitHeight = max(waitHeight, h.leaseExpiry)
}

return waitHeight
Expand Down
3 changes: 3 additions & 0 deletions docs/release-notes/release-notes-0.19.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,9 @@ The underlying functionality between those two options remain the same.
to the sweeper to improve code quality, with a renaming of the internal state
(`Failed` -> `Fatal`) used by the inputs tracked in the sweeper.

* A code refactor that [replaces min/max helpers with built-in min/max
functions](https://github.com/lightningnetwork/lnd/pull/9451).

## Tooling and Documentation

* [Improved `lncli create` command help text](https://github.com/lightningnetwork/lnd/pull/9077)
Expand Down
26 changes: 0 additions & 26 deletions lntypes/comparison.go

This file was deleted.

25 changes: 0 additions & 25 deletions lntypes/comparison_test.go

This file was deleted.

14 changes: 3 additions & 11 deletions lnwallet/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"crypto/sha256"
"errors"
"fmt"
"math"
"slices"
"sync"

Expand Down Expand Up @@ -9512,7 +9511,7 @@ func (lc *LightningChannel) MaxFeeRate(
// rather than us decreasing in local balance. The max fee rate is
// always floored by the current fee rate of the channel.
idealMaxFee := float64(baseBalance) * maxAllocation
maxFee := math.Max(float64(currentFee), idealMaxFee)
maxFee := max(float64(currentFee), idealMaxFee)
maxFeeAllocation := maxFee / float64(baseBalance)
maxFeeRate := chainfee.SatPerKWeight(maxFee / (float64(weight) / 1000))

Expand All @@ -9538,17 +9537,10 @@ func (lc *LightningChannel) IdealCommitFeeRate(netFeeRate, minRelayFeeRate,
switch lc.channelState.ChanType.HasAnchors() &&
maxFeeRate > maxAnchorCommitFeeRate {
case true:
commitFeeRate = chainfee.SatPerKWeight(
math.Min(
float64(netFeeRate),
float64(maxAnchorCommitFeeRate),
),
)
commitFeeRate = min(netFeeRate, maxAnchorCommitFeeRate)

case false:
commitFeeRate = chainfee.SatPerKWeight(
math.Min(float64(netFeeRate), float64(maxFeeRate)),
)
commitFeeRate = min(netFeeRate, maxFeeRate)
}

if commitFeeRate >= minRelayFeeRate {
Expand Down
13 changes: 5 additions & 8 deletions routing/unified_edges.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/btcsuite/btcd/btcutil"
graphdb "github.com/lightningnetwork/lnd/graph/db"
"github.com/lightningnetwork/lnd/graph/db/models"
"github.com/lightningnetwork/lnd/lntypes"
"github.com/lightningnetwork/lnd/lnwire"
"github.com/lightningnetwork/lnd/routing/route"
)
Expand Down Expand Up @@ -379,13 +378,11 @@ func (u *edgeUnifier) getEdgeNetwork(netAmtReceived lnwire.MilliSatoshi,

capMsat = edge.policy.MaxHTLC
}
maxCapMsat = lntypes.Max(capMsat, maxCapMsat)
maxCapMsat = max(capMsat, maxCapMsat)

// Track the maximum time lock of all channels that are
// candidate for non-strict forwarding at the routing node.
maxTimelock = lntypes.Max(
maxTimelock, edge.policy.TimeLockDelta,
)
maxTimelock = max(maxTimelock, edge.policy.TimeLockDelta)

outboundFee := int64(edge.policy.ComputeFee(amt))
fee := outboundFee + inboundFee
Expand Down Expand Up @@ -440,10 +437,10 @@ func (u *edgeUnifier) getEdgeNetwork(netAmtReceived lnwire.MilliSatoshi,

// minAmt returns the minimum amount that can be forwarded on this connection.
func (u *edgeUnifier) minAmt() lnwire.MilliSatoshi {
min := lnwire.MaxMilliSatoshi
minAmount := lnwire.MaxMilliSatoshi
for _, edge := range u.edges {
min = lntypes.Min(min, edge.policy.MinHTLC)
minAmount = min(minAmount, edge.policy.MinHTLC)
}

return min
return minAmount
}
2 changes: 1 addition & 1 deletion sqldb/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ func randRetryDelay(initialRetryDelay, maxRetryDelay time.Duration,
// attempt. If we double something n times, that's the same as
// multiplying the value with 2^n. We limit the power to 32 to avoid
// overflows.
factor := time.Duration(math.Pow(2, math.Min(float64(attempt), 32)))
factor := time.Duration(math.Pow(2, min(float64(attempt), 32)))
actualDelay := initialDelay * factor

// Cap the delay at the maximum configured value.
Expand Down
18 changes: 0 additions & 18 deletions watchtower/wtdb/migration4/range_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,6 @@ func (a *RangeIndex) addRange(start, end uint64) error {
"than end height %d", start, end)
}

// min is a helper closure that will return the minimum of two uint64s.
min := func(a, b uint64) uint64 {
if a < b {
return a
}

return b
}

// max is a helper closure that will return the maximum of two uint64s.
max := func(a, b uint64) uint64 {
if a > b {
return a
}

return b
}

// Collect the ranges that fall before and after the new range along
// with the start and end values of the new range.
var before, after []rangeItem
Expand Down
18 changes: 0 additions & 18 deletions watchtower/wtdb/migration8/range_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,6 @@ func (a *RangeIndex) addRange(start, end uint64) error {
"than end height %d", start, end)
}

// min is a helper closure that will return the minimum of two uint64s.
min := func(a, b uint64) uint64 {
if a < b {
return a
}

return b
}

// max is a helper closure that will return the maximum of two uint64s.
max := func(a, b uint64) uint64 {
if a > b {
return a
}

return b
}

// Collect the ranges that fall before and after the new range along
// with the start and end values of the new range.
var before, after []rangeItem
Expand Down
18 changes: 0 additions & 18 deletions watchtower/wtdb/range_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,6 @@ func (a *RangeIndex) addRange(start, end uint64) error {
"than end height %d", start, end)
}

// min is a helper closure that will return the minimum of two uint64s.
min := func(a, b uint64) uint64 {
if a < b {
return a
}

return b
}

// max is a helper closure that will return the maximum of two uint64s.
max := func(a, b uint64) uint64 {
if a > b {
return a
}

return b
}

// Collect the ranges that fall before and after the new range along
// with the start and end values of the new range.
var before, after []rangeItem
Expand Down

0 comments on commit 6f312d4

Please sign in to comment.