-
Notifications
You must be signed in to change notification settings - Fork 296
/
test_utilities.rs
1399 lines (1294 loc) · 50.2 KB
/
test_utilities.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 std::{
collections::{BTreeMap, HashMap, VecDeque},
convert::{TryFrom, TryInto},
path::PathBuf,
sync::{Arc, Mutex},
};
use ic_base_types::{CanisterId, NumBytes, SubnetId};
use ic_config::{
flag_status::FlagStatus,
subnet_config::{SchedulerConfig, SubnetConfig},
};
use ic_cycles_account_manager::CyclesAccountManager;
use ic_embedders::{
wasm_executor::{
CanisterStateChanges, PausedWasmExecution, SliceExecutionOutput, WasmExecutionResult,
WasmExecutor,
},
CompilationCache, CompilationResult, WasmExecutionInput,
};
use ic_error_types::UserError;
use ic_interfaces::execution_environment::{
ExecutionRoundType, HypervisorError, HypervisorResult, IngressHistoryWriter, InstanceStats,
RegistryExecutionSettings, Scheduler, SystemApiCallCounters, WasmExecutionOutput,
};
use ic_logger::{replica_logger::no_op_logger, ReplicaLogger};
use ic_management_canister_types::{
CanisterInstallMode, CanisterStatusType, EcdsaKeyId, InstallCodeArgs, Method, Payload, IC_00,
};
use ic_metrics::MetricsRegistry;
use ic_registry_routing_table::{CanisterIdRange, RoutingTable};
use ic_registry_subnet_type::SubnetType;
use ic_replicated_state::{
canister_state::execution_state::{self, WasmMetadata},
page_map::TestPageAllocatorFileDescriptorImpl,
testing::{CanisterQueuesTesting, ReplicatedStateTesting},
CanisterState, ExecutionState, ExportedFunctions, InputQueueType, Memory, ReplicatedState,
};
use ic_system_api::{
sandbox_safe_system_state::{SandboxSafeSystemState, SystemStateChanges},
ApiType, ExecutionParameters,
};
use ic_test_utilities::{
state::CanisterStateBuilder,
types::{
ids::{canister_test_id, subnet_test_id, user_test_id},
messages::{RequestBuilder, SignedIngressBuilder},
},
};
use ic_test_utilities_execution_environment::{generate_subnets, test_registry_settings};
use ic_types::{
consensus::ecdsa::QuadrupleId,
crypto::{canister_threshold_sig::MasterEcdsaPublicKey, AlgorithmId},
ingress::{IngressState, IngressStatus},
messages::{CallContextId, Ingress, MessageId, Request, RequestOrResponse, Response},
methods::{Callback, FuncRef, SystemMethod, WasmClosure, WasmMethod},
CanisterTimer, ComputeAllocation, Cycles, ExecutionRound, MemoryAllocation, NumInstructions,
Randomness, Time, UserId,
};
use ic_wasm_types::CanisterModule;
use maplit::btreemap;
use std::time::Duration;
use crate::{
as_round_instructions, ExecutionEnvironment, Hypervisor, IngressHistoryWriterImpl, RoundLimits,
};
use super::SchedulerImpl;
use crate::metrics::MeasurementScope;
use ic_crypto_prng::{Csprng, RandomnessPurpose::ExecutionThread};
use ic_types::time::UNIX_EPOCH;
use std::collections::BTreeSet;
/// A helper for the scheduler tests. It comes with its own Wasm executor that
/// fakes execution of Wasm code for performance, so it can process thousands
/// of messages in milliseconds.
///
/// See the comments of `TestMessage` for the description on how to create
/// fake ingress messages and inter-canister call messages.
///
/// Example usages of the test helper:
/// ```
/// let mut test = SchedulerTestBuilder::new().build();
/// let canister_id = test.create_canister();
/// let message = ingress(50);
/// test.send_ingress(canister_id, message);
/// test.execute_round(ExecutionRoundType::OrdinaryRound);
/// ```
pub(crate) struct SchedulerTest {
// The current replicated state. The option type allows taking the state for
// execution and then putting it back afterwards.
state: Option<ReplicatedState>,
// Monotonically increasing counter used during canister creation.
next_canister_id: u64,
// Monotonically increasing counter that specifies the current round.
round: ExecutionRound,
// The amount of cycles that new canisters have by default.
initial_canister_cycles: Cycles,
// The id of the user that sends ingress messages.
user_id: UserId,
// The id of a canister that is guaranteed to be xnet.
xnet_canister_id: CanisterId,
// The actual scheduler.
scheduler: SchedulerImpl,
// The fake Wasm executor.
wasm_executor: Arc<TestWasmExecutor>,
// Registry Execution Settings.
registry_settings: RegistryExecutionSettings,
// Metrics Registry.
metrics_registry: MetricsRegistry,
// ECDSA subnet public keys.
ecdsa_subnet_public_keys: BTreeMap<EcdsaKeyId, MasterEcdsaPublicKey>,
// ECDSA quadruple IDs.
ecdsa_quadruple_ids: BTreeMap<EcdsaKeyId, BTreeSet<QuadrupleId>>,
}
impl std::fmt::Debug for SchedulerTest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SchedulerTest").finish()
}
}
impl SchedulerTest {
pub fn state(&self) -> &ReplicatedState {
self.state.as_ref().unwrap()
}
pub fn state_mut(&mut self) -> &mut ReplicatedState {
self.state.as_mut().unwrap()
}
pub fn canister_state(&self, canister_id: CanisterId) -> &CanisterState {
self.state().canister_state(&canister_id).unwrap()
}
pub fn metrics_registry(&self) -> &MetricsRegistry {
&self.metrics_registry
}
pub fn canister_state_mut(&mut self, canister_id: CanisterId) -> &mut CanisterState {
self.state_mut().canister_state_mut(&canister_id).unwrap()
}
pub fn ingress_queue_size(&self, canister_id: CanisterId) -> usize {
self.canister_state(canister_id)
.system_state
.queues()
.ingress_queue_size()
}
pub fn subnet_ingress_queue_size(&self) -> usize {
self.state().subnet_queues().ingress_queue_size()
}
pub fn last_round(&self) -> ExecutionRound {
ExecutionRound::new(self.round.get().max(1) - 1)
}
pub fn advance_to_round(&mut self, round: ExecutionRound) {
self.round = round;
}
pub fn scheduler(&self) -> &SchedulerImpl {
&self.scheduler
}
pub fn xnet_canister_id(&self) -> CanisterId {
self.xnet_canister_id
}
pub fn registry_settings(&self) -> &RegistryExecutionSettings {
&self.registry_settings
}
/// Returns how many instructions were executed by a canister on a thread
/// and in an execution round. The order of elements is important and
/// matches the execution order for a fixed thread.
pub fn executed_schedule(&self) -> Vec<(ExecutionRound, CanisterId, NumInstructions)> {
let wasm_executor = self.wasm_executor.core.lock().unwrap();
wasm_executor.schedule.clone()
}
pub fn create_canister(&mut self) -> CanisterId {
self.create_canister_with(
self.initial_canister_cycles,
ComputeAllocation::zero(),
MemoryAllocation::BestEffort,
None,
None,
None,
)
}
/// Creates a canister with the given balance and allocations.
/// The `system_task` parameter can be used to optionally enable the
/// heartbeat by passing `Some(SystemMethod::CanisterHeartbeat)`.
/// In that case the heartbeat execution must be specified before each
/// round using `expect_heartbeat()`.
pub fn create_canister_with(
&mut self,
cycles: Cycles,
compute_allocation: ComputeAllocation,
memory_allocation: MemoryAllocation,
system_task: Option<SystemMethod>,
time_of_last_allocation_charge: Option<Time>,
status: Option<CanisterStatusType>,
) -> CanisterId {
let canister_id = self.next_canister_id();
let wasm_source = system_task
.map(|x| x.to_string().as_bytes().to_vec())
.unwrap_or_default();
let time_of_last_allocation_charge =
time_of_last_allocation_charge.map_or(UNIX_EPOCH, |time| time);
let mut canister_state = CanisterStateBuilder::new()
.with_canister_id(canister_id)
.with_cycles(cycles)
.with_controller(self.user_id.get())
.with_compute_allocation(compute_allocation)
.with_memory_allocation(memory_allocation.bytes())
.with_wasm(wasm_source.clone())
.with_freezing_threshold(100)
.with_time_of_last_allocation_charge(time_of_last_allocation_charge)
.with_status(status.unwrap_or(CanisterStatusType::Running))
.build();
let mut wasm_executor = self.wasm_executor.core.lock().unwrap();
canister_state.execution_state = Some(
wasm_executor
.create_execution_state(CanisterModule::new(wasm_source), canister_id)
.unwrap()
.0,
);
canister_state
.system_state
.controllers
.insert(self.xnet_canister_id.get());
self.state
.as_mut()
.unwrap()
.put_canister_state(canister_state);
canister_id
}
pub fn send_ingress(&mut self, canister_id: CanisterId, message: TestMessage) -> MessageId {
let mut wasm_executor = self.wasm_executor.core.lock().unwrap();
let mut state = self.state.take().unwrap();
let canister = state.canister_state_mut(&canister_id).unwrap();
let message_id = wasm_executor.push_ingress(
canister_id,
canister,
message,
Time::from_nanos_since_unix_epoch(u64::MAX / 2),
);
self.state = Some(state);
message_id
}
pub fn send_ingress_with_expiry(
&mut self,
canister_id: CanisterId,
message: TestMessage,
expiry_time: Time,
) -> MessageId {
let mut wasm_executor = self.wasm_executor.core.lock().unwrap();
let mut state = self.state.take().unwrap();
let canister = state.canister_state_mut(&canister_id).unwrap();
let message_id = wasm_executor.push_ingress(canister_id, canister, message, expiry_time);
self.state = Some(state);
message_id
}
pub fn ingress_status(&self, message_id: &MessageId) -> IngressStatus {
self.state.as_ref().unwrap().get_ingress_status(message_id)
}
pub fn ingress_error(&self, message_id: &MessageId) -> UserError {
match self.ingress_status(message_id) {
IngressStatus::Known { state, .. } => match state {
IngressState::Failed(error) => error,
IngressState::Received
| IngressState::Completed(_)
| IngressState::Processing
| IngressState::Done => unreachable!("Unexpected ingress state: {:?}", state),
},
IngressStatus::Unknown => unreachable!("Expected message to finish."),
}
}
pub fn ingress_state(&self, message_id: &MessageId) -> IngressState {
match self.ingress_status(message_id) {
IngressStatus::Known { state, .. } => state,
IngressStatus::Unknown => unreachable!("Expected a known ingress status."),
}
}
/// Injects a call to the management canister.
/// Note that this function doesn't support `InstallCode`
/// messages, because for such messages we additionally need to know
/// how many instructions the corresponding Wasm execution needs.
/// See `inject_install_code_call_to_ic00()`.
///
/// Use `get_responses_to_injected_calls()` to obtain the response
/// after round execution.
pub fn inject_call_to_ic00<S: ToString>(
&mut self,
method_name: S,
method_payload: Vec<u8>,
payment: Cycles,
caller: CanisterId,
input_type: InputQueueType,
) {
assert!(
method_name.to_string() != Method::InstallCode.to_string(),
"Use `inject_install_code_call_to_ic00()`."
);
self.state_mut()
.subnet_queues_mut()
.push_input(
RequestBuilder::new()
.sender(caller)
.receiver(CanisterId::ic_00())
.method_name(method_name)
.method_payload(method_payload)
.payment(payment)
.build()
.into(),
input_type,
)
.unwrap();
}
/// Injects an ingress to the management canister.
pub fn inject_ingress_to_ic00<S: ToString>(
&mut self,
method_name: S,
method_payload: Vec<u8>,
expiry_time: Time,
) {
let ingress_id = {
let mut wasm_executor = self.wasm_executor.core.lock().unwrap();
wasm_executor.next_message_id()
};
self.state_mut().subnet_queues_mut().push_ingress(
(
SignedIngressBuilder::new()
.canister_id(IC_00)
.method_name(method_name)
.method_payload(method_payload)
.nonce(ingress_id as u64)
.expiry_time(expiry_time)
.build(),
None,
)
.into(),
);
}
/// Similar to `inject_call_to_ic00()` but supports `InstallCode` messages.
/// Example usage:
/// ```text
/// let upgrade = TestInstallCode::Upgrade {
/// pre_upgrade: instructions(10),
/// start: instructions(20),
/// post_upgrade: instructions(30),
/// };
/// test.inject_install_code_call_to_ic00(canister, upgrade);
/// ```
///
/// Use `get_responses_to_injected_calls()` to obtain the response
/// after round execution.
pub fn inject_install_code_call_to_ic00(
&mut self,
target: CanisterId,
install_code: TestInstallCode,
) {
let wasm_module = wat::parse_str("(module)").unwrap();
let (mode, test_message) = match install_code {
TestInstallCode::Install { init } => (CanisterInstallMode::Install, init),
TestInstallCode::Reinstall { init } => (CanisterInstallMode::Reinstall, init),
TestInstallCode::Upgrade { post_upgrade } => {
(CanisterInstallMode::Upgrade, post_upgrade)
}
};
let message_id = {
let mut wasm_executor = self.wasm_executor.core.lock().unwrap();
wasm_executor.push_install_code(test_message)
};
let message_payload = InstallCodeArgs {
mode,
canister_id: target.get(),
wasm_module,
arg: encode_message_id_as_payload(message_id),
compute_allocation: None,
memory_allocation: None,
query_allocation: None,
sender_canister_version: None,
};
let caller = self.xnet_canister_id();
self.state_mut()
.subnet_queues_mut()
.push_input(
RequestBuilder::new()
.sender(caller)
.receiver(CanisterId::ic_00())
.method_name(Method::InstallCode)
.method_payload(message_payload.encode())
.build()
.into(),
InputQueueType::RemoteSubnet,
)
.unwrap();
}
/// Returns all responses from the management canister to
/// `self.xnet_canister_id()`.
pub fn get_responses_to_injected_calls(&mut self) -> Vec<Response> {
let mut output: Vec<Response> = vec![];
let xnet_canister_id = self.xnet_canister_id;
let subnet_queue = self.state_mut().subnet_queues_mut();
while let Some(msg) = subnet_queue.pop_canister_output(&xnet_canister_id) {
match msg {
RequestOrResponse::Request(request) => {
panic!(
"Expected the xnet message to be a Response, but got a Request: {:?}",
request
)
}
RequestOrResponse::Response(response) => {
output.push((*response).clone());
}
}
}
output
}
/// Specifies heartbeat execution for the next round.
pub fn expect_heartbeat(&mut self, canister_id: CanisterId, system_task: TestMessage) {
assert!(
self.canister_state(canister_id)
.execution_state
.as_ref()
.unwrap()
.exports_method(&WasmMethod::System(SystemMethod::CanisterHeartbeat)),
"The canister should be created with \
`create_canister_with(.., Some(SystemMethod::CanisterHeartbeat))`"
);
let mut wasm_executor = self.wasm_executor.core.lock().unwrap();
wasm_executor.push_system_task(canister_id, system_task);
}
pub fn expect_global_timer(&mut self, canister_id: CanisterId, system_task: TestMessage) {
assert!(
self.canister_state(canister_id)
.execution_state
.as_ref()
.unwrap()
.exports_method(&WasmMethod::System(SystemMethod::CanisterGlobalTimer)),
"The canister should be created with \
`create_canister_with(.., Some(SystemMethod::CanisterGlobalTimer))`"
);
let mut wasm_executor = self.wasm_executor.core.lock().unwrap();
wasm_executor.push_system_task(canister_id, system_task);
}
pub fn execute_round(&mut self, round_type: ExecutionRoundType) {
let state = self.state.take().unwrap();
let state = self.scheduler.execute_round(
state,
Randomness::from([0; 32]),
self.ecdsa_subnet_public_keys.clone(),
self.ecdsa_quadruple_ids.clone(),
self.round,
round_type,
self.registry_settings(),
);
self.state = Some(state);
self.increment_round();
}
/// Executes ordinary rounds until there is no more progress,
/// calling closure `f` after each round.
pub fn execute_all_with<F>(&mut self, mut f: F)
where
F: FnMut(&mut Self),
{
let mut number_of_executed_slices = 0;
loop {
self.execute_round(ExecutionRoundType::OrdinaryRound);
f(self);
let prev_number_of_executed_slices = number_of_executed_slices;
number_of_executed_slices = self.executed_schedule().len();
if prev_number_of_executed_slices == number_of_executed_slices {
break;
}
}
}
pub fn drain_subnet_messages(
&mut self,
long_running_canister_ids: BTreeSet<CanisterId>,
) -> ReplicatedState {
let state = self.state.take().unwrap();
let compute_allocation_used = state.total_compute_allocation();
let mut csprng = Csprng::from_seed_and_purpose(
&Randomness::from([0; 32]),
&ExecutionThread(self.scheduler.config.scheduler_cores as u32),
);
let mut round_limits = RoundLimits {
instructions: as_round_instructions(
self.scheduler.config.max_instructions_per_round / 16,
),
subnet_available_memory: self.scheduler.exec_env.subnet_available_memory(&state),
compute_allocation_used,
};
let measurements = MeasurementScope::root(&self.scheduler.metrics.round_subnet_queue);
self.scheduler.drain_subnet_queues(
state,
&mut csprng,
&mut round_limits,
&measurements,
false,
long_running_canister_ids,
self.registry_settings(),
&BTreeMap::new(),
)
}
pub fn charge_for_resource_allocations(&mut self) {
let subnet_size = self.subnet_size();
self.scheduler
.charge_canisters_for_resource_allocation_and_usage(
self.state.as_mut().unwrap(),
subnet_size,
)
}
pub fn induct_messages_on_same_subnet(&mut self) {
self.scheduler
.induct_messages_on_same_subnet(self.state.as_mut().unwrap());
}
fn increment_round(&mut self) {
let mut wasm_executor = self.wasm_executor.core.lock().unwrap();
self.round = ExecutionRound::new(self.round.get() + 1);
wasm_executor.round = self.round;
}
fn next_canister_id(&mut self) -> CanisterId {
let canister_id = canister_test_id(self.next_canister_id);
self.next_canister_id += 1;
canister_id
}
pub(crate) fn set_canister_global_timer(&mut self, canister: CanisterId, time: Time) {
let canister_state = self.canister_state_mut(canister);
canister_state.system_state.global_timer = CanisterTimer::Active(time);
}
pub(crate) fn set_time(&mut self, time: Time) {
self.state_mut().metadata.batch_time = time;
}
pub fn subnet_size(&self) -> usize {
self.registry_settings.subnet_size
}
pub fn ecdsa_signature_fee(&self) -> Cycles {
self.scheduler
.cycles_account_manager
.ecdsa_signature_fee(self.registry_settings.subnet_size)
}
pub fn http_request_fee(
&self,
request_size: NumBytes,
response_size_limit: Option<NumBytes>,
) -> Cycles {
self.scheduler.cycles_account_manager.http_request_fee(
request_size,
response_size_limit,
self.subnet_size(),
)
}
pub fn memory_cost(&self, bytes: NumBytes, duration: Duration) -> Cycles {
self.scheduler
.cycles_account_manager
.memory_cost(bytes, duration, self.subnet_size())
}
pub(crate) fn deliver_quadruple_ids(
&mut self,
ecdsa_quadruple_ids: BTreeMap<EcdsaKeyId, BTreeSet<QuadrupleId>>,
) {
self.ecdsa_quadruple_ids = ecdsa_quadruple_ids;
}
}
/// A builder for `SchedulerTest`.
pub(crate) struct SchedulerTestBuilder {
own_subnet_id: SubnetId,
nns_subnet_id: SubnetId,
subnet_type: SubnetType,
batch_time: Time,
scheduler_config: SchedulerConfig,
initial_canister_cycles: Cycles,
subnet_message_memory: u64,
registry_settings: RegistryExecutionSettings,
allocatable_compute_capacity_in_percent: usize,
rate_limiting_of_instructions: bool,
rate_limiting_of_heap_delta: bool,
deterministic_time_slicing: bool,
log: ReplicaLogger,
ecdsa_keys: Vec<EcdsaKeyId>,
metrics_registry: MetricsRegistry,
}
impl Default for SchedulerTestBuilder {
fn default() -> Self {
let subnet_type = SubnetType::Application;
let scheduler_config = SubnetConfig::new(subnet_type).scheduler_config;
let config = ic_config::execution_environment::Config::default();
Self {
own_subnet_id: subnet_test_id(1),
nns_subnet_id: subnet_test_id(2),
subnet_type,
batch_time: UNIX_EPOCH,
scheduler_config,
initial_canister_cycles: Cycles::new(1_000_000_000_000_000_000),
subnet_message_memory: config.subnet_message_memory_capacity.get(),
registry_settings: test_registry_settings(),
allocatable_compute_capacity_in_percent: 100,
rate_limiting_of_instructions: false,
rate_limiting_of_heap_delta: false,
deterministic_time_slicing: true,
log: no_op_logger(),
ecdsa_keys: vec![],
metrics_registry: MetricsRegistry::new(),
}
}
}
impl SchedulerTestBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn with_subnet_type(self, subnet_type: SubnetType) -> Self {
let scheduler_config = SubnetConfig::new(subnet_type).scheduler_config;
Self {
subnet_type,
scheduler_config,
..self
}
}
pub fn with_subnet_message_memory(self, subnet_message_memory: u64) -> Self {
Self {
subnet_message_memory,
..self
}
}
pub fn with_scheduler_config(self, scheduler_config: SchedulerConfig) -> Self {
Self {
scheduler_config,
..self
}
}
pub fn with_rate_limiting_of_instructions(self) -> Self {
Self {
rate_limiting_of_instructions: true,
..self
}
}
pub fn with_rate_limiting_of_heap_delta(self) -> Self {
Self {
rate_limiting_of_heap_delta: true,
..self
}
}
pub fn with_ecdsa_key(self, ecdsa_key: EcdsaKeyId) -> Self {
Self {
ecdsa_keys: vec![ecdsa_key],
..self
}
}
pub fn with_ecdsa_keys(self, ecdsa_keys: Vec<EcdsaKeyId>) -> Self {
Self { ecdsa_keys, ..self }
}
pub fn with_batch_time(self, batch_time: Time) -> Self {
Self { batch_time, ..self }
}
pub fn build(self) -> SchedulerTest {
let first_xnet_canister = u64::MAX / 2;
let routing_table = Arc::new(
RoutingTable::try_from(btreemap! {
CanisterIdRange { start: CanisterId::from(0x0), end: CanisterId::from(first_xnet_canister) } => self.own_subnet_id,
}).unwrap()
);
let mut state = ReplicatedState::new(self.own_subnet_id, self.subnet_type);
state.metadata.network_topology.subnets = generate_subnets(
vec![self.own_subnet_id, self.nns_subnet_id],
self.own_subnet_id,
self.subnet_type,
self.registry_settings.subnet_size,
);
state.metadata.network_topology.routing_table = routing_table;
state.metadata.network_topology.nns_subnet_id = self.nns_subnet_id;
state.metadata.batch_time = self.batch_time;
let config = SubnetConfig::new(self.subnet_type).cycles_account_manager_config;
for ecdsa_key in &self.ecdsa_keys {
state
.metadata
.network_topology
.ecdsa_signing_subnets
.insert(ecdsa_key.clone(), vec![self.own_subnet_id]);
state
.metadata
.network_topology
.subnets
.get_mut(&self.own_subnet_id)
.unwrap()
.ecdsa_keys_held
.insert(ecdsa_key.clone());
}
let ecdsa_subnet_public_keys: BTreeMap<EcdsaKeyId, MasterEcdsaPublicKey> = self
.ecdsa_keys
.into_iter()
.map(|key| {
(
key,
MasterEcdsaPublicKey {
algorithm_id: AlgorithmId::Secp256k1,
public_key: b"abababab".to_vec(),
},
)
})
.collect();
let cycles_account_manager = CyclesAccountManager::new(
self.scheduler_config.max_instructions_per_message,
self.subnet_type,
self.own_subnet_id,
config,
);
let cycles_account_manager = Arc::new(cycles_account_manager);
let rate_limiting_of_instructions = if self.rate_limiting_of_instructions {
FlagStatus::Enabled
} else {
FlagStatus::Disabled
};
let rate_limiting_of_heap_delta = if self.rate_limiting_of_heap_delta {
FlagStatus::Enabled
} else {
FlagStatus::Disabled
};
let deterministic_time_slicing = if self.deterministic_time_slicing {
FlagStatus::Enabled
} else {
FlagStatus::Disabled
};
let config = ic_config::execution_environment::Config {
allocatable_compute_capacity_in_percent: self.allocatable_compute_capacity_in_percent,
subnet_message_memory_capacity: NumBytes::from(self.subnet_message_memory),
rate_limiting_of_instructions,
rate_limiting_of_heap_delta,
deterministic_time_slicing,
..ic_config::execution_environment::Config::default()
};
let wasm_executor = Arc::new(TestWasmExecutor::new(
Arc::clone(&cycles_account_manager),
self.registry_settings.subnet_size,
));
let hypervisor = Hypervisor::new_for_testing(
&self.metrics_registry,
self.own_subnet_id,
self.subnet_type,
self.log.clone(),
Arc::clone(&cycles_account_manager),
Arc::<TestWasmExecutor>::clone(&wasm_executor),
deterministic_time_slicing,
config.embedders_config.cost_to_compile_wasm_instruction,
SchedulerConfig::application_subnet().dirty_page_overhead,
);
let hypervisor = Arc::new(hypervisor);
let ingress_history_writer =
IngressHistoryWriterImpl::new(config.clone(), self.log.clone(), &self.metrics_registry);
let ingress_history_writer: Arc<dyn IngressHistoryWriter<State = ReplicatedState>> =
Arc::new(ingress_history_writer);
let exec_env = ExecutionEnvironment::new(
self.log.clone(),
hypervisor,
Arc::clone(&ingress_history_writer),
&self.metrics_registry,
self.own_subnet_id,
self.subnet_type,
SchedulerImpl::compute_capacity_percent(self.scheduler_config.scheduler_cores),
config,
Arc::clone(&cycles_account_manager),
self.scheduler_config.scheduler_cores,
Arc::new(TestPageAllocatorFileDescriptorImpl::new()),
self.scheduler_config.heap_delta_rate_limit,
self.scheduler_config.upload_wasm_chunk_instructions,
);
let scheduler = SchedulerImpl::new(
self.scheduler_config,
self.own_subnet_id,
ingress_history_writer,
Arc::new(exec_env),
Arc::clone(&cycles_account_manager),
&self.metrics_registry,
self.log,
rate_limiting_of_heap_delta,
rate_limiting_of_instructions,
deterministic_time_slicing,
Arc::new(TestPageAllocatorFileDescriptorImpl::new()),
);
SchedulerTest {
state: Some(state),
next_canister_id: 0,
round: ExecutionRound::new(0),
initial_canister_cycles: self.initial_canister_cycles,
user_id: user_test_id(1),
xnet_canister_id: canister_test_id(first_xnet_canister),
scheduler,
wasm_executor,
registry_settings: self.registry_settings,
metrics_registry: self.metrics_registry,
ecdsa_subnet_public_keys,
ecdsa_quadruple_ids: BTreeMap::new(),
}
}
}
/// A test message specifies the results returned when the message is executed
/// by the fake Wasm executor:
/// - the number of instructions consumed by execution.
/// - the number of dirty pages produced by execution.
/// - outgoing calls to other canisters produced by execution.
///
/// A test message can be constructed using the helper functions defined below:
/// - `ingress(5)`: a message that uses 5 instructions.
/// - `ingress(5).dirty_pages(1): a message that uses 5 instructions and
/// modifies one page.
/// - `ingress(5).call(other_side(callee, 3), on_response(8))`: a message
/// that uses 5 instructions and calls a canister with id `callee`.
/// The called message uses 3 instructions. The response handler uses
/// 8 instructions.
#[derive(Clone, Debug)]
pub(crate) struct TestMessage {
// The canister id is optional and is inferred from the context if not
// provided.
canister: Option<CanisterId>,
// The number of instructions that execution of this message will use.
instructions: NumInstructions,
// The number of 4KiB pages that execution of this message will writes to.
dirty_pages: usize,
// The outgoing calls that will be produced by execution of this message.
calls: Vec<TestCall>,
}
impl TestMessage {
pub fn dirty_pages(self, dirty_pages: usize) -> TestMessage {
Self {
dirty_pages,
..self
}
}
pub fn call(mut self, other_side: TestMessage, on_response: TestMessage) -> TestMessage {
self.calls.push(TestCall {
other_side,
on_response,
});
self
}
}
// An internal helper struct to store the description of an inter-canister call.
#[derive(Clone, Debug)]
struct TestCall {
// The message to execute on the callee side.
other_side: TestMessage,
// The response handler to execute on the caller side.
on_response: TestMessage,
}
/// Description of an `install_code` message.
///
/// Note that the `start` and `canister_preupgrade` methods are not supported
/// due to limitation of the testing framework that relies on the incoming
/// payload to keep track of test message.
#[derive(Clone, Debug)]
pub(crate) enum TestInstallCode {
Install { init: TestMessage },
Reinstall { init: TestMessage },
Upgrade { post_upgrade: TestMessage },
}
/// A helper to create an ingress test message. Note that the canister id is not
/// needed and will be specified by the function that enqueues the ingress.
pub(crate) fn ingress(instructions: u64) -> TestMessage {
TestMessage {
canister: None,
instructions: NumInstructions::from(instructions),
dirty_pages: 0,
calls: vec![],
}
}
/// A helper to create the test message of the callee.
pub(crate) fn other_side(callee: CanisterId, instructions: u64) -> TestMessage {
TestMessage {
canister: Some(callee),
instructions: NumInstructions::from(instructions),
dirty_pages: 0,
calls: vec![],
}
}
/// A helper to create the test message for handling the response of a call.
/// Note that the canister id is not needed and is inferred from the context.
pub(crate) fn on_response(instructions: u64) -> TestMessage {
TestMessage {
canister: None,
instructions: NumInstructions::from(instructions),
dirty_pages: 0,
calls: vec![],
}
}
/// A generic helper to describe a phase like `start`, `init`, `pre_upgrade`,
/// `post_upgrade` of an install code message.
pub(crate) fn instructions(instructions: u64) -> TestMessage {
TestMessage {
canister: None,
instructions: NumInstructions::from(instructions),
dirty_pages: 0,
calls: vec![],
}
}
// A wrapper around the fake Wasm executor.
// This wrapper is needs to guaranteed thread-safety.
struct TestWasmExecutor {
core: Mutex<TestWasmExecutorCore>,
}
impl TestWasmExecutor {
fn new(cycles_account_manager: Arc<CyclesAccountManager>, subnet_size: usize) -> Self {
Self {
core: Mutex::new(TestWasmExecutorCore::new(
cycles_account_manager,
subnet_size,
)),
}
}
}
impl WasmExecutor for TestWasmExecutor {
// The entry point of the Wasm executor.
//
// It finds the test message corresponding to the given input and "executes"
// it by interpreting its description.
fn execute(
self: Arc<Self>,
input: WasmExecutionInput,
execution_state: &ExecutionState,
) -> (Option<CompilationResult>, WasmExecutionResult) {
let (message_id, message, call_context_id) = {
let mut guard = self.core.lock().unwrap();
guard.take_message(&input)
};
let execution = TestPausedWasmExecution {
message_id,
message,
sandbox_safe_system_state: input.sandbox_safe_system_state,
execution_parameters: input.execution_parameters,
canister_current_memory_usage: input.canister_current_memory_usage,
canister_current_message_memory_usage: input.canister_current_message_memory_usage,
call_context_id,
instructions_executed: NumInstructions::from(0),
executor: Arc::clone(&self),
};
let result = Box::new(execution).resume(execution_state);