Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.17 KB | None | 0 0
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19.  
  20. /*
  21. * The sample smart contract for documentation topic:
  22. * Writing Your First Blockchain Application
  23. */
  24.  
  25. package main
  26.  
  27. /* Imports
  28. * 4 utility libraries for formatting, handling bytes, reading and writing JSON, and string manipulation
  29. * 2 specific Hyperledger Fabric specific libraries for Smart Contracts
  30. */
  31. import (
  32. "bytes"
  33. "encoding/json"
  34. "fmt"
  35. "strconv"
  36.  
  37. "github.com/hyperledger/fabric/core/chaincode/shim"
  38. sc "github.com/hyperledger/fabric/protos/peer"
  39. )
  40.  
  41. // Define the Smart Contract structure
  42. type SmartContract struct {
  43. }
  44.  
  45. type Car struct {
  46. Make string `json:"make"`
  47. Model string `json:"model"`
  48. Colour string `json:"colour"`
  49. Owner string `json:"owner"`l
  50. }
  51.  
  52. type User struct {
  53. User_id string `json:"user_id"`
  54. User_name string `json:"user_name"`
  55. Description_user string `json:"description_user"`
  56. User_Disponibility string `json:"user_disponibility"`
  57. Skills_user Skills `json:"skills_user"`
  58. Trainings_user Trainings `json:"trainings_user"`
  59. }
  60.  
  61.  
  62.  
  63. type Skills struct {
  64. Skill_id string `json:"skill_id"`
  65. Skill_Name string `json:"skill_name"`
  66. Skill_Level string `json:"skill_level"`
  67. }
  68.  
  69. type Trainings struct {
  70. Training_id string `json:"training_id"`
  71. Training_name string `json:"training_name"`
  72. }
  73.  
  74. // Define the car structure, with 4 properties. Structure tags are used by encoding/json library
  75. type Project struct {
  76. Title string `json:"title"`
  77. Description string `json:"description"`
  78. Startdate string `json:"startdate"`
  79. Enddate string `json:"enddate"`
  80. Skills_needed string `json:"skillsneeded"`
  81. Trainings_needed string `json:"trainingsneeded"`
  82. State string `json:"state"`
  83.  
  84. }
  85.  
  86.  
  87.  
  88. /*
  89. * The Init method is called when the Smart Contract "fabcar" is instantiated by the blockchain network
  90. * Best practice is to have any Ledger initialization in separate function -- see initLedger()
  91. */
  92. func (s *SmartContract) Init(APIstub shim.ChaincodeStubInterface) sc.Response {
  93. return shim.Success(nil)
  94. }
  95.  
  96. /*
  97. * The Invoke method is called as a result of an application request to run the Smart Contract "fabcar"
  98. * The calling application program has also specified the particular smart contract function to be called, with arguments
  99. */
  100. func (s *SmartContract) Invoke(APIstub shim.ChaincodeStubInterface) sc.Response {
  101.  
  102. // Retrieve the requested Smart Contract function and arguments
  103. function, args := APIstub.GetFunctionAndParameters()
  104. // Route to the appropriate handler function to interact with the ledger appropriately
  105. if function == "queryCar" {
  106. return s.queryCar(APIstub, args)
  107. } else if function == "initLedger" {
  108. return s.initLedger(APIstub)
  109. } else if function == "createCar" {
  110. return s.createCar(APIstub, args)
  111. } else if function == "queryAllCars" {
  112. return s.queryAllCars(APIstub)
  113. } else if function == "changeCarOwner" {
  114. return s.changeCarOwner(APIstub, args)
  115. } else if function == "queryAllProjects" {
  116. return s.queryAllProjects(APIstub)
  117. } else if function == "validAcceptance" {
  118. return s.validAcceptance(APIstub, args)
  119. } else if function == "createProject" {
  120. return s.createProject(APIstub, args)
  121. } else if function == "terminate" {
  122. return s.terminate(APIstub, args)
  123. } else if function == "validCreation" {
  124. return s.validCreation(APIstub, args)
  125. } else if function == "accept" {
  126. return s.accept(APIstub, args)
  127. } else if function == "createSkill" {
  128. return s.accept(APIstub, args)
  129. }
  130.  
  131.  
  132. return shim.Error("Invalid Smart Contract function name.")
  133. }
  134.  
  135. func (s *SmartContract) queryCar(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
  136.  
  137. if len(args) != 1 {
  138. return shim.Error("Incorrect number of arguments. Expecting 1")
  139. }
  140.  
  141. carAsBytes, _ := APIstub.GetState(args[0])
  142. return shim.Success(carAsBytes)
  143. }
  144.  
  145. func (s *SmartContract) initLedger(APIstub shim.ChaincodeStubInterface) sc.Response {
  146. /* cars := []Car{
  147. Car{Make: "Toyota", Model: "Prius", Colour: "blue", Owner: "Tomoko"},
  148. Car{Make: "Ford", Model: "Mustang", Colour: "red", Owner: "Brad"},
  149. Car{Make: "Hyundai", Model: "Tucson", Colour: "green", Owner: "Jin Soo"},
  150. Car{Make: "Volkswagen", Model: "Passat", Colour: "yellow", Owner: "Max"},
  151. Car{Make: "Tesla", Model: "S", Colour: "black", Owner: "Adriana"},
  152. Car{Make: "Peugeot", Model: "205", Colour: "purple", Owner: "Michel"},
  153. Car{Make: "Chery", Model: "S22L", Colour: "white", Owner: "Aarav"},
  154. Car{Make: "Fiat", Model: "Punto", Colour: "violet", Owner: "Pari"},
  155. Car{Make: "Tata", Model: "Nano", Colour: "indigo", Owner: "Valeria"},
  156. Car{Make: "Holden", Model: "Barina", Colour: "brown", Owner: "Shotaro"},
  157. }
  158.  
  159. i := 0
  160. for i < len(cars) {
  161. fmt.Println("i is ", i)
  162. carAsBytes, _ := json.Marshal(cars[i])
  163. APIstub.PutState("CAR"+strconv.Itoa(i), carAsBytes)
  164. fmt.Println("Added", cars[i])
  165. i = i + 1
  166. }
  167.  
  168. */
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175.  
  176. project := []Project{Project{Title: "titre", Description: "descriptionprojet", Startdate: "01/10/18", Enddate: "02/10/18", Skills_needed: "angular", Trainings_needed: "Polytechnique", State: "en cours"}}
  177.  
  178.  
  179.  
  180. i := 0
  181. for i < len(project) {
  182. fmt.Println("i is ", i)
  183. projectAsBytes, _ := json.Marshal(project[i])
  184. APIstub.PutState("PROJECT"+strconv.Itoa(i), projectAsBytes)
  185. fmt.Println("Added", project[i])
  186. i = i + 1
  187. }
  188.  
  189.  
  190.  
  191. return shim.Success(nil)
  192. }
  193.  
  194. func (s *SmartContract) createCar(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
  195.  
  196. if len(args) != 5 {
  197. return shim.Error("Incorrect number of arguments. Expecting 5")
  198. }
  199.  
  200. var car = Car{Make: args[1], Model: args[2], Colour: args[3], Owner: args[4]}
  201.  
  202. carAsBytes, _ := json.Marshal(car)
  203. APIstub.PutState(args[0], carAsBytes)
  204.  
  205. return shim.Success(nil)
  206. }
  207.  
  208.  
  209. func (s *SmartContract) createProject(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
  210.  
  211. if len(args) != 8 {
  212. return shim.Error("Incorrect number of arguments. Expecting 8")
  213. }
  214.  
  215. var project = User{Title: args[1], Description: args[2], Startdate: args[3], Enddate: args[4], Skills_needed: args[5], Trainings_needed: args[6], State: args[7]}
  216.  
  217. projectAsBytes, _ := json.Marshal(project)
  218. APIstub.PutState(args[0], projectAsBytes)
  219.  
  220. return shim.Success(nil)
  221. }
  222.  
  223.  
  224. func (s *SmartContract) createUser(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
  225.  
  226.  
  227.  
  228. if len(args) != 8 {
  229. return shim.Error("Incorrect number of arguments. Expecting 8")
  230. }
  231.  
  232. var user = Project{Title: args[1], Description: args[2], Startdate: args[3], Enddate: args[4], Skills_needed: args[5], Trainings_needed: args[6], State: args[7]}
  233.  
  234. projectAsBytes, _ := json.Marshal(project)
  235. APIstub.PutState(args[0], projectAsBytes)
  236.  
  237. return shim.Success(nil)
  238. }
  239.  
  240.  
  241.  
  242. func (s *SmartContract) createSkill(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
  243.  
  244. if len(args) != 6 {
  245. return shim.Error("Incorrect number of arguments. Expecting 5")
  246. }
  247.  
  248.  
  249.  
  250. var skill = Skills{Name: args[1], Level: args[2], Projects_skill: args[3], OwnedBy: args[4], State_Skills: args[5]}
  251.  
  252. SkillsAsBytes, _ := json.Marshal(skill)
  253. APIstub.PutState(args[0], SkillsAsBytes)
  254.  
  255. return shim.Success(nil)
  256. }
  257.  
  258. func (s *SmartContract) createSkill(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
  259.  
  260. if len(args) != 6 {
  261. return shim.Error("Incorrect number of arguments. Expecting 5")
  262. }
  263.  
  264.  
  265.  
  266. var skill = Skills{Name: args[1], Level: args[2], Projects_skill: args[3], OwnedBy: args[4], State_Skills: args[5]}
  267.  
  268. SkillsAsBytes, _ := json.Marshal(skill)
  269. APIstub.PutState(args[0], SkillsAsBytes)
  270.  
  271. return shim.Success(nil)
  272. }
  273.  
  274. func (s *SmartContract) queryAllProjects(APIstub shim.ChaincodeStubInterface) sc.Response {
  275.  
  276. startKey := "PROJECT0"
  277. endKey := "PROJECT999"
  278.  
  279. resultsIterator, err := APIstub.GetStateByRange(startKey, endKey)
  280. if err != nil {
  281. return shim.Error(err.Error())
  282. }
  283. defer resultsIterator.Close()
  284.  
  285. // buffer is a JSON array containing QueryResults
  286. var buffer bytes.Buffer
  287. buffer.WriteString("[")
  288.  
  289. bArrayMemberAlreadyWritten := false
  290. for resultsIterator.HasNext() {
  291. queryResponse, err := resultsIterator.Next()
  292. if err != nil {
  293. return shim.Error(err.Error())
  294. }
  295. // Add a comma before array members, suppress it for the first array member
  296. if bArrayMemberAlreadyWritten == true {
  297. buffer.WriteString(",")
  298. }
  299. buffer.WriteString("{\"Key\":")
  300. buffer.WriteString("\"")
  301. buffer.WriteString(queryResponse.Key)
  302. buffer.WriteString("\"")
  303.  
  304. buffer.WriteString(", \"Record\":")
  305. // Record is a JSON object, so we write as-is
  306. buffer.WriteString(string(queryResponse.Value))
  307. buffer.WriteString("}")
  308. bArrayMemberAlreadyWritten = true
  309. }
  310. buffer.WriteString("]")
  311.  
  312. fmt.Printf("- queryAllCars:\n%s\n", buffer.String())
  313.  
  314. return shim.Success(buffer.Bytes())
  315. }
  316.  
  317.  
  318.  
  319. func (s *SmartContract) queryAllCars(APIstub shim.ChaincodeStubInterface) sc.Response {
  320.  
  321. startKey := "CAR0"
  322. endKey := "CAR999"
  323.  
  324. resultsIterator, err := APIstub.GetStateByRange(startKey, endKey)
  325. if err != nil {
  326. return shim.Error(err.Error())
  327. }
  328. defer resultsIterator.Close()
  329.  
  330. // buffer is a JSON array containing QueryResults
  331. var buffer bytes.Buffer
  332. buffer.WriteString("[")
  333.  
  334. bArrayMemberAlreadyWritten := false
  335. for resultsIterator.HasNext() {
  336. queryResponse, err := resultsIterator.Next()
  337. if err != nil {
  338. return shim.Error(err.Error())
  339. }
  340. // Add a comma before array members, suppress it for the first array member
  341. if bArrayMemberAlreadyWritten == true {
  342. buffer.WriteString(",")
  343. }
  344. buffer.WriteString("{\"Key\":")
  345. buffer.WriteString("\"")
  346. buffer.WriteString(queryResponse.Key)
  347. buffer.WriteString("\"")
  348.  
  349. buffer.WriteString(", \"Record\":")
  350. // Record is a JSON object, so we write as-is
  351. buffer.WriteString(string(queryResponse.Value))
  352. buffer.WriteString("}")
  353. bArrayMemberAlreadyWritten = true
  354. }
  355. buffer.WriteString("]")
  356.  
  357. fmt.Printf("- queryAllCars:\n%s\n", buffer.String())
  358.  
  359. return shim.Success(buffer.Bytes())
  360. }
  361.  
  362. func (s *SmartContract) changeCarOwner(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
  363.  
  364. if len(args) != 2 {
  365. return shim.Error("Incorrect number of arguments. Expecting 2")
  366. }
  367.  
  368. carAsBytes, _ := APIstub.GetState(args[0])
  369. car := Car{}
  370.  
  371. json.Unmarshal(carAsBytes, &car)
  372. car.Owner = args[1]
  373.  
  374. carAsBytes, _ = json.Marshal(car)
  375. APIstub.PutState(args[0], carAsBytes)
  376.  
  377. return shim.Success(nil)
  378. }
  379.  
  380. func (s *SmartContract) validCreation(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
  381.  
  382. if len(args) != 2 {
  383. return shim.Error("Incorrect number of arguments. Expecting 2")
  384. }
  385.  
  386. projectAsBytes, _ := APIstub.GetState(args[0])
  387. project := Project{}
  388.  
  389. json.Unmarshal(projectAsBytes, &project)
  390. project.State = args[1]
  391.  
  392. projectAsBytes, _ = json.Marshal(project)
  393. APIstub.PutState(args[0], projectAsBytes)
  394.  
  395. return shim.Success(nil)
  396. }
  397.  
  398. func (s *SmartContract) accept(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
  399.  
  400. if len(args) != 2 {
  401. return shim.Error("Incorrect number of arguments. Expecting 2")
  402. }
  403.  
  404. projectAsBytes, _ := APIstub.GetState(args[0])
  405. project := Project{}
  406.  
  407. json.Unmarshal(projectAsBytes, &project)
  408. project.State = args[1]
  409.  
  410. projectAsBytes, _ = json.Marshal(project)
  411. APIstub.PutState(args[0], projectAsBytes)
  412.  
  413. return shim.Success(nil)
  414. }
  415.  
  416. func (s *SmartContract) validAcceptance(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
  417.  
  418. if len(args) != 2 {
  419. return shim.Error("Incorrect number of arguments. Expecting 2")
  420. }
  421.  
  422. projectAsBytes, _ := APIstub.GetState(args[0])
  423. project := Project{}
  424.  
  425. json.Unmarshal(projectAsBytes, &project)
  426. project.State = args[1]
  427.  
  428. projectAsBytes, _ = json.Marshal(project)
  429. APIstub.PutState(args[0], projectAsBytes)
  430.  
  431. return shim.Success(nil)
  432. }
  433.  
  434.  
  435. func (s *SmartContract) terminate(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
  436.  
  437. if len(args) != 2 {
  438. return shim.Error("Incorrect number of arguments. Expecting 2")
  439. }
  440.  
  441. projectAsBytes, _ := APIstub.GetState(args[0])
  442. project := Project{}
  443.  
  444. json.Unmarshal(projectAsBytes, &project)
  445. project.State = args[1]
  446.  
  447. projectAsBytes, _ = json.Marshal(project)
  448. APIstub.PutState(args[0], projectAsBytes)
  449.  
  450. return shim.Success(nil)
  451. }
  452.  
  453.  
  454.  
  455.  
  456. // The main function is only relevant in unit test mode. Only included here for completeness.
  457. func main() {
  458.  
  459. // Create a new Smart Contract
  460. err := shim.Start(new(SmartContract))
  461. if err != nil {
  462. fmt.Printf("Error creating new Smart Contract: %s", err)
  463. }
  464. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement