-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: create common types (trie key, value, stem)
- Loading branch information
Showing
5 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
// TODO: revisit visibility | ||
pub mod constants; | ||
pub mod msm; | ||
pub mod types; | ||
pub mod utils; |
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,7 @@ | ||
mod stem; | ||
mod trie_key; | ||
mod trie_value; | ||
|
||
pub use stem::Stem; | ||
pub use trie_key::TrieKey; | ||
pub use trie_value::TrieValue; |
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,63 @@ | ||
use derive_more::{AsRef, Deref, From, Index}; | ||
use ssz::{Decode, Encode}; | ||
|
||
use super::trie_key::TrieKey; | ||
|
||
#[derive(PartialEq, Eq, AsRef, Deref, From, Index)] | ||
pub struct Stem([u8; Self::STEM_LENGTH]); | ||
|
||
impl Stem { | ||
pub const STEM_LENGTH: usize = 31; | ||
} | ||
|
||
impl From<&TrieKey> for Stem { | ||
fn from(key: &TrieKey) -> Self { | ||
let mut stem = [0u8; Self::STEM_LENGTH]; | ||
stem.copy_from_slice(&key[..Self::STEM_LENGTH]); | ||
Stem(stem) | ||
} | ||
} | ||
|
||
impl From<TrieKey> for Stem { | ||
fn from(key: TrieKey) -> Self { | ||
Self::from(&key) | ||
} | ||
} | ||
|
||
impl Encode for Stem { | ||
fn is_ssz_fixed_len() -> bool { | ||
true | ||
} | ||
|
||
fn ssz_fixed_len() -> usize { | ||
Self::STEM_LENGTH | ||
} | ||
|
||
fn ssz_append(&self, buf: &mut Vec<u8>) { | ||
buf.extend(self.as_slice()) | ||
} | ||
|
||
fn ssz_bytes_len(&self) -> usize { | ||
self.len() | ||
} | ||
} | ||
|
||
impl Decode for Stem { | ||
fn is_ssz_fixed_len() -> bool { | ||
true | ||
} | ||
|
||
fn ssz_fixed_len() -> usize { | ||
Self::STEM_LENGTH | ||
} | ||
|
||
fn from_ssz_bytes(bytes: &[u8]) -> Result<Self, ssz::DecodeError> { | ||
match <[u8; 31]>::try_from(bytes) { | ||
Ok(stem) => Ok(Self(stem)), | ||
Err(_) => Err(ssz::DecodeError::InvalidByteLength { | ||
len: bytes.len(), | ||
expected: Self::STEM_LENGTH, | ||
}), | ||
} | ||
} | ||
} |
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,27 @@ | ||
use alloy_primitives::B256; | ||
use derive_more::{Constructor, Deref, From, Index}; | ||
|
||
use super::stem::Stem; | ||
|
||
#[derive(PartialEq, Eq, Clone, Copy, Constructor, Index, Deref, From)] | ||
pub struct TrieKey(B256); | ||
|
||
impl TrieKey { | ||
pub fn from_stem_and_last_byte(stem: &Stem, suffix: u8) -> Self { | ||
let mut key = B256::right_padding_from(stem.as_slice()); | ||
key[B256::len_bytes() - 1] = suffix; | ||
key.into() | ||
} | ||
|
||
pub fn len_bytes() -> usize { | ||
B256::len_bytes() | ||
} | ||
|
||
pub fn stem(&self) -> Stem { | ||
self.into() | ||
} | ||
|
||
pub fn suffix(&self) -> u8 { | ||
self[self.len() - 1] | ||
} | ||
} |
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,5 @@ | ||
use alloy_primitives::B256; | ||
use derive_more::{Constructor, Deref, From, Index}; | ||
|
||
#[derive(PartialEq, Eq, Clone, Copy, Constructor, Index, Deref, From)] | ||
pub struct TrieValue(B256); |