In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of how to deploy the block chain eTaifang development environment, the content is detailed and easy to understand, the operation is simple and fast, and has a certain reference value. I believe you will gain something after reading this block chain Etaifang development environment deployment article. Let's take a look.
1 installation
Brew tap ethereum/ethereum brew install ethereum
When you are finished, verify with the following command:
Geth version2 operation
The so-called operation refers to running a node and joining the ethernet network, which has two options: public chain and test chain, both of which are on the Internet.
2.1 Common chain
Geth-fast-cache=512-datadir "your directory" console
2.2 Test chain
Geth-testnet-fast-cache=512-datadir "your directory" console
Both methods require synchronous blocks, which are stored in the directory specified by datadir, so make sure there is enough space.
3 build a private chain
The purpose of building a private chain is mainly for testing purposes, and it is not recommended to build a private chain for the purpose of operation. The whole step is as follows:
* * 3.1Writing genesis.json to define initial information. * * examples of contents are as follows:
{"config": {"chainId": 20, "homesteadBlock": 0, "eip155Block": 0, "eip158Block": 0}, "alloc": {"0xeb680f30715f347d4eb5cd03ac5eced297ac5046": {"balance": "100000000000000000000000000000000000000000000000000000000"}, "coinbase": "0x0000000000000000000000000000000000000000", "difficulty": "0x01", "extraData": "0x777573686f756865" "gasLimit": "0xffffffff", "nonce": "0x0000000000000001", "mixhase": "0x0000000000000000000000000000000000000000000000000000000000000000", "parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000", "timestamp": "0x00"}
3.2 initialization
Geth-datadir "your dir" init genesis.json
3.3 start-up, after seeing the small hammer, it indicates that the mining has started.
Geth-- datadir "your directory"-- rpc-- rpcaddr=0.0.0.0-- rpcport 8545-- rpccorsdomain "*"-- rpcapi "eth,net,web3,personal,admin,shh,txpool,debug,miner"-- nodiscover-- maxpeers 30-- networkid 1981-- port 30303-- mine-- minerthreads 1-- etherbase "0xeb680f30715f347d4eb5cd03ac5eced297ac5046" console
It is important to note here that "ctrl-c" on this command line does not terminate the process. If you want to stop running, type "exit" at the command line.
Geth also provides a console, which can be accessed by entering the following command:
Geth-- datadir "your directory" attach ipc: your directory / geth.ipc
The interface provided by the console includes: account, transaction, contract call, block.
4 Intelligent contract
This section uses vscode as the IDE,geth-cli to briefly describe the development process of smart contracts.
4.1 Environmental preparation
Install language Pack
Brew install solidity
Prepare the IDE plug-in
Https://github.com/juanfranblanco/vscode-solidity, go to "extension" and search for "solidity". The first one is.
4.2 contract development
Edit (simpledata.sol), as follows:
Pragma solidity ^ 0.4.23 X contract SimpleData {uint data; function set (uint x) public {data = x;} function get () view public returns (uint) {return data;}}
Compile
Echo "var output=solc-optimize-combined-json abi,bin,interface simpledata.sol" > simpledata.js
Deployment (using the private chain built earlier)
Start the private chain
To enter the console, all the following steps are completed in the console, using js syntax.
Load the compiled js:
LoadScript ('... / simpledata.js')
Get the relevant parameters required for deployment, where output is the output defined in the compile step above:
Var contractAbi = output.contracts ['simpledata.sol:SimpleData']. Abivar contract = eth.contract (JSON.parse (contractAbi)) var binCode = "0x" + output.contracts [' simpledata.sol:SimpleData'] .bin
Create users and keep their accounts in balance (deployment also costs money):
Personal.newAccount ("123456") / / the user created by setting the password miner.setEtherbase (eth.accounts [0]) / / is the miner web3.fromWei (eth.getBalance (eth.accounts [0]), "ether") / / View the balance
Publish
Var deployTx = {from: eth.accounts [0], data: binCode, gas: 1000000}; var instance = contract.new (deployTx) var contractAddress = eth.getTransactionReceipt (instance.transactionHash). ContractAddress / / contract address
Call (still on the same private chain, also executed on the console)
Var simpleData = contract.at (contractAddress) simpleData.get.call () personal.unlockAccount (eth.accounts [0]) / / this step requires simpleData.set.sendTransaction (33, {from: eth.accounts [0], gas: 100000}) simpleData.get.call ()
There are two points to note here:
The changes on the chain are done through transactions, and there is a cost to execute the contract at the same time, that is, gas, which is very different from traditional development. That's why the user was created and set as a miner to get the balance before deployment.
Note: gas and eth are not the same thing, and the final eth consumed by the contract is determined by: step and gasPrice.
The account needs to be unlocked before executing the transaction, which is similar to the need to enter the withdrawal password before transferring money when using online banking. At the same time, there is a time limit for unlocking. To avoid frequent unlocking, you can use the following statement:
Personal.unlockAccount (eth.accounts [0], password, time limit)
There is a transaction here, so it also illustrates the issue of security:
All transactions on Ethernet Square are publicly viewable, and all transaction history can be seen through Etherscan.
All transactions are signed with the initiator's private key, so as long as the private key is properly preserved, there will be no big problem. Signing with the private key does not appear in the above steps because the account itself is created by the console and the corresponding public and private keys are created at the same time.
5Truffle framework
You can see that the whole process above is quite tedious, and Truffle is created to solve these problems.
Installation
Npm install-g truffle
Development and deployment
Mkdir dapp
Cd dapp
Truffle init
Go to the contracts directory and create the following file
Pragma solidity ^ 0.4.23 X contract Storage {uint256 storageData; function set (uint256 data) public {storageData = data;} function get () view public returns (uint256) {return storageData;}}
Truffle compile
The deployment of the contract is done through migration, which should be familiar if you have used dbmigration tools such as liquidbase. Go to the migrations directory and create the corresponding file, starting with "number _", such as "2_deploy_storage.js". The contents are as follows:
Var Storage = artifacts.require ("Storage"); module.exports = function (deployer) {deployer.deploy (Storage);}
Build a test chain for development, ganache, because it is faster than the private chain built by yourself.
Npm install-g ganache-cli / / installation
Ganache-cli / / run
Before deployment, modify the truffle.js content (where networks defines various networks, which objectively acts like an "environment"):
Module.exports = {networks: {host: "127.0.0.1", port: 8545, network_id: "*" / / Match any network id}
Truffle migrate
At this point, after deployment, let's test it manually.
6 use JSON-RPC to manually invoke the contract
The JSON-RPC interface can be completed with curl, which is characterized in that the method of the contract is the data requested by rpc. If you think this is a bit of a mouthful, compare this approach to a method that uses Java Reflection to call a class.
To invoke the above contract, you first need to obtain the signature of the contract methods (get and set):
Get method
Curl-X POST-I http://localhost:8545-- data'{"jsonrpc": "method": "web3_sha3", "params": ["get ()"], "id": 1}'* * set method * * curl-X POST-I http://localhost:8545-data'{"jsonrpc": "2.0", "method": "web3_sha3", "params": ["set (uint256)"], "id": 1}'
The method signature is obtained using "web3_sha3", and its signature is 8 digits after 0x of the return value. The corresponding results of the above two calls are as follows:
Get:6d4ce63c
Set:60fe47b1
Calling the contract requires the address of the contract, which can be obtained from the ganache terminal. Look for a statement similar to the following in its terminal output, where the address is after created.
Contract created: 0x59322f0c1235719b4c0044b0c67f2437674b8c69
All that is left is that the contract is called (where the from account directly uses the account initialized when ganache starts, and to is the contract address):
Get method
Curl-X POST-I http://localhost:8545-- data'{"jsonrpc": "method": "eth_call", "params": [{"from": "0x2fe84a7fb107ade77adfeb351b92615597b68f52", "to": "0x59322f0c1235719b4c0044b0c67f2437674b8c69", "data": "0x6d4ce63c0000000000000000000000000000000000000000000000000000000000000000"}, "latest"], "id": 1}'
Set method
Curl-X POST-I http://localhost:8545-- data'{"jsonrpc": "method": "eth_sendTransaction", "params": [{"from": "0x2fe84a7fb107ade77adfeb351b92615597b68f52", "to": "0x59322f0c1235719b4c0044b0c67f2437674b8c69", "gas": "0xc350", "gaslimit": "0xc350", "data": "0x60fe47b10000000000000000000000000000000000000000000000000000000000000005"}], "id": 1}'
Notice the difference between the two calls to "method". At the same time, the first eight digits after 0x in the "data" parameter are the corresponding method signatures, followed by the data actually passed into the corresponding method.
7 programming interface
If you want to interact with a smart contract in a js file, you need the appropriate package:
Web3.js, which encapsulates the json-rpc interface
Truffle-contract module, contract abstraction of truffle
8 automatic testing
Truffle supports two types of automated testing: using javascript and using solidity, both of which are fairly simple, and I won't repeat them here. Put the test files in the test directory, and test the command:
Truffle test / / all test truffle test files / / single test 9 package management
Js package uses ordinary npm, slightly
Ethpm for package of smart contracts
Truffle install package name truffle publish package name
This is the end of the article on "how to deploy the development environment of Block chain Etay Square". Thank you for reading! I believe you all have a certain understanding of the knowledge of "how to deploy the development environment of Block chain Etai Square". If you want to learn more, you are welcome to 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.