我正在编写一个在 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)
...
我是不是漏掉了什么或者现在不支持了?请帮助我!
最佳答案
事实证明,所有参数都以 [][]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:]中。
关于go - 如何将字节数组传递给 HyperLedger Fabric 中的链码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50942671/