|
1 | | -use bdk_core::CheckPoint; |
| 1 | +use bdk_core::{CheckPoint, ToBlockHash, ToBlockTime}; |
2 | 2 | use bdk_testenv::{block_id, hash}; |
| 3 | +use bitcoin::hashes::Hash; |
3 | 4 | use bitcoin::BlockHash; |
4 | 5 |
|
5 | 6 | /// Inserting a block that already exists in the checkpoint chain must always succeed. |
@@ -55,3 +56,133 @@ fn checkpoint_destruction_is_sound() { |
55 | 56 | } |
56 | 57 | assert_eq!(cp.iter().count() as u32, end); |
57 | 58 | } |
| 59 | + |
| 60 | +/// Test helper: A block data type that includes timestamp |
| 61 | +/// Fields are (height, time) |
| 62 | +#[derive(Debug, Clone, Copy)] |
| 63 | +struct BlockWithTime(u32, u32); |
| 64 | + |
| 65 | +impl ToBlockHash for BlockWithTime { |
| 66 | + fn to_blockhash(&self) -> BlockHash { |
| 67 | + // Generate a deterministic hash from the height |
| 68 | + let hash_bytes = bitcoin::hashes::sha256d::Hash::hash(&self.0.to_le_bytes()); |
| 69 | + BlockHash::from_raw_hash(hash_bytes) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +impl ToBlockTime for BlockWithTime { |
| 74 | + fn to_blocktime(&self) -> u32 { |
| 75 | + self.1 |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +#[test] |
| 80 | +fn test_median_time_past_with_timestamps() { |
| 81 | + // Create a chain with 12 blocks (heights 0-11) with incrementing timestamps |
| 82 | + let blocks: Vec<_> = (0..=11) |
| 83 | + .map(|i| (i, BlockWithTime(i, 1000 + i * 10))) |
| 84 | + .collect(); |
| 85 | + |
| 86 | + let cp = CheckPoint::from_blocks(blocks).expect("must construct valid chain"); |
| 87 | + |
| 88 | + // Height 11: 11 previous blocks (11..=1), pseudo-median at index 6 = 1060 |
| 89 | + assert_eq!(cp.median_time_past(), Some(1060)); |
| 90 | + |
| 91 | + // Height 10: 11 previous blocks (10..=0), pseudo-median at index 5 = 1050 |
| 92 | + assert_eq!(cp.get(10).unwrap().median_time_past(), Some(1050)); |
| 93 | + |
| 94 | + // Height 5: 6 previous blocks (5..=0), pseudo-median at index 3 = 1030 |
| 95 | + assert_eq!(cp.get(5).unwrap().median_time_past(), Some(1030)); |
| 96 | + |
| 97 | + // Height 3: 4 previous blocks (3..=0), pseudo-median at index 2 = 1020 |
| 98 | + assert_eq!(cp.get(3).unwrap().median_time_past(), Some(1020)); |
| 99 | + |
| 100 | + // Height 0: 1 block at index 0 = 1000 |
| 101 | + assert_eq!(cp.get(0).unwrap().median_time_past(), Some(1000)); |
| 102 | +} |
| 103 | + |
| 104 | +#[test] |
| 105 | +fn test_previous_median_time_past_edge_cases() { |
| 106 | + // Test with minimum required blocks (11) |
| 107 | + let blocks: Vec<_> = (0..=10) |
| 108 | + .map(|i| (i, BlockWithTime(i, 1000 + i * 100))) |
| 109 | + .collect(); |
| 110 | + |
| 111 | + let cp = CheckPoint::from_blocks(blocks).expect("must construct valid chain"); |
| 112 | + |
| 113 | + // At height 10: next_mtp uses all 11 blocks (0-10) |
| 114 | + // Times: [1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000] |
| 115 | + // Median at index 5 = 1500 |
| 116 | + assert_eq!(cp.median_time_past(), Some(1500)); |
| 117 | + |
| 118 | + // At height 9: mtp uses blocks 0-9 (10 blocks) |
| 119 | + // Times: [1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900] |
| 120 | + // Median at index 5 = 1400 |
| 121 | + assert_eq!(cp.get(9).unwrap().median_time_past(), Some(1500)); |
| 122 | + |
| 123 | + // Test sparse chain where next_mtp returns None due to missing blocks |
| 124 | + let sparse = vec![ |
| 125 | + (0, BlockWithTime(0, 1000)), |
| 126 | + (5, BlockWithTime(5, 1050)), |
| 127 | + (10, BlockWithTime(10, 1100)), |
| 128 | + ]; |
| 129 | + let sparse_cp = CheckPoint::from_blocks(sparse).expect("must construct valid chain"); |
| 130 | + |
| 131 | + // At height 10: next_mtp needs blocks 0-10 but many are missing |
| 132 | + assert_eq!(sparse_cp.median_time_past(), None); |
| 133 | +} |
| 134 | + |
| 135 | +#[test] |
| 136 | +fn test_mtp_with_non_monotonic_times() { |
| 137 | + // Test both methods with shuffled timestamps |
| 138 | + let blocks = vec![ |
| 139 | + (0, BlockWithTime(0, 1500)), |
| 140 | + (1, BlockWithTime(1, 1200)), |
| 141 | + (2, BlockWithTime(2, 1800)), |
| 142 | + (3, BlockWithTime(3, 1100)), |
| 143 | + (4, BlockWithTime(4, 1900)), |
| 144 | + (5, BlockWithTime(5, 1300)), |
| 145 | + (6, BlockWithTime(6, 1700)), |
| 146 | + (7, BlockWithTime(7, 1400)), |
| 147 | + (8, BlockWithTime(8, 1600)), |
| 148 | + (9, BlockWithTime(9, 1000)), |
| 149 | + (10, BlockWithTime(10, 2000)), |
| 150 | + (11, BlockWithTime(11, 1650)), |
| 151 | + ]; |
| 152 | + |
| 153 | + let cp = CheckPoint::from_blocks(blocks).expect("must construct valid chain"); |
| 154 | + |
| 155 | + // Height 10: |
| 156 | + // mtp uses blocks 0-10: sorted |
| 157 | + // [1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000] Median at index 5 = 1500 |
| 158 | + assert_eq!(cp.get(10).unwrap().median_time_past(), Some(1500)); |
| 159 | + |
| 160 | + // Height 11: |
| 161 | + // mtp uses blocks 1-11: sorted |
| 162 | + // [1000,1100,1200,1300,1400,1600,1650,1700,1800,1900,2000] Median at index 5 = 1600 |
| 163 | + assert_eq!(cp.median_time_past(), Some(1600)); |
| 164 | + |
| 165 | + // Test with smaller chain to verify sorting at different heights |
| 166 | + let cp3 = cp.get(3).unwrap(); |
| 167 | + // Height 3: timestamps [1100, 1800, 1200, 1500] -> sorted [1100, 1200, 1500, 1800] |
| 168 | + // Pseudo-median at index 2 = 1500 |
| 169 | + assert_eq!(cp3.median_time_past(), Some(1500)); |
| 170 | +} |
| 171 | + |
| 172 | +#[test] |
| 173 | +fn test_mtp_sparse_chain() { |
| 174 | + // Sparse chain missing required sequential blocks |
| 175 | + let blocks = vec![ |
| 176 | + (0, BlockWithTime(0, 1000)), |
| 177 | + (3, BlockWithTime(3, 1030)), |
| 178 | + (7, BlockWithTime(7, 1070)), |
| 179 | + (11, BlockWithTime(11, 1110)), |
| 180 | + (15, BlockWithTime(15, 1150)), |
| 181 | + ]; |
| 182 | + |
| 183 | + let cp = CheckPoint::from_blocks(blocks).expect("must construct valid chain"); |
| 184 | + |
| 185 | + // All heights should return None due to missing sequential blocks |
| 186 | + assert_eq!(cp.median_time_past(), None); |
| 187 | + assert_eq!(cp.get(11).unwrap().median_time_past(), None); |
| 188 | +} |
0 commit comments