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 1 commit
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
27 changes: 27 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 }
sha2 = { workspace = true }
sha3 = { workspace = true }
slotlib = { workspace = true }
typed-arena = "2.0"
unionlabs = { workspace = true, features = ["rlp"] }
Loading