chaincode_example02.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. import (
  15. "fmt"
  16. "strconv"
  17. "github.com/hyperledger/fabric/core/chaincode/shim"
  18. pb "github.com/hyperledger/fabric/protos/peer"
  19. )
  20. // SimpleChaincode example simple Chaincode implementation
  21. type SimpleChaincode struct {
  22. }
  23. func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
  24. fmt.Println("ex02 Init")
  25. _, args := stub.GetFunctionAndParameters()
  26. var A, B string // Entities
  27. var Aval, Bval int // Asset holdings
  28. var err error
  29. if len(args) != 4 {
  30. return shim.Error("Incorrect number of arguments. Expecting 4")
  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. // Write the state to the ledger
  45. err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
  46. if err != nil {
  47. return shim.Error(err.Error())
  48. }
  49. err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
  50. if err != nil {
  51. return shim.Error(err.Error())
  52. }
  53. return shim.Success(nil)
  54. }
  55. func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
  56. fmt.Println("ex02 Invoke")
  57. function, args := stub.GetFunctionAndParameters()
  58. if function == "invoke" {
  59. // Make payment of X units from A to B
  60. return t.invoke(stub, args)
  61. } else if function == "delete" {
  62. // Deletes an entity from its state
  63. return t.delete(stub, args)
  64. } else if function == "query" {
  65. // the old "Query" is now implemtned in invoke
  66. return t.query(stub, args)
  67. }
  68. return shim.Error("Invalid invoke function name. Expecting \"invoke\" \"delete\" \"query\"")
  69. }
  70. // Transaction makes payment of X units from A to B
  71. func (t *SimpleChaincode) invoke(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  72. var A, B string // Entities
  73. var Aval, Bval int // Asset holdings
  74. var X int // Transaction value
  75. var err error
  76. if len(args) != 3 {
  77. return shim.Error("Incorrect number of arguments. Expecting 3")
  78. }
  79. A = args[0]
  80. B = args[1]
  81. // Get the state from the ledger
  82. // TODO: will be nice to have a GetAllState call to ledger
  83. Avalbytes, err := stub.GetState(A)
  84. if err != nil {
  85. return shim.Error("Failed to get state")
  86. }
  87. if Avalbytes == nil {
  88. return shim.Error("Entity not found")
  89. }
  90. Aval, _ = strconv.Atoi(string(Avalbytes))
  91. Bvalbytes, err := stub.GetState(B)
  92. if err != nil {
  93. return shim.Error("Failed to get state")
  94. }
  95. if Bvalbytes == nil {
  96. return shim.Error("Entity not found")
  97. }
  98. Bval, _ = strconv.Atoi(string(Bvalbytes))
  99. // Perform the execution
  100. X, err = strconv.Atoi(args[2])
  101. if err != nil {
  102. return shim.Error("Invalid transaction amount, expecting a integer value")
  103. }
  104. Aval = Aval - X
  105. Bval = Bval + X
  106. fmt.Printf("Aval = %d, Bval = %d\n", Aval, Bval)
  107. // Write the state back to the ledger
  108. err = stub.PutState(A, []byte(strconv.Itoa(Aval)))
  109. if err != nil {
  110. return shim.Error(err.Error())
  111. }
  112. err = stub.PutState(B, []byte(strconv.Itoa(Bval)))
  113. if err != nil {
  114. return shim.Error(err.Error())
  115. }
  116. return shim.Success(nil)
  117. }
  118. // Deletes an entity from state
  119. func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  120. if len(args) != 1 {
  121. return shim.Error("Incorrect number of arguments. Expecting 1")
  122. }
  123. A := args[0]
  124. // Delete the key from the state in ledger
  125. err := stub.DelState(A)
  126. if err != nil {
  127. return shim.Error("Failed to delete state")
  128. }
  129. return shim.Success(nil)
  130. }
  131. // query callback representing the query of a chaincode
  132. func (t *SimpleChaincode) query(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  133. var A string // Entities
  134. var err error
  135. if len(args) != 1 {
  136. return shim.Error("Incorrect number of arguments. Expecting name of the person to query")
  137. }
  138. A = args[0]
  139. // Get the state from the ledger
  140. Avalbytes, err := stub.GetState(A)
  141. if err != nil {
  142. jsonResp := "{\"Error\":\"Failed to get state for " + A + "\"}"
  143. return shim.Error(jsonResp)
  144. }
  145. if Avalbytes == nil {
  146. jsonResp := "{\"Error\":\"Nil amount for " + A + "\"}"
  147. return shim.Error(jsonResp)
  148. }
  149. jsonResp := "{\"Name\":\"" + A + "\",\"Amount\":\"" + string(Avalbytes) + "\"}"
  150. fmt.Printf("Query Response:%s\n", jsonResp)
  151. return shim.Success(Avalbytes)
  152. }
  153. func main() {
  154. err := shim.Start(new(SimpleChaincode))
  155. if err != nil {
  156. fmt.Printf("Error starting Simple chaincode: %s", err)
  157. }
  158. }