diff --git a/Cargo.lock b/Cargo.lock index f1838a09b..0723c6d08 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3294,6 +3294,7 @@ dependencies = [ name = "gateway" version = "1.1.1" dependencies = [ + "assert_ok", "axelar-wasm-std", "client", "cosmwasm-schema", @@ -3307,6 +3308,7 @@ dependencies = [ "rand", "report", "router-api", + "semver 1.0.23", "serde", "serde_json", "thiserror", diff --git a/contracts/gateway/Cargo.toml b/contracts/gateway/Cargo.toml index 204d90e39..7115ac6e5 100644 --- a/contracts/gateway/Cargo.toml +++ b/contracts/gateway/Cargo.toml @@ -45,12 +45,14 @@ gateway-api = { workspace = true } itertools = { workspace = true } report = { workspace = true } router-api = { workspace = true } +semver = { workspace = true } serde = { workspace = true } serde_json = { workspace = true } thiserror = { workspace = true } voting-verifier = { workspace = true, features = ["library"] } [dev-dependencies] +assert_ok = { workspace = true } cw-multi-test = "0.15.1" rand = { workspace = true } diff --git a/contracts/gateway/src/contract.rs b/contracts/gateway/src/contract.rs index 80d12bfd5..b0c493abc 100644 --- a/contracts/gateway/src/contract.rs +++ b/contracts/gateway/src/contract.rs @@ -3,9 +3,11 @@ use std::fmt::Debug; use axelar_wasm_std::{address, FnExt}; #[cfg(not(feature = "library"))] use cosmwasm_std::entry_point; -use cosmwasm_std::{Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response}; -use error_stack::ResultExt; +use cosmwasm_std::{ensure, Binary, Deps, DepsMut, Empty, Env, MessageInfo, Response}; +use cw2::VersionError; +use error_stack::{report, ResultExt}; use router_api::client::Router; +use semver::Version; use crate::msg::{ExecuteMsg, InstantiateMsg, QueryMsg}; use crate::state; @@ -17,7 +19,6 @@ mod query; const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME"); const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION"); -const BASE_VERSION: &str = "1.0.0"; #[derive(thiserror::Error, Debug)] pub enum Error { @@ -45,7 +46,15 @@ pub fn migrate( _env: Env, _msg: Empty, ) -> Result { - cw2::assert_contract_version(deps.storage, CONTRACT_NAME, BASE_VERSION)?; + let old_version = Version::parse(&cw2::get_contract_version(deps.storage)?.version)?; + ensure!( + old_version.major == 1 && old_version.minor == 1, + report!(VersionError::WrongVersion { + expected: "1.1.x".into(), + found: old_version.to_string() + }) + ); + cw2::set_contract_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?; Ok(Response::default()) @@ -110,3 +119,37 @@ pub fn query( }? .then(Ok) } + +#[cfg(test)] +mod test { + use assert_ok::assert_ok; + use cosmwasm_std::testing::{mock_dependencies, mock_env, mock_info}; + use cosmwasm_std::{Addr, Empty}; + + use crate::contract::{instantiate, migrate, CONTRACT_NAME, CONTRACT_VERSION}; + use crate::msg::InstantiateMsg; + + #[test] + fn migrate_sets_contract_version() { + let mut deps = mock_dependencies(); + let env = mock_env(); + let info = mock_info("sender", &[]); + let instantiate_msg = InstantiateMsg { + verifier_address: Addr::unchecked("verifier").to_string(), + router_address: Addr::unchecked("router").to_string(), + }; + + assert_ok!(instantiate( + deps.as_mut(), + env.clone(), + info.clone(), + instantiate_msg + )); + + migrate(deps.as_mut(), mock_env(), Empty {}).unwrap(); + + let contract_version = cw2::get_contract_version(deps.as_mut().storage).unwrap(); + assert_eq!(contract_version.contract, CONTRACT_NAME); + assert_eq!(contract_version.version, CONTRACT_VERSION); + } +}