dtysql0586 2018-11-22 20:35
浏览 156
已采纳

Chaincode GetState返回一个空响应

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))
    }

Above is the code and this is the test script i am running it against

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()
    }

}

I am trying to read from the ledger after putting the transaction in. However, I have been getting empty responses from both of the Println statements.

    // 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

It does say on the documentation that putState does not commit transactions to the ledger until its validated, but I am just trying to test my chaincode using the MockStub without setting up the fabric network. What is the fix to this problem?

P.S the problem has been solved, here is the right way to set up a struct

type ballot struct {
    PersonFirstName string
    PersonLastName  string
    BallotID        string
    VoteInit        string
}
  • 写回答

1条回答 默认 最新

  • dongsuoying9059 2018-11-23 07:47
    关注

    You haven't provided the code for the ballot struct yet. But from what you provided, I have a guess what might be going on. I think you probably haven't exported the fields and your struct looks like this:

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

    But when you tried to convert this object to JSON using json.Marshal(Ballot), none of the fields are added to the JSON object because they were not exported. All that you have to do in this case is exporting the necessary fields (using Uppercase letter at the beginning of field names). Your updated struct should look something like the following:

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

    This is a very common mistake many newcomers make. Wish you all the best in your journey forward!!!

    P.S. Please edit your question and add the code of you ballot struct here even if this solution solves your problem as that might help others in the future. Also, please add proper indentation to the code and add the last } symbol in the code block.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)