-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement DRep ratification with an 'always passing' threshold
- Loading branch information
Showing
4 changed files
with
158 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
eras/conway/impl/test/Test/Cardano/Ledger/Conway/RatifySpec.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
{-# LANGUAGE AllowAmbiguousTypes #-} | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
module Test.Cardano.Ledger.Conway.RatifySpec (spec) where | ||
|
||
import Cardano.Ledger.BaseTypes (StrictMaybe (..)) | ||
import Cardano.Ledger.Coin (Coin (..), CompactForm (..)) | ||
import Cardano.Ledger.Conway | ||
import Cardano.Ledger.Conway.Governance ( | ||
GovAction (..), | ||
Vote (..), | ||
) | ||
import Cardano.Ledger.Conway.Rules | ||
import Cardano.Ledger.Core | ||
import Data.Foldable (fold) | ||
import Data.Map.Strict (Map) | ||
import qualified Data.Map.Strict as Map | ||
import Data.Maybe (fromMaybe) | ||
import Data.Ratio ((%)) | ||
import qualified Data.Set as Set | ||
import Test.Cardano.Ledger.Common | ||
import Test.Cardano.Ledger.Conway.Arbitrary () | ||
import Test.Cardano.Ledger.Core.Arbitrary () | ||
|
||
spec :: Spec | ||
spec = do | ||
describe "Ratification" $ do | ||
drepsProp @Conway | ||
|
||
drepsProp :: forall era. Era era => Spec | ||
drepsProp = | ||
prop "DRep vote counts" $ | ||
forAll (arbitrary @(Map (DRep (EraCrypto era)) (CompactForm Coin))) $ | ||
\dRepDistr -> do | ||
forAll (shuffle (Map.keys dRepDistr)) $ \dreps -> do | ||
let size = fromIntegral $ length dreps | ||
yes = ratio (30 :: Integer) size | ||
drepsYes = onlyDrepsCred $ take yes dreps | ||
votesYes = Map.fromList $ [(cred, VoteYes) | DRepCredential cred <- drepsYes] | ||
CompactCoin stakeYes = fold $ Map.restrictKeys dRepDistr (Set.fromList drepsYes) | ||
|
||
no = ratio 40 size | ||
drepsNo = onlyDrepsCred $ take no . drop yes $ dreps | ||
votesNo = Map.fromList $ [(cred, VoteNo) | DRepCredential cred <- drepsNo] | ||
CompactCoin stakeNo = fold $ Map.restrictKeys dRepDistr (Set.fromList drepsNo) | ||
|
||
abstain = ratio 10 size | ||
drepsAbstain = onlyDrepsCred $ take abstain . drop (yes + no) $ dreps | ||
votesAbstain = Map.fromList [(cred, Abstain) | DRepCredential cred <- drepsAbstain] | ||
CompactCoin stakeAbstain = fold $ Map.restrictKeys dRepDistr (Set.fromList drepsAbstain) | ||
|
||
CompactCoin stakeAlwaysAbstain = fromMaybe (CompactCoin 0) $ Map.lookup DRepAlwaysAbstain dRepDistr | ||
CompactCoin stakeAlwaysNoConfidence = fromMaybe (CompactCoin 0) $ Map.lookup DRepAlwaysNoConfidence dRepDistr | ||
CompactCoin notVotedStake = | ||
fold $ | ||
Map.withoutKeys | ||
dRepDistr | ||
(Set.fromList (drepsYes ++ drepsNo ++ drepsAbstain ++ [DRepAlwaysAbstain, DRepAlwaysNoConfidence])) | ||
|
||
votes = Map.union votesYes $ Map.union votesNo votesAbstain | ||
|
||
CompactCoin totalStake = fold dRepDistr | ||
|
||
actual = dRepAcceptedRatio @era dRepDistr votes InfoAction | ||
expected | ||
| totalStake == stakeAbstain + stakeAlwaysAbstain = Nothing | ||
| otherwise = Just $ stakeYes % (totalStake - stakeAbstain - stakeAlwaysAbstain) | ||
|
||
actual `shouldBe` expected | ||
|
||
let expectedRephrased | ||
| stakeYes + stakeNo + notVotedStake + stakeAlwaysNoConfidence == 0 = Nothing | ||
| otherwise = Just $ stakeYes % (stakeYes + stakeNo + notVotedStake + stakeAlwaysNoConfidence) | ||
actual `shouldBe` expectedRephrased | ||
|
||
let actualNoConfidence = dRepAcceptedRatio @era dRepDistr votes (NoConfidence SNothing) | ||
expectedNoConfidence | ||
| totalStake == stakeAbstain + stakeAlwaysAbstain = Nothing | ||
| otherwise = Just $ (stakeYes + stakeAlwaysNoConfidence) % (totalStake - stakeAbstain - stakeAlwaysAbstain) | ||
actualNoConfidence `shouldBe` expectedNoConfidence | ||
where | ||
ratio pct tot = ceiling $ (pct * tot) % 100 | ||
onlyDrepsCred l = | ||
filter | ||
( \case | ||
DRepCredential _ -> True | ||
_ -> False | ||
) | ||
l |