From 3f6796fba413cca7f2ea9a7e03d7965e7eda7378 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Tue, 6 Aug 2024 09:36:14 +0000 Subject: [PATCH] fix(baseapp): return events from preblocker in FinalizeBlockResponse (backport #21159) (#21162) Co-authored-by: Facundo Medica <14063057+facundomedica@users.noreply.github.com> Co-authored-by: marbar3778 Co-authored-by: Facundo --- CHANGELOG.md | 1 + baseapp/abci.go | 5 ++++- baseapp/abci_test.go | 12 +++++++----- baseapp/baseapp.go | 8 +++++--- 4 files changed, 17 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e48783a88e8b..054d327fe8a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -40,6 +40,7 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## Bug Fixes +* (baseapp) [#21159](https://github.com/cosmos/cosmos-sdk/pull/21159) Return PreBlocker events in FinalizeBlockResponse. * [#20939](https://github.com/cosmos/cosmos-sdk/pull/20939) Fix collection reverse iterator to include `pagination.key` in the result. * (client/grpc) [#20969](https://github.com/cosmos/cosmos-sdk/pull/20969) Fix `node.NewQueryServer` method not setting `cfg`. * (testutil/integration) [#21006](https://github.com/cosmos/cosmos-sdk/pull/21006) Fix `NewIntegrationApp` method not writing default genesis to state diff --git a/baseapp/abci.go b/baseapp/abci.go index 2bef4642ba41..edfab76ef0c3 100644 --- a/baseapp/abci.go +++ b/baseapp/abci.go @@ -753,10 +753,13 @@ func (app *BaseApp) internalFinalizeBlock(ctx context.Context, req *abci.Request WithHeaderHash(req.Hash)) } - if err := app.preBlock(req); err != nil { + preblockEvents, err := app.preBlock(req) + if err != nil { return nil, err } + events = append(events, preblockEvents...) + beginBlock, err := app.beginBlock(req) if err != nil { return nil, err diff --git a/baseapp/abci_test.go b/baseapp/abci_test.go index 2c250b03ddf9..3c67385dfa67 100644 --- a/baseapp/abci_test.go +++ b/baseapp/abci_test.go @@ -2029,22 +2029,24 @@ func TestBaseApp_PreBlocker(t *testing.T) { wasHookCalled := false app.SetPreBlocker(func(ctx sdk.Context, req *abci.RequestFinalizeBlock) (*sdk.ResponsePreBlock, error) { wasHookCalled = true - return &sdk.ResponsePreBlock{ - ConsensusParamsChanged: true, - }, nil + + ctx.EventManager().EmitEvent(sdk.NewEvent("preblockertest", sdk.NewAttribute("height", fmt.Sprintf("%d", req.Height)))) + return &sdk.ResponsePreBlock{ConsensusParamsChanged: false}, nil }) app.Seal() - _, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1}) + res, err := app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1}) require.NoError(t, err) require.Equal(t, true, wasHookCalled) + require.Len(t, res.Events, 1) + require.Equal(t, "preblockertest", res.Events[0].Type) // Now try erroring app = baseapp.NewBaseApp(name, logger, db, nil) _, err = app.InitChain(&abci.RequestInitChain{}) require.NoError(t, err) - app.SetPreBlocker(func(ctx sdk.Context, req *abci.RequestFinalizeBlock) (*sdk.ResponsePreBlock, error) { + app.SetPreBlocker(func(_ sdk.Context, req *abci.RequestFinalizeBlock) (*sdk.ResponsePreBlock, error) { return nil, errors.New("some error") }) app.Seal() diff --git a/baseapp/baseapp.go b/baseapp/baseapp.go index a89b48bcbafc..a89620485087 100644 --- a/baseapp/baseapp.go +++ b/baseapp/baseapp.go @@ -704,12 +704,13 @@ func (app *BaseApp) cacheTxContext(ctx sdk.Context, txBytes []byte) (sdk.Context return ctx.WithMultiStore(msCache), msCache } -func (app *BaseApp) preBlock(req *abci.RequestFinalizeBlock) error { +func (app *BaseApp) preBlock(req *abci.RequestFinalizeBlock) ([]abci.Event, error) { + var events []abci.Event if app.preBlocker != nil { ctx := app.finalizeBlockState.Context() rsp, err := app.preBlocker(ctx, req) if err != nil { - return err + return nil, err } // rsp.ConsensusParamsChanged is true from preBlocker means ConsensusParams in store get changed // write the consensus parameters in store to context @@ -720,8 +721,9 @@ func (app *BaseApp) preBlock(req *abci.RequestFinalizeBlock) error { ctx = ctx.WithBlockGasMeter(gasMeter) app.finalizeBlockState.SetContext(ctx) } + events = ctx.EventManager().ABCIEvents() } - return nil + return events, nil } func (app *BaseApp) beginBlock(_ *abci.RequestFinalizeBlock) (sdk.BeginBlock, error) {