|
| 1 | +use alloc::vec::Vec; |
| 2 | +use polkavm::{Config, Engine, Linker, Module, ModuleConfig, ProgramBlob}; |
| 3 | + |
| 4 | +use crate::context::PvqExecutorContext; |
| 5 | +use crate::error::PvqExecutorError; |
| 6 | + |
| 7 | +type PvqExecutorResult<UserError> = Result<Vec<u8>, PvqExecutorError<UserError>>; |
| 8 | +type GasLimit = Option<i64>; |
| 9 | + |
| 10 | +pub struct PvqExecutor<Ctx: PvqExecutorContext> { |
| 11 | + engine: Engine, |
| 12 | + linker: Linker<Ctx::UserData, Ctx::UserError>, |
| 13 | + context: Ctx, |
| 14 | +} |
| 15 | + |
| 16 | +impl<Ctx: PvqExecutorContext> PvqExecutor<Ctx> { |
| 17 | + pub fn new(config: Config, mut context: Ctx) -> Self { |
| 18 | + let engine = Engine::new(&config).unwrap(); |
| 19 | + let mut linker = Linker::<Ctx::UserData, Ctx::UserError>::new(); |
| 20 | + // Register user-defined host functions |
| 21 | + context.register_host_functions(&mut linker); |
| 22 | + Self { |
| 23 | + engine, |
| 24 | + linker, |
| 25 | + context, |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + pub fn execute( |
| 30 | + &mut self, |
| 31 | + program: &[u8], |
| 32 | + args: &[u8], |
| 33 | + gas_limit: GasLimit, |
| 34 | + ) -> (PvqExecutorResult<Ctx::UserError>, GasLimit) { |
| 35 | + let blob = match ProgramBlob::parse(program.into()) { |
| 36 | + Ok(blob) => blob, |
| 37 | + Err(_) => return (Err(PvqExecutorError::InvalidProgramFormat), gas_limit), |
| 38 | + }; |
| 39 | + |
| 40 | + let mut module_config = ModuleConfig::new(); |
| 41 | + module_config.set_aux_data_size(args.len() as u32); |
| 42 | + if gas_limit.is_some() { |
| 43 | + module_config.set_gas_metering(Some(polkavm::GasMeteringKind::Sync)); |
| 44 | + } |
| 45 | + |
| 46 | + let module = match Module::from_blob(&self.engine, &module_config, blob) { |
| 47 | + Ok(module) => module, |
| 48 | + Err(err) => return (Err(err.into()), gas_limit), |
| 49 | + }; |
| 50 | + |
| 51 | + let instance_pre = match self.linker.instantiate_pre(&module) { |
| 52 | + Ok(instance_pre) => instance_pre, |
| 53 | + Err(err) => return (Err(err.into()), gas_limit), |
| 54 | + }; |
| 55 | + |
| 56 | + let mut instance = match instance_pre.instantiate() { |
| 57 | + Ok(instance) => instance, |
| 58 | + Err(err) => return (Err(err.into()), gas_limit), |
| 59 | + }; |
| 60 | + |
| 61 | + if let Some(gas_limit) = gas_limit { |
| 62 | + instance.set_gas(gas_limit); |
| 63 | + } |
| 64 | + |
| 65 | + // From this point on, we include instance.gas() in the return value |
| 66 | + let result = (|| { |
| 67 | + instance.write_memory(module.memory_map().aux_data_address(), args)?; |
| 68 | + |
| 69 | + tracing::info!("Calling entrypoint with args: {:?}", args); |
| 70 | + let res = instance.call_typed_and_get_result::<u64, (u32, u32)>( |
| 71 | + self.context.data(), |
| 72 | + "pvq", |
| 73 | + (module.memory_map().aux_data_address(), args.len() as u32), |
| 74 | + )?; |
| 75 | + |
| 76 | + let res_size = (res >> 32) as u32; |
| 77 | + let res_ptr = (res & 0xffffffff) as u32; |
| 78 | + |
| 79 | + let result = instance.read_memory(res_ptr, res_size)?; |
| 80 | + |
| 81 | + tracing::info!("Result: {:?}", result); |
| 82 | + Ok(result) |
| 83 | + })(); |
| 84 | + |
| 85 | + if gas_limit.is_some() { |
| 86 | + (result, Some(instance.gas())) |
| 87 | + } else { |
| 88 | + (result, None) |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments