The initial number of peers in a Fabric network is fixed when the network is created, but in real application scenarios you may need to add peers to an organization dynamically. Using balance-transfer v1.0 as an example (2 orgs, 4 peers), this post shows how to add a third peer to org1.

1. Add the New Peer's Identity Material

First, add the new peer's certificate and private key to the MSP directory of org1. This mainly uses the cryptogen tool (v1.1.0 or later).

1. Edit cryptogen.yaml (or create a new file): set the count parameter in the Template field to the total number of peers the organization should have. Several peers can be added at once.

Only one peer is added to org1 here, so the modified part of cryptogen.yaml looks like this:

PeerOrgs:
  - Name: Org1
    Domain: org1.example.com
    CA:
       Hostname: ca 
    Template:
      Count: 3
      SANS:
        - "localhost"
    Users:
      Count: 1

2. Run the extend command to add the material
In the artifacts/channel directory, run:

./cryptogen extend --config=./cryptogen.yaml

Note: the --config flag should point to the actual name and path of your config file.

2. Write the Docker Config File for the New Peer

Next, write a Docker config file to start the new peer. It is fairly simple: take the config of an existing peer and make a few small changes, making sure the container ports don't conflict.

I named the new peer's config file new-peer.yaml and put it in the artifacts directory. Its contents are as follows:

version: '2'

services:

  peer2.org1.example.com:
    container_name: peer2.org1.example.com
    extends:
      file:   base.yaml
      service: peer-base
    environment:
      - CORE_PEER_ID=peer2.org1.example.com
      - CORE_PEER_LOCALMSPID=Org1MSP
      - CORE_PEER_ADDRESS=peer2.org1.example.com:7051
    ports:
      - 7151:7051
      - 7153:7053
    volumes:
        - ./channel/crypto-config/peerOrganizations/org1.example.com/peers/peer2.org1.example.com/:/etc/hyperledger/crypto/peer

Start the new peer container by running this in the directory that contains the config file:

docker-compose -f new-peer.yaml up -d

3. Update the Network Config File

The network config file network-config.json tells the application the addresses and other details of the network nodes. It is what lets the app communicate with the nodes, so the new peer's information needs to be added to it.
Note: since balance-transfer 1.1, this file has been replaced by network-config.js.

In the org1 section of the JSON file, add a "peer3" entry after the existing entries in the peers field. The ip, port, and related settings must match the Docker config file above. The entry looks like this:

"peer3": {
    "requests": "grpcs://localhost:7151",
    "events": "grpcs://localhost:7153",
    "server-hostname": "peer2.org1.example.com",
    "tls_cacerts": "../artifacts/channel/crypto-config/peerOrganizations/org1.example.com/peers/peer2.org1.example.com/tls/ca.crt"
}

4. Join the New Peer to the Channel

At this point the new peer has joined the blockchain network, but it hasn't joined any channel yet, so it has no ledger data. Send a request to join it to the channel:

curl -s -X POST \
    http://localhost:4000/channels/mychannel/peers \
    -H "authorization: Bearer $ORG1_TOKEN" \
    -H "content-type: application/json" \
    -d '{
    "peers": ["peer3"]
  }'

In the request, $ORG1_TOKEN is the org1 token obtained through registration, and "peer3" is the alias of the new peer set in the network config file.

After the peer joins the channel successfully, its logs show it starting to sync blocks from the other peers:

docker logs peer2.org1.example.com

5. Install the Chaincode

Once it has joined the channel, peer3 can already take part in maintaining the ledger, but it can't yet be targeted for queries or transactions. For that, send a request to install the chaincode:

curl -s -X POST \
    http://localhost:$PORT/chaincodes \
    -H "authorization: Bearer $ORG1_TOKEN" \
    -H "content-type: application/json" \
    -d '{
    "peers": ["peer3"],
    "chaincodeName":"mycc",
    "chaincodePath":"github.com/example_cc",
    "chaincodeVersion":"v0"
  }'

After the installation succeeds, targeting the new peer for a query or transaction automatically builds the chaincode image for that peer and starts a container to run the chaincode. That completes the process of adding a new peer to an existing organization!

Tags: fabric

12 comments

  1. 您好,运行第四步新节点加入通道出现
    UnauthorizedError: No authorization token was found和UnauthorizedError: Format is Authorization: Bearer [token]这样的错误,请问应该怎么改呢

    1. zyj zyj

      Hello,$ORG1_TOKEN 是表示一个变量,需要预先先通过一个注册接口来获取,像这样:

      TOKEN=$(curl -s -X POST \
      http://$URL/users \
      -H "content-type: application/x-www-form-urlencoded" \
      -d "username=admin&orgName=$CUR_ORG&password=123")
      TOKEN=$(echo $TOKEN | jq ".token" | sed "s/\"//g")

      可以参考: https://github.com/zhayujie/fabric-tools/blob/master/multi-machine-deploy/new-org/add_org.sh 中的addOrg()方法

  2. Hunter 景文 Hunter 景文

    请问您这个network-config.json文件,似乎是仅在balance-transfer项目内存在,那对于常规启动fabric网络的来说,没有这样的文件,又该如何去进行网络的配置呢?(本人fabric 1.4.4)

    1. zyj zyj

      这里的增加节点流程是在fabric-app层完成的(基于node-sdk),一般完整的应用都会有类似的网络配置文件,可能形式会有不同,根据具体情况做相应变化即可。
      对于常规启动的fabric网络(没有sdk和app层),是没有网络配置文件的,也无法通过http接口方式新增节点。建议使用cli容器命令行的方式进行操作(参考first-network示例)。

      1. yf yf

        博主您好,相比cli容器命令行的方式,您的这种方法感觉步骤少了多少,最终效果是一样的么?

        1. zyj zyj

          效果是一样的,cli容器中的命令本质也是执行这些操作

          1. yf yf

            在现有的组织上增加新节点需要更新锚节点么?

            1. zyj zyj

              锚节点用于不同组织间的区块同步,类似于组织的"外交官"

              1.在现有组织上增加新节点需要更新锚节点吗? 不用,因为新增节点不影响该组织原有锚节点的工作。
              2.增加新的组织需要特别设置锚节点吗? 也不用,因为每个组织的orderer节点会默认充当组织内的锚节点角色。

              1. yf yf

                博主您好,我最近也在做一个fabric开发的项目,方便加个联系方式么请教您么
                微信:18362088378

  3. Anapodoton Anapodoton

    请问这个值$ORG1_TOKEN怎么获取啊

    1. zyj zyj

      这个token变量可以发送注册请求来获取: ORG1_TOKEN=$(curl ...)

  4. 催更

Leave a Comment