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

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

Merged
merged 24 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 19 additions & 0 deletions core/appmodule/environment.go
Original file line number Diff line number Diff line change
@@ -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 map[string]store.KVStoreService
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
MemStoreService map[string]store.MemoryStoreService
}
32 changes: 0 additions & 32 deletions core/appmodule/event.go
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved

This file was deleted.

17 changes: 0 additions & 17 deletions core/appmodule/event_test.go

This file was deleted.

6 changes: 3 additions & 3 deletions core/event/service.go
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,20 @@ 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.
Expand Down
11 changes: 3 additions & 8 deletions core/gas/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Limit() Gas
ConsumeGas(amount Gas, descriptor string)
RefundGas(amount Gas, descriptor string)
IsPastLimit() bool
IsOutOfGas() bool
String() string
GasRemaining() Gas
Limit() Gas
}
23 changes: 23 additions & 0 deletions runtime/environment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package runtime

import (
"cosmossdk.io/core/appmodule"
storetypes "cosmossdk.io/store/types"
)

func NewEnvironment(kvKeys []storetypes.KVStoreKey, memKeys []storetypes.MemoryStoreKey) *appmodule.Environment {

Check failure on line 8 in runtime/environment.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: appmodule.Environment

Check failure on line 8 in runtime/environment.go

View workflow job for this annotation

GitHub Actions / tests (03)

undefined: appmodule.Environment

Check failure on line 8 in runtime/environment.go

View workflow job for this annotation

GitHub Actions / tests (01)

undefined: appmodule.Environment

Check failure on line 8 in runtime/environment.go

View workflow job for this annotation

GitHub Actions / tests (01)

undefined: appmodule.Environment

Check failure on line 8 in runtime/environment.go

View workflow job for this annotation

GitHub Actions / tests (00)

undefined: appmodule.Environment

Check failure on line 8 in runtime/environment.go

View workflow job for this annotation

GitHub Actions / tests (02)

undefined: appmodule.Environment
env := &appmodule.Environment{}

Check failure on line 9 in runtime/environment.go

View workflow job for this annotation

GitHub Actions / dependency-review

undefined: appmodule.Environment

Check failure on line 9 in runtime/environment.go

View workflow job for this annotation

GitHub Actions / tests (03)

undefined: appmodule.Environment

Check failure on line 9 in runtime/environment.go

View workflow job for this annotation

GitHub Actions / tests (01)

undefined: appmodule.Environment

Check failure on line 9 in runtime/environment.go

View workflow job for this annotation

GitHub Actions / tests (01)

undefined: appmodule.Environment

Check failure on line 9 in runtime/environment.go

View workflow job for this annotation

GitHub Actions / tests (00)

undefined: appmodule.Environment

Check failure on line 9 in runtime/environment.go

View workflow job for this annotation

GitHub Actions / tests (02)

undefined: appmodule.Environment
for _, storeKey := range kvKeys {
env.KvStoreService[storeKey.Name()] = NewKVStoreService(&storeKey)
Fixed Show fixed Hide fixed
}
for _, memKey := range memKeys {
env.MemStoreService[memKey.Name()] = NewMemStoreService(&memKey)
Fixed Show fixed Hide fixed
}
env.EventService = EventService{}
env.HeaderService = HeaderService{}
env.BranchService = BranchService{}

// GasService gas.Service

return env
}
6 changes: 3 additions & 3 deletions runtime/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@

func (es EventService) EventManager(ctx context.Context) event.Manager {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return &Events{sdkCtx.EventManager()}

Check failure on line 21 in runtime/events.go

View workflow job for this annotation

GitHub Actions / dependency-review

cannot use &Events{…} (value of type *Events) as event.Manager value in return statement: *Events does not implement event.Manager (wrong type for method Emit)

Check failure on line 21 in runtime/events.go

View workflow job for this annotation

GitHub Actions / tests (03)

cannot use &Events{…} (value of type *Events) as event.Manager value in return statement: *Events does not implement event.Manager (wrong type for method Emit)

Check failure on line 21 in runtime/events.go

View workflow job for this annotation

GitHub Actions / tests (01)

cannot use &Events{…} (value of type *Events) as event.Manager value in return statement: *Events does not implement event.Manager (wrong type for method Emit)

Check failure on line 21 in runtime/events.go

View workflow job for this annotation

GitHub Actions / tests (01)

cannot use &Events{…} (value of type *Events) as event.Manager value in return statement: *Events does not implement event.Manager (wrong type for method Emit)

Check failure on line 21 in runtime/events.go

View workflow job for this annotation

GitHub Actions / tests (00)

cannot use &Events{…} (value of type *Events) as event.Manager value in return statement: *Events does not implement event.Manager (wrong type for method Emit)

Check failure on line 21 in runtime/events.go

View workflow job for this annotation

GitHub Actions / tests (02)

cannot use &Events{…} (value of type *Events) as event.Manager value in return statement: *Events does not implement event.Manager (wrong type for method Emit)
}

var _ event.Manager = (*Events)(nil)

Check failure on line 24 in runtime/events.go

View workflow job for this annotation

GitHub Actions / dependency-review

cannot use (*Events)(nil) (value of type *Events) as event.Manager value in variable declaration: *Events does not implement event.Manager (wrong type for method Emit)

Check failure on line 24 in runtime/events.go

View workflow job for this annotation

GitHub Actions / tests (03)

cannot use (*Events)(nil) (value of type *Events) as event.Manager value in variable declaration: *Events does not implement event.Manager (wrong type for method Emit)

Check failure on line 24 in runtime/events.go

View workflow job for this annotation

GitHub Actions / tests (01)

cannot use (*Events)(nil) (value of type *Events) as event.Manager value in variable declaration: *Events does not implement event.Manager (wrong type for method Emit)

Check failure on line 24 in runtime/events.go

View workflow job for this annotation

GitHub Actions / tests (01)

cannot use (*Events)(nil) (value of type *Events) as event.Manager value in variable declaration: *Events does not implement event.Manager (wrong type for method Emit)

Check failure on line 24 in runtime/events.go

View workflow job for this annotation

GitHub Actions / tests (00)

cannot use (*Events)(nil) (value of type *Events) as event.Manager value in variable declaration: *Events does not implement event.Manager (wrong type for method Emit)

Check failure on line 24 in runtime/events.go

View workflow job for this annotation

GitHub Actions / tests (02)

cannot use (*Events)(nil) (value of type *Events) as event.Manager value in variable declaration: *Events does not implement event.Manager (wrong type for method Emit)

type Events struct {
sdk.EventManagerI
Expand All @@ -29,17 +29,17 @@

func NewEventManager(ctx context.Context) event.Manager {
sdkCtx := sdk.UnwrapSDKContext(ctx)
return &Events{sdkCtx.EventManager()}

Check failure on line 32 in runtime/events.go

View workflow job for this annotation

GitHub Actions / dependency-review

cannot use &Events{…} (value of type *Events) as event.Manager value in return statement: *Events does not implement event.Manager (wrong type for method Emit)

Check failure on line 32 in runtime/events.go

View workflow job for this annotation

GitHub Actions / tests (03)

cannot use &Events{…} (value of type *Events) as event.Manager value in return statement: *Events does not implement event.Manager (wrong type for method Emit)

Check failure on line 32 in runtime/events.go

View workflow job for this annotation

GitHub Actions / tests (01)

cannot use &Events{…} (value of type *Events) as event.Manager value in return statement: *Events does not implement event.Manager (wrong type for method Emit)

Check failure on line 32 in runtime/events.go

View workflow job for this annotation

GitHub Actions / tests (00)

cannot use &Events{…} (value of type *Events) as event.Manager value in return statement: *Events does not implement event.Manager (wrong type for method Emit)

Check failure on line 32 in runtime/events.go

View workflow job for this annotation

GitHub Actions / tests (02)

cannot use &Events{…} (value of type *Events) as event.Manager value in return statement: *Events does not implement event.Manager (wrong type for method Emit)
}

// 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 {
Expand All @@ -52,6 +52,6 @@

// 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)
}
44 changes: 44 additions & 0 deletions runtime/gas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package runtime

import (
"context"

"cosmossdk.io/core/gas"
storetypes "cosmossdk.io/store/types"

sdk "github.com/cosmos/cosmos-sdk/types"
)

var _ gas.Service = GasService{}

type GasService struct {
storetypes.GasMeter
}

func (g GasService) GetGasMeter(ctx context.Context) gas.Meter {
return sdk.UnwrapSDKContext(ctx).GasMeter()
}

func (g GasService) GetBlockGasMeter(ctx context.Context) gas.Meter {
return sdk.UnwrapSDKContext(ctx).BlockGasMeter()
}

func (g GasService) WithGasMeter(ctx context.Context, meter gas.Meter) context.Context {
return sdk.UnwrapSDKContext(ctx).WithGasMeter(meter)
}

func (g GasService) WithBlockGasMeter(ctx context.Context, meter gas.Meter) context.Context {
return sdk.UnwrapSDKContext(ctx).WithGasMeter()

Check failure on line 31 in runtime/gas.go

View workflow job for this annotation

GitHub Actions / dependency-review

not enough arguments in call to sdk.UnwrapSDKContext(ctx).WithGasMeter

Check failure on line 31 in runtime/gas.go

View workflow job for this annotation

GitHub Actions / tests (03)

not enough arguments in call to sdk.UnwrapSDKContext(ctx).WithGasMeter

Check failure on line 31 in runtime/gas.go

View workflow job for this annotation

GitHub Actions / tests (01)

not enough arguments in call to sdk.UnwrapSDKContext(ctx).WithGasMeter

Check failure on line 31 in runtime/gas.go

View workflow job for this annotation

GitHub Actions / tests (00)

not enough arguments in call to sdk.UnwrapSDKContext(ctx).WithGasMeter

Check failure on line 31 in runtime/gas.go

View workflow job for this annotation

GitHub Actions / tests (02)

not enough arguments in call to sdk.UnwrapSDKContext(ctx).WithGasMeter
}

// ______________________________________________________________________________________________
// Gas Meter Wrappers
// ______________________________________________________________________________________________

type SDKGasMeter struct {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Why not just call this GasMeter?

Copy link
Member Author

Choose a reason for hiding this comment

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

the idea was to easily recoginize the difference, since there is a new api for gas metering

gas.Meter
}

type CoreGasmeter struct {
storetypes.GasMeter
}
4 changes: 4 additions & 0 deletions runtime/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
1 change: 0 additions & 1 deletion simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,6 @@ func NewSimApp(
bApp.SetParamStore(app.ConsensusParamsKeeper.ParamsStore)

// add keepers

app.AuthKeeper = authkeeper.NewAccountKeeper(appCodec, runtime.NewKVStoreService(keys[authtypes.StoreKey]), authtypes.ProtoBaseAccount, maccPerms, authcodec.NewBech32Codec(sdk.Bech32MainPrefix), sdk.Bech32MainPrefix, authtypes.NewModuleAddress(govtypes.ModuleName).String())

accountsKeeper, err := accounts.NewKeeper(
Expand Down
1 change: 0 additions & 1 deletion x/accounts/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ 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",
Expand Down
6 changes: 3 additions & 3 deletions x/accounts/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
1 change: 0 additions & 1 deletion x/consensus/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@
}

if err := k.event.EventManager(ctx).EmitKV(
ctx,
"update_consensus_params",

Check failure on line 83 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / dependency-review

cannot use "update_consensus_params" (constant of type string) as context.Context value in argument to k.event.EventManager(ctx).EmitKV: string does not implement context.Context (missing method Deadline)

Check failure on line 83 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (03)

cannot use "update_consensus_params" (constant of type string) as context.Context value in argument to k.event.EventManager(ctx).EmitKV: string does not implement context.Context (missing method Deadline)

Check failure on line 83 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (00)

cannot use "update_consensus_params" (constant of type string) as context.Context value in argument to k.event.EventManager(ctx).EmitKV: string does not implement context.Context (missing method Deadline)

Check failure on line 83 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (02)

cannot use "update_consensus_params" (constant of type string) as context.Context value in argument to k.event.EventManager(ctx).EmitKV: string does not implement context.Context (missing method Deadline)
event.Attribute{Key: "authority", Value: msg.Authority},

Check failure on line 84 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / dependency-review

cannot use event.Attribute{…} (value of type event.Attribute) as string value in argument to k.event.EventManager(ctx).EmitKV

Check failure on line 84 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (03)

cannot use event.Attribute{…} (value of type event.Attribute) as string value in argument to k.event.EventManager(ctx).EmitKV

Check failure on line 84 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (00)

cannot use event.Attribute{…} (value of type event.Attribute) as string value in argument to k.event.EventManager(ctx).EmitKV

Check failure on line 84 in x/consensus/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (02)

cannot use event.Attribute{…} (value of type event.Attribute) as string value in argument to k.event.EventManager(ctx).EmitKV
event.Attribute{Key: "parameters", Value: consensusParams.String()}); err != nil {
return nil, err
}
Expand Down
1 change: 0 additions & 1 deletion x/counter/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@
}

if err := k.event.EventManager(ctx).EmitKV(
ctx,
"increase_counter",

Check failure on line 71 in x/counter/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / dependency-review

cannot use "increase_counter" (constant of type string) as context.Context value in argument to k.event.EventManager(ctx).EmitKV: string does not implement context.Context (missing method Deadline)

Check failure on line 71 in x/counter/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (03)

cannot use "increase_counter" (constant of type string) as context.Context value in argument to k.event.EventManager(ctx).EmitKV: string does not implement context.Context (missing method Deadline)

Check failure on line 71 in x/counter/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (00)

cannot use "increase_counter" (constant of type string) as context.Context value in argument to k.event.EventManager(ctx).EmitKV: string does not implement context.Context (missing method Deadline)

Check failure on line 71 in x/counter/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (02)

cannot use "increase_counter" (constant of type string) as context.Context value in argument to k.event.EventManager(ctx).EmitKV: string does not implement context.Context (missing method Deadline)
event.Attribute{Key: "signer", Value: msg.Signer},

Check failure on line 72 in x/counter/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / dependency-review

cannot use event.Attribute{…} (value of type event.Attribute) as string value in argument to k.event.EventManager(ctx).EmitKV

Check failure on line 72 in x/counter/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (03)

cannot use event.Attribute{…} (value of type event.Attribute) as string value in argument to k.event.EventManager(ctx).EmitKV

Check failure on line 72 in x/counter/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (00)

cannot use event.Attribute{…} (value of type event.Attribute) as string value in argument to k.event.EventManager(ctx).EmitKV

Check failure on line 72 in x/counter/keeper/keeper.go

View workflow job for this annotation

GitHub Actions / tests (02)

cannot use event.Attribute{…} (value of type event.Attribute) as string value in argument to k.event.EventManager(ctx).EmitKV
event.Attribute{Key: "new count", Value: fmt.Sprint(num + msg.Count)}); err != nil {
return nil, err
}
Expand Down
Loading