Fabric 1.3新功能介紹及環境搭建
第一部分:新功能介紹
功能1. 使用Identity Mixer實現MSP(Membership Service Prodiver)
這是一種通過使用零知識證明來保持身份匿名和不可連結的方法。工具idexmigen可以在測試環境中生成Identity Mixer憑證。
1.1零知識證明
零知識證明,英文名為Zero-Knowledge Proof,是由S.Goldwasser、S.Micali以及C.Rackoff在20世紀80年代初提出的。它指的是證明者(被驗證者)能夠在不向驗證者提供任何有用的資訊的情況下,使驗證者相信某個論斷是正確的。
零知識證明實質上是一種涉及兩方或更多方的協議,即兩方或更多方完成一項任務所需採取的一系列步驟。
1.2 什麼是Idemix?
Idemix是一個加密協議套件,它提供強大的身份驗證以及隱私保護功能,如匿名,無需揭示交易者身份即可進行交易,以及不可連結性,即單個身份傳送多個交易的能力,而不會洩露交易是通過相同的身份傳送的。
Idemix流中涉及三個參與者:使用者,發行者和 驗證者。
發行者證明將一組使用者的屬性以數字證書的形式釋出,以下稱為“憑證”。
使用者稍後生成擁有憑證的“
1.3 如何將Idemix與Hyperledger Fabric一起使用
要在Hyperledger Fabric中使用Idemix,需要以下三個基本步驟:
發行者:Fabric CA 或者 idemixgen
驗證者:Idemix MSP
使用者 :java SDK類org.hyperledger.fabric_ca.sdk.HFCAClient
(例如:IdemixEnrollment idemixEnrollment =hfcaClient.idemixEnroll(x509enrollment, "idemixMSPID1"))
1.3.1 Fabric-ca的安裝過程
Fabric-ca在Ubuntu和Centos上面的安裝
第一步:安裝系統元件包
sudo apt install libtool libltdl-dev
如果在Centos上面安裝,則需要執行命令 yum install libtool libltdl-dev,其他的步驟相同。
第二步:下載原始碼並編譯
cd $GOPATH/src/github.com/hyperledger
git clone http://gerrit.hyperledger.org/r/fabric-ca
cd $GOPATH/src/github.com/hyperledger/fabric-ca
檢視版本:
cd fabric-ca/
git branch -a (切換如:git checkout release-1.3)
make fabric-ca-server
報錯:
[[email protected] fabric-ca]# make fabric-ca-server
Building fabric-ca-server in bin directory ...
# github.com/hyperledger/fabric-ca/vendor/github.com/miekg/pkcs11
vendor/github.com/miekg/pkcs11/pkcs11.go:26:18: fatal error: ltdl.h: No such file or directory
#include <ltdl.h>
^
compilation terminated.
make: *** [bin/fabric-ca-server] 錯誤 2
解決:
如果在ubunt作業系統中,只需安裝:apt install libltdl3-dev
如果在centos作業系統中,只需安裝:yum install libtool-ltdl-devel
make fabric-ca-server
make fabric-ca-client
第三步:安裝編譯好的可執行檔案
cd $GOPATH/src/github.com/hyperledger/fabric-ca/bin
cp $GOPATH/src/github.com/hyperledger/fabric-ca/bin/* /usr/local/bin
chmod -R 775 /usr/local/bin/fabric-ca-server
chmod -R 775 /usr/local/bin/fabric-ca-client
第四步驟:檢查
fabric-ca-server version
fabric-ca-server version
顯示版本資訊為安裝正確。
2.fabric-ca-server的初始化
1.3.2 Fabric-ca-server的初始化
第一步:建立資料夾
mkdir -p /opt/hyperledger/fabric-ca-server
cd $GOPATH/src/github.com/hyperledger/fabric-ca
第二步:伺服器初始化
fabric-ca-server伺服器初始化的命令如下:
fabric-ca-server init -b admin:adminpw
-b 引數後面的是fabric-ca-server伺服器管理賬號的使用者名稱和密碼
啟動fabric-ca-server:
fabric-ca-server start -H /opt/hyperledger/fabric-ca --boot admin:adminpw
1.3.3 Identity Mixer MSP配置生成器(idemixgen)
Fabric CA (version 1.3 or later)在Fabric-ca-sever初始化的目錄下會生成IssuerPublicKey和IssuerRevocationPublicKey,會在下一步使用。
1.3.4 CA金鑰生成
cd $GOPATH/src/github.com/hyperledger/fabric-ca
idemixgen ca-keygen
會在fabric-ca下生成msp目錄等
1.3.5 新增預設簽名者
生成ca和msp目錄後 ,可以將目錄中指定的預設簽名者新增到配置
命令例如:idemixgen signerconfig -u OrgUnit1 --admin -e "johndoe" -r 1234
(簽名者是組織單元“OrgUnit1”的成員,註冊標識為“johndoe”,撤銷控制代碼為“1234”,這是一個管理員)
$ idemixgen signerconfig -h
usage: idemixgen signerconfig [<flags>]
Generate a default signer for this Idemix MSP
Flags:
-h, --help Show context-sensitive help (also try --help-long and --help-man).
-u, --org-unit=ORG-UNIT The Organizational Unit of the default signer
-a, --admin Make the default signer admin
-e, --enrollment-id=ENROLLMENT-ID
The enrollment id of the default signer
-r, --revocation-handle=REVOCATION-HANDLE
The handle used to revoke this signer
功能2. 設定金鑰級背書策略
允許每個金鑰級的背書策略覆蓋預設的鏈碼級背書策略。
在Fabric1.3版本之前常見的背書策略是:
例項化或者升級chaincode程式碼時設定的,例如:
peer chaincode instantiate -C <channelid> -n mycc -P "AND('Org1.peer', 'Org2.peer')"
peer chaincode instantiate -o orderer.qklszzn.com:7050 -C roberttestchannel -n r_test_cc6 -v 1.0 -c '{"Args":["init","a","100","b","200"]}' -P "OR ('Org1MSP.member','Org2MSP.member')"
如果未指定,預設背書策略:
“OR('Org1.member','Org2.member')”。意思是組織1或組織2的任何成員
- 'Org0.admin':Org0的任何管理員
- 'Org1.member':Org1的任何成員
- 'Org1.client':Org1的任何客戶端
- 'Org1.peer':Org1的任何peer節點
在Fabric1.3版本中 shim API 提供了設定和獲取背書策略的方法
SetStateValidationParameter(key string, ep []byte) error
GetStateValidationParameter(key string) ([]byte, error)
對於私有資料有如下方法:
SetPrivateDataValidationParameter(collection, key string, ep []byte) error
GetPrivateDataValidationParameter(collection, key string) ([]byte, error)
為了幫助設定背書策略並將其編組為驗證引數位元組陣列,shim提供了便利功能,允許智慧合約開發人員根據組織的MSP識別符號處理背書策略:
type KeyEndorsementPolicy interface {
// Policy returns the endorsement policy as bytes
Policy() ([]byte, error)
// AddOrgs adds the specified orgs to the list of orgs that are required
// to endorse
AddOrgs(roleType RoleType, organizations ...string) error
// DelOrgs delete the specified channel orgs from the existing key-level endorsement
// policy for this KVS key. If any org is not present, an error will be returned.
DelOrgs([]string) error
// DelAllOrgs removes any key-level endorsement policy from this KVS key.
DelAllOrgs() error
// ListOrgs returns an array of channel orgs that are required to endorse changes
ListOrgs() ([]string, error)
}
用法:
要為金鑰設定背書策略,其中需要兩個特定組織來背書金鑰更改,將兩個組織MSPIDs傳遞給AddOrgs(),然後呼叫Policy()以構造背書策略位元組陣列,傳遞給SetStateValidationParameter()。
功能3. 使用分頁查詢CouchDB狀態資料庫
客戶端現在可以從智慧合約查詢中瀏覽結果集,從而可以支援具有高效能的大型結果集。
分頁是通過指定pagesize(單頁資料條數)和bookmark(查詢起始點),程式碼如下:
func (t *SimpleChaincode) queryMarblesWithPagination(stub shim.ChaincodeStubInterface, args []string) pb.Response {
// 0
// "queryString"
if len(args) < 3 {
return shim.Error("Incorrect number of arguments. Expecting 3")
}
queryString := args[0]
//return type of ParseInt is int64
pageSize, err := strconv.ParseInt(args[1], 10, 32)
if err != nil {
return shim.Error(err.Error())
}
bookmark := args[2]
queryResults, err := getQueryResultForQueryStringWithPagination(stub, queryString, int32(pageSize), bookmark)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(queryResults)
}
功能4. Chaincode為開發人員
作為對用Go和node編寫的智慧合約的當前Fabric支援的補充。現在支援Java,1.3版本將明確可用java進行智慧合約的編寫。
功能5. 基於peer節點通道的事件服務
基於peer通道的事件服務本身並不是新概念(它首次出現在v1.1中),但是v1.3版本標誌著舊事件中心的結束。在升級到v1.3之前,使用舊事件中心的應用程式必須切換到新的基於peer通道的事件服務。
在Fabric1.1和1.2的版本中 core.yaml中有EventHub相關配置,如下
# EventHub related configuration
events:
# The address that the Event service will be enabled on the peer
address: 0.0.0.0:7053
# total number of events that could be buffered without blocking send
buffersize: 100
# timeout duration for producer to send an event.
# if < 0, if buffer full, unblocks immediately and not send
# if 0, if buffer full, will block and guarantee the event will be sent out
# if > 0, if buffer full, blocks till timeout
timeout: 10ms
# timewindow is the acceptable difference between the peer's current
# time and the client's time as specified in a registration event
timewindow: 15m
# Keepalive settings for peer server and clients
keepalive:
# MinInterval is the minimum permitted time in seconds which clients
# can send keepalive pings. If clients send pings more frequently,
# the events server will disconnect them
minInterval: 60s
在1.3的版本,卻沒有了EventHub相關配置。
但是並不影響使用,依然可以通過
private static EventHub eventHub;
private static PeerEvents.Event goodEventBlock;
eventHub = new EventHub("test", "grpc://lh:99", null, null);
goodEventBlock = PeerEvents.Event.newBuilder().setBlock(blockBuilder).build();
BlockEvent be = new BlockEvent(eventHub, goodEventBlock);
或
channel.registerBlockListener(blockEvent -> {
log.debug("========================Event事件監聽========================");
try {
org.getBlockListener().received(execBlockInfo(blockEvent));
} catch (Exception e) {
e.printStackTrace();
org.getBlockListener().received(getFailFromString(e.getMessage()));
}
log.debug("========================Event事件監聽結束========================");
});
第二部分:Fabric1.3環境搭建
第一章 準備階段
1.1 win7 64位作業系統
1.2 虛擬機器映象:CentOS-7-x86_64-LiveGNOME-1804.iso
1.3 產品:VMware® Workstation 12 Pro 版本:12.5.9 build-7535481
第二章 虛擬機器環境準備階段
2.1 開啟SSH服務
systemctl start sshd service
隨系統一起啟動SSH服務
systemctl enable sshd.service
2.2 yum install update
2.3 CentOS升核心至最新
2.4 yum -y install zip unzip
2.5 yum install perl openssh git
2.6 yum -y install wget
2.7 下載golang
wget https://storage.googleapis.com/golang/go1.10.linux-amd64.tar.gz
2.8 sudo tar -C /usr/local -xzf go1.11.2.linux-amd64.tar.gz
2.9 sudo vi /etc/profile
export GOROOT=/usr/local/go
export PATH=$PATH:$GOROOT/bin
export GOPATH=/opt/gopath
2.10 source /etc/profile
2.11 yum install docker python-pip
2.12 下載pip
wget"https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate
或者
curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py" python get-pip.py
2.13 tar -C /usr/local -axf pip-1.5.4.tar.gz
2.14 cd /usr/local/pip-1.5.4/
2.15 python setup.py install
2.16 yum install docker python-pip
2.17 pip install --upgrade pip
2.18 pip install docker-compose
2.19 sudo service docker start
2.20 sudo yum install gcc
2.21 匯入映象
docker pull hyperledger/fabric-baseimage:latest
docker pull hyperledger/fabric-baseos:latest
docker pull hyperledger/fabric-ccenv:latest
安裝java智慧合約還需要匯入兩個映象:
docker pull docker.io/hyperledger/fabric-javaenv:amd64-1.3.0
docker pull docker.io/hyperledger/fabric-tools: latest
docker 映象匯入匯出命令參考
docker save -o nginx.tar nginx:latest
docker load -i nginx.tar
第三章 區塊鏈環境搭建
第一步:建立目錄並下載程式碼
mkdir -p $GOPATH/src/github.com/hyperledger
cd $GOPATH/src/github.com/hyperledger
git clone https://github.com/hyperledger/fabric
檢視版本:
cd fabric/
git branch -a (切換如:git checkout release-1.2)
第二步:安裝相關依賴軟體
go get github.com/golang/protobuf/protoc-gen-go
mkdir -p $GOPATH/src/github.com/hyperledger/fabric/build/docker/gotools/bin
cp $GOPATH/bin/protoc-gen-go $GOPATH/src/github.com/hyperledger/fabric/build/docker/gotools/bin
注意:go get 之後編譯好的檔案會存放到環境變數$GOBIN對應的目錄中,如果沒有設定$GOBIN的值,系統預設將生成的文體存放到$GOPATH/bin下面.
第三步:編譯Fabric的模組
進入到Fabirc原始碼所在的資料夾,執行以下命令可以一次完成Fabric5個主要模組的編譯過程,具體的命令如下所示:
cd $GOPATH/src/github.com/hyperledger/fabric
make release
make docker(可能報錯,但是不影響)
對於Macos系統,在編譯之前需要進行以下設定:
- 開啟檔案$GOPATH/src/github.com/hyperledger/fabric/Makefile
- 找到其中的第一個GO_LDFLAGS字串的位置,在該字串所在行的在行的末尾加上字串-s
- 儲存檔案Makefile
上述命令執行完成之後,會自動將將編譯好的二進位制檔案存放在以下路徑中:
- Ubuntu和Centos系統的存放路徑
$GOPATH/src/github.com/hyperledger/fabric/release/linux-amd64/bin
- Macos系統的存放路徑
$GOPATH/src/github.com/hyperledger/fabric/release/darwin-amd64/bin
第四步:Fabric模組的安裝
編譯完成之後,這些模組已經已經可以被運行了,但是目前只能在編譯檔案所在的資料夾中執行這些模組,這樣是非常不方便的。為了更加方便的使用這些模組,可以通過下面的命令將這些模組的可執行檔案複製到系統目錄中,這樣在系統中的任何路徑下面都執行這些可執行這些模組。
Ubuntu和Centos將Fabric模組編譯後的檔案複製到系統資料夾中的方法如下:
cp $GOPATH/src/github.com/hyperledger/fabric/release/linux-amd64/bin/* /usr/local/bin
Macos上面將Fabric模組編譯後的檔案複製到系統資料夾中的方法如下:
cp $GOPATH/src/github.com/hyperledger/fabric/release/darwin-amd64/bin/* /usr/local/bin
複製成功之後通過以下命令修改檔案的執行許可權,否則無法執行。
sudo chmod -R 775 /usr/local/bin/configtxgen
sudo chmod -R 775 /usr/local/bin/configtxlator
sudo chmod -R 775 /usr/local/bin/cryptogen
sudo chmod -R 775 /usr/local/bin/peer
sudo chmod -R 775 /usr/local/bin/orderer
通過上面這些命令之後,可以在系統的任何路徑下面執行這些模組了。下面通過一組命令來進檢查安裝過程是否成功。
第五步: 檢查安裝步驟
採用 version 命令列選項
Fabric的模組
模組名稱 |
功能 |
peer |
主節點模組,負責儲存區塊鏈資料,執行維護鏈碼 |
orderer |
交易打包、排序模組 |
cryptogen |
組織和證書生成模組 |
configtxgedn |
區塊和交易生成模組 |
configtxlator |
區塊和交易解析模組 |
快速啟動Fabric一個Fabric應用
生成Fabric需要的證書檔案
本例中我們將配置檔案和生成的證書檔案放在資料夾/opt/hyperledger/fabricconfig中。
建立存放證書的資料夾的命令如下所示:
mkdir -p /opt/hyperledger/fabricconfig
cryptogen提供了一個命令可以獲取cryptogen模組所需要的配置檔案的樣式,該命令如下所示:
cryptogen showtemplate
可以把上述命令生成的內容複製到一個檔案中稍加修改即可使用。
本例中我們所使用的配置檔案的內容如下所示:
OrdererOrgs:
- Name: Orderer
Domain: qklszzn.com
Specs:
- Hostname: orderer
PeerOrgs:
- Name: Org1
Domain: org1.qklszzn.com
Template:
Count: 2
Users:
Count: 3
- Name: Org2
Domain: org2.qklszzn.com
Template:
Count: 2
Users:
Count: 2
注意Domain屬性
將上述檔案的內容儲存到資料夾/opt/hyperledger/fabricconfig中,配置檔案命名為:crypto-config.yaml。儲存之後執行如下命令:
cd /opt/hyperledger/fabricconfig
cryptogen generate --config=crypto-config.yaml --output ./crypto-config
該命令執行完成之後我們會發現在資料夾/opt/hyperledger/fabricconfig中會新增加一個資料夾crypto-config,裡面存放有本例的相關配置檔案,可以通過tree命令檢視生成證書檔案的內容。
執行 : yum install tree
執行命令 tree -L 5 顯示如下:
├── crypto-config
│ ├── ordererOrganizations
│ │ └── qklszzn.com
│ │ ├── ca
│ │ ├── msp
│ │ ├── orderers
│ │ ├── tlsca
│ │ └── users
│ └── peerOrganizations
│ ├── org1.qklszzn.com
│ │ ├── ca
│ │ ├── msp
│ │ ├── peers
│ │ ├── tlsca
│ │ └── users
│ ├── org2.qklszzn.com
│ │ ├── ca
│ │ ├── msp
│ │ ├── peers
│ │ ├── tlsca
│ │ └── users
通過上述步驟所有的證書檔案都已經生成完畢,現在需要將測試域名對映到本機的IP地址上面,否則後面的操作可能會出現錯誤。
執行以下命令以便提取相關的域名,
cd /opt/hyperledger/fabricconfig
tree -L 5
在上述命令顯示的內容中提取出字尾為 qklszzn.com域名。本例中提取資訊如下:
orderer.qklszzn.com
peer0.org1.qklszzn.com
peer1.org1.qklszzn.com
peer3.org1.qklszzn.com
peer0.org2.qklszzn.com
peer1.org2.qklszzn.com
開啟端對映檔案
vi /etc/hosts
在開啟的檔案中設定如下內容
192.168.43.105 orderer.qklszzn.com
192.168.43.105 peer0.org1.qklszzn.com
192.168.43.105 peer1.org1.qklszzn.com
192.168.43.105 peer3.org1.qklszzn.com
192.168.43.105 peer0.org2.qklszzn.com
192.168.43.105 peer1.org2.qklszzn.com
輸入以上內容之後儲存/etc/hosts檔案,然後用ping命令測試以下配置是否正確。
注意:192.168.43.105為本機的IP地址
第六步 : 創始塊的生成
6.1系統創始塊的生成
Fabric是基於區塊鏈的分散式賬本,每個賬本都擁有自己的區塊鏈,賬本的區塊鏈中會儲存賬本的交易,賬本區塊鏈中的第一個區塊是個例外,該區塊不存在交易資料而是儲存配置資訊,通常將賬本的第一個區塊成為創始塊。綜上所述,Fabric中賬本的第一個區塊是需要手動生成的。configtxgen模組是專門負責生成系統的創始塊和Channel的創始塊。configtxgen模組也需要一個配置檔案來定義相關的屬性。
在Fabric原始碼中提供的configtxgen模組所需要的配置檔案的例子。該檔案的路徑是$GOPATH/src/github.com/hyperledger/fabric/sampleconfig,在這個目錄下面有一個名為configtx.yaml的檔案,對這個檔案進行修即可使用。由於創始塊檔案是提供給Orderer節點使用,因此我們建立檔案一個資料夾來存在Orderer節點相關的檔案。資料夾建立之後把樣例配置檔案複製到該資料夾中。
建立存放configtxgen模組相關配置檔案的資料夾的命令如下所示:
mkdir -p /opt/hyperledger/order/
cp -r $GOPATH/src/github.com/hyperledger/fabric/sampleconfig/configtx.yaml /opt/hyperledger/order
cd /opt/hyperledger/order
對configtx.yaml進行修改,修改後的內容如下所示:
# Copyright IBM Corp. All Rights Reserved.
#
# SPDX-License-Identifier: Apache-2.0
#
---
################################################################################
#
# ORGANIZATIONS
#
# This section defines the organizational identities that can be referenced
# in the configuration profiles.
#
################################################################################
Organizations:
- &OrdererOrg
Name: OrdererOrg
ID: OrdererMSP
MSPDir: /opt/hyperledger/fabricconfig/crypto-config/ordererOrganizations/qklszzn.com/msp
- &Org1
Name: Org1MSP
ID: Org1MSP
MSPDir: /opt/hyperledger/fabricconfig/crypto-config/peerOrganizations/org1.qklszzn.com/msp
Policies: &SampleOrgPolicies
Readers:
Type: Signature
Rule: "OR('Org1MSP.member')"
Writers:
Type: Signature
Rule: "OR('Org1MSP.member')"
Admins:
Type: Signature
Rule: "OR('Org1MSP.admin')"
AnchorPeers:
- Host: peer0.org1.qklszzn.com
Port: 7051
- &Org2
Name: Org2MSP
ID: Org2MSP
MSPDir: /opt/hyperledger/fabricconfig/crypto-config/peerOrganizations/org2.qklszzn.com/msp
Policies: &SampleOrgPolicies
Readers:
Type: Signature
Rule: "OR('Org2MSP.member')"
Writers:
Type: Signature
Rule: "OR('Org2MSP.member')"
Admins:
Type: Signature
Rule: "OR('Org2MSP.admin')"
AnchorPeers:
- Host: peer0.org2.qklszzn.com
Port: 7051
Capabilities:
Channel: &ChannelCapabilities
V1_3: true
Orderer: &OrdererCapabilities
V1_1: true
Application: &ApplicationCapabilities
V1_3: true
V1_2: false
V1_1: false
Application: &ApplicationDefaults
ACLs: &ACLsDefault
# ACL policy for lscc's "getid" function
lscc/ChaincodeExists: /Channel/Application/Readers
# ACL policy for lscc's "getdepspec" function
lscc/GetDeploymentSpec: /Channel/Application/Readers
# ACL policy for lscc's "getccdata" function
lscc/GetChaincodeData: /Channel/Application/Readers
# ACL Policy for lscc's "getchaincodes" function
lscc/GetInstantiatedChaincodes: /Channel/Application/Readers
#---Query System Chaincode (qscc) function to policy mapping for access control---#
# ACL policy for qscc's "GetChainInfo" function
qscc/GetChainInfo: /Channel/Application/Readers
# ACL policy for qscc's "GetBlockByNumber" function
qscc/GetBlockByNumber: /Channel/Application/Readers
# ACL policy for qscc's "GetBlockByHash" function
qscc/GetBlockByHash: /Channel/Application/Readers
# ACL policy for qscc's "GetTransactionByID" function
qscc/GetTransactionByID: /Channel/Application/Readers
# ACL policy for qscc's "GetBlockByTxID" function
qscc/GetBlockByTxID: /Channel/Application/Readers
#---Configuration System Chaincode (cscc) function to policy mapping for access control---#
# ACL policy for cscc's "GetConfigBlock" function
cscc/GetConfigBlock: /Channel/Application/Readers
# ACL policy for cscc's "GetConfigTree" function
cscc/GetConfigTree: /Channel/Application/Readers
# ACL policy for cscc's "SimulateConfigTreeUpdate" function
cscc/SimulateConfigTreeUpdate: /Channel/Application/Readers
#---Miscellanesous peer function to policy mapping for access control---#
# ACL policy for invoking chaincodes on peer
peer/Propose: /Channel/Application/Writers
# ACL policy for chaincode to chaincode invocation
peer/ChaincodeToChaincode: /Channel/Application/Readers
#---Events resource to policy mapping for access control###---#
# ACL policy for sending block events
event/Block: /Channel/Application/Readers
# ACL policy for sending filtered block events
event/FilteredBlock: /Channel/Application/Readers
# Organizations lists the orgs participating on the application side of the
# network.
Organizations:
Policies: &ApplicationDefaultPolicies
Readers:
Type: ImplicitMeta
Rule: "ANY Readers"
Writers:
Type: ImplicitMeta
Rule: "ANY Writers"
Admins:
Type: ImplicitMeta
Rule: "MAJORITY Admins"
Capabilities:
<<: *ApplicationCapabilities
Orderer: &OrdererDefaults
OrdererType: solo
Addresses:
- orderer.qklszzn.com:7050
# Batch Timeout: The amount of time to wait before creating a batch.
BatchTimeout: 2s
BatchSize:
MaxMessageCount: 10
AbsoluteMaxBytes: 10 MB
PreferredMaxBytes: 512 KB
# Max Channels is the maximum number of channels to allow on the ordering
# network. When set to 0, this implies no maximum number of channels.
MaxChannels: 0
Kafka:
# Brokers: A list of Kafka brokers to which the orderer connects. Edit
# this list to identify the brokers of the ordering service.
# NOTE: Use IP:port notation.
Brokers:
- 127.0.0.1:9092
# EtcdRaft defines configuration which must be set when the "etcdraft"
# orderertype is chosen.
EtcdRaft:
Consenters:
- Host: raft0.example.com
Port: 7050
ClientTLSCert: path/to/ClientTLSCert0
ServerTLSCert: path/to/ServerTLSCert0
- Host: raft1.example.com
Port: 7050
ClientTLSCert: path/to/ClientTLSCert1
ServerTLSCert: path/to/ServerTLSCert1
- Host: raft2.example.com
Port: 7050
ClientTLSCert: path/to/ClientTLSCert2
ServerTLSCert: path/to/ServerTLSCert2
# Organizations lists the orgs participating on the orderer side of the
# network.
Organizations:
Policies:
Readers:
Type: ImplicitMeta
Rule: "ANY Readers"
Writers:
Type: ImplicitMeta
Rule: "ANY Writers"
Admins:
Type: ImplicitMeta
Rule: "MAJORITY Admins"
# BlockValidation specifies what signatures must be included in the block
# from the orderer for the peer to validate it.
BlockValidation:
Type: ImplicitMeta
Rule: "ANY Writers"
Capabilities:
<<: *OrdererCapabilities
Channel: &ChannelDefaults
Policies:
# Who may invoke the 'Deliver' API
Readers:
Type: ImplicitMeta
Rule: "ANY Readers"
# Who may invoke the 'Broadcast' API
Writers:
Type: ImplicitMeta
Rule: "ANY Writers"
# By default, who may modify elements at this config level
Admins:
Type: ImplicitMeta
Rule: "MAJORITY Admins"
Capabilities:
<<: *ChannelCapabilities
Profiles:
TestTwoOrgsOrdererGenesis:
<<: *ChannelDefaults
Orderer:
<<: *OrdererDefaults
Organizations:
- *OrdererOrg
Consortiums:
SampleConsortium:
Organizations:
- *Org1
- *Org2
TestTwoOrgsChannel:
Consortium: SampleConsortium
Application:
<<: *ApplicationDefaults
Organizations:
- *Org1
- *Org2
下面是Fabric 1.1版本的,對configtx.yaml進行修改,修改後的內容如下所示:
Profiles:
TestTwoOrgsOrdererGenesis:
Orderer:
<<: *OrdererDefaults
Organizations:
- *OrdererOrg
Consortiums:
SampleConsortium:
Organizations:
- *Org1
- *Org2
TestTwoOrgsChannel:
Consortium: SampleConsortium
Application:
<<: *ApplicationDefaults
Organizations:
- *Org1
- *Org2
Organizations:
- &OrdererOrg
Name: OrdererOrg
ID: OrdererMSP
MSPDir: /opt/hyperledger/fabricconfig/crypto-config/ordererOrganizations/qklszzn.com/msp
- &Org1
Name: Org1MSP
ID: Org1MSP
MSPDir: /opt/hyperledger/fabricconfig/crypto-config/peerOrganizations/org1.qklszzn.com/msp
AnchorPeers:
- Host: peer0.org1.qklszzn.com
Port: 7051
- &Org2
Name: Org2MSP
ID: Org2MSP
MSPDir: /opt/hyperledger/fabricconfig/crypto-config/peerOrganizations/org2.qklszzn.com/msp
AnchorPeers:
- Host: peer0.org2.qklszzn.com
Port: 7051
Orderer: &OrdererDefaults
OrdererType: solo
Addresses:
- orderer.qklszzn.com:7050
BatchTimeout: 2s
BatchSize:
MaxMessageCount: 10
AbsoluteMaxBytes: 98 MB
PreferredMaxBytes: 512 KB
Kafka:
Brokers:
- 127.0.0.1:9092
Organizations:
Application: &ApplicationDefaults
Organizations:
配置檔案修改完成之後執行如下面命令生成創始塊檔案。
cd /opt/hyperledger/order
configtxgen -profile TestTwoOrgsOrdererGenesis -outputBlock ./orderer.genesis.block
上述命令執行完成之後會在資料夾/opt/hyperledger/order中生成檔案orderer.genesis.block。這是Fabric系統的創始塊檔案。
6.2賬本創始塊的生成
建立Channel也是通過configtxgen模組完成的,建立Channel初始塊的配置檔案和建立系統初始塊的配置檔案是一樣的,具體在本例中,Channel的創始塊的配置資訊已經定義在本節第一部分系統創始塊的生成中生成的配置檔案configtx.yaml中。
建立Channel的命令如下:
configtxgen -profile TestTwoOrgsChannel -outputCreateChannelTx ./roberttestchannel.tx -channelID roberttestchannel
上述命令執行完成之後會在目錄生成檔案roberttestchannel.tx,該檔案用來生成Channel。除此之外還需要生成相關的錨點檔案,生成錨點檔案需要執行以下命令:
configtxgen -profile TestTwoOrgsChannel -outputAnchorPeersUpdate ./Org1MSPanchors.tx -channelID roberttestchannel -asOrg Org1MSP
configtxgen -profile TestTwoOrgsChannel -outputAnchorPeersUpdate ./Org2MSPanchors.tx -channelID roberttestchannel -asOrg Org2MSP
上面命令執行完成之後會在相應的資料夾下面生成檔案Org1MSPanchors.tx和Org2MSPanchors.tx,這些檔案在後面會被使用到。
6.3 Orderer節點的啟動
Orderer節點負責交易的打包和區塊的生成。Orderer節點的配置資訊通常放在環境變數或者配置檔案中,本例中的配置資訊統一存放在配置檔案中。在Fabric原始碼提供了Orderer啟動所用到的配置檔案的例項,將示例配置檔案複製到Orderer的資料夾下面修改即可使用。
複製配置檔案到Orderer資料夾的命令如下所示:
cd /opt/hyperledger/order
cp $GOPATH/src/github.com/hyperledger/fabric/sampleconfig/orderer.yaml /opt/hyperledger/order
在模板配置檔案上稍加修改即可滿足本例使用,由於篇幅本例中我們只列出需要修改的部分。修改後配置檔案中發生變化的內容如下:
General:<