pub struct Trie {
pub root: NodeRef,
/* private fields */
}Expand description
Ethereum-compatible Merkle Patricia Trie
Fields§
§root: NodeRefImplementations§
Source§impl Trie
impl Trie
Sourcepub fn open(db: Box<dyn TrieDB>, root: H256) -> Self
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
Sourcepub fn db(&self) -> &dyn TrieDB
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.
Sourcepub fn get(&self, pathrlp: &[u8]) -> Result<Option<ValueRLP>, TrieError>
pub fn get(&self, pathrlp: &[u8]) -> Result<Option<ValueRLP>, TrieError>
Retrieve an RLP-encoded value from the trie given its RLP-encoded path.
Sourcepub fn insert(
&mut self,
path: PathRLP,
value: ValueRLP,
) -> Result<(), TrieError>
pub fn insert( &mut self, path: PathRLP, value: ValueRLP, ) -> Result<(), TrieError>
Insert an RLP-encoded value into the trie.
Sourcepub fn remove(&mut self, path: &[u8]) -> Result<Option<ValueRLP>, TrieError>
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
Sourcepub fn hash(&mut self, crypto: &dyn Crypto) -> Result<H256, TrieError>
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
Sourcepub fn hash_no_commit(&self, crypto: &dyn Crypto) -> H256
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
pub fn get_root_node(&self, path: Nibbles) -> Result<Arc<Node>, TrieError>
Sourcepub fn collect_changes_since_last_hash(
&mut self,
crypto: &dyn Crypto,
) -> (H256, Vec<TrieNode>)
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.
Sourcepub fn commit(&mut self, crypto: &dyn Crypto) -> Result<(), TrieError>
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.
Sourcepub fn commit_without_storing(&mut self, crypto: &dyn Crypto) -> Vec<TrieNode> ⓘ
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.
Sourcepub fn get_proof(&self, path: &[u8]) -> Result<Vec<NodeRLP>, TrieError>
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.
Sourcepub fn get_proofs(
&self,
paths: &[PathRLP],
) -> Result<(Option<NodeRLP>, Vec<NodeRLP>), TrieError>
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.
pub fn empty_in_memory() -> Self
Sourcepub fn get_embedded_root(
all_nodes: &BTreeMap<H256, Node>,
root_hash: H256,
) -> Result<NodeRef, TrieError>
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.
Sourcepub fn from_nodes(
root_hash: H256,
state_nodes: &BTreeMap<H256, Node>,
) -> Result<Self, TrieError>
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.
Sourcepub fn compute_hash_from_unsorted_iter(
iter: impl Iterator<Item = (PathRLP, ValueRLP)>,
crypto: &dyn Crypto,
) -> H256
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
Sourcepub fn get_node(&self, partial_path: &PathRLP) -> Result<Vec<u8>, TrieError>
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)
pub fn root_node(&self) -> Result<Option<Arc<Node>>, TrieError>
Sourcepub fn new_temp_with_root(root: NodeRef) -> Self
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.
Sourcepub fn validate(self) -> Result<(), TrieError>
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
Sourcepub fn validate_parallel(self) -> Result<(), TrieError>
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§
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> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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 moreSource§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.