-
Notifications
You must be signed in to change notification settings - Fork 0
/
count.mjs
349 lines (291 loc) · 10.3 KB
/
count.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
import * as fs from 'fs';
import blake2b from 'blake2b';
import WebSocket from 'ws';
import { bech32 } from 'bech32';
import {
NATIVE_SCRIPTS,
OGMIOS_HOST,
REFERENCE_SCRIPTS,
SINCE,
UNTIL,
VALIDATORS,
} from './config.mjs';
const KIND = {
AIKEN: "aiken",
PLUTARCH: "plutarch",
OPSHIN: "opshin",
PLUTUSTX: "plutus-tx",
PLUTS: "plu-ts",
HELIOS: "helios",
MARLOWE: "marlowe",
NATIVE: "native",
};
const LOGO = {
[KIND.AIKEN]: "https://raw.githubusercontent.com/aiken-lang/site/main/public/cardano-smart-contract-frameworks/aiken.png",
[KIND.PLUTARCH]: "https://raw.githubusercontent.com/aiken-lang/site/main/public/cardano-smart-contract-frameworks/plutarch.png",
[KIND.OPSHIN]: "https://raw.githubusercontent.com/aiken-lang/site/main/public/cardano-smart-contract-frameworks/opshin.png",
[KIND.PLUTUSTX]: "https://raw.githubusercontent.com/aiken-lang/site/main/public/cardano-smart-contract-frameworks/plutus-tx.png",
[KIND.PLUTS]: "https://raw.githubusercontent.com/aiken-lang/site/main/public/cardano-smart-contract-frameworks/plu-ts.png",
[KIND.HELIOS]: "https://raw.githubusercontent.com/aiken-lang/site/main/public/cardano-smart-contract-frameworks/helios.png",
[KIND.MARLOWE]: "https://raw.githubusercontent.com/aiken-lang/site/main/public/cardano-smart-contract-frameworks/marlowe.png",
};
const client = new WebSocket(OGMIOS_HOST);
// Strawman JSON-RPC 2.0 client
client.rpc = function rpc(method, params = {}) {
this.send(JSON.stringify({
jsonrpc: '2.0',
method,
params,
}));
};
client.on('open', () => {
client.rpc('findIntersection', { points: [SINCE] });
});
let unknowns = new Set();
let interactions = {
[KIND.AIKEN]: 0,
[KIND.PLUTARCH]: 0,
[KIND.OPSHIN]: 0,
[KIND.HELIOS]: 0,
[KIND.MARLOWE]: 0,
[KIND.PLUTUSTX]: 0,
[KIND.PLUTS]: 0,
[KIND.NATIVE]: 0,
total: 0,
};
let transactions = {
...interactions
};
let timeline = {
[KIND.AIKEN]: [],
[KIND.PLUTARCH]: [],
[KIND.OPSHIN]: [],
[KIND.HELIOS]: [],
[KIND.MARLOWE]: [],
[KIND.PLUTUSTX]: [],
[KIND.PLUTS]: [],
[KIND.NATIVE]: [],
epochs: [],
};
let previousInteractions = { total: 0 };
let previousTransactions = { total: 0 };
let previousEpoch = null;
client.once('message', (data) => {
const tip = (UNTIL || JSON.parse(data).result.tip).slot;
client.on('message', (data) => {
const result = JSON.parse(data).result;
if (result.direction === 'forward') {
if (previousEpoch === null) {
previousEpoch = getEpoch(result.block.slot);
}
result.block.transactions.forEach(tx => {
// Show some progress at regular intervals
if (transactions.total % 5000 === 0 && transactions.total > previousTransactions.total) {
notify(result.block);
}
const hasCollateral = (tx.collaterals || []).length > 0;
if (hasCollateral || tx.outputs.some(isScriptAddress)) {
let sources = { ...(tx.mint || {}), ...(tx.scripts || {}), ...(tx.withdrawals || {}) };
const before = { ...interactions };
countWitnesses(sources, interactions);
countUsage(tx.outputs, interactions);
countReferences(tx.references, interactions);
markTransaction(before, interactions, transactions);
}
});
if (result.block.slot >= tip) {
previousEpoch = Number.NEGATIVE_INFINITY;
notify(result.block);
fs.writeFileSync(`./unknowns-${SINCE.slot}:${tip}.json`, JSON.stringify(Array.from(unknowns), null, 2));
console.log(`${unknowns.size} unknown script hashes?`);
console.log('\n');
const epochs = (new Array(timeline.epochs.length)).fill(0).map((_, ix) => ix + getEpoch(SINCE.slot));
console.log(['framework', 'logo'].concat(epochs).join(', '));
for (let lang in timeline) {
if (lang === 'epochs' || lang === 'native') { continue; }
console.log([lang, LOGO[lang]].concat(timeline[lang]).join(', '));
}
process.exit(0);
}
}
client.rpc('nextBlock');
});
function notify({ slot }) {
delete transactions['_'];
const currentEpoch = getEpoch(slot)
if (currentEpoch > previousEpoch) {
previousEpoch = currentEpoch;
for (let lang in transactions) {
if (lang === 'total') { continue; }
timeline[lang].push(transactions[lang] - (previousTransactions[lang] ?? 0));
}
timeline.epochs.push(currentEpoch);
previousTransactions = { ...transactions };
previousInteractions = { ...interactions };
}
transactions['_'] = transactions.total - countFilter(transactions, k => k != 'total');
const total = transactions.total - transactions[KIND.NATIVE];
const p = (source) => Math.round(10000 * source / total) / 100;
const display = (kind, title) => {
return {
score: transactions[kind],
text: [
title.padEnd(10, ' '),
String(transactions[kind]).padStart(12, ' '),
`(${p(transactions[kind]).toFixed(2).padStart(5, ' ')}%)`,
].join(' '),
};
};
const summary = [
display(KIND.PLUTUSTX, 'Plutus-tx:'),
display(KIND.PLUTARCH, 'Plutarch:'),
display(KIND.AIKEN, 'Aiken:'),
display(KIND.HELIOS, 'Helios:'),
display(KIND.MARLOWE, 'Marlowe:'),
display(KIND.OPSHIN, 'OpShin:'),
display(KIND.PLUTS, 'Plu-ts:'),
] .sort((a, b) => b.score - a.score)
.concat([
display('_', 'Unsure:'),
{ text: ''.padStart(32, '=') },
{ text: `Total:${String(total).padStart(17, ' ')}` },
])
.map(({ text }) => text)
.join('\n ');
const completion = Math.floor(16 * (slot - SINCE.slot) / (tip - SINCE.slot));
const progress = '>'.padStart(completion + 1, '=').padEnd(17, ' ');
console.log(`(at ${slot}) [${progress}]\n${''.padStart(22, ' ')}TRANSACTIONS\n ${''.padStart(32, '=')}\n ${summary}\n\n`);
}
// Fill in the initial queue to leverage pipelining.
for (let i = 0; i < 100; i += 1) { client.rpc('nextBlock'); }
});
function isScriptAddress({ address }) {
return withShelleyAddress(address, false, (bytes) => {
const addrType = bytes.slice(0, 1).toString('hex')[0];
return addrType === '1' || addrType === '2' || addrType === '3' || addrType === '5' || addrType === '7' || addrType === '15';
});
}
function isNativeScript({ address }) {
return withShelleyAddress(address, false, (bytes) => {
const payment_part = bytes.slice(1, 29).toString('hex');
const delegation_part = bytes.slice(29).toString('hex');
return NATIVE_SCRIPTS.has(payment_part) || NATIVE_SCRIPTS.has(delegation_part);
});
}
function countWitnesses(scripts = {}, n) {
return Object.keys(scripts).reduce((acc, k) => {
const h = k.startsWith("stake")
? Buffer.from(bech32.fromWords(bech32.decode(k, 999).words)).toString('hex').substr(2)
: k;
const found = VALIDATORS.get(h);
if (found) {
acc[found] += 1;
} else if (NATIVE_SCRIPTS.has(h)) {
acc[KIND.NATIVE] += 1;
} else {
unknowns.add(h);
}
acc.total += 1;
return acc;
}, n);
}
function countReferences(references = [], n) {
return references.reduce((acc, { transaction, index }) => {
const ref = REFERENCE_SCRIPTS.get(`${transaction.id}#${index}`);
const found = VALIDATORS.get(ref);
if (found) {
acc[found] += 1;
} else if (ref && NATIVE_SCRIPTS.has(ref)) {
acc[KIND.NATIVE] += 1;
} else {
unknowns.add(ref);
}
acc.total += 1;
return acc;
}, n);
}
function countUsage(outs, n) {
return outs.reduce((acc, { address, script }) => {
withShelleyAddress(address, false, (bytes) => {
const addrType = bytes.slice(0, 1).toString('hex')[0];
const payment_part = bytes.slice(1, 29).toString('hex');
const payment_part_kind = VALIDATORS.get(payment_part);
if (payment_part_kind) {
acc[payment_part_kind] += 1;
acc.total += 1;
} else if (NATIVE_SCRIPTS.has(payment_part)) {
acc.total += 1;
acc[KIND.NATIVE] += 1;
} else if (addrType === '1' || addrType === '3' || addrType === '5' || addrType === '7' || addrType === '15') {
acc.total += 1;
unknowns.add(payment_part);
}
const delegation_part = bytes.slice(29).toString('hex');
const delegation_part_kind = VALIDATORS.get(delegation_part);
if (delegation_part_kind) {
acc[delegation_part_kind] += 1;
acc.total += 1;
} else if (NATIVE_SCRIPTS.has(delegation_part)) {
acc.total += 1;
acc[KIND.NATIVE] += 1;
} else if (addrType === '2' || addrType === '3') {
acc.total += 1;
unknowns.add(delegation_part);
}
const inline_script = script ? blake2b(28).update(preimage(script)).digest('hex') : null;
const inline_script_kind = VALIDATORS.get(inline_script);
if (inline_script_kind) {
acc[inline_script_kind] += 1;
acc.total += 1;
} else if (NATIVE_SCRIPTS.has(inline_script)) {
acc.total += 1;
acc[KIND.NATIVE] += 1;
} else if (inline_script) {
acc.total += 1;
unknowns.add(inline_script);
}
});
return acc;
}, n);
}
function markTransaction(before, after, n) {
Object.keys(KIND).forEach(k => {
const delta = after[KIND[k]] - before[KIND[k]];
if (delta > 0) {
n.total += 1;
n[KIND[k]] += 1;
}
});
}
function preimage({ language, cbor }) {
switch (language) {
case "plutus:v1":
return Buffer.concat([Buffer.from([1]), Buffer.from(cbor, 'hex')]);
case "plutus:v2":
return Buffer.concat([Buffer.from([2]), Buffer.from(cbor, 'hex')]);
case "plutus:v3":
return Buffer.concat([Buffer.from([3]), Buffer.from(cbor, 'hex')]);
default:
return Buffer.from("");
}
}
// Small helper to deal with intermittent Byron addresses that aren't
// bech32-encoded and can't take part in smart-contract transactions anyway.
function withShelleyAddress(address, empty, cb) {
if (!address.startsWith('addr')) { return empty; }
return cb(Buffer.from(bech32.fromWords(bech32.decode(address, 999).words)));
}
function countFilter(obj, predicate) {
return Object.keys(obj).reduce((acc, k) => {
if (!predicate(k)) {
return acc;
}
return acc + obj[k];
}, 0);
}
function getEpoch(slot) {
const FIRST_SHELLEY_EPOCH = 208;
const FIRST_SHELLEY_SLOT = 4492800;
const EPOCH_LENGTH = 432000;
return FIRST_SHELLEY_EPOCH + Math.floor((slot - FIRST_SHELLEY_SLOT) / EPOCH_LENGTH);
}