Safe TreeRoute constructor (#12691)

* Safe TreeRoute constructor
* Remove test duplicate
* Better tree route error info
This commit is contained in:
Davide Galassi
2022-11-11 16:22:26 +01:00
committed by GitHub
parent 3e6cd742e0
commit 8c423baf32
3 changed files with 43 additions and 21 deletions
@@ -179,9 +179,17 @@ pub struct TreeRoute<Block: BlockT> {
impl<Block: BlockT> TreeRoute<Block> {
/// Creates a new `TreeRoute`.
///
/// It is required that `pivot >= route.len()`, otherwise it may panics.
pub fn new(route: Vec<HashAndNumber<Block>>, pivot: usize) -> Self {
TreeRoute { route, pivot }
/// To preserve the structure safety invariats it is required that `pivot < route.len()`.
pub fn new(route: Vec<HashAndNumber<Block>>, pivot: usize) -> Result<Self, String> {
if pivot < route.len() {
Ok(TreeRoute { route, pivot })
} else {
Err(format!(
"TreeRoute pivot ({}) should be less than route length ({})",
pivot,
route.len()
))
}
}
/// Get a slice of all retracted blocks in reverse order (towards common ancestor).