|
1 | 1 | use bdk_chain::{ |
2 | 2 | local_chain::{ |
3 | | - AlterCheckPointError, CannotConnectError, ChangeSet, CheckPoint, LocalChain, Update, |
| 3 | + AlterCheckPointError, ApplyHeaderError, CannotConnectError, ChangeSet, CheckPoint, |
| 4 | + LocalChain, Update, |
4 | 5 | }, |
5 | 6 | BlockId, |
6 | 7 | }; |
7 | | -use bitcoin::BlockHash; |
| 8 | +use bitcoin::{block::Header, hashes::Hash, BlockHash}; |
8 | 9 |
|
9 | 10 | #[macro_use] |
10 | 11 | mod common; |
@@ -432,3 +433,155 @@ fn checkpoint_from_block_ids() { |
432 | 433 | } |
433 | 434 | } |
434 | 435 | } |
| 436 | + |
| 437 | +#[test] |
| 438 | +fn local_chain_apply_header_connected_to() { |
| 439 | + fn header_from_prev_blockhash(prev_blockhash: BlockHash) -> Header { |
| 440 | + Header { |
| 441 | + version: bitcoin::block::Version::default(), |
| 442 | + prev_blockhash, |
| 443 | + merkle_root: bitcoin::hash_types::TxMerkleNode::all_zeros(), |
| 444 | + time: 0, |
| 445 | + bits: bitcoin::CompactTarget::default(), |
| 446 | + nonce: 0, |
| 447 | + } |
| 448 | + } |
| 449 | + |
| 450 | + struct TestCase { |
| 451 | + name: &'static str, |
| 452 | + chain: LocalChain, |
| 453 | + header: Header, |
| 454 | + height: u32, |
| 455 | + connected_to: BlockId, |
| 456 | + exp_result: Result<Vec<(u32, Option<BlockHash>)>, ApplyHeaderError>, |
| 457 | + } |
| 458 | + |
| 459 | + let test_cases = [ |
| 460 | + { |
| 461 | + let header = header_from_prev_blockhash(h!("A")); |
| 462 | + let hash = header.block_hash(); |
| 463 | + let height = 2; |
| 464 | + let connected_to = BlockId { height, hash }; |
| 465 | + TestCase { |
| 466 | + name: "connected_to_self_header_applied_to_self", |
| 467 | + chain: local_chain![(0, h!("_")), (height, hash)], |
| 468 | + header, |
| 469 | + height, |
| 470 | + connected_to, |
| 471 | + exp_result: Ok(vec![]), |
| 472 | + } |
| 473 | + }, |
| 474 | + { |
| 475 | + let prev_hash = h!("A"); |
| 476 | + let prev_height = 1; |
| 477 | + let header = header_from_prev_blockhash(prev_hash); |
| 478 | + let hash = header.block_hash(); |
| 479 | + let height = prev_height + 1; |
| 480 | + let connected_to = BlockId { |
| 481 | + height: prev_height, |
| 482 | + hash: prev_hash, |
| 483 | + }; |
| 484 | + TestCase { |
| 485 | + name: "connected_to_prev_header_applied_to_self", |
| 486 | + chain: local_chain![(0, h!("_")), (prev_height, prev_hash)], |
| 487 | + header, |
| 488 | + height, |
| 489 | + connected_to, |
| 490 | + exp_result: Ok(vec![(height, Some(hash))]), |
| 491 | + } |
| 492 | + }, |
| 493 | + { |
| 494 | + let header = header_from_prev_blockhash(BlockHash::all_zeros()); |
| 495 | + let hash = header.block_hash(); |
| 496 | + let height = 0; |
| 497 | + let connected_to = BlockId { height, hash }; |
| 498 | + TestCase { |
| 499 | + name: "genesis_applied_to_self", |
| 500 | + chain: local_chain![(0, hash)], |
| 501 | + header, |
| 502 | + height, |
| 503 | + connected_to, |
| 504 | + exp_result: Ok(vec![]), |
| 505 | + } |
| 506 | + }, |
| 507 | + { |
| 508 | + let header = header_from_prev_blockhash(h!("Z")); |
| 509 | + let height = 10; |
| 510 | + let hash = header.block_hash(); |
| 511 | + let prev_height = height - 1; |
| 512 | + let prev_hash = header.prev_blockhash; |
| 513 | + TestCase { |
| 514 | + name: "connect_at_connected_to", |
| 515 | + chain: local_chain![(0, h!("_")), (2, h!("B")), (3, h!("C"))], |
| 516 | + header, |
| 517 | + height: 10, |
| 518 | + connected_to: BlockId { |
| 519 | + height: 3, |
| 520 | + hash: h!("C"), |
| 521 | + }, |
| 522 | + exp_result: Ok(vec![(prev_height, Some(prev_hash)), (height, Some(hash))]), |
| 523 | + } |
| 524 | + }, |
| 525 | + { |
| 526 | + let prev_hash = h!("A"); |
| 527 | + let prev_height = 1; |
| 528 | + let header = header_from_prev_blockhash(prev_hash); |
| 529 | + let connected_to = BlockId { |
| 530 | + height: prev_height, |
| 531 | + hash: h!("not_prev_hash"), |
| 532 | + }; |
| 533 | + TestCase { |
| 534 | + name: "inconsistent_prev_hash", |
| 535 | + chain: local_chain![(0, h!("_")), (prev_height, h!("not_prev_hash"))], |
| 536 | + header, |
| 537 | + height: prev_height + 1, |
| 538 | + connected_to, |
| 539 | + exp_result: Err(ApplyHeaderError::InconsistentBlocks), |
| 540 | + } |
| 541 | + }, |
| 542 | + { |
| 543 | + let prev_hash = h!("A"); |
| 544 | + let prev_height = 1; |
| 545 | + let header = header_from_prev_blockhash(prev_hash); |
| 546 | + let height = prev_height + 1; |
| 547 | + let connected_to = BlockId { |
| 548 | + height, |
| 549 | + hash: h!("not_current_hash"), |
| 550 | + }; |
| 551 | + TestCase { |
| 552 | + name: "inconsistent_current_block", |
| 553 | + chain: local_chain![(0, h!("_")), (height, h!("not_current_hash"))], |
| 554 | + header, |
| 555 | + height, |
| 556 | + connected_to, |
| 557 | + exp_result: Err(ApplyHeaderError::InconsistentBlocks), |
| 558 | + } |
| 559 | + }, |
| 560 | + { |
| 561 | + let header = header_from_prev_blockhash(h!("B")); |
| 562 | + let height = 3; |
| 563 | + let connected_to = BlockId { |
| 564 | + height: 4, |
| 565 | + hash: h!("D"), |
| 566 | + }; |
| 567 | + TestCase { |
| 568 | + name: "connected_to_is_greater", |
| 569 | + chain: local_chain![(0, h!("_")), (2, h!("B"))], |
| 570 | + header, |
| 571 | + height, |
| 572 | + connected_to, |
| 573 | + exp_result: Err(ApplyHeaderError::InconsistentBlocks), |
| 574 | + } |
| 575 | + }, |
| 576 | + ]; |
| 577 | + |
| 578 | + for (i, t) in test_cases.into_iter().enumerate() { |
| 579 | + println!("running test case {}: '{}'", i, t.name); |
| 580 | + let mut chain = t.chain; |
| 581 | + let result = chain.apply_header_connected_to(&t.header, t.height, t.connected_to); |
| 582 | + let exp_result = t |
| 583 | + .exp_result |
| 584 | + .map(|cs| cs.iter().cloned().collect::<ChangeSet>()); |
| 585 | + assert_eq!(result, exp_result, "[{}:{}] unexpected result", i, t.name); |
| 586 | + } |
| 587 | +} |
0 commit comments