Blockchain Development (I) Building a Private Chain Environment Based on Ethereum

Blockchain Development (I) Building a Private Chain Environment Based on Ethereum

Blockchain Development (I) Building a Private Chain Environment Based on Ethereum

Through the methods described in this article and the scripts in the project, we can quickly build our own private chain for blockchain development and testing. This article is based on Ethereum technology and is divided into two parts, one is the construction method under Ubuntu, and the other is the construction method under Windwos.

1. Install Geth client in Ubuntu

The reason for using Ubuntu is that Ethereum officially supports Ubuntu very well and it is the easiest to install in various Linux systems.

Please refer to the Geth official installation guide

Enter the Ubuntu command line and execute the following command

————————————————————————————————–

sudo apt-get update

sudo apt-get installsoftware-properties-common

sudo add-apt-repository -yppa:ethereum/ethereum

sudo add-apt-repository -yppa:ethereum/ethereum-dev

sudo apt-get update

sudo apt-get install ethereum

——————————————————————————————

After the system is connected to the Internet, the Ethereum client is installed, including geth, bootnode, evm, disasm, rlpdump, and ethtest.

If you enter the Geth command at this time, the Ethereum startup screen will appear

2. Install the Geth client under Windows

Windows must be a 64-bit system. Download the compiled win64 client from the official website, unzip it and run it. The download address is as follows:

https://github.com/ethereum/go-ethereum/releases/

After downloading, there is only one Geth.exe file.

To install the graphical client Mist, you can still download the compiled client from the official address. Download address:

https://github.com/ethereum/mist/releases/

After downloading and decompressing, Ethereum-Wallet is the Ethereum graphical interface.

3. Prepare the Genesis Block File

Configuring your own genesis block is to distinguish the public chain. In the same network, the genesis blocks must be the same, otherwise they cannot be connected. This method is common under Windows and Ubuntu.

Create a new file piccgenesis.json, enter the following content and save it

——————————————————————————————

{

“nonce”:”0×0000000000000042″,

“mixhash”:”0×000000000000000000000000000000000000000000000000000000000000000″,

“difficulty”: “0×4000″,

"alloc": {},

“coinbase”:”0×000000000000000000000000000000000000000″,

“timestamp”: “0×00″,

“parentHash”:”0×000000000000000000000000000000000000000000000000000000000000000″,

"extraData": "PICC GenesisBlock",

"gasLimit":"0xffffffff"

}

——————————————————————————————

Explain the function of each parameter:

mixhash

Used in conjunction with nonce for mining, a hash generated from part of the previous block. Note that it and the nonce must meet the conditions described in Ethereum’s Yellow paper, 4.3.4. Block Header Validity, (44).
nonce Nonce is a 64-bit random number used for mining. Note that it and the mixhash setting must meet the conditions described in Ethereum's Yellow paper, 4.3.4. Block Header Validity, (44).
difficulty Set the difficulty of the current block. If the difficulty is too high, CPU mining will be difficult. Set a lower difficulty here.
alloc Used to preset the account and the amount of ether in the account. Because private chain mining is relatively easy, we do not need to preset an account with coins. You can create it yourself when needed.
coinbase Miner's account, fill in casually
timestamp Set the timestamp of the genesis block
parentHash The hash value of the previous block. Since it is the genesis block, this value is 0
extraData Additional information, fill in as you like, you can fill in your personal information
gasLimit This value sets the total consumption limit of GAS, which is used to limit the total transaction information that a block can contain. Since we are a private chain, we fill in the maximum value.


4. Start the private chain node

Starting Geth can start the Ethereum blockchain. In order to build a private chain, some parameters need to be added when starting Geth. The meanings of Geth parameters are as follows:

identity

The blockchain logo, fill in as you like, used to indicate the name of the current network
init Specify the location of the genesis block file and create the initial block
datadir Set the location where the current blockchain network data is stored
port Network listening port
rpc Start rpc communication to deploy and debug smart contracts
rpcapi Set the rpc client that is allowed to connect, usually db, eth, net, web3
networkid Set the network ID of the current blockchain, which is used to distinguish different networks. It is a number
console Start the command line mode and execute commands in Geth

1. Start the blockchain node in Ubuntu

In Ubuntu, first switch to the directory you want to run. There should be a configured piccgenesis.json file in the directory. Execute the following command

——————————————————————————————

basepath=$(cd `dirname $0`; pwd)

Get the current directory

geth –datadir “$basepath/chain” init piccgenesis.json

Create data storage address and initialize genesis block

geth –identity”PICCetherum” –rpc –rpccorsdomain “*” –datadir “$basepath/chain” –port “30303″ –rpcapi “db,eth,net,web3″ –networkid 95518 console

——————————————————————————————

After startup, the interface is as follows. The cursor stays on the last command line and the Ethereum command can be executed.

——————————————————————————————

I0707 00:45:43.680087 ethdb/database.go:82]Alloted 128MB cache and 1024 file handles to /home/lihe/Desktop/chain/chaindata

I0707 00:45:43.726008ethdb/database.go:169] closed db:/home/lihe/desktop/chain/chaindata

I0707 00:45:43.728913 ethdb/database.go:82]Alloted 128MB cache and 1024 file handles to /home/lihe/Desktop/chain/chaindata

I0707 00:45:43.908795 ethdb/database.go:82]Alloted 16MB cache and 16 file handles to /home/lihe/Desktop/chain/dapp

I0707 00:45:43.969506 core/genesis.go:92]Genesis block already in chain. Writing canonical number

I0707 00:45:43.980337 eth/backend.go:274]Successfully wrote custom genesis block:6e92f8b23bcdfdf34dc813cfaf1d84b71beac80530506b5d63a2df10fe23a660

I0707 00:45:43.980618 eth/backend.go:184]Protocol Versions: [63 62], Network Id: 95518

I0707 00:45:43.981567core/blockchain.go:204] Last header: #81 [6193c4b0…] TD=10836704

I0707 00:45:43.981645core/blockchain.go:205] Last block: #81 [6193c4b0…] TD=10836704

I0707 00:45:43.981677core/blockchain.go:206] Fast block: #81 [6193c4b0…] TD=10836704

I0707 00:45:43.985253 p2p/server.go:313]Starting Server

I0707 00:45:45.834488p2p/discover/udp.go:217] Listening,enode://134881790e54c803955715e3661c27f91caaf499be813e29c9f986e2eac6 2d47e02b13a8e51776c1caea554655614ed26ce0185d84e626da7ac48a83a60113ff@[::]:30303

I0707 00:45:45.835853 node/node.go:366]HTTP endpoint opened: http://localhost:8545

I0707 00:45:45.848008 p2p/server.go:556]Listening on [::]:30303

I0707 00:45:45.849731 node/node.go:296] IPCendpoint opened: /home/lihe/Desktop/chain/geth.ipc

Welcome to the Geth JavaScript console!

instance:Geth/v1.5.0-unstable/linux/go1.5.1/PICCetherum

coinbase:0x93509a2f4b2b974b07ef0b52e07c3992601f5de1

at block: 81 (Tue, 05 Jul 2016 21:02:25CST)

datadir: /home/lihe/Desktop/chain

modules: admin:1.0 debug:1.0 eth:1.0 miner:1.0net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

>

——————————————————————————————

You can see the prompts Listening on [::]:30303 and Welcome to the Geth JavaScript console!, indicating that it has been successfully started.

Note: If you want to use Ubuntu as a permanent blockchain node, when using the nohup command, the Geth startup parameter console must be removed, otherwise Geth will stop automatically.

2. Start the blockchain node in Windows

Enter the directory of Geth under Windows, place the configured piccgenesis.json file, and execute the following command:

——————————————————————————————

geth –datadir “%cd%\chain” init piccgenesis.json

Create data storage address and initialize genesis block

geth–identity “PICCetherum” –rpc–rpccorsdomain “*” –datadir “%cd%\chain” –port”30303″ –rpcapi”db,eth,net,web3″ –networkid 95518 console

——————————————————————————————

When you see the prompts Listening on [::]:30303 and Welcome to the Geth JavaScript console!, it means that it has been successfully started.

5. Create an account using a node

After successfully starting the node, you will enter the Geth command line mode and enter the following command

——————————————————————————————

personal.newAccount()

——————————————————————————————

The system will prompt you to enter your account and password and confirm, and finally a newly generated account will be displayed.

6. Start the private chain graph node under Windows

First, follow the above steps to start Geth and create an account, then decompress Ethereum-Wallet and run Ethereum-Wallet.exe. The startup is successful. If the blockchain is normal, "PRIVATE-NET" will be displayed in the upper right corner. Click "LAUNCH APPLICATION" to enter the graphical interface.

7. Connecting to other nodes

First, you need to know your node information. Enter the command in the Geth command line interface, paying attention to uppercase and lowercase letters.

——————————————————————————————

admin.nodeInfo

——————————————————————————————

The system will display

enode:”enode://1e3c1727cd3bee9f25edeb5dbb3b880e03e41f8eec99566557f3ee0422734a8f cad17c161aa93d61bdbfb28ed152c143c7eb501db58bc63502a104a84b62d742@0.0.0.0:30303"

in

enode://1e3c1727cd3bee9f25edeb5dbb3b880e03e41f8eec99566557f3ee0422734a8fcad 17c161aa93d61bdbfb28ed152c143c7eb501db58bc63502a104a84b62d742@0.0.0.0:30303

This is the information of your own node. Please note that you should replace "0.0.0.0" with your own IP address. Send this information to other nodes and enter the following command in the command line of other nodes:

——————————————————————————————

admin.addPeer('enode://1e3c1727cd3bee9f25edeb5dbb3b880e03e41f8eec99566557f3ee0422734a8 fcad17c161aa93d61bdbfb28ed152c143c7eb501db58bc63502a104a84b62d742@192.168.1.101:30303')

——————————————————————————————

If the addition is successful, entering admin.peers will display the newly added node.

8. Using nodes for mining

In the Geth command line interface, enter miner.start() to start mining. After mining, the screen will refresh continuously. Enter miner.stop() to stop. Don’t worry about the incomplete command caused by refreshing the screen, the command will be executed normally.

At this point, a private chain network has been established, and this network can be continuously expanded like other blockchains. In the next article, I will introduce how to deploy and run smart contracts on a private chain.

Reference articles:

1. http://tech.lab.carl.pro/kb/ethereum/testnet_setup

2. http://www.ethdocs.org/en/latest/network/test-networks.html#setting-up-a-local-private-testnet

3. https://github.com/ethereum/go-ethereum/wiki/Connecting-to-the-network

4. https://github.com/ethereum/go-ethereum/wiki/JavaScript-Console

5. https://github.com/ethereum/go-ethereum/wiki/Mining

6. https://github.com/ethereum/go-ethereum/wiki/Managing-your-accounts

7. https://github.com/janx/ethereum-bootstrap


<<:  Blockchain-powered social network challenges Facebook and Twitter

>>:  Li Lihui: Blockchain solves the trust problem through blood transfusion

Recommend

Bad eyebrows face analysis

1. Broom eyebrow In physiognomy, if a person'...

How to read palmistry? It's actually very simple

Palmistry is a profound subject, so fortune teller...

Ten DeFi protocols on Ethereum have locked more than $1 billion

As of 10:00 today, the total locked value of DeFi...

What kind of personality do women with dense teeth have?

The neatness and whiteness of teeth not only affe...

What kind of palm lines indicate a man is rich?

Money is what everyone pursues, and everyone hope...

Use 8 questions to test whether you are a real Bitcoiner

At the end of 2008, the world's monetary syst...

What does a mole on the chest represent?

As the saying goes: People with great ambitions, ...

What kind of face is the most sincere?

In life, some people are willing to treat everyon...

Are people with overbites easily deceived? Are they emotionally smart?

Feelings are actually resources, but their import...