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

feat: migrate poc/* to xcq-* #31

Merged
merged 1 commit into from
Jun 24, 2024
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
17 changes: 17 additions & 0 deletions Cargo.lock

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

16 changes: 10 additions & 6 deletions xcq-executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
extern crate alloc;

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

pub trait XcqExecutorContext {
fn register_host_functions<T>(&mut self, linker: &mut Linker<T>);
Expand Down Expand Up @@ -52,7 +52,12 @@ impl<Ctx: XcqExecutorContext> XcqExecutor<Ctx> {
}
}

pub fn execute(&mut self, raw_blob: &[u8], input: &[u8]) -> Result<Vec<u8>, XcqExecutorError> {
pub fn execute(
&mut self,
raw_blob: &[u8],
method: impl AsRef<[u8]>,
input: &[u8],
) -> Result<Vec<u8>, XcqExecutorError> {
let blob = ProgramBlob::parse(raw_blob.into())?;
let module = Module::from_blob(&self.engine, &Default::default(), blob)?;
let instance_pre = self.linker.instantiate_pre(&module)?;
Expand All @@ -71,12 +76,11 @@ impl<Ctx: XcqExecutorContext> XcqExecutor<Ctx> {
0
};

// return value is u64 instead of (u32, u32) due to https://github.com/koute/polkavm/issues/116
let res = instance.call_typed::<(u32,), u64>(&mut self.context, "main", (input_ptr,))?;
let res = instance.call_typed::<(u32, u32), u64>(&mut self.context, method, (input_ptr, input.len() as u32))?;
let res_ptr = (res >> 32) as u32;
let res_len = (res & 0xffffffff) as u32;
let res_size = (res & 0xffffffff) as u32;
let result = instance
.read_memory_into_vec(res_ptr, res_len)
.read_memory_into_vec(res_ptr, res_size)
.map_err(|e| XcqExecutorError::ExecutionError(polkavm::ExecutionError::Trap(e)))?;
Ok(result)
}
Expand Down
4 changes: 3 additions & 1 deletion xcq-extension-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ repository.workspace = true
version.workspace = true

[dependencies]
parity-scale-codec = { workspace = true }
xcq-extension = { workspace = true }

[features]
default = ["std"]
std = []
std = ["parity-scale-codec/std", "xcq-extension/std"]
49 changes: 40 additions & 9 deletions xcq-extension-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,47 @@
#![cfg_attr(not(feature = "std"), no_std)]
use parity_scale_codec::{Decode, Encode};
use xcq_extension::Vec;
use xcq_extension::{DispatchError, Dispatchable};
use xcq_extension::{ExtensionId, ExtensionIdTy};

pub fn add(left: usize, right: usize) -> usize {
left + right
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;
}

// #[extension(ExtensionCore)]
// type Call;

#[cfg(test)]
mod tests {
mod generated_by_extension_decl {
use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
type ExtensionIdOf<T> = <<T as ExtensionCore>::Config as Config>::ExtensionId;
#[derive(Decode)]
pub enum ExtensionCoreCall<Impl: ExtensionCore> {
HasExtension { id: ExtensionIdOf<Impl> },
}

impl<Impl: ExtensionCore> Dispatchable for ExtensionCoreCall<Impl> {
fn dispatch(self) -> Result<Vec<u8>, DispatchError> {
match self {
Self::HasExtension { id } => Ok(Impl::has_extension(id).encode()),
}
}
}

impl<Impl: ExtensionCore> ExtensionId for ExtensionCoreCall<Impl> {
const EXTENSION_ID: ExtensionIdTy = 0u64;
}

pub type Call<Impl> = ExtensionCoreCall<Impl>;
}

pub use generated_by_extension_decl::*;
4 changes: 3 additions & 1 deletion xcq-extension-fungibles/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ repository.workspace = true
version.workspace = true

[dependencies]
parity-scale-codec = { workspace = true }
xcq-extension = { workspace = true }

[features]
default = ["std"]
std = []
std = ["parity-scale-codec/std", "xcq-extension/std"]
65 changes: 56 additions & 9 deletions xcq-extension-fungibles/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,63 @@
#![cfg_attr(not(feature = "std"), no_std)]
use parity_scale_codec::{Decode, Encode};
use xcq_extension::Vec;
use xcq_extension::{DispatchError, Dispatchable};
use xcq_extension::{ExtensionId, ExtensionIdTy};

pub fn add(left: usize, right: usize) -> usize {
left + right
pub type AccountIdFor<T> = <<T as ExtensionFungibles>::Config as Config>::AccountId;
pub type BalanceFor<T> = <<T as ExtensionFungibles>::Config as Config>::Balance;
pub type AssetIdFor<T> = <<T as ExtensionFungibles>::Config as Config>::AssetId;

pub trait ExtensionFungibles {
type Config: Config;
// fungibles::Inspect (not extensive)
// fn total_inssuance(asset: AssetIdFor<Self>) -> BalanceFor<Self>;
// fn minimum_balance(asset: AssetIdFor<Self>) -> BalanceFor<Self>;
fn total_supply(asset: AssetIdFor<Self>) -> BalanceFor<Self>;
fn balance(asset: AssetIdFor<Self>, who: AccountIdFor<Self>) -> BalanceFor<Self>;
// fungibles::InspectEnumerable
// fn asset_ids() -> Vec<AccountIdFor<Self>>;
// fn account_balances(who: AccountIdFor<Self>) -> Vec<(AssetIdFor<Self>, BalanceFor<Self>)>;
}

pub trait Config {
type AccountId: Decode;
type AssetId: Decode;
type Balance: Encode;
}

#[cfg(test)]
mod tests {
// #[extension(ExtensionFungibles)]
// type Call;

mod generated_by_extension_decl {

use super::*;

#[test]
fn it_works() {
let result = add(2, 2);
assert_eq!(result, 4);
#[derive(Decode)]
pub enum ExtensionFungiblesCall<Impl: ExtensionFungibles> {
// TODO: not extensive
Balance {
asset: AssetIdFor<Impl>,
who: AccountIdFor<Impl>,
},
TotalSupply {
asset: AssetIdFor<Impl>,
},
}

impl<Impl: ExtensionFungibles> Dispatchable for ExtensionFungiblesCall<Impl> {
fn dispatch(self) -> Result<Vec<u8>, DispatchError> {
match self {
Self::Balance { asset, who } => Ok(Impl::balance(asset, who).encode()),
Self::TotalSupply { asset } => Ok(Impl::total_supply(asset).encode()),
}
}
}

impl<Impl: ExtensionFungibles> ExtensionId for ExtensionFungiblesCall<Impl> {
const EXTENSION_ID: ExtensionIdTy = 1u64;
}

pub type Call<Impl> = ExtensionFungiblesCall<Impl>;
}

pub use generated_by_extension_decl::*;
16 changes: 15 additions & 1 deletion xcq-extension/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,21 @@ repository.workspace = true
version.workspace = true

[dependencies]
parity-scale-codec = { workspace = true }
scale-info = { workspace = true }
xcq-executor = { workspace = true }
impl-trait-for-tuples = "0.2.2"
tracing = { workspace = true }

[dev-dependencies]
xcq-extension-core = { workspace = true }
xcq-extension-fungibles = { workspace = true }

[features]
default = ["std"]
std = []
std = [
"parity-scale-codec/std",
"scale-info/std",
"xcq-executor/std",
"tracing/std",
]
9 changes: 9 additions & 0 deletions xcq-extension/src/dispatchable.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use crate::Vec;
pub trait Dispatchable {
fn dispatch(self) -> Result<Vec<u8>, DispatchError>;
}

#[derive(Debug)]
pub enum DispatchError {
PhantomData,
}
11 changes: 11 additions & 0 deletions xcq-extension/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// TODO: contain source error
use crate::DispatchError;
use parity_scale_codec::Error as CodeCError;
#[derive(Debug)]
pub enum ExtensionError {
PermissionError,
PolkavmError,
DecodeError(CodeCError),
DispatchError(DispatchError),
UnsupportedExtension,
}
5 changes: 5 additions & 0 deletions xcq-extension/src/extension_id.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pub type ExtensionIdTy = u64;

pub trait ExtensionId {
const EXTENSION_ID: ExtensionIdTy;
}
11 changes: 11 additions & 0 deletions xcq-extension/src/guest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use scale_info::prelude::string::String;
pub trait Guest {
fn program(&self) -> &[u8];
}

pub type Method = String;

pub trait Input {
fn method(&self) -> Method;
fn args(&self) -> &[u8];
}
Loading