|
| 1 | +use starknet_os::io::virtual_os_output::{VirtualOsOutput, VirtualOsRunnerOutput}; |
| 2 | +use starknet_os::runner::run_virtual_os; |
| 3 | +use starknet_types_core::felt::Felt; |
| 4 | + |
| 5 | +use crate::initial_state::FlowTestState; |
| 6 | +use crate::test_manager::TestRunner; |
| 7 | + |
| 8 | +/// The output of running the virtual OS for testing. |
| 9 | +pub(crate) struct VirtualOsTestOutput { |
| 10 | + /// The raw runner output from the virtual OS. |
| 11 | + pub(crate) runner_output: VirtualOsRunnerOutput, |
| 12 | + /// The expected values computed from the OS hints. |
| 13 | + pub(crate) expected_virtual_os_output: VirtualOsOutput, |
| 14 | + // TODO(Yoni): consider adding more data for sanity checks, such as the expected state diff. |
| 15 | +} |
| 16 | + |
| 17 | +impl VirtualOsTestOutput { |
| 18 | + /// Validates the runner output against the expected values. |
| 19 | + pub(crate) fn validate(&self) { |
| 20 | + let virtual_os_output = VirtualOsOutput::from_raw_output(&self.runner_output.raw_output) |
| 21 | + .expect("Parsing virtual OS output should not fail."); |
| 22 | + |
| 23 | + assert_eq!(virtual_os_output, self.expected_virtual_os_output); |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +impl<S: FlowTestState> TestRunner<S> { |
| 28 | + /// Runs the virtual OS and returns the test output. |
| 29 | + pub(crate) fn run_virtual(self) -> VirtualOsTestOutput { |
| 30 | + // Create expected values before running the virtual OS (os_hints is consumed). |
| 31 | + let first_block = self.os_hints.os_input.os_block_inputs.first().unwrap(); |
| 32 | + // The virtual os does support state diff encryption. |
| 33 | + let public_keys = None; |
| 34 | + let config_hash = |
| 35 | + self.os_hints.os_hints_config.chain_info.compute_os_config_hash(public_keys).unwrap(); |
| 36 | + |
| 37 | + let expected_virtual_os_output = VirtualOsOutput { |
| 38 | + version: Felt::ZERO, |
| 39 | + base_block_number: first_block.block_info.block_number, |
| 40 | + base_block_hash: first_block.new_block_hash.0, |
| 41 | + starknet_os_config_hash: config_hash, |
| 42 | + messages_to_l1: self.messages_to_l1, |
| 43 | + }; |
| 44 | + |
| 45 | + // Run the virtual OS. |
| 46 | + let runner_output = |
| 47 | + run_virtual_os(self.os_hints).expect("Running virtual OS should not fail."); |
| 48 | + |
| 49 | + VirtualOsTestOutput { runner_output, expected_virtual_os_output } |
| 50 | + } |
| 51 | + |
| 52 | + /// Runs the virtual OS and validates the output against expected values. |
| 53 | + pub(crate) fn run_virtual_and_validate(self) { |
| 54 | + self.run_virtual().validate(); |
| 55 | + } |
| 56 | +} |
0 commit comments