Skip to content

Commit

Permalink
fix(minor-router): migration to delete chains
Browse files Browse the repository at this point in the history
  • Loading branch information
cjcobb23 committed Nov 22, 2024
1 parent 7d715f8 commit 6bc9685
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 28 deletions.
4 changes: 1 addition & 3 deletions contracts/router/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ mod query;
pub const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME");
pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
const BASE_VERSION: &str = "1.0.1";

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(
deps: DepsMut,
Expand All @@ -28,8 +27,7 @@ pub fn migrate(
) -> Result<Response, axelar_wasm_std::error::ContractError> {
cw2::assert_contract_version(deps.storage, CONTRACT_NAME, BASE_VERSION)?;

let axelarnet_gateway = address::validate_cosmwasm_address(deps.api, &msg.axelarnet_gateway)?;
v1_0_1::migrate(deps.storage, axelarnet_gateway)?;
v1_0_1::migrate(deps.storage, msg.chains_to_remove)?;

// this needs to be the last thing to do during migration,
// because previous migration steps should check the old version
Expand Down
52 changes: 28 additions & 24 deletions contracts/router/src/contract/migrations/v1_0_1.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
use axelar_wasm_std::error::ContractError;
use cosmwasm_std::{Addr, Storage};
use cosmwasm_std::Storage;
use cw_storage_plus::IndexedMap;
use error_stack::{Result, ResultExt};
use router_api::ChainEndpoint;

use router_api::error::Error;

use crate::state::{ChainEndpointIndexes, GatewayIndex};


const CHAINS_PKEY: &str = "chains";
pub fn chain_endpoints_old<'a>() -> IndexedMap<'a, String, ChainEndpoint, ChainEndpointIndexes<'a>> {
return IndexedMap::new(
CHAINS_PKEY,
ChainEndpointIndexes {
gateway: GatewayIndex::new(
|_pk: &[u8], d: &ChainEndpoint| d.gateway.address.clone(),
CHAINS_PKEY,
"gateways",
),
},
);
}

use crate::state;
pub fn migrate(storage: &mut dyn Storage, chains_to_remove: Vec<String>) -> Result<(), Error> {
for chain in chains_to_remove {

pub fn migrate(storage: &mut dyn Storage, axelarnet_gateway: Addr) -> Result<(), ContractError> {
// migrate config
state::save_config(storage, &state::Config { axelarnet_gateway }).map_err(Into::into)
chain_endpoints_old().remove(storage, chain).change_context(Error::StoreFailure)?;
}
Ok(())
}
#[cfg(test)]
mod test {
Expand Down Expand Up @@ -41,24 +63,6 @@ mod test {
#[deprecated(since = "1.0.1", note = "only used during migration")]
const CONFIG: Item<Config> = Item::new("config");

Check warning on line 64 in contracts/router/src/contract/migrations/v1_0_1.rs

View workflow job for this annotation

GitHub Actions / Test Suite

constant `CONFIG` is never used

#[test]
fn config_gets_migrated() {
let mut deps = mock_dependencies();
instantiate_1_0_1_contract(deps.as_mut()).unwrap();

assert_ok!(CONFIG.load(deps.as_mut().storage));
assert!(state::load_config(&deps.storage).is_err());

let axelarnet_gateway = Addr::unchecked("axelarnet-gateway");
assert_ok!(v1_0_1::migrate(
deps.as_mut().storage,
axelarnet_gateway.clone()
));
assert!(CONFIG.load(deps.as_mut().storage).is_err());

let config = assert_ok!(state::CONFIG.load(deps.as_mut().storage));
assert_eq!(config.axelarnet_gateway, axelarnet_gateway);
}

fn instantiate_1_0_1_contract(deps: DepsMut) -> Result<Response, ContractError> {

Check warning on line 67 in contracts/router/src/contract/migrations/v1_0_1.rs

View workflow job for this annotation

GitHub Actions / Test Suite

function `instantiate_1_0_1_contract` is never used
let admin = "admin";
Expand Down
2 changes: 1 addition & 1 deletion contracts/router/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct InstantiateMsg {

#[cw_serde]
pub struct MigrateMsg {
pub axelarnet_gateway: String,
pub chains_to_remove: Vec<String>
}

// these messages are extracted into a separate package to avoid circular dependencies
Expand Down

0 comments on commit 6bc9685

Please sign in to comment.