How to build the Ethereum private chain

Source: Internet
Author: User
Tags json

"Hui Xin Yun" For everyone to update the article regularly, "Hui new cloud" It People's industrial chain platform

In the development of ethereum, many times need to build an ethereum private chain, through this article to see how to build on the Mac. write in front

Before reading this article, you should have an understanding of the Ethereum language, and if you do not understand it, it is recommended that you look at Ethereum first. go-ethereum Client Installation

The Go-ethereum client is often referred to as Geth, which is a command-line interface that performs the complete Ethereum node implemented on go. Thanks to the multi-platform features of the Go language, Geth is supported on multiple platforms (e.g. Windows, Linux, MAC). Geth is a concrete implementation of the Ethereum protocol, through Geth, you can realize the various functions of ethereum, such as the new edit delete account, open mining, ether currency transfer, intelligent contract deployment and implementation and so on. So, we chose the Geth tool for development. Since I am a Mac, I prefer to use my Mac for development. The Geth installation in Mac is as follows:

1
2
Brew tap Ethereum/ethereum
Brew Install Ethereum

Check if the installation is successful

1
Geth--help

If you output some help prompt commands, the installation is successful.
Other platforms can refer to Geth installation to build a private chain

Ethereum supports custom creation blocks, to run a private chain, we need to define our own creation block, and the creation block information is written in a JSON-formatted configuration file. First save the following to a JSON file, such as Genesis.json.
The contents of the JSON file are as follows:

1 2 3 4 5 6 7 8 9 ten-in-one, 
{"config": {"Chainid": Ten, "Homesteadblock": 0, "Eip155block": 0, "Eip158block": 0 }, "Alloc": {}, "Coinbase": "0x0000000000000000000000000000000000000000", "difficulty": "0x20000", "E Xtradata ":", "Gaslimit": "0x2fefd8", "nonce": "0x0000000000000042", "Mixhash": "0x00000000000000000 00000000000000000000000000000000000000000000000 "," Parenthash ":" 0x0000000000000000000000000000000000000000000000000000000000000000 "," timestamp ":" 0x00 "} 
Initialize: Write to Genesis block

Once you have prepared the creation block JSON configuration file, you need to initialize the blockchain and write the creation block information above into the blockchain. The first thing to do is to create a new directory DATA0 to store blockchain data (in fact, this directory data0 is the same as a root node.) When we generate the root node based on the Genesis.json, others can connect to the root node to make the transaction. The DATA0 directory structure is shown in the figure:

Next go to the Privatechain directory and execute the initialization command:

1
2
CD Privatechain
geth--datadir data0 init Genesis.json

The main body of the above command is Geth init, which indicates the initialization of the blockchain, the command can have options and parameters, where the –datadir option followed by a directory name, here is DATA0, indicating that the specified data directory is DATA0, Genesis.json is the parameters of the init command.

Running the above command reads the Genesis.json file and writes the creation block to the blockchain based on its contents. If you see the successfully wrote Genesis State Word in the log message, the initialization succeeds.

The directory after initialization succeeds is as follows:

Where Geth/chaindata is stored in block data, KeyStore is the account data. Start a private chain node

Once the initialization is complete, you have a private chain of your own, and then you can start your own private chain node and do some work by entering the following command in the terminal to start the node:

1
Geth--datadir data0--networkid 1108 Console

The main body of the above command is the Geth console, which indicates the boot node and enters the interactive console, the –DATADIR option specifies to use DATA0 as the data directory, and the –networkid option followed by a number, here is 1108, which indicates that the network ID of this private chain is 1108. Network ID will be used when connecting to other nodes, the network ID of the Ethereum public network is 1, in order not to conflict with the public chain network, when running the private chain node to specify its own network ID (the above command may fail, I restart the Mac directly, and then into the Privatechain directory, simple Rude )。

After running the above command, the Blockchain node is launched and the JavaScript Console is entered:

This is an interactive JavaScript execution environment where you can execute JavaScript code, where > is a command prompt. In this environment, there are also built-in JavaScript objects for manipulating Ethereum, which can be used directly. These objects mainly include:

ETH: contains some methods related to the operation of blockchain
Net: contains the following methods to view the state of peer network
admin: contains some methods related to management node
Miner: includes some methods to start & stop mining
Personal: mainly includes some ways to manage accounts
Txpool: contains some ways to view the pool of trading memory
web3: contains the above objects, also contains some unit conversion methods to play the JavaScript Console

After entering the Ethereum JavaScript console, you can use the built-in objects inside to do things that provide rich functionality such as viewing chunks and trades, creating accounts, mining, sending deals, deploying smart contracts, and more. Here are a few common features that are described in the following actions, preceded by a > that represent commands executed in JavaScript console. Create an account

The front just built a private chain, and does not have its own account, you can enter the JS console eth.accounts to verify:

1
2
> eth.accounts
[]

There is no account at this time, then use the Personal object to create an account:

1
2
3
4
> Personal.newaccount ()
> Passphrase:
> Repeat Passphrase:
" 0x4a3b0216e1644c1bbabda527a6da7fc5d178b58f "

Passphrase is actually the meaning of the password, entered two times the password, created an account. Execute the command again:

1
2
3
4
> Personal.newaccount ()
> Passphrase:
> Repeat Passphrase:
" 0x46b24d04105551498587e3c6ce2c3341d5988938 "

At this time again to see the account, there are two.

1
2
> eth.accounts
["0x4a3b0216e1644c1bbabda527a6da7fc5d178b58f", "0x46b24d04105551498587e3c6ce2c3341d5988938 "]

The account is saved in the KeyStore folder in the data directory by default. Look at the directory structure, found that the Data0/keystore more than two files, the two files corresponding to the two accounts just created, this is a JSON-formatted text file, you can open the view, which is stored in the private key after the password encryption information.


The format of the information in the JSON file is as follows:

1 2 3 4 5 6 7 8 9-All of the ten, 
{
  "address": "4a3b0216e1644c1bbabda527a6da7fc5d178b58f",
  "crypto": {
    "cipher": "Aes-128-ctr",
    " Ciphertext ":" 238d6d48126b762c8f13e84622b1bbb7713f7244c2f24555c99b76396fae8355 ",
    " Cipherparams ": {
      " IV ": "d0f5a3d3e6c1eeec77bf631bc938725d"
    },
    "KDF": "Scrypt",
    "Kdfparams": {
      "Dklen": +,
      "n": 262144,
      "P": 1,
      "R": 8,
      "Salt": "70dc72c4eb63bea50f7637d9ff85bb53f6ca8ace17f4245feae9c0bc9abaad82"
    },
    "Mac": "Bd7fc0c937c39f1cbbf1ca654c33b53d7f9c644c6dacfeefe1641d2f3decea04"
  },
  "id": " 57803d82-0cd4-4a78-9c29-9f9252fdcf60 ",
  " version ": 3
}
View account Balances

The ETH object provides a way to view the balance of the account:

1
2
3
4
> eth.getbalance (eth.accounts[0])
0
> Eth.getbalance (eth.accounts[1])
0

Currently two accounts have 0 of the etheric currency balance, to make the balance of the account, you can transfer from other accounts, or by mining to obtain the Ethereum award. Start & Stop Mining

To start mining by Miner.start ():

1
> Miner.start (10)

Where start parameters indicate the number of threads to use for mining. For the first time, Mr. Mining will be the Dag file needed to dig the mine, this process is a bit slow, and when the progress reaches 100%, it will start digging, and the screen will be mining information brush screen.

If you want to stop digging and the progress has reached 100%, you can enter it in JS console

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.