refactor!(core): add in environment bundler of service (#19041)

Co-authored-by: Facundo <facundomedica@gmail.com>
This commit is contained in:
Marko 2024-01-30 13:22:35 +01:00 committed by GitHub
parent 7e4d12209b
commit ca04e1144c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
28 changed files with 212 additions and 135 deletions

View File

@ -116,6 +116,16 @@ Refer to SimApp `root_v2.go` and `root.go` for an example with an app v2 and a l
<!-- explain app_config.go changes -->
### 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**`

View File

@ -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)

View File

@ -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
}

View File

@ -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)
}

View File

@ -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())
}

View File

@ -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}
}

View File

@ -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
}

18
runtime/environment.go Normal file
View File

@ -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,
}
}

View File

@ -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)
}

View File

@ -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()
}

View File

@ -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

View File

@ -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))
}

View File

@ -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)

View File

@ -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
)

View File

@ -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

View File

@ -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

View File

@ -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,

View File

@ -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
}

View File

@ -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

View File

@ -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,
)

View File

@ -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
)

View File

@ -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=

View File

@ -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() {

View File

@ -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(

View File

@ -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)

View File

@ -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
}

View File

@ -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
}

View File

@ -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
}