pub struct DifficultyCache {
pub timestamps: VecDeque<u64>,
pub cumulative_difficulties: VecDeque<u128>,
pub last_accounted_height: usize,
pub config: DifficultyCacheConfig,
}Expand description
This struct is able to calculate difficulties from blockchain information.
Fields§
§timestamps: VecDeque<u64>The list of timestamps in the window.
cumulative_difficulties: VecDeque<u128>The current cumulative difficulty of the chain.
last_accounted_height: usizeThe last height we accounted for.
config: DifficultyCacheConfigThe config
Implementations§
Source§impl DifficultyCache
impl DifficultyCache
Sourcepub async fn init_from_chain_height<D: Database + Clone>(
chain_height: usize,
config: DifficultyCacheConfig,
database: D,
chain: Chain,
) -> Result<Self, ContextCacheError>
pub async fn init_from_chain_height<D: Database + Clone>( chain_height: usize, config: DifficultyCacheConfig, database: D, chain: Chain, ) -> Result<Self, ContextCacheError>
Initialize the difficulty cache from the specified chain height.
Sourcepub async fn pop_blocks_main_chain<D: Database + Clone>(
&mut self,
numb_blocks: usize,
database: D,
) -> Result<(), ContextCacheError>
pub async fn pop_blocks_main_chain<D: Database + Clone>( &mut self, numb_blocks: usize, database: D, ) -> Result<(), ContextCacheError>
Pop some blocks from the top of the cache.
The cache will be returned to the state it would have been in numb_blocks ago.
§Invariant
This must only be used on a main-chain cache.
Sourcepub fn new_block(
&mut self,
height: usize,
timestamp: u64,
cumulative_difficulty: u128,
)
pub fn new_block( &mut self, height: usize, timestamp: u64, cumulative_difficulty: u128, )
Add a new block to the difficulty cache.
Sourcepub fn next_difficulty(&self, hf: HardFork) -> u128
pub fn next_difficulty(&self, hf: HardFork) -> u128
Returns the required difficulty for the next block.
See: https://cuprate.github.io/monero-book/consensus_rules/blocks/difficulty.html#calculating-difficulty
Sourcepub fn next_difficulties(
&self,
blocks: Vec<(u64, HardFork)>,
current_hf: HardFork,
) -> Vec<u128>
pub fn next_difficulties( &self, blocks: Vec<(u64, HardFork)>, current_hf: HardFork, ) -> Vec<u128>
Returns the difficulties for multiple next blocks, using the provided timestamps and hard-forks when needed.
The first difficulty will be the same as the difficulty from DifficultyCache::next_difficulty after that the
first timestamp and hf will be applied to the cache and the difficulty from that will be added to the list.
After all timestamps and hfs have been dealt with the cache will be returned back to its original state and the difficulties will be returned.
Sourcepub fn median_timestamp(&self, numb_blocks: usize) -> Option<u64>
pub fn median_timestamp(&self, numb_blocks: usize) -> Option<u64>
Returns the median timestamp over the last numb_blocks, including the genesis block if the block height is low enough.
Will return None if there aren’t enough blocks.
Sourcepub fn cumulative_difficulty(&self) -> u128
pub fn cumulative_difficulty(&self) -> u128
Returns the cumulative difficulty of the chain.
Sourcepub fn top_block_timestamp(&self) -> Option<u64>
pub fn top_block_timestamp(&self) -> Option<u64>
Returns the top block’s timestamp, returns None if the top block is the genesis block.
Trait Implementations§
Source§impl Clone for DifficultyCache
impl Clone for DifficultyCache
Source§fn clone(&self) -> DifficultyCache
fn clone(&self) -> DifficultyCache
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for DifficultyCache
impl Debug for DifficultyCache
impl Eq for DifficultyCache
Source§impl PartialEq for DifficultyCache
impl PartialEq for DifficultyCache
impl StructuralPartialEq for DifficultyCache
Auto Trait Implementations§
impl Freeze for DifficultyCache
impl RefUnwindSafe for DifficultyCache
impl Send for DifficultyCache
impl Sync for DifficultyCache
impl Unpin for DifficultyCache
impl UnsafeUnpin for DifficultyCache
impl UnwindSafe for DifficultyCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
§impl<T> WithSubscriber for T
impl<T> WithSubscriber for T
§fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘwhere
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘwhere
S: Into<Dispatch>,
§fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ
fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 144 bytes