Familiar with fabric and check test network

Posted by g0liatH on Thu, 03 Mar 2022 11:36:49 +0100

First at ~ / go / SRC / GitHub Compile order and peer under COM / hyperledger / fabric

Compile Orderer

$ cd $GOPATH/src/github.com/hyperledger/fabric/

$ make orderer

After the command is executed, the terminal output is as follows:

Compile peer

$ make peer

After the command is executed, the terminal output is as follows:

Compile and generate related tools

In addition to "Orderer" and "Peer", Hyperledger Fabric also provides us with a series of auxiliary tools needed to build the network environment:

  • configtxgen: a tool for generating initial block and channel transaction configuration files
  • cryptogen: a tool for generating organizational structure and corresponding identity files
  • configtxlator: converts the specified file between binary format and JSON format

To compile and generate these tools, you can also use {make}:

$ make configtxgen 
$ make cryptogen 
$ make configtxlator 

 

see. build/bin directory

$ ll build/bin/

Test fabric samples / test network

First, let's start this test example

cd fabric-samples/test-network

Then remove all containers in the previous run

sudo ./network.sh down

Then reopen the container

sudo ./network.sh up

You can check whether it has been opened through the command:

sudo docker ps -a

We can take a closer look at the results obtained after the above command:

Create channel

Channel is a private layer for communication between specific network members. Only members invited to join the channel can use it. It is invisible to other members in the network. Each channel has a separate blockchain ledger. Peers invited to join the channel can store the ledger of the channel and then verify the transactions of the channel.

Run the following command to create a Channel connection named mychannel, Org1 and Org2.

sudo ./network.sh createChannel

Add a permission for the user

sudo chmod -R 777 ~/go

Due to the need to add permissions to opt users at the same time

sudo chmod -R 777 /opt

We also need to set up the go environment

go env -w GO111MODULE=on
go env -w GOPROXY=https://goproxy.cn,direc

In this way, the chain code can be deployed. Note that sudo cannot be used here

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

If this happens, the chain code deployment is successful

xzd@xzd-virtual-machine:~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network$ ./network.sh deployCC -ccn basic -ccp ../asset-transfer-basic/chaincode-go -ccl go
deploying chaincode on channel 'mychannel'
executing with the following
- CHANNEL_NAME: mychannel
- CC_NAME: basic
- CC_SRC_PATH: ../asset-transfer-basic/chaincode-go
- CC_SRC_LANGUAGE: go
- CC_VERSION: 1.0
- CC_SEQUENCE: 1
- CC_END_POLICY: NA
- CC_COLL_CONFIG: NA
- CC_INIT_FCN: NA
- DELAY: 3
- MAX_RETRY: 5
- VERBOSE: false
Vendoring Go dependencies at ../asset-transfer-basic/chaincode-go
~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/asset-transfer-basic/chaincode-go ~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network
go: downloading github.com/hyperledger/fabric-chaincode-go v0.0.0-20210718160520-38d29fabecb9
go: downloading github.com/hyperledger/fabric-protos-go v0.0.0-20201028172056-a3136dde2354
go: downloading github.com/golang/protobuf v1.3.2
go: downloading github.com/hyperledger/fabric-contract-api-go v1.1.1
go: downloading github.com/stretchr/testify v1.5.1
go: downloading google.golang.org/grpc v1.23.0
go: downloading google.golang.org/genproto v0.0.0-20180831171423-11092d34479b
go: downloading golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297
go: downloading github.com/davecgh/go-spew v1.1.1
go: downloading github.com/pmezard/go-difflib v1.0.0
go: downloading gopkg.in/yaml.v2 v2.2.8
go: downloading github.com/xeipuuv/gojsonschema v1.2.0
go: downloading github.com/go-openapi/spec v0.19.4
go: downloading github.com/gobuffalo/packr v1.30.1
go: downloading golang.org/x/sys v0.0.0-20190710143415-6ec70d6a5542
go: downloading github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415
go: downloading github.com/gobuffalo/envy v1.7.0
go: downloading github.com/gobuffalo/packd v0.3.0
go: downloading github.com/go-openapi/jsonpointer v0.19.3
go: downloading github.com/go-openapi/jsonreference v0.19.2
go: downloading github.com/go-openapi/swag v0.19.5
go: downloading github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f
go: downloading github.com/joho/godotenv v1.3.0
go: downloading github.com/rogpeppe/go-internal v1.3.0
go: downloading github.com/PuerkitoBio/purell v1.1.1
go: downloading github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e
go: downloading golang.org/x/text v0.3.2
go: downloading github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578
~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network
Finished vendoring Go dependencies
+ peer lifecycle chaincode package basic.tar.gz --path ../asset-transfer-basic/chaincode-go --lang golang --label basic_1.0
+ res=0
Chaincode is packaged
Installing chaincode on peer0.org1...
Using organization 1
+ peer lifecycle chaincode install basic.tar.gz
+ res=0
2022-03-03 16:32:34.512 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nJbasic_1.0:3cfcf67978d6b3f7c5e0375660c995b21db19c4330946079afc3925ad7306881\022\tbasic_1.0" > 
2022-03-03 16:32:34.513 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: basic_1.0:3cfcf67978d6b3f7c5e0375660c995b21db19c4330946079afc3925ad7306881
Chaincode is installed on peer0.org1
Install chaincode on peer0.org2...
Using organization 2
+ peer lifecycle chaincode install basic.tar.gz
+ res=0
2022-03-03 16:32:44.528 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 001 Installed remotely: response:<status:200 payload:"\nJbasic_1.0:3cfcf67978d6b3f7c5e0375660c995b21db19c4330946079afc3925ad7306881\022\tbasic_1.0" > 
2022-03-03 16:32:44.528 CST [cli.lifecycle.chaincode] submitInstallProposal -> INFO 002 Chaincode code package identifier: basic_1.0:3cfcf67978d6b3f7c5e0375660c995b21db19c4330946079afc3925ad7306881
Chaincode is installed on peer0.org2
Using organization 1
+ peer lifecycle chaincode queryinstalled
+ res=0
Installed chaincodes on peer:
Package ID: basic_1.0:3cfcf67978d6b3f7c5e0375660c995b21db19c4330946079afc3925ad7306881, Label: basic_1.0
Query installed successful on peer0.org1 on channel
Using organization 1
+ peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile /home/xzd/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --channelID mychannel --name basic --version 1.0 --package-id basic_1.0:3cfcf67978d6b3f7c5e0375660c995b21db19c4330946079afc3925ad7306881 --sequence 1
+ res=0
2022-03-03 16:32:46.672 CST [chaincodeCmd] ClientWait -> INFO 001 txid [a4167851e17f523141c67776962bcee4d8b625dcc5deb5bbf0feafac0720edfc] committed with status (VALID) at localhost:7051
Chaincode definition approved on peer0.org1 on channel 'mychannel'
Using organization 1
Checking the commit readiness of the chaincode definition on peer0.org1 on channel 'mychannel'...
Attempting to check the commit readiness of the chaincode definition on peer0.org1, Retry after 3 seconds.
+ peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --version 1.0 --sequence 1 --output json
+ res=0
{
	"approvals": {
		"Org1MSP": true,
		"Org2MSP": false
	}
}
Checking the commit readiness of the chaincode definition successful on peer0.org1 on channel 'mychannel'
Using organization 2
Checking the commit readiness of the chaincode definition on peer0.org2 on channel 'mychannel'...
Attempting to check the commit readiness of the chaincode definition on peer0.org2, Retry after 3 seconds.
+ peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --version 1.0 --sequence 1 --output json
+ res=0
{
	"approvals": {
		"Org1MSP": true,
		"Org2MSP": false
	}
}
Checking the commit readiness of the chaincode definition successful on peer0.org2 on channel 'mychannel'
Using organization 2
+ peer lifecycle chaincode approveformyorg -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile /home/xzd/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --channelID mychannel --name basic --version 1.0 --package-id basic_1.0:3cfcf67978d6b3f7c5e0375660c995b21db19c4330946079afc3925ad7306881 --sequence 1
+ res=0
2022-03-03 16:32:54.879 CST [chaincodeCmd] ClientWait -> INFO 001 txid [1dad07c2c2e1d4e7023af4e6519072dfee52888116fe18f302292e88e9b9d256] committed with status (VALID) at localhost:9051
Chaincode definition approved on peer0.org2 on channel 'mychannel'
Using organization 1
Checking the commit readiness of the chaincode definition on peer0.org1 on channel 'mychannel'...
Attempting to check the commit readiness of the chaincode definition on peer0.org1, Retry after 3 seconds.
+ peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --version 1.0 --sequence 1 --output json
+ res=0
{
	"approvals": {
		"Org1MSP": true,
		"Org2MSP": true
	}
}
Checking the commit readiness of the chaincode definition successful on peer0.org1 on channel 'mychannel'
Using organization 2
Checking the commit readiness of the chaincode definition on peer0.org2 on channel 'mychannel'...
Attempting to check the commit readiness of the chaincode definition on peer0.org2, Retry after 3 seconds.
+ peer lifecycle chaincode checkcommitreadiness --channelID mychannel --name basic --version 1.0 --sequence 1 --output json
+ res=0
{
	"approvals": {
		"Org1MSP": true,
		"Org2MSP": true
	}
}
Checking the commit readiness of the chaincode definition successful on peer0.org2 on channel 'mychannel'
Using organization 1
Using organization 2
+ peer lifecycle chaincode commit -o localhost:7050 --ordererTLSHostnameOverride orderer.example.com --tls --cafile /home/xzd/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem --channelID mychannel --name basic --peerAddresses localhost:7051 --tlsRootCertFiles /home/xzd/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt --peerAddresses localhost:9051 --tlsRootCertFiles /home/xzd/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/test-network/organizations/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt --version 1.0 --sequence 1
+ res=0
2022-03-03 16:33:03.265 CST [chaincodeCmd] ClientWait -> INFO 001 txid [32a36a1aedd6e72465db7f8fcbc657e63049e21070e5a74eb508716882ac4fad] committed with status (VALID) at localhost:7051
2022-03-03 16:33:03.339 CST [chaincodeCmd] ClientWait -> INFO 002 txid [32a36a1aedd6e72465db7f8fcbc657e63049e21070e5a74eb508716882ac4fad] committed with status (VALID) at localhost:9051
Chaincode definition committed on channel 'mychannel'
Using organization 1
Querying chaincode definition on peer0.org1 on channel 'mychannel'...
Attempting to Query committed status on peer0.org1, Retry after 3 seconds.
+ peer lifecycle chaincode querycommitted --channelID mychannel --name basic
+ res=0
Committed chaincode definition for chaincode 'basic' on channel 'mychannel':
Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
Query chaincode definition successful on peer0.org1 on channel 'mychannel'
Using organization 2
Querying chaincode definition on peer0.org2 on channel 'mychannel'...
Attempting to Query committed status on peer0.org2, Retry after 3 seconds.
+ peer lifecycle chaincode querycommitted --channelID mychannel --name basic
+ res=0
Committed chaincode definition for chaincode 'basic' on channel 'mychannel':
Version: 1.0, Sequence: 1, Endorsement Plugin: escc, Validation Plugin: vscc, Approvals: [Org1MSP: true, Org2MSP: true]
Query chaincode definition successful on peer0.org2 on channel 'mychannel'
Chaincode initialization is not required

Interact with the network:

After the test network is successfully started, you can use the peer command of the command line to interact with the network. It allows users to call the deployed smart contract, update the channel, or install and deploy a new smart contract.

First of all, you need to set it. There are binary files such as peer in the bin directory and several yaml files in the config directory. If necessary, you can use the following command to add peer to the environment variable:

export PATH=<path to download location>/bin:$PATH

My address is:

export PATH=~/go/src/github.com/hyperledger/fabric/scripts/fabric-samples/bin:$PATH

After setting the environment variable, go to the test network folder

First, make sure you configure temporary variables in test network:

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

In addition, you need to configure the directory where config is located.

export FABRIC_CFG_PATH=$PWD/../config/

Setting the following environment variables allows you to operate the peer command 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

You can also operate the peer command as Org2. Select any one! I choose org1 here, which will be used later in this paragraph. I don't need to add it for the time being!!

# 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

 CORE_PEER_TLS_ROOTCERT_FILE and core_ PEER_ The two lines mspconfigpath indicate the information used for Org1's encryption.

You can use the following command to view the list of vehicles in the account book of channel, which was added when using the deployCC parameter just now.

Check the fabcar chain code:

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

Call fabcar 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 \
--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 mychannel \
-n fabcar \
-c '{"function":"InitLedger","Args":[]}'

The words "Chaincode invoke successful. result: status:200" appear. The update is successful!

Then we use org2 to execute the peer command to query:

# 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

Then you can use the following command to view the status of CAR9 in the ledger.

peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryCar","CAR9"]}'

You can see that the vehicle has been transferred to Dave, that is, the update just now has been successful.

Other operations (reprint not attempted)

Create a new channel
// Generate channel profile
$ configtxgen -profile TwoOrgsChannel -outputCreateChannelTx ./channel-artifacts/testchannel.tx -channelID testchannel  -configPath ./configtx/

// Generate the anchor node configuration file of org1
$ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID testchannel -asOrg Org1MSP -configPath ./configtx/

// Generate the anchor node configuration file of org2
$ configtxgen -profile TwoOrgsChannel -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID testchannel -asOrg Org2MSP -configPath ./configtx/

// Create channel
$ peer channel create -o localhost:7050 -c testchannel --ordererTLSHostnameOverride orderer.example.com \
-f ./channel-artifacts/testchannel.tx \
--outputBlock ./channel-artifacts/testchannel.block \
--tls --cafile /home/verayy/codes/go/src/github.com/hyperledger/fabric-samples/test-network/organizations/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

// For each org join channel, you need to switch the above configuration file
$ peer channel join -b ./channel-artifacts/testchannel.block

// Update the anchor node configuration with the administrator identity of each org
$ peer channel update -o localhost:7050 --ordererTLSHostnameOverride orderer.e

--------
Copyright notice: This article is CSDN Blogger「shuizhongmose」Original articles, follow CC 4.0 BY-SA Copyright agreement, please attach the original source link and this statement for reprint.
Original link: https://blog.csdn.net/shuizhongmose/article/details/109056691

Shut down the network

After using the test network, you can use the following command to close the test network.

./network.sh down

This command will stop and remove the container of the node and chaincode, delete the information used for encryption, and remove the things generated by the previous operation such as channel and docker volume. However, if necessary, you can use the up parameter to run the network again.

Detailed explanation of startup process: (Reprint)

  • ./network.sh creates certificates and keys for two peer nodes and one order node. By default, the script uses the encryption tool under the organizations/cryptogen folder.
  • The script uses the configtxgen tool to create the creation block of the system, which uses configtx / configtx Yaml file to create the creation block and store it in the system Genesis block folder.
  • When the above two steps are completed/ network.sh will start the network, and the script uses docker-compose-test-net under the docker folder Yaml files create peer and order nodes.
  • If the createChannel subcommand is used, the script will also run createChannel under the script folder SH script to create the required channel. The script will use the peer command to create the channel and join the two organizations.
  • If the deployCC command is run, the script will run deployCC under script on all peers SH script to install fabcar chaincode. After the definition of chaincode is submitted to the channel, the peer command will call the init function to initialize the chaincode and put the required data into the chaincode.

         

Topics: Operation & Maintenance fabric