Skip to content

Commit

Permalink
fail on unordered too and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Coats committed Oct 5, 2023
1 parent edbb25e commit f54ef47
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
5 changes: 5 additions & 0 deletions sdk/src/types/block/core/parent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ impl<const MIN: u8, const MAX: u8> Parents<MIN, MAX> {
pub fn from_vec(parents: Vec<BlockId>) -> Result<Self, Error> {
let mut set = BTreeSet::new();
for t in parents {
if let Some(last) = set.last() {
if t.lt(last) {
return Err(Error::ParentsNotUniqueSorted);
}
}
if !set.insert(t) {
return Err(Error::ParentsNotUniqueSorted);
}
Expand Down
12 changes: 6 additions & 6 deletions sdk/tests/types/parents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,19 @@ fn new_not_sorted() {
let inner = rand_block_ids(8);
let reversed = inner.iter().copied().rev().collect::<Vec<_>>();

let parents = basic::StrongParents::from_vec(reversed).unwrap();
let parents = basic::StrongParents::from_vec(reversed);

assert_eq!(parents.as_set(), &inner);
assert!(matches!(parents, Err(Error::ParentsNotUniqueSorted)));
}

#[test]
fn new_not_unique() {
let inner = rand_block_ids(7);
let non_unique = inner.iter().chain(&inner).copied().collect::<Vec<_>>();

let parents = basic::StrongParents::from_vec(non_unique).unwrap();
let parents = basic::StrongParents::from_vec(non_unique);

assert_eq!(parents.as_set(), &inner);
assert!(matches!(parents, Err(Error::ParentsNotUniqueSorted)));
}

#[test]
Expand Down Expand Up @@ -152,7 +152,7 @@ fn unpack_invalid_not_sorted() {
assert!(matches!(
parents,
Err(UnpackError::Packable(Error::ParentsNotUniqueSorted))
),);
));
}

#[test]
Expand All @@ -167,5 +167,5 @@ fn unpack_invalid_not_unique() {
assert!(matches!(
parents,
Err(UnpackError::Packable(Error::ParentsNotUniqueSorted))
),);
));
}

0 comments on commit f54ef47

Please sign in to comment.