- 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)
}
推薦閱讀
- 卓越的課件如何做(數(shù)學(xué)篇)
- 西去東來(lái):沿絲綢之路數(shù)學(xué)知識(shí)的傳播與交流
- 仿真模型可移植性規(guī)范及其應(yīng)用
- 神奇的數(shù)學(xué)(二)
- 耀世數(shù)學(xué)明珠
- 數(shù)學(xué)建模:算法與編程實(shí)現(xiàn)
- 救命的數(shù)學(xué)
- 數(shù)學(xué)的力量
- 高等數(shù)學(xué)(下冊(cè))
- 排序問(wèn)題的數(shù)學(xué)規(guī)劃松弛方法
- 無(wú)言的宇宙
- 概率論與數(shù)理統(tǒng)計(jì)
- 美妙的數(shù)學(xué)(插圖珍藏版)
- 人大附小的課堂四聲(人大附小七彩教育成果叢書)
- 午餐時(shí)間聊數(shù)學(xué)