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

  • Hyperledger Cookbook
  • Xun (Brian) Wu Chuanfeng Zhang Andrew Zhang
  • 400字
  • 2021-06-24 14:59:38

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.

主站蜘蛛池模板: 江口县| 抚顺市| 乐东| 玉溪市| 修文县| 正定县| 彰武县| 湘潭县| 长寿区| 扶余县| 南康市| 偃师市| 犍为县| 洪雅县| 炉霍县| 贵溪市| 东乌珠穆沁旗| 汝州市| 涡阳县| 三原县| 齐齐哈尔市| 乐至县| 桂林市| 平远县| 临海市| 女性| 长海县| 罗甸县| 陆河县| 莒南县| 南川市| 华池县| 河北区| 廊坊市| 汾阳市| 潜山县| 锦州市| 尤溪县| 永福县| 睢宁县| 金昌市|