Skip to content

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

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 2 commits into
base: master
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
7 changes: 2 additions & 5 deletions action/protocol/poll/lifelong_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,8 @@ func (p *lifeLongDelegatesProtocol) readActiveBlockProducers(ctx context.Context
targetEpochStartHeight = rp.GetEpochHeight(targetEpochNum) // next epoch start height
}
crypto.SortCandidates(blockProducerList, targetEpochStartHeight, crypto.CryptoSeed)
length := int(rp.NumDelegates())
if len(blockProducerList) < length {
// TODO: if the number of delegates is smaller than expected, should it return error or not?
length = len(blockProducerList)
}
// TODO: if the number of delegates is smaller than expected, should it return error or not?
length := min(len(blockProducerList), int(rp.NumDelegates()))

var activeBlockProducers state.CandidateList
for i := 0; i < length; i++ {
Expand Down
10 changes: 2 additions & 8 deletions action/protocol/staking/candidate_buckets_indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,7 @@ func (cbi *CandidatesBucketsIndexer) GetCandidates(height uint64, offset, limit
if offset >= length {
return &iotextypes.CandidateListV2{}, height, nil
}
end := offset + limit
if end > uint32(len(candidateList.Candidates)) {
end = uint32(len(candidateList.Candidates))
}
end := min(offset+limit, uint32(len(candidateList.Candidates)))
candidateList.Candidates = candidateList.Candidates[offset:end]
// fill id if it's empty for backward compatibility
for i := range candidateList.Candidates {
Expand Down Expand Up @@ -190,10 +187,7 @@ func (cbi *CandidatesBucketsIndexer) GetBuckets(height uint64, offset, limit uin
if offset >= length {
return &iotextypes.VoteBucketList{}, height, nil
}
end := offset + limit
if end > uint32(len(buckets.Buckets)) {
end = uint32(len(buckets.Buckets))
}
end := min(offset+limit, uint32(len(buckets.Buckets)))
buckets.Buckets = buckets.Buckets[offset:end]
return buckets, height, nil
}
Expand Down
10 changes: 2 additions & 8 deletions action/protocol/staking/candidate_buckets_indexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ func TestCandidatesBucketsIndexer_PutGetCandidates(t *testing.T) {
require.Zero(len(r.Candidates))
continue
}
end := v2.offset + v2.limit
if end > expectLen {
end = expectLen
}
end := min(v2.offset+v2.limit, expectLen)
// check the returned list
expect := tests[v.height].candidates.Candidates[v2.offset:end]
for i, v := range expect {
Expand Down Expand Up @@ -226,10 +223,7 @@ func TestCandidatesBucketsIndexer_PutGetBuckets(t *testing.T) {
require.Zero(len(r.Buckets))
continue
}
end := v2.offset + v2.limit
if end > expectLen {
end = expectLen
}
end := min(v2.offset+v2.limit, expectLen)
// check the returned list
expect := tests[v.height].buckets.Buckets[v2.offset:end]
for i, v := range expect {
Expand Down