-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtries-main.js
355 lines (292 loc) · 12.1 KB
/
tries-main.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
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
const utils = require('ethereumjs-util');
const fs = require("fs");
const readline = require('readline');
const blocks = require('./blocks-module');
const Statistics = require('./blocks-module').Statistics;
const Account = require('ethereumjs-account').default;
const Transaction = require('ethereumjs-tx').Transaction;
const async = require('async');
// running less blocks in parallel produces results sooner
// because a lot of parallel actions prolong single executions
const BLOCKS_IN_PARALLEL=100;
/**
* Read data about blocks and trigger Trie analysis
* @param file
* @param cb callback
* @param onLine invoked when file is processed
*/
function readBlocksData(file, cb, onLine) {
const stream = fs.createReadStream(file);
const rl = readline.createInterface({
input: stream,
crlfDelay: Infinity
});
rl.on('line', line => {
const items = line.split(",");
const blockNumber = items[0];
const blockHashStr = items[1];
const stateRootStr = items[2];
const transactionTrieStr = items[3];
const receiptTrieStr = items[4];
const fn = (taskCB) => cb(blockNumber, blockHashStr, stateRootStr, transactionTrieStr, receiptTrieStr, taskCB);
onLine(fn);
});
rl.on('close', () => onLine(null)); // null signals end
}
/**
* Read blocks from CSV files
* @param path path
* @param cb callback
* @param onDone called when all CSV files are processed
*/
function readBlocksCSVFiles(path, cb, onDone) {
fs.readdir(path, (err, files) => {
if (err) { console.error("Could not list the directory.", err); return; }
let tasks = [];
files.forEach((file, index) => {
if (file.startsWith("blocks") && file.endsWith(".csv")) {
// execute partly in sequence to prevent out-of-memory
readBlocksData(path + file, cb, (task)=>{
// add a task to the queue or run execution after last task
if (task) tasks.push(task); else async.parallelLimit(tasks, BLOCKS_IN_PARALLEL, onDone);
});
}
});
});
}
/**
* This callback analyses Account State Trie
* @type {analyseAccountsCB}
*/
analyseAccountsCB = function(stream, streamStorage, blockNumber, blockHashStr, stateRootStr, transactionTrieStr, receiptTrieStr, onDone) {
const fileName = 'csv_acc/accounts_storage_' + blockNumber + '.csv';
let streamAcc;
let stateRoot = utils.toBuffer(stateRootStr);
let totalContractAccounts = 0;
let totalContractValues = 0;
let stats = new Statistics();
// accumulate values globally per block
let totalStorage = 0;
let statsStorage = new Statistics();
console.time('Blocks-Account-' + blockNumber);
blocks.iterateSecureTrie(stateRoot, (key, value, node, depth) => {
stats.addNode(key, node, value, depth);
stats.addValue(value, depth);
// we have value when the leaf has bean reached
if (value) {
let acc = new Account(value);
if (acc.isContract()) {
totalContractAccounts++;
const accountNumber = utils.bufferToHex(key);
const stateRootStr = utils.bufferToHex(acc.stateRoot);
// This CSV file maps account addresses to state roots.
if (!streamAcc) streamAcc = fs.createWriteStream(fileName);
addCsvLineStorageRoot(streamAcc, blockNumber, accountNumber, stateRootStr);
// accumulate data for all contracts data
// we do this in a separate script to save time here
// blocks.iterateSecureTrie(acc.stateRoot, (keyC, valueC, nodeC, depthC) => {
// statsStorage.addNode(keyC, nodeC); // accumulate node info
// if (valueC) {
// totalStorage++;
// totalContractValues++;
// statsStorage.append(depthC);
// }
// });
}
// check progress for every new account
stats.printProgress(1000000);
}
// node is null for end of iteration
if (!node) {
if (stats.countValues > 0) {
console.log(`Accounts: ${blockNumber} -> ${stats.countValues}, ${totalContractAccounts}, ${totalContractValues}`);
console.timeEnd('Blocks-Account-' + blockNumber);
// TODO - these callback are a bit crazy, but I do not have time at the moment to study something better such as premises
let tasks = 1;
const onDoneTasks = ()=>{if (--tasks === 0) onDone()};
if (totalStorage > 0) {
tasks++;
// statistics for storage for all accounts
const meanC = stats.mean();
const devC = stats.dev(meanC);
addCsvLine(streamStorage, blockNumber, totalStorage,
statsStorage.totalNodes, meanC, devC,
statsStorage.minValue, statsStorage.maxValue, stats.valueSize, statsStorage.nodeSize, onDoneTasks);
}
// statistics for accounts
const mean = stats.mean();
const dev = stats.dev(mean);
addCsvLineAccount(stream, blockNumber,
stats.countValues, totalContractAccounts,
stats.totalNodes, mean, dev,
stats.minValue, stats.maxValue, stats.valueSize, stats.nodeSize, onDoneTasks);
} else {
onDone();
}
if (streamAcc) streamAcc.end();
}
// console.log(`nonce: ${new BN(acc.nonce)}`);
// console.log(`balance: ${new BN(acc.balance)}`);
// console.log(`storageRoot: ${utils.bufferToHex(acc.stateRoot)}`);
// console.log(`codeHash: ${utils.bufferToHex(acc.codeHash)}`);
});
};
/**
* This callback analyses Transaction Trie.
* @type {analyseAccountsCB}
*/
analyseTransactionCB = function(stream, blockNumber, blockHashStr, stateRootStr, transactionTrieStr, receiptTrieStr, onDone) {
let trieRoot = utils.toBuffer(transactionTrieStr);
let stats = new Statistics();
console.time('Blocks-Transactions-' + blockNumber);
// console.log(transactionTrieStr + "->" + trieRoot)
blocks.iterateTrie(trieRoot, (key, value, node, depth) => {
stats.addNode(key, node, value, depth);
stats.addValue(value, depth);
if (value) {
const trans = new Transaction(value);
console.log("Tx:" + trans);
}
if (node) {
if (stats.countValues > 0) {
console.log(`Transactions: ${blockNumber} -> ${stats.countValues}`);
console.timeEnd('Blocks-Transactions-' + blockNumber);
const mean = stats.mean();
const dev = stats.dev(mean);
addCsvLine(stream, blockNumber, stats.countValues,
stats.totalNodes, mean, dev, stats.minValue, stats.maxValue, stats.valueSize, stats.nodeSize, onDone);
} else {
onDone();
}
}
});
};
/**
* This callback analyses Receipt Trie
* @type {analyseAccountsCB}
*/
analyseReceiptCB = function(stream, blockNumber, blockHashStr, stateRootStr, transactionTrieStr, receiptTrieStr, onDone) {
let trieRoot = utils.toBuffer(receiptTrieStr);
let stats = new Statistics();
console.time('Blocks-Receipts-' + blockNumber);
// console.log(transactionTrieStr + "->" + trieRoot)
blocks.iterateTrie(trieRoot, (key, value, node, depth) => {
stats.addNode(key, node, value, depth);
stats.addValue(value, depth);
if (value) {
// const trans = new Transaction(value);
console.log("Tx Rec:" + value);
}
if (node) {
if (stats.countValues > 0) {
console.log(`Transactions: ${blockNumber} -> ${stats.countValues}`);
const mean = stats.mean();
const dev = stats.dev(mean);
addCsvLine(stream, blockNumber, stats.countValues,
stats.totalNodes, mean, dev, stats.minValue, stats.maxValue, stats.valueSize, stats.nodeSize, onDone);
} else {
onDone();
}
}
});
};
/**
* Dump one line in CSV
* @param stream
* @param blockNumber
* @param counts
* @param numNodes
* @param avrgDepth
* @param sizeNodes
* @param devDepth
*/
function addCsvLine(stream, blockNumber, counts, numNodes, avrgDepth, devDepth, min, max, valueSize, sizeNodes, onDone) {
const newLine = [];
const sizeNodesMB = sizeNodes / 1024 / 1024;
// numNodes = number of keys in the DB, the key size is 32 bytes
const keySizesMB = numNodes*32 / 1024 / 1024;
const valueSizeMB = valueSize / 1024 / 1024;
newLine.push(blockNumber);
newLine.push(counts);
newLine.push(numNodes);
newLine.push(avrgDepth);
newLine.push(devDepth);
newLine.push(min);
newLine.push(max);
newLine.push(valueSizeMB);
newLine.push(sizeNodesMB);
newLine.push(keySizesMB);
newLine.push(keySizesMB + sizeNodesMB); // total size = size of 32 byte keys PLUS size of nodes
stream.write(newLine.join(',')+ '\n', onDone);
}
function addCsvLineAccount(stream, blockNumber, allAccounts, contractAccounts, numNodes, avrgDepth, devDepth, min, max, valueSize, sizeNodes, onDone) {
const newLine = [];
const sizeNodesMB = sizeNodes / 1024 / 1024;
// numNodes = number of keys in the DB, the key size is 32 bytes
const keySizesMB = numNodes*32 / 1024 / 1024;
const valueSizeMB = valueSize / 1024 / 1024;
newLine.push(blockNumber);
newLine.push(allAccounts);
newLine.push(contractAccounts);
newLine.push(numNodes);
newLine.push(avrgDepth);
newLine.push(devDepth);
newLine.push(min);
newLine.push(max);
newLine.push(valueSizeMB);
newLine.push(sizeNodesMB);
newLine.push(keySizesMB);
newLine.push(keySizesMB + sizeNodesMB); // total size = size of 32 byte keys PLUS size of nodes
stream.write(newLine.join(',')+ '\n', onDone);
}
function addCsvLineStorageRoot(stream, blockNumber, accountNumber, storageRoot, onDone) {
const newLine = [];
newLine.push(blockNumber);
newLine.push(accountNumber);
newLine.push(storageRoot);
stream.write(newLine.join(',')+ '\n', onDone);
}
/**
* Wrap analysis into callbacks
* @param blocksDir Dir with blocks_*.csv files
* @param stream stream with CSV to dump results
* @param cb callback
*/
function processAnalysis(blocksDir, stream, cb) {
readBlocksCSVFiles(blocksDir, (blockNumber, blockHashStr, stateRootStr, transactionTrieStr, receiptTrieStr, onDone) => {
cb(stream, blockNumber, blockHashStr, stateRootStr, transactionTrieStr, receiptTrieStr, onDone);
}, () => {
stream.end();
});
}
function processAnalysisAccounts(blocksDir, path, cb) {
const stream = fs.createWriteStream(path + 'accounts.csv')
// this accumulates storage data per block
const streamStorage = fs.createWriteStream(path + "blocks_storage.csv");
console.time("Analyse-accounts");
readBlocksCSVFiles(blocksDir, (blockNumber, blockHashStr, stateRootStr, transactionTrieStr, receiptTrieStr, onDone) => {
cb(stream, streamStorage, blockNumber, blockHashStr, stateRootStr, transactionTrieStr, receiptTrieStr, onDone);
}, () => {
stream.end();
streamStorage.end();
console.timeEnd("Analyse-accounts");
});
}
/**
* Main program - read blocks from pre-generated CSV files and
* generate statistics about the tries.
*/
const args = process.argv.slice(2);
const dbPath = args[0];
// const startBlock = parseInt(args[1]);
// const endBlock = parseInt(args[2]);
// const stepBlock = parseInt(args[3]);
const CSV_PATH = "csv_blocks/";
const CSV_PATH_RES = "csv_res/";
main = function () {
processAnalysisAccounts(CSV_PATH, CSV_PATH_RES, analyseAccountsCB);
processAnalysis(CSV_PATH, fs.createWriteStream(CSV_PATH_RES + 'transactions.csv'), analyseTransactionCB);
processAnalysis(CSV_PATH, fs.createWriteStream(CSV_PATH_RES + 'receipts.csv'), analyseReceiptCB);
}
/** Init with DB path. */
blocks.init(dbPath, main);