Teach you to build the Ethereum private chain

Source: Internet
Author: User
Tags hash json
First, prepare the environment for this tutorial: Windows 7 64bits installation Geth:https://ethfans.org/wikis/ethereum-geth-mirror
Ii. construction Process 1. Start the private chain, create the account cmd into the Geth installation directory Geth--datadir chain1--nodiscover console into Geth console mode, where Chain1 is the account and chunk data directory
The eth.accounts command looks at the existing account and is currently empty.
Two accounts are created below, 1234 of which are passwords:
> Personal.newaccount ("1234")
"0xc9228294cc6bc3e3fcdba0f5d393d68f920c7789"
> Personal.newaccount (" 1234 ")
" 0x5f0880e6c3507f609548732151a99f6ddc71cb95 "

The eth.getbalance command can be used to obtain an account balance, which is currently 0 of the balance between the two accounts.
2. Create a new Gensis.json file in the Geth directory with the following file contents:
{"
   alloc": {"
      0xc9228294cc6bc3e3fcdba0f5d393d68f920c7789": {
      "balance": "999000000000000000000"
      }
   },
    "config": {
        "Chainid": Ten,
        "Homesteadblock": 0,
        "Eip155block": 0,
        "Eip158block": 0
    },
    "nonce": "0x0000000000000042",
    "Mixhash": " 0x0000000000000000000000000000000000000000000000000000000000000000 ",
    " difficulty ":" 0x2000 ",
    " Alloc ": {},
    "Coinbase": "0x0000000000000000000000000000000000000000",
    "timestamp": "0x00",
    "Parenthash": " 0x0000000000000000000000000000000000000000000000000000000000000000 ",
    " Extradata ":" ",
    " GasLimit ":" 0xFFFFFFFF "
}

Note: You need to modify the account address in the alloc above, using the account address that you actually created in the first step of your own machine.

The

does not look balance value has many 0, actually only 999 etheric coins. The meanings of each parameter are as follows: Mixhash: Used in conjunction with the nonce for mining, a hash generated by a portion of the previous block. Note that he and the nonce settings need to meet Ethereum's yellow paper, 4.3.4. Block Header Validity, (44) The conditions described in the chapter: Nonce:nonce is a 64-bit random number for mining, note that his and Mixhash settings need to meet the Ethereum yellow paper, 4.3.4. Block Header Validity, (44) The conditions described in the chapter. Difficulty: The difficulty of setting the current block, if the difficulty is too large, the CPU mining is difficult, here set a small difficulty alloc: used to preset account and account of the number of Ethereum, because the private chain mining is relatively easy, so we do not need to pre-set the account of the currency, the need to create their own. Coinbase: The miner's account, casually fill timestamp: Set the time stamp of the Genesis Block Parenthash: The hash value of the previous chunk, because it is the Genesis block, so this value is 0 extradata: additional information, fill in, you can fill in your personality information gaslimit : This value sets the total consumption limit for gas, which is used to limit the sum of the transaction information that the block can contain, because we are the private chain, so fill the maximum. Create the Genesis block with the following command: Geth--datadir chain1 init genesis.json error prompts are as follows: fatal:failed to write Genesis Block:database already conta Ins an incompatible Genesis block because the default creation block was created when the private link was previously started. We need to delete the original creation block first, as follows: Geth removedb--datadir Chain1 then execute the following command again to create the Genesis block success. Geth--datadir chain1 Init genesis.json
The following command again into the Geth console geth--datadir chain1--nodiscover console with eth.getbalance can check See account 0 Now the balance is 999 ether currency.

> eth.accounts
["0xc9228294cc6bc3e3fcdba0f5d393d68f920c7789", "0x5f0880e6c3507f609548732151a99f6ddc71cb95 "]
> Eth.getbalance (eth.accounts[0])
999000000000000000000
> Web3.fromwei (eth.getbalance ( Eth.accounts[0])
999

Note: Wei is the smallest unit of the etheric currency, Web3.fromwei can convert the Wei unit into an ethereum unit.
3. Transfer transaction: From account 0 to 10 Ethereum to account 1
> A0=eth.accounts[0]
"0xc9228294cc6bc3e3fcdba0f5d393d68f920c7789"
> A1=eth.accounts[1]
" 0x5f0880e6c3507f609548732151a99f6ddc71cb95 "
> Personal.unlockaccount (a0," 1234 ")
true
> Eth.sendtransaction ({from:a0, to:a1, Value:web3.toWei (Ten, "Ether")})
INFO [03-25|23:30:40] Submitted Transaction                    fullhash=0x701113bdeaa9b14d424babf8d
x5f0880e6c3507f609548732151a99f6ddc71cb95
" 0x701113bdeaa9b14d424babf8df19852d26e9f23198aa5d7cd2c092581f666468 "

Note: The payment account needs to be unlocked with the Personal.unlockaccount command before the transfer.
We now use eth.getbalance to check the balance of two accounts, but regret to find that the transfer transaction does not take effect.
> eth.getbalance (A0)
999000000000000000000
> eth.getbalance (A1)
0

The reason is that the trade requires the involvement of miners and there is currently no mining node to start.
4. Mining start a CMD new console to mine, use the following command to attach to the private chain that has been started Geth attach \\.\PIPE\GETH.IPC
The Miner.start () command starts mining, and in the first CMD window you will see information about the success of the mining.
> INFO [03-25|23:36:30] successfully sealed new block            number=1 hash=232457 ... 18534b


Miner.stop () command to stop mining.
At this time again with Eth.getbalance check two account balance, found that account 1 received 10 Ethereum, you will be surprised to find that the balance of account 0 increased, that is because mining rewards default deposit account 0
Third, a summary of this tutorial is a hands-on way to let you know the Ethereum, to know the rationale behind the tutorial operation, the proposal to read the article "easy to deal with bitcoin transactions and mining", the principle of Bitcoin and the etheric currency is interlinked. Want to learn more about Ethereum, recommended sites: https://ethfans.org/

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.