Configure Multiple Channel in Hyperledger Fabric

2019-02-19 00:57发布

问题:

I am trying to make two channels with hyper ledger fabric. In one channel there will be two organizations and in another channel, there will be another two organizations. In every organization, there will be two peers. So total 8 peers in 4 organizations with two channels. Until now I have done these:

  1. In crypto-config.yaml file I have declared one orderer and four organizations. In every organization, I have declared template count: 2 and users count: 1 (can anyone please explain the meaning of template count? Is it a total number of peers?)
  2. Then used cryptogen generate --config=./crypto-config.yaml command to generate keys and certificates.
  3. In configtx.yaml file, I have declared two channels with their organization's name:

Profiles:

FourOrgsOrdererGenesis:
    Orderer:
        <<: *OrdererDefaults
        Organizations:
            - *OrdererOrg
    Consortiums:
        SampleConsortium:
            Organizations:
                - *Org1
                - *Org2
                - *Org3
                - *Org4
TwoOrgsChannel1:
    Consortium: SampleConsortium
    Application:
        <<: *ApplicationDefaults
        Organizations:
            - *Org1
            - *Org2
TwoOrgsChannel2:
    Consortium: SampleConsortium
    Application:
        <<: *ApplicationDefaults
        Organizations:
            - *Org3  
            - *Org4

Organizations:

- &OrdererOrg
    Name: OrdererOrg
    ID: OrdererMSP
    MSPDir: crypto-config/ordererOrganizations/acme.com/msp
- &Org1
    Name: Org1MSP

    ID: Org1MSP

    MSPDir: crypto-config/peerOrganizations/org1.acme.com/msp

    AnchorPeers:
        - Host: peer0.org1.acme.com
          Port: 7051

- &Org2
    Name: Org2MSP

    ID: Org2MSP

    MSPDir: crypto-config/peerOrganizations/org2.acme.com/msp

    AnchorPeers:
        - Host: peer0.org2.acme.com
          Port: 7051

- &Org3
    Name: Org3MSP

    ID: Org3MSP

    MSPDir: crypto-config/peerOrganizations/org3.acme.com/msp

    AnchorPeers:
        - Host: peer0.org3.acme.com
          Port: 7051

- &Org4
    Name: Org4MSP

    ID: Org4MSP

    MSPDir: crypto-config/peerOrganizations/org4.acme.com/msp

    AnchorPeers:
        - Host: peer0.org4.acme.com
          Port: 7051

Orderer: &OrdererDefaults

OrdererType: solo

Addresses:
    - orderer.acme.com:7050

BatchTimeout: 2s

BatchSize:

    MaxMessageCount: 10

    AbsoluteMaxBytes: 99 MB

    PreferredMaxBytes: 512 KB

Kafka:
    Brokers:
        - 127.0.0.1:9092

Organizations:

Application: &ApplicationDefaults

Organizations:
  1. Then I have created genesis block using this command

    configtxgen -profile FourOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block

Probably, I need to make two genesis block as there are two channels. In that case, what will be the command instead of above one?

  1. I have used following commands to make channel transaction and anchor peer transaction.

    configtxgen -profile TwoOrgsChannel1 -outputCreateChannelTx ./channel-artifacts/channel1.tx -channelID mychannel1
    configtxgen -profile TwoOrgsChannel2 -outputCreateChannelTx ./channel-artifacts/channel2.tx -channelID mychannel2    
    configtxgen -profile TwoOrgsChannel1-outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel1 -asOrg Org1MSP
    configtxgen -profile TwoOrgsChannel1 -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel1 -asOrg Org2MSP
    configtxgen -profile TwoOrgsChannel2-outputAnchorPeersUpdate ./channel-artifacts/Org3MSPanchors.tx -channelID mychannel2 -asOrg Org3MSP
    configtxgen -profile TwoOrgsChannel2-outputAnchorPeersUpdate ./channel-artifacts/Org4MSPanchors.tx -channelID mychannel2 -asOrg Org4MSP
    
  2. Finally used this command to make up:

    CHANNEL_NAME=mychannel1 docker-compose -f docker-compose-cli.yaml up –d

Whenever I am trying to run:

CHANNEL_NAME=mychannel2 docker-compose -f docker-compose-cli.yaml up –d

It is saying already peers are running.

So my questions are:

  • How can I create multiple channels for different organizations? I did not get any elaborative examples.
  • Is it possible to communicate between two channels?
  • What if peer0 is subscribed to both channels? Will it be able to communicate both channels and transfer data in both channels?

回答1:

After some trial and error, I have found the problem. Actually, I thought, if I want to make multiple channels then I have to use docker-compose command multiple times, which is not correct. The correct syntax for creating multiple channels are:

cryptogen generate --config=./crypto-config.yaml

export FABRIC_CFG_PATH=$PWD

configtxgen -profile FourOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block

configtxgen -profile TwoOrgsChannel1 -outputCreateChannelTx ./channel-artifacts/channel1.tx -channelID mychannel1

configtxgen -profile TwoOrgsChannel2 -outputCreateChannelTx ./channel-artifacts/channel2.tx -channelID mychannel2

configtxgen -profile TwoOrgsChannel1 -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel1 -asOrg Org1MSP

configtxgen -profile TwoOrgsChannel1 -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel1 -asOrg Org2MSP

configtxgen -profile TwoOrgsChannel2 -outputAnchorPeersUpdate ./channel-artifacts/Org3MSPanchors.tx -channelID mychannel2 -asOrg Org3MSP

configtxgen -profile TwoOrgsChannel2 -outputAnchorPeersUpdate ./channel-artifacts/Org4MSPanchors.tx -channelID mychannel2 -asOrg Org4MSP

docker-compose -f docker-compose-cli.yaml up -d



回答2:

  • How can I create multiple channels for different organizations? I did not get any elaborative examples.

    I think you need one genesis block to bootstrap the orderer. Rest you need multiple channel config transaction files. (.tx). You can use these channel file to create and join the channel using either an SDK (see https://fabric-sdk-node.github.io/tutorial-channel-create.html or https://github.com/hyperledger/fabric-samples/tree/release/balance-transfer/app) or the peer command using the fabric-cli (I could not find a doc but you can try the following sections : https://github.com/hyperledger/fabric-samples/blob/release/first-network/scripts/script.sh#L61, https://github.com/hyperledger/fabric-samples/blob/release/first-network/scripts/script.sh#L92)

  • Is it possible to communicate between two channels?

One word answer no. But you can do it via cross-chaincode calls.

  • What if peer0 is subscribed to both channels? Will it be able to communicate both channels and transfer data in both channels?

Yes it can do that. You can refer to http://hyperledger-fabric.readthedocs.io/en/release/channels.html. Inter-channel data transfer is not possible directly. But you can invoke a chaincode (working on channel A) from another chaincode (working on channel B), provided you have permissions to invoke chaincode on both channels.



回答3:

Try this profile config

FourOrgsOrdererGenesis:
    Orderer:
        <<: *OrdererDefaults
        Organizations:
            - *OrdererOrg
    Consortiums:
        ConsortiumOne:
            Organizations:
                - *Org1
                - *Org2
        ConsortiumTwo:
            Organizations:
                - *Org3
                - *Org4
TwoOrgsChannel1:
    Consortium: ConsortiumOne
    Application:
        <<: *ApplicationDefaults
        Organizations:
            - *Org1
            - *Org2
TwoOrgsChannel2:
    Consortium: ConsortiumTwo
    Application:
        <<: *ApplicationDefaults
        Organizations:
            - *Org3
            - *Org4

This works for me.

Setting Hyperlegder Fabric Network Multiple Chaincodes Channels, hope this article also can help you.