|
1 | | -use bdk_chain::local_chain::{LocalChain, UpdateNotConnectedError}; |
| 1 | +use bdk_chain::local_chain::{ |
| 2 | + ChangeSet, InsertBlockNotMatchingError, LocalChain, UpdateNotConnectedError, |
| 3 | +}; |
| 4 | +use bitcoin::BlockHash; |
2 | 5 |
|
3 | 6 | #[macro_use] |
4 | 7 | mod common; |
@@ -165,3 +168,61 @@ fn invalidation_but_no_connection() { |
165 | 168 | Err(UpdateNotConnectedError(0)) |
166 | 169 | ) |
167 | 170 | } |
| 171 | + |
| 172 | +#[test] |
| 173 | +fn insert_block() { |
| 174 | + struct TestCase { |
| 175 | + original: LocalChain, |
| 176 | + insert: (u32, BlockHash), |
| 177 | + expected_result: Result<ChangeSet, InsertBlockNotMatchingError>, |
| 178 | + expected_final: LocalChain, |
| 179 | + } |
| 180 | + |
| 181 | + let test_cases = [ |
| 182 | + TestCase { |
| 183 | + original: local_chain![], |
| 184 | + insert: (5, h!("block5")), |
| 185 | + expected_result: Ok([(5, Some(h!("block5")))].into()), |
| 186 | + expected_final: local_chain![(5, h!("block5"))], |
| 187 | + }, |
| 188 | + TestCase { |
| 189 | + original: local_chain![(3, h!("A"))], |
| 190 | + insert: (4, h!("B")), |
| 191 | + expected_result: Ok([(4, Some(h!("B")))].into()), |
| 192 | + expected_final: local_chain![(3, h!("A")), (4, h!("B"))], |
| 193 | + }, |
| 194 | + TestCase { |
| 195 | + original: local_chain![(4, h!("B"))], |
| 196 | + insert: (3, h!("A")), |
| 197 | + expected_result: Ok([(3, Some(h!("A")))].into()), |
| 198 | + expected_final: local_chain![(3, h!("A")), (4, h!("B"))], |
| 199 | + }, |
| 200 | + TestCase { |
| 201 | + original: local_chain![(2, h!("K"))], |
| 202 | + insert: (2, h!("K")), |
| 203 | + expected_result: Ok([].into()), |
| 204 | + expected_final: local_chain![(2, h!("K"))], |
| 205 | + }, |
| 206 | + TestCase { |
| 207 | + original: local_chain![(2, h!("K"))], |
| 208 | + insert: (2, h!("J")), |
| 209 | + expected_result: Err(InsertBlockNotMatchingError { |
| 210 | + height: 2, |
| 211 | + original_hash: h!("K"), |
| 212 | + update_hash: h!("J"), |
| 213 | + }), |
| 214 | + expected_final: local_chain![(2, h!("K"))], |
| 215 | + }, |
| 216 | + ]; |
| 217 | + |
| 218 | + for (i, t) in test_cases.into_iter().enumerate() { |
| 219 | + let mut chain = t.original; |
| 220 | + assert_eq!( |
| 221 | + chain.insert_block(t.insert.into()), |
| 222 | + t.expected_result, |
| 223 | + "[{}] unexpected result when inserting block", |
| 224 | + i, |
| 225 | + ); |
| 226 | + assert_eq!(chain, t.expected_final, "[{}] unexpected final chain", i,); |
| 227 | + } |
| 228 | +} |
0 commit comments