-
Notifications
You must be signed in to change notification settings - Fork 0
/
split_block.js
69 lines (50 loc) · 1.87 KB
/
split_block.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
const network = process.argv.slice(2)[0];
const approved_networks = ['staging', 'testnet', 'mainnet']
const fsPromises = require('fs').promises;
const homedir = require('os').homedir();
const path = require('path')
if (!approved_networks.includes(network)) {
throw new Error(`mssing network arg, must be in ${approved_networks}.`)
}
async function readGenesis() {
const content = await fsPromises.readFile(path.join(homedir, 'genesis_block.json'))
.catch(err => {
throw new Error(`Could not find genesis_block.json in HOME directory; ${homedir}`)
})
console.log();
return JSON.parse(content);
}
function get_butter_size(json_file){
// convert JSON object to String
var json_string = JSON.stringify(json_file);
// read json string to Buffer
const buffer = Buffer.from(json_string);
return Buffer.byteLength(buffer)
}
function convert_to_MB(buffer_size){
return buffer_size / 1024 / 1024
}
async function write_file(filename, json_file){
await fsPromises.writeFile( `./${network}/${filename}`, JSON.stringify(json_file) )
.catch(err => {
throw new Error(err)
})
}
(async () => {
const genesis_file = await readGenesis();
const state_changes = genesis_file.genesis
const genesis_len = state_changes.length
genesis_file.genesis = []
await write_file(`genesis_block.json`, genesis_file)
let files_written = 0
const chunkSize = 300000;
let chunk_len = 0
for (let i = 0; i < state_changes.length; i += chunkSize) {
const chunk = state_changes.slice(i, i + chunkSize);
await write_file(`state_changes_${files_written + 1}.json`, chunk)
chunk_len += chunk.length
files_written += 1
}
console.log(`${files_written} state file written.`)
console.log(`${genesis_len} state changes written into ${files_written} chunks with ${chunk_len} size.`)
})()