-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
start.go
187 lines (164 loc) · 5.1 KB
/
start.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
/*
Package cmd includes relayer commands
Copyright © 2020 Jack Zampolin <jack.zampolin@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cmd
import (
"context"
"errors"
"fmt"
"net"
"strconv"
"strings"
"github.com/cosmos/relayer/v2/internal/relaydebug"
"github.com/cosmos/relayer/v2/relayer"
"github.com/cosmos/relayer/v2/relayer/chains/cosmos"
"github.com/cosmos/relayer/v2/relayer/processor"
"github.com/spf13/cobra"
"go.uber.org/zap"
)
// startCmd represents the start command
func startCmd(a *appState) *cobra.Command {
cmd := &cobra.Command{
Use: "start path_name",
Aliases: []string{"st"},
Short: "Start the listening relayer on a given path",
Args: withUsage(cobra.MinimumNArgs(0)),
Example: strings.TrimSpace(fmt.Sprintf(`
$ %s start # start all configured paths
$ %s start demo-path # start the 'demo-path' path
$ %s start demo-path --max-msgs 3
$ %s start demo-path2 --max-tx-size 10`, appName, appName, appName, appName)),
RunE: func(cmd *cobra.Command, args []string) error {
chains := make(map[string]*relayer.Chain)
paths := make([]relayer.NamedPath, len(args))
if len(args) > 0 {
for i, pathName := range args {
path := a.Config.Paths.MustGet(pathName)
paths[i] = relayer.NamedPath{
Name: pathName,
Path: path,
}
// collect unique chain IDs
chains[path.Src.ChainID] = nil
chains[path.Dst.ChainID] = nil
}
} else {
for n, path := range a.Config.Paths {
paths = append(paths, relayer.NamedPath{
Name: n,
Path: path,
})
// collect unique chain IDs
chains[path.Src.ChainID] = nil
chains[path.Dst.ChainID] = nil
}
}
chainIDs := make([]string, 0, len(chains))
for chainID := range chains {
chainIDs = append(chainIDs, chainID)
}
// get chain configurations
chains, err := a.Config.Chains.Gets(chainIDs...)
if err != nil {
return err
}
if err := ensureKeysExist(chains); err != nil {
return err
}
maxTxSize, maxMsgLength, err := GetStartOptions(cmd)
if err != nil {
return err
}
var prometheusMetrics *processor.PrometheusMetrics
debugAddr, err := cmd.Flags().GetString(flagDebugAddr)
if err != nil {
return err
}
if debugAddr == "" {
a.Log.Info("Skipping debug server due to empty debug address flag")
} else {
ln, err := net.Listen("tcp", debugAddr)
if err != nil {
a.Log.Error("Failed to listen on debug address. If you have another relayer process open, use --" + flagDebugAddr + " to pick a different address.")
return fmt.Errorf("failed to listen on debug address %q: %w", debugAddr, err)
}
log := a.Log.With(zap.String("sys", "debughttp"))
log.Info("Debug server listening", zap.String("addr", debugAddr))
relaydebug.StartDebugServer(cmd.Context(), log, ln)
prometheusMetrics = processor.NewPrometheusMetrics()
for _, chain := range chains {
if ccp, ok := chain.ChainProvider.(*cosmos.CosmosProvider); ok {
ccp.SetMetrics(prometheusMetrics)
}
}
}
processorType, err := cmd.Flags().GetString(flagProcessor)
if err != nil {
return err
}
initialBlockHistory, err := cmd.Flags().GetUint64(flagInitialBlockHistory)
if err != nil {
return err
}
rlyErrCh := relayer.StartRelayer(
cmd.Context(),
a.Log,
chains,
paths,
maxTxSize, maxMsgLength,
a.Config.memo(cmd),
processorType, initialBlockHistory,
prometheusMetrics,
)
// Block until the error channel sends a message.
// The context being canceled will cause the relayer to stop,
// so we don't want to separately monitor the ctx.Done channel,
// because we would risk returning before the relayer cleans up.
if err := <-rlyErrCh; err != nil && !errors.Is(err, context.Canceled) {
a.Log.Warn(
"Relayer start error",
zap.Error(err),
)
return err
}
return nil
},
}
cmd = updateTimeFlags(a.Viper, cmd)
cmd = strategyFlag(a.Viper, cmd)
cmd = debugServerFlags(a.Viper, cmd)
cmd = processorFlag(a.Viper, cmd)
cmd = initBlockFlag(a.Viper, cmd)
cmd = memoFlag(a.Viper, cmd)
return cmd
}
// GetStartOptions sets strategy specific fields.
func GetStartOptions(cmd *cobra.Command) (uint64, uint64, error) {
maxTxSize, err := cmd.Flags().GetString(flagMaxTxSize)
if err != nil {
return 0, 0, err
}
txSize, err := strconv.ParseUint(maxTxSize, 10, 64)
if err != nil {
return 0, 0, err
}
maxMsgLength, err := cmd.Flags().GetString(flagMaxMsgLength)
if err != nil {
return txSize * MB, 0, err
}
msgLen, err := strconv.ParseUint(maxMsgLength, 10, 64)
if err != nil {
return txSize * MB, 0, err
}
return txSize * MB, msgLen, nil
}