Ethereum - How to create a private chain



  • By,


    hellspawn88

    Source:http://blog.csdn.net/weixin_40...

    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:

    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
    • Type the following command
            geth --datadir "E:\EthDBSpace\PrivChain" init "E:\EthDBSpace\genesis.json"1
    
    –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" console1
    
    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')1
    
    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

    这里写图片描述

    • We can check the account balance first.Type in the geth console:
            my=eth.accounts[0]
            eth.getBalance(my)1
    2
    
    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()1
    
    • 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()1
    
    • Let’s check our account balance again
            eth.getBalance(my)1
    
    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]1
    2
    
    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)1
    
    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})1
    
    ‘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)1
    2
    
    • 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()1
    
    • Wait a few seconds and stop mining
            miner.stop()1
    

    Check the two accounts again and find that the transfer has been completed! 

     这里写图片描述



Looks like your connection to Cryptocentral was lost, please wait while we try to reconnect.