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
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
181 changes: 104 additions & 77 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ members = [

"tools/devnet-utils",
"tools/parse-wasm-client-type",
"tools/slot-calculator",
"tools/tidy",
"tools/move-bindgen",
"lib/move-bindgen-derive",
Expand Down Expand Up @@ -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

]

[workspace.package]
Expand Down Expand Up @@ -264,6 +266,7 @@ movement-light-client-types = { path = "lib/movement-light-client-types", defaul
cosmos-sdk-event = { path = "lib/cosmos-sdk-event", default-features = false }

serde-utils = { path = "lib/serde-utils", default-features = false }
slotlib = { path = "lib/slotlib", default-features = false }
ssz = { path = "lib/ssz", default-features = false }
ssz-derive = { path = "lib/ssz-derive", default-features = false }
subset-of = { path = "lib/subset-of", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions lib/arbitrum-verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ arbitrum-light-client-types = { workspace = true }
evm-storage-verifier = { workspace = true }
rlp = { workspace = true }
sha3 = { workspace = true }
slotlib = { workspace = true }
thiserror = { workspace = true }
unionlabs = { workspace = true }

Expand Down
6 changes: 2 additions & 4 deletions lib/arbitrum-verifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ use core::fmt::Debug;
use arbitrum_light_client_types::{ClientState, Header};
use evm_storage_verifier::{verify_account_storage_root, verify_storage_proof};
use sha3::{Digest, Keccak256};
use unionlabs::{
ethereum::slot::{MappingKey, Slot},
primitives::{H256, U256},
};
use slotlib::{MappingKey, Slot};
use unionlabs::primitives::{H256, U256};

#[derive(thiserror::Error, Debug, PartialEq, Clone)]
pub enum Error {
Expand Down
1 change: 1 addition & 0 deletions lib/linea-verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ linea-light-client-types = { workspace = true }
linea-types = { workspace = true }
linea-zktrie = { workspace = true }
rlp = { workspace = true }
slotlib = { workspace = true }
thiserror = { workspace = true }
unionlabs = { workspace = true }
6 changes: 2 additions & 4 deletions lib/linea-verifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ use evm_storage_verifier::{verify_account_storage_root, verify_storage_proof};
use gnark_mimc::new_mimc_constants_bls12_377;
use linea_light_client_types::{ClientState, Header};
use linea_types::account::ZkAccount;
use unionlabs::{
ethereum::slot::{MappingKey, Slot},
primitives::{H256, U256},
};
use slotlib::{MappingKey, Slot};
use unionlabs::primitives::{H256, U256};

#[derive(Debug, Clone, PartialEq, thiserror::Error)]
pub enum Error {
Expand Down
1 change: 1 addition & 0 deletions lib/scroll-verifier/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ evm-storage-verifier = { workspace = true }
rlp = { workspace = true }
scroll-codec = { workspace = true }
scroll-light-client-types = { workspace = true }
slotlib = { workspace = true }
thiserror = { workspace = true }
unionlabs = { workspace = true }
zktrie = { workspace = true }
2 changes: 1 addition & 1 deletion lib/scroll-verifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use core::fmt::Debug;
use evm_storage_verifier::{verify_account_storage_root, verify_storage_proof};
use scroll_codec::{hash_batch, HashBatchError};
use scroll_light_client_types::{ClientState, Header};
use slotlib::{MappingKey, Slot};
use unionlabs::{
ethereum::slot::{MappingKey, Slot},
primitives::{H160, H256, U256},
scroll::account::Account,
};
Expand Down
15 changes: 15 additions & 0 deletions lib/slotlib/Cargo.toml
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
3 changes: 3 additions & 0 deletions lib/slotlib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod slot;

pub use crate::slot::{MappingKey, Slot};
85 changes: 85 additions & 0 deletions lib/slotlib/src/slot.rs
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"
))
);
}
1 change: 1 addition & 0 deletions lib/unionlabs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ near-primitives-core = { version = "0.21", optional = true }
near-sdk = { workspace = true, optional = true }
schemars = { workspace = true, features = ["derive"], optional = true }
serde_bytes = "0.11.6"
slotlib = { workspace = true }
unionlabs-primitives = { workspace = true, features = ["generic-array-compat", "serde", "base64"] }

[dev-dependencies]
Expand Down
6 changes: 2 additions & 4 deletions lib/unionlabs/src/ethereum.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use sha2::Digest;
use sha3::Keccak256;
use slotlib::{MappingKey, Slot};

use crate::{
ethereum::slot::{MappingKey, Slot},
primitives::{H256, U256},
};
use crate::primitives::{H256, U256};

pub mod slot;

Expand Down
19 changes: 19 additions & 0 deletions tools/slot-calculator/Cargo.toml
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"] }
152 changes: 152 additions & 0 deletions tools/slot-calculator/src/main.rs
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
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

}

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

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?

)),
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))),
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?

};
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")),
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

)),
_ => 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
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

slot.slot()
}

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

.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"
))
);
}