Skip to content

Commit 1f21e19

Browse files
authored
Fix multiple anti-patterns from DeepSource analysis (#6951)
* Merge variable declaration and assignment * Use result of type assertion to simplify cases * Replace call to bytes.Compare with bytes.Equal * Drop unnecessary use of the blank identifier * Replace x.Sub(time.Now()) with time.Until(x) * Function literal can be simplified * Use a single append to concatenate two slices * Replace time.Now().Sub(x) with time.Since(x) * Omit comparison with boolean constant * Omit redundant nil check on slices * Nested if can be replaced with else-if * Function call can be replaced with helper function * Omit redundant control flow * Use plain channel send or receive * Simplify returning boolean expression * Merge branch 'origin-master' into fix-antipatterns * Merge branch 'master' into fix-antipatterns
1 parent 18b3203 commit 1f21e19

File tree

40 files changed

+105
-182
lines changed

40 files changed

+105
-182
lines changed

beacon-chain/blockchain/process_block.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,10 +372,8 @@ func (s *Service) savePostStateInfo(ctx context.Context, r [32]byte, b *ethpb.Si
372372
defer span.End()
373373
if initSync {
374374
s.saveInitSyncBlock(r, b)
375-
} else {
376-
if err := s.beaconDB.SaveBlock(ctx, b); err != nil {
377-
return errors.Wrapf(err, "could not save block from slot %d", b.Block.Slot)
378-
}
375+
} else if err := s.beaconDB.SaveBlock(ctx, b); err != nil {
376+
return errors.Wrapf(err, "could not save block from slot %d", b.Block.Slot)
379377
}
380378
if err := s.stateGen.SaveState(ctx, r, state); err != nil {
381379
return errors.Wrap(err, "could not save state")

beacon-chain/core/blocks/deposit.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,8 @@ func ProcessDeposit(beaconState *stateTrie.BeaconState, deposit *ethpb.Deposit,
179179
if err := beaconState.AppendBalance(amount); err != nil {
180180
return nil, err
181181
}
182-
} else {
183-
if err := helpers.IncreaseBalance(beaconState, index, amount); err != nil {
184-
return nil, err
185-
}
182+
} else if err := helpers.IncreaseBalance(beaconState, index, amount); err != nil {
183+
return nil, err
186184
}
187185

188186
return beaconState, nil

beacon-chain/core/helpers/signing_root.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ func ComputeDomainAndSign(state *state.BeaconState, epoch uint64, obj interface{
4949
// ))
5050
func ComputeSigningRoot(object interface{}, domain []byte) ([32]byte, error) {
5151
return signingData(func() ([32]byte, error) {
52-
switch object.(type) {
52+
switch t := object.(type) {
5353
case *ethpb.BeaconBlock:
54-
return stateutil.BlockRoot(object.(*ethpb.BeaconBlock))
54+
return stateutil.BlockRoot(t)
5555
case *ethpb.AttestationData:
56-
return stateutil.AttestationDataRoot(object.(*ethpb.AttestationData))
56+
return stateutil.AttestationDataRoot(t)
5757
default:
5858
// utilise generic ssz library
5959
return ssz.HashTreeRoot(object)

beacon-chain/db/kv/backup.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ func (kv *Store) Backup(ctx context.Context) error {
5454
if err != nil {
5555
return err
5656
}
57-
return b.ForEach(func(k []byte, v []byte) error {
58-
return b2.Put(k, v)
59-
})
57+
return b.ForEach(b2.Put)
6058
})
6159
})
6260
})

beacon-chain/gateway/gateway.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ func (g *Gateway) Start() {
8787
return
8888
}
8989
}()
90-
91-
return
9290
}
9391

9492
// Status of grpc gateway. Returns an error if this service is unhealthy.

beacon-chain/node/node.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -319,12 +319,10 @@ func (b *BeaconNode) startDB(cliCtx *cli.Context) error {
319319
if err != nil {
320320
return errors.Wrap(err, "could not create new database")
321321
}
322-
} else {
323322
// Only check if historical states were deleted and needed to recompute when
324323
// user doesn't want to skip.
325-
if err := d.HistoricalStatesDeleted(b.ctx); err != nil {
326-
return err
327-
}
324+
} else if err := d.HistoricalStatesDeleted(b.ctx); err != nil {
325+
return err
328326
}
329327

330328
if err := d.RunMigrations(b.ctx); err != nil {

beacon-chain/operations/attestations/prune_expired.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,5 @@ func (s *Service) expired(slot uint64) bool {
6262
expirationSlot := slot + params.BeaconConfig().SlotsPerEpoch
6363
expirationTime := s.genesisTime + expirationSlot*params.BeaconConfig().SecondsPerSlot
6464
currentTime := uint64(roughtime.Now().Unix())
65-
if currentTime >= expirationTime {
66-
return true
67-
}
68-
return false
65+
return currentTime >= expirationTime
6966
}

beacon-chain/p2p/service.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -370,18 +370,15 @@ func (s *Service) awaitStateInitialized() {
370370
stateChannel := make(chan *feed.Event, 1)
371371
stateSub := s.stateNotifier.StateFeed().Subscribe(stateChannel)
372372
defer stateSub.Unsubscribe()
373-
for {
374-
select {
375-
case event := <-stateChannel:
376-
if event.Type == statefeed.Initialized {
377-
data, ok := event.Data.(*statefeed.InitializedData)
378-
if !ok {
379-
log.Fatalf("Received wrong data over state initialized feed: %v", data)
380-
}
381-
s.genesisTime = data.StartTime
382-
s.genesisValidatorsRoot = data.GenesisValidatorsRoot
383-
return
373+
for event := range stateChannel {
374+
if event.Type == statefeed.Initialized {
375+
data, ok := event.Data.(*statefeed.InitializedData)
376+
if !ok {
377+
log.Fatalf("Received wrong data over state initialized feed: %v", data)
384378
}
379+
s.genesisTime = data.StartTime
380+
s.genesisValidatorsRoot = data.GenesisValidatorsRoot
381+
return
385382
}
386383
}
387384
}

beacon-chain/p2p/testing/mock_peermanager.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,12 @@ func (m MockPeerManager) ENR() *enr.Record {
3636
}
3737

3838
// RefreshENR .
39-
func (m MockPeerManager) RefreshENR() {
40-
return
41-
}
39+
func (m MockPeerManager) RefreshENR() {}
4240

4341
// FindPeersWithSubnet .
4442
func (m MockPeerManager) FindPeersWithSubnet(ctx context.Context, index uint64) (bool, error) {
4543
return true, nil
4644
}
4745

4846
// AddPingMethod .
49-
func (m MockPeerManager) AddPingMethod(reqFunc func(ctx context.Context, id peer.ID) error) {
50-
return
51-
}
47+
func (m MockPeerManager) AddPingMethod(reqFunc func(ctx context.Context, id peer.ID) error) {}

beacon-chain/p2p/testing/p2p.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,7 @@ func (p *TestP2P) FindPeersWithSubnet(ctx context.Context, index uint64) (bool,
318318
}
319319

320320
// RefreshENR mocks the p2p func.
321-
func (p *TestP2P) RefreshENR() {
322-
return
323-
}
321+
func (p *TestP2P) RefreshENR() {}
324322

325323
// ForkDigest mocks the p2p func.
326324
func (p *TestP2P) ForkDigest() ([4]byte, error) {

0 commit comments

Comments
 (0)