The previous article, Fabric2.0 Learning Steps — Mirroring Deployment Networks (2), described how to start a first-network network using the script byfn.sh that comes with Fabric-Sample. To clarify the startup process of a Fabric network, this section implements a custom startup of a Fabric using a binary executable.

This section plans that the Fabric platform consists of three Order nodes, two organizations, and four peer nodes. Each organization has two peer nodes. The Consensus mechanism of Fabric2.0 only supports the RAFT protocol, which implements a consensus that must have at least 3 nodes, preferably an odd number.

In this section, all operations are in/usr/local/SRC/hyperleger/fabric/scripts/fabric samples/first – network directory.

Before performing subsequent operations, set environment variables.

export PATH=${PWD}/.. /bin:${PWD}:$PATH
export FABRIC_CFG_PATH=${PWD}
Copy the code

3.1 Generating a Certificate

The Fabric platform is a licensed blockchain platform that enables platform participants to access platform resources through digital certificates, enhancing network security.

Certificate generation requires the use of the cryptogen tool and the configuration file crypto-config.yaml. According to the platform node planning, modify (remember to back up) crypto-config.yaml as follows:

In the figure above, the Order organization defines three nodes: orderer, orderer2, and orderer3. The peer organization includes Org1 and Org2. Each organization contains two nodes, including Admin user and a common user.

After modifying the configuration file, run the following command to generate certificates for the Order node, peer node, administrator, and user.

cryptogen generate --config=./crypto-config.yaml
Copy the code

After successful execution, the crypto-config directory will be generated under the current directory, as shown in the following figure:

3.2 Genesis Block

Genesis blocks are generated using the Configtxgen tool and the configtx.yaml configuration file. According to the node planning of the platform, the modification (remember to back up) is as follows:

Annotate the configuration associated with orderer4 and orderer5, because the fabric network planned for this article contains only three ORDER nodes.

After modifying the configuration file, run the following command:

configtxgen -profile SampleMultiNodeEtcdRaft -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
Copy the code

After successful execution, the genesis block file geneses.block will be generated in the channel-Artifacts directory.

Fabric1.x is clearly different from Fabric2.0 for genesis block generation, note the command profile parameter values above.

3.3 Generating a Channel transaction Profile

Fabric1.x does not differ significantly from Fabric2.0 in the generation of channel transaction profiles and uses the same commands and profiles as those used to generate genesis blocks.

Run the following command to create a channel transaction profile.

configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
Copy the code

After successful execution, the channel transaction configuration file channel.tx will be generated in the channel-Artifacts directory.

3.4 Generating anchor node configuration files

Fabric1.x is not significantly different from Fabric2.0 for anchor node configuration file generation, and the commands and configuration files used are the same as those used for genesis block generation.

Run the following command to create configuration files of Org1 and Org2.

Generate the anchor node configuration file for Org1
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP

Generate the anchor node configuration file for Org2
configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
Copy the code

After successful execution, two anchor node configuration files org1mSPANcoron.tx and org2mSPANcoron.tx will be generated in the channel-Artifacts directory.

3.5 Starting and Stopping the Network

Fabric network services are started in Docker mode, Yaml involves the docker-compose command and configuration files docker-comement-cli. yaml, docker-comement-etcdraft2. yaml, docker-comement-ca. yaml.

The fabric network planned in this paper includes 10 Docker services: 4 peer node services, 3 Order node services, 2 CA services, and 1 CLI service.

Modify the configuration file docker-comedy-etcdraft2. yaml and comment out the configuration of orderer4 and orderer5.

Run the following command to start three ORDER node services, one CLI node service, four peer node services, and two CA node services.

Configure environment variables. CA nodes are required
export BYFN_CA1_PRIVATE_KEY=$(cd crypto-config/peerOrganizations/org1.example.com/ca && ls \*_sk)
export BYFN_CA2_PRIVATE_KEY=$(cd crypto-config/peerOrganizations/org2.example.com/ca && ls \*_sk)

Start the container service based on the specified configuration file
docker-compose -f docker-compose-cli.yaml -f docker-compose-etcdraft2.yaml -f docker-compose-ca.yaml up -d
Copy the code

The command execution log is as follows:

View the running Docker service, as shown below:

Stop network execution command:

docker-compose -f docker-compose-cli.yaml -f docker-compose-etcdraft2.yaml -f docker-compose-ca.yaml  down
Copy the code

Services on all nodes of the Fabric platform are started properly. Configuration files generated in sections 3.1 to 3.4 are prepared for subsequent transaction operations. You need to log in to the CLI container to run related commands for subsequent operations.

3.6 Creating a Channel

Channels are a feature of Fabric that enables business isolation on a blockchain platform, logically enabling multi-chain operations.

To create a channel, log in to the CLI container and run the following command:

# Connect to the CLI service
docker exec -it cli bash

Define the CA file pathORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.e xample.com/msp/tlscacerts/tlsca.example.com-cert.pemCreate channel information
peer channel create -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/channel.tx --tls true --cafile $ORDERER_CA

# Move the generated files to the channel-Artifacts folder
mv mychannel.block channel-artifacts/
Copy the code

3.7 The peer joins the channel

In the previous step, we created a channel named MyChannel, but the channel has not been joined by any peer node, so the transaction operation cannot be carried out. In this section, four nodes of the two organizations are added to the MyChannel.

By viewing the environment variable, the current CLI container uses the configuration of org1.peer0.

According to the current configuration, we run the following command to add the org1.peer0 node to the myChannel.

peer channel join -b channel-artifacts/mychannel.block
Copy the code

The following figure shows the successful execution result:

Switch the node and add org1.peer1 to myChannel.

Switch to org1.peer1
source scripts/utils.sh
setGlobals 1 1
Add the current node to the channel
peer channel join -b channel-artifacts/mychannel.block
Copy the code

Refer to the above node switching method and add the two nodes of org2 organization into the channel.

# switch to org2.peer0
source scripts/utils.sh
setGlobals 0 2
Add the current node to the channel
peer channel join -b channel-artifacts/mychannel.block

Switch to org2.peer1
setGlobals 1 2
Add the current node to the channel
peer channel join -b channel-artifacts/mychannel.block
Copy the code

At this point, four nodes from both organizations have joined the MyChannel channel.

3.8 Updating anchor nodes

Anchor node is a special peer node in an organization, through which the communication between nodes in different organizations can be realized. Usually, each organization needs to set an anchor node. The following two organizations, org1 and org2, are respectively set anchor nodes. In this paper, it is assumed that peer0 node in the organization is set as the anchor node.

Switch to the org1.peer0 node
setGlobals 0 1
Set the environment variable ORDERER_CAORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.e xample.com/msp/tlscacerts/tlsca.example.com-cert.pemUpdate setup anchor node
peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls true --cafile $ORDERER_CA

Switch to the org2.peer0 node
setGlobals 0 2
Set the environment variable ORDERER_CAORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.e xample.com/msp/tlscacerts/tlsca.example.com-cert.pemUpdate setup anchor node
peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls true --cafile $ORDERER_CA
Copy the code

The following figure shows the result of executing the update command successfully:

After our efforts, we manually configured and started a fabric network consisting of 3 Orderer nodes, 2 organizations, 4 peer nodes, 2 CA nodes, 1 CLI node, and 1 MyChannel. Subsequent articles will conduct chain code operations based on this network.


Author: xiaohui249 this article links: javatech. Wang/index. PHP/a… Edition © all reprinted must be in the form of a link to indicate the author and the original source