Skip to content

Commit

Permalink
move ongoing fork to helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
nud3l committed May 21, 2020
1 parent 59ef6c6 commit 6cb1a9a
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions crates/btc-relay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,22 +401,7 @@ impl<T: Trait> Module<T> {

let best_block_height = Self::get_best_block_height();

// check if there is a next best fork
match Self::get_chain_id_from_position(1) {
Ok(id) => {
let next_best_fork_height = Self::get_block_chain_from_id(id)?.max_height;

debug::print!("Best block height: {}", best_block_height);
debug::print!("Next best fork height: {}", next_best_fork_height);
// fail if there is an ongoing fork
ensure!(
best_block_height >= next_best_fork_height + Self::confirmations(),
Error::OngoingFork
);
}
// do nothing if there is no fork
Err(_) => {}
}
Self::ensure_no_ongoing_fork(best_block_height)?;

// This call fails if not enough confirmations
Self::check_confirmations(best_block_height, confirmations, block_height, insecure)?;
Expand Down Expand Up @@ -1136,6 +1121,27 @@ impl<T: Trait> Module<T> {
}
Ok(())
}

fn ensure_no_ongoing_fork(best_block_height: u32) -> UnitResult {
// check if there is a next best fork
match Self::get_chain_id_from_position(1) {
// if yes, check that the main chain is at least Self::confirmations() ahead
Ok(id) => {
let next_best_fork_height = Self::get_block_chain_from_id(id)?.max_height;

debug::print!("Best block height: {}", best_block_height);
debug::print!("Next best fork height: {}", next_best_fork_height);
// fail if there is an ongoing fork
ensure!(
best_block_height >= next_best_fork_height + Self::confirmations(),
Error::OngoingFork
);
}
// else, do nothing if there is no fork
Err(_) => {}
}
Ok(())
}
}

decl_event! {
Expand Down

0 comments on commit 6cb1a9a

Please sign in to comment.