Skip to content

Instantly share code, notes, and snippets.

@CowboyJim
Last active April 2, 2018 17:50
Show Gist options
  • Save CowboyJim/3fd5735f6aea75a28dae5e3ddbe2099b to your computer and use it in GitHub Desktop.
Save CowboyJim/3fd5735f6aea75a28dae5e3ddbe2099b to your computer and use it in GitHub Desktop.
Create and start Ethereum blockchain network
#!/usr/bin/env python
"""
Simple script that creates a four node Ethereum private blockchain
network using the geth client. Please adjust the script constants
to match your environment.
"""
import sys
from subprocess import call
import os.path
import time
# !!! Change values for your computer !!!
data_dir = "/Users/jboone/work/blog/ethereum"
log_dir = "/tmp/eth_logs"
num_nodes = 4
base_port = 30301
rpc_base = 8101
genesis_file = 'genesis_file.json'
password_file = "pw.txt"
password = "admin"
def createGenesisBlock(data_dir):
if not os.path.isfile(genesis_file):
genesis_block="""
{
"nonce": "0x0000000000000042",
"timestamp": "0x0",
"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"extraData": "Custom Genesis Block",
"gasLimit": "0x8000000",
"difficulty": "0x0100",
"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
"coinbase": "0x3333333333333333333333333333333333333333",
"alloc": {
}
}"""
f = open(genesis_file,"w")
f.write(genesis_block)
f.write("\n")
f.close()
if not os.path.exists(data_dir):
# Create the genesis block
print "Create a new blockchain"
call(['geth',"--datadir",data_dir,"init", genesis_file])
def createPasswordFile():
if not os.path.isfile(password_file):
f=open(password_file,"w")
f.write(password)
f.close()
def createUserAccount(node_num, cur_data_dir):
keystore_dir = cur_data_dir + "/keystore"
if not os.path.exists(keystore_dir):
print "Creating keystore directory"
os.makedirs(keystore_dir)
if not os.listdir(keystore_dir):
print "Creating an account for node " + str(node_num)
call(['geth',"--datadir",cur_data_dir,"--password", password_file,
"account","new"])
def unlockUserAccount(num, rpc):
print "Unlock the account for node {0}".format(num)
url="http://localhost:" + str(rpc)
call(['geth','--exec',"personal.unlockAccount(eth.accounts[0],'admin',99999)",
'attach', url])
def createNode(num, cur_data_dir, port, rpc, log_dir):
# Create the data directory if it doesn't exist
if not os.path.exists(cur_data_dir):
print "Created data directory for node {0}".format(num)
os.makedirs(cur_data_dir)
cmd = """nohup geth --datadir="{0}" --verbosity 4 --ipcdisable \
--rpcapi \"admin,debug,eth,miner,net,personal,shh,txpool,web3\" \
--networkid 12345 --port {2} --rpc --rpcport {3} --nodiscover \
--identity \"TestNode{6}\" --autodag --rpcaddr \"0.0.0.0\" \
2>> {4}/node{5}.log &
""".format(cur_data_dir,num, port, rpc, log_dir, num, num )
#print cmd
print "Launching node {0} in the background".format(num)
call(cmd, shell=True)
def main(argv):
# Create the log directory if it doesn't exist
if not os.path.exists(log_dir):
print "Creating log directory"
os.makedirs(log_dir)
createPasswordFile()
for num in range(0, num_nodes):
port = base_port + num
rpc = rpc_base + num
cur_data_dir = "{0}/node{1}".format(data_dir, num)
createGenesisBlock(cur_data_dir)
createUserAccount(num,cur_data_dir)
createNode(num, cur_data_dir, port, rpc, log_dir)
# Give the node time to start
time.sleep(3)
unlockUserAccount(num,rpc)
if __name__ == "__main__":
main(sys.argv[1:])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment