-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
254 lines (215 loc) · 7.43 KB
/
main.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright (c) Gabriel de Quadros Ligneul
// SPDX-License-Identifier: Apache-2.0 (see LICENSE)
// This package contains the main function that executes the cartesi-rollups-graphql command.
package main
import (
_ "embed"
"fmt"
"log/slog"
"os"
"os/signal"
"strings"
"syscall"
"time"
"github.com/carlmjohnson/versioninfo"
"github.com/cartesi/rollups-graphql/pkg/bootstrap"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/joho/godotenv"
"github.com/lmittmann/tint"
"github.com/mattn/go-isatty"
"github.com/spf13/cast"
"github.com/spf13/cobra"
)
var (
MAX_FILE_SIZE uint64 = 1_440_000 // 1,44 MB
)
var startupMessage = `
GraphQL running at http://localhost:HTTP_PORT/graphql
Press Ctrl+C to stop the node
`
var tempFromBlockL1 uint64
var cmd = &cobra.Command{
Use: "cartesi-rollups-graphql [flags] [-- application [args]...]",
Short: "cartesi-rollups-graphql is a development node for Cartesi Rollups",
Run: run,
Version: versioninfo.Short(),
}
var CompletionCmd = &cobra.Command{
Use: "completion",
Short: "Generate shell completion scripts",
DisableFlagsInUseLine: true,
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
Run: func(cmd *cobra.Command, args []string) {
switch args[0] {
case "bash":
cobra.CheckErr(cmd.Root().GenBashCompletion(os.Stdout))
case "zsh":
cobra.CheckErr(cmd.Root().GenZshCompletion(os.Stdout))
case "fish":
cobra.CheckErr(cmd.Root().GenFishCompletion(os.Stdout, true))
case "powershell":
cobra.CheckErr(cmd.Root().GenPowerShellCompletion(os.Stdout))
}
},
}
var (
debug bool
color bool
opts = bootstrap.NewBootstrapOpts()
)
func ArrBytesAttr(key string, v []byte) slog.Attr {
var str string
for _, b := range v {
s := fmt.Sprintf("%02x", b)
str += s
}
return slog.String(key, str)
}
func CheckIfValidSize(size uint64) error {
if size > MAX_FILE_SIZE {
return fmt.Errorf("file size is too big %d bytes", size)
}
return nil
}
func init() {
// contracts-*
cmd.Flags().StringVar(&opts.ApplicationAddress, "contracts-application-address",
opts.ApplicationAddress, "Application contract address")
// enable-*
cmd.Flags().BoolVarP(&debug, "enable-debug", "d", false, "If set, enable debug output")
cmd.Flags().BoolVar(&color, "enable-color", true, "If set, enables logs color")
cmd.Flags().DurationVar(&opts.TimeoutWorker, "timeout-worker", opts.TimeoutWorker, "Timeout for workers. Example: cartesi-rollups-graphql --timeout-worker 30s")
// disable-*
// http-*
cmd.Flags().StringVar(&opts.HttpAddress, "http-address", opts.HttpAddress,
"HTTP address used by cartesi-rollups-graphql to serve its APIs")
cmd.Flags().IntVar(&opts.HttpPort, "http-port", opts.HttpPort,
"HTTP port used by cartesi-rollups-graphql to serve its external APIs")
// database file
cmd.Flags().StringVar(&opts.SqliteFile, "sqlite-file", opts.SqliteFile,
"The sqlite file to load the state")
cmd.Flags().Uint64VarP(&tempFromBlockL1, "from-l1-block", "", 0, "The beginning of the queried range for events")
cmd.Flags().StringVar(&opts.DbImplementation, "db-implementation", opts.DbImplementation,
"DB to use. PostgreSQL or SQLite")
cmd.Flags().BoolVar(&opts.RawEnabled, "raw-enabled", opts.RawEnabled, "If set, enables raw database")
}
func deprecatedWarningCmd(cmd *cobra.Command, flag string, replacement string) {
if cmd.Flags().Changed(flag) {
slog.Warn(fmt.Sprintf("The '%s' flag is deprecated. %s", flag, replacement))
}
}
func deprecatedFlags(cmd *cobra.Command) {
checkAndSetFlag(cmd, "contracts-application-address", func(val string) { opts.ApplicationAddress = val }, "APPLICATION_ADDRESS")
checkAndSetFlag(cmd, "enable-debug", func(val string) { debug = cast.ToBool(val) }, "GRAPHQL_DEBUG")
checkAndSetFlag(cmd, "enable-color", func(val string) { color = cast.ToBool(val) }, "COLOR")
checkAndSetFlag(cmd, "timeout-worker", func(val string) { opts.TimeoutWorker, _ = time.ParseDuration(val) }, "TIMEOUT_WORKER")
checkAndSetFlag(cmd, "http-address", func(val string) { opts.HttpAddress = val }, "HTTP_ADDRESS")
checkAndSetFlag(cmd, "http-port", func(val string) { opts.HttpPort = cast.ToInt(val) }, "HTTP_PORT")
checkAndSetFlag(cmd, "sqlite-file", func(val string) { opts.SqliteFile = val }, "SQLITE_FILE")
checkAndSetFlag(cmd, "from-l1-block", func(val string) { tempFromBlockL1 = cast.ToUint64(val) }, "FROM_BLOCK_L1")
checkAndSetFlag(cmd, "db-implementation", func(val string) { opts.DbImplementation = val }, "DB_IMPLEMENTATION")
checkAndSetFlag(cmd, "raw-enabled", func(val string) { opts.RawEnabled = cast.ToBool(val) }, "RAW_ENABLED")
}
/**
* Check if the flag is set and set the value from the environment variable
*/
func checkAndSetFlag(cmd *cobra.Command, flagName string, setOptEnv func(string), flagEnv string) {
val, isEnvVarPresent := os.LookupEnv(flagEnv)
if isEnvVarPresent {
setOptEnv(val)
}
deprecatedMsg := fmt.Sprint("Please use ", flagEnv, " instead.")
deprecatedWarningCmd(cmd, flagName, deprecatedMsg)
}
func run(cmd *cobra.Command, args []string) {
LoadEnv()
ctx := cmd.Context()
startTime := time.Now()
// setup log
logOpts := new(tint.Options)
if debug {
logOpts.Level = slog.LevelDebug
}
logOpts.AddSource = debug
logOpts.NoColor = !color || !isatty.IsTerminal(os.Stdout.Fd())
logOpts.TimeFormat = "[15:04:05.000]"
handler := tint.NewHandler(os.Stdout, logOpts)
logger := slog.New(handler)
slog.SetDefault(logger)
// check args
checkEthAddress(cmd, "address-input-box")
checkEthAddress(cmd, "address-application")
deprecatedFlags(cmd)
// handle signals with notify context
ctx, cancel := signal.NotifyContext(ctx, syscall.SIGINT, syscall.SIGTERM)
defer cancel()
startMessage := startupMessage
var inspectMessage string
// start cartesi-rollups-graphql
ready := make(chan struct{}, 1)
go func() {
select {
case <-ready:
msg := strings.Replace(
startMessage,
"\nINSPECT_MESSAGE",
inspectMessage, -1)
msg = strings.Replace(
msg,
"HTTP_PORT",
fmt.Sprint(opts.HttpPort), -1)
fmt.Println(msg)
slog.Info("cartesi-rollups-graphql: ready", "after", time.Since(startTime))
case <-ctx.Done():
}
}()
var err error = bootstrap.NewSupervisorGraphQL(opts).Start(ctx, ready)
cobra.CheckErr(err)
}
//go:embed .env
var envBuilded string
// LoadEnv from embedded .env file
func LoadEnv() {
currentEnv := map[string]bool{}
rawEnv := os.Environ()
for _, rawEnvLine := range rawEnv {
key := strings.Split(rawEnvLine, "=")[0]
currentEnv[key] = true
}
parse, err := godotenv.Unmarshal(envBuilded)
cobra.CheckErr(err)
for k, v := range parse {
if !currentEnv[k] {
slog.Debug("env: setting env", "key", k, "value", v)
err := os.Setenv(k, v)
cobra.CheckErr(err)
} else {
slog.Debug("env: skipping env", "key", k)
}
}
slog.Debug("env: loaded")
}
func main() {
cmd.AddCommand(CompletionCmd)
cobra.CheckErr(cmd.Execute())
}
func exitf(format string, args ...any) {
err := fmt.Sprintf(format, args...)
slog.Error("configuration error", "error", err)
os.Exit(1)
}
func checkEthAddress(cmd *cobra.Command, varName string) {
if cmd.Flags().Changed(varName) {
value, err := cmd.Flags().GetString(varName)
cobra.CheckErr(err)
bytes, err := hexutil.Decode(value)
if err != nil {
exitf("invalid address for --%v: %v", varName, err)
}
if len(bytes) != common.AddressLength {
exitf("invalid address for --%v: wrong length", varName)
}
}
}