Skip to content

Commit

Permalink
chore: Use let-else in embedders and execution_environment
Browse files Browse the repository at this point in the history
  • Loading branch information
dfinity-berestovskyy committed Jan 5, 2024
1 parent 97416c3 commit e198508
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 83 deletions.
17 changes: 8 additions & 9 deletions rs/embedders/src/wasm_executor.rs
Expand Up @@ -272,15 +272,14 @@ impl WasmExecutor for WasmExecutorImpl {
) -> HypervisorResult<(ExecutionState, NumInstructions, Option<CompilationResult>)> {
// Compile Wasm binary and cache it.
let wasm_binary = WasmBinary::new(canister_module);
let (embedder_cache, serialized_module, compilation_result) =
match self.get_embedder_cache(&wasm_binary, compilation_cache)? {
CacheLookup {
cache,
serialized_module: Some(serialized_module),
compilation_result,
} => (cache, serialized_module, compilation_result),
_ => panic!("Newly created WasmBinary must be compiled or deserialized."),
};
let CacheLookup {
cache: embedder_cache,
serialized_module: Some(serialized_module),
compilation_result,
} = self.get_embedder_cache(&wasm_binary, compilation_cache)?
else {
panic!("Newly created WasmBinary must be compiled or deserialized.")
};
self.observe_metrics(&serialized_module.imports_details);
let exported_functions = serialized_module.exported_functions.clone();
let wasm_metadata = serialized_module.wasm_metadata.clone();
Expand Down
14 changes: 7 additions & 7 deletions rs/embedders/src/wasmtime_embedder.rs
Expand Up @@ -1064,13 +1064,13 @@ impl WasmtimeInstance {

/// Returns the current instruction counter.
pub fn instruction_counter(&mut self) -> i64 {
match self.store.data().num_instructions_global {
Some(num_instructions) => match num_instructions.get(&mut self.store) {
Val::I64(instruction_counter) => instruction_counter,
_ => panic!("invalid instruction counter type"),
},
None => panic!("couldn't find the instruction counter in the canister globals"),
}
let Some(num_instructions) = self.store.data().num_instructions_global else {
panic!("couldn't find the instruction counter in the canister globals");
};
let Val::I64(instruction_counter) = num_instructions.get(&mut self.store) else {
panic!("invalid instruction counter type");
};
instruction_counter
}

/// Returns the heap size.
Expand Down
14 changes: 6 additions & 8 deletions rs/embedders/tests/wasmtime_embedder.rs
Expand Up @@ -244,19 +244,17 @@ mod test {
ic_types::methods::WasmMethod::Update("test_performance_counter".to_string()),
))
.unwrap();
let performance_counter1 = match res.exported_globals[0] {
Global::I64(c) => c as u64,
_ => panic!("Error getting performance_counter1"),
let Global::I64(performance_counter1) = res.exported_globals[0] else {
panic!("Error getting performance_counter1");
};
let performance_counter2 = match res.exported_globals[1] {
Global::I64(c) => c as u64,
_ => panic!("Error getting performance_counter2"),
let Global::I64(performance_counter2) = res.exported_globals[1] else {
panic!("Error getting performance_counter2");
};
let instruction_counter = instance.instruction_counter();
let system_api = &instance.store_data().system_api().unwrap();
let instructions_used = system_api.slice_instructions_executed(instruction_counter);
assert_eq!(performance_counter1, expected_instructions_counter1);
assert_eq!(performance_counter2, expected_instructions_counter2);
assert_eq!(performance_counter1 as u64, expected_instructions_counter1);
assert_eq!(performance_counter2 as u64, expected_instructions_counter2);

assert_eq!(instructions_used.get(), expected_instructions);
}
Expand Down
18 changes: 8 additions & 10 deletions rs/execution_environment/benches/system_api/execute_update.rs
Expand Up @@ -358,16 +358,14 @@ pub fn execute_update_bench(c: &mut Criterion) {
ExecuteMessageResult::Finished { response, .. } => response,
ExecuteMessageResult::Paused { .. } => panic!("Unexpected paused execution"),
};
match response {
ExecutionResponse::Ingress((_, status)) => match status {
IngressStatus::Known { state, .. } => {
if let IngressState::Failed(err) = state {
assert_eq!(err.code(), ErrorCode::CanisterDidNotReply)
}
}
_ => panic!("Unexpected ingress status"),
},
_ => panic!("Expected ingress result"),
let ExecutionResponse::Ingress((_, status)) = response else {
panic!("Expected ingress result");
};
let IngressStatus::Known { state, .. } = status else {
panic!("Unexpected ingress status");
};
if let IngressState::Failed(err) = state {
assert_eq!(err.code(), ErrorCode::CanisterDidNotReply)
}
assert_eq!(
expected_instructions,
Expand Down
18 changes: 8 additions & 10 deletions rs/execution_environment/benches/wasm_instructions/main.rs
Expand Up @@ -438,16 +438,14 @@ pub fn wasm_instructions_bench(c: &mut Criterion) {
ExecuteMessageResult::Finished { response, .. } => response,
ExecuteMessageResult::Paused { .. } => panic!("Unexpected paused execution"),
};
match response {
ExecutionResponse::Ingress((_, status)) => match status {
IngressStatus::Known { state, .. } => {
if let IngressState::Failed(err) = state {
assert_eq!(err.code(), ErrorCode::CanisterDidNotReply)
}
}
_ => panic!("Unexpected ingress status"),
},
_ => panic!("Expected ingress result"),
let ExecutionResponse::Ingress((_, status)) = response else {
panic!("Expected ingress result");
};
let IngressStatus::Known { state, .. } = status else {
panic!("Unexpected ingress status");
};
if let IngressState::Failed(err) = state {
assert_eq!(err.code(), ErrorCode::CanisterDidNotReply)
}
},
);
Expand Down
20 changes: 4 additions & 16 deletions rs/execution_environment/src/execution/response/tests.rs
Expand Up @@ -1156,23 +1156,11 @@ fn dts_and_nondts_cycles_match_after_response() {
let status_a = test_a.ingress_status(&amsg_id);
let status_b = test_b.ingress_status(&bmsg_id);
assert_eq!(status_a, status_b);
let time_a = match status_a {
IngressStatus::Known {
receiver: _,
user_id: _,
time,
state: _,
} => time,
_ => unreachable!(),
let IngressStatus::Known { time: time_a, .. } = status_a else {
unreachable!();
};
let time_b = match status_b {
IngressStatus::Known {
receiver: _,
user_id: _,
time,
state: _,
} => time,
_ => unreachable!(),
let IngressStatus::Known { time: time_b, .. } = status_b else {
unreachable!();
};
assert_eq!(time_a, time_b);
assert_eq!(start_time, time_a);
Expand Down
21 changes: 9 additions & 12 deletions rs/execution_environment/src/execution_environment/tests.rs
Expand Up @@ -1405,18 +1405,15 @@ fn create_canister_xnet_called_from_nns() {
);
test.execute_all();
let response = test.xnet_messages()[0].clone();
match response {
RequestOrResponse::Response(response) => {
assert_eq!(response.originator, nns_canister);
assert_eq!(response.respondent, CanisterId::from(own_subnet));
assert_eq!(response.refund, Cycles::new(0));
match response.response_payload {
Payload::Data(_) => (),
_ => panic!("Failed creating the canister."),
}
}
_ => panic!("Type should be RequestOrResponse::Response"),
}
let RequestOrResponse::Response(response) = response else {
panic!("Type should be RequestOrResponse::Response");
};
assert_eq!(response.originator, nns_canister);
assert_eq!(response.respondent, CanisterId::from(own_subnet));
assert_eq!(response.refund, Cycles::new(0));
let Payload::Data(_) = response.response_payload else {
panic!("Failed creating the canister.");
};
}

#[test]
Expand Down
20 changes: 9 additions & 11 deletions rs/execution_environment/src/query_handler/tests.rs
Expand Up @@ -1398,7 +1398,7 @@ fn query_stats_are_collected() {
fn test_incorrect_query_name() {
let test = ExecutionTestBuilder::new().build();
let method = "unknown method".to_string();
match test.query(
let Err(err) = test.query(
UserQuery {
source: user_test_id(2),
receiver: CanisterId::ic_00(),
Expand All @@ -1409,16 +1409,14 @@ fn test_incorrect_query_name() {
},
Arc::new(test.state().clone()),
vec![],
) {
Err(e) => {
assert_eq!(e.code(), ErrorCode::CanisterMethodNotFound);
assert_eq!(
e.description(),
format!("Query method {} not found.", method)
);
}
_ => panic!("Unexpected result."),
}
) else {
panic!("Unexpected result.");
};
assert_eq!(err.code(), ErrorCode::CanisterMethodNotFound);
assert_eq!(
err.description(),
format!("Query method {} not found.", method)
);
}

#[test]
Expand Down

0 comments on commit e198508

Please sign in to comment.