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

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

The Invoke function

The implementation of the Invoke function is as follows:

func (c *AssetMgr) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
function, args := stub.GetFunctionAndParameters()
if function == "Order" {
return c.Order(stub, args)
} else if function == "Ship" {
return c.Ship(stub, args)
} else if function == "Distribute" {
return c.Distribute(stub, args)
} else if function == "query" {
return c.query(stub, args)
} else if function == "getHistory" {
return c.getHistory(stub, args)
}
return shim.Error("Invalid function name")
}

The Order, Ship, and Distribute functions will be quite similar. These will update the ledger state. We will use order() as an example to show how we implement the chaincode function:

func (c *AssetMgr) Order(stub shim.ChaincodeStubInterface, args []string) pb.Response {
return c.UpdateAsset(stub, args, "ORDER", "SCHOOL", "OEM")
}

Here is the UpdateAsset function:

func (c *AssetMgr) UpdateAsset(stub shim.ChaincodeStubInterface, args []string, currentStatus string, from string, to string) pb.Response {
assetId := args[0] comment := args[1] location := args[2]
assetBytes, err := stub.GetState(assetId)
orgAsset := OrgAsset{}

if currentStatus == "ORDER" && orgAsset.Status != "START" {
return shim.Error(err.Error())
} else if currentStatus == "SHIP" && orgAsset.Status != "ORDER" {.}
else if currentStatus == "DISTRIBUTE" && orgAsset.Status != "SHIP" {.}
orgAsset.Comment = comment
orgAsset.Status = currentStatus
….
orgAsset0, _ := json.Marshal(orgAsset)
err = stub.PutState(assetId, orgAsset0)

return shim.Success(orgAsset0)
}
主站蜘蛛池模板: 赣州市| 柯坪县| 浦城县| 武强县| 和林格尔县| 阿拉善右旗| 蕉岭县| 黄陵县| 桓台县| 宣汉县| 拜城县| 广东省| 张家界市| 涞源县| 南投市| 克拉玛依市| 利辛县| 射阳县| 建德市| 南平市| 绵竹市| 青冈县| 化隆| 且末县| 定兴县| 云龙县| 措勤县| 容城县| 屯留县| 张家口市| 玉树县| 宜春市| 浙江省| 如东县| 盘山县| 衡山县| 新和县| 盐源县| 渝中区| 达州市| 安化县|