区块链智能合约:环境搭建-Centos7 上搭建以太坊私有测试链

官方文档

环境准备

相关工具和库:git,nodejs,go,gcc-c++,chronyd,wget,epel,cmake
1, 软件包获取工具: git,wget,epel
2, geth运行环境: nodejs,go,chronyd
3, 编译相关: gcc-c++,cmake

yum安装
yum install -y curl git golang nodejs gcc-c++
mkdir ~/go 
echo "export GOPATH=$HOME/go" >> /etc/profile
source /etc/profile
编译安装

geth

cd /usr/local
git clone https://github.com/ethereum/go-ethereum.git  
cd go-ethereum && make all
ln -sv /usr/local/go-ethereum/build/bin/geth /usr/bin 
#or echo "export PATH=$PATH:/usr/local/go-ethereum/build/bin/" >> /etc/profil

cmake

#cmake solc智能合约需要使用
cd ~
wget https://cmake.org/files/v3.9/cmake-3.9.2.tar.gz  
tar xvf cmake-3.9.2.tar.gz && cd cmake-3.9.2
./configure && make && make install
检查软件环境
systemctl is-enabled chronyd
go version
node -v
npm -v
cmake --version
geth -v

创建私有链

在go-ethereum路径中可以查看到README.md搜索"Defining the private genesis state",提示我们需要创世配置文件"genesis.json".配置如下:

{
  "config": {
        "chainId": 0,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
  "alloc"      : {},
  "coinbase"   : "0x0000000000000000000000000000000000000000",
  "difficulty" : "0x200",
  "extraData"  : "",
  "gasLimit"   : "0x2fefd8",
  "nonce"      : "0x0000000000000042",
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp"  : "0x00"
}

文档建议将nonce改为随机值,防止来至网络的连接,alloc中的内容是给一些账户地址预先充值,像这样

"alloc": {
  "0x0000000000000000000000000000000000000001": {"balance": "111111111"},
  "0x0000000000000000000000000000000000000002": {"balance": "222222222"}
}
启动测试节点

对genesis.json 稍作修改

mkdir eth-test && cd eth-test
vi genesis.json
{
  "config": {
        "chainId": 20,
        "homesteadBlock": 0,
        "eip155Block": 0,
        "eip158Block": 0
    },
  "alloc"      : {},
  "coinbase"   : "0x0000000000000000000000000000000000000000",
  "difficulty" : "0x200",
  "extraData"  : "",
  "gasLimit"   : "0x2fefd8",
  "nonce"      : "0x0000000000000042",
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp"  : "0x00"
}

初始化节点,只需要在第一次运行

geth --datadir "/root/eth-test/" init genesis.json

运行脚本

echo "(nohup geth --rpc --rpcaddr='0.0.0.0' --rpccorsdomain="*" --nodiscover --maxpeers '5' --networkid 11 --datadir '/root/eth-test/' --rpcapi "db,net,eth,web3,personal,miner,debug,admin" 2>>geth_log &)" >> start.sh
#由于将输出定向至geth_log,查看信息tail -f geth_log 

正常运行节点

bash start.sh
#更多运行参数http://ethdocs.org/en/latest/network/test-networks.html#setting-up-a-local-private-testnet

运行console

geth attach ./geth.ipc
挖矿转账

查看帐号

eth.accounts 

创建帐号

personal.newAccount('123456')

查看余额

test1 = eth.accounts[0]
eth.getBalance(test1)                        #,注意eth单位是10的18次方,显示值为真实数量乘以10的18次方

挖矿

miner.start()                                #eth.blockNumber可查看块是否增加,单机测试需要挖矿才能完成交易
miner.stop()

转账

personal.unlockAccount(test1,'123456')       #对转出的账户进行解锁

eth.sendTransaction({from: test1, to: test2, value: web3.toWei(74, 'ether')})
# 这里转账单位又不是10的18次方,所以正确转账金额需要乘以10的18次方必须小于getBalance的额度(很绕是吧?)

挖矿账户

eth.coinbase                                 #查看主账户
miner.setEtherbase(eth.accounts[0])          #默认挖矿为coinbase中的账户

完整的测试过程

eth.accounts  
personal.newAccount('123456')
personal.newAccount('123456')
eth.accounts
test1 = eth.accounts[0]
test2 = eth.accounts[1]
miner.start()                                #执行挖矿一段时间,然账户中有余额
miner.stop()
personal.unlockAccount(test1,'123456')
eth.sendTransaction({from: test1, to: test2, value: web3.toWei(1, 'ether')})
#超过余额会报错
eth.getBalance(test1)           
eth.getBalance(test2)
miner.start(1)                                #执行挖矿让交易执行,1表示使用一个cpu
miner.stop()
posted @ 2018-06-09 13:01  ops_sylar  阅读(2222)  评论(1编辑  收藏  举报