refactor(core,stf): complete gas service + simplify deps (#21166)

This commit is contained in:
Julien Robert
2024-08-08 07:30:09 +00:00
committed by GitHub
parent 7dacef600f
commit 7040177316
42 changed files with 98 additions and 131 deletions
+2 -2
View File
@@ -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=
+10 -11
View File
@@ -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
@@ -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.
+2 -13
View File
@@ -5,6 +5,7 @@ import (
"cosmossdk.io/core/gas"
"cosmossdk.io/core/store"
stfgas "cosmossdk.io/server/v2/stf/gas"
)
type (
@@ -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")
}
+4 -2
View File
@@ -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,
+5 -11
View File
@@ -17,19 +17,13 @@ const (
DescDelete = "Delete"
)
type StoreConfig struct {
ReadCostFlat, ReadCostPerByte, HasCost gas.Gas
WriteCostFlat, WriteCostPerByte, DeleteCostFlat gas.Gas
IterNextCostFlat gas.Gas
}
type Store struct {
parent store.Writer
gasMeter gas.Meter
gasConfig StoreConfig
gasConfig gas.GasConfig
}
func NewStore(gc StoreConfig, meter gas.Meter, parent store.Writer) *Store {
func NewStore(gc gas.GasConfig, meter gas.Meter, parent store.Writer) *Store {
return &Store{
parent: parent,
gasMeter: meter,
@@ -76,7 +70,7 @@ func (s *Store) Set(key, value []byte) error {
}
func (s *Store) Delete(key []byte) error {
if err := s.gasMeter.Consume(s.gasConfig.DeleteCostFlat, DescDelete); err != nil {
if err := s.gasMeter.Consume(s.gasConfig.DeleteCost, DescDelete); err != nil {
return err
}
@@ -113,11 +107,11 @@ var _ store.Iterator = (*iterator)(nil)
type iterator struct {
gasMeter gas.Meter
gasConfig StoreConfig
gasConfig gas.GasConfig
parent store.Iterator
}
func newIterator(parent store.Iterator, gm gas.Meter, gc StoreConfig) store.Iterator {
func newIterator(parent store.Iterator, gm gas.Meter, gc gas.GasConfig) store.Iterator {
return &iterator{
parent: parent,
gasConfig: gc,
+2 -2
View File
@@ -7,7 +7,7 @@ import (
"cosmossdk.io/core/store"
)
func NewMeteredWriterMap(conf StoreConfig, meter gas.Meter, state store.WriterMap) MeteredWriterMap {
func NewMeteredWriterMap(conf gas.GasConfig, meter gas.Meter, state store.WriterMap) MeteredWriterMap {
return MeteredWriterMap{
config: conf,
meter: meter,
@@ -20,7 +20,7 @@ func NewMeteredWriterMap(conf StoreConfig, meter gas.Meter, state store.WriterMa
// version of it. Since the gas meter is shared across different
// writers, the metered writers are memoized.
type MeteredWriterMap struct {
config StoreConfig
config gas.GasConfig
meter gas.Meter
state store.WriterMap
cacheMeteredStores map[string]*Store
+2 -2
View File
@@ -2,8 +2,8 @@ github.com/cosmos/gogoproto v1.5.0 h1:SDVwzEqZDDBoslaeZg+dGE55hdzHfgUA40pEanMh52
github.com/cosmos/gogoproto v1.5.0/go.mod h1:iUM31aofn3ymidYG6bUR5ZFrk+Om8p5s754eMUcyp8I=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=