Skip to content

feat: add support for program metadata generation #69

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 10 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ jobs:
- name: Check no-std
run: make check-wasm

- name: Install pvq-program-metadata-gen
run: cargo install --path pvq-program-metadata-gen

- name: Clippy
run: make clippy

Expand Down Expand Up @@ -65,6 +68,9 @@ jobs:
- name: Install polkatool
run: make polkatool

- name: Install pvq-program-metadata-gen
run: cargo install --path pvq-program-metadata-gen

- name: Build guests
run: make guests

21 changes: 20 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ members = [
"poc/runtime",

"pvq-program",
"pvq-program-metadata-gen",
"pvq-executor",
"pvq-extension-core",
"pvq-extension-fungibles",
Expand All @@ -32,6 +33,7 @@ opt-level = 3
[workspace.dependencies]
# local
pvq-program = { path = "pvq-program", default-features = false }
pvq-program-metadata-gen = { path = "pvq-program-metadata-gen" }
pvq-executor = { path = "pvq-executor", default-features = false }
pvq-extension-core = { path = "pvq-extension-core", default-features = false }
pvq-extension-fungibles = { path = "pvq-extension-fungibles", default-features = false }
Expand Down Expand Up @@ -68,13 +70,18 @@ parity-scale-codec = { version = "3.6.12", default-features = false, features =
] }
scale-info = { version = "2.11.3", default-features = false, features = [
"derive",
"serde",
] }
tracing = { version = "0.1.40", default-features = false }
serde = { version = "1.0.215", default-features = false, features = ["derive"] }
serde_json = { version = "1.0.110", default-features = false }

# std
clap = { version = "4.5.4", features = ["derive"] }
env_logger = { version = "0.11.3" }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }
tempfile = { version = "3.9.0" }
toml = { version = "0.8", features = ["preserve_order"] }

fortuples = "0.9"

Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ tools: polkatool chain-spec-builder
polkatool:
cargo install --path vendor/polkavm/tools/polkatool

pvq-program-metadata-gen:
cargo install --path pvq-program-metadata-gen

chain-spec-builder:
cargo install --path vendor/polkadot-sdk/substrate/bin/utils/chain-spec-builder

Expand All @@ -37,7 +40,7 @@ check: check-wasm
SKIP_WASM_BUILD= cargo check
cd pvq-program/examples; cargo check

clippy:
clippy: pvq-program-metadata-gen
SKIP_WASM_BUILD= cargo clippy -- -D warnings
cd guest-examples; cargo clippy --all

Expand Down
75 changes: 75 additions & 0 deletions guest-examples/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions guest-examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ parity-scale-codec = { version = "3", default-features = false, features = [
"derive",
] }
pvq-program = { path = "../pvq-program", default-features = false }
pvq-program-metadata-gen = { path = "../pvq-program-metadata-gen" }
polkavm-derive = { path = "../vendor/polkavm/crates/polkavm-derive" }
cfg-if = "1.0"
2 changes: 1 addition & 1 deletion guest-examples/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2024-11-01"
channel = "nightly-2024-11-19"
components = ["rust-src"]
5 changes: 5 additions & 0 deletions guest-examples/sum-balance/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ publish = false
parity-scale-codec = { workspace = true }
polkavm-derive = { workspace = true }
pvq-program = { workspace = true }
cfg-if = { workspace = true }

[features]
option_version_1 = []
option_version_2 = []
25 changes: 25 additions & 0 deletions guest-examples/sum-balance/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use std::env;
use std::path::PathBuf;
use std::process::Command;

fn main() {
// Tell Cargo to rerun this build script if the source file changes
println!("cargo:rerun-if-changed=src/main.rs");
let current_dir = env::current_dir().expect("Failed to get current directory");
// Determine the output directory for the metadata
let output_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR is not set"));

// Build and run the command
let status = Command::new("pvq-program-metadata-gen")
.arg("--crate-path")
.arg(&current_dir)
.arg("--output-dir")
.arg(&output_dir)
.env("RUST_LOG", "info")
.status()
.expect("Failed to execute pvq-program-metadata-gen");

if !status.success() {
panic!("Failed to generate program metadata");
}
}
19 changes: 16 additions & 3 deletions guest-examples/sum-balance/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@

#[pvq_program::program]
mod sum_balance {
type AccountId = [u8; 32];
type AssetId = u32;
type Balance = u64;

cfg_if::cfg_if! {
if #[cfg(feature = "option_version_1")] {
type AccountId = [u8; 64];
type AssetId = u64;
type Balance = u128;
} else if #[cfg(feature = "option_version_2")] {
type AccountId = [u8; 32];
type AssetId = u32;
type Balance = u64;
} else {
type AccountId = [u8; 32];
type AssetId = u32;
type Balance = u64;
}
}

#[program::extension_fn(extension_id = 4071833530116166512u64, fn_index = 1)]
fn balance(asset: AssetId, who: AccountId) -> Balance {}
Expand Down
2 changes: 2 additions & 0 deletions pvq-extension/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pvq-extension-procedural = { path = "procedural" }
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
pvq-primitives = { workspace = true }
serde = { workspace = true }

[dev-dependencies]
tracing-subscriber = { workspace = true }
Expand All @@ -29,4 +30,5 @@ std = [
"parity-scale-codec/std",
"scale-info/std",
"tracing/std",
"serde/std",
]
20 changes: 7 additions & 13 deletions pvq-extension/procedural/src/extensions_impl/expand/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use quote::quote;
/// generate the `metadata` function in the #[extensions_impl] module
pub fn expand_metadata(def: &Def) -> TokenStream2 {
let pvq_extension = &def.pvq_extension;
let scale_info = &def.scale_info;
let mut extension_id_call_list = Vec::new();
let mut extension_metadata_call_list = Vec::new();

for impl_ in &def.extension_impls {
Expand All @@ -14,30 +16,22 @@ pub fn expand_metadata(def: &Def) -> TokenStream2 {
// Replace trait_path with a call to the metadata function with the impl struct as generic parameter
let impl_struct_ident = &def.impl_struct.ident;

let extension_id_call = quote!(
#trait_path extension_id()
);
// Create a method call expression instead of a path
let method_call = quote!(
#trait_path metadata::<#impl_struct_ident>()
);

extension_id_call_list.push(extension_id_call);
extension_metadata_call_list.push(method_call);
}

// let query_metadata_by_extension_id = quote! {
// impl #pvq_extension::ExtensionImplMetadata for #extension_impl_name {
// fn extension_metadata(extension_id: #pvq_extension::ExtensionIdTy) -> #pvq_extension::metadata::ExtensionMetadata {
// let extension_metadata = match extension_id {
// #(#extension_ids => #extension_metadata_list,)*
// _ => panic!("Unknown extension id"),
// };
// extension_metadata
// }
// }
// };

let metadata = quote! {
pub fn metadata() -> #pvq_extension::metadata::Metadata {
#pvq_extension::metadata::Metadata::new(
scale_info::prelude::vec![ #( #extension_metadata_call_list, )* ],
#scale_info::prelude::collections::BTreeMap::from([ #( (#extension_id_call_list, #extension_metadata_call_list), )* ]),
)
}
};
Expand Down
3 changes: 3 additions & 0 deletions pvq-extension/procedural/src/extensions_impl/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ pub struct Def {
pub impl_struct: impl_struct::ImplStruct,
pub extension_impls: Vec<extension::ExtensionImpl>,
pub pvq_extension: syn::Path,
pub scale_info: syn::Path,
}

impl Def {
pub fn try_from(mut item: syn::ItemMod) -> syn::Result<Self> {
let pvq_extension = generate_crate_access("pvq-extension")?;
let scale_info = generate_crate_access("scale-info")?;
let item_span = item.span();
let items = &mut item
.content
Expand Down Expand Up @@ -58,6 +60,7 @@ impl Def {
.ok_or_else(|| syn::Error::new(item_span, "Missing `#[extensions_impl::impl_struct]`"))?,
extension_impls,
pvq_extension,
scale_info,
})
}
}
Expand Down
Loading
Loading