In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article is about how to build a local private chain development environment for Ethereum Bootstrap. I think it is very practical, so I share it with you. I hope you can get something after reading this article. Let's take a look at it.
Ethereum Bootstrap
Through the methods described in this article and the scripts in the project, we can quickly build our own private chain for development and testing.
The tools included in the warehouse are:
A test account import script imports five test account private keys into the Ethernet Fong node on the first deployment.
A genesis.json configuration file that provides initial funding (ethercurrency) for the corresponding five test accounts to facilitate development and testing.
A script that quickly starts the private chain node and enters interactive mode.
A sample contract: contracts/Token.sol. This is a smart contract written in the contract language Solidity. The function of the Token contract is to issue a token (which can be understood as currency, points, etc.). Only the creator of the contract has the right to issue, the owner of the token has the right to use it, and can transfer money freely.
The private key of the test account is public data placed on the 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!
Prepare for
With go-ethereum and solc installed locally, you can execute the geth and solc commands. If the operating system is ubuntu, install the official ethereum installation package.
Download this warehouse to the local through the git clone command.
Install expect, which is used by utility scripts to automate some of the 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 a successful startup, you can see output similar to the following:
At this point, the ethernet interactive console has been launched, 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 non-standard location, you can set the GETH environment variable to specify the path to the geth executable. For example:
GETH=/some/weird/dir/geth. / bin/import_keys.sh
Compile and deploy smart contracts using the ethersquare console
In the contracts directory, there is an intelligent contract sample file Token.sol, which realizes the basic token function through Solidity language. Contract holders can issue tokens and users can transfer money to each other.
We can use the Ethernet Square console to compile and deploy this contract. The Ethernet Square console is the most basic tool, and it will be more tedious to use. The community also provides other more convenient deployment tools, which are not discussed here.
The first step is to compress the contract code into one line. Create a new ssh session, switch to the geth user environment su-geth, and type: cat contracts/Token.sol | tr'\ n''.
Switch to the ethernet square 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]; } }'; 然后编译合约代码: var tokenCompiled = web3.eth.compile.solidity(tokenSource); 通过tokenCompiled.Token.code可以看到编译好的二进制代码,通过tokenCompiled.Token.info.abiDefinition可以看到合约的ABI. 接下来我们要把编译好的合约部署到网络上去. 首先我们用ABI来创建一个javascript环境中的合约对象: var contract = web3.eth.contract(tokenCompiled.Token.info.abiDefinition); 我们通过合约对象来部署合约: 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: ">The first parameter of the contract.new method sets the address of the creator of the new contract from, the code data of the new contract, and the cost gas.gas for creating the new contract is an estimate, as long as there is more gas than is required. After the contract is created, the remaining gas will be returned to the contract creator.
The second parameter of the contract.new method sets a callback function that tells us whether the deployment was successful or not.
When contract.new executes, you will be prompted for the wallet password. After the successful execution, our contract Token has been broadcast on the network. At this time, just wait for the miners to package and save our contract to the Ethernet Fong block chain, and the deployment will be completed.
On the public chain, it takes an average of 15 seconds for miners to pack, and on the private chain, we need to do it ourselves. First, start digging:
Miner.start (1)
At this point, you need to wait for a period of time. The Ethernet Fong node will generate the necessary data for mining, which will be stored in memory. After the data is generated, mining will begin, and you can see something like this in the console output later:
: hammer:Mined block
This means that a block has been dug up and the contract has been deployed to the Ethernet Fong network! At this point, we can close the mining:
Miner.stop (1)
Then we can call the contract. First, obtain the address to which the contract is deployed through token.address, which can be used later when you create a new contract object. Here we directly use the original contract object:
/ / the number of token held at the first address of the local wallet > token.getBalance (web3.eth.accounts [0]) 0 token.getBalance / the first address for issuing 100 token to the local wallet > token.issue.sendTransaction (web3.eth.accounts [0], 100, {from: web3.eth.accounts [0]}) Tx (0xc0712460a826bfea67d58a30f584e4bebdbb6138e7e6bc1dbd6880d2fce3a8ef) to: 0x37dc85ae239ec39556ae7cc35a129698152afe3c "0xc0712460a826bfea67d58a30f584e4bebdbb6138e7e6bc1dbd6880d2fce3a8ef" / / release token is a transaction Therefore, mining is needed to make it effective > miner.start (1): hammer:Mined block > miner.stop (1) / / query the number of token at the first address of the local wallet again > token.getBalance (web3.eth.accounts [0]) 100 token / transfer 30 token from the first address to the second address of the local wallet > token.transfer.sendTransaction (web3.eth.accounts [1], 30 {from: web3.eth.accounts [0]}) I1221 1115 53 xeth.go:1055 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 from the private chain will be placed in the data directory under the root directory of the warehouse, and deleting this directory will erase all data and restart the new environment.
The above is how to build the Ethereum Bootstrap local private chain development environment. The editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.