let max_recoverable_reorg_depth = (MAX_SAVEPOINTS - 1) * SAVEPOINT_INTERVAL + height % SAVEPOINT_INTERVAL; for depth in 1..max_recoverable_reorg_depth { let index_block_hash = index.block_hash(height.checked_sub(depth))?; let bitcoind_block_hash = index .client .get_block_hash(u64::from(height.saturating_sub(depth))) .into_option()?; if index_block_hash == bitcoind_block_hash { return Err(anyhow!(ReorgError::Recoverable { height, depth })); } } Err(anyhow!(ReorgError::Unrecoverable)) } _ => Ok(()), } } pub(crate) fn handle_reorg(index: &Index, height: u32, depth: u32) -> Result { log::info!("rolling back database after reorg of depth {depth} at height {height}"); if let redb::Durability::None = index.durability { panic!("set index durability to `Durability::Immediate` to test reorg handling"); } let mut wtx = index.begin_write()?; let oldest_savepoint =