-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
cozfuttu
wants to merge
4
commits into
unionlabs:main
Choose a base branch
from
Fluton-io:slot-type-lib
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
923b686
feat: move Slot type into it's own library
cozfuttu 6d1336b
Merge branch 'main' into slot-type-lib
cozfuttu dfd7a74
Merge branch 'unionlabs:main' into slot-type-lib
cozfuttu 62ba469
feat: added array support and used syn_solidity for parsing
cozfuttu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod slot; | ||
|
||
pub use crate::slot::{MappingKey, Slot}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
)) | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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