2
0

chaincode_meidi.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /*
  2. Copyright 33.cn Corp. 2017 All Rights Reserved.
  3. Chaincode for Meidi Corp.
  4. Chang history:
  5. 2017/4/12 Building successfully.
  6. 2017/4/11 init version.
  7. */
  8. package main
  9. import (
  10. "encoding/json"
  11. "errors"
  12. "fmt"
  13. "strconv"
  14. "time"
  15. "github.com/hyperledger/fabric/core/chaincode/shim"
  16. pb "github.com/hyperledger/fabric/protos/peer"
  17. )
  18. var transactionNo int = 0
  19. // SimpleChaincode example simple Chaincode implementation
  20. type SimpleChaincode struct {
  21. }
  22. type Bill struct {
  23. BillId string
  24. Maker string
  25. Acceptor string
  26. Receiver string //Receiver name
  27. IssueDate int64
  28. ExpireDate int64
  29. RecBank string //Name of receiver's Bank
  30. Amount int //Amount of money
  31. Type int //0:taels 1:Business
  32. Form int //0:paper 1:electronic
  33. Status int //0: 1: 2: 3: 4: 5:
  34. }
  35. // Record all operation include the create/change or the bill
  36. type Transaction struct {
  37. BillId string
  38. Operation string
  39. BillStatus int
  40. Time int64
  41. ID int
  42. }
  43. func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
  44. fmt.Println("Chaicode_Meidi Init")
  45. // Write the state to the ledger
  46. err := stub.PutState("MeiDiFabric", []byte("This is the block chain for MeiDiBill system..."))
  47. if err != nil {
  48. return shim.Error(err.Error())
  49. }
  50. return shim.Success(nil)
  51. }
  52. func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
  53. fmt.Println("ChainCode_Meidi Invoke")
  54. function, args := stub.GetFunctionAndParameters()
  55. if function == "createBill" {
  56. // Create the Bill
  57. return t.createBill(stub, args)
  58. } else if function == "changeBillStatus" {
  59. // Change the status of Bill
  60. return t.changeBillStatus(stub, args)
  61. } else if function == "queryBill" {
  62. //Query the Bill info from fabric
  63. return t.queryBill(stub, args)
  64. } else if function == "queryTransaction" {
  65. //Query the Bill info from fabric
  66. return t.queryTransaction(stub, args)
  67. }
  68. return shim.Error("Invalid invoke function name. Expecting \"createBill\" \"changeBillStaus\" \"queryBill\" \"queryTransaction\"")
  69. }
  70. // Deletes an entity from state
  71. func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  72. if len(args) != 1 {
  73. return shim.Error("Incorrect number of arguments. Expecting 1")
  74. }
  75. A := args[0]
  76. // Delete the key from the state in ledger
  77. err := stub.DelState(A)
  78. if err != nil {
  79. return shim.Error("Failed to delete state")
  80. }
  81. return shim.Success(nil)
  82. }
  83. func (t *SimpleChaincode) createBill(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  84. if len(args) != 11 {
  85. return shim.Error("createBill(): Incorrect number of arguments. Expecting 11")
  86. }
  87. var bill Bill
  88. var t_issue time.Time
  89. var t_expire time.Time
  90. var err error
  91. var amount int
  92. var billtype int
  93. var form int
  94. var status int
  95. var billBytes []byte
  96. layout := "2006-01-02 15:04:05"
  97. t_issue, err = time.Parse(layout, args[4])
  98. if err != nil {
  99. jsonResp := "{\"Error\":\"Failed to pharse the time, plese follow the fomat as" + layout + "\"}"
  100. return shim.Error(jsonResp)
  101. }
  102. t_expire, err = time.Parse(layout, args[5])
  103. if err != nil {
  104. jsonResp := "{\"Error\":\"Failed to pharse the time, plese follow the fomat " + layout + "\"}"
  105. return shim.Error(jsonResp)
  106. }
  107. amount, err = strconv.Atoi(args[7])
  108. if err != nil {
  109. return shim.Error("Expecting integer value for asset holding")
  110. }
  111. billtype, err = strconv.Atoi(args[8])
  112. if err != nil {
  113. return shim.Error("Expecting integer value for asset holding")
  114. }
  115. form, err = strconv.Atoi(args[9])
  116. if err != nil {
  117. return shim.Error("Expecting integer value for asset holding")
  118. }
  119. status, err = strconv.Atoi(args[10])
  120. if err != nil {
  121. return shim.Error("Expecting integer value for asset holding")
  122. }
  123. bill = Bill{BillId:args[0], Maker: args[1], Acceptor: args[2], Receiver: args[3], IssueDate: t_issue.Unix(), ExpireDate: t_expire.Unix(), RecBank: args[6], Amount: amount, Type: billtype, Form: form, Status: status}
  124. err = writeBill(stub, bill)
  125. if err != nil {
  126. return shim.Error("createBill(): Fail to write the bill" + err.Error())
  127. }
  128. billBytes, err = json.Marshal(&bill)
  129. if err != nil {
  130. return shim.Error("createBill():" + err.Error())
  131. }
  132. //Any operation of Bill will treat as transaction.
  133. transaction := Transaction{BillId: args[0], Operation: "Create", BillStatus: status, Time: time.Now().Unix(), ID: transactionNo }
  134. err = writeTransaction(stub, transaction)
  135. if err != nil {
  136. return shim.Error("createBill(): Fail to write the transaction" + err.Error())
  137. }
  138. transactionNo += 1
  139. return shim.Success(billBytes)
  140. }
  141. // Change the Bill status
  142. func (t *SimpleChaincode) changeBillStatus(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  143. if len(args) != 2 {
  144. return shim.Error("changeBillStatus(): Incorrect number of arguments. Expecting 2")
  145. }
  146. var bill Bill
  147. status, err := strconv.Atoi(args[1])
  148. if err != nil {
  149. return shim.Error("changeBillStatus(): status want Integer number")
  150. }
  151. bill,_,err = getBillById(stub, args[0])
  152. if err != nil {
  153. return shim.Error("changeBillStatus(): Fail to get Bill" + err.Error())
  154. }
  155. bill.Status = status
  156. err = writeBill(stub, bill)
  157. if err != nil {
  158. shim.Error("changeBillStatus():fail to write Bill" + err.Error())
  159. }
  160. transaction := Transaction{BillId: args[0], Operation: "changeStatus", BillStatus: status, Time: time.Now().Unix(), ID: transactionNo }
  161. err = writeTransaction(stub, transaction)
  162. if err != nil {
  163. return shim.Error("changeBillStatus(): Fail to write the transaction" + err.Error())
  164. }
  165. transactionNo += 1
  166. return shim.Success(nil)
  167. }
  168. // Query Bill info
  169. func (t *SimpleChaincode) queryBill(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  170. if len(args) != 1 {
  171. return shim.Error("queryBill(): Incorrect number of arguments. Expecting 1")
  172. }
  173. _,billBytes,err := getBillById(stub,args[0])
  174. if err != nil {
  175. shim.Error("queryBill(): Fail to get Bill" + err.Error())
  176. }
  177. return shim.Success(billBytes)
  178. }
  179. // Query Transaction info
  180. func (t *SimpleChaincode) queryTransaction(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  181. if len(args) != 1 {
  182. return shim.Error("queryTransaction(): Incorrect number of arguments. Expecting 1")
  183. }
  184. _,transBytes,err := getTransactionById(stub,args[0])
  185. if err != nil {
  186. return shim.Error("queryBill(): Fail to get Bill" + err.Error())
  187. }
  188. return shim.Success(transBytes)
  189. }
  190. // Write Bill info into the fabric
  191. func writeBill(stub shim.ChaincodeStubInterface, bill Bill) error {
  192. billBytes, err := json.Marshal(&bill)
  193. if err != nil {
  194. return errors.New("writeBill():" + err.Error())
  195. }
  196. err = stub.PutState("bill"+bill.BillId, billBytes)
  197. if err != nil {
  198. return errors.New("writeBill(): PutState Error" + err.Error())
  199. }
  200. return nil
  201. }
  202. //Write the transaction info into the Fabric
  203. func writeTransaction(stub shim.ChaincodeStubInterface, transaction Transaction) error {
  204. var tsId string
  205. tsBytes, err := json.Marshal(&transaction)
  206. if err != nil {
  207. return errors.New("writeTransaction():" + err.Error())
  208. }
  209. tsId = strconv.Itoa(transaction.ID)
  210. if err != nil {
  211. return errors.New("writeTransaction(): want Integer number")
  212. }
  213. err = stub.PutState("transaction"+tsId, tsBytes)
  214. if err != nil {
  215. return errors.New("writeTransaction(): PutState Error" + err.Error())
  216. }
  217. return nil
  218. }
  219. func getBillById(stub shim.ChaincodeStubInterface, id string) (Bill, []byte, error) {
  220. var bill Bill
  221. billBytes, err := stub.GetState("bill" + id)
  222. if err != nil {
  223. fmt.Println("Error retrieving cpBytes")
  224. }
  225. err = json.Unmarshal(billBytes, &bill)
  226. if err != nil {
  227. fmt.Println("Error unmarshalling Bill")
  228. }
  229. return bill, billBytes, nil
  230. }
  231. func getTransactionById(stub shim.ChaincodeStubInterface, id string) (Transaction, []byte, error) {
  232. var transaction Transaction
  233. transBytes, err := stub.GetState("transaction" + id)
  234. if err != nil {
  235. fmt.Println("Error retrieving transBytes")
  236. }
  237. err = json.Unmarshal(transBytes, &transaction)
  238. if err != nil {
  239. fmt.Println("Error unmarshalling transaction")
  240. }
  241. return transaction, transBytes, nil
  242. }
  243. func main() {
  244. err := shim.Start(new(SimpleChaincode))
  245. if err != nil {
  246. fmt.Printf("Error starting Simple chaincode: %s", err)
  247. }
  248. }