Skip to content

Commit ab66259

Browse files
ccejannotti
andauthored
lint: run modernize minmax (#6431)
Co-authored-by: John Jannotti <jannotti@gmail.com>
1 parent 704f501 commit ab66259

File tree

25 files changed

+40
-151
lines changed

25 files changed

+40
-151
lines changed

agreement/fuzzer/tests_test.go

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,7 @@ func TestNetworkBandwidth(t *testing.T) {
492492

493493
deadlock.Opts.Disable = true
494494
rnd := rand.New(rand.NewSource(0))
495-
k := 4 // outgoing connections
496-
if k > relayCounts {
497-
k = relayCounts
498-
}
495+
k := min(4, relayCounts) // outgoing connections
499496
statConf := &TrafficStatisticsFilterConfig{
500497
OutputFormat: 2,
501498
}
@@ -563,10 +560,7 @@ func TestUnstakedNetworkLinearGrowth(t *testing.T) {
563560

564561
relayMaxBandwidth := []int{}
565562

566-
k := 4 // outgoing connections
567-
if k > relayCount {
568-
k = relayCount
569-
}
563+
k := min(4, relayCount) // outgoing connections
570564
statConf := &TrafficStatisticsFilterConfig{
571565
OutputFormat: 0,
572566
}
@@ -674,10 +668,7 @@ func TestStakedNetworkQuadricGrowth(t *testing.T) {
674668
totalRelayedMessages := []int{}
675669
deadlock.Opts.Disable = true
676670

677-
k := 2 // outgoing connections
678-
if k > relayCount {
679-
k = relayCount
680-
}
671+
k := min(2, relayCount) // outgoing connections
681672
statConf := &TrafficStatisticsFilterConfig{
682673
OutputFormat: 0,
683674
}
@@ -784,10 +775,7 @@ func TestRegossipinngElimination(t *testing.T) {
784775
nodeCount := 20
785776
deadlock.Opts.Disable = true
786777
rnd := rand.New(rand.NewSource(0))
787-
k := 4 // outgoing connections
788-
if k > relayCounts {
789-
k = relayCounts
790-
}
778+
k := min(4, relayCounts) // outgoing connections
791779
statConf := &TrafficStatisticsFilterConfig{
792780
OutputFormat: 2,
793781
}
@@ -880,10 +868,7 @@ func BenchmarkNetworkPerformance(b *testing.B) {
880868
// disable deadlock checking code
881869
deadlock.Opts.Disable = true
882870

883-
k := 4 // outgoing connections
884-
if k > relayCount {
885-
k = relayCount
886-
}
871+
k := min(4, relayCount) // outgoing connections
887872
statConf := &TrafficStatisticsFilterConfig{
888873
OutputFormat: 0,
889874
}

agreement/gossip/networkFull_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,7 @@ func testNetworkImplRebroadcast(t *testing.T, nodesCount int, cfg config.Local)
322322
nets, counters := spinNetwork(t, nodesCount, cfg)
323323
defer shutdownNetwork(nets, counters)
324324

325-
rebroadcastNodes := nodesCount
326-
if rebroadcastNodes > 3 {
327-
rebroadcastNodes = 3
328-
}
325+
rebroadcastNodes := min(nodesCount, 3)
329326
for i := byte(0); i < byte(rebroadcastNodes); i++ {
330327
ok := nets[i].Broadcast(protocol.AgreementVoteTag, []byte{i, i + 1})
331328
assert.NoError(t, ok)

agreement/player.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -331,13 +331,7 @@ func (p *player) calculateFilterTimeout(ver protocol.ConsensusVersion, tracer *t
331331
dynamicTimeout := p.lowestCredentialArrivals.orderStatistics(dynamicFilterTimeoutCredentialArrivalHistoryIdx) + dynamicFilterTimeoutGraceInterval
332332

333333
// Make sure the dynamic filter timeout is not too small nor too large
334-
clampedTimeout := dynamicTimeout
335-
if clampedTimeout < dynamicFilterTimeoutLowerBound {
336-
clampedTimeout = dynamicFilterTimeoutLowerBound
337-
}
338-
if clampedTimeout > defaultTimeout {
339-
clampedTimeout = defaultTimeout
340-
}
334+
clampedTimeout := min(max(dynamicTimeout, dynamicFilterTimeoutLowerBound), defaultTimeout)
341335
tracer.log.Debugf("round %d, period %d: dynamicTimeout = %d, clamped timeout = %d", p.Round, p.Period, dynamicTimeout, clampedTimeout)
342336
// store dynamicFilterTimeout on the player for debugging & reporting
343337
p.dynamicFilterTimeout = dynamicTimeout

agreement/router.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,7 @@ func init() {
6464
// for consistency in analytics we are setting the minimum to be 8 rounds
6565
// (equivalent to a dynamicFilterTimeoutLowerBound of 500 ms).
6666
minCredentialRoundLag := round(8) // round 2*2000ms / 500ms
67-
credentialRoundLag = round(2 * config.Protocol.SmallLambda / dynamicFilterTimeoutLowerBound)
68-
69-
if credentialRoundLag < minCredentialRoundLag {
70-
credentialRoundLag = minCredentialRoundLag
71-
}
67+
credentialRoundLag = max(round(2*config.Protocol.SmallLambda/dynamicFilterTimeoutLowerBound), minCredentialRoundLag)
7268
if credentialRoundLag*round(dynamicFilterTimeoutLowerBound) < round(2*config.Protocol.SmallLambda) {
7369
credentialRoundLag++
7470
}

catchup/catchpointService.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -525,10 +525,7 @@ func (cs *CatchpointCatchupService) processStageBlocksDownload() (err error) {
525525
// 2. replay starts from X-CatchpointLookback+1
526526
// 3. transaction evaluation at Y requires block up to MaxTxnLife+DeeperBlockHeaderHistory back from Y
527527
proto := config.Consensus[topBlock.CurrentProtocol]
528-
lookback := proto.MaxTxnLife + proto.DeeperBlockHeaderHistory + proto.CatchpointLookback
529-
if lookback < proto.MaxBalLookback {
530-
lookback = proto.MaxBalLookback
531-
}
528+
lookback := max(proto.MaxTxnLife+proto.DeeperBlockHeaderHistory+proto.CatchpointLookback, proto.MaxBalLookback)
532529

533530
lookbackForStateProofSupport := lookbackForStateproofsSupport(&topBlock)
534531
if lookback < lookbackForStateProofSupport {

catchup/service.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -476,17 +476,11 @@ func (s *Service) fetchAndWrite(ctx context.Context, r basics.Round, prevFetchCo
476476

477477
// TODO the following code does not handle the following case: seedLookback upgrades during fetch
478478
func (s *Service) pipelinedFetch(seedLookback uint64) {
479-
maxParallelRequests := s.parallelBlocks
480-
if maxParallelRequests < seedLookback {
481-
maxParallelRequests = seedLookback
482-
}
479+
maxParallelRequests := max(s.parallelBlocks, seedLookback)
483480
minParallelRequests := seedLookback
484481

485482
// Start the limited requests at max(1, 'seedLookback')
486-
limitedParallelRequests := uint64(1)
487-
if limitedParallelRequests < seedLookback {
488-
limitedParallelRequests = seedLookback
489-
}
483+
limitedParallelRequests := max(1, seedLookback)
490484

491485
completed := make(map[basics.Round]chan bool)
492486
var wg sync.WaitGroup

cmd/catchpointdump/file.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -447,10 +447,7 @@ func loadCatchpointIntoDatabase(ctx context.Context, catchupAccessor ledger.Catc
447447
}
448448
if time.Since(lastProgressUpdate) > 50*time.Millisecond && catchpointFileSize > 0 {
449449
lastProgressUpdate = time.Now()
450-
progressRatio := int(float64(progress) * barLength / float64(catchpointFileSize))
451-
if progressRatio > barLength {
452-
progressRatio = barLength
453-
}
450+
progressRatio := min(int(float64(progress)*barLength/float64(catchpointFileSize)), barLength)
454451
printLoadCatchpointProgressLine(progressRatio, barLength, int64(progress))
455452
}
456453
}

crypto/onetimesig.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,7 @@ func (s *OneTimeSignatureSecrets) DeleteBeforeFineGrained(current OneTimeSignatu
423423
// subkeys.
424424
if current.Batch+1 == s.FirstBatch {
425425
if current.Offset > s.FirstOffset {
426-
jump := current.Offset - s.FirstOffset
427-
if jump > uint64(len(s.Offsets)) {
428-
jump = uint64(len(s.Offsets))
429-
}
426+
jump := min(current.Offset-s.FirstOffset, uint64(len(s.Offsets)))
430427

431428
s.FirstOffset += jump
432429
s.Offsets = s.Offsets[jump:]

daemon/algod/api/server/v2/test/handlers_resources_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,10 +437,7 @@ func accountAssetInformationResourceLimitsTest(t *testing.T, handlers v2.Handler
437437
assert.Equal(t, maxResults, len(*ret.AssetHoldings))
438438

439439
// Asset holdings should match the first limit assets from the account data
440-
minForResults := 0
441-
if inputNextToken > 0 {
442-
minForResults = inputNextToken
443-
}
440+
minForResults := max(inputNextToken, 0)
444441
for i := minForResults; i < minForResults+maxResults; i++ {
445442
expectedIndex := basics.AssetIndex(i + 1)
446443

data/transactions/logic/eval.go

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -522,13 +522,10 @@ func feeCredit(txgroup []transactions.SignedTxnWithAD, minFee uint64) uint64 {
522522

523523
// NewInnerEvalParams creates an EvalParams to be used while evaluating an inner group txgroup
524524
func NewInnerEvalParams(txg []transactions.SignedTxnWithAD, caller *EvalContext) *EvalParams {
525-
minAvmVersion := computeMinAvmVersion(txg)
526-
// Can't happen currently, since earliest inner callable version is higher
527-
// than any minimum imposed otherwise. But is correct to inherit a stronger
528-
// restriction from above, in case of future restriction.
529-
if minAvmVersion < caller.minAvmVersion {
530-
minAvmVersion = caller.minAvmVersion
531-
}
525+
minAvmVersion := max(computeMinAvmVersion(txg), caller.minAvmVersion)
526+
// caller.AvmVersion can't exceed the computed value currently, since earliest
527+
// inner callable version is higher than any minimum imposed otherwise. But is
528+
// correct to inherit a stronger restriction from above, in case of future restriction.
532529

533530
// Unlike NewEvalParams, do not add fee credit here. opTxSubmit has already done so.
534531

@@ -1638,10 +1635,7 @@ func (cx *EvalContext) step() error {
16381635
if len(cx.Stack) == 0 {
16391636
stackString = "<empty stack>"
16401637
} else {
1641-
num := 1
1642-
if len(spec.Return.Types) > 1 {
1643-
num = len(spec.Return.Types)
1644-
}
1638+
num := max(len(spec.Return.Types), 1)
16451639
// check for nil error here, because we might not return
16461640
// values if we encounter an error in the opcode
16471641
if err == nil {
@@ -3238,10 +3232,7 @@ func (cx *EvalContext) txnFieldToStack(stxn *transactions.SignedTxnWithAD, fs *t
32383232
return sv, fmt.Errorf("invalid ApprovalProgramPages index %d", arrayFieldIdx)
32393233
}
32403234
first := arrayFieldIdx * maxStringSize
3241-
last := first + maxStringSize
3242-
if last > uint64(len(txn.ApprovalProgram)) {
3243-
last = uint64(len(txn.ApprovalProgram))
3244-
}
3235+
last := min(first+maxStringSize, uint64(len(txn.ApprovalProgram)))
32453236
sv.Bytes = txn.ApprovalProgram[first:last]
32463237
case NumClearStateProgramPages:
32473238
sv.Uint = uint64(basics.DivCeil(len(txn.ClearStateProgram), maxStringSize))
@@ -3251,10 +3242,7 @@ func (cx *EvalContext) txnFieldToStack(stxn *transactions.SignedTxnWithAD, fs *t
32513242
return sv, fmt.Errorf("invalid ClearStateProgramPages index %d", arrayFieldIdx)
32523243
}
32533244
first := arrayFieldIdx * maxStringSize
3254-
last := first + maxStringSize
3255-
if last > uint64(len(txn.ClearStateProgram)) {
3256-
last = uint64(len(txn.ClearStateProgram))
3257-
}
3245+
last := min(first+maxStringSize, uint64(len(txn.ClearStateProgram)))
32583246
sv.Bytes = txn.ClearStateProgram[first:last]
32593247
case RekeyTo:
32603248
sv.Bytes = txn.RekeyTo[:]

0 commit comments

Comments
 (0)