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

问题描述 投票:0回答:1

我正在编写一个在织物上运行的链码,这是织物链码样本'fabcar.go'的代码片段。

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

我尝试过其他类似的功能

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)
        ...

我错过了什么或现在不支持吗?请帮帮我!

go hyperledger-fabric hyperledger
1个回答
0
投票

结果是所有args都以[] []字节的类型传递给chaincode。在定义中

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'实际上是字符串(allargs [0]),其余的args将在allargs [1:]中。

© www.soinside.com 2019 - 2024. All rights reserved.