The previous article, "advanced learning of Fabric 2.0 - image deployment network (2)", introduced how to use the script provided by Fabric sample byfn.sh To start the first network. In order to understand the startup process of Fabric network, this section completes the custom startup of Fabric through binary executable program.
In this section, the Fabric platform is planned to be composed of three order nodes, two organizations and four peer nodes, each of which has two peer nodes. The consensus mechanism of Fabric 2.0 only supports the raft protocol. The consensus implemented by the protocol must be at least 3 nodes, and the number of nodes should be odd.
All operations in this section are performed in the / usr / local / SRC / hyperleger / fabric / scripts / fabric samples / first network directory.
Set the relevant environment variables before subsequent operations.
export PATH=${PWD}/../bin:${PWD}:$PATH export FABRIC_CFG_PATH=${PWD}
3.1 generate certificate
Fabric platform is a licensed blockchain platform. Through digital certificates, platform participants can access platform resources, which makes the network more secure.
Certificate generation needs to use cryptogen tool and configuration file crypto-config.yaml . Modify (remember to back up) crypto according to the node planning of the platform- config.yaml As follows:
In the above figure, the Order organization defines three nodes: orderer, orderer2, orderer3. The peer organization includes Org1 and Org2. Each organization includes two nodes, including the Admin user and a common user.
After modifying the configuration file, run the following commands directly to complete the certificate generation of order node, peer node, administrator and user.
cryptogen generate --config=./crypto-config.yaml
After successful execution, the crypto config directory will be generated under the current directory. The directory structure is shown in the following figure:
![https://imgchr.com/i/NtsDbV]()
3.2 Chuangshi block
configtxgen tool and configuration file are needed to generate the genesis block configtx.yaml . According to the node planning of the platform, modify (remember to back up) the contents as follows:
![https://imgchr.com/i/Nts65F]()
Note the configuration related to order4 and order5, because the fabric network planned in this paper only contains three order nodes.
After modifying the configuration file, execute the following command:
configtxgen -profile SampleMultiNodeEtcdRaft -channelID byfn-sys-channel -outputBlock ./channel-artifacts/genesis.block
After successful execution, the genesis block file will be generated in the channel artifacts directory genesis.block .
![https://imgchr.com/i/NtsyUU]()
For the generation of the creation block, fabric1.x and fabric2.0 have obvious differences. Pay attention to the above command profile parameter value.
3.3 generate channel transaction profile
There is no obvious difference between fabric1.x and fabric2.0 in the generation of channel transaction configuration files. The commands and configuration files used are the same as those used in the generation of Chuangshi block.
Execute the following command to complete the creation of channel transaction profile.
configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/channel.tx -channelID mychannel
After successful execution, the channel transaction configuration file will be generated in the channel artifacts directory channel.tx .
![https://imgchr.com/i/NtsBD0]()
3.4 generate anchor node configuration file
There is no obvious difference between fabric1.x and fabric2.0 in the generation of anchor node configuration files. The commands and configuration files used are the same as those used in the generation of Genesis block.
Execute the following command to complete the creation of Org1 and Org2 organization anchor node configuration files.
# Generate anchor node profile for Org1 configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID mychannel -asOrg Org1MSP # Generate anchor node profile for Org2 configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID mychannel -asOrg Org2MSP
After successful execution, two anchor node configuration files org1 will be generated in the channel artifacts directory MSPanchors.tx And org2 MSPanchors.tx .
![https://imgchr.com/i/Ntsags]()
3.5 start & stop network
Fabric network service is started in docker mode, involving command docker compose and configuration file docker compose- cli.yaml ,docker-compose-etcdraft2.yaml,docker-compose-ca.yaml .
The planned fabric network includes 10 docker services: 4 peer node services, 3 order node services, 2 CA services and 1 cli service.
Modify the configuration file docker-compose-etcdraft 2.yaml, and comment out the relevant configuration of orderer4 and orderer5.
![https://imgchr.com/i/Nts0uq]()
![https://imgchr.com/i/Nts28J]()
Execute the following command to start three order node services, one cli node service, four peer node services and two CA node services.
# Configuration environment variable, CA node 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 container service according to specified profile docker-compose -f docker-compose-cli.yaml -f docker-compose-etcdraft2.yaml -f docker-compose-ca.yaml up -d
The command execution log is as follows:
![https://imgchr.com/i/NtsgC4]()
View the running docker service, as shown in the following figure:
![https://imgchr.com/i/NtshK1]()
Stop network execution command:
docker-compose -f docker-compose-cli.yaml -f docker-compose-etcdraft2.yaml -f docker-compose-ca.yaml down
All node services of Fabric platform are started normally. The configuration files generated in section 3.1-3.4 are prepared for subsequent transaction operations. Subsequent operations need to enter the cli container to execute relevant commands.
3.6 creating channels
Channel is a feature of Fabric, which can realize business isolation and multi chain operation logically on a blockchain platform.
To create a channel, first enter the cli container and execute the following command:
#Connect to cli service docker exec -it cli bash #Define CA file path ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem #Create 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/
3.7 peer node joining channel
In the previous step, we created a channel named mychannel, but there is no peer node in the channel, so the transaction operation cannot be performed. In this section, we add 4 nodes of 2 organizations to the mychannel channel.
By viewing the environment variables, the configuration of org1.peer0 used by the current cli container.
![https://imgchr.com/i/NtsWvR]()
According to the current configuration, we execute the following command, first add the org1.peer0 node to the mychannel channel channel.
peer channel join -b channel-artifacts/mychannel.block
The successful execution result is shown in the following figure:
![https://imgchr.com/i/NtsR29]()
Switch the node, and add org1.peer1 to the mychannel channel channel.
#Switch node org1.peer1 source scripts/utils.sh setGlobals 1 1 # Add the current node to the channel peer channel join -b channel-artifacts/mychannel.block
Referring to the above method of switching nodes, two nodes of org2 organization are also added to the channel.
#Switch node 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 node org2.peer1 setGlobals 1 2 # Add the current node to the channel peer channel join -b channel-artifacts/mychannel.block
So far, four nodes of the two organizations have joined the mychannel channel channel.
3.8 updating anchor nodes
Anchor node is a special peer node in an organization, through which communication between nodes in different organizations can be realized. Generally, an anchor node is set for each organization. The following two organizations are respectively set as anchor nodes: org1 and org2. This paper assumes that the peer0 node in the organization is set as anchor node.
#Switch to org1.peer0 node setGlobals 0 1 #Set the environment variable order_ CA ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem #Update settings anchor node peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls true --cafile $ORDERER_CA #Switch to org2.peer0 node setGlobals 0 2 #Set the environment variable order_ CA ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem #Update settings anchor node peer channel update -o orderer.example.com:7050 -c mychannel -f ./channel-artifacts/Org2MSPanchors.tx --tls true --cafile $ORDERER_CA
The successful execution result of the update command is shown in the following figure:
![https://imgchr.com/i/Nts4Dx]()
After our efforts, we manually configured and started a fabric network, including 3 orderer nodes, 2 organizations, 4 peer nodes, 2 ca nodes, 1 cli node, and 1 channel mychannel. In the following articles, we will conduct chain code operation based on the network.
Author: xiaohui249
Link to this article: http://javatech.wang/index.ph...
The author and original source must be indicated in the form of a link when the version is reproduced