Skip to content

Commit

Permalink
stake-contract: use StakeConfig for minimum_stake and slash warning
Browse files Browse the repository at this point in the history
  • Loading branch information
herr-seppia committed Dec 20, 2024
1 parent 194eb65 commit b7b7728
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions contracts/stake/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use dusk_bytes::Serializable;
use dusk_core::{
signatures::bls::PublicKey as BlsPublicKey,
stake::{
next_epoch, Reward, SlashEvent, Stake, StakeAmount, StakeData,
StakeEvent, StakeFundOwner, StakeKeys, Withdraw, WithdrawToContract,
EPOCH, MINIMUM_STAKE, STAKE_CONTRACT, STAKE_WARNINGS,
next_epoch, Reward, SlashEvent, Stake, StakeAmount, StakeConfig,
StakeData, StakeEvent, StakeFundOwner, StakeKeys, Withdraw,
WithdrawToContract, EPOCH, STAKE_CONTRACT,
},
transfer::{ContractToContract, ReceiveFromContract, TRANSFER_CONTRACT},
ContractId,
Expand All @@ -34,20 +34,22 @@ use crate::*;
/// valid stake.
#[derive(Debug, Default, Clone)]
pub struct StakeState {
stakes: BTreeMap<[u8; BlsPublicKey::SIZE], (StakeData, StakeKeys)>,
burnt_amount: u64,
config: StakeConfig,
previous_block_state:
BTreeMap<[u8; BlsPublicKey::SIZE], (Option<StakeData>, BlsPublicKey)>,
stakes: BTreeMap<[u8; BlsPublicKey::SIZE], (StakeData, StakeKeys)>,
}

const STAKE_CONTRACT_VERSION: u64 = 8;

impl StakeState {
pub const fn new() -> Self {
Self {
stakes: BTreeMap::new(),
burnt_amount: 0u64,
config: StakeConfig::new(),
previous_block_state: BTreeMap::new(),
stakes: BTreeMap::new(),
}
}

Expand Down Expand Up @@ -80,6 +82,7 @@ impl StakeState {
}

pub fn stake(&mut self, stake: Stake) {
let minimum_stake = self.config.minimum_stake;
let value = stake.value();
let signature = *stake.signature();

Expand All @@ -91,7 +94,7 @@ impl StakeState {
let prev_stake = self.get_stake(&stake.keys().account).copied();
let (loaded_stake, keys) = self.load_or_create_stake_mut(stake.keys());

if loaded_stake.amount.is_none() && value < MINIMUM_STAKE {
if loaded_stake.amount.is_none() && value < minimum_stake {
panic!("The staked value is lower than the minimum amount!");
}

Expand Down Expand Up @@ -143,6 +146,7 @@ impl StakeState {
let stake: Stake =
rkyv::from_bytes(&recv.data).expect("Invalid stake received");
let value = stake.value();
let minimum_stake = self.config.minimum_stake;

if stake.chain_id() != self.chain_id() {
panic!("The stake must target the correct chain");
Expand All @@ -157,7 +161,7 @@ impl StakeState {
assert!(value == recv.value, "Stake amount mismatch");

if loaded_stake.amount.is_none() {
if value < MINIMUM_STAKE {
if value < minimum_stake {
panic!("The staked value is lower than the minimum amount!");
}

Expand Down Expand Up @@ -254,7 +258,7 @@ impl StakeState {
if loaded_stake.reward == 0 {
self.stakes.remove(&unstake.account().to_bytes());
}
} else if stake.total_funds() < MINIMUM_STAKE {
} else if stake.total_funds() < self.config.minimum_stake {
panic!("Stake left is lower than minimum stake");
}

Expand Down Expand Up @@ -513,6 +517,7 @@ impl StakeState {
/// depleted and the provisioner eligibility is shifted to the
/// next epoch as well
pub fn slash(&mut self, account: &BlsPublicKey, to_slash: Option<u64>) {
let stake_warnings = self.config.warnings;
let (stake, _) = self
.get_stake_mut(account)
.expect("The stake to slash should exist");
Expand All @@ -525,7 +530,7 @@ impl StakeState {

stake.faults = stake.faults.saturating_add(1);
let effective_faults =
stake.faults.saturating_sub(STAKE_WARNINGS) as u64;
stake.faults.saturating_sub(stake_warnings) as u64;

let stake_amount = stake.amount.as_mut().expect("stake_to_exists");

Expand Down

0 comments on commit b7b7728

Please sign in to comment.