Skip to content

Commit

Permalink
Stop doing thousands of time checks each time we connect to a peer (#…
Browse files Browse the repository at this point in the history
…3106)

* Stop checking the entire AddressBook for each connection attempt

* Stop redundant peer time checks within the address book

* Stop calling `Instant::now` 3 times for each address book update

* Only get the time once each time an address book method is called

* Update outdated comment

* Use an OrderedMap to efficiently store address book peers

* Add address book order tests
  • Loading branch information
teor2345 authored Dec 3, 2021
1 parent 022808d commit 4d608d3
Show file tree
Hide file tree
Showing 15 changed files with 376 additions and 121 deletions.
46 changes: 45 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ skip-tree = [
# ticket #3063: redjubjub dependencies
{ name = "redjubjub", version = "=0.4.0" },

# ordered-map dependencies that should be dev-dependencies
# https://github.com/qwfy/ordered-map/pull/1
{ name = "env_logger", version = "=0.7.1" },

# ticket #2984: owo-colors dependencies
{ name = "color-eyre", version = "=0.5.11" },

Expand Down
20 changes: 12 additions & 8 deletions zebra-chain/src/serialization/date_time.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! DateTime types with specific serialization invariants.
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use chrono::{TimeZone, Utc};

use std::{
convert::{TryFrom, TryInto},
fmt,
num::TryFromIntError,
};

use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
use chrono::{TimeZone, Utc};

use super::{SerializationError, ZcashDeserialize, ZcashSerialize};

/// A date and time, represented by a 32-bit number of seconds since the UNIX epoch.
Expand Down Expand Up @@ -50,7 +50,7 @@ impl DateTime32 {
///
/// If the number of seconds since the UNIX epoch is greater than `u32::MAX`.
pub fn now() -> DateTime32 {
Utc::now()
chrono::Utc::now()
.try_into()
.expect("unexpected out of range chrono::DateTime")
}
Expand All @@ -71,14 +71,18 @@ impl DateTime32 {

/// Returns the duration elapsed since this time,
/// or if this time is in the future, returns `None`.
pub fn checked_elapsed(&self) -> Option<Duration32> {
DateTime32::now().checked_duration_since(*self)
pub fn checked_elapsed(&self, now: chrono::DateTime<Utc>) -> Option<Duration32> {
DateTime32::try_from(now)
.expect("unexpected out of range chrono::DateTime")
.checked_duration_since(*self)
}

/// Returns the duration elapsed since this time,
/// or if this time is in the future, returns zero.
pub fn saturating_elapsed(&self) -> Duration32 {
DateTime32::now().saturating_duration_since(*self)
pub fn saturating_elapsed(&self, now: chrono::DateTime<Utc>) -> Duration32 {
DateTime32::try_from(now)
.expect("unexpected out of range chrono::DateTime")
.saturating_duration_since(*self)
}

/// Returns the time that is `duration` after this time.
Expand Down
1 change: 1 addition & 0 deletions zebra-network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ bytes = "1.1.0"
chrono = "0.4"
hex = "0.4"
lazy_static = "1.4.0"
ordered-map = "0.4.2"
pin-project = "1.0.7"
rand = "0.8"
regex = "1"
Expand Down
Loading

0 comments on commit 4d608d3

Please sign in to comment.