Develop a smart contract on Ethereum [Part 1]: Sync a node

Hello, this is the first part of my tutorial series on developing a smart contract on the Ethereum network.

For starters, Ethereum is simply a blockchain network that facilitates the usage of Ether. Ether is a virtual cryptocurrency that runs on Ethereum, similar to Bitcoin. The difference between Ethereum and Bitcoin is that an Ethereum Virtual Machine (EVM) is built on top of the network. This allows developers to deploy smart contracts on Ethereum. Ether holders thus gain access to the usage of these smart contracts.

I will explain more about Ethereum in another article, for now, that is all you need to know.

About me

Personally, I have developed (on-and-off) on Ethereum since more than a year ago. Along the way, I have encountered many obstacles that wasted a lot of my time. I wish someone would just consolidate all these common obstacles and hold me by the hand when I am trying to familiarize myself with the environment. That’s why I am now writing this short series of guides to save you some time.

In this series, I will also note some consideration points and explain the reason why we do (or not do) certain things.

To get started, we want to sync an Ethereum node.

A blockchain network is made up of different nodes. In order to deploy a smart contract, we have to deploy it through a synced node*. However, that does not mean we have to sync a node ourselves. We can always deploy a contract by using a node synced by others. Still, we want to do so because it helps in our development process which you will see later.

*We can sync a node by downloading the blocks of data in the network.

Install Geth

To sync an Ethereum node, we will need the following software:

  • Geth — Client for Ethereum node
  • Ethereum Wallet — Act as a user interface for an Ethereum node.

To download Geth, go here for Windows users. Then click on the “Geth for Windows” button.

For Mac users, I recommend that you download using homebrew. You can do so with the following commands.

brew tap ethereum/ethereum
brew install ethereum

Refer to here for more details.

You can check that geth is installed properly by typing geth version in the terminal (MacOS) or PowerShell (Windows).

Install Ethereum Wallet

We call the GUI that interacts with the network “wallet”. You can find many different wallets for Ethereum with a quick Google search. I personally like using Ethereum Wallet which is developed by Ethereum itself.

You can find the installer here. Note that we want to install the Ethereum Wallet only, not Mist. Mist is basically a browser that uses the blockchain technology. It comprises of other decentralized applications (dApps) and websites. We are only interested in Ethereum Wallet only.

Mainnet vs Testnet

In Ethereum, there are two main networks — The mainnet and the testnet. The mainnet is used to transact real Ether. These Ether’s value is tied to real fiat currency value via cryptocurrency exchanges. As developers, we do not want to run tests with real money. That is what the testnet is for. The name for the testnet is also called Ropsten.

Run Geth and Ethereum Wallet

First, we want to sync the testnet first as that is what we will be using for our development. We will sync the mainnet when we are ready to deploy (next time).

You will need about 30Gb of storage space currently to sync a testnet. As there are more transactions in the mainnet, you will need about 100Gb to sync a mainnet.

Note that some users have said that you need an SSD storage for fast writing in order to sync the node properly. Personally, I find HDD storage to be alright.

For Windows Users

For easy access, I recommend you to create a folder to store the blockchain. For example, “C:\EthereumTestnet”.

After creating the folder, try running the following command.

geth --testnet --data-dir="C:\EthereumTestnet" --rpc --rpcapi eth,web3,net,personal

The argument --testnet specifies that we want to sync the Ropsten network. Thus, to sync the mainnet, you just need to take out the argument --testnet and change the data directory. For example:

geth --data-dir="C:\EthereumMainnet" --rpc --rpcapi eth,web3,net,personal

The --rpc argument enables the HTTP-RPC server which allows us to use some services such as what is stated in --rpcapi eth,web3,net,personal.

After entering the command, you should see something like this.

Geth starting up

In order not to corrupt the blockchain, especially after many hours of syncing, you should not close the command prompt abruptly. Always stop the sync by pressing Ctrl + C and wait until geth shuts down the program for you.

After pressing Ctrl + C, Geth shuts down.

However, it is difficult to remember all the command and rpc services you need. That’s why I suggest creating a shortcut or a batch file to help you. Simply create a file using any text editors you have and save it as .bat. For example, RunGethTestnet.bat. Copy and paste the command into the file and save it. The next time you need to run Geth for Ropsten, just double click on the file.

After running Geth, we have to run the Ethereum Wallet. As we want the interface to locate at the exact location where we synced our files, we are executing it from the console. Similar to Geth, I saved a batch file for it as well with an example command such as below.

If you saved Ethereum Wallet as your path, then you can simply replace it directly. For me, "C:\EthereumWallet\Ethereum Wallet.exe" is simply where I stored my Ethereum Wallet application. The argument --node-datadir="C:\EthereumTestnet" states where I store my chaindata which is the exact one I specified when I run Geth.

For Mac Users

MacOS is slightly simpler because the chaindata is downloaded automatically to the library and is accessible for us. You may nto want to specify the data directory like in Windows. Nevertheless, I recommend creating a script file to make running Geth and Ethereum Wallet easier.

Geth command for testnet
Command to run Ethereum Wallet

In order to run mainnet, you just need to remove the –testnet argument in the Geth command. The data directory for running Ethereum Wallet for mainnet will change as well into the following.

Running Ethereum Wallet for Mainnet

Note to users for both OS, Geth has to run before Ethereum Wallet. Running Ethereum Wallet alone will automatically start the syncing process as it will automatically run a Geth client in the background. This is user-friendly but does not enable the RPC services we want to use. Thus, we want to ensure RPC is enabled in both our Geth and Ethereum Wallet execution.

Take note

A few minor notes:

  1. The syncing process is very long and can take up to 2–3 days. Please have patience and consider leaving your computer turned on overnight.
  2. The syncing speed depends on your internet speed, peers count, writing speed of your storage drive.
  3. As the data are stored in blocks and linked together, corruption in one block can corrupt the whole chaindata. This can potentially waste your effort in waiting days for the node to sync. Therefore, it is very important to shut down your geth properly.
  4. The progress bar on your Ethereum Wallet is NOT accurate. Relying on it will give you a lot of anxiety and stress.
  5. Geth runs on port 30303 for external listening.
  6. The default port used for internal communication, e.g. between your wallet and geth, is 8545.

Ensure port to Geth client is opened

One major thing to take note is to ensure the connection to your Geth client is not limited. One huge headache I encountered is to let my firewall limit the number of connections I can have through the Geth client. You can spot this problem by looking at the peers count. If it stays consistently low at about 1–3 peers, there is a good chance your connection is limited. A healthy range is above 5 peers.

Refer to this article to prevent firewall from limiting your connections.

Attach to Geth

To retrieve more information about your node, you can attach to the geth client and execute commands with it using the RPC services. Here’s a simple way to check your syncing status. Attach to the client by entering the following command on a separate terminal/console.

geth attach

Again, you might want to store it in a batch/script file for your own convenience. After running the command, you should see something like this.

Type web3.eth and you should see a lot of information. To retrieve specific information on syncing, type web3.eth.syncing instead.

As you can see, the progress bar found at the top of the Ethereum Wallet is merely a comparison between the highestBlock and the currentBlock. As the highestBlock known to your computer might not be the actual highest block, the progress bar might not reflect the real progress. In fact, the highestBlock and knownStates will continue to increase as you sync your nodes.

Some final points

As the technology is rapidly developing, always use the stable version of the client and wallet.

Also, note that there is always a workaround for any of the issues you face. When you face a problem syncing, you can try to search for solutions as you are likely not the first one to face the same problem. And always remind yourself that you do not need to sync a node to develop a smart contract. Doing so only helps to facilitate your understanding of the environment as well as your development process. So, don’t beat up yourself over it if you encounter problems in this first step.

Move on to Part 2 of the tutorial series: Setting up the environment.

Originally published at on June 12, 2018.

CEO and founder of… Just kidding. I am an individual developer, obsessed with humans, and has a profound love for fried food.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store