Chaincode GetState returns an empty reponse












2














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
}









share|improve this question
























  • Can you provide the full code? It's hard to find out the problem without having the actual code.
    – Tanmoy Krishna Das
    Nov 23 at 1:27










  • Can you provide your ballot struct?
    – Tanmoy Krishna Das
    Nov 23 at 6:48










  • Also, the ballot struct has to implement the Chaincode interface. Did you implement the init and invoke methods properly?
    – Tanmoy Krishna Das
    Nov 23 at 6:55


















2














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
}









share|improve this question
























  • Can you provide the full code? It's hard to find out the problem without having the actual code.
    – Tanmoy Krishna Das
    Nov 23 at 1:27










  • Can you provide your ballot struct?
    – Tanmoy Krishna Das
    Nov 23 at 6:48










  • Also, the ballot struct has to implement the Chaincode interface. Did you implement the init and invoke methods properly?
    – Tanmoy Krishna Das
    Nov 23 at 6:55
















2












2








2







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
}









share|improve this question















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
}






go hyperledger-fabric hyperledger blockchain smartcontracts






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 26 at 11:45









Vadim Kotov

4,30153247




4,30153247










asked Nov 22 at 20:35









Chris_Z

204




204












  • Can you provide the full code? It's hard to find out the problem without having the actual code.
    – Tanmoy Krishna Das
    Nov 23 at 1:27










  • Can you provide your ballot struct?
    – Tanmoy Krishna Das
    Nov 23 at 6:48










  • Also, the ballot struct has to implement the Chaincode interface. Did you implement the init and invoke methods properly?
    – Tanmoy Krishna Das
    Nov 23 at 6:55




















  • Can you provide the full code? It's hard to find out the problem without having the actual code.
    – Tanmoy Krishna Das
    Nov 23 at 1:27










  • Can you provide your ballot struct?
    – Tanmoy Krishna Das
    Nov 23 at 6:48










  • Also, the ballot struct has to implement the Chaincode interface. Did you implement the init and invoke methods properly?
    – Tanmoy Krishna Das
    Nov 23 at 6:55


















Can you provide the full code? It's hard to find out the problem without having the actual code.
– Tanmoy Krishna Das
Nov 23 at 1:27




Can you provide the full code? It's hard to find out the problem without having the actual code.
– Tanmoy Krishna Das
Nov 23 at 1:27












Can you provide your ballot struct?
– Tanmoy Krishna Das
Nov 23 at 6:48




Can you provide your ballot struct?
– Tanmoy Krishna Das
Nov 23 at 6:48












Also, the ballot struct has to implement the Chaincode interface. Did you implement the init and invoke methods properly?
– Tanmoy Krishna Das
Nov 23 at 6:55






Also, the ballot struct has to implement the Chaincode interface. Did you implement the init and invoke methods properly?
– Tanmoy Krishna Das
Nov 23 at 6:55














1 Answer
1






active

oldest

votes


















3














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.






share|improve this answer





















  • Thank you Tanmoy!
    – Chris_Z
    Nov 23 at 14:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437717%2fchaincode-getstate-returns-an-empty-reponse%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














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.






share|improve this answer





















  • Thank you Tanmoy!
    – Chris_Z
    Nov 23 at 14:57
















3














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.






share|improve this answer





















  • Thank you Tanmoy!
    – Chris_Z
    Nov 23 at 14:57














3












3








3






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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 23 at 7:47









Tanmoy Krishna Das

463211




463211












  • Thank you Tanmoy!
    – Chris_Z
    Nov 23 at 14:57


















  • Thank you Tanmoy!
    – Chris_Z
    Nov 23 at 14:57
















Thank you Tanmoy!
– Chris_Z
Nov 23 at 14:57




Thank you Tanmoy!
– Chris_Z
Nov 23 at 14:57


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53437717%2fchaincode-getstate-returns-an-empty-reponse%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Contact image not getting when fetch all contact list from iPhone by CNContact

count number of partitions of a set with n elements into k subsets

A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks