/
fixed_gas.go
56 lines (44 loc) · 1.61 KB
/
fixed_gas.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package ante
import (
sdkerrors "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
oracletypes "github.com/NibiruChain/nibiru/x/oracle/types"
)
const OracleMessageGas = 500
var _ sdk.AnteDecorator = EnsureSinglePostPriceMessageDecorator{}
// EnsureSinglePostPriceMessageDecorator ensures that there is only one oracle vote message in the transaction
// and sets the gas meter to a fixed value.
type EnsureSinglePostPriceMessageDecorator struct{}
func NewPostPriceFixedPriceDecorator() EnsureSinglePostPriceMessageDecorator {
return EnsureSinglePostPriceMessageDecorator{}
}
func (gd EnsureSinglePostPriceMessageDecorator) AnteHandle(
ctx sdk.Context,
tx sdk.Tx,
simulate bool,
next sdk.AnteHandler,
) (newCtx sdk.Context, err error) {
hasOracleVoteMsg := false
hasOraclePreVoteMsg := false
msgs := tx.GetMsgs()
for _, msg := range msgs {
switch msg.(type) {
case *oracletypes.MsgAggregateExchangeRatePrevote:
hasOraclePreVoteMsg = true
case *oracletypes.MsgAggregateExchangeRateVote:
hasOracleVoteMsg = true
}
}
if hasOracleVoteMsg && hasOraclePreVoteMsg {
if len(msgs) > 2 {
return ctx, sdkerrors.Wrap(ErrOracleAnte, "a transaction cannot have more than a single oracle vote and prevote message")
}
ctx = ctx.WithGasMeter(NewFixedGasMeter(OracleMessageGas))
} else if hasOraclePreVoteMsg || hasOracleVoteMsg {
if len(msgs) > 1 {
return ctx, sdkerrors.Wrap(ErrOracleAnte, "a transaction that includes an oracle vote or prevote message cannot have more than those two messages")
}
ctx = ctx.WithGasMeter(NewFixedGasMeter(OracleMessageGas))
}
return next(ctx, tx, simulate)
}