|
1 | 1 | use bdk_chain::{ |
2 | 2 | local_chain::{ |
3 | | - AlterCheckPointError, CannotConnectError, ChangeSet, CheckPoint, LocalChain, |
4 | | - MissingGenesisError, Update, |
| 3 | + AlterCheckPointError, ApplyHeaderError, CannotConnectError, ChangeSet, CheckPoint, |
| 4 | + LocalChain, MissingGenesisError, Update, |
5 | 5 | }, |
6 | 6 | BlockId, |
7 | 7 | }; |
8 | | -use bitcoin::BlockHash; |
| 8 | +use bitcoin::{block::Header, hashes::Hash, BlockHash}; |
9 | 9 |
|
10 | 10 | #[macro_use] |
11 | 11 | mod common; |
@@ -506,3 +506,155 @@ fn checkpoint_from_block_ids() { |
506 | 506 | } |
507 | 507 | } |
508 | 508 | } |
| 509 | + |
| 510 | +#[test] |
| 511 | +fn local_chain_apply_header_connected_to() { |
| 512 | + fn header_from_prev_blockhash(prev_blockhash: BlockHash) -> Header { |
| 513 | + Header { |
| 514 | + version: bitcoin::block::Version::default(), |
| 515 | + prev_blockhash, |
| 516 | + merkle_root: bitcoin::hash_types::TxMerkleNode::all_zeros(), |
| 517 | + time: 0, |
| 518 | + bits: bitcoin::CompactTarget::default(), |
| 519 | + nonce: 0, |
| 520 | + } |
| 521 | + } |
| 522 | + |
| 523 | + struct TestCase { |
| 524 | + name: &'static str, |
| 525 | + chain: LocalChain, |
| 526 | + header: Header, |
| 527 | + height: u32, |
| 528 | + connected_to: BlockId, |
| 529 | + exp_result: Result<Vec<(u32, Option<BlockHash>)>, ApplyHeaderError>, |
| 530 | + } |
| 531 | + |
| 532 | + let test_cases = [ |
| 533 | + { |
| 534 | + let header = header_from_prev_blockhash(h!("A")); |
| 535 | + let hash = header.block_hash(); |
| 536 | + let height = 2; |
| 537 | + let connected_to = BlockId { height, hash }; |
| 538 | + TestCase { |
| 539 | + name: "connected_to_self_header_applied_to_self", |
| 540 | + chain: local_chain![(0, h!("_")), (height, hash)], |
| 541 | + header, |
| 542 | + height, |
| 543 | + connected_to, |
| 544 | + exp_result: Ok(vec![]), |
| 545 | + } |
| 546 | + }, |
| 547 | + { |
| 548 | + let prev_hash = h!("A"); |
| 549 | + let prev_height = 1; |
| 550 | + let header = header_from_prev_blockhash(prev_hash); |
| 551 | + let hash = header.block_hash(); |
| 552 | + let height = prev_height + 1; |
| 553 | + let connected_to = BlockId { |
| 554 | + height: prev_height, |
| 555 | + hash: prev_hash, |
| 556 | + }; |
| 557 | + TestCase { |
| 558 | + name: "connected_to_prev_header_applied_to_self", |
| 559 | + chain: local_chain![(0, h!("_")), (prev_height, prev_hash)], |
| 560 | + header, |
| 561 | + height, |
| 562 | + connected_to, |
| 563 | + exp_result: Ok(vec![(height, Some(hash))]), |
| 564 | + } |
| 565 | + }, |
| 566 | + { |
| 567 | + let header = header_from_prev_blockhash(BlockHash::all_zeros()); |
| 568 | + let hash = header.block_hash(); |
| 569 | + let height = 0; |
| 570 | + let connected_to = BlockId { height, hash }; |
| 571 | + TestCase { |
| 572 | + name: "genesis_applied_to_self", |
| 573 | + chain: local_chain![(0, hash)], |
| 574 | + header, |
| 575 | + height, |
| 576 | + connected_to, |
| 577 | + exp_result: Ok(vec![]), |
| 578 | + } |
| 579 | + }, |
| 580 | + { |
| 581 | + let header = header_from_prev_blockhash(h!("Z")); |
| 582 | + let height = 10; |
| 583 | + let hash = header.block_hash(); |
| 584 | + let prev_height = height - 1; |
| 585 | + let prev_hash = header.prev_blockhash; |
| 586 | + TestCase { |
| 587 | + name: "connect_at_connected_to", |
| 588 | + chain: local_chain![(0, h!("_")), (2, h!("B")), (3, h!("C"))], |
| 589 | + header, |
| 590 | + height: 10, |
| 591 | + connected_to: BlockId { |
| 592 | + height: 3, |
| 593 | + hash: h!("C"), |
| 594 | + }, |
| 595 | + exp_result: Ok(vec![(prev_height, Some(prev_hash)), (height, Some(hash))]), |
| 596 | + } |
| 597 | + }, |
| 598 | + { |
| 599 | + let prev_hash = h!("A"); |
| 600 | + let prev_height = 1; |
| 601 | + let header = header_from_prev_blockhash(prev_hash); |
| 602 | + let connected_to = BlockId { |
| 603 | + height: prev_height, |
| 604 | + hash: h!("not_prev_hash"), |
| 605 | + }; |
| 606 | + TestCase { |
| 607 | + name: "inconsistent_prev_hash", |
| 608 | + chain: local_chain![(0, h!("_")), (prev_height, h!("not_prev_hash"))], |
| 609 | + header, |
| 610 | + height: prev_height + 1, |
| 611 | + connected_to, |
| 612 | + exp_result: Err(ApplyHeaderError::InconsistentBlocks), |
| 613 | + } |
| 614 | + }, |
| 615 | + { |
| 616 | + let prev_hash = h!("A"); |
| 617 | + let prev_height = 1; |
| 618 | + let header = header_from_prev_blockhash(prev_hash); |
| 619 | + let height = prev_height + 1; |
| 620 | + let connected_to = BlockId { |
| 621 | + height, |
| 622 | + hash: h!("not_current_hash"), |
| 623 | + }; |
| 624 | + TestCase { |
| 625 | + name: "inconsistent_current_block", |
| 626 | + chain: local_chain![(0, h!("_")), (height, h!("not_current_hash"))], |
| 627 | + header, |
| 628 | + height, |
| 629 | + connected_to, |
| 630 | + exp_result: Err(ApplyHeaderError::InconsistentBlocks), |
| 631 | + } |
| 632 | + }, |
| 633 | + { |
| 634 | + let header = header_from_prev_blockhash(h!("B")); |
| 635 | + let height = 3; |
| 636 | + let connected_to = BlockId { |
| 637 | + height: 4, |
| 638 | + hash: h!("D"), |
| 639 | + }; |
| 640 | + TestCase { |
| 641 | + name: "connected_to_is_greater", |
| 642 | + chain: local_chain![(0, h!("_")), (2, h!("B"))], |
| 643 | + header, |
| 644 | + height, |
| 645 | + connected_to, |
| 646 | + exp_result: Err(ApplyHeaderError::InconsistentBlocks), |
| 647 | + } |
| 648 | + }, |
| 649 | + ]; |
| 650 | + |
| 651 | + for (i, t) in test_cases.into_iter().enumerate() { |
| 652 | + println!("running test case {}: '{}'", i, t.name); |
| 653 | + let mut chain = t.chain; |
| 654 | + let result = chain.apply_header_connected_to(&t.header, t.height, t.connected_to); |
| 655 | + let exp_result = t |
| 656 | + .exp_result |
| 657 | + .map(|cs| cs.iter().cloned().collect::<ChangeSet>()); |
| 658 | + assert_eq!(result, exp_result, "[{}:{}] unexpected result", i, t.name); |
| 659 | + } |
| 660 | +} |
0 commit comments