Skip to content

Instantly share code, notes, and snippets.

@zelig
Created March 27, 2015 03:16
Show Gist options
  • Select an option

  • Save zelig/071c7c9edfac197cafda to your computer and use it in GitHub Desktop.

Select an option

Save zelig/071c7c9edfac197cafda to your computer and use it in GitHub Desktop.
local ethereum cluster: usage: bash cluster.sh <cluster_root> <number_of_clusters> <network_id> <runid> <local_IP> [[params]...]
#!/bin/bash
# bash cluster <cluster_root> <number_of_clusters> <network_id> <runid> <local_IP> [[params]...]
#
# sets up a local ethereum network cluster of nodes
# - the nodes are set up with datadir root/00, root/01, ...
# - the nodes log into root/00.log root/01.log, ...
# - new accounts are created for each
# - the new account is unlocked when they launch
# - they launch on port 30300, 30301, ...
# - by collecting the nodes nodeUrl, they get connected to each other
# - if enode has no IP, local_IP is substituted
# - if network_id is not 0, they will not connect to a default client,
# resulting in a private isolated network
#
# the cluster can be killed with `killall geth` (FIXME: should record PIDs)
# and restarted from the same state
root=$1
mkdir -p $1
shift
N=$1
shift
network_id=$1
shift
run_id=$1
shift
local_ip=$1
shift
if [ ! -f "$root/nodes" ]; then
for ((i=1;i<=N;++i)); do
id=`printf "%02d" $i`
echo "getting enode for instance $id ($i/$N)"
eth="./geth -datadir $root/$id -logfile /dev/null -port 303$id -networkid $network_id"
cmd="$eth js <(echo 'console.log(admin.nodeInfo().NodeUrl)') "
echo $cmd
bash -c "$cmd" >> $root/nodes
echo "setting coinbase for instance $i:"
cmd="$eth -password <(echo $id) account new"
echo $cmd
bash -c "$cmd"
done
fi
bootnodes=`cat $root/nodes|tr '\n' ' '|perl -pe "s/\[\:\:\]/$local_ip/g"`
echo $bootnodes | tr ' ' '\n' | nl
for ((i=1;i<=N;++i)); do
id=`printf "%02d" $i`
eth="./geth -unlock primary -password <(echo $id) -networkid $network_id -datadir $root/$id -logfile $root/$id.$run_id.log -loglevel 5 -mine -port 303$id --bootnodes=\"$bootnodes\""
cmd="$eth"
echo "launching miner $i/$N ---> tail -f $root/$id.$run_id.log"
echo $cmd $*
bash -c "$cmd $* &"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment