From f54ef4757ad9d219039f81b8da222a9a78b1b7b6 Mon Sep 17 00:00:00 2001 From: Alex Coats Date: Thu, 5 Oct 2023 09:04:02 -0400 Subject: [PATCH] fail on unordered too and fix tests --- sdk/src/types/block/core/parent.rs | 5 +++++ sdk/tests/types/parents.rs | 12 ++++++------ 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/sdk/src/types/block/core/parent.rs b/sdk/src/types/block/core/parent.rs index cf9bea57f4..10e3b86f57 100644 --- a/sdk/src/types/block/core/parent.rs +++ b/sdk/src/types/block/core/parent.rs @@ -47,6 +47,11 @@ impl Parents { pub fn from_vec(parents: Vec) -> Result { 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); } diff --git a/sdk/tests/types/parents.rs b/sdk/tests/types/parents.rs index 17e24d8abb..bea783524c 100644 --- a/sdk/tests/types/parents.rs +++ b/sdk/tests/types/parents.rs @@ -67,9 +67,9 @@ fn new_not_sorted() { let inner = rand_block_ids(8); let reversed = inner.iter().copied().rev().collect::>(); - 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] @@ -77,9 +77,9 @@ fn new_not_unique() { let inner = rand_block_ids(7); let non_unique = inner.iter().chain(&inner).copied().collect::>(); - 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] @@ -152,7 +152,7 @@ fn unpack_invalid_not_sorted() { assert!(matches!( parents, Err(UnpackError::Packable(Error::ParentsNotUniqueSorted)) - ),); + )); } #[test] @@ -167,5 +167,5 @@ fn unpack_invalid_not_unique() { assert!(matches!( parents, Err(UnpackError::Packable(Error::ParentsNotUniqueSorted)) - ),); + )); }