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 7 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
14 changes: 12 additions & 2 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,12 +507,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
2 changes: 1 addition & 1 deletion x/ecocredit/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ require (
github.com/tendermint/tendermint v0.34.13
github.com/tendermint/tm-db v0.6.6
google.golang.org/genproto v0.0.0-20211223182754-3ac035c7e7cb
google.golang.org/grpc v1.43.0
google.golang.org/grpc v1.44.0
pgregory.net/rapid v0.4.7
sigs.k8s.io/yaml v1.1.0
)
Expand Down
11 changes: 2 additions & 9 deletions x/ecocredit/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,6 @@ github.com/coreos/go-systemd v0.0.0-20190620071333-e64a0ec8b42a/go.mod h1:F5haX7
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/coreos/pkg v0.0.0-20160727233714-3ac0863d7acf/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cosmos/cosmos-proto v1.0.0-alpha6/go.mod h1:msdDWOvfStHLG+Z2y2SJ0dcqimZ2vc8M1MPnZ4jOF7U=
github.com/cosmos/cosmos-sdk/api v0.1.0-alpha3/go.mod h1:Ht15guGn9F8b0lv8NkjXE9/asAvVUOt2n4gvQ4q5MyU=
github.com/cosmos/cosmos-sdk/errors v1.0.0-beta.2/go.mod h1:Gi7pzVRnvZ1N16JAXpLADzng0ePoE7YeEHaULSFB2Ts=
github.com/cosmos/cosmos-sdk/orm v1.0.0-alpha.1/go.mod h1:5mhtuO1SxHkN7nQqC41YIIIOEURJ5NieEMqlbsG5IYs=
github.com/cosmos/go-bip39 v0.0.0-20180819234021-555e2067c45d/go.mod h1:tSxLoYXyBmiFeKpvmq4dzayMdCjCnu8uqmCysIGBT2Y=
github.com/cosmos/go-bip39 v1.0.0 h1:pcomnQdrdH22njcAatO0yWojsUnCO3y2tNoV1cb6hHY=
github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4xuwvCdJw=
Expand Down Expand Up @@ -904,7 +900,6 @@ github.com/regen-network/cosmos-sdk v0.44.2-regen-1 h1:Sm7rU4rMwLpEpgrQkF/4w/V/y
github.com/regen-network/cosmos-sdk v0.44.2-regen-1/go.mod h1:fwQJdw+aECatpTvQTo1tSfHEsxACdZYU80QCZUPnHr4=
github.com/regen-network/protobuf v1.3.3-alpha.regen.1 h1:OHEc+q5iIAXpqiqFKeLpu5NwTIkVXUs48vFMwzqpqY4=
github.com/regen-network/protobuf v1.3.3-alpha.regen.1/go.mod h1:2DjTFR1HhMQhiWC5sZ4OhQ3+NtdbZ6oBDKQwq5Ou+FI=
github.com/regen-network/regen-ledger/api v0.1.0-alpha.1/go.mod h1:wN88p7fU2gFrI23sFcDwiOTrY0KVRnL0RkGolO4C3oY=
github.com/rjeczalik/notify v0.9.1/go.mod h1:rKwnCoCGeuQnwBtTSPL9Dad03Vh2n40ePRrjvIXnJho=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
Expand Down Expand Up @@ -1584,8 +1579,8 @@ google.golang.org/grpc v1.37.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQ
google.golang.org/grpc v1.38.0/go.mod h1:NREThFqKR1f3iQ6oBuvc5LadQuXVGo9rkm5ZGrQdJfM=
google.golang.org/grpc v1.40.0/go.mod h1:ogyxbiOoUXAkP+4+xa6PZSE9DZgIHtSpzjDTB9KAK34=
google.golang.org/grpc v1.42.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ590SU=
google.golang.org/grpc v1.43.0 h1:Eeu7bZtDZ2DpRCsLhUlcrLnvYaMK1Gz86a+hMVvELmM=
google.golang.org/grpc v1.43.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ590SU=
google.golang.org/grpc v1.44.0 h1:weqSxi/TMs1SqFRMHCtBgXRs8k3X39QIDEZ0pRcttUg=
google.golang.org/grpc v1.44.0/go.mod h1:k+4IHHFw41K8+bbowsex27ge2rCb65oeWqe4jJ590SU=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
Expand All @@ -1599,7 +1594,6 @@ google.golang.org/protobuf v1.25.0/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlba
google.golang.org/protobuf v1.25.1-0.20200805231151-a709e31e5d12/go.mod h1:9JNX74DMeImyA3h4bdi1ymwjUzf21/xIlbajtzgsN7c=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.27.1 h1:SnqbnDw1V7RiZcXPx5MEeqPv2s79L9i7BJUlG/+RurQ=
google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
Expand Down Expand Up @@ -1639,7 +1633,6 @@ gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
gotest.tools/v3 v3.1.0/go.mod h1:fHy7eyTmJFO5bQbUsEGQ1v4m2J3Jz9eWL54TP2/ZuYQ=
honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
16 changes: 8 additions & 8 deletions x/ecocredit/module/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,23 @@ type Module struct {
}

// NewModule returns a new Module object.
func NewModule(paramSpace paramtypes.Subspace, accountKeeper ecocredit.AccountKeeper, bankKeeper ecocredit.BankKeeper) Module {
func NewModule(paramSpace paramtypes.Subspace, accountKeeper ecocredit.AccountKeeper, bankKeeper ecocredit.BankKeeper) *Module {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pointer receiver is required to handle keeper registration in RegisterServices

if !paramSpace.HasKeyTable() {
paramSpace = paramSpace.WithKeyTable(ecocredit.ParamKeyTable())
}

return Module{
return &Module{
paramSpace: paramSpace,
bankKeeper: bankKeeper,
accountKeeper: accountKeeper,
}
}

var _ module.AppModuleBasic = Module{}
var _ servermodule.Module = Module{}
var _ restmodule.Module = Module{}
var _ climodule.Module = Module{}
var _ module.AppModuleSimulation = Module{}
var _ module.AppModuleBasic = &Module{}
var _ servermodule.Module = &Module{}
var _ restmodule.Module = &Module{}
var _ climodule.Module = &Module{}
var _ module.AppModuleSimulation = &Module{}

func (a Module) Name() string {
return ecocredit.ModuleName
Expand All @@ -61,7 +61,7 @@ func (a Module) RegisterInterfaces(registry types.InterfaceRegistry) {
ecocredit.RegisterTypes(registry)
}

func (a Module) RegisterServices(configurator servermodule.Configurator) {
func (a *Module) RegisterServices(configurator servermodule.Configurator) {
a.keeper = server.RegisterServices(configurator, a.paramSpace, a.accountKeeper, a.bankKeeper)
}

Expand Down
7 changes: 7 additions & 0 deletions x/ecocredit/server/expiration.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
func (s serverImpl) PruneOrders(ctx sdk.Context) error {
blockTime := uint64(ctx.BlockTime().Add(time.Nanosecond).UnixNano())
minTime := uint64(0)
logger := ctx.Logger()

sellOrdersIter, err := s.sellOrderByExpirationIndex.PrefixScan(ctx, minTime, blockTime)
if err != nil {
logger.Error("problem with getting prefix scan iteratorr", "err", err)
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
return err
}
defer sellOrdersIter.Close()
Expand All @@ -26,16 +28,19 @@ func (s serverImpl) PruneOrders(ctx sdk.Context) error {
if orm.ErrIteratorDone.Is(err) {
break
}
logger.Error("problem with sell order load next", "err", err)
return err
}
err = s.sellOrderTable.Delete(ctx, sellOrder.OrderId)
if err != nil {
logger.Error("problem with sell order delete", "err", err)
return err
}
}

buyOrdersIter, err := s.buyOrderByExpirationIndex.PrefixScan(ctx, minTime, blockTime)
if err != nil {
logger.Error("problem with buy order iteratorr", "err", err)
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
return err
}
defer buyOrdersIter.Close()
Expand All @@ -47,10 +52,12 @@ func (s serverImpl) PruneOrders(ctx sdk.Context) error {
if orm.ErrIteratorDone.Is(err) {
break
}
logger.Error("problem with buy order load next", "err", err)
return err
}
err = s.buyOrderTable.Delete(ctx, buyOrder.BuyOrderId)
if err != nil {
logger.Error("problem with buy order delete", "err", err)
return err
}
}
Expand Down
1 change: 0 additions & 1 deletion x/ecocredit/v1beta1/state.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.