Skip to content

Commit

Permalink
refactor(x/gov): reduce "if block" indentation (#18918)
Browse files Browse the repository at this point in the history
  • Loading branch information
lilasxie authored Dec 30, 2023
1 parent 4f1d7f9 commit 14037ae
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 30 deletions.
4 changes: 4 additions & 0 deletions errors/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

### Improvements

* [#18918](https://github.com/cosmos/cosmos-sdk/pull/18918) Improve `IsOf` by returning earlier when the checked error is nil.

## [v1.0.0](https://github.com/cosmos/cosmos-sdk/releases/tag/errors%2Fv1.0.0)

### Features
Expand Down
3 changes: 3 additions & 0 deletions errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ func WithType(err error, obj interface{}) error {
// IsOf checks if a received error is caused by one of the target errors.
// It extends the errors.Is functionality to a list of errors.
func IsOf(received error, targets ...error) bool {
if received == nil {
return false
}
for _, t := range targets {
if errors.Is(received, t) {
return true
Expand Down
2 changes: 2 additions & 0 deletions errors/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ func (s *errorsTestSuite) TestIsOf() {
err := ErrInvalidAddress
errW := Wrap(ErrLogic, "more info")

require.False(IsOf(nil), "nil should always have no causer")
require.False(IsOf(nil, err), "nil should always have no causer")
require.False(IsOf(errNil), "nil error should always have no causer")
require.False(IsOf(errNil, err), "nil error should always have no causer")

Expand Down
5 changes: 1 addition & 4 deletions x/gov/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,7 @@ func EndBlocker(ctx sdk.Context, keeper *keeper.Keeper) error {

return false, nil
})
if err != nil {
return err
}
return nil
return err
}

// executes handle(msg) and recovers from panic.
Expand Down
28 changes: 13 additions & 15 deletions x/gov/keeper/grpc_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,13 @@ func (q queryServer) Proposal(ctx context.Context, req *v1.QueryProposalRequest)
}

proposal, err := q.k.Proposals.Get(ctx, req.ProposalId)
if err != nil {
if errors.IsOf(err, collections.ErrNotFound) {
return nil, status.Errorf(codes.NotFound, "proposal %d doesn't exist", req.ProposalId)
}
return nil, status.Error(codes.Internal, err.Error())
if err == nil {
return &v1.QueryProposalResponse{Proposal: &proposal}, nil
}

return &v1.QueryProposalResponse{Proposal: &proposal}, nil
if errors.IsOf(err, collections.ErrNotFound) {
return nil, status.Errorf(codes.NotFound, "proposal %d doesn't exist", req.ProposalId)
}
return nil, status.Error(codes.Internal, err.Error())
}

// Proposals implements the Query/Proposals gRPC method
Expand Down Expand Up @@ -123,15 +122,14 @@ func (q queryServer) Vote(ctx context.Context, req *v1.QueryVoteRequest) (*v1.Qu
return nil, err
}
vote, err := q.k.Votes.Get(ctx, collections.Join(req.ProposalId, sdk.AccAddress(voter)))
if err != nil {
if errors.IsOf(err, collections.ErrNotFound) {
return nil, status.Errorf(codes.InvalidArgument,
"voter: %v not found for proposal: %v", req.Voter, req.ProposalId)
}
return nil, status.Error(codes.Internal, err.Error())
if err == nil {
return &v1.QueryVoteResponse{Vote: &vote}, nil
}

return &v1.QueryVoteResponse{Vote: &vote}, nil
if errors.IsOf(err, collections.ErrNotFound) {
return nil, status.Errorf(codes.InvalidArgument,
"voter: %v not found for proposal: %v", req.Voter, req.ProposalId)
}
return nil, status.Error(codes.Internal, err.Error())
}

// Votes returns single proposal's votes
Expand Down
22 changes: 11 additions & 11 deletions x/gov/keeper/proposal.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@ func (keeper Keeper) SubmitProposal(ctx context.Context, messages []sdk.Msg, met
if proposalType == v1.ProposalType_PROPOSAL_TYPE_OPTIMISTIC {
proposerStr, _ := keeper.authKeeper.AddressCodec().BytesToString(proposer)

if len(params.OptimisticAuthorizedAddresses) > 0 {
if !slices.Contains(params.OptimisticAuthorizedAddresses, proposerStr) {
return v1.Proposal{}, errorsmod.Wrap(types.ErrInvalidProposer, "proposer is not authorized to submit optimistic proposal")
}
if len(params.OptimisticAuthorizedAddresses) > 0 && !slices.Contains(params.OptimisticAuthorizedAddresses, proposerStr) {
return v1.Proposal{}, errorsmod.Wrap(types.ErrInvalidProposer, "proposer is not authorized to submit optimistic proposal")
}
}

Expand Down Expand Up @@ -74,14 +72,16 @@ func (keeper Keeper) SubmitProposal(ctx context.Context, messages []sdk.Msg, met
// For other Msgs, we do not verify the proposal messages any further.
// They may fail upon execution.
// ref: https://github.com/cosmos/cosmos-sdk/pull/10868#discussion_r784872842
if msg, ok := msg.(*v1.MsgExecLegacyContent); ok {
cacheCtx, _ := sdkCtx.CacheContext()
if _, err := handler(cacheCtx, msg); err != nil {
if errors.Is(types.ErrNoProposalHandlerExists, err) {
return v1.Proposal{}, err
}
return v1.Proposal{}, errorsmod.Wrap(types.ErrInvalidProposalContent, err.Error())
msg, ok := msg.(*v1.MsgExecLegacyContent)
if !ok {
continue
}
cacheCtx, _ := sdkCtx.CacheContext()
if _, err := handler(cacheCtx, msg); err != nil {
if errors.Is(types.ErrNoProposalHandlerExists, err) {
return v1.Proposal{}, err
}
return v1.Proposal{}, errorsmod.Wrap(types.ErrInvalidProposalContent, err.Error())
}

}
Expand Down

0 comments on commit 14037ae

Please sign in to comment.