forked from charleenfei/ica
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathibc_module.go
250 lines (225 loc) · 7.95 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
package inter_tx
import (
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
capabilitytypes "github.com/cosmos/cosmos-sdk/x/capability/types"
icatypes "github.com/cosmos/ibc-go/v3/modules/apps/27-interchain-accounts/types"
channeltypes "github.com/cosmos/ibc-go/v3/modules/core/04-channel/types"
porttypes "github.com/cosmos/ibc-go/v3/modules/core/05-port/types"
host "github.com/cosmos/ibc-go/v3/modules/core/24-host"
ibcexported "github.com/cosmos/ibc-go/v3/modules/core/exported"
"github.com/cosmos/interchain-accounts/x/inter-tx/keeper"
"github.com/CosmWasm/wasmd/x/wasm"
wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
wasmvmtypes "github.com/CosmWasm/wasmvm/types"
)
var _ porttypes.IBCModule = IBCModule{}
// IBCModule implements the ICS26 interface for interchain accounts controller chains
type IBCModule struct {
keeper keeper.Keeper
wasm wasmtypes.IBCContractKeeper
channelKeeper wasmtypes.ChannelKeeper
}
// NewIBCModule creates a new IBCModule given the keeper
func NewIBCModule(keeper keeper.Keeper, wasmKeeper wasmtypes.IBCContractKeeper, chanKeeper wasmtypes.ChannelKeeper) IBCModule {
return IBCModule{
keeper: keeper,
wasm: wasmKeeper,
channelKeeper: chanKeeper,
}
}
// OnChanOpenInit implements the IBCModule interface
func (im IBCModule) OnChanOpenInit(
ctx sdk.Context,
order channeltypes.Order,
connectionHops []string,
portID string,
channelID string,
chanCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty,
version string,
) error {
if err := wasm.ValidateChannelParams(channelID); err != nil {
return err
}
contractAddr, err := ContractFromPortID(portID)
if err != nil {
return sdkerrors.Wrapf(err, "contract port id")
}
msg := wasmvmtypes.IBCChannelOpenMsg{
OpenInit: &wasmvmtypes.IBCOpenInit{
Channel: wasmvmtypes.IBCChannel{
Endpoint: wasmvmtypes.IBCEndpoint{PortID: portID, ChannelID: channelID},
CounterpartyEndpoint: wasmvmtypes.IBCEndpoint{PortID: counterparty.PortId, ChannelID: counterparty.ChannelId},
Order: order.String(),
// DESIGN V3: this may be "" ??
Version: version,
ConnectionID: connectionHops[0], // At the moment this list must be of length 1. In the future multi-hop channels may be supported.
},
},
}
_, err = im.wasm.OnOpenChannel(ctx, contractAddr, msg)
if err != nil {
return err
}
// Claim channel capability passed back by IBC module
if err := im.keeper.ClaimCapability(ctx, chanCap, host.ChannelCapabilityPath(portID, channelID)); err != nil {
return err
}
return nil
}
// OnChanOpenTry implements the IBCModule interface
func (im IBCModule) OnChanOpenTry(
ctx sdk.Context,
order channeltypes.Order,
connectionHops []string,
portID,
channelID string,
chanCap *capabilitytypes.Capability,
counterparty channeltypes.Counterparty,
counterpartyVersion string,
) (string, error) {
// never called
return "", nil
}
// OnChanOpenAck implements the IBCModule interface
func (im IBCModule) OnChanOpenAck(
ctx sdk.Context,
portID,
channelID string,
counterpartyChannelID string,
counterpartyVersion string,
) error {
contractAddr, err := ContractFromPortID(portID)
if err != nil {
return sdkerrors.Wrapf(err, "contract port id")
}
channelInfo, ok := im.channelKeeper.GetChannel(ctx, portID, channelID)
if !ok {
return sdkerrors.Wrapf(channeltypes.ErrChannelNotFound, "port ID (%s) channel ID (%s)", portID, channelID)
}
channelInfo.Counterparty.ChannelId = counterpartyChannelID
// This is a bit ugly, but it is set AFTER the callback is done, yet we want to provide the contract
// access to the channel in queries. We can revisit how to better integrate with ibc-go in the future,
// but this is the best/safest we can do now. (If you remove this, you error when sending a packet during the
// OnChanOpenAck entry point)
// https://github.com/cosmos/ibc-go/pull/647/files#diff-54b5be375a2333c56f2ae1b5b4dc13ac9c734561e30286505f39837ee75762c7R25
im.channelKeeper.SetChannel(ctx, portID, channelID, channelInfo)
msg := wasmvmtypes.IBCChannelConnectMsg{
OpenAck: &wasmvmtypes.IBCOpenAck{
Channel: toWasmVMChannel(portID, channelID, channelInfo),
CounterpartyVersion: counterpartyVersion,
},
}
return im.wasm.OnConnectChannel(ctx, contractAddr, msg)
}
// OnChanOpenConfirm implements the IBCModule interface
func (im IBCModule) OnChanOpenConfirm(
ctx sdk.Context,
portID,
channelID string,
) error {
// never called
return nil
}
// OnChanCloseInit implements the IBCModule interface
func (im IBCModule) OnChanCloseInit(
ctx sdk.Context,
portID,
channelID string,
) error {
// never called
return nil
}
// OnChanCloseConfirm implements the IBCModule interface
func (im IBCModule) OnChanCloseConfirm(
ctx sdk.Context,
portID,
channelID string,
) error {
// never called
return nil
}
// OnRecvPacket implements the IBCModule interface. A successful acknowledgement
// is returned if the packet data is succesfully decoded and the receive application
// logic returns without error.
func (im IBCModule) OnRecvPacket(
ctx sdk.Context,
packet channeltypes.Packet,
relayer sdk.AccAddress,
) ibcexported.Acknowledgement {
// never called
return channeltypes.NewErrorAcknowledgement("cannot receive packet via interchain accounts authentication module")
}
// OnAcknowledgementPacket implements the IBCModule interface
func (im IBCModule) OnAcknowledgementPacket(
ctx sdk.Context,
packet channeltypes.Packet,
acknowledgement []byte,
relayer sdk.AccAddress,
) error {
contractAddr, err := ContractFromPortID(packet.SourcePort)
if err != nil {
return sdkerrors.Wrapf(err, "contract port id")
}
err = im.wasm.OnAckPacket(ctx, contractAddr, wasmvmtypes.IBCPacketAckMsg{
Acknowledgement: wasmvmtypes.IBCAcknowledgement{Data: acknowledgement},
OriginalPacket: newIBCPacket(packet),
Relayer: relayer.String(),
})
if err != nil {
return sdkerrors.Wrap(err, "on ack")
}
return nil
}
// OnTimeoutPacket implements the IBCModule interface.
func (im IBCModule) OnTimeoutPacket(
ctx sdk.Context,
packet channeltypes.Packet,
relayer sdk.AccAddress,
) error {
contractAddr, err := ContractFromPortID(packet.SourcePort)
if err != nil {
return sdkerrors.Wrapf(err, "contract port id")
}
msg := wasmvmtypes.IBCPacketTimeoutMsg{Packet: newIBCPacket(packet), Relayer: relayer.String()}
err = im.wasm.OnTimeoutPacket(ctx, contractAddr, msg)
if err != nil {
return sdkerrors.Wrap(err, "on timeout")
}
return nil
}
func ContractFromPortID(portID string) (sdk.AccAddress, error) {
if !strings.HasPrefix(portID, icatypes.PortPrefix) {
return nil, sdkerrors.Wrapf(wasmtypes.ErrInvalid, "without prefix")
}
return sdk.AccAddressFromBech32(portID[len(icatypes.PortPrefix):])
}
func newIBCPacket(packet channeltypes.Packet) wasmvmtypes.IBCPacket {
timeout := wasmvmtypes.IBCTimeout{
Timestamp: packet.TimeoutTimestamp,
}
if !packet.TimeoutHeight.IsZero() {
timeout.Block = &wasmvmtypes.IBCTimeoutBlock{
Height: packet.TimeoutHeight.RevisionHeight,
Revision: packet.TimeoutHeight.RevisionNumber,
}
}
return wasmvmtypes.IBCPacket{
Data: packet.Data,
Src: wasmvmtypes.IBCEndpoint{ChannelID: packet.SourceChannel, PortID: packet.SourcePort},
Dest: wasmvmtypes.IBCEndpoint{ChannelID: packet.DestinationChannel, PortID: packet.DestinationPort},
Sequence: packet.Sequence,
Timeout: timeout,
}
}
func toWasmVMChannel(portID, channelID string, channelInfo channeltypes.Channel) wasmvmtypes.IBCChannel {
return wasmvmtypes.IBCChannel{
Endpoint: wasmvmtypes.IBCEndpoint{PortID: portID, ChannelID: channelID},
CounterpartyEndpoint: wasmvmtypes.IBCEndpoint{PortID: channelInfo.Counterparty.PortId, ChannelID: channelInfo.Counterparty.ChannelId},
Order: channelInfo.Ordering.String(),
Version: channelInfo.Version,
ConnectionID: channelInfo.ConnectionHops[0], // At the moment this list must be of length 1. In the future multi-hop channels may be supported.
}
}