Skip to content

Commit

Permalink
skip the first 4 bits
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Oct 31, 2024
1 parent cbffb81 commit 070711c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
35 changes: 32 additions & 3 deletions crates/katana/storage/db/src/models/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ use crate::codecs::{Decode, Encode};
use crate::error::CodecError;

#[repr(u8)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub enum TrieDatabaseKeyType {
Trie,
Trie = 0,
Flat,
TrieLog,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct TrieDatabaseKey {
pub r#type: TrieDatabaseKeyType,
pub key: Vec<u8>,
Expand Down Expand Up @@ -50,3 +50,32 @@ impl Decode for TrieDatabaseKey {
Ok(TrieDatabaseKey { r#type, key })
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_trie_key_roundtrip() {
let key = TrieDatabaseKey { r#type: TrieDatabaseKeyType::Trie, key: vec![1, 2, 3] };
let encoded = key.clone().encode();
let decoded = TrieDatabaseKey::decode(encoded).unwrap();
assert_eq!(key, decoded);
}

#[test]
fn test_flat_key_roundtrip() {
let key = TrieDatabaseKey { r#type: TrieDatabaseKeyType::Flat, key: vec![4, 5, 6] };
let encoded = key.clone().encode();
let decoded = TrieDatabaseKey::decode(encoded).unwrap();
assert_eq!(key, decoded);
}

#[test]
fn test_trielog_key_roundtrip() {
let key = TrieDatabaseKey { r#type: TrieDatabaseKeyType::TrieLog, key: vec![7, 8, 9] };
let encoded = key.clone().encode();
let decoded = TrieDatabaseKey::decode(encoded).unwrap();
assert_eq!(key, decoded);
}
}
4 changes: 2 additions & 2 deletions crates/katana/storage/db/src/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,11 @@ where
fn snapshot(&mut self, _: BasicId) {}

fn merge(&mut self, _: Self::Transaction) -> Result<(), Self::DatabaseError> {
Ok(())
todo!();
}

fn transaction(&self, _: BasicId) -> Option<Self::Transaction> {
None
todo!();
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/katana/storage/provider/src/providers/db/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ impl<Db: Database> ContractTrieWriter for DbProvider<Db> {
// First we insert the contract storage changes
for (address, storage_entries) in &state_updates.storage_updates {
for (key, value) in storage_entries {
let keys = key.to_bytes_be();
let keys: BitVec<u8, Msb0> = keys.as_bits().to_owned();
storage_trie_db.insert(&address.to_bytes_be(), &keys, value).unwrap();
let bytes = key.to_bytes_be();
let bv: BitVec<u8, Msb0> = bytes.as_bits()[5..].to_owned();
storage_trie_db.insert(&address.to_bytes_be(), &bv, value).unwrap();
}
// insert the contract address in the contract_leafs to put the storage root later
contract_leafs.insert(*address, Default::default());
Expand Down

0 comments on commit 070711c

Please sign in to comment.