-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
137 lines (108 loc) · 5.06 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
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
/**
* Middleware service for handling emitted events on chronobank platform
* @module Chronobank/waves-blockprocessor
*
* Copyright 2017–2018, LaborX PTY
* Licensed under the AGPL Version 3 license.
* @author Kirill Sergeev <[email protected]>
*/
const mongoose = require('mongoose'),
config = require('./config'),
Promise = require('bluebird'),
MasterNodeService = require('middleware-common-components/services/blockProcessor/MasterNodeService'),
models = require('./models'),
_ = require('lodash'),
providerService = require('./services/providerService'),
BlockWatchingService = require('./services/blockWatchingService'),
SyncCacheService = require('./services/syncCacheService'),
AmqpService = require('middleware_common_infrastructure/AmqpService'),
InfrastructureInfo = require('middleware_common_infrastructure/InfrastructureInfo'),
InfrastructureService = require('middleware_common_infrastructure/InfrastructureService'),
filterTxsByAccountService = require('./services/filterTxsByAccountService'),
amqp = require('amqplib'),
bunyan = require('bunyan'),
log = bunyan.createLogger({name: 'core.blockProcessor', level: config.logs.level});
mongoose.Promise = Promise;
mongoose.connect(config.mongo.data.uri, {useMongoClient: true});
mongoose.accounts = mongoose.createConnection(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();
};
const init = async function () {
if (config.checkSystem)
await runSystem();
[mongoose.accounts, mongoose.connection].forEach(connection =>
connection.on('disconnected', () => {
throw new Error('mongo disconnected!');
})
);
models.init();
let amqpInstance = await amqp.connect(config.rabbit.url);
let channel = await amqpInstance.createChannel();
channel.on('close', () => {
throw new Error('rabbitmq process has finished!');
});
await channel.assertExchange('events', 'topic', {durable: false});
await channel.assertExchange('internal', 'topic', {durable: false});
await channel.assertQueue(`${config.rabbit.serviceName}_current_provider.get`, {durable: false, autoDelete: true});
await channel.bindQueue(`${config.rabbit.serviceName}_current_provider.get`, 'internal', `${config.rabbit.serviceName}_current_provider.get`);
const masterNodeService = new MasterNodeService(channel, config.rabbit.serviceName);
await masterNodeService.start();
providerService.on('provider_set', providerURI => {
let providerIndex = _.findIndex(config.node.providers, providerURI);
if (providerIndex !== -1)
channel.publish('internal', `${config.rabbit.serviceName}_current_provider.set`, new Buffer(JSON.stringify({index: providerIndex})));
});
channel.consume(`${config.rabbit.serviceName}_current_provider.get`, async () => {
let providerInstance = await providerService.get();
let providerIndex = _.findIndex(config.node.providers, provider => provider.http === providerInstance.http);
if (providerIndex !== -1)
channel.publish('internal', `${config.rabbit.serviceName}_current_provider.set`, new Buffer(JSON.stringify({index: providerIndex})));
}, {noAck: true});
const syncCacheService = new SyncCacheService();
let blockEventCallback = async block => {
log.info(`${block.signature} (${block.number}) added to cache.`);
let filtered = await filterTxsByAccountService(block.transactions);
await Promise.all(filtered.map(item => {
channel.publish('events', `${config.rabbit.serviceName}_transaction.${item.address}`, new Buffer(JSON.stringify(Object.assign(item))));
}));
};
let txEventCallback = async tx => {
let filtered = await filterTxsByAccountService([tx]);
await Promise.all(filtered.map(item => {
channel.publish('events', `${config.rabbit.serviceName}_transaction.${item.address}`, new Buffer(JSON.stringify(Object.assign(item))));
}));
};
syncCacheService.on('block', blockEventCallback);
let endBlock = await syncCacheService.start();
await new Promise(res => {
if (config.sync.shadow)
return res();
syncCacheService.on('end', () => {
log.info(`cached the whole blockchain up to block: ${endBlock}`);
res();
});
});
const blockWatchingService = new BlockWatchingService(endBlock);
blockWatchingService.on('block', blockEventCallback);
blockWatchingService.on('tx', txEventCallback);
await blockWatchingService.startSync(endBlock);
};
module.exports = init().catch(err => {
log.error(err);
process.exit(0);
});