Skip to content

Commit

Permalink
imp(all): Some minor improvements (#1)
Browse files Browse the repository at this point in the history
* some improvements

* more minor improvements / adjustments
  • Loading branch information
MalteHerrmann authored Jan 18, 2025
1 parent 415a269 commit 269136a
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 71 deletions.
24 changes: 12 additions & 12 deletions src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Block, BlockError> {
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<Block, BlockError> {
// 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.
Expand Down
3 changes: 1 addition & 2 deletions src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
14 changes: 5 additions & 9 deletions src/inputs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,10 @@ fn calculate_planned_date(voting_period: Duration, utc_time: DateTime<Utc>) -> 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(
Expand All @@ -170,11 +170,7 @@ fn calculate_planned_date(voting_period: Duration, utc_time: DateTime<Utc>) -> 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<Utc>) -> bool {
if upgrade_time.weekday() == Weekday::Sat || upgrade_time.weekday() == Weekday::Sun {
return false;
}

true
!matches!(upgrade_time.weekday(), Weekday::Sat | Weekday::Sun)
}

/// Returns a string representation of the upgrade time.
Expand Down
2 changes: 1 addition & 1 deletion src/proposal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn render_proposal(helper: &UpgradeHelper) -> Result<String, ProposalError>
"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,
Expand Down
76 changes: 32 additions & 44 deletions src/release.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, PrepareError> {
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())
}
Expand All @@ -133,25 +133,17 @@ fn build_assets_json(release: &Release, checksums: HashMap<String, String>) -> 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
}
Expand Down Expand Up @@ -187,35 +179,31 @@ fn get_os_key_from_asset_name(name: &str) -> Option<String> {
}

/// Downloads the checksum file from the release assets and returns the built checksum string.
async fn get_checksum_map(assets: Vec<Asset>) -> Result<HashMap<String, String>, 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<HashMap<String, String>, 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> {
octocrab::instance()
Expand All @@ -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"));
Expand Down
6 changes: 3 additions & 3 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 269136a

Please sign in to comment.