Skip to content

Commit

Permalink
gocognit: Lower complexity threshold to 100, fix a few complexity iss…
Browse files Browse the repository at this point in the history
…ues (prysmaticlabs#10542)

Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com>
  • Loading branch information
prestonvanloon and prylabs-bulldozer[bot] authored Apr 19, 2022
1 parent 32ebe94 commit 80ebbcf
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 28 deletions.
14 changes: 10 additions & 4 deletions beacon-chain/db/kv/migration_state_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const batchSize = 10

var migrationStateValidatorsKey = []byte("migration_state_validator")

func migrateStateValidators(ctx context.Context, db *bolt.DB) error {
func shouldMigrateValidators(db *bolt.DB) (bool, error) {
migrateDB := false
if updateErr := db.View(func(tx *bolt.Tx) error {
mb := tx.Bucket(migrationsBucket)
Expand Down Expand Up @@ -46,11 +46,17 @@ func migrateStateValidators(ctx context.Context, db *bolt.DB) error {
return nil
}); updateErr != nil {
log.WithError(updateErr).Errorf("could not migrate bucket: %s", stateBucket)
return updateErr
return false, updateErr
}

// do not migrate the DB
if !migrateDB {
return migrateDB, nil
}

func migrateStateValidators(ctx context.Context, db *bolt.DB) error {
if ok, err := shouldMigrateValidators(db); err != nil {
return err
} else if !ok {
// A migration is not required.
return nil
}

Expand Down
31 changes: 9 additions & 22 deletions beacon-chain/db/kv/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,10 @@ func (s *Store) SaveStates(ctx context.Context, states []state.ReadOnlyBeaconSta
})
}

type withValidators interface {
GetValidators() []*ethpb.Validator
}

// SaveStatesEfficient stores multiple states to the db (new schema) using the provided corresponding roots.
func (s *Store) SaveStatesEfficient(ctx context.Context, states []state.ReadOnlyBeaconState, blockRoots [][32]byte) error {
ctx, span := trace.StartSpan(ctx, "BeaconDB.SaveStatesEfficient")
Expand All @@ -161,29 +165,12 @@ func (s *Store) SaveStatesEfficient(ctx context.Context, states []state.ReadOnly
validatorsEntries := make(map[string]*ethpb.Validator) // It's a map to make sure that you store only new validator entries.
validatorKeys := make([][]byte, len(states)) // For every state, this stores a compressed list of validator keys.
for i, st := range states {
var validators []*ethpb.Validator
switch st.InnerStateUnsafe().(type) {
case *ethpb.BeaconState:
pbState, err := v1.ProtobufBeaconState(st.InnerStateUnsafe())
if err != nil {
return err
}
validators = pbState.Validators
case *ethpb.BeaconStateAltair:
pbState, err := v2.ProtobufBeaconState(st.InnerStateUnsafe())
if err != nil {
return err
}
validators = pbState.Validators
case *ethpb.BeaconStateBellatrix:
pbState, err := v3.ProtobufBeaconState(st.InnerStateUnsafe())
if err != nil {
return err
}
validators = pbState.Validators
default:
return errors.New("invalid state type")
pb, ok := st.InnerStateUnsafe().(withValidators)
if !ok {
return errors.New("could not cast state to interface with GetValidators()")
}
validators := pb.GetValidators()

// yank out the validators and store them in separate table to save space.
var hashes []byte
for _, val := range validators {
Expand Down
4 changes: 3 additions & 1 deletion nogo_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,9 @@
"external/.*": "Third party code",
"rules_go_work-.*": "Third party code",
".*\\.pb.*.go": "Generated code is ok",
".*generated\\.ssz\\.go": "Generated code is ok"
".*generated\\.ssz\\.go": "Generated code is ok",
".*_test\\.go": "Tests are ok (for now)",
"tools/analyzers/ineffassign/ineffassign\\.go": "3rd party code with a massive switch statement"
}
}
}
2 changes: 1 addition & 1 deletion tools/analyzers/gocognit/analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var Analyzer = &analysis.Analyzer{
// > 50 Untestable code, very high risk
//
// This threshold should be lowered to 50 over time.
const over = 130
const over = 100

func run(pass *analysis.Pass) (interface{}, error) {
inspect, ok := pass.ResultOf[inspect.Analyzer].(*inspector.Inspector)
Expand Down

0 comments on commit 80ebbcf

Please sign in to comment.