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 1 commit
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
14 changes: 2 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,9 @@ 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);
let (result, _) = executor.execute_method(query, input, None);
let (result, _) = executor.execute(program, args, Some(gas_limit));
result
}

Expand Down
1 change: 0 additions & 1 deletion pvq-executor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl<Ctx: PvqExecutorContext> PvqExecutor<Ctx> {
Err(_) => return (Err(PvqExecutorError::InvalidProgramFormat), gas_limit),
};

// TODO: make this configurable
let mut module_config = ModuleConfig::new();
module_config.set_aux_data_size(args.len() as u32);
if gas_limit.is_some() {
Expand Down
3 changes: 1 addition & 2 deletions pvq-extension/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ impl<C: CallDataTuple, P: PermissionController> ExtensionsExecutor<C, P> {
Self { executor }
}

/// Execute a method on an extension
///
/// # Arguments
///
Expand All @@ -37,7 +36,7 @@ impl<C: CallDataTuple, P: PermissionController> ExtensionsExecutor<C, P> {
/// # Returns
///
/// The result of the execution or an error
pub fn execute_method(&mut self, program: &[u8], args: &[u8], gas_limit: Option<i64>) -> (PvqResult, Option<i64>) {
pub fn execute(&mut self, program: &[u8], args: &[u8], gas_limit: Option<i64>) -> (PvqResult, Option<i64>) {
let (result, gas_remaining) = self.executor.execute(program, args, gas_limit);
(result.map_err(PvqError::from), gas_remaining)
}
Expand Down
4 changes: 2 additions & 2 deletions pvq-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use alloc::vec::Vec;
use parity_scale_codec::{Decode, Encode};
use scale_info::TypeInfo;

pub type PvqResult = Result<PvqResponse, PvqError>;

pub type PvqResponse = Vec<u8>;

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, TypeInfo)]
Expand All @@ -18,5 +20,3 @@ pub enum PvqError {
HostCallError,
Other,
}

pub type PvqResult = Result<PvqResponse, PvqError>;
7 changes: 4 additions & 3 deletions pvq-runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use pvq_primitives::PvqResult;
use sp_api::decl_runtime_apis;

// The runtime API for the PVQ module.
// query: the query to be executed, written in polkavm. i.e. query balance of given accounts and sum them up
// input: the input data for the query. i.e. accounts to be queried
// - `program`: PVQ binary.
// - `args`: Query arguments that is SCALE-encoded.
// - `gas_limit`: Optional gas limit for query execution. When set to `None`, execution is constrained by the default time boundary.
decl_runtime_apis! {
pub trait PvqApi {
fn execute_query(query: Vec<u8>, input: Vec<u8>) -> PvqResult;
fn execute_query(program: Vec<u8>, args: Vec<u8>, gas_limit: Option<i64>) -> PvqResult;
fn metadata() -> Vec<u8>;
}
}
8 changes: 2 additions & 6 deletions pvq-test-runner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,8 @@ impl TestRunner {
input_data
}

pub fn execute_program(
&mut self,
program_blob: &[u8],
input_data: &[u8],
) -> Result<Vec<u8>, pvq_primitives::PvqError> {
let (result, _) = self.executor.execute_method(program_blob, input_data, None);
pub fn execute_program(&mut self, program_blob: &[u8], input_data: &[u8]) -> pvq_primitives::PvqResult {
let (result, _) = self.executor.execute(program_blob, input_data, None);
result
}
}
Expand Down
Loading