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

Fix state-caching #916

Merged
merged 1 commit into from
May 22, 2024
Merged

Fix state-caching #916

merged 1 commit into from
May 22, 2024

Conversation

richardpringle
Copy link
Contributor

State-caching appears to have always been a little broken. While fixing this, I also realized that the state_key macro was doing a bunch of things that didn't make any sense, so I'm also fixing that since I have to change it anyway.

@richardpringle richardpringle force-pushed the fix-state branch 2 times, most recently from ec25ebc to 4bb14da Compare May 17, 2024 16:38
@richardpringle richardpringle marked this pull request as ready for review May 17, 2024 19:24
iFrostizz
iFrostizz previously approved these changes May 20, 2024
x/programs/rust/sdk-macros/src/lib.rs Outdated Show resolved Hide resolved
Comment on lines 232 to 233
Fields::Named(_) => quote! {
Self::#variant_ident { .. } => panic!("named enum fields are not supported")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be turned into a compile-time error instead of a runtime one for more safety

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Woooooow, I was just moving this, I didn't change it. You're absolutely correct, good catch!

context_type.unwrap().arguments = segment.arguments.clone();
}

type_path.path.segments.last() == context_type.segments.last()
} else {
false
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about writing a few unit tests here ?

Suggested change
}
}
#[cfg(test)]
mod tests {
use crate::is_context;
#[test]
fn not_a_context() {
assert!(!is_context(&Box::new(
syn::parse_str::<syn::Type>("std::u8").unwrap()
)));
}
#[test]
fn context_short() {
assert!(is_context(&Box::new(
syn::parse_str::<syn::Type>("Context").unwrap()
)));
}
#[test]
fn context_full() {
assert!(is_context(&Box::new(
syn::parse_str::<syn::Type>("wasmlanche_sdk::Context").unwrap()
)));
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would approve a PR with this haha. I don't quite think it's necessary right now because the ui tests cover the cases pretty well, but more unit tests usually make things easier to expand and extend.

{
cache: HashMap<K, Vec<u8>>,
pub struct State<'a, K: Key> {
cache: &'a RefCell<HashMap<K, Vec<u8>>>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be a bit more restrictive about https://github.com/ava-labs/hypersdk/blob/main/x/programs/v2/config.go#L161 because our cache which is now a RefCell is not thread safe ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If programs were multi-threaded, I think it would be a lot harder to guarantee determinism. I don't think it's something we're going to have to worry about in the near future.

Also, RefCell isn't Sync, you can still send it across threads
https://doc.rust-lang.org/stable/std/cell/struct.RefCell.html#impl-Send-for-RefCell%3CT%3E

@dboehm-avalabs
Copy link
Contributor

Is splitting the functions into "func" and "func_internal" needed? It seems a little messy, so it would be nice if that weren't necessary.

@richardpringle
Copy link
Contributor Author

Is splitting the functions into "func" and "func_internal" needed? It seems a little messy, so it would be nice if that weren't necessary.

TL;DR Response:
I agree, but there's some functionality I want to get in before dealing with the cosmetics.

I agree it's a little messy for now. I want to go in and generate the code for program-to-program calls (#723) then and maybe get #595 in as well, then take a look at the API and calling conventions and see what needs cleaning up.

I have a couple of different ideas on how to deal with that func_internal, but I don't think they are worth changing now in case other API changes are necessary for functionality.

}

fn mint_to_internal(
context: Context<StateKeys>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it would make more sense to pass the context by reference to internal functions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I agree. This isn't going to stay like this though.
See the comment above

Before the state-cache was created every time you called
`Program::state`, but that means that it's not persisted for the
lifetime of `Program` (and therefore we don't actually get any cacheing
behaviour).

I've fixed the lifetime issues, but this design still needs work.
@richardpringle richardpringle merged commit 3fb35af into main May 22, 2024
26 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

3 participants