以太坊etherenum入門教程


入門安裝go-etherenum

1 搭建私有鏈

1.1 安裝

官方文檔提供兩種方式,本文使用源碼編譯

下載源碼

git clone https://github.com/ethereum/go-ethereum

安裝golang

yum install golang

編譯

cd go-ethereum
make geth


1.2 config配置文件

{
  "coinbase"   : "0x0000000000000000000000000000000000000000",
  "difficulty" : "0x400",
  "extraData"  : "0x0",
  "gasLimit"   : "0x2fefd8",
  "nonce"      : "0xdeadbeefdeadbeef",
  "mixhash"    : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
  "timestamp"  : "0x00",
  "alloc"      : {}
}

異常

go-ethereum/build/bin/geth init go-ethereum/eth/piccgenesis.json Fatal: invalid genesis file: json: cannot unmarshal hex string without 0x prefix into Go struct field Genesis.extraData of type hexutil.Bytes

執(zhí)行成功的config.json

[root@iz2ze20vl8jnph0si0n7jsz watt]# go-ethereum/build/bin/geth init go-ethereum/eth/piccgenesis.json 
INFO [01-29|21:20:19] Allocated cache and file handles         database=/root/.ethereum/geth/chaindata cache=16 handles=16
Fatal: Failed to write genesis block: database already contains an incompatible genesis block (have d4e56740f876aef8, new a0e580c6769ac3dd)

原因由于上次執(zhí)行命令初始化

啟動(dòng)私有鏈節(jié)點(diǎn)

啟動(dòng)Geth即可以啟動(dòng)以太坊的區(qū)塊鏈,為了構(gòu)建私有鏈 ,需要在Geth啟動(dòng)時(shí)加入一些參數(shù),Geth參數(shù)含義如下:
build/bin/geth --datadir /home/watt/ethereumdata init eth/piccgenesis.json

config.json文件

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

有個(gè)疑問那初始化的數(shù)據(jù)存儲(chǔ)路徑在哪呢? 通過指定的路徑生成數(shù)據(jù)文件名稱,使用find命令查找如下

查看初始化數(shù)據(jù)文件大小如下

重新配置參數(shù)

獲取當(dāng)前的目錄

/home/watt/go-ethereum/build/bin/geth  --datadir "/home/watt/ethereumchain" init /home/watt/go-ethereum/eth/piccgenesis.json 

創(chuàng)建數(shù)據(jù)存放地址并初始化創(chuàng)世塊

/home/watt/go-ethereum/build/bin/geth --identity "PICCetherum"  --rpc  --rpccorsdomain "*" --datadir "/home/watt/ethereumchain" --port "30303"  --rpcapi "db,eth,net,web3"--networkid 95518 console

啟動(dòng)日志

INFO [01-29|22:08:58] Allocated cache and file handles         database=/home/watt/ethereumchain/geth/chaindata cache=16 handles=16
INFO [01-29|22:08:58] Writing custom genesis block 
INFO [01-29|22:08:58] Successfully wrote genesis state         database=chaindata                               hash=a0e580…a5e82e
INFO [01-29|22:08:58] Allocated cache and file handles         database=/home/watt/ethereumchain/geth/lightchaindata cache=16 handles=16
INFO [01-29|22:08:58] Writing custom genesis block 
INFO [01-29|22:08:58] Successfully wrote genesis state         database=lightchaindata                               hash=a0e580…a5e82e
[root@iz2ze20vl8jnph0si0n7jsz go-ethereum]# /home/watt/go-ethereum/build/bin/geth --identity "PICCetherum"  --rpc  --rpccorsdomain "*" --datadir "/home/watt/ethereumchain" --port "30303"  --rpcapi "db,eth,net,web3"--networkid 95518 console
INFO [01-29|22:09:20] Starting peer-to-peer node               instance=Geth/PICCetherum/v1.8.0-unstable-722bac84/linux-amd64/go1.8.3
INFO [01-29|22:09:20] Allocated cache and file handles         database=/home/watt/ethereumchain/geth/chaindata cache=128 handles=1024
WARN [01-29|22:09:20] Upgrading database to use lookup entries 
INFO [01-29|22:09:20] Initialised chain configuration          config="{ChainID: 15 Homestead: 0 DAO: <nil> DAOSupport: false EIP150: <nil> EIP155: 0 EIP158: 0 Byzantium: <nil> Engine: unknown}"
INFO [01-29|22:09:20] Disk storage enabled for ethash caches   dir=/home/watt/ethereumchain/geth/ethash count=3
INFO [01-29|22:09:20] Disk storage enabled for ethash DAGs     dir=/root/.ethash                        count=2
INFO [01-29|22:09:20] Initialising Ethereum protocol           versions="[63 62]" network=1
INFO [01-29|22:09:20] Database deduplication successful        deduped=0
INFO [01-29|22:09:20] Loaded most recent local header          number=0 hash=a0e580…a5e82e td=262144
INFO [01-29|22:09:20] Loaded most recent local full block      number=0 hash=a0e580…a5e82e td=262144
INFO [01-29|22:09:20] Loaded most recent local fast block      number=0 hash=a0e580…a5e82e td=262144
INFO [01-29|22:09:20] Regenerated local transaction journal    transactions=0 accounts=0
INFO [01-29|22:09:20] Starting P2P networking 
INFO [01-29|22:09:22] UDP listener up                          self=enode://4fadb2bd1b9e7efca20c99c067930c7bb5cd9c27ea38aef11fd514bed4abb1b2697651194fd1b765a51c3648e001542fbf57ef2c4912469e384139bb61c57f60@[::]:30303
INFO [01-29|22:09:22] HTTP endpoint opened: http://127.0.0.1:8545 
INFO [01-29|22:09:22] IPC endpoint opened: /home/watt/ethereumchain/geth.ipc 
INFO [01-29|22:09:22] RLPx listener up                         self=enode://4fadb2bd1b9e7efca20c99c067930c7bb5cd9c27ea38aef11fd514bed4abb1b2697651194fd1b765a51c3648e001542fbf57ef2c4912469e384139bb61c57f60@[::]:30303

卡住了,需要把root下的

rm -rf ethereumchain/ 
rm -rf .ethereum/

清除后重現(xiàn)執(zhí)行執(zhí)行輸出如下:

INFO [01-29|22:47:57] HTTP endpoint opened: http://127.0.0.1:8545 
INFO [01-29|22:47:57] RLPx listener up                         self=enode://fc07f632a636c713a2621902d5eac3b1969afd54d255812ac767484d926fd1d5113d243467e59e8c0864e63b60240dbd7d55d3b34058fa388f1002f6b20bcfec@[::]:30303
INFO [01-29|22:47:57] IPC endpoint opened: /home/watt/ethereumchain/geth.ipc 
INFO [01-29|22:48:17] Block synchronisation started 
INFO [01-29|22:48:24] Imported new block headers               count=192 elapsed=1.250s number=192 hash=723899…123390 ignored=0
INFO [01-29|22:48:24] Imported new block headers               count=384 elapsed=53.824ms number=576 hash=41a746…6a8b38 ignored=0
INFO [01-29|22:48:24] Imported new block receipts              count=2   elapsed=17.637ms bytes=8 number=2   hash=b495a1…4698c9 ignored=0
INFO [01-29|22:48:24] Imported new block headers               count=384 elapsed=55.720ms number=960 hash=916910…1d0e2a ignored=0
INFO [01-29|22:48:24] Imported new block receipts      

但是未能調(diào)用javasript console頁面 通過查詢發(fā)現(xiàn)需要增加 --dev 參數(shù) 另開窗口

輸入命令

/home/watt/go-ethereum/build/bin/geth  --dev console 2>> file_to_log_output

2


2、 使用私有鏈

查看賬戶

eth.accounts

創(chuàng)建用戶:

personal.newAccount("111111")

其中參數(shù)為此賬戶的密碼。

也可以先創(chuàng)建賬戶,然后輸入密碼:

personal.newAccount()

查看區(qū)塊數(shù)據(jù)

eth.blockNumber

啟動(dòng)挖礦

miner.start()

返回結(jié)果為true則啟動(dòng)成功,具體執(zhí)行情況可查看日志。

停止挖礦

miner.stop()

當(dāng)在執(zhí)行挖礦時(shí)日志會(huì)不停刷屏,不用管,只要命令輸入全,執(zhí)行即可停止挖礦。

查看賬戶余額

其中參數(shù)為區(qū)塊鏈地址

eth.getBalance("0x7d1f7be4112ce63b9de04a0bf95c1e87e430bd1b")

轉(zhuǎn)賬

從賬戶0x7d1f7be4112ce63b9de04a0bf95c1e87e430bd1b轉(zhuǎn)賬3個(gè)以太幣到0x587e57a516730381958f86703b1f8e970ff445d9。

eth.sendTransaction({from:"0x7d1f7be4112ce63b9de04a0bf95c1e87e430bd1b",to:"0x587e57a516730381958f86703b1f8e970ff445d9",value:web3.toWei(3,"ether")})

當(dāng)直接執(zhí)行此方法時(shí)會(huì)拋出異常:

account is locked
    at web3.js:3119:20
    at web3.js:6023:15
    at web3.js:4995:36
    at <anonymous>:1:1

很明顯,賬戶被鎖。

解鎖轉(zhuǎn)出賬戶

其中第一個(gè)參數(shù)為轉(zhuǎn)出賬戶,第二個(gè)參數(shù)為密碼。也可以直填寫第一個(gè)參數(shù),然后通過命令行提示再輸入密碼。

personal.unlockAccount("0x7d1f7be4112ce63b9de04a0bf95c1e87e430bd1b","111111")

解鎖完成之后,即可執(zhí)行轉(zhuǎn)賬操作。但此時(shí)查看時(shí)會(huì)發(fā)現(xiàn)接收賬戶依舊為原來數(shù)值。此時(shí)需要執(zhí)行挖礦命令,才會(huì)把轉(zhuǎn)賬真正完成。

看似美好,執(zhí)行命令后

> eth.accounts
["0x709447961e479f696a496243cbeff83b851908b2"]
> personal.newAccount("111111")
"0x489835dff4d2270ae2c6a148ca313858bf1e4994"
> eth.blockNumber
0
> miner.start()
null
> 

發(fā)現(xiàn)miner.start() 為null

Pre-allocating ether to your account
A difficulty of “0x400” allows you to mine Ether very quickly on your private testnet chain. If you create your chain and start mining, you should have hundreds of ether in a matter of minutes which is way more than enough to test transactions on your network. If you would still like to pre-allocate Ether to your account, you will need to:

Create a new Ethereum account after you create your private chain
Copy your new account address
Add the following command to your Custom_Genesis.json file:
"alloc":
{
        "0x709447961e479f696a496243cbeff83b851908b2":
        { "balance": "20000000000000000000" }
}
Note

Replace 0x1fb891f92eb557f4d688463d0d7c560552263b5a with your account address.

Save your genesis file and rerun your private chain command. Once geth is fully loaded, close it by .

We want to assign an address to the variable primary and check its balance.

Run the command geth account list in your terminal to see what account # your new address was assigned.

需要執(zhí)行命令

miner.setEtherbase(personal.listAccounts[0])

查看日志

INFO [01-29|23:14:53] Starting mining operation 
INFO [01-29|23:14:53] Commit new mining work                   number=1 txs=0 uncles=0 elapsed=65.571μs
WARN [01-29|23:14:53] Block sealing failed ? ? ? ? ? ? ? ? ? ? err="waiting for transactions

繼續(xù)努力

#返回錢包管理的賬戶地址列表  
#返回示例:["0x3138e3722fb4280cb67f6e858108136bfa1c9160"]
eth.accounts

#創(chuàng)建賬戶地址,參數(shù)為賬戶鎖定密碼,在轉(zhuǎn)賬前需要先解鎖賬戶
#我們把這個(gè)命令運(yùn)行兩次,創(chuàng)建兩個(gè)地址,加上默認(rèn)的,一共有了三個(gè)賬戶地址
personal.newAccount('111111')
personal.newAccount('111111')

#為賬戶設(shè)置別名,方便命令輸入
user1=eth.accounts[0]
user2=eth.accounts[1]
user3=eth.accounts[2]

#查看地址user1余額,這個(gè)地址是測試鏈默認(rèn)開通的一個(gè)地址,里面初始化有很多幣
#我們創(chuàng)建的另外兩個(gè)地址余額未0
eth.getBalance(user1)

#查看區(qū)塊高度,現(xiàn)在為0
eth.blockNumber

#轉(zhuǎn)賬測試,首先解鎖賬號(hào)user1
#命令運(yùn)行后要求輸入解鎖密碼,直接回車,默認(rèn)賬號(hào)鎖定密碼為空,返回true成功
personal.unlockAccount(user1)

#從user1向user2轉(zhuǎn)賬3個(gè)以太幣
#命令運(yùn)行后,提交交易立馬回出發(fā)挖礦
eth.sendTransaction({from:user1,to:user2,value:web3.toWei(3,"ether")})

#查看區(qū)塊高度,這時(shí)高度為1
eth.blockNumber

還是null

挖礦測試
geth啟動(dòng)后,自動(dòng)啟動(dòng)挖礦,這時(shí)運(yùn)行miner.start(),返回為null 無交易的時(shí)候不挖礦,當(dāng)有交易時(shí)自動(dòng)會(huì)觸發(fā)挖礦流程

#我們可以先停止挖礦
miner.stop()

#提交交易,這時(shí)候只提交,查看賬戶余額,但是未確認(rèn)
eth.sendTransaction({from:user1,to:user2,value:web3.toWei(3,"ether")})

#啟動(dòng)挖礦,確認(rèn)交易,再次查看賬戶余額
miner.start()

#那么挖礦獎(jiǎng)勵(lì)去哪兒了?查看礦工地址
eth.coinbase

#設(shè)置礦工地址
miner.setEtherbase(eth.coinbase)


3、參考資料

區(qū)塊鏈開發(fā)(一)搭建基于以太坊的私有鏈環(huán)境

私有鏈搭建文檔

以太坊學(xué)習(xí)筆記:私有鏈搭建操作指南

區(qū)塊鏈開發(fā)(二)以太坊客戶端基本操作命令

以太坊執(zhí)行miner.start返回null

10分鐘完成阿里云環(huán)境搭建以太坊私有鏈

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容