Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Ethereum Cookbook

You're reading from   Ethereum Cookbook Over 100 recipes covering Ethereum-based tokens, games, wallets, smart contracts, protocols, and Dapps

Arrow left icon
Product type Paperback
Published in Aug 2018
Publisher Packt
ISBN-13 9781789133998
Length 404 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Manoj P R Manoj P R
Author Profile Icon Manoj P R
Manoj P R
Arrow right icon
View More author details
Toc

Table of Contents (13) Chapters Close

Preface 1. Getting Started 2. Smart Contract Development FREE CHAPTER 3. Interacting with the Contract 4. The Truffle Suite 5. Tokens and ICOs 6. Games and DAOs 7. Advanced Solidity 8. Smart Contract Security 9. Design Decisions 10. Other Protocols and Applications 11. Miscellaneous 12. Other Books You May Enjoy

Creating your own private Ethereum network

This recipe helps you create your own private POW-based Ethereum blockchain. Here, we will be creating an entirely new custom blockchain that cannot interact with the Ethereum main-net. You will have the flexibility to control parameters such as mining and peers. This will come in handy when you want to have a consortium of your own or even for testing purposes.

Getting ready

To step through this recipe, you will need a working installation of the geth command-line interface. No other prerequisites are required.

How to do it...

  1. Create a directory to save your blockchain data:
$ mkdir datadir
  1. Create an account and save it to the directory:
$ geth account new --datadir datadir

You will be asked to enter a password for the account. An address will be displayed once the account is created. The key file for this account will be stored in the /datadir/keystore/ location.

  1. Create a genesis.json file with the following contents:
{ 
"config": {
"chainId": 1100,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0,
"byzantiumBlock": 0
},
"difficulty": "400",
"gasLimit": "2000000",
"alloc": {
"87db8fceb028cd4ded9d03f49b89124a1589cab0": {
"balance": "100000000000000000000000"
}
}
}

Various parameters in the genesis files are explained here. You can make changes as per your requirements:

  • config: The config object defines the settings for our custom blockchain. chainId is used to identify our network. Set it to a unique value for our private network. Other parameters are related to forking and versioning. Let’s not worry about them, since we are starting a network from scratch.
  • difficulty: This value is used to control the block generation time of a blockchain. The higher the difficulty, the longer it will take to mine each block. In a test network, try to keep it low to avoid long waiting times.
  • gasLimit: This value denotes the total amount of gas that can be used in each block. We will keep it high enough to avoid any bottlenecks during testing.
  • alloc: This object allows us to pre-fill accounts with Ether. This won’t create accounts for you. Since we already have an account created in step 2, use it here to allocate some wei (1 Ether = 1018 wei).
  1. Initialize your genesis file using the following command:
$ geth --datadir ./datadir init ./genesis.json
  1. Start your network:
$ geth --datadir ./datadir --networkid 1100 console 2>> network.log

Running this command will display a console like this in your terminal/command prompt:

You can use this console to interact with your private blockchain. To give you a brief idea, execute the following command to see the list of accounts:

> eth.accounts
  1. If you would like to add another peer to the network, open a second terminal/command prompt to create a second peer with a different data directory and port:
$ geth --datadir ./datadir2 init ./genesis.json
$ geth --datadir ./datadir2 --networkid 1100 --port 30302 console 2>> network.log
  1. Find out the enode address of the first node using the first JavaScript console:
> admin.nodeInfo.enode

This will return the enode address of the current node:

 enode://315d8f023dfa1ae1b59dc11462f3e13697fc8fe4886034e01530ebe36b2f8cc154a8dd9c21f5b42564668f22ae7173943b9dd9a0fbfc1430cca8c47196872914@[::]:30303
  1. Use the second node’s JavaScript console to connect with the first node:
> admin.addPeer( “enode://315d8f023dfa1ae1b59dc11462f3e13697fc8fe4886034e01530ebe36b2f8cc154a8dd9c21f5b42564668f22ae7173943b9dd9a0fbfc1430cca8c47196872914@127.0.0.1:30303”)

We replaced [::] with the IP address and port of the first node.

  1. Verify the connection from both nodes by listing the connected peers:
> admin.peers

How it works...

In steps 1 through 5, we created a private Ethereum network. This should be enough for you to run a single-node network. With the minimum difficulty and maximum gas limit, this network is ideal for development and testing. At this point, your application can connect to the network with the default Ethereum port, 8545.

In steps 6 to 9, we are creating another peer and connecting it to the first node. This allows you to have a multi-node Ethereum network. All the blocks will be synced between these two nodes and will be accessible from both nodes. You can create and connect as many nodes as you want with the procedure explained here. Make sure to set appropriate firewall rules to ensure proper communication.

There's more...

puppeth is a command-line tool that comes with geth. It can help you create a new Ethereum network, down to the genesis block, bootnodes, miners, and ethstats servers, in a user-friendly way. Start puppeth using the following command:

$ puppeth

You will be asked to enter a network name. You can either enter a new network name or use the one that you already created using puppeth. Once you enter it, you can see the list of things that puppeth can do.

Create a new genesis file using puppeth and use Proof of Authority (clique) as the consensus algorithm. Try creating a private network with the generated genesis file.

See also

This recipe explains how you can create a private network using geth. There are other options for various use cases, such as development and production. You can check out the following recipes to learn more:

  • Creating a blockchain network for development

  • Using Azure Ethereum as a service

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €18.99/month. Cancel anytime