以太坊學習筆記(一):基於POA的私有鏈搭建

簡介

這裏以太坊的一些特性就不贅述,可以在官網進行查閱:https://www.ethereum.org/。
下面主要介紹基於POA機制的環境的搭建來窺探以太坊的運行機制。

源碼下載及編譯

1、git clone https://github.com/ethereum/go-ethereum
2、git checkout -b remotes/origin/release/1.8這裏採用1.8這個分支
3、make all 進行編譯,會產生一些可執行文件。

創建節點目錄

這裏我們創建node1,node2,node3,node4節點目錄,這裏假設node3,node4爲挖礦節點

創建賬戶

1、./geth --datadir node1/data account new
2、./geth --datadir node2/data account new
3、./geth --datadir node3/data account new
4、./geth --datadir node4/data account new

其中各節點賬戶爲:

node1:UTC--2019-06-05T02-30-12.367011247Z--def0ad9fc2fb1633b0c64587e42e3c5201264187
node2:UTC--2019-06-05T02-30-35.034825952Z--3ceafa56c08c0353b062beec0646a3e2234c5df1
node3:UTC--2019-06-05T02-31-12.018584833Z--23e46dc2a103314cc3ee8085734bf17ebc18249e
node4:UTC--2019-06-05T02-31-20.086986887Z--5e390a88062c4f8eff2686cdef5b4c5abe7d7f7e

生成創世塊文件

利用編譯生成的客戶端交互工具puppeth可以生成創世塊文件。
直接運行./puppeth按照提示選擇即可,最後生成poa_test.json文件。
需要注意的是:
1、指定networkid這裏指定爲666,否則隨機生成
2、輸入指定的挖礦賬戶,這裏我們把node3的賬戶(23e46dc2a103314cc3ee8085734bf17ebc18249e)上去即可。
3、指定初始化賬戶餘額,這裏指定node1,node3賬戶,下面會介紹轉賬的操作。

初始化私有鏈

1、./geth --datadir node1/data init poa_test.json
2、./geth --datadir node2/data init poa_test.json
3、./geth --datadir node3/data init poa_test.json
4、./geth --datadir node4/data init poa_test.json

啓動節點

./geth --datadir node1/data --networkid 666 --port 3000 console
./geth --datadir node2/data --networkid 666 --port 3001 console

./geth --datadir node3/data --networkid 666 --port 3002 --unlock 23e46dc2a103314cc3ee8085734bf17ebc18249e  console

./geth --datadir node4/data --networkid 666 --port 3003 console

建立節點通信

1、在node2,node3,node4的console對話框分別進行操作,即去連接node1:

admin.addPeer("enode://e4bcc55c8798a5dabd6ee5335816471d97f21588874da48ceaccefca6058aa84c931df2ad03484a0fc41445186b9febef092cc97829e5f51f5ad2932a2d07e5b@127.0.0.1:3000")

2、在node1console查看:

 >admin.peers
[{
    caps: ["eth/62", "eth/63"],
    enode: "enode://61b428c3830f19ab1374302dbc8baeab5b89805fcff58521c249f9e26b43ab357c13a48ac89211666b66615f65eafdaa75bc27e3e42afdb58bdb3082b5b2d5ba@127.0.0.1:53830",
    id: "6ac0f60d47a27751015fe2f2441c56f469584bf6e0c2d3fb92b5cb03278aa3c7",
    name: "Geth/v1.9.0-unstable-15f24ff1-20190603/darwin-amd64/go1.10.3",
    network: {
      inbound: true,
      localAddress: "127.0.0.1:3000",
      remoteAddress: "127.0.0.1:53830",
      static: false,
      trusted: false
    },
    protocols: {
      eth: {
        difficulty: 876,
        head: "0x702ec001e9d14c9854167e79a0cfa6b4bc8cec39d142dbbf2d3ed9e7bae4936e",
        version: 63
      }
    }
}, {
    caps: ["eth/63"],
    enode: "enode://b8ae6c86e9556c3703236e2529e7b54669f785b212a09a35f9f38e8c2f4bf579da9b9c632c23547f4bb09881b1099a389f10fe47f88215c9753710dfccd7c4c5@127.0.0.1:49525",
    id: "b50b4a1e1dea303e165838cc1ac1e120d5f6927d1f69e29cefd50f41118f9e00",
    name: "Geth/v1.9.0-unstable-15f24ff1-20190603/darwin-amd64/go1.10.3",
    network: {
      inbound: true,
      localAddress: "127.0.0.1:3000",
      remoteAddress: "127.0.0.1:49525",
      static: false,
      trusted: false
    },
    protocols: {
      eth: {
        difficulty: 876,
        head: "0x702ec001e9d14c9854167e79a0cfa6b4bc8cec39d142dbbf2d3ed9e7bae4936e",
        version: 63
      }
    }
}, {
    caps: ["eth/63"],
    enode: "enode://e6eacd6fd7f31f9daa46952336e53389be5a25c904fcecc74bb7016ce47527bb7c3f93de46b0c9d697459d91b5410e90670e98cee9c86790830011e7cfe4c4eb@127.0.0.1:49466",
    id: "c38eba4254a81f77e7f50c939c757ac615e68a7135795b539df6db1d8db9e5ea",
    name: "Geth/v1.9.0-unstable-15f24ff1-20190603/darwin-amd64/go1.10.3",
    network: {
      inbound: true,
      localAddress: "127.0.0.1:3000",
      remoteAddress: "127.0.0.1:49466",
      static: false,
      trusted: false
    },
    protocols: {
      eth: {
        difficulty: 1,
        head: "0xd8bccecd1d2434211fa8407ff9d8a0a69258282ffbc65087bc8fa3b80b7489d2",
        version: 63
      }
    }
}]

挖礦

在node3 console執行:

> miner.start()

會產生區塊

INFO [06-05|14:02:40.000] Successfully sealed new block            number=335 sealhash=fca66e…7c68c1 hash=2210af…8321e1 elapsed=28.005s
INFO [06-05|14:02:40.000] ? block reached canonical chain          number=328 hash=286392…2ffdcf
INFO [06-05|14:02:40.000] ? mined potential block                  number=335 hash=2210af…8321e1
INFO [06-05|14:02:40.000] Commit new mining work                   number=336 sealhash=d8dbe5…cb77dc uncles=0 txs=0 gas=0     fees=0       elapsed=398.652µs

轉賬交易

1、在生成創世塊的時候我們指定node1,node3初始化了餘額,可以在相應的console上查詢:

> eth.getBalance("23e46dc2a103314cc3ee8085734bf17ebc18249e")
9.04625697166532776746648320380374280103671755200316906558262648061821325312e+74

2、解鎖賬戶:

> personal
{
  listAccounts: ["0xdef0ad9fc2fb1633b0c64587e42e3c5201264187"],
  listWallets: [{
      accounts: [{...}],
      status: "Locked",
      url: "keystore:///Users/gds/Desktop/work/Goworkspace/src/go-ethereum/build/bin/node1/data/keystore/UTC--2019-06-05T02-30-12.367011247Z--def0ad9fc2fb1633b0c64587e42e3c5201264187"
  }],
  deriveAccount: function(),
  ecRecover: function(),
  getListAccounts: function(callback),
  ...
  上面可以看出status爲:Locked.需要解鎖:
  > personal.unlockAccount("0xdef0ad9fc2fb1633b0c64587e42e3c5201264187")
Unlock account 0xdef0ad9fc2fb1633b0c64587e42e3c5201264187
Passphrase:
true

3、轉賬操作:

從節點node1轉到node2:
eth.sendTransaction({from:"def0ad9fc2fb1633b0c64587e42e3c5201264187",to:"3ceafa56c08c0353b062beec0646a3e2234c5df1",value:web3.toWei(5e+55,"ether")})

然後可以查詢對應節點賬戶餘額的變化,及區塊產生的信息。

添加新的可信簽名節點node4(挖礦節點)

1、在已經是授權節點node3的console上執行,即給node4節點授權

> clique.propose("0x5e390a88062c4f8eff2686cdef5b4c5abe7d7f7e",true)

此時node3產生區塊需要node4的簽名才能生效:

INFO [06-05|15:52:34.447] Successfully sealed new block            number=531 sealhash=b7b329…91d60a hash=32bf55…86bad0 elapsed=30.239s
INFO [06-05|15:52:34.447] ? mined potential block                  number=531 hash=32bf55…86bad0
INFO [06-05|15:52:34.448] Commit new mining work                   number=532 sealhash=c06e1d…9e0a51 uncles=0 txs=0 gas=0     fees=0       elapsed=939.799µs
INFO [06-05|15:52:34.448] Signed recently, must wait for others

2、node4重新啓動,需要輸入密碼形式

./geth --datadir node4/data --networkid 666 --port 3003 --unlock 5e390a88062c4f8eff2686cdef5b4c5abe7d7f7e console

這樣的話就是node3,node4一起進行挖礦,也就是說需要二者的簽名,區塊才能生成,信息如下:

INFO [06-05|16:03:04.926] Imported new chain segment               blocks=1 txs=0 mgas=0.000 elapsed=627.295µs mgasps=0.000 number=552 hash=defb20…c03f0b dirty=3.30KiB
INFO [06-05|16:03:04.926] ? block reached canonical chain          number=545 hash=67329c…0fac68
INFO [06-05|16:03:04.926] Commit new mining work                   number=553 sealhash=903d45…e7b0ad uncles=0 txs=0 gas=0     fees=0       elapsed=243.707µs
INFO [06-05|16:03:34.396] Successfully sealed new block            number=553 sealhash=903d45…e7b0ad hash=83646f…744fab elapsed=29.476s
INFO [06-05|16:03:34.397] ? mined potential block                  number=553 hash=83646f…744fab
INFO [06-05|16:03:34.397] Commit new mining work                   number=554 sealhash=85ab8f…622c3f uncles=0 txs=0 gas=0     fees=0       elapsed=640.476µs
INFO [06-05|16:03:34.397] Signed recently, must wait for others
INFO [06-05|16:04:04.255] Imported new chain segment               blocks=1 txs=0 mgas=0.000 elapsed=536.534µs mgasps=0.000 number=554 hash=8a8366…adc3fb dirty=3.30KiB
INFO [06-05|16:04:04.255] ? block reached canonical chain          number=547 hash=1b831a…42732a
INFO [06-05|16:04:04.255] Commit new mining work                   number=555 sealhash=2ed698…751942 uncles=0 txs=0 gas=0     fees=0       elapsed=224.242µs

總結

這裏簡單的介紹了基於以太坊POA機制的私有鏈的搭建。相對來講,搭建起來還是比較簡單的,版本上穩定可用。

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