Syncing Geth to the Ethereum Blockchain

(Updated, 29th May 2018: I have written an update to this article. Consider reading Ethereum Geth Syncing 101 in 2018 instead)

I run geth on my cloud server to sync with the Ethereum Blockchain. I start geth whenever I code and it goes into a round of resynchronization with the Blockchain. This takes anything from minutes (that’s if the last time I code is yesterday), or hours (if the last time I did this was months ago). 

As of the time of writing, Ropsten (which I work on because ETHs are free here) is in block 1,911,915 and the main ethereum has reached block 4,399,227. If you do a complete sync, it will take you 2 to 3 days or more.

I took a break from Ethereum for more than a month after developing Go-Dutch and had the most painful experience restarting my work. The first reason is that Ropsten did a hard fork at around block 1.7m and whatever I have synced previously wouldn’t sync anymore. Then my version of geth at 1.6.7 wouldn’t sync after Ropster’s hard fork; it still runs but just keeps failing. So I had to upgrade to 1.7.2.

I will like to document what I have learnt so that others could benefit from my ordeal. 

Fast Sync

Here’s my geth command to start:

geth --testnet --syncmode "fast" --rpc --rpcapi db,eth,net,web3,personal --cache=1024 --rpcport 8545 --rpcaddr 0.0.0.0 --rpccorsdomain "*

The parameter –syncmode “fast” does a fast sync. When this flag is turned on, geth performs a fast sync that syncs only receipts rather than the full blocks of past transactions. It still takes hours but I can live with this. Fast sync doesn’t work if you already had blocks synced previously. 2 months is a long time in Ethereum-world, so the time it takes to do a full sync of 2 months worth of new blocks is going to be so much longer than if you could restart by doing a fast sync. So if its been a long time since your last sync, you are going to be better off deleting everything and restarting so that a fast sync can happen. 

Here’s how to do it:

cd ./ethereum/testnet sudo rmdir -rf chaindata

This assumes that you are working in testnet. It deletes all your chain data so that you can restart a fast sync. Start up geth and go to bed, let it do its work.

Am I done syncing?

The next thing that you will like to know is if geth is done. One way is to read geth while it syncs. 

INFO [10-21|02:28:45] Imported new chain segment blocks=1 txs=1 mgas=0.028 elapsed=6.515ms mgasps=4.340 number=1912033 hash=0cd0d3…453bae

Here’s an example. It says here that syncing is now at block number 1,912,033. Visit https://ropsten.etherscan.io/ to check the current block. If it is quite close to what’s stated there as the current block, you know you are quite close.

There’s a more accurate way to do this. Fire up truffle console

sudo truffle console

And enter this command. 

web3.eth.syncing

If it says “false”, you are done. Geth is well synced to the block. Congratulations! 

You know that it’s not done yet if you see this:

{ 
    currentBlock: 1600000,
    highestBlock: 1700000,
    knownStates: 1190000,
    pulledStates: 1140000,
    startingBlock: 1730000
}

It says here that you have synced up to 1,600,000 blocks and the current highest block is 1,700,000. Take a break, there are 100,000 more blocks to go.

Sync as a Service

I eventually got sick of syncing every time I need to work with Ethereum. So I started syncing all the time. The good folks at ethereum.stackexchange.com provided excellent guidance to do this. You could do this as a service, or in a screen. I prefer screen. I tweaked the instructions a little. Here’s what I did.

First, install screen. Screen lets you run codes in multiple terminal window and toggle between them.

sudo apt-get update && sudo apt-get install screen -y

Then create a geth.sh script. 

sudo pico geth.sh

Here’s my geth.sh script.

#!/usr/bin/env bash
echo "Geth at work!"
screen -dmS geth geth --testnet --syncmode "fast" --rpc --rpcapi db,eth,net,web3,personal --cache=1024  --rpcport 8545 --rpcaddr 0.0.0.0 --rpccorsdomain "*"

Now make it executable.

sudo chmod +x geth.sh

Then run it.

bash geth.sh

You will see just the text “Geth at work!”. To toggle to this screen, attach to the screen with this command:

screen -x geth

You will see geth doing its syncing job. Just press CTRL+a followed by d to toggle back to where you came from when you are done watch. Just leave it syncing for as long as you wish, or until Ropsten decides to fork again.

Photo by Markus Spiske on Unsplash