© Wei-Meng Lee 2019
W.-M. LeeBeginning Ethereum Smart Contracts Programminghttps://doi.org/10.1007/978-1-4842-5086-0_3

3. Connecting to the Ethereum Blockchain

Wei-Meng Lee1 
(1)
Ang Mo Kio, Singapore
 
Now that you have a solid understanding of how a blockchain works, you are ready to connect to the real Ethereum blockchain. To get connected to the Ethereum blockchain, you need an Ethereum client – an application that runs as an Ethereum node on the blockchain. Using an Ethereum client, you can perform tasks such as the following:
  • Mine Ethers.

  • Transfer Ethers from one account to another.

  • View block information.

  • Create and deploy Smart Contracts.

  • Use and interact with Smart Contracts.

At the time of writing, there are a number of Ethereum clients that you can use to interact with the Ethereum blockchain. They are
  • Eth – A C++ Ethereum client

  • Geth – The official Ethereum client implemented using the Go programming language

  • Pyethapp – A Python Ethereum client

  • Parity – An Ethereum client written using the Rust programming language

The preceding list are all CLI (command line interface) clients. If you want to use a GUI client, you can use Mist, which is built on top of Geth. For this book, we shall focus on the use of Geth.

Downloading and Installing Geth

Geth is available for three main paltforms:
  • Linux

  • macOS

  • Windows

Also, the sources of Geth is also available for download at https://geth.ethereum.org/downloads/ if you want to learn how Geth is implemented. In the following sections, I will show you how to download and install Geth for the three main platforms.

Installing Geth for macOS

There are two ways to install Geth for macOS. The first is through the command line.

To do that, you need to use Brew.

Tip

Homebrew (commonly known as Brew) is a free and open-source software package management system that simplifies the installation of software on Apple’s macOS operating system and Linux.

If you do not have Brew, you need to install it first. To do so, in Terminal, type the following command to install Brew:
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Then type the following commands to update and upgrade Brew:
$ brew update
$ brew upgrade
To install Geth in Terminal, type the following commands in Terminal:
$ brew tap ethereum/Ethereum
$ brew install ethereum

Tip

To upgrade Geth to the latest version, use the following command: brew upgrade Ethereum. To find out the current version of Geth you have installed on your computer, use the command: geth help.

If you do not want to install Geth from Terminal, the second way to install Geth is to go to https://geth.ethereum.org/downloads/ and download Geth for macOS (see Figure 3-1).
../images/471881_1_En_3_Chapter/471881_1_En_3_Fig1_HTML.png
Figure 3-1

Downloading Geth from its web site

Once downloaded, unzip the file and move the Geth file onto your home directory.

Installing Geth for Windows

For Windows user, the easiest way to install Geth is to go to https://geth.ethereum.org/downloads/ and click the button for the Windows version of Geth.

Once downloaded, double-click the .exe to install Geth on your Windows machine.

Tip

For Windows users, if you encounter problems in running any of the commands in this book, you should use the PowerShell instead of the Command Prompt.

Installing Geth for Linux

For Linux user, you can install Geth by downloading the version of Geth for Linux from https://geth.ethereum.org/downloads/ , and unzip the file and move the geth file onto your home directory.

Alternatively, you can also install Geth through Terminal. In Terminal, type the following commands:
$ sudo apt-get install software-properties-common
$ sudo add-apt-repository -y ppa:ethereum/ethereum
$ sudo apt-get update
$ sudo apt-get install ethereum

And Geth would now be installed.

Getting Started with Geth

Using Geth, you can connect to the Ethereum blockchain and interact with it. Let’s see how this can be done.

In Terminal, type the following command:
$ geth --testnet --datadir ~/.ethereum-testnet

Note

All the commands and examples in this book will be based on macOS.

Here is what the preceding command means:
  • The geth client will attempt to connect to the Ropsten test network (--testnet) and download the entire blockchain from the network.
    • If you want to connect to another test network called Rinkeby , use the --rinkeby option.

  • The --datadir option specifies the local storage to use to store the blockchain, keystore, and other local client data (in this example, it is stored in a hidden folder named .ethereum-testnet in your home directory).

The entire blockchain for the network will now be downloaded onto your computer; in this case it will be saved in the ~/.ethereum-testnet directory.

Ethereum Networks

Apart from the Main Ethereum Network (known as mainnet), Ethereum has a number of test networks (known as testnets) that you can use for development purposes. The beauty of the testnets is that the Ether on these networks has no real monetary value, and hence you can test and explore the Ethereum blockchain to your hearts content. Only when you are finally done with your testing (and learning) do you need to connect to the mainnet for real-life deployment. At the time of writing, there are four testnets:
  • Ropsten

  • Kovan

  • Rinkeby

  • Goerli

Examining the Data Downloaded

The Geth is going to take some time to download the entire blockchain onto your computer (up to a few days depending on your connection speed). In the meantime, you can take a look at the folders created by Geth in the ~/.ethereum-testnet directory. You should see two folders as shown in Figure 3-2.
../images/471881_1_En_3_Chapter/471881_1_En_3_Fig2_HTML.jpg
Figure 3-2

Examining the folders created by Geth

The geth folder contains the blockchain that you are downloading, while the keystore folder contains the account details of your local Ethereum node. We will discuss these in more details in the next chapter.

Tip

It will take Geth quite a bit of time to synchronize all the data to the computer. At the time of writing (June 2019), the size of the Ethereum mainnet is about 250GB. So, if you are synchronizing with the mainnet, make sure you have enough disk space. You can check for the size of the various cryptocurrencies blockchain size at https://bitinfocharts.com .

Geth JavaScript Console

Observe that when Geth is running, all you see is text after text scrolling (these are the statuses of the synchronization). It would be more useful if you are able to interact with Geth directly. And this is possible through the Geth JavaScript Console.

To use the Geth JavaScript Console, add the following option in bold to the Geth command in the Terminal:
$ geth --testnet --datadir ~/.ethereum-testnet console 2>console.log
You should now see a prompt (>) after issuing this command:
$ geth --testnet --datadir ~/.ethereum-testnet console 2>console.log
Welcome to the Geth JavaScript console!
instance: Geth/v1.8.27-stable/darwin-amd64/go1.12.4
 modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0
>
The Command Prompt allows you to issue JavaScript commands (hence its name), such as personal.newAccount(), which allows you to create a new account in the current node:
> personal.newAccount()
Passphrase: <password>
Repeat passphrase: <password>
"0x9ba59863c7d2e62c6f63baefb45a96b61bcb03ee"

Note

We will discuss more Geth commands in the next chapter.

If you want to run Geth in a separate window without the Geth JavaScript Console but at the same time want to interact with Geth, you can run Geth with the following options:
$ geth --testnet --datadir ~/.ethereum-testnet --rpc --rpcport 8545

The --rpc option enables the HTTP_RPC server, and the --rpcport 8545 option means that the HTTP_RPC server is listening at port 8545 to allow other clients to connect to it.

In another Terminal window, you can connect to the Geth node using the attach option:
$ geth attach http://127.0.0.1:8545
Once connected, you will see the following:
Welcome to the Geth JavaScript console!
instance: Geth/v1.8.22-stable/darwin-amd64/go1.11.5
 modules: eth:1.0 net:1.0 rpc:1.0 web3:1.0
>
To exit the Geth JavaScript Console, use the exit command:
> exit

Sync Modes

Geth supports three sync modes:
  • Full node mode (with fast synchronization) – When you launch the Geth client without specifying the --syncmode option, the default mode used by Geth is Full Nodes with Fast Synchronization (--syncmode fast). In Full node mode, Geth will download the full blockchain onto your computer by first downloading the block headers first and filling in block bodies and receipts afterward. Once the fast sync reached the last block of the Ethereum network, it switches to a full sync mode (see next point). It also validates the latest.

  • Full node mode – Synchronizes a full node starting at the genesis block and verifying all blocks and executing all transactions. This mode is slower than the fast sync mode but comes with increased security.

  • Light node mode – A light node only downloads the header chain and requests everything else on-demand from the network. They can verify the validity of the data against the state roots in the block headers. To use the light node mode, use the --syncmode light option.

When performing a sync using Geth, you can know the state of the synchronization using the eth.syncing property:
> web3.eth.syncing
{
  currentBlock: 5529791,
  highestBlock: 5653493,
  knownStates: 0,
  pulledStates: 0,
  startingBlock: 5308415
}

The preceding output indicates that the syncing starts from block number 5308415 and the current block number is 5529791. The highest block number is 5653493. If you get a false from the eth.syncing property, this means that syncing is not performed at the moment.

At any point in time, you can always check the latest block number that has been synchronized on your local node using the eth.blockNumber property, like this:
> eth.blockNumber
5653597

Summary

In this chapter, you learned how to download and install Geth, an Ethereum client, on the various platforms. In the next chapter, you will learn how to use Geth to set up a private test network so that you can use it to build your own blockchain network.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.225.21