Skip to content
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

Working runner with gas metering and compiled pvm-prog-calc #26

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ members = [
"runtime",
"node",
"pallets/qf-polkavm-dev",
"qf-test-runner"
"qf-test-runner",
"pvm-test-runner",
]
exclude = [
"old",
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
GUEST_RUST_FLAGS="-C relocation-model=pie -C link-arg=--emit-relocs -C link-arg=--unique --remap-path-prefix=$(pwd)= --remap-path-prefix=$HOME=~"

vendor-clone:
git clone --depth=1 --branch v0.18.0 https://github.com/paritytech/polkavm.git vendor/polkavm
git clone --depth=1 --branch v0.19.0 https://github.com/paritytech/polkavm.git vendor/polkavm
git clone --depth=1 https://github.com/QuantumFusion-network/polkadot-sdk vendor/polkadot-sdk

tools: polkatool chain-spec-builder
Expand Down
15 changes: 15 additions & 0 deletions pvm-test-runner/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[package]
name = "pvm-test-runner"
description = "PolkaVM program runner. Only for testing purpose."
authors.workspace = true
edition.workspace = true
license.workspace = true
repository.workspace = true
version = "0.1.0"

[dependencies]
clap = { workspace = true, features = ["derive"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }

polkavm = { workspace = true }
142 changes: 142 additions & 0 deletions pvm-test-runner/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
use clap::Parser;
use tracing_subscriber::prelude::*;

extern crate alloc;

use polkavm::{
Config as PolkaVMConfig,
Engine, GasMeteringKind, InterruptKind, Module, ModuleConfig,
ProgramBlob
};

// For debugging purposes
macro_rules! match_interrupt {
($interrupt:expr, $pattern:pat) => {
let i = $interrupt;
assert!(
matches!(i, $pattern),
"unexpected interrupt: {i:?}, expected: {:?}",
stringify!($pattern)
);
};
}

#[derive(Parser, Debug)]
#[command(version, about)]
struct Cli {
/// Path to the PolkaVM program to execute
#[arg(short, long)]
program: std::path::PathBuf,
/// Entry point (default "add_numbers")
#[arg(short, long, default_value="add_numbers")]
entry_name: String,
/// "a" param (default 1)
#[arg(short, long, default_value="1")]
a: u32,
/// "b" param (default 2)
#[arg(short, long, default_value="2")]
b: u32,
/// Available Gas
#[arg(short, long, default_value="100")]
gas: u32,
}

fn get_native_page_size() -> usize {
4096
}

fn main() {
let registry = tracing_subscriber::registry();

let filter = tracing_subscriber::EnvFilter::builder()
.with_default_directive(tracing::Level::INFO.into())
.from_env_lossy();

registry
.with(tracing_subscriber::fmt::layer().with_filter(filter))
.try_init()
.expect("Failed to initialize tracing");

let cli = Cli::parse();

println!("Program: {:?}", cli.program);
let raw_blob = std::fs::read(cli.program).expect("Failed to read program");
let config = PolkaVMConfig::from_env()
.map_err(|e| {
tracing::debug!("Failed to initialize PolkaVM Config: {}", e);
e
})
.expect("Failed to initialize PolkaVM Config");
let engine = Engine::new(&config)
.map_err(|e| {
tracing::debug!("Failed to initialize PolkaVM Engine: {}", e);
e
})
.expect("Failed to initialize PolkaVM Engine");
let page_size = get_native_page_size() as u32;
let blob = ProgramBlob::parse(raw_blob.into())
.map_err(|e| {
tracing::debug!("Failed to parse a blob: {}", e);
e
})
.expect("Failed to parse a blob");

let mut module_config = ModuleConfig::new();
module_config.set_page_size(page_size);
module_config.set_gas_metering(Some(GasMeteringKind::Sync));
let module = Module::from_blob(&engine, &module_config, blob)
.map_err(|e| {
tracing::debug!("Failed to initialize PolkaVM Module: {}", e);
e
})
.expect("Failed to initialize PolkaVM Module");

let mut instance = module.instantiate()
.map_err(|e| {
tracing::debug!("Failed to initialize PolkaVM Instance: {}", e);
e
})
.expect("Failed to initialize PolkaVM Instance");

instance.set_gas(cli.gas.into());
println!("Gas available: {}", instance.gas());

let entry: &str = cli.entry_name.as_str();

let entry_point = module.exports().find(|export| export.symbol() == entry).expect("Entry point not found");

let pc = entry_point.program_counter();

let args = (cli.a, cli.b);

println!("Entry {} with args: {:?}", entry, args);

instance.prepare_call_typed(pc, args);
let gas_cost = module.calculate_gas_cost_for(pc).unwrap();
println!("gas_cost: {}", gas_cost);

loop {
let run_result = instance.run()
.map_err(|e| {
tracing::debug!("Failed to run an Instance: {}", e);
e
})
.expect("Failed to run an Instance");
match run_result {
InterruptKind::Step => {
println!("Step pc={:?}", instance.program_counter().unwrap());
},
InterruptKind::Finished => {
println!("Finished");
break;
}
_ => {
println!("Unexpected interrupt: {:?}", run_result);
break;
},
}
}
let res = instance.get_result_typed::<u32>();
println!("Gas left: {}", instance.gas());
println!("Result: {}", res);
}