我想在Fabric Chaincode中使用GRPC来实现跨链通信,而不是使用Fabric SDK. 但当我在Fabric-Sample/测试-Network上调用chaincode函数时,它总是出错.

Error: endorsement failure during invoke. response: status:500 message:"error in simulation: failed to execute transaction eb5e480bd4075a767f56ae263741ca0f5f19620ef88952e26b7f1952bdbe83cd: could not launch chaincode chaincode_1.2:d3f97f15a635e73d3de230c8e5899e5fb95a68cf897c03e19f9e4eeca7ca3fd5: chaincode registration failed: container exited with 2"

谁能告诉我这个错误是什么原因? 我的链码有错误或GRPC不能在链码函数中使用?

我关于GRPC的链码:

func (s *SmartContract) begin(ctx contractapi.TransactionContextInterface) error {
    server.Main()
    return nil
}

func (s *SmartContract) client(ctx contractapi.TransactionContextInterface) error {
    // client.Clientfunc is the client main function
    client.Clientfunc(Xt, R, sign, m)
}

Server.go

func Main() {
    listen, err := net.Listen("tcp", ":9090")
    if err != nil {
        fmt.Printf("failed to listen: %v", err)
        return
    }
    grpcServer := grpc.NewServer()
    pb.RegisterSendServiceServer(grpcServer, &server{})
    err2 := grpcServer.Serve(listen)
    if err2 != nil {
        fmt.Printf("failed to serve: %v", err2)
        return
    }
}

Client.go

func Clientfunc(Xt *btcec.PublicKey, R *btcec.PublicKey, s *big.Int, m []byte) []byte {
    conn, err := grpc.Dial("127.0.0.1:9090", grpc.WithTransportCredentials(insecure.NewCredentials()))
    if err != nil {
        log.Fatalf("did not connect: %v", err)
    }
    defer conn.Close()
    client := pb.NewSendServiceClient(conn)
    output := &pb.SignInput{
        XtX: Xt.X().Int64(),
        XtY: Xt.Y().Int64(),
        M:   m,
        RX:  R.X().Int64(),
        RY:  R.Y().Int64(),
        S:   s.Int64(),
    }
    resp, _ := client.Send(context.Background(), output)
    return resp.GetM()
}

推荐答案

谁能告诉我这个错误是什么原因?

Hyperledger Fabric v2.x/ Logging Control中所述,什么可以告诉您导致error 500(内部服务器错误)的原因是服务器日志(log)

根据您的运行方式:

docker logs <chaincode_container_id>
kubectl logs -n <namespace> <pod_name>
oc logs -n <namespace> <pod_name>

这可能是由于链代码中的问题(如GRPC代码中的错误),也可能是由于链代码运行的环境.


From your code, you might consider not starting the gRPC server (server.Main()) in the chaincode. Chaincode runs within the Hyperledger Fabric network and is not meant to handle network communications like a standalone application does.
Instead, you should make the gRPC server a separate service that runs independently, and then the chaincode can communicate with this service as needed.

Plus the client.Clientfunc() function seems to establish a gRPC connection, send a request, and wait for a response. This is a synchronous operation and can be problematic if the response takes a long time to arrive. It's better to use an asynchronous operation (i.e., send the request and handle the response in a callback function) to avoid blocking the chaincode execution.
And... you should not ignore the error from client.Send() ;)

确保您的GRPC服务器不需要安全连接,否则grpc.WithTransportCredentials(insecure.NewCredentials())(不带SSL/TLS的不安全连接)将失败.


通常,建议在Fabric客户端应用程序中处理与外部系统的通信(如通过GRPC),而不是在链码本身中.

如果我只想使用链码而不是Fabric应用程序,有没有办法在不同渠道的组织之间进行通信?

不同渠道上的组织之间的通信可能很复杂,因为这是Hyperledger Fabric设计的一个基本方面,即channels个彼此隔离以维护数据隐私.

您可能会考虑:

  • Chaincode Functions: One organization can invoke a chaincode function on its own channel, which in turn invokes a chaincode function on another channel. This is possible because a chaincode can be associated with multiple channels.
    Note that this approach has the limitation that the second function invocation is not part of the same transaction as the first, so it can't be rolled back if the first transaction fails.

  • Dual Membership:一个组织可以是多个渠道的一部分.因此,它可以从一个通道读取数据,并将数据写入另一个通道.然而,这是在两个独立的事务中完成的,因此原子性不能得到保证.

  • Private Data Collections (PDCs):如果目标是在特定组织之间共享私有数据,甚至跨不同渠道,则PDC可能是一种 Select .PDC允许渠道上定义的组织子集认可、提交或查询私有数据,而无需跨渠道上的所有组织分发数据.

  • Interoperability Solutions: There are also more advanced solutions being developed for blockchain interoperability, like the Interledger Protocol (ILP), that could potentially be used to move data or assets between different Fabric networks (or even between entirely different types of blockchain networks).
    However, these are still largely in the research and development stage and might not be ready for production use.

Go相关问答推荐

如何模拟嵌入. FS?

具有GRPC的RBAC(基于角色的访问控制)-网关生成的REST风格的API

GetSecretValue,get identity:get credentials:无法刷新缓存的凭据

如何描述OpenAPI规范中围棋的数据类型.JSON?

Prometheus 摘要分位数错误

无效操作:v > max(类型参数 T 与 > 不可比较)

Json.Unmarshal() 和 gin.BindJson() 之间的区别

使用 goroutine 比较 Golang 中的两棵树是等价的

通过 Terraform 中的 MapNestedAtribute 进行迭代

当我使用 CircleCI 构建 Go Image 时,我得到runtime/cgo: pthread_create failed: Operation not allowed

使用Cookie身份验证的Gorilla Golang Websocket优化

如何将验证器标记添加到嵌套字段

GOLANG:为什么 SetDeadline/SetReadDeadline/SetWriteDeadline 在使用 os.File.Fd() 时对文件不起作用?

GRPC 元数据未在 Go 中更新

将shell输出绑定到Go中的 struct 的最佳方法?

github.com/rs/zerolog 字段的延迟判断

将接口方法的参数限制为几个允许的 struct ?

如何在 Windows 中使用 github.com/AllenDang/giu 和 github.com/gordonklaus/portaudio 构建 GO 程序

使用 xml.Name 将 xml 解组为 [] struct

如何从应用程序调用谷歌云身份 API