-
Notifications
You must be signed in to change notification settings - Fork 30
/
pathEnd.go
271 lines (249 loc) · 7.39 KB
/
pathEnd.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
263
264
265
266
267
268
269
270
271
package core
import (
"strings"
sdk "github.com/cosmos/cosmos-sdk/types"
transfertypes "github.com/cosmos/ibc-go/v7/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v7/modules/core/02-client/types"
conntypes "github.com/cosmos/ibc-go/v7/modules/core/03-connection/types"
chantypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
commitmenttypes "github.com/cosmos/ibc-go/v7/modules/core/23-commitment/types"
)
var (
defaultChainPrefix = commitmenttypes.NewMerklePrefix([]byte("ibc"))
)
const (
// TODO make it to be configurable
DefaultDelayPeriod uint64 = 0
)
// PathEnd represents the local connection identifers for a relay path
// The path is set on the chain before performing operations
type PathEnd struct {
ChainID string `yaml:"chain-id,omitempty" json:"chain-id,omitempty"`
ClientID string `yaml:"client-id,omitempty" json:"client-id,omitempty"`
ConnectionID string `yaml:"connection-id,omitempty" json:"connection-id,omitempty"`
ChannelID string `yaml:"channel-id,omitempty" json:"channel-id,omitempty"`
PortID string `yaml:"port-id,omitempty" json:"port-id,omitempty"`
Order string `yaml:"order,omitempty" json:"order,omitempty"`
Version string `yaml:"version,omitempty" json:"version,omitempty"`
}
// OrderFromString parses a string into a channel order byte
func OrderFromString(order string) chantypes.Order {
switch order {
case "UNORDERED":
return chantypes.UNORDERED
case "ORDERED":
return chantypes.ORDERED
default:
return chantypes.NONE
}
}
func (pe *PathEnd) GetOrder() chantypes.Order {
return OrderFromString(strings.ToUpper(pe.Order))
}
// UpdateClient creates an sdk.Msg to update the client on src with data pulled from dst
func (pe *PathEnd) UpdateClient(dstHeader Header, signer sdk.AccAddress) sdk.Msg {
if err := dstHeader.ValidateBasic(); err != nil {
panic(err)
}
msg, err := clienttypes.NewMsgUpdateClient(
pe.ClientID,
dstHeader,
signer.String(),
)
if err != nil {
panic(err)
}
return msg
}
func (pe *PathEnd) UpdateClients(dstHeaders []Header, signer sdk.AccAddress) []sdk.Msg {
var msgs []sdk.Msg
for _, header := range dstHeaders {
msgs = append(msgs, pe.UpdateClient(header, signer))
}
return msgs
}
// ConnInit creates a MsgConnectionOpenInit
func (pe *PathEnd) ConnInit(dst *PathEnd, signer sdk.AccAddress) sdk.Msg {
var version *conntypes.Version
return conntypes.NewMsgConnectionOpenInit(
pe.ClientID,
dst.ClientID,
defaultChainPrefix,
version,
DefaultDelayPeriod,
signer.String(),
)
}
// ConnTry creates a MsgConnectionOpenTry
// NOTE: ADD NOTE ABOUT PROOF HEIGHT CHANGE HERE
func (pe *PathEnd) ConnTry(
dst *PathEnd,
dstClientState *clienttypes.QueryClientStateResponse,
dstConnState *conntypes.QueryConnectionResponse,
dstConsState *clienttypes.QueryConsensusStateResponse,
hostConsensusStateProof []byte,
signer sdk.AccAddress,
) sdk.Msg {
cs, err := clienttypes.UnpackClientState(dstClientState.ClientState)
if err != nil {
panic(err)
}
msg := conntypes.NewMsgConnectionOpenTry(
pe.ClientID,
dst.ConnectionID,
dst.ClientID,
cs,
defaultChainPrefix,
conntypes.ExportedVersionsToProto(conntypes.GetCompatibleVersions()),
DefaultDelayPeriod,
dstConnState.Proof,
dstClientState.Proof,
dstConsState.Proof,
dstConnState.ProofHeight,
cs.GetLatestHeight().(clienttypes.Height),
signer.String(),
)
msg.HostConsensusStateProof = hostConsensusStateProof
if err = msg.ValidateBasic(); err != nil {
panic(err)
}
return msg
}
// ConnAck creates a MsgConnectionOpenAck
// NOTE: ADD NOTE ABOUT PROOF HEIGHT CHANGE HERE
func (pe *PathEnd) ConnAck(
dst *PathEnd,
dstClientState *clienttypes.QueryClientStateResponse,
dstConnState *conntypes.QueryConnectionResponse,
dstConsState *clienttypes.QueryConsensusStateResponse,
hostConsensusStateProof []byte,
signer sdk.AccAddress,
) sdk.Msg {
cs, err := clienttypes.UnpackClientState(dstClientState.ClientState)
if err != nil {
panic(err)
}
msg := conntypes.NewMsgConnectionOpenAck(
pe.ConnectionID,
dst.ConnectionID,
cs,
dstConnState.Proof,
dstClientState.Proof,
dstConsState.Proof,
dstConsState.ProofHeight,
cs.GetLatestHeight().(clienttypes.Height),
conntypes.ExportedVersionsToProto(conntypes.GetCompatibleVersions())[0],
signer.String(),
)
msg.HostConsensusStateProof = hostConsensusStateProof
if err = msg.ValidateBasic(); err != nil {
panic(err)
}
return msg
}
// ConnConfirm creates a MsgConnectionOpenAck
// NOTE: ADD NOTE ABOUT PROOF HEIGHT CHANGE HERE
func (pe *PathEnd) ConnConfirm(dstConnState *conntypes.QueryConnectionResponse, signer sdk.AccAddress) sdk.Msg {
return conntypes.NewMsgConnectionOpenConfirm(
pe.ConnectionID,
dstConnState.Proof,
dstConnState.ProofHeight,
signer.String(),
)
}
// ChanInit creates a MsgChannelOpenInit
func (pe *PathEnd) ChanInit(dst *PathEnd, signer sdk.AccAddress) sdk.Msg {
return chantypes.NewMsgChannelOpenInit(
pe.PortID,
pe.Version,
pe.GetOrder(),
[]string{pe.ConnectionID},
dst.PortID,
signer.String(),
)
}
// ChanTry creates a MsgChannelOpenTry
func (pe *PathEnd) ChanTry(dst *PathEnd, dstChanState *chantypes.QueryChannelResponse, signer sdk.AccAddress) sdk.Msg {
return chantypes.NewMsgChannelOpenTry(
pe.PortID,
pe.Version,
dstChanState.Channel.Ordering,
[]string{pe.ConnectionID},
dst.PortID,
dst.ChannelID,
dstChanState.Channel.Version,
dstChanState.Proof,
dstChanState.ProofHeight,
signer.String(),
)
}
// ChanAck creates a MsgChannelOpenAck
func (pe *PathEnd) ChanAck(dst *PathEnd, dstChanState *chantypes.QueryChannelResponse, signer sdk.AccAddress) sdk.Msg {
return chantypes.NewMsgChannelOpenAck(
pe.PortID,
pe.ChannelID,
dst.ChannelID,
dstChanState.Channel.Version,
dstChanState.Proof,
dstChanState.ProofHeight,
signer.String(),
)
}
// ChanConfirm creates a MsgChannelOpenConfirm
func (pe *PathEnd) ChanConfirm(dstChanState *chantypes.QueryChannelResponse, signer sdk.AccAddress) sdk.Msg {
return chantypes.NewMsgChannelOpenConfirm(
pe.PortID,
pe.ChannelID,
dstChanState.Proof,
dstChanState.ProofHeight,
signer.String(),
)
}
// ChanCloseInit creates a MsgChannelCloseInit
func (pe *PathEnd) ChanCloseInit(signer sdk.AccAddress) sdk.Msg {
return chantypes.NewMsgChannelCloseInit(
pe.PortID,
pe.ChannelID,
signer.String(),
)
}
// ChanCloseConfirm creates a MsgChannelCloseConfirm
func (pe *PathEnd) ChanCloseConfirm(dstChanState *chantypes.QueryChannelResponse, signer sdk.AccAddress) sdk.Msg {
return chantypes.NewMsgChannelCloseConfirm(
pe.PortID,
pe.ChannelID,
dstChanState.Proof,
dstChanState.ProofHeight,
signer.String(),
)
}
// MsgTransfer creates a new transfer message
func (pe *PathEnd) MsgTransfer(dst *PathEnd, amount sdk.Coin, dstAddr string,
signer sdk.AccAddress, timeoutHeight, timeoutTimestamp uint64, memo string) sdk.Msg {
version := clienttypes.ParseChainID(dst.ChainID)
return transfertypes.NewMsgTransfer(
pe.PortID,
pe.ChannelID,
amount,
signer.String(),
dstAddr,
clienttypes.NewHeight(version, timeoutHeight),
timeoutTimestamp,
memo,
)
}
// NewPacket returns a new packet from src to dist w
func (pe *PathEnd) NewPacket(dst *PathEnd, sequence uint64, packetData []byte,
timeoutHeight, timeoutStamp uint64) chantypes.Packet {
version := clienttypes.ParseChainID(dst.ChainID)
return chantypes.NewPacket(
packetData,
sequence,
pe.PortID,
pe.ChannelID,
dst.PortID,
dst.ChannelID,
clienttypes.NewHeight(version, timeoutHeight),
timeoutStamp,
)
}