原文見 深刻理解Fabric環境搭建的詳細過程 http://www.cnblogs.com/studyzy/p/7451276.html 深藍居html
一、生成cryptogen(密碼生成器)node
咱們將使用cryptogen
工具爲咱們生成各類網絡實體的加密材料(x509證書)。這些證書是身份的表明,它們容許在咱們的網絡實體進行交流和交易時進行簽名/驗證身份驗證。git
Fabric中有兩種類型的公私鑰和證書,一種是給節點以前通信安全而準備的TLS證書,另外一種是用戶登陸和權限控制的用戶證書。這些證書原本應該是由CA來頒發,可是咱們這裏是測試環境,並無啓用CA節點,因此Fabric幫咱們提供了一個工具:cryptogen。github
cd /root/go/src/github.com/hyperledger/fabric
make cryptogen
結果以下:算法
build/bin/cryptogen CGO_CFLAGS=" " GOBIN=/root/go/src/github.com/hyperledger/fabric/build/bin go install -tags "" -ldflags "-X github.com/hyperledger/fabric/common/tools/cryptogen/metadata.Version=1.0.0" github.com/hyperledger/fabric/common/tools/cryptogen Binary available as build/bin/cryptogen
即 在./build/bin/cryptogen下生成了可執行程序cryptogendocker
二、配置網絡拓撲 crypto-config.yamlubuntu
examples/e2e_cli/crypto-config.yaml已經提供了一個Orderer Org和兩個Peer Org的配置,該模板中也對字段進行了註釋。咱們能夠把Org2拿來分析一下:安全
- Name: Org2 Domain: org2.example.com Template: Count: 2 Users: Count: 1
Name和Domain就是關於這個組織的名字和域名,這主要是用於生成證書的時候,證書內會包含該信息。而Template Count=2是說咱們要生成2套公私鑰和證書,一套是peer0.org2的,還有一套是peer1.org2的。最後Users. Count=1是說每一個Template下面會有幾個普通User(注意,Admin是Admin,不包含在這個計數中),這裏配置了1,也就是說咱們只須要一個普通用戶User1@org2.example.com 咱們能夠根據實際須要調整這個配置文件,增刪Org Users等。bash
三、使用加密生成器 cryptogen 根據網絡拓撲 crypto-config.yaml生成相應證書網絡
cd examples/e2e_cli/
../../build/bin/cryptogen generate --config=./crypto-config.yaml
生成的文件都保存到crypto-config
root@ubuntu:~/go/src/github.com/hyperledger/fabric/examples/e2e_cli# tree crypto-config crypto-config ├── ordererOrganizations │ └── example.com │ ├── ca │ │ ├── 7f4a6ddd14cd63850d8efb5fac786754550e606b7066187f248e6d1195a14401_sk │ │ └── ca.example.com-cert.pem │ ├── msp │ │ ├── admincerts │ │ │ └── Admin@example.com-cert.pem │ │ ├── cacerts │ │ │ └── ca.example.com-cert.pem │ │ └── tlscacerts │ │ └── tlsca.example.com-cert.pem │ ├── orderers │ │ └── orderer.example.com │ │ ├── msp │ │ │ ├── admincerts │ │ │ │ └── Admin@example.com-cert.pem │ │ │ ├── cacerts │ │ │ │ └── ca.example.com-cert.pem │ │ │ ├── keystore │ │ │ │ └── 61c8982b11f2c6038f3267b087ab3b2df189402b4d589f08373eef810b99196e_sk │ │ │ ├── signcerts │ │ │ │ └── orderer.example.com-cert.pem │ │ │ └── tlscacerts │ │ │ └── tlsca.example.com-cert.pem │ │ └── tls │ │ ├── ca.crt │ │ ├── server.crt │ │ └── server.key │ ├── tlsca │ │ ├── 75269ee7ffe06ce787e1fa84f0e13b6d806af2e3248cac26c215f05ceaa4cff5_sk │ │ └── tlsca.example.com-cert.pem │ └── users │ └── Admin@example.com │ ├── msp │ │ ├── admincerts │ │ │ └── Admin@example.com-cert.pem │ │ ├── cacerts │ │ │ └── ca.example.com-cert.pem │ │ ├── keystore │ │ │ └── d500892fdee19e1e5d533848b168232416fc9ff813e7472ac2978f38491f4093_sk │ │ ├── signcerts │ │ │ └── Admin@example.com-cert.pem │ │ └── tlscacerts │ │ └── tlsca.example.com-cert.pem │ └── tls │ ├── ca.crt │ ├── server.crt │ └── server.key └── peerOrganizations ├── org1.example.com │ ├── ca │ │ ├── 56c8383863e4c4571ec98d4191bdaa95f715ad0480728cb23ca3b4ee0d160960_sk │ │ └── ca.org1.example.com-cert.pem │ ├── msp │ │ ├── admincerts │ │ │ └── Admin@org1.example.com-cert.pem │ │ ├── cacerts │ │ │ └── ca.org1.example.com-cert.pem │ │ └── tlscacerts │ │ └── tlsca.org1.example.com-cert.pem │ ├── peers │ │ ├── peer0.org1.example.com │ │ │ ├── msp │ │ │ │ ├── admincerts │ │ │ │ │ └── Admin@org1.example.com-cert.pem │ │ │ │ ├── cacerts │ │ │ │ │ └── ca.org1.example.com-cert.pem │ │ │ │ ├── keystore │ │ │ │ │ └── 69ab0d05da63a094ff178579d6bfa7689ace565d707d4f0556b529e0ed3c865d_sk │ │ │ │ ├── signcerts │ │ │ │ │ └── peer0.org1.example.com-cert.pem │ │ │ │ └── tlscacerts │ │ │ │ └── tlsca.org1.example.com-cert.pem │ │ │ └── tls │ │ │ ├── ca.crt │ │ │ ├── server.crt │ │ │ └── server.key │ │ └── peer1.org1.example.com │ │ ├── msp │ │ │ ├── admincerts │ │ │ │ └── Admin@org1.example.com-cert.pem │ │ │ ├── cacerts │ │ │ │ └── ca.org1.example.com-cert.pem │ │ │ ├── keystore │ │ │ │ └── 795723f2c98428db01188afd1c68404824461295ea5412e7ea434c96a9b255cf_sk │ │ │ ├── signcerts │ │ │ │ └── peer1.org1.example.com-cert.pem │ │ │ └── tlscacerts │ │ │ └── tlsca.org1.example.com-cert.pem │ │ └── tls │ │ ├── ca.crt │ │ ├── server.crt │ │ └── server.key │ ├── tlsca │ │ ├── 12c9f413aa40d0da1bf9b333fbd2cf88281c57b0c4d6caf3c10bc93a32fe7067_sk │ │ └── tlsca.org1.example.com-cert.pem │ └── users │ ├── Admin@org1.example.com │ │ ├── msp │ │ │ ├── admincerts │ │ │ │ └── Admin@org1.example.com-cert.pem │ │ │ ├── cacerts │ │ │ │ └── ca.org1.example.com-cert.pem │ │ │ ├── keystore │ │ │ │ └── 6aa5f66155befc289a9a503b3acff7a4081925cd7f6b37be84ad06a03c5d03ea_sk │ │ │ ├── signcerts │ │ │ │ └── Admin@org1.example.com-cert.pem │ │ │ └── tlscacerts │ │ │ └── tlsca.org1.example.com-cert.pem │ │ └── tls │ │ ├── ca.crt │ │ ├── server.crt │ │ └── server.key │ └── User1@org1.example.com │ ├── msp │ │ ├── admincerts │ │ │ └── User1@org1.example.com-cert.pem │ │ ├── cacerts │ │ │ └── ca.org1.example.com-cert.pem │ │ ├── keystore │ │ │ └── f5ec31dffdd4eb3bfb24ed62749106155b40a2cdb8eef97b03a25aa5ba24edf4_sk │ │ ├── signcerts │ │ │ └── User1@org1.example.com-cert.pem │ │ └── tlscacerts │ │ └── tlsca.org1.example.com-cert.pem │ └── tls │ ├── ca.crt │ ├── server.crt │ └── server.key └── org2.example.com ├── ca │ ├── 979d49a2f3c80d0716ee151d155387b3070e7af533c27ded276be8b37843386c_sk │ └── ca.org2.example.com-cert.pem ├── msp │ ├── admincerts │ │ └── Admin@org2.example.com-cert.pem │ ├── cacerts │ │ └── ca.org2.example.com-cert.pem │ └── tlscacerts │ └── tlsca.org2.example.com-cert.pem ├── peers │ ├── peer0.org2.example.com │ │ ├── msp │ │ │ ├── admincerts │ │ │ │ └── Admin@org2.example.com-cert.pem │ │ │ ├── cacerts │ │ │ │ └── ca.org2.example.com-cert.pem │ │ │ ├── keystore │ │ │ │ └── eca432632ac8a57b094669d1a4ab93710fd59126f0f671ed4bdb07d1bbe94738_sk │ │ │ ├── signcerts │ │ │ │ └── peer0.org2.example.com-cert.pem │ │ │ └── tlscacerts │ │ │ └── tlsca.org2.example.com-cert.pem │ │ └── tls │ │ ├── ca.crt │ │ ├── server.crt │ │ └── server.key │ └── peer1.org2.example.com │ ├── msp │ │ ├── admincerts │ │ │ └── Admin@org2.example.com-cert.pem │ │ ├── cacerts │ │ │ └── ca.org2.example.com-cert.pem │ │ ├── keystore │ │ │ └── 609ab03ad06149a68d370b8f8ed45f8bef44cbac52ce7b39125129e33c5ffad5_sk │ │ ├── signcerts │ │ │ └── peer1.org2.example.com-cert.pem │ │ └── tlscacerts │ │ └── tlsca.org2.example.com-cert.pem │ └── tls │ ├── ca.crt │ ├── server.crt │ └── server.key ├── tlsca │ ├── 1fd065823b9b49550d484aa7312abbb8ef0f062cba9b7e349e94c11effbd2ef3_sk │ └── tlsca.org2.example.com-cert.pem └── users ├── Admin@org2.example.com │ ├── msp │ │ ├── admincerts │ │ │ └── Admin@org2.example.com-cert.pem │ │ ├── cacerts │ │ │ └── ca.org2.example.com-cert.pem │ │ ├── keystore │ │ │ └── 8096a721270a5f9e39a0272ee648bd6cb2b273fa8b5fac27eeda7cd4bd16da04_sk │ │ ├── signcerts │ │ │ └── Admin@org2.example.com-cert.pem │ │ └── tlscacerts │ │ └── tlsca.org2.example.com-cert.pem │ └── tls │ ├── ca.crt │ ├── server.crt │ └── server.key └── User1@org2.example.com ├── msp │ ├── admincerts │ │ └── User1@org2.example.com-cert.pem │ ├── cacerts │ │ └── ca.org2.example.com-cert.pem │ ├── keystore │ │ └── 59ff8e940b27192f1f782f3d22186202242413eabbfb192cf5b432488ef62612_sk │ ├── signcerts │ │ └── User1@org2.example.com-cert.pem │ └── tlscacerts │ └── tlsca.org2.example.com-cert.pem └── tls ├── ca.crt ├── server.crt └── server.key
四、編譯生成 交易生成器configtxgen
cd ~/go/src/github.com/hyperledger/fabric make configtxgen
運行後的結果爲:
build/bin/configtxgen CGO_CFLAGS=" " GOBIN=/home/studyzy/go/src/github.com/hyperledger/fabric/build/bin go install -tags "nopkcs11" -ldflags "-X github.com/hyperledger/fabric/common/configtx/tool/configtxgen/metadata.Version=1.0.0" github.com/hyperledger/fabric/common/configtx/tool/configtxgen Binary available as build/bin/configtxgen
官方提供的examples/e2e_cli/configtx.yaml這個文件裏面配置了由2個Org參與的Orderer共識配置TwoOrgsOrdererGenesis,以及由2個Org參與的Channel配置:TwoOrgsChannel。Orderer能夠設置共識的算法是Solo仍是Kafka,以及共識時區塊大小,超時時間等,咱們使用默認值便可,不用更改。而Peer節點的配置包含了MSP的配置,錨節點的配置。若是咱們有更多的Org,或者有更多的Channel,那麼就能夠根據模板進行對應的修改。
六、經過交易生成器configtxgen生成區塊
(1)生成創世區塊
配置修改好後,咱們就用configtxgen 生成創世區塊。並把這個區塊保存到本地channel-artifacts文件夾中:
cd examples/e2e_cli/ ../../build/bin/configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
(2)生成channel區塊
../../build/bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
另外關於錨節點的更新,咱們也須要使用這個程序來生成文件:
../../build/bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP ../../build/bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
最終,咱們在channel-artifacts文件夾中,應該是可以看到4個文件。
root@ubuntu:~/go/src/github.com/hyperledger/fabric/examples/e2e_cli# tree channel-artifacts/ channel-artifacts/ ├── channel.tx ├── genesis.block ├── Org1MSPanchors.tx └── Org2MSPanchors.tx
七、配置Fabric環境的docker-compose文件
(1)配置Orderer
Orderer的配置是在base/docker-compose-base.yaml裏
version: '2' services: orderer.example.com: container_name: orderer.example.com image: hyperledger/fabric-orderer environment: - ORDERER_GENERAL_LOGLEVEL=debug - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0 - ORDERER_GENERAL_GENESISMETHOD=file - ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block - ORDERER_GENERAL_LOCALMSPID=OrdererMSP - ORDERER_GENERAL_LOCALMSPDIR=/var/hyperledger/orderer/msp # enabled TLS - ORDERER_GENERAL_TLS_ENABLED=true - ORDERER_GENERAL_TLS_PRIVATEKEY=/var/hyperledger/orderer/tls/server.key - ORDERER_GENERAL_TLS_CERTIFICATE=/var/hyperledger/orderer/tls/server.crt - ORDERER_GENERAL_TLS_ROOTCAS=[/var/hyperledger/orderer/tls/ca.crt] working_dir: /opt/gopath/src/github.com/hyperledger/fabric command: orderer volumes: - ../channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block - ../crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/msp:/var/hyperledger/orderer/msp - ../crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/tls/:/var/hyperledger/orderer/tls ports: - 7050:7050
這裏主要關心的是,ORDERER_GENERAL_GENESISFILE=/var/hyperledger/orderer/orderer.genesis.block,而這個創世區塊就是咱們以前建立的創世區塊,這裏就是Host到Docker的映射:
- ../channel-artifacts/genesis.block:/var/hyperledger/orderer/orderer.genesis.block
另外的配置主要是TL,Log等,最後暴露出服務端口7050。、
(2)配置peer
Peer的配置是在base/docker-compose-base.yaml和peer-base.yaml裏
docker-compose-base.yaml中截取peer0.org1.example.com配置
peer0.org1.example.com: container_name: peer0.org1.example.com extends: file: peer-base.yaml service: peer-base environment: - CORE_PEER_ID=peer0.org1.example.com - CORE_PEER_ADDRESS=peer0.org1.example.com:7051 - CORE_PEER_CHAINCODELISTENADDRESS=peer0.org1.example.com:7052 - CORE_PEER_GOSSIP_EXTERNALENDPOINT=peer0.org1.example.com:7051 - CORE_PEER_LOCALMSPID=Org1MSP volumes: - /var/run/:/host/var/run/ - ../crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp:/etc/hyperledger/fabric/msp - ../crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls:/etc/hyperledger/fabric/tls ports: - 7051:7051 - 7052:7052 - 7053:7053
peer-base.yaml
version: '2' services: peer-base: image: hyperledger/fabric-peer environment: - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock # the following setting starts chaincode containers on the same # bridge network as the peers # https://docs.docker.com/compose/networking/ - CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=e2ecli_default #- CORE_LOGGING_LEVEL=ERROR - CORE_LOGGING_LEVEL=DEBUG - CORE_PEER_TLS_ENABLED=true - CORE_PEER_GOSSIP_USELEADERELECTION=true - CORE_PEER_GOSSIP_ORGLEADER=false - CORE_PEER_PROFILE_ENABLED=true - CORE_PEER_TLS_CERT_FILE=/etc/hyperledger/fabric/tls/server.crt - CORE_PEER_TLS_KEY_FILE=/etc/hyperledger/fabric/tls/server.key - CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/tls/ca.crt working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer command: peer node start
(3)配置CLI
CLI在整個Fabric網絡中扮演客戶端的角色,咱們在開發測試的時候能夠用CLI來代替SDK,執行各類SDK能執行的操做。CLI會和Peer相連,把指令發送給對應的Peer執行。CLI的配置在docker-compose-cli.yaml中
# Copyright IBM Corp. All Rights Reserved. services: orderer.example.com: extends: file: base/docker-compose-base.yaml service: orderer.example.com container_name: orderer.example.com peer0.org1.example.com: container_name: peer0.org1.example.com extends: file: base/docker-compose-base.yaml service: peer0.org1.example.com peer1.org1.example.com: container_name: peer1.org1.example.com extends: file: base/docker-compose-base.yaml service: peer1.org1.example.com peer0.org2.example.com: container_name: peer0.org2.example.com extends: file: base/docker-compose-base.yaml service: peer0.org2.example.com peer1.org2.example.com: container_name: peer1.org2.example.com extends: file: base/docker-compose-base.yaml service: peer1.org2.example.com cli: container_name: cli image: hyperledger/fabric-tools tty: true environment: - GOPATH=/opt/gopath - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock - CORE_LOGGING_LEVEL=DEBUG - CORE_PEER_ID=cli - CORE_PEER_ADDRESS=peer0.org1.example.com:7051 - CORE_PEER_LOCALMSPID=Org1MSP - CORE_PEER_TLS_ENABLED=true - CORE_PEER_TLS_CERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.crt - CORE_PEER_TLS_KEY_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/server.key - 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 - CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp working_dir: /opt/gopath/src/github.com/hyperledger/fabric/peer command: /bin/bash -c './scripts/script.sh ${CHANNEL_NAME}; sleep $TIMEOUT' volumes: - /var/run/:/host/var/run/ - ../chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go - ./crypto-config:/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ - ./scripts:/opt/gopath/src/github.com/hyperledger/fabric/peer/scripts/ - ./channel-artifacts:/opt/gopath/src/github.com/hyperledger/fabric/peer/channel-artifacts depends_on: - orderer.example.com - peer0.org1.example.com - peer1.org1.example.com - peer0.org2.example.com - peer1.org2.example.com
從這裏咱們能夠看到,CLI啓動的時候默認鏈接的是peer0.org1.example.com,而且啓用了TLS。默認是以Admin@org1.example.com這個身份鏈接到Peer的。CLI啓動的時候,會去執行./scripts/script.sh 腳本,這個腳本也就是fabric/examples/e2e_cli/scripts/script.sh 這個腳本,這個腳本完成了Fabric環境的初始化和ChainCode的安裝及運行,也就是接下來要講的步驟4和5.在文件映射配置上,咱們注意到../chaincode/go/:/opt/gopath/src/github.com/hyperledger/fabric/examples/chaincode/go,也就是說咱們要安裝的ChainCode都是在fabric/examples/chaincode/go目錄下,之後咱們要開發本身的ChainCode,只須要把咱們的代碼複製到該目錄便可。
八、初始化fabric環境
咱們將整個Fabric Docker環境的配置放在docker-compose-cli.yaml後,只須要使用如下命令便可:
docker-compose -f docker-compose-cli.yaml up -d
最後這個-d參數若是不加,那麼當前終端就會一直附加在docker-compose上,而若是加上的話,那麼docker容器就在後臺運行。運行docker ps命令能夠看啓動的結果:1Orderer+4Peer+1CLI
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES e90ce174a429 hyperledger/fabric-tools "/bin/bash" 25 seconds ago Up 23 seconds cli 32ffc851c1bf hyperledger/fabric-peer "peer node start" 32 seconds ago Up 25 seconds 0.0.0.0:9051->7051/tcp, 0.0.0.0:9052->7052/tcp, 0.0.0.0:9053->7053/tcp peer0.org2.example.com 1f0d26e45634 hyperledger/fabric-peer "peer node start" 32 seconds ago Up 26 seconds 0.0.0.0:8051->7051/tcp, 0.0.0.0:8052->7052/tcp, 0.0.0.0:8053->7053/tcp peer1.org1.example.com 807c4dc97684 hyperledger/fabric-orderer "orderer" 32 seconds ago Up 24 seconds 0.0.0.0:7050->7050/tcp orderer.example.com c297c88cf9b3 hyperledger/fabric-peer "peer node start" 32 seconds ago Up 26 seconds 0.0.0.0:7051-7053->7051-7053/tcp peer0.org1.example.com 08c95a2bbb12 hyperledger/fabric-peer "peer node start" 32 seconds ago Up 25 seconds 0.0.0.0:10051->7051/tcp, 0.0.0.0:10052->7052/tcp, 0.0.0.0:10053->7053/tcp peer1.org2.example.com
(2)建立Channel
如今咱們要進入cli容器內部,在裏面建立Channel。先用如下命令進入CLI內部Bash:
docker exec -it cli bash
建立Channel的命令是peer channel create,咱們前面建立2.4建立Channel的配置區塊時,指定了Channel的名字是mychannel,那麼這裏咱們必須建立一樣名字的Channel。
ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem peer channel create -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/channel.tx --tls true --cafile $ORDERER_CA
執行該命令後,系統會提示:
2017-08-29 20:36:47.486 UTC [channelCmd] readBlock -> DEBU 020 Received block:0
系統會在cli內部的當前目錄建立一個mychannel.block文件,這個文件很是重要,接下來其餘節點要加入這個Channel就必須使用這個文件。
(3)各個Peer加入Channel
前面說過,咱們CLI默認鏈接的是peer0.org1,那麼咱們要將這個Peer加入mychannel就很簡單,只須要運行以下命令:
peer channel join -b mychannel.block
系統返回消息:
2017-08-29 20:40:27.053 UTC [channelCmd] executeJoin -> INFO 006 Peer joined the channel!
那麼其餘幾個Peer又該怎麼加入Channel呢?這裏就須要修改CLI的環境變量,使其指向另外的Peer。好比咱們要把peer1.org1加入mychannel,那麼命令是:
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 CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp CORE_PEER_ADDRESS=peer1.org1.example.com:7051 peer channel join -b mychannel.block
系統會返回成功加入Channel的消息。
一樣的方法,將peer0.org2加入mychannel:
CORE_PEER_LOCALMSPID="Org2MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp CORE_PEER_ADDRESS=peer0.org2.example.com:7051 peer channel join -b mychannel.block
最後把peer1.org2加入mychannel:
CORE_PEER_LOCALMSPID="Org2MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer1.org2.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp CORE_PEER_ADDRESS=peer1.org2.example.com:7051 peer channel join -b mychannel.block
(4)更新錨節點
關於AnchorPeer,我理解的不夠深入,通過個人測試,即便沒有設置錨節點的狀況下,整個Fabric網絡仍然是能正常運行的。
對於Org1來講,peer0.org1是錨節點,咱們須要鏈接上它並更新錨節點:
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 CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp CORE_PEER_ADDRESS=peer0.org1.example.com:7051 peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls true --cafile $ORDERER_CA
另外對於Org2,peer0.org2是錨節點,對應的更新代碼是:
CORE_PEER_LOCALMSPID="Org2MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp CORE_PEER_ADDRESS=peer0.org2.example.com:7051 peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls true --cafile $ORDERER_CA
以上,整個Fabric網絡和Channel都準備完畢,接下來咱們來安裝和運行ChainCode。這裏仍然以最出名的Example02爲例。這個例子實現了a,b兩個帳戶,相互之間能夠轉帳。
九、鏈上代碼的安裝與運行
(1)Install ChainCode安裝鏈上代碼
鏈上代碼的安裝須要在各個相關的Peer上進行,對於咱們如今這種Fabric網絡,若是4個Peer都想對Example02進行操做,那麼就須要安裝4次。
仍然是保持在CLI的命令行下,咱們先切換到peer0.org1這個節點:
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 CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp CORE_PEER_ADDRESS=peer0.org1.example.com:7051
使用peer chaincode install命令能夠安裝指定的ChainCode並對其命名:
peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
安裝的過程其實就是對CLI中指定的代碼進行編譯打包,並把打包好的文件發送到Peer,等待接下來的實例化。
其餘節點因爲暫時還沒使用到,咱們能夠先不安裝,等到了步驟5.4的時候再安裝。
(2)Instantiate ChainCode實例化鏈上代碼
實例化鏈上代碼主要是在Peer所在的機器上對前面安裝好的鏈上代碼進行包裝,生成對應Channel的Docker鏡像和Docker容器。而且在實例化時咱們能夠指定背書策略。咱們運行如下命令完成實例化:
peer chaincode instantiate -o orderer.example.com:7050 --tls true --cafile $ORDERER_CA -C mychannel -n mycc -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')"
若是咱們新開一個Ubuntu終端,去查看peer0.org1上的日誌,那麼就能夠知道整個實例化的過程到底幹了什麼:
docker logs -f peer0.org1.example.com
主要幾行重要的日誌:
2017-08-29 21:14:12.290 UTC [chaincode-platform] generateDockerfile -> DEBU 3fd FROM hyperledger/fabric-baseos:x86_64-0.3.1 ADD binpackage.tar /usr/local/bin LABEL org.hyperledger.fabric.chaincode.id.name="mycc" \ org.hyperledger.fabric.chaincode.id.version="1.0" \ org.hyperledger.fabric.chaincode.type="GOLANG" \ org.hyperledger.fabric.version="1.0.0" \ org.hyperledger.fabric.base.version="0.3.1" ENV CORE_CHAINCODE_BUILDLEVEL=1.0.0 ENV CORE_PEER_TLS_ROOTCERT_FILE=/etc/hyperledger/fabric/peer.crt COPY peer.crt /etc/hyperledger/fabric/peer.crt 2017-08-29 21:14:12.297 UTC [util] DockerBuild -> DEBU 3fe Attempting build with image hyperledger/fabric-ccenv:x86_64-1.0.0 2017-08-29 21:14:48.907 UTC [dockercontroller] deployImage -> DEBU 3ff Created image: dev-peer0.org1.example.com-mycc-1.0 2017-08-29 21:14:48.908 UTC [dockercontroller] Start -> DEBU 400 start-recreated image successfully 2017-08-29 21:14:48.908 UTC [dockercontroller] createContainer -> DEBU 401 Create container: dev-peer0.org1.example.com-mycc-1.0
接下來的日誌就是各類初始化,驗證,寫帳本之類的。總之完畢後,咱們回到Ubuntu終端,使用docker ps能夠看到有新的容器正在運行:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES fc620b8c6399 dev-peer0.org1.example.com-mycc-1.0 "chaincode -peer.add…" 3 minutes ago Up 2 minutes dev-peer0.org1.example.com-mycc-1.0 e90ce174a429 hyperledger/fabric-tools "/bin/bash" 31 minutes ago Up 30 minutes cli 32ffc851c1bf hyperledger/fabric-peer "peer node start" 31 minutes ago Up 31 minutes 0.0.0.0:9051->7051/tcp, 0.0.0.0:9052->7052/tcp, 0.0.0.0:9053->7053/tcp peer0.org2.example.com 1f0d26e45634 hyperledger/fabric-peer "peer node start" 31 minutes ago Up 31 minutes 0.0.0.0:8051->7051/tcp, 0.0.0.0:8052->7052/tcp, 0.0.0.0:8053->7053/tcp peer1.org1.example.com 807c4dc97684 hyperledger/fabric-orderer "orderer" 31 minutes ago Up 31 minutes 0.0.0.0:7050->7050/tcp orderer.example.com c297c88cf9b3 hyperledger/fabric-peer "peer node start" 31 minutes ago Up 31 minutes 0.0.0.0:7051-7053->7051-7053/tcp peer0.org1.example.com 08c95a2bbb12 hyperledger/fabric-peer "peer node start" 31 minutes ago Up 31 minutes 0.0.0.0:10051->7051/tcp, 0.0.0.0:10052->7052/tcp, 0.0.0.0:10053->7053/tcp peer1.org2.example.com
如今鏈上代碼的實例也有了,而且在實例化的時候指定了a帳戶100,b帳戶200,咱們能夠試着調用ChainCode的查詢代碼,驗證一下,在cli容器內執行:
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
返回結果:Query Result: 100
接下來咱們能夠試着把a帳戶的10元轉給b。對應的代碼:
peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile $ORDERER_CA -C mychannel -n mycc -c '{"Args":["invoke","a","b","10"]}'
(4)在另外一個節點上查詢交易
前面的操做都是在org1下面作的,那麼處於同一個區塊鏈(同一個Channel下)的org2,是否會看org1的更改呢?咱們試着給peer0.org2安裝鏈上代碼:
CORE_PEER_LOCALMSPID="Org2MSP" CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp CORE_PEER_ADDRESS=peer0.org2.example.com:7051 peer chaincode install -n mycc -v 1.0 -p github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02
因爲mycc已經在前面org1的時候實例化了,也就是說對應的區塊已經生成了,因此在org2不能再次初始化。咱們直接運行查詢命令:
peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
返回結果:
Query Result: 90
這是由於peer0.org2也須要生成Docker鏡像,建立對應的容器,才能經過容器返回結果。咱們回到Ubuntu終端,執行docker ps,能夠看到又多了一個容器:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 1dde91b29472 dev-peer0.org2.example.com-mycc-1.0 "chaincode -peer.add…" About a minute ago Up About a minute dev-peer0.org2.example.com-mycc-1.0 fc620b8c6399 dev-peer0.org1.example.com-mycc-1.0 "chaincode -peer.add…" 11 minutes ago Up 11 minutes dev-peer0.org1.example.com-mycc-1.0 e90ce174a429 hyperledger/fabric-tools "/bin/bash" 39 minutes ago Up 39 minutes cli 32ffc851c1bf hyperledger/fabric-peer "peer node start" 39 minutes ago Up 39 minutes 0.0.0.0:9051->7051/tcp, 0.0.0.0:9052->7052/tcp, 0.0.0.0:9053->7053/tcp peer0.org2.example.com 1f0d26e45634 hyperledger/fabric-peer "peer node start" 39 minutes ago Up 39 minutes 0.0.0.0:8051->7051/tcp, 0.0.0.0:8052->7052/tcp, 0.0.0.0:8053->7053/tcp peer1.org1.example.com 807c4dc97684 hyperledger/fabric-orderer "orderer" 39 minutes ago Up 39 minutes 0.0.0.0:7050->7050/tcp orderer.example.com c297c88cf9b3 hyperledger/fabric-peer "peer node start" 39 minutes ago Up 39 minutes 0.0.0.0:7051-7053->7051-7053/tcp peer0.org1.example.com 08c95a2bbb12 hyperledger/fabric-peer "peer node start" 39 minutes ago Up 39 minutes 0.0.0.0:10051->7051/tcp, 0.0.0.0:10052->7052/tcp, 0.0.0.0:10053->7053/tcp peer1.org2.example.com