Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ type BandApp struct {
DeliverContext sdk.Context

// List of hooks
hooks []Hook
hooks Hooks
}

func init() {
Expand Down Expand Up @@ -472,27 +472,24 @@ func (app *BandApp) Name() string { return app.BaseApp.Name() }
func (app *BandApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
app.DeliverContext = ctx
res := app.mm.BeginBlock(ctx, req)
for _, hook := range app.hooks {
hook.AfterBeginBlock(ctx, req, res)
}
app.hooks.AfterBeginBlock(ctx, req, res)

return res
}

// EndBlocker application updates every end block.
func (app *BandApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
res := app.mm.EndBlock(ctx, req)
for _, hook := range app.hooks {
hook.AfterEndBlock(ctx, req, res)
}
app.hooks.AfterEndBlock(ctx, req, res)

return res
}

// Commit overrides the default BaseApp's ABCI commit by adding DeliverContext clearing.
func (app *BandApp) Commit() (res abci.ResponseCommit) {
for _, hook := range app.hooks {
hook.BeforeCommit()
}
app.hooks.BeforeCommit()
app.DeliverContext = sdk.Context{}

return app.BaseApp.Commit()
}

Expand All @@ -503,18 +500,16 @@ func (app *BandApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) abci
panic(err)
}
res := app.mm.InitGenesis(ctx, app.appCodec, genesisState)
for _, hook := range app.hooks {
hook.AfterInitChain(ctx, req, res)
}
app.hooks.AfterInitChain(ctx, req, res)

return res
}

// DeliverTx overwrite DeliverTx to apply afterDeliverTx hook
func (app *BandApp) DeliverTx(req abci.RequestDeliverTx) abci.ResponseDeliverTx {
res := app.BaseApp.DeliverTx(req)
for _, hook := range app.hooks {
hook.AfterDeliverTx(app.DeliverContext, req, res)
}
app.hooks.AfterDeliverTx(app.DeliverContext, req, res)

return res
}

Expand All @@ -526,12 +521,11 @@ func (app *BandApp) Query(req abci.RequestQuery) abci.ResponseQuery {
hookReq.Height = app.LastBlockHeight()
}

for _, hook := range app.hooks {
res, stop := hook.ApplyQuery(hookReq)
if stop {
return res
}
res, stop := app.hooks.ApplyQuery(req)
if stop {
return res
}

return app.BaseApp.Query(req)
}

Expand Down
43 changes: 43 additions & 0 deletions app/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
abci "github.com/tendermint/tendermint/abci/types"
)

type Hooks []Hook

// Hook is an interface of hook that can process along with abci application
type Hook interface {
AfterInitChain(ctx sdk.Context, req abci.RequestInitChain, res abci.ResponseInitChain)
Expand All @@ -14,3 +16,44 @@ type Hook interface {
ApplyQuery(req abci.RequestQuery) (res abci.ResponseQuery, stop bool)
BeforeCommit()
}

func (h Hooks) AfterInitChain(ctx sdk.Context, req abci.RequestInitChain, res abci.ResponseInitChain) {
for _, hook := range h {
hook.AfterInitChain(ctx, req, res)
}
}

func (h Hooks) AfterBeginBlock(ctx sdk.Context, req abci.RequestBeginBlock, res abci.ResponseBeginBlock) {
for _, hook := range h {
hook.AfterBeginBlock(ctx, req, res)
}
}

func (h Hooks) AfterDeliverTx(ctx sdk.Context, req abci.RequestDeliverTx, res abci.ResponseDeliverTx) {
for _, hook := range h {
hook.AfterDeliverTx(ctx, req, res)
}
}

func (h Hooks) AfterEndBlock(ctx sdk.Context, req abci.RequestEndBlock, res abci.ResponseEndBlock) {
for _, hook := range h {
hook.AfterEndBlock(ctx, req, res)
}
}

func (h Hooks) BeforeCommit() {
for _, hook := range h {
hook.BeforeCommit()
}
}

func (h Hooks) ApplyQuery(req abci.RequestQuery) (res abci.ResponseQuery, stop bool) {
for _, hook := range h {
res, stop = hook.ApplyQuery(req)
if stop {
return
}
}

return
}