(&mut self, block_headers: Vec<CircuitBlockHeader>)
| 188 | } |
| 189 | |
| 190 | pub fn apply_blocks(&mut self, block_headers: Vec<CircuitBlockHeader>) { |
| 191 | let mut current_target_bytes = if IS_REGTEST { |
| 192 | NETWORK_CONSTANTS.max_target.to_be_bytes() |
| 193 | } else { |
| 194 | bits_to_target(self.current_target_bits) |
| 195 | }; |
| 196 | let mut current_work: U256 = U256::from_be_bytes(self.total_work); |
| 197 | |
| 198 | let mut last_block_time = if IS_TESTNET4 { |
| 199 | if self.block_height == u32::MAX { |
| 200 | 0 |
| 201 | } else { |
| 202 | self.prev_11_timestamps[self.block_height as usize % 11] |
| 203 | } |
| 204 | } else { |
| 205 | 0 |
| 206 | }; |
| 207 | |
| 208 | for block_header in block_headers { |
| 209 | self.block_height = self.block_height.wrapping_add(1); |
| 210 | |
| 211 | let (target_to_use, expected_bits, work_to_add) = if IS_TESTNET4 { |
| 212 | if block_header.time > last_block_time + 1200 { |
| 213 | // If the block is an epoch block, then it still has to have the real target. |
| 214 | if self.block_height % BLOCKS_PER_EPOCH == 0 { |
| 215 | ( |
| 216 | current_target_bytes, |
| 217 | self.current_target_bits, |
| 218 | calculate_work(¤t_target_bytes), |
| 219 | ) |
| 220 | } else { |
| 221 | ( |
| 222 | NETWORK_CONSTANTS.max_target_bytes, |
| 223 | NETWORK_CONSTANTS.max_bits, |
| 224 | MINIMUM_WORK_TESTNET, |
| 225 | ) |
| 226 | } |
| 227 | } else { |
| 228 | ( |
| 229 | current_target_bytes, |
| 230 | self.current_target_bits, |
| 231 | calculate_work(¤t_target_bytes), |
| 232 | ) |
| 233 | } |
| 234 | } else { |
| 235 | ( |
| 236 | current_target_bytes, |
| 237 | self.current_target_bits, |
| 238 | calculate_work(¤t_target_bytes), |
| 239 | ) |
| 240 | }; |
| 241 | |
| 242 | let new_block_hash = block_header.compute_block_hash(); |
| 243 | |
| 244 | assert_eq!(block_header.prev_block_hash, self.best_block_hash); |
| 245 | |
| 246 | if IS_REGTEST { |
| 247 | assert_eq!(block_header.bits, NETWORK_CONSTANTS.max_bits); |
no test coverage detected