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

[x/programs] Improve State-keys & Argument Passing #623

Merged
merged 78 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
78 commits
Select commit Hold shift + click to select a range
c49fbc6
adding Key struct
samliok Nov 20, 2023
2eaa9aa
use Key struct in state methods
samliok Nov 20, 2023
2526ced
comments
samliok Nov 20, 2023
f457e23
key as ref and lint
samliok Nov 20, 2023
a5c652c
update sdk macro naming
samliok Nov 20, 2023
baa312a
forgot one is_context() method
samliok Nov 20, 2023
2158524
update public macro to convert from raw_ptr
samliok Nov 20, 2023
3e5b797
harden public macro, and converted params logic
samliok Nov 20, 2023
16d9dcd
reorder comments
samliok Nov 20, 2023
bfb7338
HostArgument type + Vec Example(rust)
samliok Nov 21, 2023
17cab87
added marshaling to go side, and token examples work(hype)
samliok Nov 21, 2023
a9993af
letsgoo, finally passing dynamic vecs working(needs clean up(from deb…
samliok Nov 21, 2023
b487e6a
cleanup logs
samliok Nov 21, 2023
a483cf5
combine HostArg + Arg
samliok Nov 21, 2023
1793386
vec<u8> -> &[u8]
samliok Nov 21, 2023
d97f278
avoid reallocation in as_bytes
samliok Nov 21, 2023
96190d4
temporary string example/method
samliok Nov 21, 2023
077d36b
remove VecArg junk
samliok Nov 21, 2023
ffa1dd9
cargo fmt
samliok Nov 21, 2023
21f1d81
lint
samliok Nov 21, 2023
67b69c4
flatten out tuple
samliok Nov 22, 2023
378d578
simplify ParamKind logic
samliok Nov 22, 2023
dd66f33
borsh serialization working(go/rust)
samliok Nov 22, 2023
6c45d95
move all from serde -> borsh(except simulator)
samliok Nov 22, 2023
033004a
call_program function
samliok Nov 22, 2023
4c15795
re added arg types, going to address call_program in another pr
samliok Nov 22, 2023
8dd98dc
unsafe + temp removal
samliok Nov 22, 2023
754c7bc
lint
samliok Nov 22, 2023
7c0ca41
go lint
samliok Nov 22, 2023
1e8fd38
mint recipient struct
samliok Nov 23, 2023
d97a849
mint recipient struct
samliok Nov 23, 2023
912ecc1
Update x/programs/rust/wasmlanche_sdk/src/types.rs
samliok Nov 23, 2023
0944839
lint
samliok Nov 23, 2023
c272666
remove temp go stuff
samliok Nov 24, 2023
813ffdc
tidy
samliok Nov 28, 2023
3d61aee
remove comment
samliok Nov 28, 2023
2f0090e
decompose newPtr method + nits
samliok Nov 30, 2023
ede907a
remove fixedLengthKey method
samliok Nov 30, 2023
bc9fa1a
refactor host calls
samliok Dec 1, 2023
968661c
return result in from_raw_ptr
samliok Dec 2, 2023
ef4cb9d
merge main
samliok Dec 2, 2023
a4f4a15
conflicts from merge
samliok Dec 2, 2023
236e963
represent pointers as *const u8
samliok Dec 2, 2023
2d240c8
comments
samliok Dec 2, 2023
cdd09a3
remove import
samliok Dec 2, 2023
4449ad7
remove len
samliok Dec 2, 2023
ed5ab6a
rust lint
samliok Dec 2, 2023
f207a5f
lfg some progress on serialize_param macro
samliok Dec 3, 2023
1ee07b1
why are we not putting units in second runtime?
samliok Dec 3, 2023
e1c319a
comments
samliok Dec 3, 2023
6c2cde5
counter test cleanup
samliok Dec 3, 2023
5d29b27
comment cleanup
samliok Dec 3, 2023
7db59e5
nits
samliok Dec 3, 2023
d19f883
program struct refactor
samliok Dec 4, 2023
ef3d0cc
yesss, external function calls working(needs cleanup)
samliok Dec 4, 2023
da30514
cleanup
samliok Dec 5, 2023
2f9a228
cargo fmt
samliok Dec 5, 2023
c879e1c
Fix token benchmark
hexfusion Dec 5, 2023
bc6741e
nits + Deref on Key struct
samliok Dec 6, 2023
b6facbc
bitwise operations on ptrs passing to the host
samliok Dec 8, 2023
cbe1000
Merge remote-tracking branch 'origin/refactor-statekey' into refactor…
samliok Dec 8, 2023
5ea23bb
bitwise operations working pasing parameters into wasm
samliok Dec 8, 2023
c4efc96
full circle -> external function calls(needs cleanup)
samliok Dec 8, 2023
caab4f3
ptr cleanup
samliok Dec 8, 2023
5595b01
comments and nit go cleanup
samliok Dec 8, 2023
245b06c
updated call_program host function
samliok Dec 9, 2023
764077d
SmartPtr type & cleanup utils
samliok Dec 10, 2023
cc96b79
go lint
samliok Dec 10, 2023
a17803d
rust lint
samliok Dec 10, 2023
158bfd2
update program impl
samliok Dec 10, 2023
2bd46d4
return result from rust side host function wrappers
samliok Dec 10, 2023
ee4507d
small rename
samliok Dec 10, 2023
2ab2bb1
rust lint
samliok Dec 10, 2023
f991a95
into_bytes function
samliok Dec 11, 2023
866febf
smart ptr method refactor
samliok Dec 11, 2023
f2e9f72
comments
samliok Dec 11, 2023
7cb99c4
int64 conversionn
samliok Dec 11, 2023
40f45d0
Merge branch 'main' into refactor-statekey
hexfusion Dec 12, 2023
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
Prev Previous commit
Next Next commit
cargo fmt
  • Loading branch information
samliok committed Nov 21, 2023
commit ffa1dd9a1ddbb8e1971b8d32116b83ea2d65c468
2 changes: 1 addition & 1 deletion x/programs/rust/examples/counter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use wasmlanche_sdk::{program::Program, public, state_keys, types::Address, state::from_raw_ptr};
use wasmlanche_sdk::{program::Program, public, state::from_raw_ptr, state_keys, types::Address};

#[state_keys]
enum StateKeys {
Expand Down
5 changes: 5 additions & 0 deletions x/programs/rust/sdk_macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ edition = "2021"
[lib]
proc-macro = true



[dependencies]
proc-macro2 = "1.0.67"
quote = "1.0.33"
syn = { version = "2.0.37", features = ["full"] }

[dev-dependencies]
wasmlanche_sdk = { version = "0.1.0", path = "../wasmlanche_sdk" }
20 changes: 10 additions & 10 deletions x/programs/rust/sdk_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ enum ParamKind {
Pointer,
}


/// An attribute procedural macro that makes a function visible to the VM host.
/// It does so by wrapping the `item` tokenstream in a new function that can be called by the host.
/// The wrapper function will have the same name as the original function, but with "_guest" appended to it.
Expand Down Expand Up @@ -59,10 +58,12 @@ pub fn public(_: TokenStream, item: TokenStream) -> TokenStream {
if is_context(ty) {
return (
&empty_param,
(parse_str::<Type>("i64")
.expect("valid i64 type")
.to_token_stream(),
ParamKind::Program),
(
parse_str::<Type>("i64")
.expect("valid i64 type")
.to_token_stream(),
ParamKind::Program,
),
);
} else {
panic!("Unused variables only supported for Program.")
Expand All @@ -72,7 +73,7 @@ pub fn public(_: TokenStream, item: TokenStream) -> TokenStream {
panic!("Unsupported function parameter format.");
});

// Converts the parameters that are pointers to their original type.
// Converts the parameters that are pointers to their original type.
let converted_params = full_params.clone().map(|param| {
let (param_name, param_type) = param;
let (_, param_descriptor) = param_type;
Expand All @@ -89,19 +90,18 @@ pub fn public(_: TokenStream, item: TokenStream) -> TokenStream {
#param_name.into()
};
}
// only convert from_raw_ptr if not a supported primitive type or Program
ParamKind::Pointer => {
// only convert from_raw_ptr if not a supported primitive type or Program
ParamKind::Pointer => {
return quote! {
from_raw_ptr(#param_name)
};
}
}
});

// Collect all parameter names and types into separate vectors.
let (param_names, param_types): (Vec<_>, Vec<_>) = full_params.unzip();
let param_types = param_types.iter().map(|(param_type, _)| param_type);


// Extract the original function's return type. This must be a WASM supported type.
let return_type = &input.sig.output;
Expand Down
13 changes: 9 additions & 4 deletions x/programs/rust/wasmlanche_sdk/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use serde_bare::{from_slice, to_vec};
use crate::{
errors::StateError,
host::{get_bytes, len_bytes, put_bytes},
program::Program, types::Argument,
program::Program,
types::Argument,
};

pub struct State {
Expand Down Expand Up @@ -84,24 +85,28 @@ impl State {
// pub fn from_raw_ptr<V>(ptr: i64) -> V
// where
// V: serde::de::DeserializeOwned,
// {
// {
// let (bytes, _) = bytes_and_length(ptr);
// from_slice(&bytes).expect("failed to deserialize")
// }

// Converts a raw pointer to a type that implements the Argument trait.
// Expects the first 4 bytes of the pointer to represent the [length] of the serialized value,
// with the subsequent [length] bytes comprising the serialized data.
pub fn from_raw_ptr<V>(ptr: i64) -> V
where
V: Argument,
{
{
let (bytes, _) = bytes_and_length(ptr);
V::from_bytes(&bytes)
}

#[no_mangle]
// TODO: move this logic to return a Memory struct that conatins ptr + length
/// Returns a tuple of the bytes and length of the argument.
pub fn bytes_and_length(ptr: i64) -> (Vec<u8>, i32) {
let len = unsafe { std::slice::from_raw_parts(ptr as *const u8, 4) };
let len = u32::from_be_bytes(len.try_into().unwrap()) as usize;
let value = unsafe { std::slice::from_raw_parts(ptr as *const u8, len + 4) };
(value[4..].to_vec(), len.try_into().unwrap())
}
}
13 changes: 6 additions & 7 deletions x/programs/rust/wasmlanche_sdk/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ impl Bytes32 {
// pub trait HasLen {
// fn len(&self) -> usize;
// }
// impl<T> From<i64> for VecArg<T>
// impl<T> From<i64> for VecArg<T>
// where T: From<i64> {
// fn from(value: i64) -> Self {
// let (bytes, len) = bytes_and_length(value);
Expand All @@ -68,7 +68,6 @@ impl Bytes32 {
// }
// }


/// Implement the Display trait for Bytes32 so that we can print it.
/// Enables `to_string()` on Bytes32.
impl std::fmt::Display for Bytes32 {
Expand Down Expand Up @@ -121,8 +120,9 @@ impl Argument for Bytes32 {
Cow::Borrowed(&self.0)
}
fn from_bytes(bytes: &[u8]) -> Self
where
Self: Sized {
where
Self: Sized,
{
let bytes = bytes[..ADDRESS_LEN].try_into().unwrap();
Self::new(bytes)
}
Expand Down Expand Up @@ -152,7 +152,6 @@ impl Argument for i64 {
}
}


impl Argument for i32 {
fn as_bytes(&self) -> Cow<'_, [u8]> {
Cow::Owned(self.to_be_bytes().to_vec())
Expand All @@ -174,7 +173,7 @@ impl Argument for Program {
true
}
fn from_bytes(bytes: &[u8]) -> Self {
let id : i64 = i64::from_bytes(bytes);
let id: i64 = i64::from_bytes(bytes);
Self::from(id)
}
}
Expand Down Expand Up @@ -204,7 +203,7 @@ where
// TODO: check logic on empty vec
while current_byte < num_bytes {
// Convert the bytes to the type T
let elem : T = T::from_bytes(&bytes[current_byte..]);
let elem: T = T::from_bytes(&bytes[current_byte..]);
current_byte += elem.len();
// Add the element to the result vec
result_vec.push(elem);
Expand Down
Loading