Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

421 fix failing runner test #424

Merged
merged 3 commits into from
Oct 17, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions clients/runner/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,7 @@ impl Runner {
let file_content = fs::read(bin_path.clone()).map_err(|_| Error::NoDownloadedRelease)?;
let checksum = H256::from_slice(&sha256sum(&file_content));

if checksum != release.checksum {
return Err(Error::IncorrectChecksum)
}
runner.checksum_matches(checksum, release)?;

let downloaded_release = DownloadedRelease { checksum, path: bin_path.clone(), bin_name };

Expand All @@ -149,7 +147,7 @@ impl Runner {
) -> Result<DownloadedRelease, Error> {
//recheck checksum to see if binary is the one we have
if let Some(downloaded_release) = runner.downloaded_release() {
if downloaded_release.checksum.eq(&release.checksum) {
if runner.checksum_matches(downloaded_release.checksum, &release).is_ok() {
log::info!("The on-chain release is already downloaded, skipping re-download");
return Ok(downloaded_release.clone())
}
Expand All @@ -159,7 +157,7 @@ impl Runner {
log::info!("Downloading {} at: {:?}", bin_name, bin_path);

// if not, try get the binary
let _ = retry_with_log_async(
let checksum = retry_with_log_async(
|| {
Runner::do_download_binary(bin_path.clone(), release.clone())
.into_future()
Expand All @@ -169,13 +167,15 @@ impl Runner {
)
.await?;

runner.checksum_matches(checksum, &release)?;

let downloaded_release =
DownloadedRelease { checksum: release.checksum, path: bin_path, bin_name };
runner.set_downloaded_release(Some(downloaded_release.clone()));
Ok(downloaded_release)
}

async fn do_download_binary(bin_path: PathBuf, release: ClientRelease) -> Result<(), Error> {
async fn do_download_binary(bin_path: PathBuf, release: ClientRelease) -> Result<H256, Error> {
let mut file = OpenOptions::new()
.read(true)
.write(true)
Expand All @@ -201,12 +201,7 @@ impl Runner {
let mut file_content = Vec::new();
file.read_to_end(&mut file_content)?;

let checksum = H256::from_slice(&sha256sum(&file_content));

if checksum != release.checksum {
return Err(Error::IncorrectChecksum)
}
Ok(())
Ok(H256::from_slice(&sha256sum(&file_content)))
}

async fn get_request_bytes(url: String) -> Result<Bytes, Error> {
Expand All @@ -215,6 +210,14 @@ impl Runner {
Ok(response.bytes().await?)
}

fn checksum_matches(checksum: H256, client_release: &ClientRelease) -> Result<(), Error> {
if checksum != client_release.checksum {
return Err(Error::IncorrectChecksum)
}

Ok(())
}

fn get_bin_path(runner: &impl RunnerExt, uri: &str) -> Result<(String, PathBuf), Error> {
// Remove any trailing slashes from the release URI
let parsed_uri = Url::parse(uri.trim_end_matches('/'))?;
Expand Down Expand Up @@ -478,6 +481,8 @@ pub trait RunnerExt {
fn try_load_downloaded_binary(&mut self, release: &ClientRelease) -> Result<(), Error>;
//wrapper arround static method get_bytes
async fn get_request_bytes_wrapper(&self, url: String) -> Result<Bytes, Error>;

fn checksum_matches(&self, checksum: H256, release: &ClientRelease) -> Result<(), Error>;
}

#[async_trait]
Expand Down Expand Up @@ -566,6 +571,10 @@ impl RunnerExt for Runner {
async fn get_request_bytes_wrapper(&self, url: String) -> Result<Bytes, Error> {
Runner::get_request_bytes(url).await
}

fn checksum_matches(&self, checksum: H256, release: &ClientRelease) -> Result<(), Error> {
Runner::checksum_matches(checksum, release)
}
}

#[async_trait]
Expand Down Expand Up @@ -693,6 +702,7 @@ mod tests {
fn maybe_restart_client(&mut self) -> Result<(), Error>;
fn try_load_downloaded_binary(&mut self, release: &ClientRelease) -> Result<(), Error>;
async fn get_request_bytes_wrapper(&self, url: String) -> Result<Bytes, Error>;
fn checksum_matches(&self, checksum:H256, release: &ClientRelease) -> Result<(),Error>;
}

#[async_trait]
Expand Down Expand Up @@ -729,6 +739,7 @@ mod tests {
.returning(|_| Ok(Bytes::from_static(&[1, 2, 3, 4])));
runner.expect_downloaded_release().return_const(None);
runner.expect_set_downloaded_release().return_const(());
runner.expect_checksum_matches().returning(|_, _| Ok(()));

let downloaded_release =
Runner::download_binary(&mut runner, client_release.clone()).await.unwrap();
Expand Down Expand Up @@ -772,6 +783,8 @@ mod tests {
.times(0)
.returning(|_| panic!("Unexpected call"));

runner.expect_checksum_matches().returning(|_, _| Ok(()));

// The latest on-chain release matches the currently downloaded one
let new_downloaded_release =
Runner::download_binary(&mut runner, ClientRelease::default()).await.unwrap();
Expand Down Expand Up @@ -988,6 +1001,8 @@ mod tests {
]);
assert_eq!(checksum, expected_checksum);
});
runner.expect_checksum_matches().returning(|_, _| Ok(()));

let release = ClientRelease {
checksum: H256::from_slice(&[
81, 216, 24, 152, 19, 116, 164, 71, 240, 135, 102, 16, 253, 43, 174, 235, 145, 29,
Expand Down Expand Up @@ -1020,6 +1035,8 @@ mod tests {
.expect_get_bin_path()
.returning(move |_| Ok(("client".to_string(), moved_mock_path.clone())));

runner.expect_checksum_matches().returning(|_, _| Err(Error::IncorrectChecksum));

assert_err!(
Runner::try_load_downloaded_binary(&mut runner, &Default::default()),
Error::IncorrectChecksum
Expand Down
Loading