-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
126 lines (113 loc) · 3.23 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
const assert = require('assert')
const crypto = require('crypto')
const fs = require('fs')
const async = require('async')
const init = require('./src/init')
const initRuntime = require('./src/runtime')
function verifyGenesisBlock(scope, block) {
try {
const payloadHash = crypto.createHash('sha256')
for (let i = 0; i < block.transactions.length; ++i) {
const trs = block.transactions[i]
const bytes = scope.base.transaction.getBytes(trs)
payloadHash.update(bytes)
}
const id = scope.base.block.getId(block)
assert.equal(
payloadHash.digest().toString('hex'),
block.payloadHash,
'Unexpected payloadHash',
)
assert.equal(id, block.id, 'Unexpected block id')
} catch (e) {
throw e
}
}
class Application {
constructor(options) {
this.options = options
}
run() {
const options = this.options
const pidFile = options.pidFile
global.featureSwitch = {}
global.state = {}
init(options, (error, scope) => {
if (error) {
scope.logger.fatal(error)
if (fs.existsSync(pidFile)) {
fs.unlinkSync(pidFile)
}
process.exit(1)
return
}
process.once('cleanup', () => {
scope.logger.info('Cleaning up...')
async.eachSeries(scope.modules, (module, cb) => {
if (typeof (module.cleanup) === 'function') {
module.cleanup(cb)
} else {
setImmediate(cb)
}
}, (err) => {
if (err) {
scope.logger.error('Error while cleaning up', err)
} else {
scope.logger.info('Cleaned up successfully')
}
(async () => {
try {
await global.app.sdb.close()
} catch (e) {
scope.logger.error('failed to close sdb', e)
}
})()
if (fs.existsSync(pidFile)) {
fs.unlinkSync(pidFile)
}
process.exit(1)
})
})
process.once('SIGTERM', () => {
process.emit('cleanup')
})
process.once('exit', () => {
scope.logger.info('process exited')
})
process.once('SIGINT', () => {
process.emit('cleanup')
})
process.on('uncaughtException', (err) => {
// handle the error safely
scope.logger.fatal('uncaughtException', { message: err.message, stack: err.stack })
process.emit('cleanup')
})
process.on('unhandledRejection', (err) => {
// handle the error safely
scope.logger.error('unhandledRejection', err)
process.emit('cleanup')
})
verifyGenesisBlock(scope, scope.genesisblock.block)
options.library = scope;
(async () => {
try {
await initRuntime(options)
} catch (e) {
scope.logger.error('init runtime error: ', e)
process.exit(1)
return
}
scope.bus.message('bind', scope.modules)
global.modules = scope.modules
global.library = scope
scope.logger.info('Modules ready and launched')
if (!scope.config.publicIp) {
scope.logger.warn('Failed to get public ip, block forging MAY not work!')
}
})()
})
}
}
module.exports = {
Application,
}