我使用的是Hyperledger frabic 1.0.5,并且我尝试更新以下包含片段的车辆结构。当我尝试使用stub.PutState()将新日志添加到切片时,当我调用GetState()时它不会显示。
这是我正在使用的车辆和日志结构...
type vehicle struct {
ObjectType string `json:"objType"` //docType is used to distinguish the various types of objects in state database
VIN string `json:"vin"` // modified ChassisNumber to VIN // the fieldtags are needed to keep case from bouncing around
Make string `json:"make"` // modified Manufacturer to Make
Model string `json:"model"`
Year int `json:"year"`
AssemblyDate int `json:"assemblyDate,omitempty"`
AirbagSerialNumber string `json:"airbagSerialNumber,omitempty"`
Owner string `json:"owner"`
Origin string `json:"origin"` // document origin of the vehicle
Mileage int `json:"mileage"`
Recall bool `json:"recall,omitempty"` // to be added at workshop
RecallDate int `json:"recallDate,omitempty"` // to be added at workshop
Timestamp int `json:"timestamp"`
MaintenanceLogs []log `json:"logs,omitempty"`
}
type log struct {
Timestamp int `json:"timestamp,omitempty"`
Description string `json:"description,omitempty"`
}
以下是添加新日志的功能。
func (t *AutoTraceChaincode) addMaintenanceLog(stub shim.ChaincodeStubInterface, args []string) peer.Response {
// 0 1 2
// VIN timestamp description
if len(args) != 3 {
return shim.Error("Incorrect number of arguements. Expecting 3")
}
vin := args[0]
timestamp, err := strconv.Atoi(args[1])
if err != nil {
return shim.Error("timestamp need to be a integer string")
}
description := args[2]
vehicleAsBytes, err := stub.GetState(vin)
if err != nil {
return shim.Error("Failed to get vehicle")
} else if vehicleAsBytes == nil {
return shim.Error("Vehicle does not exist")
}
updateVehicle := vehicle{}
err = json.Unmarshal(vehicleAsBytes, &updateVehicle)
if err != nil {
return shim.Error("Unable to convert vehicle to json")
}
newLog := log{
Timestamp: timestamp,
Description: description,
}
fmt.Println("newLog: ", newLog)
updateVehicle.MaintenanceLogs = append(updateVehicle.MaintenanceLogs, newLog)
fmt.Println("updateVehicle: ", updateVehicle)
vehicleJSONBytes, err := json.Marshal(updateVehicle)
if err != nil {
return shim.Error("unable to marshall struct to bytes")
}
fmt.Println("vin: ", vin)
fmt.Println("new vehicle as json bytes", string(vehicleJSONBytes))
err = stub.PutState(vin, vehicleJSONBytes)
if err != nil {
return shim.Error("unable to put log into ledger")
}
vehicleAsBytes, _ = stub.GetState(vin)
fmt.Println("new vehicle", string(vehicleAsBytes))
return shim.Success(nil)
}
和以下日志显示了当我尝试stub.PutState()和stub.GetState()时我正在获取的东西
2018-07-30 15:09:46.295 UTC [mfg.com-peer0.mfg.com-dmvcc-v1] func2 -> INFO 05f invoke is running addMaintenanceLog
2018-07-30 15:09:46.296 UTC [mfg.com-peer0.mfg.com-dmvcc-v1] func2 -> INFO 060 newLog: {12345 add repair note}
2018-07-30 15:09:46.296 UTC [mfg.com-peer0.mfg.com-dmvcc-v1] func2 -> INFO 061 updateVehicle: {vehicle mer1000001 mercedes c class 2018 0 dealer a usa 15 false 0 12345 [{12345 creation} {12345 add repair note}]}
2018-07-30 15:09:46.296 UTC [mfg.com-peer0.mfg.com-dmvcc-v1] func2 -> INFO 062 vin: mer1000001
2018-07-30 15:09:46.296 UTC [mfg.com-peer0.mfg.com-dmvcc-v1] func2 -> INFO 063 new vehicle as json bytes {"objType":"vehicle","vin":"mer1000001","make":"mercedes","model":"c class","year":2018,"owner":"dealer a","origin":"usa","mileage":15,"timestamp":12345,"logs":[{"timestamp":12345,"description":"creation"},{"timestamp":12345,"description":"add repair note"}]}
2018-07-30 15:09:46.296 UTC [mfg.com-peer0.mfg.com-dmvcc-v1] func2 -> INFO 064 new vehicle {"objType":"vehicle","vin":"mer1000001","make":"mercedes","model":"c class","year":2018,"owner":"dealer a","origin":"usa","mileage":15,"timestamp":12345,"logs":[{"timestamp":12345,"description":"creation"}]}