Skip to content

Commit

Permalink
fix some bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
jackzhhuang committed Sep 4, 2024
1 parent 58d1a2f commit ed81680
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 2 deletions.
19 changes: 18 additions & 1 deletion flexidag/src/ghostdag/protocol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ impl<
}
}

*BlockHashes::make_mut(&mut new_block_data.mergeset_blues) = self.sort_blocks(new_block_data.mergeset_blues.iter().cloned())?;
*BlockHashes::make_mut(&mut new_block_data.mergeset_blues) = self.sort_blocks_for_work_type(new_block_data.mergeset_blues.iter().cloned())?;

if new_block_data.mergeset_blues.iter().skip(1).cloned().collect::<HashSet<_>>() != blue_blocks.into_iter().map(|header| header.id()).collect::<HashSet<_>>() {
if header.number() < 10000000 {
Expand Down Expand Up @@ -517,7 +517,24 @@ impl<
Ok(sorted_blocks)
}

pub fn sort_blocks_for_work_type(&self, blocks: impl IntoIterator<Item = Hash>) -> Result<Vec<Hash>> {
let mut sorted_blocks: Vec<Hash> = blocks.into_iter().collect();

sorted_blocks.sort_by_cached_key(|block| {
let blue_work = self
.ghostdag_store
.get_blue_work(*block)
.unwrap_or_else(|err| {
error!("Failed to get blue work of block: {}, {}", *block, err);
0.into()
});
SortableBlockWithWorkType {
hash: *block,
blue_work,
}
});
Ok(sorted_blocks)
}
}

/// Chain block with attached ghostdag data
Expand Down
32 changes: 32 additions & 0 deletions flexidag/src/types/ordering.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,35 @@ impl Ord for SortableBlock {
.then_with(|| self.hash.cmp(&other.hash))
}
}

#[derive(Eq, Clone, Debug, Serialize, Deserialize)]
pub struct SortableBlockWithWorkType {
pub hash: Hash,
pub blue_work: BlueWorkType,
}

impl SortableBlockWithWorkType {
pub fn new(hash: Hash, blue_work: BlueWorkType) -> Self {
Self { hash, blue_work }
}
}

impl PartialEq for SortableBlockWithWorkType {
fn eq(&self, other: &Self) -> bool {
self.hash == other.hash
}
}

impl PartialOrd for SortableBlockWithWorkType {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}

impl Ord for SortableBlockWithWorkType {
fn cmp(&self, other: &Self) -> Ordering {
other.blue_work
.cmp(&self.blue_work)
.then_with(|| self.hash.cmp(&other.hash))
}
}
2 changes: 1 addition & 1 deletion sync/src/parallel/sender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ impl<'a> DagBlockSender<'a> {
}

self.wait_for_finish().await?;
sync_dag_store.delete_all_dag_sync_block()?;

Ok(())
}
Expand All @@ -137,7 +138,6 @@ impl<'a> DagBlockSender<'a> {
Ok(state) => {
match state {
ExecuteState::Executed(executed_block) => {
self.sync_dag_store.delete_dag_sync_block(executed_block.block().header().number(), executed_block.header().id())?;
info!("finish to execute block {:?}", executed_block.header());
self.notifier.notify(executed_block.clone())?;
worker.state = ExecuteState::Executed(executed_block);
Expand Down

0 comments on commit ed81680

Please sign in to comment.