Skip to content

Commit

Permalink
Add testcases for inactive DReps in RatifySpec
Browse files Browse the repository at this point in the history
  • Loading branch information
teodanciu committed Aug 17, 2023
1 parent ff92305 commit 69c4003
Showing 1 changed file with 43 additions and 9 deletions.
52 changes: 43 additions & 9 deletions eras/conway/impl/test/Test/Cardano/Ledger/Conway/RatifySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ drepsProp =
forAll (drepsTestData @era ratios) $
\(DRepTestData {..}) -> do
let drepState =
-- non-expired (active) dReps
Map.fromList
[(cred, DRepState (EpochNo 100) SNothing mempty) | DRepCredential cred <- Map.keys distr]
ratifyEnv = emptyRatifyEnv {reDRepDistr = distr, reDRepState = drepState}
Expand All @@ -76,35 +77,67 @@ drepsProp =
| otherwise = unCoin (stakeYes <+> stakeNoConfidence) % unCoin (totalStake <-> stakeAbstain <-> stakeAlwaysAbstain)
actualNoConfidence `shouldBe` expectedNoConfidence

let allExpiredDreps =
Map.fromList
[(cred, DRepState (EpochNo 9) SNothing mempty) | DRepCredential cred <- Map.keys distr]
actualAllExpired =
dRepAcceptedRatio @era
(emptyRatifyEnv {reDRepDistr = distr, reDRepState = allExpiredDreps, reCurrentEpoch = EpochNo 10})
votes
InfoAction
actualAllExpired `shouldBe` 0

-- Expire half of the DReps and check that the ratio is the same as if only the active DReps exist
let (activeDreps, expiredDreps) = splitAt (length distr `div` 2) (Map.keys distr)
activeDrepsState =
Map.fromList
[(cred, DRepState (EpochNo 10) SNothing mempty) | DRepCredential cred <- activeDreps]
expiredDrepsState =
Map.fromList
[(cred, DRepState (EpochNo 3) SNothing mempty) | DRepCredential cred <- expiredDreps]
someExpiredDrepsState = activeDrepsState `Map.union` expiredDrepsState

actualSomeExpired =
dRepAcceptedRatio @era
(emptyRatifyEnv {reDRepDistr = distr, reDRepState = someExpiredDrepsState, reCurrentEpoch = EpochNo 5})
(votes `Map.union` Map.fromList [(cred, VoteYes) | DRepCredential cred <- expiredDreps])
InfoAction

actualSomeExpired
`shouldBe` dRepAcceptedRatio @era
(emptyRatifyEnv {reDRepDistr = distr, reDRepState = activeDrepsState, reCurrentEpoch = EpochNo 5})
votes
InfoAction
DRepState
drepsPropAllAbstain :: forall era. Era era => Spec
drepsPropAllAbstain =
prop "If all votes are abstain, accepted ratio is zero" $
forAll (drepsTestData @era (Ratios {yes = 0, no = 0, abstain = 50 % 100, alwaysAbstain = 50 % 100, noConfidence = 0})) $
\drepTestData ->
acceptedRatio drepTestData `shouldBe` 0
activeDRepAcceptedRatio drepTestData `shouldBe` 0

drepsPropAllNoConfidence :: forall era. Era era => Spec
drepsPropAllNoConfidence =
prop "If all votes are no confidence, accepted ratio is zero" $
forAll (drepsTestData @era (Ratios {yes = 0, no = 0, abstain = 0, alwaysAbstain = 0, noConfidence = 100 % 100})) $
\drepTestData ->
acceptedRatio drepTestData `shouldBe` 0
activeDRepAcceptedRatio drepTestData `shouldBe` 0

drepsPropNoVotes :: forall era. Era era => Spec
drepsPropNoVotes =
prop "If there are no votes, accepted ratio is zero" $
forAll (drepsTestData @era (Ratios {yes = 0, no = 0, abstain = 0, alwaysAbstain = 0, noConfidence = 0})) $
\drepTestData ->
acceptedRatio drepTestData `shouldBe` 0
activeDRepAcceptedRatio drepTestData `shouldBe` 0

drepsPropAllYes :: forall era. Era era => Spec
drepsPropAllYes =
prop "If all vote yes, accepted ratio is 1 (unless there is no stake) " $
forAll (drepsTestData @era (Ratios {yes = 100 % 100, no = 0, abstain = 0, alwaysAbstain = 0, noConfidence = 0})) $
\drepTestData ->
if totalStake drepTestData == Coin 0
then acceptedRatio drepTestData `shouldBe` 0
else acceptedRatio drepTestData `shouldBe` 1
then activeDRepAcceptedRatio drepTestData `shouldBe` 0
else activeDRepAcceptedRatio drepTestData `shouldBe` 1

drepsPropNoStake ::
forall era.
Expand All @@ -124,12 +157,13 @@ drepsPropNoStake =
`shouldBe` False
)

acceptedRatio :: forall era. DRepTestData era -> Rational
acceptedRatio (DRepTestData {..}) =
let drepState =
activeDRepAcceptedRatio :: forall era. DRepTestData era -> Rational
activeDRepAcceptedRatio (DRepTestData {..}) =
let activeDrepState =
-- non-expired dReps
Map.fromList
[(cred, DRepState (EpochNo 100) SNothing mempty) | DRepCredential cred <- Map.keys distr]
ratifyEnv = emptyRatifyEnv {reDRepDistr = distr, reDRepState = drepState}
ratifyEnv = emptyRatifyEnv {reDRepDistr = distr, reDRepState = activeDrepState}
in dRepAcceptedRatio @era ratifyEnv votes InfoAction

data DRepTestData era = DRepTestData
Expand Down

0 comments on commit 69c4003

Please sign in to comment.