From e5d0126d0875267158c49afb99caa9f3786d3aa9 Mon Sep 17 00:00:00 2001 From: Igor Lins e Silva Date: Mon, 25 Sep 2023 01:05:33 -0300 Subject: [PATCH] Revert "include amqp-connection-manager" This reverts commit 0579da66 --- connections/amqp.ts | 144 ++++++----- connections/manager.class.ts | 3 +- ecosystem.config.js | 7 + package-lock.json | 21 -- package.json | 1 - tests/dsp-consumer.js | 13 +- tests/dsp-consumer.js.map | 2 +- tests/dsp-consumer.ts | 18 +- workers/delta-updater.ts | 8 +- workers/deserializer.ts | 14 +- workers/ds-pool.ts | 14 +- workers/hyperionWorker.ts | 455 +++++++++++++++++------------------ workers/indexer.ts | 8 +- 13 files changed, 339 insertions(+), 369 deletions(-) diff --git a/connections/amqp.ts b/connections/amqp.ts index e14e2bf..582b46b 100644 --- a/connections/amqp.ts +++ b/connections/amqp.ts @@ -1,21 +1,19 @@ import {debugLog, hLog} from "../helpers/common_functions"; import got, {HTTPError} from "got"; -import amqp, {ChannelWrapper} from "amqp-connection-manager"; -import {IAmqpConnectionManager} from "amqp-connection-manager/dist/types/AmqpConnectionManager"; +import {connect, Connection} from 'amqplib'; -export async function createConnection(config): Promise { - try { - const amqp_url = getAmpqUrl(config); - // const conn: Connection = await connect(amqp_url); - const conn: IAmqpConnectionManager = amqp.connect(amqp_url); - debugLog("[AMQP] connection established"); - return conn; - } catch (e) { - hLog("[AMQP] failed to connect!"); - hLog(e.message); - await new Promise(resolve => setTimeout(resolve, 5000)); - return await createConnection(config); - } +export async function createConnection(config): Promise { + try { + const amqp_url = getAmpqUrl(config); + const conn: Connection = await connect(amqp_url); + debugLog("[AMQP] connection established"); + return conn; + } catch (e) { + hLog("[AMQP] failed to connect!"); + hLog(e.message); + await new Promise(resolve => setTimeout(resolve, 5000)); + return await createConnection(config); + } } export function getAmpqUrl(config): string { @@ -29,70 +27,64 @@ export function getAmpqUrl(config): string { return `amqp://${u}:${p}@${config.host}/${v}?frameMax=${frameMaxValue}`; } -async function createChannels(connection: IAmqpConnectionManager) { - try { - // const channel = await connection.createChannel(); - const channel = connection.createChannel({ - confirm: false - }); - // const confirmChannel = await connection.createConfirmChannel(); - const confirmChannel = connection.createChannel({ - confirm: true - }) - return [channel, confirmChannel]; - } catch (e) { - hLog("[AMQP] failed to create channels"); - hLog(e); - return null; - } +async function createChannels(connection) { + try { + const channel = await connection.createChannel(); + const confirmChannel = await connection.createConfirmChannel(); + return [channel, confirmChannel]; + } catch (e) { + hLog("[AMQP] failed to create channels"); + hLog(e); + return null; + } } -export async function amqpConnect(onReconnect, config, onClose): Promise { - let connection = await createConnection(config); - if (connection) { - const channels = await createChannels(connection); - if (channels) { - connection.on('error', (err) => { - hLog(err.message); - }); - connection.on('close', () => { - hLog('Connection closed!'); - onClose(); - setTimeout(async () => { - hLog('Retrying in 5 seconds...'); - const _channels = await amqpConnect(onReconnect, config, onClose); - onReconnect(_channels); - return _channels; - }, 5000); - }); - return channels; - } else { - return null; - } - } else { - return null; - } +export async function amqpConnect(onReconnect, config, onClose) { + let connection = await createConnection(config); + if (connection) { + const channels = await createChannels(connection); + if (channels) { + connection.on('error', (err) => { + hLog(err.message); + }); + connection.on('close', () => { + hLog('Connection closed!'); + onClose(); + setTimeout(async () => { + hLog('Retrying in 5 seconds...'); + const _channels = await amqpConnect(onReconnect, config, onClose); + onReconnect(_channels); + return _channels; + }, 5000); + }); + return channels; + } else { + return null; + } + } else { + return null; + } } export async function checkQueueSize(q_name, config) { - try { - const v = encodeURIComponent(config.vhost); - const apiUrl = `${config.protocol}://${config.api}/api/queues/${v}/${encodeURIComponent(q_name)}`; - const opts = { - username: config.user, - password: config.pass - }; - const data = await got.get(apiUrl, opts).json() as any; - return data.messages; - } catch (e) { - hLog(`[WARNING] Checking queue size failed! - ${e.message}`); - if (e.response && e.response.body) { - if (e instanceof HTTPError) { - hLog(e.response.body); - } else { - hLog(JSON.stringify(e.response.body, null, 2)); - } - } - return 0; - } + try { + const v = encodeURIComponent(config.vhost); + const apiUrl = `${config.protocol}://${config.api}/api/queues/${v}/${encodeURIComponent(q_name)}`; + const opts = { + username: config.user, + password: config.pass + }; + const data = await got.get(apiUrl, opts).json() as any; + return data.messages; + } catch (e) { + hLog(`[WARNING] Checking queue size failed! - ${e.message}`); + if (e.response && e.response.body) { + if (e instanceof HTTPError) { + hLog(e.response.body); + } else { + hLog(JSON.stringify(e.response.body, null, 2)); + } + } + return 0; + } } diff --git a/connections/manager.class.ts b/connections/manager.class.ts index c892617..a0c3e79 100644 --- a/connections/manager.class.ts +++ b/connections/manager.class.ts @@ -9,7 +9,6 @@ import {StateHistorySocket} from "./state-history"; import fetch from 'cross-fetch'; import {exec} from "child_process"; import {hLog} from "../helpers/common_functions"; -import {ChannelWrapper} from "amqp-connection-manager"; export class ConnectionManager { @@ -132,7 +131,7 @@ export class ConnectionManager { } } - async createAMQPChannels(onReconnect, onClose): Promise { + async createAMQPChannels(onReconnect, onClose) { return await amqpConnect(onReconnect, this.conn.amqp, onClose); } diff --git a/ecosystem.config.js b/ecosystem.config.js index 40f050c..5478a25 100644 --- a/ecosystem.config.js +++ b/ecosystem.config.js @@ -20,4 +20,11 @@ readdirSync(chainsRoot) } }); +apps.push({ + name: 'hyperion-governor', + namespace: 'hyperion', + script: 'governor/server/index.js', + watch: false, +}); + module.exports = {apps}; diff --git a/package-lock.json b/package-lock.json index e27dc39..8fd54b0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -19,7 +19,6 @@ "@fastify/redis": "^5.0.0", "@fastify/swagger": "6.1.0", "@pm2/io": "^5.0.0", - "amqp-connection-manager": "^4.1.14", "amqplib": "^0.10.3", "async": "^3.2.4", "base-x": "^4.0.0", @@ -444,21 +443,6 @@ "url": "https://github.com/sponsors/epoberezkin" } }, - "node_modules/amqp-connection-manager": { - "version": "4.1.14", - "resolved": "https://registry.npmjs.org/amqp-connection-manager/-/amqp-connection-manager-4.1.14.tgz", - "integrity": "sha512-1km47dIvEr0HhMUazqovSvNwIlSvDX2APdUpULaINtHpiki1O+cLRaTeXb/jav4OLtH+k6GBXx5gsKOT9kcGKQ==", - "dependencies": { - "promise-breaker": "^6.0.0" - }, - "engines": { - "node": ">=10.0.0", - "npm": ">5.0.0" - }, - "peerDependencies": { - "amqplib": "*" - } - }, "node_modules/amqplib": { "version": "0.10.3", "resolved": "https://registry.npmjs.org/amqplib/-/amqplib-0.10.3.tgz", @@ -2263,11 +2247,6 @@ "resolved": "https://registry.npmjs.org/process-warning/-/process-warning-1.0.0.tgz", "integrity": "sha512-du4wfLyj4yCZq1VupnVSZmRsPJsNuxoDQFdCFHLaYiEbFBD7QE0a+I4D7hOxrVnh78QE/YipFAj9lXHiXocV+Q==" }, - "node_modules/promise-breaker": { - "version": "6.0.0", - "resolved": "https://registry.npmjs.org/promise-breaker/-/promise-breaker-6.0.0.tgz", - "integrity": "sha512-BthzO9yTPswGf7etOBiHCVuugs2N01/Q/94dIPls48z2zCmrnDptUUZzfIb+41xq0MnYZ/BzmOd6ikDR4ibNZA==" - }, "node_modules/proxy-addr": { "version": "2.0.7", "resolved": "https://registry.npmjs.org/proxy-addr/-/proxy-addr-2.0.7.tgz", diff --git a/package.json b/package.json index 46d666c..8fd8cc6 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,6 @@ "@fastify/redis": "^5.0.0", "@fastify/swagger": "6.1.0", "@pm2/io": "^5.0.0", - "amqp-connection-manager": "^4.1.14", "amqplib": "^0.10.3", "async": "^3.2.4", "base-x": "^4.0.0", diff --git a/tests/dsp-consumer.js b/tests/dsp-consumer.js index f9530df..72e87b2 100644 --- a/tests/dsp-consumer.js +++ b/tests/dsp-consumer.js @@ -16,21 +16,20 @@ class DspEventConsumer { console.log('Starting DSP Consumer...'); [this.ch] = await this.manager.createAMQPChannels((channels) => { [this.ch] = channels; - this.onConnect().catch(console.log); + this.onConnect(); }, () => { this.ch_ready = false; }); - this.onConnect().catch(console.log); + this.onConnect(); } - async onConnect() { + onConnect() { if (this.conf.settings.dsp_parser) { const q = `${this.manager.chain}:dsp`; console.log(q); - await this.ch.assertQueue(q, { durable: true }); - await this.ch.consume(q, (data) => { + this.ch.prefetch(100); + this.ch.assertQueue(q, { durable: true }); + this.ch.consume(q, (data) => { this.onMessage(data); - }, { - prefetch: 100 }); } } diff --git a/tests/dsp-consumer.js.map b/tests/dsp-consumer.js.map index f5cd15e..a45d735 100644 --- a/tests/dsp-consumer.js.map +++ b/tests/dsp-consumer.js.map @@ -1 +1 @@ -{"version":3,"file":"dsp-consumer.js","sourceRoot":"","sources":["dsp-consumer.ts"],"names":[],"mappings":";;AAAA,8CAAsD;AACtD,gEAA+D;AAK/D,MAAM,gBAAgB;IAUlB;QALQ,aAAQ,GAAY,KAAK,CAAC;QAC1B,cAAS,GAAG,CAAC,CAAC;QACd,WAAM,GAAG,CAAC,CAAC;QACX,iBAAY,GAAuB,IAAI,GAAG,EAAE,CAAC;QAGjD,MAAM,EAAE,GAAG,IAAI,4BAAmB,EAAE,CAAC;QACrC,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC,MAAM,CAAC;QACtB,IAAI,CAAC,OAAO,GAAG,IAAI,iCAAiB,CAAC,EAAE,CAAC,CAAC;IAC7C,CAAC;IAED,KAAK,CAAC,GAAG;QACL,OAAO,CAAC,GAAG,CAAC,0BAA0B,CAAC,CAAC;QACxC,CAAC,IAAI,CAAC,EAAE,CAAC,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,kBAAkB,CAAC,CAAC,QAAQ,EAAE,EAAE;YAC3D,CAAC,IAAI,CAAC,EAAE,CAAC,GAAG,QAAQ,CAAC;YACrB,IAAI,CAAC,SAAS,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QACxC,CAAC,EAAE,GAAG,EAAE;YACJ,IAAI,CAAC,QAAQ,GAAG,KAAK,CAAC;QAC1B,CAAC,CAAC,CAAC;QACH,IAAI,CAAC,SAAS,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;IACxC,CAAC;IAEO,KAAK,CAAC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,CAAC,OAAO,CAAC,CAAC;aAC1D;YACD,IAAI,CAAC,aAAa,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC;YACnC,IAAI,CAAC,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;SACpB;QAAC,OAAO,CAAC,EAAE;YACR,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;SACrB;IACL,CAAC;CAEJ;AAED,IAAI,gBAAgB,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC"} \ No newline at end of file diff --git a/tests/dsp-consumer.ts b/tests/dsp-consumer.ts index 237aa12..eec8de6 100644 --- a/tests/dsp-consumer.ts +++ b/tests/dsp-consumer.ts @@ -1,14 +1,13 @@ import {ConfigurationModule} from "../modules/config"; import {ConnectionManager} from "../connections/manager.class"; import {HyperionConfig} from "../interfaces/hyperionConfig"; -import {Message} from "amqplib/callback_api"; -import {ChannelWrapper} from "amqp-connection-manager"; +import {Channel, Message} from "amqplib/callback_api"; class DspEventConsumer { private conf: HyperionConfig; private manager: ConnectionManager; - private ch: ChannelWrapper; + private ch: Channel; private ch_ready: boolean = false; private lastBlock = 0; private lastGS = 0; @@ -24,22 +23,21 @@ class DspEventConsumer { console.log('Starting DSP Consumer...'); [this.ch] = await this.manager.createAMQPChannels((channels) => { [this.ch] = channels; - this.onConnect().catch(console.log); + this.onConnect(); }, () => { this.ch_ready = false; }); - this.onConnect().catch(console.log); + this.onConnect(); } - private async onConnect(): Promise { + private onConnect() { if (this.conf.settings.dsp_parser) { const q = `${this.manager.chain}:dsp`; console.log(q); - await this.ch.assertQueue(q, {durable: true}); - await this.ch.consume(q, (data) => { + this.ch.prefetch(100); + this.ch.assertQueue(q, {durable: true}); + this.ch.consume(q, (data) => { this.onMessage(data); - }, { - prefetch: 100 }); } } diff --git a/workers/delta-updater.ts b/workers/delta-updater.ts index bc8b0fe..d8d8a01 100644 --- a/workers/delta-updater.ts +++ b/workers/delta-updater.ts @@ -29,11 +29,9 @@ export default class MainDSWorker extends HyperionWorker { if (this.ch) { this.queueName = this.chain + ":delta_rm"; hLog(`Launched delta updater, consuming from ${this.queueName}`); - this.ch.assertQueue(this.queueName, {durable: true}).then(() => { - this.ch.consume(this.queueName, this.onConsume.bind(this),{ - prefetch: 1 - }).catch(console.log); - }); + this.ch.assertQueue(this.queueName, {durable: true}); + this.ch.prefetch(1); + this.ch.consume(this.queueName, this.onConsume.bind(this)); } } diff --git a/workers/deserializer.ts b/workers/deserializer.ts index 812de5d..bc6fbff 100644 --- a/workers/deserializer.ts +++ b/workers/deserializer.ts @@ -286,11 +286,10 @@ export default class MainDSWorker extends HyperionWorker { private initConsumer() { if (this.ch_ready) { + this.ch.prefetch(this.conf.prefetch.block); this.ch.consume(process.env['worker_queue'], (data) => { this.consumerQueue.push(data).catch(console.log); - }, { - prefetch: this.conf.prefetch.block - }).catch(console.log); + }); } } @@ -624,7 +623,12 @@ export default class MainDSWorker extends HyperionWorker { const pool_queue = `${this.chain}:ds_pool:${selected_q}`; if (this.ch_ready) { - this.ch.sendToQueue(pool_queue, bufferFromJson(trace, true), {headers}).catch(console.log); + const enqueueResult = this.ch.sendToQueue(pool_queue, bufferFromJson(trace, true), {headers}); + if (!enqueueResult) { + hLog("Backpressure"); + console.log("Header size: " + JSON.stringify(headers).length); + console.log(headers); + } return true; } else { return false; @@ -1160,7 +1164,7 @@ export default class MainDSWorker extends HyperionWorker { let jsonRow = await this.processContractRowNative(payload, block_num); if (jsonRow?.value && !jsonRow['_blacklisted']) { - debugLog(jsonRow); + console.log(jsonRow); debugLog('Delta DS failed ->>', jsonRow); jsonRow = await this.processContractRowNative(payload, block_num - 1); debugLog('Retry with previous ABI ->>', jsonRow); diff --git a/workers/ds-pool.ts b/workers/ds-pool.ts index ab3e10e..f25dbd1 100644 --- a/workers/ds-pool.ts +++ b/workers/ds-pool.ts @@ -625,11 +625,12 @@ export default class DSPoolWorker extends HyperionWorker { initConsumer() { if (this.ch_ready) { + this.ch.prefetch(this.conf.prefetch.block); this.ch.consume(this.local_queue, (data) => { this.consumerQueue.push(data); - }, { - prefetch: this.conf.prefetch.block - }).catch(console.log); + }, {}, (err, ok) => { + hLog(err, ok); + }); debugLog(`started consuming from ${this.local_queue}`); } } @@ -693,14 +694,13 @@ export default class DSPoolWorker extends HyperionWorker { this.ch_ready = true; this.ch.assertQueue(this.local_queue, { durable: true - }).then(() => { - this.initConsumer(); - }).catch(console.log); + }); + this.initConsumer(); } if (this.conf.settings.dsp_parser) { this.ch.assertQueue(`${queue_prefix}:dsp`, { durable: true - }).catch(console.log); + }); } } diff --git a/workers/hyperionWorker.ts b/workers/hyperionWorker.ts index 046464a..3f8129f 100644 --- a/workers/hyperionWorker.ts +++ b/workers/hyperionWorker.ts @@ -11,280 +11,277 @@ import {HeapInfo} from "v8"; import {debugLog, hLog} from "../helpers/common_functions"; import {StateHistorySocket} from "../connections/state-history"; import * as AbiEOS from "@eosrio/node-abieos"; -import {ChannelWrapper} from "amqp-connection-manager"; export abstract class HyperionWorker { - conf: HyperionConfig; - manager: ConnectionManager; - mLoader: HyperionModuleLoader; - chain: string; - chainId: string; + conf: HyperionConfig; + manager: ConnectionManager; + mLoader: HyperionModuleLoader; + chain: string; + chainId: string; - // AMQP Channels - ch: ChannelWrapper; - cch: ChannelWrapper; + // AMQP Channels + ch: Channel; + cch: ConfirmChannel; - rpc: JsonRpc; - client: Client; - ship: StateHistorySocket; + rpc: JsonRpc; + client: Client; + ship: StateHistorySocket; - txEnc = new TextEncoder(); - txDec = new TextDecoder(); - cch_ready = false; - ch_ready = false; + txEnc = new TextEncoder(); + txDec = new TextDecoder(); + cch_ready = false; + ch_ready = false; - events: EventEmitter; + events: EventEmitter; - filters: Filters; + filters: Filters; - failedAbiMap: Map> = new Map(); + failedAbiMap: Map> = new Map(); - protected constructor() { - this.checkDebugger(); - const cm = new ConfigurationModule(); - this.conf = cm.config; - this.filters = cm.filters; - this.manager = new ConnectionManager(cm); - this.mLoader = new HyperionModuleLoader(cm); - this.chain = this.conf.settings.chain; - this.chainId = this.manager.conn.chains[this.chain].chain_id; - this.rpc = this.manager.nodeosJsonRPC; - this.client = this.manager.elasticsearchClient; - this.ship = this.manager.shipClient; - this.events = new EventEmitter(); + protected constructor() { + this.checkDebugger(); + const cm = new ConfigurationModule(); + this.conf = cm.config; + this.filters = cm.filters; + this.manager = new ConnectionManager(cm); + this.mLoader = new HyperionModuleLoader(cm); + this.chain = this.conf.settings.chain; + this.chainId = this.manager.conn.chains[this.chain].chain_id; + this.rpc = this.manager.nodeosJsonRPC; + this.client = this.manager.elasticsearchClient; + this.ship = this.manager.shipClient; + this.events = new EventEmitter(); - this.mLoader.init().then(() => { - // Connect to RabbitMQ (amqplib) - this.events.emit('loader_ready'); - this.connectAMQP().then(() => { - this.onConnect(); - }).catch(console.log); - }); + this.mLoader.init().then(() => { + // Connect to RabbitMQ (amqplib) + this.events.emit('loader_ready'); + this.connectAMQP().then(() => { + this.onConnect(); + }).catch(console.log); + }); - const bytesToString = (bytes: number) => { - const e = Math.log(bytes) / Math.log(1024) | 0; - const n = (bytes / Math.pow(1024, e)).toFixed(2); - return n + ' ' + (e == 0 ? 'bytes' : (['KB', 'MB', 'GB', 'TB'])[e - 1]); - }; + const bytesToString = (bytes: number) => { + const e = Math.log(bytes) / Math.log(1024) | 0; + const n = (bytes / Math.pow(1024, e)).toFixed(2); + return n + ' ' + (e == 0 ? 'bytes' : (['KB', 'MB', 'GB', 'TB'])[e - 1]); + }; - // handle ipc messages - process.on('message', (msg: any) => { - switch (msg.event) { - case 'request_v8_heap_stats': { - const report: HeapInfo = v8.getHeapStatistics(); - const used_pct = report.used_heap_size / report.heap_size_limit; - process.send({ - event: 'v8_heap_report', - id: process.env.worker_role + ':' + process.env.worker_id, - data: { - heap_usage: (used_pct * 100).toFixed(2) + "%", - ...report - } - }); - break; - } - case 'request_memory_usage': { - const report = process.memoryUsage(); - process.send({ - event: 'memory_report', - id: process.env.worker_role + ':' + process.env.worker_id, - data: { - resident: bytesToString(report.rss), - } - }); - break; - } - default: { - this.onIpcMessage(msg); - } - } - }); - } + // handle ipc messages + process.on('message', (msg: any) => { + switch (msg.event) { + case 'request_v8_heap_stats': { + const report: HeapInfo = v8.getHeapStatistics(); + const used_pct = report.used_heap_size / report.heap_size_limit; + process.send({ + event: 'v8_heap_report', + id: process.env.worker_role + ':' + process.env.worker_id, + data: { + heap_usage: (used_pct * 100).toFixed(2) + "%", + ...report + } + }); + break; + } + case 'request_memory_usage': { + const report = process.memoryUsage(); + process.send({ + event: 'memory_report', + id: process.env.worker_role + ':' + process.env.worker_id, + data: { + resident: bytesToString(report.rss), + } + }); + break; + } + default: { + this.onIpcMessage(msg); + } + } + }); + } - async connectAMQP() { - [this.ch, this.cch] = await this.manager.createAMQPChannels((channels: ChannelWrapper[]) => { - if (channels) { - [this.ch, this.cch] = channels; - hLog('AMQP Reconnecting...'); - this.onConnect(); - } - }, () => { - this.ch_ready = false; - this.cch_ready = false; - }); - } + async connectAMQP() { + [this.ch, this.cch] = await this.manager.createAMQPChannels((channels) => { + [this.ch, this.cch] = channels; + hLog('AMQP Reconnecting...'); + this.onConnect(); + }, () => { + this.ch_ready = false; + this.cch_ready = false; + }); + } - onConnect() { - this.ch_ready = true; - this.cch_ready = true; - this.assertQueues(); - this.ch.on('close', () => { - this.ch_ready = false; - }); - this.cch.on('close', () => { - this.cch_ready = false; - }); - this.events.emit('ready'); - } + onConnect() { + this.ch_ready = true; + this.cch_ready = true; + this.assertQueues(); + this.ch.on('close', () => { + this.ch_ready = false; + }); + this.cch.on('close', () => { + this.cch_ready = false; + }); + this.events.emit('ready'); + } - checkDebugger() { - if (/--inspect/.test(process.execArgv.join(' '))) { - const inspector = require('inspector'); - hLog('DEBUGGER ATTACHED', inspector.url()); - } - } + checkDebugger() { + if (/--inspect/.test(process.execArgv.join(' '))) { + const inspector = require('inspector'); + hLog('DEBUGGER ATTACHED', inspector.url()); + } + } - private anyFromCode(act: any) { - return this.chain + '::' + act.account + '::*' - } + private anyFromCode(act: any) { + return this.chain + '::' + act.account + '::*' + } - private anyFromName(act: any) { - return this.chain + '::*::' + act.name; - } + private anyFromName(act: any) { + return this.chain + '::*::' + act.name; + } - private codeActionPair(act: any) { - return this.chain + '::' + act.account + '::' + act.name; - } + private codeActionPair(act: any) { + return this.chain + '::' + act.account + '::' + act.name; + } - private anyFromDeltaCode(delta: any) { - return this.chain + '::' + delta.code + '::*' - } + private anyFromDeltaCode(delta: any) { + return this.chain + '::' + delta.code + '::*' + } - private anyFromDeltaTable(delta: any) { - return this.chain + '::*::' + delta.table; - } + private anyFromDeltaTable(delta: any) { + return this.chain + '::*::' + delta.table; + } - private codeDeltaPair(delta: any) { - return this.chain + '::' + delta.code + '::' + delta.table; - } + private codeDeltaPair(delta: any) { + return this.chain + '::' + delta.code + '::' + delta.table; + } - protected checkBlacklist(act) { + protected checkBlacklist(act) { - // test for chain::code::* - if (this.filters.action_blacklist.has(this.anyFromCode(act))) { - return true; - } + // test for chain::code::* + if (this.filters.action_blacklist.has(this.anyFromCode(act))) { + return true; + } - // test for chain::*::name - if (this.filters.action_blacklist.has(this.anyFromName(act))) { - return true; - } + // test for chain::*::name + if (this.filters.action_blacklist.has(this.anyFromName(act))) { + return true; + } - // test for chain::code::name - return this.filters.action_blacklist.has(this.codeActionPair(act)); - } + // test for chain::code::name + return this.filters.action_blacklist.has(this.codeActionPair(act)); + } - protected checkWhitelist(act) { + protected checkWhitelist(act) { - // test for chain::code::* - if (this.filters.action_whitelist.has(this.anyFromCode(act))) { - return true; - } + // test for chain::code::* + if (this.filters.action_whitelist.has(this.anyFromCode(act))) { + return true; + } - // test for chain::*::name - if (this.filters.action_whitelist.has(this.anyFromName(act))) { - return true; - } + // test for chain::*::name + if (this.filters.action_whitelist.has(this.anyFromName(act))) { + return true; + } - // test for chain::code::name - return this.filters.action_whitelist.has(this.codeActionPair(act)); - } + // test for chain::code::name + return this.filters.action_whitelist.has(this.codeActionPair(act)); + } - protected checkDeltaBlacklist(delta) { + protected checkDeltaBlacklist(delta) { - // test blacklist for chain::code::* - if (this.filters.delta_blacklist.has(this.anyFromDeltaCode(delta))) { - return true; - } + // test blacklist for chain::code::* + if (this.filters.delta_blacklist.has(this.anyFromDeltaCode(delta))) { + return true; + } - // test blacklist for chain::*::table - if (this.filters.delta_blacklist.has(this.anyFromDeltaTable(delta))) { - return true; - } + // test blacklist for chain::*::table + if (this.filters.delta_blacklist.has(this.anyFromDeltaTable(delta))) { + return true; + } - // test blacklist for chain::code::table - return this.filters.delta_blacklist.has(this.codeDeltaPair(delta)); - } + // test blacklist for chain::code::table + return this.filters.delta_blacklist.has(this.codeDeltaPair(delta)); + } - protected checkDeltaWhitelist(delta) { + protected checkDeltaWhitelist(delta) { - // test whitelist for chain::code::* - if (this.filters.delta_whitelist.has(this.anyFromDeltaCode(delta))) { - return true; - } + // test whitelist for chain::code::* + if (this.filters.delta_whitelist.has(this.anyFromDeltaCode(delta))) { + return true; + } - // test whitelist for chain::*::table - if (this.filters.delta_whitelist.has(this.anyFromDeltaTable(delta))) { - return true; - } + // test whitelist for chain::*::table + if (this.filters.delta_whitelist.has(this.anyFromDeltaTable(delta))) { + return true; + } - // test whitelist for chain::code::table - return this.filters.delta_whitelist.has(this.codeDeltaPair(delta)); - } + // test whitelist for chain::code::table + return this.filters.delta_whitelist.has(this.codeDeltaPair(delta)); + } - loadAbiHex(contract, block_num, abi_hex) { - // check local blacklist for corrupted abis that failed to load before - let _status; - if (this.failedAbiMap.has(contract) && this.failedAbiMap.get(contract).has(block_num)) { - _status = false; - debugLog('ignore saved abi for', contract, block_num); - } else { - _status = AbiEOS.load_abi_hex(contract, abi_hex); - if (!_status) { - hLog(`AbiEOS.load_abi_hex error for ${contract} at ${block_num}`); - if (this.failedAbiMap.has(contract)) { - this.failedAbiMap.get(contract).add(block_num); - } else { - this.failedAbiMap.set(contract, new Set([block_num])); - } - } else { - this.removeFromFailed(contract); - } - } - return _status; - } + loadAbiHex(contract, block_num, abi_hex) { + // check local blacklist for corrupted abis that failed to load before + let _status; + if (this.failedAbiMap.has(contract) && this.failedAbiMap.get(contract).has(block_num)) { + _status = false; + debugLog('ignore saved abi for', contract, block_num); + } else { + _status = AbiEOS.load_abi_hex(contract, abi_hex); + if (!_status) { + hLog(`AbiEOS.load_abi_hex error for ${contract} at ${block_num}`); + if (this.failedAbiMap.has(contract)) { + this.failedAbiMap.get(contract).add(block_num); + } else { + this.failedAbiMap.set(contract, new Set([block_num])); + } + } else { + this.removeFromFailed(contract); + } + } + return _status; + } - removeFromFailed(contract) { - if (this.failedAbiMap.has(contract)) { - this.failedAbiMap.delete(contract); - hLog(`${contract} was removed from the failed map!`); - } - } + removeFromFailed(contract) { + if (this.failedAbiMap.has(contract)) { + this.failedAbiMap.delete(contract); + hLog(`${contract} was removed from the failed map!`); + } + } - async loadCurrentAbiHex(contract) { - let _status; - if (this.failedAbiMap.has(contract) && this.failedAbiMap.get(contract).has(-1)) { - _status = false; - debugLog('ignore current abi for', contract); - } else { - const currentAbi = await this.rpc.getRawAbi(contract); - if (currentAbi.abi.byteLength > 0) { - const abi_hex = Buffer.from(currentAbi.abi).toString('hex'); - _status = AbiEOS.load_abi_hex(contract, abi_hex); - if (!_status) { - hLog(`AbiEOS.load_abi_hex error for ${contract} at head`); - if (this.failedAbiMap.has(contract)) { - this.failedAbiMap.get(contract).add(-1); - } else { - this.failedAbiMap.set(contract, new Set([-1])); - } - } else { - this.removeFromFailed(contract); - } - } else { - _status = false; - } - } - return _status; - } + async loadCurrentAbiHex(contract) { + let _status; + if (this.failedAbiMap.has(contract) && this.failedAbiMap.get(contract).has(-1)) { + _status = false; + debugLog('ignore current abi for', contract); + } else { + const currentAbi = await this.rpc.getRawAbi(contract); + if (currentAbi.abi.byteLength > 0) { + const abi_hex = Buffer.from(currentAbi.abi).toString('hex'); + _status = AbiEOS.load_abi_hex(contract, abi_hex); + if (!_status) { + hLog(`AbiEOS.load_abi_hex error for ${contract} at head`); + if (this.failedAbiMap.has(contract)) { + this.failedAbiMap.get(contract).add(-1); + } else { + this.failedAbiMap.set(contract, new Set([-1])); + } + } else { + this.removeFromFailed(contract); + } + } else { + _status = false; + } + } + return _status; + } - abstract run(): Promise + abstract run(): Promise - abstract assertQueues(): void + abstract assertQueues(): void - abstract onIpcMessage(msg: any): void + abstract onIpcMessage(msg: any): void } diff --git a/workers/indexer.ts b/workers/indexer.ts index acf71a4..9ab5561 100644 --- a/workers/indexer.ts +++ b/workers/indexer.ts @@ -48,11 +48,9 @@ export default class IndexerWorker extends HyperionWorker { this.indexQueue.pause(); this.ch_ready = false; }); - this.ch.assertQueue(process.env.queue, {durable: true}).then(r => { - this.ch.consume(process.env.queue, this.indexQueue.push, { - prefetch: this.conf.prefetch.index - }).catch(console.log); - }); + this.ch.assertQueue(process.env.queue, {durable: true}); + this.ch.prefetch(this.conf.prefetch.index); + this.ch.consume(process.env.queue, this.indexQueue.push); } } catch (e) { console.error('rabbitmq error!');