-
Notifications
You must be signed in to change notification settings - Fork 0
/
client-tx.go
116 lines (101 loc) · 2.76 KB
/
client-tx.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
package relayer
import (
"fmt"
sdk "github.com/cosmos/cosmos-sdk/types"
"golang.org/x/sync/errgroup"
)
// CreateClients creates clients for src on dst and dst on src given the configured paths
func (c *Chain) CreateClients(dst *Chain) (err error) {
var (
clients = &RelayMsgs{Src: []sdk.Msg{}, Dst: []sdk.Msg{}}
eg = new(errgroup.Group)
)
srcH, dstH, err := UpdatesWithHeaders(c, dst)
if err != nil {
return err
}
// Create client for the destination chain on the source chain if it doesn't exist
eg.Go(func() error {
if srcCs, err := c.QueryClientState(srcH.Header.Height); err != nil && srcCs == nil {
if c.debug {
c.logCreateClient(dst, dstH.Header.Height)
}
ubdPeriod, err := dst.QueryUnbondingPeriod()
if err != nil {
return err
}
clients.Src = append(
clients.Src,
c.PathEnd.CreateClient(
dstH,
dst.GetTrustingPeriod(),
ubdPeriod,
c.MustGetAddress(),
))
}
return nil
})
eg.Go(func() error {
// Create client for the source chain on destination chain if it doesn't exist
if dstCs, err := dst.QueryClientState(dstH.Header.Height); err != nil && dstCs == nil {
if dst.debug {
dst.logCreateClient(c, srcH.Header.Height)
}
ubdPeriod, err := c.QueryUnbondingPeriod()
if err != nil {
return err
}
clients.Dst = append(
clients.Dst,
dst.PathEnd.CreateClient(
srcH,
c.GetTrustingPeriod(),
ubdPeriod,
dst.MustGetAddress(),
))
}
return nil
})
if err := eg.Wait(); err != nil {
return err
}
// Send msgs to both chains
if clients.Ready() {
if clients.Send(c, dst); clients.success {
c.Log(fmt.Sprintf("★ Clients created: [%s]client(%s) and [%s]client(%s)",
c.ChainID, c.PathEnd.ClientID, dst.ChainID, dst.PathEnd.ClientID))
}
}
return nil
}
// UpdateClients updates clients for src on dst and dst on src given the configured paths
func (c *Chain) UpdateClients(dst *Chain) (err error) {
clients := &RelayMsgs{Src: []sdk.Msg{}, Dst: []sdk.Msg{}}
srcH, dstH, err := UpdatesWithHeaders(c, dst)
if err != nil {
return err
}
srcUH, dstUH, err := InjectTrustedFieldsHeaders(c, dst, srcH, dstH)
if err != nil {
return err
}
clients.Src = append(clients.Src, c.PathEnd.UpdateClient(dstUH, c.MustGetAddress()))
clients.Dst = append(clients.Dst, dst.PathEnd.UpdateClient(srcUH, dst.MustGetAddress()))
// Send msgs to both chains
if clients.Ready() {
if clients.Send(c, dst); clients.success {
c.Log(fmt.Sprintf("★ Clients updated: [%s]client(%s) {%d}->{%d} and [%s]client(%s) {%d}->{%d}",
c.ChainID,
c.PathEnd.ClientID,
MustGetHeight(srcUH.TrustedHeight),
srcUH.Header.Height,
dst.ChainID,
dst.PathEnd.ClientID,
MustGetHeight(dstUH.TrustedHeight),
dstUH.Header.Height,
),
)
}
}
return nil
}