Skip to content

refactor: use the built-in max/min to simplify the code #5206

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions common/deliverclient/blocksprovider/bft_deliverer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1132,10 +1132,7 @@ func TestBFTDeliverer_CensorshipMonitorEvents(t *testing.T) {
minDur := 100 * time.Millisecond
for i := 0; i < 40; i++ {
round := (i + 1) / 4
dur := time.Duration(minDur.Nanoseconds() * int64(math.Pow(2.0, float64(round))))
if dur > 10*time.Second {
dur = 10 * time.Second
}
dur := min(time.Duration(minDur.Nanoseconds()*int64(math.Pow(2.0, float64(round)))), 10*time.Second)
assert.Equal(t, dur, setup.fakeSleeper.SleepArgsForCall(i), fmt.Sprintf("i=%d", i))
}

Expand Down
5 changes: 1 addition & 4 deletions core/chaincode/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ func (c *Config) load() {
c.ExecuteTimeout = defaultExecutionTimeout
}
c.InstallTimeout = viper.GetDuration("chaincode.installTimeout")
c.StartupTimeout = viper.GetDuration("chaincode.startuptimeout")
if c.StartupTimeout < minimumStartupTimeout {
c.StartupTimeout = minimumStartupTimeout
}
c.StartupTimeout = max(viper.GetDuration("chaincode.startuptimeout"), minimumStartupTimeout)

c.SCCAllowlist = map[string]bool{}
for k, v := range viper.GetStringMapString("chaincode.system") {
Expand Down
5 changes: 1 addition & 4 deletions core/chaincode/lifecycle/scc.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,7 @@ type Invocation struct {
// to the underlying lifecycle implementation.
func (i *Invocation) InstallChaincode(input *lb.InstallChaincodeArgs) (proto.Message, error) {
if logger.IsEnabledFor(zapcore.DebugLevel) {
end := 35
if len(input.ChaincodeInstallPackage) < end {
end = len(input.ChaincodeInstallPackage)
}
end := min(len(input.ChaincodeInstallPackage), 35)

// the first tens of bytes contain the (compressed) portion
// of the package metadata and so they'll be different across
Expand Down
5 changes: 1 addition & 4 deletions gossip/discovery/discovery_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,10 +235,7 @@ func (d *gossipDiscoveryImpl) InitiateSync(peerNum int) {
d.lock.RLock()

n := d.aliveMembership.Size()
k := peerNum
if k > n {
k = n
}
k := min(peerNum, n)

aliveMembersAsSlice := d.aliveMembership.ToSlice()
for _, i := range util.GetRandomIndices(k, n-1) {
Expand Down
5 changes: 1 addition & 4 deletions gossip/privdata/distributor.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,7 @@ func (d *distributorImpl) disseminationPlanForMsg(colAP privdata.CollectionAcces
}

// PHASE 2 - Select additional peers to satisfy colAP.MaximumPeerCount() if there are still peers in the remainingPeersAcrossOrgs pool
numRemainingPeersToSelect := maximumPeerRemainingCount
if len(remainingPeersAcrossOrgs) < maximumPeerRemainingCount {
numRemainingPeersToSelect = len(remainingPeersAcrossOrgs)
}
numRemainingPeersToSelect := min(len(remainingPeersAcrossOrgs), maximumPeerRemainingCount)
if numRemainingPeersToSelect > 0 {
d.logger.Debugf("MaximumPeerCount not yet satisfied after picking one peer per org, selecting %d more peer(s) for dissemination", numRemainingPeersToSelect)
}
Expand Down
5 changes: 1 addition & 4 deletions integration/raft/cft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1129,10 +1129,7 @@ func measureTPS(txNum int, network *nwo.Network, orderer *nwo.Orderer, envs chan
cond.Broadcast()
bcastWG.Wait()

elapsed := time.Since(start)
if elapsed < time.Second {
elapsed = time.Second
}
elapsed := max(time.Since(start), time.Second)

return txNum / int(elapsed.Seconds())
}
Expand Down
10 changes: 2 additions & 8 deletions orderer/common/follower/follower_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,7 @@ func (c *Chain) increaseRetryInterval(retryInterval *time.Duration, upperLimit t
return
}
// assuming this will never overflow int64, as upperLimit cannot be over MaxInt64/2
*retryInterval = time.Duration(1.5 * float64(*retryInterval))
if *retryInterval > upperLimit {
*retryInterval = upperLimit
}
*retryInterval = min(time.Duration(1.5*float64(*retryInterval)), upperLimit)
c.logger.Debugf("retry interval increased to: %v", *retryInterval)
}

Expand All @@ -329,10 +326,7 @@ func (c *Chain) decreaseRetryInterval(retryInterval *time.Duration, lowerLimit t
return
}

*retryInterval = *retryInterval - lowerLimit
if *retryInterval < lowerLimit {
*retryInterval = lowerLimit
}
*retryInterval = max(*retryInterval-lowerLimit, lowerLimit)
c.logger.Debugf("retry interval decreased to: %v", *retryInterval)
}

Expand Down
5 changes: 1 addition & 4 deletions orderer/consensus/smartbft/synchronizer_bft.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ func (s *BFTSynchronizer) synchronize() (*types.Decision, error) {
}

// === Create a buffer to accept the blocks delivered from the BFTDeliverer.
capacityBlocks := uint(s.LocalConfigCluster.ReplicationBufferSize) / uint(s.Support.SharedConfig().BatchSize().AbsoluteMaxBytes)
if capacityBlocks < 100 {
capacityBlocks = 100
}
capacityBlocks := max(uint(s.LocalConfigCluster.ReplicationBufferSize)/uint(s.Support.SharedConfig().BatchSize().AbsoluteMaxBytes), 100)
s.mutex.Lock()
s.syncBuff = NewSyncBuffer(capacityBlocks)
s.mutex.Unlock()
Expand Down