-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.mjs
144 lines (111 loc) · 3.56 KB
/
fetch.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
import WebSocket from 'ws';
import fetch from 'node-fetch';
import { writeFile } from 'node:fs/promises';
import { spawn } from 'node:child_process';
const HOST = '127.0.0.1:1337';
const HTTP_URL = `http://${HOST}`;
const WS_URL = `ws://${HOST}`;
let client;
let lastKnownEpoch;
let lastKnownTip;
let currentEra = 'byron';
const node = spawn('run-cardano-node', [ 'mainnet' ]);
process.on('exit', (code) => {
console.log(`Last known epoch: ${lastKnownEpoch}`);
console.log(`Last known tip: ${JSON.stringify(lastKnownTip)}`);
console.log(`Current era: ${currentEra}`);
if (client) { client.close(); }
node.exit();
});
node.stdout.on('data', (data) => console.error(data.toString()));
node.stderr.on('data', (data) => console.error(data.toString()));
tick();
function tick(delay = 0) {
setTimeout(async () => {
const health = await fetch(`${HTTP_URL}/health`).then(x => x.json());
if (health.currentEpoch == null || health.connectionStatus !== 'connected') {
console.log('Awaiting for node to start...');
return tick(1000);
}
let ready;
if (!client) {
client = new WebSocket(WS_URL);
client.on('close', (e) => {
console.log(`Connection closed with code: ${e}`);
process.exit();
});
client.on('error', (e) => {
console.log(e);
process.exit(1);
});
ready = new Promise(resolve => {
client.once('open', resolve);
});
} else {
ready = Promise.resolve();
}
await ready;
if (health.currentEpoch === lastKnownEpoch) { return tick(); }
lastKnownEpoch = health.currentEpoch;
lastKnownTip = health.lastKnownTip;
currentEra = health.currentEra;
if (currentEra === 'byron') {
console.log(`Current era at epoch ${lastKnownEpoch} is still byron...`);
tick();
} else {
console.log(`Acquire state for epoch ${lastKnownEpoch}`);
client.once('message', onAcquired);
rpc('acquireLedgerState', { point: lastKnownTip });
}
}, delay);
}
function rpc(method, params) {
client.send(JSON.stringify({
jsonrpc: '2.0',
method,
params,
}));
}
function onAcquired(msg) {
const { result, error } = JSON.parse(msg);
if (error || result?.acquired !== 'ledgerState') {
console.log(`Error while acquiring ledger state: ${JSON.stringify(error)}`);
process.exit(1);
}
console.log(`Acquired ledger state at ${JSON.stringify(result)}`);
let queries = [
() => rpc('queryLedgerState/liveStakeDistribution'),
() => rpc('queryLedgerState/rewardsProvenance'),
];
let tasks = [];
let count = queries.length;
client.on('message', async function collect(msg) {
const { result, method, error } = JSON.parse(msg)
if (error) {
console.log(`Error while fetching ${method}: ${JSON.stringify(error)}`);
process.exit(1);
}
tasks.push(writeFile(`../raw/${method}/${lastKnownEpoch}.json`, JSON.stringify(result, null, 2)));
count -= 1;
if (count === 0) {
client.off('message', collect);
try {
await Promise.all(tasks.concat(new Promise((resolve, reject) => {
client.once('message', (msg) => {
const { result, error } = JSON.parse(msg);
if (error || result?.released !== 'ledgerState') {
return reject(`Error while releasing ledger state: ${JSON.stringify(error)}`);
}
resolve();
});
rpc('releaseLedgerState');
})));
} catch (e) {
console.log(e);
process.exit(1);
}
tick();
}
});
queries.forEach(send => send());
}