DEV Community

Cover image for Tutorial: Play with Geth (Go Ethereum)
Yongchang He
Yongchang He

Posted on

Tutorial: Play with Geth (Go Ethereum)

Interact with a command-line tool for running a private Ethereum blockchain network (MacOS)

This tutorial is meant for those with a basic knowledge of Ethereum and smart contracts, who have some knowledge of HTML and JavaScript, but who are new to dApps.
The purpose of building this blog is to write down the detailed operation history and my memo for learning the dApps.
If you are also interested and want to get hands dirty, just follow these steps below and have fun!~

Prerequisites

  • MacOS
  • CLI with Homebrew installed
  • Coding IDE

Intro & Review

In this tutorial we will be covering:

  1. Install Geth
  2. Run Geth
  3. Create new accounts
  4. Create genesis block
  5. Deploy private blockchain
  6. Mining Ethereum blocks
  7. Sending Tokens

What is Geth?

Geth(Go Ethereum) is a command-line interface for running Ethereum nodes implemented in Go Language. Using Geth you can join the Ethereum network, transfer ether between accounts or even mine ethers.

Getting started

Install Geth

We should first install Geth using the following CLI command:



brew tap ethereum/ethereum
brew install ethereum


Enter fullscreen mode Exit fullscreen mode

Then we can navigate to our favourite directory, create a folder with your favourite name (e.g. named my-private-blockchain)and navigate into the folder using the following command:



mkdir private-blockchain-geth
cd private-blockchain-geth


Enter fullscreen mode Exit fullscreen mode

Image description

Run Geth

We can then run the following command to see if Geth has been installed successfully:



geth


Enter fullscreen mode Exit fullscreen mode

If your CLI gets information like mine, congrats! You are on the track now. Geth is attempting to download the entire blockchain data into your PC, and the data can take up a ton of disk space. Just Control C to stop the command.

Image description

If you are very interested in what has happened just now, you can open a new CLI and navigate to the following directory to see the history logs:



cd Library/Ethereum/geth/
ls


Enter fullscreen mode Exit fullscreen mode

Image description

Create new accounts

Let's run the following command to create the first account on our private blockchain:



geth account new


Enter fullscreen mode Exit fullscreen mode

This command will then prompt us to enter a password to ensure security. After entering the password two times, our account 0 has been created.

Image description

Run geth account new again, to create our Account 1:

Image description

Create genesis block

We need to create a genesis block - the first block of the blockchain - to initialize our blockchain network.

Next let's open the folder /private-blockchain-geth using our favourite IDE (Mine is VSCode, and currently there is no file in this directory), create a file named genesis-block.json, copy and paste the following code to this file:



{
    "config": {
      "chainId": 15,
      "homesteadBlock": 0,
      "eip150Block": 0,
      "eip155Block": 0,
      "eip158Block": 0,
      "byzantiumBlock": 0,
      "constantinopleBlock": 0,
      "petersburgBlock": 0,
      "ethash": {}
    },
    "difficulty": "1",
    "gasLimit": "8000000",
    "alloc": {}
  }


Enter fullscreen mode Exit fullscreen mode

Image description

The next step is to initialize the genesis block using this command:



geth --datadir . init genesis-block.json


Enter fullscreen mode Exit fullscreen mode

Image description

Deploy private blockchain

The log "Successfully wrote genesis state" indicates that we have created our first block correctly. Now we can deploy our blockchain using the following command:



geth --allow-insecure-unlock --datadir . --keystore ~/Library/ethereum/keystore --networkid 4568 --http --http.addr '0.0.0.0' --http.corsdomain "*" --http.port 8502 --http.api 'personal,eth,net,web3,txpool,miner' --mine --miner.etherbase=YOUR_ACCOUNT_0_ADDRESS


Enter fullscreen mode Exit fullscreen mode

Note: Replace YOUR_ACCOUNT_0_ADDRESS with your account 0 Public address of the key

Image description

Image description

We have kicked off an interactive session that keeps printing new INFO of our blockchain. Leave it running and let's start a new CLI, navigate to the same directory /private-blockchain-geth and initialize Geth JavaScript console by running the following command:



geth attach geth.ipc


Enter fullscreen mode Exit fullscreen mode

Image description

Mining Ethereum blocks

To begin mining our blockchain, we can run:



miner.start()


Enter fullscreen mode Exit fullscreen mode

Let's allow it to run for a while, and then stop the mining by typing:



miner.stop()


Enter fullscreen mode Exit fullscreen mode

Now We have rewarded some tokens for mining new blocks. We can verify this by running the following command:



eth.getBalance(eth.accounts[0])


Enter fullscreen mode Exit fullscreen mode

Image description

Sending Tokens

Run the following command to finish the authentication process of account 0 before sending tokens:



personal.unlockAccount(eth.accounts[0])


Enter fullscreen mode Exit fullscreen mode

Input the password we have set to unlock account 0:
Image description

Now let's transfer tokens from account 0 to account 1 by running this command:



eth.sendTransaction({from: eth.accounts[0], to: eth.accounts[1], value: 500000})


Enter fullscreen mode Exit fullscreen mode

Value here is in Wei (Where 1 ETH equals 1 x 10 ^ 18 Wei)

We should get a green transaction hash if the transaction has been done correctly.
Image description

Since we had stopped the mining process before we were creating the transaction, this transaction is added to the memory pool rather than being added to the blockchain. At this point, account 1 cannot receive the transferred token if we run:



eth.getBalance(eth.accounts[1])


Enter fullscreen mode Exit fullscreen mode

Image description

Let's start the mining process again for a while:
Image description

And run the following again:



eth.getBalance(eth.accounts[1])


Enter fullscreen mode Exit fullscreen mode

Finally transferred tokens have been received by account 1:

Image description

Pretty COOL!

References
https://dev.to/heydamali/a-guide-to-private-ethereum-mining-with-geth-go-ethereum-13ol

https://geth.ethereum.org/docs/interface/managing-your-accounts

https://cointelegraph.com/news/high-severity-ethereum-geth-nodes-crash-eth-hashrate-drops-etc-remains-safe

https://trufflesuite.com/tutorial/index.html

Top comments (6)

Collapse
 
jrmaktub profile image
Jorge Ramires

Thanks for the tutorial! Had a lot of fun! Do you have more projects like this to practice? Would love to try them

Collapse
 
yongchanghe profile image
Yongchang He

Hello there,

Glad you love it!

I do have published several tutorials related with playing web3, blockchain, smart contract side of things. Such as build your own multi-node chain, play with truffle and ganacheUI, Solidity smart contract practice and so on.

Contact me if more resources or tutorials or answers needed.

Cheers and have fun!

Collapse
 
jrmaktub profile image
Jorge Ramires

Do you have more tutorials or projects with Geth specifically? Something to practice and learn. Thank you very much!

Thread Thread
 
yongchanghe profile image
Yongchang He

This one is with Geth. Using this one you may want to try and practice with building your own multiple nodes on different PCs that connected to your local chain, and can interact with each other and the smart contract that deployed on the chain.
You can find docs and more funs on official Geth.

Cheers!

Collapse
 
bellatrix profile image
Sakshi

Thanks for the wonderful tutorial!

Collapse
 
yongchanghe profile image
Yongchang He

Great to hear that :). Thank you!