-
Notifications
You must be signed in to change notification settings - Fork 0
/
bchoc
executable file
·282 lines (232 loc) · 9.41 KB
/
bchoc
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
#!/usr/bin/env python3
from os import getenv
import argparse
from typing import *
from datetime import datetime, timezone
from uuid import UUID
from blockchain import Blockchain
def doAdd(args: argparse.Namespace):
bchain = Blockchain()
for id in args.itemIDs:
if bchain.get_block(id) != None:
print("Error: Evidence ID {} not unique.".format(id))
exit(-1)
caseID = UUID(args.caseID)
print("Case: {}".format(caseID))
for id in args.itemIDs:
bchain.write_block(caseID, id, "CHECKEDIN", "")
print("Added item: {}".format(id))
print(" Status: CHECKEDIN")
print(" Time of action: {}".format(datetime.now()))
def doCheckout(args: argparse.Namespace):
bchain = Blockchain()
id = args.itemID
block = bchain.get_block(id)
if block is not None:
state = block['state']
else:
print("Error: Evidence item {} does not exist. Please add it.".format(id))
exit(-1)
if state == "INITIAL":
print("Error: Evidence item {} is not a valid item.".format(id))
exit(-1)
elif state == "CHECKEDOUT":
print("Error: Evidence item {} is already checked out. Must check it in first.".format(id))
exit(-1)
elif state == "DISPOSED":
print("Error: Evidence item {} has been disposed. Cannot check it out.".format(id))
exit(-1)
elif state == "DESTROYED":
print("Error: Evidence item {} has been destroyed. Cannot check it out.".format(id))
exit(-1)
elif state == "RELEASED":
print("Error: Evidence item {} has been released. Cannot check it out.".format(id))
exit(-1)
# Checkout item
case = block['case_id']
bchain.write_block(case, id, "CHECKEDOUT", "")
print("Case: {}".format(case))
print("Checked out item: {}".format(id))
print(" Status: CHECKEDOUT")
print(" Time of action: {}".format(datetime.now()))
def doCheckin(args: argparse.Namespace):
bchain = Blockchain()
id = args.itemID
block = bchain.get_block(id)
if block is not None:
state = block['state']
else:
print("Error: Evidence item {} does not exist. Please add it.".format(id))
exit(-1)
if state == "INITIAL":
print("Error: Evidence item {} is not a valid item.".format(id))
exit(-1)
elif state == "CHECKEDIN":
print("Error: Evidence item {} is already checked in. Must check it out first.".format(id))
exit(-1)
elif state == "DISPOSED":
print("Error: Evidence item {} is disposed. Cannot check it in.".format(id))
exit(-1)
elif state == "DESTROYED":
print("Error: Evidence item {} is destroyed. Cannot check it in.".format(id))
exit(-1)
elif state == "RELEASED":
print("Error: Evidence item {} is released. Cannot check it in.".format(id))
exit(-1)
# Check in item
case = block['case_id']
bchain.write_block(case, id, "CHECKEDIN", "")
print("Case: {}".format(case))
print("Checked in item: {}".format(id))
print(" Status: CHECKEDIN")
print(" Time of action: {}".format(datetime.now()))
def print_blocks(blocks):
for block in blocks:
if block["state"] == "INITIAL":
print("Case: 00000000-0000-0000-0000-000000000000")
print("Item: 0")
else:
print(f"Case: {block['case_id']}")
print(f"Item: {block['item_id']}")
print(f"Action: {block['state']}")
print(f"Time: {block['timestamp'].isoformat()}")
print()
def doLog(args: argparse.Namespace):
bchain = Blockchain()
is_reversed = args.reversed
n = args.numEntries
case_id = args.caseID
item_id = args.itemID
blocks = bchain.read_blocks()
if is_reversed: blocks = reversed(blocks)
if case_id is not None:
blocks = filter(lambda block: str(block["case_id"]) == case_id, blocks)
if item_id is not None:
blocks = filter(lambda block: block["item_id"] == item_id, blocks)
print_blocks(blocks if n is None else blocks[:n])
def doRemove(args: argparse.Namespace):
bchain = Blockchain()
id = args.itemID
block = bchain.get_block(id)
if block is not None:
state = block['state']
else:
print("Error: Evidence item {} does not exist. Please add it.".format(id))
exit(-1)
if state != "CHECKEDIN":
print("Error: Evidence item {} must be checked in to remove it. Please check it in.".format(id))
exit(-1)
reason = args.reason
owner = args.owner # What happens if this is not given? None?
if reason == "RELEASED" and owner == None:
print("Error: Must give a reason in order to release.")
exit(-1)
elif reason == "DISPOSED" and owner != None:
print("Error: Disposed evidence does not need an owner.")
exit(-1)
elif reason == "DESTROYED" and owner != None:
print("Error: Destroyed evidence does not need an owner.")
exit(-1)
elif reason != "RELEASED" and reason != "DISPOSED" and reason != "DESTROYED":
print("Error: Not a valid reason to remove an item.")
exit(-1)
# Remove evidence
case = block['case_id']
if reason == "RELEASED":
bchain.write_block(case, id, "RELEASED", owner + "\x00")
print("Case: {}".format(case))
print("Released item: {}".format(id))
print(" Status: RELEASED")
print(" Time of action: {}".format(datetime.now()))
elif reason == "DISPOSED":
bchain.write_block(case, id, "DISPOSED", "")
print("Case: {}".format(case))
print("Disposed of item: {}".format(id))
print(" Status: DISPOSED")
print(" Time of action: {}".format(datetime.now()))
elif reason == "DESTROYED":
bchain.write_block(case, id, "DESTROYED", "")
print("Case: {}".format(case))
print("Destroyed item: {}".format(id))
print(" Status: DESTROYED")
print(" Time of action: {}".format(datetime.now()))
def doInit(args: argparse.Namespace):
bchain = Blockchain()
if bchain.check_init():
print("Verified the initial block.")
else:
print("The blockchain file provided is invalid.")
exit(-1)
def doVerify(args: argparse.Namespace):
doInit(args)
bchain = Blockchain()
if bchain.verify_valid():
print("Verified validity of blocks.")
else:
print("One or more blocks are invalid.")
exit(-1)
if bchain.verify_duplicate_parents():
print("Verified linear structure.")
else:
print("Multiple blocks have the same parent.")
exit(-1)
if bchain.verify_checksums():
print("Verified checksums.")
else:
print("The blockchain contains a block with incorrect checksum.")
exit(-1)
if bchain.verify_status_good():
print("Verified status values.")
else:
print("Invalid status value in one of the blocks.")
exit(-1)
if bchain.verify_releases_are_good():
print("Verified the integrity of release blocks.")
else:
print("A release operation done improperly.")
exit(-1)
if bchain.verify_add_is_first():
print("Verified the correct use of add method.")
else:
print("Operations ran on items before their addition.")
exit(-1)
if bchain.verify_remove_is_final():
print("Verified the correct use of remove method.")
else:
print("Operations ran on items after their removal.")
exit(-1)
if bchain.verify_check_order():
print("Verified the correct order of checkins and checkouts.")
else:
print("An item checked or checked out while in an improper state.")
exit(-1)
print("BLOCKCHAIN VERIFICATION FINISHED")
def loadChainFromFile(filePath):
pass
def main() -> None:
modes: List[str] = ["add", "checkout", "checkin", "log", "remove", "init", "verify"]
argumentParser: argparse.ArgumentParser = argparse.ArgumentParser()
subparsers = argumentParser.add_subparsers(dest="mode")
modeParsers: Dict[str, Any] = {
mode: subparsers.add_parser(mode) for mode in modes
}
modeParsers["add"].add_argument("-c", "--case_id", dest="caseID", type=str, required=True)
modeParsers["add"].add_argument("-i", "--item_id", dest="itemIDs", type=int, required=True, action="append")
modeParsers["checkout"].add_argument("-i", "--item_id", dest="itemID", type=int, required=True)
modeParsers["checkin"].add_argument("-i", "--item_id", dest="itemID", type=int, required=True)
modeParsers["log"].add_argument("-r", "--reverse", dest="reversed", action="store_true")
modeParsers["log"].add_argument("-n", "--num_entries", dest="numEntries", type=int)
modeParsers["log"].add_argument("-c", "--case_id", dest="caseID", type=str)
modeParsers["log"].add_argument("-i", "--item_id", dest="itemID", type=int)
modeParsers["remove"].add_argument("-i", "--item_id", dest="itemID", type=int, required=True)
modeParsers["remove"].add_argument("-y", "--why", dest="reason", type=str, required=True)
modeParsers["remove"].add_argument("-o", "--owner", dest="owner", type=str)
args: argparse.Namespace = argumentParser.parse_args()
modeFunctionName: str = f"do{args.mode.capitalize()}"
globalVals: Dict[str, Any] = globals()
if (modeFunctionName not in globalVals):
print(f'Function "{modeFunctionName}" not implemented')
exit(-1)
globalVals[modeFunctionName](args)
if __name__ == '__main__':
main()