Development of local private chain development environment for cottage currency building

Source: Internet
Author: User
Tags pack git clone

Blockchain Enthusiast (qq:53016353)

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 warehouse 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.


Get ready
You can perform geth and SOLC commands by installing Go-ethereum and solc locally. 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
Start Geth
Enter this warehouse directory: CD ethereum-bootstrap
Import test Account private key:./bin/import_keys.sh
Start the private chain node:./bin/private_blockchain.sh. After successful startup, you can see output similar to the following:
Now that the Ethereum Interactive console has started, 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


Compiling and deploying smart contracts using the Ethereum console
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; &n Bsp    event Issue (Address account, uint amount);     Event Transfer (address from, address to, uint amount);      function Token () {        issuer = Msg.sender,    }     &nbs P;function issue (Address account, uint amount) {        if (msg.sender! = issuer) throw;   &NBSP ;     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 a 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"


The release token is a transaction and therefore needs to be mined to make it effective
> Miner.start (1)
: hammer:mined block
> miner.stop (1)


Check the token number of the first address of the local wallet again
> 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])
70
> 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.