Fabric smart contract experiment - student information management system (Windows system)

1. Experimental objectives

Complete the process of fabric test network construction, chain code development, debugging, deployment and invocation through one host. This experimental project is a student information management system (part), which requires chaincode to provide student information management functions: including adding new student information, modifying student information, querying student information, etc.

Limited operating requirements:

1. Use the test-network example under the fabric-samples package to build a local test chaincode

2. Use the binary tools in the bin folder to interact with the chaincode

2. Experimental prerequisites

1. Docker has been installed locally and has successfully pulled Fabric-related images

2. Binary tools have been configured into system variables and can be used directly through the command line

3. Successfully use git to clone fabric-samples to the corresponding directory of the local GOPATH

4. The configuration of system variables related to "Start Fabric Test Network" has been completed (used to indicate the identity of the current operator)

3. Create a working directory

Create relevant folders in the GOPATH directory to store the necessary materials for this experiment

The directory path I created here is: $GOPATH/src/chaincode/student-go

Then copy the test-network, config, and bin folders under the fabric-sample package to the student-go directory (for testing the network)

Finally, create a chaincode folder under the student-go folder to store the chaincode

4. Write chain code

1. Create a chaincode file : Create a student.go file in the chiancode directory. All the chaincodes in this experiment are written in this file; because the chaincode deployment needs to be modularized, the chaincode directory must be initialized modularly (when go.mod is generated in the chaincode source code directory, indicating that the modular initialization has been completed, and the go.sum file will appear when the third-party library is referenced)

 

 2. The pull of the third-party library may fail due to network speed or failure to set GO111MODULE="auto", etc. You can try multiple times

go get github.com/hyperledger/fabric-chaincode-go
go get github.com/hyperledger/fabric-protos-go

3. Smart contract design ideas

Because this is a student information management system, the necessary data is student information, so it can be defined with a structure. In addition, operations such as adding, deleting, modifying and checking are provided for the structure. Note: Fabric smart contracts require a structure to inherit related objects in order to have the ability to execute in the Fabric network

4. Start coding

(1) The definition of the current package and the reference of the third-party package

 (2) Define the structure: This project will define two structures, the first is the structure that inherits the interface of the Fabric smart contract object, and the other is the student information structure

(3) Define and check whether the student ID is duplicated: From a logical understanding, student information with duplicate IDs is not allowed to be entered again, so we need to define this tool method for use. Note: This method is bound in the smart contract object (StudentContract structure), because the smart contract must be provided to allow external calls, and the StudentContract structure is an object that inherits the Hyperledger smart contract interface and has the ability to be called externally )

(3) Define the method of adding student information: the method of adding student information is also bound in the smart contract object, and it is necessary to check whether the student ID is duplicated before adding information

 (4) Define the method of reading student information: read student information according to the student ID, if the information does not exist, a corresponding reminder needs to be given

 (5) Define the method of updating student information: This method is actually a full coverage method, that is, all fields of the student are transmitted for each update

(6) Define the method to obtain all student information

(7) Define the main method: the main method can start the chain code here (it can be automatically executed after being deployed on the fabric network, waiting to be called)

 5. Deploy the chaincode

1. You need to start the test network before deploying the chain code, so you must first enter the test-network directory in the terminal

 2. Start the test network

Start the test network through the network.sh script (close the test and clear the relevant data: ./network.sh down):

./network.sh up

3. Create Channel

The main function of the channel created here is to enable communication between two organization nodes. The name of the channel created here is the default name: mychannel

./network.sh creatChannel

4. Deploy the chaincode

Also deploy the chaincode through the network.sh script:

ccn chaincode name ccp chaincode directory ccl chaincode language type

./network.sh deployCC -ccn student -ccp ../chaincode -ccl go

6. Chaincode call

Here you need to call the deployed chaincode through the peer tool. Therefore, it is necessary to ensure that under the current terminal path (test-network), the peer tool can be used directly through the command

1. Call the query method

In this project, there are two query methods, namely reading student information and obtaining all student information.

Call the chaincode method mode through the peer tool:

When the method has empty parameters, put the method name in args (such as querying all student information);

When parameters need to be passed, the method name is placed in function, and the passed parameters are placed in args (such as querying a student's information)

Query all student information (the output information will be empty when no information is added at the beginning):

peer chaincode query -C mychannel -n student -c '{"args":["All"]}'

Query a student's information:

peer chaincode query -C mychannel -n student -c '{"function":"Get","args":["007"]}'

 2. Call the execution method

In this project, as long as it involves data changes, it needs to be called through execution. Such as: adding student information, modifying student information, etc. The execution method needs to identify itself, so the relevant certificate needs to be specified during the call:

 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 student --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":"Create","Args":["001","chenchen","123456789"]}'

Guess you like

Origin blog.csdn.net/weixin_61275790/article/details/126793941