diff --git a/CHANGELOG.md b/CHANGELOG.md index 639d5cd7764..1a4bbe9310f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/app/keepers/keepers.go b/app/keepers/keepers.go index 80daf4cacdd..65b7c488152 100644 --- a/app/keepers/keepers.go +++ b/app/keepers/keepers.go @@ -402,6 +402,7 @@ func (appKeepers *AppKeepers) InitNormalKeepers( appKeepers.PoolManagerKeeper, appKeepers.ProtoRevKeeper, appKeepers.DistrKeeper, + appKeepers.ConsensusParamsKeeper, dataDir, ) appKeepers.TxFeesKeeper = &txFeesKeeper diff --git a/go.mod b/go.mod index 0e230fb3e5f..38f510dfa30 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 139510323fe..6d891bcb9ac 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/osmomath/go.mod b/osmomath/go.mod index b2536b4addd..557bcdc93f5 100644 --- a/osmomath/go.mod +++ b/osmomath/go.mod @@ -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 diff --git a/osmoutils/go.mod b/osmoutils/go.mod index 0a4c864c16e..0e221b379f5 100644 --- a/osmoutils/go.mod +++ b/osmoutils/go.mod @@ -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 diff --git a/x/epochs/go.mod b/x/epochs/go.mod index c590906dc75..5f49a5694f3 100644 --- a/x/epochs/go.mod +++ b/x/epochs/go.mod @@ -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 diff --git a/x/ibc-hooks/go.mod b/x/ibc-hooks/go.mod index d1d15c077e6..02163514658 100644 --- a/x/ibc-hooks/go.mod +++ b/x/ibc-hooks/go.mod @@ -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 diff --git a/x/txfees/keeper/keeper.go b/x/txfees/keeper/keeper.go index e1a1cc01d93..f390d1a9ad3 100644 --- a/x/txfees/keeper/keeper.go +++ b/x/txfees/keeper/keeper.go @@ -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 { @@ -21,6 +23,7 @@ type Keeper struct { poolManager types.PoolManager protorevKeeper types.ProtorevKeeper distributionKeeper types.DistributionKeeper + consensusKeeper types.ConsensusKeeper dataDir string } @@ -33,6 +36,7 @@ func NewKeeper( poolManager types.PoolManager, protorevKeeper types.ProtorevKeeper, distributionKeeper types.DistributionKeeper, + consensusKeeper types.ConsensusKeeper, dataDir string, ) Keeper { return Keeper{ @@ -42,6 +46,7 @@ func NewKeeper( poolManager: poolManager, protorevKeeper: protorevKeeper, distributionKeeper: distributionKeeper, + consensusKeeper: consensusKeeper, dataDir: dataDir, } } @@ -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) +} diff --git a/x/txfees/keeper/mempool-1559/code.go b/x/txfees/keeper/mempool-1559/code.go index 53089173da8..0d1d8ba4043 100644 --- a/x/txfees/keeper/mempool-1559/code.go +++ b/x/txfees/keeper/mempool-1559/code.go @@ -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. diff --git a/x/txfees/module.go b/x/txfees/module.go index 462d8d97812..2a8a23f023d 100644 --- a/x/txfees/module.go +++ b/x/txfees/module.go @@ -10,6 +10,7 @@ only specify their tx fee parameters for a single "base" asset. package txfees import ( + "bytes" "context" "encoding/json" "fmt" @@ -31,8 +32,9 @@ import ( ) var ( - _ module.AppModule = AppModule{} - _ module.AppModuleBasic = AppModuleBasic{} + _ module.AppModule = AppModule{} + _ module.AppModuleBasic = AppModuleBasic{} + cachedConsParamBytes []byte ) const ModuleName = types.ModuleName @@ -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 @@ -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 + 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 + } +} diff --git a/x/txfees/module_test.go b/x/txfees/module_test.go index 1e33838057b..28d32dfe528 100644 --- a/x/txfees/module_test.go +++ b/x/txfees/module_test.go @@ -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) { @@ -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 +} diff --git a/x/txfees/types/expected_keepers.go b/x/txfees/types/expected_keepers.go index 9e73aceb1f3..94b69533b00 100644 --- a/x/txfees/types/expected_keepers.go +++ b/x/txfees/types/expected_keepers.go @@ -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" ) @@ -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) +}