-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
150 lines (115 loc) · 5.31 KB
/
index.js
File metadata and controls
150 lines (115 loc) · 5.31 KB
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
/**
* Copyright 2017–2018, LaborX PTY
* Licensed under the AGPL Version 3 license.
* @author Egor Zuev <zyev.egor@gmail.com>
*/
const mongoose = require('mongoose'),
config = require('./config'),
models = require('./models'),
MasterNodeService = require('middleware-common-components/services/blockProcessor/MasterNodeService'),
Promise = require('bluebird'),
filterTxsByAccountsService = require('./services/filterTxsByAccountsService'),
amqp = require('amqplib'),
bunyan = require('bunyan'),
providerService = require('./services/providerService'),
_ = require('lodash'),
AmqpService = require('middleware_common_infrastructure/AmqpService'),
InfrastructureInfo = require('middleware_common_infrastructure/InfrastructureInfo'),
InfrastructureService = require('middleware_common_infrastructure/InfrastructureService'),
BlockWatchingService = require('./services/blockWatchingService'),
SyncCacheService = require('./services/syncCacheService'),
log = bunyan.createLogger({name: 'core.blockProcessor', level: config.logs.level});
/**
* @module entry point
* @description process blocks, and notify, through rabbitmq, other
* services about new block or tx, where we meet registered address
*/
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.hash} (${block.number}) added to cache.`);
await channel.publish('events', `${config.rabbit.serviceName}_block`, new Buffer(JSON.stringify({block: block.number})));
let filtered = await filterTxsByAccountsService(block.txs);
for (let item of filtered)
for (let tx of item.txs)
await channel.publish('events', `${config.rabbit.serviceName}_transaction.${item.address}`, new Buffer(JSON.stringify(tx)));
};
let txEventCallback = async tx => {
let filtered = await filterTxsByAccountsService([tx]);
for (let item of filtered)
for (let tx of item.txs)
await channel.publish('events', `${config.rabbit.serviceName}_transaction.${item.address}`, new Buffer(JSON.stringify(tx)));
};
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();
};
module.exports = init().catch(err => {
if (_.get(err, 'code') === 0) {
log.info('nodes are down or not synced!');
} else {
log.error(err);
}
process.exit(0);
});