-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcars.go
97 lines (77 loc) · 2.66 KB
/
cars.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
// Simple CRUD chaincode for store information about cars
package cars
import (
"time"
"github.com/hyperledger-labs/cckit/extensions/owner"
"github.com/hyperledger-labs/cckit/router"
p "github.com/hyperledger-labs/cckit/router/param"
)
const CarEntity = `CAR`
const CarRegisteredEvent = `CAR_REGISTERED`
// CarPayload chaincode method argument
type CarPayload struct {
Id string
Title string
Owner string
}
// Car struct for chaincode state
type Car struct {
Id string
Title string
Owner string
UpdatedAt time.Time // set by chaincode method
}
// Key for car entry in chaincode state
func (c Car) Key() ([]string, error) {
return []string{CarEntity, c.Id}, nil
}
func New() *router.Chaincode {
r := router.New(`cars`) // also initialized logger with "cars" prefix
r.Init(invokeInit)
r.Group(`car`).
Query(`List`, queryCars). // chain code method name is carList
Query(`Get`, queryCar, p.String(`id`)). // chain code method name is carGet, method has 1 string argument "id"
Invoke(`Register`, invokeCarRegister, p.Struct(`car`, &CarPayload{}), // 1 struct argument
owner.Only) // allow access to method only for chaincode owner (authority)
return router.NewChaincode(r)
}
// ======= Init ==================
func invokeInit(c router.Context) (interface{}, error) {
return owner.SetFromCreator(c)
}
// ======= Chaincode methods =====
// car get info chaincode method handler
func queryCar(c router.Context) (interface{}, error) {
// get state entry by composite key using CarKeyPrefix and car.Id
// and unmarshal from []byte to Car struct
return c.State().Get(&Car{Id: c.ParamString(`id`)})
}
// cars car list chaincode method handler
func queryCars(c router.Context) (interface{}, error) {
return c.State().List(
CarEntity, // get list of state entries of type CarKeyPrefix
&Car{}) // unmarshal from []byte and append to []Car slice
}
// carRegister car register chaincode method handler
func invokeCarRegister(c router.Context) (interface{}, error) {
// arg name defined in router method definition
p := c.Param(`car`).(CarPayload)
t, _ := c.Time() // tx time
car := &Car{ // data for chaincode state
Id: p.Id,
Title: p.Title,
Owner: p.Owner,
UpdatedAt: t,
}
// trigger multiple event
if err := c.Event().Set(CarRegisteredEvent+`First`, car); err != nil {
return nil, err
}
if err := c.Event().Set(CarRegisteredEvent, car); err != nil {
return nil, err
}
return car, // peer.Response payload will be json serialized car data
//put json serialized data to state
// create composite key using CarKeyPrefix and car.Id
c.State().Insert(car)
}