Skip to content
This repository has been archived by the owner on Oct 30, 2023. It is now read-only.

Commit

Permalink
allow for module updates (#384)
Browse files Browse the repository at this point in the history
  • Loading branch information
CyberHoward committed Jul 2, 2023
1 parent 0432aee commit d0be31d
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 32 deletions.
141 changes: 119 additions & 22 deletions contracts/native/version-control/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ pub fn propose_modules(
let config = CONFIG.load(deps.storage)?;

for (module, mod_ref) in modules {
if PENDING_MODULES.has(deps.storage, &module)
let store_has_module = PENDING_MODULES.has(deps.storage, &module)
|| REGISTERED_MODULES.has(deps.storage, &module)
|| YANKED_MODULES.has(deps.storage, &module)
{
|| YANKED_MODULES.has(deps.storage, &module);
if !config.allow_direct_module_registration_and_updates && store_has_module {
return Err(VCError::NotUpdateableModule(module));
}

Expand All @@ -84,7 +84,7 @@ pub fn propose_modules(
}
}

if config.allow_direct_module_registration {
if config.allow_direct_module_registration_and_updates {
// assert that its data is equal to what it wants to be registered under.
module::assert_module_data_validity(
&deps.querier,
Expand Down Expand Up @@ -378,7 +378,7 @@ pub fn remove_namespaces(
pub fn update_config(
deps: DepsMut,
info: MessageInfo,
allow_direct_module_registration: Option<bool>,
allow_direct_module_registration_and_updates: Option<bool>,
namespace_limit: Option<u32>,
namespace_registration_fee: Option<Coin>,
) -> VCResult {
Expand All @@ -403,15 +403,18 @@ pub fn update_config(
])
}

if let Some(allow) = allow_direct_module_registration {
let previous_allow = config.allow_direct_module_registration;
config.allow_direct_module_registration = allow;
if let Some(allow) = allow_direct_module_registration_and_updates {
let previous_allow = config.allow_direct_module_registration_and_updates;
config.allow_direct_module_registration_and_updates = allow;
attributes.extend(vec![
(
"previous_allow_direct_module_registration",
previous_allow.to_string(),
),
("allow_direct_module_registration", allow.to_string()),
(
"allow_direct_module_registration_and_updates",
allow.to_string(),
),
])
}

Expand All @@ -423,7 +426,10 @@ pub fn update_config(
"previous_allow_direct_module_registration",
format!("{:?}", previous_fee),
),
("allow_direct_module_registration", fee.to_string()),
(
"allow_direct_module_registration_and_updates",
fee.to_string(),
),
])
}

Expand Down Expand Up @@ -537,7 +543,7 @@ mod test {
mock_env(),
info,
InstantiateMsg {
allow_direct_module_registration: Some(true),
allow_direct_module_registration_and_updates: Some(true),
namespace_limit: 10,
namespace_registration_fee: None,
},
Expand All @@ -551,14 +557,14 @@ mod test {
}

/// Initialize the version_control with admin as creator and test account
fn mock_init_with_account(mut deps: DepsMut, direct_registration: bool) -> VCResult {
fn mock_init_with_account(mut deps: DepsMut, direct_registration_and_update: bool) -> VCResult {
let admin_info = mock_info(TEST_ADMIN, &[]);
contract::instantiate(
deps.branch(),
mock_env(),
admin_info,
InstantiateMsg {
allow_direct_module_registration: Some(direct_registration),
allow_direct_module_registration_and_updates: Some(direct_registration_and_update),
namespace_limit: 10,
namespace_registration_fee: None,
},
Expand Down Expand Up @@ -723,7 +729,7 @@ mod test {
execute_as_admin(
deps.as_mut(),
ExecuteMsg::UpdateConfig {
allow_direct_module_registration: None,
allow_direct_module_registration_and_updates: None,
namespace_limit: None,
namespace_registration_fee: Some(one_namespace_fee.clone()),
},
Expand Down Expand Up @@ -908,7 +914,7 @@ mod test {
mock_init(deps.as_mut())?;

let msg = ExecuteMsg::UpdateConfig {
allow_direct_module_registration: None,
allow_direct_module_registration_and_updates: None,
namespace_limit: Some(100),
namespace_registration_fee: None,
};
Expand All @@ -927,7 +933,7 @@ mod test {
mock_init(deps.as_mut())?;

let msg = ExecuteMsg::UpdateConfig {
allow_direct_module_registration: None,
allow_direct_module_registration_and_updates: None,
namespace_limit: Some(100),
namespace_registration_fee: None,
};
Expand All @@ -946,7 +952,7 @@ mod test {
mock_init(deps.as_mut())?;

let msg = ExecuteMsg::UpdateConfig {
allow_direct_module_registration: None,
allow_direct_module_registration_and_updates: None,
namespace_limit: Some(0),
namespace_registration_fee: None,
};
Expand All @@ -972,7 +978,7 @@ mod test {
mock_init(deps.as_mut())?;

let msg = ExecuteMsg::UpdateConfig {
allow_direct_module_registration: Some(false),
allow_direct_module_registration_and_updates: Some(false),
namespace_limit: None,
namespace_registration_fee: None,
};
Expand All @@ -991,7 +997,7 @@ mod test {
mock_init(deps.as_mut())?;

let msg = ExecuteMsg::UpdateConfig {
allow_direct_module_registration: Some(false),
allow_direct_module_registration_and_updates: Some(false),
namespace_limit: None,
namespace_registration_fee: None,
};
Expand All @@ -1004,7 +1010,7 @@ mod test {
CONFIG
.load(&deps.storage)
.unwrap()
.allow_direct_module_registration
.allow_direct_module_registration_and_updates
)
.is_equal_to(false);

Expand All @@ -1022,7 +1028,7 @@ mod test {
mock_init(deps.as_mut())?;

let msg = ExecuteMsg::UpdateConfig {
allow_direct_module_registration: None,
allow_direct_module_registration_and_updates: None,
namespace_limit: None,
namespace_registration_fee: Some(Coin {
denom: "ujunox".to_string(),
Expand All @@ -1049,7 +1055,7 @@ mod test {
};

let msg = ExecuteMsg::UpdateConfig {
allow_direct_module_registration: None,
allow_direct_module_registration_and_updates: None,
namespace_limit: None,
namespace_registration_fee: Some(new_fee.clone()),
};
Expand Down Expand Up @@ -1301,6 +1307,97 @@ mod test {
Ok(())
}

#[test]
fn update_existing_module() -> VersionControlTestResult {
let mut deps = mock_dependencies();
deps.querier = mock_manager_querier().build();
mock_init_with_account(deps.as_mut(), true)?;
let new_module = test_module();

let msg = ExecuteMsg::ProposeModules {
modules: vec![(new_module.clone(), ModuleReference::App(0))],
};

// add namespaces
execute_as(
deps.as_mut(),
TEST_OWNER,
ExecuteMsg::ClaimNamespaces {
account_id: TEST_ACCOUNT_ID,
namespaces: vec![new_module.namespace.to_string()],
},
)?;

// add modules
let res = execute_as(deps.as_mut(), TEST_OWNER, msg);
assert_that!(&res).is_ok();
let module = REGISTERED_MODULES.load(&deps.storage, &new_module)?;
assert_that!(&module).is_equal_to(&ModuleReference::App(0));

// update module code-id without changing version

let msg = ExecuteMsg::ProposeModules {
modules: vec![(new_module.clone(), ModuleReference::App(1))],
};

let res = execute_as(deps.as_mut(), TEST_OWNER, msg);
assert_that!(&res).is_ok();
let module = REGISTERED_MODULES.load(&deps.storage, &new_module)?;
assert_that!(&module).is_equal_to(&ModuleReference::App(1));
Ok(())
}

#[test]
fn update_existing_module_fails() -> VersionControlTestResult {
let mut deps = mock_dependencies();
deps.querier = mock_manager_querier().build();
mock_init_with_account(deps.as_mut(), false)?;
let new_module = test_module();

let msg = ExecuteMsg::ProposeModules {
modules: vec![(new_module.clone(), ModuleReference::App(0))],
};

// add namespaces
execute_as(
deps.as_mut(),
TEST_OWNER,
ExecuteMsg::ClaimNamespaces {
account_id: TEST_ACCOUNT_ID,
namespaces: vec![new_module.namespace.to_string()],
},
)?;

// add modules
let res = execute_as(deps.as_mut(), TEST_OWNER, msg);
assert_that!(&res).is_ok();
// approve
let msg = ExecuteMsg::ApproveOrRejectModules {
approves: vec![new_module.clone()],
rejects: vec![],
};

// approve by admin
let res = execute_as(deps.as_mut(), TEST_ADMIN, msg.clone());
assert_that!(&res).is_ok();

let module = REGISTERED_MODULES.load(&deps.storage, &new_module)?;
assert_that!(&module).is_equal_to(&ModuleReference::App(0));

// try update module code-id without changing version

let msg = ExecuteMsg::ProposeModules {
modules: vec![(new_module.clone(), ModuleReference::App(1))],
};
let res = execute_as(deps.as_mut(), TEST_OWNER, msg);
// should error as module is already approved and registered.
assert_that!(&res).is_err();

let module = REGISTERED_MODULES.load(&deps.storage, &new_module)?;
assert_that!(&module).is_equal_to(&ModuleReference::App(0));
Ok(())
}

#[test]
fn try_add_module_to_approval_with_admin() -> VersionControlTestResult {
let mut deps = mock_dependencies();
Expand Down
9 changes: 5 additions & 4 deletions contracts/native/version-control/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,16 @@ pub fn instantiate(deps: DepsMut, _env: Env, info: MessageInfo, msg: Instantiate
cw2::set_contract_version(deps.storage, VERSION_CONTROL, CONTRACT_VERSION)?;

let InstantiateMsg {
allow_direct_module_registration,
allow_direct_module_registration_and_updates,
namespace_limit,
namespace_registration_fee,
} = msg;

CONFIG.save(
deps.storage,
&Config {
allow_direct_module_registration: allow_direct_module_registration.unwrap_or(false),
allow_direct_module_registration_and_updates:
allow_direct_module_registration_and_updates.unwrap_or(false),
namespace_limit,
namespace_registration_fee: namespace_registration_fee.unwrap_or(Coin {
denom: "none".to_string(),
Expand Down Expand Up @@ -99,13 +100,13 @@ pub fn execute(deps: DepsMut, env: Env, info: MessageInfo, msg: ExecuteMsg) -> V
account_base: base,
} => add_account(deps, info, account_id, base),
ExecuteMsg::UpdateConfig {
allow_direct_module_registration,
allow_direct_module_registration_and_updates,
namespace_limit,
namespace_registration_fee,
} => update_config(
deps,
info,
allow_direct_module_registration,
allow_direct_module_registration_and_updates,
namespace_limit,
namespace_registration_fee,
),
Expand Down
2 changes: 1 addition & 1 deletion contracts/native/version-control/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ mod testing {
mock_env(),
info,
version_control::InstantiateMsg {
allow_direct_module_registration: Some(true),
allow_direct_module_registration_and_updates: Some(true),
namespace_limit: 10,
namespace_registration_fee: None,
},
Expand Down
2 changes: 1 addition & 1 deletion contracts/native/version-control/src/queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ mod test {
mock_env(),
info,
InstantiateMsg {
allow_direct_module_registration: Some(true),
allow_direct_module_registration_and_updates: Some(true),
namespace_limit: 10,
namespace_registration_fee: None,
},
Expand Down
9 changes: 6 additions & 3 deletions packages/abstract-core/src/native/version_control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub type ModuleMapEntry = (ModuleInfo, ModuleReference);
/// Contains configuration info of version control.
#[cosmwasm_schema::cw_serde]
pub struct Config {
pub allow_direct_module_registration: bool,
pub allow_direct_module_registration_and_updates: bool,
pub namespace_limit: u32,
pub namespace_registration_fee: cosmwasm_std::Coin,
}
Expand Down Expand Up @@ -92,7 +92,10 @@ pub struct AccountBase {
/// Version Control Instantiate Msg
#[cosmwasm_schema::cw_serde]
pub struct InstantiateMsg {
pub allow_direct_module_registration: Option<bool>,
/// allows users to directly register modules without going through approval
/// Also allows them to change the module reference of an existing module
/// SHOULD ONLY BE `true` FOR TESTING
pub allow_direct_module_registration_and_updates: Option<bool>,
pub namespace_limit: u32,
pub namespace_registration_fee: Option<Coin>,
}
Expand Down Expand Up @@ -144,7 +147,7 @@ pub enum ExecuteMsg {
/// 1. Whether the contract allows direct module registration
/// 2. the number of namespaces an Account can claim
UpdateConfig {
allow_direct_module_registration: Option<bool>,
allow_direct_module_registration_and_updates: Option<bool>,
namespace_limit: Option<u32>,
namespace_registration_fee: Option<Coin>,
},
Expand Down
2 changes: 1 addition & 1 deletion packages/abstract-interface/src/deployment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl<Chain: CwEnv> Abstract<Chain> {

self.version_control.instantiate(
&abstract_core::version_control::InstantiateMsg {
allow_direct_module_registration: Some(true),
allow_direct_module_registration_and_updates: Some(true),
namespace_limit: 1,
namespace_registration_fee: None,
},
Expand Down

0 comments on commit d0be31d

Please sign in to comment.