Setting up a node and participating in a network
Here, you will learn how to set up a node using the geth
command-line tool. You will also see how to connect to a public network and perform operations such as ledger syncing and mining.
Getting ready
You will need a working installation of the geth
command-line interface. You can also start a node with parity
or any other Ethereum protocol implementation, but the steps may differ for each client.
Commands starting with $
have to be run on the command prompt/terminal and those starting with >
will work only on the web3
JavaScript console.
How to do it...
- Verify your installation by running the following version command:
$ geth version
- Start your node with the following command:
$ geth
This will start an Ethereum node and will connect with the main network. As soon as it finds any peer nodes, it will start downloading the blocks from them.
You can configure the parameters before starting a node. This will help you do things like connect to a different network, expose APIs, and much more. Let's look at a sample initialization and the parameters used in it:
$ geth --networkid 3 --datadir "./ropsten-db" --keystore "./ropsten-keys" --syncmode "fast" --rpc --rpcport "8546" --rpcapi "web3,eth,miner,admin" --rpccorsdomain "*" --port 30301 console
Let's look into each parameter in detail:
--networkid <id>
: A parameter to identify each network. You can either connect to the main/test network (1=Frontier(default), 2=Morden (disused), 3=Ropsten(PoW), 4=Rinkeby(PoA)) or any private network that you have set up.--datadir <path>
: Directory path for storing the blockchain database and keystore. You can change the defaultkeystore
directory with the--keystore
parameter.--syncmode <mode>
: A parameter to specify the type ofsync
method. You can choose fast, full, or light, based on your needs.--rpc
: Enables an RPC server through HTTP. You can also change parameters such as--rpcaddr
,--rpcport
, and--rpcapi
.--rpccorsdomain <list>
: Domains from which cross-domain requests are accepted. Use*
as a wildcard or specify domains as a comma-separated list.--port <port>
: Changes the default network listening port (30303
).--console
: Starts theweb3
JavaScript console.
Note
If you get stuck anywhere, you can always make use of the inbuilt help interface. Just enter geth help
and it will return a comprehensive list of all commands with their respective description that you can run using geth
.
- It might take a few minutes to identify the peers that are already on the network. Run the following command to return the list of peers that are currently connected to your node. Your node will start syncing once it finds at least one peer:
> admin.peers
- Check the current syncing status by running the following command. It will return false if not syncing:
> eth.syncing
- Run the
geth attach
command if you would like to connect to this node from a different console. You can also explicitly specify the host and the port. In our case, it islocalhost
and8546
:
$ geth attach http://localhost:8546
Note
Your firewall may restrict the node from communicating with an external peer. This can cause issues with the synchronization process. Also, ensure that you are not exposing your RPC APIs to the internet, which can result in attacks.