Skip to content

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

License

Notifications You must be signed in to change notification settings

hyc0812/private-blockchain-geth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

Tutorial: Play with Geth (MacOS)

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

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

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

Image description

Run Geth

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

geth

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

Image description

Create new accounts

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

geth account new

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": {}
  }

Image description

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

geth --datadir . init genesis-block.json

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

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

Image description

Mining Ethereum blocks

To begin mining our blockchain, we can run:

miner.start()

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

miner.stop()

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

eth.getBalance(eth.accounts[0])

Image description

Sending Tokens

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

personal.unlockAccount(eth.accounts[0])

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})

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])

Image description

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

And run the following again:

eth.getBalance(eth.accounts[1])

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published