Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add begin and end blocker support to module/server #720

Merged
merged 18 commits into from
Mar 7, 2022
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
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ go.sum: go.mod

tidy:
./scripts/go-mod-tidy-all.sh
.PHONY: tidy

###############################################################################
### Documentation ###
Expand Down Expand Up @@ -431,8 +432,3 @@ regen-mocks:
mockgen -source=x/ecocredit/server/core/keeper.go -package mocks -destination x/ecocredit/server/core/mocks/keeper.go
mockgen -source=x/ecocredit/expected_keepers.go -package mocks -destination x/ecocredit/mocks/expected_keepers.go
.PHONY: regen-mocks


tidy:
./scripts/go-mod-tidy-all.sh
.PHONY: tidy
14 changes: 12 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,12 +579,22 @@ func (app *RegenApp) Name() string { return app.BaseApp.Name() }

// BeginBlocker application updates every begin block
func (app *RegenApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) abci.ResponseBeginBlock {
return app.mm.BeginBlock(ctx, req)
resp := app.mm.BeginBlock(ctx, req)
events := app.smm.BeginBlock(ctx, req)
resp.Events = append(resp.Events, events...)
return resp
}

// EndBlocker application updates every end block
func (app *RegenApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) abci.ResponseEndBlock {
return app.mm.EndBlock(ctx, req)
resp := app.mm.EndBlock(ctx, req)
events, vals := app.smm.EndBlock(ctx, req)
if len(resp.ValidatorUpdates) > 0 && len(vals) > 0 {
panic("validator EndBlock updates already set by the SDK Module Manager")
}
resp.ValidatorUpdates = vals
resp.Events = append(resp.Events, events...)
return resp
}

// InitChainer application update at chain initialization
Expand Down
35 changes: 35 additions & 0 deletions types/module/server/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
)

// Manager is the server module manager
// It supports registration for begin and end blockers
type Manager struct {
baseApp *baseapp.BaseApp
cdc *codec.ProtoCodec
Expand All @@ -29,6 +30,8 @@ type Manager struct {
exportGenesisHandlers map[string]module.ExportGenesisHandler
registerInvariantsHandler map[string]RegisterInvariantsHandler
weightedOperationsHandlers []WeightedOperationsHandler
beginBlockers []BeginBlockerModule
endBlockers []EndBlockerModule
}

// RegisterInvariants registers all module routes and module querier routes
Expand Down Expand Up @@ -136,6 +139,12 @@ func (mm *Manager) RegisterModules(modules []module.Module) error {
mm.requiredServices[typ] = true
}

if mod, ok := mod.(BeginBlockerModule); ok {
mm.beginBlockers = append(mm.beginBlockers, mod)
}
if mod, ok := mod.(EndBlockerModule); ok {
mm.endBlockers = append(mm.endBlockers, mod)
}
}

return nil
Expand Down Expand Up @@ -216,6 +225,32 @@ func initGenesis(ctx sdk.Context, cdc codec.Codec,
}, nil
}

func (mm *Manager) BeginBlock(ctx sdk.Context, req abci.RequestBeginBlock) []abci.Event {
ctx = ctx.WithEventManager(sdk.NewEventManager())

for _, m := range mm.beginBlockers {
m.BeginBlock(ctx, req)
}
return ctx.EventManager().ABCIEvents()
}

func (mm *Manager) EndBlock(ctx sdk.Context, req abci.RequestEndBlock) ([]abci.Event, []abci.ValidatorUpdate) {
ctx = ctx.WithEventManager(sdk.NewEventManager())
var vals []abci.ValidatorUpdate

for _, m := range mm.endBlockers {
moduleValUpdates := m.EndBlock(ctx, req)
if len(moduleValUpdates) > 0 {
if len(vals) > 0 {
panic("validator EndBlock updates already set by a previous module")
}

vals = moduleValUpdates
}
}
return ctx.EventManager().ABCIEvents(), vals
}

// ExportGenesis performs export genesis functionality for modules.
func (mm *Manager) ExportGenesis(ctx sdk.Context) map[string]json.RawMessage {
genesisData, err := exportGenesis(ctx, mm.cdc, mm.exportGenesisHandlers)
Expand Down
12 changes: 12 additions & 0 deletions types/module/server/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package server

import (
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkmodule "github.com/cosmos/cosmos-sdk/types/module"
abci "github.com/tendermint/tendermint/abci/types"

"github.com/regen-network/regen-ledger/types/module"
)
Expand All @@ -14,6 +16,16 @@ type Module interface {
RegisterServices(Configurator)
}

// BeginBlockerModule is a module exposing begin blocker for server module manager
type BeginBlockerModule interface {
BeginBlock(sdk.Context, abci.RequestBeginBlock)
}

// EndBlockerModule is a module exposing end blocker for server module manager
type EndBlockerModule interface {
EndBlock(sdk.Context, abci.RequestEndBlock) []abci.ValidatorUpdate
}

type Configurator interface {
sdkmodule.Configurator

Expand Down
10 changes: 6 additions & 4 deletions x/ecocredit/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ require (
github.com/cockroachdb/apd/v2 v2.0.2 // indirect
github.com/coinbase/rosetta-sdk-go v0.7.0 // indirect
github.com/confio/ics23/go v0.6.6 // indirect
github.com/cosmos/btcutil v1.0.4 // indirect
github.com/cosmos/cosmos-proto v1.0.0-alpha7 // indirect
github.com/cosmos/cosmos-sdk/errors v1.0.0-beta.2 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
Expand All @@ -56,7 +57,6 @@ require (
github.com/dgryski/go-farm v0.0.0-20200201041132-a6ae2369ad13 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/dvsekhvalnov/jose2go v0.0.0-20200901110807-248326c1351b // indirect
github.com/enigmampc/btcutil v1.0.3-0.20200723161021-e2fb6adb2a25 // indirect
github.com/felixge/httpsnoop v1.0.1 // indirect
github.com/fsnotify/fsnotify v1.5.1 // indirect
github.com/go-kit/kit v0.12.0 // indirect
Expand Down Expand Up @@ -120,8 +120,8 @@ require (
github.com/zondax/hid v0.9.0 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
golang.org/x/crypto v0.0.0-20210915214749-c084706c2272 // indirect
golang.org/x/net v0.0.0-20211005001312-d4b1ae081e3b // indirect
golang.org/x/sys v0.0.0-20211004093028-2c5d950f24ef // indirect
golang.org/x/net v0.0.0-20210917221730-978cfadd31cf // indirect
golang.org/x/sys v0.0.0-20210917161153-d61c044b1678 // indirect
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 // indirect
golang.org/x/text v0.3.7 // indirect
gopkg.in/ini.v1 v1.63.2 // indirect
Expand All @@ -130,12 +130,14 @@ require (
nhooyr.io/websocket v1.8.6 // indirect
)

replace github.com/cosmos/cosmos-sdk => github.com/regen-network/cosmos-sdk v0.44.2-regen-1
replace github.com/cosmos/cosmos-sdk => github.com/regen-network/cosmos-sdk v0.45.0-regen-1

replace github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

replace github.com/regen-network/regen-ledger/orm => ../../orm

replace github.com/regen-network/regen-ledger/types => ../../types

replace github.com/tendermint/tendermint => github.com/tendermint/tendermint v0.34.14

replace github.com/regen-network/regen-ledger/api => ../../api
Loading