區塊鏈智能合約:環境搭建-Centos7 上搭建以太坊私有測試鏈

官方文檔html

環境準備

相關工具和庫:git,nodejs,go,gcc-c++,chronyd,wget,epel,cmake
1, 軟件包獲取工具: git,wget,epel
2, geth運行環境: nodejs,go,chronyd
3, 編譯相關: gcc-c++,cmakenode

yum安裝
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()
相關文章
相關標籤/搜索