forked from dyne/zenswarm-oracle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrestroom.mjs
479 lines (445 loc) · 14.2 KB
/
restroom.mjs
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
import fs from 'fs';
import path from 'path'
import readdirp from 'readdirp';
import express from "express";
import chalk from "chalk";
import bodyParser from "body-parser";
import zencode from "@restroom-mw/core";
import db from "@restroom-mw/db";
import fabric from "@restroom-mw/fabric";
import rrhttp from "@restroom-mw/http";
import rrredis from "@restroom-mw/redis";
import sawroom from "@restroom-mw/sawroom";
import ethereum from "@restroom-mw/ethereum";
import timestamp from "@restroom-mw/timestamp";
import files from "@restroom-mw/files";
import ui from "@restroom-mw/ui";
import { zencode_exec } from "zenroom"
import mqtt from "mqtt"
import http from "http";
import morgan from "morgan";
import winston from "winston";
import dotenv from "dotenv";
import axios from 'axios';
import chokidar from 'chokidar';
import yaml from 'js-yaml';
import WebSocket from 'ws';
import readLastLines from 'read-last-lines';
dotenv.config();
const L = new winston.createLogger({
level: 'debug',
transports: [
new winston.transports.File({ filename: './access.log', level: 'debug' }),
],
exitOnError: false
});
const MIN_PORT = 25000;
const MAX_PORT = 30000;
/*
* Load current L1 blockchains database
*/
import { readFile } from 'fs/promises';
const blockchainDB = JSON.parse(
await readFile(
new URL('./blockchain_db.json', import.meta.url)
)
).subscriptions;
/*
* Run a zenroom script (outside restroom mw)
*/
const zen = async (zencode, keys, data) => {
const params = {};
if (keys !== undefined && keys !== null) {
params.keys = typeof keys === 'string' ? keys : JSON.stringify(keys);
}
if (data !== undefined && data !== null) {
params.data = typeof data === 'string' ? data : JSON.stringify(data);
}
try {
return await zencode_exec(zencode, params);
} catch (e) {
console.log("Error from zencode_exec: ", e);
}
}
/*
* Monitor file with L1 data to be monitored
*/
const intervalIDs = []
const startL1Cron = (path) => {
// clean all intervals
while(intervalIDs.length > 0) {
clearInterval(intervalIDs.pop())
}
fs.readFile(path, (err, data) => {
if(err) {
console.error("Could not read L1 nodes");
return;
}
//start the new ones as in the file
data = yaml.load(data);
console.log(`UPDATE_L1_LIST ${Date.now()}`)
if(!data) {
console.log("Could not read YAML")
return;
}
Object.keys(data.ledgers).forEach( (key) => {
const ledger = data.ledgers[key];
const fnLogger = msg => console.log(`POLLING ${key} ${Date.now()} ${msg}`)
if(ledger.interval > 0) {
intervalIDs.push(setInterval(() => {
axios
.post(`http://127.0.0.1:${HTTP_PORT}/api/${ledger.contract}`)
.then( res => {
fnLogger(JSON.stringify(res.data))
})
.catch( err => {
fnLogger(JSON.stringify(err))
})
}, ledger.interval * 1000))
}
})
});
}
const startL1Watcher = () => {
const file = path.join(FILES_DIR, L1NODES);
//startL1Cron(file);
chokidar.watch(file).on('all', (_, path) => {
startL1Cron(path);
});
}
/*
* Post announce message to Issuer
*/
const announce = (identity) => {
const data = {
"add-identity": ANNOUNCE_URL,
"post": {
"data": {
"identity": identity
}
}
}
axios
.post(`http://127.0.0.1:${HTTP_PORT}/api/zenswarm-oracle-announce`, {"data": data})
.then( res => {
console.log(JSON.stringify(res.data))
//startL1Watcher();
dispatchSubscriptions();
})
.catch( e => {
console.error("Error in announce contract");
process.exit(-1);
})
};
/*
* Create VMLet identity
*/
const saveVMLetStatus = async () => {
// generate private keys
const generatePrivateKeysScript = fs.readFileSync(path.join(PRIVATE_ZENCODE_DIR,
"consensus-generate-all-private-keys.zen"), 'utf8')
let keyring = { "ed25519_keypair": { "private_key":"5Jgk9ARKbPXRwPCuVHv94M4q9iKpF67Apk6hP3rRLtby", "public_key": "Mx2D1WbAdREJphfuAcRCge54zMndKfmozynzRZYP5aw"}};
const keys = await zen(generatePrivateKeysScript, null, null);
if(!keys) {
process.exit(-1)
}
Object.assign(keyring, JSON.parse(keys.result))
fs.writeFileSync(
path.join(ZENCODE_DIR, "zenswarm-oracle-generate-all-public-keys.keys"),
keys.result)
fs.writeFileSync(
path.join(ZENCODE_DIR, "keyring.json"),
JSON.stringify(keyring))
// generate relative public keys
axios
.get(`http://127.0.0.1:${HTTP_PORT}/api/zenswarm-oracle-generate-all-public-keys`)
.then( res => {
// put all togheter in the identity
const identity = {
"uid":`${HOST}:${HTTP_PORT}`,
"ip":HOST,
"baseUrl":`http://${HOST}`,
"port_http":`${HTTP_PORT}`,
"port_https":`${HTTPS_PORT}`,
"version":"2",
"announceAPI":"/api/zenswarm-oracle-announce",
"timestampAPI":"/api/zenswarm-oracle-get-timestamp.zen",
"updateAPI":"/api/zenswarm-oracle-update",
"http-postAPI": "/api/zenswarm-oracle-http-post",
"pingAPI" : "/api/zenswarm-oracle-ping.zen",
"oracle-key-issuance": "/api/zenswarm-oracle-key-issuance.chain",
"tracker":"https://apiroom.net/",
"ethereum-notarizationAPI":"/api/ethereum-to-ethereum-notarization.chain",
"sawroom-notarizationAPI":"/api/sawroom-to-ethereum-notarization.chain",
"get-identityAPI":"/api/zenswarm-oracle-get-identity",
"type": "restroom-mw",
"region": REGION,
"country": `${COUNTRY}`
}
Object.assign(identity, res.data)
fs.writeFileSync(
path.join(ZENCODE_DIR, "identity.json"),
JSON.stringify({"identity": identity}))
announce(identity)
})
.catch(e => {
console.error("Error in generate public key contract");
console.error(e)
process.exit(-1);
})
}
/*
* Utils: generate a random number between min and max
*/
function between(min, max) {
return Math.floor(
Math.random() * (max - min) + min
)
}
function startHttp(initial_port, callback) {
let port = initial_port;
const httpServer = http.createServer(app);
let retry = 1000;
if(port <= 0) port = between(MIN_PORT, MAX_PORT);
console.log(`CHOSEN_HTTP_PORT ${port}`)
httpServer.listen(port, function() {
console.log(`LISTENING ${httpServer.address().port}`);
callback();
}).on('error', function(err) {
console.log(`ERROR ${err.code}`)
if(err.code == 'EADDRINUSE') {
port = between(MIN_PORT, MAX_PORT);
console.log(`CHOSEN_HTTP_PORT ${port}`)
if(retry-- > 0)
httpServer.listen(port);
else
throw new Error("Could not find a free port")
} else {
console.log(err);
process.exit(-1);
}
});
return port
}
let HTTP_PORT = parseInt(process.env.HTTP_PORT, 10) || 0;
let HTTPS_PORT = parseInt(process.env.HTTPS_PORT, 10) || 0;
const HOST = process.env.HOST || "0.0.0.0";
const COUNTRY = process.env.COUNTRY || "NONE";
const ZENCODE_DIR = process.env.ZENCODE_DIR;
const PRIVATE_ZENCODE_DIR = process.env.PRIVATE_ZENCODE_DIR;
const OPENAPI = JSON.parse(process.env.OPENAPI || true);
const L1NODES = process.env.L1NODES || "L1.yaml";
const FILES_DIR = process.env.FILES_DIR || "contracts";
const REGION = process.env.REGION || "NONE";
const SUBSCRIPTIONS = process.env.SUBSCRIPTIONS || "";
const ANNOUNCE_URL = process.env.ANNOUNCE_URL || "https://apiroom.net/api/zenswarm/zenswarm-issuer-add-identity.chain";
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(morgan('combined', { stream: L.stream.write }))
app.set("json spaces", 2);
// Used by the dashboard
app.use(function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
next();
});
app.use(db.default);
app.use(fabric.default);
app.use(rrhttp.default);
app.use(rrredis.default);
app.use(sawroom.default);
app.use(ethereum.default);
app.use(timestamp.default);
app.use(files.default);
if (OPENAPI) {
app.use("/docs", ui.default({ path: ZENCODE_DIR }));
}
app.use("/api/*", zencode.default);
app.get('/logs', async (req, res) => {
const logs = await readLastLines.read('./access.log', 200)
res.send(logs)
})
app.use(function(err, req, res, next) {
L.error(`${req.method} - ${err.message} - ${req.originalUrl} - ${req.ip}`);
next(err)
})
if(!fs.existsSync(ZENCODE_DIR)) {
fs.mkdirSync(ZENCODE_DIR, { recursive: true });
}
const contracts = fs.readdirSync(ZENCODE_DIR);
if (contracts.length > 0) {
const httpStarted = async () => {
process.env.HTTPS_PORT = HTTPS_PORT;
await saveVMLetStatus();
console.log(`🚻 Restroom started on http://${chalk.bold.blue(HOST)}:${HTTP_PORT} and http://${chalk.bold.blue(HOST)}:${HTTPS_PORT}`);
console.log(`📁 the ZENCODE directory is: ${chalk.magenta.underline(ZENCODE_DIR)} \n`);
if (OPENAPI) {
console.log(`To see the OpenApi interface head your browser to: ${chalk.bold.blue.underline('http://' + HOST + ':' + HTTP_PORT + '/docs')}`);
console.log(`To disable OpenApi, run ${chalk.bold('OPENAPI=0 yarn start')}`);
} else {
console.log(`⚠️ The OpenApi is not enabled! NO UI IS SERVED. To enable it run run ${chalk.bold('OPENAPI=1 yarn start')}`);
}
console.log("\nExposing");
readdirp(ZENCODE_DIR, { fileFilter: '*.zen|*.yaml|*.yml' }).on('data', (c) => {
const endpoint = `/api/${c.path.replace('.zen', '')}`
console.log(`\t${chalk.bold.green(endpoint)}`);
});
}
HTTP_PORT = startHttp(HTTP_PORT, () => {
process.env.HTTP_PORT = HTTP_PORT;
HTTPS_PORT = startHttp(HTTPS_PORT, httpStarted);
});
} else {
console.log(`🚨 The ${chalk.magenta.underline(ZENCODE_DIR)} folder is empty, please add some ZENCODE smart contract before running Restroom`);
}
/*
* Subscribe to ETH node
*/
function subscribeEth(blockchain) {
try {
const ws = new WebSocket(blockchain.ws);
ws.onopen = function() {
const id = Math.floor(Math.random() * 65536);
let subscriptionId = null;
ws.send(JSON.stringify({
id,
jsonrpc:"2.0",
method: "eth_subscribe",
params: ["newHeads"]
}));
const processMsg = function(event) {
let msg = JSON.parse(event.data)
if(msg.method == "eth_subscription"
&& msg.params && msg.params.subscription == subscriptionId) {
const block = msg.params.result;
msg['endpoint'] = blockchain.http;
Object.assign(msg, {blockchain})
L.info("ETH_NEW_HEAD " + block.hash);
axios.post(`http://127.0.0.1:${HTTP_PORT}/api/ethereum-to-ethereum-notarization.chain`,
{data: msg}).then(function(data) {
L.info(`ETH_NOTARIZE ${data.data.txid}`);
}).catch(function(e) {
L.warn(`ETH_NOTARIZE_ERROR ${e}`)
});
}
}
ws.onmessage = function(e) {
const msg = JSON.parse(e.data);
if(msg.result && msg.id == id) {
subscriptionId = msg.result
// from now on messages will be processed as blocks
ws.onmessage = processMsg;
}
}
ws.onclose = function() {
Log.warn("ETH_CLOSE")
}
}
} catch(e) {
L.error(`ETH_WS_ERROR ${e}`);
process.exit(-1);
}
}
function subscribeSaw(blockchain) {
try {
const ws = new WebSocket(blockchain.ws);
ws.onopen = function() {
ws.send(JSON.stringify({
action: "subscribe"
}));
ws.onmessage = function(event) {
try {
let msg = JSON.parse(event.data)
const block = msg.block_id;
msg['endpoint'] = blockchain.http;
Object.assign(msg, {blockchain})
L.info("SAW_NEW_HEAD " + block);
axios.post(`http://127.0.0.1:${HTTP_PORT}/api/sawroom-to-ethereum-notarization.chain`,
{data: msg}).then(function(data) {
L.info(`SAW_NOTARIZE ${data.data.txid}`);
}).catch(function(e) {
L.warn(`SAW_NOTARIZE_ERROR ${e}`)
});
} catch(e) {
L.warn(`SAW_WS_ERROR: ${e}`)
}
}
ws.onclose = function() {
L.warn("SAW_CLOSE")
}
}
} catch(e) {
L.error(`SAW_WS_ERROR ${e}`);
process.exit(-1);
}
}
function subscribeIota(blockchain) {
try {
const client = mqtt.connect(blockchain.mqtt);
client.subscribe('milestones/latest');
client.on('message', function(topic, message) {
try {
let msg = JSON.parse(message.toString('utf-8'));
msg[ 'index' ] = msg[ 'index' ].toString();
msg[ 'timestamp' ] = msg[ 'timestamp' ].toString();
const block_index = msg.index;
msg[ 'endpoint' ] = blockchain.http;
Object.assign(msg, {blockchain});
L.info("IOTA_NEW_HEAD " + block_index);
axios.get(`${blockchain.http}api/v1/milestones/${block_index}`)
.then(function(res) {
msg[ 'messageId' ] = res.data.data.messageId;
L.info(`IOTA_ID: ${res.data.data.messageId}`);
axios.get(`${blockchain.http}api/v1/messages/${msg.messageId}`)
.then(function(res) {
Object.assign(msg, {parentMessageIds: res.data.data.parentMessageIds});
axios.post(`http://127.0.0.1:${HTTP_PORT}/api/iota-to-ethereum-notarization.chain`,
{data: msg}).then(function(data) {
L.info(`IOTA_NOTARIZE ${data.data.txid}`);
}).catch(function(e) {
L.warn(`IOTA_NOTARIZE_ERROR ${e}`)
});
})
}).catch(function(e) {
L.warn(`IOTA_MESSAGE_ID_ERROR ${e}`);
});
} catch(e) {
L.warn(`IOTA_MESSAGE_ERROR: ${e}`);
}
});
} catch(e) {
L.error(`IOTA_MQTT_ERROR ${e}`);
process.exit(-1);
}
}
const subscribeFn = {
"ethereum": subscribeEth,
"sawtooth": subscribeSaw,
"iota": subscribeIota
}
function dispatchSubscriptions() {
let subscriptions = {}
if(SUBSCRIPTIONS != '') {
SUBSCRIPTIONS.split(" ").forEach( (v) => {
try {
const blockchain = blockchainDB[v]
if(!blockchain) {
console.log("UNKNOWN_BLOCKCHAIN " + v);
return
}
const fn = subscribeFn[blockchain['type']];
if(!fn) {
console.log("UNKNOWN_SUBSCRIPTION " + v);
return
}
subscriptions[v] = blockchain;
fn({name: v, ...blockchain});
} catch(e) {
console.warn(e)
}
});
}
fs.writeFileSync(
path.join(ZENCODE_DIR, "blockchain-subscriptions.json"),
JSON.stringify({subscriptions}));
}