-
Notifications
You must be signed in to change notification settings - Fork 107
/
msg_filter_ante.go
56 lines (45 loc) · 1.46 KB
/
msg_filter_ante.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 (
"fmt"
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type (
// ConsumerKeeper defines the interface required by a consumer module keeper.
ConsumerKeeper interface {
GetProviderChannel(ctx sdk.Context) (string, bool)
}
// MsgFilterDecorator defines an AnteHandler decorator that enables message
// filtering based on certain criteria.
MsgFilterDecorator struct {
ConsumerKeeper ConsumerKeeper
}
)
func NewMsgFilterDecorator(k ConsumerKeeper) MsgFilterDecorator {
return MsgFilterDecorator{
ConsumerKeeper: k,
}
}
func (mfd MsgFilterDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
currHeight := ctx.BlockHeight()
// If the CCV channel has not yet been established, then we must only allow certain
// message types.
if _, ok := mfd.ConsumerKeeper.GetProviderChannel(ctx); !ok {
if !hasValidMsgsPreCCV(tx.GetMsgs()) {
return ctx, fmt.Errorf("tx contains unsupported message types at height %d", currHeight)
}
}
return next(ctx, tx, simulate)
}
func hasValidMsgsPreCCV(msgs []sdk.Msg) bool {
for _, msg := range msgs {
msgType := sdk.MsgTypeURL(msg)
// Only accept IBC messages prior to the CCV channel being established.
// Note, rather than listing out all possible IBC message types, we assume
// all IBC message types have a correct and canonical prefix -- /ibc.*
if !strings.HasPrefix(msgType, "/ibc.") {
return false
}
}
return true
}