目錄node
一.經過自動化腳本啓動服務git
自動化腳本byfn.sh位於目錄$HOME/hyfa/fabric-samples/first-network下,具體用途以下github
$./byfn.sh -h //查看幫助說明 Usage: byfn.sh <mode> [-c <channel name>] [-t <timeout>] [-d <delay>] [-f <docker-compose-file>] [-s <dbtype>] [-l <language>] [-i <imagetag>] [-v] <mode> - one of 'up', 'down', 'restart', 'generate' or 'upgrade' - 'up' - bring up the network with docker-compose up - 'down' - clear the network with docker-compose down - 'restart' - restart the network - 'generate' - generate required certificates and genesis block - 'upgrade' - upgrade the network from version 1.1.x to 1.2.x -c <channel name> - channel name to use (defaults to "mychannel") -t <timeout> - CLI timeout duration in seconds (defaults to 10) -d <delay> - delay duration in seconds (defaults to 3) -f <docker-compose-file> - specify which docker-compose file use (defaults to docker-compose-cli.yaml) -s <dbtype> - the database backend to use: goleveldb (default) or couchdb -l <language> - the chaincode language: golang (default) or node -i <imagetag> - the tag to be used to launch the network (defaults to "latest") -v - verbose mode byfn.sh -h (print this message) Typically, one would first generate the required certificates and genesis block, then bring up the network. e.g.: byfn.sh generate -c mychannel byfn.sh up -c mychannel -s couchdb byfn.sh up -c mychannel -s couchdb -i 1.2.x byfn.sh up -l node byfn.sh down -c mychannel byfn.sh upgrade -c mychannel Taking all defaults: byfn.sh generate byfn.sh up byfn.sh down
1.生成組織結構及×××書golang
$ sudo ./byfn.sh generate
生成組織結構及×××書(保存在當前目錄的crypto-config目錄)算法
drwxr-xr-x 3 root root 4096 Jul 11 11:26 ordererOrganizations
drwxr-xr-x 4 root root 4096 Jul 11 11:26 peerOrganizationsdocker
生成Orderer創世區塊文件(保存在當前目錄的chanel-artifacts目錄下)bash
$HOME/hyfa/fabric-samples/first-network/channel-artifacts/genesis.block網絡
生成應用通道交易配置文件(保存在當前目錄的chanel-artifacts目錄下)app
$HOME/hyfa/fabric-samples/first-network/channel-artifacts/channel.tx運維
組織中錨節點更新配置文件(保存在當前目錄的chanel-artifacts目錄下)
--檢測當前應用通道中新加入的節點
--跨組織的數據交換
Org1MSPanchors.tx
Org2MSPanchors.tx
2.建立容器與通道
$sudo ./byfn.sh up
列出新拉取(pull)的docker鏡像
dev-peer1.org2.example.com-mycc-1.0-26c2ef32838554aac4f7ad6f100aca865e87959c9a126e86d764c8d01f8346ab latest 526ebbb4eccd 44 seconds ago 147MB dev-peer0.org1.example.com-mycc-1.0-384f11f484b9302df90b453200cfb25174305fce8f53f4e94d45ee3b6cab0ce9 latest ba6eb425f830 57 seconds ago 147MB dev-peer0.org2.example.com-mycc-1.0-15b571b3ce849066b7ec74497da3b27e54e0df1345daff3951b94245ce09c42b latest b582a75829b3 About a minute ago 147MB
列出新建立的docker容器名稱
$sudo docker ps |awk '{print $NF}' |grep -v NAMES|sort ====返回結果==== cli dev-peer0.org1.example.com-mycc-1.0 dev-peer0.org2.example.com-mycc-1.0 dev-peer1.org2.example.com-mycc-1.0 orderer.example.com peer0.org1.example.com peer0.org2.example.com peer1.org1.example.com peer1.org2.example.com
建立通道mychannel
Channel name : mychannel
Creating channel...
節點加入通道
Having all peers join the channel...
set +x
2018-07-11 06:42:21.742 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-07-11 06:42:21.867 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer0.org1 joined channel 'mychannel' =====================
set +x
2018-07-11 06:42:24.942 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-07-11 06:42:25.053 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer1.org1 joined channel 'mychannel' =====================
set +x
2018-07-11 06:42:28.129 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
2018-07-11 06:42:28.232 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
===================== peer0.org2 joined channel 'mychannel' =====================
3.生成錨節點配置
Updating anchor peers for org1... + peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org1MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem + res=0 + set +x 2018-07-11 06:42:34.493 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized 2018-07-11 06:42:34.504 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update ===================== Anchor peers updated for org 'Org1MSP' on channel 'mychannel' ===================== Updating anchor peers for org2... + peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem + res=0 + set +x 2018-07-11 06:42:37.574 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized 2018-07-11 06:42:37.586 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update ===================== Anchor peers updated for org 'Org2MSP' on channel 'mychannel' =====================
4.在節點上安裝鏈碼並實例化
Installing chaincode on peer0.org1... + peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/ + res=0 + set +x 2018-07-11 06:42:40.666 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2018-07-11 06:42:40.666 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc 2018-07-11 06:42:40.969 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" > ===================== Chaincode is installed on peer0.org1 ===================== Install chaincode on peer0.org2... + peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/ + res=0 + set +x 2018-07-11 06:42:41.051 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2018-07-11 06:42:41.051 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc 2018-07-11 06:42:41.240 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" > ===================== Chaincode is installed on peer0.org2 ===================== Instantiating chaincode on peer0.org2... + peer chaincode instantiate -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc -l golang -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P 'AND ('\''Org1MSP.peer'\'','\''Org2MSP.peer'\'')' + res=0 + set +x 2018-07-11 06:42:41.306 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2018-07-11 06:42:41.306 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc ===================== Chaincode is instantiated on peer0.org2 on channel 'mychannel' =====================
5.調用鏈碼對帳本數據進行操做
Querying chaincode on peer0.org1... ===================== Querying on peer0.org1 on channel 'mychannel'... ===================== Attempting to Query peer0.org1 ...3 secs + peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}' + res=0 + set +x 100 ===================== Query successful on peer0.org1 on channel 'mychannel' ===================== Sending invoke transaction on peer0.org1 peer0.org2... + peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"Args":["invoke","a","b","10"]}' + res=0 + set +x 2018-07-11 06:43:08.200 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200 ===================== Invoke transaction successful on peer0.org1 peer0.org2 on channel 'mychannel' ===================== Installing chaincode on peer1.org2... + peer chaincode install -n mycc -v 1.0 -l golang -p github.com/chaincode/chaincode_example02/go/ + res=0 + set +x 2018-07-11 06:43:08.271 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2018-07-11 06:43:08.271 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc 2018-07-11 06:43:08.449 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" > ===================== Chaincode is installed on peer1.org2 ===================== Querying chaincode on peer1.org2... ===================== Querying on peer1.org2 on channel 'mychannel'... ===================== Attempting to Query peer1.org2 ...3 secs + peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}' + res=0 + set +x 90 ===================== Query successful on peer1.org2 on channel 'mychannel' =====================
自動化腳本執行完畢
========= All GOOD, BYFN execution completed =========== _____ _ _ ____ | ____| | \ | | | _ \ | _| | \| | | | | | | |___ | |\ | | |_| | |_____| |_| \_| |____/
6.關閉服務
$sudo ./byfn.sh down //經過down參數關閉服務
二.經過手動方式啓動服務
在實際的應用場景下,咱們會常常對某些配置進行個性化定製,以知足實際生產需求,因此,咱們在本小節介紹如何經過手動方式啓動對應的服務,並經過修改相關配置文件知足實際場景,手動步驟與自動化腳本基本一致。
1.生成組織結構及×××書
$ cd hyfa/fabric-samples/first-network/
爲fabric網絡生成指定拓撲結構的組織關係和×××書
$ sudo ../bin/cryptogen generate --config=./crypto-config.yaml
此命令依賴 crypto-config.yaml 配置文件
命令輸出
org1.example.com org2.example.com
證書和密鑰(即MSP)將被輸出到目錄 first-network/crypto-config 的目錄中
drwxr-xr-x 3 root root 4096 Jul 11 00:31 ordererOrganizations drwxr-xr-x 4 root root 4096 Jul 11 00:31 peerOrganizations
peerOrganizations
全部的Peer節點組織(2個組織, 4個節點)的相關身份信息. 其中最關鍵的是MSP目錄, 表明了實體的身份信息
$ tree -L 6 crypto-config //經過tree命令查看目錄樹
crypto-config
├── ordererOrganizations
│ └── example.com
│ ├── ca
│ │ ├── ca.example.com-cert.pem
│ │ └── ded244dc5aa11c2b42e43fa4de6b952259078c78131fc86031a02baff21abd78_sk
│ ├── 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
│ │ │ ├── cacerts
│ │ │ ├── keystore
│ │ │ ├── signcerts
│ │ │ └── tlscacerts
│ │ └── tls
│ │ ├── ca.crt
│ │ ├── server.crt
│ │ └── server.key
│ ├── tlsca
│ │ ├── 1e1955762d0669ba18f078a04ac7b19918f40554aeddd6841dc6c059eefbbcf9_sk
│ │ └── tlsca.example.com-cert.pem
│ └── users
│ └── Admin@example.com
│ ├── msp
│ │ ├── admincerts
│ │ ├── cacerts
│ │ ├── keystore
│ │ ├── signcerts
│ │ └── tlscacerts
│ └── tls
│ ├── ca.crt
│ ├── client.crt
│ └── client.key
└── peerOrganizations
├── org1.example.com # 第一個組織的相關材料,每一個組織會生成單獨的根證書
│ ├── ca #存放組織的根證書和對應的私鑰文件,默認採用EC算法,證書爲自簽名。爲根證書。
│ │ ├── 15d058166d13e584fca35523a71a12b2fbaf6de5072d3661801476dfbfb3ea4f_sk
│ │ └── ca.org1.example.com-cert.pem
│ ├── msp #存放表明該組織的身份信息
│ │ ├── admincerts #組織管理員的身份驗證證書,被根證書籤名
│ │ │ └── Admin@org1.example.com-cert.pem
│ │ ├── cacerts #組織的根證書,同CA目錄下文件
│ │ │ └── ca.org1.example.com-cert.pem
│ │ ├── config.yaml
│ │ └── tlscacerts #用於TLS的CA 證書,自簽名
│ │ └── tlsca.org1.example.com-cert.pem
│ ├── peers # 存放屬於該組織的全部Peer節點
│ │ ├── peer0.org1.example.com #第一個peer的信息,包括其msp證書和tls 證書兩類
│ │ │ ├── msp
│ │ │ │ ├── admincerts#組織管理員的身份驗證證書。Peer將基於這些證書來認證交易簽署者是否爲管理員身份
│ │ │ │ ├── cacerts #存放組織的根證書
│ │ │ │ ├── config.yaml
│ │ │ │ ├── keystore #本節點的身份私鑰,用來簽名
│ │ │ │ ├── signcerts #驗證本節點簽名的證書,被組織根證書籤名
│ │ │ │ └── tlscacerts #TLS 鏈接用的×××書,即組織TLS 證書
│ │ │ └── tls # 存放tls相關的證書和私鑰
│ │ │ ├── ca.crt #組織的根證書
│ │ │ ├── server.crt #驗證本節點簽名的證書,被組織根證書籤名
│ │ │ └── server.key #本節點的身份私鑰,用來簽名
│ │ └── peer1.org1.example.com
│ │ ├── msp
│ │ │ ├── admincerts
│ │ │ ├── cacerts
│ │ │ ├── config.yaml
│ │ │ ├── keystore
│ │ │ ├── signcerts
│ │ │ └── tlscacerts
│ │ └── tls
│ │ ├── ca.crt
│ │ ├── server.crt
│ │ └── server.key
│ ├── tlsca
│ │ ├── d45d38d7d48e3e41332c66d673869080be030c0e76d47cc6c8d6909f89f401e8_sk
│ │ └── tlsca.org1.example.com-cert.pem
│ └── users
│ ├── Admin@org1.example.com
│ │ ├── msp
│ │ │ ├── admincerts
│ │ │ ├── cacerts
│ │ │ ├── keystore
│ │ │ ├── signcerts
│ │ │ └── tlscacerts
│ │ └── tls
│ │ ├── ca.crt
│ │ ├── client.crt
│ │ └── client.key
│ └── User1@org1.example.com
│ ├── msp
│ │ ├── admincerts
│ │ ├── cacerts
│ │ ├── keystore
│ │ ├── signcerts
│ │ └── tlscacerts
│ └── tls
│ ├── ca.crt
│ ├── client.crt
│ └── client.key
└── org2.example.com
├── ca
│ ├── 5d261ccc23b0a7345dc3ee387a4714c6ec3261f957d40e82198b1d6141b715a9_sk
│ └── ca.org2.example.com-cert.pem
├── msp
│ ├── admincerts
│ │ └── Admin@org2.example.com-cert.pem
│ ├── cacerts
│ │ └── ca.org2.example.com-cert.pem
│ ├── config.yaml
│ └── tlscacerts
│ └── tlsca.org2.example.com-cert.pem
├── peers
│ ├── peer0.org2.example.com
│ │ ├── msp
│ │ │ ├── admincerts
│ │ │ ├── cacerts
│ │ │ ├── config.yaml
│ │ │ ├── keystore
│ │ │ ├── signcerts
│ │ │ └── tlscacerts
│ │ └── tls
│ │ ├── ca.crt
│ │ ├── server.crt
│ │ └── server.key
│ └── peer1.org2.example.com
│ ├── msp
│ │ ├── admincerts
│ │ ├── cacerts
│ │ ├── config.yaml
│ │ ├── keystore
│ │ ├── signcerts
│ │ └── tlscacerts
│ └── tls
│ ├── ca.crt
│ ├── server.crt
│ └── server.key
├── tlsca
│ ├── c16ccfac347d187e318e38de956036528f29e542c4cb2451dafd0d33b44efebf_sk
│ └── tlsca.org2.example.com-cert.pem
└── users
├── Admin@org2.example.com
│ ├── msp
│ │ ├── admincerts
│ │ ├── cacerts
│ │ ├── keystore
│ │ ├── signcerts
│ │ └── tlscacerts
│ └── tls
│ ├── ca.crt
│ ├── client.crt
│ └── client.key
└── User1@org2.example.com
├── msp
│ ├── admincerts
│ ├── cacerts
│ ├── keystore
│ ├── signcerts
│ └── tlscacerts
└── tls
├── ca.crt
├── client.crt
└── client.key
Cryptogen 按照配置文件中指定的結構生成了對應的組織和密鑰、證書文件
其中最關鍵的是各個資源下的msp 目錄內容,存儲了生成的表明MSP 身份的各類證書文件,通常包括:
這些身份文件隨後能夠分發到對應的Orderer 節點和Peer 節點上,並放到對應的MSP路徑下,用於簽名使用。
2.建立創世區塊並啓動Orderer
指定使用 configtx.yaml 文件中定義的 TwoOrgsOrdererGenesis 模板, 生成Orderer服務系統通道的初始區塊文件
$ sudo ../bin/configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block
命令輸出
2018-07-11 00:52:00.625 PDT [common/tools/configtxgen] main -> WARN 001 Omitting the channel ID for configtxgen is deprecated. Explicitly passing the channel ID will be required in the future, defaulting to 'testchainid'. 2018-07-11 00:52:00.625 PDT [common/tools/configtxgen] main -> INFO 002 Loading configuration 2018-07-11 00:52:00.632 PDT [common/tools/configtxgen/encoder] NewChannelGroup -> WARN 003 Default policy emission is deprecated, please include policy specificiations for the channel group in configtx.yaml 2018-07-11 00:52:00.632 PDT [common/tools/configtxgen/encoder] NewOrdererGroup -> WARN 004 Default policy emission is deprecated, please include policy specificiations for the orderer group in configtx.yaml 2018-07-11 00:52:00.632 PDT [common/tools/configtxgen/encoder] NewOrdererOrgGroup -> WARN 005 Default policy emission is deprecated, please include policy specificiations for the orderer org group OrdererOrg in configtx.yaml 2018-07-11 00:52:00.634 PDT [msp] getMspConfig -> INFO 006 Loading NodeOUs 2018-07-11 00:52:00.634 PDT [common/tools/configtxgen/encoder] NewOrdererOrgGroup -> WARN 007 Default policy emission is deprecated, please include policy specificiations for the orderer org group Org1MSP in configtx.yaml 2018-07-11 00:52:00.635 PDT [msp] getMspConfig -> INFO 008 Loading NodeOUs 2018-07-11 00:52:00.635 PDT [common/tools/configtxgen/encoder] NewOrdererOrgGroup -> WARN 009 Default policy emission is deprecated, please include policy specificiations for the orderer org group Org2MSP in configtx.yaml 2018-07-11 00:52:00.635 PDT [common/tools/configtxgen] doOutputBlock -> INFO 00a Generating genesis block 2018-07-11 00:52:00.635 PDT [common/tools/configtxgen] doOutputBlock -> INFO 00b Writing genesis block $ ls channel-artifacts/ -rw-r--r-- 1 root root 12655 Jul 11 00:52 channel-artifacts/genesis.block //生成的創世區塊
3.生成應用通道交易配置文件
$ export CHANNEL_NAME=mychannel //設置臨時變量
指定使用 configtx.yaml 配置文件中的 TwoOrgsChannel 模板, 來生成新建通道的配置交易文件, TwoOrgsChannel 模板指定了Org1和Org2都屬於後面新建的應用通道
$ sudo ../bin/configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID $CHANNEL_NAME
命令輸出
2018-07-11 00:54:21.557 PDT [common/tools/configtxgen] main -> INFO 001 Loading configuration 2018-07-11 00:54:21.563 PDT [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 002 Generating new channel configtx 2018-07-11 00:54:21.563 PDT [common/tools/configtxgen/encoder] NewApplicationGroup -> WARN 003 Default policy emission is deprecated, please include policy specificiations for the application group in configtx.yaml 2018-07-11 00:54:21.563 PDT [msp] getMspConfig -> INFO 004 Loading NodeOUs 2018-07-11 00:54:21.564 PDT [common/tools/configtxgen/encoder] NewApplicationOrgGroup -> WARN 005 Default policy emission is deprecated, please include policy specificiations for the application org group Org1MSP in configtx.yaml 2018-07-11 00:54:21.564 PDT [msp] getMspConfig -> INFO 006 Loading NodeOUs 2018-07-11 00:54:21.564 PDT [common/tools/configtxgen/encoder] NewApplicationOrgGroup -> WARN 007 Default policy emission is deprecated, please include policy specificiations for the application org group Org2MSP in configtx.yaml 2018-07-11 00:54:21.565 PDT [common/tools/configtxgen] doOutputChannelCreateTx -> INFO 008 Writing new channel tx $ ll channel-artifacts/channel.tx -rw-r--r-- 1 root root 346 Jul 11 00:54 channel-artifacts/channel.tx //生成的通道文件
4.生成各組織的錨節點更新文件
錨節點配置更新文件用來對組織的錨節點進行配置
一樣基於 configtx.yaml 配置文件生成新建通道文件, 每一個組織都須要分別生成且注意指定對應的組織名稱
$ sudo ../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org1MSP $ sudo ../bin/configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME -asOrg Org2MSP
命令輸出
2018-07-11 00:57:34.629 PDT [common/tools/configtxgen] main -> INFO 001 Loading configuration 2018-07-11 00:57:34.635 PDT [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update 2018-07-11 00:57:34.635 PDT [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update 2018-07-11 00:57:40.028 PDT [common/tools/configtxgen] main -> INFO 001 Loading configuration 2018-07-11 00:57:40.037 PDT [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 002 Generating anchor peer update 2018-07-11 00:57:40.037 PDT [common/tools/configtxgen] doOutputAnchorPeersUpdate -> INFO 003 Writing anchor peer update
5.建立容器,啓動網絡
$ sudo docker-compose -f docker-compose-cli.yaml up -d
-f: 指定docker-compose文件
-d:Detached mode,後臺運行容器
命令輸出
Creating network "net_byfn" with the default driver Creating volume "net_peer0.org2.example.com" with default driver Creating volume "net_peer1.org2.example.com" with default driver Creating volume "net_peer1.org1.example.com" with default driver Creating volume "net_peer0.org1.example.com" with default driver Creating volume "net_orderer.example.com" with default driver Creating peer1.org1.example.com ... Creating peer1.org2.example.com ... Creating peer0.org2.example.com ... Creating orderer.example.com ... Creating peer0.org1.example.com ... Creating orderer.example.com Creating peer0.org2.example.com Creating peer0.org1.example.com Creating peer1.org1.example.com Creating peer1.org2.example.com ... done Creating cli ... Creating cli ... done
查看建立的docker容器
生成的docker容器名稱
cli peer1.org2.example.com peer1.org1.example.com peer0.org2.example.com peer0.org1.example.com orderer.example.com
6.建立通道並配置
執行以下命令進入到CLI容器中(後繼操做都在容器中執行)
$ sudo docker exec -it cli bash
直接進入容器該路徑下,相關操做不要更換路徑
root@138a8115a08c:/opt/gopath/src/github.com/hyperledger/fabric/peer# pwd /opt/gopath/src/github.com/hyperledger/fabric/peer
6.1 配置變量
export CHANNEL_NAME=mychannel
6.2 建立通道
$ 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
命令返回:
2018-07-11 11:44:50.198 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized 2018-07-11 11:44:50.228 UTC [cli/common] readBlock -> INFO 002 Got status: &{NOT_FOUND} 2018-07-11 11:44:50.232 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized 2018-07-11 11:44:50.436 UTC [cli/common] readBlock -> INFO 004 Received block: 0
該命令自動在本地生成與該應用通道同名的初始區塊 mychannel.block, 只有擁有該文件才能夠加入建立的應用通道中
參數說明:
-o: 指定orderer節點的地址
-c: 指定要建立的應用通道的名稱(必須與在建立應用通道交易配置文件時的通道名稱保持一致)
-f: 指定建立應用通道時所使用的應用通道交易配置文件
--tls: 開啓TLS驗證
--cafile: 指定TLS_CA證書路徑
查看生成的文件
root@138a8115a08c:/opt/gopath/src/github.com/hyperledger/fabric/peer# ll total 36 drwxr-xr-x 5 root root 4096 Jul 11 11:44 ./ drwxr-xr-x 3 root root 4096 Jul 11 08:00 ../ drwxrwxr-x 2 1000 1000 4096 Jul 11 07:57 channel-artifacts/ drwxr-xr-x 4 root root 4096 Jul 11 07:31 crypto/ -rw-r--r-- 1 root root 15671 Jul 11 11:44 mychannel.block drwxrwxr-x 2 1000 1000 4096 Jul 10 07:59 scripts/
6.3 當前節點加入通道
應用通道所包含組織的成員節點能夠加入通道中
$ peer channel join -b mychannel.block
join命令: 將本Peer節點加入到某個應用通道中
命令返回
2018-07-11 13:20:01.980 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized 2018-07-11 13:20:02.084 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
查看加入列表
$ peer channel list Channels peers has joined: mychannel
6.4更新錨節點
錨節點經過廣播的方式通知有新節點加入
使用Org1的管理員身份更新錨節點配置(默認)
$ peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org1MSPanchors.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
命令返回
2018-07-11 12:00:12.677 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized 2018-07-11 12:00:12.690 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
變動腳本變量(好比當前變量CORE_PEER_LOCALMSPID默認爲Org1MSP)
CORE_PEER_LOCALMSPID="Org2MSP" CORE_PEER_ADDRESS=peer0.org2.example.com:7051 CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp 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
執行更新錨節點配置命令
$ peer channel update -o orderer.example.com:7050 -c $CHANNEL_NAME -f ./channel-artifacts/Org2MSPanchors.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
命令返回
2018-07-11 12:05:37.325 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized 2018-07-11 12:05:37.338 UTC [channelCmd] update -> INFO 002 Successfully submitted channel update
若是須要加入其它組織,變動腳本變量後執行更新錨節點操做便可,至此,手動配置網絡完成, 能夠測試鏈碼ChainCode
切換:pee1.org1.example.com
CORE_PEER_LOCALMSPID="Org1MSP" CORE_PEER_ADDRESS=peer1.org1.example.com:7051 CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabri c/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.exa mple.com/msp CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fa bric/peer/crypto/peerOrganizations/org1.example.com/peers/peer1.org1.exa mple.com/tls/ca.crt
三.測試鏈碼
進去CLI容器進行相關運維操做
$ sudo docker exec -it cli bash
Peer加入應用通道後, 能夠執行鏈碼相關操做,進行測試 鏈碼在調用以前, 必須先通過安裝和實例化兩個步驟, 部署到Peer節點上.
1.查看並環境變量
$ echo $CORE_PEER_LOCALMSPID #確認當前指定的org Org1MSP $ echo $CORE_PEER_ADDRESS #確認當前指定的peer peer0.org1.example.com:7051 $ export CHANNEL_NAME=mychannel #設置通道名稱
2.安裝並實例化鏈碼
2.1 鏈碼使用須知
-- 將其安裝在指定的節點上
-- 安裝完成後要對其進行實例化
-- 調用鏈碼(查詢, 執行事務)
2.2 安裝並實例化鏈碼
安裝鏈碼
$ peer chaincode install -n mycc -v 1.0 -p github.com/chaincode/chaincode_example02/go/
參數說明:
-n: 指定要安裝的鏈碼的名稱
-v: 指定鏈碼的版本
-p: 指定要安裝的鏈碼的所在路徑,注意不要寫絕對路徑
命令返回:
2018-07-11 13:01:11.090 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2018-07-11 13:01:11.090 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc 2018-07-11 13:01:11.351 UTC [chaincodeCmd] install -> INFO 003 Installed remotely response:<status:200 payload:"OK" >
實例化鏈碼
$ peer chaincode instantiate -o orderer.example.com:7050 \
--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 \
-C $CHANNEL_NAME \
-n mycc -v 1.0 \
-c '{"Args":["init","a", "100", "b","200"]}' \
-P "OR('Org1MSP.peer','Org2MSP.peer')"
參數說明:
-o: 指定Oderer節點地址
--tls: 開啓TLS驗證
--cafile: 指定TLS_CA證書路
-n: 指定要實例化的鏈碼名稱
-v: 指定要實例化的鏈碼的版本號
-C: 指定通道名稱
-c: 實例化鏈碼時指定的參數
("init","a", "100", "b","200":設置a帳戶初始化金額爲100,b帳戶的初始金額爲200)
-P: 指定背書策略(指定交易在哪些節點上面進行簽名)
命令返回:
2018-07-11 13:21:13.234 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 001 Using default escc 2018-07-11 13:21:13.234 UTC [chaincodeCmd] checkChaincodeCmdParams -> INFO 002 Using default vscc
3.代碼調用測試
3.1 查詢帳戶餘額
$ peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}' #查詢a帳戶餘額 100 $ peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","b"]}' #查詢b帳戶餘額 200
3.2 轉帳測試
peer chaincode invoke -o orderer.example.com:7050 \ --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 \ -C $CHANNEL_NAME -n mycc \ -c '{"Args":["invoke","a","b","10"]}'
參數說明:
-o: 指定orderer節點地址
--tls: 開啓TLS驗證
--cafile: 指定TLS_CA證書路徑
-n: 指定鏈碼名稱
-C: 指定通道名稱
-c: 指定調用鏈碼的所需參數
func invoke(accoutF string, accountT stirng, amount string)
3.3 查詢當前餘額
$ peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","a"]}' #查詢a帳戶餘額 90 $ peer chaincode query -C $CHANNEL_NAME -n mycc -c '{"Args":["query","b"]}' #查詢b帳戶餘額 210