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

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.

主站蜘蛛池模板: 雷州市| 江达县| 加查县| 邵东县| 马边| 陇西县| 桂东县| 华池县| 久治县| 深圳市| 博兴县| 西贡区| 三台县| 雷州市| 兴隆县| 大渡口区| 诏安县| 余庆县| 临江市| 南澳县| 永春县| 玉门市| 秦皇岛市| 罗田县| 泽库县| 天津市| 广丰县| 肥城市| 五大连池市| 通海县| 平邑县| 金乡县| 会昌县| 宜城市| 化隆| 铜山县| 萍乡市| 霞浦县| 壶关县| 环江| 南雄市|