This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
350 lines (305 loc) · 9.4 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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
package main
import (
"context"
"net/url"
"os"
"sync"
"sync/atomic"
"time"
"github.com/goccy/go-json"
"github.com/gorilla/websocket"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/vistastaking/ssv-indexer/db"
"go.uber.org/zap"
)
const maxConnections = 100
type connectionPool struct {
connections chan *websocket.Conn
activeConnections int32
logger *zap.Logger
}
func newConnectionPool(logger *zap.Logger) *connectionPool {
pool := &connectionPool{
connections: make(chan *websocket.Conn, maxConnections),
logger: logger,
}
// Prepopulate the pool
for i := 0; i < maxConnections; i++ {
conn, err := createNewConnection(pool.logger)
if err != nil {
pool.logger.Error("Failed to create a new connection during prepopulation", zap.Error(err))
continue
}
pool.connections <- conn
atomic.AddInt32(&pool.activeConnections, 1)
}
return pool
}
func (p *connectionPool) getConnection() (*websocket.Conn, error) {
select {
case conn := <-p.connections:
// Reusing an existing connection.
return conn, nil
default:
// Need to create a new connection if we haven't reached the max limit.
if atomic.LoadInt32(&p.activeConnections) < maxConnections {
conn, err := createNewConnection(p.logger)
if err != nil {
p.logger.Error("Failed to create a new connection", zap.Error(err))
return nil, err
}
// Successfully created a new connection, so increment the counter.
atomic.AddInt32(&p.activeConnections, 1)
return conn, nil
}
// If we've reached the max limit, block until a connection becomes available.
conn := <-p.connections
return conn, nil
}
}
func (p *connectionPool) releaseConnection(conn *websocket.Conn) {
select {
case p.connections <- conn:
// Successfully returned the connection to the pool.
default:
// Pool is full; close the connection and decrement the counter.
conn.Close()
atomic.AddInt32(&p.activeConnections, -1)
p.logger.Info("Closed a connection because the pool is full", zap.Int32("activeConnections", atomic.LoadInt32(&p.activeConnections)))
}
}
func createNewConnection(logger *zap.Logger) (*websocket.Conn, error) {
queryURL := url.URL{Scheme: "ws", Host: "localhost:16000", Path: "/query"}
conn, _, err := websocket.DefaultDialer.Dial(queryURL.String(), nil)
if err != nil {
logger.Error("Failed to dial a new WebSocket connection", zap.Error(err))
return nil, err
}
return conn, nil
}
func main() {
logger, err := zap.NewProduction()
if err != nil {
panic(err)
}
defer logger.Sync()
dbConn, err := pgxpool.New(context.Background(), os.Getenv("DATABASE_URL"))
if err != nil {
logger.Fatal("Unable to connect to database", zap.Error(err))
}
defer dbConn.Close()
queries := db.New(dbConn)
streamURL := url.URL{Scheme: "ws", Host: "localhost:16000", Path: "/stream"}
logger.Info("Connecting to WebSocket for real-time data", zap.String("url", streamURL.String()))
streamConn, _, err := websocket.DefaultDialer.Dial(streamURL.String(), nil)
if err != nil {
logger.Fatal("Error connecting to websocket for real-time data", zap.Error(err))
}
defer streamConn.Close()
_, message, err := streamConn.ReadMessage()
if err != nil {
logger.Fatal("Error reading message from WebSocket", zap.Error(err))
}
var response WebSocketResponse
err = json.Unmarshal(message, &response)
if err != nil {
logger.Fatal("Error parsing JSON", zap.Error(err))
}
// Start real-time data listener in a new goroutine
go func() {
logger.Info("Listening for real-time data...")
for {
_, message, err := streamConn.ReadMessage()
if err != nil {
logger.Error("Error reading message from WebSocket", zap.Error(err),
zap.String("source", "real-time"))
continue
}
var response WebSocketResponse
err = json.Unmarshal(message, &response)
if err != nil {
logger.Error("Error parsing JSON", zap.Error(err),
zap.String("source", "real-time"))
continue
}
processResponse(response, queries, logger, "real-time")
}
}()
historicalSyncStart := time.Now()
publicKeys := []string{
"839d8cb89121aa3cfbd5c6f2cb44611e7689bd8d43c45c19398adcfbe7085404093ba2cb46f32d7b246099fef3ba1835",
"b2e5fb301adcf8470b7e72e011aa9e453aea25a6928522257fde9c772f5471626a2a44d165e254ab1eee64a0fa322007",
"ac649ae2598870994d8da99c1db613d8829498abd723bcbffd947c09b86caa69ec461a56bdf09e3960e5987847a5c76a",
}
startIndex := 1389687
endIndex := response.Filter.From
if endIndex < startIndex {
logger.Fatal("Invalid start and end indices",
zap.Int("startIndex", startIndex),
zap.Int("endIndex", endIndex),
)
}
logger.Info("Starting indexing process in 5 seconds...",
zap.Int("startIndex", startIndex),
zap.Int("endIndex", endIndex),
)
time.Sleep(5 * time.Second)
type request struct {
payload []byte
}
totalRequests := int32(len(publicKeys) * (endIndex - startIndex))
requestC := make(chan request, totalRequests)
connPool := newConnectionPool(logger)
var remainingRequests int32 = totalRequests
var wg sync.WaitGroup
for i := 0; i < maxConnections; i++ {
wg.Add(1)
go func() {
defer wg.Done()
for req := range requestC {
conn, err := connPool.getConnection()
if err != nil {
logger.Error("Failed to get connection from pool", zap.Error(err))
continue
}
err = conn.WriteMessage(websocket.TextMessage, req.payload)
if err != nil {
logger.Error("Failed to write message", zap.Error(err))
connPool.releaseConnection(conn)
continue
}
_, message, err := conn.ReadMessage()
if err != nil {
logger.Error("Failed to read message", zap.Error(err))
connPool.releaseConnection(conn)
continue
}
var response WebSocketResponse
if err := json.Unmarshal(message, &response); err != nil {
logger.Error("Failed to unmarshal response", zap.Error(err))
connPool.releaseConnection(conn)
continue
}
processResponse(response, queries, logger, "historical")
connPool.releaseConnection(conn)
atomic.AddInt32(&remainingRequests, -1)
logger.Info("Processed request",
zap.Int32("remainingRequests", atomic.LoadInt32(&remainingRequests)),
zap.Int32("totalRequests", totalRequests),
)
}
}()
}
// Generate requests
for _, publicKey := range publicKeys {
for i := startIndex; i < endIndex; i++ {
payload, err := json.Marshal(map[string]interface{}{
"type": "decided",
"filter": map[string]interface{}{
"publicKey": publicKey,
"role": "ATTESTER",
"from": i,
"to": i,
},
})
if err != nil {
logger.Error("Failed to marshal payload", zap.Error(err))
continue
}
requestC <- request{
payload: payload,
}
}
}
close(requestC) // No more requests to send, close the channel
wg.Wait() // Wait for all response processing goroutines to finish
logger.Info("Finished syncing historical data",
zap.Int("startIndex", startIndex),
zap.Int("endIndex", endIndex),
zap.Duration("durationSeconds", time.Since(historicalSyncStart)),
)
// Prevent main from exiting to keep real-time listener running
select {}
}
func processResponse(response WebSocketResponse, queries *db.Queries, logger *zap.Logger, source string) {
if response.Type == "decided" {
publicKey := response.Filter.PublicKey
from := response.Filter.From
to := response.Filter.To
role := response.Filter.Role
var data []Data
var dataStr []string
err := json.Unmarshal(response.Data, &data)
if err != nil {
err = json.Unmarshal(response.Data, &dataStr)
if err != nil {
logger.Error("Error parsing data field", zap.Error(err),
zap.String("publicKey", publicKey),
zap.Int("from", from),
zap.Int("to", to),
zap.String("role", role),
zap.String("source", source))
return
}
if len(dataStr) > 0 && dataStr[0] == "no messages" {
return
}
}
if len(data) > 0 {
signers := data[0].Signers
signature := data[0].Signature
round := data[0].Message.Round
root := data[0].Message.Root
height := data[0].Message.Height
arg := db.CreateIndexedDataParams{
PublicKey: publicKey,
FromValue: int32(from),
ToValue: int32(to),
Role: role,
Signers: int32SliceToInt32Array(signers),
Signature: signature,
Round: int32(round),
Root: int32SliceToInt32Array(root),
Height: int32(height),
}
err := queries.CreateIndexedData(context.Background(), arg)
if err != nil {
logger.Error("Error inserting data into database", zap.Error(err),
zap.String("publicKey", publicKey),
zap.Int("from", from),
zap.Int("to", to),
zap.String("role", role),
zap.String("source", source))
} else {
logger.Info("New entry stored in the database", zap.Any("data", arg),
zap.String("source", source))
}
} else {
arg := db.CreateIndexedDataParams{
PublicKey: publicKey,
FromValue: int32(from),
ToValue: int32(to),
Role: role,
}
err := queries.CreateIndexedData(context.Background(), arg)
if err != nil {
logger.Error("Error inserting data into database", zap.Error(err),
zap.String("publicKey", publicKey),
zap.Int("from", from),
zap.Int("to", to),
zap.String("role", role),
zap.String("source", source))
} else {
logger.Info("New entry stored in the database", zap.Any("data", arg),
zap.String("source", source))
}
}
}
}
func int32SliceToInt32Array(slice []int) []int32 {
result := make([]int32, len(slice))
for i, value := range slice {
result[i] = int32(value)
}
return result
}