Skip to content

Commit 3163a2a

Browse files
authored
Streamline EndBlock (#2540)
## Describe your changes and provide context Flatten call stack of EndBlock ## Testing performed to validate your change unit test
1 parent 32dae87 commit 3163a2a

File tree

26 files changed

+201
-372
lines changed

26 files changed

+201
-372
lines changed

app/abci.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/cosmos/cosmos-sdk/telemetry"
88
sdk "github.com/cosmos/cosmos-sdk/types"
9+
"github.com/cosmos/cosmos-sdk/types/legacytm"
910
"github.com/sei-protocol/sei-chain/app/legacyabci"
1011
"github.com/sei-protocol/sei-chain/utils/metrics"
1112
abci "github.com/tendermint/tendermint/abci/types"
@@ -50,11 +51,19 @@ func (app *App) MidBlock(ctx sdk.Context, height int64) []abci.Event {
5051
return app.BaseApp.MidBlock(ctx, height)
5152
}
5253

53-
func (app *App) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) (res abci.ResponseEndBlock) {
54+
func (app *App) EndBlock(ctx sdk.Context, height int64, blockGasUsed int64) (res abci.ResponseEndBlock) {
5455
spanCtx, span := app.GetBaseApp().TracingInfo.StartWithContext("EndBlock", ctx.TraceSpanContext())
5556
defer span.End()
5657
ctx = ctx.WithTraceSpanContext(spanCtx)
57-
return app.BaseApp.EndBlock(ctx, req)
58+
defer telemetry.MeasureSince(time.Now(), "abci", "end_block")
59+
ctx = ctx.WithEventManager(sdk.NewEventManager())
60+
defer telemetry.MeasureSince(time.Now(), "module", "total_end_block")
61+
res.ValidatorUpdates = legacyabci.EndBlock(ctx, height, blockGasUsed, app.EndBlockKeepers)
62+
res.Events = sdk.MarkEventsToIndex(ctx.EventManager().ABCIEvents(), app.IndexEvents)
63+
if cp := app.GetConsensusParams(ctx); cp != nil {
64+
res.ConsensusParamUpdates = legacytm.ABCIToLegacyConsensusParams(cp)
65+
}
66+
return res
5867
}
5968

6069
func (app *App) CheckTx(ctx context.Context, req *abci.RequestCheckTxV2) (*abci.ResponseCheckTxV2, error) {

app/app.go

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ type App struct {
337337
TokenFactoryKeeper tokenfactorykeeper.Keeper
338338

339339
BeginBlockKeepers legacyabci.BeginBlockKeepers
340+
EndBlockKeepers legacyabci.EndBlockKeepers
340341

341342
// mm is the module manager
342343
mm *module.Manager
@@ -744,37 +745,18 @@ func New(
744745
IBCKeeper: app.IBCKeeper,
745746
EvmKeeper: &app.EvmKeeper,
746747
}
748+
app.EndBlockKeepers = legacyabci.EndBlockKeepers{
749+
CrisisKeeper: &app.CrisisKeeper,
750+
GovKeeper: &app.GovKeeper,
751+
StakingKeeper: &app.StakingKeeper,
752+
OracleKeeper: &app.OracleKeeper,
753+
EvmKeeper: &app.EvmKeeper,
754+
}
747755

748756
app.mm.SetOrderMidBlockers(
749757
oracletypes.ModuleName,
750758
)
751759

752-
app.mm.SetOrderEndBlockers(
753-
crisistypes.ModuleName,
754-
govtypes.ModuleName,
755-
stakingtypes.ModuleName,
756-
capabilitytypes.ModuleName,
757-
authtypes.ModuleName,
758-
banktypes.ModuleName,
759-
distrtypes.ModuleName,
760-
slashingtypes.ModuleName,
761-
minttypes.ModuleName,
762-
genutiltypes.ModuleName,
763-
evidencetypes.ModuleName,
764-
authz.ModuleName,
765-
feegrant.ModuleName,
766-
paramstypes.ModuleName,
767-
upgradetypes.ModuleName,
768-
vestingtypes.ModuleName,
769-
ibchost.ModuleName,
770-
ibctransfertypes.ModuleName,
771-
oracletypes.ModuleName,
772-
epochmoduletypes.ModuleName,
773-
evmtypes.ModuleName,
774-
wasm.ModuleName,
775-
tokenfactorytypes.ModuleName,
776-
)
777-
778760
// NOTE: The genutils module must occur after staking so that pools are
779761
// properly initialized with tokens from genesis accounts.
780762
// NOTE: Capability module must occur first so that it can initialize any capabilities
@@ -861,7 +843,6 @@ func New(
861843

862844
app.SetAnteHandler(anteHandler)
863845
app.SetMidBlocker(app.MidBlocker)
864-
app.SetEndBlocker(app.EndBlocker)
865846
app.SetPrepareProposalHandler(app.PrepareProposalHandler)
866847
app.SetProcessProposalHandler(app.ProcessProposalHandler)
867848
app.SetFinalizeBlocker(app.FinalizeBlocker)
@@ -1037,11 +1018,6 @@ func (app *App) MidBlocker(ctx sdk.Context, height int64) []abci.Event {
10371018
return app.mm.MidBlock(ctx, height)
10381019
}
10391020

1040-
// EndBlocker application updates every end block
1041-
func (app *App) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
1042-
return app.mm.EndBlock(ctx, req)
1043-
}
1044-
10451021
// InitChainer application update at chain initialization
10461022
func (app *App) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci.ResponseInitChain {
10471023
var genesisState GenesisState
@@ -1443,10 +1419,7 @@ func (app *App) ProcessBlock(ctx sdk.Context, txs [][]byte, req BlockProcessRequ
14431419
}
14441420
}
14451421

1446-
endBlockResp = app.EndBlock(ctx, abci.RequestEndBlock{
1447-
Height: req.GetHeight(),
1448-
BlockGasUsed: evmTotalGasUsed,
1449-
})
1422+
endBlockResp = app.EndBlock(ctx, req.GetHeight(), evmTotalGasUsed)
14501423

14511424
events = append(events, endBlockResp.Events...)
14521425
return events, txResults, endBlockResp, nil

app/apptesting/test_suite.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,7 @@ func (s *KeeperTestHelper) SetupTokenFactory() {
120120

121121
// EndBlock ends the block.
122122
func (s *KeeperTestHelper) EndBlock() {
123-
reqEndBlock := abci.RequestEndBlock{Height: s.Ctx.BlockHeight()}
124-
s.App.EndBlocker(s.Ctx, reqEndBlock)
123+
legacyabci.EndBlock(s.Ctx, s.Ctx.BlockHeight(), 0, s.App.EndBlockKeepers)
125124
}
126125

127126
// AllocateRewardsToValidator allocates reward tokens to a distribution module then allocates rewards to the validator address.

app/legacyabci/begin_block.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ func BeginBlock(
5656
slashing.BeginBlocker(ctx, votes, *keepers.SlashingKeeper)
5757
evidence.BeginBlocker(ctx, byzantineValidators, *keepers.EvidenceKeeper)
5858
staking.BeginBlocker(ctx, *keepers.StakingKeeper)
59-
ibcclient.BeginBlocker(ctx, keepers.IBCKeeper.ClientKeeper)
59+
func() {
60+
defer telemetry.ModuleMeasureSince("ibc", time.Now(), telemetry.MetricKeyBeginBlocker)
61+
ibcclient.BeginBlocker(ctx, keepers.IBCKeeper.ClientKeeper)
62+
}()
6063
keepers.EvmKeeper.BeginBlock(ctx)
6164
}

app/legacyabci/end_block.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package legacyabci
2+
3+
import (
4+
sdk "github.com/cosmos/cosmos-sdk/types"
5+
"github.com/cosmos/cosmos-sdk/x/crisis"
6+
crisiskeeper "github.com/cosmos/cosmos-sdk/x/crisis/keeper"
7+
"github.com/cosmos/cosmos-sdk/x/gov"
8+
govkeeper "github.com/cosmos/cosmos-sdk/x/gov/keeper"
9+
"github.com/cosmos/cosmos-sdk/x/staking"
10+
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
11+
evmkeeper "github.com/sei-protocol/sei-chain/x/evm/keeper"
12+
"github.com/sei-protocol/sei-chain/x/oracle"
13+
oraclekeeper "github.com/sei-protocol/sei-chain/x/oracle/keeper"
14+
abci "github.com/tendermint/tendermint/abci/types"
15+
)
16+
17+
type EndBlockKeepers struct {
18+
CrisisKeeper *crisiskeeper.Keeper
19+
GovKeeper *govkeeper.Keeper
20+
StakingKeeper *stakingkeeper.Keeper
21+
OracleKeeper *oraclekeeper.Keeper
22+
EvmKeeper *evmkeeper.Keeper
23+
}
24+
25+
func EndBlock(ctx sdk.Context, height int64, blockGasUsed int64, keepers EndBlockKeepers) []abci.ValidatorUpdate {
26+
crisis.EndBlocker(ctx, *keepers.CrisisKeeper)
27+
gov.EndBlocker(ctx, *keepers.GovKeeper)
28+
vals := staking.EndBlocker(ctx, *keepers.StakingKeeper)
29+
oracle.EndBlocker(ctx, *keepers.OracleKeeper)
30+
keepers.EvmKeeper.EndBlock(ctx, height, blockGasUsed)
31+
return vals
32+
}

app/test_helpers.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ func (s *TestWrapper) BeginBlock() {
205205
}
206206

207207
func (s *TestWrapper) EndBlock() {
208-
reqEndBlock := abci.RequestEndBlock{Height: s.Ctx.BlockHeight()}
209-
s.App.EndBlocker(s.Ctx, reqEndBlock)
208+
legacyabci.EndBlock(s.Ctx, s.Ctx.BlockHeight(), 0, s.App.EndBlockKeepers)
210209
}
211210

212211
func setupReceiptStore() (seidbtypes.StateStore, error) {

sei-cosmos/tests/mocks/types_module_module.go

Lines changed: 0 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sei-cosmos/types/module/module.go

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -258,19 +258,13 @@ func (gam GenesisOnlyAppModule) RegisterServices(Configurator) {}
258258
// ConsensusVersion implements AppModule/ConsensusVersion.
259259
func (gam GenesisOnlyAppModule) ConsensusVersion() uint64 { return 1 }
260260

261-
// EndBlock returns an empty module end-block
262-
func (GenesisOnlyAppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
263-
return []abci.ValidatorUpdate{}
264-
}
265-
266261
// Manager defines a module manager that provides the high level utility for managing and executing
267262
// operations for a group of modules
268263
type Manager struct {
269264
Modules map[string]AppModule
270265
OrderInitGenesis []string
271266
OrderExportGenesis []string
272267
OrderMidBlockers []string
273-
OrderEndBlockers []string
274268
OrderMigrations []string
275269
}
276270

@@ -288,7 +282,6 @@ func NewManager(modules ...AppModule) *Manager {
288282
Modules: moduleMap,
289283
OrderInitGenesis: modulesStr,
290284
OrderExportGenesis: modulesStr,
291-
OrderEndBlockers: modulesStr,
292285
}
293286
}
294287

@@ -309,12 +302,6 @@ func (m *Manager) SetOrderMidBlockers(moduleNames ...string) {
309302
m.OrderMidBlockers = moduleNames
310303
}
311304

312-
// SetOrderEndBlockers sets the order of set end-blocker calls
313-
func (m *Manager) SetOrderEndBlockers(moduleNames ...string) {
314-
m.assertNoForgottenModules("SetOrderEndBlockers", moduleNames)
315-
m.OrderEndBlockers = moduleNames
316-
}
317-
318305
// SetOrderMigrations sets the order of migrations to be run. If not set
319306
// then migrations will be run with an order defined in `DefaultMigrationsOrder`.
320307
func (m *Manager) SetOrderMigrations(moduleNames ...string) {
@@ -604,39 +591,6 @@ func (m *Manager) MidBlock(ctx sdk.Context, height int64) []abci.Event {
604591
return ctx.EventManager().ABCIEvents()
605592
}
606593

607-
// EndBlock performs end block functionality for all modules. It creates a
608-
// child context with an event manager to aggregate events emitted from all
609-
// modules.
610-
func (m *Manager) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
611-
ctx = ctx.WithEventManager(sdk.NewEventManager())
612-
validatorUpdates := []abci.ValidatorUpdate{}
613-
defer telemetry.MeasureSince(time.Now(), "module", "total_end_block")
614-
for _, moduleName := range m.OrderEndBlockers {
615-
module, ok := m.Modules[moduleName].(EndBlockAppModule)
616-
if !ok {
617-
continue
618-
}
619-
moduleStartTime := time.Now()
620-
moduleValUpdates := module.EndBlock(ctx, req)
621-
telemetry.ModuleMeasureSince(moduleName, moduleStartTime, "module", "end_block")
622-
// use these validator updates if provided, the module manager assumes
623-
// only one module will update the validator set
624-
if len(moduleValUpdates) > 0 {
625-
if len(validatorUpdates) > 0 {
626-
panic("validator EndBlock updates already set by a previous module")
627-
}
628-
629-
validatorUpdates = moduleValUpdates
630-
}
631-
632-
}
633-
634-
return abci.ResponseEndBlock{
635-
ValidatorUpdates: validatorUpdates,
636-
Events: ctx.EventManager().ABCIEvents(),
637-
}
638-
}
639-
640594
// GetVersionMap gets consensus version from all modules
641595
func (m *Manager) GetVersionMap() VersionMap {
642596
vermap := make(VersionMap)

sei-cosmos/types/module/module_test.go

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,6 @@ func TestManagerOrderSetters(t *testing.T) {
111111
require.Empty(t, mm.OrderMidBlockers)
112112
mm.SetOrderMidBlockers("module2", "module1")
113113
require.Equal(t, []string{"module2", "module1"}, mm.OrderMidBlockers)
114-
115-
require.Equal(t, []string{"module1", "module2"}, mm.OrderEndBlockers)
116-
mm.SetOrderEndBlockers("module2", "module1")
117-
require.Equal(t, []string{"module2", "module1"}, mm.OrderEndBlockers)
118114
}
119115

120116
func TestManager_RegisterInvariants(t *testing.T) {
@@ -262,28 +258,3 @@ func TestManager_MidBlock(t *testing.T) {
262258
mockAppModule2.EXPECT().MidBlock(gomock.Any(), gomock.Eq(height)).Times(1)
263259
mm.MidBlock(sdk.NewContext(nil, tmproto.Header{}, false, nil), height)
264260
}
265-
266-
func TestManager_EndBlock(t *testing.T) {
267-
mockCtrl := gomock.NewController(t)
268-
t.Cleanup(mockCtrl.Finish)
269-
270-
mockAppModule1 := mocks.NewMockAppModule(mockCtrl)
271-
mockAppModule2 := mocks.NewMockAppModule(mockCtrl)
272-
mockAppModule1.EXPECT().Name().Times(2).Return("module1")
273-
mockAppModule2.EXPECT().Name().Times(2).Return("module2")
274-
mm := module.NewManager(mockAppModule1, mockAppModule2)
275-
require.NotNil(t, mm)
276-
require.Equal(t, 2, len(mm.Modules))
277-
278-
req := abci.RequestEndBlock{Height: 10}
279-
280-
mockAppModule1.EXPECT().EndBlock(gomock.Any(), gomock.Eq(req)).Times(1).Return([]abci.ValidatorUpdate{{}})
281-
mockAppModule2.EXPECT().EndBlock(gomock.Any(), gomock.Eq(req)).Times(1)
282-
ret := mm.EndBlock(sdk.NewContext(nil, tmproto.Header{}, false, nil), req)
283-
require.Equal(t, []abci.ValidatorUpdate{{}}, ret.ValidatorUpdates)
284-
285-
// test panic
286-
mockAppModule1.EXPECT().EndBlock(gomock.Any(), gomock.Eq(req)).Times(1).Return([]abci.ValidatorUpdate{{}})
287-
mockAppModule2.EXPECT().EndBlock(gomock.Any(), gomock.Eq(req)).Times(1).Return([]abci.ValidatorUpdate{{}})
288-
require.Panics(t, func() { mm.EndBlock(sdk.NewContext(nil, tmproto.Header{}, false, nil), req) })
289-
}

sei-cosmos/x/crisis/module.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,3 @@ func (am AppModule) ExportGenesisStream(ctx sdk.Context, cdc codec.JSONCodec) <-
177177

178178
// ConsensusVersion implements AppModule/ConsensusVersion.
179179
func (AppModule) ConsensusVersion() uint64 { return 1 }
180-
181-
// EndBlock returns the end blocker for the crisis module. It returns no validator
182-
// updates.
183-
func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate {
184-
EndBlocker(ctx, *am.keeper)
185-
return []abci.ValidatorUpdate{}
186-
}

0 commit comments

Comments
 (0)