du060334 2018-04-10 17:47
浏览 663

运行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.

  • 写回答

2条回答 默认 最新

  • dongpu42006096 2018-04-10 18:41
    关注

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

报告相同问题?

悬赏问题

  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题