forked from leapdao/leap-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
128 lines (104 loc) · 3.65 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
/**
* Copyright (c) 2018-present, Leap DAO (leapdao.org)
*
* This source code is licensed under the Mozilla Public License Version 2.0
* found in the LICENSE file in the root directory of this source tree.
*/
/* eslint-disable no-console */
/* global app, bridgeState, blockTicker, eventsRelay, db */
const cliArgs = require('./src/utils/cliArgs');
const cleanupLotion = require('./src/utils/cleanupLotion');
const readConfig = require('./src/utils/readConfig');
const { readPrivKey, writePrivKey } = require('./src/utils/privKey');
const Db = require('./src/api/db');
const jsonrpc = require('./src/api/jsonrpc');
const txHandler = require('./src/tx');
const blockHandler = require('./src/block');
const periodHandler = require('./src/period');
const { printStartupInfo } = require('./src/utils');
const BridgeState = require('./src/bridgeState');
const BlockTicker = require('./src/utils/BlockTicker');
const EventsRelay = require('./src/eventsRelay');
const lotion = require('./lotion');
const delayedSender = require('./src/txHelpers/delayedSender');
const heartbeatService = require('./src/heartbeat');
const { logNode, logTendermint } = require('./src/utils/debug');
async function run() {
const config = await (async () => {
let result;
try {
result = readConfig(cliArgs.config, cliArgs.rootNetwork);
} catch (err) {
console.error(err.message);
process.exit(0);
}
return result;
})();
global.app = lotion({
networkId: `${config.network}-${config.networkId}`,
genesis: config.genesis,
devMode: cliArgs.devMode,
abciPort: cliArgs.abciPort,
peers: config.peers,
p2pPort: cliArgs.p2pPort,
tendermintAddr: cliArgs.tendermintAddr,
tendermintPort: cliArgs.tendermintPort,
createEmptyBlocks: false,
logTendermint: log => {
logTendermint(
log.replace(/I\[\d{2}-\d{2}\|\d{2}:\d{2}:\d{2}\.\d{3}\] /g, '')
);
},
unsafeRpc: cliArgs.unsafeRpc,
dataPath: cliArgs.dataPath,
});
if (cliArgs.fresh) {
await cleanupLotion(app);
process.exit(0);
}
global.db = Db(app);
const privKey = await readPrivKey(app, cliArgs);
const sender = delayedSender(cliArgs.tendermintPort);
global.eventsRelay = new EventsRelay(config.eventsDelay, sender);
global.bridgeState = new BridgeState(
db,
privKey,
config,
eventsRelay.relayBuffer,
sender
);
global.blockTicker = new BlockTicker(bridgeState.web3, [
bridgeState.onNewBlock,
]);
await writePrivKey(app, cliArgs, bridgeState.account.privateKey);
await bridgeState.init();
await blockTicker.init();
const nodeConfig = Object.assign({}, cliArgs, { network: config });
app.useTx(txHandler(bridgeState, nodeConfig));
app.useBlock(blockHandler(bridgeState, db, nodeConfig));
app.usePeriod(periodHandler(bridgeState));
const lastGoodState = await bridgeState.loadState();
heartbeatService(bridgeState, sender);
app.listen(lastGoodState).then(async params => {
blockTicker.subscribe(eventsRelay.onNewBlock);
await printStartupInfo(params, bridgeState);
const api = await jsonrpc(bridgeState, cliArgs.tendermintPort, db, app);
api
.listenHttp({ port: cliArgs.rpcport, host: cliArgs.rpcaddr })
.then(addr => {
logNode(
`Http JSON RPC server is listening at ${addr.address}:${addr.port}`
);
});
api.listenWs({ port: cliArgs.wsport, host: cliArgs.wsaddr }).then(addr => {
logNode(
`Ws JSON RPC server is listening at ${addr.address}:${addr.port}`
);
});
});
}
run();
process.on('unhandledRejection', error => {
// Will print "unhandledRejection err is not defined"
console.log('unhandledRejection', error);
});