Skip to content

Commit

Permalink
feat: use crate faketime to fake time (#111)
Browse files Browse the repository at this point in the history
The time crate is moved into [a separate repository](https://github.com/nervosnetwork/faketime).

To fake time in test:

- Use `faketime::millis_tempfile` to create a temp timestamp file.
- Enable faketime via `faketime::enable` in current thread.

To fake time in child threads:

- Use `faketime::millis_tempfile` to create a temp timestamp file.
- Set child thread name to `FAKETIME=PATH`, where PATH is the path to the
timestamp file.

To fake time of the generated binary, set the environment variable

```
echo 123456 > /tmp/faketime
FAKETIME=/tmp/faketime ckb run
```
  • Loading branch information
doitian authored Dec 24, 2018
1 parent 02f73ba commit 5adfd82
Show file tree
Hide file tree
Showing 27 changed files with 337 additions and 327 deletions.
25 changes: 15 additions & 10 deletions Cargo.lock

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

8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ check:
build:
cargo build --release

prod:
RUSTFLAGS="--cfg disable_faketime" cargo build --release

prod-test:
RUSTFLAGS="--cfg disable_faketime" RUSTDOCFLAGS="--cfg disable_faketime" cargo test --all -- --nocapture

fmt:
cargo fmt --all -- --check

Expand Down Expand Up @@ -47,6 +53,6 @@ security-audit:
docker: build
docker build -f docker/hub/Dockerfile -t nervos/ckb:latest .

.PHONY: build build-integration-test docker
.PHONY: build build-integration-test prod prod-test docker
.PHONY: fmt test clippy proto doc doc-deps check stats
.PHONY: ci ci-quick info security-audit
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ git clone https://github.com/nervosnetwork/ckb.git
cd ckb

# build in release mode
cargo build --release
make build
```

You can run the full test suite, or just run a specific package test:
Expand Down
2 changes: 1 addition & 1 deletion chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ ckb-core = { path = "../core" }
ckb-shared = { path = "../shared" }
ckb-chain-spec = { path = "../spec" }
ckb-db = { path = "../db" }
ckb-time = { path = "../util/time" }
ckb-notify = { path = "../notify" }
ckb-verification = { path = "../verification" }
faketime = "0.2.0"
numext-fixed-hash = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"] }
numext-fixed-uint = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"] }
channel = { package= "crossbeam-channel", version = "0.3" }
Expand Down
6 changes: 3 additions & 3 deletions chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use ckb_notify::{ForkBlocks, NotifyController, NotifyService};
use ckb_shared::error::SharedError;
use ckb_shared::index::ChainIndex;
use ckb_shared::shared::{ChainProvider, Shared, TipHeader};
use ckb_time::now_ms;
use ckb_verification::{BlockVerifier, Verifier};
use faketime::unix_time_as_millis;
use log::{self, debug, error, log_enabled};
use numext_fixed_hash::H256;
use numext_fixed_uint::U256;
Expand Down Expand Up @@ -149,7 +149,7 @@ impl<CI: ChainIndex + 'static> ChainService<CI> {
let cannon_total_difficulty = parent_ext.total_difficulty + block.header().difficulty();

let ext = BlockExt {
received_at: now_ms(),
received_at: unix_time_as_millis(),
total_difficulty: cannon_total_difficulty.clone(),
total_uncles_count: parent_ext.total_uncles_count + block.uncles().len() as u64,
};
Expand Down Expand Up @@ -435,7 +435,7 @@ pub mod test {
let cellbase = create_cellbase(number);
let header = HeaderBuilder::default()
.parent_hash(parent_header.hash().clone())
.timestamp(now_ms())
.timestamp(unix_time_as_millis())
.number(number)
.difficulty(difficulty)
.nonce(nonce)
Expand Down
2 changes: 1 addition & 1 deletion miner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ ckb-pow = { path = "../pow" }
ckb-util = { path = "../util" }
ckb-notify = { path = "../notify" }
ckb-pool = { path = "../pool" }
ckb-time = { path = "../util/time" }
faketime = "0.2.0"
rand = "0.6"
serde = "1.0"
serde_derive = "1.0"
Expand Down
4 changes: 2 additions & 2 deletions miner/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ckb_pool::txs_pool::TransactionPoolController;
use ckb_shared::error::SharedError;
use ckb_shared::index::ChainIndex;
use ckb_shared::shared::{ChainProvider, Shared};
use ckb_time::now_ms;
use faketime::unix_time_as_millis;
use fnv::{FnvHashMap, FnvHashSet};
use log::error;
use numext_fixed_hash::H256;
Expand Down Expand Up @@ -123,7 +123,7 @@ impl<CI: ChainIndex + 'static> Agent<CI> {
let (cellbase, commit_transactions, proposal_transactions, header_builder) = {
let tip_header = self.shared.tip_header().read();
let header = tip_header.inner();
let now = cmp::max(now_ms(), header.timestamp() + 1);
let now = cmp::max(unix_time_as_millis(), header.timestamp() + 1);
let difficulty = self
.shared
.calculate_difficulty(header)
Expand Down
2 changes: 1 addition & 1 deletion network/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ tokio = "0.1.8"
futures = { version = "0.1.19", features = ["use_std"] }
snap = "0.2"
libp2p = { git = "https://github.com/libp2p/rust-libp2p", rev="cfdfca1a06fb2deb9ebcc15a63d715ebddb23bd0", default-features = false, features = ["libp2p-secio", "libp2p-secio-secp256k1"] }
ckb-time = { path = "../util/time" }
faketime = "0.2.0"
6 changes: 4 additions & 2 deletions network/src/ckb_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::protocol_service::ProtocolService;
use crate::CKBProtocolHandler;
use crate::Network;
use crate::PeerId;
use ckb_time::now_ms;
use faketime::unix_time_as_millis;
use futures::future::{self, Future};
use futures::Stream;
use libp2p::core::{Endpoint, Multiaddr, UniqueConnecState};
Expand Down Expand Up @@ -82,7 +82,9 @@ impl CKBService {
move |data| {
// update kad_system when we received data
kad_system.update_kbuckets(peer_id.clone());
network.modify_peer(&peer_id, |peer| peer.last_message_time = Some(now_ms()));
network.modify_peer(&peer_id, |peer| {
peer.last_message_time = Some(unix_time_as_millis())
});
let protocol_handler = Arc::clone(&protocol_handler);
let network = Arc::clone(&network);
let handle_received = future::lazy(move || {
Expand Down
4 changes: 2 additions & 2 deletions network/src/peers_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::network_group::{Group, NetworkGroup};
use crate::peer_store::PeerStore;
use crate::{Error, ErrorKind, PeerId, PeerIndex, ProtocolId};
use bytes::Bytes;
use ckb_time::now_ms;
use ckb_util::RwLock;
use faketime::unix_time_as_millis;
use fnv::{FnvHashMap, FnvHashSet};
use futures::sync::mpsc::UnboundedSender;
use libp2p::core::{Endpoint, Multiaddr, UniqueConnec};
Expand Down Expand Up @@ -380,7 +380,7 @@ impl PeersRegistry {
.write()
.new_connected_peer(&peer_id, connected_addr.clone());
let mut peer = PeerConnection::new(connected_addr, endpoint);
peer.connected_time = Some(now_ms());
peer.connected_time = Some(unix_time_as_millis());
let peer_index = self.peers.or_insert(peer_id.clone(), peer);
debug!(target: "network", "allocate peer_index {} to peer {:?}", peer_index, peer_id);
peer_index
Expand Down
6 changes: 3 additions & 3 deletions network/src/ping_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::protocol_service::ProtocolService;
use crate::transport::TransportOutput;
use crate::Network;
use crate::PeerId;
use ckb_time::now_ms;
use faketime::unix_time_as_millis;
use futures::future::{self, Future};
use futures::stream::FuturesUnordered;
use futures::Stream;
Expand Down Expand Up @@ -141,15 +141,15 @@ impl<T: Send> ProtocolService<T> for PingService {
})
}
});
let ping_start_time = now_ms();
let ping_start_time = unix_time_as_millis();
let ping_future =
Future::then(Timeout::new(ping_future, ping_timeout), {
let network = Arc::clone(&network);
move |result| -> Result<(), IoError> {
let mut peer_store = network.peer_store().write();
match result {
Ok(peer_id) => {
let now = now_ms();
let now = unix_time_as_millis();
let ping = now - ping_start_time;
network.modify_peer(&peer_id, |peer| {
peer.ping = Some(ping);
Expand Down
4 changes: 2 additions & 2 deletions network/src/tests/peers_registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::{
peers_registry::{PeersRegistry, EVICTION_PROTECT_PEERS},
random_peer_id, ToMultiaddr,
};
use ckb_time::now_ms;
use ckb_util::RwLock;
use faketime::unix_time_as_millis;
use std::default::Default;
use std::sync::Arc;

Expand Down Expand Up @@ -117,7 +117,7 @@ fn test_accept_inbound_peer_eviction() {
peer.ping = Some(0);
}
// peers which most recently sent messages
let now = now_ms();
let now = unix_time_as_millis();
for _ in 0..EVICTION_PROTECT_PEERS {
let peer_id = peers_iter.next().unwrap();
let mut peer = peers_registry.get_mut(&peer_id).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion pool/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ edition = "2018"
ckb-core = { path = "../core" }
ckb-shared = { path = "../shared" }
ckb-chain-spec = { path = "../spec" }
ckb-time = { path = "../util/time" }
ckb-notify = { path = "../notify" }
ckb-verification = { path = "../verification" }
faketime = "0.2.0"
numext-fixed-hash = { version = "0.1", features = ["support_rand", "support_heapsize", "support_serde"] }
lru-cache = { git = "https://github.com/nervosnetwork/lru-cache" }
linked-hash-map = { git = "https://github.com/nervosnetwork/linked-hash-map", rev = "df27f21" }
Expand Down
4 changes: 2 additions & 2 deletions pool/src/tests/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use ckb_notify::{ForkBlocks, MsgNewTip, MsgSwitchFork, NotifyService};
use ckb_shared::index::ChainIndex;
use ckb_shared::shared::{ChainProvider, Shared, SharedBuilder};
use ckb_shared::store::ChainKVStore;
use ckb_time::now_ms;
use faketime::unix_time_as_millis;
use log::error;
use numext_fixed_hash::H256;
use std::fs::File;
Expand Down Expand Up @@ -573,7 +573,7 @@ fn apply_transactions<CI: ChainIndex + 'static>(
let header_builder = HeaderBuilder::default()
.parent_hash(parent.hash().clone())
.number(parent.number() + 1)
.timestamp(now_ms())
.timestamp(unix_time_as_millis())
.cellbase_id(cellbase_id)
.difficulty(pool.shared.calculate_difficulty(&parent).unwrap());

Expand Down
3 changes: 1 addition & 2 deletions sync/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ fnv = "1.0"
ckb-protocol = { path = "../protocol" }
ckb-util = { path = "../util" }
ckb-pool = { path = "../pool" }
ckb-time = { path = "../util/time" }
faketime = "0.2.0"
bitflags = "1.0"
ckb-verification = { path = "../verification" }
serde = "1.0"
Expand All @@ -28,6 +28,5 @@ ckb-chain-spec = { path = "../spec" }
[dev-dependencies]
ckb-notify = { path = "../notify" }
ckb-db = { path = "../db" }
ckb-time = { path = "../util/time", features = ["mock_timer"] }
env_logger = "0.6"
crossbeam-channel = "0.3"
4 changes: 2 additions & 2 deletions sync/src/synchronizer/block_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use ckb_core::header::Header;
use ckb_network::PeerIndex;
use ckb_shared::index::ChainIndex;
use ckb_shared::shared::{ChainProvider, TipHeader};
use ckb_time::now_ms;
use ckb_util::{try_option, RwLockUpgradableReadGuard};
use faketime::unix_time_as_millis;
use log::debug;
use numext_fixed_hash::H256;
use std::cmp;
Expand Down Expand Up @@ -38,7 +38,7 @@ where
.entry(self.peer)
.or_insert_with(Default::default);

if inflight.timestamp < now_ms().saturating_sub(BLOCK_DOWNLOAD_TIMEOUT) {
if inflight.timestamp < unix_time_as_millis().saturating_sub(BLOCK_DOWNLOAD_TIMEOUT) {
debug!(target: "sync", "[block downloader] inflight block download timeout");
inflight.clear();
}
Expand Down
4 changes: 2 additions & 2 deletions sync/src/synchronizer/block_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ mod tests {
use ckb_chain_spec::consensus::Consensus;
use ckb_core::block::BlockBuilder;
use ckb_core::header::{Header, HeaderBuilder};
use ckb_time::now_ms;
use faketime::unix_time_as_millis;
use std::collections::HashSet;
use std::iter::FromIterator;

fn gen_block(parent_header: Header) -> Block {
let header = HeaderBuilder::default()
.parent_hash(parent_header.hash().clone())
.timestamp(now_ms())
.timestamp(unix_time_as_millis())
.number(parent_header.number() + 1)
.nonce(parent_header.nonce() + 1)
.build();
Expand Down
Loading

0 comments on commit 5adfd82

Please sign in to comment.