官方文档html
相关工具和库:git,nodejs,go,gcc-c++,chronyd,wget,epel,cmake
1, 软件包获取工具: git,wget,epel
2, geth运行环境: nodejs,go,chronyd
3, 编译相关: gcc-c++,cmakenode
yum install -y curl git golang nodejs gcc-c++ mkdir ~/go echo "export GOPATH=$HOME/go" >> /etc/profile source /etc/profile
gethc++
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
cmakegit
#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".配置以下:github
{ "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中的内容是给一些帐户地址预先充值,像这样golang
"alloc": { "0x0000000000000000000000000000000000000001": {"balance": "111111111"}, "0x0000000000000000000000000000000000000002": {"balance": "222222222"} }
对genesis.json 稍做修改web
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" }
初始化节点,只须要在第一次运行npm
geth --datadir "/root/eth-test/" init genesis.json
运行脚本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
正常运行节点api
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()