diff --git a/Cargo.lock b/Cargo.lock index f6295df72..45dccede0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1570,7 +1570,7 @@ dependencies = [ [[package]] name = "grin_api" -version = "5.2.0" +version = "5.3.0" dependencies = [ "bytes 0.5.6", "chrono", @@ -1603,7 +1603,7 @@ dependencies = [ [[package]] name = "grin_chain" -version = "5.2.0" +version = "5.3.0" dependencies = [ "bit-vec", "bitflags 1.3.2", @@ -1627,7 +1627,7 @@ dependencies = [ [[package]] name = "grin_config" -version = "5.2.0" +version = "5.3.0" dependencies = [ "dirs 2.0.2", "grin_core", @@ -1644,7 +1644,7 @@ dependencies = [ [[package]] name = "grin_core" -version = "5.2.0" +version = "5.3.0" dependencies = [ "blake2-rfc", "byteorder", @@ -1670,7 +1670,7 @@ dependencies = [ [[package]] name = "grin_keychain" -version = "5.2.0" +version = "5.3.0" dependencies = [ "blake2-rfc", "byteorder", @@ -1692,7 +1692,7 @@ dependencies = [ [[package]] name = "grin_p2p" -version = "5.2.0" +version = "5.3.0" dependencies = [ "async-std", "bitflags 1.3.2", @@ -1726,7 +1726,7 @@ dependencies = [ [[package]] name = "grin_pool" -version = "5.2.0" +version = "5.3.0" dependencies = [ "blake2-rfc", "chrono", @@ -1758,7 +1758,7 @@ dependencies = [ [[package]] name = "grin_servers" -version = "5.2.0" +version = "5.3.0" dependencies = [ "atomic_float", "chrono", @@ -1795,7 +1795,7 @@ dependencies = [ [[package]] name = "grin_store" -version = "5.2.0" +version = "5.3.0" dependencies = [ "byteorder", "chrono", @@ -1817,7 +1817,7 @@ dependencies = [ [[package]] name = "grin_util" -version = "5.2.0" +version = "5.3.0" dependencies = [ "backtrace", "base64 0.12.3", @@ -2830,7 +2830,7 @@ dependencies = [ [[package]] name = "mwc" -version = "5.2.0" +version = "5.3.0" dependencies = [ "blake2-rfc", "built", diff --git a/core/src/core/block.rs b/core/src/core/block.rs index 43b0c03b9..7762905b0 100644 --- a/core/src/core/block.rs +++ b/core/src/core/block.rs @@ -240,7 +240,10 @@ impl Default for BlockHeader { BlockHeader { version: HeaderVersion(1), height: 0, - timestamp: DateTime::::from_utc(NaiveDateTime::from_timestamp(0, 0), Utc), + timestamp: DateTime::::from_utc( + NaiveDateTime::from_timestamp_opt(0, 0).unwrap(), + Utc, + ), prev_hash: ZERO_HASH, prev_root: ZERO_HASH, output_root: ZERO_HASH, @@ -297,8 +300,16 @@ fn read_block_header(reader: &mut R) -> Result MAX_DATE.and_hms(0, 0, 0).timestamp() - || timestamp < MIN_DATE.and_hms(0, 0, 0).timestamp() + if timestamp + > chrono::NaiveDate::MAX + .and_hms_opt(0, 0, 0) + .unwrap() + .timestamp() + || timestamp + < chrono::NaiveDate::MIN + .and_hms_opt(0, 0, 0) + .unwrap() + .timestamp() { return Err(ser::Error::CorruptedData(format!( "Incorrect timestamp {} at block header", @@ -306,10 +317,15 @@ fn read_block_header(reader: &mut R) -> Result::from_utc(NaiveDateTime::from_timestamp(timestamp, 0), Utc), + timestamp: DateTime::::from_utc(ts.unwrap(), Utc), prev_hash, prev_root, output_root, @@ -681,8 +697,12 @@ impl Block { let version = consensus::header_version(height); let now = Utc::now().timestamp(); - let timestamp = DateTime::::from_utc(NaiveDateTime::from_timestamp(now, 0), Utc); + let ts = NaiveDateTime::from_timestamp_opt(now, 0); + if ts.is_none() { + return Err(Error::Other("Converting Utc::now() into timestamp".into())); + } + let timestamp = DateTime::::from_utc(ts.unwrap(), Utc); // Now build the block with all the above information. // Note: We have not validated the block here. // Caller must validate the block as necessary. diff --git a/core/src/genesis.rs b/core/src/genesis.rs index 3bdf61f58..95590d009 100644 --- a/core/src/genesis.rs +++ b/core/src/genesis.rs @@ -34,7 +34,7 @@ use util::secp::Signature; pub fn genesis_dev() -> core::Block { core::Block::with_header(core::BlockHeader { height: 0, - timestamp: Utc.ymd(1997, 8, 4).and_hms(0, 0, 0), + timestamp: Utc.with_ymd_and_hms(1997, 8, 4, 0, 0, 0).unwrap(), pow: ProofOfWork { nonce: 0, ..Default::default() @@ -47,7 +47,7 @@ pub fn genesis_dev() -> core::Block { pub fn genesis_floo() -> core::Block { let gen = core::Block::with_header(core::BlockHeader { height: 0, - timestamp: Utc.ymd(2019, 5, 26).and_hms(16, 30, 1), + timestamp: Utc.with_ymd_and_hms(2019, 5, 26, 16, 30, 1).unwrap(), prev_root: Hash::from_hex( "000000000000000000257647fb29ce964ddf2b27c639ae60c4c90fafe5c42e53", ) @@ -160,7 +160,7 @@ pub fn genesis_floo() -> core::Block { pub fn genesis_main() -> core::Block { let gen = core::Block::with_header(core::BlockHeader { height: 0, - timestamp: Utc.ymd(2019, 11, 11).and_hms(9, 0, 0), + timestamp: Utc.with_ymd_and_hms(2019, 11, 11, 9, 0, 0).unwrap(), prev_root: Hash::from_hex( "00000000000000000004f2fb2ee749923a8131028aeae637070b0b4145617d42", ) diff --git a/core/src/pow.rs b/core/src/pow.rs index 58f301db5..7328eb1e6 100644 --- a/core/src/pow.rs +++ b/core/src/pow.rs @@ -125,7 +125,8 @@ pub fn pow_size( // and if we're back where we started, update the time (changes the hash as // well) if bh.pow.nonce == start_nonce { - bh.timestamp = DateTime::::from_utc(NaiveDateTime::from_timestamp(0, 0), Utc); + bh.timestamp = + DateTime::::from_utc(NaiveDateTime::from_timestamp_opt(0, 0).unwrap(), Utc); } } } diff --git a/p2p/src/peers.rs b/p2p/src/peers.rs index e8aabc139..0424e589f 100644 --- a/p2p/src/peers.rs +++ b/p2p/src/peers.rs @@ -447,7 +447,7 @@ impl Peers { // Delete defunct peers from storage let _ = self.store.delete_peers(|peer| { - let diff = now - Utc.timestamp(peer.last_connected, 0); + let diff = now - Utc.timestamp_opt(peer.last_connected, 0).unwrap(); let should_remove = peer.flags == State::Defunct && diff > Duration::seconds(global::PEER_EXPIRATION_REMOVE_TIME); diff --git a/servers/src/grin/seed.rs b/servers/src/grin/seed.rs index 498500d3c..bfd8d200e 100644 --- a/servers/src/grin/seed.rs +++ b/servers/src/grin/seed.rs @@ -64,8 +64,8 @@ pub fn connect_and_monitor( libp2p_connection::set_seed_list(&seed_list, true); - let mut prev = MIN_DATE.and_hms(0, 0, 0); - let mut prev_expire_check = MIN_DATE.and_hms(0, 0, 0); + let mut prev = DateTime::::MIN_UTC; + let mut prev_expire_check = DateTime::::MIN_UTC; let mut prev_ping = Utc::now(); let mut start_attempt = 0; let mut connecting_history: HashMap> = HashMap::new(); diff --git a/servers/src/mining/mine_block.rs b/servers/src/mining/mine_block.rs index e9c2403e7..abd878ea8 100644 --- a/servers/src/mining/mine_block.rs +++ b/servers/src/mining/mine_block.rs @@ -168,7 +168,11 @@ fn build_block( b.header.pow.nonce = thread_rng().gen(); b.header.pow.secondary_scaling = difficulty.secondary_scaling; - b.header.timestamp = DateTime::::from_utc(NaiveDateTime::from_timestamp(now_sec, 0), Utc); + let ts = NaiveDateTime::from_timestamp_opt(now_sec, 0); + if ts.is_none() { + return Err(Error::General("Utc::now into timestamp".into())); + } + b.header.timestamp = DateTime::::from_utc(ts.unwrap(), Utc); debug!( "Built new block with {} inputs and {} outputs, block difficulty: {}, cumulative difficulty {}", diff --git a/src/bin/tui/mining.rs b/src/bin/tui/mining.rs index c951008d0..3a980a191 100644 --- a/src/bin/tui/mining.rs +++ b/src/bin/tui/mining.rs @@ -65,13 +65,14 @@ impl StratumWorkerColumn { impl TableViewItem for WorkerStats { fn to_column(&self, column: StratumWorkerColumn) -> String { - let naive_datetime = NaiveDateTime::from_timestamp( + let naive_datetime = NaiveDateTime::from_timestamp_opt( self.last_seen .duration_since(time::UNIX_EPOCH) .unwrap() .as_secs() as i64, 0, - ); + ) + .unwrap_or_default(); let datetime: DateTime = DateTime::from_utc(naive_datetime, Utc); match column { @@ -127,7 +128,8 @@ impl DiffColumn { impl TableViewItem for DiffBlock { fn to_column(&self, column: DiffColumn) -> String { - let naive_datetime = NaiveDateTime::from_timestamp(self.time as i64, 0); + let naive_datetime = + NaiveDateTime::from_timestamp_opt(self.time as i64, 0).unwrap_or_default(); let datetime: DateTime = DateTime::from_utc(naive_datetime, Utc); match column {