0x00 Ethereum簡介
Ethereum(以太坊)是運行智能合約去中心化的開源平臺。用于數(shù)字貨幣,商業(yè)合同哗咆,眾籌項目等,主要解決雙方信任問題猾担。
0x01 環(huán)境構(gòu)建
參考 https://github.com/ethereum/go-ethereum
go語言構(gòu)建袭灯,簡單運行只要geth即可。
0x02 私有鏈搭建
命令參數(shù)
--ipcdisable
--nodiscover不會主動廣播
--networkid網(wǎng)絡標識绑嘹,處于同一個網(wǎng)絡和協(xié)議版本的才能進行連接
# 生成第一個實例
# 初始化稽荧,genesis.json記錄創(chuàng)世紀塊
geth --datadir "/home/xxx/blockchain" init genesis.json
# genesis.json配置如下:config用于升級,alloc用于預置賬戶
{
"config": {
"chainId": 0,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc" : {},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x20000",
"extraData" : "",
"gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00"
}
# 運行
geth --datadir /home/xxx/blockchain --networkid 36101 --ipcdisable --nodiscover console
# 啟動后通過admin.nodeInfo, eth.accounts等js函數(shù)查看當前數(shù)據(jù)
# 建立關聯(lián)的賬戶工腋,挖礦后所得存入當前賬戶
personal.newAccount("password")
# 開始挖礦姨丈,參數(shù)1表明1個線程
# miner.start(1)
# 停止挖礦
# miner.stop()
# 生成第二個實例,并與第一個實例建立連接
# 初始化夷蚊,genesis.json記錄創(chuàng)世紀塊构挤,這里要注意使用于第一個相同的配置文件,這樣擁有相同的創(chuàng)世紀塊才能連接
geth --datadir "/home/xxx/blockchain" init genesis.json
# 運行
geth --datadir /home/xxx/blockchain --networkid 36101 --ipcdisable --nodiscover console
# 連接第一個實例惕鼓,ecodeuri為第一個實例的節(jié)點筋现,通過admin.nodeInfo取得的ecode值,并將[::]替換為實例一的ip
admin.addPeer("ecodeuri")
# 查看當前連接的節(jié)點
admin.peers
# 挖礦一段時間后賬戶會存入eth幣箱歧,block增加
# 日志打印mined potential block矾飞,則增加block,增加eth幣
# 當前賬戶幣值
# wei
eth.getBalance(eth.accounts[0]).toNumber()
# ether=1后面18個0的wei
web3.fromWei(eth.getBalance(eth.accounts[0]), "ether")
# 轉(zhuǎn)賬交易呀邢,from轉(zhuǎn)給to 1個ether玫恳,from和to填寫eth.accounts[0]對應值
eth.sendTransaction({from:'0xce417f51ebbc59e5cff597acecf10cedbf6075d3', to:'0x1b23bba8d4106cdfe8bb1d951c6b605db1ee750e',value:web3.toWei(1,"ether")})
# 轉(zhuǎn)賬時要求輸入密碼解鎖賬戶
personal.unlockAccount('0xce417f51ebbc59e5cff597acecf10cedbf6075d3')
# 轉(zhuǎn)賬會等待礦工記錄完成才算交易結(jié)束
# 查看未記賬的交易
eth.getBlock("pending", true).transactions