Rust 1.85 lints (#7019)

N/A


  2 changes:
1. Replace Option::map_or(true, ...) with is_none_or(...)
2. Remove unnecessary `Into::into` blocks where the type conversion is apparent from the types
This commit is contained in:
Pawan Dhananjay
2025-02-23 18:36:13 -08:00
committed by GitHub
parent ff739d56be
commit b3b6aea1c5
33 changed files with 102 additions and 128 deletions

View File

@@ -1,65 +1,33 @@
//! This build script downloads the latest Web3Signer release and places it in the `OUT_DIR` so it
//! can be used for integration testing.
use reqwest::{
header::{self, HeaderValue},
Client,
};
use serde_json::Value;
use reqwest::Client;
use std::env;
use std::fs;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use zip::ZipArchive;
/// Use `None` to download the latest Github release.
/// Use `Some("21.8.1")` to download a specific version.
const FIXED_VERSION_STRING: Option<&str> = None;
pub async fn download_binary(dest_dir: PathBuf, github_token: &str) {
let version_file = dest_dir.join("version");
let client = Client::builder()
// Github gives a 403 without a user agent.
.user_agent("web3signer_tests")
.build()
.unwrap();
// This function no longer makes any attempt to avoid downloads, because in practice we use it
// with a fresh temp directory every time we run the tests. We might want to change this in future
// to enable reproducible/offline testing.
pub async fn download_binary(dest_dir: PathBuf) {
let version = if let Some(version) = FIXED_VERSION_STRING {
version.to_string()
} else if let Ok(env_version) = env::var("LIGHTHOUSE_WEB3SIGNER_VERSION") {
env_version
} else {
// Get the latest release of the web3 signer repo.
let mut token_header_value = HeaderValue::from_str(github_token).unwrap();
token_header_value.set_sensitive(true);
let latest_response: Value = client
.get("https://api.github.com/repos/ConsenSys/web3signer/releases/latest")
.header(header::AUTHORIZATION, token_header_value)
.send()
.await
.unwrap()
.error_for_status()
.unwrap()
.json()
.await
.unwrap();
latest_response
.get("tag_name")
.unwrap()
.as_str()
.unwrap()
.to_string()
// The Consenys artifact server resolves `latest` to the latest release. We previously hit
// the Github API to establish the version, but that is no longer necessary.
"latest".to_string()
};
eprintln!("Downloading web3signer version: {version}");
if version_file.exists() && fs::read(&version_file).unwrap() == version.as_bytes() {
// The latest version is already downloaded, do nothing.
return;
} else {
// Ignore the result since we don't care if the version file already exists.
let _ = fs::remove_file(&version_file);
}
// Download the latest release zip.
// Download the release zip.
let client = Client::builder().build().unwrap();
let zip_url = format!("https://artifacts.consensys.net/public/web3signer/raw/names/web3signer.zip/versions/{}/web3signer-{}.zip", version, version);
let zip_response = client
.get(zip_url)
@@ -73,8 +41,9 @@ pub async fn download_binary(dest_dir: PathBuf, github_token: &str) {
.unwrap();
// Write the zip to a file.
let zip_path = dest_dir.join(format!("{}.zip", version));
let zip_path = dest_dir.join(format!("web3signer-{version}.zip"));
fs::write(&zip_path, zip_response).unwrap();
// Unzip the zip.
let mut zip_file = fs::File::open(&zip_path).unwrap();
ZipArchive::new(&mut zip_file)
@@ -88,15 +57,33 @@ pub async fn download_binary(dest_dir: PathBuf, github_token: &str) {
if web3signer_dir.exists() {
fs::remove_dir_all(&web3signer_dir).unwrap();
}
fs::rename(
dest_dir.join(format!("web3signer-{}", version)),
web3signer_dir,
)
.unwrap();
// Delete zip and unzipped dir.
let versioned_web3signer_dir = find_versioned_web3signer_dir(&dest_dir);
eprintln!(
"Renaming versioned web3signer dir at: {}",
versioned_web3signer_dir.display()
);
fs::rename(versioned_web3signer_dir, web3signer_dir).unwrap();
// Delete zip.
fs::remove_file(&zip_path).unwrap();
// Update the version file to avoid duplicate downloads.
fs::write(&version_file, version).unwrap();
}
fn find_versioned_web3signer_dir(dest_dir: &Path) -> PathBuf {
for entry in fs::read_dir(dest_dir).unwrap() {
let entry = entry.unwrap();
let path = entry.path();
if path
.file_name()
.and_then(|n| n.to_str())
.map(|s| s.starts_with("web3signer-"))
.unwrap_or(false)
&& entry.file_type().unwrap().is_dir()
{
return path;
}
}
panic!("no directory named web3signer-* found after ZIP extraction")
}

View File

@@ -178,14 +178,7 @@ mod tests {
pub async fn new(network: &str, listen_address: &str, listen_port: u16) -> Self {
GET_WEB3SIGNER_BIN
.get_or_init(|| async {
// Read a Github API token from the environment. This is intended to prevent rate-limits on CI.
// We use a name that is unlikely to accidentally collide with anything the user has configured.
let github_token = env::var("LIGHTHOUSE_GITHUB_TOKEN");
download_binary(
TEMP_DIR.lock().path().to_path_buf(),
github_token.as_deref().unwrap_or(""),
)
.await;
download_binary(TEMP_DIR.lock().path().to_path_buf()).await;
})
.await;