ethereum开发环境构建(client端基于python)
1. 安装go-eth和solidity编译器
a. mac系统
brew tap ethereum/ethereum
brew install ethereum solidity
b. ubuntu系统
sudo add-apt-repository ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install ethereum solc
2. 配置私有链
a. 获取私有链配置数据
git clone https://github.com/janx/ethereum-bootstrap
b. 配置私有链
cd ethereum-bootstrap #进入仓库目录
./bin/import_keys.sh #导入测试账户私钥
./bin/private_blockchain_init.sh #初始化blockchain
c. 启动私有链节点
geth --datadir data --networkid 31415926 --rpc --rpccorsdomain "*" --rpcapi="db,eth,net,web3,personal,miner,admin" --nodiscover console
如果要启动公链节点,可以这样:
geth --fast --cache=1024 --rpc --rpccorsdomain "*" --rpcapi="db,eth,net,web3,personal,miner,admin" console
至此,一个私有链环境搭建完毕, 下面开始写python代码与此节点交互
3. 安装python客户端
a. 安装python3编译器
详情参考 https://www.python.org/
b. 安装第三方库
pip3 install web3
pip3 install py-solc
4. 编写第一个demo
import time
from web3 import Web3, HTTPProvider
# 连接私有链节点
w3 = Web3(HTTPProvider("http://localhost:8545"))
# 获取前面两个账户
from_user = w3.eth.accounts[0]
to_user = w3.eth.accounts[1]
# 解锁账户
w3.personal.unlockAccount(from_user, '123')
w3.personal.unlockAccount(to_user, '123')
# 开始挖矿
w3.miner.start(1)
# 查询两个账户的余额
print(w3.eth.getBalance(from_user))
print(w3.eth.getBalance(to_user))
# 从第1个账户转123块到第2个账户
w3.eth.sendTransaction({'to': to_user, 'from': from_user, 'value': 123})
# 休息15秒,再来查询他们第余额,观察是否变化
time.sleep(15)
print(w3.eth.getBalance(from_user))
print(w3.eth.getBalance(to_user))
========================== end ==========================
posted on 2018-01-27 17:47 bigdata2013lee 阅读(1695) 评论(1) 编辑 收藏 举报