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在其验证之前不会将事务提交到分类帐,但我只是尝试使用MockStub测试我的链码,而不设置fabric网络。这个问题的解决办法是什么?
问题已经解决了,这是建立一个结构的正确方法
type ballot struct {
PersonFirstName string
PersonLastName string
BallotID string
VoteInit string
}发布于 2018-11-23 07:47:15
你还没有提供选票结构的代码。但从你所提供的来看,我猜到了可能发生了什么。我认为您可能还没有导出这些字段,您的结构如下所示:
type ballot struct {
personFirstName string
personLastName string
ballotID string
voteInit string
}但是,当您试图使用json.Marshal(Ballot)将这个对象转换为JSON时,没有一个字段被添加到JSON对象中,因为它们没有导出。在这种情况下,您所要做的就是导出必要的字段(在字段名的开头使用大写字母)。更新后的结构应该如下所示:
type ballot struct {
PersonFirstName string
PersonLastName string
BallotID string
VoteInit string
}这是许多新来者犯的一个非常普遍的错误。祝您旅途顺利!
请编辑您的问题,并在这里添加您的选票结构的代码,即使这个解决方案解决了您的问题,因为这可能会在未来帮助其他人。此外,请在代码中添加适当的缩进,并在代码块中添加最后一个}符号。
https://stackoverflow.com/questions/53437717
复制相似问题