Skip to content
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

Remove aliasing of math standard lib #2163

Merged
merged 2 commits into from
Oct 11, 2023
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
10 changes: 10 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ linters:
- gosec
- gosimple
- govet
- importas
- ineffassign
# - lll
- misspell
Expand Down Expand Up @@ -107,6 +108,15 @@ linters-settings:
gosec:
excludes:
- G107 # Url provided to HTTP request as taint input https://securego.io/docs/rules/g107
importas:
# Do not allow unaliased imports of aliased packages.
no-unaliased: false
# Do not allow non-required aliases.
no-extra-aliases: false
# List of aliases
alias:
- pkg: github.com/ava-labs/avalanchego/utils/math
alias: safemath
revive:
rules:
# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#bool-literal-in-expr
Expand Down
14 changes: 7 additions & 7 deletions network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ import (
"context"
"errors"
"fmt"
"math"
"net"
"strings"
"sync"
"sync/atomic"
"time"

gomath "math"

"github.com/pires/go-proxyproto"

"github.com/prometheus/client_golang/prometheus"
Expand All @@ -37,11 +36,12 @@ import (
"github.com/ava-labs/avalanchego/utils/constants"
"github.com/ava-labs/avalanchego/utils/ips"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/utils/sampler"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/version"

safemath "github.com/ava-labs/avalanchego/utils/math"
)

const (
Expand Down Expand Up @@ -143,7 +143,7 @@ type network struct {
// Call [onCloseCtxCancel] to cancel [onCloseCtx] during close()
onCloseCtxCancel func()

sendFailRateCalculator math.Averager
sendFailRateCalculator safemath.Averager

// Tracks which peers know about which peers
gossipTracker peer.GossipTracker
Expand Down Expand Up @@ -289,7 +289,7 @@ func NewNetwork(
onCloseCtx: onCloseCtx,
onCloseCtxCancel: cancel,

sendFailRateCalculator: math.NewSyncAverager(math.NewAverager(
sendFailRateCalculator: safemath.NewSyncAverager(safemath.NewAverager(
0,
config.SendFailRateHalflife,
time.Now(),
Expand Down Expand Up @@ -1388,8 +1388,8 @@ func (n *network) NodeUptime(subnetID ids.ID) (UptimeResult, error) {
}

return UptimeResult{
WeightedAveragePercentage: gomath.Abs(totalWeightedPercent / totalWeight),
RewardingStakePercentage: gomath.Abs(100 * rewardingStake / totalWeight),
WeightedAveragePercentage: math.Abs(totalWeightedPercent / totalWeight),
RewardingStakePercentage: math.Abs(100 * rewardingStake / totalWeight),
}, nil
}

Expand Down
10 changes: 5 additions & 5 deletions snow/engine/common/bootstrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ package common

import (
"context"

stdmath "math"
"math"

"go.uber.org/zap"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/utils/set"

safemath "github.com/ava-labs/avalanchego/utils/math"
)

const (
Expand Down Expand Up @@ -195,14 +195,14 @@ func (b *bootstrapper) Accepted(ctx context.Context, nodeID ids.NodeID, requestI
weight := b.Beacons.GetWeight(nodeID)
for _, containerID := range containerIDs {
previousWeight := b.acceptedVotes[containerID]
newWeight, err := math.Add64(weight, previousWeight)
newWeight, err := safemath.Add64(weight, previousWeight)
if err != nil {
b.Ctx.Log.Error("failed calculating the Accepted votes",
zap.Uint64("weight", weight),
zap.Uint64("previousWeight", previousWeight),
zap.Error(err),
)
newWeight = stdmath.MaxUint64
newWeight = math.MaxUint64
}
b.acceptedVotes[containerID] = newWeight
}
Expand Down
10 changes: 5 additions & 5 deletions snow/engine/snowman/syncer/state_syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ package syncer
import (
"context"
"fmt"

stdmath "math"
"math"

"go.uber.org/zap"

Expand All @@ -19,9 +18,10 @@ import (
"github.com/ava-labs/avalanchego/snow/engine/snowman/block"
"github.com/ava-labs/avalanchego/snow/validators"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/version"

safemath "github.com/ava-labs/avalanchego/utils/math"
)

var _ common.StateSyncer = (*stateSyncer)(nil)
Expand Down Expand Up @@ -250,7 +250,7 @@ func (ss *stateSyncer) AcceptedStateSummary(ctx context.Context, nodeID ids.Node
continue
}

newWeight, err := math.Add64(nodeWeight, ws.weight)
newWeight, err := safemath.Add64(nodeWeight, ws.weight)
if err != nil {
ss.Ctx.Log.Error("failed to calculate new summary weight",
zap.Stringer("nodeID", nodeID),
Expand All @@ -260,7 +260,7 @@ func (ss *stateSyncer) AcceptedStateSummary(ctx context.Context, nodeID ids.Node
zap.Uint64("previousWeight", ws.weight),
zap.Error(err),
)
newWeight = stdmath.MaxUint64
newWeight = math.MaxUint64
}

ss.Ctx.Log.Verbo("updating summary weight",
Expand Down
4 changes: 2 additions & 2 deletions snow/engine/snowman/syncer/state_syncer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/ava-labs/avalanchego/utils/set"
"github.com/ava-labs/avalanchego/version"

safeMath "github.com/ava-labs/avalanchego/utils/math"
safemath "github.com/ava-labs/avalanchego/utils/math"
)

var (
Expand Down Expand Up @@ -251,7 +251,7 @@ func TestBeaconsAreReachedForFrontiersUponStartup(t *testing.T) {
}

// check that vdrs are reached out for frontiers
require.Len(contactedFrontiersProviders, safeMath.Min(vdrs.Len(), common.MaxOutstandingBroadcastRequests))
require.Len(contactedFrontiersProviders, safemath.Min(vdrs.Len(), common.MaxOutstandingBroadcastRequests))
for beaconID := range contactedFrontiersProviders {
// check that beacon is duly marked as reached out
require.Contains(syncer.pendingSeeders, beaconID)
Expand Down
20 changes: 10 additions & 10 deletions snow/validators/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
package validators

import (
"math"
"testing"

stdmath "math"

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/utils/crypto/bls"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/utils/sampler"
"github.com/ava-labs/avalanchego/utils/set"

safemath "github.com/ava-labs/avalanchego/utils/math"
)

func TestSetAddZeroWeight(t *testing.T) {
Expand Down Expand Up @@ -43,8 +43,8 @@ func TestSetAddOverflow(t *testing.T) {
s := NewSet()
require.NoError(s.Add(ids.GenerateTestNodeID(), nil, ids.Empty, 1))

err := s.Add(ids.GenerateTestNodeID(), nil, ids.Empty, stdmath.MaxUint64)
require.ErrorIs(err, math.ErrOverflow)
err := s.Add(ids.GenerateTestNodeID(), nil, ids.Empty, math.MaxUint64)
require.ErrorIs(err, safemath.ErrOverflow)

require.Equal(uint64(1), s.Weight())
}
Expand All @@ -71,8 +71,8 @@ func TestSetAddWeightOverflow(t *testing.T) {
nodeID := ids.GenerateTestNodeID()
require.NoError(s.Add(nodeID, nil, ids.Empty, 1))

err := s.AddWeight(nodeID, stdmath.MaxUint64-1)
require.ErrorIs(err, math.ErrOverflow)
err := s.AddWeight(nodeID, math.MaxUint64-1)
require.ErrorIs(err, safemath.ErrOverflow)

require.Equal(uint64(2), s.Weight())
}
Expand Down Expand Up @@ -150,7 +150,7 @@ func TestSetRemoveWeightUnderflow(t *testing.T) {
require.NoError(s.Add(nodeID, nil, ids.Empty, 1))

err := s.RemoveWeight(nodeID, 2)
require.ErrorIs(err, math.ErrUnderflow)
require.ErrorIs(err, safemath.ErrUnderflow)

require.Equal(uint64(2), s.Weight())
}
Expand Down Expand Up @@ -354,7 +354,7 @@ func TestSetSample(t *testing.T) {
require.ErrorIs(err, sampler.ErrOutOfRange)

nodeID1 := ids.GenerateTestNodeID()
require.NoError(s.Add(nodeID1, nil, ids.Empty, stdmath.MaxInt64-1))
require.NoError(s.Add(nodeID1, nil, ids.Empty, math.MaxInt64-1))

sampled, err = s.Sample(1)
require.NoError(err)
Expand All @@ -381,7 +381,7 @@ func TestSetString(t *testing.T) {
s := NewSet()
require.NoError(s.Add(nodeID0, nil, ids.Empty, 1))

require.NoError(s.Add(nodeID1, nil, ids.Empty, stdmath.MaxInt64-1))
require.NoError(s.Add(nodeID1, nil, ids.Empty, math.MaxInt64-1))

expected := "Validator Set: (Size = 2, Weight = 9223372036854775807)\n" +
" Validator[0]: NodeID-111111111111111111116DBWJs, 1\n" +
Expand Down
9 changes: 4 additions & 5 deletions utils/sampler/weighted_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ package sampler

import (
"fmt"
"math"
"testing"

stdmath "math"

"github.com/stretchr/testify/require"

"github.com/ava-labs/avalanchego/utils/math"
safemath "github.com/ava-labs/avalanchego/utils/math"
)

var (
Expand Down Expand Up @@ -89,8 +88,8 @@ func TestAllWeighted(t *testing.T) {
}

func WeightedInitializeOverflowTest(t *testing.T, s Weighted) {
err := s.Initialize([]uint64{1, stdmath.MaxUint64})
require.ErrorIs(t, err, math.ErrOverflow)
err := s.Initialize([]uint64{1, math.MaxUint64})
require.ErrorIs(t, err, safemath.ErrOverflow)
}

func WeightedOutOfRangeTest(t *testing.T, s Weighted) {
Expand Down
9 changes: 4 additions & 5 deletions utils/sampler/weighted_without_replacement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ package sampler

import (
"fmt"
"math"
"testing"

stdmath "math"

"github.com/stretchr/testify/require"

"golang.org/x/exp/slices"

"github.com/ava-labs/avalanchego/utils/math"
safemath "github.com/ava-labs/avalanchego/utils/math"
)

var (
Expand Down Expand Up @@ -87,8 +86,8 @@ func WeightedWithoutReplacementInitializeOverflowTest(
t *testing.T,
s WeightedWithoutReplacement,
) {
err := s.Initialize([]uint64{1, stdmath.MaxUint64})
require.ErrorIs(t, err, math.ErrOverflow)
err := s.Initialize([]uint64{1, math.MaxUint64})
require.ErrorIs(t, err, safemath.ErrOverflow)
}

func WeightedWithoutReplacementOutOfRangeTest(
Expand Down
10 changes: 5 additions & 5 deletions vms/avm/states/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (
"bytes"
"errors"
"fmt"
"math"
"sync"
"time"

stdmath "math"

"github.com/prometheus/client_golang/prometheus"

"go.uber.org/zap"
Expand All @@ -24,12 +23,13 @@ import (
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/snow/choices"
"github.com/ava-labs/avalanchego/utils/logging"
"github.com/ava-labs/avalanchego/utils/math"
"github.com/ava-labs/avalanchego/utils/timer"
"github.com/ava-labs/avalanchego/utils/wrappers"
"github.com/ava-labs/avalanchego/vms/avm/block"
"github.com/ava-labs/avalanchego/vms/avm/txs"
"github.com/ava-labs/avalanchego/vms/components/avax"

safemath "github.com/ava-labs/avalanchego/utils/math"
)

const (
Expand Down Expand Up @@ -715,7 +715,7 @@ func (s *state) Prune(lock sync.Locker, log logging.Logger) error {
eta := timer.EstimateETA(
startTime,
progress-startProgress,
stdmath.MaxUint64-startProgress,
math.MaxUint64-startProgress,
)
log.Info("committing state pruning",
zap.Int("numPruned", numPruned),
Expand All @@ -728,7 +728,7 @@ func (s *state) Prune(lock sync.Locker, log logging.Logger) error {
// could take an extremely long period of time; which we should not
// delay processing for.
pruneDuration := now.Sub(lastCommit)
sleepDuration := math.Min(
sleepDuration := safemath.Min(
pruneCommitSleepMultiplier*pruneDuration,
pruneCommitSleepCap,
)
Expand Down
Loading
Loading