Skip to content
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

Feat: software upgrade via chain manager [NTRN-400] #115

Merged
merged 2 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/dao/neutron-chain-manager/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = "A chain manager implementation that grants fine-grained admin per
edition = "2021"
name = "neutron-chain-manager"
repository = "https://github.com/neutron-org/neutron-dao"
version = "0.3.0"
version = "0.4.0"

[lib]
crate-type = ["cdylib", "rlib"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub const MSG_TYPE_SOFTWARE_UPGRADE: &str = "/cosmos.upgrade.v1beta1.MsgSoftwareUpgrade";
pub const MSG_TYPE_CANCEL_SOFTWARE_UPGRADE: &str = "/cosmos.upgrade.v1beta1.MsgCancelUpgrade";
54 changes: 33 additions & 21 deletions contracts/dao/neutron-chain-manager/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use crate::adminmodule_module_types::{
MSG_TYPE_CANCEL_SOFTWARE_UPGRADE, MSG_TYPE_SOFTWARE_UPGRADE,
};
use crate::cron_module_types::{
MsgUpdateParamsCron, ParamsRequestCron, ParamsResponseCron, MSG_TYPE_ADD_SCHEDULE,
MSG_TYPE_REMOVE_SCHEDULE, MSG_TYPE_UPDATE_PARAMS_CRON, PARAMS_QUERY_PATH_CRON,
};
use crate::dex_module_param_types::{
use crate::dex_module_types::{
MsgUpdateParamsDex, ParamsRequestDex, ParamsResponseDex, MSG_TYPE_UPDATE_PARAMS_DEX,
PARAMS_QUERY_PATH_DEX,
};
use crate::tokenfactory_module_param_types::{
use crate::tokenfactory_module_types::{
MsgUpdateParamsTokenfactory, ParamsRequestTokenfactory, ParamsResponseTokenfactory,
MSG_TYPE_UPDATE_PARAMS_TOKENFACTORY, PARAMS_QUERY_PATH_TOKENFACTORY,
};
Expand Down Expand Up @@ -236,29 +239,38 @@ fn check_proposal_execute_message(
let typed_proposal: ProposalExecuteMessageJSON =
serde_json_wasm::from_str(proposal.message.as_str())?;

if typed_proposal.type_field.as_str() == MSG_TYPE_UPDATE_PARAMS_CRON {
check_cron_update_msg_params(deps, strategy, proposal)?;
Ok(())
} else if typed_proposal.type_field.as_str() == MSG_TYPE_UPDATE_PARAMS_TOKENFACTORY {
check_tokenfactory_update_msg_params(deps, strategy, proposal)?;
Ok(())
} else if typed_proposal.type_field.as_str() == MSG_TYPE_UPDATE_PARAMS_DEX {
check_dex_update_msg_params(deps, strategy, proposal)?;
Ok(())
} else if typed_proposal.type_field.as_str() == MSG_TYPE_ADD_SCHEDULE {
if strategy.has_cron_add_schedule_permission() {
match typed_proposal.type_field.as_str() {
MSG_TYPE_UPDATE_PARAMS_CRON => {
check_cron_update_msg_params(deps, strategy, proposal)?;
Ok(())
} else {
Err(ContractError::Unauthorized {})
}
} else if typed_proposal.type_field.as_str() == MSG_TYPE_REMOVE_SCHEDULE {
if strategy.has_cron_remove_schedule_permission() {
MSG_TYPE_UPDATE_PARAMS_TOKENFACTORY => {
check_tokenfactory_update_msg_params(deps, strategy, proposal)?;
Ok(())
} else {
Err(ContractError::Unauthorized {})
}
} else {
Err(ContractError::Unauthorized {})
MSG_TYPE_UPDATE_PARAMS_DEX => {
check_dex_update_msg_params(deps, strategy, proposal)?;
Ok(())
}
MSG_TYPE_ADD_SCHEDULE => match strategy.has_cron_add_schedule_permission() {
true => Ok(()),
false => Err(ContractError::Unauthorized {}),
},
MSG_TYPE_REMOVE_SCHEDULE => match strategy.has_cron_remove_schedule_permission() {
true => Ok(()),
false => Err(ContractError::Unauthorized {}),
},
MSG_TYPE_SOFTWARE_UPGRADE => match strategy.has_software_upgrade_permission() {
true => Ok(()),
false => Err(ContractError::Unauthorized {}),
},
MSG_TYPE_CANCEL_SOFTWARE_UPGRADE => {
match strategy.has_cancel_software_upgrade_permission() {
true => Ok(()),
false => Err(ContractError::Unauthorized {}),
}
}
_ => Err(ContractError::Unauthorized {}),
}
}

Expand Down
5 changes: 3 additions & 2 deletions contracts/dao/neutron-chain-manager/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
pub mod adminmodule_module_types;
pub mod contract;
mod cron_module_types;
mod dex_module_param_types;
mod dex_module_types;
mod error;
pub mod msg;
pub mod state;
#[cfg(test)]
mod testing;
mod tokenfactory_module_param_types;
mod tokenfactory_module_types;
pub mod utils;
38 changes: 38 additions & 0 deletions contracts/dao/neutron-chain-manager/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,34 @@ impl Strategy {
}
}
}

pub fn has_software_upgrade_permission(&self) -> bool {
match self {
Strategy::AllowAll => true,
Strategy::AllowOnly(permissions) => {
match permissions.get(&PermissionType::SoftwareUpgradePermission) {
Some(Permission::SoftwareUpgradePermission(software_upgrade_params)) => {
software_upgrade_params.upgrade
}
_ => false,
}
}
}
}

pub fn has_cancel_software_upgrade_permission(&self) -> bool {
match self {
Strategy::AllowAll => true,
Strategy::AllowOnly(permissions) => {
match permissions.get(&PermissionType::SoftwareUpgradePermission) {
Some(Permission::SoftwareUpgradePermission(software_upgrade_params)) => {
software_upgrade_params.cancel_upgrade
}
_ => false,
}
}
}
}
}

#[cw_serde]
Expand All @@ -183,6 +211,7 @@ pub enum Permission {
UpdateTokenfactoryParamsPermission(TokenfactoryUpdateParamsPermission),
UpdateDexParamsPermission(DexUpdateParamsPermission),
CronPermission(CronPermission),
SoftwareUpgradePermission(SoftwareUpgradePermission),
}

impl From<Permission> for PermissionType {
Expand All @@ -195,6 +224,7 @@ impl From<Permission> for PermissionType {
}
Permission::UpdateDexParamsPermission(_) => PermissionType::UpdateDexParamsPermission,
Permission::CronPermission(_) => PermissionType::CronPermission,
Permission::SoftwareUpgradePermission(_) => PermissionType::SoftwareUpgradePermission,
}
}
}
Expand All @@ -207,6 +237,7 @@ pub enum PermissionType {
UpdateTokenfactoryParamsPermission,
UpdateDexParamsPermission,
CronPermission,
SoftwareUpgradePermission,
}

#[cw_serde]
Expand Down Expand Up @@ -260,6 +291,13 @@ pub struct DexUpdateParamsPermission {
pub good_til_purge_allowance: bool,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct SoftwareUpgradePermission {
pub upgrade: bool,
pub cancel_upgrade: bool,
}

#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub struct ProposalExecuteMessageJSON {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::cron_module_types::{ParamsCron, ParamsResponseCron};
use crate::dex_module_param_types::{ParamsDex, ParamsResponseDex};
use crate::tokenfactory_module_param_types::{ParamsResponseTokenfactory, ParamsTokenfactory};
use crate::dex_module_types::{ParamsDex, ParamsResponseDex};
use crate::tokenfactory_module_types::{ParamsResponseTokenfactory, ParamsTokenfactory};
use cosmwasm_std::testing::{MockApi, MockQuerier, MockStorage};
use cosmwasm_std::{
coin, from_json, to_json_binary, ContractResult, Empty, OwnedDeps, Querier, QuerierResult,
Expand Down
Loading