-
Notifications
You must be signed in to change notification settings - Fork 63
/
ibc_module.go
262 lines (223 loc) · 8.88 KB
/
ibc_module.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
package ibc_hooks
import (
// external libraries
sdk "github.com/cosmos/cosmos-sdk/types"
capabilitytypes "github.com/cosmos/ibc-go/modules/capability/types"
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
// ibc-go
channeltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v8/modules/core/05-port/types"
ibcexported "github.com/cosmos/ibc-go/v8/modules/core/exported"
)
var _ porttypes.Middleware = &IBCMiddleware{}
type IBCMiddleware struct {
App porttypes.IBCModule
ICS4Middleware *ICS4Middleware
}
func NewIBCMiddleware(app porttypes.IBCModule, ics4 *ICS4Middleware) IBCMiddleware {
return IBCMiddleware{
App: app,
ICS4Middleware: ics4,
}
}
// OnChanOpenInit implements the IBCMiddleware interface
func (im IBCMiddleware) OnChanOpenInit(
ctx sdk.Context,
order channeltypes.Order,
connectionHops []string,
portID string,
channelID string,
channelCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty,
version string,
) (string, error) {
if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenInitOverrideHooks); ok {
return hook.OnChanOpenInitOverride(im, ctx, order, connectionHops, portID, channelID, channelCap, counterparty, version)
}
if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenInitBeforeHooks); ok {
hook.OnChanOpenInitBeforeHook(ctx, order, connectionHops, portID, channelID, channelCap, counterparty, version)
}
finalVersion, err := im.App.OnChanOpenInit(ctx, order, connectionHops, portID, channelID, channelCap, counterparty, version)
if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenInitAfterHooks); ok {
hook.OnChanOpenInitAfterHook(ctx, order, connectionHops, portID, channelID, channelCap, counterparty, version, finalVersion, err)
}
return version, err
}
// OnChanOpenTry implements the IBCMiddleware interface
func (im IBCMiddleware) OnChanOpenTry(
ctx sdk.Context,
order channeltypes.Order,
connectionHops []string,
portID,
channelID string,
channelCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty,
counterpartyVersion string,
) (string, error) {
if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenTryOverrideHooks); ok {
return hook.OnChanOpenTryOverride(im, ctx, order, connectionHops, portID, channelID, channelCap, counterparty, counterpartyVersion)
}
if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenTryBeforeHooks); ok {
hook.OnChanOpenTryBeforeHook(ctx, order, connectionHops, portID, channelID, channelCap, counterparty, counterpartyVersion)
}
version, err := im.App.OnChanOpenTry(ctx, order, connectionHops, portID, channelID, channelCap, counterparty, counterpartyVersion)
if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenTryAfterHooks); ok {
hook.OnChanOpenTryAfterHook(ctx, order, connectionHops, portID, channelID, channelCap, counterparty, counterpartyVersion, version, err)
}
return version, err
}
// OnChanOpenAck implements the IBCMiddleware interface
func (im IBCMiddleware) OnChanOpenAck(
ctx sdk.Context,
portID,
channelID string,
counterpartyChannelID string,
counterpartyVersion string,
) error {
if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenAckOverrideHooks); ok {
return hook.OnChanOpenAckOverride(im, ctx, portID, channelID, counterpartyChannelID, counterpartyVersion)
}
if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenAckBeforeHooks); ok {
hook.OnChanOpenAckBeforeHook(ctx, portID, channelID, counterpartyChannelID, counterpartyVersion)
}
err := im.App.OnChanOpenAck(ctx, portID, channelID, counterpartyChannelID, counterpartyVersion)
if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenAckAfterHooks); ok {
hook.OnChanOpenAckAfterHook(ctx, portID, channelID, counterpartyChannelID, counterpartyVersion, err)
}
return err
}
// OnChanOpenConfirm implements the IBCMiddleware interface
func (im IBCMiddleware) OnChanOpenConfirm(
ctx sdk.Context,
portID,
channelID string,
) error {
if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenConfirmOverrideHooks); ok {
return hook.OnChanOpenConfirmOverride(im, ctx, portID, channelID)
}
if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenConfirmBeforeHooks); ok {
hook.OnChanOpenConfirmBeforeHook(ctx, portID, channelID)
}
err := im.App.OnChanOpenConfirm(ctx, portID, channelID)
if hook, ok := im.ICS4Middleware.Hooks.(OnChanOpenConfirmAfterHooks); ok {
hook.OnChanOpenConfirmAfterHook(ctx, portID, channelID, err)
}
return err
}
// OnChanCloseInit implements the IBCMiddleware interface
func (im IBCMiddleware) OnChanCloseInit(
ctx sdk.Context,
portID,
channelID string,
) error {
// Here we can remove the limits when a new channel is closed. For now, they can remove them manually on the contract
if hook, ok := im.ICS4Middleware.Hooks.(OnChanCloseInitOverrideHooks); ok {
return hook.OnChanCloseInitOverride(im, ctx, portID, channelID)
}
if hook, ok := im.ICS4Middleware.Hooks.(OnChanCloseInitBeforeHooks); ok {
hook.OnChanCloseInitBeforeHook(ctx, portID, channelID)
}
err := im.App.OnChanCloseInit(ctx, portID, channelID)
if hook, ok := im.ICS4Middleware.Hooks.(OnChanCloseInitAfterHooks); ok {
hook.OnChanCloseInitAfterHook(ctx, portID, channelID, err)
}
return err
}
// OnChanCloseConfirm implements the IBCMiddleware interface
func (im IBCMiddleware) OnChanCloseConfirm(
ctx sdk.Context,
portID,
channelID string,
) error {
// Here we can remove the limits when a new channel is closed. For now, they can remove them manually on the contract
if hook, ok := im.ICS4Middleware.Hooks.(OnChanCloseConfirmOverrideHooks); ok {
return hook.OnChanCloseConfirmOverride(im, ctx, portID, channelID)
}
if hook, ok := im.ICS4Middleware.Hooks.(OnChanCloseConfirmBeforeHooks); ok {
hook.OnChanCloseConfirmBeforeHook(ctx, portID, channelID)
}
err := im.App.OnChanCloseConfirm(ctx, portID, channelID)
if hook, ok := im.ICS4Middleware.Hooks.(OnChanCloseConfirmAfterHooks); ok {
hook.OnChanCloseConfirmAfterHook(ctx, portID, channelID, err)
}
return err
}
// OnRecvPacket implements the IBCMiddleware interface
func (im IBCMiddleware) OnRecvPacket(
ctx sdk.Context,
packet channeltypes.Packet,
relayer sdk.AccAddress,
) ibcexported.Acknowledgement {
if hook, ok := im.ICS4Middleware.Hooks.(OnRecvPacketOverrideHooks); ok {
return hook.OnRecvPacketOverride(im, ctx, packet, relayer)
}
if hook, ok := im.ICS4Middleware.Hooks.(OnRecvPacketBeforeHooks); ok {
hook.OnRecvPacketBeforeHook(ctx, packet, relayer)
}
ack := im.App.OnRecvPacket(ctx, packet, relayer)
if hook, ok := im.ICS4Middleware.Hooks.(OnRecvPacketAfterHooks); ok {
hook.OnRecvPacketAfterHook(ctx, packet, relayer, ack)
}
return ack
}
// OnAcknowledgementPacket implements the IBCMiddleware interface
func (im IBCMiddleware) OnAcknowledgementPacket(
ctx sdk.Context,
packet channeltypes.Packet,
acknowledgement []byte,
relayer sdk.AccAddress,
) error {
if hook, ok := im.ICS4Middleware.Hooks.(OnAcknowledgementPacketOverrideHooks); ok {
return hook.OnAcknowledgementPacketOverride(im, ctx, packet, acknowledgement, relayer)
}
if hook, ok := im.ICS4Middleware.Hooks.(OnAcknowledgementPacketBeforeHooks); ok {
hook.OnAcknowledgementPacketBeforeHook(ctx, packet, acknowledgement, relayer)
}
err := im.App.OnAcknowledgementPacket(ctx, packet, acknowledgement, relayer)
if hook, ok := im.ICS4Middleware.Hooks.(OnAcknowledgementPacketAfterHooks); ok {
hook.OnAcknowledgementPacketAfterHook(ctx, packet, acknowledgement, relayer, err)
}
return err
}
// OnTimeoutPacket implements the IBCMiddleware interface
func (im IBCMiddleware) OnTimeoutPacket(
ctx sdk.Context,
packet channeltypes.Packet,
relayer sdk.AccAddress,
) error {
if hook, ok := im.ICS4Middleware.Hooks.(OnTimeoutPacketOverrideHooks); ok {
return hook.OnTimeoutPacketOverride(im, ctx, packet, relayer)
}
if hook, ok := im.ICS4Middleware.Hooks.(OnTimeoutPacketBeforeHooks); ok {
hook.OnTimeoutPacketBeforeHook(ctx, packet, relayer)
}
err := im.App.OnTimeoutPacket(ctx, packet, relayer)
if hook, ok := im.ICS4Middleware.Hooks.(OnTimeoutPacketAfterHooks); ok {
hook.OnTimeoutPacketAfterHook(ctx, packet, relayer, err)
}
return err
}
// SendPacket implements the ICS4 Wrapper interface
func (im IBCMiddleware) SendPacket(
ctx sdk.Context,
chanCap *capabilitytypes.Capability,
sourcePort string,
sourceChannel string,
timeoutHeight ibcclienttypes.Height,
timeoutTimestamp uint64,
data []byte,
) (sequence uint64, err error) {
return im.ICS4Middleware.SendPacket(ctx, chanCap, sourcePort, sourceChannel, timeoutHeight, timeoutTimestamp, data)
}
// WriteAcknowledgement implements the ICS4 Wrapper interface
func (im IBCMiddleware) WriteAcknowledgement(
ctx sdk.Context,
chanCap *capabilitytypes.Capability,
packet ibcexported.PacketI,
ack ibcexported.Acknowledgement,
) error {
return im.ICS4Middleware.WriteAcknowledgement(ctx, chanCap, packet, ack)
}
func (im IBCMiddleware) GetAppVersion(ctx sdk.Context, portID, channelID string) (string, bool) {
return im.ICS4Middleware.GetAppVersion(ctx, portID, channelID)
}