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 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系
  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题