Skip to content

Commit

Permalink
pass in program id
Browse files Browse the repository at this point in the history
  • Loading branch information
MaanavKhaitan committed Nov 18, 2024
1 parent c03980a commit 3989eec
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
10 changes: 6 additions & 4 deletions contracts/src/coprocessor/JobManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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";
Expand All @@ -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();
Expand All @@ -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";
Expand All @@ -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();
Expand Down
24 changes: 18 additions & 6 deletions zkvm-utils/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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,

Expand All @@ -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,

Expand Down Expand Up @@ -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,
Expand All @@ -82,6 +94,7 @@ pub async fn main() -> Result<()> {
}
Command::ExecuteOffchainJob {
guest_binary_path,
program_id,
onchain_input,
offchain_input,
max_cycles,
Expand All @@ -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,
Expand All @@ -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<u8>,
onchain_input: Vec<u8>,
job_id: [u8; 32],
max_cycles: u64,
zkvm_executor: &ZkvmExecutorService<LocalSigner<SigningKey>>,
) -> 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
Expand All @@ -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<u8>,
onchain_input: Vec<u8>,
offchain_input: Vec<u8>,
max_cycles: u64,
Expand All @@ -148,7 +161,6 @@ async fn execute_offchain_job_ffi(
zkvm_executor: &ZkvmExecutorService<LocalSigner<SigningKey>>,
) -> 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
Expand Down

0 comments on commit 3989eec

Please sign in to comment.