use core::{
pin::Pin,
task::{Poll, Context},
time::Duration,
};
use std::time::SystemTime;
use tendermint::Network;
use crate::{ReadWriteAsBorsh, transaction::Transaction, block::Block, AggregateSignature};
pin_project_lite::pin_project! {
pub(super) struct MinimumBlockTime<Sleep = ()> {
ms_since_epoch: u64,
#[pin]
future: Option<Sleep>,
}
}
impl MinimumBlockTime {
pub(super) fn new<
T: Transaction,
N: Network<[u8; 32], [u8; 64], AggregateSignature, ReadWriteAsBorsh<Block<T>>>,
>(
ms_since_epoch: u64,
) -> MinimumBlockTime<N::Sleep> {
let future = {
let minimum_system_time =
SystemTime::UNIX_EPOCH.checked_add(Duration::from_millis(ms_since_epoch));
let time_remaining = minimum_system_time
.and_then(|minimum_system_time| minimum_system_time.duration_since(SystemTime::now()).ok());
time_remaining.map(N::sleep)
};
MinimumBlockTime { ms_since_epoch, future }
}
}
impl<Sleep: Future<Output = ()>> Future for MinimumBlockTime<Sleep> {
type Output = u64;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let mut projection = self.project();
let current_time = {
let time_since_epoch = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH);
time_since_epoch.ok().and_then(|duration| u64::try_from(duration.as_millis()).ok())
};
let block_time =
match current_time.filter(|current_time| current_time >= projection.ms_since_epoch) {
None => {
if let Some(future) = projection.future.as_mut().as_pin_mut() {
let Poll::Ready(()) = future.poll(cx) else {
return Poll::Pending;
};
}
*projection.ms_since_epoch
}
Some(current_time) => current_time,
};
projection.future.set(None);
Poll::Ready(block_time)
}
}
#[cfg(test)]
#[tokio::test]
async fn minimum_block_time() {
let seconds = Duration::from_secs(10);
let now = || SystemTime::now().duration_since(SystemTime::UNIX_EPOCH).unwrap().as_millis();
{
let satisfied = MinimumBlockTime::<tokio::time::Sleep> { ms_since_epoch: 0, future: None };
assert!(
u128::from(satisfied.await).abs_diff(now()) < seconds.as_millis(),
"`MinimumBlockTime` didn't return the current time"
);
}
{
let not_satisfied =
MinimumBlockTime::<tokio::time::Sleep> { ms_since_epoch: u64::MAX, future: None };
assert_eq!(not_satisfied.await, u64::MAX, "`MinimumBlockTime` didn't return the minimum time");
}
{
let target = u64::try_from(now() + seconds.as_millis()).unwrap();
let mut until_target = alloc::boxed::Box::pin(MinimumBlockTime {
ms_since_epoch: target,
future: Some(tokio::time::sleep(seconds)),
});
assert!((&mut until_target).await >= target, "yielded time was less than target");
assert!(now() > u128::from(target), "future resolved prior to the sleep returning");
assert!((&mut until_target).await > target, "future couldn't be polled multiple times");
}
}