MCPcopy Create free account
hub / github.com/BitVM/BitVM / apply_blocks

Method apply_blocks

header-chain/src/header_chain.rs:190–283  ·  view source on GitHub ↗
(&mut self, block_headers: Vec<CircuitBlockHeader>)

Source from the content-addressed store, hash-verified

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(&current_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(&current_target_bytes),
232 )
233 }
234 } else {
235 (
236 current_target_bytes,
237 self.current_target_bits,
238 calculate_work(&current_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);

Callers 1

header_chain_circuitFunction · 0.80

Calls 8

bits_to_targetFunction · 0.85
calculate_workFunction · 0.85
check_hash_validFunction · 0.85
validate_timestampFunction · 0.85
calculate_new_difficultyFunction · 0.85
target_to_bitsFunction · 0.85
compute_block_hashMethod · 0.80
appendMethod · 0.45

Tested by

no test coverage detected