-
Notifications
You must be signed in to change notification settings - Fork 28
/
client.go
83 lines (74 loc) · 1.95 KB
/
client.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
package core
import (
"fmt"
"log"
sdk "github.com/cosmos/cosmos-sdk/types"
)
func CreateClients(src, dst *ProvableChain) error {
var (
clients = &RelayMsgs{Src: []sdk.Msg{}, Dst: []sdk.Msg{}}
)
srcH, dstH, err := UpdatesWithHeaders(src, dst)
if err != nil {
return err
}
srcAddr, err := src.GetAddress()
if err != nil {
return err
}
dstAddr, err := dst.GetAddress()
if err != nil {
return err
}
{
msg, err := dst.CreateMsgCreateClient(src.Path().ClientID, dstH, srcAddr)
if err != nil {
return err
}
clients.Src = append(clients.Src, msg)
}
{
msg, err := src.CreateMsgCreateClient(dst.Path().ClientID, srcH, dstAddr)
if err != nil {
return err
}
clients.Dst = append(clients.Dst, msg)
}
// Send msgs to both chains
if clients.Ready() {
// TODO: Add retry here for out of gas or other errors
if clients.Send(src, dst); clients.Success() {
log.Println(fmt.Sprintf("★ Clients created: [%s]client(%s) and [%s]client(%s)",
src.ChainID(), src.Path().ClientID, dst.ChainID(), dst.Path().ClientID))
}
}
return nil
}
func UpdateClients(src, dst *ProvableChain) error {
var (
clients = &RelayMsgs{Src: []sdk.Msg{}, Dst: []sdk.Msg{}}
)
// First, update the light clients to the latest header and return the header
sh, err := NewSyncHeaders(src, dst)
if err != nil {
return err
}
srcUpdateHeader, dstUpdateHeader, err := sh.GetHeaders(src, dst)
if err != nil {
return err
}
if dstUpdateHeader != nil {
clients.Src = append(clients.Src, src.Path().UpdateClient(dstUpdateHeader, mustGetAddress(src)))
}
if srcUpdateHeader != nil {
clients.Dst = append(clients.Dst, dst.Path().UpdateClient(srcUpdateHeader, mustGetAddress(dst)))
}
// Send msgs to both chains
if clients.Ready() {
if clients.Send(src, dst); clients.Success() {
log.Println(fmt.Sprintf("★ Clients updated: [%s]client(%s) and [%s]client(%s)",
src.ChainID(), src.Path().ClientID, dst.ChainID(), dst.Path().ClientID))
}
}
return nil
}