Skip to content

Commit

Permalink
refactor(core,stf): complete gas service + simplify deps (#21166)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienrbrt authored Aug 8, 2024
1 parent 7dacef6 commit 7040177
Show file tree
Hide file tree
Showing 42 changed files with 98 additions and 131 deletions.
5 changes: 0 additions & 5 deletions collections/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tidwall/btree v1.7.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240709173604-40e1e62336c5 // indirect
google.golang.org/grpc v1.64.1 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Expand Down
10 changes: 0 additions & 10 deletions collections/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ github.com/tidwall/btree v1.7.0 h1:L1fkJH/AuEh5zBnnBbmTwQ5Lt+bRJ5A8EWecslvo9iI=
github.com/tidwall/btree v1.7.0/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 h1:LfspQV/FYTatPTr/3HzIcmiUFH7PGP+OQ6mgDYo3yuQ=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240709173604-40e1e62336c5 h1:SbSDUWW1PAO24TNpLdeheoYPd7kllICcLU52x6eD4kQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240709173604-40e1e62336c5/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/grpc v1.64.1 h1:LKtvyfbX3UGVPFcGqJ9ItpVWW6oN/2XqTxfAnwRRXiA=
google.golang.org/grpc v1.64.1/go.mod h1:hiQF4LFZelK2WKaP6W0L92zGHtiQdZxk8CrSdvyjeP0=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
Expand Down
1 change: 1 addition & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Features

* [#21166](https://github.com/cosmos/cosmos-sdk/pull/21166) Comment out `appmodule.HasServices` to simplify dependencies. This interface is however still supported.
* [#19953](https://github.com/cosmos/cosmos-sdk/pull/19953) Add transaction service.
* [#18379](https://github.com/cosmos/cosmos-sdk/pull/18379) Add branch service.
* [#18457](https://github.com/cosmos/cosmos-sdk/pull/18457) Add branch.ExecuteWithGasLimit.
Expand Down
35 changes: 17 additions & 18 deletions core/appmodule/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package appmodule
import (
"context"

"google.golang.org/grpc"

"cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/legacy"
)
Expand Down Expand Up @@ -35,23 +33,24 @@ type ValidatorUpdate = appmodule.ValidatorUpdate

// HasServices is the extension interface that modules should implement to register
// implementations of services defined in .proto files.
type HasServices interface {
AppModule
// This API is supported by the Cosmos SDK module managers but is excluded from core to limit dependencies.
// type HasServices interface {
// AppModule

// RegisterServices registers the module's services with the app's service
// registrar.
//
// Two types of services are currently supported:
// - read-only gRPC query services, which are the default.
// - transaction message services, which must have the protobuf service
// option "cosmos.msg.v1.service" (defined in "cosmos/msg/v1/service.proto")
// set to true.
//
// The service registrar will figure out which type of service you are
// implementing based on the presence (or absence) of protobuf options. You
// do not need to specify this in golang code.
RegisterServices(grpc.ServiceRegistrar) error
}
// // RegisterServices registers the module's services with the app's service
// // registrar.
// //
// // Two types of services are currently supported:
// // - read-only gRPC query services, which are the default.
// // - transaction message services, which must have the protobuf service
// // option "cosmos.msg.v1.service" (defined in "cosmos/msg/v1/service.proto")
// // set to true.
// //
// // The service registrar will figure out which type of service you are
// // implementing based on the presence (or absence) of protobuf options. You
// // do not need to specify this in golang code.
// RegisterServices(grpc.ServiceRegistrar) error
// }

// HasPrepareCheckState is an extension interface that contains information about the AppModule
// and PrepareCheckState.
Expand Down
5 changes: 0 additions & 5 deletions core/gas/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ type Service interface {
// will be returned.
GasMeter(context.Context) Meter

// BlockGasMeter returns the current block-level gas meter. A non-nil meter
// is always returned. When one is unavailable in the context an infinite gas meter
// will be returned.
BlockGasMeter(context.Context) Meter

// GasConfig returns the gas costs.
GasConfig(ctx context.Context) GasConfig
}
Expand Down
10 changes: 2 additions & 8 deletions core/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,12 @@ module cosmossdk.io/core

go 1.20

require (
github.com/cosmos/gogoproto v1.5.0
google.golang.org/grpc v1.64.1
)
require github.com/cosmos/gogoproto v1.5.0

require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240709173604-40e1e62336c5 // indirect
google.golang.org/protobuf v1.34.2 // indirect
)

Expand Down
11 changes: 1 addition & 10 deletions core/go.sum
Original file line number Diff line number Diff line change
@@ -1,19 +1,10 @@
github.com/cosmos/gogoproto v1.5.0 h1:SDVwzEqZDDBoslaeZg+dGE55hdzHfgUA40pEanMh52o=
github.com/cosmos/gogoproto v1.5.0/go.mod h1:iUM31aofn3ymidYG6bUR5ZFrk+Om8p5s754eMUcyp8I=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
golang.org/x/sys v0.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240709173604-40e1e62336c5 h1:SbSDUWW1PAO24TNpLdeheoYPd7kllICcLU52x6eD4kQ=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240709173604-40e1e62336c5/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/grpc v1.64.1 h1:LKtvyfbX3UGVPFcGqJ9ItpVWW6oN/2XqTxfAnwRRXiA=
google.golang.org/grpc v1.64.1/go.mod h1:hiQF4LFZelK2WKaP6W0L92zGHtiQdZxk8CrSdvyjeP0=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
2 changes: 1 addition & 1 deletion core/testing/go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
github.com/cosmos/gogoproto v1.5.0 h1:SDVwzEqZDDBoslaeZg+dGE55hdzHfgUA40pEanMh52o=
github.com/cosmos/gogoproto v1.5.0/go.mod h1:iUM31aofn3ymidYG6bUR5ZFrk+Om8p5s754eMUcyp8I=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/tidwall/btree v1.7.0 h1:L1fkJH/AuEh5zBnnBbmTwQ5Lt+bRJ5A8EWecslvo9iI=
Expand Down
9 changes: 8 additions & 1 deletion runtime/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"slices"

abci "github.com/cometbft/cometbft/api/cometbft/abci/v1"
"google.golang.org/grpc"

runtimev1alpha1 "cosmossdk.io/api/cosmos/app/runtime/v1alpha1"
"cosmossdk.io/core/appmodule"
Expand Down Expand Up @@ -77,7 +78,7 @@ func (a *App) RegisterModules(modules ...module.AppModule) error {

if mod, ok := appModule.(module.HasServices); ok {
mod.RegisterServices(a.configurator)
} else if module, ok := appModule.(appmodule.HasServices); ok {
} else if module, ok := appModule.(hasServicesV1); ok {
if err := module.RegisterServices(a.configurator); err != nil {
return err
}
Expand Down Expand Up @@ -271,3 +272,9 @@ func (a *App) UnsafeFindStoreKey(storeKey string) storetypes.StoreKey {
}

var _ servertypes.Application = &App{}

// hasServicesV1 is the interface for registering service in baseapp Cosmos SDK.
// This API is part of core/appmodule but commented out for dependencies.
type hasServicesV1 interface {
RegisterServices(grpc.ServiceRegistrar) error
}
4 changes: 3 additions & 1 deletion runtime/services/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ func ExtractAutoCLIOptions(appModules map[string]appmodule.AppModule) map[string
mod.RegisterServices(cfg)
}

if mod, ok := mod.(appmodule.HasServices); ok {
if mod, ok := mod.(interface {
RegisterServices(grpc.ServiceRegistrar) error
}); ok {
err := mod.RegisterServices(cfg)
if err != nil {
panic(err)
Expand Down
10 changes: 8 additions & 2 deletions runtime/v2/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ func (m *MM[T]) RunMigrations(ctx context.Context, fromVM appmodulev2.VersionMap
func (m *MM[T]) RegisterServices(app *App[T]) error {
for _, module := range m.modules {
// register msg + query
if services, ok := module.(appmodule.HasServices); ok {
if services, ok := module.(hasServicesV1); ok {
if err := registerServices(services, app, protoregistry.GlobalFiles); err != nil {
return err
}
Expand Down Expand Up @@ -554,7 +554,7 @@ func (m *MM[T]) assertNoForgottenModules(
return nil
}

func registerServices[T transaction.Tx](s appmodule.HasServices, app *App[T], registry *protoregistry.Files) error {
func registerServices[T transaction.Tx](s hasServicesV1, app *App[T], registry *protoregistry.Files) error {
c := &configurator{
grpcQueryDecoders: map[string]func() gogoproto.Message{},
stfQueryRouter: app.queryRouterBuilder,
Expand Down Expand Up @@ -710,3 +710,9 @@ func defaultMigrationsOrder(modules []string) []string {
}
return out
}

// hasServicesV1 is the interface for registering service in baseapp Cosmos SDK.
// This API is part of core/appmodule but commented out for dependencies.
type hasServicesV1 interface {
RegisterServices(grpc.ServiceRegistrar) error
}
10 changes: 7 additions & 3 deletions runtime/v2/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
"cosmossdk.io/core/app"
"cosmossdk.io/core/appmodule"
appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/comet"
"cosmossdk.io/core/genesis"
Expand All @@ -33,7 +32,7 @@ import (

var (
_ appmodulev2.AppModule = appModule[transaction.Tx]{}
_ appmodule.HasServices = appModule[transaction.Tx]{}
_ hasServicesV1 = appModule[transaction.Tx]{}
)

type appModule[T transaction.Tx] struct {
Expand Down Expand Up @@ -176,7 +175,12 @@ func ProvideModuleManager[T transaction.Tx](
}

// ProvideEnvironment provides the environment for keeper modules, while maintaining backward compatibility and provide services directly as well.
func ProvideEnvironment[T transaction.Tx](logger log.Logger, config *runtimev2.Module, key depinject.ModuleKey, appBuilder *AppBuilder[T]) (
func ProvideEnvironment[T transaction.Tx](
logger log.Logger,
config *runtimev2.Module,
key depinject.ModuleKey,
appBuilder *AppBuilder[T],
) (
appmodulev2.Environment,
store.KVStoreService,
store.MemoryStoreService,
Expand Down
5 changes: 4 additions & 1 deletion runtime/v2/services/autocli.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ func ExtractAutoCLIOptions(appModules map[string]appmodule.AppModule) (map[strin

// try to auto-discover options based on the last msg and query
// services registered for the module
if mod, ok := mod.(appmodule.HasServices); ok {
// this supports the legacy core/appmodule v1 service registration
if mod, ok := mod.(interface {
RegisterServices(grpc.ServiceRegistrar) error
}); ok {
err := mod.RegisterServices(autoCliRegistrar)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions server/v2/appmanager/go.sum
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
github.com/cosmos/gogoproto v1.5.0 h1:SDVwzEqZDDBoslaeZg+dGE55hdzHfgUA40pEanMh52o=
github.com/cosmos/gogoproto v1.5.0/go.mod h1:iUM31aofn3ymidYG6bUR5ZFrk+Om8p5s754eMUcyp8I=
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/exp v0.0.0-20240314144324-c7f7c6466f7f h1:3CW0unweImhOzd5FmYuRsD4Y4oQFKZIjAnKbjV4WIrw=
Expand Down
21 changes: 10 additions & 11 deletions server/v2/stf/README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# State Transition Function (STF)

STF is a function that takes a state and an action as input and returns the next state. It does not assume the execution model of the application nor consensus.
# State Transition Function (STF)

STF is a function that takes a state and an action as input and returns the next state. It does not assume the execution model of the application nor consensus.

The state transition function receives a read only instance of state. It does not directly write to disk, instead it will return the state changes which has undergone within the application. The state transition function is deterministic, meaning that given the same input, it will always produce the same output.

## BranchDB

BranchDB is a cache of all the reads done within a block, simulation or transaction validation. It takes a read-only instance of state and creates its own write instance using a btree. After all state transitions are done, the new change sets are returned to the caller.
BranchDB is a cache of all the reads done within a block, simulation or transaction validation. It takes a read-only instance of state and creates its own write instance using a btree. After all state transitions are done, the new change sets are returned to the caller.

The BranchDB can be replaced and optimized for specific use cases. The implementation is as follows

Expand All @@ -17,19 +16,19 @@ The BranchDB can be replaced and optimized for specific use cases. The implement

## GasMeter

GasMeter is a utility that keeps track of the gas consumed by the state transition function. It is used to limit the amount of computation that can be done within a block.
GasMeter is a utility that keeps track of the gas consumed by the state transition function. It is used to limit the amount of computation that can be done within a block.

The GasMeter can be replaced and optimized for specific use cases. The implementation is as follows:

```go
type (
// gasMeter is a function type that takes a gas limit as input and returns a gas.Meter.
// It is used to measure and limit the amount of gas consumed during the execution of a function.
gasMeter func(gasLimit uint64) gas.Meter
// gasMeter is a function type that takes a gas limit as input and returns a gas.Meter.
// It is used to measure and limit the amount of gas consumed during the execution of a function.
gasMeter func(gasLimit uint64) gas.Meter

// wrapGasMeter is a function type that wraps a gas meter and a store writer map.
wrapGasMeter func(meter gas.Meter, store store.WriterMap) store.WriterMap
// wrapGasMeter is a function type that wraps a gas meter and a store writer map.
wrapGasMeter func(meter gas.Meter, store store.WriterMap) store.WriterMap
)
```

THe wrappGasMeter is used in order to consume gas. Application developers can seamlsessly replace the gas meter with their own implementation in order to customize consumption of gas.
THe wrappGasMeter is used in order to consume gas. Application developers can seamlsessly replace the gas meter with their own implementation in order to customize consumption of gas.
15 changes: 2 additions & 13 deletions server/v2/stf/core_gas_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"cosmossdk.io/core/gas"
"cosmossdk.io/core/store"
stfgas "cosmossdk.io/server/v2/stf/gas"
)

type (
Expand All @@ -25,21 +26,9 @@ type gasService struct{}

// GasConfig implements gas.Service.
func (g gasService) GasConfig(ctx context.Context) gas.GasConfig {
panic("unimplemented")
return stfgas.DefaultConfig
}

func (g gasService) GasMeter(ctx context.Context) gas.Meter {
return ctx.(*executionContext).meter
}

func (g gasService) BlockGasMeter(ctx context.Context) gas.Meter {
panic("stf has no block gas meter")
}

func (g gasService) WithGasMeter(ctx context.Context, meter gas.Meter) context.Context {
panic("unimplemented")
}

func (g gasService) WithBlockGasMeter(ctx context.Context, meter gas.Meter) context.Context {
panic("unimplemented")
}
6 changes: 4 additions & 2 deletions server/v2/stf/gas/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ func DefaultGasMeter(gasLimit uint64) coregas.Meter {
return NewMeter(gasLimit)
}

var DefaultConfig = StoreConfig{
// DefaultGasConfig returns the default gas config.
// Unless overridden, the default gas costs are:
var DefaultConfig = coregas.GasConfig{
HasCost: 1000,
DeleteCostFlat: 1000,
DeleteCost: 1000,
ReadCostFlat: 1000,
ReadCostPerByte: 3,
WriteCostFlat: 2000,
Expand Down
Loading

0 comments on commit 7040177

Please sign in to comment.