Book Image

Ethereum Cookbook

By : Manoj P R
Book Image

Ethereum Cookbook

By: Manoj P R

Overview of this book

Ethereum and Blockchain will change the way software is built for business transactions. Most industries have been looking to leverage these new technologies to gain efficiencies and create new business models and opportunities. The Ethereum Cookbook covers various solutions such as setting up Ethereum, writing smart contracts, and creating tokens, among others. You’ll learn about the security vulnerabilities, along with other protocols of Ethereum. Once you have understood the basics, you’ll move on to exploring various design decisions and tips to make your application scalable and secure. In addition to this, you’ll work with various Ethereum packages such as Truffle, Web3, and Ganache. By the end of this book, you’ll have comprehensively grasped the Ethereum principles and ecosystem.
Table of Contents (13 chapters)

Setting up a node and participating in a network

Here, you will learn how to set up a node using the geth command-line tool. You will also see how to connect to a public network and perform operations such as ledger syncing and mining.

Getting ready

You will need a working installation of the geth command-line interface. You can also start a node with parity or any other Ethereum protocol implementation, but the steps may differ for each client.

Commands starting with $ have to be run on the command prompt/terminal and those starting with > will work only on the web3 JavaScript console.

How to do it...

  1. Verify your installation by running the following version command:
$ geth version
  1. Start your node with the following command:
$ geth

This will start an Ethereum node and will connect with the main network. As soon as it finds any peer nodes, it will start downloading the blocks from them.

You can configure the parameters before starting a node. This will help you do things like connect to a different network, expose APIs, and much more. Let's look at a sample initialization and the parameters used in it:

$ geth --networkid 3 --datadir "./ropsten-db" --keystore "./ropsten-keys" --syncmode "fast" --rpc --rpcport "8546" --rpcapi "web3,eth,miner,admin" --rpccorsdomain "*" --port 30301 console

Let's look into each parameter in detail:

  • --networkid <id>: A parameter to identify each network. You can either connect to the main/test network (1=Frontier(default), 2=Morden (disused), 3=Ropsten(PoW), 4=Rinkeby(PoA)) or any private network that you have set up.
  • --datadir <path>: Directory path for storing the blockchain database and keystore. You can change the default keystore directory with the --keystore parameter.
  • --syncmode <mode>: A parameter to specify the type of sync method. You can choose fast, full, or light, based on your needs.
  • --rpc: Enables an RPC server through HTTP. You can also change parameters such as --rpcaddr, --rpcport, and --rpcapi.
  • --rpccorsdomain <list>: Domains from which cross-domain requests are accepted. Use * as a wildcard or specify domains as a comma-separated list.
  • --port <port>: Changes the default network listening port (30303).
  • --console: Starts the web3 JavaScript console.
If you get stuck anywhere, you can always make use of the inbuilt help interface. Just enter geth help and it will return a comprehensive list of all commands with their respective description that you can run using geth.
  1. It might take a few minutes to identify the peers that are already on the network. Run the following command to return the list of peers that are currently connected to your node. Your node will start syncing once it finds at least one peer:
> admin.peers
  1. Check the current syncing status by running the following command. It will return false if not syncing:
> eth.syncing
  1. Run the geth attach command if you would like to connect to this node from a different console. You can also explicitly specify the host and the port. In our case, it is localhost and 8546:
$ geth attach http://localhost:8546
Your firewall may restrict the node from communicating with an external peer. This can cause issues with the synchronization process. Also, ensure that you are not exposing your RPC APIs to the internet, which can result in attacks.