Skip to content

Commit

Permalink
Support multiple slash ratios in native staking
Browse files Browse the repository at this point in the history
  • Loading branch information
maurolacy committed Nov 15, 2023
1 parent 285bc53 commit 8431c6e
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions contracts/provider/native-staking/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ pub struct NativeStakingContract<'a> {
pub delegators: Map<'a, (&'a str, &'a Addr), bool>,
}

pub(crate) enum SlashingReason {
Offline,
DoubleSign,
}

#[cfg_attr(not(feature = "library"), sylvia::entry_points)]
#[contract]
#[error(ContractError)]
Expand Down Expand Up @@ -92,15 +97,15 @@ impl NativeStakingContract<'_> {
let mut msgs = vec![];
for validator in tombstoned {
// Slash the validator (if bonded)
let slash_msg = self.handle_slashing(&mut deps, &cfg, validator)?;
let slash_msg = self.handle_slashing(&mut deps, &cfg, validator, SlashingReason::DoubleSign)?;
if let Some(msg) = slash_msg {
msgs.push(msg)
}
}
for validator in jailed {
// Slash the validator (if bonded)
// TODO: Slash with a different slash ratio! (downtime / offline slash ratio)
let slash_msg = self.handle_slashing(&mut deps, &cfg, validator)?;
let slash_msg = self.handle_slashing(&mut deps, &cfg, validator, SlashingReason::Offline)?;
if let Some(msg) = slash_msg {
msgs.push(msg)
}
Expand All @@ -120,7 +125,12 @@ impl NativeStakingContract<'_> {
deps: &mut DepsMut,
config: &Config,
validator: &str,
reason: SlashingReason,
) -> Result<Option<WasmMsg>, ContractError> {
let slash_ratio = match reason {
SlashingReason::Offline => config.slash_ratio_offline,
SlashingReason::DoubleSign => config.slash_ratio_dsign,
};
// Get all mesh delegators to this validator
let owners = self
.delegators
Expand Down Expand Up @@ -150,7 +160,7 @@ impl NativeStakingContract<'_> {
continue;
}

let slash_amount = delegation * config.max_slashing;
let slash_amount = delegation * slash_ratio;

slash_infos.push(SlashInfo {
user: owner.to_string(),
Expand Down

0 comments on commit 8431c6e

Please sign in to comment.