Skip to content

Commit 1cf0a6b

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

File tree

8 files changed

+428
-10
lines changed

8 files changed

+428
-10
lines changed

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/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)