Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/interchainstaking/keeper: please use early returns as idiomatic code and reduce nesting #702

Closed
1 of 3 tasks
odeke-em opened this issue Oct 16, 2023 · 0 comments · Fixed by #705
Closed
1 of 3 tasks

Comments

@odeke-em
Copy link
Contributor

Summary of Bug

This code section

connection, found := k.IBCKeeper.ConnectionKeeper.GetConnection(ctx, zone.ConnectionId)
if found {
consState, found := k.IBCKeeper.ClientKeeper.GetLatestClientConsensusState(ctx, connection.GetClientID())
if found {
tmConsState, ok := consState.(*tmtypes.ConsensusState)
if ok {
if len(zone.IbcNextValidatorsHash) == 0 || !bytes.Equal(zone.IbcNextValidatorsHash, tmConsState.NextValidatorsHash.Bytes()) {
k.Logger(ctx).Info("IBC ValSet has changed; requerying valset")
// trigger valset update.
period := int64(k.GetParam(ctx, types.KeyValidatorSetInterval))
query := stakingTypes.QueryValidatorsRequest{}
err := k.EmitValSetQuery(ctx, zone.ConnectionId, zone.ChainId, query, sdkmath.NewInt(period))
if err != nil {
k.Logger(ctx).Error("unable to trigger valset update query", "error", err.Error())
// failing to emit the valset update is not terminal but constitutes
// an error, as if this starts happening frequent it is something
// we should investigate.
}
zone.IbcNextValidatorsHash = tmConsState.NextValidatorsHash.Bytes()
k.SetZone(ctx, zone)
}
}
}
}

could be improved by following https://cyber.orijtech.com/scsec/cosmos-go-coding-guide#reduce-code-nesting-with-early-returns-and-inversions

Expected Behaviour

connection, found := k.IBCKeeper.ConnectionKeeper.GetConnection(ctx, zone.ConnectionId) 
if !found { 
      return false
}

consState, found := k.IBCKeeper.ClientKeeper.GetLatestClientConsensusState(ctx, connection.GetClientID()) 
if !found { 
       return false
}
 		
tmConsState, ok := consState.(*tmtypes.ConsensusState) 
if !ok {
      return false
}

changedValSet := len(zone.IbcNextValidatorsHash) == 0 || !bytes.Equal(zone.IbcNextValidatorsHash, tmConsState.NextValidatorsHash.Bytes())
if !changedValSet {
      return false
}
 
k.Logger(ctx).Info("IBC ValSet has changed; requerying valset") 
// trigger valset update. 
period := int64(k.GetParam(ctx, types.KeyValidatorSetInterval)) 
query := stakingTypes.QueryValidatorsRequest{} 
err := k.EmitValSetQuery(ctx, zone.ConnectionId, zone.ChainId, query, sdkmath.NewInt(period)) 
if err != nil { 
 	k.Logger(ctx).Error("unable to trigger valset update query", "error", err.Error()) 
 	// failing to emit the valset update is not terminal but constitutes 
 	// an error, as if this starts happening frequent it is something 
 	// we should investigate. 
} 

zone.IbcNextValidatorsHash = tmConsState.NextValidatorsHash.Bytes() 
k.SetZone(ctx, zone) 

/cc @elias-orijtech @joe-bowman


For Admin Use

  • Not duplicate issue
  • Appropriate labels applied
  • Appropriate contributors tagged/assigned
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant