This commit is contained in:
pawan
2019-12-11 17:16:50 +05:30
parent e3318168bb
commit 9fbc01cf93

View File

@@ -210,13 +210,18 @@ impl DepositCache {
/// ///
/// Fetches the `DepositLog` that was emitted at or just before `block_number` /// Fetches the `DepositLog` that was emitted at or just before `block_number`
/// and returns the deposit count as `index + 1`. /// and returns the deposit count as `index + 1`.
///
/// Returns 0 if no logs are present.
/// Note: This function assumes that `block_number` > `deposit_contract_deploy_block`
pub fn get_deposit_count_from_cache(&self, block_number: u64) -> Option<u64> { pub fn get_deposit_count_from_cache(&self, block_number: u64) -> Option<u64> {
self.logs Some(
.iter() self.logs
.take_while(|log| log.block_number >= block_number) .iter()
.collect::<Vec<_>>() .take_while(|log| block_number >= log.block_number)
.last() .collect::<Vec<_>>()
.map(|x| x.index + 1) .last()
.map_or(0, |x| x.index + 1),
)
} }
/// Gets the deposit root at block height = block_number. /// Gets the deposit root at block height = block_number.
@@ -224,7 +229,7 @@ impl DepositCache {
/// Fetches the `DepositLog` that was emitted at or just before `block_number` /// Fetches the `DepositLog` that was emitted at or just before `block_number`
/// and returns the deposit root at that state. /// and returns the deposit root at that state.
pub fn get_deposit_root_from_cache(&self, block_number: u64) -> Option<Hash256> { pub fn get_deposit_root_from_cache(&self, block_number: u64) -> Option<Hash256> {
let index = self.get_deposit_count_from_cache(block_number)? - 1; let index = self.get_deposit_count_from_cache(block_number)?;
let roots = self.roots.get(0..index as usize)?; let roots = self.roots.get(0..index as usize)?;
let tree = DepositDataTree::create(roots, index as usize, DEPOSIT_CONTRACT_TREE_DEPTH); let tree = DepositDataTree::create(roots, index as usize, DEPOSIT_CONTRACT_TREE_DEPTH);
Some(tree.root()) Some(tree.root())