-
Notifications
You must be signed in to change notification settings - Fork 296
/
canister_lifecycle.rs
1020 lines (930 loc) · 36 KB
/
canister_lifecycle.rs
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
use assert_matches::assert_matches;
use candid::Encode;
use ic_config::subnet_config::CyclesAccountManagerConfig;
use ic_config::Config;
use ic_error_types::{ErrorCode, RejectCode};
use ic_ic00_types::{
self as ic00, CanisterChange, CanisterIdRecord, CanisterInstallMode,
CanisterSettingsArgsBuilder, CanisterStatusResultV2, CanisterStatusType, EmptyBlob,
InstallCodeArgs, LogVisibility, Method, Payload, UpdateSettingsArgs, IC_00,
};
use ic_registry_provisional_whitelist::ProvisionalWhitelist;
use ic_replica_tests as utils;
use ic_replica_tests::assert_reject;
use ic_test_utilities::assert_utils::assert_balance_equals;
use ic_test_utilities::universal_canister::management::SkipPreUpgrade;
use ic_test_utilities::universal_canister::{call_args, management, wasm, UNIVERSAL_CANISTER_WASM};
use ic_types::{ingress::WasmResult, CanisterId, ComputeAllocation, Cycles, NumBytes, PrincipalId};
use maplit::btreeset;
use std::{collections::BTreeSet, mem::size_of, str::FromStr};
const BALANCE_EPSILON: u64 = 1_000_000;
const NUM_CYCLES: u128 = 1_000_000_000;
const CANISTER_FREEZE_BALANCE_RESERVE: Cycles = Cycles::new(5_000_000_000_000);
#[test]
fn can_create_canister_from_another_canister() {
utils::simple_canister_test(|canister| {
// Canister ids are generated based on a counter within a subnet.
// The universal canister was the first one to be created. The
// canister we're about to create should be the second.
let expected_canister_id = CanisterId::from(1);
let expected_response_payload =
Encode!(&CanisterIdRecord::from(expected_canister_id)).unwrap();
let num_cycles = Cycles::new(1 << 70);
// Call method "create_canister" on ic:00. This should create a canister
// with the auto-generated id above.
assert_eq!(
canister.update(wasm().call(management::create_canister(num_cycles.into_parts()))),
Ok(WasmResult::Reply(expected_response_payload))
);
});
}
#[test]
// Tests canister creation, stopping, starting, and checking the status.
fn full_canister_lifecycle_from_another_canister() {
utils::simple_canister_test(|canister| {
let expected_canister_id = CanisterId::from(1);
let canister_id_record = Encode!(&CanisterIdRecord::from(expected_canister_id)).unwrap();
let num_cycles = Cycles::new(1 << 70);
// Create a new canister from within a canister.
assert_eq!(
canister.update(wasm().call(management::create_canister(num_cycles.into_parts()))),
Ok(WasmResult::Reply(canister_id_record,)),
);
// Verify that the newly created canister is running.
assert_matches!(
canister.update(wasm().call(
management::canister_status(expected_canister_id))
),
Ok(WasmResult::Reply(res)) if CanisterStatusResultV2::decode(&res).unwrap().status() == CanisterStatusType::Running
);
// Let the canister stop the newly created canister.
assert_eq!(
canister.update(wasm().call(management::stop_canister(expected_canister_id))),
Ok(WasmResult::Reply(EmptyBlob.encode())),
);
// Verify that the newly created canister is now stopped.
assert_matches!(
canister.update(wasm().call(
management::canister_status(expected_canister_id))
),
Ok(WasmResult::Reply(res)) if CanisterStatusResultV2::decode(&res).unwrap().status() == CanisterStatusType::Stopped
);
// Start the canister again.
assert_eq!(
canister.update(wasm().call(management::start_canister(expected_canister_id))),
Ok(WasmResult::Reply(EmptyBlob.encode())),
);
// Verify that the canister is running again.
assert_matches!(
canister.update(wasm().call(
management::canister_status(expected_canister_id))
),
Ok(WasmResult::Reply(res)) if CanisterStatusResultV2::decode(&res).unwrap().status() == CanisterStatusType::Running
);
});
}
#[test]
// Tests canister stopping, starting, and checking the status
// by sending ingress messages to ic:00.
fn full_canister_lifecycle_ingress() {
utils::canister_test(|test| {
let canister = test.create_universal_canister();
// Create a new canister
let expected_canister_id = CanisterId::from(1);
let canister_id_record = Encode!(&CanisterIdRecord::from(expected_canister_id)).unwrap();
let num_cycles = Cycles::new(1 << 70);
// Create a new canister from within a canister.
assert_eq!(
test.ingress(
canister,
"update",
wasm().call_with_cycles(
ic00::IC_00,
Method::CreateCanister,
call_args().other_side(EmptyBlob.encode()),
num_cycles,
)
),
Ok(WasmResult::Reply(canister_id_record.clone(),)),
);
// Set the controller to the anonymous user that's used in tests.
test.ingress(
canister,
"update",
wasm().call_with_cycles(
ic00::IC_00,
Method::UpdateSettings,
call_args().other_side(
UpdateSettingsArgs::new(
expected_canister_id,
CanisterSettingsArgsBuilder::new()
.with_controllers(vec![PrincipalId::new_anonymous()])
.build(),
)
.encode(),
),
num_cycles,
),
)
.unwrap();
// Verify that the newly created canister is running.
assert_matches!(
test.ingress(IC_00, Method::CanisterStatus, canister_id_record.clone()),
Ok(WasmResult::Reply(res)) if CanisterStatusResultV2::decode(&res).unwrap().status() == CanisterStatusType::Running
);
// Let ic:00 stop the newly created canister.
assert_eq!(
test.ingress(IC_00, Method::StopCanister, canister_id_record.clone()),
Ok(WasmResult::Reply(EmptyBlob.encode()))
);
// Verify that the newly created canister is now stopped.
assert_matches!(
test.ingress(IC_00, Method::CanisterStatus, canister_id_record.clone()),
Ok(WasmResult::Reply(res)) if CanisterStatusResultV2::decode(&res).unwrap().status() == CanisterStatusType::Stopped
);
// Start the canister again.
assert_eq!(
test.ingress(IC_00, Method::StartCanister, canister_id_record.clone()),
Ok(WasmResult::Reply(EmptyBlob.encode()))
);
// Verify that the newly created canister is running.
assert_matches!(
test.ingress(IC_00, Method::CanisterStatus, canister_id_record),
Ok(WasmResult::Reply(res)) if CanisterStatusResultV2::decode(&res).unwrap().status() == CanisterStatusType::Running
);
})
}
#[test]
fn delete_canister_with_incorrect_controller_fails() {
utils::canister_test(|test| {
let canister_a = test.create_universal_canister();
let canister_b = test.create_universal_canister();
// Canister A tries to delete canister B but it's not the controller.
// Should fail.
assert_reject(
test.ingress(
canister_a,
"update",
wasm().call_simple(
ic00::IC_00,
Method::DeleteCanister,
call_args().other_side(CanisterIdRecord::from(canister_b).encode()),
),
),
RejectCode::CanisterError,
);
});
}
#[test]
fn delete_canister_delete_self_fails() {
utils::canister_test(|test| {
let canister_id = test.create_universal_canister();
// Set the controller of the canister to be itself.
assert_eq!(
test.ingress(
IC_00,
Method::UpdateSettings,
UpdateSettingsArgs::new(
canister_id,
CanisterSettingsArgsBuilder::new()
.with_controllers(vec![canister_id.get()])
.build(),
)
.encode()
),
Ok(WasmResult::Reply(EmptyBlob.encode()))
);
// Canister tries to delete itself. Should fail because a self-controlling
// canister cannot be deleted.
assert_reject(
test.ingress(
canister_id,
"update",
wasm().call_simple(
ic00::IC_00,
Method::DeleteCanister,
call_args().other_side(CanisterIdRecord::from(canister_id).encode()),
),
),
RejectCode::CanisterError,
);
});
}
#[test]
fn delete_running_canister_fails() {
utils::canister_test(|test| {
// Create a new canister
let canister_a = test.create_universal_canister();
let canister_b = test.create_canister().unwrap();
// Verify that the canister_b is running
let canister_id_record = CanisterIdRecord::from(canister_b).encode();
assert_matches!(
test.ingress(IC_00, Method::CanisterStatus, canister_id_record),
Ok(WasmResult::Reply(res)) if CanisterStatusResultV2::decode(&res).unwrap().status() == CanisterStatusType::Running
);
// Set the controller of canister_b to be canister_a
assert_eq!(
test.ingress(
IC_00,
Method::UpdateSettings,
UpdateSettingsArgs::new(
canister_b,
CanisterSettingsArgsBuilder::new()
.with_controllers(vec![canister_a.into()])
.build(),
)
.encode()
),
Ok(WasmResult::Reply(EmptyBlob.encode()))
);
// Delete the canister. Should fail since it's running.
assert_reject(
test.ingress(
canister_a,
"update",
wasm().call_simple(
ic00::IC_00,
Method::DeleteCanister,
call_args().other_side(CanisterIdRecord::from(canister_b).encode()),
),
),
RejectCode::CanisterError,
);
});
}
#[test]
fn delete_stopped_canister_succeeds() {
utils::canister_test(|test| {
// Create a new canister
let canister_a = test.create_universal_canister();
let canister_b = test.create_canister().unwrap();
// Let ic:00 stop canister_b
let canister_id_record = CanisterIdRecord::from(canister_b).encode();
assert_eq!(
test.ingress(IC_00, "stop_canister", canister_id_record),
Ok(WasmResult::Reply(EmptyBlob.encode()))
);
// Set the controller of canister_b to be canister_a
test.ingress(
IC_00,
Method::UpdateSettings,
UpdateSettingsArgs::new(
canister_b,
CanisterSettingsArgsBuilder::new()
.with_controllers(vec![canister_a.into()])
.build(),
)
.encode(),
)
.unwrap();
// Ask canister_a to delete canister_b. Should succeed.
test.ingress(
canister_a,
"update",
wasm().call_simple(
ic00::IC_00,
Method::DeleteCanister,
call_args().other_side(CanisterIdRecord::from(canister_b).encode()),
),
)
.unwrap();
// Canister calls can not even be made to this canister.
});
}
#[test]
fn provisional_create_canister_with_cycles_respects_whitelist() {
{
let (config, _tmpdir) = Config::temp_config();
// By default all principals are allowed. This call should succeed.
utils::canister_test_with_config(config, |test| {
test.ingress(
IC_00,
Method::ProvisionalCreateCanisterWithCycles,
ic00::ProvisionalCreateCanisterWithCyclesArgs::new(Some(NUM_CYCLES), None).encode(),
)
.unwrap();
});
}
{
let (config, _tmpdir) = Config::temp_config();
let mut ic_config = utils::get_ic_config();
let provisional_whitelist = ProvisionalWhitelist::Set(BTreeSet::new());
ic_config.set_provisional_whitelist(provisional_whitelist);
// No sender IDs allowed. This call should fail.
utils::canister_test_with_ic_config(config, ic_config, |test| {
assert_matches!(
test.ingress(
IC_00,
Method::ProvisionalCreateCanisterWithCycles,
ic00::ProvisionalCreateCanisterWithCyclesArgs::new(Some(NUM_CYCLES), None).encode(),
),
Err(err) if err.code() == ErrorCode::CanisterMethodNotFound
);
});
}
let (config, _tmpdir) = Config::temp_config();
let mut ic_config = utils::get_ic_config();
let canister_id_1 = CanisterId::from(0);
let provisional_whitelist =
ProvisionalWhitelist::Set(btreeset! {PrincipalId::new_anonymous(), canister_id_1.get()});
ic_config.set_provisional_whitelist(provisional_whitelist);
utils::canister_test_with_ic_config(config, ic_config, |test| {
// Creating universal canisters. These succeed because the user is in the
// whitelist.
let canister_id_1 = test.create_universal_canister();
let canister_id_2 = test.create_universal_canister();
// canister_id_1 is in the whitelist.
assert_matches!(
test.ingress(
canister_id_1,
"update",
wasm().call_simple(
ic00::IC_00,
Method::ProvisionalCreateCanisterWithCycles,
call_args().other_side(
ic00::ProvisionalCreateCanisterWithCyclesArgs::new(Some(NUM_CYCLES), None)
.encode(),
),
),
),
Ok(WasmResult::Reply(_))
);
// canister_id_2 isn't in the whitelist.
assert_reject(
test.ingress(
canister_id_2,
"update",
wasm().call_simple(
ic00::IC_00,
Method::ProvisionalCreateCanisterWithCycles,
call_args().other_side(
ic00::ProvisionalCreateCanisterWithCyclesArgs::new(Some(NUM_CYCLES), None)
.encode(),
),
),
),
RejectCode::DestinationInvalid,
);
});
}
#[test]
fn provisional_top_up_canister_does_not_exist() {
{
let (config, _tmpdir) = Config::temp_config();
utils::canister_test_with_config(config, |test| {
assert_matches!(
test.ingress(
IC_00,
Method::ProvisionalTopUpCanister,
ic00::ProvisionalTopUpCanisterArgs::new(IC_00 /* any principal */, NUM_CYCLES).encode(),
),
Err(err) if err.code() == ErrorCode::CanisterNotFound
);
});
}
}
#[test]
fn provisional_top_up_canister_respects_whitelist() {
{
let (config, _tmpdir) = Config::temp_config();
// By default all principals are allowed. This call should succeed.
utils::canister_test_with_config(config, |test| {
let canister_id = test.create_universal_canister();
test.ingress(
IC_00,
Method::ProvisionalTopUpCanister,
ic00::ProvisionalTopUpCanisterArgs::new(canister_id, NUM_CYCLES).encode(),
)
.unwrap();
});
}
let (config, _tmpdir) = Config::temp_config();
let mut ic_config = utils::get_ic_config();
let canister_id_1 = CanisterId::from(0);
let provisional_whitelist =
ProvisionalWhitelist::Set(btreeset! {PrincipalId::new_anonymous(), canister_id_1.get()});
ic_config.set_provisional_whitelist(provisional_whitelist);
utils::canister_test_with_ic_config(config, ic_config, |test| {
// Creating universal canisters. These succeed because the user is in the
// whitelist.
let canister_id_1 = test.create_universal_canister();
let canister_id_2 = test.create_universal_canister();
// canister_id_1 is in the whitelist.
assert_matches!(
test.ingress(
canister_id_1,
"update",
wasm().call_simple(
ic00::IC_00,
Method::ProvisionalTopUpCanister,
call_args().other_side(
ic00::ProvisionalTopUpCanisterArgs::new(canister_id_1, NUM_CYCLES).encode()
),
),
),
Ok(WasmResult::Reply(_))
);
// canister_id_2 isn't in the whitelist.
assert_reject(
test.ingress(
canister_id_2,
"update",
wasm().call_simple(
ic00::IC_00,
Method::ProvisionalTopUpCanister,
call_args().other_side(
ic00::ProvisionalTopUpCanisterArgs::new(canister_id_1, NUM_CYCLES).encode(),
),
),
),
RejectCode::DestinationInvalid,
);
});
}
#[test]
fn can_create_canister_with_cycles_from_another_canister() {
utils::canister_test(|test| {
let num_cycles = CANISTER_FREEZE_BALANCE_RESERVE + Cycles::new(5_000_000_000_000);
// Create a universal canister.
let canister_id = test.create_universal_canister_with_args(vec![], num_cycles.get());
let old_canister_cycles_balance_before =
test.canister_state(&canister_id).system_state.balance();
// Create another canister with some cycles.
let config = CyclesAccountManagerConfig::application_subnet();
let cycles_for_new_canister = config.canister_creation_fee + Cycles::new(100_000_000);
let new_canister_id_payload = test
.ingress(
canister_id,
"update",
wasm().call_with_cycles(
IC_00,
Method::CreateCanister,
call_args().other_side(EmptyBlob.encode()),
cycles_for_new_canister,
),
)
.unwrap()
.bytes();
let new_canister_id = CanisterIdRecord::decode(new_canister_id_payload.as_slice())
.unwrap()
.get_canister_id();
let old_canister_cycles_balance_after =
test.canister_state(&canister_id).system_state.balance();
println!(
"old canister balance after: {}",
old_canister_cycles_balance_after
);
let new_canister_cycles_balance =
test.canister_state(&new_canister_id).system_state.balance();
// Check that the balance of the sending canister after creating a new canister
// is at most its previous balance minus the cycles transferred.
assert!(
old_canister_cycles_balance_after
<= old_canister_cycles_balance_before - cycles_for_new_canister,
"Cycle balance of the creating canister should decrease by at least {}",
cycles_for_new_canister
);
// Check that the balance of the created canister is at most the cycles
// transferred.
assert!(new_canister_cycles_balance <= cycles_for_new_canister,
"Cycle balance of the newly created canister is larger than the cycles transferred to it"
);
});
}
#[test]
fn provisional_create_canister_with_cycles_and_top_up() {
let (config, _tmpdir) = Config::temp_config();
let mut ic_config = utils::get_ic_config();
let provisional_whitelist = ProvisionalWhitelist::Set(btreeset!(
// PrincipalId of CanisterA that is created below. This code assumes
// that we can predict the canister Ids that are generated in
// CanisterManager and may need updating if that is no longer the case.
PrincipalId::from_str("rwlgt-iiaaa-aaaaa-aaaaa-cai").unwrap(),
PrincipalId::new_anonymous(),
));
ic_config.set_provisional_whitelist(provisional_whitelist);
utils::canister_test_with_ic_config(config, ic_config, |test| {
// Create a canister
let canister_a_id = test.create_universal_canister();
let canister_a_cycles_before: u128 = test
.canister_state(&canister_a_id)
.system_state
.balance()
.get();
let canister_b_cycles_init = canister_a_cycles_before / 2; // Create with 1/2 * cycles of canister_a
let canister_b_cycles_top_up = canister_b_cycles_init; // Top-Up with just as many cycles
// From `canister_a` call `IC_00` to create a canister with cycles.
let canister_b_id = test
.ingress(
canister_a_id,
"update",
wasm().call_simple(
IC_00,
Method::ProvisionalCreateCanisterWithCycles,
call_args().other_side(
ic00::ProvisionalCreateCanisterWithCyclesArgs::new(
Some(canister_b_cycles_init),
None,
)
.encode(),
),
),
)
.unwrap()
.bytes();
let canister_b_id = CanisterIdRecord::decode(canister_b_id.as_slice())
.unwrap()
.get_canister_id();
let canister_b_cycles: u128 = test
.canister_state(&canister_b_id)
.system_state
.balance()
.get();
assert_eq!(canister_b_cycles, canister_b_cycles_init);
// From `canister_a` call `IC_00` to top-up `canister_b`.
test.ingress(
canister_a_id,
"update",
wasm().call_simple(
IC_00,
Method::ProvisionalTopUpCanister,
call_args().other_side(
ic00::ProvisionalTopUpCanisterArgs::new(
canister_b_id,
canister_b_cycles_top_up,
)
.encode(),
),
),
)
.unwrap()
.bytes();
let canister_b_cycles_after_top_up: u128 = test
.canister_state(&canister_b_id)
.system_state
.balance()
.get();
// Canister B now has (init + top-up) cycles
assert_eq!(
canister_b_cycles_after_top_up,
canister_b_cycles_init + canister_b_cycles_top_up
);
// From `canister_a` call `IC_00` to create a canister without cycles argument.
// The default value is `config.hypervisor.default_provisional_cycles_balance`.
let canister_c_id = test
.ingress(
canister_a_id,
"update",
wasm().call_simple(
IC_00,
Method::ProvisionalCreateCanisterWithCycles,
call_args().other_side(
ic00::ProvisionalCreateCanisterWithCyclesArgs::new(None, None).encode(),
),
),
)
.unwrap()
.bytes();
let canister_c_id = CanisterIdRecord::decode(canister_c_id.as_slice())
.unwrap()
.get_canister_id();
let canister_c_cycles = test.canister_state(&canister_c_id).system_state.balance();
assert_eq!(
canister_c_cycles,
Config::temp_config()
.0
.hypervisor
.default_provisional_cycles_balance
);
});
}
#[test]
fn can_get_canister_information() {
utils::canister_test(|test| {
// Create a new canister
let canister_a = test.create_universal_canister();
let num_cycles = Cycles::new(1 << 70);
let canister_b = test.create_canister_with_cycles(num_cycles.get()).unwrap();
// Set the controller of canister_b to be canister_a
assert_eq!(
test.ingress(
IC_00,
Method::UpdateSettings,
UpdateSettingsArgs::new(
canister_b,
CanisterSettingsArgsBuilder::new()
.with_controllers(vec![canister_a.into()])
.build(),
)
.encode()
),
Ok(WasmResult::Reply(EmptyBlob.encode()))
);
// Request the status of canister_b.
assert_matches!(
test.ingress(
canister_a,
"update",
wasm().call_simple(
ic00::IC_00,
Method::CanisterStatus,
call_args().other_side(CanisterIdRecord::from(canister_b).encode()),
),
),
// We can check exact equality because no costs are incurred for a
// canister that's created but has no code installed on it.
Ok(WasmResult::Reply(res)) if CanisterStatusResultV2::decode(&res).unwrap() == CanisterStatusResultV2::new(
CanisterStatusType::Running,
None,
canister_a.get(),
vec![canister_a.get()],
NumBytes::from((2 * size_of::<CanisterChange>() + 2 * size_of::<PrincipalId>()) as u64),
num_cycles.get(),
ComputeAllocation::default().as_percent(),
None,
2592000,
Some(5_000_000_000_000u128),
LogVisibility::default(),
0u128,
0u128,
0u128,
0u128,
0u128,
0u128,
)
);
// Install code to canister_b.
test.ingress(
canister_a,
"update",
wasm().call_simple(
ic00::IC_00,
Method::InstallCode,
call_args().other_side(
InstallCodeArgs::new(
CanisterInstallMode::Install,
canister_b,
UNIVERSAL_CANISTER_WASM.to_vec(),
vec![],
None,
None,
None,
)
.encode(),
),
),
)
.unwrap();
// Request the status of canister_b.
assert_matches!(
test.ingress(
canister_a,
"update",
wasm().call_simple(
ic00::IC_00,
Method::CanisterStatus,
call_args().other_side(CanisterIdRecord::from(canister_b).encode()),
),
),
Ok(WasmResult::Reply(res)) => assert_canister_status_result_equals(
CanisterStatusResultV2::new(
CanisterStatusType::Running,
Some(ic_crypto_sha2::Sha256::hash(UNIVERSAL_CANISTER_WASM).to_vec()),
canister_a.get(),
vec![canister_a.get()],
// We don't assert a specific memory size since the universal canister's
// size changes between updates.
NumBytes::from(0),
num_cycles.get(),
ComputeAllocation::default().as_percent(),
None,
259200,
None,
LogVisibility::default(),
0u128,
0u128,
0u128,
0u128,
0u128,
0u128,
),
CanisterStatusResultV2::decode(&res).unwrap(),
2 * BALANCE_EPSILON,
)
);
});
}
#[test]
fn cannot_run_method_on_empty_canister() {
utils::canister_test(|test| {
let canister = test.create_canister().unwrap();
match test.ingress(canister, "hello", vec![]) {
Err(err) => {
assert_eq!(err.code(), ErrorCode::CanisterWasmModuleNotFound);
assert_eq!(
err.description(),
"Attempt to execute a message on canister rwlgt-iiaaa-aaaaa-aaaaa-cai which contains no Wasm module"
);
}
rest => panic!("Unexpected behaviour {:?}", rest),
}
})
}
#[test]
fn installing_a_canister_with_lower_memory_allocation_than_it_uses_fails() {
utils::canister_test(|test| {
let num_cycles = CANISTER_FREEZE_BALANCE_RESERVE + Cycles::new(5_000_000_000_000);
let canister_id = test.create_canister_with_cycles(num_cycles.get()).unwrap();
// Install code to canister_id with low memory allocation.
assert_matches!(
test.ingress(
ic00::IC_00,
Method::InstallCode,
InstallCodeArgs::new(
CanisterInstallMode::Install,
canister_id,
UNIVERSAL_CANISTER_WASM.to_vec(),
vec![],
None,
// Set memory allocation to 42 bytes (i.e. something ridiculously small).
Some(42),
None,
)
.encode(),
),
Err(err) if err.code() == ErrorCode::InsufficientMemoryAllocation
);
// Install code to canister_id with enough memory allocation.
test.ingress(
ic00::IC_00,
Method::InstallCode,
InstallCodeArgs::new(
CanisterInstallMode::Install,
canister_id,
UNIVERSAL_CANISTER_WASM.to_vec(),
vec![],
None,
None,
None,
)
.encode(),
)
.unwrap();
// Re-install code to canister_id with low memory allocation.
assert_matches!(
test.ingress(
ic00::IC_00,
Method::InstallCode,
InstallCodeArgs::new(
CanisterInstallMode::Reinstall,
canister_id,
UNIVERSAL_CANISTER_WASM.to_vec(),
vec![],
None,
// Set memory allocation to 10 bytes.
Some(10),
None,
)
.encode(),
),
Err(err) if err.code() == ErrorCode::InsufficientMemoryAllocation
);
// Canister is still callable.
assert_matches!(
test.ingress(
canister_id,
"update",
wasm().reply_data(b"Hello").build(),
),
Ok(WasmResult::Reply(res)) if std::str::from_utf8(&res).unwrap() == "Hello"
);
});
}
#[test]
fn upgrading_a_canister_with_lower_memory_allocation_than_it_needs_fails() {
utils::canister_test(|test| {
let num_cycles = CANISTER_FREEZE_BALANCE_RESERVE + Cycles::new(5_000_000_000_000);
let canister_id = test.create_canister_with_cycles(num_cycles.get()).unwrap();
// Install code to canister_id.
test.ingress(
ic00::IC_00,
Method::InstallCode,
InstallCodeArgs::new(
CanisterInstallMode::Install,
canister_id,
UNIVERSAL_CANISTER_WASM.to_vec(),
vec![],
None,
None,
None,
)
.encode(),
)
.unwrap();
// Attempt to upgrade the canister with a very low memory allocation.
assert_matches!(
test.ingress(
ic00::IC_00,
Method::InstallCode,
InstallCodeArgs::new(
CanisterInstallMode::Upgrade,
canister_id,
UNIVERSAL_CANISTER_WASM.to_vec(),
vec![],
None,
Some(10),
None,
)
.encode(),
),
Err(err) if err.code() == ErrorCode::InsufficientMemoryAllocation
);
// Canister is still callable.
assert_matches!(
test.ingress(
canister_id,
"update",
wasm().reply_data(b"Hello").build(),
),
Ok(WasmResult::Reply(res)) if std::str::from_utf8(&res).unwrap() == "Hello"
);
});
}
// Asserts that two `CanisterStatusResult`s are almost equal. Because
// `CanisterStatusResult` contains cycles balances, we can't assert on actual
// equality, so use the helper method that asserts two cycles balances are equal
// within some accuracy. Additionally, we ignore the memory size.
fn assert_canister_status_result_equals(
expected: CanisterStatusResultV2,
actual: CanisterStatusResultV2,
epsilon: u64,
) {
assert_eq!(expected.status(), actual.status());
assert_eq!(expected.module_hash(), actual.module_hash());
assert_eq!(expected.controller(), actual.controller());
assert_balance_equals(
Cycles::from(expected.cycles()),
Cycles::from(actual.cycles()),
Cycles::from(epsilon),
);
}
#[test]
// Tests canister Upgrade with skipping pre_upgrade hook, using CanisterInstallModeV2.
fn test_canister_skip_upgrade() {
utils::simple_canister_test(|canister| {
let num_cycles = Cycles::new(1 << 70);
// Create a new canister from within a canister.
let reply = match canister
.update(wasm().call(management::create_canister(num_cycles.into_parts())))
.unwrap()
{
WasmResult::Reply(reply) => reply,
_ => panic!("Unexpected result"),
};
let canister_id = CanisterIdRecord::decode(&reply).unwrap().get_canister_id();
// Install canister code.
assert_matches!(
canister.update(wasm().call(management::install_code(
canister_id,
UNIVERSAL_CANISTER_WASM
))),
Ok(WasmResult::Reply(_))
);
let set_trap_pre_upgrade = wasm().set_pre_upgrade(wasm().trap()).reply().build();
// Set pre_upgrade to trap.
assert_matches!(
canister.update(
wasm().inter_update(canister_id, call_args().other_side(set_trap_pre_upgrade),)
),
Ok(WasmResult::Reply(_))
);
// Upgrade without skipping pre_upgrade should fail.
assert_matches!(
canister.update(wasm().call(
management::install_code(canister_id, UNIVERSAL_CANISTER_WASM).with_mode(
management::InstallMode::Upgrade(Some(SkipPreUpgrade(Some(false)))),
),
)),
Ok(WasmResult::Reject(_))
);
// Upgrade with skipping pre upgrade should succeed.
assert_matches!(