[Ethereum Tutorial] How to create a private chain

来源:互联网 发布:jsbrowser是什么软件 编辑:程序博客网 时间:2024/05/23 23:17

[Ethereum Tutorial] How to create a private chain


Introduction:

  • This tutorial describes how to build an 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 lead to unnecessary waste of money.Because you need gas to do anything on the ethereum chain.So for experimental development, we usually use 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 is 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 PrivChain 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 do 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 will be initialized for a period of time.After the command prompt appears, it indicates that you have entered the geth console successfully.
    这里写图片描述

  • Type in the geth console

        personal.newAccount('Your password')

The personal. NewAccount function is used to create an account, where the parameter is the account password.

  • Press enter and the result is shown below
    这里写图片描述

After the account creation is successful, the created account address is displayed in a green font at the bottom of the command.This address is the public key of the account

  • We can check 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 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 is 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 is account address.The my variable here records the address of the first account.

  • The results are as follows:
    这里写图片描述
    You can see that there’s no money in my account.It costs money to 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 is to let the system create problems, so that the whole network node that wants to get the billing right of the new block will be solved.The first node that solves the problem will acquire the billing rights of the new block and receive eth as a reward.The nodes that are interested in billing are called miner nodes.Now we need to make some money on our private chain by digging up mines so that we can follow up the transfer experiment.

  • Type in the geth console:
        miner.start()
  • When you press enter, you will see the client start mining, and the command line will continuously display the progress of the production block.
    这里写图片描述

Since it is a private chain, there is only one node that is mining, so there is no competition.So you can wait a few seconds to stop mining.The amount of money made at this point was 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 this point we will see a large sum of money in our balance, which is calculated by wei.See the conversion table for wei to eth http://www.ethdocs.org/en/latest/ether.html

Transfer:

Below we will attempt to transfer money on 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

  • You need to unlock your account before transferring it.Type the following command on the geth console
        personal.unlockAccount(my)

Now ‘my’ account have money, and ‘other’ account have no money. Here we need to transfer ‘my’ account money to other account, so we need to unlock ‘my’ account.

  • After pressing the enter key, the prompt needs to enter the password.After you 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
‘to’ account is the target account, here we enter the ‘other’ variable that records the address of the second account
‘value’ specifies the amount to be transferred, which is wei. Here we transfer 10000wei

  • The results are as follows
    这里写图片描述

    You can see that the transfer request has been submitted

  • Let’s check the balance of the two accounts again

        eth.getBalance(my)        eth.getBalance(other)
  • The check balance results are as follows:
    这里写图片描述

At this point, a strange thing is found. The previous transfer request has been submitted. Why hasn’t the balance of the two accounts changed?In retrospect, ethereum used the POW consensus to motivate the miners’ billing.Since we created the private chain, we only have one node at the moment, so no other nodes have been involved in the bookkeeping.So we need to do 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 two accounts again and find that the transfer has been completed!
这里写图片描述

原创粉丝点击