-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
229 lines (176 loc) · 6.32 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package main
import (
"context"
"fmt"
"log"
"math/big"
"sync"
"time"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/common"
"github.com/diadata-org/oracle-monitoring/internal/config"
"github.com/diadata-org/oracle-monitoring/internal/database"
"github.com/diadata-org/oracle-monitoring/internal/helpers"
"github.com/diadata-org/oracle-monitoring/internal/scraper"
)
var allOracles []string
func main() {
db := database.NewPostgresDB()
if err := db.Connect(); err != nil {
log.Fatalf("failed to connect to the database: %v", err)
}
defer db.Close()
rpcmap, err := db.GetRPCByChainID()
if err != nil {
log.Printf("failed to get rpcmap: %v", err)
return
}
wsurlmap, err := db.GetWSByChainID()
if err != nil {
log.Printf("failed to get rpcmap: %v", err)
return
}
log.Println("starting historical")
for chainid := range rpcmap {
go runScraper(db, chainid, true, rpcmap, wsurlmap)
go runEventScraper(db, chainid, true, rpcmap, wsurlmap)
}
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
for range ticker.C {
for chainid := range rpcmap {
go runScraper(db, chainid, false, rpcmap, wsurlmap)
}
}
}
func runEventScraper(db database.Database, chainID string, isHistorical bool, rpcmap, wsurlmap map[string]string) {
var wg sync.WaitGroup
metricsChan := make(chan helpers.OracleMetrics)
updateEventChan := make(chan helpers.OracleUpdateEvent)
oracles, err := getOracles(db, chainID)
if err != nil {
log.Fatalf("failed to get oracles: %v", err)
}
if len(oracles) > 0 {
fmt.Printf("\n Event based Scrapping started for chain %s, total oracles %d isHistorical %t", chainID, len(oracles), isHistorical)
ctx := context.Background()
sc := scraper.NewScraper(ctx, metricsChan, updateEventChan, rpcmap, wsurlmap, big.NewInt(0), big.NewInt(0), oracles, &wg, chainID)
oraclesArray := []common.Address{}
var latest time.Time
go func() {
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
for range ticker.C {
oracles, err := getOraclesByCreationTime(db, chainID, latest)
if err != nil {
fmt.Println("getOraclesByCreationTime err", err)
continue
}
fmt.Println("oracles added", len(oracles))
for _, oracle := range oracles {
if latest.Before(oracle.CreatedDate) {
latest = oracle.CreatedDate
}
oraclesArray = append(oraclesArray, oracle.ContractAddress)
}
sc.UpdateEvents(oraclesArray)
sc.UpdateDeployedDate(oracles)
}
}()
go processMetrics(db, metricsChan)
go processCreation(db, updateEventChan)
}
}
func runScraper(db database.Database, chainID string, isHistorical bool, rpcmap, wsurlmap map[string]string) {
var wg sync.WaitGroup
metricsChan := make(chan helpers.OracleMetrics)
updateEventChan := make(chan helpers.OracleUpdateEvent)
oracles, err := getOracles(db, chainID)
if err != nil {
log.Fatalf("failed to get oracles: %v", err)
}
if len(oracles) > 0 {
minimum, maximum := calculateMinMaxBlocks(oracles)
fmt.Printf("\n Scrapping started for chain %s, up to minimum block %s, maximum block %s and total oracles %d isHistorical %t", chainID, minimum, maximum, len(oracles), isHistorical)
ctx := context.Background()
sc := scraper.NewScraper(ctx, metricsChan, updateEventChan, rpcmap, wsurlmap, minimum, maximum, oracles, &wg, chainID)
if isHistorical {
go sc.UpdateHistorical()
} else {
sc.UpdateRecent()
}
go processMetrics(db, metricsChan)
go processCreation(db, updateEventChan)
}
}
func getOraclesByCreationTime(db database.Database, chainID string, createdtime time.Time) (oracles []helpers.Oracle, err error) {
oracleConfigs, err := db.SelectOraclesWithCreationTime(chainID, createdtime)
if err != nil {
return nil, fmt.Errorf("failed to get the saved metadata from the DB: %v", err)
}
for _, oracleconfig := range oracleConfigs {
var oracle helpers.Oracle
var oracleABI *abi.ABI
oracleABI, err := config.LoadContractAbi(fmt.Sprintf("internal/abi/%s.json", "oracle-v2"))
if err != nil {
return nil, fmt.Errorf("failed to load the oracle contract ABI: %v", err)
}
oracle.NodeUrl = oracleconfig.NodeUrl
oracle.ContractABI = oracleABI
oracle.CreatedDate = oracleconfig.CreatedDate
oracle.ChainID = oracleconfig.ChainId
oracle.ContractAddress = common.HexToAddress(oracleconfig.ContractAddress)
oracle.LatestScrapedBlock = new(big.Int).SetUint64(oracleconfig.LatestScrapedBlock)
oracles = append(oracles, oracle)
}
return oracles, nil
}
func getOracles(db database.Database, chainID string) (oracles []helpers.Oracle, err error) {
oracleConfigs, err := db.SelectOracles(chainID)
if err != nil {
return nil, fmt.Errorf("failed to get the saved metadata from the DB: %v", err)
}
for _, oracleconfig := range oracleConfigs {
var oracle helpers.Oracle
var oracleABI *abi.ABI
oracleABI, err := config.LoadContractAbi(fmt.Sprintf("internal/abi/%s.json", "oracle-v2"))
if err != nil {
return nil, fmt.Errorf("failed to load the oracle contract ABI: %v", err)
}
oracle.NodeUrl = oracleconfig.NodeUrl
oracle.ContractABI = oracleABI
oracle.ChainID = oracleconfig.ChainId
oracle.ContractAddress = common.HexToAddress(oracleconfig.ContractAddress)
oracle.LatestScrapedBlock = new(big.Int).SetUint64(oracleconfig.LatestScrapedBlock)
oracles = append(oracles, oracle)
}
return oracles, nil
}
func calculateMinMaxBlocks(oracles []helpers.Oracle) (*big.Int, *big.Int) {
minimum := new(big.Int).Set(oracles[0].LatestScrapedBlock)
maximum := new(big.Int).Set(oracles[0].LatestScrapedBlock)
for _, oracle := range oracles {
if minimum.Cmp(oracle.LatestScrapedBlock) == 1 {
minimum.Set(oracle.LatestScrapedBlock)
}
if maximum.Cmp(oracle.LatestScrapedBlock) == -1 {
maximum.Set(oracle.LatestScrapedBlock)
}
}
return minimum, maximum
}
func processMetrics(db database.Database, metricsChan chan helpers.OracleMetrics) {
for metrics := range metricsChan {
if err := db.InsertOracleMetrics(&metrics); err != nil {
log.Println("Error inserting oracle metrics:", err)
}
}
}
func processCreation(db database.Database, updateEvent chan helpers.OracleUpdateEvent) {
for ue := range updateEvent {
log.Println("updating oracle creation date", ue)
if err := db.UpdateOracleCreation(ue.Address, ue.Block, ue.BlockTimestamp, ue.ChainID); err != nil {
log.Println("Error inserting oracle creation:", err)
}
}
}