diff --git a/UPGRADING.md b/UPGRADING.md index efca0e43aa43..f79846af2072 100644 --- a/UPGRADING.md +++ b/UPGRADING.md @@ -116,6 +116,16 @@ Refer to SimApp `root_v2.go` and `root.go` for an example with an app v2 and a l +### Core + +`appmodule.Environment` interface was introduced to fetch different services from the application. This can be used as an alternative to using `sdk.UnwrapContext(ctx)` to fetch the services. It needs to be passed into a module at instantiation. + +Circuit Breaker is used as an example. + +```go +app.CircuitKeeper = circuitkeeper.NewKeeper(runtime.NewEnvironment((keys[circuittypes.StoreKey]), nil), appCodec, authtypes.NewModuleAddress(govtypes.ModuleName).String(), app.AuthKeeper.AddressCodec()) +``` + ### Modules #### `**all**` diff --git a/core/CHANGELOG.md b/core/CHANGELOG.md index 78875aa5df6e..4f5d609d73c3 100644 --- a/core/CHANGELOG.md +++ b/core/CHANGELOG.md @@ -40,12 +40,14 @@ Ref: https://keepachangelog.com/en/1.0.0/ * [#18379](https://github.com/cosmos/cosmos-sdk/pull/18379) Add branch service. * [#18457](https://github.com/cosmos/cosmos-sdk/pull/18457) Add branch.ExecuteWithGasLimit. +* [#19041](https://github.com/cosmos/cosmos-sdk/pull/19041) Add `appmodule.Environment` interface to fetch different services ### API Breaking * [#18857](https://github.com/cosmos/cosmos-sdk/pull/18857) Moved `FormatCoins` to `x/tx`. * [#18861](httpes://github.com/cosmos/cosmos-sdk/pull/18861) Moved `coin.ParseCoin` to `client/v2/internal`. * [#18866](https://github.com/cosmos/cosmos-sdk/pull/18866) All items related to depinject have been moved to `cosmossdk.io/depinject` (`Provide`, `Invoke`, `Register`) +* [#19041](https://github.com/cosmos/cosmos-sdk/pull/19041) `HasEventListeners` was removed from appmodule due to the fact that it was not used anywhere in the SDK nor implemented ## [v0.12.0](https://github.com/cosmos/cosmos-sdk/releases/tag/core%2Fv0.12.0) diff --git a/core/appmodule/environment.go b/core/appmodule/environment.go new file mode 100644 index 000000000000..f7305db9fc3d --- /dev/null +++ b/core/appmodule/environment.go @@ -0,0 +1,19 @@ +package appmodule + +import ( + "cosmossdk.io/core/branch" + "cosmossdk.io/core/event" + "cosmossdk.io/core/gas" + "cosmossdk.io/core/header" + "cosmossdk.io/core/store" +) + +// Environment is used to get all services to their respective module +type Environment struct { + BranchService branch.Service + EventService event.Service + GasService gas.Service + HeaderService header.Service + KVStoreService store.KVStoreService + MemStoreService store.MemoryStoreService +} diff --git a/core/appmodule/event.go b/core/appmodule/event.go deleted file mode 100644 index 0622d6b5a975..000000000000 --- a/core/appmodule/event.go +++ /dev/null @@ -1,32 +0,0 @@ -package appmodule - -import ( - "context" - - "google.golang.org/protobuf/runtime/protoiface" -) - -// HasEventListeners is the extension interface that modules should implement to register -// event listeners. -type HasEventListeners interface { - AppModule - - // RegisterEventListeners registers the module's events listeners. - RegisterEventListeners(registrar *EventListenerRegistrar) -} - -// EventListenerRegistrar allows registering event listeners. -type EventListenerRegistrar struct { - listeners []any -} - -// GetListeners gets the event listeners that have been registered -func (e *EventListenerRegistrar) GetListeners() []any { - return e.listeners -} - -// RegisterEventListener registers an event listener for event type E. If a non-nil error is returned by the listener, -// it will cause the process which emitted the event to fail. -func RegisterEventListener[E protoiface.MessageV1](registrar *EventListenerRegistrar, listener func(context.Context, E) error) { - registrar.listeners = append(registrar.listeners, listener) -} diff --git a/core/appmodule/event_test.go b/core/appmodule/event_test.go deleted file mode 100644 index 739bfa6fb3c9..000000000000 --- a/core/appmodule/event_test.go +++ /dev/null @@ -1,17 +0,0 @@ -package appmodule - -import ( - "context" - "reflect" - "testing" - - "github.com/stretchr/testify/require" - "google.golang.org/protobuf/types/known/timestamppb" -) - -func TestEventListenerRegistrar(t *testing.T) { - registrar := &EventListenerRegistrar{} - RegisterEventListener(registrar, func(ctx context.Context, dummy *timestamppb.Timestamp) error { return nil }) - require.Len(t, registrar.listeners, 1) - require.Equal(t, reflect.Func, reflect.TypeOf(registrar.listeners[0]).Kind()) -} diff --git a/core/event/service.go b/core/event/service.go index b8e09673a58a..941f142db5f9 100644 --- a/core/event/service.go +++ b/core/event/service.go @@ -21,23 +21,27 @@ type Manager interface { // Callers SHOULD assume that these events may be included in consensus. These events // MUST be emitted deterministically and adding, removing or changing these events SHOULD // be considered state-machine breaking. - Emit(ctx context.Context, event protoiface.MessageV1) error + Emit(event protoiface.MessageV1) error // EmitKV emits an event based on an event and kv-pair attributes. // // These events will not be part of consensus and adding, removing or changing these events is // not a state-machine breaking change. - EmitKV(ctx context.Context, eventType string, attrs ...Attribute) error + EmitKV(eventType string, attrs ...Attribute) error // EmitNonConsensus emits events represented as a protobuf message (as described in ADR 032), without // including it in blockchain consensus. // // These events will not be part of consensus and adding, removing or changing events is // not a state-machine breaking change. - EmitNonConsensus(ctx context.Context, event protoiface.MessageV1) error + EmitNonConsensus(event protoiface.MessageV1) error } // KVEventAttribute is a kv-pair event attribute. type Attribute struct { Key, Value string } + +func NewAttribute(key, value string) Attribute { + return Attribute{Key: key, Value: value} +} diff --git a/core/gas/meter.go b/core/gas/meter.go index 2bb735faccab..0774fd2a7733 100644 --- a/core/gas/meter.go +++ b/core/gas/meter.go @@ -26,15 +26,10 @@ type Service interface { WithBlockGasMeter(ctx context.Context, meter Meter) context.Context } -// Meter represents a gas meter. +// Meter represents a gas meter for modules consumption type Meter interface { - GasConsumed() Gas - GasConsumedToLimit() Gas - GasRemaining() Gas + Consume(amount Gas, descriptor string) + Refund(amount Gas, descriptor string) + Remaining() Gas Limit() Gas - ConsumeGas(amount Gas, descriptor string) - RefundGas(amount Gas, descriptor string) - IsPastLimit() bool - IsOutOfGas() bool - String() string } diff --git a/runtime/environment.go b/runtime/environment.go new file mode 100644 index 000000000000..6f34e9f67dd7 --- /dev/null +++ b/runtime/environment.go @@ -0,0 +1,18 @@ +package runtime + +import ( + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/store" +) + +// NewEnvironment creates a new environment for the application +// if memstoreservice is needed, it can be added to the environment: environment.MemStoreService = memstoreservice +func NewEnvironment(kvService store.KVStoreService) appmodule.Environment { + return appmodule.Environment{ + EventService: EventService{}, + HeaderService: HeaderService{}, + BranchService: BranchService{}, + GasService: GasService{}, + KVStoreService: kvService, + } +} diff --git a/runtime/events.go b/runtime/events.go index 752dcae2bb30..150de335d5df 100644 --- a/runtime/events.go +++ b/runtime/events.go @@ -34,12 +34,12 @@ func NewEventManager(ctx context.Context) event.Manager { // Emit emits an typed event that is defined in the protobuf file. // In the future these events will be added to consensus. -func (e Events) Emit(ctx context.Context, event protoiface.MessageV1) error { +func (e Events) Emit(event protoiface.MessageV1) error { return e.EventManagerI.EmitTypedEvent(event) } // EmitKV emits a key value pair event. -func (e Events) EmitKV(ctx context.Context, eventType string, attrs ...event.Attribute) error { +func (e Events) EmitKV(eventType string, attrs ...event.Attribute) error { attributes := make([]sdk.Attribute, 0, len(attrs)) for _, attr := range attrs { @@ -52,6 +52,6 @@ func (e Events) EmitKV(ctx context.Context, eventType string, attrs ...event.Att // Emit emits an typed event that is defined in the protobuf file. // In the future these events will be added to consensus. -func (e Events) EmitNonConsensus(ctx context.Context, event protoiface.MessageV1) error { +func (e Events) EmitNonConsensus(event protoiface.MessageV1) error { return e.EventManagerI.EmitTypedEvent(event) } diff --git a/runtime/gas.go b/runtime/gas.go index 0493b8b95d8a..5a18ec511b54 100644 --- a/runtime/gas.go +++ b/runtime/gas.go @@ -2,29 +2,99 @@ package runtime import ( "context" + "fmt" "cosmossdk.io/core/gas" + storetypes "cosmossdk.io/store/types" sdk "github.com/cosmos/cosmos-sdk/types" ) -var _ gas.Service = (*GasService)(nil) +var _ gas.Service = GasService{} type GasService struct{} func (g GasService) GetGasMeter(ctx context.Context) gas.Meter { - sdkCtx := sdk.UnwrapSDKContext(ctx) - return sdkCtx.GasMeter() + return CoreGasmeter{gm: sdk.UnwrapSDKContext(ctx).GasMeter()} } func (g GasService) GetBlockGasMeter(ctx context.Context) gas.Meter { - return sdk.UnwrapSDKContext(ctx).BlockGasMeter() + return CoreGasmeter{gm: sdk.UnwrapSDKContext(ctx).BlockGasMeter()} } func (g GasService) WithGasMeter(ctx context.Context, meter gas.Meter) context.Context { - return sdk.UnwrapSDKContext(ctx).WithGasMeter(meter) + return sdk.UnwrapSDKContext(ctx).WithGasMeter(SDKGasMeter{gm: meter}) } func (g GasService) WithBlockGasMeter(ctx context.Context, meter gas.Meter) context.Context { - return sdk.UnwrapSDKContext(ctx).WithBlockGasMeter(meter) + return sdk.UnwrapSDKContext(ctx).WithGasMeter(SDKGasMeter{gm: meter}) +} + +// ______________________________________________________________________________________________ +// Gas Meter Wrappers +// ______________________________________________________________________________________________ + +// SDKGasMeter is a wrapper around the SDK's GasMeter that implements the GasMeter interface. +type SDKGasMeter struct { + gm gas.Meter +} + +func (gm SDKGasMeter) GasConsumed() storetypes.Gas { + return gm.gm.Remaining() +} + +func (gm SDKGasMeter) GasConsumedToLimit() storetypes.Gas { + if gm.IsPastLimit() { + return gm.gm.Limit() + } + return gm.gm.Remaining() +} + +func (gm SDKGasMeter) GasRemaining() storetypes.Gas { + return gm.gm.Remaining() +} + +func (gm SDKGasMeter) Limit() storetypes.Gas { + return gm.gm.Limit() +} + +func (gm SDKGasMeter) ConsumeGas(amount storetypes.Gas, descriptor string) { + gm.gm.Consume(amount, descriptor) +} + +func (gm SDKGasMeter) RefundGas(amount storetypes.Gas, descriptor string) { + gm.gm.Refund(amount, descriptor) +} + +func (gm SDKGasMeter) IsPastLimit() bool { + return gm.gm.Remaining() <= gm.gm.Limit() +} + +func (gm SDKGasMeter) IsOutOfGas() bool { + return gm.gm.Remaining() >= gm.gm.Limit() +} + +func (gm SDKGasMeter) String() string { + return fmt.Sprintf("BasicGasMeter:\n limit: %d\n consumed: %d", gm.gm.Limit(), gm.gm.Remaining()) +} + +// CoreGasmeter is a wrapper around the SDK's GasMeter that implements the GasMeter interface. +type CoreGasmeter struct { + gm storetypes.GasMeter +} + +func (cgm CoreGasmeter) Consume(amount gas.Gas, descriptor string) { + cgm.gm.ConsumeGas(amount, descriptor) +} + +func (cgm CoreGasmeter) Refund(amount gas.Gas, descriptor string) { + cgm.gm.RefundGas(amount, descriptor) +} + +func (cgm CoreGasmeter) Remaining() gas.Gas { + return cgm.gm.GasRemaining() +} + +func (cgm CoreGasmeter) Limit() gas.Gas { + return cgm.gm.Limit() } diff --git a/runtime/module.go b/runtime/module.go index 888bdef52c5e..1cd814f15844 100644 --- a/runtime/module.go +++ b/runtime/module.go @@ -74,6 +74,7 @@ func init() { ProvideBasicManager, ProvideAppVersionModifier, ProvideAddressCodec, + ProvideEnvironment, ), appconfig.Invoke(SetupAppBuilder), ) @@ -251,6 +252,10 @@ func ProvideAppVersionModifier(app *AppBuilder) baseapp.AppVersionModifier { return app.app } +func ProvideEnvironment(kvService store.KVStoreService) appmodule.Environment { + return NewEnvironment(kvService) +} + type ( // ValidatorAddressCodec is an alias for address.Codec for validator addresses. ValidatorAddressCodec address.Codec diff --git a/runtime/store.go b/runtime/store.go index 230f53c38c5d..5de89d180a65 100644 --- a/runtime/store.go +++ b/runtime/store.go @@ -28,6 +28,10 @@ type memStoreService struct { key *storetypes.MemoryStoreKey } +func NewMemStoreService(storeKey *storetypes.MemoryStoreKey) store.MemoryStoreService { + return &memStoreService{key: storeKey} +} + func (m memStoreService) OpenMemoryStore(ctx context.Context) store.KVStore { return newKVStore(sdk.UnwrapSDKContext(ctx).KVStore(m.key)) } diff --git a/simapp/app.go b/simapp/app.go index 0a53f3e085b8..9f3e6e3b53ba 100644 --- a/simapp/app.go +++ b/simapp/app.go @@ -285,7 +285,6 @@ func NewSimApp( addressCodec := authcodec.NewBech32Codec(sdk.Bech32MainPrefix) // add keepers - accountsKeeper, err := accounts.NewKeeper( appCodec, runtime.NewKVStoreService(keys[accounts.StoreKey]), @@ -354,7 +353,7 @@ func NewSimApp( stakingtypes.NewMultiStakingHooks(app.DistrKeeper.Hooks(), app.SlashingKeeper.Hooks()), ) - app.CircuitKeeper = circuitkeeper.NewKeeper(appCodec, runtime.NewKVStoreService(keys[circuittypes.StoreKey]), authtypes.NewModuleAddress(govtypes.ModuleName).String(), app.AuthKeeper.AddressCodec()) + app.CircuitKeeper = circuitkeeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(keys[circuittypes.StoreKey])), appCodec, authtypes.NewModuleAddress(govtypes.ModuleName).String(), app.AuthKeeper.AddressCodec()) app.BaseApp.SetCircuitBreaker(&app.CircuitKeeper) app.AuthzKeeper = authzkeeper.NewKeeper(runtime.NewKVStoreService(keys[authzkeeper.StoreKey]), appCodec, app.MsgServiceRouter(), app.AuthKeeper) diff --git a/tests/go.mod b/tests/go.mod index de2448ef8d2d..561de7dc3862 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -226,7 +226,9 @@ replace ( cosmossdk.io/api => ../api cosmossdk.io/client/v2 => ../client/v2 cosmossdk.io/depinject => ../depinject + cosmossdk.io/core => ../core cosmossdk.io/x/accounts => ../x/accounts + cosmossdk.io/x/tx => ../x/tx cosmossdk.io/x/auth => ../x/auth cosmossdk.io/x/authz => ../x/authz cosmossdk.io/x/bank => ../x/bank @@ -252,7 +254,4 @@ replace ( github.com/99designs/keyring => github.com/cosmos/keyring v1.2.0 // We always want to test against the latest version of the SDK. github.com/cosmos/cosmos-sdk => ../. - // Fix upstream GHSA-h395-qcrw-5vmq and GHSA-3vp4-m3rf-835h vulnerabilities. - // TODO Remove it: https://github.com/cosmos/cosmos-sdk/issues/10409 - github.com/gin-gonic/gin => github.com/gin-gonic/gin v1.9.1 ) diff --git a/x/accounts/go.mod b/x/accounts/go.mod index 61e88f068ff9..64918a6848ae 100644 --- a/x/accounts/go.mod +++ b/x/accounts/go.mod @@ -160,6 +160,7 @@ replace github.com/cosmos/cosmos-sdk => ../../. replace ( cosmossdk.io/api => ../../api cosmossdk.io/depinject => ../../depinject + cosmossdk.io/core => ../../core cosmossdk.io/x/auth => ../auth cosmossdk.io/x/bank => ../bank cosmossdk.io/x/distribution => ../distribution diff --git a/x/accounts/msg_server.go b/x/accounts/msg_server.go index 52fafeec0c38..a4dce5600190 100644 --- a/x/accounts/msg_server.go +++ b/x/accounts/msg_server.go @@ -44,12 +44,8 @@ func (m msgServer) Init(ctx context.Context, request *v1.MsgInit) (*v1.MsgInitRe eventManager := m.k.eventService.EventManager(ctx) err = eventManager.EmitKV( - ctx, "account_creation", - event.Attribute{ - Key: "address", - Value: accAddrString, - }, + event.NewAttribute("address", accAddrString), ) if err != nil { return nil, err diff --git a/x/accounts/testing/counter/counter.go b/x/accounts/testing/counter/counter.go index 920699fa3226..55aecb82fb8d 100644 --- a/x/accounts/testing/counter/counter.go +++ b/x/accounts/testing/counter/counter.go @@ -114,9 +114,11 @@ func (a Account) TestDependencies(ctx context.Context, _ *counterv1.MsgTestDepen chainID := a.hs.GetHeaderInfo(ctx).ChainID // test gas meter - gasBefore := a.gs.GetGasMeter(ctx).GasConsumedToLimit() - a.gs.GetGasMeter(ctx).ConsumeGas(10, "test") - gasAfter := a.gs.GetGasMeter(ctx).GasConsumedToLimit() + gasBefore := a.gs.GetGasMeter(ctx).Limit() + a.gs.GetGasMeter(ctx).Consume(gasBefore, "before") + a.gs.GetGasMeter(ctx).Consume(10, "test") + gasAfter := a.gs.GetGasMeter(ctx).Limit() + a.gs.GetGasMeter(ctx).Consume(gasBefore, "After") return &counterv1.MsgTestDependenciesResponse{ ChainId: chainID, diff --git a/x/accounts/utils_test.go b/x/accounts/utils_test.go index 08f708ff0782..94b53d96bbaa 100644 --- a/x/accounts/utils_test.go +++ b/x/accounts/utils_test.go @@ -24,13 +24,13 @@ func (a addressCodec) BytesToString(bz []byte) (string, error) { return string type eventService struct{} -func (e eventService) Emit(ctx context.Context, event protoiface.MessageV1) error { return nil } +func (e eventService) Emit(event protoiface.MessageV1) error { return nil } -func (e eventService) EmitKV(ctx context.Context, eventType string, attrs ...event.Attribute) error { +func (e eventService) EmitKV(eventType string, attrs ...event.Attribute) error { return nil } -func (e eventService) EmitNonConsensus(ctx context.Context, event protoiface.MessageV1) error { +func (e eventService) EmitNonConsensus(event protoiface.MessageV1) error { return nil } diff --git a/x/circuit/CHANGELOG.md b/x/circuit/CHANGELOG.md index 8f9cd263ca6d..005ba39481a1 100644 --- a/x/circuit/CHANGELOG.md +++ b/x/circuit/CHANGELOG.md @@ -31,4 +31,8 @@ Ref: https://keepachangelog.com/en/1.0.0/ ## [Unreleased] +### API Breaking + +* [#19041](https://github.com/cosmos/cosmos-sdk/pull/19041) `appmodule.Environment` is received on the Keeper to get access to different application services + ## [v0.1.0](https://github.com/cosmos/cosmos-sdk/releases/tag/x/circuit/v0.1.0) - 2023-11-07 diff --git a/x/circuit/depinject.go b/x/circuit/depinject.go index fdca2207413c..1ec1d4d9662d 100644 --- a/x/circuit/depinject.go +++ b/x/circuit/depinject.go @@ -4,7 +4,6 @@ import ( modulev1 "cosmossdk.io/api/cosmos/circuit/module/v1" "cosmossdk.io/core/address" "cosmossdk.io/core/appmodule" - "cosmossdk.io/core/store" "cosmossdk.io/depinject" "cosmossdk.io/depinject/appconfig" authtypes "cosmossdk.io/x/auth/types" @@ -30,9 +29,9 @@ func init() { type ModuleInputs struct { depinject.In - Config *modulev1.Module - Cdc codec.Codec - StoreService store.KVStoreService + Config *modulev1.Module + Cdc codec.Codec + Environment appmodule.Environment AddressCodec address.Codec } @@ -53,8 +52,8 @@ func ProvideModule(in ModuleInputs) ModuleOutputs { } circuitkeeper := keeper.NewKeeper( + in.Environment, in.Cdc, - in.StoreService, authority.String(), in.AddressCodec, ) diff --git a/x/circuit/go.mod b/x/circuit/go.mod index 455529b282bc..38159d512bbb 100644 --- a/x/circuit/go.mod +++ b/x/circuit/go.mod @@ -164,12 +164,10 @@ require ( replace github.com/cosmos/cosmos-sdk => ../../. replace ( + cosmossdk.io/core => ../../core cosmossdk.io/depinject => ../../depinject cosmossdk.io/x/auth => ../auth cosmossdk.io/x/bank => ../bank - cosmossdk.io/x/distribution => ../distribution - cosmossdk.io/x/mint => ../mint - cosmossdk.io/x/protocolpool => ../protocolpool - cosmossdk.io/x/slashing => ../slashing cosmossdk.io/x/staking => ../staking + cosmossdk.io/x/tx => ../tx ) diff --git a/x/circuit/go.sum b/x/circuit/go.sum index 4d2159b607f1..1405279c56f3 100644 --- a/x/circuit/go.sum +++ b/x/circuit/go.sum @@ -4,8 +4,6 @@ cosmossdk.io/api v0.7.3-0.20231113122742-912390d5fc4a h1:Zr++x1RCJWi+K8bTZsQKdjt cosmossdk.io/api v0.7.3-0.20231113122742-912390d5fc4a/go.mod h1:7B/5XWh1HYwJk3DzWeNoxOSI+nGx1m5UyYfHLFuKzkw= cosmossdk.io/collections v0.4.0 h1:PFmwj2W8szgpD5nOd8GWH6AbYNi1f2J6akWXJ7P5t9s= cosmossdk.io/collections v0.4.0/go.mod h1:oa5lUING2dP+gdDquow+QjlF45eL1t4TJDypgGd+tv0= -cosmossdk.io/core v0.12.1-0.20231114100755-569e3ff6a0d7 h1:hOzi4yo2Fc7h3mod+xX4m4QA4+Uq+PkFRjY/yalZ0B8= -cosmossdk.io/core v0.12.1-0.20231114100755-569e3ff6a0d7/go.mod h1:3v0JJNNd8ye0cOvJ+wUUvE7Ke0d2qxnNIDcXd5mziHk= cosmossdk.io/errors v1.0.1 h1:bzu+Kcr0kS/1DuPBtUFdWjzLqyUuCiyHjyJB6srBV/0= cosmossdk.io/errors v1.0.1/go.mod h1:MeelVSZThMi4bEakzhhhE/CKqVv3nOJDA25bIqRDu/U= cosmossdk.io/log v1.3.0 h1:L0Z0XstClo2kOU4h3V1iDoE5Ji64sg5HLOogzGg67Oo= @@ -14,8 +12,6 @@ cosmossdk.io/math v1.2.0 h1:8gudhTkkD3NxOP2YyyJIYYmt6dQ55ZfJkDOaxXpy7Ig= cosmossdk.io/math v1.2.0/go.mod h1:l2Gnda87F0su8a/7FEKJfFdJrM0JZRXQaohlgJeyQh0= cosmossdk.io/store v1.0.2 h1:lSg5BTvJBHUDwswNNyeh4K/CbqiHER73VU4nDNb8uk0= cosmossdk.io/store v1.0.2/go.mod h1:EFtENTqVTuWwitGW1VwaBct+yDagk7oG/axBMPH+FXs= -cosmossdk.io/x/tx v0.13.0 h1:8lzyOh3zONPpZv2uTcUmsv0WTXy6T1/aCVDCqShmpzU= -cosmossdk.io/x/tx v0.13.0/go.mod h1:CpNQtmoqbXa33/DVxWQNx5Dcnbkv2xGUhL7tYQ5wUsY= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= diff --git a/x/circuit/keeper/genesis_test.go b/x/circuit/keeper/genesis_test.go index 3569c5fc69db..e11e0da5b30c 100644 --- a/x/circuit/keeper/genesis_test.go +++ b/x/circuit/keeper/genesis_test.go @@ -48,7 +48,7 @@ func (s *GenesisTestSuite) SetupTest() { s.Require().NoError(err) s.addrBytes = bz - s.keeper = keeper.NewKeeper(s.cdc, runtime.NewKVStoreService(key), authority.String(), ac) + s.keeper = keeper.NewKeeper(runtime.NewEnvironment(runtime.NewKVStoreService(key)), s.cdc, authority.String(), ac) } func (s *GenesisTestSuite) TestInitExportGenesis() { diff --git a/x/circuit/keeper/keeper.go b/x/circuit/keeper/keeper.go index 25513991f8c1..aa6a71a03ad6 100644 --- a/x/circuit/keeper/keeper.go +++ b/x/circuit/keeper/keeper.go @@ -5,6 +5,8 @@ import ( "cosmossdk.io/collections" "cosmossdk.io/core/address" + "cosmossdk.io/core/appmodule" + "cosmossdk.io/core/event" "cosmossdk.io/core/store" "cosmossdk.io/x/circuit/types" @@ -15,6 +17,7 @@ import ( type Keeper struct { cdc codec.BinaryCodec storeService store.KVStoreService + eventService event.Service authority []byte @@ -28,17 +31,20 @@ type Keeper struct { } // NewKeeper constructs a new Circuit Keeper instance -func NewKeeper(cdc codec.BinaryCodec, storeService store.KVStoreService, authority string, addressCodec address.Codec) Keeper { +func NewKeeper(env appmodule.Environment, cdc codec.BinaryCodec, authority string, addressCodec address.Codec) Keeper { auth, err := addressCodec.StringToBytes(authority) if err != nil { panic(err) } + storeService := env.KVStoreService + sb := collections.NewSchemaBuilder(storeService) k := Keeper{ cdc: cdc, storeService: storeService, + eventService: env.EventService, authority: auth, addressCodec: addressCodec, Permissions: collections.NewMap( diff --git a/x/circuit/keeper/keeper_test.go b/x/circuit/keeper/keeper_test.go index d556388212ec..f3091989dd44 100644 --- a/x/circuit/keeper/keeper_test.go +++ b/x/circuit/keeper/keeper_test.go @@ -42,8 +42,9 @@ func initFixture(t *testing.T) *fixture { encCfg := moduletestutil.MakeTestEncodingConfig(circuit.AppModuleBasic{}) ac := addresscodec.NewBech32Codec("cosmos") mockStoreKey := storetypes.NewKVStoreKey("test") - storeService := runtime.NewKVStoreService(mockStoreKey) - k := keeper.NewKeeper(encCfg.Codec, storeService, authtypes.NewModuleAddress("gov").String(), ac) + + env := runtime.NewEnvironment(runtime.NewKVStoreService(mockStoreKey)) + k := keeper.NewKeeper(env, encCfg.Codec, authtypes.NewModuleAddress("gov").String(), ac) bz, err := ac.StringToBytes(authtypes.NewModuleAddress("gov").String()) require.NoError(t, err) diff --git a/x/circuit/keeper/msg_server.go b/x/circuit/keeper/msg_server.go index 0c6bb07cbc40..3c60cb07848b 100644 --- a/x/circuit/keeper/msg_server.go +++ b/x/circuit/keeper/msg_server.go @@ -7,10 +7,10 @@ import ( "strings" "cosmossdk.io/collections" + "cosmossdk.io/core/event" errorsmod "cosmossdk.io/errors" "cosmossdk.io/x/circuit/types" - sdk "github.com/cosmos/cosmos-sdk/types" sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" ) @@ -63,15 +63,15 @@ func (srv msgServer) AuthorizeCircuitBreaker(ctx context.Context, msg *types.Msg return nil, err } - sdkCtx := sdk.UnwrapSDKContext(ctx) - sdkCtx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - "authorize_circuit_breaker", - sdk.NewAttribute("granter", msg.Granter), - sdk.NewAttribute("grantee", msg.Grantee), - sdk.NewAttribute("permission", msg.Permissions.String()), - ), - }) + err = srv.Keeper.eventService.EventManager(ctx).EmitKV( + "authorize_circuit_breaker", + event.NewAttribute("granter", msg.Granter), + event.NewAttribute("grantee", msg.Grantee), + event.NewAttribute("permission", msg.Permissions.String()), + ) + if err != nil { + return nil, err + } return &types.MsgAuthorizeCircuitBreakerResponse{ Success: true, @@ -121,14 +121,14 @@ func (srv msgServer) TripCircuitBreaker(ctx context.Context, msg *types.MsgTripC urls := strings.Join(msg.GetMsgTypeUrls(), ",") - sdkCtx := sdk.UnwrapSDKContext(ctx) - sdkCtx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - "trip_circuit_breaker", - sdk.NewAttribute("authority", msg.Authority), - sdk.NewAttribute("msg_url", urls), - ), - }) + err = srv.Keeper.eventService.EventManager(ctx).EmitKV( + "trip_circuit_breaker", + event.NewAttribute("authority", msg.Authority), + event.NewAttribute("msg_url", urls), + ) + if err != nil { + return nil, err + } return &types.MsgTripCircuitBreakerResponse{ Success: true, @@ -180,14 +180,14 @@ func (srv msgServer) ResetCircuitBreaker(ctx context.Context, msg *types.MsgRese urls := strings.Join(msg.GetMsgTypeUrls(), ",") - sdkCtx := sdk.UnwrapSDKContext(ctx) - sdkCtx.EventManager().EmitEvents(sdk.Events{ - sdk.NewEvent( - "reset_circuit_breaker", - sdk.NewAttribute("authority", msg.Authority), - sdk.NewAttribute("msg_url", urls), - ), - }) + err = srv.Keeper.eventService.EventManager(ctx).EmitKV( + "reset_circuit_breaker", + event.NewAttribute("authority", msg.Authority), + event.NewAttribute("msg_url", urls), + ) + if err != nil { + return nil, err + } return &types.MsgResetCircuitBreakerResponse{Success: true}, nil } diff --git a/x/consensus/keeper/keeper.go b/x/consensus/keeper/keeper.go index 31315de40532..a0ccf562dea2 100644 --- a/x/consensus/keeper/keeper.go +++ b/x/consensus/keeper/keeper.go @@ -80,10 +80,9 @@ func (k Keeper) UpdateParams(ctx context.Context, msg *types.MsgUpdateParams) (* } if err := k.event.EventManager(ctx).EmitKV( - ctx, "update_consensus_params", - event.Attribute{Key: "authority", Value: msg.Authority}, - event.Attribute{Key: "parameters", Value: consensusParams.String()}); err != nil { + event.NewAttribute("authority", msg.Authority), + event.NewAttribute("parameters", consensusParams.String())); err != nil { return nil, err } diff --git a/x/counter/keeper/keeper.go b/x/counter/keeper/keeper.go index 8fc152fa3e52..6d7fd422e8c3 100644 --- a/x/counter/keeper/keeper.go +++ b/x/counter/keeper/keeper.go @@ -68,10 +68,9 @@ func (k Keeper) IncreaseCount(ctx context.Context, msg *types.MsgIncreaseCounter } if err := k.event.EventManager(ctx).EmitKV( - ctx, "increase_counter", - event.Attribute{Key: "signer", Value: msg.Signer}, - event.Attribute{Key: "new count", Value: fmt.Sprint(num + msg.Count)}); err != nil { + event.NewAttribute("signer", msg.Signer), + event.NewAttribute("new count", fmt.Sprint(num+msg.Count))); err != nil { return nil, err }