Repository navigation
Expand file tree
/
Copy pathmtixcc.go
More file actions
488 lines (429 loc) · 15.7 KB
/
Copy pathmtixcc.go
File metadata and controls
488 lines (429 loc) · 15.7 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
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
package main
import (
"encoding/json"
"errors"
"fmt"
"strconv"
"time"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/protos/peer"
pb "github.com/hyperledger/fabric/protos/peer"
)
// SimpleAsset implements a simple chaincode to manage movie ticket sales
type SimpleAsset struct {
}
// MovieShowing represents a movie showtime including:
// showId (key), theater name, Hall name, movie name, date/time of the showing,
// ticket price, numSeats (max tickets), tixSold (number of tickets sold)
type MovieShowing struct {
Theater string
Hall string
Movie string
DateTime string
Price string
NumSeats string
TixSold string
}
// Init is called during chaincode instantiation to initialize data
func (t *SimpleAsset) Init(stub shim.ChaincodeStubInterface) peer.Response {
// Init is currently not used to initialize any data on the ledger
return shim.Success(nil)
}
// Invoke is called per transaction on the chaincode.
func (t *SimpleAsset) Invoke(stub shim.ChaincodeStubInterface) pb.Response {
// Extract the function and args from the transaction proposal
fn, args := stub.GetFunctionAndParameters()
result := "Success"
var err error
if fn == "ScheduleShow" {
result, err = ScheduleMovieShowing(stub, args)
} else if fn == "StockConcession" {
result, err = StockConcession(stub, args)
} else if fn == "BuyTix" {
result, err = BuyTix(stub, args)
} else if fn == "BuyConcession" {
result, err = BuyConcession(stub, args)
} else if fn == "TicketsAvailable" {
_, err = TicketsAvailable(stub, args)
} else if fn == "ConcessionsAvailable" {
_, err = ConcessionsAvailable(stub, args)
} else if fn == "SodasAvailable" {
_, err = SodasAvailable(stub, args)
} else if fn == "ExchangeWaterSoda" {
result, err = ExchangeWaterSoda(stub, args)
} else { // assume 'get' even if fn is nil
result, err = get(stub, args)
}
if err != nil {
return shim.Error(err.Error())
}
// Return the result as success payload
return shim.Success([]byte(result))
}
/*
TicketPurchase (created with transactions):
txnId showId quantity purchaseTime revenue numSodas seller
1 1 4 2019-02-05T13:00:00-05:00 quantity * price 4 W1
*/
type TicketPurchase struct {
TxnID string
ShowID string
Quantity string
PurchaseTime string
Revenue string
Seller string
}
// BuyTix purchases 1 or more tickets (quantity) for a specific movie showing
// from a specified seller (window number). This reduces the number of tix available
// The purchase transaction is added to the ledger.
// If the key exists, it will override the value with the new one
func BuyTix(stub shim.ChaincodeStubInterface, args []string) (string, error) {
printArgs("BuyTix", args)
if len(args) != 3 {
fmt.Println("BuyTix(): Incorrect number of arguments. Expecting 3")
return "", errors.New("BuyTix() : Incorrect number of arguments. Expecting 3 ")
}
var buytix TicketPurchase
buytix.TxnID = stub.GetTxID()
// args: showId, quantity, seller
buytix.ShowID = args[0]
buytix.Quantity = args[1]
//first, verify that enough tickets are available
showing, err := TicketsAvailable(stub, args[0:2])
if err != nil {
//not enough tickets available
fmt.Println(err)
return "", err
}
buytix.Seller = args[2]
// Set current date / time as purchase date
t := time.Now()
nowFormatted := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second())
buytix.PurchaseTime = nowFormatted //"2019-02-05T13:00:00-05:00"
//Compute revenue for this sale
var q, _ = strconv.Atoi(buytix.Quantity)
var p, _ = strconv.ParseFloat(showing.Price, 64)
var s, _ = strconv.Atoi(showing.TixSold)
rev := float64(q) * p
buytix.Revenue = fmt.Sprintf("%f", rev)
fmt.Println("BuyTix() - purchase attributes : ", buytix)
buytixJSON, err := json.Marshal(buytix)
if err != nil {
fmt.Println("buytix to buytixJSON error: ", err)
return "", err
}
fmt.Println("buytix to buytixJSON created: ", string(buytixJSON))
//update the show with the new number of purchased tickets
showing.TixSold = fmt.Sprintf("%d", s+q)
showJSON, err := json.Marshal(showing)
if err != nil {
fmt.Println("movieShowing to showJSON error: ", err)
return "", err
}
err = stub.PutState(buytix.ShowID, showJSON)
if err != nil {
return "", fmt.Errorf("BuyTix failed to update purchased quantity for: %s", buytix.ShowID)
}
err = stub.PutState(buytix.TxnID, buytixJSON)
if err != nil {
return "", fmt.Errorf("Failed to complete ticket purchase for: %s", string(buytixJSON))
}
results := fmt.Sprintf("Buy Tickets created ID=%s Movie=%s Quantity=%s Tickets sold=%s", buytix.TxnID, showing.Movie, buytix.Quantity, showing.TixSold)
return results, nil
}
// ScheduleMovieShowing schedules and stores the provided movie showing on the ledger.
// If the key exists, it will override the value with the new one
func ScheduleMovieShowing(stub shim.ChaincodeStubInterface, args []string) (string, error) {
printArgs("ScheduleMovieShowing", args)
txID := args[0]
show, err := CreateMovieShowing(args[1:])
if err != nil {
return "", err
}
showJSON, err := json.Marshal(show)
if err != nil {
fmt.Println("show to showJSON error: ", err)
return "", err
}
fmt.Println("show to showJSON created: ", string(showJSON))
err = stub.PutState(txID, showJSON)
if err != nil {
return "", fmt.Errorf("Failed to set asset: %s", args[0])
}
results := fmt.Sprintf("Show created ID=%s Movie=%s", txID, show.Movie)
return results, nil
}
// TicketsAvailable verifies a quantity of tickets are available for a given show
func TicketsAvailable(stub shim.ChaincodeStubInterface, args []string) (MovieShowing, error) {
printArgs("TicketsAvailable", args)
var show MovieShowing
if len(args) != 2 {
fmt.Println("TicketsAvailable(): Incorrect number of arguments. Expecting 3, received", len(args))
return show, fmt.Errorf("TicketsAvailable(): Incorrect number of arguments. Expecting 3, received %d", len(args))
}
// args: showId, quantity
showID := args[0]
quantity := args[1]
bytes, err := stub.GetState(showID)
if err != nil {
return show, fmt.Errorf("TicketsAvailable failed to get show information for: %s", showID)
}
err = json.Unmarshal(bytes, &show)
var q, _ = strconv.Atoi(quantity)
var n, _ = strconv.Atoi(show.NumSeats)
var s, _ = strconv.Atoi(show.TixSold)
var tixAvailable = n - s
fmt.Println("TicketsAvailable requesting ", quantity, " number available is ", tixAvailable)
if q > tixAvailable {
return show, fmt.Errorf("Error - Out of tickets: Attempting to buy %s tickets for %s, but only %d are available", quantity, showID, tixAvailable)
}
return show, nil
}
//CreateMovieShowing creates a new movie showing object
//including the Theater, Hall, date, time, price, etc.
func CreateMovieShowing(args []string) (MovieShowing, error) {
var aShow MovieShowing
printArgs("CreateMovieShowing", args)
if len(args) != 7 {
msg := fmt.Sprintf("CreateMovieShowing(): Incorrect number of arguments. Expecting 7 received %d", len(args))
return aShow, errors.New(msg)
}
aShow = MovieShowing{args[0], args[1], args[2], args[3], args[4], args[5], args[6]}
fmt.Println("CreateMovieShowing() - Show attributes : ", aShow)
return aShow, nil
}
// Get returns the value of the specified asset key
func get(stub shim.ChaincodeStubInterface, args []string) (string, error) {
if len(args) != 1 {
return "", fmt.Errorf("Incorrect arguments. Expecting a key")
}
value, err := stub.GetState(args[0])
if err != nil {
return "", fmt.Errorf("Failed to get asset: %s with error: %s", args[0], err)
}
if value == nil {
return "", fmt.Errorf("Asset not found: %s", args[0])
}
return string(value), nil
}
// Concession represents a refreshment for sale including:
// ConID (key), soda, water or popcorn
// Inventory, the # of units in stock
// Price, sales prices
type Concession struct {
Theater string
ConID string
Inventory string
Price string
}
// ConPurchase represents the purchase of a concession
// TxnID - unique key
// ConID type of concession
// Quantity # of units purchased
// PurchaseTime day/time of txn
// Revenue total revenue for sale
// ShowTime the movie show time
type ConPurchase struct {
TxnID string
Theater string
ConID string
Quantity string
Revenue string
PurchaseTime string
}
// SodaCount represents the number of Soda's purchased for each showTime
// a limit of 200 sodas per show is enforced
type SodaCount struct {
ShowTime string
Quantity string
}
// StockConcession sets the inventory of specified concession to the specified value
func StockConcession(stub shim.ChaincodeStubInterface, args []string) (string, error) {
printArgs("StockConcession", args)
if len(args) != 4 {
msg := fmt.Sprintf("StockConcession(): Incorrect number of arguments. Expecting 4 received %d", len(args))
return "", errors.New(msg)
}
// Use composite key for concessions including theater and concession type
//E.g. Regal1-popcorn
theater := args[0]
concession := args[1]
id := fmt.Sprintf("%s-%s", theater, concession)
con := Concession{theater, concession, args[2], args[3]}
conJSON, err := json.Marshal(con)
if err != nil {
fmt.Println("con to conJSON error: ", err)
return "", err
}
fmt.Println("con to conJSON created: ", string(conJSON))
err = stub.PutState(id, conJSON)
if err != nil {
return "", fmt.Errorf("Failed to stock: %s", args[0])
}
results := fmt.Sprintf("Stock created ConID=%s Quantity=%s", con.ConID, con.Inventory)
return results, nil
}
// ExchangeWaterSoda exchanges 1 water for 1 soda, for the provided Theater and showTime
func ExchangeWaterSoda(stub shim.ChaincodeStubInterface, args []string) (string, error) {
printArgs("BuyConcession", args)
if len(args) != 2 {
msg := fmt.Sprintf("ExchangeWaterSoda(): Incorrect number of arguments. Expecting 2 received %d", len(args))
return "", errors.New(msg)
}
theater := args[0]
showTime := args[1]
// first buy buy -1 water then buy 1 soda
newargs := [4]string{theater, "Water", "-1", ""}
var myArgs = newargs[:]
res, err := BuyConcession(stub, myArgs)
newargs = [4]string{theater, "Soda", "1", showTime}
myArgs = newargs[:]
res, err = BuyConcession(stub, myArgs)
return res, err
}
// BuyConcession purchases 1 or more units (quantity) for a specific concession type
// This reduces the inventory and the purchase transaction is added to the ledger.
func BuyConcession(stub shim.ChaincodeStubInterface, args []string) (string, error) {
printArgs("BuyConcession", args)
if len(args) != 4 {
msg := fmt.Sprintf("BuyConcession(): Incorrect number of arguments. Expecting 4 received %d", len(args))
return "", errors.New(msg)
}
var buycon ConPurchase
buycon.TxnID = stub.GetTxID()
// args: showId, quantity, seller
buycon.Theater = args[0]
buycon.ConID = args[1]
buycon.Quantity = args[2]
//first, verify that enough tickets are available
concession, err := ConcessionsAvailable(stub, args)
if err != nil {
//not enough available
fmt.Println(err)
return "", err
}
// Set current date / time as purchase date
t := time.Now()
nowFormatted := fmt.Sprintf("%d-%02d-%02dT%02d:%02d:%02d",
t.Year(), t.Month(), t.Day(),
t.Hour(), t.Minute(), t.Second())
buycon.PurchaseTime = nowFormatted //"2019-02-05T13:00:00-05:00"
//Compute revenue for this sale
var q, _ = strconv.Atoi(buycon.Quantity)
var p, _ = strconv.ParseFloat(concession.Price, 64)
var i, _ = strconv.Atoi(concession.Inventory)
rev := float64(q) * p
buycon.Revenue = fmt.Sprintf("%f", rev)
fmt.Println("BuyCon() - purchase attributes : ", buycon)
buyconJSON, err := json.Marshal(buycon)
if err != nil {
fmt.Println("buycon to buyconJSON error: ", err)
return "", err
}
fmt.Println("buycon to buyconJSON created: ", string(buyconJSON))
//update the conession with the new inventory count
concession.Inventory = fmt.Sprintf("%d", i-q)
conJSON, err := json.Marshal(concession)
if err != nil {
fmt.Println("concession to conJSON error: ", err)
return "", err
}
err = stub.PutState(concession.ConID, conJSON)
if err != nil {
return "", fmt.Errorf("BuyCon failed to update inventory for: %s", concession.ConID)
}
err = stub.PutState(buycon.TxnID, buyconJSON)
if err != nil {
return "", fmt.Errorf("Failed to complete ticket purchase for: %s", string(conJSON))
}
results := fmt.Sprintf("Buy Concession ConID=%s Quantity=%s Inventory=%s", concession.ConID, buycon.Quantity, concession.Inventory)
return results, nil
}
// ConcessionsAvailable verifies a quantity of concession units are available for a given type
func ConcessionsAvailable(stub shim.ChaincodeStubInterface, args []string) (Concession, error) {
printArgs("ConcessionsAvailable", args)
var con Concession
if len(args) != 4 {
msg := fmt.Sprintf("ConcessionsAvailable(): Incorrect number of arguments. Expecting 3, received %d", len(args))
return con, errors.New(msg)
}
// args: theater, concession type, quantity
theater := args[0]
conID := args[1]
quantity := args[2]
//showTime := args[3]
id := fmt.Sprintf("%s-%s", theater, conID)
bytes, err := stub.GetState(id)
if err != nil {
return con, fmt.Errorf("ConcessionsAvailable failed to get show information for: %s", conID)
}
err = json.Unmarshal(bytes, &con)
var q, _ = strconv.Atoi(quantity)
var i, _ = strconv.Atoi(con.Inventory)
fmt.Printf("ConcessionsAvailable requesting %s of %s. Inventory available is %s", quantity, con.ConID, con.Inventory)
if q > i {
return con, fmt.Errorf("Error - Out of Concessions: Attempting to buy %s units of %s at theater %s, but only %s are available", quantity, conID, theater, con.Inventory)
}
//Make sure there are enough sodas for this showTime
if conID == "soda" {
_, err = SodasAvailable(stub, args[2:])
if err != nil {
//not enough sodas available for this showtime
fmt.Println(err)
return con, err
}
}
return con, nil
}
// SodasAvailable verifies a quantity of sodas are available for a given showTime
func SodasAvailable(stub shim.ChaincodeStubInterface, args []string) (SodaCount, error) {
printArgs("SodasAvailable", args)
var scount SodaCount
if len(args) != 2 {
msg := fmt.Sprintf("SodasAvailable(): Incorrect number of arguments. Expecting 2, received %d", len(args))
return scount, errors.New(msg)
}
// args: theater, concession type, quantity
quantity := args[0]
showTime := args[1]
bytes, err := stub.GetState(showTime)
if err != nil {
return scount, fmt.Errorf("SodasAvailable() failed to get current quantity for showTime: %s", showTime)
}
err = json.Unmarshal(bytes, &scount)
curr, _ := strconv.Atoi(scount.Quantity)
new, _ := strconv.Atoi(quantity)
total := curr + new
if total > 200 {
return scount, fmt.Errorf("Error - Out of Concessions: 200 sodas is the limit for a single showTime. Attempting to buy %s units of sodas for showTime %s, but %s have been sold", quantity, showTime, scount.Quantity)
}
fmt.Printf("Sodas sold for showTime %s is %s, now requesting %s \n", showTime, scount.Quantity, quantity)
//update the showTime with the new number of purchased sodas
quantity = fmt.Sprintf("%d", total)
scount = SodaCount{showTime, quantity}
sodaJSON, err := json.Marshal(scount)
if err != nil {
fmt.Println("scount to sodaJSON error: ", err)
return scount, err
}
err = stub.PutState(showTime, sodaJSON)
if err != nil {
return scount, fmt.Errorf("SodasAvailable failed to update purchased quantity for showTime: %s", showTime)
}
return scount, nil
}
var queryString = "{\"selector\": {\"Hall\": \"hall1\"}}"
// main function starts up the chaincode in the container during instantiate
func main() {
if err := shim.Start(new(SimpleAsset)); err != nil {
fmt.Printf("Error starting SimpleAsset chaincode: %s", err)
}
}
func printArgs(caller string, args []string) {
fmt.Println(caller, "() args: ")
for i := 0; i < len(args); i++ {
fmt.Println("args:", i, args[i])
}
}