Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Remove the time dependency where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaka committed Mar 13, 2018
1 parent 3f33370 commit 2c5ec71
Show file tree
Hide file tree
Showing 41 changed files with 169 additions and 179 deletions.
8 changes: 0 additions & 8 deletions Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ docopt = "0.8"
clap = "2"
term_size = "0.3"
textwrap = "0.9"
time = "0.1"
num_cpus = "1.2"
number_prefix = "0.2"
rpassword = "1.0"
Expand Down
1 change: 0 additions & 1 deletion ethcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ macros = { path = "../util/macros" }
rust-crypto = "0.2.34"
rustc-hex = "1.0"
stats = { path = "../util/stats" }
time = "0.1"
trace-time = { path = "../util/trace-time" }
using_queue = { path = "../util/using_queue" }
table = { path = "../util/table" }
Expand Down
1 change: 0 additions & 1 deletion ethcore/light/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ vm = { path = "../vm" }
plain_hasher = { path = "../../util/plain_hasher" }
rlp = { path = "../../util/rlp" }
rlp_derive = { path = "../../util/rlp_derive" }
time = "0.1"
smallvec = "0.4"
futures = "0.1"
rand = "0.4"
Expand Down
14 changes: 7 additions & 7 deletions ethcore/light/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use ethcore::header::BlockNumber;
use ethcore::receipt::Receipt;

use stats::Corpus;
use time::{SteadyTime, Duration};
use std::time::{Instant, Duration};
use heapsize::HeapSizeOf;
use ethereum_types::{H256, U256};
use memory_cache::MemoryLruCache;
Expand Down Expand Up @@ -69,7 +69,7 @@ pub struct Cache {
bodies: MemoryLruCache<H256, encoded::Body>,
receipts: MemoryLruCache<H256, Vec<Receipt>>,
chain_score: MemoryLruCache<H256, U256>,
corpus: Option<(Corpus<U256>, SteadyTime)>,
corpus: Option<(Corpus<U256>, Instant)>,
corpus_expiration: Duration,
}

Expand Down Expand Up @@ -139,7 +139,7 @@ impl Cache {

/// Get gas price corpus, if recent enough.
pub fn gas_price_corpus(&self) -> Option<Corpus<U256>> {
let now = SteadyTime::now();
let now = Instant::now();

self.corpus.as_ref().and_then(|&(ref corpus, ref tm)| {
if *tm + self.corpus_expiration >= now {
Expand All @@ -152,7 +152,7 @@ impl Cache {

/// Set the cached gas price corpus.
pub fn set_gas_price_corpus(&mut self, corpus: Corpus<U256>) {
self.corpus = Some((corpus, SteadyTime::now()))
self.corpus = Some((corpus, Instant::now()))
}

/// Get the memory used.
Expand All @@ -175,18 +175,18 @@ impl HeapSizeOf for Cache {
#[cfg(test)]
mod tests {
use super::Cache;
use time::Duration;
use std::time::Duration;

#[test]
fn corpus_inaccessible() {
let mut cache = Cache::new(Default::default(), Duration::hours(5));
let mut cache = Cache::new(Default::default(), Duration::from_secs(5 * 3600));

cache.set_gas_price_corpus(vec![].into());
assert_eq!(cache.gas_price_corpus(), Some(vec![].into()));

{
let corpus_time = &mut cache.corpus.as_mut().unwrap().1;
*corpus_time = *corpus_time - Duration::hours(6);
*corpus_time = *corpus_time - Duration::from_secs(6 * 3600);
}
assert!(cache.gas_price_corpus().is_none());
}
Expand Down
16 changes: 8 additions & 8 deletions ethcore/light/src/client/header_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -732,7 +732,7 @@ mod tests {
use kvdb::KeyValueDB;
use kvdb_memorydb;

use time::Duration;
use std::time::Duration;
use parking_lot::Mutex;

fn make_db() -> Arc<KeyValueDB> {
Expand All @@ -745,7 +745,7 @@ mod tests {
let genesis_header = spec.genesis_header();
let db = make_db();

let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::hours(6))));
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::from_secs(6 * 3600))));

let chain = HeaderChain::new(db.clone(), None, &spec, cache).unwrap();

Expand Down Expand Up @@ -778,7 +778,7 @@ mod tests {
let spec = Spec::new_test();
let genesis_header = spec.genesis_header();
let db = make_db();
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::hours(6))));
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::from_secs(6 * 3600))));

let chain = HeaderChain::new(db.clone(), None, &spec, cache).unwrap();

Expand Down Expand Up @@ -860,7 +860,7 @@ mod tests {
fn earliest_is_latest() {
let spec = Spec::new_test();
let db = make_db();
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::hours(6))));
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::from_secs(6 * 3600))));

let chain = HeaderChain::new(db.clone(), None, &spec, cache).unwrap();

Expand All @@ -873,7 +873,7 @@ mod tests {
let spec = Spec::new_test();
let genesis_header = spec.genesis_header();
let db = make_db();
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::hours(6))));
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::from_secs(6 * 3600))));

{
let chain = HeaderChain::new(db.clone(), None, &spec, cache.clone()).unwrap();
Expand Down Expand Up @@ -909,7 +909,7 @@ mod tests {
let spec = Spec::new_test();
let genesis_header = spec.genesis_header();
let db = make_db();
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::hours(6))));
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::from_secs(6 * 3600))));

{
let chain = HeaderChain::new(db.clone(), None, &spec, cache.clone()).unwrap();
Expand Down Expand Up @@ -964,7 +964,7 @@ mod tests {
let spec = Spec::new_test();
let genesis_header = spec.genesis_header();
let db = make_db();
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::hours(6))));
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::from_secs(6 * 3600))));

let chain = HeaderChain::new(db.clone(), None, &spec, cache.clone()).unwrap();

Expand All @@ -978,7 +978,7 @@ mod tests {
let spec = Spec::new_test();
let genesis_header = spec.genesis_header();
let db = make_db();
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::hours(6))));
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::from_secs(6 * 3600))));

let chain = HeaderChain::new(db.clone(), None, &spec, cache).unwrap();

Expand Down
4 changes: 2 additions & 2 deletions ethcore/light/src/client/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ mod tests {
use std::sync::Arc;
use cache::Cache;
use client::fetch;
use time::Duration;
use std::time::Duration;
use parking_lot::Mutex;
use kvdb_memorydb;
use ethcore::db::NUM_COLUMNS;
Expand All @@ -116,7 +116,7 @@ mod tests {
fn it_works() {
let db = Arc::new(kvdb_memorydb::create(NUM_COLUMNS.unwrap_or(0)));
let spec = Spec::new_test();
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::hours(6))));
let cache = Arc::new(Mutex::new(Cache::new(Default::default(), Duration::from_secs(6 * 3600))));

Service::start(Default::default(), &spec, fetch::unavailable(), db, cache).unwrap();
}
Expand Down
1 change: 0 additions & 1 deletion ethcore/light/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ extern crate rlp_derive;
extern crate serde;
extern crate smallvec;
extern crate stats;
extern crate time;
extern crate vm;
extern crate keccak_hash as hash;
extern crate triehash;
Expand Down
18 changes: 9 additions & 9 deletions ethcore/light/src/net/load_timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
use std::collections::{HashMap, VecDeque};
use std::fs::File;
use std::path::PathBuf;
use std::time::{Duration, Instant};

use request::{CompleteRequest, Kind};

use bincode;
use time;
use parking_lot::{RwLock, Mutex};

/// Number of time periods samples should be kept for.
Expand Down Expand Up @@ -107,7 +107,7 @@ impl LoadDistribution {
};

LoadTimer {
start: time::precise_time_ns(),
start: Instant::now(),
n: n,
dist: self,
kind: kind,
Expand Down Expand Up @@ -151,10 +151,10 @@ impl LoadDistribution {
store.store(&*samples);
}

fn update(&self, kind: Kind, elapsed: u64, n: u64) {
fn update(&self, kind: Kind, elapsed: Duration, n: u64) {
macro_rules! update_counters {
($counters: expr) => {
$counters.0 = $counters.0.saturating_add(elapsed);
$counters.0 = $counters.0.saturating_add({ elapsed.as_secs() * 1_000_000_000 + elapsed.subsec_nanos() as u64 });
$counters.1 = $counters.1.saturating_add(n);
}
};
Expand All @@ -180,15 +180,15 @@ impl LoadDistribution {
/// A timer for a single request.
/// On drop, this will update the distribution.
pub struct LoadTimer<'a> {
start: u64,
start: Instant,
n: u64,
dist: &'a LoadDistribution,
kind: Kind,
}

impl<'a> Drop for LoadTimer<'a> {
fn drop(&mut self) {
let elapsed = time::precise_time_ns() - self.start;
let elapsed = self.start.elapsed();
self.dist.update(self.kind, elapsed, self.n);
}
}
Expand Down Expand Up @@ -225,7 +225,7 @@ mod tests {
let dist = LoadDistribution::load(&NullStore);
assert_eq!(dist.expected_time_ns(Kind::Headers), hardcoded_serve_time(Kind::Headers));

dist.update(Kind::Headers, 100_000, 100);
dist.update(Kind::Headers, Duration::new(0, 100_000), 100);
dist.end_period(&NullStore);

assert_eq!(dist.expected_time_ns(Kind::Headers), 1000);
Expand All @@ -238,7 +238,7 @@ mod tests {
let mut sum = 0;

for (i, x) in (0..10).map(|x| x * 10_000).enumerate() {
dist.update(Kind::Headers, x, 1);
dist.update(Kind::Headers, Duration::new(0, x), 1);
dist.end_period(&NullStore);

sum += x;
Expand All @@ -248,7 +248,7 @@ mod tests {

// should be weighted below the maximum entry.
let arith_average = (sum as f64 / (i + 1) as f64) as u64;
assert!(moving_average < x);
assert!(moving_average < x as u64);

// when there are only 2 entries, they should be equal due to choice of
// ALPHA = 1/N.
Expand Down
Loading

0 comments on commit 2c5ec71

Please sign in to comment.