// 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 }
// 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 }
//============================================================================================================================== // 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 }
//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) }