Ubuntu以太坊私有鏈搭建

本文作者:陳進堅
博客地址:https://jian1098.github.io
CSDN博客:https://blog.csdn.net/c_jian
簡書:https://www.jianshu.com/u/8ba9ac5706b6
聯繫方式:[email protected]

安裝geth

jian@ubuntu:~$ sudo add-apt-repository -y ppa:ethereum/ethereum
jian@ubuntu:~$ sudo apt-get update
jian@ubuntu:~$ sudo apt-get install ethereum

查看geth版本

jian@ubuntu:~$ geth version
WARN [12-20|00:00:20.173] Sanitizing cache to Go's GC limits       provided=1024 updated=661
Geth
Version: 1.8.20-stable
Git Commit: 24d727b6d6e2c0cde222fa12155c4a6db5caaf2e
Architecture: amd64
Protocol Versions: [63 62]
Network Id: 1
Go Version: go1.10.4
Operating System: linux
GOPATH=
GOROOT=/usr/lib/go-1.10

創世區塊

新建ethereum文件夾,然後在ethereum文件夾中創建data文件夾(存儲區塊數據)和genesis.json文件,打開genesis.json文件將創世區塊代碼複製到文件中並保存

jian@ubuntu:~$ mkdir ethereum
jian@ubuntu:~$ cd ethereum/
jian@ubuntu:~/ethereum$ mkdir data
jian@ubuntu:~/ethereum$ vi genesis.json

創世區塊數據

{
	"nonce": "0x0000000000000042",
	"mixhash": "0x0000000000000000000000000000000000000000000000000000000000000000",
	"difficulty": "0x400",
	"alloc": {},
	"coinbase": "0x3333333333333333333333333333333333333333",
	"timestamp": "0x0",
	"parentHash": "0x0000000000000000000000000000000000000000000000000000000000000000",
	"extraData": "0x",
	"gasLimit": "0x8000000",
	"config": {}
}

參數說明

參數名 說明
nonce nonce就是一個64位隨機數,用於挖礦,注意他和mixhash的設置需要滿足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章節所描述的條件。
difficulty 設置當前區塊的難度,如果難度過大,cpu挖礦就很難,這裏設置較小難度
alloc 用來預置賬號以及賬號的以太幣數量,因爲私有鏈挖礦比較容易,所以我們不需要預置有幣的賬號,需要的時候自己創建即可以。
coinbase 礦工的賬號,任意填寫一個賬號即可。
imestamp 設置創世塊的時間戳
parentHash 上一個區塊的hash值,因爲是創世塊,所以這個值是0
extraData 附加信息,隨便填,可以填你的個性信息
gasLimit 該值設置對GAS的消耗總量限制,用來限制區塊能包含的交易信息總和,因爲我們是私有鏈,所以填最大。
mixhash 與nonce配合用於挖礦,由上一個區塊的一部分生成的hash。注意他和nonce的設置需要滿足以太坊的Yellow paper, 4.3.4. Block Header Validity, (44)章節所描述的條件。.

創建創世區塊

jian@ubuntu:~/ethereum$ geth --datadir data init genesis.json

WARN [12-20|00:01:37.255] Sanitizing cache to Go's GC limits       provided=1024 updated=661
INFO [12-20|00:01:37.257] Maximum peer count                       ETH=25 LES=0 total=25
INFO [12-20|00:01:37.276] Allocated cache and file handles         database=/home/jian/ethereum/data/geth/chaindata cache=16 handles=16
INFO [12-20|00:01:37.285] Writing custom genesis block 
INFO [12-20|00:01:37.286] Persisted trie from memory database      nodes=0 size=0.00B time=2.214µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [12-20|00:01:37.287] Successfully wrote genesis state         database=chaindata                               hash=6231b0…a0300b
INFO [12-20|00:01:37.287] Allocated cache and file handles         database=/home/jian/ethereum/data/geth/lightchaindata cache=16 handles=16
INFO [12-20|00:01:37.292] Writing custom genesis block 
INFO [12-20|00:01:37.293] Persisted trie from memory database      nodes=0 size=0.00B time=2.226µs gcnodes=0 gcsize=0.00B gctime=0s livenodes=1 livesize=0.00B
INFO [12-20|00:01:37.294] Successfully wrote genesis state         database=lightchaindata                               hash=6231b0…a0300b

啓動私有鏈

jian@ubuntu:~/ethereum$ geth --port 3000 --networkid 15 --datadir="data" --maxpeers=3 --rpc --rpcport 8545 --rpcaddr 127.0.0.1 --rpccorsdomain "*"  --rpcapi="db,eth,web3,personal,admin,txpool" console

啓動成功會進入js控制檯

Welcome to the Geth JavaScript console!

instance: Geth/v1.8.20-stable-24d727b6/linux-amd64/go1.10.4
 modules: admin:1.0 debug:1.0 eth:1.0 ethash:1.0 miner:1.0 net:1.0 personal:1.0 rpc:1.0 txpool:1.0 web3:1.0

>

退出私有鏈只需要輸入exit即可,如果要讓另外一臺電腦訪問私有鏈,需要將 --rpcaddr 設置爲本機ip

創建錢包

在js控制檯輸入命令

> personal.newAccount('123456')
"0xee340225c3b9348847c828881a34ae3237e91456"

得到解鎖密碼爲123456,錢包地址爲"0xee340225c3b9348847c828881a34ae3237e91456"的以太坊錢包,第一個創建的錢包地址會自動作爲默認錢包,挖礦得到的ETH會存入默認錢包中,查看默認錢包:

> eth.coinbase
"0xee340225c3b9348847c828881a34ae3237e91456"

錢包列表

列出所有錢包

> eth.accounts
["0xee340225c3b9348847c828881a34ae3237e91456"]

查詢餘額

> eth.getBalance('0xee340225c3b9348847c828881a34ae3237e91456')
0

或者根據錢包列表的序號

> eth.getBalance(eth.accounts[0])
0

如果是默認錢包還可以這樣

> eth.getBalance(eth.coinbase)
0

開啓挖礦

挖礦會生成區塊和執行交易,挖礦得到的ETH會存入默認錢包中,如果想修改默認錢包可以用以下命令

> miner.setEtherbase("0xa91Ae941e92eb6Fa78FE0d8215F01cbE6b7C014c")

開啓挖礦,1表示1個線程

> miner.start(1)

INFO [12-20|00:16:58.760] Generating DAG in progress               epoch=0 percentage=0 elapsed=4.311s
INFO [12-20|00:17:03.084] Generating DAG in progress               epoch=0 percentage=1 elapsed=8.635s
INFO [12-20|00:17:07.498] Generating DAG in progress               epoch=0 percentage=2 elapsed=13.049s
INFO [12-20|00:17:11.975] Generating DAG in progress               epoch=0 percentage=3 elapsed=17.526s
INFO [12-20|00:17:16.302] Generating DAG in progress               epoch=0 percentage=4 elapsed=21.853s
INFO [12-20|00:17:20.855] Generating DAG in progress               epoch=0 percentage=5 
......

第一次啓動挖礦會先生成挖礦所需的DAG文件,這個過程有點慢,大概需要10分鐘,等到 percentage = 100後,就會開始挖礦,此時屏幕會被挖礦信息刷屏。看到類似下面的信息表示挖礦成功

INFO [12-20|00:42:41.957] 🔗 block reached canonical chain          number=2 hash=9a053e…3b1a92
INFO [12-20|00:42:41.957] 🔨 mined potential block                  number=9 hash=3067bd…6eb8e3
INFO [12-20|00:42:41.978] Commit new mining work                   number=10 sealhash=3958ea…fc97b1 uncles=0 txs=0 gas=0 fees=0 elapsed=103.483µs
INFO [12-20|00:42:47.737] Generating DAG in progress               epoch=1 percentage=31 elapsed=17m30.277s
INFO [12-20|00:42:53.206] Successfully sealed new block            number=10 sealhash=3958ea…fc97b1 hash=ae06e1…929511 elapsed=11.228s
INFO [12-20|00:42:53.207] 🔗 block reached canonical chain          number=3  hash=c5335d…b00c0c
INFO [12-20|00:42:53.207] 🔨 mined potential block                  number=10 hash=ae06e1…929511
INFO [12-20|00:42:53.223] Commit new mining work                   number=11 sealhash=512ca2…b5efb9 uncles=0 txs=0 gas=0 fees=0 elapsed=92.66µs
......

此時新打開終端,用 geth attach 命令連接到js控制檯再查詢默認錢包餘額,發現已經有幣了

jian@ubuntu:~$ cd ~/ethereum/data
jian@ubuntu:~/ethereum/data$ geth attach ipc:geth.ipc
> eth.getBalance(eth.coinbase)
175000000000000000000

餘額的單位是GWEI,要轉爲ETH只要除以10的18次方即可,這裏的餘額爲175ETH

停止挖礦

> miner.stop()

區塊高度

區塊高度也叫區塊數量、最新區塊

> eth.blockNumber
186

解鎖錢包

以太坊錢包大概每5分鐘就會被鎖住,交易和創建合約都需要先解鎖,輸入下面的命令和錢包密碼即可解鎖

> personal.unlockAccount("0xee340225c3b9348847c828881a34ae3237e91456") 
Unlock account 0xee340225c3b9348847c828881a34ae3237e91456
Passphrase: 
true

發送交易

發送交易前請先解鎖轉出地錢包並啓動挖礦,然後使用eth.sendTransaction命令發送交易,下面的命令將從eth.accounts[0]轉5個ETH到eth.accounts[1],也可以替換成具體的錢包地址:

> amount = web3.toWei(5,'ether') 
"5000000000000000000" 
> eth.sendTransaction({from:eth.accounts[0],to:eth.accounts[1],value:amount}) 

I0322 19:39:36.300675 internal/ethapi/api.go:1047] Tx(0x0c59f431068937cbe9e230483bc79f59bd7146edc8ff5ec37fea6710adcab825) to: 0x814d39aa21f3eed069f2b21da7b5f857f7343afa 
"0x0c59f431068937cbe9e230483bc79f59bd7146edc8ff5ec37fea6710adcab825" 

發送成功將返回交易哈希,等待挖礦執行交易,再查看餘額可以看到已經到賬

> web3.fromWei(eth.getBalance(eth.accounts[1]),'ether') 
5 

查詢交易

可以根據交易哈希查詢該交易的詳細信息

> eth.getTransaction("0x0c59f431068937cbe9e230483bc79f59bd7146edc8ff5ec37fea6710adcab825") 
{ 
	blockHash: "0xf5d3da50065ce5793c9571a031ad6fe5f1af326a3c4fb7ce16458f4d909c1613", 
	blockNumber: 33, 
	from: "0xc232e2add308136571bb8f9197ba4ae4e5ba9836", 
	gas: 90000, 
	gasPrice: 20000000000, 
	hash: "0x0c59f431068937cbe9e230483bc79f59bd7146edc8ff5ec37fea6710adcab825", 
	input: "0x", 
	nonce: 0, 
	r: "0x433fe5845391b6da3d8aa0d2b53674e09fb6126f0070a600686809b57e4ef77d", 
	s: "0x6b0086fb76c46024f849141074a5bc79c49d5f9a658fd0fedbbe354889c34d8d", 
	to: "0x814d39aa21f3eed069f2b21da7b5f857f7343afa", 
	transactionIndex: 0, 
	v: "0x1b", 
	value: 5000000000000000000 
} 

連接節點

如果局域網內有多個私有鏈節點,可以將這些節點連接起來組成鏈,這些節點會同步相同的區塊和交易,相當於小型的公鏈。但是要將這些節點連接起來需要滿足3個條件:

  • 節點所在的機器,相互之間可以互聯互通網絡
  • 有相同的創世區塊
  • 在啓動私有鏈的geth命令中設置相同的networkid

假設有兩個節點:節點一(ubuntu端)和節點二(windows端),它們的geth啓動命令中的networkid都是1108,首先要知道節點二的enode信息,啓動節點二的私有鏈,在js console中執行下面的命令查看enode信息:

> admin.nodeInfo.enode  
"enode://9e86289ea859ca041f235aed87a091d0cd594b377cbe13e1c5f5a08a8a280e62d4019ac54063ed6a1d0e3c3eaedad0b73c40b99a16a176993f0373ffe92be672@[::]:30304"

複製上面給出的信息添加到admin.Peer的參數中,注意要把enode中的[::]替換成節點二的IP地址。然後在節點一的js console中執行admin.addPeer(),就可以連接到節點二了

> admin.addPeer("enode://9e86289ea859ca041f235aed87a091d0cd594b377cbe13e1c5f5a08a8a280e62d4019ac54063ed6a1d0e3c3eaedad0b73c40b99a16a176993f0373ffe92be672@192.168.1.111:30304") 

連接成功後,節點一和節點二就會開始同步的區塊,保證區塊數據一致。使用admin.peers命令可以查看當前網絡連接在一起的所有節點

> admin.peers
[{
    caps: ["eth/62", "eth/63"],
    id: "070b16d058df62a934bb1b9f6e216f6763d63c51011c559d0ad155ae428294c69aed1e35a46c36a6613989941b67b530ab92c7667bd63e7e8a7a5717a266751f",
    name: "Geth/v1.8.12-stable/linux-amd64/go1.9.2",
    network: {
      inbound: true,
      localAddress: "172.31.238.175:30303",
      remoteAddress: "145.239.0.91:51238",
      static: false,
      trusted: false
    },
    protocols: {
      eth: {
        difficulty: 8.379127609415399e+21,
        head: "0x7ce58e50b8bc9af80c6900729801832ec9a5f86e549930e085f1a208dacf5608",
        version: 63
      }
    }
}, {
    caps: ["eth/62", "eth/63"],
    id: "19211a5d0fbcabf5184e49639839bda772080cba500a4a0e9ced1f89c3a43fea1c00d4d8de5a463d6002bfa594e46e2c03611a6514117f1bd8c7e889bfb4ec7a",
    name: "Geth/v1.8.18-unstable-126dfde6/linux-amd64/go1.9.4",
    network: {
      inbound: true,
      localAddress: "172.31.238.175:30303",
      remoteAddress: "120.78.87.202:53548",
      static: false,
      trusted: false
    },
    protocols: {
      eth: {
        difficulty: 5.481064970682881e+21,
        head: "0xdb982b1ff69083b090549c3809dd6b58f87ca50db97e0b4144aac9269ad198be",
        version: 63
      }
    }
}]

參考文章

[1] https://my.oschina.net/u/2349981/blog/856956

[2] http://blog.csdn.net/u013096666/article/details/72639906

[3] http://qiita.com/oggata/items/eea4d5e37f38785f6079

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章