|
1 | 1 | use bdk_chain::local_chain::{
|
2 |
| - AlterCheckPointError, CannotConnectError, ChangeSet, LocalChain, Update, |
| 2 | + AlterCheckPointError, CannotConnectError, ChangeSet, LocalChain, MissingGenesisError, Update, |
3 | 3 | };
|
4 | 4 | use bitcoin::BlockHash;
|
5 | 5 |
|
@@ -350,3 +350,76 @@ fn local_chain_insert_block() {
|
350 | 350 | assert_eq!(chain, t.expected_final, "[{}] unexpected final chain", i,);
|
351 | 351 | }
|
352 | 352 | }
|
| 353 | + |
| 354 | +#[test] |
| 355 | +fn local_chain_disconnect_from() { |
| 356 | + struct TestCase { |
| 357 | + name: &'static str, |
| 358 | + original: LocalChain, |
| 359 | + disconnect_from: (u32, BlockHash), |
| 360 | + exp_result: Result<ChangeSet, MissingGenesisError>, |
| 361 | + exp_final: LocalChain, |
| 362 | + } |
| 363 | + |
| 364 | + let test_cases = [ |
| 365 | + TestCase { |
| 366 | + name: "try_replace_genesis_should_fail", |
| 367 | + original: local_chain![(0, h!("_"))], |
| 368 | + disconnect_from: (0, h!("_")), |
| 369 | + exp_result: Err(MissingGenesisError), |
| 370 | + exp_final: local_chain![(0, h!("_"))], |
| 371 | + }, |
| 372 | + TestCase { |
| 373 | + name: "try_replace_genesis_should_fail_2", |
| 374 | + original: local_chain![(0, h!("_")), (2, h!("B")), (3, h!("C"))], |
| 375 | + disconnect_from: (0, h!("_")), |
| 376 | + exp_result: Err(MissingGenesisError), |
| 377 | + exp_final: local_chain![(0, h!("_")), (2, h!("B")), (3, h!("C"))], |
| 378 | + }, |
| 379 | + TestCase { |
| 380 | + name: "from_does_not_exist", |
| 381 | + original: local_chain![(0, h!("_")), (3, h!("C"))], |
| 382 | + disconnect_from: (2, h!("B")), |
| 383 | + exp_result: Ok(ChangeSet::default()), |
| 384 | + exp_final: local_chain![(0, h!("_")), (3, h!("C"))], |
| 385 | + }, |
| 386 | + TestCase { |
| 387 | + name: "from_has_different_blockhash", |
| 388 | + original: local_chain![(0, h!("_")), (2, h!("B"))], |
| 389 | + disconnect_from: (2, h!("not_B")), |
| 390 | + exp_result: Ok(ChangeSet::default()), |
| 391 | + exp_final: local_chain![(0, h!("_")), (2, h!("B"))], |
| 392 | + }, |
| 393 | + TestCase { |
| 394 | + name: "disconnect_one", |
| 395 | + original: local_chain![(0, h!("_")), (2, h!("B"))], |
| 396 | + disconnect_from: (2, h!("B")), |
| 397 | + exp_result: Ok(ChangeSet::from_iter([(2, None)])), |
| 398 | + exp_final: local_chain![(0, h!("_"))], |
| 399 | + }, |
| 400 | + TestCase { |
| 401 | + name: "disconnect_three", |
| 402 | + original: local_chain![(0, h!("_")), (2, h!("B")), (3, h!("C")), (4, h!("D"))], |
| 403 | + disconnect_from: (2, h!("B")), |
| 404 | + exp_result: Ok(ChangeSet::from_iter([(2, None), (3, None), (4, None)])), |
| 405 | + exp_final: local_chain![(0, h!("_"))], |
| 406 | + }, |
| 407 | + ]; |
| 408 | + |
| 409 | + for (i, t) in test_cases.into_iter().enumerate() { |
| 410 | + println!("Case {}: {}", i, t.name); |
| 411 | + |
| 412 | + let mut chain = t.original; |
| 413 | + let result = chain.disconnect_from(t.disconnect_from.into()); |
| 414 | + assert_eq!( |
| 415 | + result, t.exp_result, |
| 416 | + "[{}:{}] unexpected changeset result", |
| 417 | + i, t.name |
| 418 | + ); |
| 419 | + assert_eq!( |
| 420 | + chain, t.exp_final, |
| 421 | + "[{}:{}] unexpected final chain", |
| 422 | + i, t.name |
| 423 | + ); |
| 424 | + } |
| 425 | +} |
0 commit comments