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

refactor(x/gov): reduce "if block" indentation #18918

Merged
merged 3 commits into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
lilasxie marked this conversation as resolved.
Show resolved Hide resolved
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())
lilasxie marked this conversation as resolved.
Show resolved Hide resolved
}

// 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
Loading