diff --git a/beacon_node/eth1/src/deposit_cache.rs b/beacon_node/eth1/src/deposit_cache.rs index f2b43f55ea..04f78e9440 100644 --- a/beacon_node/eth1/src/deposit_cache.rs +++ b/beacon_node/eth1/src/deposit_cache.rs @@ -4,6 +4,8 @@ use std::ops::Range; use tree_hash::TreeHash; use types::{Deposit, Hash256}; +const DEPOSIT_CONTRACT_TREE_DEPTH: usize = 32; + #[derive(Debug, PartialEq, Clone)] pub enum Error { /// A deposit log was added when a prior deposit was not already in the cache. @@ -203,6 +205,30 @@ impl DepositCache { Ok((tree.root(), deposits)) } } + + /// Gets the deposit count at block height = block_number. + /// + /// Fetches the `DepositLog` that was emitted at or just before `block_number` + /// and returns the deposit count as `index + 1`. + pub fn get_deposit_count_from_cache(&self, block_number: u64) -> Option { + self.logs + .iter() + .take_while(|log| log.block_number >= block_number) + .collect::>() + .last() + .map(|x| x.index + 1) + } + + /// Gets the deposit root at block height = block_number. + /// + /// Fetches the `DepositLog` that was emitted at or just before `block_number` + /// and returns the deposit root at that state. + pub fn get_deposit_root_from_cache(&self, block_number: u64) -> Option { + let index = self.get_deposit_count_from_cache(block_number)? - 1; + let roots = self.roots.get(0..index as usize)?; + let tree = DepositDataTree::create(roots, index as usize, DEPOSIT_CONTRACT_TREE_DEPTH); + Some(tree.root()) + } } /// Returns `int` as little-endian bytes with a length of 32.