Add mechanism to get blockhash's last valid slot (#10239)

automerge
This commit is contained in:
Tyera Eulberg
2020-05-26 13:06:21 -06:00
committed by GitHub
parent 1bfc4c1489
commit 4e431bc818
8 changed files with 160 additions and 17 deletions

View File

@ -936,6 +936,15 @@ impl Bank {
&self.fee_rate_governor
}
pub fn get_blockhash_last_valid_slot(&self, blockhash: &Hash) -> Option<Slot> {
let blockhash_queue = self.blockhash_queue.read().unwrap();
// This calculation will need to be updated to consider epoch boundaries if BlockhashQueue
// length is made variable by epoch
blockhash_queue
.get_hash_age(blockhash)
.map(|age| self.slot + blockhash_queue.len() as u64 - age)
}
pub fn confirmed_last_blockhash(&self) -> (Hash, FeeCalculator) {
const NUM_BLOCKHASH_CONFIRMATIONS: usize = 3;

View File

@ -58,6 +58,12 @@ impl BlockhashQueue {
.map(|age| self.hash_height - age.hash_height <= max_age as u64)
}
pub fn get_hash_age(&self, hash: &Hash) -> Option<u64> {
self.ages
.get(hash)
.map(|age| self.hash_height - age.hash_height)
}
/// check if hash is valid
#[cfg(test)]
pub fn check_hash(&self, hash: Hash) -> bool {
@ -119,6 +125,10 @@ impl BlockhashQueue {
.iter()
.map(|(k, v)| recent_blockhashes::IterItem(v.hash_height, k, &v.fee_calculator))
}
pub fn len(&self) -> usize {
self.max_age
}
}
#[cfg(test)]
mod tests {