From e56e3e96c22c2b89131bfc1f31afc005cd0219de Mon Sep 17 00:00:00 2001 From: Malte Herrmann Date: Fri, 10 Jan 2025 22:23:08 +0100 Subject: [PATCH 1/2] some improvements --- src/block.rs | 24 ++++++++-------- src/helper.rs | 3 +- src/inputs.rs | 15 +++++----- src/release.rs | 76 +++++++++++++++++++++----------------------------- src/version.rs | 6 ++-- 5 files changed, 55 insertions(+), 69 deletions(-) diff --git a/src/block.rs b/src/block.rs index fda3887..25deb26 100644 --- a/src/block.rs +++ b/src/block.rs @@ -64,22 +64,22 @@ pub fn round_to_nearest_500(height: u64) -> u64 { /// Gets the latest block from the Evmos network. async fn get_latest_block(base_url: &Url) -> Result { - let url = base_url.join(LATEST_BLOCK_ENDPOINT)?; - let body = get_body(url).await?; - - process_block_body(body) + process_block_body( + get_body( + base_url.join(LATEST_BLOCK_ENDPOINT)? + ).await? + ) } /// Gets the block at the given height from the Evmos network. async fn get_block(base_url: &Url, height: u64) -> Result { - // Combine the REST endpoint with the block height - let url = base_url - .join(BLOCKS_ENDPOINT)? - .join(height.to_string().as_str())?; - - let body = get_body(url).await?; - - process_block_body(body) + process_block_body( + get_body( + base_url + .join(BLOCKS_ENDPOINT)? + .join(height.to_string().as_str())? + ).await? + ) } /// Returns the appropriate REST provider for the given network. diff --git a/src/helper.rs b/src/helper.rs index bc8bfa8..8268efc 100644 --- a/src/helper.rs +++ b/src/helper.rs @@ -90,8 +90,7 @@ impl UpgradeHelper { } // Check if the upgrade time is valid - let valid_time = inputs::is_valid_upgrade_time(self.upgrade_time); - if !valid_time { + if !inputs::is_valid_upgrade_time(self.upgrade_time) { return Err(ValidationError::UpgradeTime(self.upgrade_time)); } diff --git a/src/inputs.rs b/src/inputs.rs index e3c55d5..d74ea85 100644 --- a/src/inputs.rs +++ b/src/inputs.rs @@ -150,10 +150,10 @@ fn calculate_planned_date(voting_period: Duration, utc_time: DateTime) -> D } // NOTE: we don't want to upgrade on a weekend, so we shift the upgrade to the next monday - if end_of_voting.weekday() == Weekday::Sat { - end_of_voting = end_of_voting.add(Duration::days(2)); - } else if end_of_voting.weekday() == Weekday::Sun { - end_of_voting = end_of_voting.add(Duration::days(1)); + match end_of_voting.weekday() { + Weekday::Sat => end_of_voting = end_of_voting.add(Duration::days(2)), + Weekday::Sun => end_of_voting = end_of_voting.add(Duration::days(1)), + _ => {} } Utc.with_ymd_and_hms( @@ -170,11 +170,10 @@ fn calculate_planned_date(voting_period: Duration, utc_time: DateTime) -> D /// Checks if the passed upgrade time is valid. /// The upgrade time cannot be on a weekend. pub fn is_valid_upgrade_time(upgrade_time: DateTime) -> bool { - if upgrade_time.weekday() == Weekday::Sat || upgrade_time.weekday() == Weekday::Sun { - return false; + match upgrade_time.weekday() { + Weekday::Sat | Weekday::Sun => false, + _ => true, } - - true } /// Returns a string representation of the upgrade time. diff --git a/src/release.rs b/src/release.rs index f3faab1..84e6d4b 100644 --- a/src/release.rs +++ b/src/release.rs @@ -122,7 +122,7 @@ mod release_notes_tests { /// Returns the asset string for the release assets. /// The asset string is used in the Evmos CLI command. pub async fn get_asset_string(release: &Release) -> Result { - let checksums = get_checksum_map(release.assets.clone()).await?; + let checksums = get_checksum_map(&release.assets).await?; Ok(build_assets_json(release, checksums).to_string()) } @@ -133,25 +133,17 @@ fn build_assets_json(release: &Release, checksums: HashMap) -> V "binaries": {} }); - for asset in release.assets.clone() { - let os_key = match get_os_key_from_asset_name(&asset.name) { - Some(key) => key, - None => { - continue; - } - }; - - let checksum = match checksums.get(&asset.name) { - Some(checksum) => checksum, - None => { - continue; - } - }; - - let url = format!("{}?checksum={}", asset.browser_download_url, checksum); - - insert_into_assets(&mut assets, os_key, url); - } + release.assets.iter() + .filter_map(|asset| { + let os_key = get_os_key_from_asset_name(&asset.name)?; + let checksum = checksums.get(&asset.name)?; + let url = format!("{}?checksum={}", asset.browser_download_url, checksum); + + Some((os_key, url)) + }) + .for_each(|(os_key, url)| { + insert_into_assets(&mut assets, os_key, url); + }); assets } @@ -187,35 +179,31 @@ fn get_os_key_from_asset_name(name: &str) -> Option { } /// Downloads the checksum file from the release assets and returns the built checksum string. -async fn get_checksum_map(assets: Vec) -> Result, PrepareError> { - let checksum = match get_checksum_from_assets(assets.as_slice()) { - Some(checksum) => checksum, - None => return Err(PrepareError::GetChecksumAsset), - }; +async fn get_checksum_map(assets: &[Asset]) -> Result, PrepareError> { + let checksum = get_checksum_from_assets(assets) + .ok_or(PrepareError::GetChecksumAsset)?; + let body = get_body(checksum.browser_download_url.clone()).await?; - let mut checksums = HashMap::new(); - - for line in body.lines() { - let line = line.trim(); - let parts: Vec<&str> = line.split_whitespace().collect(); - - if parts.len() != 2 { - println!("Invalid checksum line: {}", line); - continue; - } - - // NOTE: Windows links are not supported in the submit-legacy-proposal command - if parts[1].contains("Windows") { - continue; - } - - checksums.insert(parts[1].to_string(), parts[0].to_string()); - } + let checksums = body + .lines() + .filter_map(|line| parse_checksum_line(line.trim())) + .collect(); Ok(checksums) } +/// Parses a single line from the checksum file into an (asset name, checksum) pair. +/// Returns None if the line is invalid or contains a Windows asset. +fn parse_checksum_line(line: &str) -> Option<(String, String)> { + let mut parts = line.split_whitespace(); + let checksum = parts.next()?.to_string(); + let asset_name = parts.next()?.to_string(); + + (!parts.next().is_some() && !asset_name.contains("Windows")) + .then_some((asset_name, checksum)) +} + /// Returns an Octocrab instance. pub fn get_instance() -> Arc { octocrab::instance() @@ -230,7 +218,7 @@ mod assets_tests { async fn test_get_checksum_map_pass() { let release: Release = serde_json::from_str(include_str!("testdata/release.json")).unwrap(); - let checksums = get_checksum_map(release.assets.clone()).await.unwrap(); + let checksums = get_checksum_map(&release.assets).await.unwrap(); assert!(checksums.contains_key("evmos_14.0.0_Linux_amd64.tar.gz")); assert!(checksums.contains_key("evmos_14.0.0_Linux_arm64.tar.gz")); diff --git a/src/version.rs b/src/version.rs index 47a4194..7733980 100644 --- a/src/version.rs +++ b/src/version.rs @@ -4,9 +4,9 @@ use regex::Regex; /// Returns a boolean value if the defined version fulfills the semantic /// versioning requirements. pub fn is_valid_version(version: &str) -> bool { - let valid = r"^v\d+\.\d+\.\d+(-rc\d+)*$"; - - Regex::new(valid).unwrap().is_match(version) + Regex::new(r"^v\d+\.\d+\.\d+(-rc\d+)*$") + .unwrap() + .is_match(version) } /// Returns a boolean value if the defined target version fits From 75fee75f4319d0abd0745224b7dd526319d31918 Mon Sep 17 00:00:00 2001 From: Malte Herrmann Date: Sat, 18 Jan 2025 23:14:13 +0100 Subject: [PATCH 2/2] more minor improvements / adjustments --- src/inputs.rs | 5 +---- src/proposal.rs | 2 +- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/inputs.rs b/src/inputs.rs index d74ea85..f2c357a 100644 --- a/src/inputs.rs +++ b/src/inputs.rs @@ -170,10 +170,7 @@ fn calculate_planned_date(voting_period: Duration, utc_time: DateTime) -> D /// Checks if the passed upgrade time is valid. /// The upgrade time cannot be on a weekend. pub fn is_valid_upgrade_time(upgrade_time: DateTime) -> bool { - match upgrade_time.weekday() { - Weekday::Sat | Weekday::Sun => false, - _ => true, - } + !matches!(upgrade_time.weekday(), Weekday::Sat | Weekday::Sun) } /// Returns a string representation of the upgrade time. diff --git a/src/proposal.rs b/src/proposal.rs index f6b059d..a2168e5 100644 --- a/src/proposal.rs +++ b/src/proposal.rs @@ -30,7 +30,7 @@ pub fn render_proposal(helper: &UpgradeHelper) -> Result "height": height_link, "name": helper.proposal_name, "n_blocks": n_blocks, - "network": format!("{}", helper.network), // TODO: implement serialize trait here? + "network": helper.network, "previous_version": get_release_md_link(helper.previous_version.as_str()), "version": get_release_md_link(helper.target_version.as_str()), "voting_time": helper.voting_period,