-
Notifications
You must be signed in to change notification settings - Fork 0
/
ante.go
134 lines (111 loc) · 5.01 KB
/
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
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package app
import (
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/x/auth/ante"
customante "github.com/jinxprotocol/v4-chain/protocol/app/ante"
libante "github.com/jinxprotocol/v4-chain/protocol/lib/ante"
clobante "github.com/jinxprotocol/v4-chain/protocol/x/clob/ante"
clobtypes "github.com/jinxprotocol/v4-chain/protocol/x/clob/types"
)
// HandlerOptions are the options required for constructing an SDK AnteHandler.
// Note: This struct is defined here in order to add `ClobKeeper`. We use
// struct embedding to include the normal cosmos-sdk `HandlerOptions`.
type HandlerOptions struct {
ante.HandlerOptions
ClobKeeper clobtypes.ClobKeeper
}
// NewAnteHandler returns an AnteHandler that checks and increments sequence
// numbers, checks signatures & account numbers, deducts fees from the first
// signer, and handles in-memory clob messages.
//
// Link to default `AnteHandler` used by cosmos sdk:
// https://github.com/cosmos/cosmos-sdk/blob/3bb27795742dab2451b232bab02b82566d1a0192/x/auth/ante/ante.go#L25
func NewAnteHandler(options HandlerOptions) (sdk.AnteHandler, error) {
if options.AccountKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "account keeper is required for ante builder")
}
if options.BankKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "bank keeper is required for ante builder")
}
if options.ClobKeeper == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "clob keeper is required for ante builder")
}
if options.SignModeHandler == nil {
return nil, errorsmod.Wrap(sdkerrors.ErrLogic, "sign mode handler is required for ante builder")
}
anteDecorators := newAnteDecoratorChain(options)
// TODO(STAB-24): This change can be reverted to using ChainAnteDecorators again once
// https://github.com/cosmos/cosmos-sdk/pull/16076 is merged, released, and we pick-up the SDK version containing
// the change.
anteHandlers := make([]sdk.AnteHandler, len(anteDecorators)+1)
// Install the terminator ante handler.
anteHandlers[len(anteDecorators)] = func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, error) {
return ctx, nil
}
for i := 0; i < len(anteDecorators); i++ {
// Make a copy of the value to ensure that we can hold a reference to it. This avoids the golang common mistake:
// https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
ii := i
anteHandlers[ii] = func(ctx sdk.Context, tx sdk.Tx, simulate bool) (sdk.Context, error) {
return anteDecorators[ii].AnteHandle(ctx, tx, simulate, anteHandlers[ii+1])
}
}
return anteHandlers[0], nil
}
// newAnteDecoratorChain returns a list of AnteDecorators in the expected application chain ordering
func newAnteDecoratorChain(options HandlerOptions) []sdk.AnteDecorator {
return []sdk.AnteDecorator{
// Note: app-injected messages, and clob transactions don't require Gas fees.
libante.NewAppInjectedMsgAnteWrapper(
clobante.NewSingleMsgClobTxAnteWrapper(
ante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
),
),
// Set `FreeInfiniteGasMeter` for app-injected messages, and clob transactions.
customante.NewFreeInfiniteGasDecorator(),
ante.NewExtensionOptionsDecorator(options.ExtensionOptionChecker),
customante.NewValidateMsgTypeDecorator(),
// Note: app-injected messages are not signed on purpose.
libante.NewAppInjectedMsgAnteWrapper(
ante.NewValidateBasicDecorator(),
),
ante.NewTxTimeoutHeightDecorator(),
ante.NewValidateMemoDecorator(options.AccountKeeper),
ante.NewConsumeGasForTxSizeDecorator(options.AccountKeeper),
// Note: app-injected messages, and clob transactions don't require Gas fees.
libante.NewAppInjectedMsgAnteWrapper(
clobante.NewSingleMsgClobTxAnteWrapper(
ante.NewDeductFeeDecorator(
options.AccountKeeper,
options.BankKeeper,
options.FeegrantKeeper,
options.TxFeeChecker,
),
),
),
// SetPubKeyDecorator must be called before all signature verification decorators
// Note: app-injected messages are not signed on purpose.
libante.NewAppInjectedMsgAnteWrapper(
ante.NewSetPubKeyDecorator(options.AccountKeeper),
),
ante.NewValidateSigCountDecorator(options.AccountKeeper),
// Note: app-injected messages don't require Gas fees.
libante.NewAppInjectedMsgAnteWrapper(
ante.NewSigGasConsumeDecorator(options.AccountKeeper, options.SigGasConsumer),
),
// Note: app-injected messages are not signed on purpose.
libante.NewAppInjectedMsgAnteWrapper(
customante.NewSigVerificationDecorator(options.AccountKeeper, options.SignModeHandler),
),
// Note: app-injected messages, and short-term clob txs don't have sequence numbers on purpose.
libante.NewAppInjectedMsgAnteWrapper(
clobante.NewShortTermSingleMsgClobTxAnteWrapper(
ante.NewIncrementSequenceDecorator(options.AccountKeeper),
),
),
clobante.NewRateLimitDecorator(options.ClobKeeper),
clobante.NewClobDecorator(options.ClobKeeper),
}
}