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

fix(baseapp): nil check in posthandler events #19058

Merged
merged 2 commits into from
Jan 15, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Every Module contains its own CHANGELOG.md. Please refer to the module you are i

### Bug Fixes

* (baseapp) [#19058](https://github.com/cosmos/cosmos-sdk/pull/19058) Fix baseapp posthandler branch would fail if the `runMsgs` had returned an error.
* (baseapp) [#18609](https://github.com/cosmos/cosmos-sdk/issues/18609) Fixed accounting in the block gas meter after BeginBlock and before DeliverTx, ensuring transaction processing always starts with the expected zeroed out block gas meter.
* (baseapp) [#18727](https://github.com/cosmos/cosmos-sdk/pull/18727) Ensure that `BaseApp.Init` firstly returns any errors from a nil commit multistore instead of panicking on nil dereferencing and before sealing the app.
* (client) [#18622](https://github.com/cosmos/cosmos-sdk/pull/18622) Fixed a potential under/overflow from `uint64->int64` when computing gas fees as a LegacyDec.
Expand Down
10 changes: 7 additions & 3 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -944,11 +944,15 @@ func (app *BaseApp) runTx(mode execMode, txBytes []byte) (gInfo sdk.GasInfo, res
// Note that the state is still preserved.
postCtx := runMsgCtx.WithEventManager(sdk.NewEventManager())

newCtx, err := app.postHandler(postCtx, tx, mode == execModeSimulate, err == nil)
if err != nil {
return gInfo, nil, anteEvents, err
newCtx, errPostHandler := app.postHandler(postCtx, tx, mode == execModeSimulate, err == nil)
if errPostHandler != nil {
return gInfo, nil, anteEvents, errors.Join(err, errPostHandler)
}

// we don't want runTx to panic if runMsgs has failed earlier
if result == nil {
result = &sdk.Result{}
}
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved
result.Events = append(result.Events, newCtx.EventManager().ABCIEvents()...)
}

Expand Down
28 changes: 20 additions & 8 deletions baseapp/baseapp_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package baseapp_test

import (
"bytes"
"context"
"crypto/sha256"
"fmt"
Expand Down Expand Up @@ -45,9 +46,10 @@ var (

type (
BaseAppSuite struct {
baseApp *baseapp.BaseApp
cdc *codec.ProtoCodec
txConfig client.TxConfig
baseApp *baseapp.BaseApp
cdc *codec.ProtoCodec
txConfig client.TxConfig
logBuffer *bytes.Buffer
}

SnapshotsConfig struct {
Expand All @@ -66,8 +68,10 @@ func NewBaseAppSuite(t *testing.T, opts ...func(*baseapp.BaseApp)) *BaseAppSuite

txConfig := authtx.NewTxConfig(cdc, authtx.DefaultSignModes)
db := dbm.NewMemDB()
logBuffer := new(bytes.Buffer)
logger := log.NewLogger(logBuffer, log.ColorOption(false))

app := baseapp.NewBaseApp(t.Name(), log.NewTestLogger(t), db, txConfig.TxDecoder(), opts...)
app := baseapp.NewBaseApp(t.Name(), logger, db, txConfig.TxDecoder(), opts...)
require.Equal(t, t.Name(), app.Name())

app.SetInterfaceRegistry(cdc.InterfaceRegistry())
Expand All @@ -81,9 +85,10 @@ func NewBaseAppSuite(t *testing.T, opts ...func(*baseapp.BaseApp)) *BaseAppSuite
require.Nil(t, app.LoadLatestVersion())

return &BaseAppSuite{
baseApp: app,
cdc: cdc,
txConfig: txConfig,
baseApp: app,
cdc: cdc,
txConfig: txConfig,
logBuffer: logBuffer,
}
}

Expand Down Expand Up @@ -638,7 +643,6 @@ func TestBaseAppPostHandler(t *testing.T) {
}

suite := NewBaseAppSuite(t, anteOpt)

baseapptestutil.RegisterCounterServer(suite.baseApp.MsgServiceRouter(), CounterServerImpl{t, capKey1, []byte("foo")})

_, err := suite.baseApp.InitChain(&abci.RequestInitChain{
Expand Down Expand Up @@ -673,6 +677,14 @@ func TestBaseAppPostHandler(t *testing.T) {
require.False(t, res.TxResults[0].IsOK(), fmt.Sprintf("%v", res))

require.True(t, postHandlerRun)

// regression test, should not panic when runMsgs fails
tx = wonkyMsg(t, suite.txConfig, tx)
txBytes, err = suite.txConfig.TxEncoder()(tx)
require.NoError(t, err)
_, err = suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1, Txs: [][]byte{txBytes}})
require.NoError(t, err)
require.NotContains(t, suite.logBuffer.String(), "panic recovered in runTx")
}

// Test and ensure that invalid block heights always cause errors.
Expand Down
14 changes: 14 additions & 0 deletions baseapp/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,3 +400,17 @@ func setFailOnHandler(t *testing.T, cfg client.TxConfig, tx signing.Tx, fail boo
require.NoError(t, err)
return builder.GetTx()
}

// wonkyMsg is to be used to run a MsgCounter2 message when the MsgCounter2 handler is not registered.
func wonkyMsg(t *testing.T, cfg client.TxConfig, tx signing.Tx) signing.Tx {
t.Helper()
builder := cfg.NewTxBuilder()
builder.SetMemo(tx.GetMemo())

msgs := tx.GetMsgs()
msgs = append(msgs, &baseapptestutil.MsgCounter2{})

err := builder.SetMsgs(msgs...)
require.NoError(t, err)
return builder.GetTx()
}
Loading