fabric1.4链码返回为空问题
 

开发者模式其实只是简化了区块链网络的创建,但是链码跑上面如果出错还是会有错误日志的。一般错误日志的。 使用docker logs 查询对应的容器进程。


初始化putState的代码你是放在Init方法里面调用的吗? 怀疑开发者模式的话就直接类似fabic-samples/facar 调用basic-network单节点网络来安装执行你的链码试试。


如果你实在找不问题, 你可以把完整的链码给我,我本地环境可以跑下。

 

没想到真的能得到回答,我没怀疑开发者模式,我只是不知道哪里出了问题导致链码没有返回值。跪求帮跑下,在fabcar基础上改的,我没找到为啥,返回就是空。

完整链码如下:

package main


import (

"bytes"

"encoding/json"

"fmt"


"github.com/hyperledger/fabric/core/chaincode/shim"

sc "github.com/hyperledger/fabric/protos/peer"

)


// Define the Smart Contract structure

type SmartContract struct {

}


// Define the car structure, with 4 properties.  Structure tags are used by encoding/json library

type Student struct {

  id   string `json:"id"`

name  string `json:"name"`

mark string `json:"mark"`

record  string `json:"record"`

}


/*

 * The Init method is called when the Smart Contract "fabcar" is instantiated by the blockchain network

 * Best practice is to have any Ledger initialization in separate function -- see initLedger()

 */

func (s *SmartContract) Init(APIstub shim.ChaincodeStubInterface) sc.Response {

return shim.Success(nil)

}


func (s *SmartContract) Invoke(APIstub shim.ChaincodeStubInterface) sc.Response {


// Retrieve the requested Smart Contract function and arguments

function, args := APIstub.GetFunctionAndParameters()

// Route to the appropriate handler function to interact with the ledger appropriately

if function == "queryStudent" {

return s.queryStudent(APIstub, args)

} else if function == "initLedger" {

return s.initLedger(APIstub)

} else if function == "create" {

return s.create(APIstub, args)

} else if function == "queryAll" {

return s.queryAll(APIstub)


return shim.Error("Invalid Smart Contract function name.")

}



func (s *SmartContract) initLedger(APIstub shim.ChaincodeStubInterface) sc.Response {

student1 := Student{id: "18180100001", name: "Peter", mark: "97", record: "Play Computer"}

student2 := Student{id: "18180100002", name: "Tom", mark: "88", record: "Sing, Dance, Rap, Play Basketball"}

    student3 := Student{id: "18180100003", name: "Bob", mark: "64", record: "Fuck, Gay"}

student4 := Student{id: "18180100004", name: "Andy", mark: "72", record: "Cheat in the exam, Copy"}

student5 string = "Fuck you the bug"

Bytes1, _ := json.Marshal(student1)

APIstub.PutState("STUDENT1", []byte(Bytes1))

Bytes2, _ := json.Marshal(student2)

APIstub.PutState("STUDENT2", []byte(Bytes2))

Bytes3, _ := json.Marshal(student3)

APIstub.PutState("STUDENT3", []byte(Bytes3))

return shim.Success(nil)

}


func (s *SmartContract) queryStudent(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {


if len(args) != 1 {

return shim.Error("Incorrect number of arguments. Expecting 1")

}


Bytes, _ := APIstub.GetState(args[0])

jsonResp := "{\"Name\":\"" + args[0] + "\",\"Information\":\"" + string(Bytes) + "\"}"

    fmt.Printf("Query Response:%s\n", jsonResp)

return shim.Success(Bytes)

}

func (s *SmartContract) create(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {


if len(args) != 5 {

return shim.Error("Incorrect number of arguments. Expecting 5")

}


var car = Student{id: args[1], name: args[2], mark: args[3], record: args[4] }


Bytes, _ := json.Marshal(car)

APIstub.PutState(args[0], AsBytes)


return shim.Success(nil)

}


func (s *SmartContract) queryAll(APIstub shim.ChaincodeStubInterface) sc.Response {


startKey := "STUDENT0"

endKey := "STUDENT999"


resultsIterator, err := APIstub.GetStateByRange(startKey, endKey)

if err != nil {

return shim.Error(err.Error())

}

defer resultsIterator.Close()


// buffer is a JSON array containing QueryResults

var buffer bytes.Buffer

buffer.WriteString("[")


bArrayMemberAlreadyWritten := false

for resultsIterator.HasNext() {

queryResponse, err := resultsIterator.Next()

if err != nil {

return shim.Error(err.Error())

}

// Add a comma before array members, suppress it for the first array member

if bArrayMemberAlreadyWritten == true {

buffer.WriteString(",")

}

buffer.WriteString("{\"Key\":")

buffer.WriteString("\"")

buffer.WriteString(queryResponse.Key)

buffer.WriteString("\"")


buffer.WriteString(", \"Record\":")

// Record is a JSON object, so we write as-is

buffer.WriteString(string(queryResponse.Value))

buffer.WriteString("}")

bArrayMemberAlreadyWritten = true

}

buffer.WriteString("]")

fmt.Printf(" queryAll:\n%s\n", buffer.String())


return shim.Success(buffer.Bytes())

}




// The main function is only relevant in unit test mode. Only included here for completeness.

func main() {


// Create a new Smart Contract

err := shim.Start(new(SmartContract))

if err != nil {

fmt.Printf("Error creating new Smart Contract: %s", err)

}

}


 

GOLAND过期了,最好装个类似GOLAND一样好的IDE写链码,有些语法问题。

TIM截图20190707085730.png


TIM截图20190707085755.png

数据拿不出来是结构体的时候,每个字段必须用大写

Student {

   Id   Name  Mark Record  }


我把修正的代码放在了https://github.com/zealzeng/fabric-samples/tree/master/yezzi-student

运行效果:

[root@k8s-master fabcar]# docker exec -e "CORE_PEER_LOCALMSPID=Org1MSP" -e "CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp" cli peer chaincode invoke -o orderer.example.com:7050 -C mychannel -n fabcar -c '{"function":"queryStudent","Args":["STUDENT1"]}'
2019-07-06 17:13:58.878 UTC [chaincodeCmd] chaincodeInvokeOrQuery -> INFO 001 Chaincode invoke successful. result: status:200 payload:"{\"id\":\"18180100001\",\"mark\":\"97\",\"name\":\"Peter\",\"record\":\"Play Computer\"}"


 

感谢感谢,十分感谢,!!!!

发表于:2019-07-06 11:00
赏金:¥0.1
状态: 已解决
  问题人
Yezzi
问题:1 答题:0
心得:0 被赞:0
  答题人
Zealot
问题:4 答题:98
心得:44 被赞:0
支付宝扫码支付