Fabric 2.2 install, test, and modify the chain code

Please check here for fabric installation, a very complete fabric installation information.

You may encounter bugs, golang environment configuration problems. For the environment variable configuration of golang, please refer to here . The command compiled in this environment is

sudo gedit /etc/profile
or
sudo  vim /etc/profile

The vim exit command is: wq, I was silly at the time, I used: wq!, and the swp file appeared. (The swp file needs to be deleted)

Golang may be troublesome to download dependencies with the go get command because of many wall reasons. So change the mirror

Reference plan click

There may be many permission problems during the installation process, so it is best to add sudo when executing each command

In order to save effort, I changed the file permissions to read, write and executable (777)

Modify the command as sudo chmod -R 777 "filename"

You may encounter curl problems during the installation process. After setting the proxy, you may need to temporarily bypass the proxy. The command that needs to be used is: curl -x ""

curl -x "" http://www.stackoverflow.com

When using sudo apt-get update, a GPG error may be prompted: The following signatures cannot be verified because there is no public key

Solution reference here

After the installation is complete, a test session is required

Test command:

./network.sh up  (启动网络)
./network.sh createChannel(创建通道,默认通道名字为mychannel)
./network.sh deployCC(部署链码,默认是go语言版本)

Refer to official documents here:

When the chain code is executed, the environment variables of the go language may be changed by executing the sudo command (reset to the default environment variables)

The solution is: (Forgot which blog you are on. I found it and released it)

Deployment chain code: fabcar

The example given on the official website is wrong.

Find the fabcar file and execute sudo ./startFabric.sh to initialize. (This is not a console with text-network) for reference

Go back to the text-network console to configure

export PATH=${PWD}/../bin:$PATH
export FABRIC_CFG_PATH=$PWD/../config/
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/[email protected]/msp
export CORE_PEER_ADDRESS=localhost:7051

Test code

运行以下命令以获取已添加到通道账本中的汽车列表:
peer chaincode query -C mychannel -n fabcar -c '{"Args":["queryAllCars"]}'

 Use the following command to call the fabcar chaincode to change the owner of the car on the ledger:

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 fabcar --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":"changeCarOwner","Args":["CAR9","Dave"]}'

There are many commands, please google by yourself (may be an error when executing the chain code, execute sudo chomod 777 text-network/)

Modify the chain code:

Modified in fabcar. Modify the go file under chaincode/fabcar/go.

func (s *SmartContract) Queryhelloworld(ctx contractapi.TransactionContextInterface, carNumber string)(int,error)  {
	message:="hello world"
	fmt.Printf(message)
	return 111111,nil

	//return message,nil
}

After modification, execute sudo ./startFbric.sh

Enter the test-network console. Take action

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

111111 will appear

Indicates success! Good luck! ! ! ! ! ! ! ! ! !

Guess you like

Origin blog.csdn.net/zhuiyunzhugang/article/details/110234358