HyperLeger Fabric開發(九)——HyperLeger Fabric部署實戰(單機)

HyperLeger Fabric開發(九)——HyperLeger Fabric部署實戰(單機)

系統環境:RHEL 7.3操做系統Fabric release 1.3
工程目錄:$GOPATH/src/github.com/hyperledger/fabric/deploy
將fabric-samples/chaincode/sacc拷貝到的工程目錄chaincode目錄下,並在工程目錄下建立channel-artifacts目錄。node

1、crypto-config.yaml文件

一、Orderer節點組織

(1)單節點git

OrdererOrgs:
  - Name: Orderer
    Domain: example.com
    Specs:
      - Hostname: orderer

(2)多節點github

OrdererOrgs:
  - Name: Orderer
    Domain: example.com
    Specs:
      - Hostname: orderer1
      - Hostname: orderer2
      - Hostname: orderer3

排序組織節點全限定域名爲Hostname + Domain
(3)重寫全限定域名golang

# CommonName
# 默認值爲 {{.Hostname}}.{{.Domain}}
OrdererOrgs:
  - Name: Orderer
    Domain: example.com
    Specs:
      - Hostname: orderer
        CommonName: myorderer.example.com

(4)替換Specs爲Template配置式算法

# Template 使用模板定義節點
# Count 節點總數
# Start 節點下標起始值
# Hostname 全限定域名 命名格式 
# Prefix 默認 peer
# Index 取Start值 無配置從1開始自增
OrdererOrgs:
  - Name: Orderer
    Domain: example.com
    Template:
      Count: 2
      # Start: 5
      # Hostname: {{.Prefix}}{{.Index}} # default

二、Peer節點組織

# Domain 參考OrdererOrgs
# EnableNodeOUs 容許節點 OUS -> out of service
# Template 參考OrdererOrgs 可替換爲Specs配置式
# Users -> Count 添加到管理員的用戶賬戶數
PeerOrgs:
  - Name: Org1
    Domain: org1.example.com
    EnableNodeOUs: true
    Template:
      Count: 2
    Users:
      Count: 1
  - Name: Org2
    Domain: org2.example.com
    EnableNodeOUs: true
    Template:
      Count: 2
    Users:
      Count: 1

四個節點的全限定域名爲
peer0.org1.example.com
peer1.org1.example.com
peer0.org2.example.com
peer1.org2.example.com
Template Count=2表示爲組織生成2套公私鑰和證書。Users Count=1表示每一個Template包含的普通User(Admin不包含在計數中)數量爲1,即普通用戶User1@org2.example.com。docker

2、公私鑰、證書生成

一、公私鑰、證書簡介

Fabric中有兩種類型的公私鑰和證書,一種是給節點以前通信安全而準備的TLS證書,另外一種是用戶登陸和權限控制的用戶證書。證書原本應該是由CA來頒發,但目前只有兩個社區,因此目前暫時沒有啓用CA節點,但Fabric提供了一個crytogen工具來生成證書。數據庫

二、crypto-config.yaml文件編寫

HyperLedger官方在fabric-samples/first-network目錄下提供了一份供cryptogen工具使用的配置文件crypto-config.yaml,文件包含須要生成證書和公私鑰的Orderer組織與Peer組織配置。配置文件內容以下:緩存

# 管理排序節點的組織
OrdererOrgs:
  # 定義第一個排序節點組織
  - Name: Orderer
    Domain: example.com
    Specs:
      - Hostname: orderer

# 管理Peer節點的組織
PeerOrgs:
  # 定義第一個組織
  - Name: Org1
    Domain: org1.example.com
    EnableNodeOUs: true
    Template:
      Count: 2
    Users:
      Count: 1
  # 定義第二個組織
  - Name: Org2
    Domain: org2.example.com
    EnableNodeOUs: true
    Template:
      Count: 2
    Users:
      Count: 1

crypto-config.yaml文件包含Orderer組織配置(包含1個Orderer)和Peer組織的配置(包含2個Peer組織org一、org2)。
Name:定義名稱
Domain與Hostname:組合構成節點的名稱,即生成的目錄名稱。
Template Count:用來指定每一個org下所擁有的節點數,本例配置每一個org各2個Peer。
Users Count:用來指定添加進節點的默認用戶數
使用cryptogen能夠生成crypto-config.yaml文件模板,命令以下:安全

cryptogen showtemplate > crypto-config.yaml

三、公私鑰、證書生成

公私鑰、證書生成命令以下:
cryptogen generate --config=./crypto-config.yaml
生成的目錄路徑爲fabric-samples/first-network/crypto-config
證書表明身份,用來在實體間進行通訊以及交易的時候進行簽名與驗證身份。
查看證書文件(數字證書是通過CA認證過的公鑰)的標準爲X.509,編碼格式爲pem,以-----BEGIN開頭,以-----END結尾。X.509數字證書不但包括用戶名和公共密鑰,並且還包括有關該用戶的其它信息。數字證書除了擴展名爲PEM,也能夠是CRT、KEY。
CRT:Certificate縮寫,PEM編碼格式。
KEY:用來存放一個公鑰或私鑰,並不是X.509證書,PEM格式。
證書的默認簽名算法爲ECDSA,Hash算法爲SHA-256。Fabric中設計了三種類型證書:
登記證書(ECert):頒發給提供了註冊憑證的用戶或節點實體,長期有效。(主要就是通ECert對實體身份檢驗)
通訊證書(TLSCert):TLS證書用來保障通訊鏈路安全,控制對網絡層的接入訪問,能夠對遠端實體身份校驗,防止竊聽。
交易證書(TCert):頒發給用戶,控制每一個交易的權限,通常針對某個交易,短時間有效。(此功能Fabric還暫未啓用)
在ChainCode裏能夠經過shim API的GetCreator函數提取調用當前交易的客戶端的×××書,使用GO語言的pem包將證書解碼,而後使用x.509包解析證書中的信息。
生成crypto-config目錄以下:bash

├── ordererOrganizations
│   └── example.com
│       ├── ca
│       │   ├── 705338b2bb55ab5ea4045260ec511cdd4adea4b268be0764e7608934ecc656d8_sk
│       │   └── ca.example.com-cert.pem
│       ├── msp
│       │   ├── admincerts
│       │   │   └── Admin@example.com-cert.pem
│       │   ├── cacerts
│       │   │   └── ca.example.com-cert.pem
│       │   └── tlscacerts
│       │       └── tlsca.example.com-cert.pem
│       ├── orderers
│       │   └── orderer.example.com
│       │       ├── msp
│       │       │   ├── admincerts
│       │       │   │   └── Admin@example.com-cert.pem
│       │       │   ├── cacerts
│       │       │   │   └── ca.example.com-cert.pem
│       │       │   ├── keystore
│       │       │   │   └── 2f0b6bbf98dd6336af2aac04066f58635a46a1d9442a5b68b84c3dce177afa10_sk
│       │       │   ├── signcerts
│       │       │   │   └── orderer.example.com-cert.pem
│       │       │   └── tlscacerts
│       │       │       └── tlsca.example.com-cert.pem
│       │       └── tls
│       │           ├── ca.crt
│       │           ├── server.crt
│       │           └── server.key
│       ├── tlsca
│       │   ├── eb30f77a962408e9d068452478556ff937941ac737d354871cf464a8e7986842_sk
│       │   └── tlsca.example.com-cert.pem
│       └── users
│           └── Admin@example.com
│               ├── msp
│               │   ├── admincerts
│               │   │   └── Admin@example.com-cert.pem
│               │   ├── cacerts
│               │   │   └── ca.example.com-cert.pem
│               │   ├── keystore
│               │   │   └── ef96219b1fd2fb9f5e35ef0e78395bd5b7d6658c0f42676905f2bca21a3f57d4_sk
│               │   ├── signcerts
│               │   │   └── Admin@example.com-cert.pem
│               │   └── tlscacerts
│               │       └── tlsca.example.com-cert.pem
│               └── tls
│                   ├── ca.crt
│                   ├── client.crt
│                   └── client.key
└── peerOrganizations
├── org1.example.com
│   ├── ca
│   │   ├── 9bdc3eb7bf1c0c8ca37ef2308bf3ba4279f6c6434bc6f75feedc66c7d8ceffb4_sk
│   │   └── ca.org1.example.com-cert.pem
│   ├── msp
│   │   ├── admincerts
│   │   │   └── Admin@org1.example.com-cert.pem
│   │   ├── cacerts
│   │   │   └── ca.org1.example.com-cert.pem
│   │   ├── config.yaml
│   │   └── tlscacerts
│   │       └── tlsca.org1.example.com-cert.pem
│   ├── peers
│   │   ├── peer0.org1.example.com
│   │   │   ├── msp
│   │   │   │   ├── admincerts
│   │   │   │   │   └── Admin@org1.example.com-cert.pem
│   │   │   │   ├── cacerts
│   │   │   │   │   └── ca.org1.example.com-cert.pem
│   │   │   │   ├── config.yaml
│   │   │   │   ├── keystore
│   │   │   │   │   └── b5549b156ed07dd8484234a1247c83dd03b5f0711f5c287eb9412780e244c59c_sk
│   │   │   │   ├── signcerts
│   │   │   │   │   └── peer0.org1.example.com-cert.pem
│   │   │   │   └── tlscacerts
│   │   │   │       └── tlsca.org1.example.com-cert.pem
│   │   │   └── tls
│   │   │       ├── ca.crt
│   │   │       ├── server.crt
│   │   │       └── server.key
│   │   └── peer1.org1.example.com
│   │       ├── msp
│   │       │   ├── admincerts
│   │       │   │   └── Admin@org1.example.com-cert.pem
│   │       │   ├── cacerts
│   │       │   │   └── ca.org1.example.com-cert.pem
│   │       │   ├── config.yaml
│   │       │   ├── keystore
│   │       │   │   └── 8fd0ffd3b475955a61de020f3174f31edd509749ee01a1caf4565e99d515dc08_sk
│   │       │   ├── signcerts
│   │       │   │   └── peer1.org1.example.com-cert.pem
│   │       │   └── tlscacerts
│   │       │       └── tlsca.org1.example.com-cert.pem
│   │       └── tls
│   │           ├── ca.crt
│   │           ├── server.crt
│   │           └── server.key
│   ├── tlsca
│   │   ├── 9a4a1c0f9bb06e6f4f08e94bddc6a69adf2426de9d0c19c1f0d5223d69e5b9a5_sk
│   │   └── tlsca.org1.example.com-cert.pem
│   └── users
│       ├── Admin@org1.example.com
│       │   ├── msp
│       │   │   ├── admincerts
│       │   │   │   └── Admin@org1.example.com-cert.pem
│       │   │   ├── cacerts
│       │   │   │   └── ca.org1.example.com-cert.pem
│       │   │   ├── keystore
│       │   │   │   └── f97f63bb97d902e3a444b28cd344f9196d0a03793e380162bc1e415fc7dba64c_sk
│       │   │   ├── signcerts
│       │   │   │   └── Admin@org1.example.com-cert.pem
│       │   │   └── tlscacerts
│       │   │       └── tlsca.org1.example.com-cert.pem
│       │   └── tls
│       │       ├── ca.crt
│       │       ├── client.crt
│       │       └── client.key
│       └── User1@org1.example.com
│           ├── msp
│           │   ├── admincerts
│           │   │   └── User1@org1.example.com-cert.pem
│           │   ├── cacerts
│           │   │   └── ca.org1.example.com-cert.pem
│           │   ├── keystore
│           │   │   └── 88e6d225882f7d162f48108c0a7181d6f56984764a990618a0dd2607f2a18488_sk
│           │   ├── signcerts
│           │   │   └── User1@org1.example.com-cert.pem
│           │   └── tlscacerts
│           │       └── tlsca.org1.example.com-cert.pem
│           └── tls
│               ├── ca.crt
│               ├── client.crt
│               └── client.key
└── org2.example.com
├── ca
│   ├── b5116bb58cd2927a0e2d64c40646f26bd5759ea96e65e30473c92aecadabd868_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
│   │   │   │   └── Admin@org2.example.com-cert.pem
│   │   │   ├── cacerts
│   │   │   │   └── ca.org2.example.com-cert.pem
│   │   │   ├── config.yaml
│   │   │   ├── keystore
│   │   │   │   └── 1afc87575ab9ea9a55d245c6635a4282ce3f190d05ebb2aac4826723321b792b_sk
│   │   │   ├── signcerts
│   │   │   │   └── peer0.org2.example.com-cert.pem
│   │   │   └── tlscacerts
│   │   │       └── tlsca.org2.example.com-cert.pem
│   │   └── tls
│   │       ├── ca.crt
│   │       ├── server.crt
│   │       └── server.key
│   └── peer1.org2.example.com
│       ├── msp
│       │   ├── admincerts
│       │   │   └── Admin@org2.example.com-cert.pem
│       │   ├── cacerts
│       │   │   └── ca.org2.example.com-cert.pem
│       │   ├── config.yaml
│       │   ├── keystore
│       │   │   └── 73ebe67ce05bcd196850adc3b8d8396d61574e5668005f932d26073d6bde8bf5_sk
│       │   ├── signcerts
│       │   │   └── peer1.org2.example.com-cert.pem
│       │   └── tlscacerts
│       │       └── tlsca.org2.example.com-cert.pem
│       └── tls
│           ├── ca.crt
│           ├── server.crt
│           └── server.key
├── tlsca
│   ├── b9c54fa6859f886073a928786d541b7a885c5238b0d117d3aafb7989a1172471_sk
│   └── tlsca.org2.example.com-cert.pem
└── users
├── Admin@org2.example.com
│   ├── msp
│   │   ├── admincerts
│   │   │   └── Admin@org2.example.com-cert.pem
│   │   ├── cacerts
│   │   │   └── ca.org2.example.com-cert.pem
│   │   ├── keystore
│   │   │   └── c52eab0380ac1bc6918893403c5ba9c076634f82b1af161740ae9cb05ff2fada_sk
│   │   ├── signcerts
│   │   │   └── Admin@org2.example.com-cert.pem
│   │   └── tlscacerts
│   │       └── tlsca.org2.example.com-cert.pem
│   └── tls
│       ├── ca.crt
│       ├── client.crt
│       └── client.key
└── User1@org2.example.com
  ├── msp
  │   ├── admincerts
  │   │   └── User1@org2.example.com-cert.pem
  │   ├── cacerts
  │   │   └── ca.org2.example.com-cert.pem
  │   ├── keystore
  │   │   └── 919400ca0386ba0d44ff8d01c1636325d0c96f03766b35e4fdec3940e02e97e5_sk
  │   ├── signcerts
  │   │   └── User1@org2.example.com-cert.pem
  │   └── tlscacerts
  │       └── tlsca.org2.example.com-cert.pem
  └── tls
      ├── ca.crt
      ├── client.crt
      └── client.key

3、證書的結構

一、組織的證書結構

每一個組織都會生成單獨的根證書。
 (1)ca
ca目錄存放組織的根證書和對應的私鑰文件,採用EC算法,證書爲自簽名(自已簽發本身的公鑰)。組織內的實體將基於該證書做爲根證書。
(2)msp
msp目錄存放表明該組織的身份信息。
A、admincerts:存放被根證書籤名的組織管理員的身份驗證證書。
B、cacerts:存放組織的根證書,與ca目錄下的根證書文件相同。
C、tlscacerts:用於TLS的ca證數,證書爲自簽名。
(3)peers(orders)
每一個Orderer或Peer節點的證書結構都是相同的,包括msp和tls目錄。
(4)tlsca
存放組織的TLS證書。
(5)users
用於存放屬於該組織的用戶實體。

二、Peer節點的證書結構

每一個Orderer或Peer節點的證書結構都是相同的,包括msp和tls目錄。
(1)msp
admincerts:存放組織管理員的身份驗證證書,用於驗證交易簽名者是否爲管理員身份。
cacerts:存放組織的根證書。
keystore:本節點的身份私鑰,用來簽名。
signcerts:驗證本節點簽名的證書,被組織根證書籤名。
tlscacerts:TLS鏈接用的×××書,即組織的TLS證書。
(2)tls
存放tls相關的證書和私鑰。
ca.crt:組織的根證書。
server.crt:驗證本節點簽名的證書,被組織根證書籤名。
server.key:本節點的身份私鑰,用來簽名。

三、用戶實體的證書結構

(1)Admin@org1.example.com

存放管理員用戶的信息,包括msp目錄和tls目錄

(A)msp

admincerts:存放管理員證書(Admin@org1.example.com-cert.pem)

cacerts:存放組織的根證書(ca.org1.example.com-cert.pem)

keystore:存放本用戶的身份私鑰,用來簽名

signcerts:存放管理員用戶的身份驗證證書(Admin@org1.example.com-cert.pem),由組織根證書籤名,要放到Peer節點的msp/admincerts下才會被Peer節點承認

tlscacerts:TLS鏈接用的×××書,即組織TLS證書(tlsca.org1.example.com-cert.pem)
(B)tls

ca.crt:組織的根證書

client.crt: 管理員用戶的身份驗證證書,由組織根證書籤名

client.key:管理員的身份私鑰,用來簽名

(2)User1@org1.example.com

User1是User1用戶的信息,包括msp證書和tls證書

(A)msp

admincerts:用戶×××書(User1@org1.example.com-cert.pem)

cacerts:存放組織的根證書(ca.org1.example.com-cert.pem)
keystore:本用戶的身份私鑰,用來簽名

signcerts:用戶的身份驗證證書(User1@org1.example.com-cert.pem),由組織根證書籤名

tlscacerts:TLS鏈接用的×××書,即組織TLS證書(tlsca.org1.example.com-cert.pem)

(B)tls

ca.crt:組織的根證書。

client.crt: 用戶的身份驗證證書,由組織根證書籤名。

client.key:用戶身份私鑰,用來簽名。

4、configtx.yaml文件

一、configtx.yaml文件簡介

configtx.yaml文件定義了將要建立通道的配置信息,一般包括如下部分:
A、Profiles:包含了通道的配置模板,經過configtxgen工具的參數 -profile來指定使用哪一個模板。
B、Organizations: 定義了組織以及相應的MSP。
C、Orderer: 定義系統通道的相關配置,如排序節點地址、共識算法。
D、Application: 定義應用通道相關配置,被Profiles引用。

二、Organization

Organizations主要定義一系列的組織結構,根據服務對象類型的不一樣,分爲Orderer 組織和普通應用組織,Orderer類型組織包括名稱、ID、MSP目錄路徑、管理員策略等,應用類型組織還會配置錨節點信息。Organizations所定義的組織會被Profiles部分使用。

Organizations:
    - &OrdererOrg
        Name: OrdererOrg
        ID: OrdererMSP
        MSPDir: crypto-config/ordererOrganizations/example.com/msp
    - &Org1
        Name: Org1MSP
        ID: Org1MSP
        MSPDir: crypto-config/peerOrganizations/org1.example.com/msp
        AnchorPeers:
            - Host: peer0.org1.example.com
              Port: 7051
    - &Org2
        Name: Org2MSP
        ID: Org2MSP
        MSPDir: crypto-config/peerOrganizations/org2.example.com/msp
        AnchorPeers:
            - Host: peer0.org2.example.com
              Port: 7051

三、Orderer

Orderer部分定義Orderer系統通道自身的配置信息,包括Orderer類型、地址、批處理限制、Kafka信息、最大應用通道數目等,參與到此Orderer的組織信息。

Orderer: &OrdererDefaults
    OrdererType: solo
    Addresses:
        - orderer.example.com:7050                   
    BatchTimeout: 2s
    BatchSize:
        MaxMessageCount: 10
        AbsoluteMaxBytes: 99 MB
        PreferredMaxBytes: 512 KB
    Kafka:
        Brokers:
            - 127.0.0.1:9092
    Organizations:

四、Application

Application定義應用通道相關配置,被Profiles引用。

Application: &ApplicationDefaults
    Organizations:

五、Profiles

Profiles定義了系統通道和應用通道兩種不一樣類型的通道。系統通道必須定義Orderer和Consortiums兩部分,應用通道必須定義Application和Consortium兩部分。

Profiles:
    TwoOrgsOrdererGenesis:
        Orderer:
            <<: *OrdererDefaults
            Organizations:
                - *OrdererOrg
        Consortiums:
            SampleConsortium:
                Organizations:
                    - *Org1
                    - *Org2
    TwoOrgsChannel:
        Consortium: SampleConsortium
        Application:
            <<: *ApplicationDefaults
            Organizations:
                - *Org1
                - *Org2

應用通道模板中必須包括Orderer和Consortiums信息。
Orderer:指定Orderer系統通道自身的配置信息,一般引用Orderer部分的定義以及Organizations部分的定義。網絡啓動時,必須首先創Orderer系統通道。
Consortiums: Orderer所服務的聯盟列表。每一個聯盟中組織彼此使用相同的通道建立策略,能夠彼此建立應用通道。
應用通道模板中必須包括Application、Consortium信息:
Application:指定屬於某應用通道的信息,主要包括屬於通道的組織信息。
Consortium:應用通道所關聯的聯盟的名稱。

六、configtx.yaml實例

Organizations:
    - &OrdererOrg
        Name: OrdererOrg
        ID: OrdererMSP # MSP ID
        MSPDir: crypto-config/ordererOrganizations/example.com/msp #MSP相關文件本地路徑
    - &Org1
        Name: Org1MSP
        ID: Org1MSP
        MSPDir: crypto-config/peerOrganizations/org1.example.com/msp
        AnchorPeers: #錨節點地址,用於跨組織的Gossip通訊
            - Host: peer0.org1.example.com
              Port: 7051
    - &Org2
        Name: Org2MSP
        ID: Org2MSP
        MSPDir: crypto-config/peerOrganizations/org2.example.com/msp
        AnchorPeers: #錨節點地址,用於跨組織的Gossip通訊
            - Host: peer0.org2.example.com
              Port: 7051
Orderer: &OrdererDefaults
    OrdererType: solo # Orderer共識插件類型,分solo或kafka
    Addresses:
        - orderer.example.com:7050 #服務地址
    BatchTimeout: 2s #出塊間隔
    BatchSize: #寫入區塊內的交易個數
        MaxMessageCount: 10 #一批消息的最大個數
        AbsoluteMaxBytes: 98 MB #一批交易的最大字節數,任什麼時候候均不能超過
        PreferredMaxBytes: 512 KB #批量交易的建議字節數
    Kafka:
        Brokers: #Kafka端口
            - 127.0.0.1:9092
    Organizations: #參與維護Orderer的組織,默認空

Application: &ApplicationDefaults
    Organizations: #加入到通道的組織信息,此處爲不包括任何組織

Profiles:
    TwoOrgsOrdererGenesis: #Orderer系統通道配置
        Orderer:
            <<: *OrdererDefaults #引用OrdererDefaults併合併到當前
            Organizations: #屬於Orderer通道的組織
                - *OrdererOrg 
        Consortiums: #Orderer所服務的聯盟列表
            SampleConsortium:
                Organizations:
                    - *Org1
                    - *Org2
    TwoOrgsChannel: #應用通道配置
        Consortium: SampleConsortium #應用通道關聯的聯盟
        Application: 
            <<: *ApplicationDefaults #引用ApplicationDefaults併合併到當前
            Organizations: #初始加入應用通道的組織
                - *Org1
                - *Org2

configtx.yaml配置文件通常包括Profiles、Organizations、Orderer和Application四個部分。
Profiles用於配置聯盟,包括Orderer系統通道模板和應用通道模板,其中TwoOrgsOrdererGenesi爲系統通道模板,TwoOrgsChannel爲應用通道模板。

5、configtxgen工具

一、configtxgen主要功能

configtxgen是Fabric提供的工具,用於生成通道所須要的配置文件。configtxgen工具以一個yaml文件做爲輸入,通常爲configtx.yaml。
configtxgen主要功能以下:
A、生成啓動orderer須要的創世區塊,並支持檢查區塊內容
B、生成建立應用通道須要的配置交易,並支持檢查交易內容
C、生成錨點Peer的更新配置交易

二、configtxgen命令使用

配置選項:
-profile string:從configtx.yaml中查找到指定的profile來生成配置,默認使用Sample-InsecureSolo
-channelID string:指定操做的通道名稱,默認是mychannel
生成選項:
-outputBlock string:將初始區塊寫入指定文件
-outputCreateChannelTx string:將通道建立交易寫入指定文件
-outputAnchorPeersUpdate string:建立更新錨點Peer的配置更新請求,須要同時使用-asOrg來指明組織身份
-asOrg string:以指明的組織身份執行更新配置交易(入更新錨節點)的生成,意味着寫集合中包含了該組織有權限操做的鍵值
查看選項:
-inspectBlock string:打印指定區塊文件中的配置信息
-inspectChannelCreateTx:打印通道建立交易文件中的配置更新信息

三、Configtxgen示例

定義好configtx.yml文件後,須要把configtxgen工具以及msp目錄都拷貝到yaml文件的所在的目錄下,configtxgen默認會讀取當前目錄的configtx.yaml做爲輸入:
A、建立排序節點的初始區塊:
configtxgen -profile Genesis -outputBlock genesis.block
經過profile參數來指定生成yaml文件中Profile.Genesis的配置,經過 -outputBlock參數來將區塊寫入genesis.block文件。
B、建立應用通道mychannel的初始區塊的交易文件channel.tx:
configtxgen -profile Channel -outputCreateChannelTx channel.tx -channelID mychannel
經過-outputCreateChannelTx參數將生成的交易寫入channel.tx文件,經過-channelID來指定建立通道的名稱爲mychannel。
C、建立配置區塊的交易文件Org1MSPanchors.tx以更新mychannel中PeerOrg1的錨節點:
configtxgen -profile Channel -outputAnchorPeersUpdate Org1MSPanchors.tx -channelID mychannel -asOrg PeerOrg1MSP
經過-asOrg來指定使用PeerOrg1MSP身份建立配置區塊,而且經過-outputAnchorPeersUpdate參數將配置區塊寫入到文件Org1MSPanchors.tx中。
建立配置區塊的交易文件Org2MSPanchors.tx以更新mychannel中 PeerOrg2的錨節點:
configtxgen -profile Channel -outputAnchorPeersUpdate Org2MSPanchors.tx -channelID mychannel -asOrg PeerOrg2MSP

6、Fabric網絡部署

一、生成創世區塊

生成Orderer節點啓動所需的創世區塊
configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/orderer.genesis.block
經過profile參數來指定生成yaml文件中Profile.Genesis的配置,經過 -outputBlock參數來將區塊寫入genesis.block文件。
查看通道配置
configtxgen -profile TwoOrgsOrdererGenesis -inspectBlock ./channel-artifacts/orderer.genesis.block

二、生成業務通道交易配置文件

生成建立應用通道的交易配置文件
configtxgen -profile TwoOrgsChannel -channelID assetchannel -outputCreateChannelTx ./channel-artifacts/assetchannel.tx
經過-outputCreateChannelTx參數將生成的交易寫入channel.tx文件,經過-channelID來指定建立通道的名稱爲assetchannel。
查看配置文件內容
configtxgen -profile TwoOrgsChannel -inspectChannelCreateTx ./channel-artifacts/assetchannel.tx

三、生成更新組織錨節點配置文件

生成更新組織錨節點的配置信息文件
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID assetchannel -asOrg Org1MSP
經過-asOrg來指定使用Org1MSP身份建立配置區塊,而且經過-outputAnchorPeersUpdate參數將配置區塊寫入到文件Org1MSPanchors.tx中。
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID assetchannel -asOrg Org2MSP
經過-asOrg來指定使用Org2MSP身份建立配置區塊,而且經過-outputAnchorPeersUpdate參數將配置區塊寫入到文件Org2MSPanchors.tx中。

四、docker-compose.yaml文件編寫

version: '2'

services:
  orderer.example.com:
    container_name: orderer.example.com
    image: hyperledger/fabric-orderer
    environment:
      - ORDERER_GENERAL_LOGLEVEL=debug
      - ORDERER_GENERAL_LISTENADDRESS=0.0.0.0
      - ORDERER_GENERAL_GENESISMETHOD=file
      - ORDERER_GENERAL_GENESISFILE=orderer.genesis.block
      - ORDERER_GENERAL_LOCALMSPID=OrdererMSP
      - ORDERER_GENERAL_LOCALMSPDIR=/etc/hyperledger/orderer/msp
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/orderer
    command: orderer
    volumes:
      - ./crypto-config/ordererOrganizations/example.com/orderers/orderer.example.com/:/etc/hyperledger/orderer
      - ./channel-artifacts/orderer.genesis.block:/etc/hyperledger/fabric/orderer.genesis.block
    ports:
      - 7050:7050

  peer:
    container_name: peer
    image: hyperledger/fabric-peer
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock # docker的服務端注入
      - CORE_LOGGING_PEER=debug
      - CORE_CHAINCODE_LOGGING_LEVEL=DEBUG
      - CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/peer/msp # msp證書(節點證書)
      - CORE_LEDGER_STATE_STATEDATABASE=goleveldb # 狀態數據庫的存儲引擎(or CouchDB)
      - CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=deploy_default# 鏈碼與Peer節點使用同一網絡
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric
    command: peer node start

  peer0.org1.example.com:
    extends:
      service: peer
    container_name: peer0.org1.example.com
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_PEER_ID=peer0.org1.example.com
      - CORE_PEER_LOCALMSPID=Org1MSP
      - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
    ports:
      - 17051:7051 # grpc服務端口
      - 17053:7053 # eventhup端口
    volumes:
      - /var/run/:/host/var/run/
      - ./crypto-config/peerOrganizations/org1.example.com/peers/peer0.org1.example.com:/etc/hyperledger/peer
    depends_on:
      - orderer.example.com

  peer1.org1.example.com:
    extends:
      service: peer
    container_name: peer1.org1.example.com
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_PEER_ID=peer1.org1.example.com
      - CORE_PEER_LOCALMSPID=Org1MSP
      - CORE_PEER_ADDRESS=peer1.org1.example.com:7051
    ports:
      - 27051:7051 # grpc服務端口
      - 27053:7053 # eventhup端口
    volumes:
      - /var/run/:/host/var/run/
      - ./crypto-config/peerOrganizations/org1.example.com/peers/peer1.org1.example.com:/etc/hyperledger/peer
    depends_on:
      - orderer.example.com

  peer0.org2.example.com:
    extends:
      service: peer
    container_name: peer0.org2.example.com
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_PEER_ID=peer0.org2.example.com
      - CORE_PEER_LOCALMSPID=Org2MSP
      - CORE_PEER_ADDRESS=peer0.org2.example.com:7051
    ports:
      - 37051:7051 # grpc服務端口
      - 37053:7053 # eventhup端口
    volumes:
      - /var/run/:/host/var/run/
      - ./crypto-config/peerOrganizations/org2.example.com/peers/peer0.org2.example.com:/etc/hyperledger/peer
    depends_on:
      - orderer.example.com

  peer1.org2.example.com:
    extends:
      service: peer
    container_name: peer1.org2.example.com
    environment:
      - CORE_VM_ENDPOINT=unix:///host/var/run/docker.sock
      - CORE_PEER_ID=peer1.org2.example.com
      - CORE_PEER_LOCALMSPID=Org2MSP
      - CORE_PEER_ADDRESS=peer1.org2.example.com:7051
    ports:
      - 47051:7051 # grpc服務端口
      - 47053:7053 # eventhup端口
    volumes:
      - /var/run/:/host/var/run/
      - ./crypto-config/peerOrganizations/org2.example.com/peers/peer1.org2.example.com:/etc/hyperledger/peer
    depends_on:
      - orderer.example.com

  cli:
    container_name: cli
    image: hyperledger/fabric-tools
    tty: true
    environment:
      - GOPATH=/opt/gopath
      - CORE_LOGGING_LEVEL=DEBUG
      - CORE_PEER_ID=cli
      - CORE_PEER_ADDRESS=peer0.org1.example.com:7051
      - CORE_PEER_LOCALMSPID=Org1MSP
      - CORE_PEER_MSPCONFIGPATH=/etc/hyperledger/peer/users/Admin@org1.example.com/msp
    working_dir: /opt/gopath/src/github.com/hyperledger/fabric/
    command: /bin/bash
    volumes:
        - ./channel-artifacts:/etc/hyperledger/channel-artifacts
        - ./chaincode:/opt/gopath/src/github.com/chaincode # 鏈碼路徑注入
        - ./crypto-config/peerOrganizations/org1.example.com/:/etc/hyperledger/peer

五、啓動Fabric網絡

使用docker-compose啓動Fabric網絡
docker-compose -f docker-compose.yaml up
若是Fabric網絡啓動失敗,查看日誌信息,肯定錯誤緣由。修改docker-compose.yaml文件後從新啓動。在啓動前須要先關閉網絡,清除網絡緩存。
關閉全部的Docker容器
docker rm -f $(docker ps -aq)
清除網絡緩存
docker network prune

7、Fabric區塊鏈部署

一、建立業務通道

進入cli容器:
docker exec -it cli bash
進入/etc/hyperledger/channel-artifacts目錄:
cd /etc/hyperledger/channel-artifacts
建立業務通道:
peer channel create -o orderer.example.com:7050 -c assetchannel -f assetchannel.tx
在當前目錄下會生成assetchannel.block區塊

二、加入通道

peer channel join -b assetchannel.block

三、設置主節點

peer channel update -o orderer.example.com:7050 -c assetchannel -f Org1MSPanchors.tx

四、鏈碼安裝

安裝sacc鏈碼:
peer chaincode install -n sacc -v 1.0.0 -l golang -p github.com/chaincode/sacc

五、鏈碼實例化

peer chaincode instantiate -o orderer.example.com:7050 -C assetchannel -n sacc -l golang -v 1.0.0 -c '{"Args":["user1","100"]}'

六、鏈碼查詢

查詢用戶餘額:
peer chaincode query -n sacc -c '{"Args":["get","user1"]}' -C assetchannel
設置用戶餘額:
peer chaincode invoke -n sacc -c '{"Args":["set", "user1", "1000"]}' -C assetchannel

8、Fabric區塊鏈部署的問題

一、工程示例

HyperLeger Fabric開發(九)——HyperLeger Fabric部署實戰(單機)
上述工程位於deploy目錄下。

二、網絡名稱問題

Error: Error endorsing chaincode: rpc error: code = Unknown desc = Error starting container: API error (404): {「message」:「network deploy_default not found」}
docker–compose啓動網絡時會建立一個默認網絡名:[directory]_default
CORE_VM_DOCKER_HOSTCONFIG_NETWORKMODE=deploy_default環境變量用於指定建立的網絡名稱。網絡名稱可使用docker network ls命令查看。

相關文章
相關標籤/搜索