This repository has been archived by the owner on Mar 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
271 lines (235 loc) · 8.28 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// Copyright (c) 2018 QLC Chain Team
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
require('dotenv').config();
import express, { json as _json } from 'express';
import * as http from 'http';
import request from 'request-promise-native';
import cors from 'cors';
import { promisify } from 'util';
import { logger } from './log';
const { PerformanceObserver, performance } = require('perf_hooks');
const Timestamp = require('./timestamps').default;
const PushServer = require('./wss').default;
const LogDb = require('./logs').default;
/** Configuration **/
const qlcNodeUrl = process.env.QLC_NODE_URL || `http://qlc_node:29735`; // Nano node RPC url
const qlcWorkNodeUrl = process.env.QLC_WORK_NODE_URL || `http://qlc_node:29735`; // Nano work node RPC url
const listeningPort = process.env.APP_PORT || 8888; // Port this app will listen on
const statTime = 10;
const useRedisCache = !!process.env.USE_REDIS || false; // Change this if you are not running a Redis server. Will use in memory cache instead.
const redisCacheUrl = process.env.REDIS_HOST || `redis`; // Url to the redis server (If used)
const redisCacheTime = 60 * 60 * 24; // Store work for 24 Hours
const memoryCacheLength = 800; // How much work to store in memory (If used)
const obs = new PerformanceObserver(items => {
const entry = items.getEntries()[0];
logger.info(`${entry.name} cost ${entry.duration} ms`);
performance.clearMarks();
});
obs.observe({ entryTypes: ['measure'] });
let ts = new Timestamp(process.env.DB_HOST, process.env.DB_PORT, process.env.DB_USER, process.env.DB_PASS, process.env.DB_NAME);
let logdb = new LogDb(process.env.DB_HOST, process.env.DB_PORT, process.env.DB_USER, process.env.DB_PASS, process.env.DB_NAME);
let loggerstream = {
write: function(message) {
logger.info(message);
}
};
const workCache = [];
let getCache, putCache;
const subscriptionMap = {};
// Statistics reporting?
let tpsCount = 0;
const server = http.createServer(app);
const wss = new PushServer(server, subscriptionMap);
// Set up the webserver
const app = express();
server.on('request', app);
app.use(cors());
app.use(require('morgan')('combined', { stream: loggerstream }));
app.use(_json());
app.use((req, res, next) => {
if (req.headers['content-type']) {
return next();
}
req.headers['content-type'] = 'application/json';
next();
});
// Allow certain requests to the Nano RPC and cache work requests
app.post('/', async (req, res) => {
const allowedActions = [
'account_history',
'account_history_topn',
'account_info',
'accounts_frontiers',
'accounts_balances',
'accounts_pending',
'block',
'blocks',
'block_count',
'blocks_info',
'delegators_count',
'pending',
'process',
'representatives_online',
'validate_account_number',
'work_generate',
'tokens'
];
if (!req.body.action || allowedActions.indexOf(req.body.action) === -1) {
return res.status(500).json({
error: `Action ${req.body.action} not allowed`
});
}
let workRequest = false;
let representativeRequest = false;
let repCacheKey = `online-representatives`;
// Cache work requests
if (req.body.action === 'work_generate') {
if (!req.body.hash)
return res.status(500).json({
error: `Requires valid hash to perform work`
});
const cachedWork = useRedisCache ? await getCache(req.body.hash) : getCache(req.body.hash); // Only redis is an as operation
if (cachedWork && cachedWork.length) {
return res.json({
work: cachedWork
});
}
workRequest = true;
}
// Cache the online representatives request
if (req.body.action === 'representatives_online') {
const cachedValue = useRedisCache ? await getCache(repCacheKey) : getCache(repCacheKey); // Only redis is an async operation
if (cachedValue && cachedValue.length) {
return res.json(JSON.parse(cachedValue));
}
representativeRequest = true;
}
performance.mark('A');
request({
method: 'post',
uri: workRequest ? qlcWorkNodeUrl : qlcNodeUrl,
body: req.body,
json: true,
timeout: 200000
})
.then(async proxyRes => {
if (proxyRes) {
if (workRequest && proxyRes.work) {
putCache(req.body.hash, proxyRes.work);
}
if (representativeRequest && proxyRes.representatives) {
putCache(repCacheKey, JSON.stringify(proxyRes), 5 * 60); // Cache online representatives for 5 minutes
}
}
// Add timestamps to certain requests
if (req.body.action === 'account_history' || req.body.action === 'account_history_topn') {
proxyRes = await ts.mapAccountHistory(proxyRes);
}
if (req.body.action === 'blocks_info') {
proxyRes = await ts.mapBlocksInfo(req.body.hashes, proxyRes);
}
if (req.body.action === 'pending') {
proxyRes = await ts.mapPending(proxyRes);
}
performance.mark('B');
performance.measure(req.body.action, 'A', 'B');
res.json(proxyRes);
})
.catch(err => {
performance.mark('C');
performance.measure(`${req.body.action} error`, 'A', 'C');
logger.error(`${req.body.action}: ${err.message}`);
res.status(500).json({ error: err.toString() });
});
});
app.post('/new-block', async (req, res) => {
res.sendStatus(200);
tpsCount++;
const fullBlock = req.body;
try {
logger.info(`receive rpc callback ${fullBlock.hash} of ${fullBlock.block}`);
fullBlock.block = JSON.parse(fullBlock.block);
ts.saveHashTimestamp(fullBlock.hash);
} catch (err) {
return logger.error(`Error parsing block data! ${err.message}, ${err.stack}`);
}
let destinations = [];
if (fullBlock.block.type === 'state') {
if (fullBlock.is_send === 'true' && fullBlock.block.link_as_account) {
destinations.push(fullBlock.block.link_as_account);
}
destinations.push(fullBlock.account);
} else {
destinations.push(fullBlock.block.destination);
}
// Send it to all!
destinations.forEach(destination => {
if (!subscriptionMap[destination]) return; // Nobody listening for this
logger.info(`Sending block to subscriber ${destination}: ${fullBlock.amount}`);
subscriptionMap[destination].forEach(ws => {
const event = {
event: 'newTransaction',
data: fullBlock
};
ws.send(JSON.stringify(event));
});
});
});
app.get('/health-check', (req, res) => {
res.sendStatus(200);
});
app.post('/logs', async (req, res) => {
res.json({ OK: true });
const log = req.body;
logger.info(`REC: ${JSON.stringify(log)}`);
logdb.saveLog2Db(log);
});
server.listen(listeningPort, () => logger.info(`QLC Wallet server listening on port ${listeningPort}!`));
// Configure the cache functions to work based on if we are using redis or not
if (useRedisCache) {
const cacheClient = require('redis').createClient({
host: redisCacheUrl
});
cacheClient.on('ready', () => logger.info(`Redis Work Cache: Connected`));
cacheClient.on('error', err => logger.error(`Redis Work Cache: Error `, err));
cacheClient.on('end', () => logger.info(`Redis Work Cache: Connection closed`));
getCache = promisify(cacheClient.get).bind(cacheClient);
putCache = (hash, work, time) => {
cacheClient.set(hash, work, 'EX', time || redisCacheTime); // Store the work for 24 hours
};
} else {
getCache = hash => {
const existingHash = workCache.find(w => w.hash === hash);
return existingHash ? existingHash.work : null;
};
putCache = (hash, work, time) => {
if (time) return; // If a specific time is specified, don't cache at all for now
workCache.push({
hash,
work
});
if (workCache.length >= memoryCacheLength) {
workCache.shift(); // If the list is too long, prune it.
}
};
}
function printStats() {
const connectedClients = wss.length();
const tps = tpsCount / statTime;
logger.info(`[Stats] Connected clients: ${connectedClients}; TPS Average: ${tps}`);
tpsCount = 0;
}
setInterval(printStats, statTime * 1000); // Print stats every x seconds
// const WebSocket = require("ws");
// const ws = new WebSocket(`ws://localhost:${listeningPort}`);
// ws.on("open", function open() {
// ws.send(
// JSON.stringify({
// event: "subscribe",
// data: ["test_account1", "test_account2"]
// })
// );
// });
// ws.on("message", data => console.log(data));