-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
97 lines (80 loc) · 3.31 KB
/
index.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
/**
* Copyright 2017–2018, LaborX PTY
* Licensed under the AGPL Version 3 license.
* @author Egor Zuev <zyev.egor@gmail.com>
*/
/**
* Middleware service for handling emitted events on chronobank platform
* @module Chronobank/eth-chrono-sc-processor
*/
const config = require('./config'),
Promise = require('bluebird'),
mongoose = require('mongoose'),
accountModel = require('./models/accountModel'),
smEvents = require('./factories/sc/smartContractsEventsFactory'),
filterTxsBySMEventsService = require('./services/filterTxsBySMEventsService'),
bunyan = require('bunyan'),
AmqpService = require('middleware_common_infrastructure/AmqpService'),
InfrastructureInfo = require('middleware_common_infrastructure/InfrastructureInfo'),
InfrastructureService = require('middleware_common_infrastructure/InfrastructureService'),
log = bunyan.createLogger({name: 'plugins.chronoSCProcessor', level: config.logs.level}),
amqp = require('amqplib');
mongoose.Promise = Promise; // Use custom Promises
mongoose.connect(config.mongo.accounts.uri, {useMongoClient: true});
const runSystem = async function () {
const rabbit = new AmqpService(
config.systemRabbit.url,
config.systemRabbit.exchange,
config.systemRabbit.serviceName
);
const info = new InfrastructureInfo(require('./package.json'));
const system = new InfrastructureService(info, rabbit, {checkInterval: 10000});
await system.start();
system.on(system.REQUIREMENT_ERROR, (requirement, version) => {
log.error(`Not found requirement with name ${requirement.name} version=${requirement.version}.` +
` Last version of this middleware=${version}`);
process.exit(1);
});
await system.checkRequirements();
system.periodicallyCheck();
};
let init = async () => {
if (config.checkSystem)
await runSystem();
mongoose.connection.on('disconnected', () => {
throw new Error('mongo disconnected!');
});
const conn = await amqp.connect(config.rabbit.url);
const channel = await conn.createChannel();
channel.on('close', () => {
throw new Error('rabbitmq process has finished!');
});
await accountModel.update({address: smEvents.address}, {$set: {address: smEvents.address}}, {
upsert: true,
setDefaultsOnInsert: true
});
await channel.assertExchange('events', 'topic', {durable: false});
await channel.assertQueue(`app_${config.rabbit.serviceName}.chrono_sc_processor`);
await channel.bindQueue(`app_${config.rabbit.serviceName}.chrono_sc_processor`, 'events', `${config.rabbit.serviceName}_transaction.${smEvents.address}`);
channel.prefetch(2);
// Listen to Rabbit
channel.consume(`app_${config.rabbit.serviceName}.chrono_sc_processor`, async (data) => {
try {
let payload = JSON.parse(data.content.toString());
if (!payload.blockNumber || payload.blockNumber === -1)
return channel.ack(data);
let filteredEvents = filterTxsBySMEventsService(payload);
for (let event of filteredEvents) {
log.info(`emitted event ${event.name} on transaction ${payload.hash}`);
channel.publish('events', `${config.rabbit.serviceName}_chrono_sc.${event.name.toLowerCase()}`, new Buffer(JSON.stringify(event)));
}
} catch (err) {
log.error(err);
}
channel.ack(data);
});
};
module.exports = init().catch(err => {
log.error(err);
process.exit(0);
});