Skip to content

Commit

Permalink
poc query balance working
Browse files Browse the repository at this point in the history
  • Loading branch information
indirection42 committed May 23, 2024
1 parent 9df7301 commit 20a65c1
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 48 deletions.
4 changes: 2 additions & 2 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 Down
10 changes: 8 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,21 @@ poc-host-%: poc-guest-%

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
polkatool link --run-only-if-newer -s poc/guests/target/riscv32ema-unknown-none-elf/release/poc-guest-$* -o output/poc-guest-$*.polkavm

dummy-poc-guest-%:
mkdir -p output
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 Down
10 changes: 5 additions & 5 deletions poc/executor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ 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, u32), u64>(&mut self.context, "main", (input_ptr, input.len() as u32))?;
// TODO: NEED TO FIX, failed to get return values via guest's stack
// 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)))?;
let res = res.to_le_bytes();
Ok(res.to_vec())
let result = res.to_le_bytes().to_vec();
Ok(result)
}
}
16 changes: 8 additions & 8 deletions poc/guests/Cargo.lock

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

8 changes: 4 additions & 4 deletions poc/guests/pass-custom-type/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ 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 {
Expand All @@ -51,7 +51,7 @@ extern "C" fn main(ptr: u32) -> u64 {
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 +62,8 @@ 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 size = core::mem::size_of::<u32>();
(&res as *const u32 as u64) << 32 | size as u64
}
_ => 0,
}
Expand Down
20 changes: 11 additions & 9 deletions poc/guests/query-balance/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,20 @@ fn panic(_info: &core::panic::PanicInfo) -> ! {

#[polkavm_derive::polkavm_import]
extern "C" {
fn query_balances(variant: u32, accounts_ptr: u32, accounts_len: u32) -> u64;
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, len: u32) -> u64 {
extern "C" fn main(ptr: u32, size: u32) -> u64 {
// ready first byte from ptr
let byte_ptr = ptr as *const u8;
let variant = unsafe { core::ptr::read_volatile(byte_ptr) };
// TODO: need to figure out which encoding/decoding mechanism is appropriate, self-describing or just specify size or not when not.
// Specifying type may bloat the code, should be researched.
// some principles: make host functions api more standardized
// like query_balance(variant, single_account_ptr);
unsafe { query_balances(variant as u32, byte_ptr.offset(1) as u32, len - 1) }
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) };
}
// (&sum as *const u64 as u64) << 32 | (core::mem::size_of::<u64>() as u64)
sum
}
3 changes: 2 additions & 1 deletion poc/hosts/pass-custom-type/src/main.rs
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
36 changes: 19 additions & 17 deletions poc/runtime/src/xcq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,28 @@ impl poc_executor::XcqExecutorContext for HostFunctions {
fn register_host_functions<T>(&mut self, linker: &mut poc_executor::Linker<T>) {
linker
.func_wrap(
"query_balances",
move |caller: poc_executor::Caller<_>, variant: u32, accounts_ptr: u32, accounts_len: u32| -> u64 {
"query_balance",
move |caller: poc_executor::Caller<_>,
variant: u32,
account_id_ptr: u32,
account_id_size: u32|
-> u64 {
// variant 0 means free balance
// variant 1 means reserved balance
// variant 2 means free+reserved
let mut sum = 0u64;
let account_id_in_bytes = caller
.read_memory_into_vec(accounts_ptr, accounts_len)
let account_id_encoded = caller
.read_memory_into_vec(account_id_ptr, account_id_size)
.expect("read_memory_into_vec failed");
let account_ids = Vec::<AccountId>::decode(&mut &account_id_in_bytes[..]).expect("decode failed");
for account_id in account_ids {
if variant == 0 {
sum += Balances::free_balance(&account_id)
} else if variant == 1 {
sum += Balances::reserved_balance(&account_id)
} else if variant == 2 {
sum += Balances::free_balance(&account_id) + Balances::reserved_balance(&account_id)
}
let account_id = AccountId::decode(&mut &account_id_encoded[..]).expect("decode failed");
if variant == 0 {
Balances::free_balance(&account_id)
} else if variant == 1 {
Balances::reserved_balance(&account_id)
} else if variant == 2 {
Balances::free_balance(&account_id) + Balances::reserved_balance(&account_id)
} else {
panic!("invalid variant")
}
sum
},
)
.unwrap();
Expand Down Expand Up @@ -71,8 +73,8 @@ mod tests {
let alice_account: AccountId32 = AccountId32::from(alice_public);
let bob_account: AccountId32 = AccountId32::from(bob_public);
let mut data = vec![0u8];
let accounts = vec![alice_account, bob_account];
data.extend_from_slice(&accounts.encode());
data.extend_from_slice(&alice_account.encode());
data.extend_from_slice(&bob_account.encode());
dbg!(hex::encode((raw_blob.to_vec(), data).encode()));
}
#[test]
Expand Down

0 comments on commit 20a65c1

Please sign in to comment.