Skip to content

Commit

Permalink
split Trie and TrieMut trait
Browse files Browse the repository at this point in the history
  • Loading branch information
ufoscout committed Apr 3, 2024
1 parent 7a1cb80 commit ebe3b5e
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion benches/trie.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use criterion::{criterion_group, criterion_main, Criterion};
use uuid::Uuid;

use eth_trie::MemoryDB;
use eth_trie::{EthTrie, Trie};
use eth_trie::*;

fn insert_worse_case_benchmark(c: &mut Criterion) {
c.bench_function("insert one", |b| {
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod trie;

pub use db::{MemoryDB, DB, versioned::VersionedDB};
pub use errors::{MemDBError, TrieError};
pub use trie::{EthTrie, Trie, TrieCommit};
pub use trie::{EthTrie, Trie, TrieMut, TrieCommit};

#[doc = include_str!("../README.md")]
#[cfg(doctest)]
Expand Down
2 changes: 1 addition & 1 deletion src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod trie_tests {
use rand::Rng;

use crate::db::MemoryDB;
use crate::trie::{EthTrie, Trie, TrieCommit};
use crate::trie::{EthTrie, Trie, TrieCommit, TrieMut};

fn assert_root(data: Vec<(&[u8], &[u8])>, hash: &str) {
let expected_hash: H256 = hash.parse().unwrap();
Expand Down
15 changes: 9 additions & 6 deletions src/trie/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use crate::nibbles::Nibbles;
use crate::node::Node;

use self::ops::TrieOps;
use self::trie_ref::TrieRef;

pub type TrieResult<T> = Result<T, TrieError>;
const HASHED_LENGTH: usize = 32;
Expand All @@ -23,7 +22,7 @@ pub trait Trie<D: DB> {

/// Returns the temporary root hash of the trie.
/// This root hash is not saved in the db until commit is called.
fn uncommitted_root(&self) -> H256;
// fn uncommitted_root(&self) -> H256;

/// Returns an iterator over the trie.
fn iter(&self) -> TrieIterator<D>;
Expand All @@ -42,6 +41,10 @@ pub trait Trie<D: DB> {
proof: Vec<Vec<u8>>,
) -> TrieResult<Option<Vec<u8>>>;

}

pub trait TrieMut<D: DB> {

/// Inserts value into trie and modifies it if it exists
fn insert(&mut self, key: &[u8], value: &[u8]) -> TrieResult<()>;

Expand Down Expand Up @@ -268,10 +271,6 @@ pub fn new(db: D) -> EthTrie<D> {

impl<D: DB> Trie<D> for EthTrie<D> {

fn uncommitted_root(&self) -> H256 {
self.root_hash
}

fn iter(&self) -> TrieIterator<D> {
let nodes = vec![(self.root.clone()).into()];
TrieIterator {
Expand All @@ -298,6 +297,10 @@ impl<D: DB> Trie<D> for EthTrie<D> {
TrieOps::verify_proof(root_hash, key, proof)
}

}

impl<D: DB> TrieMut<D> for EthTrie<D> {

fn insert(&mut self, key: &[u8], value: &[u8]) -> TrieResult<()> {
let node = TrieOps::insert(key, value, &self.root_hash, self.db.borrow_mut(), &mut self.root, &mut self.passing_keys)?;
self.root = node;
Expand Down
10 changes: 5 additions & 5 deletions src/trie/trie_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use keccak_hash::H256;

use crate::{nibbles::Nibbles, node::Node, Trie, DB};

use super::{ops::TrieOps, TrieCache, TrieIterator, TrieKeys, TrieResult};
use super::{ops::TrieOps, TrieCache, TrieIterator, TrieKeys, TrieMut, TrieResult};

#[derive(Debug)]
pub struct TrieRef<C, GP, R, D: DB>
Expand Down Expand Up @@ -36,10 +36,6 @@ impl <C, GP, R, D: DB> TrieRef<C, GP, R, D> {

impl <C: BorrowMut<TrieCache>, GP: BorrowMut<TrieKeys>, R: BorrowMut<D>, D: DB> Trie<D> for TrieRef<C, GP, R, D> {

fn uncommitted_root(&self) -> H256 {
self.root_hash
}

fn iter(&self) -> TrieIterator<D> {
let nodes = vec![(self.root.clone()).into()];
TrieIterator {
Expand All @@ -65,6 +61,10 @@ impl <C: BorrowMut<TrieCache>, GP: BorrowMut<TrieKeys>, R: BorrowMut<D>, D: DB>
) -> TrieResult<Option<Vec<u8>>> {
TrieOps::verify_proof(root_hash, key, proof)
}

}

impl <C: BorrowMut<TrieCache>, GP: BorrowMut<TrieKeys>, R: BorrowMut<D>, D: DB> TrieMut<D> for TrieRef<C, GP, R, D> {

fn insert(&mut self, key: &[u8], value: &[u8]) -> TrieResult<()> {
let node = TrieOps::insert(key, value, &self.root_hash, self.db.borrow_mut(), &mut self.root, self.passing_keys.borrow_mut())?;
Expand Down

0 comments on commit ebe3b5e

Please sign in to comment.