2019-11-05 04:46:52 +00:00
|
|
|
mod cache;
|
2020-02-07 01:42:49 +00:00
|
|
|
mod cache_arena;
|
2019-11-05 04:46:52 +00:00
|
|
|
mod impls;
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test;
|
2020-03-04 21:07:27 +00:00
|
|
|
use smallvec::SmallVec;
|
2019-11-05 04:46:52 +00:00
|
|
|
|
2020-03-04 21:07:27 +00:00
|
|
|
type SmallVec8<T> = SmallVec<[T; 8]>;
|
2020-02-07 01:42:49 +00:00
|
|
|
pub type CacheArena = cache_arena::CacheArena<Hash256>;
|
|
|
|
|
2019-11-05 04:46:52 +00:00
|
|
|
pub use crate::cache::TreeHashCache;
|
|
|
|
pub use crate::impls::int_log;
|
|
|
|
use ethereum_types::H256 as Hash256;
|
|
|
|
use tree_hash::TreeHash;
|
|
|
|
|
2020-01-07 23:24:44 +00:00
|
|
|
#[derive(Debug, PartialEq, Clone)]
|
2019-11-05 04:46:52 +00:00
|
|
|
pub enum Error {
|
|
|
|
/// Attempting to provide more than 2^depth leaves to a Merkle tree is disallowed.
|
|
|
|
TooManyLeaves,
|
|
|
|
/// Shrinking a Merkle tree cache by providing it with less leaves than it currently has is
|
|
|
|
/// disallowed (for simplicity).
|
|
|
|
CannotShrink,
|
|
|
|
/// Cache is inconsistent with the list of dirty indices provided.
|
|
|
|
CacheInconsistent,
|
2020-02-07 01:42:49 +00:00
|
|
|
CacheArenaError(cache_arena::Error),
|
|
|
|
/// Unable to find left index in Merkle tree.
|
|
|
|
MissingLeftIdx(usize),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<cache_arena::Error> for Error {
|
|
|
|
fn from(e: cache_arena::Error) -> Error {
|
|
|
|
Error::CacheArenaError(e)
|
|
|
|
}
|
2019-11-05 04:46:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Trait for types which can make use of a cache to accelerate calculation of their tree hash root.
|
|
|
|
pub trait CachedTreeHash<Cache>: TreeHash {
|
|
|
|
/// Create a new cache appropriate for use with values of this type.
|
2020-02-07 01:42:49 +00:00
|
|
|
fn new_tree_hash_cache(&self, arena: &mut CacheArena) -> Cache;
|
2019-11-05 04:46:52 +00:00
|
|
|
|
|
|
|
/// Update the cache and use it to compute the tree hash root for `self`.
|
2020-02-07 01:42:49 +00:00
|
|
|
fn recalculate_tree_hash_root(
|
|
|
|
&self,
|
|
|
|
arena: &mut CacheArena,
|
|
|
|
cache: &mut Cache,
|
|
|
|
) -> Result<Hash256, Error>;
|
2019-11-05 04:46:52 +00:00
|
|
|
}
|