mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-09 20:11:09 +00:00
Introduce basic skeleton for Polkador runtime.
This commit is contained in:
@@ -17,70 +17,57 @@ pub fn panic_fmt() -> ! {
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
fn ext_print(utf8_data: *const u8, utf8_len: i32);
|
||||
fn ext_print(utf8_data: *const u8, utf8_len: u32);
|
||||
fn ext_print_num(value: u64);
|
||||
fn ext_set_storage(key_data: *const u8, key_len: i32, value_data: *const u8, value_len: i32);
|
||||
fn ext_get_allocated_storage(key_data: *const u8, key_len: i32, written_out: *mut i32) -> *mut u8;
|
||||
fn ext_set_storage(key_data: *const u8, key_len: u32, value_data: *const u8, value_len: u32);
|
||||
fn ext_get_allocated_storage(key_data: *const u8, key_len: u32, written_out: *mut u32) -> *mut u8;
|
||||
fn ext_get_storage_into(key_data: *const u8, key_len: u32, value_data: *mut u8, value_len: u32) -> u32;
|
||||
fn ext_deposit_log(log_data: *const u8, log_len: u32);
|
||||
}
|
||||
|
||||
pub fn storage(key: &[u8]) -> Vec<u8> {
|
||||
let mut length: i32 = 0;
|
||||
let mut length: u32 = 0;
|
||||
unsafe {
|
||||
let ptr = ext_get_allocated_storage(&key[0], key.len() as i32, &mut length);
|
||||
let ptr = ext_get_allocated_storage(&key[0], key.len() as u32, &mut length);
|
||||
Vec::from_raw_parts(ptr, length as usize, length as usize)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait IsValue {
|
||||
const value: usize;
|
||||
}
|
||||
|
||||
pub struct Value20; impl IsValue for Value20 { const value = 20usize; }
|
||||
pub struct Value32; impl IsValue for Value32 { const value = 32usize; }
|
||||
pub struct Value64; impl IsValue for Value64 { const value = 64usize; }
|
||||
pub struct Value65; impl IsValue for Value65 { const value = 65usize; }
|
||||
|
||||
pub fn storage_into<T: IsValue>(key: &[u8]) -> Option<[u8; T::value]> {
|
||||
let mut result = [0u8; T::value];
|
||||
let written = unsafe {
|
||||
ext_get_storage_into(&key[0], key.len() as u32, &result[0], result.len())
|
||||
};
|
||||
match written {
|
||||
T::value => Some(result),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_storage(key: &[u8], value: &[u8]) {
|
||||
unsafe {
|
||||
ext_set_storage(
|
||||
&key[0] as *const u8, key.len() as i32,
|
||||
&value[0] as *const u8, value.len() as i32
|
||||
&key[0] as *const u8, key.len() as u32,
|
||||
&value[0] as *const u8, value.len() as u32
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn code() -> Vec<u8> {
|
||||
storage(b"\0code")
|
||||
}
|
||||
|
||||
pub fn set_code(new: &[u8]) {
|
||||
set_storage(b"\0code", new)
|
||||
}
|
||||
|
||||
fn value_vec(mut value: usize, initial: Vec<u8>) -> Vec<u8> {
|
||||
let mut acc = initial;
|
||||
while value > 0 {
|
||||
acc.push(value as u8);
|
||||
value /= 256;
|
||||
pub fn deposit_log(log: &[u8]) {
|
||||
unsafe {
|
||||
ext_deposit_log(
|
||||
&log[0] as *const u8, log.len() as u32,
|
||||
)
|
||||
}
|
||||
acc
|
||||
}
|
||||
|
||||
pub fn set_validator(index: usize, validator: &[u8]) {
|
||||
set_storage(&value_vec(index, b"\0validator".to_vec()), validator);
|
||||
}
|
||||
|
||||
pub fn validator(index: usize) -> Vec<u8> {
|
||||
storage(&value_vec(index, b"\0validator".to_vec()))
|
||||
}
|
||||
|
||||
pub fn set_validator_count(count: usize) {
|
||||
(count..validator_count()).for_each(|i| set_validator(i, &[]));
|
||||
set_storage(b"\0validator_count", &value_vec(count, Vec::new()));
|
||||
}
|
||||
|
||||
pub fn validator_count() -> usize {
|
||||
storage(b"\0validator_count").into_iter().rev().fold(0, |acc, i| (acc << 8) + (i as usize))
|
||||
}
|
||||
|
||||
pub fn validators() -> Vec<Vec<u8>> {
|
||||
(0..validator_count()).into_iter().map(validator).collect()
|
||||
}
|
||||
|
||||
pub fn set_validators(validators: &[&[u8]]) {
|
||||
set_validator_count(validators.len());
|
||||
validators.iter().enumerate().for_each(|(v, i)| set_validator(v, i));
|
||||
}
|
||||
|
||||
pub trait Printable {
|
||||
@@ -90,7 +77,7 @@ pub trait Printable {
|
||||
impl<'a> Printable for &'a [u8] {
|
||||
fn print(self) {
|
||||
unsafe {
|
||||
ext_print(&self[0] as *const u8, self.len() as i32);
|
||||
ext_print(&self[0] as *const u8, self.len() as u32);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,12 +96,10 @@ pub fn print<T: Printable + Sized>(value: T) {
|
||||
macro_rules! impl_stub {
|
||||
($name:ident) => {
|
||||
pub mod _internal {
|
||||
extern crate alloc;
|
||||
|
||||
#[no_mangle]
|
||||
pub fn $name(input_data: *mut u8, input_len: usize) -> u64 {
|
||||
let input = unsafe {
|
||||
::alloc::vec::Vec::from_raw_parts(input_data, input_len, input_len)
|
||||
super::alloc::vec::Vec::from_raw_parts(input_data, input_len, input_len)
|
||||
};
|
||||
|
||||
let output = super::$name(input);
|
||||
|
||||
Reference in New Issue
Block a user