当前位置: 首页>>代码示例>>Golang>>正文


Golang ChaincodeStub.QueryChaincode方法代码示例

本文整理汇总了Golang中github.com/openblockchain/obc-peer/openchain/chaincode/shim.ChaincodeStub.QueryChaincode方法的典型用法代码示例。如果您正苦于以下问题:Golang ChaincodeStub.QueryChaincode方法的具体用法?Golang ChaincodeStub.QueryChaincode怎么用?Golang ChaincodeStub.QueryChaincode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在github.com/openblockchain/obc-peer/openchain/chaincode/shim.ChaincodeStub的用法示例。


在下文中一共展示了ChaincodeStub.QueryChaincode方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。

示例1: invoke

// Transaction queries another chaincode and updates its own state
func (t *SimpleChaincode) invoke(stub *shim.ChaincodeStub, 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) != 3 {
		return nil, errors.New("Incorrect number of arguments. Expecting 3")
	}

	chaincodeUrl := args[0]     // Expecting "github.com/openblockchain/obc-peer/openchain/example/chaincode/chaincode_example02"
	chaincodeVersion := args[1] // Expecting "0.0.1"
	sum = args[2]

	// Query chaincode_example02
	f := "query"
	queryArgs := []string{"a"}
	response, err := stub.QueryChaincode(chaincodeUrl, chaincodeVersion, 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, chaincodeVersion, 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:masterDev1985,项目名称:cc_fat,代码行数:56,代码来源: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) != 3 {
		return nil, errors.New("Incorrect number of arguments. Expecting 3")
	}

	chaincodeUrl := args[0]     // Expecting "github.com/openblockchain/obc-peer/openchain/example/chaincode/chaincode_example02"
	chaincodeVersion := args[1] // Expecting "0.0.1"
	sum = args[2]

	// Query chaincode_example02
	f := "query"
	queryArgs := []string{"a"}
	response, err := stub.QueryChaincode(chaincodeUrl, chaincodeVersion, 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, chaincodeVersion, 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:masterDev1985,项目名称:cc_fat,代码行数:56,代码来源:chaincode_example05.go


注:本文中的github.com/openblockchain/obc-peer/openchain/chaincode/shim.ChaincodeStub.QueryChaincode方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。