博主以前的文章都是教你們怎麼快速的搭建一個Fabric的環境,可是其中大量的工做都隱藏到了官方的腳本中,並不方便你們深刻理解其中的過程,因此博主這裏就將其中的過程一步步分解,方便你們!html
前面的準備工做我就不用多說了,也就是各類軟件和開發環境的安裝,安裝好之後,咱們git clone下來最新的代碼,並切換到v1.0.0,而且下載好咱們須要使用的docker鏡像,也就是到步驟6,接下來咱們要解析的是以後的步驟,也就是真正的搭建Fabric的過程。node
Fabric中有兩種類型的公私鑰和證書,一種是給節點以前通信安全而準備的TLS證書,另外一種是用戶登陸和權限控制的用戶證書。這些證書原本應該是由CA來頒發,可是咱們這裏是測試環境,並無啓用CA節點,因此Fabric幫咱們提供了一個工具:cryptogen。git
咱們既然得到了Fabric的源代碼,那麼就能夠輕易的使用make命令編譯須要的程序。Fabric官方提供了專門編譯cryptogen的入口,咱們只須要運行如下命令便可:github
cd ~/go/src/github.com/hyperledger/fabric make cryptogen
運行後系統返回結果:算法
build/bin/cryptogen CGO_CFLAGS=" " GOBIN=/home/studyzy/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程序。docker
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
咱們配置好crypto-config.yaml文件後,就能夠用cryptogen去讀取該文件,並生成對應的公私鑰和證書了:網絡
cd examples/e2e_cli/
../../build/bin/cryptogen generate --config=./crypto-config.yaml
生成的文件都保存到crypto-config文件夾,咱們能夠進入該文件夾查看生成了哪些文件:併發
tree crypto-config
與前面1.1說到的相似,咱們能夠經過make命令生成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 生成創世區塊。並把這個區塊保存到本地channel-artifacts文件夾中:
cd examples/e2e_cli/
../../build/bin/configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
../../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個文件。
channel-artifacts/
├── channel.tx
├── genesis.block
├── Org1MSPanchors.tx
└── Org2MSPanchors.tx
前面對節點和用戶的公私鑰以及證書,還有創世區塊都生成完畢,接下來咱們就能夠配置docker-compose的yaml文件,啓動Fabric的Docker環境了。
Orderer的配置是在base/docker-compose-base.yaml裏面,咱們看看其中的內容:
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。
Peer的配置是在base/docker-compose-base.yaml和peer-base.yaml裏面,咱們摘取其中的peer0.org1看看其中的內容:
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 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的配置中,主要是給Peer分配好各類服務的地址,以及TLS和MSP信息。
CLI在整個Fabric網絡中扮演客戶端的角色,咱們在開發測試的時候能夠用CLI來代替SDK,執行各類SDK能執行的操做。CLI會和Peer相連,把指令發送給對應的Peer執行。CLI的配置在docker-compose-cli.yaml中,咱們看看其中的內容:
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,只須要把咱們的代碼複製到該目錄便可。
【注意:請註釋掉cli中command這一行,咱們不須要CLI啓動的時候自動執行腳本,咱們在步驟4,5要一步步的手動執行!】
咱們將整個Fabric Docker環境的配置放在docker-compose-cli.yaml後,只須要使用如下命令便可:
docker-compose -f docker-compose-cli.yaml up -d
最後這個-d參數若是不加,那麼當前終端就會一直附加在docker-compose上,而若是加上的話,那麼docker容器就在後臺運行。運行docker ps命令能夠看啓動的結果:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
6f98f57714b5 hyperledger/fabric-tools "/bin/bash" 8 seconds ago Up 7 seconds cli
6e7b3fd0e803 hyperledger/fabric-peer "peer node start" 11 seconds ago Up 8 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
9e67abfb982f hyperledger/fabric-orderer "orderer" 11 seconds ago Up 8 seconds 0.0.0.0:7050->7050/tcp orderer.example.com
908d7fe2a4c7 hyperledger/fabric-peer "peer node start" 11 seconds ago Up 9 seconds 0.0.0.0:7051-7053->7051-7053/tcp peer0.org1.example.com
6bb187ac10ec hyperledger/fabric-peer "peer node start" 11 seconds ago Up 10 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
150baa520ed0 hyperledger/fabric-peer "peer node start" 12 seconds ago Up 9 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
能夠看到1Orderer+4Peer+1CLI都啓動了。
如今咱們要進入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就必須使用這個文件。
前面說過,咱們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
關於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兩個帳戶,相互之間能夠轉帳。
鏈上代碼的安裝須要在各個相關的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的時候再安裝。
實例化鏈上代碼主要是在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
07791d4a99b7 dev-peer0.org1.example.com-mycc-1.0 "chaincode -peer.a..." About a minute ago Up About a minute dev-peer0.org1.example.com-mycc-1.0
6f98f57714b5 hyperledger/fabric-tools "/bin/bash" About an hour ago Up About an hour cli
6e7b3fd0e803 hyperledger/fabric-peer "peer node start" About an hour ago Up About an hour 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
9e67abfb982f hyperledger/fabric-orderer "orderer" About an hour ago Up About an hour 0.0.0.0:7050->7050/tcp orderer.example.com
908d7fe2a4c7 hyperledger/fabric-peer "peer node start" About an hour ago Up About an hour 0.0.0.0:7051-7053->7051-7053/tcp peer0.org1.example.com
6bb187ac10ec hyperledger/fabric-peer "peer node start" About an hour ago Up About an hour 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
150baa520ed0 hyperledger/fabric-peer "peer node start" About an hour ago Up About an hour 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
如今鏈上代碼的實例也有了,而且在實例化的時候指定了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"]}'
前面的操做都是在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"]}'
這個時候咱們發現運行該命令後要等好久(我這裏花了40秒)才返回結果:
Query Result: 90
這是由於peer0.org2也須要生成Docker鏡像,建立對應的容器,才能經過容器返回結果。咱們回到Ubuntu終端,執行docker ps,能夠看到又多了一個容器:
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
3e37aba50189 dev-peer0.org2.example.com-mycc-1.0 "chaincode -peer.a..." 2 minutes ago Up 2 minutes dev-peer0.org2.example.com-mycc-1.0
07791d4a99b7 dev-peer0.org1.example.com-mycc-1.0 "chaincode -peer.a..." 21 minutes ago Up 21 minutes dev-peer0.org1.example.com-mycc-1.0
6f98f57714b5 hyperledger/fabric-tools "/bin/bash" About an hour ago Up About an hour cli
6e7b3fd0e803 hyperledger/fabric-peer "peer node start" About an hour ago Up About an hour 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
9e67abfb982f hyperledger/fabric-orderer "orderer" About an hour ago Up About an hour 0.0.0.0:7050->7050/tcp orderer.example.com
908d7fe2a4c7 hyperledger/fabric-peer "peer node start" About an hour ago Up About an hour 0.0.0.0:7051-7053->7051-7053/tcp peer0.org1.example.com
6bb187ac10ec hyperledger/fabric-peer "peer node start" About an hour ago Up About an hour 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
150baa520ed0 hyperledger/fabric-peer "peer node start" About an hour ago Up About an hour 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
經過以上的分解,但願你們對Fabric環境的建立有了更深刻的理解。我這裏的示例仍然是官方的示例,並無什麼太新的東西。只要把這每一步搞清楚,那麼接下來咱們在生產環境建立更多的Org,建立大量的Channel,執行各類ChainCode都是一模一樣。
最後,你們若是想進一步探討Fabric或者使用中遇到什麼問題能夠加入QQ羣【494085548】你們一塊兒討論。