上QQ阅读APP看书,第一时间看更新
The Init function
The implementation of our Init function is as follows:
func (c *AssetMgr) Init(stub shim.ChaincodeStubInterface) pb.Response {
args := stub.GetStringArgs()
if len(args) != 3 {
return shim.Error("Incorrect arguments. Expecting a key and a value")
}
assetId := args[0]
assetType := args[1]
deviceId := args[2]
//create asset
assetData := OrgAsset{
Id: assetId,
AssetType: assetType,
Status: "START",
Location: "N/A",
DeviceId: deviceId,
Comment: "Initialized asset",
From: "N/A",
To: "N/A"}
assetBytes, _ := json.Marshal(assetData)
assetErr := stub.PutState(assetId, assetBytes)
if assetErr != nil {
return shim.Error(fmt.Sprintf("Failed to create asset: %s", args[0]))
}
return shim.Success(nil)
}
func (c *AssetMgr) Init(stub shim.ChaincodeStubInterface) pb.Response {
args := stub.GetStringArgs()
assetId := args[0] assetType := args[1] deviceId := args[2]
//create asset
assetData := OrgAsset{Id: assetId,AssetType:
assetType, Status: "START",Location: "N/A",DeviceId:
deviceId,Comment: "Initialized asset",From: "N/A", To: "N/A"}
assetBytes, _ := json.Marshal(assetData)
assetErr := stub.PutState(assetId, assetBytes)
…
return shim.Success(nil)
}