forked from flare-foundation/flare-system-c-chain-indexer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
112 lines (93 loc) · 2.46 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
package main
import (
"context"
"flag"
"flare-ftso-indexer/config"
"flare-ftso-indexer/database"
"flare-ftso-indexer/indexer"
"flare-ftso-indexer/logger"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/ava-labs/coreth/ethclient"
"github.com/pkg/errors"
"gorm.io/gorm"
)
func main() {
if err := run(context.Background()); err != nil {
logger.Fatal("Fatal error: %s", err)
}
}
func run(ctx context.Context) error {
flag.Parse()
cfg, err := config.BuildConfig()
if err != nil {
return errors.Wrap(err, "config error")
}
config.GlobalConfigCallback.Call(cfg)
ethClient, err := dialRPCNode(cfg)
if err != nil {
return errors.Wrap(err, "Could not connect to the RPC nodes")
}
db, err := database.ConnectAndInitialize(ctx, &cfg.DB)
if err != nil {
return errors.Wrap(err, "Database connect and initialize errors")
}
if cfg.DB.HistoryDrop > 0 {
startIndex, err := database.GetMinBlockWithHistoryDrop(
ctx, cfg.Indexer.StartIndex, cfg.DB.HistoryDrop, ethClient,
)
if err != nil {
return errors.Wrap(err, "Could not set the starting indexs")
}
if startIndex != cfg.Indexer.StartIndex {
logger.Info("Setting new startIndex due to history drop: %d", startIndex)
cfg.Indexer.StartIndex = startIndex
}
}
return runIndexer(ctx, cfg, db, ethClient)
}
func dialRPCNode(cfg *config.Config) (ethclient.Client, error) {
nodeURL, err := cfg.Chain.FullNodeURL()
if err != nil {
return nil, err
}
return ethclient.Dial(nodeURL.String())
}
func runIndexer(ctx context.Context, cfg *config.Config, db *gorm.DB, ethClient ethclient.Client) error {
cIndexer, err := indexer.CreateBlockIndexer(cfg, db, ethClient)
if err != nil {
return err
}
bOff := backoff.NewExponentialBackOff()
err = backoff.RetryNotify(
func() error {
return cIndexer.IndexHistory(ctx)
},
bOff,
func(err error, d time.Duration) {
logger.Error("Index history error: %s. Will retry after %s", err, d)
},
)
if err != nil {
return errors.Wrap(err, "Index history fatal error")
}
if cfg.DB.HistoryDrop > 0 {
go database.DropHistory(
ctx, db, cfg.DB.HistoryDrop, database.HistoryDropIntervalCheck, ethClient,
)
}
err = backoff.RetryNotify(
func() error {
return cIndexer.IndexContinuous(ctx)
},
bOff,
func(err error, d time.Duration) {
logger.Error("Index continuous error: %s. Will retry after %s", err, d)
},
)
if err != nil {
return errors.Wrap(err, "Index continuous fatal error")
}
logger.Info("Finished indexing")
return nil
}