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

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)
}
主站蜘蛛池模板: 喀什市| 项城市| 滨州市| 镇巴县| 乐都县| 布尔津县| 遵化市| 巧家县| 吕梁市| 百色市| 河北区| 左权县| 彰化县| 固安县| 牙克石市| 美姑县| 淅川县| 南和县| 沁阳市| 灵寿县| 吉木萨尔县| 七台河市| 义马市| 东阿县| 密云县| 黄陵县| 台南县| 伊金霍洛旗| 桦川县| 淮滨县| 阳新县| 徐闻县| 教育| 湖州市| 民勤县| 蒙自县| 昆明市| 肇州县| 额尔古纳市| 东山县| 盐亭县|