From 80ebbcf03edefd516c1c3560b22d57c608faf866 Mon Sep 17 00:00:00 2001 From: Preston Van Loon Date: Tue, 19 Apr 2022 09:12:24 +0200 Subject: [PATCH] gocognit: Lower complexity threshold to 100, fix a few complexity issues (#10542) Co-authored-by: prylabs-bulldozer[bot] <58059840+prylabs-bulldozer[bot]@users.noreply.github.com> --- .../db/kv/migration_state_validators.go | 14 ++++++--- beacon-chain/db/kv/state.go | 31 ++++++------------- nogo_config.json | 4 ++- tools/analyzers/gocognit/analyzer.go | 2 +- 4 files changed, 23 insertions(+), 28 deletions(-) diff --git a/beacon-chain/db/kv/migration_state_validators.go b/beacon-chain/db/kv/migration_state_validators.go index 6f1703c3d..429d44c48 100644 --- a/beacon-chain/db/kv/migration_state_validators.go +++ b/beacon-chain/db/kv/migration_state_validators.go @@ -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) @@ -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 } diff --git a/beacon-chain/db/kv/state.go b/beacon-chain/db/kv/state.go index 8d9d0e28c..ec4c78ec1 100644 --- a/beacon-chain/db/kv/state.go +++ b/beacon-chain/db/kv/state.go @@ -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") @@ -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 { diff --git a/nogo_config.json b/nogo_config.json index ef538c359..1e0e0e8f2 100644 --- a/nogo_config.json +++ b/nogo_config.json @@ -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" } } } diff --git a/tools/analyzers/gocognit/analyzer.go b/tools/analyzers/gocognit/analyzer.go index 6cef89591..9c5d01640 100644 --- a/tools/analyzers/gocognit/analyzer.go +++ b/tools/analyzers/gocognit/analyzer.go @@ -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)