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

CW2: add a cw2::VersionError error type #857

Closed
larry0x opened this issue Feb 8, 2023 · 0 comments · Fixed by #858
Closed

CW2: add a cw2::VersionError error type #857

larry0x opened this issue Feb 8, 2023 · 0 comments · Fixed by #858

Comments

@larry0x
Copy link
Contributor

larry0x commented Feb 8, 2023

A major use case of cw2 is when migrating a contract, check the stored version info to make sure we're not migrating a wrong contract, or migrating from a wrong version. Therefore, it makes sense to add a VersionError type which is to be emitted if this check fails:

#[derive(thiserror::Error, Debug)]
enum VersionError {
    #[error("contract version info not found")]
    NotFound,

    #[error("wrong contract: expecting `{expected}`, found `{found}`")]
    WrongContract {
        expected: String,
        found: String,
    },

    #[error("wrong contract version: expecting `{expected}`, found `{found}`")]
    WrongVersion {
        expected: String,
        found: String,
    },
}
// run this as part of the migration process
fn assert_version(
    storage: &dyn Storage,
    expected_contract: &str,
    expected_version: &str,
) -> Result<(), VersionError> {
    let Some(ContractVersion { contract, version }) = CONTRACT.may_load(storage)? else {
        return Err(VersionError::NotFound);
    };

    if contract != expected_contract {
        return Err(VersionError::WrongContract {
            expected: expected_contract.into(),
            found: contract,
        });
    }

    if version != expected_version {
        return Err(VersionError::WrongVersion {
            expected: expected_version.into(),
            found: version,
        });
    }

    Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant