chaincode_example02.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*
  2. Copyright IBM Corp. 2016 All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. //WARNING - this chaincode's ID is hard-coded in chaincode_example04 to illustrate one way of
  15. //calling chaincode from a chaincode. If this example is modified, chaincode_example04.go has
  16. //to be modified as well with the new ID of chaincode_example02.
  17. //chaincode_example05 show's how chaincode ID can be passed in as a parameter instead of
  18. //hard-coding.
  19. import (
  20. "fmt"
  21. "strconv"
  22. "github.com/hyperledger/fabric/core/chaincode/shim"
  23. pb "github.com/hyperledger/fabric/protos/peer"
  24. )
  25. // SimpleChaincode example simple Chaincode implementation
  26. type SimpleChaincode struct {
  27. }
  28. func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
  29. fmt.Println("ex02 Init")
  30. _, args := stub.GetFunctionAndParameters()
  31. var A, B string // Entities
  32. var Aval, Bval int // Asset holdings
  33. var err error
  34. if len(args) != 4 {
  35. return shim.Error("Incorrect number of arguments. Expecting 4")
  36. }
  37. // Initialize the chaincode
  38. A = args[0]
  39. Aval, err = strconv.Atoi(args[1])
  40. if err != nil {
  41. return shim.Error("Expecting integer value for asset holding")
  42. }
  43. B = args[2]
  44. Bval, err = strconv.Atoi(args[3])
  45. if err != nil {
  46. return shim.Error("Expecting integer value for asset holding")
  47. }
  48. fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)
  49. // Write the state to the ledger
  50. err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
  51. if err != nil {
  52. return shim.Error(err.Error())
  53. }
  54. err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
  55. if err != nil {
  56. return shim.Error(err.Error())
  57. }
  58. return shim.Success(nil)
  59. }
  60. func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
  61. fmt.Println("ex02 Invoke")
  62. function, args := stub.GetFunctionAndParameters()
  63. if function == "invoke" {
  64. // Make payment of X units from A to B
  65. return t.invoke(stub, args)
  66. } else if function == "delete" {
  67. // Deletes an entity from its state
  68. return t.delete(stub, args)
  69. } else if function == "query" {
  70. // the old "Query" is now implemtned in invoke
  71. return t.query(stub, args)
  72. }
  73. return shim.Error("Invalid invoke function name. Expecting \"invoke\" \"delete\" \"query\"")
  74. }
  75. // Transaction makes payment of X units from A to B
  76. func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  77. var A, B string // Entities
  78. var Aval, Bval int // Asset holdings
  79. var X int // Transaction value
  80. var err error
  81. if len(args) != 3 {
  82. return shim.Error("Incorrect number of arguments. Expecting 3")
  83. }
  84. A = args[0]
  85. B = args[1]
  86. // Get the state from the ledger
  87. // TODO: will be nice to have a GetAllState call to ledger
  88. Avalbytes, err := stub.GetState(A)
  89. if err != nil {
  90. return shim.Error("Failed to get state")
  91. }
  92. if Avalbytes == nil {
  93. return shim.Error("Entity not found")
  94. }
  95. Aval, _ = strconv.Atoi(string(Avalbytes))
  96. Bvalbytes, err := stub.GetState(B)
  97. if err != nil {
  98. return shim.Error("Failed to get state")
  99. }
  100. if Bvalbytes == nil {
  101. return shim.Error("Entity not found")
  102. }
  103. Bval, _ = strconv.Atoi(string(Bvalbytes))
  104. // Perform the execution
  105. X, err = strconv.Atoi(args[2])
  106. if err != nil {
  107. return shim.Error("Invalid transaction amount, expecting a integer value")
  108. }
  109. Aval = Aval - X
  110. Bval = Bval + X
  111. fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)
  112. // Write the state back to the ledger
  113. err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
  114. if err != nil {
  115. return shim.Error(err.Error())
  116. }
  117. err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
  118. if err != nil {
  119. return shim.Error(err.Error())
  120. }
  121. return shim.Success(nil)
  122. }
  123. // Deletes an entity from state
  124. func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  125. if len(args) != 1 {
  126. return shim.Error("Incorrect number of arguments. Expecting 1")
  127. }
  128. A := args[0]
  129. // Delete the key from the state in ledger
  130. err := stub.DelState(A)
  131. if err != nil {
  132. return shim.Error("Failed to delete state")
  133. }
  134. return shim.Success(nil)
  135. }
  136. // query callback representing the query of a chaincode
  137. func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  138. var A string // Entities
  139. var err error
  140. if len(args) != 1 {
  141. return shim.Error("Incorrect number of arguments. Expecting name of the person to query")
  142. }
  143. A = args[0]
  144. // Get the state from the ledger
  145. Avalbytes, err := stub.GetState(A)
  146. if err != nil {
  147. jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}"
  148. return shim.Error(jsonResp)
  149. }
  150. if Avalbytes == nil {
  151. jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}"
  152. return shim.Error(jsonResp)
  153. }
  154. jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
  155. fmt.Printf("Query Response:%s\n", jsonResp)
  156. return shim.Success(Avalbytes)
  157. }
  158. func main() {
  159. err := shim.Start(new(SimpleChaincode))
  160. if err != nil {
  161. fmt.Printf("Error starting Simple chaincode: %s", err)
  162. }
  163. }