diff --git a/crates/sdk/src/lib.rs b/crates/sdk/src/lib.rs index a49b4232b0..801f057377 100644 --- a/crates/sdk/src/lib.rs +++ b/crates/sdk/src/lib.rs @@ -397,13 +397,32 @@ where &self, app_exe: impl Into, inputs: StdIn, + ) -> Result<(Vec, (u64, u64)), SdkError> { + self.execute_metered_cost_with_max_cost(app_exe, inputs, None) + } + + /// Executes with cost metering to measure computational cost in trace cells. + /// Returns both user public values, and cost along with instruction count. + /// + /// # Arguments + /// * `app_exe` - The executable to run + /// * `inputs` - Program inputs + /// * `max_cost` - Optional maximum execution cost. If None, uses the default value. + pub fn execute_metered_cost_with_max_cost( + &self, + app_exe: impl Into, + inputs: StdIn, + max_cost: Option, ) -> Result<(Vec, (u64, u64)), SdkError> { let app_prover = self.app_prover(app_exe)?; let vm = app_prover.vm(); let exe = app_prover.exe(); - let ctx = vm.build_metered_cost_ctx(); + let mut ctx = vm.build_metered_cost_ctx(); + if let Some(max_cost) = max_cost { + ctx = ctx.with_max_execution_cost(max_cost); + } let interpreter = vm .metered_cost_interpreter(&exe) .map_err(VirtualMachineError::from)?; diff --git a/crates/sdk/tests/integration_test.rs b/crates/sdk/tests/integration_test.rs index 304207ea5d..408d77294d 100644 --- a/crates/sdk/tests/integration_test.rs +++ b/crates/sdk/tests/integration_test.rs @@ -481,3 +481,36 @@ fn test_inner_proof_codec_roundtrip() -> eyre::Result<()> { verify_app_proof(&app_vk, &decoded_app_proof)?; Ok(()) } + +#[test] +fn test_execute_metered_cost_with_custom_max_cost() -> eyre::Result<()> { + setup_tracing(); + let app_config = small_test_app_config(1); + let sdk = Sdk::new(app_config)?; + let exe = app_exe_for_test(); + + // Test with default max cost + let (public_values_default, (cost_default, instret_default)) = + sdk.execute_metered_cost(exe.clone(), StdIn::default())?; + + // Test with custom max cost (should be the same as default for this simple program) + let (public_values_custom, (cost_custom, instret_custom)) = + sdk.execute_metered_cost_with_max_cost(exe.clone(), StdIn::default(), None)?; + + // Results should be identical + assert_eq!(public_values_default, public_values_custom); + assert_eq!(cost_default, cost_custom); + assert_eq!(instret_default, instret_custom); + + // Test with a higher max cost (should work normally) + let high_max_cost = cost_default * 2; // Use 200% of the normal cost + let (public_values_high, (cost_high, instret_high)) = + sdk.execute_metered_cost_with_max_cost(exe, StdIn::default(), Some(high_max_cost))?; + + // With higher max cost, execution should work normally + assert_eq!(public_values_high, public_values_default); + assert_eq!(cost_high, cost_default); + assert_eq!(instret_high, instret_default); + + Ok(()) +}