Skip to content

Commit

Permalink
Merge pull request #108 from who-biz/resolve-warnings
Browse files Browse the repository at this point in the history
Resolve Compiler Warnings
- Updates zip dependency to 0.5.11, fix deprecation warns
- Fix unused documentation warns
- Unused assignment warnings (overwriting initialized value with identical value)
- Resolve "variant is never constructed" warns in TUI table
- Remove use of deprecated functions in DateTime, in favor of TimeZone module
  • Loading branch information
who-biz committed Sep 28, 2023
2 parents 73d19b5 + 4179d5b commit da04eeb
Show file tree
Hide file tree
Showing 10 changed files with 410 additions and 511 deletions.
816 changes: 350 additions & 466 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions chain/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,8 +520,6 @@ impl<'a> Iterator for DifficultyIter<'a> {
} else {
if let Some(ref store) = self.store {
prev_header = store.get_previous_header(&head).ok();
} else {
prev_header = None;
}
}

Expand Down Expand Up @@ -763,8 +761,6 @@ impl<'a> Iterator for BottleIter<'a> {
} else {
if let Some(ref store) = self.store {
prev_header = store.get_previous_header(&head).ok();
} else {
prev_header = None;
}
}

Expand Down
18 changes: 9 additions & 9 deletions core/src/core/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
pub mod feijoada;

use chrono;
use chrono::prelude::{DateTime, NaiveDateTime, Utc};
use chrono::Duration;
use chrono::{DateTime, NaiveDateTime, TimeZone, Utc};
use keccak_hash::keccak_256;
use std::collections::HashSet;
use std::convert::TryInto;
Expand Down Expand Up @@ -251,9 +251,9 @@ impl Default for BlockHeader {
BlockHeader {
version: HeaderVersion::default(),
height: 0,
timestamp: DateTime::<Utc>::from_utc(
NaiveDateTime::from_timestamp_opt(0, 0).unwrap(),
Utc,
timestamp: TimeZone::from_utc_datetime(
&Utc,
&NaiveDateTime::from_timestamp_opt(0, 0).unwrap(),
),
prev_hash: ZERO_HASH,
prev_root: ZERO_HASH,
Expand Down Expand Up @@ -343,9 +343,9 @@ fn read_block_header(reader: &mut dyn Reader) -> Result<BlockHeader, ser::Error>
Ok(BlockHeader {
version,
height,
timestamp: DateTime::<Utc>::from_utc(
NaiveDateTime::from_timestamp_opt(timestamp, 0).unwrap(),
Utc,
timestamp: TimeZone::from_utc_datetime(
&Utc,
&NaiveDateTime::from_timestamp_opt(timestamp, 0).unwrap(),
),
prev_hash,
prev_root,
Expand Down Expand Up @@ -685,7 +685,7 @@ impl Block {

let now = Utc::now().timestamp();
let timestamp =
DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp_opt(now, 0).unwrap(), Utc);
TimeZone::from_utc_datetime(&Utc, &NaiveDateTime::from_timestamp_opt(now, 0).unwrap());

// Now build the block with all the above information.
// Note: We have not validated the block here.
Expand Down Expand Up @@ -735,7 +735,7 @@ impl Block {
let height = prev.height + 1;
let now = Utc::now().timestamp();
let timestamp =
DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp_opt(now, 0).unwrap(), Utc);
TimeZone::from_utc_datetime(&Utc, &NaiveDateTime::from_timestamp_opt(now, 0).unwrap());

// Now build the block with all the above information.
// Note: We have not validated the block here.
Expand Down
6 changes: 3 additions & 3 deletions core/src/core/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ impl<H: Hashed> ShortIdentifiable for H {
pub struct ShortId([u8; 6]);

impl DefaultHashable for ShortId {}
/// We want to sort short_ids in a canonical and consistent manner so we can
/// verify sort order in the same way we do for full inputs|outputs|kernels
/// themselves.
// We want to sort short_ids in a canonical and consistent manner so we can
// verify sort order in the same way we do for full inputs|outputs|kernels
// themselves.
hashable_ord!(ShortId);

impl ::std::fmt::Debug for ShortId {
Expand Down
8 changes: 5 additions & 3 deletions core/src/pow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ mod types;
use crate::core::{Block, BlockHeader};
use crate::genesis;
use crate::global;
use chrono::prelude::{DateTime, NaiveDateTime, Utc};
use chrono::{NaiveDateTime, TimeZone, Utc};

pub use self::common::EdgeType;
pub use self::types::*;
Expand Down Expand Up @@ -144,8 +144,10 @@ 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::<Utc>::from_utc(NaiveDateTime::from_timestamp_opt(0, 0).unwrap(), Utc);
bh.timestamp = TimeZone::from_utc_datetime(
&Utc,
&NaiveDateTime::from_timestamp_opt(0, 0).unwrap(),
);
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions servers/src/mining/mine_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
//! them into a block and returns it.

use crate::util::RwLock;
use chrono::prelude::{DateTime, NaiveDateTime, Utc};
use chrono::{NaiveDateTime, TimeZone, Utc};
use rand::{thread_rng, Rng};
use serde_json::{json, Value};
use std::sync::Arc;
Expand Down Expand Up @@ -190,8 +190,10 @@ fn build_block(
b.header.pow.seed = seed_u8;
b.header.pow.nonce = thread_rng().gen();
b.header.pow.secondary_scaling = difficulty.secondary_scaling;
b.header.timestamp =
DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp_opt(now_sec, 0).unwrap(), Utc);
b.header.timestamp = TimeZone::from_utc_datetime(
&Utc,
&NaiveDateTime::from_timestamp_opt(now_sec, 0).unwrap(),
);
b.header.policy = get_emitted_policy(b.header.height);

let bottle_cursor = chain.bottles_iter(get_emitted_policy(b.header.height))?;
Expand Down
34 changes: 17 additions & 17 deletions src/bin/tui/mining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use std::cmp::Ordering;

use crate::tui::chrono::prelude::{DateTime, NaiveDateTime, Utc};
use crate::tui::chrono::prelude::{NaiveDateTime, TimeZone, Utc};
use cursive::direction::Orientation;
use cursive::event::Key;
use cursive::traits::{Boxable, Identifiable};
Expand All @@ -41,7 +41,7 @@ enum StratumWorkerColumn {
Id,
IsConnected,
LastSeen,
PowDifficulty,
// PowDifficulty,
NumAccepted,
NumRejected,
NumStale,
Expand All @@ -54,7 +54,7 @@ impl StratumWorkerColumn {
StratumWorkerColumn::Id => "Worker ID",
StratumWorkerColumn::IsConnected => "Connected",
StratumWorkerColumn::LastSeen => "Last Seen",
StratumWorkerColumn::PowDifficulty => "PowDifficulty",
// StratumWorkerColumn::PowDifficulty => "PowDifficulty",
StratumWorkerColumn::NumAccepted => "Num Accepted",
StratumWorkerColumn::NumRejected => "Num Rejected",
StratumWorkerColumn::NumStale => "Num Stale",
Expand All @@ -73,13 +73,13 @@ impl TableViewItem<StratumWorkerColumn> for WorkerStats {
0,
)
.unwrap();
let datetime: DateTime<Utc> = DateTime::from_utc(naive_datetime, Utc);
let datetime = TimeZone::from_utc_datetime(&Utc, &naive_datetime);

match column {
StratumWorkerColumn::Id => self.id.clone(),
StratumWorkerColumn::IsConnected => self.is_connected.to_string(),
StratumWorkerColumn::LastSeen => datetime.to_string(),
StratumWorkerColumn::PowDifficulty => self.pow_difficulty.to_string(),
// StratumWorkerColumn::PowDifficulty => self.pow_difficulty.to_string(),
StratumWorkerColumn::NumAccepted => self.num_accepted.to_string(),
StratumWorkerColumn::NumRejected => self.num_rejected.to_string(),
StratumWorkerColumn::NumStale => self.num_stale.to_string(),
Expand All @@ -95,7 +95,7 @@ impl TableViewItem<StratumWorkerColumn> for WorkerStats {
StratumWorkerColumn::Id => Ordering::Equal,
StratumWorkerColumn::IsConnected => Ordering::Equal,
StratumWorkerColumn::LastSeen => Ordering::Equal,
StratumWorkerColumn::PowDifficulty => Ordering::Equal,
// StratumWorkerColumn::PowDifficulty => Ordering::Equal,
StratumWorkerColumn::NumAccepted => Ordering::Equal,
StratumWorkerColumn::NumRejected => Ordering::Equal,
StratumWorkerColumn::NumStale => Ordering::Equal,
Expand All @@ -109,7 +109,7 @@ enum DiffColumn {
Hash,
PoWType,
Difficulty,
SecondaryScaling,
// SecondaryScaling,
Time,
Duration,
}
Expand All @@ -121,7 +121,7 @@ impl DiffColumn {
DiffColumn::Hash => "Hash",
DiffColumn::PoWType => "Algorithm",
DiffColumn::Difficulty => "Network Difficulty",
DiffColumn::SecondaryScaling => "Sec. Scaling",
// DiffColumn::SecondaryScaling => "Sec. Scaling",
DiffColumn::Time => "Block Time",
DiffColumn::Duration => "Duration",
}
Expand All @@ -131,14 +131,14 @@ impl DiffColumn {
impl TableViewItem<DiffColumn> for DiffBlock {
fn to_column(&self, column: DiffColumn) -> String {
let naive_datetime = NaiveDateTime::from_timestamp_opt(self.time as i64, 0).unwrap();
let datetime: DateTime<Utc> = DateTime::from_utc(naive_datetime, Utc);
let datetime = TimeZone::from_utc_datetime(&Utc, &naive_datetime);
let pow_type = self.algorithm.clone();
match column {
DiffColumn::Height => self.block_height.to_string(),
DiffColumn::Hash => self.block_hash.to_string(),
DiffColumn::PoWType => pow_type,
DiffColumn::Difficulty => self.difficulty.to_string(),
DiffColumn::SecondaryScaling => self.secondary_scaling.to_string(),
// DiffColumn::SecondaryScaling => self.secondary_scaling.to_string(),
DiffColumn::Time => format!("{}", datetime).to_string(),
DiffColumn::Duration => format!("{}s", self.duration).to_string(),
}
Expand All @@ -153,7 +153,7 @@ impl TableViewItem<DiffColumn> for DiffBlock {
DiffColumn::Hash => Ordering::Equal,
DiffColumn::PoWType => Ordering::Equal,
DiffColumn::Difficulty => Ordering::Equal,
DiffColumn::SecondaryScaling => Ordering::Equal,
// DiffColumn::SecondaryScaling => Ordering::Equal,
DiffColumn::Time => Ordering::Equal,
DiffColumn::Duration => Ordering::Equal,
}
Expand Down Expand Up @@ -190,9 +190,9 @@ impl TUIStatusListener for TUIMiningView {
.column(StratumWorkerColumn::LastSeen, "Last Seen", |c| {
c.width_percent(16)
})
// .column(StratumWorkerColumn::PowDifficulty, "Pow Difficulty", |c| {
// c.width_percent(12)
// })
/*.column(StratumWorkerColumn::PowDifficulty, "Pow Difficulty", |c| {
c.width_percent(12)
})*/
.column(StratumWorkerColumn::NumAccepted, "Num Accepted", |c| {
c.width_percent(10)
})
Expand Down Expand Up @@ -273,9 +273,9 @@ impl TUIStatusListener for TUIMiningView {
.column(DiffColumn::Difficulty, "Difficulty", |c| {
c.width_percent(20)
})
// .column(DiffColumn::SecondaryScaling, "Sec. Scaling", |c| {
// c.width_percent(10)
// })
/*.column(DiffColumn::SecondaryScaling, "Sec. Scaling", |c| {
c.width_percent(10)
})*/
.column(DiffColumn::Time, "Block Time", |c| c.width_percent(25))
.column(DiffColumn::Duration, "Duration", |c| c.width_percent(25));

Expand Down
4 changes: 2 additions & 2 deletions src/bin/tui/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ impl TUIStatusView {
} else {
0
};
let start = prev_update_time.timestamp_nanos();
let fin = Utc::now().timestamp_nanos();
let start = prev_update_time.timestamp_nanos_opt().unwrap();
let fin = Utc::now().timestamp_nanos_opt().unwrap();
let dur_ms = (fin - start) as f64 * NANO_TO_MILLIS;

format!("Sync step 2/7: Downloading {}(MB) chain state for state sync: {}% at {:.1?}(kB/s)",
Expand Down
2 changes: 1 addition & 1 deletion util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ log4rs = { version = "0.8.1", features = [
] }
log = "0.4"
walkdir = "2"
zip = { version = "0.5", default-features = false }
zip = { version = "0.5.11", default-features = false }
parking_lot = { version = "0.6" }
zeroize = "1.3.0"

Expand Down
21 changes: 18 additions & 3 deletions util/src/zip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,21 @@ use self::zip_rs::result::{ZipError, ZipResult};
use self::zip_rs::write::FileOptions;
use zip as zip_rs;

// Sanitize file path for normal components, excluding '/', '..', and '.'
// From private function in zip crate
fn path_to_string(path: &std::path::Path) -> String {
let mut path_str = String::new();
for component in path.components() {
if let std::path::Component::Normal(os_str) = component {
if !path_str.is_empty() {
path_str.push('/');
}
path_str.push_str(&*os_str.to_string_lossy());
}
}
path_str
}

/// Create a zip archive from source dir and list of relative file paths.
/// Permissions are set to 644 by default.
pub fn create_zip(dst_file: &File, src_dir: &Path, files: Vec<PathBuf>) -> io::Result<()> {
Expand All @@ -40,7 +55,7 @@ pub fn create_zip(dst_file: &File, src_dir: &Path, files: Vec<PathBuf>) -> io::R
let file_path = src_dir.join(x);
if let Ok(file) = File::open(file_path.clone()) {
info!("compress: {:?} -> {:?}", file_path, x);
writer.get_mut().start_file_from_path(x, options)?;
writer.get_mut().start_file(path_to_string(x), options)?;
io::copy(&mut BufReader::new(file), &mut writer)?;
// Flush the BufWriter after each file so we start then next one correctly.
writer.flush()?;
Expand All @@ -60,7 +75,7 @@ pub fn extract_files(from_archive: File, dest: &Path, files: Vec<PathBuf>) -> io
let mut archive = zip_rs::ZipArchive::new(from_archive).expect("archive file exists");
for x in files {
if let Ok(file) = archive.by_name(x.to_str().expect("valid path")) {
let path = dest.join(file.sanitized_name());
let path = dest.join(file.mangled_name());
let parent_dir = path.parent().expect("valid parent dir");
fs::create_dir_all(&parent_dir).expect("create parent dir");
let outfile = fs::File::create(&path).expect("file created");
Expand Down Expand Up @@ -148,7 +163,7 @@ where

for i in 0..archive.len() {
let mut file = archive.by_index(i)?;
let san_name = file.sanitized_name();
let san_name = file.mangled_name();
if san_name.to_str().unwrap_or("").replace("\\", "/") != file.name().replace("\\", "/")
|| !expected(&san_name)
{
Expand Down

0 comments on commit da04eeb

Please sign in to comment.