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

Engagement half-life #200

Merged
merged 4 commits into from
Jan 6, 2023
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
28 changes: 23 additions & 5 deletions contracts/tg4-engagement/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#[cfg(not(feature = "library"))]
use cosmwasm_std::entry_point;
use cosmwasm_std::{
coin, to_binary, Addr, BankMsg, Binary, Coin, CustomQuery, Decimal, Deps, DepsMut, Empty, Env,
Event, MessageInfo, Order, StdResult, Timestamp, Uint128,
coin, to_binary, Addr, BankMsg, Binary, Coin, CustomQuery, Decimal, Deps, DepsMut, Env, Event,
MessageInfo, Order, StdResult, Timestamp, Uint128,
};
use cw2::set_contract_version;
use cw_storage_plus::Bound;
Expand All @@ -14,8 +14,8 @@ use tg4::{

use crate::error::ContractError;
use crate::msg::{
DelegatedResponse, ExecuteMsg, HalflifeInfo, HalflifeResponse, InstantiateMsg, PreauthResponse,
QueryMsg, RewardsResponse, SudoMsg,
DelegatedResponse, ExecuteMsg, HalflifeInfo, HalflifeResponse, InstantiateMsg, MigrateMsg,
PreauthResponse, QueryMsg, RewardsResponse, SudoMsg,
};
use crate::state::{
Distribution, Halflife, WithdrawAdjustment, DISTRIBUTION, HALFLIFE, PREAUTH_SLASHING,
Expand Down Expand Up @@ -913,8 +913,26 @@ fn list_members_by_points<Q: CustomQuery>(
}

#[cfg_attr(not(feature = "library"), entry_point)]
pub fn migrate(deps: DepsMut, _env: Env, _msg: Empty) -> Result<Response, ContractError> {
pub fn migrate(
deps: DepsMut<TgradeQuery>,
_env: Env,
msg: MigrateMsg,
) -> Result<Response, ContractError> {
ensure_from_older_version(deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
maurolacy marked this conversation as resolved.
Show resolved Hide resolved
if let Some(duration) = msg.halflife {
// Update half life's duration
// Zero duration means no / remove half life
HALFLIFE.update(deps.storage, |hf| -> StdResult<_> {
Ok(Halflife {
halflife: if duration.seconds() > 0 {
Some(duration)
} else {
None
},
last_applied: hf.last_applied,
})
})?;
};
Ok(Response::new())
}

Expand Down
6 changes: 6 additions & 0 deletions contracts/tg4-engagement/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,12 @@ pub struct ListSlashersResponse {
pub slashers: Vec<String>,
}

#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, JsonSchema, Debug)]
#[serde(rename_all = "snake_case")]
pub struct MigrateMsg {
pub halflife: Option<Duration>,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
51 changes: 51 additions & 0 deletions contracts/tg4-engagement/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -921,3 +921,54 @@ mod slashing {
assert_eq!(suite.token_balance(members[2]).unwrap(), 0);
}
}

mod migration {
use super::*;
use crate::msg::MigrateMsg;

#[test]
fn migration_can_alter_cfg() {
let mut suite = SuiteBuilder::new()
.with_halflife(Duration::new(100))
.build();
let admin = suite.admin().to_string();

let cfg = suite.halflife().unwrap();
assert_eq!(cfg.halflife_info.unwrap().halflife, Duration::new(100));

suite
.migrate(
&admin,
&MigrateMsg {
halflife: Some(Duration::new(200)),
},
)
.unwrap();

let cfg = suite.halflife().unwrap();
assert_eq!(cfg.halflife_info.unwrap().halflife.seconds(), 200);
}

#[test]
fn migration_can_remove_halflife() {
let mut suite = SuiteBuilder::new()
.with_halflife(Duration::new(100))
.build();
let admin = suite.admin().to_string();

let cfg = suite.halflife().unwrap();
assert_eq!(cfg.halflife_info.unwrap().halflife, Duration::new(100));

suite
.migrate(
&admin,
&MigrateMsg {
halflife: Some(Duration::new(0)),
},
)
.unwrap();

let cfg = suite.halflife().unwrap();
assert!(cfg.halflife_info.is_none());
}
}
28 changes: 27 additions & 1 deletion contracts/tg4-engagement/src/multitest/suite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fn contract_engagement() -> Box<dyn Contract<TgradeMsg, TgradeQuery>> {
crate::contract::instantiate,
crate::contract::query,
)
.with_migrate(crate::contract::migrate)
.with_sudo(crate::contract::sudo);

Box::new(contract)
Expand Down Expand Up @@ -133,6 +134,7 @@ impl SuiteBuilder {

Suite {
app,
code_id: contract_id,
contract,
owner,
denom,
Expand All @@ -145,9 +147,11 @@ impl SuiteBuilder {
pub struct Suite {
#[derivative(Debug = "ignore")]
pub app: TgradeApp,
/// The code id of the engagement contract
code_id: u64,
/// Engagement contract address
pub contract: Addr,
/// Mixer contract address
/// Admin of engagement contract
pub owner: Addr,
/// Denom of tokens which might be distributed by this contract
pub denom: String,
Expand All @@ -170,6 +174,10 @@ impl Suite {
)
}

pub fn admin(&self) -> &str {
self.owner.as_str()
}

pub fn withdraw_funds<'s>(
&mut self,
executor: &str,
Expand Down Expand Up @@ -340,4 +348,22 @@ impl Suite {
)?;
Ok(resp.members)
}

/// Queries engagement contract for its halflife
pub fn halflife(&self) -> StdResult<HalflifeResponse> {
self.app
.wrap()
.query_wasm_smart(&self.contract, &QueryMsg::Halflife {})
}

/// Migrates the contract to the same version (same code id), but possibly changing
/// some cfg values via MigrateMsg.
pub fn migrate(&mut self, addr: &str, msg: &MigrateMsg) -> AnyResult<AppResponse> {
self.app.migrate_contract(
Addr::unchecked(addr),
self.contract.clone(),
msg,
self.code_id,
)
}
}