Get a specific change by sequence number. # Arguments `txn` - Read transaction `view` - View to query `sequence` - Sequence number to retrieve # Returns The history entry at the given sequence, or an error if out of range.
(
txn: &T,
view: &ViewState,
sequence: u64,
)
| 251 | /// |
| 252 | /// The history entry at the given sequence, or an error if out of range. |
| 253 | pub fn get_change_at_sequence<T: ViewTxnT>( |
| 254 | txn: &T, |
| 255 | view: &ViewState, |
| 256 | sequence: u64, |
| 257 | ) -> HistoryResult<HistoryEntry> { |
| 258 | if sequence >= view.change_count { |
| 259 | return Err(HistoryError::SequenceOutOfRange { |
| 260 | sequence, |
| 261 | max: view.change_count.saturating_sub(1), |
| 262 | }); |
| 263 | } |
| 264 | |
| 265 | let node_id = txn |
| 266 | .get_change_at_seq(view, sequence) |
| 267 | .map_err(|e| HistoryError::Database(e.to_string()))? |
| 268 | .ok_or_else(|| HistoryError::SequenceOutOfRange { |
| 269 | sequence, |
| 270 | max: view.change_count.saturating_sub(1), |
| 271 | })?; |
| 272 | |
| 273 | let hash = txn |
| 274 | .get_external(node_id) |
| 275 | .map_err(|e| HistoryError::Database(e.to_string()))? |
| 276 | .ok_or_else(|| HistoryError::ChangeNotFound { |
| 277 | hash: format!("{:?}", node_id), |
| 278 | })?; |
| 279 | |
| 280 | // Get the Merkle state - we need to iterate to find it |
| 281 | // This is a bit inefficient but maintains correctness |
| 282 | let iter = txn |
| 283 | .iter_changes(view, sequence) |
| 284 | .map_err(|e| HistoryError::Database(e.to_string()))?; |
| 285 | |
| 286 | for result in iter { |
| 287 | match result { |
| 288 | Ok((seq, id, merkle)) if seq == sequence && id == node_id => { |
| 289 | return Ok(HistoryEntry::new(sequence, node_id, hash, merkle)); |
| 290 | } |
| 291 | Ok((seq, _, _)) if seq > sequence => break, |
| 292 | Err(e) => return Err(HistoryError::Database(e.to_string())), |
| 293 | _ => continue, |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | // Fallback with zero merkle if we couldn't find it |
| 298 | Ok(HistoryEntry::new(sequence, node_id, hash, Merkle::ZERO)) |
| 299 | } |
| 300 | |
| 301 | /// Find the sequence number for a change by its hash. |
| 302 | /// |
no test coverage detected