Skip to content

Commit abe8ab3

Browse files
committed
fix:staking optimization, add fix costs for whitelisted messages
1 parent d8a1c9e commit abe8ab3

File tree

10 files changed

+432
-13
lines changed

10 files changed

+432
-13
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Ref: https://keepachangelog.com/en/1.0.0/
3737

3838
## Unreleased
3939

40+
* (deps) [#776](https://github.com/crypto-org-chain/ethermint/pull/776) fix: Optimize staking endblocker with an in-memory KV store and standardize gas consumption for staking related messages
4041
* (evm) [#725](https://github.com/crypto-org-chain/ethermint/pull/725) feat(RPC): add authorizationList from eth_getTransactionByHash response for EIP-7702 transactions
4142
* (evm) [#740](https://github.com/crypto-org-chain/ethermint/pull/740) fix: missing tx context during vm initialisation
4243
* (evm) [#742](https://github.com/crypto-org-chain/ethermint/pull/742) fix: prevent nil pointer dereference in tracer hooks

ante/cosmos/fixed_gas.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package cosmos
2+
3+
import (
4+
storetypes "cosmossdk.io/store/types"
5+
sdk "github.com/cosmos/cosmos-sdk/types"
6+
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
7+
)
8+
9+
// FixedGasConsumedDecorator consumes a fixed amount of gas for whitelisted messages
10+
// and then replaces the gas meter with a fixed gas meter to prevent additional gas consumption
11+
type FixedGasConsumedDecorator struct{}
12+
13+
var WhitelistedMessages = map[string]uint64{
14+
// staking messages
15+
sdk.MsgTypeURL(&stakingtypes.MsgCreateValidator{}): 200000,
16+
sdk.MsgTypeURL(&stakingtypes.MsgDelegate{}): 190000,
17+
sdk.MsgTypeURL(&stakingtypes.MsgUndelegate{}): 200000,
18+
sdk.MsgTypeURL(&stakingtypes.MsgBeginRedelegate{}): 232000,
19+
sdk.MsgTypeURL(&stakingtypes.MsgCancelUnbondingDelegation{}): 200000,
20+
sdk.MsgTypeURL(&stakingtypes.MsgEditValidator{}): 90000,
21+
}
22+
23+
// NewFixedGasConsumedDecorator creates a new FixedGasConsumedDecorator
24+
func NewFixedGasConsumedDecorator() FixedGasConsumedDecorator {
25+
return FixedGasConsumedDecorator{}
26+
}
27+
28+
type fixedGasMeter struct {
29+
consumed storetypes.Gas
30+
}
31+
32+
// NewFixedGasMeter returns a new gas meter with a fixed amount of consumed gas.
33+
func NewFixedGasMeter(consumed storetypes.Gas) storetypes.GasMeter {
34+
return &fixedGasMeter{
35+
consumed: consumed,
36+
}
37+
}
38+
39+
var _ storetypes.GasMeter = &fixedGasMeter{}
40+
41+
func (fgm *fixedGasMeter) GasConsumed() storetypes.Gas { return fgm.consumed }
42+
func (fgm *fixedGasMeter) GasConsumedToLimit() storetypes.Gas { return fgm.consumed }
43+
func (fgm *fixedGasMeter) GasRemaining() storetypes.Gas { return 0 }
44+
func (fgm *fixedGasMeter) Limit() storetypes.Gas { return fgm.consumed }
45+
func (fgm *fixedGasMeter) ConsumeGas(storetypes.Gas, string) {}
46+
func (fgm *fixedGasMeter) RefundGas(storetypes.Gas, string) {}
47+
func (fgm *fixedGasMeter) IsPastLimit() bool { return false }
48+
func (fgm *fixedGasMeter) IsOutOfGas() bool { return false }
49+
func (fgm *fixedGasMeter) String() string { return "fixedGasMeter" }
50+
51+
func (fgcd FixedGasConsumedDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
52+
msgs := tx.GetMsgs()
53+
totalFixedGas := uint64(0)
54+
whitelistedMsgsPresent := false
55+
56+
for _, msg := range msgs {
57+
msgTypeURL := sdk.MsgTypeURL(msg)
58+
if fixedGas, found := WhitelistedMessages[msgTypeURL]; found {
59+
whitelistedMsgsPresent = true
60+
totalFixedGas += fixedGas
61+
}
62+
}
63+
64+
if whitelistedMsgsPresent {
65+
ctx.GasMeter().ConsumeGas(totalFixedGas, "fixed gas for whitelisted messages")
66+
consumedGas := ctx.GasMeter().GasConsumed()
67+
ctx = ctx.WithGasMeter(NewFixedGasMeter(consumedGas))
68+
}
69+
70+
return next(ctx, tx, simulate)
71+
}

evmd/ante/ante_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ func (suite *AnteTestSuite) TestAnteHandler() {
356356
"success - DeliverTx EIP712 signed Cosmos Tx with DelegateMsg",
357357
func() sdk.Tx {
358358
from := acc.GetAddress()
359-
gas := uint64(200000)
359+
gas := uint64(300000)
360360
coinAmount := sdk.NewCoin(evmtypes.DefaultEVMDenom, sdkmath.NewInt(100*int64(gas)))
361361
amount := sdk.NewCoins(coinAmount)
362362
txBuilder := suite.CreateTestEIP712TxBuilderMsgDelegate(from, privKey, "ethermint_9000-1", gas, amount)
@@ -369,7 +369,7 @@ func (suite *AnteTestSuite) TestAnteHandler() {
369369
from := acc.GetAddress()
370370
coinAmount := sdk.NewCoin(evmtypes.DefaultEVMDenom, sdkmath.NewInt(20))
371371
amount := sdk.NewCoins(coinAmount)
372-
gas := uint64(200000)
372+
gas := uint64(300000)
373373
txBuilder := suite.CreateTestEIP712MsgCreateValidator(from, privKey, "ethermint_9000-1", gas, amount)
374374
return txBuilder.GetTx()
375375
}, false, false, true,
@@ -380,7 +380,7 @@ func (suite *AnteTestSuite) TestAnteHandler() {
380380
from := acc.GetAddress()
381381
coinAmount := sdk.NewCoin(evmtypes.DefaultEVMDenom, sdkmath.NewInt(20))
382382
amount := sdk.NewCoins(coinAmount)
383-
gas := uint64(200000)
383+
gas := uint64(300000)
384384
txBuilder := suite.CreateTestEIP712MsgCreateValidator2(from, privKey, "ethermint_9000-1", gas, amount)
385385
return txBuilder.GetTx()
386386
}, false, false, true,

evmd/ante/evm_handler.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func newLegacyCosmosAnteHandlerEip712(ctx sdk.Context, options HandlerOptions, e
5555
cosmos.NewLegacyEip712SigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
5656
authante.NewIncrementSequenceDecorator(options.AccountKeeper),
5757
ibcante.NewRedundantRelayDecorator(options.IBCKeeper),
58+
cosmos.NewFixedGasConsumedDecorator(),
5859
}
5960
decorators = append(decorators, extra...)
6061
return sdk.ChainAnteDecorators(decorators...)

0 commit comments

Comments
 (0)