github.com/yacovm/fabric@v2.0.0-alpha.0.20191128145320-c5d4087dc723+incompatible/integration/chaincode/simple/chaincode.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package simple 8 9 import ( 10 "fmt" 11 "strconv" 12 13 "github.com/hyperledger/fabric-chaincode-go/shim" 14 pb "github.com/hyperledger/fabric-protos-go/peer" 15 ) 16 17 // SimpleChaincode example simple Chaincode implementation 18 type SimpleChaincode struct { 19 } 20 21 func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response { 22 fmt.Println("Init invoked") 23 _, args := stub.GetFunctionAndParameters() 24 var A, B string // Entities 25 var Aval, Bval int // Asset holdings 26 var err error 27 28 if len(args) != 4 { 29 return shim.Error("Incorrect number of arguments. Expecting 4") 30 } 31 32 // Initialize the chaincode 33 A = args[0] 34 Aval, err = strconv.Atoi(args[1]) 35 if err != nil { 36 return shim.Error("Expecting integer value for asset holding") 37 } 38 B = args[2] 39 Bval, err = strconv.Atoi(args[3]) 40 if err != nil { 41 return shim.Error("Expecting integer value for asset holding") 42 } 43 fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval) 44 45 // Write the state to the ledger 46 err = stub.PutState(A, []byte(strconv.Itoa(Aval))) 47 if err != nil { 48 return shim.Error(err.Error()) 49 } 50 51 err = stub.PutState(B, []byte(strconv.Itoa(Bval))) 52 if err != nil { 53 return shim.Error(err.Error()) 54 } 55 56 fmt.Println("Init returning with success") 57 return shim.Success(nil) 58 } 59 60 func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response { 61 fmt.Println("ex02 Invoke") 62 function, args := stub.GetFunctionAndParameters() 63 switch function { 64 case "invoke": 65 // Make payment of X units from A to B 66 return t.invoke(stub, args) 67 case "delete": 68 // Deletes an entity from its state 69 return t.delete(stub, args) 70 case "query": 71 // the old "Query" is now implemtned in invoke 72 return t.query(stub, args) 73 case "respond": 74 // return with an error 75 return t.respond(stub, args) 76 default: 77 return shim.Error(`Invalid invoke function name. Expecting "invoke", "delete", "query", or "respond"`) 78 } 79 } 80 81 // Transaction makes payment of X units from A to B 82 func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response { 83 var A, B string // Entities 84 var Aval, Bval int // Asset holdings 85 var X int // Transaction value 86 var err error 87 88 if len(args) != 3 { 89 return shim.Error("Incorrect number of arguments. Expecting 3") 90 } 91 92 A = args[0] 93 B = args[1] 94 95 // Get the state from the ledger 96 // TODO: will be nice to have a GetAllState call to ledger 97 Avalbytes, err := stub.GetState(A) 98 if err != nil { 99 return shim.Error("Failed to get state") 100 } 101 if Avalbytes == nil { 102 return shim.Error("Entity not found") 103 } 104 Aval, _ = strconv.Atoi(string(Avalbytes)) 105 106 Bvalbytes, err := stub.GetState(B) 107 if err != nil { 108 return shim.Error("Failed to get state") 109 } 110 if Bvalbytes == nil { 111 return shim.Error("Entity not found") 112 } 113 Bval, _ = strconv.Atoi(string(Bvalbytes)) 114 115 // Perform the execution 116 X, err = strconv.Atoi(args[2]) 117 if err != nil { 118 return shim.Error("Invalid transaction amount, expecting a integer value") 119 } 120 Aval = Aval - X 121 Bval = Bval + X 122 fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval) 123 124 // Write the state back to the ledger 125 err = stub.PutState(A, []byte(strconv.Itoa(Aval))) 126 if err != nil { 127 return shim.Error(err.Error()) 128 } 129 130 err = stub.PutState(B, []byte(strconv.Itoa(Bval))) 131 if err != nil { 132 return shim.Error(err.Error()) 133 } 134 135 return shim.Success(nil) 136 } 137 138 // Deletes an entity from state 139 func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string) pb.Response { 140 if len(args) != 1 { 141 return shim.Error("Incorrect number of arguments. Expecting 1") 142 } 143 144 A := args[0] 145 146 // Delete the key from the state in ledger 147 err := stub.DelState(A) 148 if err != nil { 149 return shim.Error("Failed to delete state") 150 } 151 152 return shim.Success(nil) 153 } 154 155 // query callback representing the query of a chaincode 156 func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response { 157 var A string // Entities 158 var err error 159 160 if len(args) != 1 { 161 return shim.Error("Incorrect number of arguments. Expecting name of the person to query") 162 } 163 164 A = args[0] 165 166 // Get the state from the ledger 167 Avalbytes, err := stub.GetState(A) 168 if err != nil { 169 jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}" 170 return shim.Error(jsonResp) 171 } 172 173 if Avalbytes == nil { 174 jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}" 175 return shim.Error(jsonResp) 176 } 177 178 jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}" 179 fmt.Printf("Query Response:%s\n", jsonResp) 180 return shim.Success(Avalbytes) 181 } 182 183 // respond simply generates a response payload from the args 184 func (t *SimpleChaincode) respond(stub shim.ChaincodeStubInterface, args []string) pb.Response { 185 if len(args) != 3 { 186 return shim.Error("expected three arguments") 187 } 188 189 status, err := strconv.ParseInt(args[0], 10, 32) 190 if err != nil { 191 return shim.Error(err.Error()) 192 } 193 message := args[1] 194 payload := []byte(args[2]) 195 196 return pb.Response{ 197 Status: int32(status), 198 Message: message, 199 Payload: payload, 200 } 201 }