本文整理匯總了Golang中github.com/hyperledger/fabric/core/chaincode/shim.ChaincodeStubInterface.QueryChaincode方法的典型用法代碼示例。如果您正苦於以下問題:Golang ChaincodeStubInterface.QueryChaincode方法的具體用法?Golang ChaincodeStubInterface.QueryChaincode怎麽用?Golang ChaincodeStubInterface.QueryChaincode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類github.com/hyperledger/fabric/core/chaincode/shim.ChaincodeStubInterface
的用法示例。
在下文中一共展示了ChaincodeStubInterface.QueryChaincode方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: Invoke
// Invoke queries another chaincode and updates its own state
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
var sum string // Sum entity
var Aval, Bval, sumVal int // value of sum entity - to be computed
var err error
if len(args) != 2 {
return nil, errors.New("Incorrect number of arguments. Expecting 2")
}
chaincodeURL := args[0] // Expecting "github.com/hyperledger/fabric/core/example/chaincode/chaincode_example02"
sum = args[1]
// Query chaincode_example02
f := "query"
queryArgs := util.ToChaincodeArgs(f, "a")
response, err := stub.QueryChaincode(chaincodeURL, queryArgs)
if err != nil {
errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
Aval, err = strconv.Atoi(string(response))
if err != nil {
errStr := fmt.Sprintf("Error retrieving state from ledger for queried chaincode: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
queryArgs = util.ToChaincodeArgs(f, "b")
response, err = stub.QueryChaincode(chaincodeURL, queryArgs)
if err != nil {
errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
Bval, err = strconv.Atoi(string(response))
if err != nil {
errStr := fmt.Sprintf("Error retrieving state from ledger for queried chaincode: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
// Compute sum
sumVal = Aval + Bval
// Write sumVal back to the ledger
err = stub.PutState(sum, []byte(strconv.Itoa(sumVal)))
if err != nil {
return nil, err
}
fmt.Printf("Invoke chaincode successful. Got sum %d\n", sumVal)
return []byte(strconv.Itoa(sumVal)), nil
}
示例2: iq
//helper
func (p *PassthruChaincode) iq(invoke bool, stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
if function == "" {
return nil, errors.New("Chaincode ID not provided")
}
chaincodeID := function
if invoke {
return stub.InvokeChaincode(chaincodeID, util.ToChaincodeArgs(args...))
}
return stub.QueryChaincode(chaincodeID, util.ToChaincodeArgs(args...))
}
示例3: Query
// Query callback representing the query of a chaincode
func (t *SimpleChaincode) Query(stub shim.ChaincodeStubInterface, function string, args []string) ([]byte, error) {
if function != "query" {
return nil, errors.New("Invalid query function name. Expecting \"query\"")
}
var sum string // Sum entity
var Aval, Bval, sumVal int // value of sum entity - to be computed
var err error
// Can query another chaincode within query, but cannot put state or invoke another chaincode (in transaction context)
if len(args) != 2 {
return nil, errors.New("Incorrect number of arguments. Expecting 2")
}
chaincodeURL := args[0]
sum = args[1]
// Query chaincode_example02
f := "query"
queryArgs := util.ToChaincodeArgs(f, "a")
response, err := stub.QueryChaincode(chaincodeURL, queryArgs)
if err != nil {
errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
Aval, err = strconv.Atoi(string(response))
if err != nil {
errStr := fmt.Sprintf("Error retrieving state from ledger for queried chaincode: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
queryArgs = util.ToChaincodeArgs(f, "b")
response, err = stub.QueryChaincode(chaincodeURL, queryArgs)
if err != nil {
errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
Bval, err = strconv.Atoi(string(response))
if err != nil {
errStr := fmt.Sprintf("Error retrieving state from ledger for queried chaincode: %s", err.Error())
fmt.Printf(errStr)
return nil, errors.New(errStr)
}
// Compute sum
sumVal = Aval + Bval
fmt.Printf("Query chaincode successful. Got sum %d\n", sumVal)
jsonResp := "{\"Name\":\"" + sum + "\",\"Value\":\"" + strconv.Itoa(sumVal) + "\"}"
fmt.Printf("Query Response:%s\n", jsonResp)
return []byte(strconv.Itoa(sumVal)), nil
}