This repository was archived by the owner on May 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.go
More file actions
413 lines (380 loc) · 13.9 KB
/
database.go
File metadata and controls
413 lines (380 loc) · 13.9 KB
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
package main
import (
"fmt"
"strconv"
"encoding/json"
"strings"
"regexp"
"github.com/inklabsfoundation/inkchain/core/chaincode/shim"
pb "github.com/inklabsfoundation/inkchain/protos/peer"
)
const (
// invoke func name
MarkAsSchool = "markAsSchool"
UnmarkAsSchool = "unmarkAsSchool"
SetTranscript = "setTranscript"
QueryTranscript = "queryTranscript"
OfferInsuranceOrder = "offerInsuranceOrder"
AcceptInsuranceOrder = "acceptInsuranceOrder"
ExerciseInsuranceOrder = "exerciseInsuranceOrder"
AbandonInsuranceOrder = "abandonInsuranceOrder"
EnforceInsuranceOrder = "enforceInsuranceOrder"
Debug = "debug"
)
const (
// TRANSCRIPT_School_TranscriptId
TranscriptPrefix = "TRANSCRIPT_"
// INSURANCE_InsuranceId
InsuranceOrderPrefix = "INSURANCE_"
// SCHOOL_School
SchoolPrefix = "SCHOOL_"
OneDayUnixTimeSpan = 86400
)
var GuidRegex *regexp.Regexp = regexp.MustCompile("^[0-9a-fA-F]{32}$")
// Demo chaincode for asset registering, querying and transferring
type DatabaseChaincode struct {
}
type DecryptedTranscript struct {
Student string `json:"student"`
Year uint `json:"year"`
Term uint `json:"term"`
Course string `json:"course"`
Credit uint `json:"credit"`
Grade uint `json:"grade"`
}
type InsuranceOrderCall struct {
School string `json:"school"`
Student string `json:"student"` // long
Year uint64 `json:"year"`
Term uint64 `json:"term"`
Credit uint64 `json:"credit"`
Writer string `json:"writer"` // short
Strike uint64 `json:"strike"` // grade < strike <=> should exercise
Premium uint64 `json:"premium"` // amount paid to accept the order
Payoff uint64 `json:"payoff"` // amount paid if exercised
OfferExpiry uint64 `json:"offer_expiry"` // must be accepted before this UNIX timestamp
Accepted bool `json:"accepted"`
RightExpiry uint64 `json:"right_expiry"` // must be exercised before this UNIX timestamp
Exercised bool `json:"exercised"`
Forced bool `json:"forced"`
}
func main() {
err := shim.Start(new(DatabaseChaincode))
if err != nil {
fmt.Printf("Error starting DatabaseChaincode: %s", err)
}
}
func (t *DatabaseChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
fmt.Println("DatabaseChaincode is initializing.")
args := stub.GetStringArgs()
if args == nil || len(args) != 2 {
return shim.Error("INVALID_ARGUMENTS: Deploy(bureauOfEducationId).")
}
stub.PutState("BureauOfEducationId", []byte(args[1]))
return shim.Success([]byte("Initialization was successful."))
}
func (t *DatabaseChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
function, args := stub.GetFunctionAndParameters()
switch function {
case MarkAsSchool:
// sender = bureau of education
// args[0] = school address
return t.markAsSchool(stub, args)
case UnmarkAsSchool:
// sender = bureau of education
// args[0] = school address
return t.unmarkAsSchool(stub, args)
case SetTranscript:
// sender is the school
// args[0] = transcript id
// args[1] = encrypted transcript
return t.setTranscript(stub, args)
case QueryTranscript:
// args[0] = school id
// args[1] = transcript id
return t.queryTranscript(stub, args)
case OfferInsuranceOrder:
// args[0] = insurance id
// args[1] = school id
// args[2] = student id = long
// args[3] = year
// args[4] = term
// args[5] = credit
// sender = writer = short
// args[6] = strike
// args[7] = premium
// args[8] = payoff
// args[9] = offer_expiry
// args[10] = right_expiry
return t.offerInsuranceOrder(stub, args)
case AcceptInsuranceOrder:
// sender = student id
// args[0] = insurance id
return t.acceptInsuranceOrder(stub, args)
case ExerciseInsuranceOrder:
// sender = writer
// args[0] = insurance id
return t.exerciseInsuranceOrder(stub, args)
case AbandonInsuranceOrder:
// sender = student id
// args[0] = insurance id
return t.abandonInsuranceOrder(stub, args)
case EnforceInsuranceOrder:
// sender = student id
// args[0] = insurance id
// args[1] = transcript id
// args[2] = secret key
return t.enforceInsuranceOrder(stub, args)
case Debug:
return t.debug(stub, args)
}
return shim.Error("NO_INTERFACE: unknown function invoked.")
}
func (t *DatabaseChaincode) markAsSchool(stub shim.ChaincodeStubInterface, args []string) pb.Response {
sender, err := stub.GetSender()
if err != nil {
return shim.Error("E_UNEXPECTED: GetSender failed.")
}
sender = strings.ToLower(sender)
bureauId := "i" + sender
bureauEduId, err := stub.GetState("BureauOfEducationId")
if err != nil || bureauEduId == nil {
return shim.Error("UNEXPECTED: could not retrieve BureauOfEducationId.")
}
bureauOfEducationId := string(bureauId)
if bureauId != bureauOfEducationId {
return shim.Error("ACCESS_DENIED: MarkAsSchool can only be called by the bureau of education.")
}
if len(args) != 1 {
return shim.Error("INVALID_ARGUMENTS: MarkAsSchool(schoolAddress).")
}
schoolId := args[0]
if stub.PutState(SchoolPrefix + schoolId, []byte { 1 }) != nil {
return shim.Error("E_UNEXPECTED: could not mark as school.")
}
return shim.Success([]byte("Marked as school."))
}
func (t *DatabaseChaincode) unmarkAsSchool(stub shim.ChaincodeStubInterface, args []string) pb.Response {
sender, err := stub.GetSender()
if err != nil {
return shim.Error("E_UNEXPECTED: GetSender failed.")
}
sender = strings.ToLower(sender)
bureauId := "i" + sender
bureauEduId, err := stub.GetState("BureauOfEducationId")
if err != nil || bureauEduId == nil {
return shim.Error("UNEXPECTED: could not retrieve BureauOfEducationId.")
}
bureauOfEducationId := string(bureauId)
if bureauId != bureauOfEducationId {
return shim.Error("ACCESS_DENIED: UnmarkAsSchool can only be called by the bureau of education.")
}
if len(args) != 1 {
return shim.Error("INVALID_ARGUMENTS: UnmarkAsSchool(schoolAddress).")
}
schoolId := args[0]
if stub.PutState(SchoolPrefix + schoolId, []byte { 0 }) != nil {
return shim.Error("E_UNEXPECTED: could not unmark as school.")
}
return shim.Success([]byte("Unmarked as school."))
}
func (t *DatabaseChaincode) setTranscript(stub shim.ChaincodeStubInterface, args []string) pb.Response {
sender, err := stub.GetSender()
if err != nil {
return shim.Error("E_UNEXPECTED: GetSender failed.")
}
sender = strings.ToLower(sender)
schoolId := "i" + sender
isSchool, err := stub.GetState(SchoolPrefix + schoolId)
if err != nil || isSchool[0] != 1 {
return shim.Error("ACCESS_DENIED: SetTranscript can only be called by a school.")
}
if len(args) != 2 {
return shim.Error("INVALID_ARGUMENTS: SetTranscript(transcriptId, transcriptPayload).")
}
if !GuidRegex.MatchString(args[0]) {
return shim.Error("INVALID_ARGUMENTS: transcriptId must be N-formatted GUID.")
}
transcriptId := strings.ToLower(args[0])
transcript := args[1]
if stub.PutState(TranscriptPrefix + schoolId + "_" + transcriptId, []byte(transcript)) != nil {
return shim.Error("E_UNEXPECTED: could not add or update transcript.")
}
return shim.Success([]byte("Added or updated transcript."))
}
func (t *DatabaseChaincode) queryTranscript(stub shim.ChaincodeStubInterface, args []string) pb.Response {
if len(args) != 2 {
return shim.Error("INVALID_ARGUMENTS: QueryTranscript(schoolId, transcriptId).")
}
if !GuidRegex.MatchString(args[1]) {
return shim.Error("INVALID_ARGUMENTS: transcriptId must be N-formatted GUID.")
}
schoolId := args[0]
transcriptId := strings.ToLower(args[1])
result, err := stub.GetState(TranscriptPrefix + schoolId + "_" + transcriptId)
if err != nil || result == nil {
return shim.Error("NOT_FOUND: the transcript is not found.")
}
return shim.Success(result)
}
func (t *DatabaseChaincode) offerInsuranceOrder(stub shim.ChaincodeStubInterface, args []string) pb.Response {
sender, err := stub.GetSender()
if err != nil {
return shim.Error("E_UNEXPECTED: GetSender failed.")
}
sender = strings.ToLower(sender)
txTs, err := stub.GetTxTimestamp()
if err != nil {
return shim.Error("E_UNEXPECTED: GetTxTimestamp failed.")
}
unixNow := uint64(txTs.Seconds)
if len(args) != 11 {
return shim.Error("INVALID_ARGUMENTS: OfferInsuranceOrder(insuranceId, schoolId, studentId, year, term, credit, strike, premium, payoff, offer_expiry, right_expiry).")
}
if !GuidRegex.MatchString(args[0]) {
return shim.Error("INVALID_ARGUMENTS: insuranceId must be N-formatted GUID.")
}
insuranceId := strings.ToLower(args[0])
insurance := InsuranceOrderCall { Accepted: false, Exercised: false, Forced: false }
insurance.School = args[1]
insurance.Student = args[2]
insurance.Year, err = strconv.ParseUint(args[3], 10, 32)
if err != nil || insurance.Year < 2010 || insurance.Year > 2099 {
return shim.Error("INVALID_ARGUMENTS: year must be between 2000 and 2099 (inclusive).")
}
insurance.Term, err = strconv.ParseUint(args[4], 10, 32)
if err != nil || insurance.Term > 5 {
return shim.Error("INVALID_ARGUMENTS: term must be 0, 1, 2 or 3.")
}
insurance.Credit, err = strconv.ParseUint(args[5], 10, 32)
if err != nil {
return shim.Error("INVALID_ARGUMENTS: credit must fit into uint32.")
}
insurance.Writer = "i" + sender
insurance.Strike, err = strconv.ParseUint(args[6], 10, 32)
if err != nil {
return shim.Error("INVALID_ARGUMENTS: strike must fit into uint32.")
}
insurance.Premium, err = strconv.ParseUint(args[7], 10, 32)
if err != nil {
return shim.Error("INVALID_ARGUMENTS: premium must fit into uint32.")
}
insurance.Payoff, err = strconv.ParseUint(args[8], 10, 32)
if err != nil || insurance.Payoff <= insurance.Premium {
return shim.Error("INVALID_ARGUMENTS: payoff must be greater than premium.")
}
insurance.OfferExpiry, err = strconv.ParseUint(args[9], 10, 64)
if err != nil || insurance.OfferExpiry < unixNow + 7 * OneDayUnixTimeSpan {
return shim.Error("INVALID_ARGUMENTS: offer_expiry must be at least 7 days more from now.")
}
insurance.RightExpiry, err = strconv.ParseUint(args[10], 10, 64)
if err != nil || insurance.RightExpiry < unixNow + 30 * OneDayUnixTimeSpan {
return shim.Error("INVALID_ARGUMENTS: right_expiry must be at least 30 days more from now.")
}
return shim.Error("E_NOTIMPL: to be implemented " + insuranceId + ".")
}
func (t *DatabaseChaincode) acceptInsuranceOrder(stub shim.ChaincodeStubInterface, args []string) pb.Response {
sender, err := stub.GetSender()
if err != nil {
return shim.Error("E_UNEXPECTED: GetSender failed.")
}
sender = strings.ToLower(sender)
studentId := "i" + sender
txTs, err := stub.GetTxTimestamp()
if err != nil {
return shim.Error("E_UNEXPECTED: GetTxTimestamp failed.")
}
unixNow := uint64(txTs.Seconds)
if unixNow == 0 {
return shim.Error("E_TIME: " + sender + "?")
}
if len(args) != 1 {
return shim.Error("INVALID_ARGUMENTS: AcceptInsuranceOrder(insuranceId).")
}
if !GuidRegex.MatchString(args[0]) {
return shim.Error("INVALID_ARGUMENTS: insuranceId must be N-formatted GUID.")
}
insuranceId := strings.ToLower(args[0])
return shim.Error("E_NOTIMPL: to be implemented, " + studentId + ", " + insuranceId + ".")
}
func (t *DatabaseChaincode) exerciseInsuranceOrder(stub shim.ChaincodeStubInterface, args []string) pb.Response {
sender, err := stub.GetSender()
if err != nil {
return shim.Error("E_UNEXPECTED: GetSender failed.")
}
sender = strings.ToLower(sender)
writerId := "i" + sender
txTs, err := stub.GetTxTimestamp()
if err != nil {
return shim.Error("E_UNEXPECTED: GetTxTimestamp failed.")
}
unixNow := uint64(txTs.Seconds)
if unixNow == 0 {
return shim.Error("E_TIME: " + sender + "?")
}
if len(args) != 1 {
return shim.Error("INVALID_ARGUMENTS: ExerciseInsuranceOrder(insuranceId).")
}
if !GuidRegex.MatchString(args[0]) {
return shim.Error("INVALID_ARGUMENTS: insuranceId must be N-formatted GUID.")
}
insuranceId := strings.ToLower(args[0])
return shim.Error("E_NOTIMPL: to be implemented, " + writerId + ", " + insuranceId + ".")
}
func (t *DatabaseChaincode) abandonInsuranceOrder(stub shim.ChaincodeStubInterface, args []string) pb.Response {
sender, err := stub.GetSender()
if err != nil {
return shim.Error("E_UNEXPECTED: GetSender failed.")
}
sender = strings.ToLower(sender)
studentId := "i" + sender
txTs, err := stub.GetTxTimestamp()
if err != nil {
return shim.Error("GetTxTimestamp failed.")
}
unixNow := uint64(txTs.Seconds)
if unixNow != 0 {
return shim.Error("E_TIME: " + sender + ".")
}
if len(args) != 1 {
return shim.Error("INVALID_ARGUMENTS: AbandonInsuranceOrder(insuranceId).")
}
if !GuidRegex.MatchString(args[0]) {
return shim.Error("INVALID_ARGUMENTS: insuranceId must be N-formatted GUID.")
}
insuranceId := strings.ToLower(args[0])
return shim.Error("E_NOTIMPL: to be implemented, " + studentId + ", " + insuranceId + ".")
}
func (t *DatabaseChaincode) enforceInsuranceOrder(stub shim.ChaincodeStubInterface, args []string) pb.Response {
sender, err := stub.GetSender()
if err != nil {
return shim.Error("E_UNEXPECTED: GetSender failed.")
}
sender = strings.ToLower(sender)
studentId := "i" + sender
txTs, err := stub.GetTxTimestamp()
if err != nil {
return shim.Error("E_UNEXPECTED: GetTxTimestamp failed.")
}
unixNow := uint64(txTs.Seconds)
if unixNow != 0 {
return shim.Error("E_TIME: " + sender + ".")
}
if len(args) != 3 {
return shim.Error("INVALID_ARGUMENTS: EnforceInsuranceOrder(insuranceId, transcriptId, secretKey).")
}
if !GuidRegex.MatchString(args[0]) {
return shim.Error("INVALID_ARGUMENTS: insuranceId must be N-formatted GUID.")
}
if !GuidRegex.MatchString(args[1]) {
return shim.Error("INVALID_ARGUMENTS: transcriptId must be N-formatted GUID.")
}
insuranceId := strings.ToLower(args[0])
transcriptId := strings.ToLower(args[1])
secretKey := args[2]
return shim.Error("E_NOTIMPL: to be implemented, " + studentId + ", " + insuranceId + ", " + transcriptId + ", " + secretKey + ".")
}
func (t *DatabaseChaincode) debug(stub shim.ChaincodeStubInterface, args []string) pb.Response {
json.Marshal(nil)
return shim.Error("ACCESS_DENIED: undebuggable.")
}