From 3989eec14a77021506f0b2e51d946b22046fdd6b Mon Sep 17 00:00:00 2001 From: Maanav Khaitan Date: Mon, 18 Nov 2024 20:09:09 +0530 Subject: [PATCH] pass in program id --- contracts/src/coprocessor/JobManager.sol | 10 ++++++---- zkvm-utils/src/main.rs | 24 ++++++++++++++++++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/contracts/src/coprocessor/JobManager.sol b/contracts/src/coprocessor/JobManager.sol index 9b5ac8f..866e65b 100644 --- a/contracts/src/coprocessor/JobManager.sol +++ b/contracts/src/coprocessor/JobManager.sol @@ -92,7 +92,7 @@ contract JobManager is string memory elfPath = getElfPath(programID); // This would normally be a separate call by relayer, but for tests we call it here - (bytes memory resultWithMetadata, bytes memory signature) = executeOnchainJob(elfPath, onchainInput, jobID, maxCycles);(elfPath, onchainInput, jobID, maxCycles); + (bytes memory resultWithMetadata, bytes memory signature) = executeOnchainJob(elfPath, programID, onchainInput, jobID, maxCycles);(elfPath, onchainInput, jobID, maxCycles); submitResult(resultWithMetadata, signature); return jobID; @@ -215,8 +215,8 @@ contract JobManager is Consumer(job.consumer).receiveResult(jobID, result); } - function executeOnchainJob(string memory elfPath, bytes memory onchainInput, bytes32 jobID, uint64 maxCycles) internal returns (bytes memory, bytes memory) { - string[] memory imageRunnerInput = new string[](12); + function executeOnchainJob(string memory elfPath, bytes32 programID, bytes memory onchainInput, bytes32 jobID, uint64 maxCycles) internal returns (bytes memory, bytes memory) { + string[] memory imageRunnerInput = new string[](13); uint256 i = 0; imageRunnerInput[i++] = "cargo"; imageRunnerInput[i++] = "run"; @@ -227,6 +227,7 @@ contract JobManager is imageRunnerInput[i++] = "-q"; imageRunnerInput[i++] = "execute-onchain-job"; imageRunnerInput[i++] = elfPath; + imageRunnerInput[i++] = programID.toHexString(); imageRunnerInput[i++] = onchainInput.toHexString(); imageRunnerInput[i++] = jobID.toHexString(); imageRunnerInput[i++] = maxCycles.toString(); @@ -235,7 +236,7 @@ contract JobManager is function executeOffchainJob(OffchainJobRequest calldata request, bytes calldata offchainInput, string calldata privateKey) internal returns (bytes memory, bytes memory, bytes memory, bytes memory) { string memory elfPath = getElfPath(request.programID); - string[] memory imageRunnerInput = new string[](15); + string[] memory imageRunnerInput = new string[](16); uint256 i = 0; imageRunnerInput[i++] = "cargo"; imageRunnerInput[i++] = "run"; @@ -246,6 +247,7 @@ contract JobManager is imageRunnerInput[i++] = "-q"; imageRunnerInput[i++] = "execute-offchain-job"; imageRunnerInput[i++] = elfPath; + imageRunnerInput[i++] = request.programID.toHexString(); imageRunnerInput[i++] = request.onchainInput.toHexString(); imageRunnerInput[i++] = offchainInput.toHexString(); imageRunnerInput[i++] = request.maxCycles.toString(); diff --git a/zkvm-utils/src/main.rs b/zkvm-utils/src/main.rs index e9f2b6c..adb340e 100644 --- a/zkvm-utils/src/main.rs +++ b/zkvm-utils/src/main.rs @@ -12,7 +12,6 @@ use anyhow::{Context, Result}; use clap::Parser; use ivm_abi::{abi_encode_offchain_job_request, get_job_id, JobParams}; use ivm_proto::VmType; -use ivm_zkvm::Zkvm; use ivm_zkvm_executor::service::ZkvmExecutorService; use k256::ecdsa::SigningKey; @@ -26,6 +25,9 @@ enum Command { /// The guest binary path guest_binary_path: String, + /// The hex-encoded program ID + program_id: String, + /// The hex-encoded input to provide to the guest binary onchain_input: String, @@ -40,6 +42,9 @@ enum Command { /// The guest binary path guest_binary_path: String, + /// The hex-encoded program ID + program_id: String, + /// The hex encoded input to provide to the guest binary onchain_input: String, @@ -68,11 +73,18 @@ pub async fn main() -> Result<()> { let zkvm_executor = ZkvmExecutorService::new(signer); match Command::parse() { - Command::ExecuteOnchainJob { guest_binary_path, onchain_input, job_id, max_cycles } => { + Command::ExecuteOnchainJob { + guest_binary_path, + program_id, + onchain_input, + job_id, + max_cycles, + } => { let job_id_decoded: [u8; 32] = hex::decode(job_id.strip_prefix("0x").unwrap_or(&job_id))?.try_into().unwrap(); execute_onchain_job_ffi( guest_binary_path, + hex::decode(program_id.strip_prefix("0x").unwrap_or(&program_id))?, hex::decode(onchain_input.strip_prefix("0x").unwrap_or(&onchain_input))?, job_id_decoded, max_cycles, @@ -82,6 +94,7 @@ pub async fn main() -> Result<()> { } Command::ExecuteOffchainJob { guest_binary_path, + program_id, onchain_input, offchain_input, max_cycles, @@ -91,6 +104,7 @@ pub async fn main() -> Result<()> { } => { execute_offchain_job_ffi( guest_binary_path, + hex::decode(program_id.strip_prefix("0x").unwrap_or(&program_id))?, hex::decode(onchain_input.strip_prefix("0x").unwrap_or(&onchain_input))?, hex::decode(offchain_input.strip_prefix("0x").unwrap_or(&offchain_input))?, max_cycles, @@ -110,15 +124,13 @@ pub async fn main() -> Result<()> { /// for an onchain job. async fn execute_onchain_job_ffi( elf_path: String, + program_id: Vec, onchain_input: Vec, job_id: [u8; 32], max_cycles: u64, zkvm_executor: &ZkvmExecutorService>, ) -> Result<()> { let elf = std::fs::read(elf_path).unwrap(); - // TODO: pass this in instead of re-deriving it? - let program_id = ivm_zkvm::Sp1.derive_verifying_key(&elf)?; - let (result_with_metadata, zkvm_operator_signature) = zkvm_executor .execute_onchain_job(job_id, max_cycles, program_id, onchain_input, elf, VmType::Sp1) .await @@ -139,6 +151,7 @@ async fn execute_onchain_job_ffi( /// for an offchain job. async fn execute_offchain_job_ffi( elf_path: String, + program_id: Vec, onchain_input: Vec, offchain_input: Vec, max_cycles: u64, @@ -148,7 +161,6 @@ async fn execute_offchain_job_ffi( zkvm_executor: &ZkvmExecutorService>, ) -> Result<()> { let elf = std::fs::read(elf_path).unwrap(); - let program_id = ivm_zkvm::Sp1.derive_verifying_key(&elf)?; let offchain_input_hash = keccak256(offchain_input.clone()); // Create a signed job request