Revert "include amqp-connection-manager"

This reverts commit 0579da66
This commit is contained in:
Igor Lins e Silva
2023-09-25 01:05:33 -03:00
parent e804aff53c
commit e5d0126d08
13 changed files with 339 additions and 369 deletions
+68 -76
View File
@@ -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<IAmqpConnectionManager> {
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<Connection> {
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<ChannelWrapper[] | null> {
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;
}
}
+1 -2
View File
@@ -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<ChannelWrapper[]> {
async createAMQPChannels(onReconnect, onClose) {
return await amqpConnect(onReconnect, this.conn.amqp, onClose);
}
+7
View File
@@ -20,4 +20,11 @@ readdirSync(chainsRoot)
}
});
apps.push({
name: 'hyperion-governor',
namespace: 'hyperion',
script: 'governor/server/index.js',
watch: false,
});
module.exports = {apps};
-21
View File
@@ -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",
-1
View File
@@ -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",
+6 -7
View File
@@ -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
});
}
}
+1 -1
View File
@@ -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;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,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,OAAO,CAAC,SAAS,EAAE,CAAC,OAAO,CAAC,CAAC,CAAC;aACvD;iBAAM,IAAI,OAAO,CAAC,SAAS,KAAK,IAAI,CAAC,SAAS,EAAE;gBAC7C,IAAI,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,EAAE;oBACvC,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;iBACvD;aACJ;iBAAM;gBACH,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC,CAAC,IAAI,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"}
+8 -10
View File
@@ -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<void> {
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
});
}
}
+3 -5
View File
@@ -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));
}
}
+9 -5
View File
@@ -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);
+7 -7
View File
@@ -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);
});
}
}
+226 -229
View File
@@ -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<string, Set<number>> = new Map();
failedAbiMap: Map<string, Set<number>> = 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<void>
abstract run(): Promise<void>
abstract assertQueues(): void
abstract assertQueues(): void
abstract onIpcMessage(msg: any): void
abstract onIpcMessage(msg: any): void
}
+3 -5
View File
@@ -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!');