Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion crates/sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,13 +397,32 @@ where
&self,
app_exe: impl Into<ExecutableFormat>,
inputs: StdIn,
) -> Result<(Vec<u8>, (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<ExecutableFormat>,
inputs: StdIn,
max_cost: Option<u64>,
) -> Result<(Vec<u8>, (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)?;
Expand Down
33 changes: 33 additions & 0 deletions crates/sdk/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}