1. 程式人生 > 其它 >fabric使用配置檔案configtx.yaml生成創世區塊時遇到的坑

fabric使用配置檔案configtx.yaml生成創世區塊時遇到的坑

技術標籤:區塊鏈區塊鏈hyperledgerfabric

首先我的fabric版本是2.3.0,在參考馮翔老師的《區塊鏈開發實戰》之Hyperledger fabric關鍵技術這本書上的配置檔案configtx.yaml生成創世區塊時出現了很多問題。
書上的配置檔案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: 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: &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: &ApplicationDefaults Organizations:

上述的配置檔案在fabric2.3.0中出現了以下問題:

Error 1:Error reading configuration: While parsing config: yaml: unknown anchor 'OrdererDefaults' referenced [recovered]

在這裡插入圖片描述

因為使用的是最新版本,與書本上的版本不同,而最新版本在結構上做了一些改動。在使用‘*’之前,一定要有‘&’定義過了。所以需要把Profiles的內容移到最後。

Error 2:Error reading configuration: yaml: line 50: did not find expected key

在這裡插入圖片描述
這個是空格出了問題,在提示的行號附近檢查空格是否出錯。.yaml檔案不能使用tab鍵。

Error 3:Error reading configuration: yaml: map merge requires map or sequence of maps as the value

在這裡插入圖片描述
這個錯誤說的是什麼對映的問題。我實在不知道我的configtx.yaml到底是哪裡寫錯了,因為我剛開始是手打的,就有很多空格問題啊等等。這個問題在StackOverflow上有解答:在這裡插入圖片描述
就是本文Error 1裡提到的,然而沒有用。
最後是直接複製可以用的configtx.yaml,在上面進行修改,切記不要修改空格。就沒有這些問題了。

Error 4:Error on outputBlock: could not create bootstrapper: could not create channel group: error adding policies to channel group: no policies defined

在這裡插入圖片描述
這個錯誤也是因為新版本!在configtx.yaml中要有相對應的policies才行。

我根據fabric-samples裡面的配置檔案修改了configtx.yaml。
修正後的configtx.yaml:

Organizations:
    - &OrdererOrg
        Name: OrdererOrg
        ID: OrdererMSP
        MSPDir: ../fabricconfig/crypto-config/ordererOrganizations/example.com/msp
        Policies:
            Readers:
                Type: Signature
                Rule: "OR('OrdererMSP.member')"
            Writers:
                Type: Signature
                Rule: "OR('OrdererMSP.member')"
            Admins:
                Type: Signature
                Rule: "OR('OrdererMSP.admin')"

    - &Org1
        Name: Org1MSP
        ID: Org1MSP
        MSPDir: ../fabricconfig/crypto-config/peerOrganizations/org1.example.com/msp
        Policies:
            Readers:
                Type: Signature
                Rule: "OR('Org1MSP.admin', 'Org1MSP.peer', 'Org1MSP.client')"
            Writers:
                Type: Signature
                Rule: "OR('Org1MSP.admin', 'Org1MSP.client')"
            Admins:
                Type: Signature
                Rule: "OR('Org1MSP.admin')"
            Endorsement:
                Type: Signature
                Rule: "OR('Org1MSP.peer')"
        AnchorPeers:
            - Host: peer0.org1.example.com
              Port: 7051
    - &Org2
        Name: Org2MSP
        ID: Org2MSP
        MSPDir: ../fabricconfig/crypto-config/peerOrganizations/org2.example.com/msp
        Policies:
            Readers:
                Type: Signature
                Rule: "OR('Org2MSP.admin', 'Org2MSP.peer', 'Org2MSP.client')"
            Writers:
                Type: Signature
                Rule: "OR('Org2MSP.admin', 'Org2MSP.client')"
            Admins:
                Type: Signature
                Rule: "OR('Org2MSP.admin')"
            Endorsement:
                Type: Signature
                Rule: "OR('Org2MSP.peer')"
        AnchorPeers:
            - Host: peer0.org2.example.com
              Port: 7051
Capabilities:
    Channel: &ChannelCapabilities
        V2_0: true

    Orderer: &OrdererCapabilities
        V2_0: true

    Application: &ApplicationCapabilities
        V2_0: true

Application: &ApplicationDefaults
    Organizations:

    Policies: &ApplicationDefaultPolicies
        LifecycleEndorsement:
            Type: ImplicitMeta
            Rule: "MAJORITY Endorsement"
        Endorsement:
            Type: ImplicitMeta
            Rule: "MAJORITY Endorsement"
        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.example.com:7050
    BatchTimeout: 2s
    BatchSize:
        MaxMessageCount: 10
        AbsoluteMaxBytes: 99 MB
        PreferredMaxBytes: 512 KB
    Kafka:
        Brokers:
            - 127.0.0.1:9092
    Organizations:

    Policies:
        Readers:
            Type: ImplicitMeta
            Rule: "ANY Readers"
        Writers:
            Type: ImplicitMeta
            Rule: "ANY Writers"
        Admins:
            Type: ImplicitMeta
            Rule: "MAJORITY Admins"
        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

還有個需要注意的是MSPDir的值是相對路徑。
成功生成創世區塊:
在這裡插入圖片描述