Hyperledger Fabric v1.4的私人和公共數據-Coinmonks

//鏈碼
包主

導入(
「位元組」
「編碼/ json」
「 fmt」
「 strconv」
「時間」

「 github.com/hyperledger/fabric/core/chaincode/shim」
pb「 github.com/hyperledger/fabric/protos/peer」

輸入SimpleChaincode struct {
}

輸入命令struct {
ObjectType字元串`json:「 docType」`
Order_raw_material_id字元串`json:「 order_raw_material_id」`
Manufacturer_id字元串`json:「 manufacturer_id」`
Provider_id字元串`json:「 provider_id」`
Material_name字元串`json:「 material_name」`
Order_medicine_id字元串`json:「 order_medicine_id」`
數量字元串`json:「 quantity」`
狀態字元串`json:「 status」`
}

鍵入orderPrivateDetails struct {
價格字元串`json:「 price」`
}

func main(){
錯誤:= shim.Start(new(SimpleChaincode))
如果err= nil {
fmt.Printf(「啟動簡單鏈碼時出錯:%s」,錯誤)
}
}

func(t * SimpleChaincode)Init(stub shim.ChaincodeStubInterface)pb.Response {
返回shim.Success(nil)
}

func(t * SimpleChaincode)調用(存根shim.ChaincodeStubInterface)pb.Response {
函數,args:= stub.GetFunctionAndParameters()
fmt.Println(「調用正在運行」 +函數)

如果函數==「 initOrder」 {
返回t.initOrder(stub,args)
} else if function ==「 changeStatus」 {
返回t.changeStatus(stub,args)
} else if function ==「 changeProvider_id」 {
返回t.changeProvider_id(stub,args)
} else if function ==「 changeQuantity」 {
返回t.changeQuantity(stub,args)
} else if function ==「 readOrder」 {
返回t.readOrder(stub,args)
} else if function ==「 getHistoryForOrder」 {
返回t.getHistoryForOrder(stub,args)
} else if function ==「 getOrdersByRange」 {
返回t.getOrdersByRange(stub,args)
} else if function ==「 queryOrderByOrder_medicine_id」 {
返回t.queryOrderByOrder_medicine_id(stub,args)
} else if function ==「 readOrderPrivateDetails」 {
返回t.readOrderPrivateDetails(stub,args)
} else if function ==「 changeOrderPrivateDetails」 {
返回t.changeOrderPrivateDetails(stub,args)
}

fmt.Println(「調用未找到函數:」 +函數)
返回shim.Error(「接收到的未知函數調用」)
}

func(t * SimpleChaincode)initOrder(stub shim.ChaincodeStubInterface,args()string)pb.Response {
var err錯誤

如果len(args)= 7 {
return shim.Error(「參數數量不正確。應為7」)
}

fmt.Println(「-開始初始化順序」)
如果len(args(0))<= 0 {
return shim.Error(「第一個參數必須為非空字元串」)
}
如果len(args(1))<= 0 {
return shim.Error(「第二個參數必須是非空字元串」)
}
如果len(args(2))<= 0 {
return shim.Error(「第三個參數必須是非空字元串」)
}
如果len(args(3))<= 0 {
return shim.Error(「第四個參數必須為非空字元串」)
}
如果len(args(4))<= 0 {
return shim.Error(「第5個參數必須為非空字元串」)
}
如果len(args(5))<= 0 {
return shim.Error(「第六個參數必須是一個非空字元串」)
}
如果len(args(6))<= 0 {
return shim.Error(「第7個參數必須為非空字元串」)
}

orderName:= args(0)
Manufacturer_id:= args(1)
provider_id:= args(2)
material_name:=參數(3)
order_medicine_id:=參數(4)
數量:= args(5)
狀態:= args(6)

orderAsBytes,err:= stub.GetState(orderName)
如果err= nil {
返回shim.Error(「無法獲得訂單:」 + err.Error())
} if orderAsBytes= nil {
fmt.Println(「此orderName已經存在:」 + orderName)
return shim.Error(「此orderName已經存在:」 + orderName)
}

objectType:=「訂單」
訂單:=&order {對象類型,訂單名稱,製造商ID,提供商ID,物料名稱,訂單醫學ID,數量,狀態}
orderJSONasBytes,err:= json.Marshal(順序)
如果err= nil {
返回shim.Error(err.Error())
}

err = stub.PutState(orderName,orderJSONasBytes)
如果err= nil {
返回shim.Error(err.Error())
}

// ====創建帶有價格的訂單私人詳細信息對象,將其編組為JSON,然後保存為狀態====
transMap,錯誤:= stub.GetTransient()
如果err= nil {
返回shim.Error(「出現瞬態錯誤:」 + err.Error())
}

如果_,確定:= transMap(「 order」); 好 {
return shim.Error(「訂單必須是瞬態映射中的鍵」)
}

如果len(transMap(「 order」))== 0 {
return shim.Error(「瞬態映射中的順序值必須是非空的JSON字元串」)
}

鍵入orderTransientInput struct {
價格字元串`json:「 price」`
}

var orderInput orderTransientInput
err = json.Unmarshal(transMap(「 order」),&orderInput)
如果err= nil {
返回shim.Error(「無法解碼JSON的」 +字元串(transMap(「 order」)))
}

orderPrivateDetails:=&orderPrivateDetails {
價格:orderInput.Price,
}
orderPrivateDetailsBytes,err:= json.Marshal(orderPrivateDetails)
如果err= nil {
返回shim.Error(err.Error())
}
err = stub.PutPrivateData(「 collectionMaterialOrderPrivateDetails」,orderName,orderPrivateDetailsBytes)
如果err= nil {
返回shim.Error(err.Error())
}

indexName:=「 order_medicine_id」
order_medicine_idIndexKey,err:= stub.CreateCompositeKey(indexName,()string {order.Order_medicine_id,order.Order_raw_material_id})
如果err= nil {
返回shim.Error(err.Error())
}

值:=()byte {0x00}
stub.PutState(order_medicine_idIndexKey,值)

fmt.Println(「-結束初始化順序」)
返回shim.Success(nil)
}

func(t * SimpleChaincode)readOrder(stub shim.ChaincodeStubInterface,args()string)pb.Response {
var名稱,jsonResp字元串
var err錯誤

如果len(args)= 1 {
return shim.Error(「參數數量錯誤。期望查詢的訂單名稱」)
}

名稱= args(0)
valAsbytes,err:= stub.GetState(name)
如果err= nil {
jsonResp =「 {」錯誤「:」無法獲取「 +名稱+」 「}」的狀態
返回shim.Error(jsonResp)
}否則,如果valAsbytes == nil {
jsonResp =「 {」錯誤「:」訂單不存在:「 +名稱+」 「}」
返回shim.Error(jsonResp)
}

返回shim.Success(valAsbytes)
}

func(t * SimpleChaincode)changeStatus(stub shim.ChaincodeStubInterface,args()string)pb.Response {

如果len(args)<2 {
return shim.Error(「參數數量不正確。應為2」)
}

orderName:= args(0)
newStatus:= args(1)
fmt.Println(「-start changeStatus」,orderName,newStatus)

orderAsBytes,err:= stub.GetState(orderName)
如果err= nil {
返回shim.Error(「無法獲得訂單:」 + err.Error())
}否則,如果orderAsBytes == nil {
返回shim.Error(「訂單不存在」)
}

statusToChange:=訂單{}
err = json.Unmarshal(orderAsBytes,&statusToChange)
如果err= nil {
返回shim.Error(err.Error())
}
statusToChange.Status = newStatus

orderJSONasBytes,_:= json.Marshal(statusToChange)
err = stub.PutState(orderName,orderJSONasBytes)
如果err= nil {
返回shim.Error(err.Error())
}

fmt.Println(「-end changeStatus(success)」)
返回shim.Success(nil)
}

func(t * SimpleChaincode)changeProvider_id(stub shim.ChaincodeStubInterface,args()string)pb.Response {

如果len(args)<2 {
return shim.Error(「參數數量不正確。應為2」)
}

orderName:= args(0)
newProvider_id:= args(1)
fmt.Println(「-start changeStatus」,orderName,newProvider_id)

orderAsBytes,err:= stub.GetState(orderName)
如果err= nil {
返回shim.Error(「無法獲得訂單:」 + err.Error())
}否則,如果orderAsBytes == nil {
返回shim.Error(「訂單不存在」)
}

provider_idToChange:=訂單{}
err = json.Unmarshal(orderAsBytes,&provider_idToChange)
如果err= nil {
返回shim.Error(err.Error())
}

orderJSONasBytes,_:= json.Marshal(provider_idToChange)
err = stub.PutState(orderName,orderJSONasBytes)
如果err= nil {
返回shim.Error(err.Error())
}

fmt.Println(「-end changeStatus(success)」)
返回shim.Success(nil)
}

func(t * SimpleChaincode)changeQuantity(stub shim.ChaincodeStubInterface,args()string)pb.Response {

如果len(args)<2 {
return shim.Error(「參數數量不正確。應為2」)
}

orderName:= args(0)
newQuantity:= args(1)
fmt.Println(「-start changeStatus」,orderName,newQuantity)

orderAsBytes,err:= stub.GetState(orderName)
如果err= nil {
返回shim.Error(「無法獲得訂單:」 + err.Error())
}否則,如果orderAsBytes == nil {
返回shim.Error(「訂單不存在」)
}

QuantityToChange:=訂單{}
err = json.Unmarshal(orderAsBytes,&quantityToChange)
如果err= nil {
返回shim.Error(err.Error())
}
QuantityToChange.Quantity = newQuantity

orderJSONasBytes,_:= json.Marshal(quantityToChange)
err = stub.PutState(orderName,orderJSONasBytes)
如果err= nil {
返回shim.Error(err.Error())
}

fmt.Println(「-end changeStatus(success)」)
返回shim.Success(nil)
}

func(t * SimpleChaincode)queryOrderByOrder_medicine_id(stub shim.ChaincodeStubInterface,args()string)pb.Response {

如果len(args)<1 {
return shim.Error(「參數數量不正確。應為1」)
}

order_medicine_id:=參數(0)

queryString:= fmt.Sprintf(「 {」 selector 「:{」 docType 「:」 order 「,」 order_medicine_id 「:」%s 「}}」,order_medicine_id)

queryResults,err:= getQueryResultForQueryString(stub,queryString)
如果err= nil {
返回shim.Error(err.Error())
}
返回shim.Success(queryResults)
}

func getQueryResultForQueryString(stub shim.ChaincodeStubInterface,queryString string)(()byte,error){

fmt.Printf(「-getQueryResultForQueryString queryString: n%s n」,queryString)

resultsIterator,err:= stub.GetQueryResult(queryString)
如果err= nil {
返回nil,err
}
延遲resultsIterator.Close()

緩衝區,錯誤:= ConstructQueryResponseFromIterator(resultsIterator)
如果err= nil {
返回nil,err
}

fmt.Printf(「-getQueryResultForQueryString queryResult: n%s n」,buffer.String())

返回buffer.Bytes(),無
}

func ConstructQueryResponseFromIterator(resultsIterator shim.StateQueryIteratorInterface)(* bytes.Buffer,錯誤){

var buffer bytes.Buffer
buffer.WriteString(「(」)

bArrayMemberAlreadyWritten:=否
對於resultsIterator.HasNext(){
queryResponse,err:= resultsIterator.Next()
如果err= nil {
返回nil,err
}

如果bArrayMemberAlreadyWritten == true {
buffer.WriteString(「,」)
}
buffer.WriteString(「 {」 Key 「:」)
buffer.WriteString(「 」「)
buffer.WriteString(queryResponse.Key)
buffer.WriteString(「 」「)

buffer.WriteString(「,」記錄「:」)
buffer.WriteString(string(queryResponse.Value))
buffer.WriteString(「}」)
bArrayMemberAlreadyWritten = true
}
buffer.WriteString(「)」)

return&buffer,無
}

func(t * SimpleChaincode)getHistoryForOrder(stub shim.ChaincodeStubInterface,args()string)pb.Response {

如果len(args)<1 {
return shim.Error(「參數數量不正確。應為1」)
}

orderName:= args(0)

fmt.Printf(「-start getHistoryForOrder:%s n」,orderName)

resultsIterator,err:= stub.GetHistoryForKey(orderName)
如果err= nil {
返回shim.Error(err.Error())
}
延遲resultsIterator.Close()

var buffer bytes.Buffer
buffer.WriteString(「(」)

bArrayMemberAlreadyWritten:=否
對於resultsIterator.HasNext(){
響應,錯誤:= resultsIterator.Next()
如果err= nil {
返回shim.Error(err.Error())
}

如果bArrayMemberAlreadyWritten == true {
buffer.WriteString(「,」)
}
buffer.WriteString(「 {」 TxId 「:」)
buffer.WriteString(「 」「)
buffer.WriteString(response.TxId)
buffer.WriteString(「 」「)

buffer.WriteString(「,」 Value 「:」)

如果response.IsDelete {
buffer.WriteString(「 null」)
}其他{
buffer.WriteString(string(response.Value))
}

buffer.WriteString(「,」 Timestamp 「:」)
buffer.WriteString(「 」「)
buffer.WriteString(time.Unix(response.Timestamp.Seconds,int64(response.Timestamp.Nanos))。String())
buffer.WriteString(「 」「)

buffer.WriteString(「,」 IsDelete 「:」)
buffer.WriteString(「 」「)
buffer.WriteString(strconv.FormatBool(response.IsDelete))
buffer.WriteString(「 」「)

buffer.WriteString(「}」)
bArrayMemberAlreadyWritten = true
}
buffer.WriteString(「)」)

fmt.Printf(「-getHistoryForOrder返回: n%s n」,buffer.String())

返回shim.Success(buffer.Bytes())
}

func(t * SimpleChaincode)getOrdersByRange(stub shim.ChaincodeStubInterface,args()string)pb.Response {

如果len(args)<2 {
return shim.Error(「參數數量不正確。應為2」)
}

startKey:=參數(0)
endKey:=參數(1)

resultsIterator,err:= stub.GetStateByRange(startKey,endKey)
如果err= nil {
返回shim.Error(err.Error())
}
延遲resultsIterator.Close()

緩衝區,錯誤:= ConstructQueryResponseFromIterator(resultsIterator)
如果err= nil {
返回shim.Error(err.Error())
}

fmt.Printf(「-getOrdersByRange queryResult: n%s n」,buffer.String())

返回shim.Success(buffer.Bytes())
}

func(t * SimpleChaincode)readOrderPrivateDetails(stub shim.ChaincodeStubInterface,args()string)pb.Response {
var名稱,jsonResp字元串
var err錯誤

如果len(args)= 1 {
return shim.Error(「參數數量錯誤。期望查詢的訂單名稱」)
}

名稱= args(0)
valAsbytes,err:= stub.GetPrivateData(「 collectionMaterialOrderPrivateDetails」,name)//從鏈碼狀態獲取訂單私有詳細信息
如果err= nil {
jsonResp =「 {」 Error 「:」無法獲取「 +名稱+」的私人詳細信息:「 + err.Error()+」 「}」
返回shim.Error(jsonResp)
}否則,如果valAsbytes == nil {
jsonResp =「 {」錯誤「:」訂單私人詳細信息不存在:「 +名稱+」 「}」
返回shim.Error(jsonResp)
}

返回shim.Success(valAsbytes)
}

func(t * SimpleChaincode)changeOrderPrivateDetails(stub shim.ChaincodeStubInterface,args()string)pb.Response {

如果len(args)<2 {
return shim.Error(「參數數量不正確。應為2」)
}

orderName:= args(0)
newPrice:= args(1)
fmt.Println(「-start changeStatus」,orderName,newPrice)

orderAsBytes,錯誤:= stub.GetPrivateData(「 collectionMaterialOrderPrivateDetails」,orderName)
如果err= nil {
返回shim.Error(「無法獲得訂單:」 + err.Error())
}否則,如果orderAsBytes == nil {
返回shim.Error(「訂單不存在」)
}

priceToChange:= orderPrivateDetails {}
err = json.Unmarshal(orderAsBytes,&priceToChange)
如果err= nil {
返回shim.Error(err.Error())
}
priceToChange.Price = newPrice

orderJSONasBytes,_:= json.Marshal(priceToChange)
err = stub.PutPrivateData(「 collectionMaterialOrderPrivateDetails」,orderName,orderJSONasBytes)
如果err= nil {
返回shim.Error(err.Error())
}

fmt.Println(「-end changeStatus(success)」)
返回shim.Success(nil)
}

你可能還喜歡