Skip to content

Commit 7e6948f

Browse files
fix(baseapp): nil check in posthandler events (backport #19058) (#19067)
Co-authored-by: Julien Robert <julien@rbrt.fr>
1 parent 3e9a3e9 commit 7e6948f

File tree

4 files changed

+42
-11
lines changed

4 files changed

+42
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
5858

5959
### Bug Fixes
6060

61+
* (baseapp) [#19058](https://github.com/cosmos/cosmos-sdk/pull/19058) Fix baseapp posthandler branch would fail if the `runMsgs` had returned an error.
6162
* (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.
6263
* (baseapp) [#18895](https://github.com/cosmos/cosmos-sdk/pull/18895) Fix de-duplicating vote extensions during validation in ValidateVoteExtensions.
6364

baseapp/baseapp.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -936,11 +936,15 @@ func (app *BaseApp) runTx(mode execMode, txBytes []byte) (gInfo sdk.GasInfo, res
936936
// Note that the state is still preserved.
937937
postCtx := runMsgCtx.WithEventManager(sdk.NewEventManager())
938938

939-
newCtx, err := app.postHandler(postCtx, tx, mode == execModeSimulate, err == nil)
940-
if err != nil {
941-
return gInfo, nil, anteEvents, err
939+
newCtx, errPostHandler := app.postHandler(postCtx, tx, mode == execModeSimulate, err == nil)
940+
if errPostHandler != nil {
941+
return gInfo, nil, anteEvents, errors.Join(err, errPostHandler)
942942
}
943943

944+
// we don't want runTx to panic if runMsgs has failed earlier
945+
if result == nil {
946+
result = &sdk.Result{}
947+
}
944948
result.Events = append(result.Events, newCtx.EventManager().ABCIEvents()...)
945949
}
946950

baseapp/baseapp_test.go

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package baseapp_test
22

33
import (
4+
"bytes"
45
"context"
56
"crypto/sha256"
67
"fmt"
@@ -45,9 +46,10 @@ var (
4546

4647
type (
4748
BaseAppSuite struct {
48-
baseApp *baseapp.BaseApp
49-
cdc *codec.ProtoCodec
50-
txConfig client.TxConfig
49+
baseApp *baseapp.BaseApp
50+
cdc *codec.ProtoCodec
51+
txConfig client.TxConfig
52+
logBuffer *bytes.Buffer
5153
}
5254

5355
SnapshotsConfig struct {
@@ -65,8 +67,10 @@ func NewBaseAppSuite(t *testing.T, opts ...func(*baseapp.BaseApp)) *BaseAppSuite
6567

6668
txConfig := authtx.NewTxConfig(cdc, authtx.DefaultSignModes)
6769
db := dbm.NewMemDB()
70+
logBuffer := new(bytes.Buffer)
71+
logger := log.NewLogger(logBuffer, log.ColorOption(false))
6872

69-
app := baseapp.NewBaseApp(t.Name(), log.NewTestLogger(t), db, txConfig.TxDecoder(), opts...)
73+
app := baseapp.NewBaseApp(t.Name(), logger, db, txConfig.TxDecoder(), opts...)
7074
require.Equal(t, t.Name(), app.Name())
7175

7276
app.SetInterfaceRegistry(cdc.InterfaceRegistry())
@@ -80,9 +84,10 @@ func NewBaseAppSuite(t *testing.T, opts ...func(*baseapp.BaseApp)) *BaseAppSuite
8084
require.Nil(t, app.LoadLatestVersion())
8185

8286
return &BaseAppSuite{
83-
baseApp: app,
84-
cdc: cdc,
85-
txConfig: txConfig,
87+
baseApp: app,
88+
cdc: cdc,
89+
txConfig: txConfig,
90+
logBuffer: logBuffer,
8691
}
8792
}
8893

@@ -631,7 +636,6 @@ func TestBaseAppPostHandler(t *testing.T) {
631636
}
632637

633638
suite := NewBaseAppSuite(t, anteOpt)
634-
635639
baseapptestutil.RegisterCounterServer(suite.baseApp.MsgServiceRouter(), CounterServerImpl{t, capKey1, []byte("foo")})
636640

637641
_, err := suite.baseApp.InitChain(&abci.RequestInitChain{
@@ -666,6 +670,14 @@ func TestBaseAppPostHandler(t *testing.T) {
666670
require.False(t, res.TxResults[0].IsOK(), fmt.Sprintf("%v", res))
667671

668672
require.True(t, postHandlerRun)
673+
674+
// regression test, should not panic when runMsgs fails
675+
tx = wonkyMsg(t, suite.txConfig, tx)
676+
txBytes, err = suite.txConfig.TxEncoder()(tx)
677+
require.NoError(t, err)
678+
_, err = suite.baseApp.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1, Txs: [][]byte{txBytes}})
679+
require.NoError(t, err)
680+
require.NotContains(t, suite.logBuffer.String(), "panic recovered in runTx")
669681
}
670682

671683
// Test and ensure that invalid block heights always cause errors.

baseapp/utils_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,17 @@ func setFailOnHandler(cfg client.TxConfig, tx signing.Tx, fail bool) signing.Tx
390390
builder.SetMsgs(msgs...)
391391
return builder.GetTx()
392392
}
393+
394+
// wonkyMsg is to be used to run a MsgCounter2 message when the MsgCounter2 handler is not registered.
395+
func wonkyMsg(t *testing.T, cfg client.TxConfig, tx signing.Tx) signing.Tx {
396+
t.Helper()
397+
builder := cfg.NewTxBuilder()
398+
builder.SetMemo(tx.GetMemo())
399+
400+
msgs := tx.GetMsgs()
401+
msgs = append(msgs, &baseapptestutil.MsgCounter2{})
402+
403+
err := builder.SetMsgs(msgs...)
404+
require.NoError(t, err)
405+
return builder.GetTx()
406+
}

0 commit comments

Comments
 (0)