Skip to content

Commit

Permalink
Merge branch 'EXC-1262-fix-impl-partial-eq-for-execution-state' into …
Browse files Browse the repository at this point in the history
…'master'

fix: Fix implementation of PartialEq for ExecutionState

Closes EXC-1262 

Closes EXC-1262

See merge request dfinity-lab/public/ic!11135
  • Loading branch information
dragoljub-duric committed Mar 7, 2023
2 parents 887e10e + c879272 commit 9a22746
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 4 deletions.
29 changes: 25 additions & 4 deletions rs/replicated_state/src/canister_state/execution_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,16 +410,37 @@ pub struct ExecutionState {
// equality (and doesn't need to be).
impl PartialEq for ExecutionState {
fn eq(&self, rhs: &Self) -> bool {
// Destruction is done on purpose, to ensure if the new
// field is added to 'ExecutionState' compiler will throw
// an error. Hence pointing to appropriate change here.
let &ExecutionState {
canister_root: _,
session_nonce: _,
ref wasm_binary,
ref wasm_memory,
ref stable_memory,
ref exported_globals,
ref exports,
ref metadata,
ref last_executed_round,
} = rhs;

(
&self.wasm_binary.binary,
&self.wasm_memory,
&self.stable_memory,
&self.exported_globals,
&self.exports,
&self.metadata,
&self.last_executed_round,
) == (
&rhs.wasm_binary.binary,
&rhs.wasm_memory,
&rhs.exported_globals,
&rhs.exports,
&wasm_binary.binary,
wasm_memory,
stable_memory,
exported_globals,
exports,
metadata,
last_executed_round,
)
}
}
Expand Down
98 changes: 98 additions & 0 deletions rs/replicated_state/src/canister_state/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use std::collections::BTreeMap;
use std::path::PathBuf;

use super::*;
use crate::canister_state::execution_state::CustomSection;
use crate::canister_state::execution_state::CustomSectionType;
use crate::canister_state::execution_state::WasmMetadata;
use crate::canister_state::system_state::CyclesUseCase;
use crate::CallOrigin;
Expand Down Expand Up @@ -608,3 +613,96 @@ fn canister_state_callback_round_trip() {

assert_eq!(callback, round_trip);
}

#[test]
fn execution_state_test_partial_eq() {
let state_1 = ExecutionState::new(
Default::default(),
execution_state::WasmBinary::new(CanisterModule::new(vec![1, 2, 3])),
ExportedFunctions::new(Default::default()),
Memory::new_for_testing(),
Memory::new_for_testing(),
vec![Global::I64(14)],
WasmMetadata::default(),
);

assert_eq!(state_1, state_1.clone());

assert_eq!(
ExecutionState {
canister_root: PathBuf::new(),
..state_1.clone()
},
state_1
);

assert_eq!(
ExecutionState {
session_nonce: Some(([11; 32], 10)),
..state_1.clone()
},
state_1
);

assert_ne!(
ExecutionState {
wasm_binary: execution_state::WasmBinary::new(CanisterModule::new(vec![1, 2, 4])),
..state_1.clone()
},
state_1
);

assert_ne!(
ExecutionState {
exports: ExportedFunctions::new(BTreeSet::from([WasmMethod::System(
SystemMethod::CanisterGlobalTimer
)])),
..state_1.clone()
},
state_1
);
let mut memory = Memory::new_for_testing();
memory.size = NumWasmPages::from(1);
assert_ne!(
ExecutionState {
wasm_memory: memory.clone(),
..state_1.clone()
},
state_1
);
assert_ne!(
ExecutionState {
stable_memory: memory,
..state_1.clone()
},
state_1
);

assert_ne!(
ExecutionState {
exported_globals: vec![Global::I64(13)],
..state_1.clone()
},
state_1
);
let mut custom_sections: BTreeMap<String, CustomSection> = BTreeMap::new();
custom_sections.insert(
String::from("candid"),
CustomSection::new(CustomSectionType::Private, vec![0; 10 * 1024]),
);
assert_ne!(
ExecutionState {
metadata: WasmMetadata::new(custom_sections),
..state_1.clone()
},
state_1
);

assert_ne!(
ExecutionState {
last_executed_round: ExecutionRound::from(12345),
..state_1.clone()
},
state_1
);
}

0 comments on commit 9a22746

Please sign in to comment.