Ethereum Local private chain development environment construction

Source: Internet
Author: User
Tags install go pack git clone
original link: http://ethfans.org/posts/ethereum-private-network-bootstrap


Ethereum-bootstrap is a github project that I built to help students who have just come into ethereum to quickly build their own local development environment and experience the development of smart contracts. Reading this article requires a basic knowledge of programming. Project address: Https://github.com/janx/ethereum-bootstrap Ethereum Bootstrap

Through the methods described in this article and the script in the project, we can quickly set up their own private chain for development testing.

The tools included in the repository are: A Test Account import script that imports five test account private keys into the Ethereum node at first deployment. A Genesis.json configuration file that provides initial funding (etheric currency) for the corresponding five test accounts for easy development testing. A script that quickly launches a private chain node and enters interactive mode. Example of a contract: Contracts/token.sol. This is a smart contract written using the contract language solidity. The function of the token contract is to issue a token (which can be understood as currency, points, etc.), and only the creator of the contract has the right of issuance, the owner of the token has access rights and is free to transfer funds.

The test Account private key is a publicly available data on GitHub and should never be used in a formal environment or on a public chain. If you use these private keys outside the test environment, your funds will be stolen. ready to install Go-ethereum and Solc locally, you can execute the Geth and SOLC commands. If the operating system is Ubuntu, install the official Ethereum installation package. Download the repository to local via the git clone command. Install expect, a tool script that uses it to automate some processes. For example on Ubuntu: sudo apt-get install expect boot Geth into this repository directory: CD ethereum-bootstrap import test Account private key:./bin/import_keys.sh Start Private There are chain nodes:./bin/private_blockchain.sh. After successful startup, you can see output similar to the following: The Ethereum Interactive console has been started and we can start testing and development.

Note: The tool script assumes that your geth is installed in the default location and can be executed directly through Geth. If the Geth command is installed in a nonstandard location, you can set the GETH environment variable to specify the path to the Geth executable file. For example:

Geth=/some/weird/dir/geth./bin/import_keys.sh using the Ethereum console to compile and deploy smart contracts

In the Contracts directory there is a smart contract sample file Token.sol, through the solidity language to achieve the basic token function, the contract holder can issue tokens, users can transfer each other.

We can use the Ethereum console to compile and deploy this contract. The Ethereum console is the most basic tool that can be used more cumbersome. The community also offers other more convenient deployment tools, which are not discussed here.

The first step is to compress the contract code into one line first. Create a new SSH session, switch to the Geth user environment Su-geth, and enter: Cat Contracts/token.sol | Tr ' \ n '.

Switch to the Ethereum console and save the contract code as a variable:

var tokensource = ' contract Token {     address issuer;     Mapping (address = uint) balances;      Event Issue (Address account, uint amount);     Event Transfer (address from, address to, uint amount);      function Token () {         issuer = Msg.sender;     }      function issue (Address account, uint amount) {         if (msg.sender! = issuer) throw;         Balances[account] + = amount;     }      function transfer (address to, uint amount) {         if (Balances[msg.sender] < amount) throw;          Balances[msg.sender]-= amount;         Balances[to] + = amount;          Transfer (Msg.sender, to, amount);     }      function GetBalance (Address account) constant returns (UINT) {         return balances[account];     } ';

Then compile the contract code:

var tokencompiled = web3.eth.compile.solidity (Tokensource);

With TokenCompiled.Token.code you can see the compiled binary code, and through TokenCompiled.Token.info.abiDefinition you can see the ABI of the contract.

Next we will deploy the compiled contract to the network.

First we use the ABI to create a contract object in a JavaScript environment:

var contract = Web3.eth.contract (tokenCompiled.Token.info.abiDefinition);

We deploy contracts through contract objects:

var initializer = {From:web3.eth.accounts[0], data:tokenCompiled.Token.code, gas:300000};

var callback = function (e, contract) {
    if (!e) {
      if (!contract.address) {
        console.log ("Contract transaction Send:transactionhash: "+ Contract.transactionhash +" waiting to be mined ... ");
      else {
        Console.log ("Contract mined!");
        Console.log (contract);}}
;

var token = contract.new (initializer, callback);

The first parameter of the Contract.new method sets the creator address of this new contract from, the new contract's code data, and the cost gas used to create the new contract. Gas is an estimate, as long as there is more gas than required, the remaining gas will be returned to the contract creator after the contract creation is completed.

The second parameter of the Contract.new method sets a callback function that tells us if the deployment was successful.

Contract.new will prompt for the wallet password when executing. After successful execution, our contract token has been broadcast to the network. As long as we wait for the miners to pack our contracts and save them on the Ethereum blockchain, the deployment is complete.

On the public chain, miners pack on average 15 seconds, and on the private chain we need to do it ourselves. First Open mining:

Miner.start (1)

It will take a while for the Ethereum node to generate the necessary data for mining, which will be put into memory. After the data has been generated, the mining will begin, and you will see something similar in the console output later:

: hammer:mined block

Information, which means that a block has been dug up and the contract has been deployed to the Ethereum network. At this point we can close the mining:

Miner.stop (1)

Then we can invoke the contract. The address to which the contract was deployed is first obtained through token.address, which can be used later when a new contract object is created. Here we use the original contract object directly:

The number of tokens held at the first address of the local wallet > Token.getbalance (web3.eth.accounts[0]) 0//Issue 100 tokens to the first address of the local wallet >
Token.issue.sendTransaction (Web3.eth.accounts[0], (+), {from:web3.eth.accounts[0]}); I1221 11:48:30.512296 11155 xeth.go:1055] Tx (0XC0712460A826BFEA67D58A30F584E4BEBDBB6138E7E6BC1DBD6880D2FCE3A8EF) to : 0x37dc85ae239ec39556ae7cc35a129698152afe3c "0xc0712460a826bfea67d58a30f584e4bebdbb6138e7e6bc1dbd6880d2fce3a8ef "//Issue token is a transaction, so it needs to be mined to make it effective > Miner.start (1): hammer:mined block > Miner.stop (1)//Check the token of the first address of the local wallet again Quantity > Token.getbalance (web3.eth.accounts[0]) 100//Turn 30 tokens from the first address to the second address of the local wallet > token.transfer.sendTransaction ( Web3.eth.accounts[1], (+, {from:web3.eth.accounts[0]}) I1221 11:53:31.852541 11155 xeth.go:1055] Tx ( 0X1D209CEF921DEA5592D8604AC0DA680348987B131235943E372F8DF35FD43D1B) to:
0x37dc85ae239ec39556ae7cc35a129698152afe3c "0X1D209CEF921DEA5592D8604AC0DA680348987B131235943E372F8DF35FD43D1B" > Miner.start (1) > Miner.stop (2) > Token.getbaLance (Web3.eth.accounts[0]) > Token.getbalance (web3.eth.accounts[1]) 30 
other

All data in the private chain is placed in the database directory at the repository root, and deleting the directory clears all data and restarts the new environment.

After doing this you should have a general understanding of the development of the Ethereum private chain, if you want to learn more, one can take a look at the script code executed above, what exactly you do, what command line parameters are used, and two you can read the Chinese version of the solidity document that is being updated on Ethfans.

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.