dongwuge6201 2018-06-20 07:32
浏览 225
已采纳

如何在HyperLedger Fabric中将字节数组传递给chaincode

I am writing a chaincode to run on fabric, and this is code piece of 'fabcar.go', the sample of fabric chaincode.

I notice that I can pass a []string parameters from my java application using fabric-java-sdk, but I have problems when I try to pass some []byte parameters form my application.

I ve tried other funcs like

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

but still do not know how to do it.

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

Did I miss anything or it's not supported now? help me Please!

  • 写回答

1条回答 默认 最新

  • down_load1117 2018-06-22 15:28
    关注

    Turns out to be that all args are passed in type of [][]byte to chaincode. In the definition is

    args           [][]byte
    

    in

    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
    }
    

    It's just the function GetFunctionAndParameters() wrapper those bytes to string.

    // 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
    }
    

    the returned value 'function' is actually string(allargs[0]), and the rest of the args will be in allargs[1:].

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿