-
Notifications
You must be signed in to change notification settings - Fork 0
/
project.mo
369 lines (330 loc) · 15.7 KB
/
project.mo
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
import Int "mo:base/Int";
import Option "mo:base/Option";
import Buffer "mo:base/Buffer";
import Result "mo:base/Result";
import Nat "mo:base/Nat";
import Text "mo:base/Text";
import Bool "mo:base/Bool";
import HashMap "mo:base/HashMap";
import TrieMap "mo:base/TrieMap";
import Hash "mo:base/Hash";
import Iter "mo:base/Iter";
import Principal "mo:base/Principal";
import Order "mo:base/Order";
import Array "mo:base/Array";
import Debug "mo:base/Debug";
import IC "IC";
import Calculator "Calculator";
import Verifier "Verifier";
import Error "mo:base/Error";
import Time "mo:base/Time";
actor bc2305 {
//=============== DAY 5 - THE VERIFIER ===============\\
// https://github.com/motoko-bootcamp/motoko-starter/tree/main/days/day-5/project
type CanisterId = IC.CanisterId;
type CanisterSettings = IC.CanisterSettings;
type ManagementCanister = IC.ManagementCanister;
type CanisterStatus = IC.CanisterStatus;
type StudentProfile = Verifier.StudentProfile;
type TestError = Verifier.TestError;
type TestResult = Verifier.TestResult;
type CalculatorOperation = Verifier.CalculatorOperation;
let managementCanisterId : Text = "aaaaa-aa";
stable var studentProfileStoreEntries : [(Principal, StudentProfile)] = [];
stable var verifyStoreEntries : [(Principal, Principal)] = [];
stable var logStoreEntries : [(Time.Time, Text)] = [];
// Create an in-memory array of neurons so we can work with them easier during canister operation. These will be saved to stable memory during upgrades.
let studentProfileStore = HashMap.fromIter<Principal, StudentProfile>(studentProfileStoreEntries.vals(), Iter.size(studentProfileStoreEntries.vals()), Principal.equal, Principal.hash);
let verifyStore = HashMap.fromIter<Principal, Principal>(verifyStoreEntries.vals(), Iter.size(verifyStoreEntries.vals()), Principal.equal, Principal.hash);
let logStore = HashMap.fromIter<Time.Time, Text>(logStoreEntries.vals(), Iter.size(logStoreEntries.vals()), Int.equal, Int.hash);
var logMessage : Text = "";
// Part 1 - Profile Management
public shared ({ caller }) func addMyProfile(profile : StudentProfile) : async Result.Result<(), Text> {
Debug.print("Adding profile for " # debug_show (caller));
studentProfileStore.put(caller, profile);
return #ok();
};
public shared ({ caller }) func updateMyProfile(profile : StudentProfile) : async Result.Result<(), Text> {
Debug.print("Updating profile for " # debug_show (caller));
return await updateProfile(profile, caller);
};
public func updateProfile(profile : StudentProfile, principalId : Principal) : async Result.Result<(), Text> {
Debug.print("Updating profile for " # debug_show (principalId));
let result : ?StudentProfile = studentProfileStore.get(principalId);
switch (result) {
case (null) {
return #err("No student profile found for principal " # debug_show (principalId) # ".");
};
case (?record) {
let newRecord : StudentProfile = {
name = switch (Text.size(profile.name)) {
case (0) { record.name };
case (_) { profile.name };
};
team = switch (Text.size(profile.team)) {
case (0) { record.team };
case (_) { profile.team };
};
graduate = profile.graduate;
};
studentProfileStore.put(principalId, newRecord);
return #ok();
};
};
};
public shared ({ caller }) func deleteMyProfile() : async Result.Result<(), Text> {
Debug.print("Deleting profile for " # debug_show (caller));
let result : ?StudentProfile = studentProfileStore.get(caller);
switch (result) {
case (null) {
return #err("You do not have a student profile that you can delete.");
};
case (?record) {
studentProfileStore.delete(caller);
return #ok();
};
};
};
public func seeAProfile(p : Principal) : async Result.Result<StudentProfile, Text> {
Debug.print("Checking profile for " # debug_show (p));
let result : ?StudentProfile = studentProfileStore.get(p);
switch (result) {
case (null) {
return #err("No student profile record found for the supplied principal.");
};
case (?record) { return #ok(record) };
};
};
public query func seeAllProfiles() : async [StudentProfile] {
Debug.print("Displaying all profiles.");
return Iter.toArray<StudentProfile>(studentProfileStore.vals());
};
public query func seeAllVerificationRequests() : async [(Principal, Principal)] {
Debug.print("Displaying all verification requests.");
return Iter.toArray<(Principal, Principal)>(verifyStore.entries());
};
public func clearVerificationRequests() : async () {
Debug.print("Clearing all verification requests.");
for (key in verifyStore.keys()) {
verifyStore.delete(key);
};
verifyStoreEntries := [];
};
public query func seeAllLogMessages() : async [(Time.Time, Text)] {
Debug.print("Displaying all log messages.");
return Iter.toArray<(Time.Time, Text)>(logStore.entries());
};
public func clearLogMessages() : async () {
Debug.print("Clearing all log messages.");
for (key in logStore.keys()) {
logStore.delete(key);
};
logStoreEntries := [];
};
//Part 2 - Calculator Test
public shared ({ caller }) func test(p : Principal) : async TestResult {
logMessage := "Testing for " # debug_show (p);
Debug.print(logMessage);
logStore.put(Time.now(), logMessage);
// TODO: Change this to "true" for local testing.
let isLocal : Bool = Principal.isAnonymous(p);
var testActor = actor (Principal.toText(p)) : actor {
add : shared (n : Int) -> async Int;
sub : shared (n : Nat) -> async Int;
reset : shared () -> async Int;
};
if (isLocal) {
testActor := await Calculator.Calculator();
logStore.put(Time.now(), "Using local actor");
};
var opResult : Result.Result<Int, Text> = await performCalculatorOperation(testActor, #reset);
var expectedVal : Int = 0;
switch (opResult) {
// Capture unexpected errors
case (#err(err)) {
return #err(#UnexpectedError(err));
};
// Capture incorrect value errors
case (#ok(n)) {
if (n != expectedVal) {
return #err(#UnexpectedValue("n should be " # debug_show (expectedVal) # ", but is instead " # debug_show (n)));
};
};
};
opResult := await performCalculatorOperation(testActor, #add(10));
expectedVal := 10;
switch (opResult) {
// Capture unexpected errors
case (#err(err)) {
return #err(#UnexpectedError(err));
};
// Capture incorrect value errors
case (#ok(n)) {
if (n != expectedVal) {
return #err(#UnexpectedValue("n should be " # debug_show (expectedVal) # ", but is instead " # debug_show (n)));
};
};
};
opResult := await performCalculatorOperation(testActor, #sub(5));
expectedVal := 5;
switch (opResult) {
// Capture unexpected errors
case (#err(err)) {
return #err(#UnexpectedError(err));
};
// Capture incorrect value errors
case (#ok(n)) {
if (n != expectedVal) {
return #err(#UnexpectedValue("n should be " # debug_show (expectedVal) # ", but is instead " # debug_show (n)));
};
};
};
/*opResult := await performCalculatorOperation(testActor, #nonExistent);
expectedVal := 0;
switch (opResult) {
// Capture unexpected errors
case (#err(err)) {
return #err(#UnexpectedError(err));
};
// Capture incorrect value errors
case (#ok(n)) {
if (n != expectedVal) {
return #err(#UnexpectedValue("n should be " # debug_show(expectedVal) # ", but is instead " # debug_show(n)));
};
};
};*/
return #ok();
};
private func performCalculatorOperation(
testActor : actor {
add : shared (n : Int) -> async Int;
sub : shared (n : Nat) -> async Int;
reset : shared () -> async Int;
},
operation : CalculatorOperation,
) : async Result.Result<Int, Text> {
try {
switch (operation) {
case (#add(n)) {
return #ok(await testActor.add(n));
};
case (#sub(n)) {
return #ok(await testActor.sub(n));
};
case (#reset) {
return #ok(await testActor.reset());
};
case (#nonExistent) {
return #err("Error executing operation " # debug_show (operation));
};
};
} catch (e) {
Debug.print("Error executing operation " # debug_show (operation) # ". Details: " # debug_show (Error.code(e)) # " " # debug_show (Error.message(e)));
return #err("Error executing operation " # debug_show (operation));
};
};
//Part 3 - Verifying the controller of the calculator
// In this section we want to make sure that the owner of the verified canister is actually the student that registered it.
// Otherwise, a student could use the canister of another one.
public shared ({ caller }) func verifyOwnership(canisterId : CanisterId, principalId : Principal) : async Bool {
logMessage := "Verifying ownership of canister " # debug_show (canisterId) # " for principal " # debug_show (principalId);
Debug.print(logMessage);
logStore.put(Time.now(), logMessage);
verifyStore.put(canisterId, principalId);
var managementCanistor = actor (managementCanisterId) : ManagementCanister;
try {
let canisterStatus : CanisterStatus = await managementCanistor.canister_status({
canister_id : CanisterId = canisterId;
});
logStore.put(Time.now(), "Canister status: " # debug_show (canisterStatus));
let controllers : [Principal] = canisterStatus.settings.controllers;
logStore.put(Time.now(), "Controllers: " # debug_show (controllers));
for (controller in controllers.vals()) {
if (Principal.equal(principalId, controller)) {
logStore.put(Time.now(), "Controller matches principalId");
return true;
};
};
logStore.put(Time.now(), "None of the controllers match the principalId");
return false;
} catch (e) {
// Currently, the canister_status method of the management canister can only be used when the canister calling it is
// also one of the controller of the canister you are trying to check the status.
// Fortunately there is a trick to still get the controller!
logMessage := "Error verifying ownership. Details: " # debug_show (Error.code(e)) # " " # debug_show (Error.message(e));
Debug.print(logMessage);
logStore.put(Time.now(), logMessage);
let controllers : [Principal] = IC.parseControllersFromCanisterStatusErrorIfCallerNotController(Error.message(e));
logStore.put(Time.now(), "Controllers: " # debug_show (controllers));
for (controller in controllers.vals()) {
if (Principal.equal(principalId, controller)) {
logStore.put(Time.now(), "Controller matches principalId");
return true;
};
};
logStore.put(Time.now(), "None of the controllers match the principalId");
return false;
};
return false;
};
//Part 4 - Graduation
public shared ({ caller }) func verifyWork(canisterId : CanisterId, principalId : Principal) : async Result.Result<(), Text> {
logMessage := "Verifying work of canister " # debug_show (canisterId) # " for principal " # debug_show (principalId);
Debug.print(logMessage);
logStore.put(Time.now(), logMessage);
verifyStore.put(canisterId, principalId);
// Verify that the principalId is a controller of the canisterId
logMessage := "Verify that the principalId is a controller of the canisterId";
Debug.print(logMessage);
logStore.put(Time.now(), logMessage);
let ownershipVerified : Bool = await verifyOwnership(canisterId, principalId);
if (ownershipVerified != true) {
return #err("Ownership verification failure.");
};
// Verify that the principalId is a controller of the canisterId
let testPassed : TestResult = await test(canisterId);
switch (testPassed) {
case (#ok) {
let result : Result.Result<StudentProfile, Text> = await seeAProfile(principalId);
switch (result) {
case (#err(err)) {
return #err("No student profile found. Cannot update graduation status.");
};
case (#ok(sp)) {
let np : StudentProfile = {
name = sp.name;
team = sp.team;
graduate = true;
};
ignore await updateProfile(np, principalId);
return #ok();
};
};
};
case (#err(#UnexpectedValue(err))) {
return #err("Test failed - unexpected value: " # debug_show (err));
};
case (#err(#UnexpectedError(err))) {
return #err("Test failed - unexpected error: " # debug_show (err));
};
};
};
//---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
// REGION: UPGARDE MANAGEMENT ---------- ---------- ---------- ---------- ----------
//---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
// Make a final update to stable variables, before the runtime commits their values to Internet Computer stable memory, and performs an upgrade.
// So, here we want to take our values that are in our HashMap (and not stable) and put them into the stable array instead.
// Ref: https://internetcomputer.org/docs/current/developer-docs/build/cdks/motoko-dfinity/upgrades#preupgrade-and-postupgrade-system-methods
system func preupgrade() {
studentProfileStoreEntries := Iter.toArray(studentProfileStore.entries());
verifyStoreEntries := Iter.toArray(verifyStore.entries());
logStoreEntries := Iter.toArray(logStore.entries());
};
// Runs after an upgrade has initialized the replacement actor, including its stable variables, but before executing any shared function call (or message) on that actor.
// Here, we want to reset the stable var, as we'll be storing the data to be used in our HashMap.
system func postupgrade() {
studentProfileStoreEntries := [];
verifyStoreEntries := [];
logStoreEntries := [];
};
};