Skip to content
Merged
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: 3 additions & 4 deletions cl/antiquary/antiquary.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"sync/atomic"
"time"

"github.com/ledgerwatch/log/v3"
"golang.org/x/sync/semaphore"

"github.com/ledgerwatch/erigon-lib/common/datadir"
Expand All @@ -17,9 +18,7 @@ import (
"github.com/ledgerwatch/erigon/cl/persistence/blob_storage"
state_accessors "github.com/ledgerwatch/erigon/cl/persistence/state"
"github.com/ledgerwatch/erigon/cl/phase1/core/state"
"github.com/ledgerwatch/erigon/cl/utils"
"github.com/ledgerwatch/erigon/turbo/snapshotsync/freezeblocks"
"github.com/ledgerwatch/log/v3"
)

const safetyMargin = 2_000 // We retire snapshots 2k blocks after the finalized head
Expand Down Expand Up @@ -214,7 +213,7 @@ func (a *Antiquary) Loop() error {
if from >= to {
continue
}
to = utils.Min64(to, to-safetyMargin) // We don't want to retire snapshots that are too close to the finalized head
to = min(to, to-safetyMargin) // We don't want to retire snapshots that are too close to the finalized head
to = (to / snaptype.Erigon2MergeLimit) * snaptype.Erigon2MergeLimit
if to-from < snaptype.Erigon2MergeLimit {
continue
Expand Down Expand Up @@ -320,7 +319,7 @@ func (a *Antiquary) antiquateBlobs() error {
// perform blob antiquation if it is time to.
currentBlobsProgress := a.sn.FrozenBlobs()
minimunBlobsProgress := ((a.cfg.DenebForkEpoch * a.cfg.SlotsPerEpoch) / snaptype.Erigon2MergeLimit) * snaptype.Erigon2MergeLimit
currentBlobsProgress = utils.Max64(currentBlobsProgress, minimunBlobsProgress)
currentBlobsProgress = max(currentBlobsProgress, minimunBlobsProgress)
// read the finalized head
to, err := beacon_indicies.ReadHighestFinalized(roTx)
if err != nil {
Expand Down
12 changes: 4 additions & 8 deletions cl/cltypes/solid/uint64slice_byte.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,8 @@ func NewUint64Slice(limit int) *byteBasedUint64Slice {
// Clear clears the slice by setting its length to 0 and zeroing out its backing array.
func (arr *byteBasedUint64Slice) Clear() {
arr.l = 0
for i := range arr.u {
arr.u[i] = 0
}
for i := range arr.treeCacheBuffer {
arr.treeCacheBuffer[i] = 0
}
clear(arr.u)
clear(arr.treeCacheBuffer)
}

// CopyTo copies the slice to a target slice.
Expand Down Expand Up @@ -199,14 +195,14 @@ func (arr *byteBasedUint64Slice) HashVectorSSZ() ([32]byte, error) {
for i := 0; i < maxTo; i += chunkSize {
offset = (i / chunkSize) * length.Hash
from := i
to := int(utils.Min64(uint64(from+chunkSize), uint64(maxTo)))
to := min(from+chunkSize, maxTo)

if !bytes.Equal(arr.treeCacheBuffer[offset:offset+length.Hash], emptyHashBytes) {
continue
}
layerBuffer = layerBuffer[:to-from]
copy(layerBuffer, arr.u[from:to])
if err := computeFlatRootsToBuffer(uint8(utils.Min64(treeCacheDepthUint64Slice, uint64(depth))), layerBuffer, arr.treeCacheBuffer[offset:]); err != nil {
if err := computeFlatRootsToBuffer(uint8(min(treeCacheDepthUint64Slice, uint64(depth))), layerBuffer, arr.treeCacheBuffer[offset:]); err != nil {
return [32]byte{}, err
}
}
Expand Down
2 changes: 1 addition & 1 deletion cl/cltypes/solid/validator_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ func (v *ValidatorSet) HashSSZ() ([32]byte, error) {
layerBuffer := make([]byte, validatorsLeafChunkSize*length.Hash)
for i := 0; i < v.l; i += validatorsLeafChunkSize {
from := uint64(i)
to := utils.Min64(from+uint64(validatorsLeafChunkSize), uint64(v.l))
to := min(from+uint64(validatorsLeafChunkSize), uint64(v.l))
offset := (i / validatorsLeafChunkSize) * length.Hash

if !bytes.Equal(v.treeCacheBuffer[offset:offset+length.Hash], emptyHashBytes) {
Expand Down
4 changes: 2 additions & 2 deletions cl/phase1/core/state/accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func Epoch(b abstract.BeaconStateBasic) uint64 {
}

func IsAggregator(cfg *clparams.BeaconChainConfig, committeeLength, committeeIndex uint64, slotSignature libcommon.Bytes96) bool {
modulo := utils.Max64(1, committeeLength/cfg.TargetAggregatorsPerCommittee)
modulo := max(1, committeeLength/cfg.TargetAggregatorsPerCommittee)
hashSlotSignatue := utils.Sha256(slotSignature[:])
return binary.LittleEndian.Uint64(hashSlotSignatue[:8])%modulo == 0
}
Expand Down Expand Up @@ -208,7 +208,7 @@ func ExpectedWithdrawals(b abstract.BeaconState, currentEpoch uint64) []*cltypes
// Determine the upper bound for the loop and initialize the withdrawals slice with a capacity of bound
maxValidators := uint64(b.ValidatorLength())
maxValidatorsPerWithdrawalsSweep := b.BeaconConfig().MaxValidatorsPerWithdrawalsSweep
bound := utils.Min64(maxValidators, maxValidatorsPerWithdrawalsSweep)
bound := min(maxValidators, maxValidatorsPerWithdrawalsSweep)
withdrawals := make([]*cltypes.Withdrawal, 0, bound)

// Loop through the validators to calculate expected withdrawals
Expand Down
2 changes: 1 addition & 1 deletion cl/phase1/core/state/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ func (b *CachingBeaconState) _refreshActiveBalancesIfNeeded() {
}
return true
})
*b.totalActiveBalanceCache = utils.Max64(b.BeaconConfig().EffectiveBalanceIncrement, *b.totalActiveBalanceCache)
*b.totalActiveBalanceCache = max(b.BeaconConfig().EffectiveBalanceIncrement, *b.totalActiveBalanceCache)
b.totalActiveBalanceRootCache = utils.IntegerSquareRoot(*b.totalActiveBalanceCache)
}

Expand Down
4 changes: 2 additions & 2 deletions cl/phase1/core/state/cache_accessors.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func (b *CachingBeaconState) GetAttestingIndicies(
// See: https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#get_validator_churn_limit
func (b *CachingBeaconState) GetValidatorChurnLimit() uint64 {
activeIndsCount := uint64(len(b.GetActiveValidatorsIndices(Epoch(b))))
return utils.Max64(
return max(
activeIndsCount/b.BeaconConfig().ChurnLimitQuotient,
b.BeaconConfig().MinPerEpochChurnLimit,
)
Expand All @@ -349,7 +349,7 @@ func (b *CachingBeaconState) GetValidatorChurnLimit() uint64 {
// https://github.com/ethereum/consensus-specs/blob/dev/specs/deneb/beacon-chain.md#new-get_validator_activation_churn_limit
func (b *CachingBeaconState) GetValidatorActivationChurnLimit() uint64 {
if b.Version() >= clparams.DenebVersion {
return utils.Min64(
return min(
b.BeaconConfig().MaxPerEpochActivationChurnLimit,
b.GetValidatorChurnLimit(),
)
Expand Down
3 changes: 1 addition & 2 deletions cl/phase1/core/state/cache_mutators.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/ledgerwatch/erigon-lib/common/math"
"github.com/ledgerwatch/erigon/cl/clparams"
"github.com/ledgerwatch/erigon/cl/cltypes/solid"
"github.com/ledgerwatch/erigon/cl/utils"
)

func (b *CachingBeaconState) getSlashingProposerReward(whistleBlowerReward uint64) uint64 {
Expand Down Expand Up @@ -34,7 +33,7 @@ func (b *CachingBeaconState) SlashValidator(slashedInd uint64, whistleblowerInd
return 0, err
}

newWithdrawableEpoch := utils.Max64(currentWithdrawableEpoch, epoch+b.BeaconConfig().EpochsPerSlashingsVector)
newWithdrawableEpoch := max(currentWithdrawableEpoch, epoch+b.BeaconConfig().EpochsPerSlashingsVector)
if err := b.SetWithdrawableEpochForValidatorAtIndex(int(slashedInd), newWithdrawableEpoch); err != nil {
return 0, err
}
Expand Down
8 changes: 3 additions & 5 deletions cl/phase1/core/state/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package state
import (
"sort"

"github.com/ledgerwatch/erigon/cl/cltypes/solid"
"github.com/ledgerwatch/erigon/cl/phase1/core/state/lru"

"github.com/ledgerwatch/erigon/cl/clparams"
"github.com/ledgerwatch/erigon/cl/cltypes"
"github.com/ledgerwatch/erigon/cl/utils"
"github.com/ledgerwatch/erigon/cl/cltypes/solid"
"github.com/ledgerwatch/erigon/cl/phase1/core/state/lru"
)

func copyLRU[K comparable, V any](dst *lru.Cache[K, V], src *lru.Cache[K, V]) *lru.Cache[K, V] {
Expand Down Expand Up @@ -37,7 +35,7 @@ func GetIndexedAttestation(attestation *solid.Attestation, attestingIndicies []u

func ValidatorFromDeposit(conf *clparams.BeaconChainConfig, deposit *cltypes.Deposit) solid.Validator {
amount := deposit.Data.Amount
effectiveBalance := utils.Min64(amount-amount%conf.EffectiveBalanceIncrement, conf.MaxEffectiveBalance)
effectiveBalance := min(amount-amount%conf.EffectiveBalanceIncrement, conf.MaxEffectiveBalance)

validator := solid.NewValidator()
validator.SetPublicKey(deposit.Data.PubKey)
Expand Down
2 changes: 1 addition & 1 deletion cl/phase1/network/services/sync_contribution_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (s *syncContributionService) ProcessMessage(ctx context.Context, subnet *ui
return fmt.Errorf("contribution has no participants")
}

modulo := utils.Max64(1, s.beaconCfg.SyncCommitteeSize/s.beaconCfg.SyncCommitteeSubnetCount/s.beaconCfg.TargetAggregatorsPerSyncSubcommittee)
modulo := max(1, s.beaconCfg.SyncCommitteeSize/s.beaconCfg.SyncCommitteeSubnetCount/s.beaconCfg.TargetAggregatorsPerSyncSubcommittee)
hashSignature := utils.Sha256(selectionProof[:])
if !s.test && binary.LittleEndian.Uint64(hashSignature[:8])%modulo != 0 {
return fmt.Errorf("selects the validator as an aggregator")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package statechange
import (
"github.com/ledgerwatch/erigon/cl/abstract"
"github.com/ledgerwatch/erigon/cl/cltypes/solid"
"github.com/ledgerwatch/erigon/cl/utils"
)

// ProcessEffectiveBalanceUpdates updates the effective balance of validators. Specs at: https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#effective-balances-updates
Expand All @@ -24,7 +23,7 @@ func ProcessEffectiveBalanceUpdates(state abstract.BeaconState) error {
eb := validator.EffectiveBalance()
if balance+downwardThreshold < eb || eb+upwardThreshold < balance {
// Set new effective balance
effectiveBalance := utils.Min64(balance-(balance%beaconConfig.EffectiveBalanceIncrement), beaconConfig.MaxEffectiveBalance)
effectiveBalance := min(balance-(balance%beaconConfig.EffectiveBalanceIncrement), beaconConfig.MaxEffectiveBalance)
state.SetEffectiveBalanceForValidatorAtIndex(index, effectiveBalance)
}
return true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package statechange
import (
"github.com/ledgerwatch/erigon/cl/abstract"
"github.com/ledgerwatch/erigon/cl/phase1/core/state"
"github.com/ledgerwatch/erigon/cl/utils"
)

// ProcessInactivityScores will updates the inactivity registry of each validator.
Expand All @@ -19,12 +18,12 @@ func ProcessInactivityScores(s abstract.BeaconState, eligibleValidatorsIndicies
return err
}
if unslashedIndicies[s.BeaconConfig().TimelyTargetFlagIndex][validatorIndex] {
score -= utils.Min64(1, score)
score -= min(1, score)
} else {
score += s.BeaconConfig().InactivityScoreBias
}
if !state.InactivityLeaking(s) {
score -= utils.Min64(s.BeaconConfig().InactivityScoreRecoveryRate, score)
score -= min(s.BeaconConfig().InactivityScoreRecoveryRate, score)
}
if err := s.SetValidatorInactivityScore(int(validatorIndex), score); err != nil {
return err
Expand Down
14 changes: 0 additions & 14 deletions cl/utils/math.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,3 @@ func IntegerSquareRoot(n uint64) uint64 {

return uint64(math.Sqrt(float64(n)))
}

func Max64(a, b uint64) uint64 {
if a > b {
return a
}
return b
}

func Min64(a, b uint64) uint64 {
if a < b {
return a
}
return b
}
51 changes: 0 additions & 51 deletions cl/utils/math_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package utils_test

import (
"math"
"testing"

"github.com/ledgerwatch/erigon/cl/utils"
Expand Down Expand Up @@ -95,53 +94,3 @@ func TestIntegerSquareRoot(t *testing.T) {
}
}
}

func TestMax64(t *testing.T) {
testCases := []struct {
a uint64
b uint64
expected uint64
}{
{0, 0, 0},
{0, 1, 1},
{1, 0, 1},
{1, 1, 1},
{10, 5, 10},
{5, 10, 10},
{math.MaxUint64, 0, math.MaxUint64},
{0, math.MaxUint64, math.MaxUint64},
{math.MaxUint64, math.MaxUint64, math.MaxUint64},
}

for _, tc := range testCases {
max := utils.Max64(tc.a, tc.b)
if max != tc.expected {
t.Errorf("Max64 returned incorrect result for %d and %d. Expected: %d, Got: %d", tc.a, tc.b, tc.expected, max)
}
}
}

func TestMin64(t *testing.T) {
testCases := []struct {
a uint64
b uint64
expected uint64
}{
{0, 0, 0},
{0, 1, 0},
{1, 0, 0},
{1, 1, 1},
{10, 5, 5},
{5, 10, 5},
{math.MaxUint64, 0, 0},
{0, math.MaxUint64, 0},
{math.MaxUint64, math.MaxUint64, math.MaxUint64},
}

for _, tc := range testCases {
min := utils.Min64(tc.a, tc.b)
if min != tc.expected {
t.Errorf("Min64 returned incorrect result for %d and %d. Expected: %d, Got: %d", tc.a, tc.b, tc.expected, min)
}
}
}
35 changes: 15 additions & 20 deletions cmd/capcli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,20 @@ import (
"strings"
"time"

"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/turbo/debug"
"github.com/ledgerwatch/log/v3"
"github.com/spf13/afero"
"google.golang.org/grpc"

libcommon "github.com/ledgerwatch/erigon-lib/common"

"github.com/ledgerwatch/erigon-lib/common/datadir"
"github.com/ledgerwatch/erigon-lib/downloader/snaptype"
sentinel "github.com/ledgerwatch/erigon-lib/gointerfaces/sentinelproto"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/metrics"

"github.com/ledgerwatch/erigon/cl/antiquary"
"github.com/ledgerwatch/erigon/cl/clparams"
"github.com/ledgerwatch/erigon/cl/clparams/initial_state"
"github.com/ledgerwatch/erigon/cl/utils/eth_clock"
"github.com/ledgerwatch/erigon/cmd/caplin/caplin1"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/eth/ethconfig/estimate"
"github.com/ledgerwatch/erigon/turbo/snapshotsync/freezeblocks"

"github.com/ledgerwatch/erigon-lib/common/datadir"
"github.com/ledgerwatch/erigon-lib/downloader/snaptype"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon/cl/persistence/beacon_indicies"
"github.com/ledgerwatch/erigon/cl/persistence/format/snapshot_format"
"github.com/ledgerwatch/erigon/cl/persistence/format/snapshot_format/getters"
Expand All @@ -42,13 +37,13 @@ import (
"github.com/ledgerwatch/erigon/cl/phase1/network"
"github.com/ledgerwatch/erigon/cl/phase1/stages"
"github.com/ledgerwatch/erigon/cl/rpc"
"github.com/ledgerwatch/erigon/cl/utils"

"github.com/ledgerwatch/log/v3"
"github.com/spf13/afero"
"google.golang.org/grpc"

sentinel "github.com/ledgerwatch/erigon-lib/gointerfaces/sentinelproto"
"github.com/ledgerwatch/erigon/cl/utils/eth_clock"
"github.com/ledgerwatch/erigon/cmd/caplin/caplin1"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/eth/ethconfig"
"github.com/ledgerwatch/erigon/eth/ethconfig/estimate"
"github.com/ledgerwatch/erigon/turbo/debug"
"github.com/ledgerwatch/erigon/turbo/snapshotsync/freezeblocks"
)

var CLI struct {
Expand Down Expand Up @@ -378,7 +373,7 @@ func (c *CheckSnapshots) Run(ctx *Context) error {
}
previousBlockSlot := genesisHeader.Header.Slot
for i := uint64(1); i < to; i++ {
if utils.Min64(0, i-320) > previousBlockSlot {
if min(0, i-320) > previousBlockSlot {
return fmt.Errorf("snapshot %d has invalid slot", i)
}
// Checking of snapshots is a chain contiguity problem
Expand Down
13 changes: 7 additions & 6 deletions cmd/integration/commands/refetence_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,18 @@ import (
"sync/atomic"
"time"

"github.com/ledgerwatch/log/v3"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore"

common2 "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/common/cmp"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/backup"
mdbx2 "github.com/ledgerwatch/erigon-lib/kv/mdbx"

"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/turbo/debug"
"github.com/ledgerwatch/log/v3"
"github.com/spf13/cobra"
"golang.org/x/sync/errgroup"
"golang.org/x/sync/semaphore"
)

var stateBuckets = []string{
Expand Down Expand Up @@ -262,7 +263,7 @@ func mdbxTopDup(ctx context.Context, chaindata string, bucket string, logger log

var _max int
for _, i := range cnt {
_max = cmp.Max(i, _max)
_max = max(i, _max)
}
for k, i := range cnt {
if i > _max-10 {
Expand Down
Loading