Skip to content

Commit

Permalink
QSP-9 Prevent Casting to Int if Possible (#6349)
Browse files Browse the repository at this point in the history
* no cast to int

* fix up significant casting issues

* more casting

* even more casting fixes

* more casts

* fix subnets

* back to ints

* final touches

* broken test fix

* add in blocks test fix

* unskip

* revert bytes fixes

* casting fixes

* Update beacon-chain/db/kv/state.go

* Update beacon-chain/db/kv/blocks.go

* fmt

* slash:

* fix val tests

* fix up conf

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
Co-authored-by: Victor Farazdagi <simple.square@gmail.com>
  • Loading branch information
3 people committed Jun 26, 2020
1 parent 78465e2 commit 252f758
Show file tree
Hide file tree
Showing 43 changed files with 138 additions and 125 deletions.
9 changes: 4 additions & 5 deletions beacon-chain/blockchain/info.go
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/hex"
"fmt"
"net/http"
"strconv"

"github.com/emicklei/dot"
)
Expand Down Expand Up @@ -51,10 +50,10 @@ func (s *Service) TreeHandler(w http.ResponseWriter, _ *http.Request) {

for i := len(nodes) - 1; i >= 0; i-- {
// Construct label for each node.
slot := strconv.Itoa(int(nodes[i].Slot))
weight := strconv.Itoa(int(nodes[i].Weight / 1e9)) // Convert unit Gwei to unit ETH.
votes := strconv.Itoa(int(nodes[i].Weight / 1e9 / avgBalance))
index := strconv.Itoa(i)
slot := fmt.Sprintf("%d", nodes[i].Slot)
weight := fmt.Sprintf("%d", nodes[i].Weight/1e9) // Convert unit Gwei to unit ETH.
votes := fmt.Sprintf("%d", nodes[i].Weight/1e9/avgBalance)
index := fmt.Sprintf("%d", i)
g := nodes[i].Graffiti[:]
graffiti := hex.EncodeToString(g[:8])
label := "slot: " + slot + "\n votes: " + votes + "\n weight: " + weight + "\n graffiti: " + graffiti
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/blockchain/process_block.go
Expand Up @@ -222,7 +222,7 @@ func (s *Service) onBlockInitialSyncStateTransition(ctx context.Context, signed
}

// Rate limit how many blocks (2 epochs worth of blocks) a node keeps in the memory.
if len(s.getInitSyncBlocks()) > int(initialSyncBlockCacheSize) {
if uint64(len(s.getInitSyncBlocks())) > initialSyncBlockCacheSize {
if err := s.beaconDB.SaveBlocks(ctx, s.getInitSyncBlocks()); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/blockchain/service.go
Expand Up @@ -304,7 +304,7 @@ func (s *Service) Stop() error {
// Status always returns nil unless there is an error condition that causes
// this service to be unhealthy.
func (s *Service) Status() error {
if runtime.NumGoroutine() > int(s.maxRoutines) {
if int64(runtime.NumGoroutine()) > s.maxRoutines {
return fmt.Errorf("too many goroutines %d", runtime.NumGoroutine())
}
return nil
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/cache/checkpoint_state.go
Expand Up @@ -20,7 +20,7 @@ var (
// maxCheckpointStateSize defines the max number of entries check point to state cache can contain.
// Choosing 10 to account for multiple forks, this allows 5 forks per epoch boundary with 2 epochs
// window to accept attestation based on latest spec.
maxCheckpointStateSize = 10
maxCheckpointStateSize = uint64(10)

// Metrics.
checkpointStateMiss = promauto.NewCounter(prometheus.CounterOpts{
Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/cache/checkpoint_state_test.go
Expand Up @@ -118,20 +118,20 @@ func TestCheckpointStateCache_MaxSize(t *testing.T) {
if err != nil {
t.Fatal(err)
}
for i := 0; i < maxCheckpointStateSize+100; i++ {
if err := st.SetSlot(uint64(i)); err != nil {
for i := uint64(0); i < maxCheckpointStateSize+100; i++ {
if err := st.SetSlot(i); err != nil {
t.Fatal(err)
}
info := &CheckpointState{
Checkpoint: &ethpb.Checkpoint{Epoch: uint64(i)},
Checkpoint: &ethpb.Checkpoint{Epoch: i},
State: st,
}
if err := c.AddCheckpointState(info); err != nil {
t.Fatal(err)
}
}

if len(c.cache.ListKeys()) != maxCheckpointStateSize {
if uint64(len(c.cache.ListKeys())) != maxCheckpointStateSize {
t.Errorf(
"Expected hash cache key size to be %d, got %d",
maxCheckpointStateSize,
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/cache/committee.go
Expand Up @@ -19,7 +19,7 @@ var (
// maxCommitteesCacheSize defines the max number of shuffled committees on per randao basis can cache.
// Due to reorgs, it's good to keep the old cache around for quickly switch over. 10 is a generous
// cache size as it considers 3 concurrent branches over 3 epochs.
maxCommitteesCacheSize = 10
maxCommitteesCacheSize = uint64(10)

// CommitteeCacheMiss tracks the number of committee requests that aren't present in the cache.
CommitteeCacheMiss = promauto.NewCounter(prometheus.CounterOpts{
Expand Down Expand Up @@ -96,7 +96,7 @@ func (c *CommitteeCache) Committee(slot uint64, seed [32]byte, index uint64) ([]
indexOffSet := index + (slot%params.BeaconConfig().SlotsPerEpoch)*committeeCountPerSlot
start, end := startEndIndices(item, indexOffSet)

if int(end) > len(item.ShuffledIndices) || end < start {
if end > uint64(len(item.ShuffledIndices)) || end < start {
return nil, errors.New("requested index out of bound")
}

Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/cache/committee_fuzz_test.go
Expand Up @@ -38,7 +38,7 @@ func TestCommitteeCache_FuzzCommitteesByEpoch(t *testing.T) {
}
}

if len(cache.CommitteeCache.ListKeys()) != maxCommitteesCacheSize {
if uint64(len(cache.CommitteeCache.ListKeys())) != maxCommitteesCacheSize {
t.Error("Incorrect key size")
}
}
Expand All @@ -62,7 +62,7 @@ func TestCommitteeCache_FuzzActiveIndices(t *testing.T) {
}
}

if len(cache.CommitteeCache.ListKeys()) != maxCommitteesCacheSize {
if uint64(len(cache.CommitteeCache.ListKeys())) != maxCommitteesCacheSize {
t.Error("Incorrect key size")
}
}
2 changes: 1 addition & 1 deletion beacon-chain/cache/committee_test.go
Expand Up @@ -182,7 +182,7 @@ func TestCommitteeCache_CanRotate(t *testing.T) {
}

k := cache.CommitteeCache.ListKeys()
if len(k) != maxCommitteesCacheSize {
if uint64(len(k)) != maxCommitteesCacheSize {
t.Errorf("wanted: %d, got: %d", maxCommitteesCacheSize, len(k))
}

Expand Down
6 changes: 3 additions & 3 deletions beacon-chain/cache/common.go
Expand Up @@ -8,12 +8,12 @@ import (
var (
// maxCacheSize is 4x of the epoch length for additional cache padding.
// Requests should be only accessing committees within defined epoch length.
maxCacheSize = int(4 * params.BeaconConfig().SlotsPerEpoch)
maxCacheSize = 4 * params.BeaconConfig().SlotsPerEpoch
)

// trim the FIFO queue to the maxSize.
func trim(queue *cache.FIFO, maxSize int) {
for s := len(queue.ListKeys()); s > maxSize; s-- {
func trim(queue *cache.FIFO, maxSize uint64) {
for s := uint64(len(queue.ListKeys())); s > maxSize; s-- {
_, err := queue.Pop(popProcessNoopFunc)
if err != nil {
// popProcessNoopFunc never returns an error, but we handle this anyway to make linter
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/cache/depositcache/deposits_test.go
Expand Up @@ -191,7 +191,7 @@ func TestBeaconDB_DepositsNumberAndRootAtHeight_ReturnsAppropriateCountAndRoot(t
}

n, root := dc.DepositsNumberAndRootAtHeight(context.Background(), big.NewInt(11))
if int(n) != 5 {
if n != 5 {
t.Errorf("Returned unexpected deposits number %d wanted %d", n, 5)
}

Expand All @@ -217,7 +217,7 @@ func TestBeaconDB_DepositsNumberAndRootAtHeight_ReturnsEmptyTrieIfBlockHeightLes
}

n, root := dc.DepositsNumberAndRootAtHeight(context.Background(), big.NewInt(2))
if int(n) != 0 {
if n != 0 {
t.Errorf("Returned unexpected deposits number %d wanted %d", n, 0)
}

Expand Down
6 changes: 3 additions & 3 deletions beacon-chain/cache/subnet_ids.go
Expand Up @@ -25,12 +25,12 @@ var SubnetIDs = newSubnetIDs()
func newSubnetIDs() *subnetIDs {
// Given a node can calculate committee assignments of current epoch and next epoch.
// Max size is set to 2 epoch length.
cacheSize := int(params.BeaconConfig().MaxCommitteesPerSlot * params.BeaconConfig().SlotsPerEpoch * 2)
attesterCache, err := lru.New(cacheSize)
cacheSize := params.BeaconConfig().MaxCommitteesPerSlot * params.BeaconConfig().SlotsPerEpoch * 2
attesterCache, err := lru.New(int(cacheSize))
if err != nil {
panic(err)
}
aggregatorCache, err := lru.New(cacheSize)
aggregatorCache, err := lru.New(int(cacheSize))
if err != nil {
panic(err)
}
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/cache/subnet_ids_test.go
Expand Up @@ -65,15 +65,15 @@ func TestSubnetIDsCache_PersistentCommitteeRoundtrip(t *testing.T) {
c.AddPersistentCommittee(pubkey[:], []uint64{uint64(i)}, 0)
}

for i := 0; i < 20; i++ {
for i := uint64(0); i < 20; i++ {
pubkey := [48]byte{byte(i)}

idxs, ok, _ := c.GetPersistentSubnets(pubkey[:])
if !ok {
t.Errorf("Couldn't find entry in cache for pubkey %#x", pubkey)
continue
}
if int(idxs[0]) != i {
if idxs[0] != i {
t.Fatalf("Wanted index of %d but got %d", i, idxs[0])
}
}
Expand Down
2 changes: 1 addition & 1 deletion beacon-chain/core/blocks/block_operations.go
Expand Up @@ -1109,7 +1109,7 @@ func ProcessVoluntaryExits(
if exit == nil || exit.Exit == nil {
return nil, errors.New("nil voluntary exit in block body")
}
if int(exit.Exit.ValidatorIndex) >= beaconState.NumValidators() {
if exit.Exit.ValidatorIndex >= uint64(beaconState.NumValidators()) {
return nil, fmt.Errorf(
"validator index out of bound %d > %d",
exit.Exit.ValidatorIndex,
Expand Down
10 changes: 5 additions & 5 deletions beacon-chain/core/blocks/eth1_data_test.go
Expand Up @@ -11,9 +11,9 @@ import (
"github.com/prysmaticlabs/prysm/shared/params"
)

func FakeDeposits(n int) []*ethpb.Eth1Data {
func FakeDeposits(n uint64) []*ethpb.Eth1Data {
deposits := make([]*ethpb.Eth1Data, n)
for i := 0; i < n; i++ {
for i := uint64(0); i < n; i++ {
deposits[i] = &ethpb.Eth1Data{
DepositCount: 1,
DepositRoot: []byte("root"),
Expand All @@ -30,23 +30,23 @@ func TestEth1DataHasEnoughSupport(t *testing.T) {
votingPeriodLength uint64
}{
{
stateVotes: FakeDeposits(4 * int(params.BeaconConfig().SlotsPerEpoch)),
stateVotes: FakeDeposits(4 * params.BeaconConfig().SlotsPerEpoch),
data: &ethpb.Eth1Data{
DepositCount: 1,
DepositRoot: []byte("root"),
},
hasSupport: true,
votingPeriodLength: 7,
}, {
stateVotes: FakeDeposits(4 * int(params.BeaconConfig().SlotsPerEpoch)),
stateVotes: FakeDeposits(4 * params.BeaconConfig().SlotsPerEpoch),
data: &ethpb.Eth1Data{
DepositCount: 1,
DepositRoot: []byte("root"),
},
hasSupport: false,
votingPeriodLength: 8,
}, {
stateVotes: FakeDeposits(4 * int(params.BeaconConfig().SlotsPerEpoch)),
stateVotes: FakeDeposits(4 * params.BeaconConfig().SlotsPerEpoch),
data: &ethpb.Eth1Data{
DepositCount: 1,
DepositRoot: []byte("root"),
Expand Down
14 changes: 7 additions & 7 deletions beacon-chain/core/epoch/epoch_processing.go
Expand Up @@ -113,7 +113,7 @@ func ProcessRegistryUpdates(state *stateTrie.BeaconState) (*stateTrie.BeaconStat
sort.Sort(sortableIndices{indices: activationQ, validators: vals})

// Only activate just enough validators according to the activation churn limit.
limit := len(activationQ)
limit := uint64(len(activationQ))
activeValidatorCount, err := helpers.ActiveValidatorCount(state, currentEpoch)
if err != nil {
return nil, errors.Wrap(err, "could not get active validator count")
Expand All @@ -125,8 +125,8 @@ func ProcessRegistryUpdates(state *stateTrie.BeaconState) (*stateTrie.BeaconStat
}

// Prevent churn limit cause index out of bound.
if int(churnLimit) < limit {
limit = int(churnLimit)
if churnLimit < limit {
limit = churnLimit
}

activationExitEpoch := helpers.ActivationExitEpoch(currentEpoch)
Expand Down Expand Up @@ -274,22 +274,22 @@ func ProcessFinalUpdates(state *stateTrie.BeaconState) (*stateTrie.BeaconState,

// Set total slashed balances.
slashedExitLength := params.BeaconConfig().EpochsPerSlashingsVector
slashedEpoch := int(nextEpoch % slashedExitLength)
slashedEpoch := nextEpoch % slashedExitLength
slashings := state.Slashings()
if len(slashings) != int(slashedExitLength) {
if uint64(len(slashings)) != slashedExitLength {
return nil, fmt.Errorf(
"state slashing length %d different than EpochsPerHistoricalVector %d",
len(slashings),
slashedExitLength,
)
}
if err := state.UpdateSlashingsAtIndex(uint64(slashedEpoch) /* index */, 0 /* value */); err != nil {
if err := state.UpdateSlashingsAtIndex(slashedEpoch /* index */, 0 /* value */); err != nil {
return nil, err
}

// Set RANDAO mix.
randaoMixLength := params.BeaconConfig().EpochsPerHistoricalVector
if state.RandaoMixesLength() != int(randaoMixLength) {
if uint64(state.RandaoMixesLength()) != randaoMixLength {
return nil, fmt.Errorf(
"state randao length %d different than EpochsPerHistoricalVector %d",
state.RandaoMixesLength(),
Expand Down
6 changes: 3 additions & 3 deletions beacon-chain/core/epoch/epoch_processing_test.go
Expand Up @@ -399,7 +399,7 @@ func TestProcessRegistryUpdates_EligibleToActivate(t *testing.T) {
if err != nil {
t.Error(err)
}
for i := 0; i < int(limit)+10; i++ {
for i := uint64(0); i < limit+10; i++ {
base.Validators = append(base.Validators, &ethpb.Validator{
ActivationEligibilityEpoch: params.BeaconConfig().FarFutureEpoch,
EffectiveBalance: params.BeaconConfig().MaxEffectiveBalance,
Expand All @@ -417,11 +417,11 @@ func TestProcessRegistryUpdates_EligibleToActivate(t *testing.T) {
t.Errorf("Could not update registry %d, wanted activation eligibility epoch %d got %d",
i, currentEpoch, validator.ActivationEligibilityEpoch)
}
if i < int(limit) && validator.ActivationEpoch != helpers.ActivationExitEpoch(currentEpoch) {
if uint64(i) < limit && validator.ActivationEpoch != helpers.ActivationExitEpoch(currentEpoch) {
t.Errorf("Could not update registry %d, validators failed to activate: wanted activation epoch %d, got %d",
i, helpers.ActivationExitEpoch(currentEpoch), validator.ActivationEpoch)
}
if i >= int(limit) && validator.ActivationEpoch != params.BeaconConfig().FarFutureEpoch {
if uint64(i) >= limit && validator.ActivationEpoch != params.BeaconConfig().FarFutureEpoch {
t.Errorf("Could not update registry %d, validators should not have been activated, wanted activation epoch: %d, got %d",
i, params.BeaconConfig().FarFutureEpoch, validator.ActivationEpoch)
}
Expand Down
8 changes: 4 additions & 4 deletions beacon-chain/core/helpers/committee_test.go
Expand Up @@ -535,14 +535,14 @@ func TestShuffledIndices_ShuffleRightLength(t *testing.T) {

func TestUpdateCommitteeCache_CanUpdate(t *testing.T) {
ClearCache()
validatorCount := int(params.BeaconConfig().MinGenesisActiveValidatorCount)
validatorCount := params.BeaconConfig().MinGenesisActiveValidatorCount
validators := make([]*ethpb.Validator, validatorCount)
indices := make([]uint64, validatorCount)
for i := 0; i < validatorCount; i++ {
for i := uint64(0); i < validatorCount; i++ {
validators[i] = &ethpb.Validator{
ExitEpoch: params.BeaconConfig().FarFutureEpoch,
}
indices[i] = uint64(i)
indices[i] = i
}
state, err := beaconstate.InitializeFromProto(&pb.BeaconState{
Validators: validators,
Expand All @@ -567,7 +567,7 @@ func TestUpdateCommitteeCache_CanUpdate(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if len(indices) != int(params.BeaconConfig().TargetCommitteeSize) {
if uint64(len(indices)) != params.BeaconConfig().TargetCommitteeSize {
t.Errorf("Did not save correct indices lengths, got %d wanted %d", len(indices), params.BeaconConfig().TargetCommitteeSize)
}
}
Expand Down
12 changes: 6 additions & 6 deletions beacon-chain/core/helpers/shuffle_test.go
Expand Up @@ -53,18 +53,18 @@ func TestShuffleList_OK(t *testing.T) {

func TestSplitIndices_OK(t *testing.T) {
var l []uint64
validators := 64000
for i := 0; i < validators; i++ {
l = append(l, uint64(i))
numValidators := uint64(64000)
for i := uint64(0); i < numValidators; i++ {
l = append(l, i)
}
split := SplitIndices(l, params.BeaconConfig().SlotsPerEpoch)
if len(split) != int(params.BeaconConfig().SlotsPerEpoch) {
if uint64(len(split)) != params.BeaconConfig().SlotsPerEpoch {
t.Errorf("Split list failed due to incorrect length, wanted:%v, got:%v", params.BeaconConfig().SlotsPerEpoch, len(split))
}

for _, s := range split {
if len(s) != validators/int(params.BeaconConfig().SlotsPerEpoch) {
t.Errorf("Split list failed due to incorrect length, wanted:%v, got:%v", validators/int(params.BeaconConfig().SlotsPerEpoch), len(s))
if uint64(len(s)) != numValidators/params.BeaconConfig().SlotsPerEpoch {
t.Errorf("Split list failed due to incorrect length, wanted:%v, got:%v", numValidators/params.BeaconConfig().SlotsPerEpoch, len(s))
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/core/helpers/validators.go
Expand Up @@ -243,7 +243,7 @@ func ComputeProposerIndex(bState *stateTrie.BeaconState, activeIndices []uint64,
return 0, err
}
candidateIndex = activeIndices[candidateIndex]
if int(candidateIndex) >= bState.NumValidators() {
if candidateIndex >= uint64(bState.NumValidators()) {
return 0, errors.New("active index out of range")
}
b := append(seed[:], bytesutil.Bytes8(i/32)...)
Expand Down Expand Up @@ -295,7 +295,7 @@ func ComputeProposerIndexWithValidators(validators []*ethpb.Validator, activeInd
return 0, err
}
candidateIndex = activeIndices[candidateIndex]
if int(candidateIndex) >= len(validators) {
if candidateIndex >= uint64(len(validators)) {
return 0, errors.New("active index out of range")
}
b := append(seed[:], bytesutil.Bytes8(i/32)...)
Expand Down

0 comments on commit 252f758

Please sign in to comment.