Skip to content

Commit

Permalink
Add Commit callback
Browse files Browse the repository at this point in the history
  • Loading branch information
prettymuchbryce committed Mar 22, 2023
1 parent d53b29b commit cda4b81
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 0 deletions.
4 changes: 4 additions & 0 deletions baseapp/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,10 @@ func (app *BaseApp) Commit() abci.ResponseCommit {
// empty/reset the deliver state
app.deliverState = nil

if app.commiter != nil {
app.commiter(app.checkState.ctx)
}

var halt bool

switch {
Expand Down
1 change: 1 addition & 0 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type BaseApp struct { //nolint: maligned
processProposal sdk.ProcessProposalHandler // the handler which runs on ABCI ProcessProposal
prepareProposal sdk.PrepareProposalHandler // the handler which runs on ABCI PrepareProposal
endBlocker sdk.EndBlocker // logic to run after all txs, and to determine valset changes
commiter sdk.Commiter // logic to run during commit
addrPeerFilter sdk.PeerFilter // filter peers by address and port
idPeerFilter sdk.PeerFilter // filter peers by node ID
fauxMerkleMode bool // if true, IAVL MountStores uses MountStoresDB for simulation speed.
Expand Down
8 changes: 8 additions & 0 deletions baseapp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,14 @@ func (app *BaseApp) SetEndBlocker(endBlocker sdk.EndBlocker) {
app.endBlocker = endBlocker
}

func (app *BaseApp) SetCommiter(commiter sdk.Commiter) {
if app.sealed {
panic("SetCommiter() on sealed BaseApp")
}

app.commiter = commiter
}

func (app *BaseApp) SetAnteHandler(ah sdk.AnteHandler) {
if app.sealed {
panic("SetAnteHandler() on sealed BaseApp")
Expand Down
1 change: 1 addition & 0 deletions telemetry/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
const (
MetricKeyBeginBlocker = "begin_blocker"
MetricKeyEndBlocker = "end_blocker"
MetricKeyCommiter = "commiter"
MetricLabelNameModule = "module"
)

Expand Down
4 changes: 4 additions & 0 deletions types/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ type BeginBlocker func(ctx Context, req abci.RequestBeginBlock) (abci.ResponseBe
// e.g. BFT timestamps rather than block height for any periodic EndBlock logic
type EndBlocker func(ctx Context, req abci.RequestEndBlock) (abci.ResponseEndBlock, error)

// Commiter runs code during commit after the block has been committed, and the `checkState` has been
// branched for the new block.
type Commiter func(ctx Context)

// PeerFilter responds to p2p filtering queries from Tendermint
type PeerFilter func(info string) abci.ResponseQuery

Expand Down
24 changes: 24 additions & 0 deletions types/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,12 @@ type EndBlockAppModule interface {
EndBlock(sdk.Context, abci.RequestEndBlock) []abci.ValidatorUpdate
}

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

// GenesisOnlyAppModule is an AppModule that only has import/export functionality
type GenesisOnlyAppModule struct {
AppModuleGenesis
Expand Down Expand Up @@ -258,6 +264,7 @@ type Manager struct {
OrderExportGenesis []string
OrderBeginBlockers []string
OrderEndBlockers []string
OrderCommiters []string
OrderMigrations []string
}

Expand All @@ -276,6 +283,7 @@ func NewManager(modules ...AppModule) *Manager {
OrderExportGenesis: modulesStr,
OrderBeginBlockers: modulesStr,
OrderEndBlockers: modulesStr,
OrderCommiters: modulesStr,
}
}

Expand Down Expand Up @@ -351,6 +359,11 @@ func (m *Manager) SetOrderEndBlockers(moduleNames ...string) {
m.OrderEndBlockers = moduleNames
}

// SetOrderCommiters sets the order of set commiter calls
func (m *Manager) SetOrderCommiters(moduleNames ...string) {
m.OrderCommiters = moduleNames
}

// SetOrderMigrations sets the order of migrations to be run. If not set
// then migrations will be run with an order defined in `DefaultMigrationsOrder`.
func (m *Manager) SetOrderMigrations(moduleNames ...string) {
Expand Down Expand Up @@ -706,6 +719,17 @@ func (m *Manager) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) (abci.Resp
}, nil
}

// Commit performs commit functionality for all modules.
func (m *Manager) Commit(ctx sdk.Context) {
for _, moduleName := range m.OrderCommiters {
module, ok := m.Modules[moduleName].(CommitAppModule)
if !ok {
continue
}
module.Commit(ctx)
}
}

// GetVersionMap gets consensus version from all modules
func (m *Manager) GetVersionMap() VersionMap {
vermap := make(VersionMap)
Expand Down

0 comments on commit cda4b81

Please sign in to comment.