|
| 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 | +} |
0 commit comments