-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
304 lines (265 loc) · 11.2 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
const express = require("express");
const bodyParser = require("body-parser");
const axios = require("axios");
const sqlite3 = require('sqlite3').verbose();
if (!process.env.RPC) {
throw Error("env var RPC not set");
}
const db = process.env.DB_FILE !== undefined ?
new sqlite3.Database(process.env.DB_FILE, (err) => {
if (err) throw Error(`opening DB at ${process.env.DB_FILE} failed: ${err.message}`);
db.run(`CREATE TABLE IF NOT EXISTS data(key TEXT PRIMARY KEY, val TEXT, ts INTEGER)`, (err) => {
if (err) throw Error(`create table failed: ${err.message}`);
});
console.log(`opened and initialized DB ${process.env.DB_FILE}`);
}) :
undefined;
const port = process.env.PORT || 3000;
const rpc = process.env.RPC;
let app; // http
let client; // ws
let server; // ws
let socket; // socket.io
(async () => {
console.log("init");
const rpcUrl = new URL(rpc);
if(rpcUrl.protocol === "ws:" || rpcUrl.protocol === "wss:") {
handleWsConnection(rpcUrl);
} else {
handleHttpConnection(rpcUrl);
}
})()
// value type: { ts, val, readCnt, writeCnt } where `ts` is a timestamp (int) and val the cached value (whatever was returned)
const cache = new Map();
// print some stats on shutdown (e.g. Ctrl-C)
process.on("SIGINT", printStatsAndExit);
function printStats() {
console.log("stats:");
console.log(`nr http requests processed: ${httpReqCnt}`);
console.log(`nr http upstream responses: ${httpUpstreamResCnt}`);
console.log(`nr http cached responses: ${httpCacheResCnt}`);
if (db !== undefined) {
console.log("DB cache stats:");
db.get(`SELECT COUNT(*) as count FROM data`, (err, row) => {
if (err) throw err;
console.log(`DB contains ${row.count} entries`);
});
} else {
console.log("in-memory cache stats:");
for (const key of cache.keys()) {
value = cache.get(key);
console.log(`key ${key}: ${value.readCnt} reads, ${value.writeCnt} writes`);
}
}
}
function printStatsAndExit() {
printStats();
process.exit();
}
function getCacheKey(req) {
return `${req.body.method}${JSON.stringify(req.body.params)}`;
}
function getFromDb(key) {
return new Promise((resolve, reject) => {
db.get(`SELECT val FROM data WHERE key = ?`, [key], (err, row) => {
if (err) {
reject(err);
} else {
resolve(row);
}
});
});
}
// returns the response to send from cache or undefined if not cached.
// Reasons for undefined response: unsupported method, not yet set, outdated
// param key: cache key
// param maxAgeMs: maximum age the cached entry may have to be considered. Can be `Infinity`
// param reqId: json-rpc id. Is just passed through
async function getResponseFromCache(key, maxAgeMs, reqId) {
let val;
if (cache.has(key)) { // try from cache
if (Date.now() - cache.get(key).ts <= maxAgeMs) {
val = cache.get(key).val;
// increment counter
cache.set(key, { ...cache.get(key), readCnt: cache.get(key).readCnt+1 });
} else {
console.debug(`cached entry skipped, too old (> ${maxAgeMs} ms)`);
}
} else if (db !== undefined) { // try from DB
try {
const row = await getFromDb(key);
// If the key doesn't exist, row will be undefined
if (row) {
if (Date.now() - cache.get(key).ts <= maxAgeMs) {
//console.log(`DB: req ${reqId} retrieved key ${key}, value ${row.val}`);
// We expect the stored value to be a JSON string, so parse it before returning.
val = JSON.parse(row.val);
} else {
console.debug(`DB entry skipped, too old (> ${maxAgeMs} ms)`);
}
}
} catch (err) {
console.error(`DB read error: ${err.message}`);
}
}
// receipts may have been null previously, in that case ignore the cached value
if (val !== undefined && val !== null) {
return {
jsonrpc: "2.0",
id: reqId,
result: val
};
}
}
function writeResponseToCache(key, val) {
const newValue = {
val,
ts: Date.now(),
readCnt: cache.has(key) ? cache.get(key).readCnt : 0,
writeCnt: cache.has(key) ? cache.get(key).writeCnt+1 : 1
};
console.debug(`writing to cache: key ${key}, value ${JSON.stringify(newValue)}`);
if (db !== undefined) {
db.run(`INSERT OR REPLACE INTO data(key, val, ts) VALUES(?, ?, ?)`, [key, JSON.stringify(newValue.val), newValue.ts], function(err) {
if (err) return console.error(err.message);
});
} else {
cache.set(key, newValue);
}
}
// ********************************************************
// HTTP
// ********************************************************
let httpReqCnt = 0;
let httpUpstreamResCnt = 0;
let httpCacheResCnt = 0;
// key: method+params, value: timestamp
const duplicateDetector = new Map();
const DUPLICATE_DELAY_TRIGGER_THRESHOLD_MS=1000;
const DUPLICATE_MIN_DELAY_MS=500;
const DUPLICATE_RANDOM_MAX_EXTRA_DELAY_MS=1000;
const CACHE_MAX_AGE = process.env.CACHE_MAX_AGE || 10;
async function handleHttpConnection(rpcUrl) {
app = express();
app.use(bodyParser.json());
app.listen(port, () => {
console.log(`listening on port ${port}`);
});
app.get("/printstats", printStats);
app.post("/", async (req, res) => {
console.log(`#${req.body.id} RPC request at ${Date.now()/1000}: ${JSON.stringify(req.body)}`);
//const data = {"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1};
// check if duplicate - throttle if so in order to give the cache a chance to already be filled
const cacheKey = getCacheKey(req);
if (duplicateDetector.has(cacheKey)) {
const prevCallTs = duplicateDetector.get(cacheKey);
if (Date.now() - prevCallTs < DUPLICATE_DELAY_TRIGGER_THRESHOLD_MS) {
// adding randomization to the delay in order to avoid scenarios where all duplicates meet a barely outdated cache
const delayMs = DUPLICATE_MIN_DELAY_MS + Math.floor(Math.random() * DUPLICATE_RANDOM_MAX_EXTRA_DELAY_MS);
console.debug(`delaying potential duplicate request for ${delayMs} ms...`);
await new Promise(resolve => setTimeout(resolve, delayMs));
}
}
duplicateDetector.set(cacheKey, Date.now());
// Check if we can reuse a cached response
const cacheMaxAgeMs = ["eth_chainId", "net_version", "eth_getTransactionReceipt"].includes(req.body.method) ? Infinity : CACHE_MAX_AGE*1000;
const cachedResponse = await getResponseFromCache(cacheKey, cacheMaxAgeMs, req.body.id);
if (cachedResponse) {
console.log(`#${req.body.id} Cached response for ${req.body.method}: ${JSON.stringify(cachedResponse)}`);
res.send(cachedResponse);
httpCacheResCnt++;
} else {
// ... forward request to upstream
try {
const rpcRes = await upstreamHttpRequest(rpc, req.body);
console.log(`#${req.body.id} Upstream response for ${req.body.method}: ${JSON.stringify(rpcRes.data)}`);
httpUpstreamResCnt++;
// send response to the client
res.send(rpcRes.data);
// cache some of them
if (
["eth_chainId", "eth_blockNumber", "net_version"].includes(req.body.method) ||
(req.body.method === "eth_call" && req.body.params.some(param => typeof param === 'object' && 'blockHash' in param)) // eth_call is immutable if referring to a specific block
) {
writeResponseToCache(cacheKey, rpcRes.data.result);
}
} catch(err) {
console.error(`upstream request permanently failed: ${err}`);
res.status(500).send(err);
}
}
httpReqCnt++;
});
}
// param rpc: url of the upstream http rpc
// param reqBody: body of the POST request
// returns the response data
// throws on permanent failure (after exhausting all retries)
// if the request fails, it retries with exponential backoff
async function upstreamHttpRequest(rpc, reqBody, nrRetries = 10, initialTimeoutMs = 2000) {
try {
return await axios.post(rpc, reqBody);
} catch(err) {
console.debug(`upstream retry - #${nrRetries} attempts left, next timeout: ${initialTimeoutMs} ms`);
let forwardedErr = "unspecified";
if(err.response) {
console.error(`upstream err | response: ${JSON.stringify(err.response)}`);
forwardedErr = JSON.stringify(err.response);
} else if(err.request) {
console.error("upstream err | no response´");
forwardedErr = JSON.stringify(err.request.data);
} else {
console.error("upstream err | no response, no request -> WTF´");
}
// retry loop implemented via recursion. Double timeout for exponential backoff
if (nrRetries > 0) {
await new Promise(resolve => setTimeout(resolve, initialTimeoutMs));
return upstreamHttpRequest(rpc, reqBody, nrRetries-1, initialTimeoutMs*2)
} else {
throw Error(forwardErr) // if giving up, hand over the last error
}
}
}
// ********************************************************
// Websocket
// ********************************************************
function handleWsConnection(rpcUrl) {
console.log("opening websocket connection...");
const WS = require("ws");
server = new WS.WebSocketServer({port: port});
client = new WS(rpc);
let connCnt = 0;
server.on("connection", conn => {
const connId = connCnt++;
console.log(`WS #${connId} client connection established`);
conn.on("message", data => {
console.log(`WS #${connId} forwarding: ${data.toString()}`);
client.send(data.toString(), (err) => {
if(err !== undefined) {
console.log(`### WS #${connId} send error: ${err}`);
}
});
});
client.on("message", msg => {
console.log(`WS #${connId} backwarding: ${msg.toString()}`);
conn.send(msg.toString());
});
});
server.on("close", () => console.log("### A connection was closed"));
server.on("error", (err) => console.log(`### Error ${err}`));
client.once("open", () => console.log("WS upstream connected!"));
}
/*
app.post("/ws", (req, res) => {
console.log(`req id ${req.body.id}, body: ${JSON.stringify(req.body, null, 2)}`);
//const data = {"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1};
client.send(JSON.stringify(req.body), res => console.log("Send response: ", res));
});
*/
/*
app.post("/io", (req, res) => {
console.log(`req.body: ${JSON.stringify(req.body, null, 2)}`);
//const data = {"jsonrpc":"2.0","method":"eth_blockNumber","params":[],"id":1};
socket.emit(JSON.stringify(req.body), res => console.log("RPC response: ", res));
});
*/