Skip to content

Commit 329e398

Browse files
committed
feat: add rewind to client
1 parent b2b6141 commit 329e398

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

src/block_range_scanner.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -964,6 +964,41 @@ impl BlockRangeScannerClient {
964964
Ok(ReceiverStream::new(blocks_receiver))
965965
}
966966

967+
/// Streams blocks in reverse order from `start_height` to `end_height`.
968+
///
969+
/// # Arguments
970+
///
971+
/// * `start_height` - The starting block number or tag (defaults to Latest if None).
972+
/// * `end_height` - The ending block number or tag (defaults to Earliest if None).
973+
///
974+
/// # Errors
975+
///
976+
/// * `BlockRangeScannerError::ServiceShutdown` - if the service is already shutting down.
977+
pub async fn rewind<BN: Into<BlockNumberOrTag>>(
978+
&self,
979+
start_height: Option<BN>,
980+
end_height: Option<BN>,
981+
) -> Result<ReceiverStream<BlockRangeMessage>, BlockRangeScannerError> {
982+
let (blocks_sender, blocks_receiver) = mpsc::channel(MAX_BUFFERED_MESSAGES);
983+
let (response_tx, response_rx) = oneshot::channel();
984+
985+
let command = Command::Rewind {
986+
sender: blocks_sender,
987+
start_height: start_height.map(|n| n.into()),
988+
end_height: end_height.map(|n| n.into()),
989+
response: response_tx,
990+
};
991+
992+
self.command_sender
993+
.send(command)
994+
.await
995+
.map_err(|_| BlockRangeScannerError::ServiceShutdown)?;
996+
997+
response_rx.await.map_err(|_| BlockRangeScannerError::ServiceShutdown)??;
998+
999+
Ok(ReceiverStream::new(blocks_receiver))
1000+
}
1001+
9671002
/// Unsubscribes the current subscriber.
9681003
///
9691004
/// # Errors

0 commit comments

Comments
 (0)