Fabric test network fabric samples

Posted by debuitls on Mon, 13 Dec 2021 15:51:53 +0100

Fabric 2.2.0

Test the network using Fabirc

1. Environmental preparation

https://hyperledger-fabric.readthedocs.io/zh_CN/release-2.2/install.html

Install docker

curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun

Install docker-compose

curl -L https://github.com/docker/compose/releases/download/1.27.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose

chmod +x /usr/local/bin/docker-compose

docker-compose --version

Go env

wget https://golang.org/dl/go1.17.3.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.17.3.linux-amd64.tar.gz

-- Add environment variable 
vim /etc/profile 
export PATH=$PATH:/usr/local/go/bin

-- Environment variable validation
source /etc/profile

Download the latest release of Fabric samples, docker images, and binaries.

curl -sSL https://bit.ly/2ysbOFE | bash -s -- 2.2.0 1.4.7

Unzip hyperledger-fabric-linux-amd64-2.2 0.tar. gz

Copy the bin config directory to the fabric samples directory

2. Run test network

1. Execute test network script

Execute in the fabric samples / test network directory

 ./network.sh up

Run log

Creating network "net_test" with the default driver
Creating volume "net_orderer.example.com" with default driver
Creating volume "net_peer0.org1.example.com" with default driver
Creating volume "net_peer0.org2.example.com" with default driver
Creating peer0.org1.example.com ... done
Creating orderer.example.com    ... done
Creating peer0.org2.example.com ... done
Creating cli                    ... done
CONTAINER ID   IMAGE                               COMMAND             CREATED         STATUS                  PORTS                                                 NAMES
9e6d0c18e55a   hyperledger/fabric-tools:latest     "/bin/bash"         1 second ago    Up Less than a second                                                         cli
c05cdf8aa405   hyperledger/fabric-peer:latest      "peer node start"   2 seconds ago   Up 1 second             7051/tcp, 0.0.0.0:9051->9051/tcp, :::9051->9051/tcp   peer0.org2.example.com
3378db3f7d05   hyperledger/fabric-peer:latest      "peer node start"   2 seconds ago   Up 1 second             0.0.0.0:7051->7051/tcp, :::7051->7051/tcp             peer0.org1.example.com
56d54fa3e64d   hyperledger/fabric-orderer:latest   "orderer"           2 seconds ago   Up 1 second             0.0.0.0:7050->7050/tcp, :::7050->7050/tcp             orderer.example.com

2. Create channel

Now that the peer and order nodes are running on our machine, we can use scripts to create a Fabric channel for transactions between Org1 and Org2. A channel is a private communication layer between specific network members. Channels can only be used by organizations invited to join the channel and are not visible to other members of the network. Each channel has a separate blockchain ledger. The Invited Organization "joins" its peers in the channel to store the channel ledger and verify transactions on the channel.

You can use network The SH script creates a channel between Org1 and Org2 and adds their peer nodes to the channel. Run the following command to create a channel with the default name "mychannel":

./network.sh createChannel

If the command succeeds, you can see the following message in the log

Channel 'mychannel' joined

You can also use the channel flag to create a channel with a custom name. For example, the following command creates a channel named "channel1":

./network.sh createChannel -c channel1

The channel flag also allows you to create multiple channels by specifying different channel names. After creating "mychannel" or "channel1", you can create a second channel named "channel2" using the following command:

./network.sh createChannel -c channel2

3. Start the chain code on the channel

After creating the channel, you can start to interact with the channel ledger using smart contracts. The smart contract contains business logic for managing assets on the blockchain ledger. Application networks run by members can call smart contracts on the ledger to create, change and transfer these assets. The application also queries through smart contracts to read data on the ledger.

To ensure that transactions are valid, transactions created using smart contracts usually need to be signed by multiple organizations before they can be submitted to the channel ledger. Multiple signatures are an integral part of Fabric trust model. A transaction needs multiple endorsements to prevent a single organization on a channel from tampering with the ledger of its peer node using the business logic that the channel does not agree with. To sign a transaction, each organization needs to invoke and execute a smart contract on its peer node, and then sign the output of the transaction. If the output is consistent and there are enough organizational signatures, the transaction can be submitted to the ledger. This policy, called endorsement policy, specifies the set organization contract on the channel that needs to execute intelligent transaction, and is set as part of the chain code definition for each chain code.

In Fabric, smart contracts are deployed on the network in the form of software packages as chain codes. The chain code is installed on the peer node of the organization, and then deployed to a channel, which can be used to recognize transactions and blockchain ledger interaction. Before deploying the chain code to the channel, the members of the channel need to reach a consensus on the chain code definition and establish chain code governance. When the required number of organizations agree, the chain code definition can be submitted to the channel and the chain code can be used.

Use network SH after creating a channel, you can start the chain code on the channel using the following command:

./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go

The deployCC subcommand will be in peer0 org1. example. COM and peer0 org2. example. Install the asset transfer (basic) chain code on. Com. Then deploy the chain code of the specified channel on the channel using the channel flag (or mychannel if no channel is specified). If you deploy a set of chain code for the first time, the script will install the dependencies of the chain code. By default, the script installs the Go version of asset transfer (Basic) chain code. However, you can use the language note-l to install the chain code of Java or javascript version. You can find the asset transfer (basic) chain code in the asset transfer basic folder of the Fabric samples directory. This directory contains sample chain codes as cases and used to highlight Fabric features

Interact with the network

After you enable the test network, you can use the peer CLI to interact with your network. peer CLI allows you to invoke deployed smart contracts, update channels, or install and deploy new smart contracts.

Ensure that you are operating from the test network directory. If you follow the instructions to install the sample binaries and Docker images, you can find the peer binaries in the bin folder of the fabric samples code library. Add these binaries to your CLI path using the following command:

export PATH=${PWD}/../bin:$PATH

You also need to add fabric samples to the fabric library_ CFG_ Set the path to point to the core Yaml file:

export FABRIC_CFG_PATH=$PWD/../config/

You can now set the environment variables to allow you to operate the peer CLI as Org1:

# Environment variables for Org1

export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org1MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp
export CORE_PEER_ADDRESS=localhost:7051

CORE_PEER_TLS_ROOTCERT_FILE and core_ PEER_ The mspconfigpath environment variable points to the encrypted material in the organizations folder of Org1. If you use/ network.sh deployCC -ccl go install and start the asset transfer (basic) chain code, You can call the initledger method of the chain code (Go) to give some initial assets on the ledger (if you use typescript or javascript, such as. / network.sh deployCC -l javascript, you will call the initledger function of the relevant chain code). Run the following command to initialize the ledger with some assets:

peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"InitLedger","Args":[]}'

If the command succeeds, you will observe output similar to the following:

-> INFO 001 Chaincode invoke successful. result: status:200

Now you can use your CLI tool to query the ledger. Run the following command to get the list of assets added to the channel ledger:

peer chaincode query -C mychannel -n basic -c '{"Args":["GetAllAssets"]}'

If successful, you will see the following output:

[
  {"ID": "asset1", "color": "blue", "size": 5, "owner": "Tomoko", "appraisedValue": 300},
  {"ID": "asset2", "color": "red", "size": 5, "owner": "Brad", "appraisedValue": 400},
  {"ID": "asset3", "color": "green", "size": 10, "owner": "Jin Soo", "appraisedValue": 500},
  {"ID": "asset4", "color": "yellow", "size": 10, "owner": "Max", "appraisedValue": 600},
  {"ID": "asset5", "color": "black", "size": 15, "owner": "Adriana", "appraisedValue": 700},
  {"ID": "asset6", "color": "white", "size": 15, "owner": "Michel", "appraisedValue": 800}
]

When a network member wants to transfer some assets or change some assets in the ledger, the chain code will be called. Use the following instructions to change the asset owner on the ledger by calling the asset transfer (basic) chain code:

peer chaincode invoke -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile ${PWD}/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem -C mychannel -n basic --peerAddresses localhost:7051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles ${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt -c '{"function":"TransferAsset","Args":["asset6","Christopher"]}'

If the command succeeds, you should see the following response:

2019-12-04 17:38:21.048 EST [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200

Because the endorsement strategy of the asset transfer (basic) chain code requires that the transaction be signed by Org1 and Org2 at the same time, the chain code call instruction needs to use the -- peeraddresesses tag to point to peer0 Org1. example. COM and peer0 Org2. example. com. Because the network TLS is enabled, the instruction also needs to point to the TLS certificate of each peer node with the -- tlsRootCertFiles tag.

After calling the chain code, we can use another query to see how the call changes the assets of the blockchain ledger. Because we have queried the peer of Org1, we can run this query chain code through the peer of Org2. Set the following environment variables to operate Org2:

# Environment variables for Org2

export CORE_PEER_TLS_ENABLED=true
export CORE_PEER_LOCALMSPID="Org2MSP"
export CORE_PEER_TLS_ROOTCERT_FILE=${PWD}/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt
export CORE_PEER_MSPCONFIGPATH=${PWD}/organizations/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
export CORE_PEER_ADDRESS=localhost:9051

You can query running in peer0 org2. example. Com asset transfer (basic) chain code:

peer chaincode query -C mychannel -n basic -c '{"Args":["ReadAsset","asset6"]}'

The result shows that "asset6" was transferred to Christopher:

{"ID":"asset6","color":"white","size":15,"owner":"Christopher","appraisedValue":800}

Shut down the network after using the test network, you can use the following command to shut down the network:/ network.sh down this command will stop and delete the node and chain code container, delete the organization encryption material, and remove the chain code image from the Docker Registry. The command also deletes previously run channel items and docker volumes. If you encounter any problems, you are also allowed to run again/ network.sh up

Topics: Blockchain fabric