chaincode_meidi.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 (
  19. transactionNo int = 0
  20. layout = "2006-01-02 15:04:05"
  21. loc *time.Location
  22. )
  23. func init() {
  24. loc, _ = time.LoadLocation("Asia/Shanghai")
  25. }
  26. // SimpleChaincode example simple Chaincode implementation
  27. type SimpleChaincode struct {
  28. }
  29. type Bill struct {
  30. Acceptor string `json:"acceptor"`
  31. AcceptorActno string `json:"acceptorActno"`
  32. AcceptorBankId int `json:"acceptorBankId"`
  33. AcceptorBankName string `json:"acceptorBankName"`
  34. CanTransfer string `json:"canTransfer"`
  35. DraftNumber string `json:"draftNumber"`
  36. DraftState string `json:"draftState"`
  37. DraftType string `json:"draftType"`
  38. FaceAmount int64 `json:"faceAmount"`
  39. MaturityDate string `json:"maturityDate"`
  40. PayeeAccount string `json:"payeeAccount"`
  41. PayeeBankName string `json:"payeeBankName"`
  42. PayeeName string `json:"payeeName"`
  43. RemitDate string `json:"remitDate"`
  44. RemitterAccount string `json:"remitterAccount"`
  45. RemitterBankName string `json:"remitterBankName"`
  46. RemitterName string `json:"remitterName"`
  47. CreateTime string `json:"createTime"`
  48. UpdateTime string `json:"updateTime"`
  49. }
  50. type BillFail struct {
  51. DraftNumber string `json:"draftNumber"`
  52. ErrInfo string `json:"errInfo"`
  53. }
  54. type BatchResult struct {
  55. SuccNum int `json:"succNum"`
  56. FailNum int `json:"failNum"`
  57. FailInfos []BillFail `json:"billFails"`
  58. }
  59. // Record all operation include the create/change or the bill
  60. type Transaction struct {
  61. BillId string
  62. Operation string
  63. BillStatus string
  64. Time int64
  65. ID int
  66. }
  67. func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
  68. return shim.Success(nil)
  69. }
  70. func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
  71. function, args := stub.GetFunctionAndParameters()
  72. if function == "createBill" {
  73. return t.createBill(stub, args)
  74. } else if function == "createBillBatch" {
  75. return t.createBillBatch(stub, args)
  76. } else if function == "changeBillStatus" {
  77. return t.changeBillStatus(stub, args)
  78. } else if function == "queryBill" {
  79. return t.queryBill(stub, args)
  80. } else if function == "queryTransaction" {
  81. return t.queryTransaction(stub, args)
  82. }
  83. return shim.Error("Invalid invoke function name. Expecting \"createBill\" \"changeBillStaus\" \"queryBill\" \"queryTransaction\"")
  84. }
  85. func (t *SimpleChaincode) createBill(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  86. if len(args) != 17 {
  87. fmt.Println("createBill: args numbers is:", len(args))
  88. return shim.Error("createBill(): Incorrect number of arguments. Expecting 11")
  89. }
  90. orbankId, err := strconv.Atoi(args[2])
  91. if err != nil {
  92. fmt.Println("createBill: args[2] is:", args[2])
  93. return shim.Error("Expecting integer value for asset holding")
  94. }
  95. amount, err := strconv.ParseInt(args[8], 10, 64)
  96. if err != nil {
  97. fmt.Println("createBill: args[8] is:", args[8])
  98. return shim.Error("Expecting integer value for asset holding")
  99. }
  100. datetime := time.Now().In(loc).Format(layout)
  101. bill := Bill{
  102. Acceptor: args[0],
  103. AcceptorActno: args[1],
  104. AcceptorBankId: orbankId,
  105. AcceptorBankName: args[3],
  106. CanTransfer: args[4],
  107. DraftNumber: args[5],
  108. DraftState: args[6],
  109. DraftType: args[7],
  110. FaceAmount: amount,
  111. MaturityDate: args[9],
  112. PayeeAccount: args[10],
  113. PayeeBankName: args[11],
  114. PayeeName: args[12],
  115. RemitDate: args[13],
  116. RemitterAccount: args[14],
  117. RemitterBankName: args[15],
  118. RemitterName: args[16],
  119. CreateTime: datetime,
  120. UpdateTime: datetime,
  121. }
  122. err = writeBill(stub, bill)
  123. if err != nil {
  124. fmt.Println("createBill: writeBill fail:", err.Error())
  125. return shim.Error("createBill(): Fail to write the bill" + err.Error())
  126. }
  127. return shim.Success(nil)
  128. }
  129. func (t *SimpleChaincode) createBillBatch(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  130. if len(args) != 1 {
  131. fmt.Println("createBillBatch: args numbers is:", len(args))
  132. return shim.Error("createBillBatch: Incorrect number of arguments. Expecting 11")
  133. }
  134. var billInfos []Bill
  135. err := json.Unmarshal([]byte(args[0]), &billInfos)
  136. if err != nil {
  137. fmt.Println("getBillById: Unmarshal fail:", err.Error())
  138. return shim.Error("createBillBatch(): json.Unmarshal fail:" + err.Error())
  139. }
  140. datetime := time.Now().In(loc).Format(layout)
  141. result := BatchResult{}
  142. for _, bill := range billInfos {
  143. bill.CreateTime = datetime
  144. bill.UpdateTime = datetime
  145. err = writeBill(stub, bill)
  146. if err != nil {
  147. b := BillFail{
  148. DraftNumber: bill.DraftNumber,
  149. ErrInfo: err.Error(),
  150. }
  151. result.FailInfos = append(result.FailInfos, b)
  152. result.FailNum++
  153. fmt.Println("createBillBatch: writeBill fail:", err.Error())
  154. } else {
  155. result.SuccNum++
  156. }
  157. }
  158. billBytes, err := json.Marshal(&result)
  159. if err != nil {
  160. fmt.Println("createBillBatch: Marshal fail:", err.Error())
  161. return shim.Error("createBillBatch(): json.Marshal fail:" + err.Error())
  162. }
  163. return shim.Success(billBytes)
  164. }
  165. // Change the Bill status
  166. func (t *SimpleChaincode) changeBillStatus(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  167. if len(args) != 2 {
  168. fmt.Println("changeBillStatus: args numbers is :", len(args))
  169. return shim.Error("changeBillStatus(): Incorrect number of arguments. Expecting 2")
  170. }
  171. /*
  172. var bill Bill
  173. status, err := strconv.Atoi(args[1])
  174. if err != nil {
  175. return shim.Error("changeBillStatus(): status want Integer number")
  176. }
  177. bill, _, err = getBillById(stub, args[0])
  178. if err != nil {
  179. return shim.Error("changeBillStatus(): Fail to get Bill" + err.Error())
  180. }
  181. bill.Status = status
  182. err = writeBill(stub, bill)
  183. if err != nil {
  184. shim.Error("changeBillStatus():fail to write Bill" + err.Error())
  185. }
  186. transaction := Transaction{BillId: args[0], Operation: "changeStatus", BillStatus: status, Time: time.Now().Unix(), ID: transactionNo}
  187. err = writeTransaction(stub, transaction)
  188. if err != nil {
  189. return shim.Error("changeBillStatus(): Fail to write the transaction" + err.Error())
  190. }
  191. transactionNo += 1
  192. */
  193. return shim.Success(nil)
  194. }
  195. // Query Bill info
  196. func (t *SimpleChaincode) queryBill(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  197. if len(args) != 1 {
  198. fmt.Println("queryBill: args numbers is :", len(args))
  199. return shim.Error("queryBill(): Incorrect number of arguments. Expecting 1")
  200. }
  201. _, billBytes, err := getBillById(stub, args[0])
  202. if err != nil {
  203. fmt.Println("getBillById fail:", err.Error())
  204. return shim.Error("queryBill(): Fail to get Bill" + err.Error())
  205. }
  206. return shim.Success(billBytes)
  207. }
  208. // Query Transaction info
  209. func (t *SimpleChaincode) queryTransaction(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  210. if len(args) != 1 {
  211. fmt.Println("queryTransaction args numbers is:", len(args))
  212. return shim.Error("queryTransaction(): Incorrect number of arguments. Expecting 1")
  213. }
  214. _, transBytes, err := getTransactionById(stub, args[0])
  215. if err != nil {
  216. fmt.Println("getTransactionById fail:", err.Error())
  217. return shim.Error("queryBill(): Fail to get Bill" + err.Error())
  218. }
  219. return shim.Success(transBytes)
  220. }
  221. // Write Bill info into the fabric
  222. func writeBill(stub shim.ChaincodeStubInterface, bill Bill) error {
  223. billBytes, err := json.Marshal(&bill)
  224. if err != nil {
  225. fmt.Println("writeBill: Marshal fail:", err.Error())
  226. return errors.New("writeBill():" + err.Error())
  227. }
  228. err = stub.PutState("Bill:"+bill.DraftNumber, billBytes)
  229. if err != nil {
  230. fmt.Println("writeBill: PutState fail:", err.Error())
  231. return errors.New("writeBill(): PutState Error" + err.Error())
  232. }
  233. return nil
  234. }
  235. //Write the transaction info into the Fabric
  236. func writeTransaction(stub shim.ChaincodeStubInterface, transaction Transaction) error {
  237. var tsId string
  238. tsBytes, err := json.Marshal(&transaction)
  239. if err != nil {
  240. fmt.Println("writeTransaction: Marshal fail:", err.Error())
  241. return errors.New("writeTransaction():" + err.Error())
  242. }
  243. tsId = strconv.Itoa(transaction.ID)
  244. if err != nil {
  245. fmt.Println("writeTransaction: Itoa fail:", err.Error())
  246. return errors.New("writeTransaction(): want Integer number")
  247. }
  248. err = stub.PutState("Transaction:"+tsId, tsBytes)
  249. if err != nil {
  250. fmt.Println("writeTransaction: PutState fail:", err.Error())
  251. return errors.New("writeTransaction(): PutState Error" + err.Error())
  252. }
  253. return nil
  254. }
  255. func getBillById(stub shim.ChaincodeStubInterface, id string) (Bill, []byte, error) {
  256. var bill Bill
  257. billBytes, err := stub.GetState("Bill:" + id)
  258. if err != nil {
  259. fmt.Println("getBillById: GetState fail:", err.Error())
  260. fmt.Println("Error retrieving cpBytes")
  261. }
  262. err = json.Unmarshal(billBytes, &bill)
  263. if err != nil {
  264. fmt.Println("getBillById: Unmarshal fail:", err.Error())
  265. fmt.Println("Error unmarshalling Bill")
  266. }
  267. return bill, billBytes, nil
  268. }
  269. func getTransactionById(stub shim.ChaincodeStubInterface, id string) (Transaction, []byte, error) {
  270. var transaction Transaction
  271. transBytes, err := stub.GetState("transaction:" + id)
  272. if err != nil {
  273. fmt.Println("Error retrieving transBytes")
  274. }
  275. err = json.Unmarshal(transBytes, &transaction)
  276. if err != nil {
  277. fmt.Println("Error unmarshalling transaction")
  278. }
  279. return transaction, transBytes, nil
  280. }
  281. func main() {
  282. err := shim.Start(new(SimpleChaincode))
  283. if err != nil {
  284. fmt.Printf("Error starting Simple chaincode: %s", err)
  285. }
  286. }