如何将字节数组传递给 HyperLedger Fabric 中的链码
how to pass byte array to chaincode in HyperLedger Fabric
我正在 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)
...
我是不是漏掉了什么或者现在不支持了?请帮助我!
原来是所有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:].
我正在 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)
...
我是不是漏掉了什么或者现在不支持了?请帮助我!
原来是所有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:].