Ubuntu 16.04下安装以太坊编译环境以及设置合约功能(支持geth 1.6以及solc 0.4.16版本以上)
来源:互联网 发布:各国的创世神话 知乎 编辑:程序博客网 时间:2024/05/22 08:14
由于没有苹果电脑,所以在这里使用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)
- Ubuntu 16.04下安装以太坊编译环境以及设置合约功能(支持geth 1.6以及solc 0.4.16版本以上)
- 【以太坊】ubuntu安装以太坊ethereum的测试网络ropsten-net以及雷电网络raiden-network环境
- ubuntu下安装eclipse以及配置python编译环境
- Ubuntu环境下Thrift的安装、编译以及测试
- 以太坊(3):以太坊私有链环境下的智能合约的编写、编译、创建与执行
- 以太坊开发实战:geth客户端安装
- 在ubuntu得linux环境下安装jdk7以上版本
- 以太坊代币智能合约底层开发教程Geth基本命令
- Ubuntu下geth搭建私有链+部署智能合约+与合约交互笔记
- Ubuntu下 Awstats的安装以及设置
- Ubuntu下安装Gnome以及一些设置
- Ubuntu下安装Gnome以及一些设置
- 安装geth 搭建以太坊私链
- 区块链学堂(第二课):以太坊基本概念+Geth 安装
- ubuntu环境下 python 3.0以上版本对sqlite3的支持问题
- Thrift 学习笔记1——Ubuntu环境下Thrift的安装、编译以及测试
- Thrift 学习笔记1——Ubuntu环境下Thrift的安装、编译以及测试
- ubuntu下如何查看软件安装目录以及安装版本
- Ubuntu 14.04 install python-pip
- java每日一练
- 四元数的“前世”与“今生”
- java中的IO File类的用法详解加代码演示
- 使用ATtiny85单片机驱动四个RGB LED指示灯
- Ubuntu 16.04下安装以太坊编译环境以及设置合约功能(支持geth 1.6以及solc 0.4.16版本以上)
- invalid mode 'kCFRunLoopCommonModes' provided to CFRunLoopRunSpecific
- Java中String.format()使用
- HDU 1171 Big Event in HDU(01背包)
- Statement、PreparedStatement和CallableStatement
- Struts2 Action中调用get***命名方法,并且返回json,执行出错
- mysql 提示表不存在的解决方法error: 1146: Table doesn't exist
- shell脚本搭建容器云(-)
- ortp库使用入门