- 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:
- 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/
- Create a local folder called itasset and navigate to that folder:
mkdir ~/itasset && cd ~/itasset
- 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
- Create the chaincode source file, assetmgr.go, for writing IT asset management:
touch assetmgr.go
- 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.
- 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 {
}
- Define the asset:
//define organization asset information, the record can be trace in bloackchain
type OrgAsset struct {
}
- 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 {
}
- 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.
推薦閱讀
- 數(shù)學(xué)星球:人類文明與數(shù)學(xué)(萬物皆數(shù)學(xué))
- 實(shí)用運(yùn)籌學(xué):案例、方法及應(yīng)用
- 神奇的數(shù)學(xué)(二)
- GMAT批判性推理:邏輯分類精講
- 世界是隨機(jī)的:大數(shù)據(jù)時(shí)代的概率統(tǒng)計(jì)學(xué)
- 數(shù)理邏輯
- 丈量世界:時(shí)間、空間與數(shù)學(xué)(萬物皆數(shù)學(xué))
- 博弈論與信息經(jīng)濟(jì)學(xué):PBL教程
- 迷人的數(shù)學(xué)+美麗的數(shù)學(xué)(共2冊)
- 牛津通識讀本:數(shù)學(xué)(中文版)
- 數(shù)理統(tǒng)計(jì)及其在數(shù)學(xué)建模中的實(shí)踐(使用MATLAB)
- 周髀算經(jīng)
- 復(fù)分析:可視化方法
- ANSYS Workbench機(jī)械工程應(yīng)用精華30例
- 線性代數(shù)及應(yīng)用