Prepare 8 virtual machines, where Vagrant is used to create Centos7, 1 for the NFS service (where NFS service is used so that certificates, blocks, etc., are not required to be replicated across hosts), and 7 for deploying fabric nodes, as shown below:

The VIRTUAL machine creation configuration file and fabric deployment resources are already packaged, click here to get them. If you already have them, go to that folder and start deploying fabric2.0 Raft networks.

Fabric configuration generation

We need to generate certificate files, genesis blocks, and channel files in the /share directory of the NFS Server.

Start the NFS Server VM, switch to user root, and go to the /share directory.

#Starting a VM
vagrant up nfsserver
#Logging In to a VM
vagrant ssh nfsserver
#### run #### in the virtual machine below
#Switching to user root
sudo su
cd /share
Copy the code

Next, generate the certificate

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

To generate a creation block, create a channel-Artifacts folder.

mkdir channel-artifacts
./bin/configtxgen -profile TwoOrgsOrdererGenesis -outputBlock ./channel-artifacts/genesis.block -channelID system
Copy the code

Generate channels.

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

OK, now that the configuration files for fabric are generated, deploy the Orderer cluster.

Deploy the Orderer cluster

The YAML file for the Orderer node is ready and can be started by docker-compose.

Start orderer’s three virtual machines first

vagrant up orderer0
vagrant up orderer1
vagrant up orderer2
Copy the code

Start the orderer0 node, orderer1 node, and Orderer2 node on the three Orderer VMS respectively.

docker-compose -f docker-compose-orderer-00.yaml up -d
docker-compose -f docker-compose-orderer-01.yaml up -d
docker-compose -f docker-compose-orderer-02.yaml up -d
Copy the code

After the Orderer cluster is deployed, Org1 and Org2 can be deployed

Deploy Org1 and Org2

Start the remaining four VMS: Peer0-org1, peer1-org1, peer0-org2, and peer1-org2.

vagrant up peer0org1
vagrant up peer1org1
vagrant up peer0org2
vagrant up peer1org2
Copy the code

Start the peer0-org1, peer1-org1, peer0-org2, and peer1-org2 nodes on the four VMS.

docker-compose -f docker-compose-peer0-org1.yaml up -d
docker-compose -f docker-compose-peer1-org1.yaml up -d
docker-compose -f docker-compose-peer0-org2.yaml up -d
docker-compose -f docker-compose-peer1-org2.yaml up -d
Copy the code

Join channel

After all nodes are started and running properly, you can use the CLI tool to create channels, add channels, deploy contracts, and invoke contracts.

Create a channel on the host of the peer0-org1 node, and log in to the CLI container.

docker exec -it cli bash
Copy the code

The following contents are executed in the CLI container

To create the channel, use the Orderer node certificate.

ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer0. example.com/msp/tlscacerts/tlsca.example.com-cert.pem peer channel create -o orderer0.example.com:7050 -c mychannel -f ./channel-artifacts/mychannel.tx --outputBlock ./channel-artifacts/mychannel.block --tls --cafile $ORDERER_CACopy the code

Join channel

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

Ok, the channel has been created and peer0-org1 has joined the channel. Now add the other three peer nodes to the channel.

Peer1-org1, peer0-org2, and peer1-org2 run the following command on the three hosts to add them to the channel.

#Enter cli
docker exec -it cli bash
peer channel join -b ./channel-artifacts/mychannel.block
Copy the code

With channels added, contracts can be deployed.

Deployment of contract

The Fabric2.0 chain code life cycle changes significantly and requires the following steps to complete contract deployment:

  • Packaging chain code
  • Install the chain code
  • Organization approved
  • Submit the chain code

The package chain code needs to be executed on each peer node. When the contract is packaged, the dependency package needs to be downloaded by using go Mod Vebdor.

#Cli container
cd chaincode/go/abstore
#Downloading dependent files
go mod vendor
#Back to the original menucd .. /.. /.. /#Packaging chain code
peer lifecycle chaincode package cc.tar.gz --path github.com/hyperledger/fabric/peer/chaincode/go/abstore --lang golang --label mycc
Copy the code

Install the chain code on each peer node.

#Cli container
#Install the chain code
peer lifecycle chaincode install cc.tar.gz
Copy the code

Query chain code installation

#Cli container
#Query the installed chain code to obtain the package_id of the chain code
peer lifecycle chaincode queryinstalled
Copy the code

The current organization approval chain, Fabric2.0 requires more than half of the organizations to approve the chain code by default. There are two organizations in the fabric network deployed in this case, so both organizations need to approve the chain code. You only need to approve the chain code in the CLI container of any peer node in the organization.

The package_id of the chain code is required to approve the chain code. In this deployment network, you only need to run the following commands in peer0-org1 and peer0-org2

peer lifecycle chaincode approveformyorg --tls true --cafile $ORDERER_CA --channelID mychannel -n mycc -v 1 --init-required --package-id mycc:3ffc03cb2be5e15eac954c8c836ca994d5f298901b35087bd12b336caf2142fb --sequence 1 --waitForEvent
Copy the code

Example Query the approval of chain codes

peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name mycc -v 1 --sequence 1 --output json --init-required
Copy the code

When a result like the one shown above occurs, which means that more than half of the organizations have approved, the submission of the chain code can be performed.

To submit the chain code, run the following command in the CLI container of any peer host. Ensure that each approved organization submits the chain code at the same time, that is, all the peers in each approved organization participate in the submission. Peer0.org1.example.com and peer0.org2.example.com are specified during initialization.

peer lifecycle chaincode commit -o orderer0.example.com:7050 --tls true --cafile $ORDERER_CA -C mychannel -n mycc -v 1 --sequence 1 --init-required --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.co m/tls/ca.crt --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.co m/tls/ca.crtCopy the code

View the submitted chain code

peer lifecycle chaincode querycommitted -C mychannel -n mycc
Copy the code

To initialize the command, run the following command in the CLI container of any peer host. Ensure that all approved organizations participate in the initialization, that is, all the peers in each approved organization participate in the initialization. Peer0.org1.example.com and peer0.org2.example.com are specified during initialization.

peer chaincode invoke -o orderer0.example.com:7050 --tls true --cafile $ORDERER_CA -C mychannel -n mycc --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.co m/tls/ca.crt --peerAddresses peer0.org2.example.com:7051 --tlsRootCertFiles /opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.co m/tls/ca.crt --isInit -c '{"Args":["Init","a","100","b","100"]}'Copy the code

Query the value of a, the result shows 100.

peer chaincode query -C mychannel -n mycc -c '{"Args":["query","a"]}'
Copy the code