Explore Courses Blog Tutorials Interview Questions
0 votes
in Blockchain by (12.7k points)

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)


    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)




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



    if res.Payload == nil {

        t.Log("initBallot failed to create a ballot")




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?

1 Answer

0 votes
by (29.5k points)

I think the error lies in the ballot struct which you haven't provided. 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

Browse Categories