“First—Network”场景,1个Orderer,2个组织,每个组织2个Peer。

生成素材

生成各个公钥,私钥和CA //todo

启动网络

$  ~ docker ps
CONTAINER ID        IMAGE                        COMMAND             CREATED             STATUS              PORTS                                              NAMES
f053fe5f10c4        hyperledger/fabric-tools     "/bin/bash"         40 minutes ago      Up 40 minutes                                                          cli
a0f6fba6a519        hyperledger/fabric-peer      "peer node start"   40 minutes ago      Up 40 minutes       0.0.0.0:8051->7051/tcp, 0.0.0.0:8053->7053/tcp     peer1.org1.example.com
ba162fa79aca        hyperledger/fabric-peer      "peer node start"   40 minutes ago      Up 40 minutes       0.0.0.0:9051->7051/tcp, 0.0.0.0:9053->7053/tcp     peer0.org2.example.com
ea3bd8c47081        hyperledger/fabric-peer      "peer node start"   40 minutes ago      Up 40 minutes       0.0.0.0:10051->7051/tcp, 0.0.0.0:10053->7053/tcp   peer1.org2.example.com
d9df86737daf        hyperledger/fabric-orderer   "orderer"           40 minutes ago      Up 40 minutes       0.0.0.0:7050->7050/tcp                             orderer.example.com
bff4a03185e5        hyperledger/fabric-peer      "peer node start"   40 minutes ago      Up 40 minutes       0.0.0.0:7051->7051/tcp, 0.0.0.0:7053->7053/tcp     peer0.org1.example.com

首先创建channel

export CHANNEL_NAME=mychannel
peer channel create -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/channel.tx --tls --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

此时,Orderer收到请求,创建channel,并为该channel生成一个区块链

root@d9df86737daf:/var/hyperledger/production/orderer/chains/mychannel# pwd
/var/hyperledger/production/orderer/chains/mychannel
root@d9df86737daf:/var/hyperledger/production/orderer/chains/mychannel# ll
total 20
drwxr-xr-x 2 root root  4096 Jan 25 09:11 ./
drwxr-xr-x 4 root root  4096 Jan 25 09:11 ../
-rw-r----- 1 root root 11980 Jan 25 09:11 blockfile_000000
root@d9df86737daf:/var/hyperledger/production/orderer/chains/mychannel#

这个初始块内包含了类似Orderer自身创世块的9个证书(参见MSP,以及Orderer自己的证书。

$  mychannel grep -a  "END CER" blockfile_000000 -B1|grep -v "END\\\\|\\\\-\\\\-"|sort|uniq |wc -l
      10
$  mychannel grep -a  "END CER" blockfile_000000 -B1|grep -v "END\\\\|\\\\-\\\\-"|sort|uniq|xargs -I{} sh -c "grep -ar {} /Users/nuc/fabric/fabric-samples/first-network/crypto-config;"|grep signcerts|grep orderer.example
/Users/nuc/fabric/fabric-samples/first-network/crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp/signcerts/orderer.example.com-cert.pem:Bezm0h6Hg1Evpizx2MECIGtRP0ahweubMgnUjmSgq7Vf8X+tJF10oA0hxuv+E+SL

加入channel

上一步创建channel后,会返回一个创世块,mychannel.block,节点可以用这个块来加入channel。注意这个块和Orderer生成的块基本一致,除了文件头尾有些不同。

# 从docker提取到本地后查看
$ ls -l
total 48
-rw-r-----  1 nuc  staff  11980  1 25 17:45 blockfile_000000
-rw-r--r--  1 nuc  staff  11987  1 25 17:37 mychannel.block

执行命令,加入channel

CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/[email protected]/msp
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
CORE_PEER_LOCALMSPID="Org1MSP"
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
peer channel join -b mychannel.block

此时,peer0上就有了这个区块链

root@bff4a03185e5:/var/hyperledger/production/ledgersData/chains/chains/mychannel# ll
total 20
drwxr-xr-x 2 root root  4096 Jan 25 09:41 ./
drwxr-xr-x 3 root root  4096 Jan 25 09:41 ../
-rw-r----- 1 root root 11981 Jan 25 09:41 blockfile_000000
root@bff4a03185e5:/var/hyperledger/production/ledgersData/chains/chains/mychannel#

该区块与Orderer的基本一致。