當前位置: 首頁>>代碼示例>>Golang>>正文


Golang ChaincodeStub.QueryChaincode方法代碼示例

本文整理匯總了Golang中github.com/hyperledger/fabric/core/chaincode/shim.ChaincodeStub.QueryChaincode方法的典型用法代碼示例。如果您正苦於以下問題:Golang ChaincodeStub.QueryChaincode方法的具體用法?Golang ChaincodeStub.QueryChaincode怎麽用?Golang ChaincodeStub.QueryChaincode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在github.com/hyperledger/fabric/core/chaincode/shim.ChaincodeStub的用法示例。


在下文中一共展示了ChaincodeStub.QueryChaincode方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: Invoke

// Invoke queries another chaincode and updates its own state
func (t *SimpleChaincode) Invoke(stub *shim.ChaincodeStub, 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 := []string{"a"}
	response, err := stub.QueryChaincode(chaincodeURL, f, 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 = []string{"b"}
	response, err = stub.QueryChaincode(chaincodeURL, f, 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
}
開發者ID:C0rWin,項目名稱:fabric,代碼行數:55,代碼來源:chaincode_example05.go

示例2: Query

// Query callback representing the query of a chaincode
func (t *SimpleChaincode) Query(stub *shim.ChaincodeStub, 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 := []string{"a"}
	response, err := stub.QueryChaincode(chaincodeURL, f, 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 = []string{"b"}
	response, err = stub.QueryChaincode(chaincodeURL, f, 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
}
開發者ID:C0rWin,項目名稱:fabric,代碼行數:55,代碼來源:chaincode_example05.go

示例3: get_number

//==============================================================================================================================
//	get_number - Queries the numbers chaincode and calls get_number, chaincode name currently hardcoded.
//				 Returns the current number value stored in the world state
//==============================================================================================================================
func (t *SimpleChaincode) get_number(stub *shim.ChaincodeStub, args []string) ([]byte, error) {

	res, err := stub.QueryChaincode("970000a712cf185274f39566276b18591c125ab41ee2674b3ccfeeed3e95a21b7129a4b97598e7807a362b345609caa8985f28f68e3a1614e0f12dcd99d3a589", "get_number", args)

	if err != nil {
		return nil, errors.New("Unable to query chaincode")
	}

	return res, nil

}
開發者ID:jpayne23,項目名稱:testDeployCC,代碼行數:15,代碼來源:tester.go

示例4: iq

//helper
func (p *PassthruChaincode) iq(invoke bool, stub *shim.ChaincodeStub, function string, args []string) ([]byte, error) {
	if function == "" {
		return nil, errors.New("Chaincode ID not provided")
	}
	chaincodeID := function

	var f string
	var cargs []string
	if len(args) > 0 {
		f = args[0]
		cargs = args[1:]
	}

	if invoke {
		return stub.InvokeChaincode(chaincodeID, f, cargs)
	}
	return stub.QueryChaincode(chaincodeID, f, cargs)
}
開發者ID:Colearo,項目名稱:fabric,代碼行數:19,代碼來源:passthru.go


注:本文中的github.com/hyperledger/fabric/core/chaincode/shim.ChaincodeStub.QueryChaincode方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。