[Ethereum Tutorial] How to create a private chain

Source: Internet
Author: User
Tags json pow
[Ethereum Tutorial] How to create a private chain Introduction:This tutorial describes what to build a Ethereum private chain for development experiments The purpose of creating a private chain:The purpose of building a private chain is to facilitate the experiment on the chain. An experimental development on a public chain can leads to unnecessary waste of money. Because you need gas to does anything on the Ethereum chain. For experimental development, we usually the use of our own private chain to do experiments. Environment Setup: Operating System: WINDOWS10
Ethereum Client: Geth 1.6.7
You can download it here:
Https://gethstore.blob.core.windows.net/builds/geth-windows-amd64-1.7.0-6c6c7b2a.exe How to Create a private chain: Create A creation Genesis file:

First we need to create a "Genesis" JSON configuration file that describes some parameters of the Genesis block. The following is the contents of the file:

    {
       "Coinbase": "0x0000000000000000000000000000000000000000",
       "config": {
              "Homesteadblock": 5
        },
       "difficulty": "0x20000",
       "Extradata": "0x",
       "Gaslimit": "0x2fefd8",
       "Mixhash": " 0x00000000000000000000000000000000000000647572616c65787365646c6578 ",
       " nonce ":" 0x0 ",
       " Parenthash ":" 0x0000000000000000000000000000000000000000000000000000000000000000 ",
       " timestamp ":" 0x00 ",
       " Alloc ": {
              "Dbdbdb2cbd23b783741e8d7fcf51e459b497e4a6":
              {
                  "balance": "100000000000000000000000000000"
              }
       }
    }

Copy the above code into a text file and rename it genesis. JSON Genesis. :

To prevent private chain data from conflict with the public chain, it's recommended to create your own private chain data Folder. On my computer in the E Disk I created a ethdbspace folder as the Ethereum workspace used in experiments, and created a Pr Ivchain folder as my first private chain Data folder.
In this case, the Genesis. JSON file is placed under the Ethdbspace folder for management convenience. Open the Windows command line
Type the following command

        Geth--datadir "E:\EthDBSpace\PrivChain" Init "E:\EthDBSpace\genesis.json"

–datadir option is used to specify the data directory of our private chain. On my computer is e:\ethdbspace\privchain*.
The init command is the Genesis command followed by our Genesis configuration file path. Press ENTER and the result is shown below

The Genesis of private chain is complete! Create Account:

In order to does experiments on the private chain, we also need to create our own account on the private chain. Type in the Windows command line

        Geth--datadir "E:\EthDBSpace\PrivChain" console

Since the blockchain has been successfully created, we don ' t need to specify the Genesis. Json file path again when we enter the client for the second time, but Directly-datadir indicates the private chain data Path.
The console command is used to start the command line of Geth.

When you press ENTER, the client is initialized for a period of time. After the command prompt appears, it indicates, which has entered the Geth console successfully.

Type in the Geth console

        Personal.newaccount (' Your password ')

The personal. NewAccount function is a used to create a account, where the parameter is the account password. Press ENTER and the result is shown below

After the account creation was successful, the created account address was displayed in a green font at the bottom of the CO Mmand. This address was the public key in the account We can check for the account balance first. Type in the Geth console:

        My=eth.accounts[0]
        eth.getbalance (my)

The purpose of the first sentence is to assign the address of the the account we just created to my variable. This simplifies the code. Eth. Accounts Array records all the account addresses on the machine. Since we created our account for the first time, there are only one account on the computer. So here we use ETH. Accounts [0] to extract the first account address. Eth. GetBalance function is used to get the account balance, and the parameter are account address. The My variable here records the address of the first account. The results is as follows:

You can see that there's no money on my account. It costs transfer funds, issue contracts and execute contracts in the Ethereum block chain. And money comes from mining, and now we need to make some money by mining. Mining:

Currently, Ethereum uses the POW (Proof of work) consensus mechanism. The consensus mechanism is used to motivate people to maintain block billing. The main method of this mechanism are to let the system create problems, so, the whole network node that wants to get t He billing right of the new block would be solved. The first node that solves the problem would acquire the billing rights of the new block and receive ETH as a reward. The nodes that is interested in billing is called miner nodes. Now we need-make some-money in our private chain by digging up mines so, we can follow up the transfer experiment. Type in the Geth console:

        Miner.start ()
When you press ENTER, you'll see the client start mining, and the command line would continuously display the progress of The production block.

Since It is a private chain, there are only one node, the mining, so there are no competition. So you can wait for a few seconds to stop mining. The amount of money made is enough to carry out follow-up experiments. Type the following command to stop mining

        Miner.stop ()
Let's check our account balance again
        Eth.getbalance (My)

At the this point we'll see a large sum of money in our balance, which was calculated by Wei. See the conversion table for Wei to eth http://www.ethdocs.org/en/latest/ether.html Transfer:

Below we'll attempt to transfer in our own private chain to create a second account. Type the following command on the Geth console

        Personal.newaccount (' 123 ')
        Other=eth.accounts[1]

Assign the second account address to other variable to facilitate subsequent coding we need to unlock your account before Transferring it. Type the following command on the Geth console

        Personal.unlockaccount (My)

Now ' my ' account has money, and ' other ' account has no money. Here we need to transfer ' my ' account money to the other account, so we need to unlock ' my ' account.

After pressing the ENTER key, the prompt needs to enter the password. After your type your password, you can unlock your account.

Start transferring money. Type the following command on the Geth console

        Eth.sendtransaction ({from:my, to:other, value:10000})

' From ' account if for the transfer, here we enter my variable that records the first account address
The "to" account are the target account, here we enter the ' other ' variable that records the address of the second account
' Value ' specifies the amount to being transferred, which is Wei. Here we transfer 10000wei

The results is as follows

You can see, the transfer request has been submitted

Let ' s check the balance of the accounts again

        Eth.getbalance (my)
        eth.getbalance (Other)
The check balance results is as follows:

At this point, a strange thing is found. The previous transfer request has been submitted. Why hasn ' t the balance of the accounts changed? In retrospect, Ethereum used the POW consensus to motivate the miners ' billing. Since we created the private chain, we only has one node at the moment, so no other nodes has been involved in the Bookk Eeping. So we need to does the mining ourselves to record this transfer into the block. So keep mining! Type in the Geth console

        Miner.start ()
Wait a few seconds and stop mining
        Miner.stop ()

Check the accounts again and find that the transfer have been completed!

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.