gpt4 book ai didi

go - 运行 Chaincode 程序时出错

转载 作者:数据小太阳 更新时间:2023-10-29 03:43:49 35 4
gpt4 key购买 nike

我正在我的 MacOS 上用 GO 编写链码程序。以下是代码:

package main

import (
"encoding/json"
"fmt"

"github.com/hyperledger/fabric/core/chaincode/shim"
sc "github.com/hyperledger/fabric/protos/peer"
)

//Init and Invoke
type SmartContract struct {
}

type SomeDocument struct {
DocumentID string `json:"docid"`
CreatorID string `json:"uid"`
DocHolder string `json:"doc_holder"`
Location string `json:"location"`
DocumentType string `json:"doc_type"`
Content string `json:"doc_content"`
}

func (s *SmartContract) Init(APIstub shim.ChaincodeStubInterface) sc.Response {
return shim.Success(nil)
}

func (s *SmartContract) Invoke(APIstub shim.ChaincodeStubInterface) sc.Response {

return shim.Success(nil)
}

func main() {
first_doc := SomeDocument{"1001", "123456789012", "ABCD EFGH", "New Delhi", "School Form", "I want to enroll in this school"}
theJson, _ := json.Marshal(first_doc) //returns JSON encoding of first_stamp
fmt.Printf("%+v\n", string(theJson))
err := shim.Start(new(SmartContract))
if err != nil {
fmt.Printf("Error creating new Smart Document: %s", err)
} else {
fmt.Println("Success")
}
}

由于这一行,它显示了以下错误,shim.Start(new(SmartContract)):

{"docid":"1001","uid":"123456789012","doc_holder":"ABCD EFGH","location":"New Delhi","doc_type":"School Form","doc_content":"I want to enroll in this school"}
2018-04-10 23:34:41.598 IST [shim] SetupChaincodeLogging -> INFO 001 Chaincode log level not provided; defaulting to: INFO
2018-04-10 23:34:41.598 IST [shim] SetupChaincodeLogging -> INFO 002 Chaincode (build level: ) starting up ...
Error creating new Smart Document: error chaincode id not provided%

我找不到任何解决方案。

是否可以指定结构属性的长度限制?。例如 DocumentID 的长度应该是 10。

最佳答案

在您查看 the source 时,您会看到它在做什么

chaincodename := viper.GetString("chaincode.id.name")
if chaincodename == "" {
return errors.New("error chaincode id not provided")
}

我相信你可以做到:

os.Setenv("chaincode.id.name", "whatever")

chaincode.id.name=whatever go run main.go

克服它。

至于指定属性的长度,当然可以。您要么使用具有长度的数据结构(例如 [10]byte),要么将字段设为私有(private)并在 setter 方法中验证长度。

https://play.golang.org/p/WvG-ZWKhrZ7

package main

import (
"fmt"
"encoding/json"
)

type SomeDocument struct {
DocumentID [10]byte `json:"docid"`
CreatorID string `json:"uid"`
DocHolder string `json:"doc_holder"`
Location string `json:"location"`
DocumentType string `json:"doc_type"`
Content string `json:"doc_content"`
}

func (s *SomeDocument) SetDocumentID(id string) {
copy(s.DocumentID[:], []byte(id))
}

func (s SomeDocument) MarshalJSON() ([]byte, error) {
tmp := struct {
DocumentID string `json:"docid"`
CreatorID string `json:"uid"`
DocHolder string `json:"doc_holder"`
Location string `json:"location"`
DocumentType string `json:"doc_type"`
Content string `json:"doc_content"`
}{
string(s.DocumentID[:]),
s.CreatorID,
s.DocHolder,
s.Location,
s.DocumentType,
s.Content,
}
return json.Marshal(tmp)
}

func main() {
s := SomeDocument{}
s.SetDocumentID("1234567890abcd")
js, err := json.Marshal(s)
if err != nil {
panic(err)
}
fmt.Println(string(js))
// {"docid":"1234567890","uid":"","doc_holder":"","location":"","doc_type":"","doc_content":""}

}

关于go - 运行 Chaincode 程序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49760013/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com