Skip to content

Commit

Permalink
remove trait bound
Browse files Browse the repository at this point in the history
  • Loading branch information
indirection42 committed Jun 6, 2024
1 parent 283979e commit c24dc24
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 27 deletions.
3 changes: 1 addition & 2 deletions poc/extensions/src/dispatchable.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
pub trait Dispatchable {
// TODO: check if origin is specified as associated type
fn dispatch(self) -> Result<Vec<u8>, DispatchError>;
}

pub enum DispatchError {
UnsupportedContext,
PhantomData,
}
4 changes: 2 additions & 2 deletions poc/extensions/src/extension_core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{DispatchError, Dispatchable};
use crate::{ExtensionId, ExtensionIdTy};
use parity_scale_codec::{Decode, Encode};

pub trait ExtensionCore: ExtensionId {
pub trait ExtensionCore {
type Config: Config;
fn some_host_function(
args: <Self::Config as Config>::ArgsOfSomeHostFunction,
Expand Down Expand Up @@ -36,7 +36,7 @@ mod generated_by_extension_decl {
}

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

// TODO: remove this when formalized
Expand Down
14 changes: 8 additions & 6 deletions poc/extensions/src/extension_fungibles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{ExtensionId, ExtensionIdTy};
use core::marker::PhantomData;
use parity_scale_codec::{Decode, Encode};

pub trait ExtensionFungibles: ExtensionId {
pub trait ExtensionFungibles {
fn free_balance_of(who: [u8; 32]) -> u32;
fn reserved_balance_of(who: [u8; 32]) -> u32;
}
Expand All @@ -16,21 +16,23 @@ mod generated_by_extension_decl {
use super::*;
#[derive(Decode)]
pub enum ExtensionFungiblesCall<Impl: ExtensionFungibles> {
FreeBalanceOf { who: [u8; 32], _marker: PhantomData<Impl> },
ReservedBalanceOf { who: [u8; 32], _marker: PhantomData<Impl> },
FreeBalanceOf { who: [u8; 32] },
ReservedBalanceOf { who: [u8; 32] },
_Marker(PhantomData<Impl>),
}

impl<Impl: ExtensionFungibles> Dispatchable for ExtensionFungiblesCall<Impl> {
fn dispatch(self) -> Result<Vec<u8>, DispatchError> {
match self {
Self::FreeBalanceOf { who, .. } => Ok(Impl::free_balance_of(who).encode()),
Self::ReservedBalanceOf { who, .. } => Ok(Impl::reserved_balance_of(who).encode()),
Self::FreeBalanceOf { who } => Ok(Impl::free_balance_of(who).encode()),
Self::ReservedBalanceOf { who } => Ok(Impl::reserved_balance_of(who).encode()),
Self::_Marker(_) => Err(DispatchError::PhantomData),
}
}
}

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

// TODO: remove this when formalized
Expand Down
3 changes: 1 addition & 2 deletions poc/extensions/src/guest.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::ExtensionId;
pub trait Guest: ExtensionId {
pub trait Guest {
fn program(&self) -> &[u8];
}

Expand Down
20 changes: 5 additions & 15 deletions poc/extensions/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,14 @@ trait ExtensionTuple {

struct Context<E: ExtensionTuple, P: PermController> {
invoke_source: InvokeSource,
phantom_p: PhantomData<P>,
phantom_e: PhantomData<E>,
_marker: PhantomData<(E, P)>,
}

impl<E: ExtensionTuple, P: PermController> Context<E, P> {
pub fn new(invoke_source: InvokeSource) -> Self {
Self {
invoke_source,
phantom_p: PhantomData,
phantom_e: PhantomData,
_marker: PhantomData,
}
}
}
Expand Down Expand Up @@ -126,10 +124,6 @@ mod tests {
type ResultOfSomeHostFunction = u32;
}

impl ExtensionId for ExtensionCoreImpl {
const EXTENSION_ID: ExtensionIdTy = 0u64;
}

impl ExtensionCore for ExtensionCoreImpl {
type Config = ConfigImpl;
fn some_host_function(
Expand All @@ -142,10 +136,6 @@ mod tests {
// extension_fungibles impls
pub struct ExtensionFungiblesImpl;

impl ExtensionId for ExtensionFungiblesImpl {
const EXTENSION_ID: ExtensionIdTy = 1u64;
}

impl ExtensionFungibles for ExtensionFungiblesImpl {
fn free_balance_of(_who: [u8; 32]) -> u32 {
100
Expand Down Expand Up @@ -184,9 +174,7 @@ mod tests {
&self.args
}
}
impl ExtensionId for GuestImpl {
const EXTENSION_ID: ExtensionIdTy = 0u64;
}

// TODO: refine the test
#[test]
fn extensions_executor_fails() {
Expand All @@ -201,4 +189,6 @@ mod tests {
let res = executor.execute_method(guest, input);
assert!(res.is_err())
}

// TODO: add success test
}

0 comments on commit c24dc24

Please sign in to comment.