Add functions to get deposit_count and deposit_root from deposit cache

This commit is contained in:
pawan
2019-12-10 19:27:01 +05:30
parent 8a62f3f456
commit f82e2755d0

View File

@@ -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<u64> {
self.logs
.iter()
.take_while(|log| log.block_number >= block_number)
.collect::<Vec<_>>()
.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<Hash256> {
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.