區塊鏈智能合約:環境搭建-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()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM