/
app.js
142 lines (108 loc) · 3.68 KB
/
app.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
138
139
140
141
142
import appConfig from '../src/data/config.json';
import path from 'path';
import Web3 from 'web3';
import Config from 'truffle-config';
import Resolver from 'truffle-resolver';
import callAsync from '../src/util/web3Util';
import {getBuiltContract} from '../src/util/buildDir';
import EventWatcher from '../src/EventWatcher';
const tokenJson = getBuiltContract('Token');
import log4js from 'log4js';
log4js.configure({
appenders: {
elasticsearch: { type: 'file', filename: 'elasticsearch.log' },
out: { type: 'stdout' },
},
categories: { default: { appenders: ['elasticsearch', 'out'], level: 'debug' } }
});
const logger = log4js.getLogger('elasticsearch');
import {AwsEsPublicClient} from '../src/util/esClient';
const EVENT_INDEX = 'toss_event_' + appConfig.elasticsearch.indexPostfix;
const TAG_INDEX = 'toss_tag_' + appConfig.elasticsearch.indexPostfix;
const BET_INDEX = 'toss_bet_' + appConfig.elasticsearch.indexPostfix;
const esClient = new AwsEsPublicClient(
{ log: 'error' },
appConfig.elasticsearch.esNode,
appConfig.elasticsearch.region,
appConfig.elasticsearch.useSSL
);
(async (callback) => {
const web3 = new Web3();
const config = Config.detect({'network': appConfig.network});
const resolver = new Resolver(config);
const provider = config.provider;
const Main = resolver.require("../contracts/Main.sol");
const EventBase = resolver.require("../contracts/EventBase.sol");
const Token = resolver.require("../contracts/Token.sol");
web3.setProvider(provider);
Token.setProvider(provider);
Main.setProvider(provider);
EventBase.setProvider(provider);
const coinbase = await callAsync(web3.eth.getCoinbase);
Token.defaults({from: coinbase});
Main.defaults({from: coinbase});
EventBase.defaults({from: coinbase});
web3.eth.defaultAccount = coinbase;
/**
* Emergency stop
*/
const fatal = function() {
let _fatal = logger.fatal.bind(logger);
for (let key in arguments) {
if (arguments.hasOwnProperty(key)) {
_fatal = _fatal.bind(logger, arguments[key]);
}
}
_fatal();
callback();
process.exit(1);
};
await esClient.ping({
// ping usually has a 3000ms timeout
requestTimeout: 5000
}).then(() => {
logger.info('elasticsearch cluster is up');
}).catch((error) => {
fatal(error, 'elasticsearch cluster is down! exiting');
});
logger.info('Trying to resolve cache_state.json');
const cacheStateFile = path.resolve(__dirname, 'cache_state.json');
logger.info(`Network id: ${Token.network_id}`);
logger.info(`Trying to find first block.`);
const tokenTransactionHash = tokenJson.networks[Token.network_id].transactionHash;
logger.info(`Token.transactionHash: ${tokenTransactionHash}.`);
const firstBlock = (await callAsync(web3.eth.getTransactionReceipt.bind(web3.eth, tokenTransactionHash))).blockNumber;
logger.info(`First block: #${firstBlock}`);
const eventWatcher = new EventWatcher(
EVENT_INDEX,
TAG_INDEX,
BET_INDEX,
esClient,
logger,
web3,
config,
{
Token,
Main,
EventBase,
},
appConfig.EventBase,
cacheStateFile,
firstBlock,
true
);
await eventWatcher.initPromise;
try {
await eventWatcher.cacheEvents();
await eventWatcher.cacheEventUpdates();
eventWatcher.tryWatchEvents();
eventWatcher.tryWatchEventUpdates();
setInterval(() => {
logger.info('Auto retry retryWatchEvents() and retryWatchEventUpdates() after 1 minute.');
eventWatcher.tryWatchEvents();
eventWatcher.tryWatchEventUpdates();
}, 1000 * 20);
} catch (err) {
fatal(err);
}
})(() => { logger.trace('Exit...'); }).catch((error) => { logger.fatal(error, 'Emergency stop'); });