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

feat: move Slot type into it's own library #3639

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

cozfuttu
Copy link

Closes #3417

Created a small binary for slot calculation as well. Usage:
cargo run --bin slot-calculator --layout "mapping(uint256 => mapping(uint256 => uint256)[])" --keys 100 1 123

Copy link

vercel bot commented Jan 24, 2025

@cozfuttu is attempting to deploy a commit to the unionbuild Team on Vercel.

A member of the Team first needs to authorize it.

tools/slot-calculator/src/main.rs Outdated Show resolved Hide resolved
tools/slot-calculator/src/main.rs Outdated Show resolved Hide resolved
@@ -176,6 +177,7 @@ members = [
"lib/state-lens-light-client-types",
"lib/create3",
"lib/linea-types",
"lib/slotlib",
Copy link
Contributor

Choose a reason for hiding this comment

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

another small comment: please call this library unionlabs-slot

fn parse_mapping_key<'a>(key_type: &'a Type, key: &'a str) -> MappingKey<'a> {
match key_type {
Type::Uint(_, size) => {
let size = size.and_then(|s| Some(s.get())).unwrap_or(256);
Copy link
Contributor

Choose a reason for hiding this comment

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

instead of .and_then with Some(), just use .map

let size = size.and_then(|s| Some(s.get())).unwrap_or(256);
match size {
256 => MappingKey::Uint256(U256::from(
key.parse::<u64>().expect("Invalid uint256 key"),
Copy link
Contributor

Choose a reason for hiding this comment

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

parsing as u64 when this is a u256?

}

fn main() {
let matches = Command::new("Slot Calculator (post-order)")
Copy link
Contributor

Choose a reason for hiding this comment

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

use clap derive instead of the builder api

Comment on lines +28 to +35
match &parsed_layout.items[0] {
Item::Variable(var) => match &var.ty {
Type::Mapping(map) => Ok(Type::Mapping(map.clone())),
Type::Array(arr) => Ok(Type::Array(arr.clone())),
_ => return Err("Unsupported type".to_string()),
},
_ => return Err("Unsupported item".to_string()),
}
Copy link
Contributor

Choose a reason for hiding this comment

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

why not just parse directly into Type?

Copy link
Contributor

Choose a reason for hiding this comment

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

then you could skip the visibility & name logic entirely

)),
Type::Array(arr) => arena.alloc(Slot::Array(
build_slot(arr.ty.as_ref(), keys, arena),
U256::from(key.parse::<u64>().expect("Invalid array index")),
Copy link
Contributor

Choose a reason for hiding this comment

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

parse as u256

) -> &'a Slot<'a> {
let key: &str = match keys.pop_front() {
Some(k) => k,
None => return arena.alloc(Slot::Offset(U256::from(0u32))),
Copy link
Contributor

Choose a reason for hiding this comment

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

what's the reasoning behind defaulting to a 0 slot here?

Comment on lines +90 to +95
if split_keys.len() != 0 {
eprintln!(
"Warning: Unused keys: {:?}. The calculated slot might be wrong. Please check the layout and keys you provided.",
split_keys
);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

this should be a hard error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move Slot type into it's own library
3 participants