Skip to content
Snippets Groups Projects
Commit 7e00c816 authored by Éloïs's avatar Éloïs
Browse files

[ref] fork-tree: replacement of some `expect` by `if let`

parent 80fc2813
Branches
No related tags found
1 merge request!193Resolve "Migrate high-volume DBs to LMDB"
...@@ -221,6 +221,13 @@ impl ForkTree { ...@@ -221,6 +221,13 @@ impl ForkTree {
None None
} }
} }
fn is_main_branch_node(&self, node: &TreeNode) -> bool {
if let Some(main_branch_block_hash) = self.get_main_branch_block_hash(node.data.id) {
main_branch_block_hash == node.data.hash
} else {
false
}
}
/// Get fork branch nodes ids /// Get fork branch nodes ids
pub fn get_fork_branch_nodes_ids(&self, node_id: TreeNodeId) -> Vec<TreeNodeId> { pub fn get_fork_branch_nodes_ids(&self, node_id: TreeNodeId) -> Vec<TreeNodeId> {
let mut branch = Vec::with_capacity(self.max_depth); let mut branch = Vec::with_capacity(self.max_depth);
...@@ -234,12 +241,8 @@ impl ForkTree { ...@@ -234,12 +241,8 @@ impl ForkTree {
); );
return vec![]; return vec![];
}; };
if !self.main_branch.contains_key(&node.data.id)
|| self if !self.is_main_branch_node(&node) {
.get_main_branch_block_hash(node.data.id)
.expect("safe unwrap")
!= node.data.hash
{
branch.push(node_id); branch.push(node_id);
} }
...@@ -253,12 +256,8 @@ impl ForkTree { ...@@ -253,12 +256,8 @@ impl ForkTree {
); );
}; };
let mut parent_id = first_parent_id; let mut parent_id = first_parent_id;
while !self.main_branch.contains_key(&parent.data.id)
|| self while !self.is_main_branch_node(&parent) {
.get_main_branch_block_hash(parent.data.id)
.expect("safe unwrap")
!= parent.data.hash
{
branch.push(parent_id); branch.push(parent_id);
if let Some(next_parent_id) = parent.parent { if let Some(next_parent_id) = parent.parent {
...@@ -288,12 +287,7 @@ impl ForkTree { ...@@ -288,12 +287,7 @@ impl ForkTree {
if let Some(parent_id) = node.parent { if let Some(parent_id) = node.parent {
if let Some(Some(mut parent)) = self.nodes.get(parent_id.0).cloned() { if let Some(Some(mut parent)) = self.nodes.get(parent_id.0).cloned() {
while !self.main_branch.contains_key(&parent.data.id) while !self.is_main_branch_node(&parent) {
|| self
.get_main_branch_block_hash(parent.data.id)
.expect("safe unwrap")
!= parent.data.hash
{
branch.push(parent.data); branch.push(parent.data);
if let Some(parent_id) = parent.parent { if let Some(parent_id) = parent.parent {
...@@ -380,15 +374,14 @@ impl ForkTree { ...@@ -380,15 +374,14 @@ impl ForkTree {
main_branch: bool, main_branch: bool,
) { ) {
let new_node = TreeNode::new(parent, data); let new_node = TreeNode::new(parent, data);
let mut new_node_id = self.get_free_node_id();
if new_node_id.is_none() { let new_node_id = if let Some(new_node_id) = self.get_free_node_id() {
new_node_id = Some(TreeNodeId(self.nodes.len())); self.nodes[new_node_id.0] = Some(new_node);
self.nodes.push(Some(new_node)); new_node_id
} else { } else {
self.nodes[new_node_id.expect("safe unwrap").0] = Some(new_node); self.nodes.push(Some(new_node));
} TreeNodeId(self.nodes.len() - 1)
let new_node_id = new_node_id.expect("safe unwrap"); };
if let Some(parent) = parent { if let Some(parent) = parent {
// Remove previous sheet // Remove previous sheet
...@@ -421,7 +414,7 @@ impl ForkTree { ...@@ -421,7 +414,7 @@ impl ForkTree {
fn pruning(&mut self) { fn pruning(&mut self) {
// get root node infos // get root node infos
let root_node_id = self.root.expect("safe unwrap"); let root_node_id = self.root.expect("dev error: pruning an empty fork tree.");
let root_node = self.get_node(root_node_id); let root_node = self.get_node(root_node_id);
let root_node_block_id: BlockNumber = root_node.data.id; let root_node_block_id: BlockNumber = root_node.data.id;
......
...@@ -54,7 +54,12 @@ pub fn fork_resolution_algo<DB: DbReadable>( ...@@ -54,7 +54,12 @@ pub fn fork_resolution_algo<DB: DbReadable>(
let branch_head_blockstamp = branch.last().expect("safe unwrap"); let branch_head_blockstamp = branch.last().expect("safe unwrap");
let branch_head_median_time = let branch_head_median_time =
durs_bc_db_reader::blocks::get_fork_block(db, *branch_head_blockstamp)? durs_bc_db_reader::blocks::get_fork_block(db, *branch_head_blockstamp)?
.expect("safe unwrap") .unwrap_or_else(|| {
panic!(
"Db corrupted: fork block {} referenced in fork tree but not exist in db.",
branch_head_blockstamp
)
})
.block .block
.common_time(); .common_time();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment