-
Notifications
You must be signed in to change notification settings - Fork 109
/
check_refund.go
113 lines (95 loc) · 3.3 KB
/
check_refund.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
package ante
import (
"fmt"
rewardtypes "github.com/axelarnetwork/axelar-core/x/reward/types"
cdctypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
antetypes "github.com/cosmos/cosmos-sdk/x/auth/ante"
"github.com/axelarnetwork/axelar-core/x/ante/types"
)
// CheckRefundFeeDecorator record potential refund for tss and vote txs
type CheckRefundFeeDecorator struct {
registry cdctypes.InterfaceRegistry
ak antetypes.AccountKeeper
staking types.Staking
reward types.Reward
snapshotter types.Snapshotter
}
// NewCheckRefundFeeDecorator constructor for CheckRefundFeeDecorator
func NewCheckRefundFeeDecorator(registry cdctypes.InterfaceRegistry, ak antetypes.AccountKeeper, staking types.Staking, snapshotter types.Snapshotter, reward types.Reward) CheckRefundFeeDecorator {
return CheckRefundFeeDecorator{
registry,
ak,
staking,
reward,
snapshotter,
}
}
// AnteHandle record qualified refund for the tss and vote transactions
func (d CheckRefundFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
msgs := tx.GetMsgs()
if !anyRefundable(msgs) {
return next(ctx, tx, simulate)
}
if err := d.validateRefundQualification(ctx, msgs); err != nil {
return ctx, err
}
feeTx, ok := tx.(sdk.FeeTx)
if !ok {
return ctx, sdkerrors.Wrap(sdkerrors.ErrTxDecode, "tx must be a FeeTx")
}
fee := feeTx.GetFee()
if len(fee) > 0 {
req := msgs[0].(*rewardtypes.RefundMsgRequest)
err := d.reward.SetPendingRefund(ctx, *req, fee[0])
if err != nil {
return ctx, sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, err.Error())
}
}
return next(ctx, tx, simulate)
}
func anyRefundable(msgs []sdk.Msg) bool {
if len(msgs) == 0 {
return false
}
for _, msg := range msgs {
switch msg.(type) {
case *rewardtypes.RefundMsgRequest:
return true
}
}
return false
}
func (d CheckRefundFeeDecorator) validateRefundQualification(ctx sdk.Context, msgs []sdk.Msg) error {
// If we allow txs to be refunded when there are msgs that are not RefundMsgRequests we open the door to slip all kinds of msgs in to get them refunded.
// So we need to make sure that all msgs in the batch are refundable, otherwise reject the tx.
for _, msg := range msgs {
switch msg := msg.(type) {
case *rewardtypes.RefundMsgRequest:
if !msgRegistered(d.registry, msg.InnerMessage.TypeUrl) {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, fmt.Sprintf("message type %s is not refundable", msg.InnerMessage.TypeUrl))
}
sender := msg.GetSigners()[0]
operatorAddr := d.snapshotter.GetOperator(ctx, sender)
if operatorAddr == nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "signer is not a registered proxy")
}
validator := d.staking.Validator(ctx, operatorAddr)
if validator == nil {
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, "signer is not associated with a validator")
}
default:
return sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, fmt.Sprintf("message type %T is not refundable", msg))
}
}
return nil
}
func msgRegistered(r cdctypes.InterfaceRegistry, targetURL string) bool {
for _, url := range r.ListImplementations("reward.v1beta1.Refundable") {
if targetURL == url {
return true
}
}
return false
}