github.com/kaituanwang/hyperledger@v2.0.1+incompatible/core/chaincode/testdata/src/chaincodes/example02/chaincode.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package main 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("ex02 Init") 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 return shim.Success(nil) 57 } 58 59 func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response { 60 fmt.Println("ex02 Invoke") 61 function, args := stub.GetFunctionAndParameters() 62 if function == "invoke" { 63 // Make payment of X units from A to B 64 return t.invoke(stub, args) 65 } else if function == "delete" { 66 // Deletes an entity from its state 67 return t.delete(stub, args) 68 } else if function == "query" { 69 // the old "Query" is now implemtned in invoke 70 return t.query(stub, args) 71 } 72 73 return shim.Error("Invalid invoke function name. Expecting \"invoke\" \"delete\" \"query\"") 74 } 75 76 // Transaction makes payment of X units from A to B 77 func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response { 78 var A, B string // Entities 79 var Aval, Bval int // Asset holdings 80 var X int // Transaction value 81 var err error 82 83 if len(args) != 3 { 84 return shim.Error("Incorrect number of arguments. Expecting 3") 85 } 86 87 A = args[0] 88 B = args[1] 89 90 // Get the state from the ledger 91 // TODO: will be nice to have a GetAllState call to ledger 92 Avalbytes, err := stub.GetState(A) 93 if err != nil { 94 return shim.Error("Failed to get state") 95 } 96 if Avalbytes == nil { 97 return shim.Error("Entity not found") 98 } 99 Aval, _ = strconv.Atoi(string(Avalbytes)) 100 101 Bvalbytes, err := stub.GetState(B) 102 if err != nil { 103 return shim.Error("Failed to get state") 104 } 105 if Bvalbytes == nil { 106 return shim.Error("Entity not found") 107 } 108 Bval, _ = strconv.Atoi(string(Bvalbytes)) 109 110 // Perform the execution 111 X, err = strconv.Atoi(args[2]) 112 if err != nil { 113 return shim.Error("Invalid transaction amount, expecting a integer value") 114 } 115 Aval = Aval - X 116 Bval = Bval + X 117 fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval) 118 119 // Write the state back to the ledger 120 err = stub.PutState(A, []byte(strconv.Itoa(Aval))) 121 if err != nil { 122 return shim.Error(err.Error()) 123 } 124 125 err = stub.PutState(B, []byte(strconv.Itoa(Bval))) 126 if err != nil { 127 return shim.Error(err.Error()) 128 } 129 130 return shim.Success(nil) 131 } 132 133 // Deletes an entity from state 134 func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string) pb.Response { 135 if len(args) != 1 { 136 return shim.Error("Incorrect number of arguments. Expecting 1") 137 } 138 139 A := args[0] 140 141 // Delete the key from the state in ledger 142 err := stub.DelState(A) 143 if err != nil { 144 return shim.Error("Failed to delete state") 145 } 146 147 return shim.Success(nil) 148 } 149 150 // query callback representing the query of a chaincode 151 func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response { 152 var A string // Entities 153 var err error 154 155 if len(args) != 1 { 156 return shim.Error("Incorrect number of arguments. Expecting name of the person to query") 157 } 158 159 A = args[0] 160 161 // Get the state from the ledger 162 Avalbytes, err := stub.GetState(A) 163 if err != nil { 164 jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}" 165 return shim.Error(jsonResp) 166 } 167 168 if Avalbytes == nil { 169 jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}" 170 return shim.Error(jsonResp) 171 } 172 173 jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}" 174 fmt.Printf("Query Response:%s\n", jsonResp) 175 return shim.Success(Avalbytes) 176 } 177 178 func main() { 179 err := shim.Start(new(SimpleChaincode)) 180 if err != nil { 181 fmt.Printf("Error starting Simple chaincode: %s", err) 182 } 183 }