-
Notifications
You must be signed in to change notification settings - Fork 580
/
msgs.go
188 lines (153 loc) · 5.25 KB
/
msgs.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
package types
import (
"strings"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
host "github.com/cosmos/ibc-go/v8/modules/core/24-host"
ibcerrors "github.com/cosmos/ibc-go/v8/modules/core/errors"
)
const MaximumCounterpartyPayeeLength = 2048 // maximum length of the counterparty payee in bytes (value chosen arbitrarily)
var (
_ sdk.Msg = (*MsgRegisterPayee)(nil)
_ sdk.Msg = (*MsgRegisterCounterpartyPayee)(nil)
_ sdk.Msg = (*MsgPayPacketFee)(nil)
_ sdk.Msg = (*MsgPayPacketFeeAsync)(nil)
_ sdk.HasValidateBasic = (*MsgRegisterPayee)(nil)
_ sdk.HasValidateBasic = (*MsgRegisterCounterpartyPayee)(nil)
_ sdk.HasValidateBasic = (*MsgPayPacketFee)(nil)
_ sdk.HasValidateBasic = (*MsgPayPacketFeeAsync)(nil)
)
// NewMsgRegisterPayee creates a new instance of MsgRegisterPayee
func NewMsgRegisterPayee(portID, channelID, relayerAddr, payeeAddr string) *MsgRegisterPayee {
return &MsgRegisterPayee{
PortId: portID,
ChannelId: channelID,
Relayer: relayerAddr,
Payee: payeeAddr,
}
}
// ValidateBasic implements sdk.Msg and performs basic stateless validation
func (msg MsgRegisterPayee) ValidateBasic() error {
if err := host.PortIdentifierValidator(msg.PortId); err != nil {
return err
}
if err := host.ChannelIdentifierValidator(msg.ChannelId); err != nil {
return err
}
_, err := sdk.AccAddressFromBech32(msg.Relayer)
if err != nil {
return errorsmod.Wrap(err, "failed to create sdk.AccAddress from relayer address")
}
_, err = sdk.AccAddressFromBech32(msg.Payee)
if err != nil {
return errorsmod.Wrap(err, "failed to create sdk.AccAddress from payee address")
}
return nil
}
// GetSigners implements sdk.Msg
func (msg MsgRegisterPayee) GetSigners() []sdk.AccAddress {
signer, err := sdk.AccAddressFromBech32(msg.Relayer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{signer}
}
// NewMsgRegisterCounterpartyPayee creates a new instance of MsgRegisterCounterpartyPayee
func NewMsgRegisterCounterpartyPayee(portID, channelID, relayerAddr, counterpartyPayeeAddr string) *MsgRegisterCounterpartyPayee {
return &MsgRegisterCounterpartyPayee{
PortId: portID,
ChannelId: channelID,
Relayer: relayerAddr,
CounterpartyPayee: counterpartyPayeeAddr,
}
}
// ValidateBasic performs a basic check of the MsgRegisterCounterpartyAddress fields
func (msg MsgRegisterCounterpartyPayee) ValidateBasic() error {
if err := host.PortIdentifierValidator(msg.PortId); err != nil {
return err
}
if err := host.ChannelIdentifierValidator(msg.ChannelId); err != nil {
return err
}
_, err := sdk.AccAddressFromBech32(msg.Relayer)
if err != nil {
return errorsmod.Wrap(err, "failed to create sdk.AccAddress from relayer address")
}
if strings.TrimSpace(msg.CounterpartyPayee) == "" {
return ErrCounterpartyPayeeEmpty
}
if len(msg.CounterpartyPayee) > MaximumCounterpartyPayeeLength {
return errorsmod.Wrapf(ibcerrors.ErrInvalidAddress, "counterparty payee address must not exceed %d bytes", MaximumCounterpartyPayeeLength)
}
return nil
}
// GetSigners implements sdk.Msg
func (msg MsgRegisterCounterpartyPayee) GetSigners() []sdk.AccAddress {
signer, err := sdk.AccAddressFromBech32(msg.Relayer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{signer}
}
// NewMsgPayPacketFee creates a new instance of MsgPayPacketFee
func NewMsgPayPacketFee(fee Fee, sourcePortID, sourceChannelID, signer string, relayers []string) *MsgPayPacketFee {
return &MsgPayPacketFee{
Fee: fee,
SourcePortId: sourcePortID,
SourceChannelId: sourceChannelID,
Signer: signer,
Relayers: relayers,
}
}
// ValidateBasic performs a basic check of the MsgPayPacketFee fields
func (msg MsgPayPacketFee) ValidateBasic() error {
// validate channelId
if err := host.ChannelIdentifierValidator(msg.SourceChannelId); err != nil {
return err
}
// validate portId
if err := host.PortIdentifierValidator(msg.SourcePortId); err != nil {
return err
}
// signer check
if _, err := sdk.AccAddressFromBech32(msg.Signer); err != nil {
return errorsmod.Wrap(err, "failed to convert msg.Signer into sdk.AccAddress")
}
// enforce relayer is not set
if len(msg.Relayers) != 0 {
return ErrRelayersNotEmpty
}
return msg.Fee.Validate()
}
// GetSigners implements sdk.Msg
func (msg MsgPayPacketFee) GetSigners() []sdk.AccAddress {
signer, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
return []sdk.AccAddress{signer}
}
// NewMsgPayPacketAsync creates a new instance of MsgPayPacketFee
func NewMsgPayPacketFeeAsync(packetID channeltypes.PacketId, packetFee PacketFee) *MsgPayPacketFeeAsync {
return &MsgPayPacketFeeAsync{
PacketId: packetID,
PacketFee: packetFee,
}
}
// ValidateBasic performs a basic check of the MsgPayPacketFeeAsync fields
func (msg MsgPayPacketFeeAsync) ValidateBasic() error {
if err := msg.PacketId.Validate(); err != nil {
return err
}
return msg.PacketFee.Validate()
}
// GetSigners implements sdk.Msg
// The signer of the fee message must be the refund address
func (msg MsgPayPacketFeeAsync) GetSigners() []sdk.AccAddress {
signer, err := sdk.AccAddressFromBech32(msg.PacketFee.RefundAddress)
if err != nil {
panic(err)
}
return []sdk.AccAddress{signer}
}