DEV Community

Axat Bhardwaj
Axat Bhardwaj

Posted on • Updated on

How to set up Go Ethereum (Geth) private Network/Blockchain and deploy a Smart contract on it (2021)

I'll divide the process in 10 steps :-

  1. Install geth
  2. Configure the Genesis block
  3. Create local nodes
  4. Generate initial accounts
  5. Initialize and start the nodes
  6. Connect to each node from the command line
  7. Interconnect the nodes
  8. Send some Ether
  9. Start a miner
  10. Deploy a Smart Contract

here we go …!

1. Install geth

geth is the command-line interface for running a full Ethereum node implemented in Go. To install it on a Mac, run the following commands:

$ sudo add-apt-repository -y ppa:ethereum/ethereum
$ sudo apt-get update
$ sudo apt-get install ethereum
Enter fullscreen mode Exit fullscreen mode

2. Configure the Genesis block

Next, we need to create a JSON file that will define how the first block of the blockchain will be created. Each node in the network must be initialized with the same genesis block.
Create a file called genesis.json and save it in the parent folder of the nodes directories. Copy the content below but replace the addresses with those of the accounts created in the 3rd step.

{
    "nonce": "0x0000000000000042",
    "timestamp": "0x0",
    "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "extraData": "0x00",
    "gasLimit": "0x8000000",
    "difficulty": "0x400",
    "mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
    "coinbase": "0x3333333333333333333333333333333333333333",
    "alloc": {
        "0x15024A50E641Ad330e0096bDca8Ea06f9f972Dcd": { //acc1 add it can be diff in ur case
            "balance": "1000000000000000000"
        },
        "0xE3F2Defe3e3DDB4694a985bA6AA052Ad888BCF3D": { //acc2 add it can be diff in ur case
            "balance": "2000000000000000000"
        }
    },
    "config": {
        "chainId": 111,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip150Block": 0,
        "eip158Block": 0
    }
}
Enter fullscreen mode Exit fullscreen mode

The config section ensures that certain protocol upgrades are immediately available. The alloc section pre-funds accounts.

3. Create local nodes

For the purpose of this private blockchain, we will create some nodes locally in our machine.
Create 3 new directories. Each of them will represent a single node in the blockchain:

 $ mkdir geth
 $ cd geth
 $ mkdir pvtchain
 $ cd pvtchain
 $ mkdir node01 node02 node03
Enter fullscreen mode Exit fullscreen mode

4. Generate initial accounts

Let's create 2 Ethereum accounts in the first node so that later we can do some transactions for testing purposes:

$ geth --datadir "/PATH_TO_NODE01/" account new
Enter fullscreen mode Exit fullscreen mode

You will be prompted for a secret password. Write down the address of the created accounts and memorize the password.

5. Initialize and start the nodes

The first step is to initialize each node with the genesis file previously created. For this, run this command for each of the nodes, replacing it with the correct path:

$ geth --datadir "/PATH_TO_NODE/" init /PATH_TO/genesis.json
Then start each node by running the following command for each of them. You will need to use different numbers both for the port and the rpcport.
$ geth --identity "node1" --http  --http.port "8000" --http.corsdomain "*" --datadir "/home/ubuntu/geth/pvtchain/node1" --port "30303" --nodiscover --http.api "db,eth,net,web3,personal,miner,admin" --networkid 1234 --nat "any" --allow-insecure-unlock
$ geth --identity "node2" --http  --http.port "8001" --http.corsdomain "*" --datadir "/home/ubuntu/geth/pvtchain/node2" --port "30304" --nodiscover --http.api "db,eth,net,web3,personal,miner,admin" --networkid 1234 --nat "any" --allow-insecure-unlock
$ geth --identity "node3" --http  --http.port "8002" --http.corsdomain "*" --datadir "/home/ubuntu/geth/pvtchain/node3" --port "30305" --nodiscover --http.api "db,eth,net,web3,personal,miner,admin" --networkid 1234 --nat "any" --allow-insecure-unlock
Enter fullscreen mode Exit fullscreen mode

💡 pls remember "/home/ubuntu/geth/pvtchain/node1 is my path to node directory

6. Connect to each node from the command line

In order to interact with the nodes, you need to open a terminal and use geth to attach to it. Run the following command for each of the nodes, in a separate terminal and use the correct port number for each of them:

$ geth attach <http://127.0.0.1:8000>
$ geth attach <http://127.0.0.1:8001>
$ geth attach <http://127.0.0.1:8002>
Enter fullscreen mode Exit fullscreen mode

7. Interconnect the nodes

The first node will be our admin node. Run the following command in the console to read some characteristics about it:

> admin.nodeInfo
Enter fullscreen mode Exit fullscreen mode

Copy the enode address completely:
enode: "enode://26f7b83...ed769122f1692e@[::]:30303?discport=0"
Now you can connect the other nodes to this one by running the following command in their respective terminals:

> admin.addPeer("enode://26f7b8...92e@[::]:30303?discport=0")
Enter fullscreen mode Exit fullscreen mode

You can verify that the nodes are peering by entering the following command in the terminal corresponding to the admin node:

> net.peerCount
2
Enter fullscreen mode Exit fullscreen mode

8. Send some Ether

Next, we will execute a transaction. Go to the console attached to the first node and run the following command to check that you can see the accounts created before:

> eth.accounts
Enter fullscreen mode Exit fullscreen mode

Then check the balance of the first account:

> eth.getBalance(eth.accounts[0])
Enter fullscreen mode Exit fullscreen mode

Now unlock the first account in order to start a transaction from it:

> personal.unlockAccount(eth.accounts[0])
Enter fullscreen mode Exit fullscreen mode

Now send some Ether:

> eth.sendTransaction({from:eth.accounts[0], to:eth.accounts[1], value:1000})
Enter fullscreen mode Exit fullscreen mode

9. Start a miner

The first thing to do is to set up an account to receive the mining awards. This is called the etherbase account. We will use the admin node as a miner. Go to the console attached to it and run the following command:

> miner.setEtherbase(eth.accounts[0])
Enter fullscreen mode Exit fullscreen mode

Then run the following command:

> miner.start()
Enter fullscreen mode Exit fullscreen mode

You should see a null message in the console, and then in the other console where you started the node, you should see the progress of the mining that you just started.
You can now verify the balance of the account and check that the transfer was done properly.

10. Deploy a Smart Contract

In this section, we will create and deploy a simple smart contract into our network. For this, we will be using Truffle, which is a development framework for Ethereum.
Run the following command to install the Truffle library:

$ npm install -g truffle
Enter fullscreen mode Exit fullscreen mode

Next, we will initialize a new Truffle project by running this command in a new folder:

$ truffle init
Enter fullscreen mode Exit fullscreen mode

You will notice that some initial files and directories were created. We need to modify the truffle-config.js so that Truffle knows how to connect to our network. Use the following content for that file, considering that for the port number you need to use the port where your first node is running, and for the from the address you must complete the address of the first account created in that node:
Next, open the contracts folder and create a new file called hello.sol. Complete it with the following code, which is a simple contract written in the Solidity language:
The next step is to update the migrations file so that Truffle knows that it needs to compile and migrate the contract that we just created. Open the 1_initial_migration.js file located in the migrations folder and modify its content to match the following:
We can now use Truffle to compile our contract and deploy it into our network. Notice that Truffle will also compile and deploy its migrations contract, which is used to track the deployed versions of our contracts and keep them updated.
Go to the contracts folder and run the following command:

$ truffle compile
Enter fullscreen mode Exit fullscreen mode

Next, we need to unlock the account in the first node again, as we are going to deploy the contract using that account. Go to the terminal that is attached to that node and run this command:

> personal.unlockAccount(eth.accounts[0])
Enter fullscreen mode Exit fullscreen mode

Finally, go back to the contracts folder and run this command to deploy the contract:

$ truffle migrate
Enter fullscreen mode Exit fullscreen mode

The contract should now be deployed in your network at a new address.
The contract is deployed in our network
Cheers !!

Axat Bhardwaj,
Blockchain Enthusiast & Developer,
Connect with me on LinkedIn: https://www.linkedin.com/in/axatbhardwaj

Top comments (0)