forked from yeasy/blockchain_guide
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chaincode_example04.go
466 lines (403 loc) · 11.3 KB
/
chaincode_example04.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
/*
authors:
"swb"<swbsin@163.com>
"Gymgle"<ymgongcn@gmail.com>
MIT License
*/
package main
import (
"crypto/md5"
"crypto/rand"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io"
"strconv"
"time"
"github.com/hyperledger/fabric/core/chaincode/shim"
pb "github.com/hyperledger/fabric/protos/peer"
)
type SimpleChaincode struct {
}
var BackGroundNo int = 0
var RecordNo int = 0
type School struct {
Name string
Location string
Address string
PriKey string
PubKey string
StudentAddress []string
}
type Student struct {
Name string
Address string
BackgroundId []int
}
// 学历信息,当离开学校才能记入
type Background struct {
Id int
ExitTime int64
Status string //0:毕业 1:退学
}
type Record struct {
Id int
SchoolAddress string
StudentAddress string
SchoolSign string
ModifyTime int64
ModifyOperation string // 0:正常毕业 1:退学 2:入学
}
/*
* 区块链网络实例化"diploma"智能合约时调用该方法
*/
func (t *SimpleChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
return shim.Success(nil)
}
/*
* 客户端发起请求执行"diploma"智能合约时会调用 Invoke 方法
*/
func (t *SimpleChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
// 获取请求调用智能合约的方法和参数
function, args := stub.GetFunctionAndParameters()
// Route to the appropriate handler function to interact with the ledger appropriately
if function == "createSchool" {
return t.createSchool(stub, args)
} else if function == "createStudent" {
return t.createStudent(stub, args)
} else if function == "enrollStudent" {
return t.enrollStudent(stub, args)
} else if function == "updateDiploma" {
return t.updateDiploma(stub, args)
} else if function == "getRecords" {
return t.getRecords(stub)
} else if function == "getRecordById" {
return t.getRecordById(stub, args)
} else if function == "getStudentByAddress" {
return t.getStudentByAddress(stub, args)
} else if function == "getSchoolByAddress" {
return t.getSchoolByAddress(stub, args)
} else if function == "getBackgroundById" {
return t.getBackgroundById(stub, args)
}
return shim.Success(nil)
}
/*
* 添加一所新学校
* args[0] 学校名称
* args[1] 学校所在位置
*/
func (t *SimpleChaincode) createSchool(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 2 {
return shim.Error("Incorrect number of arguments. Expecting 2")
}
var school School
var schoolBytes []byte
var stuAddress []string
var address, priKey, pubKey string
address, priKey, pubKey = GetAddress()
school = School{Name: args[0], Location: args[1], Address: address, PriKey: priKey, PubKey: pubKey, StudentAddress: stuAddress}
err := writeSchool(stub, school)
if err != nil {
shim.Error("Error write school")
}
schoolBytes, err = json.Marshal(&school)
if err != nil {
return shim.Error("Error retrieving schoolBytes")
}
return shim.Success(schoolBytes)
}
/*
* 添加一名新学生
* args[0] 学生的姓名
*/
func (t *SimpleChaincode) createStudent(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}
var student Student
var studentBytes []byte
var stuAddress string
var bgID []int
stuAddress, _, _ = GetAddress()
student = Student{Name: args[0], Address: stuAddress, BackgroundId: bgID}
err := writeStudent(stub, student)
if err != nil {
return shim.Error("Error write student")
}
studentBytes, err = json.Marshal(&student)
if err != nil {
return shim.Error("Error retrieving studentBytes")
}
return shim.Success(studentBytes)
}
/*
* 学校招生(返回学校信息)
* args[0] 学校账户地址
* args[1] 学校签名
* args[2] 学生账户地址
*/
func (t *SimpleChaincode) enrollStudent(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 3 {
return shim.Error("Incorrect number of arguments. Expecting 3")
}
schAddress := args[0]
schoolSign := args[1]
stuAddress := args[2]
var school School
var schBytes []byte
var err error
// 根据学校账户地址获取学校信息
schBytes, err = stub.GetState(schAddress)
if err != nil {
return shim.Error("Error retrieving data")
}
err = json.Unmarshal(schBytes, &school)
if err != nil {
return shim.Error("Error unmarshalling data")
}
var record Record
record = Record{Id: RecordNo, SchoolAddress: schAddress, StudentAddress: stuAddress, SchoolSign: schoolSign, ModifyTime: time.Now().Unix(), ModifyOperation: "2"} // 2 表示入学
err = writeRecord(stub, record)
if err != nil {
return shim.Error("Error write record")
}
school.StudentAddress = append(school.StudentAddress, stuAddress)
err = writeSchool(stub, school)
if err != nil {
return shim.Error("Error write school")
}
RecordNo = RecordNo + 1
recordBytes, err := json.Marshal(&record)
if err != nil {
return shim.Error("Error retrieving recordBytes")
}
return shim.Success(recordBytes)
}
/*
* 由学校更新学生学历信息,并签名(返回记录信息)
* args[0] 学校账户地址
* args[1] 学校签名
* args[2] 待修改学生的账户地址
* args[3] 对该学生的学历进行怎样的修改,0:正常毕业 1:退学
*/
func (t *SimpleChaincode) updateDiploma(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 4 {
return shim.Error("Incorrect number of arguments. Expecting 4")
}
schAddress := args[0]
schoolSign := args[1]
stuAddress := args[2]
modOperation := args[3]
var recordBytes []byte
var student Student
var stuBytes []byte
var err error
// 根据学生账户地址获取学生信息
stuBytes, err = stub.GetState(stuAddress)
if err != nil {
return shim.Error("Error retrieving data")
}
err = json.Unmarshal(stuBytes, &student)
if err != nil {
return shim.Error("Error unmarshalling data")
}
var record Record
record = Record{Id: RecordNo, SchoolAddress: schAddress, StudentAddress: stuAddress, SchoolSign: schoolSign, ModifyTime: time.Now().Unix(), ModifyOperation: modOperation}
err = writeRecord(stub, record)
if err != nil {
return shim.Error("Error write record")
}
var background Background
background = Background{Id: BackGroundNo, ExitTime: time.Now().Unix(), Status: modOperation}
err = writeBackground(stub, background)
if err != nil {
return shim.Error("Error write background")
}
// 如果学生正常毕业,也要更新学生的教育背景
if modOperation == "0" {
student.BackgroundId = append(student.BackgroundId, BackGroundNo)
student = Student{Name: student.Name, Address: student.Address, BackgroundId: student.BackgroundId}
err = writeStudent(stub, student)
if err != nil {
return shim.Error("Error write student")
}
}
BackGroundNo = BackGroundNo + 1
recordBytes, err = json.Marshal(&record)
if err != nil {
return shim.Error("Error retrieving schoolBytes")
}
return shim.Success(recordBytes)
}
/*
* 通过学生的地址获取学生的学历信息
* args[0] address
*/
func (t *SimpleChaincode) getStudentByAddress(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}
stuBytes, err := stub.GetState(args[0])
if err != nil {
shim.Error("Error retrieving data")
}
return shim.Success(stuBytes)
}
/*
* 通过地址获取学校的信息
* args[0] address
*/
func (t *SimpleChaincode) getSchoolByAddress(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}
schBytes, err := stub.GetState(args[0])
if err != nil {
shim.Error("Error retrieving data")
}
return shim.Success(schBytes)
}
/*
* 通过 Id 获取记录
* args[0] 记录的 Id
*/
func (t *SimpleChaincode) getRecordById(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 1 {
return shim.Error("Incorrect number of arguments. Expecting 1")
}
recBytes, err := stub.GetState("Record" + args[0])
if err != nil {
return shim.Error("Error retrieving data")
}
return shim.Success(recBytes)
}
/*
* 获取全部记录(如果记录数大于10,返回前10个)
*/
func (t *SimpleChaincode) getRecords(stub shim.ChaincodeStubInterface) pb.Response {
var records []Record
var number string
var err error
var record Record
var recBytes []byte
if RecordNo < 10 {
i := 0
for i <= RecordNo {
number = strconv.Itoa(i)
recBytes, err = stub.GetState("Record" + number)
if err != nil {
return shim.Error("Error get detail")
}
err = json.Unmarshal(recBytes, &record)
if err != nil {
return shim.Error("Error unmarshalling data")
}
records = append(records, record)
i = i + 1
}
} else {
i := 0
for i < 10 {
number = strconv.Itoa(i)
recBytes, err = stub.GetState("Record" + number)
if err != nil {
return shim.Error("Error get detail")
}
err = json.Unmarshal(recBytes, &record)
if err != nil {
return shim.Error("Error unmarshalling data")
}
records = append(records, record)
i = i + 1
}
}
recordsBytes, err := json.Marshal(&records)
if err != nil {
shim.Error("Error get records")
}
return shim.Success(recordsBytes)
}
/*
* 通过 Id 获取所存储的学历信息
* args[0] ID
*/
func (t *SimpleChaincode) getBackgroundById(stub shim.ChaincodeStubInterface, args []string) pb.Response {
backBytes, err := stub.GetState("BackGround" + args[0])
if err != nil {
return shim.Error("Error retrieving data")
}
return shim.Success(backBytes)
}
func writeRecord(stub shim.ChaincodeStubInterface, record Record) error {
var recID string
recordBytes, err := json.Marshal(&record)
if err != nil {
return err
}
recID = strconv.Itoa(record.Id)
err = stub.PutState("Record"+recID, recordBytes)
if err != nil {
return errors.New("PutState Error" + err.Error())
}
return nil
}
func writeSchool(stub shim.ChaincodeStubInterface, school School) error {
schBytes, err := json.Marshal(&school)
if err != nil {
return err
}
err = stub.PutState(school.Address, schBytes)
if err != nil {
return errors.New("PutState Error" + err.Error())
}
return nil
}
func writeStudent(stub shim.ChaincodeStubInterface, student Student) error {
stuBytes, err := json.Marshal(&student)
if err != nil {
return err
}
err = stub.PutState(student.Address, stuBytes)
if err != nil {
return errors.New("PutState Error" + err.Error())
}
return nil
}
func writeBackground(stub shim.ChaincodeStubInterface, background Background) error {
var backID string
backBytes, err := json.Marshal(&background)
if err != nil {
return err
}
backID = strconv.Itoa(background.Id)
err = stub.PutState("BackGround"+backID, backBytes)
if err != nil {
return errors.New("PutState Error" + err.Error())
}
return nil
}
/*
* 生成Address
*/
func GetAddress() (string, string, string) {
var address, priKey, pubKey string
b := make([]byte, 48)
if _, err := io.ReadFull(rand.Reader, b); err != nil {
return "", "", ""
}
h := md5.New()
h.Write([]byte(base64.URLEncoding.EncodeToString(b)))
address = hex.EncodeToString(h.Sum(nil))
priKey = address + "1"
pubKey = address + "2"
return address, priKey, pubKey
}
func main() {
err := shim.Start(new(SimpleChaincode))
if err != nil {
fmt.Printf("Error starting Simple chaincode: %s", err)
}
}