Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Poc query balance #21

Merged
merged 6 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ jobs:
- name: Check format
run: cargo fmt --all -- --check

- name: Make dummpy poc-guest.polkavm file
run: mkdir -p output; touch output/poc-guest.polkavm
- name: Make dummpy poc-guest-%.polkavm files
run: make dummy-poc-guests

- name: Cargo clippy
run: SKIP_WASM_BUILD= cargo clippy -- -D warnings
Expand All @@ -59,7 +59,7 @@ jobs:

- uses: Swatinem/rust-cache@v2
with:
workspaces: poc/guest -> poc/guest/target
workspaces: poc/guests -> poc/guests/target
cache-all-crates: true

- name: Install toolchain targeting RV32E
Expand All @@ -69,10 +69,7 @@ jobs:
mv rve-nightly ~/.rustup/toolchains/

- name: Cargo clippy
run: cd poc/guest; cargo clippy -- -D warnings
run: cd poc/guests; cargo clippy -- -D warnings

- name: Install polkatool
run: make polkatool

- name: Generate poc-guest.polkavm
run: make poc-guest
3 changes: 2 additions & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ version = "0.1.0"
[workspace]
resolver = "2"
members = [
"poc/host",
"poc/hosts/*",
"poc/runtime",
"poc/executor",

Expand All @@ -25,7 +25,7 @@ members = [
"examples/example-fungibles",
"examples/example-helloworld",
]
exclude = ["poc/guest", "vendor"]
exclude = ["poc/guests", "vendor"]

[profile.release]
# runtime requires unwinding.
Expand Down
26 changes: 18 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
GUEST_RUST_FLAGS="-C relocation-model=pie -C link-arg=--emit-relocs -C link-arg=--unique --remap-path-prefix=$(pwd)= --remap-path-prefix=$HOME=~"

run: chainspec
bunx @acala-network/chopsticks@latest --config poc/runtime/chopsticks.yml --genesis output/chainspec.json

poc-host: poc-guest
RUST_LOG=trace cargo run -p poc-host
poc-host-%: poc-guest-%
RUST_LOG=trace cargo run -p poc-host-$*

poc-guests: poc-guest-pass-custom-type poc-guest-query-balance

dummy-poc-guests: dummy-poc-guest-pass-custom-type dummy-poc-guest-query-balance

poc-guest-%:
cd poc/guests; RUSTFLAGS=$(GUEST_RUST_FLAGS) cargo build -q --release --bin poc-guest-$* -p poc-guest-$*
mkdir -p output
polkatool link --run-only-if-newer -s poc/guests/target/riscv32ema-unknown-none-elf/release/poc-guest-$* -o output/poc-guest-$*.polkavm

poc-guest:
cd poc/guest; RUSTFLAGS="-C relocation-model=pie -C link-arg=--emit-relocs -C link-arg=--unique --remap-path-prefix=$(pwd)= --remap-path-prefix=$$HOME=~" cargo build -q --release --bin poc-guest -p poc-guest
dummy-poc-guest-%:
mkdir -p output
polkatool link --run-only-if-newer -s poc/guest/target/riscv32ema-unknown-none-elf/release/poc-guest -o output/poc-guest.polkavm
touch output/poc-guest-$*.polkavm

tools: polkatool chain-spec-builder

polkatool:
cargo install --git https://github.com/koute/polkavm polkatool
cargo install --path vendor/polkavm/tools/polkatool

chain-spec-builder:
cargo install --git https://github.com/paritytech/polkadot-sdk --tag polkadot-v1.9.0 staging-chain-spec-builder
Expand All @@ -26,11 +36,11 @@ check-wasm:

check: check-wasm
SKIP_WASM_BUILD= cargo check
cd poc/guest; cargo check
cd poc/guests; cargo check

clippy:
SKIP_WASM_BUILD= cargo clippy -- -D warnings
cd poc/guest; cargo clippy
cd poc/guests; cargo clippy

chainspec:
cargo build -p poc-runtime --release
Expand Down
9 changes: 4 additions & 5 deletions poc/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
extern crate alloc;

pub use alloc::vec::Vec;
pub use polkavm::{Config, Engine, Linker, Module, ProgramBlob};
pub use polkavm::{Caller, Config, Engine, Linker, Module, ProgramBlob};

pub trait XcqExecutorContext {
fn register_host_functions<T>(&mut self, linker: &mut Linker<T>);
Expand Down Expand Up @@ -71,12 +71,11 @@ impl<Ctx: XcqExecutorContext> XcqExecutor<Ctx> {
0
};

// return value is u64 instead of (u32, u32) due to https://github.com/koute/polkavm/issues/116
let res = instance.call_typed::<(u32,), u64>(&mut self.context, "main", (input_ptr,))?;
let res = instance.call_typed::<(u32, u32), u64>(&mut self.context, "main", (input_ptr, input.len() as u32))?;
let res_ptr = (res >> 32) as u32;
let res_len = (res & 0xffffffff) as u32;
let res_size = (res & 0xffffffff) as u32;
let result = instance
.read_memory_into_vec(res_ptr, res_len)
.read_memory_into_vec(res_ptr, res_size)
.map_err(|e| XcqExecutorError::ExecutionError(polkavm::ExecutionError::Trap(e)))?;
Ok(result)
}
Expand Down
7 changes: 0 additions & 7 deletions poc/guest/Cargo.toml

This file was deleted.

File renamed without changes.
17 changes: 12 additions & 5 deletions poc/guest/Cargo.lock → poc/guests/Cargo.lock

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

7 changes: 7 additions & 0 deletions poc/guests/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[workspace]
members = ["pass-custom-type", "query-balance"]
resolver = "2"


[workspace.dependencies]
polkavm-derive = { path = "../../vendor/polkavm/crates/polkavm-derive" }
7 changes: 7 additions & 0 deletions poc/guests/pass-custom-type/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "poc-guest-pass-custom-type"
version = "0.1.0"
edition = "2021"

[dependencies]
polkavm-derive = { workspace = true }
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,15 @@ fn host_call<Args: Copy, Return: Copy>(input: Args, out: &mut Return) {
// return value is u64 instead of (u32, u32) due to https://github.com/koute/polkavm/issues/116
// higher 32bits are address, lower 32bits are size
#[polkavm_derive::polkavm_export]
extern "C" fn main(ptr: u32) -> u64 {
extern "C" fn main(ptr: u32, _size: u32) -> u64 {
// ready first byte from ptr
let byte = unsafe { core::ptr::read_volatile(ptr as *const u8) };
match byte {
0 => {
let val = b"test";
let size = core::mem::size_of_val(val);
let val_ptr = val.as_ptr();

(val_ptr as u64) << 32 | (size as u64 / core::mem::size_of::<u8>() as u64)
(val_ptr as u64) << 32 | size as u64
}
1 => {
let val = unsafe { core::ptr::read_volatile((ptr + 1) as *const u8) };
Expand All @@ -62,8 +61,13 @@ extern "C" fn main(ptr: u32) -> u64 {
let mut ret: GuestReturn = unsafe { core::mem::zeroed() };
host_call(guest_args, &mut ret);
let res = ret.data0 as u32 + 1;
let size = core::mem::size_of_val(&res);
(&res as *const u32 as u64) << 32 | (size as u64 / core::mem::size_of::<u32>() as u64)
let ptr = polkavm_derive::sbrk(core::mem::size_of_val(&res));
if ptr.is_null() {
return 0;
}
unsafe { core::ptr::write_volatile(ptr as *mut u32, res) };
let size = core::mem::size_of::<u32>();
(ptr as u64) << 32 | size as u64
}
_ => 0,
}
Expand Down
8 changes: 8 additions & 0 deletions poc/guests/query-balance/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "poc-guest-query-balance"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
polkavm-derive = { workspace = true }
35 changes: 35 additions & 0 deletions poc/guests/query-balance/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#![no_std]
#![no_main]

#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
unsafe {
core::arch::asm!("unimp", options(noreturn));
}
}

#[polkavm_derive::polkavm_import]
extern "C" {
fn query_balance(variant: u32, account_id_ptr: u32, account_id_size: u32) -> u64;
}
// return value is u64 instead of (u32, u32) due to https://github.com/koute/polkavm/issues/116
// higher 32bits are address, lower 32bits are size
#[polkavm_derive::polkavm_export]
extern "C" fn main(ptr: u32, size: u32) -> u64 {
// ready first byte from ptr
let mut sum = 0u64;
let variant = unsafe { core::ptr::read_volatile(ptr as *const u8) };
// hardcode since we know account_id_num
let account_id_size = (size - 1) / 2;
for i in 0..2 {
sum += unsafe { query_balance(variant as u32, ptr + 1 + (account_id_size * i), account_id_size) };
}
let ptr = polkavm_derive::sbrk(core::mem::size_of_val(&sum));
if ptr.is_null() {
return 0;
}
unsafe {
core::ptr::write_volatile(ptr as *mut u64, sum);
}
(ptr as u64) << 32 | (core::mem::size_of::<u64>() as u64)
}
File renamed without changes.
4 changes: 2 additions & 2 deletions poc/host/Cargo.toml → poc/hosts/pass-custom-type/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "poc-host"
name = "poc-host-pass-custom-type"
version = "0.1.0"
edition = "2021"

Expand All @@ -10,4 +10,4 @@ env_logger = { workspace = true }

polkavm = { workspace = true }

poc-executor = { path = "../executor" }
poc-executor = { path = "../../executor" }
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ impl poc_executor::XcqExecutorContext for HostFunctions {
core::mem::size_of::<GuestReturn>(),
)
};
println!("host_call: res: {:?}", res_bytes);
caller.write_memory(out_ptr, res_bytes).unwrap();
},
)
Expand All @@ -48,7 +49,7 @@ impl poc_executor::XcqExecutorContext for HostFunctions {
fn main() {
env_logger::init();

let raw_blob = include_bytes!("../../../output/poc-guest.polkavm");
let raw_blob = include_bytes!("../../../../output/poc-guest-pass-custom-type.polkavm");

let config = Config::from_env().unwrap();

Expand Down
28 changes: 17 additions & 11 deletions poc/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ parity-scale-codec = { version = "3.0.0", default-features = false }
scale-info = { version = "2.6.0", default-features = false }

# this is a frame-based runtime, thus importing `frame` with runtime feature enabled.
frame = { version = "0.1.1", package = "polkadot-sdk-frame", default-features = false, features = ["experimental", "runtime"] }
frame = { version = "0.1.1", package = "polkadot-sdk-frame", default-features = false, features = [
"experimental",
"runtime",
] }

# pallets that we want to use
pallet-balances = { version = "31.0.0", default-features = false }
Expand All @@ -22,25 +25,28 @@ sp-genesis-builder = { version = "0.10.0", default-features = false }

poc-executor = { path = "../executor", default-features = false }

[dev-dependencies]
hex = "0.4"

[build-dependencies]
substrate-wasm-builder = { version = "22.0.0", optional = true }

[features]
default = ["std"]
std = [
"parity-scale-codec/std",
"scale-info/std",
"parity-scale-codec/std",
"scale-info/std",

"frame/std",
"frame/std",

"pallet-balances/std",
"pallet-sudo/std",
"pallet-timestamp/std",
"pallet-transaction-payment-rpc-runtime-api/std",
"pallet-transaction-payment/std",
"pallet-balances/std",
"pallet-sudo/std",
"pallet-timestamp/std",
"pallet-transaction-payment-rpc-runtime-api/std",
"pallet-transaction-payment/std",

"sp-genesis-builder/std",
"substrate-wasm-builder",
"sp-genesis-builder/std",
"substrate-wasm-builder",

"poc-executor/std",
]
Loading