Skip to content

Commit

Permalink
Feat(metadata): basic metadata api support (#38)
Browse files Browse the repository at this point in the history
* Modify xcq-types
* add metadata api
  • Loading branch information
indirection42 authored Aug 1, 2024
1 parent 2350545 commit 38a9804
Show file tree
Hide file tree
Showing 38 changed files with 1,133 additions and 571 deletions.
20 changes: 8 additions & 12 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ clap = { version = "4.5.4", features = ["derive"] }
env_logger = { version = "0.11.3" }
tracing-subscriber = { version = "0.3.18", features = ["env-filter"] }

impl-trait-for-tuples = "0.2"
fortuples = "0.9"

# proc macros
syn = { version = "2", features = ["full", "extra-traits"] }
syn = { version = "2", features = ["full", "visit-mut", "extra-traits"] }
quote = "1"
proc-macro2 = "1"
proc-macro-crate = "3"
Expand Down
50 changes: 2 additions & 48 deletions poc/guests/Cargo.lock

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

1 change: 0 additions & 1 deletion poc/guests/query-balance-fungibles/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,4 @@ edition = "2021"
publish = false

[dependencies]
parity-scale-codec = { version = "3.6.12", default-features = false }
polkavm-derive = { workspace = true }
11 changes: 6 additions & 5 deletions poc/guests/query-balance-fungibles/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ extern "C" {
#[polkavm_derive::polkavm_export]
extern "C" fn main(ptr: u32, size: u32) -> u64 {
// no variant for this input, since the return type is same for total_supply/balance
let num_query = unsafe { core::ptr::read_volatile(ptr as *const u8) };
let query_size = (size - 1) / num_query as u32;
let extension_id = unsafe { core::ptr::read_volatile(ptr as *const u64) };
let num_query = unsafe { core::ptr::read_volatile((ptr + 8) as *const u8) };
let query_size = (size - 9) / num_query as u32;
let mut sum = 0u64;
// in this settings, the return type is same for total_supply/balance
// otherwise, we need to recognize return type through input data
for i in 0..num_query {
let res = unsafe { call(1, ptr + 1 + query_size * i as u32, query_size) };
let res_ptr = (res >> 32) as *const u8;
let res_len = (res & 0xffffffff) as u32;
let res = unsafe { call(extension_id, ptr + 9 + query_size * i as u32, query_size) };
let res_len = (res >> 32) as u32;
let res_ptr = (res & 0xffffffff) as *const u8;
let res_bytes = unsafe { core::slice::from_raw_parts(res_ptr, res_len as usize) };
sum += u64::from_le_bytes(res_bytes.try_into().unwrap());
}
Expand Down
1 change: 1 addition & 0 deletions poc/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ xcq-executor = { workspace = true }
xcq-extension = { workspace = true }
xcq-extension-core = { workspace = true }
xcq-extension-fungibles = { workspace = true }
xcq-primitives = { workspace = true }

[dev-dependencies]
hex = "0.4"
Expand Down
3 changes: 3 additions & 0 deletions poc/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@ impl_runtime_apis! {
fn execute_query(query: Vec<u8>, input: Vec<u8>) -> xcq::XcqResult {
xcq::execute_query(query, input)
}
fn metadata() -> Vec<u8> {
xcq::metadata().encode()
}
}
}

Expand Down
61 changes: 32 additions & 29 deletions poc/runtime/src/xcq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ pub type XcqResponse = Vec<u8>;
pub type XcqError = String;
pub type XcqResult = Result<XcqResponse, XcqError>;

use xcq_extension::{ExtensionsExecutor, Guest, Input, InvokeSource, Method};
use xcq_extension::{impl_extensions, ExtensionsExecutor, Guest, Input, InvokeSource, Method};
use xcq_primitives::metadata::Metadata;
decl_runtime_apis! {
pub trait XcqApi {
fn execute_query(query: Vec<u8>, input: Vec<u8>) -> XcqResult;
fn metadata() -> Vec<u8>;
}
}

Expand All @@ -21,38 +23,34 @@ impl xcq_extension_core::Config for ExtensionImpl {
type ExtensionId = u64;
}

impl xcq_extension_core::ExtensionCore for ExtensionImpl {
type Config = ExtensionImpl;
fn has_extension(id: <Self::Config as xcq_extension_core::Config>::ExtensionId) -> bool {
matches!(id, 0 | 1)
}
}

// extension_fungibles impls
impl xcq_extension_fungibles::Config for ExtensionImpl {
type AccountId = crate::interface::AccountId;
type AccountId = [u8; 32];
type Balance = crate::interface::Balance;
type AssetId = crate::interface::AssetId;
}

impl xcq_extension_fungibles::ExtensionFungibles for ExtensionImpl {
type Config = ExtensionImpl;
fn balance(
asset: xcq_extension_fungibles::AssetIdFor<Self>,
who: xcq_extension_fungibles::AccountIdFor<Self>,
) -> xcq_extension_fungibles::BalanceFor<Self> {
crate::Assets::balance(asset, who)
impl_extensions! {
impl xcq_extension_core::ExtensionCore for ExtensionImpl {
type Config = ExtensionImpl;
fn has_extension(id: <Self::Config as xcq_extension_core::Config>::ExtensionId) -> bool {
matches!(id, xcq_extension_core::EXTENSION_ID | xcq_extension_fungibles::EXTENSION_ID)
}
}
fn total_supply(asset: xcq_extension_fungibles::AssetIdFor<Self>) -> xcq_extension_fungibles::BalanceFor<Self> {
crate::Assets::total_supply(asset)

impl xcq_extension_fungibles::ExtensionFungibles for ExtensionImpl {
type Config = ExtensionImpl;
fn balance(
asset: <Self::Config as xcq_extension_fungibles::Config>::AssetId,
who: <Self::Config as xcq_extension_fungibles::Config>::AccountId,
) -> <Self::Config as xcq_extension_fungibles::Config>::Balance {
crate::Assets::balance(asset, crate::interface::AccountId::from(who))
}
fn total_supply(asset: <Self::Config as xcq_extension_fungibles::Config>::AssetId) -> <Self::Config as xcq_extension_fungibles::Config>::Balance {
crate::Assets::total_supply(asset)
}
}
}

type Extensions = (
xcq_extension_core::Call<ExtensionImpl>,
xcq_extension_fungibles::Call<ExtensionImpl>,
);

// guest impls
pub struct GuestImpl {
pub program: Vec<u8>,
Expand Down Expand Up @@ -87,6 +85,10 @@ pub fn execute_query(query: Vec<u8>, input: Vec<u8>) -> XcqResult {
executor.execute_method(guest, input)
}

pub fn metadata() -> Metadata {
ExtensionImpl::runtime_metadata().into()
}

#[cfg(test)]
mod tests {

Expand All @@ -97,8 +99,8 @@ mod tests {

#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug)]
enum FungiblesMethod {
Balance { asset: AssetId, who: AccountId },
TotalSupply { asset: AssetId },
Balance { asset: AssetId, who: AccountId },
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug)]
Expand All @@ -109,7 +111,7 @@ mod tests {
fn call_transparent_data_hex() {
let raw_blob = include_bytes!("../../../output/poc-guest-transparent-call.polkavm");
// call fungible extension
let mut data = 1u64.encode();
let mut data = xcq_extension_fungibles::EXTENSION_ID.encode();
let method = FungiblesMethod::TotalSupply { asset: 21 };
data.extend_from_slice(&method.encode());
dbg!(hex::encode((raw_blob.to_vec(), data).encode()));
Expand All @@ -123,14 +125,15 @@ mod tests {
.public();
let alice_account = AccountId::from(alice_public);
// query num
let mut data = vec![2u8];
let mut data = xcq_extension_fungibles::EXTENSION_ID.encode();
data.extend_from_slice(&vec![2u8]);
let method1 = FungiblesMethod::Balance {
asset: 21,
who: alice_account.clone(),
who: alice_account.clone().into(),
};
let method2 = FungiblesMethod::Balance {
asset: 1984,
who: alice_account,
who: alice_account.into(),
};
data.extend_from_slice(&method1.encode());
data.extend_from_slice(&method2.encode());
Expand Down
1 change: 1 addition & 0 deletions xcq-extension-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ version.workspace = true
parity-scale-codec = { workspace = true }
xcq-extension = { workspace = true }
scale-info = { workspace = true }
xcq-primitives = { workspace = true }

[features]
default = ["std"]
Expand Down
31 changes: 16 additions & 15 deletions xcq-extension-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#![cfg_attr(not(feature = "std"), no_std)]
use parity_scale_codec::{Decode, Encode};
use scale_info::prelude::vec::Vec;
use xcq_extension::extension;
use parity_scale_codec::Encode;
use xcq_extension::decl_extensions;

#[extension]
pub trait ExtensionCore {
type Config: Config;
fn has_extension(id: <Self::Config as Config>::ExtensionId) -> bool;
// crypto functions
// fn blake2_64(data: Vec<u8>) -> [u8; 8];
// fn blake2_128(data: Vec<u8>) -> [u8; 16];
// fn blake2_256(data: Vec<u8>) -> [u8; 32];
// fn twox_64(data: Vec<u8>) -> [u8; 8];
// fn read_storage(key: Vec<u8>) -> Option<Vec<u8>>;
}
pub trait Config {
type ExtensionId: Decode;
type ExtensionId: Encode;
}

decl_extensions! {
pub trait ExtensionCore {
type Config: Config;
fn has_extension(id: <Self::Config as Config>::ExtensionId) -> bool;
// crypto functions
// fn blake2_64(data: Vec<u8>) -> [u8; 8];
// fn blake2_128(data: Vec<u8>) -> [u8; 16];
// fn blake2_256(data: Vec<u8>) -> [u8; 32];
// fn twox_64(data: Vec<u8>) -> [u8; 8];
// fn read_storage(key: Vec<u8>) -> Option<Vec<u8>>;
}
}
1 change: 1 addition & 0 deletions xcq-extension-fungibles/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ version.workspace = true
parity-scale-codec = { workspace = true }
xcq-extension = { workspace = true }
scale-info = { workspace = true }
xcq-primitives = { workspace = true }

[features]
default = ["std"]
Expand Down
Loading

0 comments on commit 38a9804

Please sign in to comment.