-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
base: main
Are you sure you want to change the base?
Changes from all commits
923b686
6d1336b
dfd7a74
62ba469
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
edition.workspace = true | ||
license-file.workspace = true | ||
name = "slotlib" | ||
repository.workspace = true | ||
version = "0.1.0" | ||
|
||
[dependencies] | ||
hex-literal = { workspace = true } | ||
sha2 = { workspace = true } | ||
sha3 = { workspace = true } | ||
unionlabs-primitives = { workspace = true } | ||
|
||
[lints] | ||
workspace = true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod slot; | ||
|
||
pub use crate::slot::{MappingKey, Slot}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
use sha2::Digest; | ||
use sha3::Keccak256; | ||
use unionlabs_primitives::{H256, U256}; | ||
|
||
// Duplication of keccak256 function defined in /lib/unionlabs/ethereum.rs, otherwise it creates a dependency cycle. | ||
#[inline] | ||
#[must_use] | ||
pub fn keccak256(bytes: impl AsRef<[u8]>) -> H256 { | ||
Keccak256::new().chain_update(bytes).finalize().into() | ||
} | ||
|
||
/// Solidity storage slot calculations. Note that this currently does not handle dynamic arrays with packed values; the index passed to [`Slot::Array`] will need to be calculated manually in this case. | ||
pub enum Slot<'a> { | ||
/// (base slot, index) | ||
Array(&'a Slot<'a>, U256), | ||
/// (base slot, mapping key) | ||
Mapping(&'a Slot<'a>, MappingKey<'a>), | ||
Offset(U256), | ||
} | ||
|
||
impl Slot<'_> { | ||
// https://docs.soliditylang.org/en/latest/internals/layout_in_storage.html#mappings-and-dynamic-arrays | ||
#[inline] | ||
#[must_use = "calculating the slot has no effect"] | ||
// REVIEW: Make const? <https://crates.io/crates/keccak-const/0.2.0> | ||
pub fn slot(&self) -> U256 { | ||
match self { | ||
// keccak256(p) | ||
Slot::Array(p, idx) => { | ||
U256::from_be_bytes(*keccak256(p.slot().to_be_bytes()).get()) + *idx | ||
} | ||
// keccak256(h(k) . p) | ||
Slot::Mapping(p, k) => { | ||
let mut hasher = Keccak256::new(); | ||
match &k { | ||
MappingKey::String(string) => hasher.update(string.as_bytes()), | ||
MappingKey::Uint256(k) => hasher.update(k.to_be_bytes()), | ||
MappingKey::Uint64(k) => hasher.update(U256::from(*k).to_be_bytes()), | ||
MappingKey::Bytes32(k) => hasher.update(k.get()), | ||
}; | ||
|
||
U256::from_be_bytes( | ||
hasher | ||
.chain_update(p.slot().to_be_bytes()) | ||
.finalize() | ||
.into(), | ||
) | ||
} | ||
Slot::Offset(p) => *p, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum MappingKey<'a> { | ||
String(&'a str), | ||
Uint256(U256), | ||
Uint64(u64), | ||
Bytes32(H256), | ||
} | ||
|
||
#[test] | ||
fn test() { | ||
// Test contract uploaded here: https://sepolia.etherscan.io/address/0x6845dbaa9513d3d07737ea9f6e350011dcfeb9bd | ||
|
||
// mapping(uint256 => mapping(uint256 => uint256)[]) | ||
let slot = Slot::Mapping( | ||
&Slot::Array( | ||
&Slot::Mapping( | ||
&Slot::Offset(0u32.into()), | ||
MappingKey::Uint256(123u32.into()), | ||
), | ||
1u32.into(), | ||
), | ||
MappingKey::Uint256(100u32.into()), | ||
) | ||
.slot(); | ||
|
||
assert_eq!( | ||
<H256>::new(slot.to_be_bytes()), | ||
<H256>::new(hex_literal::hex!( | ||
"00a9b48fe93e5d10ebc2d9021d1477088c6292bf047876944343f57fdf3f0467" | ||
)) | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
edition = { workspace = true } | ||
license-file = { workspace = true } | ||
name = "slot-calculator" | ||
publish = false | ||
repository = { workspace = true } | ||
version = "0.1.0" | ||
|
||
[lints] | ||
workspace = true | ||
|
||
[dependencies] | ||
clap = { workspace = true, features = ["derive", "help"] } | ||
hex-literal = { workspace = true } | ||
proc-macro2 = "1.0.93" | ||
slotlib = { workspace = true } | ||
syn-solidity = "0.8.19" | ||
typed-arena = "2.0" | ||
unionlabs = { workspace = true, features = ["rlp"] } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
use clap::{Arg, Command}; | ||
use proc_macro2::TokenStream; | ||
use slotlib::{MappingKey, Slot}; | ||
use std::{collections::VecDeque, str::FromStr}; | ||
use syn_solidity::{parse2, Item, Type}; | ||
use typed_arena::Arena; | ||
use unionlabs::primitives::{H256, U256}; | ||
|
||
// Examples: | ||
// mapping(uint256 => uint256) | ||
// uint256[] => ["uint256[]"] | ||
// mapping(uint256 => uint256[]) | ||
// mapping(uint256 => mapping(uint256 => uint256)) | ||
// mapping(uint256 => mapping(uint256 => mapping(uint256 => uint256)[])[]) | ||
// mapping(uint256 => mapping(uint256 => mapping(uint256 => mapping(uint256 => uint256)[]))[]) | ||
// mapping(uint256 => mapping(uint256 => uint256)[]) | ||
fn parse_layout(layout: &mut String) -> Result<Type, String> { | ||
// Check if the layout already includes a visibility modifier and a variable name | ||
if !layout.contains("public") && !layout.contains("internal") && !layout.contains("private") { | ||
layout.push_str(" public dummyName;"); | ||
} | ||
|
||
let parsed_layout = layout | ||
.parse::<TokenStream>() | ||
.map_err(|_| "Failed to parse layout".to_string())?; | ||
let parsed_layout = parse2(parsed_layout).map_err(|e| e.to_string())?; | ||
|
||
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()), | ||
} | ||
Comment on lines
+28
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not just parse directly into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. then you could skip the visibility & name logic entirely |
||
} | ||
|
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. instead of .and_then with Some(), just use .map |
||
match size { | ||
256 => MappingKey::Uint256(U256::from( | ||
key.parse::<u64>().expect("Invalid uint256 key"), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. parsing as u64 when this is a u256? |
||
)), | ||
64 => MappingKey::Uint64(key.parse::<u64>().expect("Invalid uint64 key")), | ||
_ => panic!("Unsupported uint size: {}", size), | ||
} | ||
} | ||
Type::FixedBytes(_, size) => { | ||
let size = size.get(); | ||
match size { | ||
32 => MappingKey::Bytes32(H256::from_str(key).expect("Invalid bytes32 key")), | ||
_ => panic!("Unsupported bytes size: {}", size), | ||
} | ||
} | ||
Type::String(_) => MappingKey::String(key), | ||
_ => panic!("Unsupported mapping key type"), | ||
} | ||
} | ||
|
||
fn build_slot<'a>( | ||
parsed_layout: &'a Type, | ||
keys: &mut VecDeque<&'a str>, | ||
arena: &'a Arena<Slot<'a>>, | ||
) -> &'a Slot<'a> { | ||
let key: &str = match keys.pop_front() { | ||
Some(k) => k, | ||
None => return arena.alloc(Slot::Offset(U256::from(0u32))), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the reasoning behind defaulting to a 0 slot here? |
||
}; | ||
match parsed_layout { | ||
Type::Mapping(mapping) => arena.alloc(Slot::Mapping( | ||
build_slot(mapping.value.as_ref(), keys, arena), | ||
parse_mapping_key(mapping.key.as_ref(), key), | ||
)), | ||
Type::Array(arr) => arena.alloc(Slot::Array( | ||
build_slot(arr.ty.as_ref(), keys, arena), | ||
U256::from(key.parse::<u64>().expect("Invalid array index")), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. parse as u256 |
||
)), | ||
_ => panic!("Unsupported layout type or wrong key count."), | ||
} | ||
} | ||
|
||
fn calculate_slot(layout: &str, keys: &str) -> U256 { | ||
let parsed_layout = parse_layout(&mut layout.to_string()).unwrap(); | ||
let mut split_keys: VecDeque<&str> = keys.split(" ").collect(); | ||
|
||
let arena = Arena::new(); | ||
let slot = build_slot(&parsed_layout, &mut split_keys, &arena); | ||
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 | ||
); | ||
} | ||
Comment on lines
+90
to
+95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be a hard error |
||
slot.slot() | ||
} | ||
|
||
fn main() { | ||
let matches = Command::new("Slot Calculator (post-order)") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use clap derive instead of the builder api |
||
.version("1.0") | ||
.about("Calculates Solidity storage slots for various layouts in a post-order manner") | ||
.arg( | ||
Arg::new("layout") | ||
.long("layout") | ||
.value_name("LAYOUT") | ||
.required(true) | ||
.help("e.g. 'mapping(uint256 => mapping(uint256 => uint256)[])'"), | ||
) | ||
.arg( | ||
Arg::new("keys") | ||
.long("keys") | ||
.value_name("KEYS") | ||
.required(true) | ||
.num_args(1..) // Accept one or more | ||
.help("The keys in the order that matches your snippet, e.g. 123 1 100"), | ||
) | ||
.get_matches(); | ||
|
||
let layout = matches | ||
.get_one::<String>("layout") | ||
.expect("Missing --layout"); | ||
let keys_collected: Vec<String> = matches | ||
.get_many::<String>("keys") | ||
.expect("Missing --keys") | ||
.map(|s| s.to_string()) | ||
.collect(); | ||
|
||
let keys_str = keys_collected.join(" "); | ||
|
||
let slot_hex = calculate_slot(layout, &keys_str); | ||
|
||
println!( | ||
"Calculated storage slot: {}", | ||
<H256>::new(slot_hex.to_be_bytes()) | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_calculate_slot() { | ||
let layout = "mapping(uint256 => mapping(uint256 => uint256)[]) public test;"; | ||
let keys = "100 1 123"; | ||
|
||
let slot = calculate_slot(layout, keys); | ||
|
||
assert_eq!( | ||
<H256>::new(slot.to_be_bytes()), | ||
<H256>::new(hex_literal::hex!( | ||
"00a9b48fe93e5d10ebc2d9021d1477088c6292bf047876944343f57fdf3f0467" | ||
)) | ||
); | ||
} |
There was a problem hiding this comment.
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