-
Notifications
You must be signed in to change notification settings - Fork 23
2.2.1 migration #451
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
Open
Subham2804
wants to merge
2
commits into
master
Choose a base branch
from
migration-2.2.1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
2.2.1 migration #451
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| pub mod v2_1_0; | ||
| pub mod v2_2_0; | ||
| pub mod v2_2_1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,43 +1,22 @@ | ||
| use cosmwasm_std::{Decimal, DepsMut, Response}; | ||
| use cw2::{assert_contract_version, get_contract_version, set_contract_version, VersionError}; | ||
| use cw2::{assert_contract_version, set_contract_version}; | ||
|
|
||
| use crate::{ | ||
| contract::{CONTRACT_NAME, CONTRACT_VERSION}, | ||
| error::ContractError, | ||
| state::SWAP_FEE, | ||
| }; | ||
| use crate::{contract::CONTRACT_NAME, error::ContractError, state::SWAP_FEE}; | ||
|
|
||
| const FROM_VERSION: &str = "2.1.0"; | ||
| const TO_VERSION: &str = "2.2.0"; | ||
|
|
||
| pub fn migrate(deps: DepsMut) -> Result<Response, ContractError> { | ||
| let contract = format!("crates.io:{CONTRACT_NAME}"); | ||
| let version = get_contract_version(deps.storage)?; | ||
| let from_version = version.version; | ||
|
|
||
| if version.contract != contract { | ||
| return Err(ContractError::Version(VersionError::WrongContract { | ||
| expected: contract, | ||
| found: version.contract, | ||
| })); | ||
| } | ||
|
|
||
| if from_version != FROM_VERSION { | ||
| return Err(ContractError::Version(VersionError::WrongVersion { | ||
| expected: FROM_VERSION.to_string(), | ||
| found: from_version, | ||
| })); | ||
| } | ||
|
|
||
| assert_contract_version(deps.storage, &contract, FROM_VERSION)?; | ||
| assert_contract_version(deps.storage, &format!("crates.io:{CONTRACT_NAME}"), FROM_VERSION)?; | ||
|
|
||
| if SWAP_FEE.may_load(deps.storage)?.is_none() { | ||
| SWAP_FEE.save(deps.storage, &Decimal::zero())?; | ||
| } | ||
|
|
||
| set_contract_version(deps.storage, format!("crates.io:{CONTRACT_NAME}"), CONTRACT_VERSION)?; | ||
| set_contract_version(deps.storage, format!("crates.io:{CONTRACT_NAME}"), TO_VERSION)?; | ||
|
|
||
| Ok(Response::new() | ||
| .add_attribute("action", "migrate") | ||
| .add_attribute("from_version", from_version) | ||
| .add_attribute("to_version", CONTRACT_VERSION)) | ||
| .add_attribute("from_version", FROM_VERSION) | ||
| .add_attribute("to_version", TO_VERSION)) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| use cosmwasm_std::{DepsMut, Response}; | ||
| use cw2::{assert_contract_version, set_contract_version}; | ||
|
|
||
| use crate::{contract::CONTRACT_NAME, error::ContractError}; | ||
|
|
||
| const FROM_VERSION: &str = "2.2.0"; | ||
| const TO_VERSION: &str = "2.2.1"; | ||
|
|
||
| pub fn migrate(deps: DepsMut) -> Result<Response, ContractError> { | ||
| // make sure we're migrating the correct contract and from the correct version | ||
| assert_contract_version(deps.storage, &format!("crates.io:{CONTRACT_NAME}"), FROM_VERSION)?; | ||
|
|
||
| set_contract_version(deps.storage, format!("crates.io:{CONTRACT_NAME}"), TO_VERSION)?; | ||
|
|
||
| Ok(Response::new() | ||
| .add_attribute("action", "migrate") | ||
| .add_attribute("from_version", FROM_VERSION) | ||
| .add_attribute("to_version", TO_VERSION)) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 0 additions & 69 deletions
69
contracts/credit-manager/tests/tests/test_migration_v2_2_0.rs
This file was deleted.
Oops, something went wrong.
119 changes: 119 additions & 0 deletions
119
contracts/credit-manager/tests/tests/test_migration_v2_2_1.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| use cosmwasm_std::{attr, testing::mock_env, Decimal, Event}; | ||
| use cw2::{ContractVersion, VersionError}; | ||
| use mars_credit_manager::{contract::migrate, error::ContractError, state::SWAP_FEE}; | ||
| use mars_testing::mock_dependencies; | ||
| use mars_types::credit_manager::MigrateMsg; | ||
|
|
||
| const CONTRACT_NAME: &str = "crates.io:mars-credit-manager"; | ||
|
|
||
| #[test] | ||
| fn v2_1_0_to_v2_2_0_wrong_contract_name() { | ||
| let mut deps = mock_dependencies(&[]); | ||
| cw2::set_contract_version(deps.as_mut().storage, "contract_xyz", "2.1.0").unwrap(); | ||
|
|
||
| let err = migrate(deps.as_mut(), mock_env(), MigrateMsg::V2_1_0ToV2_2_0 {}).unwrap_err(); | ||
|
|
||
| assert_eq!( | ||
| err, | ||
| ContractError::Version(VersionError::WrongContract { | ||
| expected: CONTRACT_NAME.to_string(), | ||
| found: "contract_xyz".to_string() | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn v2_1_0_to_v2_2_0_wrong_contract_version() { | ||
| let mut deps = mock_dependencies(&[]); | ||
| cw2::set_contract_version(deps.as_mut().storage, CONTRACT_NAME, "2.0.3").unwrap(); | ||
|
|
||
| let err = migrate(deps.as_mut(), mock_env(), MigrateMsg::V2_1_0ToV2_2_0 {}).unwrap_err(); | ||
|
|
||
| assert_eq!( | ||
| err, | ||
| ContractError::Version(VersionError::WrongVersion { | ||
| expected: "2.1.0".to_string(), | ||
| found: "2.0.3".to_string() | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn v2_1_0_to_v2_2_0_successful_migration() { | ||
| let mut deps = mock_dependencies(&[]); | ||
| cw2::set_contract_version(deps.as_mut().storage, CONTRACT_NAME, "2.1.0").unwrap(); | ||
|
|
||
| let res = migrate(deps.as_mut(), mock_env(), MigrateMsg::V2_1_0ToV2_2_0 {}).unwrap(); | ||
|
|
||
| assert_eq!(res.messages, vec![]); | ||
| assert_eq!(res.events, vec![] as Vec<Event>); | ||
| assert!(res.data.is_none()); | ||
| assert_eq!( | ||
| res.attributes, | ||
| vec![attr("action", "migrate"), attr("from_version", "2.1.0"), attr("to_version", "2.2.0")] | ||
| ); | ||
|
|
||
| let new_contract_version = ContractVersion { | ||
| contract: CONTRACT_NAME.to_string(), | ||
| version: "2.2.0".to_string(), | ||
| }; | ||
| assert_eq!(cw2::get_contract_version(deps.as_ref().storage).unwrap(), new_contract_version); | ||
|
|
||
| // Ensure swap fee exists post-migration (zero by default if absent) | ||
| let swap_fee = SWAP_FEE.may_load(deps.as_ref().storage).unwrap().unwrap_or_else(Decimal::zero); | ||
| assert!(swap_fee <= Decimal::one()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn v2_2_0_to_v2_2_1_wrong_contract_name() { | ||
| let mut deps = mock_dependencies(&[]); | ||
| cw2::set_contract_version(deps.as_mut().storage, "contract_xyz", "2.2.0").unwrap(); | ||
|
|
||
| let err = migrate(deps.as_mut(), mock_env(), MigrateMsg::V2_2_0ToV2_2_1 {}).unwrap_err(); | ||
|
|
||
| assert_eq!( | ||
| err, | ||
| ContractError::Version(VersionError::WrongContract { | ||
| expected: CONTRACT_NAME.to_string(), | ||
| found: "contract_xyz".to_string() | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn v2_2_0_to_v2_2_1_wrong_contract_version() { | ||
| let mut deps = mock_dependencies(&[]); | ||
| cw2::set_contract_version(deps.as_mut().storage, CONTRACT_NAME, "2.1.0").unwrap(); | ||
|
|
||
| let err = migrate(deps.as_mut(), mock_env(), MigrateMsg::V2_2_0ToV2_2_1 {}).unwrap_err(); | ||
|
|
||
| assert_eq!( | ||
| err, | ||
| ContractError::Version(VersionError::WrongVersion { | ||
| expected: "2.2.0".to_string(), | ||
| found: "2.1.0".to_string() | ||
| }) | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn v2_2_0_to_v2_2_1_successful_migration() { | ||
| let mut deps = mock_dependencies(&[]); | ||
| cw2::set_contract_version(deps.as_mut().storage, CONTRACT_NAME, "2.2.0").unwrap(); | ||
|
|
||
| let res = migrate(deps.as_mut(), mock_env(), MigrateMsg::V2_2_0ToV2_2_1 {}).unwrap(); | ||
|
|
||
| assert_eq!(res.messages, vec![]); | ||
| assert_eq!(res.events, vec![] as Vec<Event>); | ||
| assert!(res.data.is_none()); | ||
| assert_eq!( | ||
| res.attributes, | ||
| vec![attr("action", "migrate"), attr("from_version", "2.2.0"), attr("to_version", "2.2.1")] | ||
| ); | ||
|
|
||
| let new_contract_version = ContractVersion { | ||
| contract: CONTRACT_NAME.to_string(), | ||
| version: "2.2.1".to_string(), | ||
| }; | ||
| assert_eq!(cw2::get_contract_version(deps.as_ref().storage).unwrap(), new_contract_version); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| pub mod v2_1_0; | ||
| pub mod v2_1_1; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Upgrading a contract still at 2.1.0 with this 2.2.1 binary can only call
MigrateMsg::V2_1_0ToV2_2_0, which sets the stored cw2 version to the hard-codedTO_VERSIONof"2.2.0". Because a CosmWasm migrate call runs only once per code upload, there is no way to reach 2.2.1 metadata in the same upgrade, leaving the on-chain contract_version behind the deployed code and blocking future migrations that expect 2.2.1 as the starting point.Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although this does not affect us as we are on latest versions (2.1.0 for RB, 2.2.0 for CM) I think it is a potential cause of bugs. Perhaps we should think about this pattern and see if there are better alternatives. The problem with using CONTRACT_VERSION is that it breaks old tests