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

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.

主站蜘蛛池模板: 广东省| 雅安市| 兰州市| 阿勒泰市| 兴仁县| 淮南市| 宜良县| 黎城县| 安福县| 恭城| 华蓥市| 金平| 高青县| 丰宁| 新乡县| 运城市| 罗平县| 洛浦县| 虎林市| 东至县| 蒙自县| 广南县| 定边县| 崇义县| 聊城市| 佳木斯市| 濉溪县| 吉林市| 邮箱| 介休市| 柞水县| 江陵县| 谷城县| 徐州市| 博野县| 桃园市| 叙永县| 永春县| 聂拉木县| 喀喇| 永丰县|