Skip to content

Commit

Permalink
rename "max slashing" to "slash ratio"
Browse files Browse the repository at this point in the history
  • Loading branch information
uint committed Nov 14, 2023
1 parent db4e9d1 commit 745ee54
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 66 deletions.
34 changes: 17 additions & 17 deletions contracts/provider/external-staking/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::msg::{
StakeInfo, StakesResponse, TxResponse, ValidatorPendingRewards,
};
use crate::stakes::Stakes;
use crate::state::{Config, Distribution, MaxSlashing, Stake};
use crate::state::{Config, Distribution, SlashRatio, Stake};

pub const CONTRACT_NAME: &str = env!("CARGO_PKG_NAME");
pub const CONTRACT_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -100,21 +100,21 @@ impl ExternalStakingContract<'_> {
vault: String,
unbonding_period: u64,
remote_contact: crate::msg::AuthorizedEndpoint,
max_slashing: MaxSlashing,
slash_ratio: SlashRatio,
) -> Result<Response, ContractError> {
let vault = ctx.deps.api.addr_validate(&vault)?;
let vault = VaultApiHelper(vault);

if max_slashing.double_sign > Decimal::one() || max_slashing.offline > Decimal::one() {
return Err(ContractError::InvalidMaxSlashing);
if slash_ratio.double_sign > Decimal::one() || slash_ratio.offline > Decimal::one() {
return Err(ContractError::InvalidSlashRatio);
}

let config = Config {
denom,
rewards_denom,
vault,
unbonding_period,
max_slashing,
slash_ratio,
};

self.config.save(ctx.deps.storage, &config)?;
Expand Down Expand Up @@ -855,9 +855,9 @@ impl ExternalStakingContract<'_> {
reason: SlashingReason,
) -> Result<Option<WasmMsg>, ContractError> {
let config = self.config.load(storage)?;
let max_slashing = match reason {
SlashingReason::Offline => config.max_slashing.offline,
SlashingReason::DoubleSign => config.max_slashing.double_sign,
let slash_ratio = match reason {
SlashingReason::Offline => config.slash_ratio.offline,
SlashingReason::DoubleSign => config.slash_ratio.double_sign,
};
// Get the list of users staking via this validator
let users = self
Expand All @@ -884,7 +884,7 @@ impl ExternalStakingContract<'_> {
// Calculating slashing with always the `high` value of the range goes against the user
// in some scenario (pending stakes while slashing); but the scenario is relatively
// unlikely.
let stake_slash = stake_high * max_slashing;
let stake_slash = stake_high * slash_ratio;
// Requires proper saturating methods in commit/rollback_stake/unstake
stake.stake = ValueRange::new(
stake_low.saturating_sub(stake_slash),
Expand All @@ -903,7 +903,7 @@ impl ExternalStakingContract<'_> {
self.distribution.save(storage, validator, &distribution)?;

// Slash the unbondings
let pending_slashed = stake.slash_pending(&env.block, max_slashing);
let pending_slashed = stake.slash_pending(&env.block, slash_ratio);

self.stakes.stake.save(storage, (&user, validator), stake)?;

Expand Down Expand Up @@ -1173,7 +1173,7 @@ pub mod cross_staking {

use super::*;
use cosmwasm_std::{from_binary, Binary};
use mesh_apis::{cross_staking_api::CrossStakingApi, local_staking_api::MaxSlashResponse};
use mesh_apis::{cross_staking_api::CrossStakingApi, local_staking_api::SlashRatioResponse};

#[contract(module=crate::contract)]
#[messages(mesh_apis::cross_staking_api as CrossStakingApi)]
Expand Down Expand Up @@ -1399,11 +1399,11 @@ pub mod cross_staking {
}

#[msg(query)]
fn max_slash(&self, ctx: QueryCtx) -> Result<MaxSlashResponse, ContractError> {
let Config { max_slashing, .. } = self.config.load(ctx.deps.storage)?;
Ok(MaxSlashResponse {
max_slash_dsign: max_slashing.double_sign,
max_slash_offline: max_slashing.offline,
fn max_slash(&self, ctx: QueryCtx) -> Result<SlashRatioResponse, ContractError> {
let Config { slash_ratio, .. } = self.config.load(ctx.deps.storage)?;
Ok(SlashRatioResponse {
slash_ratio_dsign: slash_ratio.double_sign,
slash_ratio_offline: slash_ratio.offline,
})
}
}
Expand Down Expand Up @@ -1443,7 +1443,7 @@ mod tests {
connection_id: "connection_id_1".to_string(),
port_id: "port_id_1".to_string(),
},
MaxSlashing {
SlashRatio {
double_sign: Decimal::percent(10),
offline: Decimal::percent(10),
},
Expand Down
4 changes: 2 additions & 2 deletions contracts/provider/external-staking/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ pub enum ContractError {
#[error("Invalid denom, {0} expected")]
InvalidDenom(String),

#[error("You cannot use a max slashing rate over 1.0 (100%)")]
InvalidMaxSlashing,
#[error("You cannot specify a slash ratio over 1.0 (100%)")]
InvalidSlashRatio,

#[error("Not enough tokens staked, up to {0} can be unbond")]
NotEnoughStake(Uint128),
Expand Down
10 changes: 5 additions & 5 deletions contracts/provider/external-staking/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::contract::cross_staking::test_utils::CrossStakingApi;
use crate::contract::multitest_utils::{CodeId, ExternalStakingContractProxy};
use crate::error::ContractError;
use crate::msg::{AuthorizedEndpoint, ReceiveVirtualStake, StakeInfo, ValidatorPendingRewards};
use crate::state::{MaxSlashing, Stake};
use crate::state::{SlashRatio, Stake};
use crate::test_methods_impl::test_utils::TestMethods;
use utils::{
assert_rewards, get_last_external_staking_pending_tx_id, AppExt as _, ContractExt as _,
Expand Down Expand Up @@ -53,8 +53,8 @@ fn setup<'app>(
let native_staking_instantiate = NativeStakingInstantiateMsg {
denom: OSMO.to_owned(),
proxy_code_id: native_staking_proxy_code.code_id(),
max_slashing_dsign: Decimal::percent(LOCAL_SLASHING_PERCENTAGE_DSIGN),
max_slashing_offline: Decimal::percent(LOCAL_SLASHING_PERCENTAGE_OFFLINE),
slash_ratio_dsign: Decimal::percent(LOCAL_SLASHING_PERCENTAGE_DSIGN),
slash_ratio_offline: Decimal::percent(LOCAL_SLASHING_PERCENTAGE_OFFLINE),
};

let staking_init = StakingInitInfo {
Expand All @@ -77,7 +77,7 @@ fn setup<'app>(
vault.contract_addr.to_string(),
unbond_period,
remote_contact,
MaxSlashing {
SlashRatio {
double_sign: Decimal::percent(SLASHING_PERCENTAGE),
offline: Decimal::percent(SLASHING_PERCENTAGE),
},
Expand All @@ -101,7 +101,7 @@ fn instantiate() {

let max_slash = contract.cross_staking_api_proxy().max_slash().unwrap();
assert_eq!(
max_slash.max_slash_dsign,
max_slash.slash_ratio_dsign,
Decimal::percent(SLASHING_PERCENTAGE)
);
}
Expand Down
6 changes: 3 additions & 3 deletions contracts/provider/external-staking/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ pub struct Config {
pub vault: VaultApiHelper,
/// Unbonding period for claims in seconds
pub unbonding_period: u64,
/// Max slash percentage
pub max_slashing: MaxSlashing,
/// The slash ratio
pub slash_ratio: SlashRatio,
}

#[cw_serde]
pub struct MaxSlashing {
pub struct SlashRatio {
pub double_sign: Decimal,
pub offline: Decimal,
}
Expand Down
12 changes: 6 additions & 6 deletions contracts/provider/native-staking/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ impl NativeStakingContract<'_> {
ctx: InstantiateCtx,
denom: String,
proxy_code_id: u64,
max_slashing_dsign: Decimal,
max_slashing_offline: Decimal,
slash_ratio_dsign: Decimal,
slash_ratio_offline: Decimal,
) -> Result<Response, ContractError> {
if max_slashing_dsign > Decimal::one() || max_slashing_offline > Decimal::one() {
return Err(ContractError::InvalidMaxSlashing);
if slash_ratio_dsign > Decimal::one() || slash_ratio_offline > Decimal::one() {
return Err(ContractError::InvalidSlashRatio);
}

let config = Config {
denom,
proxy_code_id,
vault: ctx.info.sender,
max_slashing_dsign,
max_slashing_offline,
slash_ratio_dsign,
slash_ratio_offline,
};
self.config.save(ctx.deps.storage, &config)?;
set_contract_version(ctx.deps.storage, CONTRACT_NAME, CONTRACT_VERSION)?;
Expand Down
4 changes: 2 additions & 2 deletions contracts/provider/native-staking/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ pub enum ContractError {
#[error("Missing proxy contract for {0}")]
NoProxy(String),

#[error("You cannot use a max slashing rate over 1.0 (100%)")]
InvalidMaxSlashing,
#[error("You cannot specify a slash ratio over 1.0 (100%)")]
InvalidSlashRatio,
}
14 changes: 7 additions & 7 deletions contracts/provider/native-staking/src/local_staking_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use sylvia::types::QueryCtx;
use sylvia::{contract, types::ExecCtx};

#[allow(unused_imports)]
use mesh_apis::local_staking_api::{self, LocalStakingApi, MaxSlashResponse};
use mesh_apis::local_staking_api::{self, LocalStakingApi, SlashRatioResponse};

use crate::contract::{NativeStakingContract, REPLY_ID_INSTANTIATE};
use crate::error::ContractError;
Expand Down Expand Up @@ -121,15 +121,15 @@ impl LocalStakingApi for NativeStakingContract<'_> {

/// Returns the maximum percentage that can be slashed
#[msg(query)]
fn max_slash(&self, ctx: QueryCtx) -> Result<MaxSlashResponse, Self::Error> {
fn max_slash(&self, ctx: QueryCtx) -> Result<SlashRatioResponse, Self::Error> {
let Config {
max_slashing_dsign,
max_slashing_offline,
slash_ratio_dsign,
slash_ratio_offline,
..
} = self.config.load(ctx.deps.storage)?;
Ok(MaxSlashResponse {
max_slash_dsign: max_slashing_dsign,
max_slash_offline: max_slashing_offline,
Ok(SlashRatioResponse {
slash_ratio_dsign,
slash_ratio_offline,
})
}
}
6 changes: 3 additions & 3 deletions contracts/provider/native-staking/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ fn instantiation() {
assert_eq!(config.denom, OSMO);

let res = staking.local_staking_api_proxy().max_slash().unwrap();
assert_eq!(res.max_slash_dsign, slashing_rate_dsign());
assert_eq!(res.slash_ratio_dsign, slashing_rate_dsign());
}

#[test]
Expand Down Expand Up @@ -267,8 +267,8 @@ fn releasing_proxy_stake() {
msg: to_binary(&crate::contract::InstantiateMsg {
denom: OSMO.to_owned(),
proxy_code_id: staking_proxy_code.code_id(),
max_slashing_dsign: slashing_rate_dsign(),
max_slashing_offline: slashing_rate_offline(),
slash_ratio_dsign: slashing_rate_dsign(),
slash_ratio_offline: slashing_rate_offline(),
})
.unwrap(),
label: None,
Expand Down
8 changes: 4 additions & 4 deletions contracts/provider/native-staking/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ pub struct Config {
/// The address of the vault contract (where we get and return stake)
pub vault: Addr,

/// Max slash percentage for double signing
pub max_slashing_dsign: Decimal,
/// The slash ratio for double signing
pub slash_ratio_dsign: Decimal,

/// max slash percentage for being offline
pub max_slashing_offline: Decimal,
/// The slash ratio for being offline
pub slash_ratio_offline: Decimal,
}
10 changes: 5 additions & 5 deletions contracts/provider/vault/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::cmp::min;

use mesh_apis::cross_staking_api::CrossStakingApiHelper;
use mesh_apis::local_staking_api::{
LocalStakingApiHelper, LocalStakingApiQueryMsg, MaxSlashResponse,
LocalStakingApiHelper, LocalStakingApiQueryMsg, SlashRatioResponse,
};
use mesh_apis::vault_api::{self, SlashInfo, VaultApi};
use mesh_sync::Tx::InFlightStaking;
Expand Down Expand Up @@ -193,7 +193,7 @@ impl VaultContract<'_> {
&mut ctx,
&config,
&contract.0,
slashable.max_slash_dsign,
slashable.slash_ratio_dsign,
amount.clone(),
true,
)?;
Expand Down Expand Up @@ -461,13 +461,13 @@ impl VaultContract<'_> {
// As we control the local staking contract it might be better to just raw-query it
// on demand instead of duplicating the data.
let query = LocalStakingApiQueryMsg::MaxSlash {};
let MaxSlashResponse {
max_slash_dsign, ..
let SlashRatioResponse {
slash_ratio_dsign, ..
} = deps.querier.query_wasm_smart(&local_staking, &query)?;

let local_staking = LocalStaking {
contract: LocalStakingApiHelper(local_staking),
max_slash: max_slash_dsign,
max_slash: slash_ratio_dsign,
};

self.local_staking
Expand Down
8 changes: 4 additions & 4 deletions contracts/provider/vault/src/multitest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use cw_multi_test::{App as MtApp, StakingInfo};
use mesh_apis::ibc::AddValidator;
use mesh_external_staking::contract::multitest_utils::ExternalStakingContractProxy;
use mesh_external_staking::msg::{AuthorizedEndpoint, ReceiveVirtualStake, StakeInfo};
use mesh_external_staking::state::MaxSlashing;
use mesh_external_staking::state::SlashRatio;
use mesh_external_staking::state::Stake;
use mesh_external_staking::test_methods_impl::test_utils::TestMethods;
use mesh_native_staking::contract::multitest_utils::NativeStakingContractProxy;
Expand Down Expand Up @@ -124,8 +124,8 @@ fn setup_inner<'app>(

let native_staking_inst_msg = mesh_native_staking::contract::InstantiateMsg {
denom: OSMO.to_string(),
max_slashing_dsign: Decimal::percent(10),
max_slashing_offline: Decimal::percent(10),
slash_ratio_dsign: Decimal::percent(10),
slash_ratio_offline: Decimal::percent(10),
proxy_code_id: native_staking_proxy_code.code_id(),
};

Expand Down Expand Up @@ -172,7 +172,7 @@ fn setup_cross_stake<'app>(
vault.contract_addr.to_string(),
unbond_period,
remote_contact,
MaxSlashing {
SlashRatio {
double_sign: Decimal::percent(slash_percent),
offline: Decimal::percent(slash_percent),
},
Expand Down
6 changes: 3 additions & 3 deletions packages/apis/src/cross_staking_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use cosmwasm_std::{to_binary, Addr, Binary, Coin, Deps, Response, StdError, Wasm
use sylvia::types::{ExecCtx, QueryCtx};
use sylvia::{interface, schemars};

pub use crate::local_staking_api::MaxSlashResponse;
pub use crate::local_staking_api::SlashRatioResponse;

/// This is the interface to any cross staking contract needed by the vault contract.
/// That is, using the vault collateral to stake on a system that doesn't use the collateral
Expand Down Expand Up @@ -45,7 +45,7 @@ pub trait CrossStakingApi {

/// Returns the maximum percentage that can be slashed
#[msg(query)]
fn max_slash(&self, ctx: QueryCtx) -> Result<MaxSlashResponse, Self::Error>;
fn max_slash(&self, ctx: QueryCtx) -> Result<SlashRatioResponse, Self::Error>;
}

#[cw_serde]
Expand Down Expand Up @@ -97,7 +97,7 @@ impl CrossStakingApiHelper {
Ok(wasm)
}

pub fn max_slash(&self, deps: Deps) -> Result<MaxSlashResponse, StdError> {
pub fn max_slash(&self, deps: Deps) -> Result<SlashRatioResponse, StdError> {
let query = CrossStakingApiQueryMsg::MaxSlash {};
deps.querier.query_wasm_smart(&self.0, &query)
}
Expand Down
10 changes: 5 additions & 5 deletions packages/apis/src/local_staking_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use sylvia::types::{ExecCtx, QueryCtx};
use sylvia::{interface, schemars};

#[cw_serde]
pub struct MaxSlashResponse {
pub max_slash_dsign: Decimal,
pub max_slash_offline: Decimal,
pub struct SlashRatioResponse {
pub slash_ratio_dsign: Decimal,
pub slash_ratio_offline: Decimal,
}

/// This is the interface to any local staking contract needed by the vault contract.
Expand Down Expand Up @@ -46,7 +46,7 @@ pub trait LocalStakingApi {

/// Returns the maximum percentage that can be slashed
#[msg(query)]
fn max_slash(&self, ctx: QueryCtx) -> Result<MaxSlashResponse, Self::Error>;
fn max_slash(&self, ctx: QueryCtx) -> Result<SlashRatioResponse, Self::Error>;
}

#[cw_serde]
Expand Down Expand Up @@ -94,7 +94,7 @@ impl LocalStakingApiHelper {
Ok(wasm)
}

pub fn max_slash(&self, deps: Deps) -> Result<MaxSlashResponse, StdError> {
pub fn max_slash(&self, deps: Deps) -> Result<SlashRatioResponse, StdError> {
let query = LocalStakingApiQueryMsg::MaxSlash {};
deps.querier.query_wasm_smart(&self.0, &query)
}
Expand Down

0 comments on commit 745ee54

Please sign in to comment.