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测试我的链码,而无需设置结构网络。解决此问题的方法是什么?
P.S问题已解决,这是设置结构的正确方法
type ballot struct {
PersonFirstName string
PersonLastName string
BallotID string
VoteInit string
}
答案 0 :(得分:4)
您尚未提供选票结构的代码。但是根据您提供的信息,我猜测可能会发生什么。我认为您可能还没有导出字段,并且您的结构看起来像这样:
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
}
这是许多新手经常犯的错误。祝您在旅途中一切顺利!
P.S。即使此解决方案可以解决您的问题,也请在此处编辑您的问题并添加您的投票结构代码,因为这可能会在将来对其他人有所帮助。另外,请在代码中添加适当的缩进,并在代码块中添加最后一个}符号。