-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
172 lines (155 loc) · 5.39 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package main
import (
"fmt"
"os"
"os/signal"
"syscall"
"github.com/aurora-is-near/relayer2-base/cmd"
"github.com/aurora-is-near/relayer2-base/db"
"github.com/aurora-is-near/relayer2-base/db/badger"
commonEndpoint "github.com/aurora-is-near/relayer2-base/endpoint"
"github.com/aurora-is-near/relayer2-base/endpoint/processor"
"github.com/aurora-is-near/relayer2-base/indexer/prehistory"
"github.com/aurora-is-near/relayer2-base/indexer/tar"
"github.com/aurora-is-near/relayer2-base/log"
"github.com/aurora-is-near/relayer2-base/rpc/node"
publicCmd "github.com/aurora-is-near/relayer2-public/cmd"
"github.com/aurora-is-near/relayer2-public/endpoint"
"github.com/aurora-is-near/relayer2-public/indexer"
"github.com/aurora-is-near/relayer2-public/middleware"
"github.com/aurora-is-near/relayer2-public/standaloneproxy"
"github.com/fsnotify/fsnotify"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
func main() {
c := cmd.RootCmd()
c.AddCommand(cmd.VersionCmd())
c.AddCommand(publicCmd.AddKeysCmd())
c.AddCommand(publicCmd.DeleteKeysCmd())
c.AddCommand(publicCmd.GenerateKeysCmd())
c.AddCommand(cmd.GetBlockCmd())
c.AddCommand(cmd.FlattenDB())
c.AddCommand(cmd.StartCmd(func(cmd *cobra.Command, args []string) {
logger := log.Log()
bh, err := badger.NewBlockHandler()
if err != nil {
logger.Fatal().Err(err).Msg("failed to start block handler")
}
fh, err := badger.NewFilterHandler()
if err != nil {
logger.Fatal().Err(err).Msg("failed to start filter handler")
}
handler := db.StoreHandler{
BlockHandler: bh,
FilterHandler: fh,
}
defer handler.Close()
// create json-rpc node
rpcNode, err := node.New()
if err != nil {
logger.Fatal().Err(err).Msg("failed to create json-rpc server")
}
baseEndpoint := commonEndpoint.New(handler)
// order of processors are important
baseEndpoint.WithProcessor(processor.NewEnableDisable())
p, err := standaloneproxy.New()
baseEndpoint.WithProcessor(p)
defer p.Close()
if err != nil {
logger.Err(err).Msg("failed to set up standalone proxy")
}
ethEndpoint := commonEndpoint.NewEth(baseEndpoint)
ethProcessor := commonEndpoint.NewEthProcessorAware(ethEndpoint)
netEndpoint := commonEndpoint.NewNet(baseEndpoint)
netProcessor := commonEndpoint.NewNetProcessorAware(netEndpoint)
web3Endpoint := commonEndpoint.NewWeb3(baseEndpoint)
web3Processor := commonEndpoint.NewWeb3ProcessorAware(web3Endpoint)
parityEndpoint := commonEndpoint.NewParity(baseEndpoint)
parityProcessor := commonEndpoint.NewParityProcessorAware(parityEndpoint)
debugEndpoint := commonEndpoint.NewDebug(baseEndpoint)
debugProcessor := commonEndpoint.NewDebugProcessorAware(debugEndpoint)
eventsEndpoint := endpoint.NewEventsEth(baseEndpoint, rpcNode.Broker)
engineEth := endpoint.NewEngineEth(baseEndpoint)
engineNet := endpoint.NewEngineNet(engineEth)
defer engineEth.Close()
// use the endpoint services to json-rpc server
rpcNode.WithMiddleware(middleware.FilterIP())
rpcNode.WithMiddleware(middleware.Proxy(baseEndpoint))
// register endpoints and events to json-rpc server
err = rpcNode.RegisterEndpoints("eth", ethProcessor)
if err != nil {
logger.Fatal().Err(err).Msg("failed to register `eth` endpoints")
}
err = rpcNode.RegisterEndpoints("net", netProcessor)
if err != nil {
logger.Fatal().Err(err).Msg("failed to register `net` endpoints")
}
err = rpcNode.RegisterEndpoints("web3", web3Processor)
if err != nil {
logger.Fatal().Err(err).Msg("failed to register `web3` endpoints")
}
err = rpcNode.RegisterEndpoints("parity", parityProcessor)
if err != nil {
logger.Fatal().Err(err).Msg("failed to register `parity` endpoints")
}
err = rpcNode.RegisterEndpoints("debug", debugProcessor)
if err != nil {
logger.Fatal().Err(err).Msg("failed to register `debug` endpoints")
}
err = rpcNode.RegisterEndpoints("eth", engineEth)
if err != nil {
logger.Fatal().Err(err).Msg("failed to register `eth` endpoints for engine")
}
err = rpcNode.RegisterEndpoints("net", engineNet)
if err != nil {
logger.Fatal().Err(err).Msg("failed to register `net` endpoints for engine")
}
err = rpcNode.RegisterEvents("eth", eventsEndpoint)
if err != nil {
logger.Fatal().Err(err).Msg("failed to register `eth` events")
}
rpcNode.Start()
defer rpcNode.Close()
var tarIndexer *tar.Indexer
tarIndexer, err = tar.New(handler)
if err != nil {
logger.Fatal().Err(err).Msg("failed to start tar indexer")
}
if tarIndexer != nil {
tarIndexer.Start()
defer tarIndexer.Close()
}
var indxr *indexer.Indexer
if rpcNode.Broker != nil {
indxr, err = indexer.NewWithBroker(handler, rpcNode.Broker)
} else {
indxr, err = indexer.New(handler)
}
if err != nil {
logger.Fatal().Err(err).Msg("failed to start indexer")
}
indxr.Start()
defer indxr.Close()
preIndxr, err := prehistory.New(handler)
if err != nil {
logger.Fatal().Err(err).Msg("failed to start indexer")
}
if preIndxr != nil {
preIndxr.Start()
defer preIndxr.Close()
}
// Set the handlers for components that needs to updated after config changes
viper.OnConfigChange(func(e fsnotify.Event) {
logger.HandleConfigChange()
baseEndpoint.HandleConfigChange()
})
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
<-sig
}))
if err := c.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "There was an error while executing Relayer CLI '%s'", err)
os.Exit(1)
}
}