/
main.go
83 lines (68 loc) · 2.48 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
package main
import (
"fmt"
"time"
"github.com/trylotus/connector"
"github.com/trylotus/connector/config"
"github.com/trylotus/connector/kafkautils"
"github.com/trylotus/connectors/flow"
"github.com/onflow/flow-go-sdk/access/grpc"
"github.com/rs/zerolog/log"
"github.com/spf13/pflag"
_ "go.uber.org/automaxprocs"
"google.golang.org/protobuf/proto"
)
func main() {
c, err := connector.NewConnector()
if err != nil {
log.Fatal().Err(err).Msg("failed to instantiate connector")
}
c.Config.SetDefault("rpc.url", grpc.MainnetHost)
c.Config.SetDefault("maxRetry", 7)
c.Config.SetDefault("maxGrpcMsgSize", 64*1024*1024) // 64 MB
c.Config.SetDefault("maxApiUsage", 20)
c.Config.SetDefault("maxWorkerPoolSize", 5)
c.Config.SetDefault("cacheSize", 2000)
c.Config.SetDefault("channelSize", 1000)
c.Config.SetDefault("timeout", 3*time.Minute)
c.Config.SetDefault("reportInterval", 1*time.Minute)
pflag.Int64P("from-block", "f", 0, "block number to start backfill from (optional")
pflag.Int64P("num-blocks", "b", 0, "number of blocks to backfill (optional)")
pflag.Parse()
c.Config.BindPFlags(pflag.CommandLine)
if err := validateFlags(c.Config); err != nil {
log.Fatal().Err(err).Msg("input is not correct")
}
conf := &flow.Config{
Host: c.Config.GetString("rpc.url"),
FromBlock: c.Config.GetUint64("from-block"),
NumBlocks: c.Config.GetUint64("num-blocks"),
MaxGrpcMsgSize: c.Config.GetInt("maxGrpcMsgSize"),
MaxApiUsage: c.Config.GetInt("maxApiUsage"),
MaxWorkerPoolSize: c.Config.GetInt("maxWorkerPoolSize"),
CacheSize: c.Config.GetInt("cacheSize"),
ChannelSize: c.Config.GetInt("channelSize"),
Timeout: c.Config.GetDuration("timeout"),
ReportInterval: c.Config.GetDuration("reportInterval"),
}
var topicTypes []proto.Message
topicTypes = append(topicTypes, &flow.Block{}, &flow.Transaction{})
// Register topic and protobuf type mappings
c.RegisterProtos(kafkautils.MsgTypeFct, topicTypes...)
if conf.FromBlock > 0 || conf.NumBlocks > 0 {
c.RegisterProtos(kafkautils.MsgTypeBf, topicTypes...)
}
m := flow.New(c, conf)
m.Start()
}
func validateFlags(conf config.IConfig) error {
fromBlock := conf.GetInt64("from-block")
numBlocks := conf.GetInt64("num-blocks")
if fromBlock < 0 {
return fmt.Errorf("backfill input value cannot be negative. from-block: %d", fromBlock)
}
if numBlocks < 0 {
return fmt.Errorf("backfill input value cannot be negative. num-blocks: %d", numBlocks)
}
return nil
}