From ddef50ccfc0d5027131914a190995168004207df Mon Sep 17 00:00:00 2001 From: zhetaicheleba Date: Mon, 27 Oct 2025 15:20:20 +0800 Subject: [PATCH] refactor: use the built-in max to simplify the code Signed-off-by: zhetaicheleba --- cl/beacon/handler/committees.go | 5 +---- cl/beacon/handler/duties_attester.go | 5 +---- .../state/historical_states_reader/attesting_indicies.go | 5 +---- cl/phase1/core/state/cache_accessors.go | 7 ++----- cl/phase1/forkchoice/checkpoint_state.go | 5 +---- cl/utils/eth2shuffle/shuffle.go | 5 +---- 6 files changed, 7 insertions(+), 25 deletions(-) diff --git a/cl/beacon/handler/committees.go b/cl/beacon/handler/committees.go index 47b9b33ce7b..621b2149687 100644 --- a/cl/beacon/handler/committees.go +++ b/cl/beacon/handler/committees.go @@ -134,10 +134,7 @@ func (a *ApiHandler) getCommittees(w http.ResponseWriter, r *http.Request) (*bea return nil, err } - committeesPerSlot := min(a.beaconChainCfg.MaxCommitteesPerSlot, uint64(len(activeIdxs))/a.beaconChainCfg.SlotsPerEpoch/a.beaconChainCfg.TargetCommitteeSize) - if committeesPerSlot < 1 { - committeesPerSlot = 1 - } + committeesPerSlot := max(min(a.beaconChainCfg.MaxCommitteesPerSlot, uint64(len(activeIdxs))/a.beaconChainCfg.SlotsPerEpoch/a.beaconChainCfg.TargetCommitteeSize), 1) mixPosition := (epoch + a.beaconChainCfg.EpochsPerHistoricalVector - a.beaconChainCfg.MinSeedLookahead - 1) % a.beaconChainCfg.EpochsPerHistoricalVector mix, err := a.stateReader.ReadRandaoMixBySlotAndIndex(tx, stateGetter, epoch*a.beaconChainCfg.SlotsPerEpoch, mixPosition) diff --git a/cl/beacon/handler/duties_attester.go b/cl/beacon/handler/duties_attester.go index 205234444db..2ecbf2ec69b 100644 --- a/cl/beacon/handler/duties_attester.go +++ b/cl/beacon/handler/duties_attester.go @@ -177,10 +177,7 @@ func (a *ApiHandler) getAttesterDuties(w http.ResponseWriter, r *http.Request) ( return nil, err } - committeesPerSlot := min(a.beaconChainCfg.MaxCommitteesPerSlot, uint64(len(activeIdxs))/a.beaconChainCfg.SlotsPerEpoch/a.beaconChainCfg.TargetCommitteeSize) - if committeesPerSlot < 1 { - committeesPerSlot = 1 - } + committeesPerSlot := max(min(a.beaconChainCfg.MaxCommitteesPerSlot, uint64(len(activeIdxs))/a.beaconChainCfg.SlotsPerEpoch/a.beaconChainCfg.TargetCommitteeSize), 1) mixPosition := (epoch + a.beaconChainCfg.EpochsPerHistoricalVector - a.beaconChainCfg.MinSeedLookahead - 1) % a.beaconChainCfg.EpochsPerHistoricalVector mix, err := a.stateReader.ReadRandaoMixBySlotAndIndex(tx, stateGetter, epoch*a.beaconChainCfg.SlotsPerEpoch, mixPosition) diff --git a/cl/persistence/state/historical_states_reader/attesting_indicies.go b/cl/persistence/state/historical_states_reader/attesting_indicies.go index 16d4b649441..59588cbea6e 100644 --- a/cl/persistence/state/historical_states_reader/attesting_indicies.go +++ b/cl/persistence/state/historical_states_reader/attesting_indicies.go @@ -116,10 +116,7 @@ func (r *HistoricalStatesReader) ComputeCommittee(mix common.Hash, indicies []ui } func committeeCount(cfg *clparams.BeaconChainConfig, epoch uint64, idxs []uint64) uint64 { - committeCount := min(cfg.MaxCommitteesPerSlot, uint64(len(idxs))/cfg.SlotsPerEpoch/cfg.TargetCommitteeSize) - if committeCount < 1 { - committeCount = 1 - } + committeCount := max(min(cfg.MaxCommitteesPerSlot, uint64(len(idxs))/cfg.SlotsPerEpoch/cfg.TargetCommitteeSize), 1) return committeCount } diff --git a/cl/phase1/core/state/cache_accessors.go b/cl/phase1/core/state/cache_accessors.go index c514dd8837b..4036fcb1226 100644 --- a/cl/phase1/core/state/cache_accessors.go +++ b/cl/phase1/core/state/cache_accessors.go @@ -222,12 +222,9 @@ func (b *CachingBeaconState) SyncRewards() (proposerReward, participantReward ui // CommitteeCount returns current number of committee for epoch. func (b *CachingBeaconState) CommitteeCount(epoch uint64) uint64 { - committeCount := min(b.BeaconConfig().MaxCommitteesPerSlot, uint64( + committeCount := max(min(b.BeaconConfig().MaxCommitteesPerSlot, uint64( len(b.GetActiveValidatorsIndices(epoch)), - )/b.BeaconConfig().SlotsPerEpoch/b.BeaconConfig().TargetCommitteeSize) - if committeCount < 1 { - committeCount = 1 - } + )/b.BeaconConfig().SlotsPerEpoch/b.BeaconConfig().TargetCommitteeSize), 1) return committeCount } diff --git a/cl/phase1/forkchoice/checkpoint_state.go b/cl/phase1/forkchoice/checkpoint_state.go index ca24d416517..59fae27bb85 100644 --- a/cl/phase1/forkchoice/checkpoint_state.go +++ b/cl/phase1/forkchoice/checkpoint_state.go @@ -165,10 +165,7 @@ func (c *checkpointState) getActiveIndicies(epoch uint64) (activeIndicies []uint // committeeCount retrieves size of sync committee func (c *checkpointState) committeeCount(epoch, lenIndicies uint64) uint64 { - committeCount := min(c.beaconConfig.MaxCommitteesPerSlot, lenIndicies/c.beaconConfig.SlotsPerEpoch/c.beaconConfig.TargetCommitteeSize) - if committeCount < 1 { - committeCount = 1 - } + committeCount := max(min(c.beaconConfig.MaxCommitteesPerSlot, lenIndicies/c.beaconConfig.SlotsPerEpoch/c.beaconConfig.TargetCommitteeSize), 1) return committeCount } diff --git a/cl/utils/eth2shuffle/shuffle.go b/cl/utils/eth2shuffle/shuffle.go index cf7a0088e17..3b5b4f5894e 100644 --- a/cl/utils/eth2shuffle/shuffle.go +++ b/cl/utils/eth2shuffle/shuffle.go @@ -82,10 +82,7 @@ func innerPermuteIndex(hashFn HashFn, rounds uint8, index uint64, listSize uint6 // Why? Don't do double work: we consider every pair only once. // (Otherwise we would swap it back in place) // Pick the highest index of the pair as position to retrieve randomness with. - position := index - if flip > position { - position = flip - } + position := max(flip, index) // spec: source = hash(seed + int_to_bytes1(round) + int_to_bytes4(position // 256)) // - seed is still in 0:32 (excl., 32 bytes) // - round number is still in 32