Skip to content

Commit 3f6796f

Browse files
mergify[bot]facundomedicatac0turtle
authored
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 <marbar3778@yahoo.com> Co-authored-by: Facundo <facundomedica@gmail.com>
1 parent 3fc8074 commit 3f6796f

4 files changed

Lines changed: 17 additions & 9 deletions

File tree

CHANGELOG.md

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

4141
## Bug Fixes
4242

43+
* (baseapp) [#21159](https://github.com/cosmos/cosmos-sdk/pull/21159) Return PreBlocker events in FinalizeBlockResponse.
4344
* [#20939](https://github.com/cosmos/cosmos-sdk/pull/20939) Fix collection reverse iterator to include `pagination.key` in the result.
4445
* (client/grpc) [#20969](https://github.com/cosmos/cosmos-sdk/pull/20969) Fix `node.NewQueryServer` method not setting `cfg`.
4546
* (testutil/integration) [#21006](https://github.com/cosmos/cosmos-sdk/pull/21006) Fix `NewIntegrationApp` method not writing default genesis to state

baseapp/abci.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -753,10 +753,13 @@ func (app *BaseApp) internalFinalizeBlock(ctx context.Context, req *abci.Request
753753
WithHeaderHash(req.Hash))
754754
}
755755

756-
if err := app.preBlock(req); err != nil {
756+
preblockEvents, err := app.preBlock(req)
757+
if err != nil {
757758
return nil, err
758759
}
759760

761+
events = append(events, preblockEvents...)
762+
760763
beginBlock, err := app.beginBlock(req)
761764
if err != nil {
762765
return nil, err

baseapp/abci_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2029,22 +2029,24 @@ func TestBaseApp_PreBlocker(t *testing.T) {
20292029
wasHookCalled := false
20302030
app.SetPreBlocker(func(ctx sdk.Context, req *abci.RequestFinalizeBlock) (*sdk.ResponsePreBlock, error) {
20312031
wasHookCalled = true
2032-
return &sdk.ResponsePreBlock{
2033-
ConsensusParamsChanged: true,
2034-
}, nil
2032+
2033+
ctx.EventManager().EmitEvent(sdk.NewEvent("preblockertest", sdk.NewAttribute("height", fmt.Sprintf("%d", req.Height))))
2034+
return &sdk.ResponsePreBlock{ConsensusParamsChanged: false}, nil
20352035
})
20362036
app.Seal()
20372037

2038-
_, err = app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1})
2038+
res, err := app.FinalizeBlock(&abci.RequestFinalizeBlock{Height: 1})
20392039
require.NoError(t, err)
20402040
require.Equal(t, true, wasHookCalled)
2041+
require.Len(t, res.Events, 1)
2042+
require.Equal(t, "preblockertest", res.Events[0].Type)
20412043

20422044
// Now try erroring
20432045
app = baseapp.NewBaseApp(name, logger, db, nil)
20442046
_, err = app.InitChain(&abci.RequestInitChain{})
20452047
require.NoError(t, err)
20462048

2047-
app.SetPreBlocker(func(ctx sdk.Context, req *abci.RequestFinalizeBlock) (*sdk.ResponsePreBlock, error) {
2049+
app.SetPreBlocker(func(_ sdk.Context, req *abci.RequestFinalizeBlock) (*sdk.ResponsePreBlock, error) {
20482050
return nil, errors.New("some error")
20492051
})
20502052
app.Seal()

baseapp/baseapp.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -704,12 +704,13 @@ func (app *BaseApp) cacheTxContext(ctx sdk.Context, txBytes []byte) (sdk.Context
704704
return ctx.WithMultiStore(msCache), msCache
705705
}
706706

707-
func (app *BaseApp) preBlock(req *abci.RequestFinalizeBlock) error {
707+
func (app *BaseApp) preBlock(req *abci.RequestFinalizeBlock) ([]abci.Event, error) {
708+
var events []abci.Event
708709
if app.preBlocker != nil {
709710
ctx := app.finalizeBlockState.Context()
710711
rsp, err := app.preBlocker(ctx, req)
711712
if err != nil {
712-
return err
713+
return nil, err
713714
}
714715
// rsp.ConsensusParamsChanged is true from preBlocker means ConsensusParams in store get changed
715716
// write the consensus parameters in store to context
@@ -720,8 +721,9 @@ func (app *BaseApp) preBlock(req *abci.RequestFinalizeBlock) error {
720721
ctx = ctx.WithBlockGasMeter(gasMeter)
721722
app.finalizeBlockState.SetContext(ctx)
722723
}
724+
events = ctx.EventManager().ABCIEvents()
723725
}
724-
return nil
726+
return events, nil
725727
}
726728

727729
func (app *BaseApp) beginBlock(_ *abci.RequestFinalizeBlock) (sdk.BeginBlock, error) {

0 commit comments

Comments
 (0)