Refactor: Continue reducing cognitive complexity (#10862)

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

* Refactor api/gateway/apimiddleware/process_field.go

* Refactor beacon-chain/sync/initial-sync/blocks_queue.go

* Refactor validator/db/kv/migration_optimal_attester_protection.go

* goimports

Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
This commit is contained in:
Jie Hou
2022-06-16 02:34:59 +08:00
committed by GitHub
parent 838963c9f7
commit 6c878b1665
4 changed files with 184 additions and 158 deletions

View File

@@ -115,30 +115,7 @@ func (s *Store) migrateOptimalAttesterProtectionUp(ctx context.Context) error {
// Migrate attester protection from the more optimal format to the old format in the DB.
func (s *Store) migrateOptimalAttesterProtectionDown(ctx context.Context) error {
// First we extract the public keys we are migrating down for.
pubKeys := make([][fieldparams.BLSPubkeyLength]byte, 0)
err := s.view(func(tx *bolt.Tx) error {
mb := tx.Bucket(migrationsBucket)
if b := mb.Get(migrationOptimalAttesterProtectionKey); b == nil {
// Migration has not occurred, meaning data is already in old format
// so no need to perform a down migration.
return nil
}
bkt := tx.Bucket(pubKeysBucket)
if bkt == nil {
return nil
}
return bkt.ForEach(func(pubKey, v []byte) error {
if pubKey == nil {
return nil
}
pkBucket := bkt.Bucket(pubKey)
if pkBucket == nil {
return nil
}
pubKeys = append(pubKeys, bytesutil.ToBytes48(pubKey))
return nil
})
})
pubKeys, err := s.extractPubKeysForMigratingDown()
if err != nil {
return err
}
@@ -247,3 +224,34 @@ func (s *Store) migrateOptimalAttesterProtectionDown(ctx context.Context) error
return migrationsBkt.Delete(migrationOptimalAttesterProtectionKey)
})
}
func (s *Store) extractPubKeysForMigratingDown() ([][fieldparams.BLSPubkeyLength]byte, error) {
pubKeys := make([][fieldparams.BLSPubkeyLength]byte, 0)
err := s.view(func(tx *bolt.Tx) error {
mb := tx.Bucket(migrationsBucket)
if b := mb.Get(migrationOptimalAttesterProtectionKey); b == nil {
// Migration has not occurred, meaning data is already in old format
// so no need to perform a down migration.
return nil
}
bkt := tx.Bucket(pubKeysBucket)
if bkt == nil {
return nil
}
return bkt.ForEach(func(pubKey, v []byte) error {
if pubKey == nil {
return nil
}
pkBucket := bkt.Bucket(pubKey)
if pkBucket == nil {
return nil
}
pubKeys = append(pubKeys, bytesutil.ToBytes48(pubKey))
return nil
})
})
if err != nil {
return nil, err
}
return pubKeys, nil
}