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

feat: eip target gas from consensus params #7270

Merged
merged 12 commits into from
Jan 9, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Features

* [#6993](https://github.com/osmosis-labs/osmosis/pull/6993) feat(math): add mutative api for BigDec.BigInt()
* [#7270](https://github.com/osmosis-labs/osmosis/pull/7270) feat: eip target gas from consensus params

### Misc Improvements

Expand Down
1 change: 1 addition & 0 deletions app/keepers/keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,7 @@ func (appKeepers *AppKeepers) InitNormalKeepers(
appKeepers.PoolManagerKeeper,
appKeepers.ProtoRevKeeper,
appKeepers.DistrKeeper,
appKeepers.ConsensusParamsKeeper,
dataDir,
)
appKeepers.TxFeesKeeper = &txFeesKeeper
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -380,9 +380,9 @@ replace (
// v1.0.0-beta.3 is incompatible, so we use v1.0.0-beta.2
github.com/cosmos/cosmos-proto => github.com/cosmos/cosmos-proto v1.0.0-beta.2

// Our cosmos-sdk branch is: https://github.com/osmosis-labs/cosmos-sdk/tree/osmo/v0.47.5, current branch: osmo/v0.47.5. Direct commit link: https://github.com/osmosis-labs/cosmos-sdk/commit/7ff0763fd1f96b46dc7f6527a3ef08d47b785493
// https://github.com/osmosis-labs/cosmos-sdk/releases/tag/v0.47.5-v21-osmo-5
github.com/cosmos/cosmos-sdk => github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-5
// Our cosmos-sdk branch is: https://github.com/osmosis-labs/cosmos-sdk/tree/osmo/v0.47.5, current branch: osmo/v0.47.5. Direct commit link: https://github.com/osmosis-labs/cosmos-sdk/commit/65d7acfdc2a94cc49837bb9abec462f129029540
// https://github.com/osmosis-labs/cosmos-sdk/releases/tag/v0.47.5-v21-osmo-6
github.com/cosmos/cosmos-sdk => github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-6
github.com/cosmos/gogoproto => github.com/cosmos/gogoproto v1.4.10
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1485,8 +1485,8 @@ github.com/ory/dockertest/v3 v3.10.0 h1:4K3z2VMe8Woe++invjaTB7VRyQXQy5UY+loujO4a
github.com/ory/dockertest/v3 v3.10.0/go.mod h1:nr57ZbRWMqfsdGdFNLHz5jjNdDb7VVFnzAeW1n5N1Lg=
github.com/osmosis-labs/cometbft v0.37.2-v21-osmo-1 h1:dAPp/n4+qqbJgPkMXcU+M0xUWWywHiOrW9RF3zLBbsI=
github.com/osmosis-labs/cometbft v0.37.2-v21-osmo-1/go.mod h1:Y2MMMN//O5K4YKd8ze4r9jmk4Y7h0ajqILXbH5JQFVs=
github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-5 h1:yNtKKl4wVbYcX3vW79GbKNsB0Gb670ihBrnD5+qWtek=
github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-5/go.mod h1:4BNXIoS9XecywMSdPr/DIwCgH2/Qjyr2+qhrHu8YSEo=
github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-6 h1:80nco4oGzcrS5XGdg6UoAKn961Ra9UpE+W9YvBJ2h2E=
github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-6/go.mod h1:4BNXIoS9XecywMSdPr/DIwCgH2/Qjyr2+qhrHu8YSEo=
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3 h1:YlmchqTmlwdWSmrRmXKR+PcU96ntOd8u10vTaTZdcNY=
github.com/osmosis-labs/go-mutesting v0.0.0-20221208041716-b43bcd97b3b3/go.mod h1:lV6KnqXYD/ayTe7310MHtM3I2q8Z6bBfMAi+bhwPYtI=
github.com/osmosis-labs/osmosis/osmomath v0.0.7-0.20240109034818-d2a4cb704d18 h1:yK4tv3+4C+SebP1LeTkle4EuLAOSKsqpaXenlDwysrU=
Expand Down
6 changes: 3 additions & 3 deletions osmomath/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ replace (
// v1.0.0-beta.3 is incompatible, so we use v1.0.0-beta.2
github.com/cosmos/cosmos-proto => github.com/cosmos/cosmos-proto v1.0.0-beta.2

// Our cosmos-sdk branch is: https://github.com/osmosis-labs/cosmos-sdk/tree/osmo/v0.47.5, current branch: osmo/v0.47.5. Direct commit link: https://github.com/osmosis-labs/cosmos-sdk/commit/7ff0763fd1f96b46dc7f6527a3ef08d47b785493
// https://github.com/osmosis-labs/cosmos-sdk/releases/tag/v0.47.5-v21-osmo-5
github.com/cosmos/cosmos-sdk => github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-5
// Our cosmos-sdk branch is: https://github.com/osmosis-labs/cosmos-sdk/tree/osmo/v0.47.5, current branch: osmo/v0.47.5. Direct commit link: https://github.com/osmosis-labs/cosmos-sdk/commit/65d7acfdc2a94cc49837bb9abec462f129029540
// https://github.com/osmosis-labs/cosmos-sdk/releases/tag/v0.47.5-v21-osmo-6
github.com/cosmos/cosmos-sdk => github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-6
github.com/cosmos/gogoproto => github.com/cosmos/gogoproto v1.4.10
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

Expand Down
6 changes: 3 additions & 3 deletions osmoutils/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ replace (
// v1.0.0-beta.3 is incompatible, so we use v1.0.0-beta.2
github.com/cosmos/cosmos-proto => github.com/cosmos/cosmos-proto v1.0.0-beta.2

// Our cosmos-sdk branch is: https://github.com/osmosis-labs/cosmos-sdk/tree/osmo/v0.47.5, current branch: osmo/v0.47.5. Direct commit link: https://github.com/osmosis-labs/cosmos-sdk/commit/7ff0763fd1f96b46dc7f6527a3ef08d47b785493
// https://github.com/osmosis-labs/cosmos-sdk/releases/tag/v0.47.5-v21-osmo-5
github.com/cosmos/cosmos-sdk => github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-5
// Our cosmos-sdk branch is: https://github.com/osmosis-labs/cosmos-sdk/tree/osmo/v0.47.5, current branch: osmo/v0.47.5. Direct commit link: https://github.com/osmosis-labs/cosmos-sdk/commit/65d7acfdc2a94cc49837bb9abec462f129029540
// https://github.com/osmosis-labs/cosmos-sdk/releases/tag/v0.47.5-v21-osmo-6
github.com/cosmos/cosmos-sdk => github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-6
github.com/cosmos/gogoproto => github.com/cosmos/gogoproto v1.4.10
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

Expand Down
6 changes: 3 additions & 3 deletions x/epochs/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ replace (
// v1.0.0-beta.3 is incompatible, so we use v1.0.0-beta.2
github.com/cosmos/cosmos-proto => github.com/cosmos/cosmos-proto v1.0.0-beta.2

// Our cosmos-sdk branch is: https://github.com/osmosis-labs/cosmos-sdk/tree/osmo/v0.47.5, current branch: osmo/v0.47.5. Direct commit link: https://github.com/osmosis-labs/cosmos-sdk/commit/7ff0763fd1f96b46dc7f6527a3ef08d47b785493
// https://github.com/osmosis-labs/cosmos-sdk/releases/tag/v0.47.5-v21-osmo-5
github.com/cosmos/cosmos-sdk => github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-5
// Our cosmos-sdk branch is: https://github.com/osmosis-labs/cosmos-sdk/tree/osmo/v0.47.5, current branch: osmo/v0.47.5. Direct commit link: https://github.com/osmosis-labs/cosmos-sdk/commit/65d7acfdc2a94cc49837bb9abec462f129029540
// https://github.com/osmosis-labs/cosmos-sdk/releases/tag/v0.47.5-v21-osmo-6
github.com/cosmos/cosmos-sdk => github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-6
github.com/cosmos/gogoproto => github.com/cosmos/gogoproto v1.4.10
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

Expand Down
7 changes: 3 additions & 4 deletions x/ibc-hooks/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,9 @@ replace (

// v1.0.0-beta.3 is incompatible, so we use v1.0.0-beta.2
github.com/cosmos/cosmos-proto => github.com/cosmos/cosmos-proto v1.0.0-beta.2

// Our cosmos-sdk branch is: https://github.com/osmosis-labs/cosmos-sdk/tree/osmo/v0.47.5, current branch: osmo/v0.47.5. Direct commit link: https://github.com/osmosis-labs/cosmos-sdk/commit/7ff0763fd1f96b46dc7f6527a3ef08d47b785493
// https://github.com/osmosis-labs/cosmos-sdk/releases/tag/v0.47.5-v21-osmo-5
github.com/cosmos/cosmos-sdk => github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-5
// Our cosmos-sdk branch is: https://github.com/osmosis-labs/cosmos-sdk/tree/osmo/v0.47.5, current branch: osmo/v0.47.5. Direct commit link: https://github.com/osmosis-labs/cosmos-sdk/commit/65d7acfdc2a94cc49837bb9abec462f129029540
// https://github.com/osmosis-labs/cosmos-sdk/releases/tag/v0.47.5-v21-osmo-6
github.com/cosmos/cosmos-sdk => github.com/osmosis-labs/cosmos-sdk v0.47.5-v21-osmo-6
github.com/cosmos/gogoproto => github.com/cosmos/gogoproto v1.4.10
github.com/gogo/protobuf => github.com/regen-network/protobuf v1.3.3-alpha.regen.1

Expand Down
15 changes: 15 additions & 0 deletions x/txfees/keeper/keeper.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/osmosis-labs/osmosis/v21/x/txfees/types"

storetypes "github.com/cosmos/cosmos-sdk/store/types"

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
)

type Keeper struct {
Expand All @@ -21,6 +23,7 @@ type Keeper struct {
poolManager types.PoolManager
protorevKeeper types.ProtorevKeeper
distributionKeeper types.DistributionKeeper
consensusKeeper types.ConsensusKeeper
dataDir string
}

Expand All @@ -33,6 +36,7 @@ func NewKeeper(
poolManager types.PoolManager,
protorevKeeper types.ProtorevKeeper,
distributionKeeper types.DistributionKeeper,
consensusKeeper types.ConsensusKeeper,
dataDir string,
) Keeper {
return Keeper{
Expand All @@ -42,6 +46,7 @@ func NewKeeper(
poolManager: poolManager,
protorevKeeper: protorevKeeper,
distributionKeeper: distributionKeeper,
consensusKeeper: consensusKeeper,
dataDir: dataDir,
}
}
Expand All @@ -54,3 +59,13 @@ func (k Keeper) GetFeeTokensStore(ctx sdk.Context) sdk.KVStore {
store := ctx.KVStore(k.storeKey)
return prefix.NewStore(store, types.FeeTokensStorePrefix)
}

// GetParamsNoUnmarshal returns the current consensus parameters from the consensus params store as raw bytes.
func (k Keeper) GetParamsNoUnmarshal(ctx sdk.Context) []byte {
return k.consensusKeeper.GetParamsNoUnmarshal(ctx)
}

// UnmarshalParamBytes unmarshals the consensus params bytes to the consensus params type.
func (k Keeper) UnmarshalParamBytes(ctx sdk.Context, bz []byte) (*tmproto.ConsensusParams, error) {
return k.consensusKeeper.UnmarshalParamBytes(ctx, bz)
}
5 changes: 3 additions & 2 deletions x/txfees/keeper/mempool-1559/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,9 @@ var (

// Max increase per block is a factor of 1.06, max decrease is 9/10
// If recovering at ~30M gas per block, decrease is .94
MaxBlockChangeRate = sdk.NewDec(1).Quo(sdk.NewDec(10))
TargetGas = int64(75_000_000)
MaxBlockChangeRate = sdk.NewDec(1).Quo(sdk.NewDec(10))
TargetGas = int64(187_500_000)
TargetBlockSpacePercent = sdk.MustNewDecFromStr("0.625")
// In face of continuous spam, will take ~19 blocks from base fee > spam cost, to mempool eviction
// ceil(log_{1.06}(RecheckFeeConstant))
// So potentially 1.8 minutes of impaired UX from 1559 nodes on top of time to get to base fee > spam.
Expand Down
60 changes: 58 additions & 2 deletions x/txfees/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ only specify their tx fee parameters for a single "base" asset.
package txfees

import (
"bytes"
"context"
"encoding/json"
"fmt"
Expand All @@ -31,8 +32,9 @@ import (
)

var (
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
_ module.AppModule = AppModule{}
_ module.AppModuleBasic = AppModuleBasic{}
cachedConsParamBytes []byte
)

const ModuleName = types.ModuleName
Expand Down Expand Up @@ -151,6 +153,10 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
// BeginBlock executes all ABCI BeginBlock logic respective to the txfees module.
func (am AppModule) BeginBlock(ctx sdk.Context, _ abci.RequestBeginBlock) {
mempool1559.BeginBlockCode(ctx)

// Check if the block gas limit has changed.
// If it has, update the target gas for eip1559.
am.CheckAndSetTargetGas(ctx)
}

// EndBlock executes all ABCI EndBlock logic respective to the txfees module. It
Expand All @@ -162,3 +168,53 @@ func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.Val

// ConsensusVersion implements AppModule/ConsensusVersion.
func (AppModule) ConsensusVersion() uint64 { return 1 }

// On start, we unmarshal the consensus params once and cache them.
// Then, on every block, we check if the current consensus param bytes have changed in comparison to the cached value.
// If they have, we unmarshal the current consensus params, update the target gas, and cache the value.
// This is done to improve performance by not having to fetch and unmarshal the consensus params on every block.
func (am AppModule) CheckAndSetTargetGas(ctx sdk.Context) {
// Check if the block gas limit has changed.
// If it has, update the target gas for eip1559.
consParamsBytes := am.keeper.GetParamsNoUnmarshal(ctx)

// If cachedConsParamBytes is nil, set equal to consParamsBytes and set the target gas.
if cachedConsParamBytes == nil {
cachedConsParamBytes = consParamsBytes
newConsensusParams, err := am.keeper.UnmarshalParamBytes(ctx, consParamsBytes)
if err != nil {
panic(err)
}

// Check if newConsensusParams.Block is nil to prevent panic
if newConsensusParams.Block == nil || newConsensusParams.Block.MaxGas == 0 {
return
}

if newConsensusParams.Block.MaxGas == -1 {
return
}

newBlockMaxGas := mempool1559.TargetBlockSpacePercent.Mul(sdk.NewDec(newConsensusParams.Block.MaxGas)).TruncateInt().Int64()
mempool1559.TargetGas = newBlockMaxGas
Copy link
Member

Choose a reason for hiding this comment

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

This isn't right, it should be max gas times .625

Copy link
Member Author

Choose a reason for hiding this comment

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

Done here d148048

return
}

// If the consensus params have changed, unmarshal and update the target gas.
if !bytes.Equal(consParamsBytes, cachedConsParamBytes) {
newConsensusParams, err := am.keeper.UnmarshalParamBytes(ctx, consParamsBytes)
if err != nil {
panic(err)
}

if newConsensusParams.Block.MaxGas == -1 {
return
}

// Sure, its possible that the thing that changes in consensus params was something other than the block gas limit,
// but just double setting it here is fine instead of doing more logic to see what actually changed.
newBlockMaxGas := mempool1559.TargetBlockSpacePercent.Mul(sdk.NewDec(newConsensusParams.Block.MaxGas)).TruncateInt().Int64()
mempool1559.TargetGas = newBlockMaxGas
cachedConsParamBytes = consParamsBytes
}
}
62 changes: 62 additions & 0 deletions x/txfees/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@ package txfees_test
import (
"encoding/json"
"testing"
"time"

abci "github.com/cometbft/cometbft/abci/types"
abcitypes "github.com/cometbft/cometbft/abci/types"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/stretchr/testify/require"

osmosisapp "github.com/osmosis-labs/osmosis/v21/app"

simapp "github.com/osmosis-labs/osmosis/v21/app"
mempool1559 "github.com/osmosis-labs/osmosis/v21/x/txfees/keeper/mempool-1559"
)

func TestSetBaseDenomOnInitBlock(t *testing.T) {
Expand All @@ -38,3 +41,62 @@ func TestSetBaseDenomOnInitBlock(t *testing.T) {
require.Nil(t, err)
require.NotEmpty(t, baseDenom)
}

func TestBeginBlock(t *testing.T) {
app := simapp.Setup(false)
ctx := app.BaseApp.NewContext(false, tmproto.Header{ChainID: "osmosis-1", Height: 1})

genesisState := osmosisapp.GenesisStateWithValSet(app)
stateBytes, err := json.MarshalIndent(genesisState, "", " ")
if err != nil {
panic(err)
}

app.InitChain(
abcitypes.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: sims.DefaultConsensusParams,
AppStateBytes: stateBytes,
ChainId: "osmosis-1",
},
)

// Begin block hasn't happened yet, target gas should be equal to hard coded default value
hardCodedGasTarget := int64(187_500_000)
require.Equal(t, hardCodedGasTarget, mempool1559.TargetGas)

// Run begin block
ctx = RunBeginBlock(ctx, app)

// Target gas should be updated to the value set in InitChain
defaultBlockTargetGas := mempool1559.TargetBlockSpacePercent.Mul(sdk.NewDec(sims.DefaultConsensusParams.Block.MaxGas)).TruncateInt().Int64()
require.Equal(t, defaultBlockTargetGas, mempool1559.TargetGas)

// Run begin block again, should not update target gas
ctx = RunBeginBlock(ctx, app)
require.Equal(t, defaultBlockTargetGas, mempool1559.TargetGas)

// Update the consensus params
newDefaultBlockMaxGas := int64(300_000_000)
newConsensusParams := *sims.DefaultConsensusParams
newConsensusParams.Block.MaxGas = newDefaultBlockMaxGas
app.ConsensusParamsKeeper.Set(ctx, &newConsensusParams)

// Ensure that the consensus params have not been updated yet
require.Equal(t, defaultBlockTargetGas, mempool1559.TargetGas)

// Run begin block again, should update target gas
RunBeginBlock(ctx, app)
expectedNewBlockTargetGas := mempool1559.TargetBlockSpacePercent.Mul(sdk.NewDec(newDefaultBlockMaxGas)).TruncateInt().Int64()
require.Equal(t, expectedNewBlockTargetGas, mempool1559.TargetGas)
}

func RunBeginBlock(ctx sdk.Context, app *simapp.OsmosisApp) sdk.Context {
oldHeight := ctx.BlockHeight()
oldHeader := ctx.BlockHeader()
app.Commit()
newHeader := tmproto.Header{Height: oldHeight + 1, ChainID: oldHeader.ChainID, Time: oldHeader.Time.Add(time.Second)}
app.BeginBlock(abci.RequestBeginBlock{Header: newHeader})
ctx = app.GetBaseApp().NewContext(false, newHeader)
return ctx
}
7 changes: 7 additions & 0 deletions x/txfees/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"

tmproto "github.com/cometbft/cometbft/proto/tendermint/types"

"github.com/osmosis-labs/osmosis/osmomath"
poolmanagertypes "github.com/osmosis-labs/osmosis/v21/x/poolmanager/types"
)
Expand Down Expand Up @@ -87,3 +89,8 @@ type ProtorevKeeper interface {
type DistributionKeeper interface {
FundCommunityPool(ctx sdk.Context, amount sdk.Coins, sender sdk.AccAddress) error
}

type ConsensusKeeper interface {
GetParamsNoUnmarshal(ctx sdk.Context) []byte
UnmarshalParamBytes(ctx sdk.Context, bz []byte) (*tmproto.ConsensusParams, error)
}