|
1 | | -use bdk_chain::local_chain::{ |
2 | | - AlterCheckPointError, CannotConnectError, ChangeSet, LocalChain, Update, |
| 1 | +use bdk_chain::{ |
| 2 | + local_chain::{ |
| 3 | + AlterCheckPointError, CannotConnectError, ChangeSet, CheckPoint, LocalChain, Update, |
| 4 | + }, |
| 5 | + BlockId, |
3 | 6 | }; |
4 | 7 | use bitcoin::BlockHash; |
5 | 8 |
|
@@ -350,3 +353,82 @@ fn local_chain_insert_block() { |
350 | 353 | assert_eq!(chain, t.expected_final, "[{}] unexpected final chain", i,); |
351 | 354 | } |
352 | 355 | } |
| 356 | + |
| 357 | +#[test] |
| 358 | +fn checkpoint_from_block_ids() { |
| 359 | + struct TestCase<'a> { |
| 360 | + name: &'a str, |
| 361 | + blocks: &'a [(u32, BlockHash)], |
| 362 | + exp_result: Result<(), Option<(u32, BlockHash)>>, |
| 363 | + } |
| 364 | + |
| 365 | + let test_cases = [ |
| 366 | + TestCase { |
| 367 | + name: "in_order", |
| 368 | + blocks: &[(0, h!("A")), (1, h!("B")), (3, h!("D"))], |
| 369 | + exp_result: Ok(()), |
| 370 | + }, |
| 371 | + TestCase { |
| 372 | + name: "with_duplicates", |
| 373 | + blocks: &[(1, h!("B")), (2, h!("C")), (2, h!("C'"))], |
| 374 | + exp_result: Err(Some((2, h!("C")))), |
| 375 | + }, |
| 376 | + TestCase { |
| 377 | + name: "not_in_order", |
| 378 | + blocks: &[(1, h!("B")), (3, h!("D")), (2, h!("C"))], |
| 379 | + exp_result: Err(Some((3, h!("D")))), |
| 380 | + }, |
| 381 | + TestCase { |
| 382 | + name: "empty", |
| 383 | + blocks: &[], |
| 384 | + exp_result: Err(None), |
| 385 | + }, |
| 386 | + TestCase { |
| 387 | + name: "single", |
| 388 | + blocks: &[(21, h!("million"))], |
| 389 | + exp_result: Ok(()), |
| 390 | + }, |
| 391 | + ]; |
| 392 | + |
| 393 | + for (i, t) in test_cases.into_iter().enumerate() { |
| 394 | + println!("running test case {}: '{}'", i, t.name); |
| 395 | + let result = CheckPoint::from_block_ids( |
| 396 | + t.blocks |
| 397 | + .iter() |
| 398 | + .map(|&(height, hash)| BlockId { height, hash }), |
| 399 | + ); |
| 400 | + match t.exp_result { |
| 401 | + Ok(_) => { |
| 402 | + assert!(result.is_ok(), "[{}:{}] should be Ok", i, t.name); |
| 403 | + let result_vec = { |
| 404 | + let mut v = result |
| 405 | + .unwrap() |
| 406 | + .into_iter() |
| 407 | + .map(|cp| (cp.height(), cp.hash())) |
| 408 | + .collect::<Vec<_>>(); |
| 409 | + v.reverse(); |
| 410 | + v |
| 411 | + }; |
| 412 | + assert_eq!( |
| 413 | + &result_vec, t.blocks, |
| 414 | + "[{}:{}] not equal to original block ids", |
| 415 | + i, t.name |
| 416 | + ); |
| 417 | + } |
| 418 | + Err(exp_last) => { |
| 419 | + assert!(result.is_err(), "[{}:{}] should be Err", i, t.name); |
| 420 | + let err = result.unwrap_err(); |
| 421 | + assert_eq!( |
| 422 | + err.as_ref() |
| 423 | + .map(|last_cp| (last_cp.height(), last_cp.hash())), |
| 424 | + exp_last, |
| 425 | + "[{}:{}] error's last cp height should be {:?}, got {:?}", |
| 426 | + i, |
| 427 | + t.name, |
| 428 | + exp_last, |
| 429 | + err |
| 430 | + ); |
| 431 | + } |
| 432 | + } |
| 433 | + } |
| 434 | +} |
0 commit comments