官术网_书友最值得收藏!

How to do it...

We will implement our school IT-asset management system using chaincode, and define the Asset object, and the Init, Invoke, and query functions. To do this, follow these steps:

  1. Since we will use Go to write chaincode, install it in Unix (Ubuntu). Make sure Go version 1.10.x is installed. If you haven't yet installed Go, run the following command:
wget https://dl.google.com/go/go1.11.4.linux-amd64.tar.gz
sudo tar -zxvf go1.11.4.linux-amd64.tar.gz -C /usr/local/
  1. Create a local folder called itasset and navigate to that folder:
mkdir ~/itasset && cd ~/itasset
  1. To set up the PATH variable for Go, enter the following command:
ubuntu@ip-172-31-0-111:~$ export GOPATH=/home/ubuntu/itasset/
ubuntu@ip-172-31-0-111:~$ export PATH=/usr/local/go/bin:$GOPATH/bin/:$PATH
ubuntu@ip-172-31-0-111:~$ cd /home/ubuntu/itasset/
ubuntu@ip-172-31-0-111:~/itasset$ mkdir -p $GOPATH/src/assetmgr
ubuntu@ip-172-31-0-111:~/itasset$ cd $GOPATH/src/assetmgr
  1. Create the chaincode source file, assetmgr.go, for writing IT asset management:
touch assetmgr.go
  1. Our assetmgr chaincode needs to implement the Chaincode interface and the business functions for IT asset management. As we discussed in the previous section, we will implement three chaincode functions in blockchain, shown as follows:
Order: function called by school administer to order a device from OEM
Ship: function called by OEM to transport the device to school
Distribute: function called by School to distribute the device to students.

Once the student receives the device, the asset management process is completed. We will keep track of the device's asset information, so we also need to define the device with related tracking information in the chaincode.

  1. Based on our chaincode implementation analysis, let's define the skeleton of the AssetMgr chaincode. Define the import section:
package main
import (
"encoding/json"
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)
type AssetMgr struct {
}
  1. Define the asset:
//define organization asset information, the record can be trace in bloackchain
type OrgAsset struct {
}
  1. Define the Init and Invoke methods:
func (c *AssetMgr) Init(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Success(nil)
}
func (c *AssetMgr) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Error("Invalid function name")
}
func (c *AssetMgr) Order(stub shim.ChaincodeStubInterface, args []string) pb.Response {
}
func (c *AssetMgr) Ship(stub shim.ChaincodeStubInterface, args []string) pb.Response {
}
func (c *AssetMgr) Distribute(stub shim.ChaincodeStubInterface, args []string) pb.Response {
}
  1. Define the chaincode's main function:
func main() {
err := shim.Start(new(AssetMgr))
if err != nil {
fmt.Printf("Error creating new AssetMgr Contract: %s", err)
}
}

We have now defined our AssetMgr skeleton. Next, we need to implement all of these unimplemented functions in our chaincode. We will start by defining the OrgAsset entity.

主站蜘蛛池模板: 麻栗坡县| 沾益县| 潜山县| 江安县| 双城市| 资源县| 东丰县| 通山县| 鹿泉市| 榆树市| 北辰区| 古田县| 枞阳县| 长垣县| 巴中市| 大英县| 漾濞| 定襄县| 建湖县| 彭州市| 西安市| 甘孜| 新源县| 出国| 香河县| 清水河县| 习水县| 鄂尔多斯市| 无棣县| 泸州市| 上饶县| 丰都县| 郸城县| 花莲县| 福泉市| 彭水| 茂名市| 如东县| 武邑县| 汝州市| 库车县|