-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
209 lines (166 loc) · 3.76 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
package main
import (
"context"
"fmt"
"os"
"os/signal"
"path"
"syscall"
"time"
_ "embed"
"github.com/chainbound/apollo/chainservice"
"github.com/chainbound/apollo/db"
"github.com/chainbound/apollo/dsl"
"github.com/chainbound/apollo/log"
"github.com/chainbound/apollo/output"
"github.com/chainbound/apollo/types"
"github.com/rs/zerolog"
"github.com/urfave/cli/v2"
)
//go:embed config.example.yml
var cfg []byte
//go:embed schema.example.hcl
var schema []byte
var logger zerolog.Logger
func main() {
var opts types.ApolloOpts
logger = log.NewLogger("main")
app := &cli.App{
Name: "apollo",
Usage: "Run the chain analyzer",
Flags: BuildFlags(&opts),
Commands: []*cli.Command{
{
Name: "init",
Usage: "Initialize apollo by creating the configs",
Action: func(c *cli.Context) error {
if err := Init(); err != nil {
return err
}
return nil
},
},
},
Action: func(c *cli.Context) error {
err := Run(opts)
return err
},
}
err := app.Run(os.Args)
if err != nil {
logger.Fatal().Err(err).Msg("running app")
}
}
func Init() error {
p, err := os.UserConfigDir()
if err != nil {
return err
}
dirPath := path.Join(p, "apollo")
_, err = os.Stat(dirPath)
if os.IsNotExist(err) {
err := os.MkdirAll(dirPath, 0744)
if err != nil {
return err
}
}
configPath := path.Join(dirPath, "config.yml")
if err := os.WriteFile(configPath, cfg, 0644); err != nil {
return err
}
fmt.Println("config written", configPath)
schemaPath := path.Join(dirPath, "schema.hcl")
if err := os.WriteFile(schemaPath, schema, 0644); err != nil {
return err
}
fmt.Println("schema written", schemaPath)
return nil
}
func Run(opts types.ApolloOpts) error {
lvl := zerolog.Level(int8(opts.LogLevel))
logger.Info().Int("log_level", int(lvl)).Msg("logger")
zerolog.SetGlobalLevel(lvl)
var pdb *db.DB
confDir, err := ConfigDir()
if err != nil {
return err
}
confPath, err := ConfigPath()
if err != nil {
return err
}
cfg, err := NewConfig(confPath)
if err != nil {
return err
}
schema, err := dsl.NewSchema(confDir)
if err != nil {
return err
}
// Validate the schema
if err := schema.Validate(opts); err != nil {
return err
}
cfg.DbSettings.DefaultTimeout = time.Second * 20
if opts.Db {
pdb, err = db.NewDB(cfg.DbSettings).Connect()
if err != nil {
return err
}
}
defaultTimeout := time.Second * 30
service := chainservice.NewChainService(defaultTimeout, opts.RateLimit, opts.LogParts, cfg.Rpc)
setupCloseHandler(service)
out := output.NewOutputHandler()
if opts.Db {
out = out.WithDB(pdb)
}
if opts.Csv {
out = out.WithCsv(output.NewCsvHandler())
}
if opts.Stdout {
out = out.WithStdOut()
}
// First check if there are any methods to be called, it might just be events
chainResults := make(chan types.CallResult)
// Long timeout
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30)
defer cancel()
go func() error {
err := service.Start(ctx, schema, opts, chainResults)
if err != nil {
return err
}
return nil
}()
for res := range chainResults {
if res.Err != nil {
logger.Warn().Str("chain", string(res.Chain)).Msg(res.Err.Error())
continue
}
save, err := schema.EvalSave(service, res)
if err != nil {
return fmt.Errorf("evaluating save block: %w", err)
}
// Result got filtered out
if save == nil {
continue
}
err = out.HandleResult(res.QueryName, save)
if err != nil {
return fmt.Errorf("handling result: %w", err)
}
}
service.DumpMetrics()
return nil
}
func setupCloseHandler(svc *chainservice.ChainService) {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
logger.Warn().Msg("ctrl+c pressed, exiting...")
svc.DumpMetrics()
os.Exit(0)
}()
}