-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(conductor): remove websocket subscription to Celestia height…
…s in favor of periodic polling
- Loading branch information
1 parent
21cc874
commit e1c08f6
Showing
10 changed files
with
185 additions
and
210 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
crates/astria-conductor/src/celestia/latest_height_stream.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
use std::{ | ||
pin::Pin, | ||
time::Duration, | ||
}; | ||
|
||
use astria_eyre::eyre::{ | ||
Result, | ||
WrapErr as _, | ||
}; | ||
use celestia_client::{ | ||
celestia_rpc::HeaderClient as _, | ||
jsonrpsee::http_client::HttpClient, | ||
}; | ||
use futures::{ | ||
Future, | ||
FutureExt as _, | ||
Stream, | ||
StreamExt as _, | ||
}; | ||
use tokio_stream::wrappers::IntervalStream; | ||
|
||
pub(super) fn stream_latest_heights( | ||
client: HttpClient, | ||
poll_period: Duration, | ||
) -> LatestHeightStream { | ||
let f = Box::new(move |_: tokio::time::Instant| { | ||
let client = client.clone(); | ||
async move { | ||
client | ||
.header_network_head() | ||
.await | ||
.wrap_err("failed to fetch network head") | ||
.map(|header| header.height().value()) | ||
} | ||
.boxed() | ||
}); | ||
let mut interval = tokio::time::interval(poll_period); | ||
interval.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Skip); | ||
LatestHeightStream { | ||
inner: IntervalStream::new(interval).then(f), | ||
} | ||
} | ||
|
||
type HeightFromHeaderFut = Pin<Box<dyn Future<Output = Result<u64>> + Send>>; | ||
|
||
pub(super) struct LatestHeightStream { | ||
inner: futures::stream::Then< | ||
IntervalStream, | ||
HeightFromHeaderFut, | ||
Box<dyn FnMut(tokio::time::Instant) -> HeightFromHeaderFut + Send>, | ||
>, | ||
} | ||
|
||
impl Stream for LatestHeightStream { | ||
type Item = Result<u64>; | ||
|
||
fn poll_next( | ||
mut self: std::pin::Pin<&mut Self>, | ||
cx: &mut std::task::Context<'_>, | ||
) -> std::task::Poll<Option<Self::Item>> { | ||
self.inner.poll_next_unpin(cx) | ||
} | ||
} |
Oops, something went wrong.