Ubuntu 16.04下安装以太坊编译环境以及设置合约功能(支持geth 1.6以及solc 0.4.16版本以上)

xiaoxiao2021-02-27  114

由于没有苹果电脑,所以在这里使用Linux环境进行操作,Windows也可以,但是没有试过,也看过不少文章,说道会遇到很多问题。

本文解决了下面几个问题:

1.geth升级到1.6版本后,不再使用eth.getCompilers()或者admin.setSolc()等通过JS的方式实时编译,而是采用了ABI合约接口的二进制表示。通过转化为json方式到geth的console平台进行编译

具体看下面文章说明:

https://ethereum.stackexchange.com/questions/15435/how-to-compile-solidity-contracts-with-geth-v1-6/15436

2.最新的solc0.4.16版本做了改变,以前用solc命令来将.sol文件转成abi合约接口二进制,现在使用solcjs来转变,转变后,要将.abi和.bin用文本编辑器做下修改。要修改成大致这样的格式:

生成的*.abi要改成如下样子,原始没有eth.contract([{这些:

 

var simpleContract = eth.contract([{"constant":false,"inputs":[{"name":"_a","type":"uint256"},{"name":"_b","type":"uint256"}],"name":"multiply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_a","type":"uint256"},{"name":"_b","type":"uint256"}],"name":"arithmetics","outputs":[{"name":"o_sum","type":"uint256"},{"name":"o_product","type":"uint256"}],"payable":false,"type":"function"}])

生成的.bin文件要改成如下样子,原始没有simpleContract.new(,from,data这些

 

 

var simple = simpleContract.new( { from: eth.accounts[0], data: "0x6060604052341561000f57600080fd5b5b6101178061001f6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063165c4a161460475780638c12d8f0146084575b600080fd5b3415605157600080fd5b606e600480803590602001909190803590602001909190505060c8565b6040518082815260200191505060405180910390f35b3415608e57600080fd5b60ab600480803590602001909190803590602001909190505060d6565b604051808381526020018281526020019250505060405180910390f35b600081830290505b92915050565b600080828401915082840290505b92509290505600a165627a7a72305820389009d0e8aec0e9007e8551ca12061194d624aaaf623e9e7e981da7e69b2e090029", gas: 500000 } )

 

 

 

 

 

1.1.1. 安装Ubuntu 16.04版本

1.1.1.1. 安装和配置SSH

 

Ubuntu下允许root用户ssh远程登录

http://www.linuxidc.com/Linux/2016-07/133256.htm

1.1.1.2. UBUNTU的默认root密码是多少,修改root密码

http://jingyan.baidu.com/article/5225f26b0ac250e6fb09084e.html

1.1.1.3. 安装Ethereum和solc

$sudo apt-get install software-properties-common

$sudo add-apt-repository -y ppa:ethereum/ethereum

$sudo apt-get update

$sudo apt-get install ethereum

$sudo apt-get install solc

1.1.1.4. 安装Node.js下载源码.tar.gz,进行编译和安装

tar -zxvf node-v6.9.2.tar.gz cd node-v6.9.2

sudo ./configure

sudo make

sudo make install

1.1.1.5. 安装solc

$solc --help是否有效?如果没有笑,可以再试着用下面方式安装solc

$npm install -g solc

$npm install -g solc-cli

操作完后,再试下$solc --help

1.1.1.6. 挖矿,创建用户,建立简单合约

请查看下面这篇文章

https://alanbuxton.wordpress.com/2017/07/19/first-steps-with-ethereum-private-networks-and-smart-contracts-on-ubuntu-16-04/

归纳操作如下面所示:

 

打开一个xshell终端,连接Ubuntu,运行ethereum dev环境 geth --datadir "~/ethdev" --dev --ipcdisable --rpcapi "db,eth,net,web3" 打开一个xshell终端,进入console操作环境 geth --dev console 2>> file_to_log_output 打开一个xshell终端,查看日志 tail -f file_to_log_output console操作环境,运行下面命令 >personal.newAccount('123456') >personal.newAccount('123456') >eth.accounts >user1 = eth.accounts[0] >user2 = eth.accounts[1] >eth.getBalance(user1) >eth.sendTransaction({from: user1, to: user2, value: web3.toWei(3,"ether")}) >personal.unlockAccount("0x**************","123456") >miner.start() >miner.stop() >eth.blockNumber() 编译合约 linux的默认路径是/root/, 在默认路径下面生成Test.sol文件 $ more simple.sol pragma solidity ^0.4.13; contract Simple {   function arithmetics(uint _a, uint _b) returns (uint o_sum, uint o_product) {     o_sum = _a + _b;     o_product = _a * _b;   }   function multiply(uint _a, uint _b) returns (uint) {     return _a * _b;   } } ethuser@host01:~/contract$ cat Simple.abi var simpleContract = eth.contract([{"constant":false,"inputs":[{"name":"_a","type":"uint256"},{"name":"_b","type":"uint256"}],"name":"multiply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_a","type":"uint256"},{"name":"_b","type":"uint256"}],"name":"arithmetics","outputs":[{"name":"o_sum","type":"uint256"},{"name":"o_product","type":"uint256"}],"payable":false,"type":"function"}]) and ethuser@host01:~/contract$ cat Simple.bin personal.unlockAccount(eth.accounts[0]) var simple = simpleContract.new( { from: eth.accounts[0], data: "0x6060604052341561000f57600080fd5b5b6101178061001f6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063165c4a161460475780638c12d8f0146084575b600080fd5b3415605157600080fd5b606e600480803590602001909190803590602001909190505060c8565b6040518082815260200191505060405180910390f35b3415608e57600080fd5b60ab600480803590602001909190803590602001909190505060d6565b604051808381526020018281526020019250505060405180910390f35b600081830290505b92915050565b600080828401915082840290505b92509290505600a165627a7a72305820389009d0e8aec0e9007e8551ca12061194d624aaaf623e9e7e981da7e69b2e090029", gas: 500000 } ) >loadScript("Simple.abi") >loadScript("Simple.bin") >simple.multiply >miner.start(1) >miner.stop() >simple.multiply.call(5,6)

 

 

 

 

 

转载请注明原文地址: https://www.6miu.com/read-16504.html

最新回复(0)