-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate-genesis.js
111 lines (98 loc) · 2.99 KB
/
generate-genesis.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
const {spawn} = require("child_process")
const program = require("commander")
const nunjucks = require("nunjucks")
const fs = require("fs")
const web3 = require("web3")
const roles = require("./roles")
const initHolders = require("./init-holders")
program.option("-c, --chainid <chainid>", "chain id", "189")
require("./generate-validatorset");
const {toJSON} = require("lodash/seq");
program.version("0.0.1")
program.option(
"-o, --output <output-file>",
"Genesis json file",
"./genesis.json"
)
program.option(
"-t, --template <template>",
"Genesis template json",
"./genesis-template.json"
)
program.option("--docker <docker>",
"if use docker solc",
true);
program.parse(process.argv)
// compile contract
function compileContract(key, contractFile, contractName) {
return new Promise((resolve, reject) => {
let ls;
if (program.docker) {
ls = spawn("docker", [
"run",
"--rm",
"-v",
process.cwd() + ":/contract",
"ethereum/solc:0.6.4",
"--bin-runtime",
"/=/",
"--optimize",
"--optimize-runs",
"200",
"/contract/" + contractFile
])
} else {
ls = spawn("solc", [
"--bin-runtime",
"/=/",
"--optimize",
"--optimize-runs",
"200",
contractFile
])
}
const result = []
ls.stdout.on("data", data => {
console.error(`data===: ${data}`)
result.push(data.toString())
})
ls.stderr.on("data", data => {
console.error(`stderr: ${data}`)
})
ls.on("message", data => {
console.error(`msg====: ${data}`)
})
ls.on("close", code => {
console.log(`child process exited with code ${code}`)
console.log("res====" + result)
resolve(result.join(""))
})
}).then(compiledData => {
compiledData = compiledData.replace(
`======= ${program.docker ? "/contract/" : ''}${contractFile}:${contractName} =======\nBinary of the runtime part:`,
"@@@@"
)
const matched = compiledData.match(/@@@@\n([a-f0-9]+)/)
return {key, compiledData: matched[1], contractName, contractFile}
})
}
// compile files
Promise.all([
compileContract(
"validatorContract",
"contracts/BMCValidatorSet.sol",
"BMCValidatorSet"
)
]).then(result => {
const data = {
chainId: program.chainid,
initHolders: initHolders,
extraData: web3.utils.bytesToHex(roles.extraValidatorBytes)
}
result.forEach(r => {
data[r.key] = r.compiledData
})
const templateString = fs.readFileSync(program.template).toString()
const resultString = nunjucks.renderString(templateString, data)
fs.writeFileSync(program.output, resultString)
})