mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-07-24 06:35:45 +00:00
Introduce first groundwork for Wasm executor (#27)
* Introduce first groundwork for Wasm executor. * Remove old Rust-runtime code. * Avoid commiting compled files. * Add runtime precompile. * Rename so module makes more sense. * Further renaming. * Ensure tests work. * Allow bringing in of externalities. - Add util functions/macros. - Add uncompacted runtime. - Add some external crates from pwasm-std for managing allocs/memory stuff. * Nice macros for imports. * Allow passing in of data through allocators. Make memcpy and malloc work. Basic allocator. * Can now pass in bytes to WasmExecutor. * Additional cleanup. * Switch usages of `OutData` to `u64` No need to be able to return bytes anymore. * convert to safe but extremely verbose type conversion. @rphmeier any more concise way of doing this? * Remove StaticExternalities distinction. * Remove another unused use. * Refactor wasm utils out * Remove extraneous copies that weren't really testing anything. * Try to use wasm 0.15 * Make it work! * Call-time externalities working. * Add basic externalities. * Fix grumbles and note unwraps to be sorted. * Test storage externality. Unforunately had to change signatures of externalities to avoid immutable function returning a reference. Not sure what to do about this... * Fix nits. * Compile collation logic. * Move back to refs. Yey. * Remove "object" id for storage access. * Fix test. * Fix up rest of tests. * remove unwrap. * Expose set/get code in externalities Also improve tests and add nice wrappers in rust-wasm. * Add validator set. * Introduce validator set into externalities and test. * Add another external function. * Remove code and validators; use storage for everything. * Introduce validators function. * Tests (and a fix) for the validators getter. * Allow calls into runtime to return data. * Remove unneeded trace. * Make runtime printing a bit nicer. * Create separate runtimes for testing and polkadot. * Remove commented code. * Use new path. * Refactor into shared support module. * Fix warning. * Remove unwraps. * Make macro a little less unhygenic. * Add wasm files.
This commit is contained in:
committed by
Robert Habermeier
parent
45c3e40a62
commit
a670208a33
Generated
+33
@@ -0,0 +1,33 @@
|
||||
[[package]]
|
||||
name = "pwasm-alloc"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"pwasm-libc 0.1.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "pwasm-libc"
|
||||
version = "0.1.0"
|
||||
|
||||
[[package]]
|
||||
name = "runtime-polkadot"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"runtime-support 0.1.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "runtime-support"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"pwasm-alloc 0.1.0",
|
||||
"pwasm-libc 0.1.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "runtime-test"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"runtime-support 0.1.0",
|
||||
]
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
[workspace]
|
||||
members = [
|
||||
"test",
|
||||
"polkadot",
|
||||
]
|
||||
|
||||
[profile.release]
|
||||
panic = "abort"
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
#!/bin/sh
|
||||
|
||||
cargo +nightly build --target=wasm32-unknown-unknown --release
|
||||
dirs=`find * -maxdepth 0 -type d | grep -v pwasm- | grep -v support`
|
||||
for i in $dirs
|
||||
do
|
||||
if [[ -e $i/Cargo.toml ]]
|
||||
then
|
||||
wasm-gc target/wasm32-unknown-unknown/release/runtime_$i.wasm target/wasm32-unknown-unknown/release/runtime_$i.compact.wasm
|
||||
fi
|
||||
done
|
||||
Executable
+6
@@ -0,0 +1,6 @@
|
||||
#!/bin/sh
|
||||
|
||||
rustup update nightly
|
||||
rustup target add wasm32-unknown-unknown --toolchain nightly
|
||||
rustup update stable
|
||||
cargo install --git https://github.com/alexcrichton/wasm-gc
|
||||
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "runtime-polkadot"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
runtime-support = { path = "../support", version = "0.1" }
|
||||
@@ -0,0 +1,36 @@
|
||||
#![no_std]
|
||||
#![feature(lang_items)]
|
||||
#![cfg_attr(feature = "strict", deny(warnings))]
|
||||
|
||||
#![feature(alloc)]
|
||||
extern crate alloc;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
#[macro_use]
|
||||
extern crate runtime_support;
|
||||
use runtime_support::{set_storage, code, set_code, storage, validators, set_validators, print};
|
||||
|
||||
impl_stub!(test_data_in);
|
||||
fn test_data_in(input: Vec<u8>) -> Vec<u8> {
|
||||
print(b"set_storage" as &[u8]);
|
||||
set_storage(b"input", &input);
|
||||
|
||||
print(b"code" as &[u8]);
|
||||
set_storage(b"code", &code());
|
||||
|
||||
print(b"set_code" as &[u8]);
|
||||
set_code(&input);
|
||||
|
||||
print(b"storage" as &[u8]);
|
||||
let copy = storage(b"input");
|
||||
|
||||
print(b"validators" as &[u8]);
|
||||
let mut v = validators();
|
||||
v.push(copy);
|
||||
|
||||
print(b"set_validators" as &[u8]);
|
||||
set_validators(&v.iter().map(Vec::as_slice).collect::<Vec<_>>());
|
||||
|
||||
print(b"finished!" as &[u8]);
|
||||
b"all ok!".to_vec()
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
[package]
|
||||
name = "pwasm-alloc"
|
||||
version = "0.1.0"
|
||||
authors = ["Nikolay Volf <nikvolf@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/paritytech/pwasm-std"
|
||||
homepage = "https://github.com/paritytech/pwasm-std"
|
||||
documentation = "https://paritytech.github.io/pwasm-std/pwasm_std/"
|
||||
description = "Parity WebAssembly standard library internal allocator"
|
||||
keywords = ["wasm", "parity", "webassembly", "blockchain"]
|
||||
categories = ["no-std", "embedded"]
|
||||
|
||||
[dependencies]
|
||||
pwasm-libc = { path = "../pwasm-libc", version = "0.1" }
|
||||
|
||||
[features]
|
||||
strict = []
|
||||
@@ -0,0 +1,12 @@
|
||||
# pwasm-libc
|
||||
|
||||
Parity WASM contracts standard library libc bindings
|
||||
|
||||
[Documentation](https://paritytech.github.io/pwasm-std/pwasm_alloc/)
|
||||
|
||||
# License
|
||||
|
||||
`pwasm_alloc` is primarily distributed under the terms of both the MIT
|
||||
license and the Apache License (Version 2.0), at your choice.
|
||||
|
||||
See LICENSE-APACHE, and LICENSE-MIT for details.
|
||||
@@ -0,0 +1,30 @@
|
||||
#![warn(missing_docs)]
|
||||
#![cfg_attr(feature = "strict", deny(warnings))]
|
||||
#![no_std]
|
||||
#![crate_type = "rlib"]
|
||||
#![feature(global_allocator)]
|
||||
#![feature(alloc)]
|
||||
#![feature(allocator_api)]
|
||||
|
||||
//! Custom allocator crate for wasm
|
||||
|
||||
extern crate alloc;
|
||||
extern crate pwasm_libc;
|
||||
|
||||
use alloc::heap::{Alloc, Layout, AllocErr};
|
||||
|
||||
/// Wasm allocator
|
||||
pub struct WasmAllocator;
|
||||
|
||||
unsafe impl<'a> Alloc for &'a WasmAllocator {
|
||||
unsafe fn alloc(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
|
||||
Ok(pwasm_libc::malloc(layout.size()))
|
||||
}
|
||||
|
||||
unsafe fn dealloc(&mut self, ptr: *mut u8, _layout: Layout) {
|
||||
pwasm_libc::free(ptr)
|
||||
}
|
||||
}
|
||||
|
||||
#[global_allocator]
|
||||
static ALLOCATOR: WasmAllocator = WasmAllocator;
|
||||
@@ -0,0 +1,15 @@
|
||||
[package]
|
||||
name = "pwasm-libc"
|
||||
version = "0.1.0"
|
||||
authors = ["Sergey Pepyakin <s.pepyakin@gmail.com>"]
|
||||
license = "MIT/Apache-2.0"
|
||||
readme = "README.md"
|
||||
repository = "https://github.com/paritytech/pwasm-std"
|
||||
homepage = "https://github.com/paritytech/pwasm-std"
|
||||
documentation = "https://paritytech.github.io/pwasm-std/pwasm_std/"
|
||||
description = "Parity WebAssembly standard library libc bindings"
|
||||
keywords = ["wasm", "parity", "webassembly", "blockchain"]
|
||||
categories = ["no-std", "embedded"]
|
||||
|
||||
[features]
|
||||
strict = []
|
||||
@@ -0,0 +1,12 @@
|
||||
# pwasm-libc
|
||||
|
||||
Parity WASM contracts standard library libc bindings
|
||||
|
||||
[Documentation](https://paritytech.github.io/pwasm-std/pwasm_libc/)
|
||||
|
||||
# License
|
||||
|
||||
`pwasm-libc` is primarily distributed under the terms of both the MIT
|
||||
license and the Apache License (Version 2.0), at your choice.
|
||||
|
||||
See LICENSE-APACHE, and LICENSE-MIT for details.
|
||||
@@ -0,0 +1,46 @@
|
||||
#![warn(missing_docs)]
|
||||
#![cfg_attr(feature = "strict", deny(warnings))]
|
||||
#![no_std]
|
||||
|
||||
//! libc externs crate
|
||||
|
||||
extern "C" {
|
||||
fn ext_memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8;
|
||||
fn ext_memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8;
|
||||
fn ext_memset(dest: *mut u8, c: i32, n: usize) -> *mut u8;
|
||||
fn ext_malloc(size: usize) -> *mut u8;
|
||||
fn ext_free(ptr: *mut u8);
|
||||
}
|
||||
|
||||
// Declaring these function here prevents Emscripten from including it's own verisons
|
||||
// into final binary.
|
||||
|
||||
/// memcpy extern
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
|
||||
ext_memcpy(dest, src, n)
|
||||
}
|
||||
|
||||
/// memmove extern
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 {
|
||||
ext_memmove(dest, src, n)
|
||||
}
|
||||
|
||||
/// memset extern
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn memset(dest: *mut u8, c: i32, n: usize) -> *mut u8 {
|
||||
ext_memset(dest, c, n)
|
||||
}
|
||||
|
||||
/// malloc extern
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn malloc(size: usize) -> *mut u8 {
|
||||
ext_malloc(size)
|
||||
}
|
||||
|
||||
/// free extern
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn free(ptr: *mut u8) {
|
||||
ext_free(ptr);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
[package]
|
||||
name = "runtime-support"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
|
||||
[dependencies]
|
||||
pwasm-libc = { path = "../pwasm-libc", version = "0.1" }
|
||||
pwasm-alloc = { path = "../pwasm-alloc", version = "0.1" }
|
||||
|
||||
[features]
|
||||
strict = []
|
||||
@@ -0,0 +1,125 @@
|
||||
#![no_std]
|
||||
#![feature(lang_items)]
|
||||
#![cfg_attr(feature = "strict", deny(warnings))]
|
||||
|
||||
#![feature(alloc)]
|
||||
|
||||
extern crate alloc;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
extern crate pwasm_libc;
|
||||
extern crate pwasm_alloc;
|
||||
|
||||
#[lang = "panic_fmt"]
|
||||
#[no_mangle]
|
||||
pub fn panic_fmt() -> ! {
|
||||
loop {}
|
||||
}
|
||||
|
||||
extern "C" {
|
||||
fn ext_print(utf8_data: *const u8, utf8_len: i32);
|
||||
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;
|
||||
}
|
||||
|
||||
pub fn storage(key: &[u8]) -> Vec<u8> {
|
||||
let mut length: i32 = 0;
|
||||
unsafe {
|
||||
let ptr = ext_get_allocated_storage(&key[0], key.len() as i32, &mut length);
|
||||
Vec::from_raw_parts(ptr, length as usize, length as usize)
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
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 {
|
||||
fn print(self);
|
||||
}
|
||||
|
||||
impl<'a> Printable for &'a [u8] {
|
||||
fn print(self) {
|
||||
unsafe {
|
||||
ext_print(&self[0] as *const u8, self.len() as i32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Printable for u64 {
|
||||
fn print(self) {
|
||||
unsafe { ext_print_num(self); }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn print<T: Printable + Sized>(value: T) {
|
||||
value.print();
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
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)
|
||||
};
|
||||
|
||||
let output = super::$name(input);
|
||||
&output[0] as *const u8 as u64 + ((output.len() as u64) << 32)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,10 @@
|
||||
[package]
|
||||
name = "runtime-test"
|
||||
version = "0.1.0"
|
||||
authors = ["Parity Technologies <admin@parity.io>"]
|
||||
|
||||
[lib]
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
runtime-support = { path = "../support", version = "0.1" }
|
||||
@@ -0,0 +1,36 @@
|
||||
#![no_std]
|
||||
#![feature(lang_items)]
|
||||
#![cfg_attr(feature = "strict", deny(warnings))]
|
||||
|
||||
#![feature(alloc)]
|
||||
extern crate alloc;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
#[macro_use]
|
||||
extern crate runtime_support;
|
||||
use runtime_support::{set_storage, code, set_code, storage, validators, set_validators, print};
|
||||
|
||||
impl_stub!(test_data_in);
|
||||
fn test_data_in(input: Vec<u8>) -> Vec<u8> {
|
||||
print(b"set_storage" as &[u8]);
|
||||
set_storage(b"input", &input);
|
||||
|
||||
print(b"code" as &[u8]);
|
||||
set_storage(b"code", &code());
|
||||
|
||||
print(b"set_code" as &[u8]);
|
||||
set_code(&input);
|
||||
|
||||
print(b"storage" as &[u8]);
|
||||
let copy = storage(b"input");
|
||||
|
||||
print(b"validators" as &[u8]);
|
||||
let mut v = validators();
|
||||
v.push(copy);
|
||||
|
||||
print(b"set_validators" as &[u8]);
|
||||
set_validators(&v.iter().map(Vec::as_slice).collect::<Vec<_>>());
|
||||
|
||||
print(b"finished!" as &[u8]);
|
||||
b"all ok!".to_vec()
|
||||
}
|
||||
Reference in New Issue
Block a user