go - 如何将字节数组传递给 HyperLedger Fabric 中的链码

标签 go hyperledger-fabric hyperledger

我正在编写一个在 fabric 上运行的链代码,这是“fabcar.go”的代码片段,fabric 链代码的示例。

我注意到我可以使用 fabric-java-sdk 从我的 java 应用程序传递一个 []string 参数,但是当我尝试从我的应用程序传递一些 []byte 参数时我遇到了问题。

我已经尝试过其他功能,例如

func (stub *ChaincodeStub) GetArgs() [][]byte
func (stub *ChaincodeStub) GetArgsSlice() ([]byte, error)
func (stub *ChaincodeStub) GetBinding() ([]byte, error)

但还是不知道怎么做。

func (s *SmartContract) Invoke(APIstub shim.ChaincodeStubInterface) sc.Response {
        // Retrieve the requested Smart Contract function and arguments
        function, args := APIstub.GetFunctionAndParameters()
        // Route to the appropriate handler function to interact with the ledger appropriately
        if function == "queryCar" {
            return s.queryCar(APIstub, args)
        ...

我是不是漏掉了什么或者现在不支持了?请帮助我!

最佳答案

事实证明,所有参数都以 [][]byte 类型传递给链码。在定义中是

args           [][]byte

type ChaincodeStub struct {
    TxID           string
    ChannelId      string
    chaincodeEvent *pb.ChaincodeEvent
    args           [][]byte
    handler        *Handler
    signedProposal *pb.SignedProposal
    proposal       *pb.Proposal

    // Additional fields extracted from the signedProposal
    creator   []byte
    transient map[string][]byte
    binding   []byte

    decorations map[string][]byte
}

它只是函数 GetFunctionAndParameters() 将这些字节包装成字符串。

// GetFunctionAndParameters documentation can be found in interfaces.go
func (stub *ChaincodeStub) GetFunctionAndParameters() (function string, params []string) {
    allargs := stub.GetStringArgs()
    function = ""
    params = []string{}
    if len(allargs) >= 1 {
        function = allargs[0]
        params = allargs[1:]
    }
    return
}

返回值'function'实际上是string(allargs[0]),其余的args将在allargs[1:]中。

关于go - 如何将字节数组传递给 HyperLedger Fabric 中的链码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50942671/

相关文章:

hyperledger-fabric - 错误 : failed to create deliver client: orderer client failed to connect to orderer: failed to create new connection: context deadline exceeded

hyperledger-fabric - 在 Hyperledger Fabric 中,orderer 创世 block 的重要性是什么?

hyperledger-fabric - Hyperledger Fabric 网络中的多个组织具有相同的根 ca

go - Golang中的0x1是什么意思?

go - 如何在golang中返回int或nil?

node.js - 找不到模块; Hyperledger Fabric 教程

hyperledger-fabric - Hyperledger Fabric 交叉通信

hyperledger-fabric - Hyperledger Fabric 2.0 -chaincode 作为外部服务 :Unknown chaincodeType: EXTERNAL

go - 使用 kubernetes go 客户端扩展部署副本

json - 传递给函数的JSON对象在Golang中失败