-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·153 lines (132 loc) · 4.35 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
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/aergoio/aergo-indexer-2.0/indexer"
doc "github.com/aergoio/aergo-indexer-2.0/indexer/documents"
"github.com/aergoio/aergo-lib/log"
"github.com/spf13/cobra"
)
var (
rootCmd = &cobra.Command{
Use: "indexer",
Short: "Aergo Indexer",
Long: "Aergo Metadata Indexer",
Run: rootRun,
}
runMode string
checkMode bool
onsyncMode bool
fix bool
host string
port int32
dbURL string
prefix string
aergoAddress string
cluster bool
from uint64
to uint64
cccvNftServerType string
balanceWhitelist []string
tokenVerifyAddress string
contractVerifyAddress string
tokenVerifyWhitelist []string
contractVerifyWhitelist []string
logger *log.Logger
)
func init() {
fs := rootCmd.PersistentFlags()
fs.StringVarP(&host, "host", "H", "localhost", "host address of aergo server")
fs.Int32VarP(&port, "port", "p", 7845, "port number of aergo server")
fs.StringVarP(&aergoAddress, "aergo", "A", "", "host and port of aergo server. Alternative to setting host and port separately")
fs.StringVarP(&dbURL, "dburl", "E", "localhost:9200", "Database URL")
fs.StringVarP(&prefix, "prefix", "P", "testnet", "index name prefix")
fs.BoolVarP(&cluster, "cluster", "C", false, "elasticsearch cluster type")
fs.BoolVar(&fix, "fix", false, "fix mode to overwrite data")
fs.BoolVar(&checkMode, "check", true, "check indices of range of heights")
fs.BoolVar(&onsyncMode, "onsync", true, "onsync data in indices")
fs.StringVarP(&runMode, "mode", "M", "", "indexer running mode(all,check,onsync) Alternative to setting check, onsync separately")
fs.Uint64Var(&from, "from", 0, "start syncing from this block number")
fs.Uint64Var(&to, "to", 0, "stop syncing at this block number")
fs.StringVar(&cccvNftServerType, "cccv", "", "indexing cccv nft by network type.(mainnet,testnet)")
fs.StringVarP(&tokenVerifyAddress, "token", "t", "", "address for query verified token")
fs.StringVarP(&contractVerifyAddress, "contract", "c", "", "address for query contract code")
fs.StringSliceVarP(&balanceWhitelist, "balance_whitelist", "W", []string{}, "whitelist for update account balance")
fs.StringArrayVar(&tokenVerifyWhitelist, "token_whitelist", []string{}, "whitelist for update verified token")
fs.StringArrayVar(&contractVerifyWhitelist, "contract_whitelist", []string{}, "whitelist for update verified contract")
}
func main() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}
func rootRun(cmd *cobra.Command, args []string) {
logger = log.NewLogger("indexer")
logger.Info().Msg("Starting indexer for SCAN 2.0 ...")
doc.InitEsMappings(cluster)
// init indexer
indx, err := indexer.NewIndexer(
indexer.SetServerAddr(getServerAddress()),
indexer.SetDBAddr(dbURL),
indexer.SetPrefix(prefix),
indexer.SetNetworkTypeForCccv(cccvNftServerType),
indexer.SetRunMode(getRunMode()),
indexer.SetFix(fix),
indexer.SetLogger(logger),
indexer.SetWhiteListAddresses(balanceWhitelist),
indexer.SetTokenVerifyAddress(tokenVerifyAddress),
indexer.SetContractVerifyAddress(contractVerifyAddress),
indexer.SetTokenVerifyWhitelist(tokenVerifyWhitelist),
indexer.SetContractVerifyWhitelist(contractVerifyWhitelist),
)
if err != nil {
logger.Warn().Err(err).Msg("Could not start indexer")
return
}
// start indexer
exitOnComplete := indx.Start(from, to)
if exitOnComplete == true {
return
}
interrupt := handleKillSig(func() {
indx.Stop()
}, logger)
// Wait main routine to stop
<-interrupt.C
}
func getServerAddress() string {
if len(aergoAddress) > 0 {
return aergoAddress
}
return fmt.Sprintf("%s:%d", host, port)
}
func getRunMode() string {
if len(runMode) > 0 {
return runMode
} else if onsyncMode && checkMode {
return "all"
} else if checkMode {
return "check"
}
return "onsync"
}
type interrupt struct {
C chan struct{}
}
func handleKillSig(handler func(), logger *log.Logger) interrupt {
i := interrupt{
C: make(chan struct{}),
}
sigChannel := make(chan os.Signal, 1)
signal.Notify(sigChannel, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGTERM)
go func() {
for signal := range sigChannel {
logger.Info().Msgf("Receive signal %s, Shutting down...", signal)
handler()
close(i.C)
}
}()
return i
}