Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement IntervalStream in Clock::stream_slots #281

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions ethereum-consensus/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
[features]
default = ["serde", "async"]
serde = ["hex", "serde_json", "serde_yaml"]
async = ["tokio", "tokio-stream", "async-stream"]
async = ["tokio", "tokio-stream"]
spec-tests = ["serde", "serde_yaml"]
ec = [
"clap",
Expand Down Expand Up @@ -50,7 +50,6 @@ hex = { version = "0.4.3", optional = true }

tokio = { version = "1.18.2", features = ["full"], optional = true }
tokio-stream = { version = "0.1.8", optional = true }
async-stream = { version = "0.3.3", optional = true }

bs58 = "0.4.0"

Expand Down
27 changes: 17 additions & 10 deletions ethereum-consensus/src/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,19 +196,25 @@ impl<T: TimeProvider + Send + Sync> Clock<T> {
pub type SystemClock = Clock<SystemTimeProvider>;

#[cfg(feature = "async")]
use tokio_stream::Stream;
use tokio::time::interval_at;
yash-atreya marked this conversation as resolved.
Show resolved Hide resolved
#[cfg(feature = "async")]
use tokio_stream::{wrappers::IntervalStream, Stream, StreamExt};

#[cfg(feature = "async")]
impl<T: TimeProvider + Send + Sync> Clock<T> {
/// Returns a stream of slots. The stream will start from the next slot i.e it will not emit the
/// current slot.
pub fn stream_slots(&self) -> impl Stream<Item = Slot> + '_ {
async_stream::stream! {
yash-atreya marked this conversation as resolved.
Show resolved Hide resolved
loop {
let slot = self.current_slot().expect("after genesis");
yield slot;
let duration_until_next_slot = self.duration_until_slot(slot + 1);
tokio::time::sleep(duration_until_next_slot).await;
}
}
let slot = self.current_slot().expect("after genesis");
let duration_until_next_slot = self.duration_until_slot(slot + 1);

// Stream will not emit the `current_slot`, it starts from the next slot. This is until a custom stream implementation is added to fix this. Ref: https://github.com/ralexstokes/ethereum-consensus/pull/281#discussion_r1350932968
let interval = interval_at(
tokio::time::Instant::now() + duration_until_next_slot,
Duration::from_secs(self.seconds_per_slot),
);
let interval_stream = IntervalStream::new(interval);
interval_stream.map(move |_| self.current_slot().expect("after genesis"))
}
}

Expand Down Expand Up @@ -329,6 +335,7 @@ mod tests {
}
slots.push(slot);
}
assert_eq!(slots, (current_slot..target_slot).collect::<Vec<_>>());
// `current_slot + 1` because the tick for the current slot is not emitted as a compromise to use Interval, IntervalStream Ref: https://github.com/ralexstokes/ethereum-consensus/pull/281#discussion_r1350932968. A custom stream implementation is required to emit the current slot.
assert_eq!(slots, (current_slot + 1..target_slot).collect::<Vec<_>>());
}
}