溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Hyperledger Fabric服務啟動與鏈碼測試

發布時間:2020-07-14 03:11:05 來源:網絡 閱讀:24786 作者:暗黑魔君 欄目:編程語言

目錄

  1. 通過自動化腳本啟動服務
  2. 通過手動方式啟動服務
  3. 測試鏈碼

一.通過自動化腳本啟動服務

自動化腳本byfn.sh位于目錄$HOME/hyfa/fabric-samples/first-network下,具體用途如下

$./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.生成組織結構及×××書

$ 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 peerOrganizations

  • 生成Orderer創世區塊文件(保存在當前目錄的chanel-artifacts目錄下)

    $HOME/hyfa/fabric-samples/first-network/channel-artifacts/genesis.block

  • 生成應用通道交易配置文件(保存在當前目錄的chanel-artifacts目錄下)

    $HOME/hyfa/fabric-samples/first-network/channel-artifacts/channel.tx

  • 組織中錨節點更新配置文件(保存在當前目錄的chanel-artifacts目錄下)
    --檢測當前應用通道中新加入的節點
    --跨組織的數據交換

    Org1MSPanchors.tx
    Org2MSPanchors.tx

2.創建容器與通道

$sudo ./byfn.sh up 
  • 創建相應的docker容器

列出新拉取(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...

    • peer channel create -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/channel.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:21.437 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
      2018-07-11 06:42:21.456 UTC [cli/common] readBlock -> INFO 002 Got status: &{NOT_FOUND}
      2018-07-11 06:42:21.459 UTC [channelCmd] InitCmdFactory -> INFO 003 Endorser and orderer connections initialized
      2018-07-11 06:42:21.669 UTC [cli/common] readBlock -> INFO 004 Received block: 0
      ===================== Channel 'mychannel' created =====================
  • 節點加入通道

    Having all peers join the channel...

    • peer channel join -b mychannel.block
    • res=0
    • 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' =====================

    • peer channel join -b mychannel.block
    • res=0
    • 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' =====================

    • peer channel join -b mychannel.block
    • res=0
    • 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' =====================

    • peer channel join -b mychannel.block
    • res=0
    • set +x
      2018-07-11 06:42:31.304 UTC [channelCmd] InitCmdFactory -> INFO 001 Endorser and orderer connections initialized
      2018-07-11 06:42:31.418 UTC [channelCmd] executeJoin -> INFO 002 Successfully submitted proposal to join channel
      ===================== peer1.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參數關閉服務
  • 同時刪除所生成的組織結構及×××書目錄
  • 同時刪除所生成的chanel-artifacts/目錄下的四個配置文件
  • 同時刪除所有啟動的docker容器以及對應的鏡像文件

二.通過手動方式啟動服務

在實際的應用場景下,我們會經常對某些配置進行個性化定制,以滿足實際生產需求,所以,我們在本小節介紹如何通過手動方式啟動對應的服務,并通過修改相關配置文件滿足實際場景,手動步驟與自動化腳本基本一致。

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
  • ordererOrganizations
    包括構成Orderer組織(1個Orderer節點)的身份信息
  • 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 身份的各種證書文件,一般包括:

  • admincerts :管理員的×××書文件
  • cacerts :信任的根證書文件
  • key store :節點的簽名私鑰文件
  • signcerts :節點的簽名×××書文件
  • tlscacerts: TLS 連接用的證書
  • intermediatecerts (可選):信任的中間證書
  • crls (可選):證書撤銷列表
  • config.yaml (可選):記錄OrganizationalUnitldentifiers 信息,包括根證書位置和ID信息

這些身份文件隨后可以分發到對應的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
  • 使用Org2的管理員身份更新錨節點配置

變更腳本變量(比如當前變量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
向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女