Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocking the system clock #5123

Merged
merged 3 commits into from
Nov 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion chain/chain-primitives/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::fmt::{self, Display};
use std::io;

use chrono::{DateTime, Utc};
use chrono::DateTime;
use near_primitives::time::Utc;

use failure::{Backtrace, Context, Fail};
use log::error;

Expand Down
8 changes: 4 additions & 4 deletions chain/chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use std::time::{Duration as TimeDuration, Instant};

use borsh::BorshSerialize;
use chrono::Duration;
use chrono::Utc;
use itertools::Itertools;
use near_primitives::time::{Clock, Utc};
use rand::rngs::StdRng;
use rand::seq::SliceRandom;
use rand::SeedableRng;
Expand Down Expand Up @@ -476,7 +476,7 @@ impl Chain {
self.orphans.add(Orphan {
block: block.clone(),
provenance: Provenance::NONE,
added: Instant::now(),
added: Clock::instant(),
});
Ok(())
}
Expand Down Expand Up @@ -1145,7 +1145,7 @@ impl Chain {
// we only add blocks that couldn't have been gc'ed to the orphan pool.
if block_height >= tail_height {
let block_hash = *block.hash();
let orphan = Orphan { block, provenance, added: Instant::now() };
let orphan = Orphan { block, provenance, added: Clock::instant() };

self.orphans.add(orphan);

Expand All @@ -1165,7 +1165,7 @@ impl Chain {
ErrorKind::ChunksMissing(missing_chunks) => {
let block_hash = *block.hash();
block_misses_chunks(missing_chunks.clone());
let orphan = Orphan { block, provenance, added: Instant::now() };
let orphan = Orphan { block, provenance, added: Clock::instant() };

self.blocks_with_missing_chunks.add_block_with_missing_chunks(
orphan,
Expand Down
68 changes: 55 additions & 13 deletions chain/chain/src/doomslug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::time::{Duration, Instant};
use near_crypto::Signature;
use near_primitives::block::{Approval, ApprovalInner};
use near_primitives::hash::CryptoHash;
use near_primitives::time::Clock;
use near_primitives::types::{AccountId, ApprovalStake, Balance, BlockHeight, BlockHeightDelta};
use near_primitives::validator_signer::ValidatorSigner;

Expand Down Expand Up @@ -296,8 +297,8 @@ impl Doomslug {
tip: DoomslugTip { block_hash: CryptoHash::default(), height: 0 },
endorsement_pending: false,
timer: DoomslugTimer {
started: Instant::now(),
last_endorsement_sent: Instant::now(),
started: Clock::instant(),
last_endorsement_sent: Clock::instant(),
height: 0,
endorsement_delay,
min_delay,
Expand Down Expand Up @@ -596,11 +597,12 @@ impl Doomslug {
#[cfg(test)]
mod tests {
use std::sync::Arc;
use std::time::{Duration, Instant};
use std::time::Duration;

use near_crypto::{KeyType, SecretKey};
use near_primitives::block::{Approval, ApprovalInner};
use near_primitives::hash::hash;
use near_primitives::time::Clock;
use near_primitives::types::{AccountId, ApprovalStake};
use near_primitives::validator_signer::InMemoryValidatorSigner;

Expand All @@ -625,7 +627,7 @@ mod tests {
DoomslugThresholdMode::TwoThirds,
);

let mut now = Instant::now(); // For the test purposes the absolute value of the initial instant doesn't matter
let mut now = Clock::instant(); // For the test purposes the absolute value of the initial instant doesn't matter

// Set a new tip, must produce an endorsement
ds.set_tip(now, hash(&[1]), 1, 1);
Expand Down Expand Up @@ -781,7 +783,7 @@ mod tests {
DoomslugThresholdMode::TwoThirds,
);

let mut now = Instant::now();
let mut now = Clock::instant();

// In the comments below the format is
// account, height -> approved stake
Expand Down Expand Up @@ -914,7 +916,12 @@ mod tests {
let a2_3 = Approval::new(hash(&[3]), 3, 4, &signers[2]);

// Process first approval, and then process it again and make sure it works
tracker.process_approval(Instant::now(), &a1_1, &stakes, DoomslugThresholdMode::TwoThirds);
tracker.process_approval(
Clock::instant(),
&a1_1,
&stakes,
DoomslugThresholdMode::TwoThirds,
);

assert_eq!(
tracker
Expand All @@ -934,7 +941,12 @@ mod tests {
0
);

tracker.process_approval(Instant::now(), &a1_1, &stakes, DoomslugThresholdMode::TwoThirds);
tracker.process_approval(
Clock::instant(),
&a1_1,
&stakes,
DoomslugThresholdMode::TwoThirds,
);

assert_eq!(
tracker
Expand All @@ -955,8 +967,18 @@ mod tests {
);

// Process the remaining two approvals on the first block
tracker.process_approval(Instant::now(), &a1_2, &stakes, DoomslugThresholdMode::TwoThirds);
tracker.process_approval(Instant::now(), &a1_3, &stakes, DoomslugThresholdMode::TwoThirds);
tracker.process_approval(
Clock::instant(),
&a1_2,
&stakes,
DoomslugThresholdMode::TwoThirds,
);
tracker.process_approval(
Clock::instant(),
&a1_3,
&stakes,
DoomslugThresholdMode::TwoThirds,
);

assert_eq!(
tracker
Expand All @@ -977,7 +999,12 @@ mod tests {
);

// Process new approvals one by one, expect the approved and endorsed stake to slowly decrease
tracker.process_approval(Instant::now(), &a2_1, &stakes, DoomslugThresholdMode::TwoThirds);
tracker.process_approval(
Clock::instant(),
&a2_1,
&stakes,
DoomslugThresholdMode::TwoThirds,
);

assert_eq!(
tracker
Expand All @@ -997,7 +1024,12 @@ mod tests {
5
);

tracker.process_approval(Instant::now(), &a2_2, &stakes, DoomslugThresholdMode::TwoThirds);
tracker.process_approval(
Clock::instant(),
&a2_2,
&stakes,
DoomslugThresholdMode::TwoThirds,
);

assert_eq!(
tracker
Expand All @@ -1018,7 +1050,12 @@ mod tests {
);

// As we update the last of the three approvals, the tracker for the first block should be completely removed
tracker.process_approval(Instant::now(), &a2_3, &stakes, DoomslugThresholdMode::TwoThirds);
tracker.process_approval(
Clock::instant(),
&a2_3,
&stakes,
DoomslugThresholdMode::TwoThirds,
);

assert!(tracker.approval_trackers.get(&ApprovalInner::Skip(1)).is_none());

Expand All @@ -1043,7 +1080,12 @@ mod tests {
5
);

tracker.process_approval(Instant::now(), &a2_3, &stakes, DoomslugThresholdMode::TwoThirds);
tracker.process_approval(
Clock::instant(),
&a2_3,
&stakes,
DoomslugThresholdMode::TwoThirds,
);

assert_eq!(
tracker
Expand Down
2 changes: 1 addition & 1 deletion chain/chain/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;

use borsh::{BorshDeserialize, BorshSerialize};
use cached::{Cached, SizedCache};
use chrono::Utc;
use near_primitives::time::Utc;

use near_chain_primitives::error::{Error, ErrorKind};
use near_primitives::block::{Approval, Tip};
Expand Down
5 changes: 3 additions & 2 deletions chain/chain/src/store_validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use validate::StoreValidatorError;

use crate::RuntimeAdapter;
use near_primitives::shard_layout::get_block_shard_uid_rev;
use near_primitives::time::Clock;

mod validate;

Expand Down Expand Up @@ -98,7 +99,7 @@ impl StoreValidator {
store: store.clone(),
inner: StoreValidatorCache::new(),
timeout: None,
start_time: Instant::now(),
start_time: Clock::instant(),
errors: vec![],
tests: 0,
}
Expand Down Expand Up @@ -345,7 +346,7 @@ impl StoreValidator {
Ok(())
}
pub fn validate(&mut self) {
self.start_time = Instant::now();
self.start_time = Clock::instant();

// Init checks
// Check Head-Tail validity and fill cache with their values
Expand Down
16 changes: 9 additions & 7 deletions chain/chain/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::{HashMap, HashSet};
use std::sync::{Arc, RwLock};

use borsh::{BorshDeserialize, BorshSerialize};
use chrono::Utc;

use num_rational::Rational;
use tracing::debug;

Expand Down Expand Up @@ -56,6 +56,7 @@ use crate::types::{
use crate::Doomslug;
use crate::{BlockHeader, DoomslugThresholdMode, RuntimeAdapter};
use near_primitives::epoch_manager::ShardConfig;
use near_primitives::time::Clock;

#[derive(BorshSerialize, BorshDeserialize, Hash, PartialEq, Eq, Ord, PartialOrd, Clone, Debug)]
struct AccountNonce(AccountId, Nonce);
Expand Down Expand Up @@ -1220,7 +1221,7 @@ pub fn setup_with_tx_validity_period(
let chain = Chain::new(
runtime.clone(),
&ChainGenesis {
time: Utc::now(),
time: Clock::utc(),
height: 0,
gas_limit: 1_000_000,
min_gas_price: 100,
Expand Down Expand Up @@ -1267,7 +1268,7 @@ pub fn setup_with_validators(
let chain = Chain::new(
runtime.clone(),
&ChainGenesis {
time: Utc::now(),
time: Clock::utc(),
height: 0,
gas_limit: 1_000_000,
min_gas_price: 100,
Expand Down Expand Up @@ -1386,7 +1387,7 @@ pub fn display_chain(me: &Option<AccountId>, chain: &mut Chain, tail: bool) {
impl ChainGenesis {
pub fn test() -> Self {
ChainGenesis {
time: Utc::now(),
time: Clock::utc(),
height: 0,
gas_limit: 1_000_000,
min_gas_price: 0,
Expand All @@ -1402,14 +1403,15 @@ impl ChainGenesis {

#[cfg(test)]
mod test {
use std::time::Instant;
use std::convert::TryFrom;

use borsh::BorshSerialize;
use rand::Rng;

use near_primitives::hash::{hash, CryptoHash};
use near_primitives::receipt::Receipt;
use near_primitives::sharding::ReceiptList;
use near_primitives::time::Clock;
use near_primitives::types::{AccountId, EpochId, NumShards};
use near_store::test_utils::create_test_store;

Expand Down Expand Up @@ -1459,10 +1461,10 @@ mod test {
)
})
.collect::<Vec<_>>();
let start = Instant::now();
let start = Clock::instant();
let naive_result = runtime_adapter.naive_build_receipt_hashes(&receipts);
let naive_duration = start.elapsed();
let start = Instant::now();
let start = Clock::instant();
let shard_layout = runtime_adapter.get_shard_layout(&EpochId::default()).unwrap();
let prod_result = runtime_adapter.build_receipts_hashes(&receipts, &shard_layout);
let prod_duration = start.elapsed();
Expand Down
5 changes: 3 additions & 2 deletions chain/chain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::collections::HashMap;
use std::sync::Arc;

use borsh::{BorshDeserialize, BorshSerialize};
use chrono::{DateTime, Utc};
use chrono::DateTime;
use near_primitives::time::Utc;
use num_rational::Rational;
use serde::Serialize;

Expand Down Expand Up @@ -800,7 +801,7 @@ pub enum ValidatorInfoIdentifier {

#[cfg(test)]
mod tests {
use chrono::Utc;
use near_primitives::time::Utc;

use near_crypto::KeyType;
use near_primitives::block::{genesis_chunks, Approval};
Expand Down
6 changes: 3 additions & 3 deletions chain/chain/tests/doomslug.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#[cfg(test)]
#[cfg(feature = "expensive_tests")]
mod tests {
use near_primitives::time::Clock;
use rand::{thread_rng, Rng};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use std::time::{Duration, Instant};

use rand::{thread_rng, Rng};

use near_chain::{Doomslug, DoomslugThresholdMode};
use near_crypto::{KeyType, SecretKey};
use near_primitives::block::Approval;
Expand Down Expand Up @@ -75,7 +75,7 @@ mod tests {
})
.collect::<Vec<_>>();

let mut now = Instant::now();
let mut now = Clock::instant();
let started = now;

let gst = now + time_to_gst;
Expand Down
Loading