Last active
January 31, 2019 06:41
-
-
Save kvhnuke/f192bd0832edf80378a2e87f49be30c2 to your computer and use it in GitHub Desktop.
Generate genesis block with n number of prefilled accounts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bip39 from "bip39"; | |
import hdWallet from "ethereumjs-wallet/hdkey"; | |
import cluster from "cluster"; | |
import fs from "fs"; | |
const ETH_PATH = "m/44'/60'/0'/0"; | |
const NUM_CPUS = 8; | |
const NUM_ACCOUNTS_PER_CORE = 200000; | |
const mnemonic = | |
"source pitch chronic mother need ghost unable asset supreme tragic else interest asset eagle update rule slim elder adapt next emerge trash doctor fun"; | |
const seed = bip39.mnemonicToSeed(mnemonic); | |
const wallet = hdWallet.fromMasterSeed(seed); | |
const genesisFile = { | |
config: { | |
chainId: 15, | |
homesteadBlock: 0, | |
eip155Block: 0, | |
eip158Block: 0 | |
}, | |
difficulty: "20", | |
gasLimit: "2100000", | |
alloc: {} | |
}; | |
const cleanup = () => { | |
fs.writeFile( | |
"./sample-genesis.json", | |
JSON.stringify(genesisFile, null, 2), | |
err => { | |
if (err) { | |
return console.log(err); | |
} | |
} | |
); | |
}; | |
let received = 0; | |
if (cluster.isMaster) { | |
for (var i = 0; i < NUM_CPUS; i++) { | |
const worker_env = { | |
start: NUM_ACCOUNTS_PER_CORE * i, | |
limit: NUM_ACCOUNTS_PER_CORE | |
}; | |
const proc = cluster.fork(worker_env); | |
proc.on("message", message => { | |
if (message === "done") proc.kill(0); | |
else { | |
received++; | |
if (received % 1000 === 0) console.log(received); | |
genesisFile.alloc[message] = { balance: "30000000000000000000" }; | |
if (received > NUM_ACCOUNTS_PER_CORE * NUM_CPUS - 1) cleanup(); | |
} | |
}); | |
} | |
} else { | |
const worker_env = process.env; | |
for ( | |
var i = parseInt(worker_env.start); | |
i < parseInt(worker_env.start) + parseInt(worker_env.limit); | |
i++ | |
) { | |
process.send( | |
wallet | |
.derivePath(ETH_PATH) | |
.deriveChild(i) | |
.getWallet() | |
.getAddressString() | |
.replace("0x", "") | |
); | |
} | |
process.send("done"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment