2023-01-05 12:45:09 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"flag"
|
2023-01-06 12:32:20 +01:00
|
|
|
"strings"
|
2023-01-05 12:45:09 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2023-02-24 11:28:30 +01:00
|
|
|
"github.com/grassrootseconomics/cic-chain-events/internal/filter"
|
2023-01-05 12:45:09 +01:00
|
|
|
"github.com/grassrootseconomics/cic-chain-events/internal/pipeline"
|
2023-03-08 15:30:40 +01:00
|
|
|
"github.com/grassrootseconomics/cic-chain-events/internal/pub"
|
2023-01-05 12:45:09 +01:00
|
|
|
"github.com/grassrootseconomics/cic-chain-events/internal/syncer"
|
|
|
|
"github.com/knadh/goyesql/v2"
|
2023-03-08 15:30:40 +01:00
|
|
|
"github.com/knadh/koanf/v2"
|
|
|
|
"github.com/labstack/echo/v4"
|
2023-01-05 12:45:09 +01:00
|
|
|
"github.com/zerodha/logf"
|
|
|
|
)
|
|
|
|
|
2023-03-08 15:30:40 +01:00
|
|
|
type (
|
|
|
|
internalServiceContainer struct {
|
|
|
|
apiService *echo.Echo
|
|
|
|
pub *pub.Pub
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-01-05 12:45:09 +01:00
|
|
|
var (
|
2023-03-08 15:30:40 +01:00
|
|
|
build string
|
|
|
|
|
|
|
|
confFlag string
|
|
|
|
debugFlag bool
|
|
|
|
migrationsFolderFlag string
|
|
|
|
queriesFlag string
|
2023-01-05 12:45:09 +01:00
|
|
|
|
|
|
|
ko *koanf.Koanf
|
|
|
|
lo logf.Logger
|
|
|
|
q goyesql.Queries
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
flag.StringVar(&confFlag, "config", "config.toml", "Config file location")
|
2023-03-08 15:30:40 +01:00
|
|
|
flag.BoolVar(&debugFlag, "debug", false, "Enable debug logging")
|
|
|
|
flag.StringVar(&migrationsFolderFlag, "migrations", "migrations/", "Migrations folder location")
|
2023-01-05 12:45:09 +01:00
|
|
|
flag.StringVar(&queriesFlag, "queries", "queries.sql", "Queries file location")
|
|
|
|
flag.Parse()
|
|
|
|
|
2023-03-08 15:30:40 +01:00
|
|
|
lo = initLogger()
|
|
|
|
ko = initConfig()
|
2023-01-05 12:45:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2023-03-08 15:30:40 +01:00
|
|
|
lo.Info("main: starting cic-chain-events", "build", build)
|
2023-02-24 11:28:30 +01:00
|
|
|
|
2023-03-08 15:30:40 +01:00
|
|
|
parsedQueries := initQueries(queriesFlag)
|
2023-01-05 12:45:09 +01:00
|
|
|
graphqlFetcher := initFetcher()
|
2023-03-08 15:30:40 +01:00
|
|
|
pgStore := initPgStore(migrationsFolderFlag, parsedQueries)
|
|
|
|
natsConn, jsCtx := initJetStream()
|
|
|
|
jsPub := initPub(natsConn, jsCtx)
|
2023-01-05 12:45:09 +01:00
|
|
|
|
|
|
|
pipeline := pipeline.NewPipeline(pipeline.PipelineOpts{
|
|
|
|
BlockFetcher: graphqlFetcher,
|
|
|
|
Filters: []filter.Filter{
|
2023-01-06 12:32:20 +01:00
|
|
|
initAddressFilter(),
|
2023-03-08 15:30:40 +01:00
|
|
|
initGasGiftFilter(jsPub),
|
|
|
|
initTransferFilter(jsPub),
|
|
|
|
initRegisterFilter(jsPub),
|
2023-01-05 12:45:09 +01:00
|
|
|
},
|
|
|
|
Logg: lo,
|
|
|
|
Store: pgStore,
|
|
|
|
})
|
|
|
|
|
2023-03-08 15:30:40 +01:00
|
|
|
internalServices := &internalServiceContainer{
|
|
|
|
pub: jsPub,
|
|
|
|
}
|
|
|
|
syncerStats := &syncer.Stats{}
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
|
|
|
|
signalCh, closeCh := createSigChannel()
|
|
|
|
defer closeCh()
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
2023-01-18 20:40:14 +01:00
|
|
|
|
2023-01-05 12:45:09 +01:00
|
|
|
headSyncer, err := syncer.NewHeadSyncer(syncer.HeadSyncerOpts{
|
|
|
|
Logg: lo,
|
|
|
|
Pipeline: pipeline,
|
2023-03-08 15:30:40 +01:00
|
|
|
Pool: initHeadSyncerWorkerPool(ctx),
|
2023-01-05 12:45:09 +01:00
|
|
|
Stats: syncerStats,
|
|
|
|
WsEndpoint: ko.MustString("chain.ws_endpoint"),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2023-01-11 09:13:59 +01:00
|
|
|
lo.Fatal("main: crticial error loading head syncer", "error", err)
|
2023-01-05 12:45:09 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
janitor := syncer.NewJanitor(syncer.JanitorOpts{
|
2023-01-18 20:40:14 +01:00
|
|
|
BatchSize: uint64(ko.MustInt64("syncer.janitor_queue_size")),
|
2023-01-05 12:45:09 +01:00
|
|
|
Logg: lo,
|
|
|
|
Pipeline: pipeline,
|
2023-03-08 15:30:40 +01:00
|
|
|
Pool: initJanitorWorkerPool(ctx),
|
2023-01-05 12:45:09 +01:00
|
|
|
Stats: syncerStats,
|
|
|
|
Store: pgStore,
|
2023-01-18 20:40:14 +01:00
|
|
|
SweepInterval: time.Second * time.Duration(ko.MustInt64("syncer.janitor_sweep_interval")),
|
2023-01-05 12:45:09 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := headSyncer.Start(ctx); err != nil {
|
2023-03-08 15:30:40 +01:00
|
|
|
lo.Info("main: starting head syncer")
|
2023-01-11 09:13:59 +01:00
|
|
|
lo.Fatal("main: critical error starting head syncer", "error", err)
|
2023-01-05 12:45:09 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2023-03-08 15:30:40 +01:00
|
|
|
lo.Info("main: starting janitor")
|
2023-01-05 12:45:09 +01:00
|
|
|
if err := janitor.Start(ctx); err != nil {
|
2023-01-11 09:13:59 +01:00
|
|
|
lo.Fatal("main: critical error starting janitor", "error", err)
|
2023-01-05 12:45:09 +01:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-03-08 15:30:40 +01:00
|
|
|
internalServices.apiService = initApiServer()
|
2023-01-06 12:32:20 +01:00
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2023-03-08 15:30:40 +01:00
|
|
|
host := ko.MustString("service.address")
|
|
|
|
lo.Info("main: starting API server", "host", host)
|
|
|
|
if err := internalServices.apiService.Start(host); err != nil {
|
2023-01-06 12:32:20 +01:00
|
|
|
if strings.Contains(err.Error(), "Server closed") {
|
2023-01-11 09:13:59 +01:00
|
|
|
lo.Info("main: shutting down server")
|
2023-01-06 12:32:20 +01:00
|
|
|
} else {
|
2023-01-11 09:13:59 +01:00
|
|
|
lo.Fatal("main: critical error shutting down server", "err", err)
|
2023-01-05 12:45:09 +01:00
|
|
|
}
|
2023-01-06 12:32:20 +01:00
|
|
|
}
|
|
|
|
}()
|
2023-01-05 12:45:09 +01:00
|
|
|
|
2023-03-08 15:30:40 +01:00
|
|
|
lo.Info("main: graceful shutdown triggered", "signal", <-signalCh)
|
|
|
|
cancel()
|
|
|
|
startGracefulShutdown(context.Background(), internalServices)
|
2023-01-06 12:32:20 +01:00
|
|
|
|
|
|
|
wg.Wait()
|
2023-01-05 12:45:09 +01:00
|
|
|
}
|