-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunner.go
145 lines (115 loc) · 3.37 KB
/
runner.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
// Package runner is the main function for running our program
package runner
import (
"context"
"sync"
"time"
"github.com/0xPolygon/panoptichain/config"
"github.com/0xPolygon/panoptichain/log"
"github.com/0xPolygon/panoptichain/network"
"github.com/0xPolygon/panoptichain/observer"
"github.com/0xPolygon/panoptichain/provider"
"github.com/0xPolygon/panoptichain/util"
)
var providers []provider.Provider
var observers observer.ObserverSet
// Start will start the main infinite loop of this program.
func Start(ctx context.Context) {
log.Info().Msg("Starting main loop")
var wg sync.WaitGroup
wg.Add(len(providers))
for _, p := range providers {
go func(p provider.Provider) {
defer wg.Done()
for {
if err := p.RefreshState(ctx); err != nil {
log.Error().Err(err).Send()
}
if err := p.PublishEvents(ctx); err != nil {
log.Error().Err(err).Send()
}
util.BlockFor(ctx, time.Second*time.Duration(p.PollingInterval()))
}
}(p)
}
wg.Wait()
}
// Init will configure all the providers and observers of the system.
func Init(ctx context.Context) error {
providers = make([]provider.Provider, 0)
eb := observer.NewEventBus()
var rpcProviders []*provider.RPCProvider
for _, r := range config.Config().Providers.RPCs {
n, err := network.GetNetworkByName(r.Name)
if err != nil {
return err
}
interval := config.Config().Runner.Interval
if r.Interval > 0 {
interval = r.Interval
}
p := provider.NewRPCProvider(provider.RPCProviderOpts{
Network: n,
URL: r.URL,
Label: r.Label,
EventBus: eb,
Interval: interval,
Contracts: r.Contracts,
TimeToMine: r.TimeToMine,
Accounts: r.Accounts,
})
providers = append(providers, p)
rpcProviders = append(rpcProviders, p)
}
if hd := config.Config().Providers.HashDivergence; hd != nil {
interval := config.Config().Runner.Interval
if hd.Interval > 0 {
interval = hd.Interval
}
p := provider.NewHashDivergenceProvider(rpcProviders, eb, interval)
providers = append(providers, p)
}
for _, h := range config.Config().Providers.HeimdallEndpoints {
n, err := network.GetNetworkByName(h.Name)
if err != nil {
return err
}
interval := config.Config().Runner.Interval
if h.Interval > 0 {
interval = h.Interval
}
p := provider.NewHeimdallProvider(n, h.TendermintURL, h.HeimdallURL, h.Label, eb, interval)
providers = append(providers, p)
}
for _, s := range config.Config().Providers.SensorNetworks {
n, err := network.GetNetworkByName(s.Name)
if err != nil {
return err
}
interval := config.Config().Runner.Interval
if s.Interval > 0 {
interval = s.Interval
}
p := provider.NewSensorNetworkProvider(ctx, n, s.Project, s.Database, s.Label, eb, interval)
providers = append(providers, p)
}
if system := config.Config().Providers.System; system != nil {
interval := config.Config().Runner.Interval
if system.Interval > 0 {
interval = system.Interval
}
p := provider.NewSystemProvider(eb, interval)
providers = append(providers, p)
}
if er := config.Config().Providers.ExchangeRates; er != nil {
interval := config.Config().Runner.Interval
if er.Interval > 0 {
interval = er.Interval
}
p := provider.NewExchangeRatesProvider(er.CoinbaseURL, er.Tokens, eb, interval)
providers = append(providers, p)
}
observers = observer.GetEnabledObserverSet()
observers.Register(eb)
return nil
}