-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
264 lines (215 loc) · 7.97 KB
/
index.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
const Trie = require('merkle-patricia-tree/secure');
const Levelup = require('levelup');
const Leveldown = require('leveldown');
const RLP = require('rlp');
const Config = require("./config.json")
var ProgressBar = require('progress');
const emptyStorageRoot = '0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421';
const addressStringSize = 43;
// Connect to the chaindata db
const db = Levelup(Leveldown(Config.DB_ADDRESS));
function readAddressFile(path) {
var fs = require('fs');
const stream = fs.createReadStream(path);
stream.on('readable', function () {
let address;
address = stream.read(44);
console.log(String(address));
});
}
// streamTrie and return all the key and values
function streamTrie(db, root) {
return new Promise(function (resolve, reject) {
root = add0xPrefix(root);
const trie = new Trie(db, root);
var allData = [];
trie.createReadStream()
.on('data', function (data) {
//values are rlp encoded
var dataDecoded = {};
dataDecoded['key'] = data.key.toString('hex');
dataDecoded['value'] = RLP.decode(data.value);
allData.push(dataDecoded);
})
.on('end', function () {
resolve(allData);
})
.on('error', function (err) {
reject(new Error(err));
})
})
}
// caculate the storage size given a storageRoot
function calculateStorageSize(db, storageRoot) {
return new Promise(function (resolve, reject) {
if (storageRoot === null) resolve(null);
if (storageRoot === emptyStorageRoot) resolve(0);
streamTrie(db, storageRoot).then(data => {
resolve(data.length);
})
.catch(function onRejected(error) {
reject(new Error(error))
});
})
}
function testStateRoot(statelist) {
const getRawBatch =
Object.values(
statelist
).map(
_stateRoot => streamTrie(db, _stateRoot)
);
Promise.all(getRawBatch).then(rawBatch => {
rawBatch.forEach(raw => {
index = rawBatch.indexOf(raw);
height = Object.keys(statelist)[index];
if (raw.length === 0) {
console.log(height + ': empty!');
} else {
raw.forEach(data => {
if (data.value[2] != emptyStorageRoot) {
console.log(data.key)
console.log(data.value)
console.log("\n\n");
}
})
console.log(height + ': Notempty!');
}
})
})
}
function add0xPrefix(str) {
if (str[0] === '0' && str[1] === 'x') {
return str;
} else {
return "0x" + str;
}
}
// geth the state size of an account on a certain stateRoot
function getStorageRoot(db, stateRoot, accountKey) {
return new Promise(function (resolve, reject) {
stateRoot = add0xPrefix(stateRoot);
// accountKey = add0xPrefix(accountKey);
//Creating a trie object
var trie = new Trie(db, stateRoot);
trie.get(accountKey, function (err, value) {
if (err) {
reject(new Error(err))
}
if (value === null) {
resolve(null);
} else {
var valudeDecoded = RLP.decode(value);
var storageRoot = valudeDecoded[2].toString('hex')
resolve("0x" + storageRoot);
}
})
})
}
function getStorageSizeList(db, stateRootList, accountAddress) {
return new Promise(function (resolve, reject) {
heightList = Object.keys(stateRootList);
const getStorageRootBatch = Object.values(stateRootList)
.map(stateRoot => getStorageRoot(db, stateRoot, accountAddress));
Promise.all(getStorageRootBatch)
.then(storageRootBatch => {
return Promise.all(
storageRootBatch.map(storageRoot => calculateStorageSize(db, storageRoot))
);
})
.then(storageSizeBatch => {
var storageSizeList = {};
for (i = 0; i < storageSizeBatch.length; i++) {
storageSizeList[heightList[i]] = storageSizeBatch[i];
}
resolve(storageSizeList)
})
.catch(function onRejected(error) {
reject(new Error(error))
});
})
}
function convertResult2CSV(result, stateRootList) {
return new Promise(function (resolve) {
var csv = "Contract Address,";
// add height as header to the csv
Object.keys(stateRootList).forEach(height => {
csv = csv + height + ",";
})
csv = csv.slice(0, -1) + '\n';
//add size data to the csv
Object.keys(result).forEach(address => {
csv = csv + address + ",";
Object.values(result[address]).forEach(size => {
csv = csv + size + ","
})
csv = csv.slice(0, -1) + '\n';
})
resolve(csv);
})
}
function main(db, stateRootList, accountList_path, result_path) {
const fsRead = require('fs');
const fsWrite = require('fs');
const streamRead = fsRead.createReadStream(accountList_path);
// remove the existed csv file
try {
fsWrite.unlinkSync(result_path)
//file removed
} catch (err) {
console.error(err)
}
var initcsv = "Contract Address,";
// add height as header to the csv
Object.keys(stateRootList).forEach(height => {
initcsv = initcsv + height + ",";
})
initcsv = initcsv.slice(0, -1) + '\n';
fsWrite.writeFile(result_path, initcsv, function (err) {
if (err) throw err;
});
// init progress bar
fileStat = fsRead.statSync(accountList_path)
var bar = new ProgressBar('processing [:bar] :etas', {
total: fileStat.size / addressStringSize,
incomplete: ' '
});
console.log(fileStat)
//read from file
streamRead.on('readable', async function () {
let raw;
var promiseBatch = [];
var addressList = [];
// console.log("ready");
while (raw = streamRead.read(addressStringSize)) {
let address = String(raw).substr(0, addressStringSize - 1)
bar.interrupt(address + ' (' + bar.curr + '/' + bar.total + ') ')
// calculate storage size and write to csv
// use await for preventing overuse of memory
let csv = ""
await getStorageSizeList(db, stateRootList, address)
.then(accountSizeBatch => {
csv = csv + address + ",";
Object.values(accountSizeBatch).forEach(size => {
csv = csv + size + ",";
})
csv = csv.slice(0, -1) + '\n';
// console.log(csv);
fsWrite.appendFile(result_path, csv, function (err) {
if (err) throw err;
});
bar.tick();
})
}
})
}
// const stateRoot_test = "0xe8b330fe7b24c08a8792a1af7f732b8065d03cfc456f506f4c9ea1651a44fd48";
// const AccountKey_test = "ffbf5e17acaefdd291820cfea154909363f896621155df2e426d88e15252a6bd";
// const AccountAddress_test = '0xC669eAAD75042BE84daAF9b461b0E868b9Ac1871';
// const StorageRoot_test = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421";
// readAddressFile(accountKeyList)
// testStateRoot({"150001": "0xe8b330fe7b24c08a8792a1af7f732b8065d03cfc456f506f4c9ea1651a44fd48"})
// getStorageRoot(db, stateRoot_test, AccountAddress_test).then(console.log)
// getStorageSizeList(db, stateRootList, AccountAddress_test).then(console.log)
const stateRootList = require(Config.STATE_ROOT_INPUT_ADDRESS);
main(db, stateRootList, Config.ACCOUNT_LIST_ADDRESS, Config.RESULT_ADDRESS)