-
Notifications
You must be signed in to change notification settings - Fork 7
/
ante.go
414 lines (371 loc) · 15.3 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
// Copyright 2021 ChainSafe Systems
// SPDX-License-Identifier: MIT
package ante
import (
"bytes"
chainlinkkeeper "github.com/ChainSafe/chainlink-cosmos/x/chainlink/keeper"
"github.com/ChainSafe/chainlink-cosmos/x/chainlink/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
authante "github.com/cosmos/cosmos-sdk/x/auth/ante"
authkeeper "github.com/cosmos/cosmos-sdk/x/auth/keeper"
"github.com/cosmos/cosmos-sdk/x/auth/signing"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
"github.com/ethereum/go-ethereum/common"
)
const (
ErrFeedDoesNotExist = "feed does not exist"
ErrSignerIsNotFeedOwner = "account %s (%s) is not a feed owner"
ErrAccountAlreadyExists = "there is already a chainlink account associated with this cosmos address"
ErrUnregisteredDataProvider = "linked account not found in account store"
ErrDoesNotExist = "no chainlink account associated with this cosmos address"
ErrSubmitterDoesNotMatch = "submitter address does not match"
)
func NewAnteHandler(
ak authkeeper.AccountKeeper, bankKeeper bankkeeper.Keeper, chainLinkKeeper chainlinkkeeper.Keeper,
sigGasConsumer authante.SignatureVerificationGasConsumer,
signModeHandler signing.SignModeHandler, externalTxDataValidationFunc func(sdk.Msg) bool,
) sdk.AnteHandler {
return func(
ctx sdk.Context, tx sdk.Tx, sim bool,
) (newCtx sdk.Context, err error) {
anteHandler := sdk.ChainAnteDecorators(
authante.NewSetUpContextDecorator(), // outermost AnteDecorator. SetUpContext must be called first
authante.NewRejectExtensionOptionsDecorator(),
authante.NewMempoolFeeDecorator(),
authante.NewValidateBasicDecorator(),
authante.TxTimeoutHeightDecorator{},
authante.NewValidateMemoDecorator(ak),
authante.NewConsumeGasForTxSizeDecorator(ak),
authante.NewRejectFeeGranterDecorator(),
authante.NewSetPubKeyDecorator(ak), // SetPubKeyDecorator must be called before all signature verification decorators
authante.NewValidateSigCountDecorator(ak),
authante.NewDeductFeeDecorator(ak, bankKeeper),
authante.NewSigGasConsumeDecorator(ak, sigGasConsumer),
authante.NewSigVerificationDecorator(ak, signModeHandler),
authante.NewIncrementSequenceDecorator(ak),
// all customized anteHandler below
NewModuleOwnerDecorator(chainLinkKeeper),
NewFeedDecorator(chainLinkKeeper),
NewFeedDataDecorator(chainLinkKeeper),
NewValidationDecorator(externalTxDataValidationFunc),
NewAccountDecorator(chainLinkKeeper),
)
return anteHandler(ctx, tx, sim)
}
}
type ModuleOwnerDecorator struct {
chainLinkKeeper chainlinkkeeper.Keeper
}
func NewModuleOwnerDecorator(chainLinkKeeper chainlinkkeeper.Keeper) ModuleOwnerDecorator {
return ModuleOwnerDecorator{
chainLinkKeeper: chainLinkKeeper,
}
}
func (mod ModuleOwnerDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
if len(tx.GetMsgs()) == 0 {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid Msg: empty Msg: %T", tx)
}
existingModuleOwnerList, err := mod.chainLinkKeeper.GetAllModuleOwner(sdk.WrapSDKContext(ctx), nil)
if err != nil {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrLogic, "module owner check failed at anteHandler[ModuleOwnerDecorator]")
}
// no checking if module owner list is empty
if len(existingModuleOwnerList.GetModuleOwner()) == 0 {
return next(ctx, tx, simulate)
}
signers := make([]sdk.AccAddress, 0)
// get the signers of module owner Msg types
for _, msg := range tx.GetMsgs() {
switch t := msg.(type) {
case *types.MsgModuleOwner:
if len(t.GetSigners()) == 0 {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid Tx: empty signer: %T", t)
}
signers = append(signers, t.GetSigners()[0])
case *types.MsgModuleOwnershipTransfer:
if len(t.GetSigners()) == 0 {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid Tx: empty signer: %T", t)
}
signers = append(signers, t.GetSigners()[0])
case *types.MsgFeed:
if len(t.GetSigners()) == 0 {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid Tx: empty signer: %T", t)
}
signers = append(signers, t.GetSigners()[0])
default:
continue
}
}
for _, signer := range signers {
if !(types.MsgModuleOwners)(existingModuleOwnerList.GetModuleOwner()).Contains(signer) {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "account %s (%s) is not a module owner", common.BytesToAddress(signer.Bytes()), signer)
}
}
return next(ctx, tx, simulate)
}
type FeedDecorator struct {
chainLinkKeeper chainlinkkeeper.Keeper
}
func NewFeedDecorator(chainLinkKeeper chainlinkkeeper.Keeper) FeedDecorator {
return FeedDecorator{
chainLinkKeeper: chainLinkKeeper,
}
}
func (fd FeedDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
if len(tx.GetMsgs()) == 0 {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid Msg: empty Msg: %T", tx)
}
for _, msg := range tx.GetMsgs() {
switch t := msg.(type) {
case *types.MsgFeed:
feed := fd.chainLinkKeeper.GetFeed(ctx, t.GetFeedId())
if !feed.Feed.Empty() {
return ctx, sdkerrors.Wrap(sdkerrors.ErrUnknownRequest, "feed already exists")
}
// check reward schema strategy
err := feedRewardSchemaStrategyChecker(t.GetFeedReward().GetStrategy())
if err != nil {
return ctx, err
}
case *types.MsgAddDataProvider:
feed := fd.chainLinkKeeper.GetFeed(ctx, t.GetFeedId())
if feed.Feed.Empty() {
return ctx, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, ErrFeedDoesNotExist)
}
if (types.DataProviders)(feed.GetFeed().GetDataProviders()).Contains(t.GetDataProvider().GetAddress()) {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "data provider already registered")
}
signer := t.GetSigners()[0]
if !feed.GetFeed().GetFeedOwner().Equals(signer) {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, ErrSignerIsNotFeedOwner, common.BytesToAddress(signer.Bytes()), signer)
}
case *types.MsgRemoveDataProvider:
feed := fd.chainLinkKeeper.GetFeed(ctx, t.GetFeedId())
if feed.Feed.Empty() {
return ctx, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, ErrFeedDoesNotExist)
}
if !(types.DataProviders)(feed.GetFeed().GetDataProviders()).Contains(t.GetAddress()) {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "data provider not present")
}
signer := t.GetSigners()[0]
if !feed.GetFeed().GetFeedOwner().Equals(signer) {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, ErrSignerIsNotFeedOwner, common.BytesToAddress(signer.Bytes()), signer)
}
case *types.MsgSetSubmissionCount:
feed := fd.chainLinkKeeper.GetFeed(ctx, t.GetFeedId())
if feed.Feed.Empty() {
return ctx, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, ErrFeedDoesNotExist)
}
signer := t.GetSigners()[0]
if !feed.GetFeed().GetFeedOwner().Equals(signer) {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, ErrSignerIsNotFeedOwner, common.BytesToAddress(signer.Bytes()), signer)
}
case *types.MsgSetHeartbeatTrigger:
feed := fd.chainLinkKeeper.GetFeed(ctx, t.GetFeedId())
if feed.Feed.Empty() {
return ctx, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, ErrFeedDoesNotExist)
}
signer := t.GetSigners()[0]
if !feed.GetFeed().GetFeedOwner().Equals(signer) {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, ErrSignerIsNotFeedOwner, common.BytesToAddress(signer.Bytes()), signer)
}
case *types.MsgSetDeviationThresholdTrigger:
feed := fd.chainLinkKeeper.GetFeed(ctx, t.GetFeedId())
if feed.Feed.Empty() {
return ctx, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, ErrFeedDoesNotExist)
}
signer := t.GetSigners()[0]
if !feed.GetFeed().GetFeedOwner().Equals(signer) {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, ErrSignerIsNotFeedOwner, common.BytesToAddress(signer.Bytes()), signer)
}
case *types.MsgSetFeedReward:
feed := fd.chainLinkKeeper.GetFeed(ctx, t.GetFeedId())
if feed.Feed.Empty() {
return ctx, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, ErrFeedDoesNotExist)
}
signer := t.GetSigners()[0]
if !feed.GetFeed().GetFeedOwner().Equals(signer) {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, ErrSignerIsNotFeedOwner, common.BytesToAddress(signer.Bytes()), signer)
}
// check reward schema strategy
err := feedRewardSchemaStrategyChecker(t.GetFeedReward().GetStrategy())
if err != nil {
return ctx, err
}
case *types.MsgFeedOwnershipTransfer:
feed := fd.chainLinkKeeper.GetFeed(ctx, t.GetFeedId())
if feed.Feed.Empty() {
return ctx, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, ErrFeedDoesNotExist)
}
signer := t.GetSigners()[0]
if !feed.GetFeed().GetFeedOwner().Equals(signer) {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, ErrSignerIsNotFeedOwner, common.BytesToAddress(signer.Bytes()), signer)
}
case *types.MsgRequestNewRound:
feed := fd.chainLinkKeeper.GetFeed(ctx, t.GetFeedId())
if feed.Feed.Empty() {
return ctx, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, ErrFeedDoesNotExist)
}
signer := t.GetSigners()[0]
if !feed.GetFeed().GetFeedOwner().Equals(signer) {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, ErrSignerIsNotFeedOwner, common.BytesToAddress(signer.Bytes()), signer)
}
default:
continue
}
}
return next(ctx, tx, simulate)
}
type FeedDataDecorator struct {
chainLinkKeeper chainlinkkeeper.Keeper
}
func NewFeedDataDecorator(chainLinkKeeper chainlinkkeeper.Keeper) FeedDataDecorator {
return FeedDataDecorator{
chainLinkKeeper: chainLinkKeeper,
}
}
func (fd FeedDataDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
if len(tx.GetMsgs()) == 0 {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid Msg: empty Msg: %T", tx)
}
for _, msg := range tx.GetMsgs() {
switch t := msg.(type) {
case *types.MsgFeedData:
// get tx fee
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
}
txFee := feeTx.GetFee()
if len(txFee) == 0 {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidCoins, "empty tx fee coin slices")
}
// get the first coin from txFee as the tx fee charged for MsgFeedData tx
t.TxFee = &types.Coin{
Denom: txFee[0].Denom,
Amount: txFee[0].Amount.Uint64(),
}
// get feed by feedId
feed := fd.chainLinkKeeper.GetFeed(ctx, t.GetFeedId())
if feed.Feed.Empty() {
return ctx, sdkerrors.Wrap(sdkerrors.ErrKeyNotFound, "feed not exist")
}
// basic checking
if !(types.DataProviders)(feed.GetFeed().GetDataProviders()).Contains(t.GetSubmitter()) {
return ctx, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "submitter is not a valid data provider")
}
if uint32(len(t.GetObservationFeedDataSignatures())) < feed.GetFeed().GetSubmissionCount() {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "not enough signatures")
}
// observation signatures VS original observation data
if len(t.GetObservationFeedData()) != len(t.GetObservationFeedDataSignatures()) {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "number of observation signatures and observation data does not match")
}
feedDataValidationFlag := len(t.GetObservationFeedData())
for _, observation := range t.GetObservationFeedData() {
for _, observationSignature := range t.GetObservationFeedDataSignatures() {
if signaturePlainDataValidate(observationSignature, observation) {
feedDataValidationFlag--
break
}
}
}
if feedDataValidationFlag != 0 {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "observation signatures validation against observation data failed")
}
for _, pubKey := range t.GetCosmosPubKeys() {
cosmosAddr, err := types.DeriveCosmosAddrFromPubKey(string(pubKey))
if err != nil {
return ctx, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "invalid data provider: invalid cosmos pubkey")
}
dataProviderAddr, err := sdk.AccAddressFromBech32(cosmosAddr.String())
if err != nil {
return ctx, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "invalid data provider: failed to derive cosmos address, invalid cosmos pubkey")
}
// valid data provider checking
if !(types.DataProviders)(feed.GetFeed().GetDataProviders()).Contains(dataProviderAddr) {
return ctx, sdkerrors.Wrap(sdkerrors.ErrUnauthorized, "invalid data provider: data provider not in the list")
}
resp := fd.chainLinkKeeper.GetAccount(ctx, &types.GetAccountRequest{AccountAddress: dataProviderAddr})
if resp.GetAccount().GetSubmitter().String() == "" {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, ErrUnregisteredDataProvider)
}
// chainlink pubKey VS observation signature validation
chainlinkPubKeyValidationFlag := false
for _, signature := range t.GetObservationFeedDataSignatures() {
if pubKeySignatureValidate(resp.GetAccount().GetChainlinkPublicKey(), signature) {
chainlinkPubKeyValidationFlag = true
break
}
}
if !chainlinkPubKeyValidationFlag {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, "observation signature validation against chainlink pubKey failed")
}
}
default:
continue
}
}
return next(ctx, tx, simulate)
}
type ValidationDecorator struct {
validationFn func(sdk.Msg) bool
}
func NewValidationDecorator(validationFunc func(sdk.Msg) bool) ValidationDecorator {
return ValidationDecorator{
validationFn: validationFunc,
}
}
func (fd ValidationDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
if len(tx.GetMsgs()) == 0 {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid Msg: empty Msg: %T", tx)
}
for _, msg := range tx.GetMsgs() {
switch t := msg.(type) {
case *types.MsgFeedData:
t.IsFeedDataValid = t.Validate(fd.validationFn)
default:
continue
}
}
return next(ctx, tx, simulate)
}
type AccountDecorator struct {
chainLinkKeeper chainlinkkeeper.Keeper
}
func NewAccountDecorator(chainLinkKeeper chainlinkkeeper.Keeper) AccountDecorator {
return AccountDecorator{
chainLinkKeeper: chainLinkKeeper,
}
}
func (fd AccountDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (newCtx sdk.Context, err error) {
if len(tx.GetMsgs()) == 0 {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrUnknownRequest, "invalid Msg: empty Msg: %T", tx)
}
for _, msg := range tx.GetMsgs() {
switch t := msg.(type) {
case *types.MsgAccount:
// case to add a new chainlink account to the Account Store
req := &types.GetAccountRequest{AccountAddress: t.Submitter}
resp := fd.chainLinkKeeper.GetAccount(ctx, req)
if resp.Account.Submitter.String() != "" {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, ErrAccountAlreadyExists)
}
// case to edit an existing chainlink account in the Account Store
case *types.MsgEditAccount:
req := &types.GetAccountRequest{AccountAddress: t.Submitter}
// submitters must match
resp := fd.chainLinkKeeper.GetAccount(ctx, req)
if resp.Account.Submitter.String() == "" {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, ErrDoesNotExist)
}
if !bytes.Equal(t.Submitter.Bytes(), resp.Account.Submitter.Bytes()) {
return ctx, sdkerrors.Wrap(sdkerrors.ErrInvalidAddress, ErrSubmitterDoesNotMatch)
}
default:
continue
}
}
return next(ctx, tx, simulate)
}