forked from hyperledger-labs/yui-relayer
/
channel.go
177 lines (159 loc) · 6.14 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
package core
import (
"fmt"
"log"
"time"
retry "github.com/avast/retry-go"
chantypes "github.com/cosmos/ibc-go/modules/core/04-channel/types"
)
// CreateChannel runs the channel creation messages on timeout until they pass
// TODO: add max retries or something to this function
func CreateChannel(src, dst *ProvableChain, ordered bool, to time.Duration) error {
var order chantypes.Order
if ordered {
order = chantypes.ORDERED
} else {
order = chantypes.UNORDERED
}
ticker := time.NewTicker(to)
failures := 0
for ; true; <-ticker.C {
chanSteps, err := createChannelStep(src, dst, order)
if err != nil {
return err
}
if !chanSteps.Ready() {
break
}
chanSteps.Send(src, dst)
switch {
// In the case of success and this being the last transaction
// debug logging, log created connection and break
case chanSteps.Success() && chanSteps.Last:
log.Println(fmt.Sprintf("★ Channel created: [%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
// 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++
log.Println(fmt.Sprintf("retrying transaction..."))
time.Sleep(5 * time.Second)
if failures > 2 {
return fmt.Errorf("! Channel failed: [%s]chan{%s}port{%s} -> [%s]chan{%s}port{%s}",
src.ChainID(), src.Path().ClientID, src.Path().ChannelID,
dst.ChainID(), dst.Path().ClientID, dst.Path().ChannelID)
}
}
}
return nil
}
func createChannelStep(src, dst *ProvableChain, ordering chantypes.Order) (*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 (
srcUpdateHeader, dstUpdateHeader HeaderI
)
err = retry.Do(func() error {
srcUpdateHeader, dstUpdateHeader, err = sh.GetHeaders(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(src, dst, sh.GetProvableHeight(src.ChainID()), sh.GetProvableHeight(dst.ChainID()))
if err != nil {
return nil, err
}
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 dstUpdateHeader != nil {
out.Src = append(out.Src, src.Path().UpdateClient(dstUpdateHeader, 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 srcUpdateHeader != nil {
out.Dst = append(out.Dst, dst.Path().UpdateClient(srcUpdateHeader, 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 srcUpdateHeader != nil {
out.Dst = append(out.Dst, dst.Path().UpdateClient(srcUpdateHeader, 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 dstUpdateHeader != nil {
out.Src = append(out.Src, src.Path().UpdateClient(dstUpdateHeader, 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 dstUpdateHeader != nil {
out.Src = append(out.Src, src.Path().UpdateClient(dstUpdateHeader, 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 srcUpdateHeader != nil {
out.Dst = append(out.Dst, dst.Path().UpdateClient(srcUpdateHeader, 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 ChainI, srcChan, dstChan *chantypes.QueryChannelResponse) {
log.Println(fmt.Sprintf("- [%s]@{%d}chan(%s)-{%s} : [%s]@{%d}chan(%s)-{%s}",
src.ChainID(),
mustGetHeight(srcChan.ProofHeight),
src.Path().ChannelID,
srcChan.Channel.State,
dst.ChainID(),
mustGetHeight(dstChan.ProofHeight),
dst.Path().ChannelID,
dstChan.Channel.State,
))
}