-
Notifications
You must be signed in to change notification settings - Fork 1
/
ripper.js
417 lines (392 loc) · 17.1 KB
/
ripper.js
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
const env = require('node-env-file')
env(__dirname + '/.env')
const ethers = require('ethers')
const fs = require('fs')
const WebSocket = require('ws')
const { dbInit, dbAppData, dbContractCache } = require('./db')
const { system, events, jobTimer } = require('./utils')
const { start, stop, getId } = jobTimer
const { log, printLogo, logError } = require('./utils/log')
const provider = new ethers.providers.JsonRpcProvider(process.env.RPC_NODE, 1)
let wsProvider
let kickstart = false // import downloaded data
let kickstartPath
const application = require('./application/sync.js')
const popularDir = process.env.BASEPATH + '/derived/popular/'
const subscriptionRouter = require('./application/subscriptions/router.js')
// Set up db variables on first and only first run
const initialInit = async () => {
log('Initial Init. Welcome!', 1)
await dbAppData.setInt('block_sync', 46146) // first ever eth transaction was in 46147
await dbInit.initFunctions()
await dbAppData.setBool('init', true)
await dbAppData.setBool('working', false)
const block = await provider.getBlockNumber()
if (process.env.OPERATION_MODE === 'subscription') {
log('NOTICE: Subscription mode enabled. Archive data will not be available.')
const confirmations = Number(process.env.CONFIRMATIONS) || 20
await dbAppData.setInt('block_sync', block - confirmations)
await dbAppData.setString('mode', 'subscription')
} else if (process.env.OPERATION_MODE === 'pruned') {
if (!process.env.PRUNE_DEPTH || typeof process.env.PRUNE_DEPTH === 'undefined' || Number(process.env.PRUNE_DEPTH) < 1) {
console.log('Error. Must set prune depth in .env. Nuke db and try again')
process.exit(1)
}
const start = block - Number(process.env.PRUNE_DEPTH)
await dbAppData.setInt('block_sync', start)
await dbAppData.setString('mode', 'pruned')
} else {
await dbAppData.setString('mode', 'sync')
}
}
const echoSettings = () => {
let settings = '\n\nApplication Settings:\n'
settings += ' exec_node:'.padEnd(30) + process.env.EXEC_NODE + '\n'
settings += ' basepath:'.padEnd(30) + process.env.BASEPATH + '\n'
settings += ' rpc:'.padEnd(30) + process.env.RPC_NODE + '\n'
settings += ' rpcws:'.padEnd(30) + process.env.RPC_NODE_WS + '\n'
settings += ' log_level:'.padEnd(30) + process.env.LOG_LEVEL + '\n'
settings += ' log_to_file:'.padEnd(30) + process.env.LOG_TO_FILE + '\n'
settings += ' log_file_location:'.padEnd(30) + process.env.LOG_FILE_LOCATION + '\n'
settings += ' database_name:'.padEnd(30) + process.env.DB_NAME + '\n'
settings += ' redis_url:'.padEnd(30) + process.env.REDIS_URL + '\n'
settings += ' confirmations:'.padEnd(30) + process.env.CONFIRMATIONS + '\n'
settings += ' operation_mode:'.padEnd(30) + process.env.OPERATION_MODE + '\n'
settings += ' prune_depth:'.padEnd(30) + process.env.PRUNE_DEPTH + '\n'
settings += ' optimize_disk_writes:'.padEnd(30) + process.env.OPTIMIZE_DISK_WRITES + '\n'
settings += ' commit_every_n_blocks:'.padEnd(30) + process.env.COMMIT_EVERYN_BLOCKS + '\n'
settings += ' json_file_max:'.padEnd(30) + process.env.JSON_TX_FILE_MAX + '\n'
settings += ' reconnect_timeout:'.padEnd(30) + process.env.WS_RECONNECT_TIMEOUT + '\n'
settings += ' use_multi_threads:'.padEnd(30) + process.env.USE_MULTI_THREADS + '\n'
settings += ' number_of_threads:'.padEnd(30) + process.env.MULTI_THREADS + '\n'
settings += ' number_of_threads:'.padEnd(30) + process.env.MULTI_THREADS + '\n'
settings += ' use_redis_data:'.padEnd(30) + process.env.USE_REDIS_DATA + '\n'
settings += ' index_cache_disable:'.padEnd(30) + process.env.INDEX_CACHE_DISABLE + '\n'
settings += ' dont_index:'.padEnd(30) + process.env.DONT_INDEX + '\n'
settings += ' dev_stop_flags:'.padEnd(30) + process.env.DEV_STOP_FLAGS_ENABLE + '\n'
settings += ' sub_suspend_all:'.padEnd(30) + process.env.SUB_SUSPEND_ALL + '\n'
settings += ' sub_use_unix_socket:'.padEnd(30) + process.env.SUB_USE_UNIX_SOCKET + '\n'
settings += ' sub_unix_socket:'.padEnd(30) + process.env.SUB_UNIX_SOCKET + '\n'
settings += ' sub_use_redis:'.padEnd(30) + process.env.SUB_USE_REDIS + '\n'
settings += ' sub_type_account:'.padEnd(30) + process.env.SUB_TYPE_ACCOUNT + '\n'
settings += ' kickstart_dir:'.padEnd(30) + process.env.KICKSTARTDIR + '\n'
log(settings, 1)
}
// Set up db variables during normal operations
const init = async () => {
echoSettings()
// ensure ethers it the right version
const version = ethers.version.split('/')[1].split('.')[0] // ethers/5.7.2
if (version !== '5') {
log('Error: the version of ethers.js should be version 5.x.x', 1)
}
// dont run if another process is currently running
const procDir = '/proc'
const procStore = __dirname + '/derived/application/proc'
if (fs.existsSync(procStore)) {
const proc = await fs.readFileSync(procStore)
if (fs.existsSync(procDir + '/' + proc)) {
const procInfo = await fs.readFileSync(procDir + '/' + proc + '/status', 'utf8')
const guts = procInfo.split('\n')
if (guts[0].includes('node')) {
await logStats('process checker')
log('Error: Only one process may run at a time! Pid: ' + proc + ' is still running. Exiting...', 1)
process.exit(1)
}
}
}
await fs.writeFileSync(procStore, String(process.pid))
await dbInit.initTables() // ensure basic tables exist
await dbInit.initApplicationDefaults()
// Note once you set the mode of the application you are not allowed to modify it, at the cost of nuking the database.
const mode = await dbAppData.getString('mode')
const modeEnv = process.env.OPERATION_MODE
if (
(mode === 'subscription' && modeEnv !== 'subscription') ||
(mode === 'pruned' && modeEnv !== 'pruned') ||
(mode === 'sync' && modeEnv !== 'sync')
) {
let message = 'ERROR: \n\nThe Operation mode in the environment does not match the mode \"' + mode + '\", which was set previously. Process cannot continue, Exiting.\n\n'
message += ' To operate in sync mode, reset the database and set env.OPERATION_MODE=sync\n'
message += ' To operate in pruned mode, reset the database and set env.OPERATION_MODE=pruned\n'
message += ' To operate in subscription mode, reset the database and set env.OPERATION_MODE=subscription\n'
message += ' To continue operating in ' + mode + ' mode, set env.OPERATION_MODE' + mode + '\n\n'
message += ' -> To resolve, open cli directly: node application/cli/index.js and \"Nuke the database\" after fixing .env\n\n'
log(message, 1)
process.exit(1)
}
if (await dbAppData.getBool('init') !== true) { // must happen after table inits
await initialInit()
}
const blockHeight = await provider.getBlockNumber()
await dbInit.initPartitions(blockHeight) // ensure enough partitions to proceed
const indexCacheDisable = process.env.INDEX_CACHE_DISABLE || 'false'
if (indexCacheDisable === 'false') {
if (kickstart === true) {
await kickstartCache(kickstartPath)
}
const accountsFile = popularDir + 'topAccts.json'
let generate = false
if (fs.existsSync(accountsFile)) { // double check there is at least an entry in accountsFile
const _accounts = fs.readFileSync(accountsFile)
let accounts = JSON.parse(_accounts)
if (accounts.length === 0) {
generate = true
}
} else { // the file doesnt exist at all
generate = true
}
if (generate === true) {
log('NOTICE: No popular Accounts file. This will be generated before proceeding.', 1)
log('Please wait. The process will be executed on a separate thread and will take around 10 - 15 minutes. See README.md.', 1)
log('to check on progress, observe /derived/application/log', 1)
await system.execCmd(process.env.EXEC_NODE + ' ' + process.env.BASEPATH + 'extras/popularContracts.js')
}
await dbInit.assignPopularAddresses() // establish the contract cache
}
if (kickstart === true) {
await kickstartData(kickstartPath)
}
await subscriptionRouter.init()
// Under normal circumstance reset stop flags and continue
const devFlagDisable = process.env.DEV_STOP_FLAGS_ENABLE === 'true' ? false : true
if (devFlagDisable) {
await dbAppData.markUnPaused()
await dbAppData.setBool('working', false)
}
}
const sleep = (m) => { return new Promise(r => setTimeout(r, m)) }
const interactiveMode = async () => {
log('NOTICE: Entering Interactive Mode', 1)
require('./application/cli')
}
const logStats = async (source) => {
log(source + ' has finished', 1)
system.memStats(true, 'Final System Memory Usage')
stop('Main Application', true)
log('NOTICE: Exit was clean. Marking Unpaused', 2)
}
let onlyOnce = false
const cleanup = async (errorCode) => {
if (onlyOnce === true) return
onlyOnce = true
await dbAppData.markPaused()
// Weve been told to quit but marking unpaused first
const working = await dbAppData.getBool('working')
if (working === true) {
const source = await events.asyncListener('exit')
if (source === 'finalize') {
await logStats(source)
await dbAppData.markUnPaused()
process.exit(errorCode)
} else {
log('ERROR: Couldn\'t determine source of exit cmd', 4)
process.exit(1)
}
} else {
await logStats('local')
await dbAppData.markUnPaused()
process.exit(errorCode)
}
}
const doSynchronize = async (block) => {
let dont = false
const lastSyncPoint = await dbAppData.getInt('block_sync')
const diff = block - lastSyncPoint
log('New Block:' + block + ' behind by: ' + diff + ' blocks', 1)
const working = await dbAppData.getBool('working')
if (working === false && dont === false) {
const response = await application.synchronize(block)
if (response === 'exit') {
dont = true
events.emitMessage('close', 'sync_complete')
} else if (response === 'error') {
logError('An Error occurred during this cycle. Will retry from last sync point.', 1)
}
}
}
const setUpWsProviderAndGo = async () => {
if (wsProvider && wsProvider._websocket &&
(wsProvider._websocket._readyState === WebSocket.OPEN ||
wsProvider._websocket._readyState === WebSocket.CONNECTING)) {
log('WebSocket already in use: ' + wsProvider._websocket._readyState, 1)
return
}
wsProvider = new ethers.providers.WebSocketProvider(process.env.RPC_NODE_WS)
const id = getId()
wsProvider['ripperId'] = id
log('NOTICE: Opening New WS Provider with id: ' + id)
let deBounce = false
wsProvider.on("block", async (block) => {
if (deBounce === false) {
deBounce = true
await doSynchronize(block)
} else {
console.log('DEBUG: Received several blocks simultaneously.')
}
// this is for HW issue, it doesnt care
// whether or not doSynchronize is finished.
setTimeout(() => {
deBounce = false;
}, 1000)
})
const timeoutDuration = Number(process.env.WS_RECONNECT_TIMEOUT) * 1000 || 5000
wsProvider._websocket.on("error", async (error) => {
logError(error)
})
wsProvider._websocket.on("close", async (error) => {
log('Websocket with id: ' + wsProvider.ripperId + ' was closed.')
logError(error)
log('NOTICE: >>>>>>> WEBSOCKET CLOSED <<<<<<', 1)
setTimeout(async () => {
await setUpWsProviderAndGo()
}, timeoutDuration)
})
}
// Expectations: the sql file is not corrupt
// the file contains the exact amount of entries used to create chaindata
const kickstartCache = async (kickstartPath) => {
const cacheAdded = dbAppData.getBool('kickstart:cache')
if (cacheAdded === false) { // and not undefined
log('kickstart: previously there was an error adding contract cache. Probably need to nuke and start over.', 1)
process.exit(1)
}
const size = await dbContractCache.cacheSize() // nonzero chance that the user continues to set the flag
if (size > 0) {
log('kickstart: The contract cache was previously initialized.', 1)
return
}
try {
log('kickstart: initializing the index_cache', 1)
await system.execCmd('psql -d ' + process.env.DB_NAME + ' -f ' + kickstartPath + '/index_cache.sql')
await dbAppData.setBool('kickstart:cache', true)
} catch (error) {
await dbAppData.setBool('kickstart:cache', false)
logError(error)
log('The sql command to add the contract cache from the kickstart folder failed. Might need to nuke the db.')
}
}
const kickstartData = async (kickstartPath) => {
const kickstartedData = await dbAppData.getBool('kickstart:data')
if (kickstartedData === true) {
log('NOTICE: App has been successfully kickstarted. Please remove the kickstart arguments from the command line.')
process.exit()
}
await dbAppData.setBool('kickstart:data', false)
const dir = await fs.readdirSync(kickstartPath)
const sorter = []
let imported = await dbAppData.getInt('kickstart:import')
if (typeof imported === 'undefined') imported = 0
dir.forEach((file) => {
if (file.includes('_blocks.sql')) {
const fileNum = Number(file.replace('_blocks.sql', ''))
if (fileNum > imported) {
sorter.push(fileNum)
}
}
})
const sorted = sorter.sort((a, b) => { return a > b ? 1 : -1 })
for (let i = 0; i < sorted.length; i++) {
const file = kickstartPath + '/' + String(sorted[i]) + '_blocks.sql'
try {
log('Loading in kickstarter file: ' + file, 1)
await system.execCmd('psql -d ' + process.env.DB_NAME + ' -f ' + file)
await dbAppData.setInt('kickstart:import', sorted[i])
await dbAppData.setInt('block_sync', sorted[i])
await dbAppData.setInt('last_block_scanned', sorted[i])
await sleep(500)
if (i === sorted.length - 1) await dbAppData.setBool('kickstart:data', true)
} catch (error) {
logError(error)
log('The sql command to add the transaction info from the kickstart folder failed. Might need to nuke the db.')
}
}
}
;(async () => {
try {
start('Main Application')
printLogo()
process.on('SIGHUP', async () => {
log('NOTICE: >>>>>>> SIGHUP acknowledged <<<<<< Will Exit at end of this cycle.', 1)
await cleanup(0)
})
process.on('SIGINT', async () => {
log('NOTICE: >>>>>>> Ctl-C acknowledged <<<<<< Will Exit at end of this cycle. pId:' + process.pid, 1)
await cleanup(0)
})
process.on('SIGTERM', async () => {
log('NOTICE: >>>>>>> SIGTERM acknowledged <<<<<< Will Exit at end of this cycle.', 1)
await cleanup(0)
})
let found = false
for (let i = 2; i < process.argv.length; i++) {
if (process.argv[i] === 'cli' || process.argv[i] === '-cli' || process.argv[i] === '--cli' || process.argv[i] === 'i') {
found = true
} else if (process.argv[i] === '--k' || process.argv[i] === '-k' || process.argv[i] === 'k') {
// kickstart. it is expected that the database has not been initialized here
if (typeof process.argv[i + 1] !== 'undefined') {
if (await fs.existsSync(process.argv[i + 1])) {
const dir = await fs.readdirSync(process.argv[i + 1])
if (dir.length > 2) {
let checks = 0
dir.forEach((file) => {
if (file === 'index_cache.sql') checks += 1
if (file.includes('_blocks.sql')) checks += 1
})
if (checks < 2) {
log('Attempting to kickstart but the directory supplied did not pass the checks.', 1)
log('There must be at minimum two files within: index_cache.sql and 0_blocks.sql', 1)
process.exit(1)
}
const indexCacheDisable = process.env.INDEX_CACHE_DISABLE || 'false'
if (indexCacheDisable === 'true') {
log('In order to kickstart you must set .env.INDEX_CACHE_DISABLE=false', 1)
process.exit(1)
}
kickstart = true
kickstartPath = process.argv[i + 1]
} else {
log('Attempting to kickstart but the directory supplied does not appear to be valid.', 1)
process.exit()
}
}
}
}
}
await init()
log('NOTICE: Waiting for a block...', 1)
const pause = await dbAppData.pauseStatus()
if (pause) {
log('NOTICE: >>>>>>> Main: Pause flag detected <<<<<< Entering interactive mode.', 1)
found = true
}
const inJob = await dbAppData.getBool('working')
if (inJob === true) {
log('NOTICE: >>>>>>> Main: Working flag detected <<<<<< Entering interactive mode.', 1)
found = true
}
events.emitter.on('close', async (source) => {
if (source === 'sync_complete') {
logStats(source)
// log('NOTICE: ' + source + ' has finished, exiting', 4, system.memStats(false))
await dbAppData.markUnPaused()
process.exit(0)
} else if (source === 'ws_error') {
logError()
logStats(source)
// log('NOTICE: ' + source + ' has finished, exiting', 4, system.memStats(false))
await dbAppData.markUnPaused()
process.exit(0)
}
})
if (found) {
await interactiveMode()
} else {
await setUpWsProviderAndGo()
}
// if (!found) await cleanup(0)
} catch (error) {
logError(error, 'Application Error')
await cleanup(1)
}
})()