From 1148f9d3ba6e9bcbfa945e9e6b4024f88141f1a8 Mon Sep 17 00:00:00 2001 From: Joe Monem Date: Wed, 8 Jan 2025 12:19:39 +0200 Subject: [PATCH] ref: update outdated permissions during migration, check for expired start and end times --- packages/std/src/ado_base/permissioning.rs | 14 +++++ packages/std/src/ado_contract/execute.rs | 65 ++++++++++++++++------ 2 files changed, 62 insertions(+), 17 deletions(-) diff --git a/packages/std/src/ado_base/permissioning.rs b/packages/std/src/ado_base/permissioning.rs index b51c03c3a..812e0cd97 100644 --- a/packages/std/src/ado_base/permissioning.rs +++ b/packages/std/src/ado_base/permissioning.rs @@ -195,6 +195,20 @@ impl LocalPermission { let start_time = start.get_time(&env.block); let exp_time = expiration.get_time(&env.block); + // Check if start time is after current time + if start_time.is_expired(&env.block) { + return Err(ContractError::StartTimeInThePast { + current_time: env.block.time.seconds(), + current_block: env.block.height, + }); + } + + // Check if expiration time is after current time + if exp_time.is_expired(&env.block) { + return Err(ContractError::ExpirationInPast {}); + } + + // Check if start time is before expiration time if start_time > exp_time { return Err(ContractError::StartTimeAfterEndTime {}); } diff --git a/packages/std/src/ado_contract/execute.rs b/packages/std/src/ado_contract/execute.rs index a07f0892d..24c61c3ef 100644 --- a/packages/std/src/ado_contract/execute.rs +++ b/packages/std/src/ado_contract/execute.rs @@ -6,26 +6,24 @@ use { std::ops::Deref, }; -use crate::ado_contract::ADOContract; -use crate::amp::addresses::AndrAddr; -use crate::amp::messages::AMPPkt; -use crate::common::context::ExecuteContext; -use crate::common::reply::ReplyId; -use crate::error::from_semver; -use crate::os::{aos_querier::AOSQuerier, economics::ExecuteMsg as EconomicsExecuteMsg}; use crate::{ - ado_base::{AndromedaMsg, InstantiateMsg}, - error::ContractError, + ado_base::{ + permissioning::{LocalPermission, Permission}, + AndromedaMsg, InstantiateMsg, + }, + ado_contract::ADOContract, + amp::{addresses::AndrAddr, messages::AMPPkt}, + common::{context::ExecuteContext, reply::ReplyId}, + error::{from_semver, ContractError}, + os::{aos_querier::AOSQuerier, economics::ExecuteMsg as EconomicsExecuteMsg}, }; use cosmwasm_std::{ attr, ensure, from_json, to_json_binary, Addr, Api, ContractInfoResponse, CosmosMsg, Deps, DepsMut, Env, MessageInfo, QuerierWrapper, Response, StdError, Storage, SubMsg, WasmMsg, }; use cw2::{get_contract_version, set_contract_version}; - use semver::Version; -use serde::de::DeserializeOwned; -use serde::Serialize; +use serde::{de::DeserializeOwned, Serialize}; type ExecuteContextFunction = fn(ExecuteContext, M) -> Result; @@ -178,15 +176,48 @@ impl ADOContract<'_> { // Check if using old permission structure (without 'start' variant) let json_str = String::from_utf8(to_json_binary(&local_permission)?.0)?; if !json_str.contains("start") { - // Clear all permissions if using old structure - self.execute_clear_all_permissions(ExecuteContext::new( + let ctx = ExecuteContext::new( deps.branch(), MessageInfo { sender: owner, funds: vec![], }, env, - ))?; + ); + + // Determine permission type from JSON structure + if json_str.contains("whitelist") { + self.execute_set_permission( + ctx, + vec![AndrAddr::from_string(actor)], + first_action.clone(), + Permission::Local(LocalPermission::Whitelisted { + start: None, + expiration: None, + }), + )?; + } else if json_str.contains("blacklist") { + self.execute_set_permission( + ctx, + vec![AndrAddr::from_string(actor)], + first_action.clone(), + Permission::Local(LocalPermission::Blacklisted { + start: None, + expiration: None, + }), + )?; + } else if json_str.contains("limited") { + self.execute_set_permission( + ctx, + vec![AndrAddr::from_string(actor)], + first_action.clone(), + Permission::Local(LocalPermission::Limited { + start: None, + expiration: None, + uses: 0, + }), + )?; + } } } } @@ -224,7 +255,7 @@ impl ADOContract<'_> { )?) .is_ok() { - self.rates.clear(deps.storage); + self.rates.clear(deps.branch().storage); } break; } @@ -234,7 +265,7 @@ impl ADOContract<'_> { } } - set_contract_version(deps.storage, contract_name, contract_version)?; + set_contract_version(deps.branch().storage, contract_name, contract_version)?; Ok(Response::default()) }