use ckb_types::core::{BlockExt, BlockView};
use ckb_types::packed::ProposalShortId;
#[cfg(debug_assertions)]
use is_sorted::IsSorted;
use std::collections::{HashSet, VecDeque};
#[derive(Debug, Default)]
pub struct ForkChanges {
pub(crate) attached_blocks: VecDeque<BlockView>,
pub(crate) detached_blocks: VecDeque<BlockView>,
pub(crate) detached_proposal_id: HashSet<ProposalShortId>,
pub(crate) dirty_exts: VecDeque<BlockExt>,
}
impl ForkChanges {
pub fn attached_blocks(&self) -> &VecDeque<BlockView> {
&self.attached_blocks
}
pub fn detached_blocks(&self) -> &VecDeque<BlockView> {
&self.detached_blocks
}
pub fn detached_proposal_id(&self) -> &HashSet<ProposalShortId> {
&self.detached_proposal_id
}
pub fn has_detached(&self) -> bool {
!self.detached_blocks.is_empty()
}
pub fn verified_len(&self) -> usize {
self.attached_blocks.len() - self.dirty_exts.len()
}
#[cfg(debug_assertions)]
pub fn is_sorted(&self) -> bool {
IsSorted::is_sorted_by_key(&mut self.attached_blocks().iter(), |blk| {
blk.header().number()
}) && IsSorted::is_sorted_by_key(&mut self.detached_blocks().iter(), |blk| {
blk.header().number()
})
}
}