-
Notifications
You must be signed in to change notification settings - Fork 625
/
msg_server.go
168 lines (127 loc) · 6.62 KB
/
msg_server.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
package keeper
import (
"context"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/ibc-go/v4/modules/apps/29-fee/types"
channeltypes "github.com/cosmos/ibc-go/v4/modules/core/04-channel/types"
)
var _ types.MsgServer = Keeper{}
// RegisterPayee defines a rpc handler method for MsgRegisterPayee
// RegisterPayee is called by the relayer on each channelEnd and allows them to set an optional
// payee to which reverse and timeout relayer packet fees will be paid out. The payee should be registered on
// the source chain from which packets originate as this is where fee distribution takes place. This function may be
// called more than once by a relayer, in which case, the latest payee is always used.
func (k Keeper) RegisterPayee(goCtx context.Context, msg *types.MsgRegisterPayee) (*types.MsgRegisterPayeeResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
payee, err := sdk.AccAddressFromBech32(msg.Payee)
if err != nil {
return nil, err
}
if k.bankKeeper.BlockedAddr(payee) {
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "%s is not authorized to be a payee", payee)
}
// only register payee address if the channel exists and is fee enabled
if _, found := k.channelKeeper.GetChannel(ctx, msg.PortId, msg.ChannelId); !found {
return nil, channeltypes.ErrChannelNotFound
}
if !k.IsFeeEnabled(ctx, msg.PortId, msg.ChannelId) {
return nil, types.ErrFeeNotEnabled
}
k.SetPayeeAddress(ctx, msg.Relayer, msg.Payee, msg.ChannelId)
k.Logger(ctx).Info("registering payee address for relayer", "relayer", msg.Relayer, "payee", msg.Payee, "channel", msg.ChannelId)
EmitRegisterPayeeEvent(ctx, msg.Relayer, msg.Payee, msg.ChannelId)
return &types.MsgRegisterPayeeResponse{}, nil
}
// RegisterCounterpartyPayee defines a rpc handler method for MsgRegisterCounterpartyPayee
// RegisterCounterpartyPayee is called by the relayer on each channelEnd and allows them to specify the counterparty
// payee address before relaying. This ensures they will be properly compensated for forward relaying since
// the destination chain must include the registered counterparty payee address in the acknowledgement. This function
// may be called more than once by a relayer, in which case, the latest counterparty payee address is always used.
func (k Keeper) RegisterCounterpartyPayee(goCtx context.Context, msg *types.MsgRegisterCounterpartyPayee) (*types.MsgRegisterCounterpartyPayeeResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
// only register counterparty payee if the channel exists and is fee enabled
if _, found := k.channelKeeper.GetChannel(ctx, msg.PortId, msg.ChannelId); !found {
return nil, channeltypes.ErrChannelNotFound
}
if !k.IsFeeEnabled(ctx, msg.PortId, msg.ChannelId) {
return nil, types.ErrFeeNotEnabled
}
k.SetCounterpartyPayeeAddress(ctx, msg.Relayer, msg.CounterpartyPayee, msg.ChannelId)
k.Logger(ctx).Info("registering counterparty payee for relayer", "relayer", msg.Relayer, "counterparty payee", msg.CounterpartyPayee, "channel", msg.ChannelId)
EmitRegisterCounterpartyPayeeEvent(ctx, msg.Relayer, msg.CounterpartyPayee, msg.ChannelId)
return &types.MsgRegisterCounterpartyPayeeResponse{}, nil
}
// PayPacketFee defines a rpc handler method for MsgPayPacketFee
// PayPacketFee is an open callback that may be called by any module/user that wishes to escrow funds in order to relay the packet with the next sequence
func (k Keeper) PayPacketFee(goCtx context.Context, msg *types.MsgPayPacketFee) (*types.MsgPayPacketFeeResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if !k.IsFeeEnabled(ctx, msg.SourcePortId, msg.SourceChannelId) {
// users may not escrow fees on this channel. Must send packets without a fee message
return nil, types.ErrFeeNotEnabled
}
if k.IsLocked(ctx) {
return nil, types.ErrFeeModuleLocked
}
refundAcc, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return nil, err
}
if err := k.bankKeeper.IsSendEnabledCoins(ctx, msg.Fee.Total()...); err != nil {
return nil, err
}
if k.bankKeeper.BlockedAddr(refundAcc) {
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to escrow fees", refundAcc)
}
// get the next sequence
sequence, found := k.GetNextSequenceSend(ctx, msg.SourcePortId, msg.SourceChannelId)
if !found {
return nil, channeltypes.ErrSequenceSendNotFound
}
packetID := channeltypes.NewPacketId(msg.SourcePortId, msg.SourceChannelId, sequence)
packetFee := types.NewPacketFee(msg.Fee, msg.Signer, msg.Relayers)
if err := k.escrowPacketFee(ctx, packetID, packetFee); err != nil {
return nil, err
}
return &types.MsgPayPacketFeeResponse{}, nil
}
// PayPacketFee defines a rpc handler method for MsgPayPacketFee
// PayPacketFee is an open callback that may be called by any module/user that wishes to escrow funds in order to
// incentivize the relaying of a known packet. Only packets which have been sent and have not gone through the
// packet life cycle may be incentivized.
func (k Keeper) PayPacketFeeAsync(goCtx context.Context, msg *types.MsgPayPacketFeeAsync) (*types.MsgPayPacketFeeAsyncResponse, error) {
ctx := sdk.UnwrapSDKContext(goCtx)
if !k.IsFeeEnabled(ctx, msg.PacketId.PortId, msg.PacketId.ChannelId) {
// users may not escrow fees on this channel. Must send packets without a fee message
return nil, types.ErrFeeNotEnabled
}
if k.IsLocked(ctx) {
return nil, types.ErrFeeModuleLocked
}
refundAcc, err := sdk.AccAddressFromBech32(msg.PacketFee.RefundAddress)
if err != nil {
return nil, err
}
if err := k.bankKeeper.IsSendEnabledCoins(ctx, msg.PacketFee.Fee.Total()...); err != nil {
return nil, err
}
if k.bankKeeper.BlockedAddr(refundAcc) {
return nil, sdkerrors.Wrapf(sdkerrors.ErrUnauthorized, "%s is not allowed to escrow fees", refundAcc)
}
nextSeqSend, found := k.GetNextSequenceSend(ctx, msg.PacketId.PortId, msg.PacketId.ChannelId)
if !found {
return nil, sdkerrors.Wrapf(channeltypes.ErrSequenceSendNotFound, "channel does not exist, portID: %s, channelID: %s", msg.PacketId.PortId, msg.PacketId.ChannelId)
}
// only allow incentivizing of packets which have been sent
if msg.PacketId.Sequence >= nextSeqSend {
return nil, channeltypes.ErrPacketNotSent
}
// only allow incentivizng of packets which have not completed the packet life cycle
if bz := k.GetPacketCommitment(ctx, msg.PacketId.PortId, msg.PacketId.ChannelId, msg.PacketId.Sequence); len(bz) == 0 {
return nil, sdkerrors.Wrapf(channeltypes.ErrPacketCommitmentNotFound, "packet has already been acknowledged or timed out")
}
if err := k.escrowPacketFee(ctx, msg.PacketId, msg.PacketFee); err != nil {
return nil, err
}
return &types.MsgPayPacketFeeAsyncResponse{}, nil
}