From b25568583e3ed306ac3c51cc17641201378a2a2e Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Mon, 31 Jan 2022 13:45:08 +0100 Subject: [PATCH] Perform proper migration logic --- contracts/cw20-ics20/src/contract.rs | 21 +++++++++++++++++++-- contracts/cw20-ics20/src/lib.rs | 1 + contracts/cw20-ics20/src/migrations.rs | 16 ++++++++++++++++ 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 contracts/cw20-ics20/src/migrations.rs diff --git a/contracts/cw20-ics20/src/contract.rs b/contracts/cw20-ics20/src/contract.rs index dbb52f015..cbcd8767b 100644 --- a/contracts/cw20-ics20/src/contract.rs +++ b/contracts/cw20-ics20/src/contract.rs @@ -192,26 +192,43 @@ pub fn execute_allow( } #[cfg_attr(not(feature = "library"), entry_point)] -pub fn migrate(deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result { +pub fn migrate(mut deps: DepsMut, _env: Env, _msg: MigrateMsg) -> Result { let version: Version = CONTRACT_VERSION.parse().map_err(from_semver)?; let stored = get_contract_version(deps.storage)?; let storage_version: Version = stored.version.parse().map_err(from_semver)?; + // First, ensure we are working from an equal or older version of this contract + // wrong type if CONTRACT_NAME != stored.contract { return Err(ContractError::CannotMigrate { previous_contract: stored.contract, }); } + // existing one is newer if storage_version > version { return Err(ContractError::CannotMigrateVersion { previous_version: stored.version, }); } + // Then, run the proper migration + // unsupported old version + if storage_version < "0.11.1".parse().map_err(from_semver)? { + return Err(ContractError::CannotMigrateVersion { + previous_version: stored.version, + }); + } // for 0.12.0-alpha1 or earlier, migrate from the config.gov_contract to ADMIN if storage_version <= "0.12.0-alpha1".parse().map_err(from_semver)? { - // DO migration + // Do v1 migration + let old_config = crate::migrations::v1::CONFIG.load(deps.storage)?; + ADMIN.set(deps.branch(), Some(old_config.gov_contract))?; + let config = Config { + default_timeout: old_config.default_timeout, + }; + CONFIG.save(deps.storage, &config)?; } + // otherwise no migration (yet) if storage_version < version { // we don't need to save anything if migrating from the same version diff --git a/contracts/cw20-ics20/src/lib.rs b/contracts/cw20-ics20/src/lib.rs index 5e579aafa..b9ceef245 100644 --- a/contracts/cw20-ics20/src/lib.rs +++ b/contracts/cw20-ics20/src/lib.rs @@ -2,6 +2,7 @@ pub mod amount; pub mod contract; mod error; pub mod ibc; +mod migrations; pub mod msg; pub mod state; mod test_helpers; diff --git a/contracts/cw20-ics20/src/migrations.rs b/contracts/cw20-ics20/src/migrations.rs new file mode 100644 index 000000000..888df9418 --- /dev/null +++ b/contracts/cw20-ics20/src/migrations.rs @@ -0,0 +1,16 @@ +// v1 format is anything older than 0.12.0 +pub mod v1 { + use schemars::JsonSchema; + use serde::{Deserialize, Serialize}; + + use cosmwasm_std::Addr; + use cw_storage_plus::Item; + + #[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)] + pub struct ConfigV1 { + pub default_timeout: u64, + pub gov_contract: Addr, + } + + pub const CONFIG: Item = Item::new("ics20_config"); +}