Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
kvinwang committed Feb 17, 2023
1 parent 561109c commit a41ccf9
Show file tree
Hide file tree
Showing 19 changed files with 793 additions and 77 deletions.
13 changes: 13 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ members = [
"crates/pink/runtime",
"crates/pink/runtime/unittests",
"crates/pink/capi",
"crates/pink/macro",
"crates/pink/pink-extension",
"crates/pink/pink-extension-runtime",
"crates/pink-libs/s3",
Expand Down
1 change: 1 addition & 0 deletions crates/pink/capi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pink-extension = { path = "../pink-extension" }
scale = { package = "parity-scale-codec", version = "3", default-features = false, features = [
"derive",
] }
pink-macro = { path = "../macro" }

[build-dependencies]
bindgen = "0.64.0"
14 changes: 14 additions & 0 deletions crates/pink/capi/src/helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
pub trait ParamType {
type T;
type E;
}
impl<T> ParamType for Option<T> {
type T = T;
type E = ();
}
impl<T, E> ParamType for Result<T, E> {
type T = T;
type E = E;
}
pub type InnerType<T> = <T as ParamType>::T;
pub type InnerError<T> = <T as ParamType>::E;
3 changes: 2 additions & 1 deletion crates/pink/capi/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod types;
pub mod v1;
pub mod v1;
pub mod helper;
66 changes: 66 additions & 0 deletions crates/pink/capi/src/v1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,69 @@
#[allow(dead_code)]
mod types;
pub use types::*;

pub trait CrossCall {
fn cross_call(&self, id: u32, data: &[u8]) -> Vec<u8>;
}
pub trait ECall: CrossCall {}
pub trait OCall: CrossCall {}

pub mod ecall {
use super::ECall;
use crate::types::{AccountId, Balance, Hash};
use pink_macro::cross_call;
use scale::{Decode, Encode};

#[cross_call(ECall)]
pub trait ECalls {
#[xcall(id = 1)]
fn set_cluster_id(&self, cluster_id: Vec<u8>);
#[xcall(id = 2)]
fn setup(
&self,
gas_price: Balance,
deposit_per_item: Balance,
deposit_per_byte: Balance,
treasury_account: AccountId,
);
#[xcall(id = 3)]
fn deposit(&self, who: AccountId, value: Balance);
#[xcall(id = 4)]
fn set_key_seed(&self, seed: Vec<u8>);
#[xcall(id = 5)]
fn set_key(&self, key: Vec<u8>);
#[xcall(id = 6)]
fn get_key(&self) -> Vec<u8>;
#[xcall(id = 7)]
fn upload_code(
&self,
account: AccountId,
code: Vec<u8>,
deterministic: bool,
) -> Result<Hash, Vec<u8>>;
#[xcall(id = 8)]
fn upload_sidevm_code(&self, account: AccountId, code: Vec<u8>) -> Result<Hash, Vec<u8>>;
#[xcall(id = 9)]
fn get_sidevm_code(&self, hash: Hash) -> Option<Vec<u8>>;
#[xcall(id = 10)]
fn set_system_contract(&self, address: AccountId);
#[xcall(id = 11)]
fn system_contract(&self) -> Option<AccountId>;
#[xcall(id = 12)]
fn get(&self, key: Vec<u8>) -> Option<Vec<u8>>;
#[xcall(id = 13)]
fn root(&self) -> Hash;
#[xcall(id = 14)]
fn free_balance(&self, account: AccountId) -> Balance;
#[xcall(id = 15)]
fn total_balance(&self, account: AccountId) -> Balance;
#[xcall(id = 16)]
fn code_hash(&self, account: AccountId) -> Option<Hash>;
#[xcall(id = 17)]
fn set_system_contract_code(&self, code_hash: Hash) -> Result<(), Vec<u8>>;
#[xcall(id = 18)]
fn code_exists(&self, sidevm: bool) -> bool;
}
}

pub mod ocall {}
12 changes: 4 additions & 8 deletions crates/pink/capi/src/v1/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,19 @@
#include <inttypes.h>
#include <stddef.h>

typedef struct
{
int (*storage_get)(const uint8_t *key, size_t keylen, int32_t *rc, uint8_t *value, size_t *value_len);
} ocalls_t;
typedef void (*output_fn_t)(void *ctx, const uint8_t *data, size_t len);
typedef void (*cross_call_fn_t)(uint32_t call_id, const uint8_t *data, size_t len, void *ctx, output_fn_t output);

typedef struct
{
int is_dylib;
int enclaved;
ocalls_t ocalls;
cross_call_fn_t ocall;
} config_t;

typedef struct
{
// int (*contract_call)();
// int (*contract_instantiate)();
int (*cluster_set_id)(const uint8_t *id, size_t len);
cross_call_fn_t ecall;
} ecalls_t;

typedef int init_t(const config_t *config, ecalls_t *ecalls);
31 changes: 14 additions & 17 deletions crates/pink/capi/src/v1/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,32 +383,29 @@ pub struct max_align_t {
pub __bindgen_padding_0: u64,
pub __clang_max_align_nonce2: u128,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct ocalls_t {
pub storage_get: ::core::option::Option<
unsafe extern "C" fn(
key: *const u8,
keylen: usize,
rc: *mut i32,
value: *mut u8,
value_len: *mut usize,
) -> ::core::ffi::c_int,
>,
}
pub type output_fn_t = ::core::option::Option<
unsafe extern "C" fn(ctx: *mut ::core::ffi::c_void, data: *const u8, len: usize),
>;
pub type cross_call_fn_t = ::core::option::Option<
unsafe extern "C" fn(
call_id: u32,
data: *const u8,
len: usize,
ctx: *mut ::core::ffi::c_void,
output: output_fn_t,
),
>;
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct config_t {
pub is_dylib: ::core::ffi::c_int,
pub enclaved: ::core::ffi::c_int,
pub ocalls: ocalls_t,
pub ocall: cross_call_fn_t,
}
#[repr(C)]
#[derive(Debug, Default, Copy, Clone)]
pub struct ecalls_t {
pub cluster_set_id: ::core::option::Option<
unsafe extern "C" fn(id: *const u8, len: usize) -> ::core::ffi::c_int,
>,
pub ecall: cross_call_fn_t,
}
pub type init_t = ::core::option::Option<
unsafe extern "C" fn(config: *const config_t, ecalls: *mut ecalls_t) -> ::core::ffi::c_int,
Expand Down
20 changes: 20 additions & 0 deletions crates/pink/macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
[package]
description = "Helper macros for pink runtime"
homepage = "https://github.com/Phala-Network/phala-blockchain"
license = "Apache-2.0"
edition = "2021"
name = "pink-macro"
version = "0.1.1"

[lib]
proc-macro = true

[dependencies]
proc-macro-crate = "1.0.0"
proc-macro2 = "1.0"
syn = { version = "1.0", features = ["full", "visit-mut", "extra-traits", "parsing"] }
heck = "0.4.0"

[dev-dependencies]
insta = "1.14.0"
rustfmt-snippet = { version = "0.1.0" }
14 changes: 14 additions & 0 deletions crates/pink/macro/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use proc_macro::TokenStream;

mod macro_xcall;
#[cfg(test)]
mod tests;

#[proc_macro_attribute]
pub fn cross_call(config: TokenStream, input: TokenStream) -> TokenStream {
macro_xcall::patch(
syn::parse_macro_input!(config),
syn::parse_macro_input!(input),
)
.into()
}
Loading

0 comments on commit a41ccf9

Please sign in to comment.