Skip to main content

Trie

Struct Trie 

Source
pub struct Trie {
    pub root: NodeRef,
    /* private fields */
}
Expand description

Ethereum-compatible Merkle Patricia Trie

Fields§

§root: NodeRef

Implementations§

Source§

impl Trie

Source

pub fn new(db: Box<dyn TrieDB>) -> Self

Creates a new Trie from a clean DB

Source

pub fn open(db: Box<dyn TrieDB>, root: H256) -> Self

Creates a trie from an already-initialized DB and sets root as the root node of the trie

Source

pub fn db(&self) -> &dyn TrieDB

Return a reference to the internal database.

Warning: All changes made to the db will bypass the trie and may cause the trie to suddenly become inconsistent.

Source

pub fn get(&self, pathrlp: &[u8]) -> Result<Option<ValueRLP>, TrieError>

Retrieve an RLP-encoded value from the trie given its RLP-encoded path.

Source

pub fn insert( &mut self, path: PathRLP, value: ValueRLP, ) -> Result<(), TrieError>

Insert an RLP-encoded value into the trie.

Source

pub fn remove(&mut self, path: &[u8]) -> Result<Option<ValueRLP>, TrieError>

Remove a value from the trie given its RLP-encoded path. Returns the value if it was succesfully removed or None if it wasn’t part of the trie

Source

pub fn hash(&mut self, crypto: &dyn Crypto) -> Result<H256, TrieError>

Return the hash of the trie’s root node. Returns keccak(RLP_NULL) if the trie is empty Also commits changes to the DB

Source

pub fn hash_no_commit(&self, crypto: &dyn Crypto) -> H256

Return the hash of the trie’s root node. Returns keccak(RLP_NULL) if the trie is empty

Source

pub fn get_root_node(&self, path: Nibbles) -> Result<Arc<Node>, TrieError>

Source

pub fn collect_changes_since_last_hash( &mut self, crypto: &dyn Crypto, ) -> (H256, Vec<TrieNode>)

Returns a list of changes in a TrieNode format since last root hash processed.

§Returns

A tuple containing the hash and the list of changes.

Source

pub fn commit(&mut self, crypto: &dyn Crypto) -> Result<(), TrieError>

Compute the hash of the root node and flush any changes into the database.

This method will also compute the hash of all internal nodes indirectly. It will not clear the cached nodes.

Source

pub fn commit_without_storing(&mut self, crypto: &dyn Crypto) -> Vec<TrieNode>

Computes the nodes that would be added if updating the trie. Nodes are given with their hash pre-calculated.

Source

pub fn get_proof(&self, path: &[u8]) -> Result<Vec<NodeRLP>, TrieError>

Obtain a merkle proof for the given path. The proof will contain all the encoded nodes traversed until reaching the node where the path is stored (including this last node). The proof will still be constructed even if the path is not stored in the trie, proving its absence.

Note: This method has a different behavior in regard to non-existent trie root nodes. Normal behavior is to return Err(InconsistentTrie), but this method will return Ok(Vec::new()) instead.

Source

pub fn get_proofs( &self, paths: &[PathRLP], ) -> Result<(Option<NodeRLP>, Vec<NodeRLP>), TrieError>

Obtains all encoded nodes traversed until reaching the node where every path is stored. The list doesn’t include the root node, this is returned separately. Will still be constructed even if some path is not stored in the trie.

Source

pub fn empty_in_memory() -> Self

Source

pub fn get_embedded_root( all_nodes: &BTreeMap<H256, Node>, root_hash: H256, ) -> Result<NodeRef, TrieError>

Gets node with embedded references to child nodes, all in just one Node.

Source

pub fn from_nodes( root_hash: H256, state_nodes: &BTreeMap<H256, Node>, ) -> Result<Self, TrieError>

Builds a trie from a set of nodes with an empty InMemoryTrieDB as a backend because the nodes are embedded in the root.

Note: This method will not ensure that all node references are valid. Invalid references will cause other methods (including, but not limited to Trie::get, Trie::insert and Trie::remove) to return Err(InconsistentTrie). Note: This method will ignore any dangling nodes. All nodes that are not accessible from the root node are considered dangling.

Source

pub fn compute_hash_from_unsorted_iter( iter: impl Iterator<Item = (PathRLP, ValueRLP)>, crypto: &dyn Crypto, ) -> H256

Builds an in-memory trie from the given elements and returns its hash

Source

pub fn get_node(&self, partial_path: &PathRLP) -> Result<Vec<u8>, TrieError>

Obtain the encoded node given its path. Allows usage of full paths (byte slice of 32 bytes) or compact-encoded nibble slices (with length lower than 32)

Source

pub fn root_node(&self) -> Result<Option<Arc<Node>>, TrieError>

Source

pub fn new_temp() -> Self

Creates a new Trie based on a temporary InMemory DB

Source

pub fn new_temp_with_root(root: NodeRef) -> Self

Creates a new Trie based on a temporary InMemory DB, with a specified root

This is usually used to create a Trie from a root that was embedded with the rest of the nodes.

Source

pub fn validate(self) -> Result<(), TrieError>

Validates that the Trie isn’t missing any nodes expected in the branches

This is used internally with debug assertions to check the status of the trie after syncing operations. Note: this operation validates the hashes because the iterator uses get_node_checked. We shouldn’t downgrade that to the unchecked version

Source

pub fn validate_parallel(self) -> Result<(), TrieError>

Validate the trie structure in parallel by splitting at the root branch node. Each of the root’s 16 subtrees is validated independently using rayon.

Trait Implementations§

Source§

impl Default for Trie

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl From<Trie> for ProofTrie

Source§

fn from(value: Trie) -> Self

Converts to this type from the input type.
Source§

impl IntoIterator for Trie

Source§

type Item = (Nibbles, Node)

The type of the elements being iterated over.
Source§

type IntoIter = TrieIterator

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more

Auto Trait Implementations§

§

impl !Freeze for Trie

§

impl !RefUnwindSafe for Trie

§

impl !UnwindSafe for Trie

§

impl Send for Trie

§

impl Sync for Trie

§

impl Unpin for Trie

§

impl UnsafeUnpin for Trie

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V