1
use std::rc::Rc;
2

            
3
use anyhow::Result;
4
use bonsol_schema::ProgramInputType;
5
use risc0_binfmt::MemoryImage;
6
use risc0_zkvm::{get_prover_server, ExecutorEnv, ExecutorImpl, ProverOpts, ProverServer, Receipt};
7

            
8
use crate::input_resolver::ProgramInput;
9

            
10
/// Creates a new risc0 executor environment from the provided inputs, it hadles setting up the execution env in the same way across types of provers.
11
pub fn new_risc0_exec_env(
12
    image: MemoryImage,
13
    sorted_inputs: Vec<ProgramInput>,
14
) -> Result<ExecutorImpl<'static>> {
15
    let mut env_builder = ExecutorEnv::builder();
16
    for input in sorted_inputs.into_iter() {
17
        match input {
18
            ProgramInput::Resolved(ri) => {
19
                if ri.input_type == ProgramInputType::PublicProof {
20
                    let receipt: Receipt = bincode::deserialize(&ri.data)?;
21
                    env_builder.add_assumption(receipt);
22
                } else {
23
                    env_builder.write_slice(&ri.data);
24
                }
25
            }
26
            _ => {
27
                return Err(anyhow::anyhow!("Invalid input type"));
28
            }
29
        }
30
    }
31
    let env = env_builder.build()?;
32
    ExecutorImpl::new(env, image)
33
}
34

            
35
/// Gets the default r0 prover for this application
36
/// Since the cli and the node both produce proofs there is a need for a central prover configuration.
37
pub fn get_risc0_prover() -> Result<Rc<dyn ProverServer>> {
38
    let opts = ProverOpts::default();
39
    get_prover_server(&opts)
40
}