forked from kaisert/hyperchecker_demo
-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
200 lines (174 loc) · 5.4 KB
/
main.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
package main
import (
"fmt"
"github.com/hyperledger/fabric/core/chaincode/shim"
"github.com/hyperledger/fabric/protos/peer"
"time"
"encoding/json"
)
type ApartementRegister struct {
}
type Renter struct {
name string
surname string
movedIn time.Time
}
type Block struct {
id string
street string
number string
renters []Renter
nOfRooms string
}
//cache of blocks id
var blocks map[string]bool
func createId(street string, number string) string {
return fmt.Sprintf("%s%d", street, number)
}
//retrieve a block on the ledger
func getBlock(stub shim.ChaincodeStubInterface, key string) (*Block, error) {
var block Block
block_marshalled, err := stub.GetState(key)
err = json.Unmarshal(block_marshalled, &block)
return &block, err
}
//save a block on the ledger
func putBlock(stub shim.ChaincodeStubInterface, key string, block *Block) error {
block_marshalled, _ := json.Marshal(*block)
return stub.PutState(key, block_marshalled)
}
//returns information about a renter
func queryRenter(stub shim.ChaincodeStubInterface, street string, number string, name string) peer.Response {
id := createId(street, number)
if !blocks[id] {
return shim.Error(fmt.Sprintf("No block %s registered", id))
}
block, _ := getBlock(stub, id)
var renter *Renter
for _, r := range block.renters {
if r.name == name {
renter = &r
}
}
if renter == nil {
return shim.Error(fmt.Sprintf("Could not find renter %s in block %s", name, id))
} else {
renter_marshalled, _ := json.Marshal(*renter)
return shim.Success(renter_marshalled)
}
}
//registers a new renter in an apartmentblock
func registerNewRenter(stub shim.ChaincodeStubInterface, street string, number string, name string, surname string) peer.Response {
id := createId(street, number)
if !blocks[id] {
return shim.Error(fmt.Sprintf("No block %s registered", id))
}
block, err := getBlock(stub, id)
if err != nil {
return shim.Error(fmt.Sprintf("could not retrieve %s", id))
}
now := time.Now()
renter := Renter{
name: name,
surname: surname,
movedIn: now,
}
block.renters = append(block.renters, renter)
block_marshalled, _ := json.Marshal(*block)
err = stub.PutState(id, block_marshalled)
if err != nil {
return shim.Error(fmt.Sprintf("could not update %s", id))
}
block_marshalled, err = stub.GetState(id)
json.Unmarshal(block_marshalled, block)
return shim.Success([]byte(fmt.Sprintf("Block %s has now %d renters", id, len(block.renters))))
}
//creates a new block
func newBlock(stub shim.ChaincodeStubInterface, street string, number string, nOfRooms string) peer.Response {
id := createId(street, number)
if blocks[id] {
return shim.Error(fmt.Sprintf("Block %s already exists", id))
}
blocks[id] = true
block := new(Block)
block.id = id
block.street = street
block.number = number
block.nOfRooms = nOfRooms
putBlock(stub, id, block)
blocks_marshalled, _ := json.Marshal(blocks)
stub.PutState("blocksIdCache", blocks_marshalled)
return shim.Success([]byte(fmt.Sprintf("Successfully created block %s.", id)))
}
//returns the number of blocks
func blocksCount() peer.Response {
count := len(blocks)
return shim.Success([]byte(fmt.Sprintf("%d blocks found", count)))
}
//returns the number of renters in a specific block
func rentersCount(stub shim.ChaincodeStubInterface, street string, number string) peer.Response {
id := createId(street, number)
block, err := getBlock(stub, id)
if block == nil || err != nil {
return shim.Error(fmt.Sprintf("could not retrieve %s %d", street, number))
}
return shim.Success([]byte(fmt.Sprintf("%d renters in %s found", len(block.renters), block.id)))
}
//Finds an apartmentblock whithout any renters
func findEmptyBlock(stub shim.ChaincodeStubInterface) peer.Response {
for id, _ := range blocks {
block, err := getBlock(stub, id)
if err != nil {
shim.Error(fmt.Sprintf("Could not find block %s", id))
}
if len(block.renters) == 0 {
block_marshalled, err := json.Marshal(*block)
if err != nil {
return shim.Error("Could not marshal block.")
}
return shim.Success(block_marshalled)
}
}
return shim.Error("No blocks empty")
}
//Initialisation of the Chaincode
func (m *ApartementRegister) Init(stub shim.ChaincodeStubInterface) peer.Response {
blocks = make(map[string]bool)
return shim.Success([]byte("Successfully initialized Chaincode."))
}
//Entry Point of an invocation
func (m *ApartementRegister) Invoke(stub shim.ChaincodeStubInterface) peer.Response {
function, para := stub.GetFunctionAndParameters()
switch(function) {
case "queryRenter":
if len(para) < 3 {
return shim.Error("not enough arguments for queryRenter. 3 required")
} else {
return queryRenter(stub, para[0], para[1], para[2])
}
case "registerRenter":
if len(para) < 3 {
return shim.Error("not enough arguments for registerRenter. 4 required")
} else {
return registerNewRenter(stub, para[0], para[1], para[2], para[3])
}
case "newBlock":
return newBlock(stub, para[0], para[1], para[2])
case "blocksCount":
return blocksCount()
case "rentersCount":
if len(para) < 2 {
return shim.Error("not enough arguments for rentersCount. 2 required")
} else {
return rentersCount(stub, para[0], para[1])
}
case "findEmptyBlock":
return findEmptyBlock(stub)
}
return shim.Error(fmt.Sprintf("No function %s implemented", function))
}
func main() {
if err := shim.Start(new(ApartementRegister)); err != nil {
fmt.Printf("Error starting SimpleAsset chaincode: %s", err)
}
}