-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathanalyzer.go
263 lines (229 loc) · 7.1 KB
/
analyzer.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
255
256
257
258
259
260
261
262
263
// Package analyzer implements the `analyze` sub-command.
package analyzer
import (
"context"
"os"
"sync"
migrate "github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/postgres" // postgres driver for golang_migrate
_ "github.com/golang-migrate/migrate/v4/source/file" // support file scheme for golang_migrate
_ "github.com/golang-migrate/migrate/v4/source/github" // support github scheme for golang_migrate
"github.com/spf13/cobra"
"github.com/oasisprotocol/oasis-indexer/analyzer"
"github.com/oasisprotocol/oasis-indexer/analyzer/consensus"
"github.com/oasisprotocol/oasis-indexer/analyzer/evmtokenbalances"
"github.com/oasisprotocol/oasis-indexer/analyzer/evmtokens"
"github.com/oasisprotocol/oasis-indexer/analyzer/runtime"
cmdCommon "github.com/oasisprotocol/oasis-indexer/cmd/common"
"github.com/oasisprotocol/oasis-indexer/common"
"github.com/oasisprotocol/oasis-indexer/config"
"github.com/oasisprotocol/oasis-indexer/log"
"github.com/oasisprotocol/oasis-indexer/storage"
)
const (
moduleName = "analysis_service"
)
var (
// Path to the configuration file.
configFile string
analyzeCmd = &cobra.Command{
Use: "analyze",
Short: "Analyze blocks",
Run: runAnalyzer,
}
)
func runAnalyzer(cmd *cobra.Command, args []string) {
// Initialize config.
cfg, err := config.InitConfig(configFile)
if err != nil {
log.NewDefaultLogger("init").Error("config init failed",
"error", err,
)
os.Exit(1)
}
// Initialize common environment.
if err = cmdCommon.Init(cfg); err != nil {
log.NewDefaultLogger("init").Error("init failed",
"error", err,
)
os.Exit(1)
}
logger := cmdCommon.Logger()
if cfg.Analysis == nil {
logger.Error("analysis config not provided")
os.Exit(1)
}
service, err := Init(cfg.Analysis)
if err != nil {
os.Exit(1)
}
defer service.Shutdown()
service.Start()
}
// Init initializes the analysis service.
func Init(cfg *config.AnalysisConfig) (*Service, error) {
logger := cmdCommon.Logger()
logger.Info("initializing analysis service", "config", cfg)
if cfg.Storage.WipeStorage {
logger.Warn("wiping storage")
if err := wipeStorage(cfg.Storage); err != nil {
return nil, err
}
logger.Info("storage wiped")
}
m, err := migrate.New(
cfg.Storage.Migrations,
cfg.Storage.Endpoint,
)
if err != nil {
logger.Error("migrator failed to start",
"error", err,
)
return nil, err
}
switch err = m.Up(); {
case err == migrate.ErrNoChange:
logger.Info("no migrations needed to be applied")
case err != nil:
logger.Error("migrations failed",
"error", err,
)
return nil, err
default:
logger.Info("migrations completed")
}
service, err := NewService(cfg)
if err != nil {
logger.Error("service failed to start",
"error", err,
)
return nil, err
}
return service, nil
}
func wipeStorage(cfg *config.StorageConfig) error {
logger := cmdCommon.Logger().WithModule(moduleName)
// Initialize target storage.
storage, err := cmdCommon.NewClient(cfg, logger)
if err != nil {
return err
}
defer storage.Shutdown()
ctx := context.Background()
return storage.Wipe(ctx)
}
// Service is the Oasis Indexer's analysis service.
type Service struct {
Analyzers map[string]analyzer.Analyzer
target storage.TargetStorage
logger *log.Logger
}
type A = analyzer.Analyzer
// addAnalyzer adds the analyzer produced by `analyzerGenerator()` to `analyzers`.
// It expects an initial state (analyzers, errSoFar) and returns the updated state, which
// should be fed into subsequent call to the function.
// As soon as an analyzerGenerator returns an error, all subsequent calls will
// short-circuit and return the same error, leaving `analyzers` unchanged.
func addAnalyzer(analyzers map[string]A, errSoFar error, analyzerGenerator func() (A, error)) (map[string]A, error) {
if errSoFar != nil {
return analyzers, errSoFar
}
a, errSoFar := analyzerGenerator()
if errSoFar != nil {
return analyzers, errSoFar
}
analyzers[a.Name()] = a
return analyzers, nil
}
// NewService creates new Service.
func NewService(cfg *config.AnalysisConfig) (*Service, error) {
logger := cmdCommon.Logger().WithModule(moduleName)
// Initialize target storage.
client, err := cmdCommon.NewClient(cfg.Storage, logger)
if err != nil {
return nil, err
}
// Initialize analyzers.
analyzers := map[string]A{}
if cfg.Analyzers.Consensus != nil {
analyzers, err = addAnalyzer(analyzers, err, func() (A, error) {
return consensus.NewMain(&cfg.Source, cfg.Analyzers.Consensus, client, logger)
})
}
if cfg.Analyzers.Emerald != nil {
analyzers, err = addAnalyzer(analyzers, err, func() (A, error) {
return runtime.NewRuntimeAnalyzer(common.RuntimeEmerald, &cfg.Source, cfg.Analyzers.Emerald, client, logger)
})
}
if cfg.Analyzers.Sapphire != nil {
analyzers, err = addAnalyzer(analyzers, err, func() (A, error) {
return runtime.NewRuntimeAnalyzer(common.RuntimeSapphire, &cfg.Source, cfg.Analyzers.Sapphire, client, logger)
})
}
if cfg.Analyzers.Cipher != nil {
analyzers, err = addAnalyzer(analyzers, err, func() (A, error) {
return runtime.NewRuntimeAnalyzer(common.RuntimeCipher, &cfg.Source, cfg.Analyzers.Cipher, client, logger)
})
}
if cfg.Analyzers.EmeraldEvmTokens != nil {
analyzers, err = addAnalyzer(analyzers, err, func() (A, error) {
return evmtokens.NewMain(common.RuntimeEmerald, &cfg.Source, client, logger)
})
}
if cfg.Analyzers.SapphireEvmTokens != nil {
analyzers, err = addAnalyzer(analyzers, err, func() (A, error) {
return evmtokens.NewMain(common.RuntimeSapphire, &cfg.Source, client, logger)
})
}
if cfg.Analyzers.EmeraldEvmTokenBalances != nil {
analyzers, err = addAnalyzer(analyzers, err, func() (A, error) {
return evmtokenbalances.NewMain(common.RuntimeEmerald, &cfg.Source, client, logger)
})
}
if cfg.Analyzers.SapphireEvmTokenBalances != nil {
analyzers, err = addAnalyzer(analyzers, err, func() (A, error) {
return evmtokenbalances.NewMain(common.RuntimeSapphire, &cfg.Source, client, logger)
})
}
if cfg.Analyzers.MetadataRegistry != nil {
analyzers, err = addAnalyzer(analyzers, err, func() (A, error) {
return analyzer.NewMetadataRegistryAnalyzer(cfg.Analyzers.MetadataRegistry, client, logger)
})
}
if cfg.Analyzers.AggregateStats != nil {
analyzers, err = addAnalyzer(analyzers, err, func() (A, error) {
return analyzer.NewAggregateStatsAnalyzer(cfg.Analyzers.AggregateStats, client, logger)
})
}
if err != nil {
return nil, err
}
logger.Info("initialized all analyzers")
return &Service{
Analyzers: analyzers,
target: client,
logger: logger,
}, nil
}
// Start starts the analysis service.
func (a *Service) Start() {
a.logger.Info("starting analysis service")
var wg sync.WaitGroup
for _, an := range a.Analyzers {
wg.Add(1)
go func(an analyzer.Analyzer) {
defer wg.Done()
an.Start()
}(an)
}
wg.Wait()
}
// Shutdown gracefully shuts down the service.
func (a *Service) Shutdown() {
a.target.Shutdown()
}
// Register registers the process sub-command.
func Register(parentCmd *cobra.Command) {
analyzeCmd.Flags().StringVar(&configFile, "config", "./config/local.yml", "path to the config.yml file")
parentCmd.AddCommand(analyzeCmd)
}