|
1 |
| -use bdk_core::CheckPoint; |
| 1 | +use bdk_core::{CheckPoint, ToBlockHash}; |
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,218 @@ 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 | + fn block_time(&self) -> Option<u32> { |
| 73 | + Some(self.1) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +#[test] |
| 78 | +fn test_median_time_past_with_no_timestamps_available() { |
| 79 | + // Test with BlockHash (no timestamps available) |
| 80 | + let blocks = vec![ |
| 81 | + (0, hash!("genesis")), |
| 82 | + (1, hash!("A")), |
| 83 | + (2, hash!("B")), |
| 84 | + (3, hash!("C")), |
| 85 | + ]; |
| 86 | + |
| 87 | + let cp = CheckPoint::<BlockHash>::from_blocks(blocks).expect("must construct valid chain"); |
| 88 | + assert_eq!(cp.median_time_past(), None, "BlockHash has no timestamp"); |
| 89 | +} |
| 90 | + |
| 91 | +#[test] |
| 92 | +fn test_median_time_past_with_timestamps() { |
| 93 | + // Create a chain with 12 blocks (heights 0-11) with incrementing timestamps |
| 94 | + let blocks: Vec<_> = (0..=11) |
| 95 | + .map(|i| (i, BlockWithTime(i, 1000 + i * 10))) |
| 96 | + .collect(); |
| 97 | + |
| 98 | + let cp = CheckPoint::from_blocks(blocks).expect("must construct valid chain"); |
| 99 | + |
| 100 | + // Height 11: 11 previous blocks (10..0), pseudo-median at index 5 = 1050 |
| 101 | + assert_eq!(cp.median_time_past(), Some(1050)); |
| 102 | + |
| 103 | + // Height 10: 10 previous blocks (9..0), pseudo-median at index 4 = 1040 |
| 104 | + assert_eq!(cp.get(10).unwrap().median_time_past(), Some(1040)); |
| 105 | + |
| 106 | + // Height 5: 5 previous blocks (4..0), pseudo-median at index 2 = 1020 |
| 107 | + assert_eq!(cp.get(5).unwrap().median_time_past(), Some(1020)); |
| 108 | + |
| 109 | + // Height 0: genesis has no previous blocks |
| 110 | + assert_eq!(cp.get(0).unwrap().median_time_past(), None); |
| 111 | +} |
| 112 | + |
| 113 | +#[test] |
| 114 | +fn test_median_time_past_sparse_chain() { |
| 115 | + // Sparse chain with gaps: only heights 0, 5, 10, 15, 20 |
| 116 | + let blocks = vec![ |
| 117 | + (0, BlockWithTime(0, 1000)), |
| 118 | + (5, BlockWithTime(5, 1050)), |
| 119 | + (10, BlockWithTime(10, 1100)), |
| 120 | + (15, BlockWithTime(15, 1150)), |
| 121 | + (20, BlockWithTime(20, 1200)), |
| 122 | + ]; |
| 123 | + |
| 124 | + let cp = CheckPoint::from_blocks(blocks).expect("must construct valid chain"); |
| 125 | + |
| 126 | + // All non-genesis heights should return None due to missing sequential blocks |
| 127 | + assert_eq!(cp.median_time_past(), None); // Height 20: missing 19..11 |
| 128 | + assert_eq!(cp.get(10).unwrap().median_time_past(), None); // Height 10: missing 9..1 |
| 129 | + assert_eq!(cp.get(5).unwrap().median_time_past(), None); // Height 5: missing 4..1 |
| 130 | + assert_eq!(cp.get(0).unwrap().median_time_past(), None); // Genesis: no previous blocks |
| 131 | +} |
| 132 | + |
| 133 | +#[test] |
| 134 | +fn test_median_time_past_with_sequential_blocks() { |
| 135 | + // Complete sequential chain from 0 to 5 |
| 136 | + let blocks: Vec<_> = (0..=5) |
| 137 | + .map(|i| (i, BlockWithTime(i, 1000 + i * 10))) |
| 138 | + .collect(); |
| 139 | + |
| 140 | + let cp = CheckPoint::from_blocks(blocks).expect("must construct valid chain"); |
| 141 | + |
| 142 | + // Height 5: 5 previous blocks, pseudo-median at index 2 = 1020 |
| 143 | + assert_eq!(cp.median_time_past(), Some(1020)); |
| 144 | + |
| 145 | + // Height 3: 3 previous blocks, pseudo-median at index 1 = 1010 |
| 146 | + assert_eq!(cp.get(3).unwrap().median_time_past(), Some(1010)); |
| 147 | +} |
| 148 | + |
| 149 | +#[test] |
| 150 | +fn test_median_time_past_unsorted_timestamps() { |
| 151 | + // Non-monotonic timestamps to test sorting |
| 152 | + let blocks = vec![ |
| 153 | + (0, BlockWithTime(0, 1000)), |
| 154 | + (1, BlockWithTime(1, 1100)), // Jump forward |
| 155 | + (2, BlockWithTime(2, 1050)), // Back |
| 156 | + (3, BlockWithTime(3, 1150)), // Forward |
| 157 | + (4, BlockWithTime(4, 1075)), // Back |
| 158 | + ]; |
| 159 | + |
| 160 | + let cp = CheckPoint::from_blocks(blocks).expect("must construct valid chain"); |
| 161 | + |
| 162 | + // Height 4: previous times [1150, 1050, 1100, 1000] -> sorted [1000, 1050, 1100, 1150] |
| 163 | + // Pseudo-median at index 1 = 1050 |
| 164 | + assert_eq!(cp.median_time_past(), Some(1050)); |
| 165 | +} |
| 166 | + |
| 167 | +#[test] |
| 168 | +fn test_both_mtp_methods_comparison() { |
| 169 | + // Create a chain with 15 blocks to test both MTP methods |
| 170 | + let blocks: Vec<_> = (0..=14) |
| 171 | + .map(|i| (i, BlockWithTime(i, 1000 + i * 10))) |
| 172 | + .collect(); |
| 173 | + |
| 174 | + let cp = CheckPoint::from_blocks(blocks).expect("must construct valid chain"); |
| 175 | + |
| 176 | + // At height 12: |
| 177 | + // median_time_past: uses blocks 1-11, median of [1010..1110] = 1060 |
| 178 | + // next_median_time_past: uses blocks 2-12, median of [1020..1120] = 1070 |
| 179 | + let cp12 = cp.get(12).unwrap(); |
| 180 | + assert_eq!(cp12.median_time_past(), Some(1060)); |
| 181 | + assert_eq!(cp12.next_median_time_past(), Some(1070)); |
| 182 | + |
| 183 | + // At height 11: |
| 184 | + // median_time_past: uses blocks 0-10, median of [1000..1100] = 1050 |
| 185 | + // next_median_time_past: uses blocks 1-11, median of [1010..1110] = 1060 |
| 186 | + let cp11 = cp.get(11).unwrap(); |
| 187 | + assert_eq!(cp11.median_time_past(), Some(1050)); |
| 188 | + assert_eq!(cp11.next_median_time_past(), Some(1060)); |
| 189 | + |
| 190 | + // Verify the relationship: cp(n).next_mtp == cp(n+1).mtp |
| 191 | + assert_eq!(cp11.next_median_time_past(), cp12.median_time_past()); |
| 192 | +} |
| 193 | + |
| 194 | +#[test] |
| 195 | +fn test_next_median_time_past_edge_cases() { |
| 196 | + // Test with minimum required blocks (11) |
| 197 | + let blocks: Vec<_> = (0..=10) |
| 198 | + .map(|i| (i, BlockWithTime(i, 1000 + i * 100))) |
| 199 | + .collect(); |
| 200 | + |
| 201 | + let cp = CheckPoint::from_blocks(blocks).expect("must construct valid chain"); |
| 202 | + |
| 203 | + // At height 10: next_mtp uses all 11 blocks (0-10) |
| 204 | + // Times: [1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000] |
| 205 | + // Median at index 5 = 1500 |
| 206 | + assert_eq!(cp.next_median_time_past(), Some(1500)); |
| 207 | + |
| 208 | + // At height 9: next_mtp uses blocks 0-9 (10 blocks) |
| 209 | + // Times: [1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900] |
| 210 | + // Median at index 4 = 1400 |
| 211 | + assert_eq!(cp.get(9).unwrap().next_median_time_past(), Some(1400)); |
| 212 | + |
| 213 | + // Test sparse chain where next_mtp returns None due to missing blocks |
| 214 | + let sparse = vec![ |
| 215 | + (0, BlockWithTime(0, 1000)), |
| 216 | + (5, BlockWithTime(5, 1050)), |
| 217 | + (10, BlockWithTime(10, 1100)), |
| 218 | + ]; |
| 219 | + let sparse_cp = CheckPoint::from_blocks(sparse).expect("must construct valid chain"); |
| 220 | + |
| 221 | + // At height 10: next_mtp needs blocks 0-10 but many are missing |
| 222 | + assert_eq!(sparse_cp.next_median_time_past(), None); |
| 223 | +} |
| 224 | + |
| 225 | +#[test] |
| 226 | +fn test_mtp_with_non_monotonic_times() { |
| 227 | + // Test both methods with shuffled timestamps |
| 228 | + let blocks = vec![ |
| 229 | + (0, BlockWithTime(0, 1500)), |
| 230 | + (1, BlockWithTime(1, 1200)), |
| 231 | + (2, BlockWithTime(2, 1800)), |
| 232 | + (3, BlockWithTime(3, 1100)), |
| 233 | + (4, BlockWithTime(4, 1900)), |
| 234 | + (5, BlockWithTime(5, 1300)), |
| 235 | + (6, BlockWithTime(6, 1700)), |
| 236 | + (7, BlockWithTime(7, 1400)), |
| 237 | + (8, BlockWithTime(8, 1600)), |
| 238 | + (9, BlockWithTime(9, 1000)), |
| 239 | + (10, BlockWithTime(10, 2000)), |
| 240 | + (11, BlockWithTime(11, 1050)), |
| 241 | + ]; |
| 242 | + |
| 243 | + let cp = CheckPoint::from_blocks(blocks).expect("must construct valid chain"); |
| 244 | + |
| 245 | + // Height 11: |
| 246 | + // median_time_past uses blocks 0-10: sorted |
| 247 | + // [1000,1100,1200,1300,1400,1500,1600,1700,1800,1900,2000] Median at index 5 = 1500 |
| 248 | + assert_eq!(cp.median_time_past(), Some(1500)); |
| 249 | + |
| 250 | + // next_median_time_past uses blocks 1-11: sorted |
| 251 | + // [1000,1050,1100,1200,1300,1400,1600,1700,1800,1900,2000] Median at index 5 = 1400 |
| 252 | + assert_eq!(cp.next_median_time_past(), Some(1400)); |
| 253 | +} |
| 254 | + |
| 255 | +#[test] |
| 256 | +fn test_mtp_sparse_chain_both_methods() { |
| 257 | + // Sparse chain missing required sequential blocks |
| 258 | + let blocks = vec![ |
| 259 | + (0, BlockWithTime(0, 1000)), |
| 260 | + (3, BlockWithTime(3, 1030)), |
| 261 | + (7, BlockWithTime(7, 1070)), |
| 262 | + (11, BlockWithTime(11, 1110)), |
| 263 | + (15, BlockWithTime(15, 1150)), |
| 264 | + ]; |
| 265 | + |
| 266 | + let cp = CheckPoint::from_blocks(blocks).expect("must construct valid chain"); |
| 267 | + |
| 268 | + // All heights should return None due to missing sequential blocks |
| 269 | + assert_eq!(cp.median_time_past(), None); |
| 270 | + assert_eq!(cp.next_median_time_past(), None); |
| 271 | + assert_eq!(cp.get(11).unwrap().median_time_past(), None); |
| 272 | + assert_eq!(cp.get(11).unwrap().next_median_time_past(), None); |
| 273 | +} |
0 commit comments