Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Minimum GasUsed proportional to GasLimit #1087

Merged
merged 20 commits into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
### State Machine Breaking

* (ante) [tharsis#1060](https://github.com/tharsis/ethermint/pull/1060) Check `EnableCreate`/`EnableCall` in `AnteHandler` to short-circuit EVM transactions.
* (evm) [tharsis#1087](https://github.com/tharsis/ethermint/pull/1087) Minimum GasUsed proportional to GasLimit and `MinGasDenominator` EVM module param.

### API Breaking

Expand Down
1 change: 1 addition & 0 deletions docs/api/proto-docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ Params defines the EVM module parameters
| `enable_call` | [bool](#bool) | | enable call toggles state transitions that use the vm.Call function |
| `extra_eips` | [int64](#int64) | repeated | extra eips defines the additional EIPs for the vm.Config |
| `chain_config` | [ChainConfig](#ethermint.evm.v1.ChainConfig) | | chain config defines the EVM chain configuration parameters |
| `min_gas_multiplier` | [string](#string) | | min gas denominator bounds the minimum gasUsed to be charged to senders based on GasLimit |



Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ require (
github.com/tendermint/tendermint v0.34.20-0.20220517115723-e6f071164839
github.com/tendermint/tm-db v0.6.7
github.com/tyler-smith/go-bip39 v1.1.0
google.golang.org/genproto v0.0.0-20220414192740-2d67ff6cf2b4
google.golang.org/genproto v0.0.0-20220518221133-4f43b3371335
google.golang.org/grpc v1.46.2
google.golang.org/protobuf v1.28.0
gopkg.in/yaml.v2 v2.4.0
Expand Down Expand Up @@ -138,7 +138,7 @@ require (
github.com/tklauser/numcpus v0.2.2 // indirect
github.com/zondax/hid v0.9.0 // indirect
go.etcd.io/bbolt v1.3.6 // indirect
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 // indirect
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898 // indirect
golang.org/x/net v0.0.0-20220412020605-290c469a71a5 // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
Expand Down
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1241,8 +1241,9 @@ golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20210915214749-c084706c2272/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20211108221036-ceb1ce70b4fa/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4 h1:kUhD7nTDoI3fVd9G4ORWrbV5NY0liEs/Jg2pv5f+bBA=
golang.org/x/crypto v0.0.0-20220411220226-7b82a4e95df4/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898 h1:SLP7Q4Di66FONjDJbCYrCRrh97focO6sLogHO7/g8F0=
crypto-facs marked this conversation as resolved.
Show resolved Hide resolved
golang.org/x/crypto v0.0.0-20220518034528-6f7dac969898/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20180321215751-8460e604b9de/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20180807140117-3d87b88a115f/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -1731,8 +1732,8 @@ google.golang.org/genproto v0.0.0-20220304144024-325a89244dc8/go.mod h1:kGP+zUP2
google.golang.org/genproto v0.0.0-20220310185008-1973136f34c6/go.mod h1:kGP+zUP2Ddo0ayMi4YuN7C3WZyJvGLZRh8Z5wnAqvEI=
google.golang.org/genproto v0.0.0-20220324131243-acbaeb5b85eb/go.mod h1:hAL49I2IFola2sVEjAn7MEwsja0xp51I0tlGAf9hz4E=
google.golang.org/genproto v0.0.0-20220407144326-9054f6ed7bac/go.mod h1:8w6bsBMX6yCPbAVTeqQHvzxW0EIFigd5lZyahWgyfDo=
google.golang.org/genproto v0.0.0-20220414192740-2d67ff6cf2b4 h1:myaecH64R0bIEDjNORIel4iXubqzaHU1K2z8ajBwWcM=
google.golang.org/genproto v0.0.0-20220414192740-2d67ff6cf2b4/go.mod h1:8w6bsBMX6yCPbAVTeqQHvzxW0EIFigd5lZyahWgyfDo=
google.golang.org/genproto v0.0.0-20220518221133-4f43b3371335 h1:2D0OT6tPVdrQTOnVe1VQjfJPTED6EZ7fdJ/f6Db6OsY=
google.golang.org/genproto v0.0.0-20220518221133-4f43b3371335/go.mod h1:RAyBrSAP7Fh3Nc84ghnVLDPuV51xc9agzmm4Ph6i0Q4=
google.golang.org/grpc v1.33.2 h1:EQyQC3sa8M+p6Ulc8yy9SWSS2GVwyRc83gAbG8lrl4o=
google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc=
google.golang.org/grpc/cmd/protoc-gen-go-grpc v1.1.0/go.mod h1:6Kw0yEErY5E/yWrBtf03jp27GLLJujG4z/JK95pnjjw=
Expand Down
6 changes: 6 additions & 0 deletions proto/ethermint/evm/v1/evm.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ message Params {
(gogoproto.moretags) = "yaml:\"chain_config\"",
(gogoproto.nullable) = false
];
// min gas denominator bounds the minimum gasUsed to be charged
// to senders based on GasLimit
string min_gas_multiplier = 6 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Dec",
(gogoproto.nullable) = false
];
}

// ChainConfig defines the Ethereum ChainConfig parameters using *sdk.Int values
Expand Down
55 changes: 55 additions & 0 deletions tests/e2e/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -837,3 +837,58 @@ func (s *IntegrationTestSuite) TestBatchETHTransactions() {
s.Require().Equal(accountNonce+uint64(i)+1, tx.Nonce())
}
}

func (s *IntegrationTestSuite) TestGasConsumptionOnNormalTransfer() {
testCases := []struct {
name string
gasLimit uint64
expectedGasUsed uint64
}{
{
"gas used is the same as gas limit",
21000,
21000,
},
{
"gas used is half of Gas limit",
70000,
35000,
},
{
"gas used is less than half of gasLimit",
30000,
21000,
},
}

recipient := common.HexToAddress("0x378c50D9264C63F3F92B806d4ee56E9D86FfB3Ec")
chainID, err := s.network.Validators[0].JSONRPCClient.ChainID(s.ctx)
s.Require().NoError(err)
from := common.BytesToAddress(s.network.Validators[0].Address)
for _, tc := range testCases {
s.Run(tc.name, func() {
nonce := s.getAccountNonce(from)
s.Require().NoError(err)
gasPrice := s.getGasPrice()
msgTx := evmtypes.NewTx(
chainID,
nonce,
&recipient,
nil,
tc.gasLimit,
gasPrice,
nil, nil,
nil,
nil,
)
msgTx.From = from.Hex()
err = msgTx.Sign(s.ethSigner, s.network.Validators[0].ClientCtx.Keyring)
s.Require().NoError(err)
err := s.network.Validators[0].JSONRPCClient.SendTransaction(s.ctx, msgTx.AsTransaction())
s.Require().NoError(err)
s.waitForTransaction()
receipt := s.expectSuccessReceipt(msgTx.AsTransaction().Hash())
s.Equal(receipt.GasUsed, tc.expectedGasUsed)
})
}
}
10 changes: 10 additions & 0 deletions x/evm/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package keeper

import (
sdk "github.com/cosmos/cosmos-sdk/types"
v2 "github.com/tharsis/ethermint/x/evm/migrations/v2"
)

// Migrator is a struct for handling in-place store migrations.
type Migrator struct {
keeper Keeper
Expand All @@ -11,3 +16,8 @@ func NewMigrator(keeper Keeper) Migrator {
keeper: keeper,
}
}

// Migrate1to2 migrates the store from consensus version v1 to v2
func (m Migrator) Migrate1to2(ctx sdk.Context) error {
return v2.AddMinGasMultiplierParam(ctx, &m.keeper.paramSpace)
}
16 changes: 12 additions & 4 deletions x/evm/keeper/state_transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,12 +398,12 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context, msg core.Message, trace
if msg.Gas() < leftoverGas {
return nil, sdkerrors.Wrap(types.ErrGasOverflow, "apply message")
}
gasUsed := msg.Gas() - leftoverGas
refund := GasToRefund(stateDB.GetRefund(), gasUsed, refundQuotient)
if refund > gasUsed {
temporaryGasUsed := msg.Gas() - leftoverGas
refund := GasToRefund(stateDB.GetRefund(), temporaryGasUsed, refundQuotient)
if refund > temporaryGasUsed {
return nil, sdkerrors.Wrap(types.ErrGasOverflow, "apply message")
}
gasUsed -= refund
temporaryGasUsed -= refund

// EVM execution error needs to be available for the JSON-RPC client
var vmError string
Expand All @@ -418,6 +418,14 @@ func (k *Keeper) ApplyMessageWithConfig(ctx sdk.Context, msg core.Message, trace
}
}

// calculate a minimum amount of gas to be charged to sender if GasLimit
// is considerably higher than GasUsed to stay more aligned with Tendermint gas mechanics
// for more info https://github.com/tharsis/ethermint/issues/1085
// NOTE: MinGasDenominator can not be negative as it is validated on ValidateParams
gasLimit := sdk.NewDec(int64(msg.Gas()))
minimumGasUsed := gasLimit.Mul(cfg.Params.MinGasMultiplier)
gasUsed := sdk.MaxDec(minimumGasUsed, sdk.NewDec(int64(temporaryGasUsed))).TruncateInt().Uint64()

return &types.MsgEthereumTxResponse{
GasUsed: gasUsed,
VmError: vmError,
Expand Down
18 changes: 18 additions & 0 deletions x/evm/migrations/v2/migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package v2

import (
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/tharsis/ethermint/x/evm/types"
)

// AddMinGasMultiplierParam updates the module parameter MinGasMultiplier to 0.5
func AddMinGasMultiplierParam(ctx sdk.Context, paramStore *paramtypes.Subspace) error {
if !paramStore.HasKeyTable() {
ps := paramStore.WithKeyTable(types.ParamKeyTable())
paramStore = &ps
}

paramStore.Set(ctx, types.ParamStoreKeyMinGasMultiplier, types.DefaultMinGasMultiplier)
return nil
}
46 changes: 46 additions & 0 deletions x/evm/migrations/v2/migration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package v2_test

import (
"fmt"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/stretchr/testify/require"
"github.com/tharsis/ethermint/app"
"github.com/tharsis/ethermint/encoding"
v2 "github.com/tharsis/ethermint/x/evm/migrations/v2"
"github.com/tharsis/ethermint/x/evm/types"
"testing"
)

func TestAddMinGasMultiplierParam(t *testing.T) {
encCfg := encoding.MakeConfig(app.ModuleBasics)
erc20Key := sdk.NewKVStoreKey(types.StoreKey)
tErc20Key := sdk.NewTransientStoreKey(fmt.Sprintf("%s_test", types.StoreKey))
ctx := testutil.DefaultContext(erc20Key, tErc20Key)
paramStore := paramtypes.NewSubspace(
encCfg.Marshaler, encCfg.Amino, erc20Key, tErc20Key, "erc20",
)
paramStore = paramStore.WithKeyTable(types.ParamKeyTable())
require.True(t, paramStore.HasKeyTable())

// check no param
require.False(t, paramStore.Has(ctx, types.ParamStoreKeyMinGasMultiplier))

// Run migrations
err := v2.AddMinGasMultiplierParam(ctx, &paramStore)
require.NoError(t, err)

// Make sure the params are set
require.True(t, paramStore.Has(ctx, types.ParamStoreKeyMinGasMultiplier))

var minGasMultiplier sdk.Dec

// Make sure the new params are set
require.NotPanics(t, func() {
paramStore.Get(ctx, types.ParamStoreKeyMinGasMultiplier, &minGasMultiplier)
})

// check the params is up
require.Equal(t, minGasMultiplier, types.DefaultMinGasMultiplier)
}
12 changes: 8 additions & 4 deletions x/evm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (AppModuleBasic) RegisterLegacyAminoCodec(_ *codec.LegacyAmino) {

// ConsensusVersion returns the consensus state-breaking version for the module.
func (AppModuleBasic) ConsensusVersion() uint64 {
return 1
return 2
}

// DefaultGenesis returns default genesis state as raw bytes for the evm
Expand Down Expand Up @@ -117,13 +117,17 @@ func (AppModule) Name() string {
func (am AppModule) RegisterInvariants(ir sdk.InvariantRegistry) {
}

// RegisterQueryService registers a GRPC query service to respond to the
// module-specific GRPC queries.
// RegisterServices registers a GRPC query service to respond to the
// module-specific GRPC queries and runs necessary migrations.
func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), am.keeper)
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)

_ = keeper.NewMigrator(*am.keeper)
migrator := keeper.NewMigrator(*am.keeper)
// register v1 -> v2 migration for MinGasMultiplierParam
if err := cfg.RegisterMigration(types.ModuleName, 1, migrator.Migrate1to2); err != nil {
panic(fmt.Errorf("failed to migrate %s to v2: %w", types.ModuleName, err))
}
}

// Route returns the message routing key for the evm module.
Expand Down
2 changes: 1 addition & 1 deletion x/evm/simulation/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func RandomizedGenState(simState *module.SimulationState) {
func(r *rand.Rand) { extraEIPs = GenExtraEIPs(r) },
)

params := types.NewParams(types.DefaultEVMDenom, true, true, types.DefaultChainConfig(), extraEIPs...)
params := types.NewParams(types.DefaultEVMDenom, true, true, types.DefaultChainConfig(), types.DefaultMinGasMultiplier, extraEIPs...)
evmGenesis := types.NewGenesisState(params, []types.GenesisAccount{})

bz, err := json.MarshalIndent(evmGenesis, "", " ")
Expand Down
Loading