-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg_type.go
71 lines (60 loc) · 2.19 KB
/
msg_type.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
package ante
import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/furyanprotocol/v4-chain/protocol/lib"
libante "github.com/furyanprotocol/v4-chain/protocol/lib/ante"
)
// ValidateMsgTypeDecorator checks that the tx has the expected message types.
// Specifically, if the list of msgs in the tx contains an "app-injected message", the tx
// must only contain a single message.
// This decorator will not get exeuted on ReCheckTx since it does not depend on app state.
type ValidateMsgTypeDecorator struct{}
func NewValidateMsgTypeDecorator() ValidateMsgTypeDecorator {
return ValidateMsgTypeDecorator{}
}
func (vbd ValidateMsgTypeDecorator) AnteHandle(
ctx sdk.Context,
tx sdk.Tx,
simulate bool,
next sdk.AnteHandler,
) (sdk.Context, error) {
// no need to validate format on recheck tx, call next antehandler
if ctx.IsReCheckTx() {
return next(ctx, tx, simulate)
}
msgs := tx.GetMsgs()
numMsgs := len(msgs)
if numMsgs == 0 { // invalid.
return ctx, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "msgs cannot be empty")
}
for _, msg := range msgs {
// 1. "App-injected message" check.
if libante.IsAppInjectedMsg(msg) {
// "App-injected message" must be the only msg in the tx.
if numMsgs > 1 {
return ctx, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "app-injected msg must be the only msg in a tx")
}
// "App-injected message" must only be included in DeliverTx.
if !lib.IsDeliverTxMode(ctx) {
return ctx, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "app-injected msg must only be included in DeliverTx")
}
}
// 2. Internal-only message check.
if libante.IsInternalMsg(msg) {
return ctx, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "internal msg cannot be submitted externally")
}
// 3. Nested message check.
if libante.IsNestedMsg(msg) {
if err := libante.ValidateNestedMsg(msg); err != nil {
return ctx, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, err.Error())
}
}
// 4. Unsupported message check.
if libante.IsUnsupportedMsg(msg) {
return ctx, errorsmod.Wrap(sdkerrors.ErrInvalidRequest, "unsupported msg")
}
}
return next(ctx, tx, simulate)
}