DEV Community

loading...
Cover image for Running Your Own Ethereum Node with Geth

Running Your Own Ethereum Node with Geth

nheindev profile image Noah Hein ・4 min read

Introduction

You may want to run your own Ethereum node for a multitude of reasons. If you do, you need to run an Ethereum client of some sort. There are plenty out there, but I settled on geth. An easier alternative, and one that has a nice GUI is ganache. I've also heard of hardhat.

I won't speak to the other tools I just mentioned, but I know geth can be used to deploy to the mainnet, and I wanted to futureproof my knowledge a bit. That means it doesn't have a few of the bells and whistles built-in like those other clients do.

So while we will be deploying a local node for testing a dapp; this could be used to mine real ether, transfer funds, and all the other blockchain goodies that you would expect.

This was a bit of a pain to do and I had to go scrounging through github issues to find something that worked for me. There wasn't any offical documentation that I could tell, so maybe that can be my first PR for geth. Or maybe you'll beat me to it after reading this!

Installation

I'm running Linux Mint and all commands will be listed as such

You will need to install Ethereum before anything else.

Setup the launchpad repoistory

sudo add-apt-repository -y ppa:ethereum/ethereum
Enter fullscreen mode Exit fullscreen mode

I recommend using the stable version:

sudo apt-get update
sudo apt-get install ethereum
Enter fullscreen mode Exit fullscreen mode

However, you can also use the development version like so:

sudo apt-get update
sudo apt-get install ethereum-unstable
Enter fullscreen mode Exit fullscreen mode

If you are on another OS you can check here.

Genesis Creation

Every blockchain has to start with a single block, and that is commonly referred to as the genesis block. It doesn't hold any transactions or any relevant data. It is merely a point for all the other blocks to be based off of.

Fun bit of history, you can see Ethereum's first block here.

I will use gethTutorial as my root folder for this example.

mkdir gethTutorial && cd gethTutorial
Enter fullscreen mode Exit fullscreen mode

You will want to create a folder inside of whatever root directory you're in called private.

mkdir -p private && cd private
Enter fullscreen mode Exit fullscreen mode

Now for your first geth command! You will run this inside the private directory

puppeth
Enter fullscreen mode Exit fullscreen mode

It will ask you for a network name. I used nheintestnet. You can use whatever you'd like.

It will ask you 6 questions, and I'll list the proper responses here:

  1. What would you like to do? 2. configure new genesis
  2. What would you like to do? 1. Create new genesis from scratch
  3. Which consensus engine to use? 1. Ethash - proof-of-work
  4. Which accounts should be pre-founded? Default option (hit enter)
  5. Should the precompile-addresses (0x1 .. 0xff) be pre-founded with 1 wei? Default option (hit enter)
  6. Specify your chain/netowrk ID if you want an explicit one: 4224

After that it will create a new series of questions:

  1. What would you like to do? 2. Manage existing genesis
    • 2. Export genesis configurations
    • which folder to save genesis spec into? Default option (hit enter)

It will continue to ask questions, but you can exit the interface with CRTL+C at this point.

you should have 4 .json files at this point. The one of note is the plain one that has no "-" suffix. In my case: nheintestnet.json

Populating the Blockchain

We now are going to create a place to store the blockchain data and the keystores. Keystores will hold your private keys that are associated with the accounts you make on this node.

geth --datadir ~/gethTutorial/private init nheintestnet.json
Enter fullscreen mode Exit fullscreen mode

You should now see chaindata and keystore folders.

Creating Accounts

Well we have a blockchain now, we just need to get some accounts to get things going.

geth --datadir . account new
Enter fullscreen mode Exit fullscreen mode

It will ask you to enter a password.

Do this process 2 more times so that you have 3 accounts in total.

You should be able to see all your accounts by running

geth --datadir . account list
Enter fullscreen mode Exit fullscreen mode

Starting Up the Blockchain

in your private folder create a file: startnode.sh

inside you will want to paste this:

geth --networkid 4225 --mine --miner.threads 1 --datadir "/home/noah/gethTutorial/private" --nodiscover --rpc --rpcport "8545" --port "30303" --rpccorsdomain "\*" --nat "any" --rpcapi eth,web3,personal,net --unlock 0 --password /home/noah/gethTutorial/private/password.sec --ipcpath "~/Library/Ethereum/geth.ipc" --allow-insecure-unlock
Enter fullscreen mode Exit fullscreen mode

You will want to change your locations of the --datadir flag, and the --password flag arguments. You can try your luck with the ~ in the file names. The documentation says it should work from what I read, but I couldn't get it to work. I reccomend just writing the /home/user/ in the arguments.

In the private folder, create a file: password.sec

In this file, put the password of the first account you created.

To make the script executable

chmod +x startnode.sh
Enter fullscreen mode Exit fullscreen mode

Then execute the script:

./startnode.sh
Enter fullscreen mode Exit fullscreen mode

This will create a running Ethereum server in the terminal you ran the script in. Leave this terminal running.

Connecting to the Blockchain

Create a new terminal, and let's try to connect to our new server.

geth attach http://127.0.0.1:8545

This should open up a Geth Javascript console. You can run JS code here.

for a quick test, try to list the accounts that we have connected to this blockchain.

eth.accounts
Enter fullscreen mode Exit fullscreen mode

It should spit back an array of the 3 account addresses we created.

You are now running a geth node on your computer, and also successfully connected to it! Congrats!

Discussion (1)

Collapse
swizzard profile image
sam

Cryptocurrencies are destroying the planet.

Forem Open with the Forem app