Skip to content

Commit

Permalink
feat!: add MidBlock interface on ABCI, bump tendermint to MidBlock ap…
Browse files Browse the repository at this point in the history
…plied

(cherry picked from commit c86299b)
  • Loading branch information
crypin committed Jun 20, 2023
1 parent d5670a7 commit 44cb0ec
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 6 deletions.
7 changes: 7 additions & 0 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ func (app *BaseApp) BeginBlock(req abci.RequestBeginBlock) (res abci.ResponseBeg
return res
}

func (app *BaseApp) MidBlock(req abci.RequestMidBlock) (res abci.ResponseMidBlock) {
if app.midBlocker != nil {
return app.midBlocker(app.deliverState.ctx, req)
}
return abci.ResponseMidBlock{}
}

// EndBlock implements the ABCI interface.
func (app *BaseApp) EndBlock(req abci.RequestEndBlock) (res abci.ResponseEndBlock) {
defer telemetry.MeasureSince(time.Now(), "abci", "end_block")
Expand Down
7 changes: 4 additions & 3 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ type BaseApp struct { //nolint: maligned
logger log.Logger
name string // application name from abci.Info
interfaceRegistry types.InterfaceRegistry
txDecoder sdk.TxDecoder // unmarshal []byte into sdk.Tx
TxDecoder sdk.TxDecoder // unmarshal []byte into sdk.Tx

anteHandler sdk.AnteHandler // ante handler for fee and auth

Expand Down Expand Up @@ -134,6 +134,7 @@ type moduleRouter struct {
type abciData struct {
initChainer sdk.InitChainer // initialize state with validators and state blob
beginBlocker sdk.BeginBlocker // logic to run before any txs
midBlocker sdk.MidBlocker // logic to run before any txs after begin block
endBlocker sdk.EndBlocker // logic to run after all txs, and to determine valset changes

// absent validators from begin block
Expand Down Expand Up @@ -181,7 +182,7 @@ func NewBaseApp(
grpcQueryRouter: NewGRPCQueryRouter(),
msgServiceRouter: NewMsgServiceRouter(),
},
txDecoder: txDecoder,
TxDecoder: txDecoder,
}

for _, option := range options {
Expand Down Expand Up @@ -656,7 +657,7 @@ func (app *BaseApp) runTx(mode runTxMode, txBytes []byte) (gInfo sdk.GasInfo, re
defer consumeBlockGas()
}

tx, err := app.txDecoder(txBytes)
tx, err := app.TxDecoder(txBytes)
if err != nil {
return sdk.GasInfo{}, nil, nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion baseapp/deliver_tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1501,7 +1501,7 @@ func TestTxDecoder(t *testing.T) {
tx := newTxCounter(1, 0)
txBytes := codec.MustMarshal(tx)

dTx, err := app.txDecoder(txBytes)
dTx, err := app.TxDecoder(txBytes)
require.NoError(t, err)

cTx := dTx.(txTest)
Expand Down
8 changes: 8 additions & 0 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,14 @@ func (app *BaseApp) SetBeginBlocker(beginBlocker sdk.BeginBlocker) {
app.beginBlocker = beginBlocker
}

func (app *BaseApp) SetMidBlocker(midBlocker sdk.MidBlocker) {
if app.sealed {
panic("SetMidBlocker() on sealed BaseApp")
}

app.midBlocker = midBlocker
}

func (app *BaseApp) SetEndBlocker(endBlocker sdk.EndBlocker) {
if app.sealed {
panic("SetEndBlocker() on sealed BaseApp")
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ replace (

github.com/jhump/protoreflect => github.com/jhump/protoreflect v1.9.0

github.com/tendermint/tendermint => github.com/crescent-network/tendermint v0.34.22-mid-block-1

// latest grpc doesn't work with with our modified proto compiler, so we need to enforce
// the following version across all dependencies.
google.golang.org/grpc => google.golang.org/grpc v1.33.2
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46t
github.com/creachadair/taskgroup v0.3.2 h1:zlfutDS+5XG40AOxcHDSThxKzns8Tnr9jnr6VqkYlkM=
github.com/creachadair/taskgroup v0.3.2/go.mod h1:wieWwecHVzsidg2CsUnFinW1faVN4+kq+TDlRJQ0Wbk=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/crescent-network/tendermint v0.34.22-mid-block-1 h1:u3u6hcEEZxoMQTviFrmNfmkmr2jStlhEuy2EeNJcIg4=
github.com/crescent-network/tendermint v0.34.22-mid-block-1/go.mod h1:YpP5vBEAKUT4g6oyfjKgFeZmdB/GjkJAxfF+cgmJg6Y=
github.com/danieljoos/wincred v1.0.2 h1:zf4bhty2iLuwgjgpraD2E9UbvO+fe54XXGJbOwe23fU=
github.com/danieljoos/wincred v1.0.2/go.mod h1:SnuYRW9lp1oJrZX/dXJqr0cPK5gYXqx3EJbmjhLdK9U=
github.com/davecgh/go-spew v0.0.0-20171005155431-ecdeabc65495/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -749,8 +751,6 @@ github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15 h1:hqAk8riJvK4RM
github.com/tendermint/crypto v0.0.0-20191022145703-50d29ede1e15/go.mod h1:z4YtwM70uOnk8h0pjJYlj3zdYwi9l03By6iAIF5j/Pk=
github.com/tendermint/go-amino v0.16.0 h1:GyhmgQKvqF82e2oZeuMSp9JTN0N09emoSZlb2lyGa2E=
github.com/tendermint/go-amino v0.16.0/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME=
github.com/tendermint/tendermint v0.34.22 h1:XMhtC8s8QqJO4l/dn+TkQvevTRSow3Vixjclr41o+2Q=
github.com/tendermint/tendermint v0.34.22/go.mod h1:YpP5vBEAKUT4g6oyfjKgFeZmdB/GjkJAxfF+cgmJg6Y=
github.com/tendermint/tm-db v0.6.6 h1:EzhaOfR0bdKyATqcd5PNeyeq8r+V4bRPHBfyFdD9kGM=
github.com/tendermint/tm-db v0.6.6/go.mod h1:wP8d49A85B7/erz/r4YbKssKw6ylsO/hKtFk7E1aWZI=
github.com/tidwall/gjson v1.6.7/go.mod h1:zeFuBCIqD4sN/gmqBzZ4j7Jd6UcA2Fc56x7QFsv+8fI=
Expand Down
7 changes: 7 additions & 0 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ func NewSimApp(
authz.ModuleName, feegrant.ModuleName,
paramstypes.ModuleName, vestingtypes.ModuleName,
)
app.mm.SetOrderMidBlockers()
app.mm.SetOrderEndBlockers(
crisistypes.ModuleName, govtypes.ModuleName, stakingtypes.ModuleName,
capabilitytypes.ModuleName, authtypes.ModuleName, banktypes.ModuleName, distrtypes.ModuleName,
Expand Down Expand Up @@ -395,6 +396,7 @@ func NewSimApp(
// initialize BaseApp
app.SetInitChainer(app.InitChainer)
app.SetBeginBlocker(app.BeginBlocker)
app.SetMidBlocker(app.MidBlocker)

anteHandler, err := ante.NewAnteHandler(
ante.HandlerOptions{
Expand Down Expand Up @@ -429,6 +431,11 @@ func (app *SimApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abc
return app.mm.BeginBlock(ctx, req)
}

func (app *SimApp) MidBlocker(ctx sdk.Context, req abci.RequestMidBlock) abci.ResponseMidBlock {
events := app.mm.MidBlock(ctx)
return abci.ResponseMidBlock{Events: events}
}

// EndBlocker application updates every end block
func (app *SimApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
return app.mm.EndBlock(ctx, req)
Expand Down
2 changes: 2 additions & 0 deletions simapp/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ type App interface {
// Application updates every begin block.
BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock

MidBlocker(ctx sdk.Context, req abci.RequestMidBlock) abci.ResponseMidBlock

// Application updates every end block.
EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock

Expand Down
2 changes: 2 additions & 0 deletions types/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ type InitChainer func(ctx Context, req abci.RequestInitChain) abci.ResponseInitC
// e.g. BFT timestamps rather than block height for any periodic BeginBlock logic
type BeginBlocker func(ctx Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock

type MidBlocker func(ctx Context, req abci.RequestMidBlock) abci.ResponseMidBlock

// EndBlocker runs code after the transactions in a block and return updates to the validator set
//
// Note: applications which set create_empty_blocks=false will not have regular block timing and should use
Expand Down
29 changes: 29 additions & 0 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,12 @@ type BeginBlockAppModule interface {
BeginBlock(sdk.Context, abci.RequestBeginBlock)
}

// MidBlockAppModule is an extension interface that contains information about the AppModule and BeginBlock.
type MidBlockAppModule interface {
AppModule
MidBlock(sdk.Context)
}

// EndBlockAppModule is an extension interface that contains information about the AppModule and EndBlock.
type EndBlockAppModule interface {
AppModule
Expand Down Expand Up @@ -237,6 +243,7 @@ type Manager struct {
OrderInitGenesis []string
OrderExportGenesis []string
OrderBeginBlockers []string
OrderMidBlockers []string
OrderEndBlockers []string
OrderMigrations []string
}
Expand All @@ -255,6 +262,7 @@ func NewManager(modules ...AppModule) *Manager {
OrderInitGenesis: modulesStr,
OrderExportGenesis: modulesStr,
OrderBeginBlockers: modulesStr,
OrderMidBlockers: modulesStr,
OrderEndBlockers: modulesStr,
}
}
Expand All @@ -277,6 +285,11 @@ func (m *Manager) SetOrderBeginBlockers(moduleNames ...string) {
m.OrderBeginBlockers = moduleNames
}

// SetOrderMidBlockers sets the order of set mid-blocker calls
func (m *Manager) SetOrderMidBlockers(moduleNames ...string) {
m.OrderMidBlockers = moduleNames
}

// SetOrderEndBlockers sets the order of set end-blocker calls
func (m *Manager) SetOrderEndBlockers(moduleNames ...string) {
m.assertNoForgottenModules("SetOrderEndBlockers", moduleNames)
Expand Down Expand Up @@ -497,6 +510,22 @@ func (m *Manager) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) abci.R
}
}

// MidBlock performs mid block functionality for all modules. It creates a
// child context with an event manager to aggregate events emitted from all
// modules.
func (m *Manager) MidBlock(ctx sdk.Context) []abci.Event {
ctx = ctx.WithEventManager(sdk.NewEventManager())

for _, moduleName := range m.OrderMidBlockers {
module, ok := m.Modules[moduleName].(MidBlockAppModule)
if ok {
module.MidBlock(ctx)
}
}

return ctx.EventManager().ABCIEvents()
}

// EndBlock performs end block functionality for all modules. It creates a
// child context with an event manager to aggregate events emitted from all
// modules.
Expand Down
6 changes: 6 additions & 0 deletions types/module/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,12 @@ func TestManagerOrderSetters(t *testing.T) {
require.Equal(t, []string{"module1", "module2"}, mm.OrderEndBlockers)
mm.SetOrderEndBlockers("module2", "module1")
require.Equal(t, []string{"module2", "module1"}, mm.OrderEndBlockers)

require.Equal(t, []string{"module1", "module2"}, mm.OrderMidBlockers)
mm.SetOrderMidBlockers("module2", "module1")
require.Equal(t, []string{"module2", "module1"}, mm.OrderMidBlockers)
mm.SetOrderMidBlockers("module1")
require.Equal(t, []string{"module1"}, mm.OrderMidBlockers)
}

func TestManager_RegisterInvariants(t *testing.T) {
Expand Down

0 comments on commit 44cb0ec

Please sign in to comment.