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 Python ModuleNotFoundError报错,如何解决?
  • ¥15 我下载图形界面重启完就变成这样了,打字也打不了,动也动不了,该怎么解决(操作系统-centos)
  • ¥15 VBA中在窗体中遍历所有checkbox控件,提取出被选中的checkbox的caption值
  • ¥15 在Ubuntu上有什么命令,或者是系统文件能告诉我链接nvme ssd的pcie槽位是不是支持热插拔功能?
  • ¥15 ansys license许可证问题
  • ¥20 QQ号和密码都能正常登录微信 QQ号和密码登录微信显示密码错误
  • ¥15 qiankun主应用注册子应用提示跨域
  • ¥15 单片机RTOS Kernel与应用分离开发,Kernel如何调起应用?
  • ¥15 快手小店商家版APP怎么第三方APP跳转到指定用户聊天界面
  • ¥15 为什么AVL fire软件仿真时的步长改变了,仿真结果也会改变