Skip to content

Commit

Permalink
feat: create common types (trie key, value, stem)
Browse files Browse the repository at this point in the history
  • Loading branch information
morph-dev committed May 22, 2024
1 parent cb16735 commit 191c815
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions portal-verkle-trie/src/lib.rs
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;
7 changes: 7 additions & 0 deletions portal-verkle-trie/src/types/mod.rs
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;
63 changes: 63 additions & 0 deletions portal-verkle-trie/src/types/stem.rs
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,
}),
}
}
}
27 changes: 27 additions & 0 deletions portal-verkle-trie/src/types/trie_key.rs
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]
}
}
5 changes: 5 additions & 0 deletions portal-verkle-trie/src/types/trie_value.rs
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);

0 comments on commit 191c815

Please sign in to comment.