Skip to content

Commit 6f312d4

Browse files
authored
Merge pull request #9451 from Juneezee/minmax
refactor: replace min/max helpers with built-in min/max
2 parents 6bf6603 + 82a221b commit 6f312d4

File tree

12 files changed

+15
-137
lines changed

12 files changed

+15
-137
lines changed

autopilot/simple_graph.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,7 @@ func NewSimpleGraph(g ChannelGraph) (*SimpleGraph, error) {
8585
func maxVal(mapping map[int]uint32) uint32 {
8686
maxValue := uint32(0)
8787
for _, value := range mapping {
88-
if maxValue < value {
89-
maxValue = value
90-
}
88+
maxValue = max(maxValue, value)
9189
}
9290
return maxValue
9391
}

contractcourt/commit_sweep_resolver.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/binary"
55
"fmt"
66
"io"
7-
"math"
87
"sync"
98

109
"github.com/btcsuite/btcd/btcutil"
@@ -391,9 +390,7 @@ func (c *commitSweepResolver) Launch() error {
391390
// expires after.
392391
unlockHeight := confHeight + c.commitResolution.MaturityDelay
393392
if c.hasCLTV() {
394-
unlockHeight = uint32(math.Max(
395-
float64(unlockHeight), float64(c.leaseExpiry),
396-
))
393+
unlockHeight = max(unlockHeight, c.leaseExpiry)
397394
}
398395

399396
// Update report now that we learned the confirmation height.

contractcourt/htlc_lease_resolver.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package contractcourt
22

33
import (
4-
"math"
5-
64
"github.com/btcsuite/btcd/wire"
75
"github.com/lightningnetwork/lnd/chainntnfs"
86
"github.com/lightningnetwork/lnd/channeldb"
@@ -42,9 +40,7 @@ func (h *htlcLeaseResolver) deriveWaitHeight(csvDelay uint32,
4240

4341
waitHeight := uint32(commitSpend.SpendingHeight) + csvDelay - 1
4442
if h.hasCLTV() {
45-
waitHeight = uint32(math.Max(
46-
float64(waitHeight), float64(h.leaseExpiry),
47-
))
43+
waitHeight = max(waitHeight, h.leaseExpiry)
4844
}
4945

5046
return waitHeight

docs/release-notes/release-notes-0.19.0.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,9 @@ The underlying functionality between those two options remain the same.
329329
to the sweeper to improve code quality, with a renaming of the internal state
330330
(`Failed` -> `Fatal`) used by the inputs tracked in the sweeper.
331331

332+
* A code refactor that [replaces min/max helpers with built-in min/max
333+
functions](https://github.com/lightningnetwork/lnd/pull/9451).
334+
332335
## Tooling and Documentation
333336

334337
* [Improved `lncli create` command help text](https://github.com/lightningnetwork/lnd/pull/9077)

lntypes/comparison.go

Lines changed: 0 additions & 26 deletions
This file was deleted.

lntypes/comparison_test.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

lnwallet/channel.go

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"crypto/sha256"
88
"errors"
99
"fmt"
10-
"math"
1110
"slices"
1211
"sync"
1312

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

@@ -9538,17 +9537,10 @@ func (lc *LightningChannel) IdealCommitFeeRate(netFeeRate, minRelayFeeRate,
95389537
switch lc.channelState.ChanType.HasAnchors() &&
95399538
maxFeeRate > maxAnchorCommitFeeRate {
95409539
case true:
9541-
commitFeeRate = chainfee.SatPerKWeight(
9542-
math.Min(
9543-
float64(netFeeRate),
9544-
float64(maxAnchorCommitFeeRate),
9545-
),
9546-
)
9540+
commitFeeRate = min(netFeeRate, maxAnchorCommitFeeRate)
95479541

95489542
case false:
9549-
commitFeeRate = chainfee.SatPerKWeight(
9550-
math.Min(float64(netFeeRate), float64(maxFeeRate)),
9551-
)
9543+
commitFeeRate = min(netFeeRate, maxFeeRate)
95529544
}
95539545

95549546
if commitFeeRate >= minRelayFeeRate {

routing/unified_edges.go

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/btcsuite/btcd/btcutil"
77
graphdb "github.com/lightningnetwork/lnd/graph/db"
88
"github.com/lightningnetwork/lnd/graph/db/models"
9-
"github.com/lightningnetwork/lnd/lntypes"
109
"github.com/lightningnetwork/lnd/lnwire"
1110
"github.com/lightningnetwork/lnd/routing/route"
1211
)
@@ -379,13 +378,11 @@ func (u *edgeUnifier) getEdgeNetwork(netAmtReceived lnwire.MilliSatoshi,
379378

380379
capMsat = edge.policy.MaxHTLC
381380
}
382-
maxCapMsat = lntypes.Max(capMsat, maxCapMsat)
381+
maxCapMsat = max(capMsat, maxCapMsat)
383382

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

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

441438
// minAmt returns the minimum amount that can be forwarded on this connection.
442439
func (u *edgeUnifier) minAmt() lnwire.MilliSatoshi {
443-
min := lnwire.MaxMilliSatoshi
440+
minAmount := lnwire.MaxMilliSatoshi
444441
for _, edge := range u.edges {
445-
min = lntypes.Min(min, edge.policy.MinHTLC)
442+
minAmount = min(minAmount, edge.policy.MinHTLC)
446443
}
447444

448-
return min
445+
return minAmount
449446
}

sqldb/interfaces.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ func randRetryDelay(initialRetryDelay, maxRetryDelay time.Duration,
183183
// attempt. If we double something n times, that's the same as
184184
// multiplying the value with 2^n. We limit the power to 32 to avoid
185185
// overflows.
186-
factor := time.Duration(math.Pow(2, math.Min(float64(attempt), 32)))
186+
factor := time.Duration(math.Pow(2, min(float64(attempt), 32)))
187187
actualDelay := initialDelay * factor
188188

189189
// Cap the delay at the maximum configured value.

watchtower/wtdb/migration4/range_index.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,24 +74,6 @@ func (a *RangeIndex) addRange(start, end uint64) error {
7474
"than end height %d", start, end)
7575
}
7676

77-
// min is a helper closure that will return the minimum of two uint64s.
78-
min := func(a, b uint64) uint64 {
79-
if a < b {
80-
return a
81-
}
82-
83-
return b
84-
}
85-
86-
// max is a helper closure that will return the maximum of two uint64s.
87-
max := func(a, b uint64) uint64 {
88-
if a > b {
89-
return a
90-
}
91-
92-
return b
93-
}
94-
9577
// Collect the ranges that fall before and after the new range along
9678
// with the start and end values of the new range.
9779
var before, after []rangeItem

0 commit comments

Comments
 (0)