-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasset_transfer_ledger_chaincode.go
1257 lines (1030 loc) · 31.1 KB
/
asset_transfer_ledger_chaincode.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed 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.
*/
package main
//WARNING - this chaincode's ID is hard-coded in chaincode_example04 to illustrate one way of
//calling chaincode from a chaincode. If this example is modified, chaincode_example04.go has
//to be modified as well with the new ID of chaincode_example02.
//chaincode_example05 show's how chaincode ID can be passed in as a parameter instead of
//hard-coding.
import (
"fmt"
"strconv"
"strings"
// "github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric-chaincode-go/shim"
// pb "github.com/hyperledger/fabric/protos/peer"
"encoding/json"
pb "github.com/hyperledger/fabric-protos-go/peer"
)
// SimpleChaincode example simple Chaincode implementation
type SimpleChaincode struct {
}
type Trainer struct {
TrainerID string
FirstName string
LastName string
EmailAddress string
City string
Description string
Nickname string
}
type Platform struct {
PlatformID string
PlatformName string
EmailAddress string
Description string
Trainees []Trainee
Vlabs []Vlab
}
type Trainee struct {
TraineeID string
FirstName string
LastName string
EmailAddress string
City string
Description string
Nickname string
ActivePlatform string
Total_Exp_Points string
VlabPointsMap2 map[string]Vlab `json:"Trainee_vlabs"`
}
type Vlab struct {
VlabID string `json:"vlabID"`
BoxName string
Domain string
SystemType string
Description string
ExpPoints string
BoxDifficulty string
TimeNeeded string
Result string
// Add other fields as needed
}
type Administrator struct{
AdministratorID string
FirstName string
LastName string
EmailAddress string
City string
Description string
Nickname string
}
type VlabOwner struct{
VLabOwnerID string
FirstName string
LastName string
EmailAddress string
City string
Description string
Nickname string
}
/*
...
*/
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
fmt.Println("ex02 Init")
return shim.Success(nil)
}
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
fmt.Println("ex02 Invoke")
function, args := stub.GetFunctionAndParameters()
if function == "createTrainee" {
return t.createTrainee(stub, args)
} else if function == "getIdentity" {
return t.getIdentity(stub, args)
} else if function == "addTraineeToPlatform" {
return t.addTraineeToPlatform(stub, args)
} else if function == "delete" {
return t.delete(stub, args)
} else if function == "ScoreTheVlab" {
return t.ScoreTheVlab(stub, args)
} else if function == "createTrainer" {
return t.createTrainer(stub, args)
} else if function == "createPlatform" {
return t.createPlatform(stub, args)
} else if function == "getAllAsset" {
return t.getAllAsset(stub, args)
} else if function == "TransferTrainee1" {
return t.TransferTrainee1(stub, args)
} else if function == "createVlab" {
return t.createVlab(stub, args)
} else if function == "addVlabToTrainee" {
return t.addVlabToTrainee(stub, args)
} else if function == "deleteTraineeFromPlatform" {
return t.deleteTraineeFromPlatform(stub, args)
} else if function == "createAdministrator" {
return t.createAdministrator(stub, args)
} else if function == "createVlabOwner" {
return t.createVlabOwner(stub, args)
} else if function == "addVlabToPlatform" {
return t.addVlabToPlatform(stub, args)
} else if function == "calculateExpPoints" {
return t.calculateExpPoints(stub, args)
}
return shim.Error("Invalid invoke function name. Expecting \"createTrainee\" \"getTrainee\" \"updateTraineeUniversity\" \"addVLabToTrainee\"")
}
func (t *SimpleChaincode) createTrainee(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 7 {
return shim.Error("Incorrect number of arguments. Expecting 4")
}
traineeID := args[0]
firstName := args[1]
lastName := args[2]
emailAddress := args[3]
city := args[4]
description := args[5]
nickname := args[6]
// Check if trainee already exists
traineeBytes, err := stub.GetState(traineeID)
if err != nil {
return shim.Error(err.Error())
}
if traineeBytes != nil {
return shim.Error("TraineeID already exists")
}
// Create a new trainee object
trainee := Trainee{
TraineeID: traineeID,
FirstName: firstName,
LastName: lastName,
EmailAddress: emailAddress,
City: city,
Description: description,
Nickname: nickname,
//VlabPointsMap: make(map[string]string),
VlabPointsMap2: make(map[string]Vlab),
}
// Convert trainee object to JSON
traineeJSON, err := json.Marshal(trainee)
if err != nil {
return shim.Error("Failed to marshal trainee to JSON")
}
// Save trainee JSON to the ledger
err = stub.PutState(traineeID, traineeJSON)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func (t *SimpleChaincode) createPlatform(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 4 {
return shim.Error("Incorrect number of arguments. Expecting 6")
}
platformID := args[0]
platformName := args[1]
emailAddress := args[2]
description := args[3]
// Check if platform exists
platformBytes, err := stub.GetState(platformID)
if err != nil {
return shim.Error(err.Error())
}
if platformBytes != nil {
return shim.Error("PlatformID already exists")
}
// Create a new Platform object
platform := Platform{
PlatformID: platformID,
PlatformName: platformName,
EmailAddress: emailAddress,
Description: description,
Trainees: []Trainee{},
Vlabs: []Vlab{},
}
// Convert Platform object to JSON
platformJSON, err := json.Marshal(platform)
if err != nil {
return shim.Error("Failed to marshal trainee to JSON")
}
// Save Platform JSON to the ledger
err = stub.PutState(platformID, platformJSON)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func (t *SimpleChaincode) addTraineeToPlatform(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 3 {
return shim.Error("Incorrect number of arguments. Expecting 2")
}
administratorID := args[0]
role := args[1]
PlatformID := args[2]
// Check if administratorID starts with "admin"
if !strings.HasPrefix(administratorID, "admin") {
return shim.Error("Not authorized for that transaction.")
}
// Retrieve trainee from the ledger
traineeBytes, err := stub.GetState(role)
if err != nil {
return shim.Error(err.Error())
}
if traineeBytes == nil {
return shim.Error("TraineeID does not exist")
}
// Unmarshal trainee JSON
trainee := Trainee{}
err = json.Unmarshal(traineeBytes, &trainee)
if err != nil {
return shim.Error("Failed to unmarshal trainee JSON")
}
// Check if Platform exists
platformBytes, err := stub.GetState(PlatformID)
if err != nil {
return shim.Error(err.Error())
}
if platformBytes == nil {
return shim.Error("PlatformID does not exists")
}
platform := Platform{}
err = json.Unmarshal(platformBytes, &platform)
if err != nil {
return shim.Error("Failed to unmarshal trainee JSON")
}
for _, val := range platform.Trainees {
if val.TraineeID == role {
return shim.Error("TraineeID is already registered in the platform "+PlatformID)
}
}
if trainee.ActivePlatform == "" {
trainee.ActivePlatform = PlatformID
// Convert trainee object to JSON
updatedTraineeJSON, err := json.Marshal(trainee)
if err != nil {
return shim.Error("Failed to marshal updated trainee to JSON")
}
// Save updated trainee JSON to the ledger
err = stub.PutState(role, updatedTraineeJSON)
if err != nil {
return shim.Error(err.Error())
}
platform.Trainees = append(platform.Trainees, trainee)
} else {
return shim.Error("TraineeID is already registered in ActivePlatform, you need to transfer first")
}
// Convert Platform object to JSON
updatedPlatformJSON, err := json.Marshal(platform)
if err != nil {
return shim.Error("Failed to marshal updated Platform to JSON")
}
// Save updated Platform JSON to the ledger
err = stub.PutState(PlatformID, updatedPlatformJSON)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func (t *SimpleChaincode) createTrainer(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 7 {
return shim.Error("Incorrect number of arguments. Expecting 4")
}
trainerID := args[0]
firstName := args[1]
lastName := args[2]
emailAddress := args[3]
city := args[4]
description := args[5]
nickname := args[6]
// Check if trainer already exists
traineeBytes, err := stub.GetState(trainerID)
if err != nil {
return shim.Error(err.Error())
}
if traineeBytes != nil {
return shim.Error("TrainerID already exists")
}
// Create a new trainer object
trainer := Trainer{
TrainerID: trainerID,
FirstName: firstName,
LastName: lastName,
EmailAddress: emailAddress,
City: city,
Description: description,
Nickname: nickname,
}
// Convert trainee object to JSON
trainerJSON, err := json.Marshal(trainer)
if err != nil {
return shim.Error("Failed to marshal trainee to JSON")
}
// Save trainee JSON to the ledger
err = stub.PutState(trainerID, trainerJSON)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func (t *SimpleChaincode) getIdentity(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}
role := args[0]
// Retrieve trainee from the ledger
identityBytes, err := stub.GetState(role)
if err != nil {
return shim.Error(err.Error())
}
if identityBytes == nil {
return shim.Error("Identity does not exist")
}
return shim.Success(identityBytes)
}
// Deletes an entity from state
func (t *SimpleChaincode) delete(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}
A := args[0]
// Delete the key from the state in ledger
err := stub.DelState(A)
if err != nil {
return shim.Error("Failed to delete state")
}
return shim.Success(nil)
}
func (t *SimpleChaincode) ScoreTheVlab(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 4 {
return shim.Error("Incorrect number of arguments. Expecting 4")
}
trainerID := args[0]
traineeID := args[1]
vlabID := args[2]
vlabResult := args[3]
// Check if trainerID starts with "trainer"
if !strings.HasPrefix(trainerID, "Trainer") {
return shim.Error("Not authorized for that transaction.")
}
// Retrieve trainee from the ledger
traineeBytes, err := stub.GetState(traineeID)
if err != nil {
return shim.Error(err.Error())
}
if traineeBytes == nil {
return shim.Error("Trainee does not exist")
}
// Unmarshal trainee JSON
trainee := Trainee{}
err = json.Unmarshal(traineeBytes, &trainee)
if err != nil {
return shim.Error("Failed to unmarshal trainee JSON")
}
// Check if the vlabID exists in VlabPointsMap2
if _, exists := trainee.VlabPointsMap2[vlabID]; !exists {
return shim.Error("Trainee does not have that vlabID")
}
// Retrieve the Vlab from the ledger
vlabBytes, err := stub.GetState(vlabID)
if err != nil {
return shim.Error(err.Error())
}
if vlabBytes == nil {
return shim.Error("Vlab does not exist")
}
// Unmarshal the Vlab JSON
vlab := Vlab{}
err = json.Unmarshal(vlabBytes, &vlab)
if err != nil {
return shim.Error("Failed to unmarshal Vlab JSON")
}
// Update trainee's vlab points
vlab.Result = vlabResult
trainee.VlabPointsMap2[vlabID] = vlab
// Get the trainee's platform from the ledger
platformBytes, err := stub.GetState(trainee.ActivePlatform)
if err != nil {
return shim.Error(err.Error())
}
// Unmarshal the platform JSON
var platform Platform
err = json.Unmarshal(platformBytes, &platform)
if err != nil {
return shim.Error(err.Error())
}
for i, trainee := range platform.Trainees {
if trainee.TraineeID == traineeID {
platform.Trainees[i].VlabPointsMap2[vlabID] = vlab
break
}
}
updatedTraineeJSON, err := json.Marshal(trainee)
if err != nil {
return shim.Error("Failed to marshal updated trainee to JSON")
}
// Save updated trainee JSON to the ledger
err = stub.PutState(traineeID, updatedTraineeJSON)
if err != nil {
return shim.Error(err.Error())
}
// Convert the updated platform to JSON
updatedPlatformJSON, err := json.Marshal(platform)
if err != nil {
return shim.Error(err.Error())
}
// Store the updated trainee in the ledger
err = stub.PutState(trainee.ActivePlatform, updatedPlatformJSON)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func (t *SimpleChaincode) getAllAsset(stub shim.ChaincodeStubInterface, args []string) pb.Response {
// Retrieve all assets from the ledger
resultsIterator, err := stub.GetStateByRange("", "")
if err != nil {
return shim.Error("Failed to get assets")
}
defer resultsIterator.Close()
// Iterate over the result set and collect the assets
var assets []string
for resultsIterator.HasNext() {
queryResult, err := resultsIterator.Next()
if err != nil {
return shim.Error("Failed to iterate over assets")
}
assets = append(assets, string(queryResult.Value))
}
// Convert assets to JSON
assetsJSON, err := json.Marshal(assets)
if err != nil {
return shim.Error("Failed to marshal assets to JSON")
}
return shim.Success(assetsJSON)
}
func (t *SimpleChaincode) createVlab(stub shim.ChaincodeStubInterface, args []string) pb.Response {
// Check the number of arguments
if len(args) != 9 {
return shim.Error("Incorrect number of arguments. Expecting vlabID")
}
vlabOwnerId := args[0]
// Check if administratorID starts with "admin"
if !strings.HasPrefix(vlabOwnerId, "vlabowner") {
return shim.Error("Not authorized for that transaction.")
}
// Extract the vlabID from the argument
vlabID := args[1]
boxName :=args[2]
domain := args[3]
systemType :=args[4]
description :=args[5]
expPoints :=args[6]
boxDifficulty :=args[7]
timeNeeded :=args[8]
// Check if the Vlab already exists in the ledger
vlabBytes, err := stub.GetState(vlabID)
if err != nil {
return shim.Error(err.Error())
}
if vlabBytes != nil {
return shim.Error("Vlab with ID "+vlabID+" already exists")
}
// Create a new Vlab instance
vlab := Vlab{
VlabID: vlabID,
BoxName: boxName,
Domain: domain,
SystemType: systemType,
Description: description,
ExpPoints: expPoints,
BoxDifficulty: boxDifficulty,
TimeNeeded: timeNeeded,
}
// Convert the Vlab instance to JSON
vlabJSON, err := json.Marshal(vlab)
if err != nil {
return shim.Error(err.Error())
}
// Store the Vlab JSON in the ledger
err = stub.PutState(vlabID, vlabJSON)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func (t *SimpleChaincode) addVlabToTrainee(stub shim.ChaincodeStubInterface, args []string) pb.Response {
// Check the number of arguments
if len(args) != 2 {
return shim.Error("Incorrect number of arguments. Expecting traineeID and vlabID")
}
// Extract the traineeID and vlabID from arguments
traineeID := args[0]
vlabID := args[1]
// Get the existing trainee from the ledger
traineeBytes, err := stub.GetState(traineeID)
if err != nil {
return shim.Error(err.Error())
}
// Check if the trainee exists
if traineeBytes == nil {
return shim.Error("Trainee with ID "+traineeID+" does not exist")
}
// Unmarshal the trainee JSON
var trainee Trainee
err = json.Unmarshal(traineeBytes, &trainee)
if err != nil {
return shim.Error(err.Error())
}
if trainee.ActivePlatform == "" {
return shim.Error("Trainee need to be registered in platform in order to add a vlab")
}
// Get the existing vlab from the ledger
vlabBytes, err := stub.GetState(vlabID)
if err != nil {
return shim.Error(err.Error())
}
// Check if the vlab exists
if vlabBytes == nil {
return shim.Error("Vlab with ID "+vlabID+" does not exist")
}
// Unmarshal the vlab JSON
var vlab Vlab
err = json.Unmarshal(vlabBytes, &vlab)
if err != nil {
return shim.Error(err.Error())
}
// Get the trainee's platform from the ledger
platformBytes, err := stub.GetState(trainee.ActivePlatform)
if err != nil {
return shim.Error(err.Error())
}
// Unmarshal the platform JSON
var platform Platform
err = json.Unmarshal(platformBytes, &platform)
if err != nil {
return shim.Error(err.Error())
}
// Check if the Vlab already exists in the Trainee's VlabPointsMap
if _, exists := trainee.VlabPointsMap2[vlabID]; exists {
return shim.Error("Vlab with ID "+vlabID+" already exists for trainee with ID "+ vlabID)
}
// Add the Vlab to the Trainee's VlabPointsMap
trainee.VlabPointsMap2[vlabID] = vlab
for i, trainee := range platform.Trainees {
if trainee.TraineeID == traineeID {
platform.Trainees[i].VlabPointsMap2[vlabID] = vlab
break
}
}
// Convert the updated trainee to JSON
updatedTraineeJSON, err := json.Marshal(trainee)
if err != nil {
return shim.Error(err.Error())
}
// Store the updated trainee in the ledger
err = stub.PutState(traineeID, updatedTraineeJSON)
if err != nil {
return shim.Error(err.Error())
}
// Convert the updated platform to JSON
updatedPlatformJSON, err := json.Marshal(platform)
if err != nil {
return shim.Error(err.Error())
}
// Store the updated trainee in the ledger
err = stub.PutState(trainee.ActivePlatform, updatedPlatformJSON)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func (t *SimpleChaincode) deleteTraineeFromPlatform(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 3 {
return shim.Error("Incorrect number of arguments. Expecting 2")
}
administratorID := args[0]
traineeID := args[1]
platformID := args[2]
// Check if administratorID starts with "admin"
if !strings.HasPrefix(administratorID, "admin") {
return shim.Error("Not authorized for that transaction.")
}
// Retrieve trainee from the ledger
traineeBytes, err := stub.GetState(traineeID)
if err != nil {
return shim.Error(err.Error())
}
if traineeBytes == nil {
return shim.Error("TraineeID does not exist")
}
// Unmarshal trainee JSON
trainee := Trainee{}
err = json.Unmarshal(traineeBytes, &trainee)
if err != nil {
return shim.Error("Failed to unmarshal trainee JSON")
}
// Check if the trainee is registered in the specified platform
if trainee.ActivePlatform != platformID {
return shim.Error("Trainee is not registered in the specified platform")
}
// Retrieve the platform from the ledger
platformBytes, err := stub.GetState(platformID)
if err != nil {
return shim.Error(err.Error())
}
if platformBytes == nil {
return shim.Error("PlatformID does not exist")
}
// Unmarshal platform JSON
platform := Platform{}
err = json.Unmarshal(platformBytes, &platform)
if err != nil {
return shim.Error("Failed to unmarshal platform JSON")
}
// Remove the trainee from the platform's Trainees list
updatedTrainees := make([]Trainee, 0)
for _, trainee := range platform.Trainees {
if trainee.TraineeID != traineeID {
updatedTrainees = append(updatedTrainees, trainee)
}
}
platform.Trainees = updatedTrainees
// Update the trainee's active platform to empty
trainee.ActivePlatform = ""
// Convert trainee object to JSON
updatedTraineeJSON, err := json.Marshal(trainee)
if err != nil {
return shim.Error("Failed to marshal updated trainee to JSON")
}
// Save updated trainee JSON to the ledger
err = stub.PutState(traineeID, updatedTraineeJSON)
if err != nil {
return shim.Error(err.Error())
}
// Convert platform object to JSON
updatedPlatformJSON, err := json.Marshal(platform)
if err != nil {
return shim.Error("Failed to marshal updated platform to JSON")
}
// Save updated platform JSON to the ledger
err = stub.PutState(platformID, updatedPlatformJSON)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func (t *SimpleChaincode) createAdministrator(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 7 {
return shim.Error("Incorrect number of arguments. Expecting 4")
}
administratorID := args[0]
firstName := args[1]
lastName := args[2]
emailAddress := args[3]
city := args[4]
description := args[5]
nickname := args[6]
// Check if Administrator already exists
adminBytes, err := stub.GetState(administratorID)
if err != nil {
return shim.Error(err.Error())
}
if adminBytes != nil {
return shim.Error("AdministratorID already exists")
}
// Create a new Administrator object
administrator := Administrator{
AdministratorID: administratorID,
FirstName: firstName,
LastName: lastName,
EmailAddress: emailAddress,
City: city,
Description: description,
Nickname: nickname,
}
// Convert Administrator object to JSON
administratorJSON, err := json.Marshal(administrator)
if err != nil {
return shim.Error("Failed to marshal Administrator to JSON")
}
// Save Administrator JSON to the ledger
err = stub.PutState(administratorID, administratorJSON)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func (t *SimpleChaincode) createVlabOwner(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 7 {
return shim.Error("Incorrect number of arguments. Expecting 4")
}
vlabOwnerID := args[0]
firstName := args[1]
lastName := args[2]
emailAddress := args[3]
city := args[4]
description := args[5]
nickname := args[6]
// Check if VlabOwner already exists
VlabOwnerBytes, err := stub.GetState(vlabOwnerID)
if err != nil {
return shim.Error(err.Error())
}
if VlabOwnerBytes != nil {
return shim.Error("VlabOwnerID already exists")
}
// Create a new VlabOwner object
vlabOwner := VlabOwner{
VLabOwnerID: vlabOwnerID,
FirstName: firstName,
LastName: lastName,
EmailAddress: emailAddress,
City: city,
Description: description,
Nickname: nickname,
}
// Convert VlabOwner object to JSON
administratorJSON, err := json.Marshal(vlabOwner)
if err != nil {
return shim.Error("Failed to marshal VlabOwner to JSON")
}
// Save VlabOwner JSON to the ledger
err = stub.PutState(vlabOwnerID, administratorJSON)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func (t *SimpleChaincode) addVlabToPlatform(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 3 {
return shim.Error("Incorrect number of arguments. Expecting 2")
}
administratorID := args[0]
vlabID := args[1]
PlatformID := args[2]
// Check if administratorID starts with "admin"
if !strings.HasPrefix(administratorID, "admin") {
return shim.Error("Not authorized for that transaction.")
}
// Retrieve vlab from the ledger
vlabIDBytes, err := stub.GetState(vlabID)
if err != nil {
return shim.Error(err.Error())
}
if vlabIDBytes == nil {
return shim.Error("vlabID does not exist")
}
// Unmarshal Vlab JSON
vlab := Vlab{}
err = json.Unmarshal(vlabIDBytes, &vlab)
if err != nil {
return shim.Error("Failed to unmarshal Vlab JSON")
}
// Check if Platform exists
platformBytes, err := stub.GetState(PlatformID)
if err != nil {
return shim.Error(err.Error())
}
if platformBytes == nil {
return shim.Error("PlatformID does not exists")
}
platform := Platform{}
err = json.Unmarshal(platformBytes, &platform)
if err != nil {
return shim.Error("Failed to unmarshal trainee JSON")
}
for _, val := range platform.Vlabs {
if val.VlabID == vlabID {
return shim.Error("vlabID is already added in the platform "+PlatformID)
}
}
platform.Vlabs = append(platform.Vlabs, vlab)
// Convert Platform object to JSON
updatedPlatformJSON, err := json.Marshal(platform)
if err != nil {
return shim.Error("Failed to marshal updated Platform to JSON")
}
// Save updated Platform JSON to the ledger
err = stub.PutState(PlatformID, updatedPlatformJSON)
if err != nil {
return shim.Error(err.Error())
}
return shim.Success(nil)
}
func (t *SimpleChaincode) TransferTrainee1(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 3 {
return shim.Error("Incorrect number of arguments. Expecting 3")
}
administratorID := args[0]
traineeID := args[1]
newPlatformID := args[2]
// Check if administratorID starts with "admin"
if !strings.HasPrefix(administratorID, "admin") {
return shim.Error("Not authorized for that transaction.")
}
// Retrieve trainee from the ledger
traineeBytes, err := stub.GetState(traineeID)
if err != nil {