Skip to content

Commit

Permalink
Optimize chain reorg and node functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jurshsmith committed Feb 23, 2024
1 parent dd8df8e commit a828b2e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
16 changes: 8 additions & 8 deletions chaindexing/src/chain_reorg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,32 @@ impl UnsavedReorgedBlock {
pub struct ReorgedBlocks;

impl ReorgedBlocks {
pub fn only_earliest_per_chain(reorged_blocks: &[ReorgedBlock]) -> Vec<ReorgedBlock> {
pub fn only_earliest_per_chain<'a>(
reorged_blocks: &'a [ReorgedBlock],
) -> Vec<&'a ReorgedBlock> {
reorged_blocks
.iter()
.fold(
HashMap::<i64, ReorgedBlock>::new(),
HashMap::<i64, &ReorgedBlock>::new(),
|mut reorged_blocks_by_chain, reorged_block| {
let ReorgedBlock { chain_id, .. } = reorged_block;

if let Some(earliest_reorged_block) = reorged_blocks_by_chain.get(chain_id) {
if reorged_block.block_number < earliest_reorged_block.block_number {
reorged_blocks_by_chain.insert(*chain_id, reorged_block.clone());
reorged_blocks_by_chain.insert(*chain_id, reorged_block);
}
} else {
reorged_blocks_by_chain
.insert(reorged_block.chain_id, reorged_block.clone());
reorged_blocks_by_chain.insert(reorged_block.chain_id, reorged_block);
}

reorged_blocks_by_chain
},
)
.values()
.cloned()
.into_values()
.collect()
}

pub fn get_ids(reorged_blocks: &[ReorgedBlock]) -> Vec<i32> {
pub fn get_ids<'a>(reorged_blocks: &'a [&'a ReorgedBlock]) -> Vec<i32> {
reorged_blocks.iter().map(|r| r.id).collect()
}
}
4 changes: 2 additions & 2 deletions chaindexing/src/nodes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ impl Node {
}
}

pub fn elect_leader(nodes: &Vec<Node>) -> Node {
pub fn elect_leader<'a>(nodes: &'a Vec<Node>) -> &'a Node {
let mut nodes_iter = nodes.iter();
let mut leader: Option<&Node> = nodes_iter.next();

Expand All @@ -32,5 +32,5 @@ pub fn elect_leader(nodes: &Vec<Node>) -> Node {
}
}

leader.unwrap().clone()
leader.unwrap()
}

0 comments on commit a828b2e

Please sign in to comment.