Skip to content

Commit 2125326

Browse files
committed
add software upgrade strategies support and prettify types.rs files for chain manager
1 parent ccc6b9a commit 2125326

File tree

7 files changed

+78
-25
lines changed

7 files changed

+78
-25
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub const MSG_TYPE_SOFTWARE_UPGRADE: &str = "/cosmos.upgrade.v1beta1.MsgSoftwareUpgrade";
2+
pub const MSG_TYPE_CANCEL_SOFTWARE_UPGRADE: &str = "/cosmos.upgrade.v1beta1.MsgCancelUpgrade";

contracts/dao/neutron-chain-manager/src/contract.rs

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1+
use crate::adminmodule_module_types::{
2+
MSG_TYPE_CANCEL_SOFTWARE_UPGRADE, MSG_TYPE_SOFTWARE_UPGRADE,
3+
};
14
use crate::cron_module_types::{
25
MsgUpdateParamsCron, ParamsRequestCron, ParamsResponseCron, MSG_TYPE_ADD_SCHEDULE,
36
MSG_TYPE_REMOVE_SCHEDULE, MSG_TYPE_UPDATE_PARAMS_CRON, PARAMS_QUERY_PATH_CRON,
47
};
5-
use crate::dex_module_param_types::{
8+
use crate::dex_module_types::{
69
MsgUpdateParamsDex, ParamsRequestDex, ParamsResponseDex, MSG_TYPE_UPDATE_PARAMS_DEX,
710
PARAMS_QUERY_PATH_DEX,
811
};
9-
use crate::tokenfactory_module_param_types::{
12+
use crate::tokenfactory_module_types::{
1013
MsgUpdateParamsTokenfactory, ParamsRequestTokenfactory, ParamsResponseTokenfactory,
1114
MSG_TYPE_UPDATE_PARAMS_TOKENFACTORY, PARAMS_QUERY_PATH_TOKENFACTORY,
1215
};
@@ -236,29 +239,38 @@ fn check_proposal_execute_message(
236239
let typed_proposal: ProposalExecuteMessageJSON =
237240
serde_json_wasm::from_str(proposal.message.as_str())?;
238241

239-
if typed_proposal.type_field.as_str() == MSG_TYPE_UPDATE_PARAMS_CRON {
240-
check_cron_update_msg_params(deps, strategy, proposal)?;
241-
Ok(())
242-
} else if typed_proposal.type_field.as_str() == MSG_TYPE_UPDATE_PARAMS_TOKENFACTORY {
243-
check_tokenfactory_update_msg_params(deps, strategy, proposal)?;
244-
Ok(())
245-
} else if typed_proposal.type_field.as_str() == MSG_TYPE_UPDATE_PARAMS_DEX {
246-
check_dex_update_msg_params(deps, strategy, proposal)?;
247-
Ok(())
248-
} else if typed_proposal.type_field.as_str() == MSG_TYPE_ADD_SCHEDULE {
249-
if strategy.has_cron_add_schedule_permission() {
242+
match typed_proposal.type_field.as_str() {
243+
MSG_TYPE_UPDATE_PARAMS_CRON => {
244+
check_cron_update_msg_params(deps, strategy, proposal)?;
250245
Ok(())
251-
} else {
252-
Err(ContractError::Unauthorized {})
253246
}
254-
} else if typed_proposal.type_field.as_str() == MSG_TYPE_REMOVE_SCHEDULE {
255-
if strategy.has_cron_remove_schedule_permission() {
247+
MSG_TYPE_UPDATE_PARAMS_TOKENFACTORY => {
248+
check_tokenfactory_update_msg_params(deps, strategy, proposal)?;
256249
Ok(())
257-
} else {
258-
Err(ContractError::Unauthorized {})
259250
}
260-
} else {
261-
Err(ContractError::Unauthorized {})
251+
MSG_TYPE_UPDATE_PARAMS_DEX => {
252+
check_dex_update_msg_params(deps, strategy, proposal)?;
253+
Ok(())
254+
}
255+
MSG_TYPE_ADD_SCHEDULE => match strategy.has_cron_add_schedule_permission() {
256+
true => Ok(()),
257+
false => Err(ContractError::Unauthorized {}),
258+
},
259+
MSG_TYPE_REMOVE_SCHEDULE => match strategy.has_cron_remove_schedule_permission() {
260+
true => Ok(()),
261+
false => Err(ContractError::Unauthorized {}),
262+
},
263+
MSG_TYPE_SOFTWARE_UPGRADE => match strategy.has_software_upgrade_permission() {
264+
true => Ok(()),
265+
false => Err(ContractError::Unauthorized {}),
266+
},
267+
MSG_TYPE_CANCEL_SOFTWARE_UPGRADE => {
268+
match strategy.has_cancel_software_upgrade_permission() {
269+
true => Ok(()),
270+
false => Err(ContractError::Unauthorized {}),
271+
}
272+
}
273+
_ => Err(ContractError::Unauthorized {}),
262274
}
263275
}
264276

File renamed without changes.
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
pub mod adminmodule_module_types;
12
pub mod contract;
23
mod cron_module_types;
3-
mod dex_module_param_types;
4+
mod dex_module_types;
45
mod error;
56
pub mod msg;
67
pub mod state;
78
#[cfg(test)]
89
mod testing;
9-
mod tokenfactory_module_param_types;
10+
mod tokenfactory_module_types;
1011
pub mod utils;

contracts/dao/neutron-chain-manager/src/msg.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,34 @@ impl Strategy {
171171
}
172172
}
173173
}
174+
175+
pub fn has_software_upgrade_permission(&self) -> bool {
176+
match self {
177+
Strategy::AllowAll => true,
178+
Strategy::AllowOnly(permissions) => {
179+
match permissions.get(&PermissionType::SoftwareUpgradePermission) {
180+
Some(Permission::SoftwareUpgradePermission(software_upgrade_params)) => {
181+
software_upgrade_params.upgrade
182+
}
183+
_ => false,
184+
}
185+
}
186+
}
187+
}
188+
189+
pub fn has_cancel_software_upgrade_permission(&self) -> bool {
190+
match self {
191+
Strategy::AllowAll => true,
192+
Strategy::AllowOnly(permissions) => {
193+
match permissions.get(&PermissionType::SoftwareUpgradePermission) {
194+
Some(Permission::SoftwareUpgradePermission(software_upgrade_params)) => {
195+
software_upgrade_params.cancel_upgrade
196+
}
197+
_ => false,
198+
}
199+
}
200+
}
201+
}
174202
}
175203

176204
#[cw_serde]
@@ -183,6 +211,7 @@ pub enum Permission {
183211
UpdateTokenfactoryParamsPermission(TokenfactoryUpdateParamsPermission),
184212
UpdateDexParamsPermission(DexUpdateParamsPermission),
185213
CronPermission(CronPermission),
214+
SoftwareUpgradePermission(SoftwareUpgradePermission),
186215
}
187216

188217
impl From<Permission> for PermissionType {
@@ -195,6 +224,7 @@ impl From<Permission> for PermissionType {
195224
}
196225
Permission::UpdateDexParamsPermission(_) => PermissionType::UpdateDexParamsPermission,
197226
Permission::CronPermission(_) => PermissionType::CronPermission,
227+
Permission::SoftwareUpgradePermission(_) => PermissionType::SoftwareUpgradePermission,
198228
}
199229
}
200230
}
@@ -207,6 +237,7 @@ pub enum PermissionType {
207237
UpdateTokenfactoryParamsPermission,
208238
UpdateDexParamsPermission,
209239
CronPermission,
240+
SoftwareUpgradePermission,
210241
}
211242

212243
#[cw_serde]
@@ -260,6 +291,13 @@ pub struct DexUpdateParamsPermission {
260291
pub good_til_purge_allowance: bool,
261292
}
262293

294+
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
295+
#[serde(rename_all = "snake_case")]
296+
pub struct SoftwareUpgradePermission {
297+
pub upgrade: bool,
298+
pub cancel_upgrade: bool,
299+
}
300+
263301
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, JsonSchema)]
264302
#[serde(rename_all = "snake_case")]
265303
pub struct ProposalExecuteMessageJSON {

contracts/dao/neutron-chain-manager/src/testing/mock_querier.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::cron_module_types::{ParamsCron, ParamsResponseCron};
2-
use crate::dex_module_param_types::{ParamsDex, ParamsResponseDex};
3-
use crate::tokenfactory_module_param_types::{ParamsResponseTokenfactory, ParamsTokenfactory};
2+
use crate::dex_module_types::{ParamsDex, ParamsResponseDex};
3+
use crate::tokenfactory_module_types::{ParamsResponseTokenfactory, ParamsTokenfactory};
44
use cosmwasm_std::testing::{MockApi, MockQuerier, MockStorage};
55
use cosmwasm_std::{
66
coin, from_json, to_json_binary, ContractResult, Empty, OwnedDeps, Querier, QuerierResult,
File renamed without changes.

0 commit comments

Comments
 (0)