diff --git a/CHANGELOG.md b/CHANGELOG.md index d70f9567..ece278ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +## 0.4.0-beta.4 + +### Bug Fix +- Fix `Instant` may panic on `duration_since, elapsed, sub`(#349, also patch to 0.4.0-alpha.3) + ## 0.4.0-beta.3 ### Features diff --git a/bench/Cargo.toml b/bench/Cargo.toml index f13a6244..65a54dfd 100644 --- a/bench/Cargo.toml +++ b/bench/Cargo.toml @@ -16,7 +16,7 @@ p2p = { path = "../tentacle", package = "tentacle" } rand = "0.8" futures = { version = "0.3.0" } tokio = { version = "1.0.0", features = ["time", "io-util", "net", "rt-multi-thread"] } -tokio-util = { version = "0.6.0", features = ["codec"] } +tokio-util = { version = "0.7.0", features = ["codec"] } crossbeam-channel = "0.5" env_logger = "0.6.0" bytes = "1.0.0" diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml index c0e51ede..025c17ce 100644 --- a/fuzz/Cargo.toml +++ b/fuzz/Cargo.toml @@ -14,7 +14,7 @@ tentacle-secio = { path = "../secio" } tokio-yamux = { path = "../yamux" } rand = "0.8" bytes = "1.0.0" -tokio-util = { version = "0.6.0", features = ["codec"] } +tokio-util = { version = "0.7.0", features = ["codec"] } # Prevent this from interfering with workspaces [workspace] @@ -30,4 +30,4 @@ path = "fuzz_targets/secio/crypto/encrypt_cipher.rs" [[bin]] name = "yamux_frame_codec" -path = "fuzz_targets/yamux/frame_codec.rs" \ No newline at end of file +path = "fuzz_targets/yamux/frame_codec.rs" diff --git a/secio/Cargo.toml b/secio/Cargo.toml index ace2e313..982b10f3 100644 --- a/secio/Cargo.toml +++ b/secio/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tentacle-secio" -version = "0.5.2" +version = "0.5.3" license = "MIT" description = "Secio encryption protocol for p2p" authors = ["piaoliu ", "Nervos Core Dev "] @@ -19,7 +19,7 @@ no-default-features = true bytes = "1.0" futures = { version = "0.3.0" } tokio = { version = "1.0", features = ["io-util"] } -tokio-util = { version = "0.6.0", features = ["codec"] } +tokio-util = { version = "0.7.0", features = ["codec"] } log = "0.4.1" molecule = "0.7.0" diff --git a/tentacle/Cargo.toml b/tentacle/Cargo.toml index 2217ed0b..f465dae2 100644 --- a/tentacle/Cargo.toml +++ b/tentacle/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tentacle" -version = "0.4.0-beta.3" +version = "0.4.0-beta.4" license = "MIT" description = "Minimal implementation for a multiplexed p2p network framework." authors = ["piaoliu ", "Nervos Core Dev "] @@ -22,7 +22,7 @@ secio = { path = "../secio", version = "0.5.0", package = "tentacle-secio" } futures = { version = "0.3.0" } tokio = { version = "1.0.0", features = ["macros"] } -tokio-util = { version = "0.6.0", features = ["codec"] } +tokio-util = { version = "0.7.0", features = ["codec"] } async-trait = "0.1" log = "0.4" bytes = "1.0.0" @@ -30,7 +30,7 @@ thiserror = "1.0" once_cell = "1.0" nohash-hasher = "0.2" -parking_lot = { version = "0.11", optional = true } +parking_lot = { version = "0.12", optional = true } tokio-tungstenite = { version = "0.16", optional = true } futures-timer = { version = "3.0.2", optional = true } async-std = { version = "1", features = ["unstable"], optional = true } @@ -66,7 +66,7 @@ env_logger = "0.6.0" crossbeam-channel = "0.5" systemstat = "0.1.3" futures-test = "0.3.5" -rustls-pemfile = "0.2" +rustls-pemfile = "0.3" [target.'cfg(unix)'.dev-dependencies] nix = "0.23.0" diff --git a/tentacle/src/runtime/async_runtime.rs b/tentacle/src/runtime/async_runtime.rs index 2f53cd2f..6a2e2e5a 100644 --- a/tentacle/src/runtime/async_runtime.rs +++ b/tentacle/src/runtime/async_runtime.rs @@ -45,7 +45,7 @@ mod os { /// this will force users to ensure that they are used in an async environment recv: Receiver>, local_addr: SocketAddr, - close_sender: Sender<()>, + _close_sender: Sender<()>, } impl TcpListener { @@ -63,7 +63,7 @@ mod os { TcpListener { recv: rx, local_addr, - close_sender: tx_c, + _close_sender: tx_c, } } diff --git a/tentacle/src/upnp/mod.rs b/tentacle/src/upnp/mod.rs index 041ae887..aba08a00 100644 --- a/tentacle/src/upnp/mod.rs +++ b/tentacle/src/upnp/mod.rs @@ -142,7 +142,9 @@ impl IgdClient { pub fn process_only_leases_support(&mut self) { for (addr, interval) in self.leases.iter_mut() { let register = interval - .map(|inner| inner.elapsed() > Duration::from_secs(40)) + .map(|inner| { + Instant::now().saturating_duration_since(inner) > Duration::from_secs(40) + }) .unwrap_or(true); if register { diff --git a/yamux/Cargo.toml b/yamux/Cargo.toml index 2316eb25..b292f639 100644 --- a/yamux/Cargo.toml +++ b/yamux/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tokio-yamux" -version = "0.3.4" +version = "0.3.5" license = "MIT" repository = "https://github.com/nervosnetwork/tentacle" description = "Rust implementation of Yamux" @@ -11,7 +11,7 @@ edition = "2021" bytes = "1.0.0" futures = { version = "0.3.0" } tokio = { version = "1.0.0" } -tokio-util = { version = "0.6.0", features = ["codec"] } +tokio-util = { version = "0.7.0", features = ["codec"] } log = "0.4" nohash-hasher = "0.2" diff --git a/yamux/src/session.rs b/yamux/src/session.rs index 301d00c4..95c3c8a6 100644 --- a/yamux/src/session.rs +++ b/yamux/src/session.rs @@ -40,7 +40,7 @@ const TIMEOUT: Duration = Duration::from_secs(30); /// So, I implemented a global time dependent on `futures-timer`, /// Because in the browser environment, it is always single-threaded, so feel free to be unsafe #[cfg(target_arch = "wasm32")] -static mut TIME: Instant = Instant::from_f64(0.0); +static mut TIME: Instant = Instant::from_u64(0); /// The session pub struct Session { @@ -276,7 +276,7 @@ where if self .pings .iter() - .any(|(_id, time)| time.elapsed() > TIMEOUT) + .any(|(_id, time)| Instant::now().saturating_duration_since(*time) > TIMEOUT) { return Err(io::ErrorKind::TimedOut.into()); } @@ -794,7 +794,7 @@ mod timer { #[derive(Debug, Copy, Clone)] pub struct Instant { /// mock - inner: f64, + inner: u64, } impl PartialEq for Instant { @@ -820,7 +820,7 @@ mod timer { } impl Instant { - pub const fn from_f64(val: f64) -> Self { + pub const fn from_u64(val: u64) -> Self { Instant { inner: val } } @@ -832,6 +832,10 @@ mod timer { *self - earlier } + pub fn saturating_duration_since(&self, earlier: Instant) -> Duration { + *self - earlier + } + pub fn elapsed(&self) -> Duration { Instant::now() - *self } @@ -841,10 +845,8 @@ mod timer { type Output = Instant; fn add(self, other: Duration) -> Instant { - let new_val = self.inner + other.as_millis() as f64; - Instant { - inner: new_val as f64, - } + let new_val = self.inner + other.as_millis() as u64; + Instant { inner: new_val } } } @@ -852,10 +854,11 @@ mod timer { type Output = Instant; fn sub(self, other: Duration) -> Instant { - let new_val = self.inner - other.as_millis() as f64; - Instant { - inner: new_val as f64, - } + let new_val = self + .inner + .checked_sub(other.as_millis() as u64) + .unwrap_or_default(); + Instant { inner: new_val } } } @@ -863,9 +866,8 @@ mod timer { type Output = Duration; fn sub(self, other: Instant) -> Duration { - let ms = self.inner - other.inner; - assert!(ms >= 0.0); - Duration::from_millis(ms as u64) + let ms = self.inner.checked_sub(other.inner).unwrap_or_default(); + Duration::from_millis(ms) } }