Skip to content

Commit d1b7ae5

Browse files
authored
warning cleanup (mimblewimble#3759)
1 parent a9f45de commit d1b7ae5

File tree

7 files changed

+45
-16
lines changed

7 files changed

+45
-16
lines changed

core/src/core/block.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,10 @@ impl Default for BlockHeader {
231231
BlockHeader {
232232
version: HeaderVersion(1),
233233
height: 0,
234-
timestamp: DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(0, 0), Utc),
234+
timestamp: DateTime::<Utc>::from_utc(
235+
NaiveDateTime::from_timestamp_opt(0, 0).unwrap(),
236+
Utc,
237+
),
235238
prev_hash: ZERO_HASH,
236239
prev_root: ZERO_HASH,
237240
output_root: ZERO_HASH,
@@ -288,16 +291,29 @@ fn read_block_header<R: Reader>(reader: &mut R) -> Result<BlockHeader, ser::Erro
288291
let (output_mmr_size, kernel_mmr_size) = ser_multiread!(reader, read_u64, read_u64);
289292
let pow = ProofOfWork::read(reader)?;
290293

291-
if timestamp > chrono::NaiveDate::MAX.and_hms(0, 0, 0).timestamp()
292-
|| timestamp < chrono::NaiveDate::MIN.and_hms(0, 0, 0).timestamp()
294+
if timestamp
295+
> chrono::NaiveDate::MAX
296+
.and_hms_opt(0, 0, 0)
297+
.unwrap()
298+
.timestamp()
299+
|| timestamp
300+
< chrono::NaiveDate::MIN
301+
.and_hms_opt(0, 0, 0)
302+
.unwrap()
303+
.timestamp()
293304
{
294305
return Err(ser::Error::CorruptedData);
295306
}
296307

308+
let ts = NaiveDateTime::from_timestamp_opt(timestamp, 0);
309+
if ts.is_none() {
310+
return Err(ser::Error::CorruptedData);
311+
}
312+
297313
Ok(BlockHeader {
298314
version,
299315
height,
300-
timestamp: DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(timestamp, 0), Utc),
316+
timestamp: DateTime::<Utc>::from_utc(ts.unwrap(), Utc),
301317
prev_hash,
302318
prev_root,
303319
output_root,
@@ -645,8 +661,13 @@ impl Block {
645661
let version = consensus::header_version(height);
646662

647663
let now = Utc::now().timestamp();
648-
let timestamp = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(now, 0), Utc);
649664

665+
let ts = NaiveDateTime::from_timestamp_opt(now, 0);
666+
if ts.is_none() {
667+
return Err(Error::Other("Converting Utc::now() into timestamp".into()));
668+
}
669+
670+
let timestamp = DateTime::<Utc>::from_utc(ts.unwrap(), Utc);
650671
// Now build the block with all the above information.
651672
// Note: We have not validated the block here.
652673
// Caller must validate the block as necessary.

core/src/genesis.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use util::secp::Signature;
3434
pub fn genesis_dev() -> core::Block {
3535
core::Block::with_header(core::BlockHeader {
3636
height: 0,
37-
timestamp: Utc.ymd(1997, 8, 4).and_hms(0, 0, 0),
37+
timestamp: Utc.with_ymd_and_hms(1997, 8, 4, 0, 0, 0).unwrap(),
3838
pow: ProofOfWork {
3939
nonce: 0,
4040
..Default::default()
@@ -48,7 +48,7 @@ pub fn genesis_dev() -> core::Block {
4848
pub fn genesis_test() -> core::Block {
4949
let gen = core::Block::with_header(core::BlockHeader {
5050
height: 0,
51-
timestamp: Utc.ymd(2018, 12, 28).and_hms(20, 48, 4),
51+
timestamp: Utc.with_ymd_and_hms(2018, 12, 28, 20, 48, 4).unwrap(),
5252
prev_root: Hash::from_hex(
5353
"00000000000000000017ff4903ef366c8f62e3151ba74e41b8332a126542f538",
5454
)
@@ -161,7 +161,7 @@ pub fn genesis_test() -> core::Block {
161161
pub fn genesis_main() -> core::Block {
162162
let gen = core::Block::with_header(core::BlockHeader {
163163
height: 0,
164-
timestamp: Utc.ymd(2019, 1, 15).and_hms(16, 1, 26),
164+
timestamp: Utc.with_ymd_and_hms(2019, 1, 15, 16, 1, 26).unwrap(),
165165
prev_root: Hash::from_hex(
166166
"0000000000000000002a8bc32f43277fe9c063b9c99ea252b483941dcd06e217",
167167
)

core/src/pow.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,8 @@ pub fn pow_size(
115115
// and if we're back where we started, update the time (changes the hash as
116116
// well)
117117
if bh.pow.nonce == start_nonce {
118-
bh.timestamp = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(0, 0), Utc);
118+
bh.timestamp =
119+
DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp_opt(0, 0).unwrap(), Utc);
119120
}
120121
}
121122
}

p2p/src/peers.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ impl Peers {
437437

438438
// Delete defunct peers from storage
439439
let _ = self.store.delete_peers(|peer| {
440-
let diff = now - Utc.timestamp(peer.last_connected, 0);
440+
let diff = now - Utc.timestamp_opt(peer.last_connected, 0).unwrap();
441441

442442
let should_remove = peer.flags == State::Defunct
443443
&& diff > Duration::seconds(global::PEER_EXPIRATION_REMOVE_TIME);

servers/src/grin/seed.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ pub fn connect_and_monitor(
6969
// check seeds first
7070
connect_to_seeds_and_peers(peers.clone(), tx.clone(), seed_list, config);
7171

72-
let mut prev = chrono::Date::<Utc>::MIN_UTC.and_hms(0, 0, 0);
73-
let mut prev_expire_check = chrono::Date::<Utc>::MIN_UTC.and_hms(0, 0, 0);
72+
let mut prev = DateTime::<Utc>::MIN_UTC;
73+
let mut prev_expire_check = DateTime::<Utc>::MIN_UTC;
74+
7475
let mut prev_ping = Utc::now();
7576
let mut start_attempt = 0;
7677
let mut connecting_history: HashMap<PeerAddr, DateTime<Utc>> = HashMap::new();

servers/src/mining/mine_block.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,11 @@ fn build_block(
168168

169169
b.header.pow.nonce = thread_rng().gen();
170170
b.header.pow.secondary_scaling = difficulty.secondary_scaling;
171-
b.header.timestamp = DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(now_sec, 0), Utc);
171+
let ts = NaiveDateTime::from_timestamp_opt(now_sec, 0);
172+
if ts.is_none() {
173+
return Err(Error::General("Utc::now into timestamp".into()));
174+
}
175+
b.header.timestamp = DateTime::<Utc>::from_utc(ts.unwrap(), Utc);
172176

173177
debug!(
174178
"Built new block with {} inputs and {} outputs, block difficulty: {}, cumulative difficulty {}",

src/bin/tui/mining.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,14 @@ impl StratumWorkerColumn {
6464

6565
impl TableViewItem<StratumWorkerColumn> for WorkerStats {
6666
fn to_column(&self, column: StratumWorkerColumn) -> String {
67-
let naive_datetime = NaiveDateTime::from_timestamp(
67+
let naive_datetime = NaiveDateTime::from_timestamp_opt(
6868
self.last_seen
6969
.duration_since(time::UNIX_EPOCH)
7070
.unwrap()
7171
.as_secs() as i64,
7272
0,
73-
);
73+
)
74+
.unwrap_or_default();
7475
let datetime: DateTime<Utc> = DateTime::from_utc(naive_datetime, Utc);
7576

7677
match column {
@@ -126,7 +127,8 @@ impl DiffColumn {
126127

127128
impl TableViewItem<DiffColumn> for DiffBlock {
128129
fn to_column(&self, column: DiffColumn) -> String {
129-
let naive_datetime = NaiveDateTime::from_timestamp(self.time as i64, 0);
130+
let naive_datetime =
131+
NaiveDateTime::from_timestamp_opt(self.time as i64, 0).unwrap_or_default();
130132
let datetime: DateTime<Utc> = DateTime::from_utc(naive_datetime, Utc);
131133

132134
match column {

0 commit comments

Comments
 (0)