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

fix: add more logic on semver check of Starknet API #1966

Merged
merged 3 commits into from
May 16, 2024
Merged
Changes from 1 commit
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
66 changes: 64 additions & 2 deletions bin/sozo/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,12 @@

let (account, rpc_url) = {
let provider = starknet.provider(env)?;
trace!("Provider initialized.");
trace!(?provider, "Provider initialized.");

Check warning on line 126 in bin/sozo/src/commands/migrate.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/migrate.rs#L126

Added line #L126 was not covered by tests

let spec_version = provider.spec_version().await?;
trace!(spec_version);

if spec_version != RPC_SPEC_VERSION {
if !is_compatible_version(&spec_version, RPC_SPEC_VERSION)? {

Check warning on line 131 in bin/sozo/src/commands/migrate.rs

View check run for this annotation

Codecov / codecov/patch

bin/sozo/src/commands/migrate.rs#L131

Added line #L131 was not covered by tests
return Err(anyhow!(
"Unsupported Starknet RPC version: {}, expected {}.",
spec_version,
Expand Down Expand Up @@ -167,3 +167,65 @@

Ok((world_address, account, rpc_url.to_string()))
}

/// Checks if the provided version string is compatible with the expected version string using
/// semantic versioning rules. Includes specific backward compatibility rules, e.g., version 0.6 is
/// compatible with 0.7.
///
/// # Arguments
///
/// * `provided_version` - The version string provided by the user.
/// * `expected_version` - The expected version string.
///
/// # Returns
///
/// * `Result<bool>` - Returns `true` if the provided version is compatible with the expected
/// version, `false` otherwise.
fn is_compatible_version(provided_version: &str, expected_version: &str) -> Result<bool> {
use semver::Version;
let provided_ver = Version::parse(provided_version)
.map_err(|e| anyhow!("Failed to parse provided version '{}': {}", provided_version, e))?;
let expected_ver = Version::parse(expected_version)
.map_err(|e| anyhow!("Failed to parse expected version '{}': {}", expected_version, e))?;

// Check major version for compatibility.
if provided_ver.major != expected_ver.major {
return Ok(false);
}

// Specific backward compatibility rule: 0.6 is compatible with 0.7.
if (provided_ver.major == 0 && provided_ver.minor == 6)
&& (expected_ver.major == 0 && expected_ver.minor == 7)
{
return Ok(true);
}

// Check minor version for general compatibility.
Ok(provided_ver.minor >= expected_ver.minor)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_is_compatible_version_major_mismatch() {
glihm marked this conversation as resolved.
Show resolved Hide resolved
assert!(!is_compatible_version("1.0.0", "2.0.0").unwrap());
}

#[test]
fn test_is_compatible_version_minor_compatible() {
assert!(is_compatible_version("1.2.0", "1.1.0").unwrap());
}

#[test]
fn test_is_compatible_version_specific_backward_compatibility() {
assert!(is_compatible_version("0.6.0", "0.7.1").unwrap());
}

#[test]
fn test_is_compatible_version_invalid_version_string() {
assert!(is_compatible_version("1.0", "1.0.0").is_err());
assert!(is_compatible_version("1.0.0", "1.0").is_err());
}
}
Loading