From b64df3910b3e6c4337e4c1dfe695c7d45429f3fd Mon Sep 17 00:00:00 2001 From: Herr Seppia Date: Mon, 1 Jul 2024 12:15:56 +0200 Subject: [PATCH] rusk: change slash tests to new mechanism --- rusk/tests/services/stake.rs | 60 +++++++++++++++++++++++++++--------- 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/rusk/tests/services/stake.rs b/rusk/tests/services/stake.rs index 3304db19e2..0a161bdb6e 100644 --- a/rusk/tests/services/stake.rs +++ b/rusk/tests/services/stake.rs @@ -310,6 +310,15 @@ pub async fn slash() -> Result<()> { assert_eq!(stake.reward, dusk(3.0)); assert_eq!(stake.amount, Some((dusk(20.0), 0))); + generator_procedure( + &rusk, + &[], + BLOCK_HEIGHT, + BLOCK_GAS_LIMIT, + vec![to_slash], + None, + ) + .expect("to work"); generator_procedure( &rusk, &[], @@ -326,11 +335,19 @@ pub async fn slash() -> Result<()> { assert_eq!(prev.reward, dusk(3.0)); assert_eq!(prev.amount, Some((dusk(20.0), 0))); + let (prev_stake, _) = prev.amount.unwrap(); + let slashed_amount = prev_stake / 10; + let after_slash = wallet.get_stake(0).unwrap(); - assert_eq!(after_slash.reward, 0); - assert_eq!(after_slash.amount, Some((dusk(20.0), 4320))); + assert_eq!(after_slash.reward, dusk(5.0)); + assert_eq!(after_slash.reward, prev.reward + slashed_amount); + assert_eq!( + after_slash.amount, + Some((prev_stake - slashed_amount, 4320)) + ); + assert_eq!(after_slash.amount, Some((dusk(18.0), 4320))); let new_balance = rusk.module_balance(STAKE_CONTRACT).unwrap(); - assert_eq!(new_balance, module_balance); + assert_eq!(new_balance, module_balance - slashed_amount); let module_balance = new_balance; generator_procedure( @@ -346,14 +363,24 @@ pub async fn slash() -> Result<()> { let last_changes = rusk.last_provisioners_change(None).unwrap(); let (_, prev) = last_changes.first().expect("Something changed").clone(); let prev = prev.expect("to have something"); - assert_eq!(prev.reward, 0); - assert_eq!(prev.amount, Some((dusk(20.0), 4320))); + assert_eq!(prev.reward, dusk(5.0)); + assert_eq!(prev.amount, Some((dusk(18.0), 4320))); + + let (prev_stake, _) = prev.amount.unwrap(); + // 20% slash + let slashed_amount = prev_stake / 10 * 2; let after_slash = wallet.get_stake(0).unwrap(); - assert_eq!(after_slash.reward, 0); - assert_eq!(after_slash.amount, Some((dusk(20.0), 4320))); + assert_eq!(after_slash.reward, dusk(8.6)); + assert_eq!(after_slash.reward, prev.reward + slashed_amount); + assert_eq!( + after_slash.amount, + Some((prev_stake - slashed_amount, 6480)) + ); + assert_eq!(after_slash.amount, Some((dusk(14.4), 6480))); + let new_balance = rusk.module_balance(STAKE_CONTRACT).unwrap(); - assert_eq!(new_balance, module_balance); + assert_eq!(new_balance, module_balance - slashed_amount); let module_balance = new_balance; generator_procedure( @@ -369,14 +396,17 @@ pub async fn slash() -> Result<()> { let last_changes = rusk.last_provisioners_change(None).unwrap(); let (_, prev) = last_changes.first().expect("Something changed").clone(); let prev = prev.expect("to have something"); - assert_eq!(prev.reward, 0); - assert_eq!(prev.amount, Some((dusk(20.0), 4320))); + assert_eq!(prev.reward, dusk(8.6)); + assert_eq!(prev.amount, Some((dusk(14.4), 6480))); + let (prev_stake, _) = prev.amount.unwrap(); + // 30% slash + let slashed_amount = prev_stake / 10 * 3; let after_slash = wallet.get_stake(0).unwrap(); - assert_eq!(after_slash.reward, 0); - assert_eq!(after_slash.amount, Some((dusk(20.0), 12960))); + assert_eq!(after_slash.reward, dusk(12.92)); + assert_eq!(after_slash.amount, Some((dusk(10.08), 17280))); let new_balance = rusk.module_balance(STAKE_CONTRACT).unwrap(); - assert_eq!(new_balance, module_balance); + assert_eq!(new_balance, module_balance - slashed_amount); generator_procedure( &rusk, @@ -392,8 +422,8 @@ pub async fn slash() -> Result<()> { let last_changes = rusk.last_provisioners_change(None).unwrap(); let (_, prev) = last_changes.first().expect("Something changed").clone(); let prev = prev.expect("to have something"); - assert_eq!(prev.reward, 0); - assert_eq!(prev.amount, Some((dusk(20.0), 4320))); + assert_eq!(prev.reward, dusk(8.6)); + assert_eq!(prev.amount, Some((dusk(14.4), 6480))); generator_procedure(&rusk, &[], 9001, BLOCK_GAS_LIMIT, vec![], None) .expect("To work properly");