/
commands.go
196 lines (165 loc) · 5.13 KB
/
commands.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
package cmd
import (
"fmt"
"github.com/synapsecns/sanguine/core/metrics"
"github.com/synapsecns/sanguine/services/omnirpc/modules/confirmedtofinalized"
"github.com/synapsecns/sanguine/services/omnirpc/modules/harmonyproxy"
"os"
"time"
"github.com/phayes/freeport"
"github.com/synapsecns/sanguine/core"
rpcConfig "github.com/synapsecns/sanguine/services/omnirpc/config"
"github.com/synapsecns/sanguine/services/omnirpc/debug"
"github.com/synapsecns/sanguine/services/omnirpc/proxy"
"github.com/synapsecns/sanguine/services/omnirpc/rpcinfo"
"github.com/urfave/cli/v2"
)
var latencyCommand = &cli.Command{
Name: "check-latency",
Usage: "checks latency for all rpc endpoints known for a chain id",
Flags: []cli.Flag{chainIDFlag},
Action: func(c *cli.Context) error {
rConfig, err := rpcConfig.GetPublicRPCConfig()
if err != nil {
return fmt.Errorf("could not get rpc map: %w", err)
}
chainConfig, ok := rConfig.Chains[uint32(c.Int(chainIDFlag.Name))]
if !ok {
return fmt.Errorf("could not get chain config for chain %d", c.Int(chainIDFlag.Name))
}
res := rpcinfo.GetRPCLatency(c.Context, time.Second*5, chainConfig.RPCs, metrics.Get())
DisplayLatency(res)
return nil
},
}
var chainListCommand = &cli.Command{
Name: "chainlist-server",
Usage: "runs a chainlist proxy server",
Flags: []cli.Flag{portFlag},
Action: func(c *cli.Context) error {
// Create a large heap allocation of 10 GiB
// See: https://blog.twitch.tv/en/2019/04/10/go-memory-ballast-how-i-learnt-to-stop-worrying-and-love-the-heap/
_ = make([]byte, 10<<30)
rConfig, err := rpcConfig.GetPublicRPCConfig()
if err != nil {
return fmt.Errorf("could not get rpc map: %w", err)
}
if c.Int(portFlag.Name) != 0 {
rConfig.Port = uint16(c.Int(portFlag.Name))
}
if rConfig.Port == 0 {
rConfig.Port = uint16(freeport.GetPort())
}
server := proxy.NewProxy(rConfig, metrics.Get())
server.Run(c.Context)
return nil
},
}
var publicConfigCommand = &cli.Command{
Name: "public-config",
Usage: "output a public config file from chainlist.org",
Flags: []cli.Flag{outputFlag},
Action: func(c *cli.Context) error {
rConfig, err := rpcConfig.GetPublicRPCConfig()
if err != nil {
return fmt.Errorf("could not get rpc map: %w", err)
}
output, err := rConfig.Marshall()
if err != nil {
return fmt.Errorf("could not get rpc map: %w", err)
}
outputConfig, err := os.Create(core.ExpandOrReturnPath(c.String(outputFlag.Name)))
if err != nil {
return fmt.Errorf("could not create config file: %w", err)
}
defer func() {
_ = outputConfig.Close()
}()
_, err = outputConfig.Write(output)
if err != nil {
return fmt.Errorf("could not write to file: %w", err)
}
fmt.Printf("written to %s \n", c.String(outputFlag.Name))
return nil
},
}
var serverCommand = &cli.Command{
Name: "server",
Usage: "run a server from a config",
Flags: []cli.Flag{
configFlag,
portFlag,
},
Action: func(c *cli.Context) error {
// Create a large heap allocation of 10 GiB
// See: https://blog.twitch.tv/en/2019/04/10/go-memory-ballast-how-i-learnt-to-stop-worrying-and-love-the-heap/
_ = make([]byte, 10<<30)
fileContents, err := os.ReadFile(core.ExpandOrReturnPath(c.String(configFlag.Name)))
if err != nil {
return fmt.Errorf("could not read file %s: %w", c.String(configFlag.Name), err)
}
rConfig, err := rpcConfig.UnmarshallConfig(fileContents)
if err != nil {
return fmt.Errorf("could not unmarshall config: %w", err)
}
if c.Int(portFlag.Name) != 0 {
rConfig.Port = uint16(c.Int(portFlag.Name))
}
if rConfig.Port == 0 {
rConfig.Port = uint16(freeport.GetPort())
}
server := proxy.NewProxy(rConfig, metrics.Get())
server.Run(c.Context)
return nil
},
}
var debugResponse = &cli.Command{
Name: "debug-response",
Usage: "used for debugging responses and finding diff between rpcs",
Flags: []cli.Flag{
fileFlag,
},
Action: func(c *cli.Context) error {
diffFile, err := os.ReadFile(c.String(fileFlag.Name))
if err != nil {
return fmt.Errorf("could not read file: %w", err)
}
//nolint:wrapcheck
return debug.HashDiff(diffFile)
},
}
// latestRewrite rewrites latest block numbers for a single rpc url.
var latestRewrite = &cli.Command{
Name: "latest-rewrite",
Usage: "A simple rpc proxy for one-off integration tests. Rewrites block queries that use \"latest\" to \"finalized\"",
Flags: []cli.Flag{
rpcFlag,
portFlag,
maxSubmitAhead,
chainIDFlag,
},
Action: func(c *cli.Context) error {
simpleProxy := confirmedtofinalized.NewProxy(c.String(rpcFlag.Name), metrics.Get(), c.Int(portFlag.Name), c.Int(maxSubmitAhead.Name), c.Int(chainIDFlag.Name))
err := simpleProxy.Run(c.Context)
if err != nil {
return fmt.Errorf("return err: %w", err)
}
return nil
},
}
var harmonyProxy = &cli.Command{
Name: "harmony-confirm",
Usage: "An experimental harmony confirmation client",
Flags: []cli.Flag{
rpcFlag,
portFlag,
},
Action: func(c *cli.Context) error {
simpleProxy := harmonyproxy.NewHarmonyProxy(c.String(rpcFlag.Name), metrics.Get(), c.Int(portFlag.Name))
err := simpleProxy.Run(c.Context)
if err != nil {
return fmt.Errorf("return err: %w", err)
}
return nil
},
}