github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/examples/chaincode/go/chaincode_example05/chaincode_example05.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "fmt" 21 "strconv" 22 23 "github.com/hyperledger/fabric/common/util" 24 "github.com/hyperledger/fabric/core/chaincode/shim" 25 pb "github.com/hyperledger/fabric/protos/peer" 26 ) 27 28 // This chaincode is a test for chaincode querying another chaincode - invokes chaincode_example02 and computes the sum of a and b and stores it as state 29 30 // SimpleChaincode example simple Chaincode implementation 31 type SimpleChaincode struct { 32 } 33 34 // Init takes two arguments, a string and int. The string will be a key with 35 // the int as a value. 36 func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response { 37 var sum string // Sum of asset holdings across accounts. Initially 0 38 var sumVal int // Sum of holdings 39 var err error 40 _, args := stub.GetFunctionAndParameters() 41 if len(args) != 2 { 42 return shim.Error("Incorrect number of arguments. Expecting 2") 43 } 44 45 // Initialize the chaincode 46 sum = args[0] 47 sumVal, err = strconv.Atoi(args[1]) 48 if err != nil { 49 return shim.Error("Expecting integer value for sum") 50 } 51 fmt.Printf("sumVal = %d\n", sumVal) 52 53 // Write the state to the ledger 54 err = stub.PutState(sum, []byte(strconv.Itoa(sumVal))) 55 if err != nil { 56 return shim.Error(err.Error()) 57 } 58 59 return shim.Success(nil) 60 } 61 62 // Invoke queries another chaincode and updates its own state 63 func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response { 64 var sum string // Sum entity 65 var Aval, Bval, sumVal int // value of sum entity - to be computed 66 var err error 67 68 if len(args) != 2 { 69 return shim.Error("Incorrect number of arguments. Expecting 2") 70 } 71 72 chaincodeURL := args[0] // Expecting "github.com/hyperledger/fabric/core/example/chaincode/chaincode_example02" 73 sum = args[1] 74 75 // Query chaincode_example02 76 f := "query" 77 queryArgs := util.ToChaincodeArgs(f, "a") 78 response := stub.InvokeChaincode(chaincodeURL, queryArgs, "") 79 if response.Status != shim.OK { 80 errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", response.Payload) 81 fmt.Printf(errStr) 82 return shim.Error(errStr) 83 } 84 Aval, err = strconv.Atoi(string(response.Payload)) 85 if err != nil { 86 errStr := fmt.Sprintf("Error retrieving state from ledger for queried chaincode: %s", err.Error()) 87 fmt.Printf(errStr) 88 return shim.Error(errStr) 89 } 90 91 queryArgs = util.ToChaincodeArgs(f, "b") 92 response = stub.InvokeChaincode(chaincodeURL, queryArgs, "") 93 if response.Status != shim.OK { 94 errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", response.Payload) 95 fmt.Printf(errStr) 96 return shim.Error(errStr) 97 } 98 Bval, err = strconv.Atoi(string(response.Payload)) 99 if err != nil { 100 errStr := fmt.Sprintf("Error retrieving state from ledger for queried chaincode: %s", err.Error()) 101 fmt.Printf(errStr) 102 return shim.Error(errStr) 103 } 104 105 // Compute sum 106 sumVal = Aval + Bval 107 108 // Write sumVal back to the ledger 109 err = stub.PutState(sum, []byte(strconv.Itoa(sumVal))) 110 if err != nil { 111 return shim.Error(err.Error()) 112 } 113 114 fmt.Printf("Invoke chaincode successful. Got sum %d\n", sumVal) 115 return shim.Success([]byte(strconv.Itoa(sumVal))) 116 } 117 118 func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response { 119 var sum string // Sum entity 120 var Aval, Bval, sumVal int // value of sum entity - to be computed 121 var err error 122 123 if len(args) != 2 { 124 return shim.Error("Incorrect number of arguments. Expecting 2") 125 } 126 127 chaincodeURL := args[0] 128 sum = args[1] 129 130 // Query chaincode_example02 131 f := "query" 132 queryArgs := util.ToChaincodeArgs(f, "a") 133 response := stub.InvokeChaincode(chaincodeURL, queryArgs, "") 134 if response.Status != shim.OK { 135 errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", response.Payload) 136 fmt.Printf(errStr) 137 return shim.Error(errStr) 138 } 139 Aval, err = strconv.Atoi(string(response.Payload)) 140 if err != nil { 141 errStr := fmt.Sprintf("Error retrieving state from ledger for queried chaincode: %s", err.Error()) 142 fmt.Printf(errStr) 143 return shim.Error(errStr) 144 } 145 146 queryArgs = util.ToChaincodeArgs(f, "b") 147 response = stub.InvokeChaincode(chaincodeURL, queryArgs, "") 148 if response.Status != shim.OK { 149 errStr := fmt.Sprintf("Failed to query chaincode. Got error: %s", response.Payload) 150 fmt.Printf(errStr) 151 return shim.Error(errStr) 152 } 153 Bval, err = strconv.Atoi(string(response.Payload)) 154 if err != nil { 155 errStr := fmt.Sprintf("Error retrieving state from ledger for queried chaincode: %s", err.Error()) 156 fmt.Printf(errStr) 157 return shim.Error(errStr) 158 } 159 160 // Compute sum 161 sumVal = Aval + Bval 162 163 fmt.Printf("Query chaincode successful. Got sum %d\n", sumVal) 164 jsonResp := "{\"Name\":\"" + sum + "\",\"Value\":\"" + strconv.Itoa(sumVal) + "\"}" 165 fmt.Printf("Query Response:%s\n", jsonResp) 166 return shim.Success([]byte(strconv.Itoa(sumVal))) 167 } 168 169 func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response { 170 function, args := stub.GetFunctionAndParameters() 171 if function == "invoke" { 172 return t.invoke(stub, args) 173 } else if function == "query" { 174 return t.query(stub, args) 175 } 176 177 return shim.Success([]byte("Invalid invoke function name. Expecting \"invoke\" \"query\"")) 178 } 179 180 func main() { 181 err := shim.Start(new(SimpleChaincode)) 182 if err != nil { 183 fmt.Printf("Error starting Simple chaincode: %s", err) 184 } 185 }