运行Chaincode程序时出错

I am writing chaincode program in GO on my MacOS. Following is the code:

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
", string(theJson))
    err := shim.Start(new(SmartContract))
    if err != nil {
        fmt.Printf("Error creating new Smart Document: %s", err)
    } else {
        fmt.Println("Success")
    }
}

It is showing the following error due to this line, 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%

I cannot find any solution.

Is it possible to specify the limit on the length of struct attributes?. For example DocumentID should be length 10.

dousenjue3214
dousenjue3214 请检查。我现在加了
2 年多之前 回复
douhe1002
douhe1002 请提供最小,完整和可验证的示例
2 年多之前 回复
duanchi1230
duanchi1230 向我们展示您尝试过的内容以及失败的地方。
2 年多之前 回复

2个回答

It looks like you trying to run your chaincode from your IDE or locally, I'd suggest to consult with documentation and particular to see how to execute chaincode in devmode.

In your case you just missing a few environment variables:

export CORE_CHAINCODE_LOGLEVEL=debug 
export CORE_PEER_ADDRESS=127.0.0.1:7052 
export CORE_CHAINCODE_ID_NAME=mycc:0 // <--- name and version of your chaincode

which needed to execute your chaincode locally.



在您查看您看到的源代码 </ p>

  chaincodename:= viper.GetString(  “ chaincode.id.name”)
if chaincodename ==“” {
返回错误。New(“未提供错误的chaincode ID”)
}
</ code> </ pre>

我相信您可以做到:</ p>

  os.Setenv(“ chaincode.id.name”,“随便什么”)
</ code> </ pre>

或</ p>

  chaincode.id.name =不管运行main.go 
</ code> </ pre>

</ p>

当然,只要指定属性的长度即可。 您可以使用具有长度的数据结构(例如 [10] byte </ code>),也可以将该字段设为私有并使用setter方法验证该长度。</ p>

< a href =“ https://play.golang.org/p/WvG-ZWKhrZ7” rel =“ nofollow noreferrer”> https://play.golang.org/p/WvG-ZWKhrZ7 </ p>

 包main 

import(
“ fmt”
“ encoding / json”

type SomeDocument struct {
DocumentID [10] bytejson:“ docid“
CreatorID字符串json:” uid“
DocHolder字符串json:” doc_holder“
位置字符串json:” location“
DocumentType字符串json:” doc_type“
内容 字符串json:“ doc_content”
}

func(s * SomeDocument)SetDocumentID(id字符串){
copy(s.DocumentID [:],[] byte(id))
}
\ nfunc(s SomeDocument)MarshalJSON()([] byte,error){
tmp:= struct {
DocumentID字符串json:“ docid”
CreatorID字符串json:“ uid”
DocHolder字符串json:“ doc_holder”
位置字符串json:“ location”
DocumentType st 环json:“ doc_type”
内容字符串json:“ doc_content”
} {
字符串(s.DocumentID [:]),
s.CreatorID,
s.DocHolder,
s .Location,
s.DocumentType,
s.Content,
}
返回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”: “”, “位置”: “”, “DOC_TYPE”: “”, “doc_content”: “”}

} \ n </ code> </ pre>
</ div>

展开原文

原文

In you look at the source you see it is doing

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

I believe you can just do:

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

or

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

To get past that.

As far as specifying the length of an attribute, of course you can. You either use a data structure with a length (e.g. [10]byte), or make the field private and verify the length in your setter method.

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":""}

}

dongweicha6077
dongweicha6077 我是否需要在主目录中设置chaincode.id.name? 仍然会出现错误,因为:无法引用未导出的名称chaincode.id我尝试了os.Setenv,但是没有用
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问