Skip to content

Commit

Permalink
Add slashed field to btc delegation state
Browse files Browse the repository at this point in the history
Use to indicate slashed delegations
Adapt tests
  • Loading branch information
Mauro Lacy committed Aug 12, 2024
1 parent f28c13e commit 91528e3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 13 deletions.
21 changes: 10 additions & 11 deletions contracts/btc-staking/src/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,6 @@ fn handle_slashed_delegation(

// TODO: Ensure the BTC delegation is active

// Mark the delegation as undelegated due to slashing
let delegator_slashing_sig = btc_del.delegator_slashing_sig.clone();
btc_undelegate(
storage,
&staking_tx_hash,
&mut btc_del,
delegator_slashing_sig.as_slice(),
)?;

// Discount the voting power from the affected finality providers
let affected_fps = DELEGATION_FPS.load(storage, staking_tx_hash.as_ref())?;
let fps = fps();
Expand All @@ -349,6 +340,11 @@ fn handle_slashed_delegation(
Ok::<_, ContractError>(fp_state)
})?;
}

// Mark the delegation as slashed
btc_del.slashed = true;
DELEGATIONS.save(storage, staking_tx_hash.as_ref(), &btc_del)?;

// Record event that the BTC delegation becomes unbonded due to slashing at this height
let slashing_event = Event::new("btc_undelegation_slashed")
.add_attribute("staking_tx_hash", staking_tx_hash.to_string())
Expand Down Expand Up @@ -803,16 +799,19 @@ pub(crate) mod tests {
);
assert_eq!(res.events[0].attributes[1].key.as_str(), "height");

// Check the delegation is not active any more (updated with the unbonding tx signature)
// Check the delegation is not active any more (slashed)
let active_delegation_undelegation = active_delegation.undelegation_info;
let btc_del = queries::delegation(deps.as_ref(), staking_tx_hash_hex).unwrap();
assert!(btc_del.slashed);

// Check the undelegation info in passing
let btc_undelegation = btc_del.undelegation_info;
assert_eq!(
btc_undelegation,
BtcUndelegationInfo {
unbonding_tx: active_delegation_undelegation.unbonding_tx.into(),
slashing_tx: active_delegation_undelegation.slashing_tx.into(),
delegator_unbonding_sig: active_delegation.delegator_slashing_sig.into(), // The slashing sig is now the unbonding sig
delegator_unbonding_sig: vec![],
delegator_slashing_sig: active_delegation_undelegation
.delegator_slashing_sig
.into(),
Expand Down
6 changes: 4 additions & 2 deletions contracts/btc-staking/src/state/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ pub struct BtcDelegation {
pub undelegation_info: BtcUndelegationInfo,
/// params version used to validate the delegation
pub params_version: u32,
/// slashed is used to indicate whether a given delegation is related to a slashed FP
pub slashed: bool,
}

impl BtcDelegation {
Expand All @@ -59,8 +61,7 @@ impl BtcDelegation {
}

fn is_slashed(&self) -> bool {
!self.undelegation_info.delegator_unbonding_sig.is_empty()
&& self.undelegation_info.delegator_unbonding_sig == self.delegator_slashing_sig
self.slashed
}

pub fn get_status(&self, btc_height: u64, w: u64) -> BTCDelegationStatus {
Expand Down Expand Up @@ -101,6 +102,7 @@ impl From<btc_staking_api::ActiveBtcDelegation> for BtcDelegation {
unbonding_time: active_delegation.unbonding_time,
undelegation_info: active_delegation.undelegation_info.into(),
params_version: active_delegation.params_version,
slashed: false,
}
}
}
Expand Down

0 comments on commit 91528e3

Please sign in to comment.