-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Every Extension is represented by a trait * Extension trait has corresponding Dispatchables (enum generic on extension impl) * dummy implementations for trait like extension_core and extension_fungibles * An ExtensionExecutor which supports initializing extension impls tuple, dispatching polkavm host calls * A PermController safeguards extensions according to invocation source
- Loading branch information
1 parent
f811522
commit f1b7052
Showing
15 changed files
with
398 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "poc-extension" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
parity-scale-codec = { version = "3.6.12", default-features = false } | ||
scale-info = { version = "2.6.0", default-features = false } | ||
poc-executor = { path = "../executor", default-features = false } | ||
impl-trait-for-tuples = "0.2.2" | ||
|
||
[features] | ||
default = ["std"] | ||
std = ["parity-scale-codec/std", "scale-info/std", "poc-executor/std"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
pub trait Dispatchable { | ||
fn dispatch(self) -> Result<Vec<u8>, DispatchError>; | ||
} | ||
|
||
pub enum DispatchError { | ||
PhantomData, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
// TODO: contain source error | ||
use crate::DispatchError; | ||
use parity_scale_codec::Error as CodeCError; | ||
pub enum ExtensionError { | ||
PermissionError, | ||
PolkavmError, | ||
DecodeError(CodeCError), | ||
DispatchError(DispatchError), | ||
UnsupportedExtension, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use crate::{DispatchError, Dispatchable}; | ||
use crate::{ExtensionId, ExtensionIdTy}; | ||
use parity_scale_codec::{Decode, Encode}; | ||
|
||
pub trait ExtensionCore { | ||
type Config: Config; | ||
fn some_host_function( | ||
args: <Self::Config as Config>::ArgsOfSomeHostFunction, | ||
) -> <Self::Config as Config>::ResultOfSomeHostFunction; | ||
} | ||
|
||
pub trait Config { | ||
type ArgsOfSomeHostFunction: Decode; | ||
type ResultOfSomeHostFunction: Encode; | ||
} | ||
|
||
// #[extension(ExtensionCore)] | ||
// type Call; | ||
|
||
mod generated_by_extension_decl { | ||
use super::*; | ||
|
||
#[derive(Decode)] | ||
pub enum ExtensionCoreCall<Impl: ExtensionCore> { | ||
SomeHostFunction { | ||
args: <Impl::Config as Config>::ArgsOfSomeHostFunction, | ||
}, | ||
} | ||
|
||
impl<Impl: ExtensionCore> Dispatchable for ExtensionCoreCall<Impl> { | ||
fn dispatch(self) -> Result<Vec<u8>, DispatchError> { | ||
match self { | ||
Self::SomeHostFunction { args } => Ok(Impl::some_host_function(args).encode()), | ||
} | ||
} | ||
} | ||
|
||
impl<Impl: ExtensionCore> ExtensionId for ExtensionCoreCall<Impl> { | ||
const EXTENSION_ID: ExtensionIdTy = 0u64; | ||
} | ||
|
||
// TODO: remove this when formalized | ||
#[allow(dead_code)] | ||
pub type Call<Impl> = ExtensionCoreCall<Impl>; | ||
} | ||
|
||
#[allow(unused_imports)] | ||
pub use generated_by_extension_decl::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use crate::{DispatchError, Dispatchable}; | ||
use crate::{ExtensionId, ExtensionIdTy}; | ||
use core::marker::PhantomData; | ||
use parity_scale_codec::{Decode, Encode}; | ||
|
||
pub trait ExtensionFungibles { | ||
fn free_balance_of(who: [u8; 32]) -> u32; | ||
fn reserved_balance_of(who: [u8; 32]) -> u32; | ||
} | ||
|
||
// #[extension(ExtensionFungibles)] | ||
// type Call; | ||
|
||
mod generated_by_extension_decl { | ||
|
||
use super::*; | ||
#[derive(Decode)] | ||
pub enum ExtensionFungiblesCall<Impl: ExtensionFungibles> { | ||
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::_Marker(_) => Err(DispatchError::PhantomData), | ||
} | ||
} | ||
} | ||
|
||
impl<Impl: ExtensionFungibles> ExtensionId for ExtensionFungiblesCall<Impl> { | ||
const EXTENSION_ID: ExtensionIdTy = 1u64; | ||
} | ||
|
||
// TODO: remove this when formalized | ||
#[allow(dead_code)] | ||
pub type Call<Impl> = ExtensionFungiblesCall<Impl>; | ||
} | ||
|
||
#[allow(unused_imports)] | ||
pub use generated_by_extension_decl::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
pub trait Guest { | ||
fn program(&self) -> &[u8]; | ||
} | ||
|
||
pub type Method = String; | ||
|
||
pub trait Input { | ||
fn method(&self) -> Method; | ||
fn args(&self) -> &[u8]; | ||
} |
Oops, something went wrong.