jjzjj

go - 运行 Chaincode 程序时出错

coder 2024-07-13 原文

我正在我的 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/

有关go - 运行 Chaincode 程序时出错的更多相关文章

  1. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  2. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  3. ruby - ECONNRESET (Whois::ConnectionError) - 尝试在 Ruby 中查询 Whois 时出错 - 2

    我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.

  4. ruby - 在 64 位 Snow Leopard 上使用 rvm、postgres 9.0、ruby 1.9.2-p136 安装 pg gem 时出现问题 - 2

    我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po

  5. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

  6. ruby - 在 Ruby 中编写命令行实用程序 - 2

    我想用ruby​​编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序

  7. ruby - 如何每月在 Heroku 运行一次 Scheduler 插件? - 2

    在选择我想要运行操作的频率时,唯一的选项是“每天”、“每小时”和“每10分钟”。谢谢!我想为我的Rails3.1应用程序运行调度程序。 最佳答案 这不是一个优雅的解决方案,但您可以安排它每天运行,并在实际开始工作之前检查日期是否为当月的第一天。 关于ruby-如何每月在Heroku运行一次Scheduler插件?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/8692687/

  8. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  9. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  10. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

随机推荐