Skip to content

Commit 9a34a02

Browse files
authored
Merge pull request #785 from careworry/master
refactor: use the built-in max/min to simplify the code
2 parents b706d57 + bba0e3b commit 9a34a02

File tree

9 files changed

+14
-48
lines changed

9 files changed

+14
-48
lines changed

rocketpool-cli/node/status.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,8 @@ func getStatus(c *cli.Context) error {
347347
remainingAmount := big.NewInt(0).Sub(status.EthMatchedLimit, status.EthMatched)
348348
remainingAmount.Sub(remainingAmount, status.PendingMatchAmount)
349349
remainingAmountEth := int(eth.WeiToEth(remainingAmount))
350-
remainingFor8EB := remainingAmountEth / 24
351-
if remainingFor8EB < 0 {
352-
remainingFor8EB = 0
353-
}
354-
remainingFor16EB := remainingAmountEth / 16
355-
if remainingFor16EB < 0 {
356-
remainingFor16EB = 0
357-
}
350+
remainingFor8EB := max(remainingAmountEth/24, 0)
351+
remainingFor16EB := max(remainingAmountEth/16, 0)
358352
fmt.Printf("The node has enough RPL staked to make %d more 8-ETH minipools (or %d more 16-ETH minipools).\n\n", remainingFor8EB, remainingFor16EB)
359353
}
360354

rocketpool-cli/service/config/directional-modal.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,7 @@ func (m *DirectionalModal) Draw(screen tcell.Screen) {
255255
}
256256
buttonsWidth -= 2
257257
screenWidth, screenHeight := screen.Size()
258-
width := screenWidth / 3
259-
if width < buttonsWidth {
260-
width = buttonsWidth
261-
}
258+
width := max(screenWidth/3, buttonsWidth)
262259
// width is now without the box border.
263260

264261
// Reset the text and find out how wide it is.

rocketpool-cli/service/config/dropdown.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,10 +333,7 @@ func (d *DropDown) Draw(screen tcell.Screen) {
333333

334334
// Draw label.
335335
if d.labelWidth > 0 {
336-
labelWidth := d.labelWidth
337-
if labelWidth > rightLimit-x {
338-
labelWidth = rightLimit - x
339-
}
336+
labelWidth := min(d.labelWidth, rightLimit-x)
340337
tview.Print(screen, d.label, x, y, labelWidth, tview.AlignLeft, d.labelColor)
341338
x += labelWidth
342339
} else {
@@ -414,10 +411,7 @@ func (d *DropDown) Draw(screen tcell.Screen) {
414411
lheight := len(d.options)
415412
_, sheight := screen.Size()
416413
if ly+lheight >= sheight && ly-2 > lheight-ly {
417-
ly = y - lheight
418-
if ly < 0 {
419-
ly = 0
420-
}
414+
ly = max(y-lheight, 0)
421415
}
422416
if ly+lheight >= sheight {
423417
lheight = sheight - ly

rocketpool-cli/service/config/pseudomodal.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,7 @@ func (m *Pseudomodal) Draw(screen tcell.Screen) {
257257
}
258258
buttonsWidth -= 2
259259
screenWidth, screenHeight := screen.Size()
260-
width := screenWidth / 3
261-
if width < buttonsWidth {
262-
width = buttonsWidth
263-
}
260+
width := max(screenWidth/3, buttonsWidth)
264261
// width is now without the box border.
265262

266263
// Reset the text and find out how wide it is.

shared/services/proposals/voting-tree.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -154,10 +154,9 @@ func (t *VotingTree) generatePollard(virtualRootIndex uint64) (*types.VotingTree
154154
rootNode := t.Nodes[index-1] // 0-indexed
155155

156156
rootLevel := uint64(math.Floor(math.Log2(float64(index)))) // The level of the root node
157-
absoluteDepth := rootLevel + t.DepthPerRound // The actual level in the tree that this pollard must come from
158-
if absoluteDepth > t.Depth {
159-
absoluteDepth = t.Depth // Clamp it to the level of the leaf nodes
160-
}
157+
// The actual level in the tree that this pollard must come from
158+
// Clamp it to the level of the leaf nodes
159+
absoluteDepth := min(rootLevel+t.DepthPerRound, t.Depth)
161160
relativeDepth := absoluteDepth - rootLevel // How far the pollard level is below the root node level
162161
//fmt.Printf("[Pollard Gen] Root level = %d, absolute depth = %d, relative depth = %d\n", rootLevel, absoluteDepth, relativeDepth)
163162

shared/services/rewards/generator-impl-v8.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1041,10 +1041,7 @@ func (r *treeGeneratorImpl_v8) getSmoothingPoolNodeDetails() error {
10411041

10421042
// Get batch start & end index
10431043
iterationStartIndex := batchStartIndex
1044-
iterationEndIndex := batchStartIndex + SmoothingPoolDetailsBatchSize
1045-
if iterationEndIndex > nodeCount {
1046-
iterationEndIndex = nodeCount
1047-
}
1044+
iterationEndIndex := min(batchStartIndex+SmoothingPoolDetailsBatchSize, nodeCount)
10481045

10491046
// Load details
10501047
var wg errgroup.Group

shared/services/rewards/generator-impl-v9-v10.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,10 +1146,7 @@ func (r *treeGeneratorImpl_v9_v10) getSmoothingPoolNodeDetails() error {
11461146

11471147
// Get batch start & end index
11481148
iterationStartIndex := batchStartIndex
1149-
iterationEndIndex := batchStartIndex + SmoothingPoolDetailsBatchSize
1150-
if iterationEndIndex > nodeCount {
1151-
iterationEndIndex = nodeCount
1152-
}
1149+
iterationEndIndex := min(batchStartIndex+SmoothingPoolDetailsBatchSize, nodeCount)
11531150

11541151
// Load details
11551152
var wg errgroup.Group

shared/utils/eth2/eth2.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,7 @@ func GetBeaconBalances(rp *rocketpool.RocketPool, bc beacon.Client, addresses []
4747

4848
// Get batch start & end index
4949
msi := bsi
50-
mei := bsi + MinipoolBalanceDetailsBatchSize
51-
if mei > len(addresses) {
52-
mei = len(addresses)
53-
}
50+
mei := min(bsi+MinipoolBalanceDetailsBatchSize, len(addresses))
5451

5552
// Load details
5653
var wg errgroup.Group
@@ -85,10 +82,7 @@ func GetBeaconBalancesFromState(rp *rocketpool.RocketPool, mpds []*rpstate.Nativ
8582

8683
// Get batch start & end index
8784
msi := bsi
88-
mei := bsi + MinipoolBalanceDetailsBatchSize
89-
if mei > len(mpds) {
90-
mei = len(mpds)
91-
}
85+
mei := min(bsi+MinipoolBalanceDetailsBatchSize, len(mpds))
9286

9387
// Load details
9488
var wg errgroup.Group

shared/utils/rp/minipools.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ func GetMinipoolValidators(rp *rocketpool.RocketPool, bc beacon.Client, addresse
2525

2626
// Get batch start & end index
2727
msi := bsi
28-
mei := bsi + MinipoolPubkeyBatchSize
29-
if mei > len(addresses) {
30-
mei = len(addresses)
31-
}
28+
mei := min(bsi+MinipoolPubkeyBatchSize, len(addresses))
3229

3330
// Load details
3431
var wg errgroup.Group

0 commit comments

Comments
 (0)