Note

If you are new to the blockchain technology, taking our Introduction to Blockchain Technology self-paced course is highly recommended. Also, for a comprehensive coverage of blockchain development in Ethereum or mastering Solidity programming, taking our below self paced courses is highly recommended:

Recap

In our previous article (How to Set up a Local Private Ethereum Blockchain), we discussed how to set up a local private Ethereum network.

In this article, we learn how to setup and run Geth on a local private Ethereum network.

Running Geth

Once the genegsis.json file has been created, you are ready to start the private Ethereum blockchain. We will start the node in one terminal/process where we can see the logs being written to stdout. In the second terminal, we will use the geth console to get the console. There are advantages of using two terminals:

  • Logs from the first terminal are written to stdout instead of being redirected. We know that everything is functional.
  • Closing the console in the second terminal doesn’t terminate the geth syncing process.

Now, let’s see how we can run geth:

1. In dir hello_world, we will start the first node by running the following command:

$ geth --datadir block_0 --identity node_0  --verbosity 6 -- ipcdisable --port  30398 --rpcport 8171

The following is a list of a few command-line options:

  • –datadir: It specifies the directory for the database and Keystore to be stored. In our case, it is set as block_0, which means the database with the first genesis block on our private blockchain was created under the block_0 directory. Everything geth persists now will write to block_0. The default datadir value is listed in the following table:
Operating system Datadir
Mac ~/Library/Ethereum
Windows %APPDATA%\Ethereum
Linux ~/.ethereum
  • –identity specifies a custom node name.
  • –verbosity defines the log level.
  • –ipcdisable disables the IPC-RPC server during the startup.
  • –port is the network listening port number for the node; the default value is 30303.
  • –rpcport specifies the http-rpc server listening port number; the default is 8545.

2. We will use the geth console to start a session of an interactive JavaScript environment, where we will interact with the first node that we just created. It will show us something like the following:

$ geth console
  Welcome to the Geth JavaScript console!
  instance: Geth/node_0/v1.8.22-stable/darwin-amd64/go1.11.5 modules:  admin:1.0 debug:1.0 eth:1.0  ethash:1.0 miner:1.0 net:1.0  personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

3. In the console, we can obtain all the node information with
the admin.nodeInfo command. We will need enode later when we want to implement it in the second node as it is a peer to the second node. To check the enode of the first node, we can type in the following command:

 

admin.nodeInfo.enode "enode://4202d9ac5c1c7c8eea8ae56dbc881fb61230c7447d81b964345bba3841 684f8449e6dce625552c3c881622a6f3dabeee596f3ff61dd212ad14cfb6f687793 ef1@100.100.100.100:30398"

Note that we assume that the 30398 port is open for listening. This might not work for users who have their firewall enabled. I used a fake IP address 100.100.100.100, and it will be used several times in this section. To find your own IP address, you can use the following command in your terminal:

$ ifconfig|grep netmask|awk '{print $2}'

4. Leave the terminal open or save the enode information for the first node somewhere. So far, we have a single node in the network. If we check on peers now, it will be an empty list, as follows:

admin.peers[]

 

To run multiple nodes of a local blockchain, we will need to make sure they are running on a different port. We will use a different port for the second node by putting down a different value in –port and –rpcport.
5. Let’s run the second node with a slightly different command, as follows:

$ geth --datadir block_1 --identity node_1  --verbosity 6 -- ipcdisable  --port 30399 --rpcport 8173 console 2>>  logs/second_node.log

We have two nodes running, but they are unaware of each other’s existence. In order to create a peer-to-peer network with these two nodes, we can use the addPeer command.

6. In the current console for the second node, we will add the first node as a peer to the second node, as follows:

>admin.addPeer("enode://4202d9ac5c1c7c8eea8ae56dbc881fb61230c7447d8 1b964345bba3841684f8449e6dce625552c3c881622a6f3dabeee596f3ff61dd212 ad14cfb6f687793ef1@100.100.100.100:30398")
True

Using admin.addPeer to add static nodes at runtime is just adding nodes ephemerally, instead of permanently. If you have restarted the process, you will notice the peer you’ve added is gone in a new session. Admin.addPeer is usually used for debugging purposes. If you would like to add a peer permanently, the peer needs to be manually configured in the geth config file called static- nodes.json, under the <datadir>/geth/ path, as shown in the following example:

[
  "enode://4202d9ac5c1c7c8eea8ae56dbc881fb61230c7447d81b964345bba3841 684f8449e6dce625552c3c881622a6f3dabeee596f3ff61dd212ad14cfb6f687793 ef1@100.100.100.100:30398"
  ]

If you have more static nodes to add, you can keep appending some enode URLs to the static-nodes.json file, as shown in the following example:

  [
  "enode://4202d9ac5c1c7c8eea8ae56dbc881fb61230c7447d81b964345bba3841 684f8449e6dce625552c3c881622a6f3dabeee596f3ff61dd212ad14cfb6f687793 ef1@100.100.100.100:30398",
  "enode://pubkey@ip:port",
  ...
  ]

7. The console returns true when it is successfully executed. We will use the following command to test the connection:

admin.peers...

Furthermore, admin.peers will list out the detailed information of the first node; we won’t list them all here.
As you can see in the following code snippet, net.peerCount will show only 1 peer in your local blockchain network now:

net.peerCount1

Check if the network is listening, with the help of the following command:
net.listeningtrue

That is all you need to start a private blockchain. As we discussed before, you can configure your development tools to connect to the local blockchain for testing and debugging before you deploy your smart contracts to testnet or mainnet.

 

Next Article

In our next article (How to Build a Local Private Ethereum Blockchain with Mining), we discuss how to develop a local private Ethereum network that includes mining component.

This article is written in collaboration with Brian Wu who is a leading author of “Learn Ethereum: Build your own decentralized applications with Ethereum and smart contracts” book. He has written 7 books on blockchain development.

Resources

Free Webinars on Blockchain

Here is the list of our free webinars that are highly recommended:

 

Free Courses

Here is the list of our 10 free self-paced courses that are highly recommended:

 

Self-Paced Blockchain Courses

If you like to learn more about Hyperledger Fabric, Hyperledger Sawtooth, Ethereum or Corda, taking the following self-paced classes is highly recommended:

  1. Intro to Blockchain Technology
  2. Blockchain Management in Hyperledger for System Admins
  3. Hyperledger Fabric for Developers
  4. Intro to Blockchain Cybersecurity
  5. Learn Solidity Programming by Examples
  6. Introduction to Ethereum Blockchain Development
  7. Learn Blockchain Dev with Corda R3
  8. Intro to Hyperledger Sawtooth for System Admins

 

Live Blockchain Courses

If you want to master Hyperledger Fabric, Ethereum or Corda, taking the following live classes is highly recommended:

 

Articles and Tutorials on Blockchain Technology

If you like to learn more about blockchain technology and how it works, reading the following articles is highly recommended:

 

Articles and Tutorials on Ethereum and Solidity

If you like to learn more about blockchain development in Ethereum with Solidity, reading the following articles and tutorials is highly recommended:

 

Articles and Tutorials on Hyperledger Family

If you like to learn more about blockchain development with Hyperledger, reading the following articles and tutorials is highly recommended:

 

Articles and Tutorials on R3 Corda

If you like to learn more about blockchain development on Corda , reading the following articles and tutorials is highly recommended:

 

Articles and Tutorials on Other Blockchain Platforms

If you like to learn more about blockchain development in other platforms, reading the following articles and tutorials is highly recommended: