Skip to content

Commit

Permalink
feat(gov): handle panics when executing x/gov proposals (#17780)
Browse files Browse the repository at this point in the history
(cherry picked from commit a0b39a1)

# Conflicts:
#	CHANGELOG.md
#	x/gov/abci.go
  • Loading branch information
robert-zaremba authored and mergify[bot] committed Sep 18, 2023
1 parent 55aa507 commit 43e0ebb
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 0 deletions.
25 changes: 25 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,31 @@ Ref: https://keepachangelog.com/en/1.0.0/

## [Unreleased]

<<<<<<< HEAD
=======
### Features

* (baseapp) [#16581](https://github.com/cosmos/cosmos-sdk/pull/16581) Implement Optimistic Execution as an experimental feature (not enabled by default).
* (client/keys) [#17639](https://github.com/cosmos/cosmos-sdk/pull/17639) Allows using and saving public keys encoded as base64
* (client) [#17513](https://github.com/cosmos/cosmos-sdk/pull/17513) Allow overwritting `client.toml`. Use `client.CreateClientConfig` in place of `client.ReadFromClientConfig` and provide a custom template and a custom config.
* (x/bank) [#14224](https://github.com/cosmos/cosmos-sdk/pull/14224) Allow injection of restrictions on transfers using `AppendSendRestriction` or `PrependSendRestriction`.
* (x/bank) [#17569](https://github.com/cosmos/cosmos-sdk/pull/17569) Introduce a new message type, `MsgBurn `, to burn coins.
* (genutil) [#17571](https://github.com/cosmos/cosmos-sdk/pull/17571) Allow creation of `AppGenesis` without a file lookup.
* (server) [#17094](https://github.com/cosmos/cosmos-sdk/pull/17094) Add duration `shutdown-grace` for resource clean up (closing database handles) before exit.
* (x/gov) [#17780](https://github.com/cosmos/cosmos-sdk/pull/17780) Recover panics and turn them into errors when executing x/gov proposals.

### Improvements

* (client) [#17503](https://github.com/cosmos/cosmos-sdk/pull/17503) Add `client.Context{}.WithAddressCodec`, `WithValidatorAddressCodec`, `WithConsensusAddressCodec` to provide address codecs to the client context. See the [UPGRADING.md](./UPGRADING.md) for more details.
* (crypto/keyring) [#17503](https://github.com/cosmos/cosmos-sdk/pull/17503) Simplify keyring interfaces to use `[]byte` instead of `sdk.Address` for addresses.
* (all) [#16537](https://github.com/cosmos/cosmos-sdk/pull/16537) Properly propagated `fmt.Errorf` errors and using `errors.New` where appropriate.
* (rpc) [#17470](https://github.com/cosmos/cosmos-sdk/pull/17470) Avoid open 0.0.0.0 to public by default and add `listen-ip-address` argument for `testnet init-files` cmd.
* (types/module) [#17554](https://github.com/cosmos/cosmos-sdk/pull/17554) Introduce `HasABCIGenesis` which is implemented by a module only when a validatorset update needs to be returned
* (baseapp) [#17667](https://github.com/cosmos/cosmos-sdk/pull/17667) Close databases opened by cosmos-sdk during BaseApp shutdown
* (types) [#17670](https://github.com/cosmos/cosmos-sdk/pull/17670) Use `ctx.CometInfo` in place of `ctx.VoteInfos`
* [#17733](https://github.com/cosmos/cosmos-sdk/pull/17733) Ensure `buf export` exports all proto dependencies

>>>>>>> a0b39a1d3 (feat(gov): handle panics when executing x/gov proposals (#17780))
### Bug Fixes

* (config) [#17649](https://github.com/cosmos/cosmos-sdk/pull/17649) Fix `mempool.max-txs` configuration is invalid in `app.config`.
Expand Down
28 changes: 28 additions & 0 deletions x/gov/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import (
"fmt"
"time"

<<<<<<< HEAD

Check failure on line 7 in x/gov/abci.go

View workflow job for this annotation

GitHub Actions / golangci-lint

expected 'STRING', found '<<' (typecheck)

Check failure on line 7 in x/gov/abci.go

View workflow job for this annotation

GitHub Actions / dependency-review

expected 'STRING', found '<<'

Check failure on line 7 in x/gov/abci.go

View workflow job for this annotation

GitHub Actions / dependency-review

expected 'STRING', found '<<'

Check failure on line 7 in x/gov/abci.go

View workflow job for this annotation

GitHub Actions / dependency-review

expected 'STRING', found '<<'

Check failure on line 7 in x/gov/abci.go

View workflow job for this annotation

GitHub Actions / dependency-review

expected ';', found '<<'
=======
"cosmossdk.io/collections"

"github.com/cosmos/cosmos-sdk/baseapp"
>>>>>>> a0b39a1d3 (feat(gov): handle panics when executing x/gov proposals (#17780))

Check failure on line 12 in x/gov/abci.go

View workflow job for this annotation

GitHub Actions / golangci-lint

illegal character U+0023 '#' (typecheck)

Check failure on line 12 in x/gov/abci.go

View workflow job for this annotation

GitHub Actions / dependency-review

illegal character U+0023 '#'
"github.com/cosmos/cosmos-sdk/telemetry"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/gov/keeper"
Expand Down Expand Up @@ -85,7 +91,17 @@ func EndBlocker(ctx sdk.Context, keeper *keeper.Keeper) {
break
}

<<<<<<< HEAD
events = append(events, res.GetEvents()...)
=======
// execute all messages
for idx, msg = range messages {
handler := keeper.Router().Handler(msg)
var res *sdk.Result
res, err = safeExecuteHandler(cacheCtx, msg, handler)
if err != nil {
break
>>>>>>> a0b39a1d3 (feat(gov): handle panics when executing x/gov proposals (#17780))
}
}

Expand Down Expand Up @@ -136,3 +152,15 @@ func EndBlocker(ctx sdk.Context, keeper *keeper.Keeper) {
return false
})
}

// executes handle(msg) and recovers from panic.
func safeExecuteHandler(ctx sdk.Context, msg sdk.Msg, handler baseapp.MsgServiceHandler,
) (res *sdk.Result, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("handling x/gov proposal msg [%s] PANICKED: %v", msg, r)
}
}()
res, err = handler(ctx, msg)
return
}
32 changes: 32 additions & 0 deletions x/gov/abci_internal_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package gov

import (
"testing"

"github.com/stretchr/testify/require"

sdk "github.com/cosmos/cosmos-sdk/types"
)

func failingHandler(_ sdk.Context, _ sdk.Msg) (*sdk.Result, error) {
panic("test-fail")
}

func okHandler(_ sdk.Context, _ sdk.Msg) (*sdk.Result, error) {
return new(sdk.Result), nil
}

func TestSafeExecuteHandler(t *testing.T) {
t.Parallel()

require := require.New(t)
var ctx sdk.Context

r, err := safeExecuteHandler(ctx, nil, failingHandler)
require.ErrorContains(err, "test-fail")
require.Nil(r)

r, err = safeExecuteHandler(ctx, nil, okHandler)
require.Nil(err)
require.NotNil(r)
}

0 comments on commit 43e0ebb

Please sign in to comment.