-
Notifications
You must be signed in to change notification settings - Fork 2
/
collector.ts
294 lines (269 loc) · 10.6 KB
/
collector.ts
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
import WebSocket from 'ws'
import * as dotenv from 'dotenv'
dotenv.config()
import * as Storage from './storage'
import * as cycle from './storage/cycle'
import * as receipt from './storage/receipt'
import * as crypto from '@shardeum-foundation/lib-crypto-utils'
import * as originalTxData from './storage/originalTxData'
import { Utils as StringUtils } from '@shardeum-foundation/lib-types'
import {
downloadTxsDataAndCycles,
compareWithOldReceiptsData,
compareWithOldCyclesData,
downloadAndSyncGenesisAccounts,
needSyncing,
toggleNeedSyncing,
downloadReceiptsBetweenCycles,
compareWithOldOriginalTxsData,
downloadOriginalTxsDataBetweenCycles,
queryFromDistributor,
DataType,
} from './class/DataSync'
import { sleep } from './utils'
import { validateData } from './class/validateData'
import { DistributorSocketCloseCodes } from './types'
import { config as CONFIG, DISTRIBUTOR_URL, explorerMode } from './config'
import { setupCollectorSocketServer } from './logSubscription/CollectorSocketconnection'
import RMQCyclesConsumer from './collectors/rmq/cycles'
import RMQOriginalTxsConsumer from './collectors/rmq/original_txs'
import RMQReceiptsConsumer from './collectors/rmq/receipts'
// config variables
if (process.env.PORT) {
CONFIG.port.collector = process.env.PORT
}
let ws: WebSocket
let reconnecting = false
let connected = false
let rmqCyclesConsumer: RMQCyclesConsumer
let rmqTransactionsConsumer: RMQOriginalTxsConsumer
let rmqReceiptsConsumer: RMQReceiptsConsumer
const { hashKey, verbose, DISTRIBUTOR_RECONNECT_INTERVAL, CONNECT_TO_DISTRIBUTOR_MAX_RETRY } =
CONFIG
const DistributorFirehoseEvent = 'FIREHOSE'
// eslint-disable-next-line @typescript-eslint/ban-types
export const checkAndSyncData = async (): Promise<Function> => {
let lastStoredReceiptCount = await receipt.queryReceiptCount()
let lastStoredOriginalTxDataCount = await originalTxData.queryOriginalTxDataCount()
let lastStoredCycleCount = await cycle.queryCycleCount()
let totalReceiptsToSync = 0
let totalCyclesToSync = 0
let totalOriginalTxsToSync = 0
let lastStoredReceiptCycle = 0
let lastStoredOriginalTxDataCycle = 0
const response = await queryFromDistributor(DataType.TOTALDATA, {})
if (
response.data &&
response.data.totalReceipts >= 0 &&
response.data.totalCycles >= 0 &&
response.data.totalOriginalTxs >= 0
) {
totalReceiptsToSync = response.data.totalReceipts
totalCyclesToSync = response.data.totalCycles
totalOriginalTxsToSync = response.data.totalOriginalTxs
console.log(
'totalReceiptsToSync',
totalReceiptsToSync,
'totalCyclesToSync',
totalCyclesToSync,
'totalOriginalTxsToSync',
totalOriginalTxsToSync
)
}
console.log(
'lastStoredReceiptCount',
lastStoredReceiptCount,
'lastStoredCycleCount',
lastStoredCycleCount,
'lastStoredOriginalTxDataCount',
lastStoredOriginalTxDataCount
)
// Make sure the data that saved are authentic by comparing receipts count of last 10 cycles for receipts data, originalTxs count of last 10 cycles for originalTxData data and 10 last cycles for cycles data
if (lastStoredReceiptCount > 0) {
const lastStoredReceiptInfo = await receipt.queryLatestReceipts(1)
if (lastStoredReceiptInfo && lastStoredReceiptInfo.length > 0)
lastStoredReceiptCycle = lastStoredReceiptInfo[0].cycle
const receiptResult = await compareWithOldReceiptsData(lastStoredReceiptCycle)
if (!receiptResult.success) {
throw Error(
'The last saved receipts of last 10 cycles data do not match with the archiver data! Clear the DB and start the server again!'
)
}
lastStoredReceiptCycle = receiptResult.matchedCycle
}
if (lastStoredOriginalTxDataCount > 0) {
const lastStoredOriginalTxDataInfo = await originalTxData.queryOriginalTxsData(1)
if (lastStoredOriginalTxDataInfo && lastStoredOriginalTxDataInfo.length > 0)
lastStoredOriginalTxDataCycle = lastStoredOriginalTxDataInfo[0].cycle
const originalTxResult = await compareWithOldOriginalTxsData(lastStoredOriginalTxDataCycle)
if (!originalTxResult.success) {
throw Error(
'The last saved originalTxsData of last 10 cycles data do not match with the archiver data! Clear the DB and start the server again!'
)
}
lastStoredOriginalTxDataCycle = originalTxResult.matchedCycle
}
if (totalCyclesToSync > lastStoredCycleCount && lastStoredCycleCount > 10) {
const cycleResult = await compareWithOldCyclesData(lastStoredCycleCount)
if (!cycleResult.success) {
throw Error(
'The last saved 10 cycles data does not match with the archiver data! Clear the DB and start the server again!'
)
}
lastStoredCycleCount = cycleResult.cycle
}
if (lastStoredReceiptCount > 0 || lastStoredOriginalTxDataCount > 0) {
if (lastStoredReceiptCount > totalReceiptsToSync) {
throw Error(
'The existing db has more receipts data than the network data! Clear the DB and start the server again!'
)
}
if (lastStoredOriginalTxDataCount > totalOriginalTxsToSync) {
throw Error(
'The existing db has more originalTxsData data than the network data! Clear the DB and start the server again!'
)
}
}
if (totalReceiptsToSync > lastStoredReceiptCount) toggleNeedSyncing()
if (totalOriginalTxsToSync > lastStoredOriginalTxDataCount) toggleNeedSyncing()
if (!needSyncing && totalCyclesToSync > lastStoredCycleCount) toggleNeedSyncing()
const syncData = async(): Promise<void> => {
await downloadAndSyncGenesisAccounts() // To sync accounts data that are from genesis accounts/accounts data that the network start with
if (needSyncing) {
console.log(
lastStoredReceiptCount,
totalReceiptsToSync,
lastStoredCycleCount,
totalCyclesToSync,
lastStoredOriginalTxDataCount,
totalOriginalTxsToSync
)
// Sync receipts and originalTxsData data first if there is old data
if (lastStoredReceiptCycle > 0 && totalCyclesToSync > lastStoredReceiptCycle) {
await downloadReceiptsBetweenCycles(lastStoredReceiptCycle, totalCyclesToSync)
lastStoredReceiptCount = await receipt.queryReceiptCount()
}
if (lastStoredOriginalTxDataCycle > 0 && totalCyclesToSync > lastStoredOriginalTxDataCycle) {
await downloadOriginalTxsDataBetweenCycles(lastStoredOriginalTxDataCycle, totalCyclesToSync)
lastStoredOriginalTxDataCount = await originalTxData.queryOriginalTxDataCount()
}
await downloadTxsDataAndCycles(
totalReceiptsToSync,
lastStoredReceiptCount,
totalOriginalTxsToSync,
lastStoredOriginalTxDataCount,
totalCyclesToSync,
lastStoredCycleCount
)
toggleNeedSyncing()
}
}
return syncData
}
const attemptReconnection = (): void => {
console.log(`Re-connecting Distributor in ${DISTRIBUTOR_RECONNECT_INTERVAL / 1000}s...`)
reconnecting = true
setTimeout(connectToDistributor, DISTRIBUTOR_RECONNECT_INTERVAL)
}
const connectToDistributor = (): void => {
const collectorInfo = {
subscriptionType: DistributorFirehoseEvent,
timestamp: Date.now(),
}
const signedObject = StringUtils.safeJsonParse(
StringUtils.safeStringify({ collectorInfo, sender: CONFIG.collectorInfo.publicKey })
)
crypto.signObj(signedObject, CONFIG.collectorInfo.secretKey, CONFIG.collectorInfo.publicKey)
const queryString = encodeURIComponent(StringUtils.safeStringify(signedObject))
console.log('--> Query String:', queryString)
const URL = `${DISTRIBUTOR_URL}?data=${queryString}`
ws = new WebSocket(URL)
ws.onopen = () => {
console.log(`✅ Socket connected to the Distributor @ ${DISTRIBUTOR_URL}`)
connected = true
reconnecting = false
}
// Listening to FIREHOSE data from the Distributor
ws.on('message', (data: string) => {
try {
if (verbose) console.log('Received FIREHOSE data from Distributor:', data)
validateData(StringUtils.safeJsonParse(data))
} catch (e) {
console.log('Error in processing received data!', e)
}
})
ws.onerror = (error) => {
console.error('Distributor WebSocket error:', error.message)
reconnecting = false
}
// Listening to Socket termination event from the Distributor
ws.onclose = (closeEvent: WebSocket.CloseEvent) => {
switch (closeEvent.code) {
case DistributorSocketCloseCodes.DUPLICATE_CONNECTION_CODE:
console.log(
'❌ Socket Connection w/ same client credentials attempted. Dropping existing connection.'
)
break
case DistributorSocketCloseCodes.SUBSCRIBER_EXPIRATION_CODE:
console.log('❌ Subscription Validity Expired. Connection Terminated.')
break
default:
console.log(`❌ Socket Connection w/ Distributor Terminated with code: ${closeEvent.code}`)
reconnecting = false
break
}
if (!reconnecting) attemptReconnection()
}
}
const startExplorerInMQMode = async (): Promise<void> => {
console.log(`Starting Explorer in RMQ mode`)
rmqCyclesConsumer = new RMQCyclesConsumer()
rmqTransactionsConsumer = new RMQOriginalTxsConsumer()
rmqReceiptsConsumer = new RMQReceiptsConsumer()
rmqCyclesConsumer.start()
rmqTransactionsConsumer.start()
rmqReceiptsConsumer.start()
// add signal listeners
process.on('SIGTERM', async () => {
console.log(`Initiated RabbitMQ connections cleanup`)
await rmqCyclesConsumer.cleanUp()
await rmqTransactionsConsumer.cleanUp()
await rmqReceiptsConsumer.cleanUp()
console.log(`Completed RabbitMQ connections cleanup`)
})
process.on('SIGINT', async () => {
console.log(`Initiated RabbitMQ connections cleanup`)
await rmqCyclesConsumer.cleanUp()
await rmqTransactionsConsumer.cleanUp()
await rmqReceiptsConsumer.cleanUp()
console.log(`Completed RabbitMQ connections cleanup`)
})
}
// Setup Log Directory
const start = async (): Promise<void> => {
let retry = 0
crypto.init(hashKey)
crypto.setCustomStringifier(StringUtils.safeStringify, 'shardus_safeStringify')
await Storage.initializeDB()
Storage.addExitListeners(ws)
const syncData = await checkAndSyncData()
setupCollectorSocketServer()
if (CONFIG.explorerMode === explorerMode.MQ) {
startExplorerInMQMode()
} else {
try {
while (!connected) {
connectToDistributor()
retry++
await sleep(DISTRIBUTOR_RECONNECT_INTERVAL)
if (!connected && retry > CONNECT_TO_DISTRIBUTOR_MAX_RETRY) {
throw Error(`Cannot connect to the Distributor @ ${DISTRIBUTOR_URL}`)
}
}
} catch (e) {
console.log('error while starting explorer in WS mode', e)
}
}
await syncData()
}
start()