diff --git a/Cargo.lock b/Cargo.lock index 1a1270790d55..d3d0f6acbc92 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4371,18 +4371,18 @@ dependencies = [ [[package]] name = "pin-project" -version = "0.4.22" +version = "0.4.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "12e3a6cdbfe94a5e4572812a0201f8c0ed98c1c452c7b8563ce2276988ef9c17" +checksum = "ca4433fff2ae79342e497d9f8ee990d174071408f28f726d6d83af93e58e48aa" dependencies = [ "pin-project-internal", ] [[package]] name = "pin-project-internal" -version = "0.4.22" +version = "0.4.23" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6a0ffd45cf79d88737d7cc85bfd5d2894bee1139b356e616fe85dc389c61aaf7" +checksum = "2c0e815c3ee9a031fdf5af21c10aa17c573c9c6a566328d99e3936c34e36461f" dependencies = [ "proc-macro2 1.0.18", "quote 1.0.7", @@ -4448,7 +4448,6 @@ dependencies = [ "polkadot-primitives", "sc-network", "smol 0.3.3", - "smol-timeout", "sp-core", "streamunordered", ] @@ -4474,7 +4473,6 @@ dependencies = [ "polkadot-primitives", "sc-keystore", "smallvec 1.4.1", - "smol-timeout", "sp-core", "sp-keyring", "streamunordered", @@ -7600,16 +7598,6 @@ dependencies = [ "num_cpus", ] -[[package]] -name = "smol-timeout" -version = "0.1.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "024818c1f00b80e8171ddcfcee33860134293aa3aced60c9cbd7a5a2d41db392" -dependencies = [ - "pin-project", - "smol 0.1.18", -] - [[package]] name = "snow" version = "0.7.1" diff --git a/node/network/availability-distribution/Cargo.toml b/node/network/availability-distribution/Cargo.toml index a966c769bddf..18c1769a52b2 100644 --- a/node/network/availability-distribution/Cargo.toml +++ b/node/network/availability-distribution/Cargo.toml @@ -25,7 +25,6 @@ sp-core = { git = "https://github.com/paritytech/substrate", branch = "master", sp-keyring = { git = "https://github.com/paritytech/substrate", branch = "master" } parking_lot = "0.11.0" futures-timer = "3.0.2" -smol-timeout = "0.1.0" env_logger = "0.7.1" assert_matches = "1.3.0" smallvec = "1" diff --git a/node/network/availability-distribution/src/tests.rs b/node/network/availability-distribution/src/tests.rs index 143fd8b05b7b..9b2b1b35e533 100644 --- a/node/network/availability-distribution/src/tests.rs +++ b/node/network/availability-distribution/src/tests.rs @@ -22,13 +22,12 @@ use polkadot_primitives::v1::{ GroupRotationInfo, HeadData, PersistedValidationData, OccupiedCore, PoV, ScheduledCore, ValidatorPair, }; -use polkadot_subsystem_testhelpers as test_helpers; +use polkadot_subsystem_testhelpers::{self as test_helpers, TimeoutExt}; use polkadot_node_network_protocol::ObservedRole; use futures::{executor, future, Future}; use futures_timer::Delay; use smallvec::smallvec; -use smol_timeout::TimeoutExt; use std::time::Duration; macro_rules! view { diff --git a/node/network/bitfield-distribution/Cargo.toml b/node/network/bitfield-distribution/Cargo.toml index 768d63e1f606..67c2671d41b8 100644 --- a/node/network/bitfield-distribution/Cargo.toml +++ b/node/network/bitfield-distribution/Cargo.toml @@ -24,6 +24,5 @@ sp-core = { git = "https://github.com/paritytech/substrate", branch = "master" } parking_lot = "0.11.0" maplit = "1.0.2" smol = "0.3.3" -smol-timeout = "0.1.0" env_logger = "0.7.1" assert_matches = "1.3.0" diff --git a/node/network/bitfield-distribution/src/lib.rs b/node/network/bitfield-distribution/src/lib.rs index 67475aea1585..4c832fc216e9 100644 --- a/node/network/bitfield-distribution/src/lib.rs +++ b/node/network/bitfield-distribution/src/lib.rs @@ -611,8 +611,7 @@ mod test { use futures::executor; use maplit::hashmap; use polkadot_primitives::v1::{Signed, ValidatorPair, AvailabilityBitfield}; - use polkadot_node_subsystem_test_helpers::make_subsystem_context; - use smol_timeout::TimeoutExt; + use polkadot_node_subsystem_test_helpers::{make_subsystem_context, TimeoutExt}; use sp_core::crypto::Pair; use std::time::Duration; use assert_matches::assert_matches; diff --git a/node/subsystem-test-helpers/Cargo.toml b/node/subsystem-test-helpers/Cargo.toml index 105e6b5816a6..869fb74d0691 100644 --- a/node/subsystem-test-helpers/Cargo.toml +++ b/node/subsystem-test-helpers/Cargo.toml @@ -13,7 +13,7 @@ futures-timer = "3.0.2" log = "0.4.8" parity-scale-codec = "1.3.4" parking_lot = "0.10.0" -pin-project = "0.4.22" +pin-project = "0.4.23" polkadot-node-primitives = { path = "../primitives" } polkadot-node-subsystem = { path = "../subsystem" } polkadot-primitives = { path = "../../primitives" } diff --git a/node/subsystem-test-helpers/src/lib.rs b/node/subsystem-test-helpers/src/lib.rs index eee22dfc6f5f..5b3248289923 100644 --- a/node/subsystem-test-helpers/src/lib.rs +++ b/node/subsystem-test-helpers/src/lib.rs @@ -24,6 +24,7 @@ use futures::poll; use futures::prelude::*; use futures_timer::Delay; use parking_lot::Mutex; +use pin_project::pin_project; use sp_core::{testing::TaskExecutor, traits::SpawnNamed}; use std::convert::Infallible; @@ -286,3 +287,45 @@ pub fn subsystem_test_harness( } }); } + +/// A future that wraps another future with a `Delay` allowing for time-limited futures. +#[pin_project] +pub struct Timeout { + #[pin] + future: F, + #[pin] + delay: Delay, +} + +/// Extends `Future` to allow time-limited futures. +pub trait TimeoutExt: Future { + fn timeout(self, duration: Duration) -> Timeout + where + Self: Sized, + { + Timeout { + future: self, + delay: Delay::new(duration), + } + } +} + +impl TimeoutExt for F {} + +impl Future for Timeout { + type Output = Option; + + fn poll(self: Pin<&mut Self>, ctx: &mut Context) -> Poll { + let this = self.project(); + + if this.delay.poll(ctx).is_ready() { + return Poll::Ready(None); + } + + if let Poll::Ready(output) = this.future.poll(ctx) { + return Poll::Ready(Some(output)); + } + + Poll::Pending + } +}