Guest User

Untitled

a guest
Nov 24th, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.27 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "encoding/json"
  5. "fmt"
  6. "strconv"
  7. "crypto/md5"
  8. "crypto/rand"
  9. "encoding/base64"
  10. "encoding/hex"
  11. "io"
  12. "github.com/hyperledger/fabric/core/chaincode/shim"
  13. pb "github.com/hyperledger/fabric/protos/peer"
  14. )
  15.  
  16. /*
  17. 模拟实现招投标业务场景,一个公司组织机构对应多个诚信记录
  18.  
  19. */
  20.  
  21. type SimpleChaincode struct {
  22. }
  23.  
  24. type Company struct {
  25. Companyid string
  26. Companyname string //公司名称
  27. Tell string //公司电话
  28. Addr string //公司地址
  29. Companymoneryid string //公司税号
  30. Tranid string //公司交易地址
  31. Credit int
  32. }
  33.  
  34. type Trecord struct {
  35. Tranid string //交易记录地址
  36. Tname string //交易项目名称
  37. Ttime string //交易时间
  38. Tcos int //交易信用值增值
  39. }
  40.  
  41. func GetAddress() (string, string, string) {
  42. var address, priKey, pubKey string
  43. b := make([]byte, 48)
  44.  
  45. if _, err := io.ReadFull(rand.Reader, b); err != nil {
  46. return "", "", ""
  47. }
  48.  
  49. h := md5.New()
  50. h.Write([]byte(base64.URLEncoding.EncodeToString(b)))
  51.  
  52. address = hex.EncodeToString(h.Sum(nil))
  53. priKey = address + "1"
  54. pubKey = address + "2"
  55.  
  56. return address, priKey, pubKey
  57. }
  58.  
  59.  
  60. /*
  61. 智能合约初始化调用,一个链码版本只运行一次
  62.  
  63. */
  64. func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
  65. fmt.Println("Init ChainCode...............")
  66. return shim.Success(nil)
  67. }
  68.  
  69. /*
  70. 智能合约发起请求时调用 {"Args":["invoke","a","b","10"]}
  71. func (stub *ChaincodeStub) GetFunctionAndParameters() (function string, params []string)
  72. 返回参数 function 标记调用方法的名称"invoke"
  73. 返回参数 params 标记调用方法的参数数组["a","b","10"]
  74. */
  75.  
  76. func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
  77. // 获取请求调用智能合约的方法和参数
  78. function, args := stub.GetFunctionAndParameters()
  79. fmt.Println("recive params....",function," ",args)
  80. // Route to the appropriate handler function to interact with the ledger appropriately
  81. if function == "addcompany" {
  82. //新注册公司
  83. fmt.Println("run function addcompany",function," ",args)
  84. return t.addcompany(stub, args)
  85. } else if function == "updatecompany" {
  86. //更新公司交易信息
  87. fmt.Println("run function updatecompany",function," ",args)
  88. return t.updatecompany(stub, args)
  89. } else if function == "addtrans" {
  90. //给定公司添加交易信息
  91. fmt.Println("run function deletecard",function," ",args)
  92. return t.addtrans(stub, args)
  93. } else if function == "querycompany" {
  94. //查询公司注册信息
  95. fmt.Println("run function query",function," ",args)
  96. return t.querycompany(stub, args)
  97. } else if function == "querytranshistory" {
  98. //查询公司历史交易信息
  99. fmt.Println("run function queryhistory",function," ",args)
  100. return t.querytranshistory(stub, args)
  101. }
  102. return shim.Success(nil)
  103. }
  104.  
  105. /*
  106. 向链码账本中添加一个新的公司信息
  107. */
  108. func (t *SimpleChaincode) addcompany(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  109. fmt.Println("向账本中添加注册公司......")
  110. tranaddr,_,_:=GetAddress()
  111. var pertmp Company
  112. credit,_:=strconv.Atoi(args[5])
  113. pertmp = Company{Companyid: args[0],Companyname: args[1], Tell: args[2], Addr: args[3], Companymoneryid: args[4], Tranid: tranaddr, Credit: credit}
  114. fmt.Println("Record is ",pertmp)
  115. jsonrecode,_ := json.Marshal(&pertmp)
  116. fmt.Println("Record byte is :",jsonrecode)
  117. err:= stub.PutState(pertmp.Companyid, jsonrecode)
  118. fmt.Println("****************************")
  119. fmt.Println("put key is ", pertmp.Companyid)
  120. if err != nil {
  121. return shim.Error("Error retrieving data")
  122. }
  123. return shim.Success(jsonrecode)
  124. }
  125.  
  126. /*
  127. 向链码中已经存在的记录做一次更新操作
  128.  
  129. */
  130. func (t *SimpleChaincode) updatecompany(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  131. fmt.Println("向账本中更新注册公司......")
  132. tranaddr,_,_:=GetAddress()
  133. var pertmp Company
  134. credit,_:=strconv.Atoi(args[5])
  135. pertmp = Company{Companyid: args[0],Companyname: args[1], Tell: args[2], Addr: args[3], Companymoneryid: args[4], Tranid: tranaddr, Credit: credit}
  136. fmt.Println("Record is ",pertmp)
  137. jsonrecode,_ := json.Marshal(&pertmp)
  138. fmt.Println("Record byte is :",jsonrecode)
  139. err:= stub.PutState(pertmp.Companyid, jsonrecode)
  140. fmt.Println("****************************")
  141. fmt.Println("put key is ", pertmp.Companyid)
  142. if err != nil {
  143. return shim.Error("Error retrieving data")
  144. }
  145. return shim.Success(jsonrecode)
  146. }
  147.  
  148. /*
  149. 根据给定的ID信息更新最新的交易信息
  150. */
  151. func (t *SimpleChaincode) addtrans(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  152. fmt.Println("给公司添加交易信息")
  153. companyid:=args[0]
  154. tname:=args[1]
  155. ttime:=args[2]
  156. tcos,_:=strconv.Atoi(args[3])
  157. companyjson,_:=stub.GetState(companyid)
  158. var tmpcompany Company
  159. err:=json.Unmarshal(companyjson,&tmpcompany)
  160. tmpcompany.Credit=tmpcompany.Credit-tcos
  161. transrecord:=Trecord{Tranid:tmpcompany.Tranid,Tname:tname,Ttime:ttime,Tcos:tcos}
  162. companyjson,_= json.Marshal(&tmpcompany)
  163. transrecordjson,_:= json.Marshal(&transrecord)
  164. err=stub.PutState(tmpcompany.Companyid,companyjson)
  165. err=stub.PutState(tmpcompany.Tranid,transrecordjson)
  166. if err != nil {
  167. return shim.Error("Error retrieving data")
  168. }
  169. fmt.Println("添加交易信息成功")
  170. return shim.Success(companyjson)
  171. }
  172.  
  173. /*
  174. 查询链码中注册公司信息
  175. */
  176. func (t *SimpleChaincode) querycompany(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  177. queryparams:=args[0]
  178. fmt.Println("query record by perid",queryparams)
  179. redBytes, err := stub.GetState(queryparams)
  180. if err != nil {
  181. shim.Error("Error retrieving data")
  182. }
  183. fmt.Println("##############################")
  184. fmt.Println("query resoult"+string(redBytes))
  185. fmt.Println("##############################")
  186. return shim.Success(redBytes)
  187. }
  188. /*
  189. 根绝给定的companyid查询出对应的所有的交易历史记录
  190.  
  191. */
  192. func (t *SimpleChaincode) querytranshistory(stub shim.ChaincodeStubInterface, args []string) pb.Response {
  193. var companyid string=args[0]
  194. var records []Trecord
  195. var company Company
  196. fmt.Println("query key histroy......................",args[0])
  197. tmpcompany, err := stub.GetState(companyid)
  198. err=json.Unmarshal(tmpcompany,&company)
  199. fmt.Println(company.Companyname,"对应的交易ID为:",company.Tranid)
  200. historyIer, err := stub.GetHistoryForKey(company.Tranid)
  201. if err != nil {
  202. fmt.Println(err)
  203. return shim.Error("query error")
  204. }
  205. for historyIer.HasNext() {
  206. var tmp Trecord=Trecord{}
  207. modification, err := historyIer.Next()
  208. if err != nil {
  209. fmt.Println(err)
  210. return shim.Error("query error")
  211. }
  212. fmt.Println("Returning information about", string(modification.Value))
  213. err=json.Unmarshal(modification.Value,&tmp)
  214. records=append(records,tmp)
  215. }
  216. jsonrecode,_ := json.Marshal(&records)
  217. return shim.Success(jsonrecode)
  218. }
  219. func main() {
  220. fmt.Println("Fabric Chain Code Test..........")
  221. err := shim.Start(new(SimpleChaincode))
  222. if err != nil {
  223. fmt.Printf("Error starting Simple chaincode: %s", err)
  224. }
  225. }
Add Comment
Please, Sign In to add comment