以太坊学习笔记(一):基于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机制的私有链的搭建。相对来讲,搭建起来还是比较简单的,版本上稳定可用。

你可能感兴趣的:(区块链,ethereum)