-
Notifications
You must be signed in to change notification settings - Fork 0
/
carRegistration.go
479 lines (385 loc) · 16.1 KB
/
carRegistration.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* The sample smart contract for documentation topic:
* Writing Your First Blockchain Application
*/
package main
/* Imports
* 4 utility libraries for formatting, handling bytes, reading and writing JSON, and string manipulation
* 2 specific Hyperledger Fabric specific libraries Smart Contracts
*/
import (
"bytes"
"encoding/json"
"fmt"
"strconv"
"time"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/core/chaincode/shim/ext/cid"
sc "github.com/hyperledger/fabric/protos/peer"
)
// Define the Smart Contract structure for Car Registration
type StatutoryCarRegistration struct {
}
// Define the car structure, with 4 properties. Structure tags are used by encoding/json library
type Car struct {
CarId string `json:"carId"`
Model string `json:"model"`
EngineNumber string `json:"engineNumber"`
ChassisNumber string `json:"chassisNumber"`
Colour string `json:"colour"`
YearOfManufacture string `json:"yearOfManufacture"`
Owner string `json:"owner"`
Dealer string `json:"dealer"`
InsurancePolicyNumber string `json:"insurancePolicyNumber"`
RegistrationNumber string `json:"registrationNumber"`
CurrentState string `json:"currentState"`
}
/*
* The Init method is called when the Smart Contract "fabcar" is instantiated by the blockchain network
* Best practice is to have any Ledger initialization in separate function -- see initLedger()
*/
func (s *StatutoryCarRegistration) Init(APIstub shim.ChaincodeStubInterface) sc.Response {
return shim.Success(nil)
}
/*
* The Invoke method is called as a result of an application request to run the Smart Contract "fabcar"
* The calling application program has also specified the particular smart contract function to be called, with arguments
*/
func (s *StatutoryCarRegistration) Invoke(APIstub shim.ChaincodeStubInterface) sc.Response {
// Retrieve the requested Smart Contract function and arguments
function, args := APIstub.GetFunctionAndParameters()
mspId, err := cid.GetMSPID(APIstub)
fmt.Println("MSPId : ",mspId)
if err != nil {
return shim.Error("Invalid MSP.")
}
// Route to the appropriate handler function to interact with the ledger appropriately
if function == "queryCar" {
return s.queryCar(APIstub, args)
} else if function == "initLedger" {
return s.initLedger(APIstub)
} else if function == "createCar" && mspId == "ManufacturerMSP"{
return s.createCar(APIstub, args)
} else if function == "queryAllCars" {
return s.queryAllCars(APIstub)
} else if function == "purchaseCar" && mspId == "DealerMSP"{
return s.purchaseCar(APIstub, args)
} else if function == "getCarHistory" {
return s.getCarHistory(APIstub, args)
} else if function == "deliverCar" && mspId == "ManufacturerMSP"{
return s.deliverCar(APIstub, args)
} else if function == "sellCar" {
return s.sellCar(APIstub, args)
} else if function == "insuredCar" && mspId == "InsurerMSP" {
return s.insuredCar(APIstub, args)
} else if function == "registeredCar" && mspId == "RTOMSP" {
return s.registeredCar(APIstub, args)
} else if function == "deliverCarToCustomer" && mspId == "DealerMSP" {
return s.deliverCarToCustomer(APIstub, args)
} else if function == "sellCar" {
return s.sellCar(APIstub, args)
} else if function == "queryCarByEngineNumber" {
return s.queryCarByEngineNumber(APIstub, args)
}
return shim.Error("Invalid Smart Contract function name.")
}
func (s *StatutoryCarRegistration) initLedger(APIstub shim.ChaincodeStubInterface) sc.Response {
cars := []Car{
Car{CarId: "1",EngineNumber: "7456001",ChassisNumber: "1HGCM82633A004352",Model: "Maruti Suzuki Ertiga",Colour:"RED",YearOfManufacture: "2019",CurrentState: "ReadyForSale"},
Car{CarId: "2",EngineNumber: "8450210",ChassisNumber: "1HGBH41JXMN109186",Model: "New Maruti Suzuki Swift",Colour:"SILVER",YearOfManufacture: "2020",CurrentState: "ReadyForSale"},
Car{CarId: "3",EngineNumber: "1436709",ChassisNumber: "1GNCS18Z3M0115610",Model: "New Maruti Suzuki Swift",Colour:"ORANGE",YearOfManufacture: "2018",CurrentState: "ReadyForSale"},
Car{CarId: "4",EngineNumber: "1480235",ChassisNumber: "1HGYT13MBYC918237",Model: "Maruti Suzuki Ertiga",Colour:"GOLD",YearOfManufacture: "2019",CurrentState: "ReadyForSale"},
Car{CarId: "5",EngineNumber: "3602154",ChassisNumber: "1GBTS14X5M0764610",Model: "Maruti Suzuki Baleno",Colour:"SILVER",YearOfManufacture: "2020",CurrentState: "ReadyForSale"},
Car{CarId: "6",EngineNumber: "8002145",ChassisNumber: "15BVT64X5M0867510",Model: "Maruti Suzuki Alto 800",Colour:"BLACK",YearOfManufacture: "2017",CurrentState: "ReadyForSale"},
Car{CarId: "7",EngineNumber: "1020354",ChassisNumber: "19UB3F77GA0011989",Model: "Maruti Suzuki Dzire",Colour:"SILVER",YearOfManufacture: "2020",CurrentState: "ReadyForSale"},
Car{CarId: "8",EngineNumber: "5630012",ChassisNumber: "1HGGY12537G284352",Model: "Maruti Suzuki Ignis",Colour:"BURGANDI",YearOfManufacture: "2019",CurrentState: "ReadyForSale"},
Car{CarId: "9",EngineNumber: "5689084",ChassisNumber: "19RV3G87MH4784100",Model: "Maruti Suzuki Dzire",Colour:"YELLOW",YearOfManufacture: "2018",CurrentState: "ReadyForSale"},
Car{CarId: "10",EngineNumber: "4853210",ChassisNumber: "1HBYH41UYXG843186",Model: "Maruti Suzuki Baleno",Colour:"GOLD",YearOfManufacture: "2020",CurrentState: "ReadyForSale"},
}
i := 0
indexName := "CarId~EngineNumber~ChassisNumber~Model"
for i < len(cars) {
fmt.Println("i is ", i)
carAsBytes, _ := json.Marshal(cars[i])
APIstub.PutState(cars[i].CarId, carAsBytes)
colorNameIndexKey, err := APIstub.CreateCompositeKey(indexName, []string{cars[i].CarId, cars[i].EngineNumber, cars[i].ChassisNumber, cars[i].Model})
if err != nil {
return shim.Error(err.Error())
}
value := []byte{0*00}
APIstub.PutState(colorNameIndexKey,value)
fmt.Println("Added", cars[i])
i = i + 1
}
return shim.Success(nil)
}
func (s *StatutoryCarRegistration) createCar(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 6 {
return shim.Error("Incorrect number of arguments. Expecting 5")
}
var car = Car{CarId: args[5], EngineNumber: args[0], ChassisNumber: args[1], Model: args[2], Colour: args[3], YearOfManufacture: args[4], CurrentState: "ReadyForSale"}
carAsBytes, _ := json.Marshal(car)
APIstub.PutState(args[5], carAsBytes)
indexName := "CarId~EngineNumber~ChassisNumber~Model"
colorNameIndexKey, err := APIstub.CreateCompositeKey(indexName, []string{car.CarId, car.EngineNumber, car.ChassisNumber, car.Model})
if err != nil {
return shim.Error(err.Error())
}
value := []byte{0*00}
APIstub.PutState(colorNameIndexKey,value)
return shim.Success(carAsBytes)
}
func (s *StatutoryCarRegistration) purchaseCar(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 2 {
return shim.Error("Incorrect number of arguments. Expecting 2 CarId, DealerName")
}
carAsBytes, _ := APIstub.GetState(args[0])
car := Car{}
json.Unmarshal(carAsBytes, &car)
car.Dealer = args[1]
car.CurrentState = "Booked";
carAsBytes, _ = json.Marshal(car)
APIstub.PutState(args[0], carAsBytes)
return shim.Success(nil)
}
func (s *StatutoryCarRegistration) deliverCar(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting only one argument CarId")
}
carAsBytes, _ := APIstub.GetState(args[0])
car := Car{}
json.Unmarshal(carAsBytes, &car)
car.CurrentState = "DeliveredToDealer"
carAsBytes, _ = json.Marshal(car)
APIstub.PutState(args[0], carAsBytes)
return shim.Success(nil)
}
func (s *StatutoryCarRegistration) sellCar(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 2 {
return shim.Error("Incorrect number of arguments. Expecting 2 CarId, BuyerName")
}
carAsBytes, _ := APIstub.GetState(args[0])
car := Car{}
json.Unmarshal(carAsBytes, &car)
car.Owner = args[1]
car.CurrentState = "Purchased"
carAsBytes, _ = json.Marshal(car)
APIstub.PutState(args[0], carAsBytes)
return shim.Success(nil)
}
func (s *StatutoryCarRegistration) insuredCar(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1 argument, CarId")
}
carAsBytes, _ := APIstub.GetState(args[0])
car := Car{}
json.Unmarshal(carAsBytes, &car)
car.CurrentState = "Insured"
carAsBytes, _ = json.Marshal(car)
APIstub.PutState(args[0], carAsBytes)
return shim.Success(nil)
}
func (s *StatutoryCarRegistration) registeredCar(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1 argument, CarId")
}
carAsBytes, _ := APIstub.GetState(args[0])
car := Car{}
json.Unmarshal(carAsBytes, &car)
car.CurrentState = "Registered"
carAsBytes, _ = json.Marshal(car)
APIstub.PutState(args[0], carAsBytes)
return shim.Success(nil)
}
func (s *StatutoryCarRegistration) deliverCarToCustomer(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1 argument, CarId")
}
carAsBytes, _ := APIstub.GetState(args[0])
car := Car{}
json.Unmarshal(carAsBytes, &car)
car.CurrentState = "DeliveredToCustomer"
carAsBytes, _ = json.Marshal(car)
APIstub.PutState(args[0], carAsBytes)
return shim.Success(nil)
}
func (s *StatutoryCarRegistration) queryCar(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1, CarId")
}
carAsBytes, _ := APIstub.GetState(args[0])
return shim.Success(carAsBytes)
}
func (s *StatutoryCarRegistration) queryCarByEngineNumber(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1, EngineNumber")
}
queryString := fmt.Sprintf("{\"selector\":{\"EngineNumber\":\"%s\"}}", args[0])
resultsIterator, err := APIstub.GetQueryResult(queryString)
if err != nil {
return shim.Error(err.Error())
}
defer resultsIterator.Close()
// buffer is a JSON array containing QueryResults
var buffer bytes.Buffer
buffer.WriteString("[")
bArrayMemberAlreadyWritten := false
for resultsIterator.HasNext() {
queryResponse, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
// Add a comma before array members, suppress it for the first array member
if bArrayMemberAlreadyWritten == true {
buffer.WriteString(",")
}
buffer.WriteString("{\"Key\":")
buffer.WriteString("\"")
buffer.WriteString(queryResponse.Key)
buffer.WriteString("\"")
buffer.WriteString(", \"Record\":")
// Record is a JSON object, so we write as-is
buffer.WriteString(string(queryResponse.Value))
buffer.WriteString("}")
bArrayMemberAlreadyWritten = true
}
buffer.WriteString("]")
fmt.Printf("- queryCarByEngineNumber:\n%s\n", buffer.String())
return shim.Success(buffer.Bytes())
}
func (s *StatutoryCarRegistration) queryAllCars(APIstub shim.ChaincodeStubInterface) sc.Response {
startKey := "1"
endKey := "999"
resultsIterator, err := APIstub.GetStateByRange(startKey, endKey)
if err != nil {
return shim.Error(err.Error())
}
defer resultsIterator.Close()
// buffer is a JSON array containing QueryResults
var buffer bytes.Buffer
buffer.WriteString("[")
bArrayMemberAlreadyWritten := false
for resultsIterator.HasNext() {
queryResponse, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
// Add a comma before array members, suppress it for the first array member
if bArrayMemberAlreadyWritten == true {
buffer.WriteString(",")
}
buffer.WriteString("{\"Key\":")
buffer.WriteString("\"")
buffer.WriteString(queryResponse.Key)
buffer.WriteString("\"")
buffer.WriteString(", \"Record\":")
// Record is a JSON object, so we write as-is
buffer.WriteString(string(queryResponse.Value))
buffer.WriteString("}")
bArrayMemberAlreadyWritten = true
}
buffer.WriteString("]")
fmt.Printf("- queryAllCars:\n%s\n", buffer.String())
return shim.Success(buffer.Bytes())
}
// add below code in the end
func (s *StatutoryCarRegistration) getCarHistory(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
if len(args) < 1 {
return shim.Error("Incorrect number of arguments. Expecting 1, CarId")
}
carID := args[0]
fmt.Printf("- start getCarHistory for : %s\n", carID)
resultsIterator, err := APIstub.GetHistoryForKey(carID)
if err != nil {
return shim.Error(err.Error())
}
defer resultsIterator.Close()
var buffer bytes.Buffer
buffer.WriteString("[")
bArrayMemberAlreadyWritten := false
for resultsIterator.HasNext() {
response, err := resultsIterator.Next()
if err != nil {
return shim.Error(err.Error())
}
if bArrayMemberAlreadyWritten == true {
buffer.WriteString(",")
}
buffer.WriteString("{\"TxId\":")
buffer.WriteString("\"")
buffer.WriteString(response.TxId)
buffer.WriteString("\"")
buffer.WriteString(", \"Value\":")
if response.IsDelete {
buffer.WriteString("null")
} else {
buffer.WriteString(string(response.Value))
}
buffer.WriteString(", \"Timestamp\":")
buffer.WriteString("\"")
buffer.WriteString(time.Unix(response.Timestamp.Seconds, int64(response.Timestamp.Nanos)).String())
buffer.WriteString("\"")
buffer.WriteString(", \"IsDelete\":")
buffer.WriteString("\"")
buffer.WriteString(strconv.FormatBool(response.IsDelete))
buffer.WriteString("\"")
buffer.WriteString("}")
bArrayMemberAlreadyWritten = true
}
buffer.WriteString("]")
fmt.Printf("- getCarHistory returning:\n%s\n", buffer.String())
return shim.Success(buffer.Bytes())
}
func (s *StatutoryCarRegistration) deleteCar(APIstub shim.ChaincodeStubInterface, args []string) sc.Response {
var jsonResp string
var carJSON Car
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1 argument, CarId")
}
carID := args[0]
// to maintain the color~name index, we need to read the marble first and get its color
valAsbytes, err := APIstub.GetState(carID) //get the marble from chaincode state
if err != nil {
jsonResp = "{\"Error\":\"Failed to get state for " + carID + "\"}"
return shim.Error(jsonResp)
} else if valAsbytes == nil {
jsonResp = "{\"Error\":\"car does not exist: " + carID + "\"}"
return shim.Error(jsonResp)
}
err = json.Unmarshal([]byte(valAsbytes), &carJSON)
if err != nil {
jsonResp = "{\"Error\":\"Failed to decode JSON of: " + carID + "\"}"
return shim.Error(jsonResp)
}
err = APIstub.DelState(carID) //remove the marble from chaincode state
if err != nil {
return shim.Error("Failed to delete state:" + err.Error())
}
return shim.Success(nil)
}
// The main function is only relevant in unit test mode. Only included here for completeness.
func main() {
// Create a new Smart Contract -Madhav
err := shim.Start(new(StatutoryCarRegistration))
if err != nil {
fmt.Printf("Error creating new Smart Contract: %s", err)
}
}