-
Notifications
You must be signed in to change notification settings - Fork 30
/
channel.go
239 lines (217 loc) · 7.97 KB
/
channel.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
package core
import (
"context"
"fmt"
"time"
retry "github.com/avast/retry-go"
chantypes "github.com/cosmos/ibc-go/v7/modules/core/04-channel/types"
"github.com/hyperledger-labs/yui-relayer/log"
"golang.org/x/exp/slog"
)
// CreateChannel runs the channel creation messages on timeout until they pass
// TODO: add max retries or something to this function
func CreateChannel(pathName string, src, dst *ProvableChain, to time.Duration) error {
logger := GetChannelPairLogger(src, dst)
defer logger.TimeTrack(time.Now(), "CreateChannel")
ticker := time.NewTicker(to)
failures := 0
for ; true; <-ticker.C {
chanSteps, err := createChannelStep(src, dst)
if err != nil {
logger.Error(
"failed to create channel step",
err,
)
return err
}
if !chanSteps.Ready() {
logger.Debug("Waiting for next channel step ...")
continue
}
chanSteps.Send(src, dst)
if chanSteps.Success() {
if err := SyncChainConfigsFromEvents(pathName, chanSteps.SrcMsgIDs, chanSteps.DstMsgIDs, src, dst); err != nil {
return err
}
}
switch {
// In the case of success and this being the last transaction
// debug logging, log created connection and break
case chanSteps.Success() && chanSteps.Last:
logger.Info(
"★ Channel created",
)
return nil
// In the case of success, reset the failures counter
case chanSteps.Success():
failures = 0
continue
// In the case of failure, increment the failures counter and exit if this is the 3rd failure
case !chanSteps.Success():
failures++
logger.Info("retrying transaction...")
time.Sleep(5 * time.Second)
if failures > 2 {
logger.Error(
"! Channel failed",
err,
)
return fmt.Errorf("! Channel failed: [%s]chan{%s}port{%s} -> [%s]chan{%s}port{%s}",
src.ChainID(), src.Path().ChannelID, src.Path().PortID,
dst.ChainID(), dst.Path().ChannelID, dst.Path().PortID,
)
}
}
}
return nil
}
func createChannelStep(src, dst *ProvableChain) (*RelayMsgs, error) {
out := NewRelayMsgs()
if err := validatePaths(src, dst); err != nil {
return nil, err
}
// First, update the light clients to the latest header and return the header
sh, err := NewSyncHeaders(src, dst)
if err != nil {
return nil, err
}
// Query a number of things all at once
var (
srcUpdateHeaders, dstUpdateHeaders []Header
)
err = retry.Do(func() error {
srcUpdateHeaders, dstUpdateHeaders, err = sh.SetupBothHeadersForUpdate(src, dst)
return err
}, rtyAtt, rtyDel, rtyErr, retry.OnRetry(func(n uint, err error) {
// logRetryUpdateHeaders(src, dst, n, err)
if err := sh.Updates(src, dst); err != nil {
panic(err)
}
}))
if err != nil {
return nil, err
}
srcChan, dstChan, err := QueryChannelPair(sh.GetQueryContext(src.ChainID()), sh.GetQueryContext(dst.ChainID()), src, dst, true)
if err != nil {
return nil, err
}
if finalized, err := checkChannelFinality(src, dst, srcChan.Channel, dstChan.Channel); err != nil {
return nil, err
} else if !finalized {
return out, nil
}
switch {
// Handshake hasn't been started on src or dst, relay `chanOpenInit` to src
case srcChan.Channel.State == chantypes.UNINITIALIZED && dstChan.Channel.State == chantypes.UNINITIALIZED:
logChannelStates(src, dst, srcChan, dstChan)
addr := mustGetAddress(src)
out.Src = append(out.Src,
src.Path().ChanInit(dst.Path(), addr),
)
// Handshake has started on dst (1 step done), relay `chanOpenTry` and `updateClient` to src
case srcChan.Channel.State == chantypes.UNINITIALIZED && dstChan.Channel.State == chantypes.INIT:
logChannelStates(src, dst, srcChan, dstChan)
addr := mustGetAddress(src)
if len(dstUpdateHeaders) > 0 {
out.Src = append(out.Src, src.Path().UpdateClients(dstUpdateHeaders, addr)...)
}
out.Src = append(out.Src, src.Path().ChanTry(dst.Path(), dstChan, addr))
// Handshake has started on src (1 step done), relay `chanOpenTry` and `updateClient` to dst
case srcChan.Channel.State == chantypes.INIT && dstChan.Channel.State == chantypes.UNINITIALIZED:
logChannelStates(dst, src, dstChan, srcChan)
addr := mustGetAddress(dst)
if len(srcUpdateHeaders) > 0 {
out.Dst = append(out.Dst, dst.Path().UpdateClients(srcUpdateHeaders, addr)...)
}
out.Dst = append(out.Dst, dst.Path().ChanTry(src.Path(), srcChan, addr))
// Handshake has started on src (2 steps done), relay `chanOpenAck` and `updateClient` to dst
case srcChan.Channel.State == chantypes.TRYOPEN && dstChan.Channel.State == chantypes.INIT:
logChannelStates(dst, src, dstChan, srcChan)
addr := mustGetAddress(dst)
if len(srcUpdateHeaders) > 0 {
out.Dst = append(out.Dst, dst.Path().UpdateClients(srcUpdateHeaders, addr)...)
}
out.Dst = append(out.Dst, dst.Path().ChanAck(src.Path(), srcChan, addr))
// Handshake has started on dst (2 steps done), relay `chanOpenAck` and `updateClient` to src
case srcChan.Channel.State == chantypes.INIT && dstChan.Channel.State == chantypes.TRYOPEN:
logChannelStates(src, dst, srcChan, dstChan)
addr := mustGetAddress(src)
if len(dstUpdateHeaders) > 0 {
out.Src = append(out.Src, src.Path().UpdateClients(dstUpdateHeaders, addr)...)
}
out.Src = append(out.Src, src.Path().ChanAck(dst.Path(), dstChan, addr))
// Handshake has confirmed on dst (3 steps done), relay `chanOpenConfirm` and `updateClient` to src
case srcChan.Channel.State == chantypes.TRYOPEN && dstChan.Channel.State == chantypes.OPEN:
logChannelStates(src, dst, srcChan, dstChan)
addr := mustGetAddress(src)
if len(dstUpdateHeaders) > 0 {
out.Src = append(out.Src, src.Path().UpdateClients(dstUpdateHeaders, addr)...)
}
out.Src = append(out.Src, src.Path().ChanConfirm(dstChan, addr))
out.Last = true
// Handshake has confirmed on src (3 steps done), relay `chanOpenConfirm` and `updateClient` to dst
case srcChan.Channel.State == chantypes.OPEN && dstChan.Channel.State == chantypes.TRYOPEN:
logChannelStates(dst, src, dstChan, srcChan)
addr := mustGetAddress(dst)
if len(srcUpdateHeaders) > 0 {
out.Dst = append(out.Dst, dst.Path().UpdateClients(srcUpdateHeaders, addr)...)
}
out.Dst = append(out.Dst, dst.Path().ChanConfirm(srcChan, addr))
out.Last = true
default:
panic(fmt.Sprintf("not implemeneted error: %v <=> %v", srcChan.Channel.State.String(), dstChan.Channel.State.String()))
}
return out, nil
}
func logChannelStates(src, dst *ProvableChain, srcChan, dstChan *chantypes.QueryChannelResponse) {
logger := GetChannelPairLogger(src, dst)
logger.Info(
"channel states",
slog.Group("src",
slog.Uint64("proof_height", mustGetHeight(srcChan.ProofHeight)),
slog.String("state", srcChan.Channel.State.String()),
),
slog.Group("dst",
slog.Uint64("proof_height", mustGetHeight(dstChan.ProofHeight)),
slog.String("state", dstChan.Channel.State.String()),
))
}
func checkChannelFinality(src, dst *ProvableChain, srcChannel, dstChannel *chantypes.Channel) (bool, error) {
logger := GetChannelPairLogger(src, dst)
sh, err := src.LatestHeight()
if err != nil {
return false, err
}
dh, err := dst.LatestHeight()
if err != nil {
return false, err
}
srcChanLatest, dstChanLatest, err := QueryChannelPair(NewQueryContext(context.TODO(), sh), NewQueryContext(context.TODO(), dh), src, dst, false)
if err != nil {
return false, err
}
if srcChannel.State != srcChanLatest.Channel.State {
logger.Debug("src channel state in transition", "from_state", srcChannel.State, "to_state", srcChanLatest.Channel.State)
return false, nil
}
if dstChannel.State != dstChanLatest.Channel.State {
logger.Debug("dst channel state in transition", "from_state", dstChannel.State, "to_state", dstChanLatest.Channel.State)
return false, nil
}
return true, nil
}
func GetChannelLogger(c Chain) *log.RelayLogger {
return log.GetLogger().
WithChannel(
c.ChainID(), c.Path().PortID, c.Path().ChannelID,
).
WithModule("core.channel")
}
func GetChannelPairLogger(src, dst Chain) *log.RelayLogger {
return log.GetLogger().
WithChannelPair(
src.ChainID(), src.Path().PortID, src.Path().ChannelID,
dst.ChainID(), dst.Path().PortID, dst.Path().ChannelID,
).
WithModule("core.channel")
}