Skip to content

Commit

Permalink
update migration for 3.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
iboss-ptk committed Aug 6, 2024
1 parent 952b393 commit b7f0f76
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 82 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion contracts/transmuter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ["Supanat Potiwarakorn <[email protected]>"]
edition = "2021"
name = "transmuter"
version = "3.1.0"
version = "3.2.0"

exclude = [
# Those files are rust-optimizer artifacts. You might want to commit them for convenience but they should not be part of the source code publication.
Expand Down
4 changes: 2 additions & 2 deletions contracts/transmuter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ mod entry_points {
pub fn migrate(
deps: DepsMut,
_env: Env,
_msg: migrations::v3_1_0::MigrateMsg,
_msg: migrations::v3_2_0::MigrateMsg,
) -> Result<Response, ContractError> {
migrations::v3_1_0::execute_migration(deps)
migrations::v3_2_0::execute_migration(deps)
}
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/transmuter/src/migrations/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod v3_1_0;
pub mod v3_2_0;
72 changes: 0 additions & 72 deletions contracts/transmuter/src/migrations/v3_1_0.rs

This file was deleted.

106 changes: 106 additions & 0 deletions contracts/transmuter/src/migrations/v3_2_0.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
use cosmwasm_schema::cw_serde;

use cosmwasm_std::{ensure_eq, DepsMut, Response, Storage};
use cw2::{ContractVersion, VersionError, CONTRACT};

use crate::{
contract::{CONTRACT_NAME, CONTRACT_VERSION},
ContractError,
};

const FROM_VERSIONS: &[&str] = &["3.0.0", "3.1.0"];
const TO_VERSION: &str = "3.2.0";

#[cw_serde]
pub struct MigrateMsg {}

pub fn execute_migration(deps: DepsMut) -> Result<Response, ContractError> {
// Assert that the stored contract version matches the expected version before migration
assert_contract_versions(deps.storage, CONTRACT_NAME, FROM_VERSIONS)?;

// Ensure that the current contract version matches the target version to prevent migration to an incorrect version
ensure_eq!(
CONTRACT_VERSION,
TO_VERSION,
cw2::VersionError::WrongVersion {
expected: TO_VERSION.to_string(),
found: CONTRACT_VERSION.to_string()
}
);

// Set the contract version to the target version after successful migration
cw2::set_contract_version(deps.storage, CONTRACT_NAME, TO_VERSION)?;

// Return a response with an attribute indicating the method that was executed
Ok(Response::new().add_attribute("method", "v3_2_0/execute_migraiton"))
}

/// Assert that the stored contract version info matches the given value.
/// This is useful during migrations, for making sure that the correct contract
/// is being migrated, and it's being migrated from the correct version.
fn assert_contract_versions(
storage: &dyn Storage,
expected_contract: &str,
expected_versions: &[&str],
) -> Result<(), VersionError> {
let ContractVersion { contract, version } = match CONTRACT.may_load(storage)? {
Some(contract) => contract,
None => return Err(VersionError::NotFound),
};

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

if !expected_versions.contains(&version.as_str()) {
return Err(VersionError::WrongVersion {
expected: expected_versions.join(","),
found: version,
});
}

Ok(())
}

#[cfg(test)]
mod tests {
use cosmwasm_std::testing::mock_dependencies;

use super::*;

#[test]
fn test_successful_migration() {
let mut deps = mock_dependencies();

for from_version in FROM_VERSIONS {
cw2::set_contract_version(&mut deps.storage, CONTRACT_NAME, from_version.to_string())
.unwrap();

let res = execute_migration(deps.as_mut()).unwrap();

assert_eq!(
res,
Response::new().add_attribute("method", "v3_2_0/execute_migraiton")
);
}
}

#[test]
fn test_invalid_version() {
let mut deps = mock_dependencies();

cw2::set_contract_version(&mut deps.storage, CONTRACT_NAME, "2.0.0").unwrap();

let err = execute_migration(deps.as_mut()).unwrap_err();
assert_eq!(
err,
ContractError::VersionError(cw2::VersionError::WrongVersion {
expected: FROM_VERSIONS.join(","),
found: "2.0.0".to_string()
})
);
}
}
13 changes: 8 additions & 5 deletions contracts/transmuter/src/test/cases/units/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
sv::{InstantiateMsg, QueryMsg},
GetModeratorResponse, ListAssetConfigsResponse,
},
migrations::v3_1_0::MigrateMsg,
migrations::v3_2_0::MigrateMsg,
test::{modules::cosmwasm_pool::CosmwasmPool, test_env::TransmuterContract},
};
use cosmwasm_schema::cw_serde;
Expand All @@ -19,6 +19,7 @@ use osmosis_std::types::{
},
};
use osmosis_test_tube::{Account, GovWithAppAccess, Module, OsmosisTestApp, Runner};
use rstest::rstest;

#[cw_serde]
struct InstantiateMsgV2 {
Expand Down Expand Up @@ -145,8 +146,10 @@ fn test_migrate_v2_to_v3() {
assert_eq!(moderator, migrate_msg.moderator.unwrap());
}

#[test]
fn test_migrate_v3_to_v3_1() {
#[rstest]
#[case("v3")]
#[case("v3_1")]
fn test_migrate_v3(#[case] from_version: &str) {
// --- setup account ---
let app = OsmosisTestApp::new();
let signer = app
Expand All @@ -166,7 +169,7 @@ fn test_migrate_v3_to_v3_1() {
UploadCosmWasmPoolCodeAndWhiteListProposal {
title: String::from("store test cosmwasm pool code"),
description: String::from("test"),
wasm_byte_code: get_prev_version_of_wasm_byte_code("v3"),
wasm_byte_code: get_prev_version_of_wasm_byte_code(from_version),
},
signer.address(),
&signer,
Expand Down Expand Up @@ -265,7 +268,7 @@ fn test_migrate_v3_to_v3_1() {
version,
cw2::ContractVersion {
contract: "crates.io:transmuter".to_string(),
version: "3.1.0".to_string()
version: "3.2.0".to_string()
}
);
}
Expand Down
Binary file not shown.

0 comments on commit b7f0f76

Please sign in to comment.