I have an urge to automate boot configuration of ethereum nodes. I have started to write a shell script and face with several issues on this way.
Here is a scenario:
- Initialize all nodes with chain config:
$geth --datadir ./node1/data init <chain config>.json
$geth --datadir ./node2/data init <chain config>.json
$geth --datadir ./node3/data init <chain config>.json
- Start nodes:
$geth --datadir ./node1/data --port 2001 (default authrpc.port 8551)
$geth --datadir ./node2/data --port 2002 --authrpc.port 8552
$geth --datadir ./node3/data --port 2003 --authrpc.port 8553
(each command starts a daemon process -- lives "forever in the background", don't need to wait its completion)
- Link all nodes with a main one:
$geth attach ipc:node1/data/geth.ipc
$admin.nodeInfo.enode
(reply would be something similar to "enode://64dccd02d5d1166cfb4913f0d0c164dff2b9c61fd55182461010569e15319c7ff5cb4dc8b502e441c38c80ae1b42c2cc95c7e170ed973bb0353d766669c5447c@195.178.22.21:2001?discport=39805")
$geth attach ipc:node2/data/geth.ipc
$admin.addPeer("enode://64dccd02d5d1166cfb4913f0d0c164dff2b9c61fd55182461010569e15319c7ff5cb4dc8b502e441c38c80ae1b42c2cc95c7e170ed973bb0353d766669c5447c@127.0.0.1:2001")
Repeat for all nodes: each node should have reference in peers on all OTHERS nodes. Known issue: https://github.com/ethereum/go-ethereum/issues
(this is not a daemon process -- it is a remote access to the "server", need to wait for completion)
- Set reward collector:
$miner.setEtherbase(<account for collecting rewards from mining>)
(wait for completion)
- To make node miner:
$geth attach ipc:node3/data/geth.ipc
$miner.start()
$miner.stop()
$eth.getBalance(eth.accounts[0])
(here is a mix -- remote access to the "server" and run daemon process within, after it we can disconnect from the node aka "server")
Here is my script so far (thank you @terdon for help with &):
# !/bin/bash
echo "Run existing geth nodes. Please make sure they has been create & configured first!"
if ! command -v geth &> /dev/null
then
echo "geth command could not be found"
exit
else
echo "geth has been found. continue shell script"
fi
geth --datadir ./node1/data --port 2001
geth --datadir ./node2/data --port 2002
geth --datadir ./node3/data --port 2003
The first issue is script doesn't run all geth nodes in parallel. geth command above starts a process without end and show process's logs output meanwhile; next geth command will be executed only when previous one has finished. Is there a way to run all of them independently?
The second issue would be at the stage 7 geth attach <node address> where you should connect and open node's console, get some information and put it indo another node's console. I think the only option is here to create a temp file where to put a variable value and later read this value from another node's console. I haven't check it yet, but I am interested in your thoughts on how to do it right.
If you see here some other caveats, I would appreciate your help with this. Thanks!
Updated post according to questions in comments.
Update^2
The solution for 2nd issue should not use temp file, as geth JS console doesn't support operation with files. Possible workaround is to run geth node with http access and use curl to get this enode value.
One console: $geth --http --http.port 2001 --http.api admin,personal,eth,net,web3 --datadir ./node1/data
Another console: $curl -X GET http://127.0.0.1:8551 -H "Content-Type: application/json" --data '{"jsonrpc":"2.0", "method":"admin_nodeInfo"}'
At this moment the curl command returns 'missing token' with unclear cause.
geth --datadir ./node1/data --port 2001 &. But presumably you need to wait for them to finish before continuing, right? Should we assume that each command run for each node needs to wait for the previous ones to finish for that node?geth runstarts a daemon process. Please check the post, I have updated it to make it clear where is a daemon process and where is a remote access to the node.