/
light.go
203 lines (175 loc) · 4.95 KB
/
light.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
package cmd
import (
"fmt"
"strconv"
"github.com/cosmos/cosmos-sdk/client/flags"
tmclient "github.com/cosmos/ibc-go/modules/light-clients/07-tendermint/types"
"github.com/datachainlab/harmony-cosmos-bridge-demo/relayer/chains/tendermint"
"github.com/datachainlab/harmony-cosmos-bridge-demo/relayer/chains/tendermint/types"
"github.com/hyperledger-labs/yui-relayer/config"
"github.com/spf13/cobra"
)
// chainCmd represents the keys command
func lightCmd(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "light",
Aliases: []string{"l"},
Short: "manage light clients held by the relayer for each chain",
}
cmd.AddCommand(lightHeaderCmd(ctx))
cmd.AddCommand(initLightCmd(ctx))
cmd.AddCommand(updateLightCmd(ctx))
cmd.AddCommand(deleteLightCmd(ctx))
return cmd
}
func initLightCmd(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "init [chain-id]",
Short: "Initiate the light client",
Long: `Initiate the light client by:
1. passing it a root of trust as a --hash/-x and --height
2. Use --force/-f to initialize from the configured node`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := ctx.Config.GetChain(args[0])
if err != nil {
return err
}
chain := c.ChainI.(*tendermint.Chain)
prover := c.ProverI.(*tendermint.Prover)
db, df, err := prover.NewLightDB()
if err != nil {
return err
}
defer df()
force, err := cmd.Flags().GetBool(flagForce)
if err != nil {
return err
}
height, err := cmd.Flags().GetInt64(flags.FlagHeight)
if err != nil {
return err
}
hash, err := cmd.Flags().GetBytesHex(flagHash)
if err != nil {
return err
}
switch {
case force: // force initialization from trusted node
_, err := prover.LightClientWithoutTrust(db)
if err != nil {
return err
}
fmt.Printf("successfully created light client for %s by trusting endpoint %s...\n", chain.ChainID(), chain.Config().RpcAddr)
case height > 0 && len(hash) > 0: // height and hash are given
_, err = prover.LightClientWithTrust(db, prover.TrustOptions(height, hash))
if err != nil {
return wrapInitFailed(err)
}
default: // return error
return errInitWrongFlags
}
return nil
},
}
return forceFlag(lightFlags(cmd))
}
func updateLightCmd(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "update [chain-id]",
Aliases: []string{"u"},
Short: "Update the light client to latest header from configured node",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := ctx.Config.GetChain(args[0])
if err != nil {
return err
}
prover := c.ProverI.(*tendermint.Prover)
bh, err := prover.GetLatestLightHeader()
if err != nil {
return err
}
ah, _, _, err := prover.UpdateLightWithHeader()
if err != nil {
return err
}
fmt.Printf("Updated light client for %s from height %d -> height %d\n", args[0], bh.SignedHeader.Header.Height, ah.(*tmclient.Header).Header.Height)
return nil
},
}
return cmd
}
func lightHeaderCmd(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "header [chain-id] [[height]]",
Aliases: []string{"hdr"},
Short: "Get a header from the light client database",
Long: "Get a header from the light client database. 0 returns last" +
"trusted header and all others return the header at that height if stored",
Args: cobra.RangeArgs(1, 2),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := ctx.Config.GetChain(args[0])
if err != nil {
return err
}
chain := c.ChainI.(*tendermint.Chain)
prover := c.ProverI.(*tendermint.Prover)
var header *types.TmHeader
switch len(args) {
case 1:
header, err = prover.GetLatestLightHeader()
if err != nil {
return err
}
case 2:
var height int64
height, err = strconv.ParseInt(args[1], 10, 64) //convert to int64
if err != nil {
return err
}
if height == 0 {
height, err = prover.GetLatestLightHeight()
if err != nil {
return err
}
if height == -1 {
return tendermint.ErrLightNotInitialized
}
}
header, err = prover.GetLightSignedHeaderAtHeight(height)
if err != nil {
return err
}
}
out, err := chain.Codec().MarshalJSON(header)
if err != nil {
return err
}
fmt.Println(string(out))
return nil
},
}
return cmd
}
func deleteLightCmd(ctx *config.Context) *cobra.Command {
cmd := &cobra.Command{
Use: "delete [chain-id]",
Aliases: []string{"d"},
Short: "wipe the light client database, forcing re-initialzation on the next run",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
c, err := ctx.Config.GetChain(args[0])
if err != nil {
return err
}
prover := c.ProverI.(*tendermint.Prover)
err = prover.DeleteLightDB()
if err != nil {
return err
}
return nil
},
}
return cmd
}