In this tutorial you will learn how to connect to the Ethereum Mainnet and then later build a private blockchain network using Go-Ethereum(geth). We are going to be working with the Genesis.json file, so if you read my previous post then you're good to go. If not you can check it out Here, and then come back.
The Genesis.json file we will be using for this tutorial can be found Here. With that said let's get started.
What is Go-Ethereum (geth)
Go-Ethereum (geth) is a blockchain node, it is downloaded into your computer and runs locally. It is a command line tool and will give you access to the blockchain via a javascript JSON RPC (this is the connection between you and the blockchain). When you run Geth, it tries to find other blockchain nodes, connects to them and start downloading blocks so it basically replicates the blockchain locally on your computer. Geth then opens a javascript JSON RPC on your computer to allow you interact with the node.
There are several ways in which Geth synchronizes. It's either as a
-
Full Sync (Slow)
- It downloads block headers.
- It downloads block bodies of every block.
- It downloads All historical transactions.
- It takes 30GB of Data.
- It takes time to process and finish.
-
Fast Sync (Default)
- It downloads block headers.
- It downloads block bodies of every block.
- It downloads the last 1024 transactions.
- It takes a few hours.
-
Light Sync (Experimental)
- It downloads only block headers.
- Takes the latest snapshot of the current state.
- It is less secure but much faster.
- It doesn't take up much space.
Downloading & Installing Geth
To install Geth you have to go Here, once you get there it will then try to retrieve the current version number of Geth and give you different options for Linux, Windows, Mac and also the sources.
Note that Geth does not have any upgrade mechanism implemented, so you will have to check from time to time for new releases.
I'm on windows and as of this writing i will download the version below. Please make sure to download the latest version that is compatible with your computer.
Geth 1.10.18 64bit installer
Starting Geth
Now that we have downloaded and installed Geth, remember that Geth is a command line tool so to run it we need to navigate to our command line interface, for me i will be using powershell. Once you have your CLI opened we want to start Geth without any parameters. To do this type:
geth
once you have done that, Geth would start downloading the ethereum mainnet on your computer. It would start something called Block synchronization and then it imports all the blocks from the network into your computer. Remember i said that there are 3 different ways in which Geth synchronizes, so when you start Geth for the first time it synchronizes using the Fast Sync. But if I interrupt it right now and restart Geth, it would start synchronizing using the Full Sync (you will notice it's much slower).
Note that the blocks that are been downloaded into your computer are normal files and I will show you where you can find this files (blocks).
Assuming I interrupted Geth (by typing Ctrl + C
on powershell) while it was synchronizing, and it switched to a Full Sync on restart, how do I switch back to a Fast Sync? well to do this you would have to locate where the files are stored and delete the geth folder. Here is where you can locate the files on your computer:
- Mac:
~/Library/Ethereum
- Linux:
~/.ethereum
- Windows:
~/AppData/Local/Ethereum
The keystore folder is where Geth stores your private keys when you create a new account, so don't delete it and always back it up properly.
Log-Outputs From Geth Explained
Here I would like to explain some of the log-ouputs we get when we run Geth. I have highlighted them in the image below.
- Notice here that the chainID is set to 1, this number is reserved for the Ethereum Mainnet only, while 2 & 3 are reserved for the Testnets. Therefore you can not use any of these numbers when creating a private blockchain network.
- The log-output also displays the Geth instance which your computer is running on. Be sure to download the latest version of Geth or else you might not be able to connect to the Ethereum Mainnet. As of this writing i'm running on Geth version 1.10.18.
- Notice the IPC endpoint, IPC stands for Interprocess communication. This endpoint is used when you want to connect to a running Geth node.
- Note that there is no RPC endpoint opened yet, because when you start Geth without any parameter then it will not open any HTTP RPC endpoint. That means if you try interacting with this running node it will not work.
Connecting & Interacting With A Running Geth Instance.
Now that we have a Geth instance running, we then want to connect to it. To do this we are going to use the IPC endpoint from Geth. We are going to run Geth on one powershell and try connecting to it from another one.
From the image above, we are running Geth on the left and we connect to it on the right using the IPC endpoint from the running
Geth instance. To connect to Geth you have to type this command:
geth attach ipc:\\.\pipe\geth.ipc
The above command brings Geth into a client mode. And now we can interact with it.
Now that we have connected to our running Geth instance, we can see the instance that we are connected to and the different
modules we have. For example we have an admin module with different functions that we can call, I highlighted them below:
We can also create a new account using this command:
personal.newAccount();
This would require a passphrase, although you won't see it as you type. Once you've have done that you should have a new account (address). The private keys for this account is stored in the keystore directory
This is basically how to connect to a running blockchain node and interact with it using JavaScript commands in your command line. There is a lot more commands that you can run, and here is where you can find them.
Creating A Private Blockchain Network.
Now that we have a basic understanding on how to run, connect and interact with a blockchain node, I would like to show you how you can actually create a private network with Geth. We are going to be working with the Genesis.json file so be sure to download it.
- Next we create a folder containing the genesis.json file and open it on powershell or you preferred terminal.
- In our terminal, we then initialize a new directory with the genesis.json file where our blocks are going to be stored.
geth init .\genesis.json --datadir mychaindata
Now Geth would initialize a new genesis block inside the directory that we specified i.e mychaindata. You should see a log that says Successfully wrote genesis state .
Now let's start our new private blockchain with the mychaindata directory. To do this type:
geth --datadir .\mychaindata\
Notice the chainID is set to 15, which is what we have in the genesis.json file. You can also see that Geth opens the IPC endpoint, also notice that Geth doesn't start the block synchronization because we are on our private network.
- Sometimes Geth starts synchronizing with another network with the same chainID, this happens because it's in discovery mode. If it finds other nodes with the same chainID then it will start downloading those blocks that are already in that chain and connect to it. When running a private network this is something we want to avoid. Therefore to avoid this you need to run this:
geth --datadir .\mychaindata\ --nodiscover
Connecting & Interacting With Our Private Network.
Now that we have a private network running, when want to connect to it from another terminal. To do this we are going to use the IPC endpoint from our running private network. This is very similar to the way we connect to a running Geth instance.
From the image above, we have our private network running on the left and we have connected to it on the right using this command:
geth attach ipc:\\.\pipe\geth.ipc
Now that we have connected to our private network, lets write some JavaScript commands to interact with it. First we want to check if we have any accounts on our private network, to do this type this command:
eth.accounts
Notice that we get back an empty array
[]
, that means we don't have any account on our private network.
Since we don't have an account, let's create one. To do this type this command:
personal.newAccount();
I'm sure you're already familiar with this command. You will be asked to provide a passphrase just like we did previously. Once that's done you should have an account created. Also the private keys for this account would be stored in the Keystore directory.
Adding Ether To Our Account
Now that we have an account on our private network, we want to add some Ether into that account. To do this we are going to Mine Ether on our private network into that account. For mining you need to have a coinbase account set, so when the miner is running it will credit your Ether to into the coinbase account. To do this run this command:
eth.coinbase
Notice that the coinbase account is the same as the account we created earlier, this is because it chooses the first account in the
eth.accounts
array[]
.
Now let's start the miner, to do this type this command:
miner.start(1);
When you start a miner you have to pass in one parameter, and this is the amount of threads it will run on. So we pass in 1.
Once the miner starts it will return null
, this is perfectly fine. But on the main window where the private network is running, you would see some logs like this:
Notice that the mining thread is set to 1, it then starts mining and committing new blocks. Every time a block is successfully sealed, Ether would be credited into your account.
Now let's have a look at the balance of our account. To do this we a going to use the web3
endpoint.
web.eth.getBalance(eth.accounts[0]);
OR
eth.getBalance(eth.accounts[0]);
As long as the miner is running and a new block is successfully sealed, Ether will always be credited into the account and the amount of Ether in the account increases. Mining will slow down after some time, this is because when a new block is mined the difficulty increases(I talked about this in my previous post).
Now we want to convert the money in our account from Wei to Ether. To do this type this command:
web3.fromWei(eth.getBalance(eth.accounts[0]), "ether");
With that said, this is how you interact and mine Ether with your private blockchain network.
That's it for this post guy's, I hope you enjoyed it as much as i did. If you have any questions or run into any issue while following this post, be sure to drop a comment and I will reply.
Keep building, catch you later!
Top comments (2)
Wow, I should try this out soon!
Yes, definitely