Skip to content

Commit

Permalink
fix(baseapp): nil check in posthandler events (backport cosmos#19058) (
Browse files Browse the repository at this point in the history
…cosmos#19067)

Co-authored-by: Julien Robert <[email protected]>
  • Loading branch information
mergify[bot] and julienrbrt authored Jan 15, 2024
1 parent 3e9a3e9 commit 7e6948f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### 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 module's beginBlock and before DeliverTx, ensuring transaction processing always starts with the expected zeroed out block gas meter.
* (baseapp) [#18895](https://github.com/cosmos/cosmos-sdk/pull/18895) Fix de-duplicating vote extensions during validation in ValidateVoteExtensions.

Expand Down
10 changes: 7 additions & 3 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -936,11 +936,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{}
}
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 @@ -65,8 +67,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 @@ -80,9 +84,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 @@ -631,7 +636,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 @@ -666,6 +670,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 @@ -390,3 +390,17 @@ func setFailOnHandler(cfg client.TxConfig, tx signing.Tx, fail bool) signing.Tx
builder.SetMsgs(msgs...)
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()
}

0 comments on commit 7e6948f

Please sign in to comment.