Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
node/approval-voting: Address second-pass feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Lldenaurois committed Jun 30, 2021
1 parent 46df410 commit 103b873
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 49 deletions.
12 changes: 6 additions & 6 deletions node/core/approval-voting/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use super::approval_db;
use super::approval_db::v1::{
blocks_at_height_key, block_entry_key, candidate_entry_key, STORED_BLOCKS_KEY, Config,
};
use super::ops::StoredBlockRange;
use super::ops::{self, StoredBlockRange};
use super::persisted_entries::{BlockEntry, CandidateEntry};

#[derive(Debug)]
Expand Down Expand Up @@ -61,31 +61,31 @@ impl Backend for DbBackend {
&self,
block_hash: &Hash,
) -> SubsystemResult<Option<BlockEntry>> {
super::ops::load_block_entry(&*self.inner, &self.config, block_hash)
ops::load_block_entry(&*self.inner, &self.config, block_hash)
.map(|e| e.map(Into::into))
}

fn load_candidate_entry(
&self,
candidate_hash: &CandidateHash,
) -> SubsystemResult<Option<CandidateEntry>> {
super::ops::load_candidate_entry(&*self.inner, &self.config, candidate_hash)
ops::load_candidate_entry(&*self.inner, &self.config, candidate_hash)
.map(|e| e.map(Into::into))
}

fn load_blocks_at_height(
&self,
block_height: &BlockNumber
) -> SubsystemResult<Vec<Hash>> {
super::ops::load_blocks_at_height(&*self.inner, &self.config, block_height)
ops::load_blocks_at_height(&*self.inner, &self.config, block_height)
}

fn load_all_blocks(&self) -> SubsystemResult<Vec<Hash>> {
super::ops::load_all_blocks(&*self.inner, &self.config)
ops::load_all_blocks(&*self.inner, &self.config)
}

fn load_stored_blocks(&self) -> SubsystemResult<Option<StoredBlockRange>> {
super::ops::load_stored_blocks(&*self.inner, &self.config)
ops::load_stored_blocks(&*self.inner, &self.config)
}

/// Atomically write the list of operations, with later operations taking precedence over prior.
Expand Down
78 changes: 35 additions & 43 deletions node/core/approval-voting/src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,45 @@ pub(super) struct NewCandidateInfo {
pub our_assignment: Option<OurAssignment>,
}

fn visit_and_remove_block_entry(
block_hash: Hash,
overlayed_db: &mut OverlayedBackend<'_, impl Backend>,
visited_candidates: &mut HashMap<CandidateHash, CandidateEntry>,
) -> SubsystemResult<Vec<Hash>> {
let block_entry = match overlayed_db.load_block_entry(&block_hash)? {
None => return Ok(Vec::new()),
Some(b) => b,
};

overlayed_db.delete_block_entry(&block_hash);
for &(_, ref candidate_hash) in block_entry.candidates() {
let candidate = match visited_candidates.entry(*candidate_hash) {
Entry::Occupied(e) => e.into_mut(),
Entry::Vacant(e) => {
e.insert(match overlayed_db.load_candidate_entry(candidate_hash)? {
None => continue, // Should not happen except for corrupt DB
Some(c) => c,
})
}
};

candidate.block_assignments.remove(&block_hash);
}

Ok(block_entry.children)
}

/// Canonicalize some particular block, pruning everything before it and
/// pruning any competing branches at the same height.
pub(super) fn canonicalize<T>(
overlay_db: &mut OverlayedBackend<'_, T>,
pub(super) fn canonicalize(
overlay_db: &mut OverlayedBackend<'_, impl Backend>,
canon_number: BlockNumber,
canon_hash: Hash,
) -> SubsystemResult<()>
where T: Backend
{
) -> SubsystemResult<()> {
let range = match overlay_db.load_stored_blocks()? {
None => return Ok(()),
Some(range) => if range.0 >= canon_number {
return Ok(())
} else {
range
},
Some(range) if range.0 >= canon_number => return Ok(()),
Some(range) => range,
};

// Storing all candidates in memory is potentially heavy, but should be fine
Expand All @@ -76,34 +99,6 @@ pub(super) fn canonicalize<T>(
// All the block heights we visited but didn't necessarily delete everything from.
let mut visited_heights = HashMap::new();

let visit_and_remove_block_entry = |
block_hash: Hash,
overlayed_db: &mut OverlayedBackend<'_, T>,
visited_candidates: &mut HashMap<CandidateHash, CandidateEntry>,
| -> SubsystemResult<Vec<Hash>> {
let block_entry = match overlayed_db.load_block_entry(&block_hash)? {
None => return Ok(Vec::new()),
Some(b) => b,
};

overlayed_db.delete_block_entry(&block_hash);
for &(_, ref candidate_hash) in block_entry.candidates() {
let candidate = match visited_candidates.entry(*candidate_hash) {
Entry::Occupied(e) => e.into_mut(),
Entry::Vacant(e) => {
e.insert(match overlayed_db.load_candidate_entry(candidate_hash)? {
None => continue, // Should not happen except for corrupt DB
Some(c) => c,
})
}
};

candidate.block_assignments.remove(&block_hash);
}

Ok(block_entry.children)
};

// First visit everything before the height.
for i in range.0..canon_number {
let at_height = overlay_db.load_blocks_at_height(&i)?;
Expand Down Expand Up @@ -220,11 +215,8 @@ pub(super) fn add_block_entry(
{
let new_range = match store.load_stored_blocks()? {
None => Some(StoredBlockRange(number, number + 1)),
Some(range) => if range.1 <= number {
Some(StoredBlockRange(range.0, number + 1))
} else {
None
}
Some(range) if range.1 <= number => Some(StoredBlockRange(range.0, number + 1)),
Some(_) => None,
};

new_range.map(|n| store.write_stored_block_range(n));
Expand Down

0 comments on commit 103b873

Please sign in to comment.