mirror of
https://github.com/sigp/lighthouse.git
synced 2026-03-18 04:13:00 +00:00
Add functions to get deposit_count and deposit_root from deposit cache
This commit is contained in:
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user