Chaincode GetState returns 空响应

Chaincode GetState returns an empty response

func (t *ballot) initBallot(stub shim.ChaincodeStubInterface, args []string) peer.Response {

    if len(args) != 2 {
        return shim.Error("Incorrect number of arguments. Expecting 2")
    }

    // ==== Input sanitation ====
    fmt.Println("- start init ballot")

    if len(args[0]) == 0 {
        return shim.Error("1st argument must be a non-empty string")
    }
    if len(args[1]) == 0 {
        return shim.Error("2nd argument must be a non-empty string")
    }

    personFirstName := args[0]
    personLastName := args[1]
    hash := sha256.New()
    hash.Write([]byte(personFirstName + personLastName)) // ballotID is created based on the person's name
    ballotID := hex.EncodeToString(hash.Sum(nil))
    voteInit := "VOTE INIT"

    // ==== Create ballot object and marshal to JSON ====
    Ballot := ballot{personFirstName, personLastName, ballotID, voteInit}
    ballotJSONByte, err := json.Marshal(Ballot)
    if err != nil {
        return shim.Error(err.Error())
    }
    err = stub.PutState(string(ballotID), ballotJSONByte)

    //FIXME:0-------------------------------------------------
    ballotAsByte, err := stub.GetState(string(ballotID))
    if err != nil {
        return shim.Error(err.Error())
    }
    BBBallot := ballot{}
    //umarshal the data to a new ballot struct
    json.Unmarshal(ballotAsByte, &BBBallot)
    //
    fmt.Println(BBBallot)
    fmt.Println(BBBallot.personFirstName)
    return shim.Success([]byte(ballotID))
    }

上面是代码,这是我运行反对

的测试脚本
func Test_Invoke_initBallot(t *testing.T) {
    scc := new(ballot)
    stub := shim.NewMockStub("voting", scc)
    res := stub.MockInvoke("1", [][]byte{[]byte("initBallot"), []byte("John"), []byte("C")})
    if res.Status != shim.OK {
        t.Log("bad status received, expected: 200; received:" + strconv.FormatInt(int64(res.Status), 10))
        t.Log("response: " + string(res.Message))
        t.FailNow()
    }
    if res.Payload == nil {
        t.Log("initBallot failed to create a ballot")
        t.FailNow()
    }

}

我试图在放入交易后从分类帐中读取。但是,我从两个 Println 语句中得到的响应都是空的。

    // PutState puts the specified `key` and `value` into the transaction's
// writeset as a data-write proposal. PutState doesn't effect the ledger
// until the transaction is validated and successfully committed.
// Simple keys must not be an empty string and must not start with null
// character (0x00), in order to avoid range query collisions with
// composite keys, which internally get prefixed with 0x00 as composite
// key namespace.
PutState(key string, value []byte) error

文档中确实说 putState 在验证之前不会将交易提交到账本,但我只是想在不设置 fabric 网络的情况下使用 MockStub 测试我的链代码。此问题的解决方法是什么?

P.S问题已经解决,这里是设置struct的正确方法

type ballot struct {
    PersonFirstName string
    PersonLastName  string
    BallotID        string
    VoteInit        string
}

您还没有提供选票结构的代码。但是根据您提供的内容,我猜可能会发生什么。我认为您可能还没有导出字段并且您的结构如下所示:

type ballot struct {
    personFirstName string
    personLastName string
    ballotID string
    voteInit string
}

但是当您尝试使用 json.Marshal(Ballot) 将此对象转换为 JSON 时,none 字段被添加到 JSON 对象中,因为它们没有被导出。在这种情况下,您所要做的就是导出必要的字段(在字段名称的开头使用大写字母)。您更新后的结构应如下所示:

type ballot struct {
    PersonFirstName string
    PersonLastName  string
    BallotID        string
    VoteInit        string
}

这是很多新手常犯的错误。祝大家一路顺风!!!

P.S。请编辑您的问题并在此处添加投票结构的代码,即使此解决方案解决了您的问题,因为这可能会在将来帮助其他人。另外,请在代码中添加适当的缩进,并在代码块中添加最后一个 } 符号。