Skip to content

feat: add gas limit #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 27, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
1,043 changes: 218 additions & 825 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ polkavm-derive = { path = "vendor/polkavm/crates/polkavm-derive", default-featur

# polkadot-sdk
sp-api = { path = "vendor/polkadot-sdk/substrate/primitives/api", default-features = false }
frame = { package = "polkadot-sdk-frame", path = "vendor/polkadot-sdk/substrate/frame", default-features = false }
pallet-balances = { path = "vendor/polkadot-sdk/substrate/frame/balances", default-features = false }
pallet-assets = { path = "vendor/polkadot-sdk/substrate/frame/assets", default-features = false }
pallet-sudo = { path = "vendor/polkadot-sdk/substrate/frame/sudo", default-features = false }
pallet-timestamp = { path = "vendor/polkadot-sdk/substrate/frame/timestamp", default-features = false }
pallet-transaction-payment = { path = "vendor/polkadot-sdk/substrate/frame/transaction-payment", default-features = false }
pallet-transaction-payment-rpc-runtime-api = { path = "vendor/polkadot-sdk/substrate/frame/transaction-payment/rpc/runtime-api", default-features = false }

# genesis builder that allows us to interacto with runtime genesis config
sp-genesis-builder = { path = "vendor/polkadot-sdk/substrate/primitives/genesis-builder", default-features = false }

# wasm builder
substrate-wasm-builder = { version = "22.0.1" }

# nostd
parity-scale-codec = { version = "3.6.12", default-features = false, features = [
Expand Down
23 changes: 10 additions & 13 deletions poc/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,31 @@ edition = "2021"
[dependencies]
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
# this is a frame-based runtime, thus importing `frame` with runtime feature enabled.
frame = { version = "0.2.0", package = "polkadot-sdk-frame", default-features = false, features = [
"experimental",
"runtime",
] }
frame = { workspace = true, features = ["experimental", "runtime"] }

# pallets that we want to use
pallet-balances = { version = "34.0.0", default-features = false }
pallet-assets = { version = "34.0.0", default-features = false }
pallet-sudo = { version = "33.0.0", default-features = false }
pallet-timestamp = { version = "32.0.0", default-features = false }
pallet-transaction-payment = { version = "33.0.0", default-features = false }
pallet-transaction-payment-rpc-runtime-api = { version = "33.0.0", default-features = false }
pallet-balances = { workspace = true }
pallet-assets = { workspace = true }
pallet-sudo = { workspace = true }
pallet-timestamp = { workspace = true }
pallet-transaction-payment = { workspace = true }
pallet-transaction-payment-rpc-runtime-api = { workspace = true }

# genesis builder that allows us to interacto with runtime genesis config
sp-genesis-builder = { version = "0.12.0", default-features = false }
sp-genesis-builder = { workspace = true }

pvq-executor = { workspace = true }
pvq-extension = { workspace = true }
pvq-extension-core = { workspace = true }
pvq-extension-fungibles = { workspace = true }
pvq-primitives = { workspace = true }
pvq-runtime-api = { workspace = true }

[dev-dependencies]
hex = "0.4"

[build-dependencies]
substrate-wasm-builder = { version = "22.0.1", optional = true }
substrate-wasm-builder = { workspace = true, optional = true }

[features]
default = ["std"]
Expand Down
22 changes: 12 additions & 10 deletions poc/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,23 @@ use frame::{
},
prelude::*,
runtime::{
apis::{
self, impl_runtime_apis, ApplyExtrinsicResult, CheckInherentsResult, ExtrinsicInclusionMode, OpaqueMetadata,
},
apis::{self, impl_runtime_apis},
prelude::*,
},
traits::AsEnsureOriginWithArg,
};

use std::borrow::Cow;

#[runtime_version]
pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("pvq-poc"),
impl_name: create_runtime_str!("pvq-poc"),
spec_name: Cow::Borrowed("pvq-poc"),
impl_name: Cow::Borrowed("pvq-poc"),
authoring_version: 1,
spec_version: 0,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
state_version: 1,
system_version: 1,
};

/// The version information used to identify this runtime when compiled natively.
Expand Down Expand Up @@ -110,6 +109,8 @@ type RuntimeExecutive = Executive<Runtime, Block, frame_system::ChainContext<Run

use pallet_transaction_payment::{FeeDetails, RuntimeDispatchInfo};

const ONE_SECOND_IN_GAS: i64 = 100000;

impl_runtime_apis! {
impl apis::Core<Block> for Runtime {
fn version() -> RuntimeVersion {
Expand Down Expand Up @@ -225,9 +226,10 @@ impl_runtime_apis! {
}
}

impl pvq::PvqApi<Block> for Runtime {
fn execute_query(query: Vec<u8>, input: Vec<u8>) -> pvq::PvqResult {
pvq::execute_query(&query, &input)
impl pvq_runtime_api::PvqApi<Block> for Runtime {
fn execute_query(program: Vec<u8>, args: Vec<u8>, gas_limit: Option<i64>) -> pvq_primitives::PvqResult {
// Set a default gas limit of 2 seconds
pvq::execute_query(&program, &args, gas_limit.unwrap_or(ONE_SECOND_IN_GAS * 2))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if user did pass a gas limit, it should still not be greater than 2s

}
fn metadata() -> Vec<u8> {
pvq::metadata().encode()
Expand Down
15 changes: 3 additions & 12 deletions poc/runtime/src/pvq.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
#[allow(unused_imports)]
use frame::deps::scale_info::prelude::{format, string::String};
use frame::deps::sp_api::decl_runtime_apis;
use frame::prelude::*;

use pvq_extension::metadata::Metadata;
pub use pvq_primitives::PvqResult;

use pvq_extension::{extensions_impl, ExtensionsExecutor, InvokeSource};
decl_runtime_apis! {
pub trait PvqApi {
fn execute_query(query: Vec<u8>, input: Vec<u8>) -> PvqResult;
fn metadata() -> Vec<u8>;
}
}

#[extensions_impl]
pub mod extensions {
Expand Down Expand Up @@ -42,9 +32,10 @@ pub mod extensions {
}
}

pub fn execute_query(query: &[u8], input: &[u8]) -> PvqResult {
pub fn execute_query(program: &[u8], args: &[u8], gas_limit: i64) -> pvq_primitives::PvqResult {
let mut executor = ExtensionsExecutor::<extensions::Extensions, ()>::new(InvokeSource::RuntimeAPI);
executor.execute_method(query, input, 0)
let (result, _) = executor.execute(program, args, Some(gas_limit));
result
}

pub fn metadata() -> Metadata {
Expand Down
8 changes: 8 additions & 0 deletions pvq-executor/src/context.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use polkavm::Linker;

pub trait PvqExecutorContext {
type UserData;
type UserError;
fn register_host_functions(&mut self, linker: &mut Linker<Self::UserData, Self::UserError>);
fn data(&mut self) -> &mut Self::UserData;
}
50 changes: 50 additions & 0 deletions pvq-executor/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use pvq_primitives::PvqError;
#[derive(Debug)]
pub enum PvqExecutorError<UserError> {
InvalidProgramFormat,
MemoryAccessError(polkavm::MemoryAccessError),
// Extract from the PVM CallError
Trap,
// Extract from the PVM CallError
NotEnoughGas,
// Usually a custom error type from the extension system definition
User(UserError),
// Other errors directly from the PVM
OtherPvmError(polkavm::Error),
}

impl<UserError> From<polkavm::MemoryAccessError> for PvqExecutorError<UserError> {
fn from(err: polkavm::MemoryAccessError) -> Self {
Self::MemoryAccessError(err)
}
}

impl<UserError> From<polkavm::Error> for PvqExecutorError<UserError> {
fn from(err: polkavm::Error) -> Self {
Self::OtherPvmError(err)
}
}

impl<UserError> From<polkavm::CallError<UserError>> for PvqExecutorError<UserError> {
fn from(err: polkavm::CallError<UserError>) -> Self {
match err {
polkavm::CallError::Trap => Self::Trap,
polkavm::CallError::NotEnoughGas => Self::NotEnoughGas,
polkavm::CallError::Error(e) => Self::OtherPvmError(e),
polkavm::CallError::User(e) => Self::User(e),
}
}
}

impl<UserError> From<PvqExecutorError<UserError>> for PvqError {
fn from(e: PvqExecutorError<UserError>) -> PvqError {
match e {
PvqExecutorError::InvalidProgramFormat => PvqError::InvalidPvqProgramFormat,
PvqExecutorError::MemoryAccessError(_) => PvqError::MemoryAccessError,
PvqExecutorError::Trap => PvqError::Trap,
PvqExecutorError::NotEnoughGas => PvqError::QueryExceedsWeightLimit,
PvqExecutorError::User(_) => PvqError::HostCallError,
PvqExecutorError::OtherPvmError(_) => PvqError::Other,
}
}
}
91 changes: 91 additions & 0 deletions pvq-executor/src/executor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
use alloc::vec::Vec;
use polkavm::{Config, Engine, Linker, Module, ModuleConfig, ProgramBlob};

use crate::context::PvqExecutorContext;
use crate::error::PvqExecutorError;

type PvqExecutorResult<UserError> = Result<Vec<u8>, PvqExecutorError<UserError>>;
type GasLimit = Option<i64>;

pub struct PvqExecutor<Ctx: PvqExecutorContext> {
engine: Engine,
linker: Linker<Ctx::UserData, Ctx::UserError>,
context: Ctx,
}

impl<Ctx: PvqExecutorContext> PvqExecutor<Ctx> {
pub fn new(config: Config, mut context: Ctx) -> Self {
let engine = Engine::new(&config).unwrap();
let mut linker = Linker::<Ctx::UserData, Ctx::UserError>::new();
// Register user-defined host functions
context.register_host_functions(&mut linker);
Self {
engine,
linker,
context,
}
}

pub fn execute(
&mut self,
program: &[u8],
args: &[u8],
gas_limit: GasLimit,
) -> (PvqExecutorResult<Ctx::UserError>, GasLimit) {
let blob = match ProgramBlob::parse(program.into()) {
Ok(blob) => blob,
Err(_) => return (Err(PvqExecutorError::InvalidProgramFormat), gas_limit),
};

let mut module_config = ModuleConfig::new();
module_config.set_aux_data_size(args.len() as u32);
if gas_limit.is_some() {
module_config.set_gas_metering(Some(polkavm::GasMeteringKind::Sync));
}

let module = match Module::from_blob(&self.engine, &module_config, blob) {
Ok(module) => module,
Err(err) => return (Err(err.into()), gas_limit),
};

let instance_pre = match self.linker.instantiate_pre(&module) {
Ok(instance_pre) => instance_pre,
Err(err) => return (Err(err.into()), gas_limit),
};

let mut instance = match instance_pre.instantiate() {
Ok(instance) => instance,
Err(err) => return (Err(err.into()), gas_limit),
};

if let Some(gas_limit) = gas_limit {
instance.set_gas(gas_limit);
}

// From this point on, we include instance.gas() in the return value
let result = (|| {
instance.write_memory(module.memory_map().aux_data_address(), args)?;

tracing::info!("Calling entrypoint with args: {:?}", args);
let res = instance.call_typed_and_get_result::<u64, (u32, u32)>(
self.context.data(),
"pvq",
(module.memory_map().aux_data_address(), args.len() as u32),
)?;

let res_size = (res >> 32) as u32;
let res_ptr = (res & 0xffffffff) as u32;

let result = instance.read_memory(res_ptr, res_size)?;

tracing::info!("Result: {:?}", result);
Ok(result)
})();

if gas_limit.is_some() {
(result, Some(instance.gas()))
} else {
(result, None)
}
}
}
88 changes: 6 additions & 82 deletions pvq-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,88 +3,12 @@
extern crate alloc;

pub use alloc::vec::Vec;
use polkavm::ModuleConfig;
pub use polkavm::{Caller, Config, Engine, Linker, Module, ProgramBlob};

pub trait PvqExecutorContext {
type UserData;
type UserError;
fn register_host_functions(&mut self, linker: &mut Linker<Self::UserData, Self::UserError>);
fn data(&mut self) -> &mut Self::UserData;
}
mod context;
mod error;
mod executor;

pub struct PvqExecutor<Ctx: PvqExecutorContext> {
engine: Engine,
linker: Linker<Ctx::UserData, Ctx::UserError>,
context: Ctx,
}

#[derive(Debug)]
pub enum PvqExecutorError<UserError> {
MemoryAllocationError,
MemoryAccessError(polkavm::MemoryAccessError),
CallError(polkavm::CallError<UserError>),
OtherPVMError(polkavm::Error),
}

impl<UserError> From<polkavm::MemoryAccessError> for PvqExecutorError<UserError> {
fn from(err: polkavm::MemoryAccessError) -> Self {
Self::MemoryAccessError(err)
}
}

impl<UserError> From<polkavm::Error> for PvqExecutorError<UserError> {
fn from(err: polkavm::Error) -> Self {
Self::OtherPVMError(err)
}
}

impl<UserError> From<polkavm::CallError<UserError>> for PvqExecutorError<UserError> {
fn from(err: polkavm::CallError<UserError>) -> Self {
Self::CallError(err)
}
}

impl<Ctx: PvqExecutorContext> PvqExecutor<Ctx> {
pub fn new(config: Config, mut context: Ctx) -> Self {
let engine = Engine::new(&config).unwrap();
let mut linker = Linker::<Ctx::UserData, Ctx::UserError>::new();
// Register user-defined host functions
context.register_host_functions(&mut linker);
Self {
engine,
linker,
context,
}
}

pub fn execute(
&mut self,
program: &[u8],
args: &[u8],
_gas_limit: u64,
) -> Result<Vec<u8>, PvqExecutorError<Ctx::UserError>> {
let blob = ProgramBlob::parse(program.into()).map_err(polkavm::Error::from)?;

// TODO: make this configurable
let mut module_config = ModuleConfig::new();
module_config.set_aux_data_size(args.len() as u32);

let module = Module::from_blob(&self.engine, &module_config, blob)?;
let instance_pre = self.linker.instantiate_pre(&module)?;
let mut instance = instance_pre.instantiate()?;

instance.write_memory(module.memory_map().aux_data_address(), args)?;
tracing::info!("Calling entrypoint with args: {:?}", args);
let res = instance.call_typed_and_get_result::<u64, (u32, u32)>(
self.context.data(),
"pvq",
(module.memory_map().aux_data_address(), args.len() as u32),
)?;
let res_size = (res >> 32) as u32;
let res_ptr = (res & 0xffffffff) as u32;
let result = instance.read_memory(res_ptr, res_size)?;
tracing::info!("Result: {:?}", result);
Ok(result)
}
}
pub use context::PvqExecutorContext;
pub use error::PvqExecutorError;
pub use executor::PvqExecutor;
Loading
Loading