-
Notifications
You must be signed in to change notification settings - Fork 39
/
spamming_prevention.go
107 lines (88 loc) · 2.9 KB
/
spamming_prevention.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package ante
import (
"sync"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
oracleexported "github.com/classic-terra/core/v2/x/oracle/exported"
)
// SpammingPreventionDecorator will check if the transaction's gas is smaller than
// configured hard cap
type SpammingPreventionDecorator struct {
oracleKeeper OracleKeeper
oraclePrevoteMap map[string]int64
oracleVoteMap map[string]int64
mu *sync.Mutex
}
// NewSpammingPreventionDecorator returns new spamming prevention decorator instance
func NewSpammingPreventionDecorator(oracleKeeper OracleKeeper) SpammingPreventionDecorator {
return SpammingPreventionDecorator{
oracleKeeper: oracleKeeper,
oraclePrevoteMap: make(map[string]int64),
oracleVoteMap: make(map[string]int64),
mu: &sync.Mutex{},
}
}
// AnteHandle handles msg tax fee checking
func (spd SpammingPreventionDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
if ctx.IsReCheckTx() {
return next(ctx, tx, simulate)
}
if !simulate {
if ctx.IsCheckTx() {
err := spd.CheckOracleSpamming(ctx, tx.GetMsgs())
if err != nil {
return ctx, err
}
}
}
return next(ctx, tx, simulate)
}
// CheckOracleSpamming check whether the msgs are spamming purpose or not
func (spd SpammingPreventionDecorator) CheckOracleSpamming(ctx sdk.Context, msgs []sdk.Msg) error {
spd.mu.Lock()
defer spd.mu.Unlock()
curHeight := ctx.BlockHeight()
for _, msg := range msgs {
switch msg := msg.(type) {
case *oracleexported.MsgAggregateExchangeRatePrevote:
feederAddr, err := sdk.AccAddressFromBech32(msg.Feeder)
if err != nil {
return err
}
valAddr, err := sdk.ValAddressFromBech32(msg.Validator)
if err != nil {
return err
}
err = spd.oracleKeeper.ValidateFeeder(ctx, feederAddr, valAddr)
if err != nil {
return err
}
if lastSubmittedHeight, ok := spd.oraclePrevoteMap[msg.Validator]; ok && lastSubmittedHeight == curHeight {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "the validator has already been submitted prevote at the current height")
}
spd.oraclePrevoteMap[msg.Validator] = curHeight
continue
case *oracleexported.MsgAggregateExchangeRateVote:
feederAddr, err := sdk.AccAddressFromBech32(msg.Feeder)
if err != nil {
return err
}
valAddr, err := sdk.ValAddressFromBech32(msg.Validator)
if err != nil {
return err
}
err = spd.oracleKeeper.ValidateFeeder(ctx, feederAddr, valAddr)
if err != nil {
return err
}
if lastSubmittedHeight, ok := spd.oracleVoteMap[msg.Validator]; ok && lastSubmittedHeight == curHeight {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "the validator has already been submitted vote at the current height")
}
spd.oracleVoteMap[msg.Validator] = curHeight
continue
default:
return nil
}
}
return nil
}