From 807ac93982d60f6e76ad973416b797698ac8c1c8 Mon Sep 17 00:00:00 2001 From: teodanciu Date: Fri, 11 Aug 2023 15:09:13 +0100 Subject: [PATCH] Handle explicitly the case of no stake or uncomputable ratio --- .../src/Cardano/Ledger/Conway/Rules/Ratify.hs | 13 +++++++---- .../Test/Cardano/Ledger/Conway/RatifySpec.hs | 23 ++++++++++++++++++- .../Test/Cardano/Ledger/Conway/Arbitrary.hs | 13 ++++++++++- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Ratify.hs b/eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Ratify.hs index 343b5d1f005..78cef2fde9f 100644 --- a/eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Ratify.hs +++ b/eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Ratify.hs @@ -60,6 +60,7 @@ data RatifyEnv era = RatifyEnv , reDRepDistr :: !(Map (DRep (EraCrypto era)) (CompactForm Coin)) , reCurrentEpoch :: !EpochNo } + deriving (Show) newtype RatifySignal era = RatifySignal @@ -141,11 +142,13 @@ spoAccepted RatifyEnv {reStakePoolDistr = PoolDistr poolDistr} gas = HardForkInitiation {} -> 101 % 100 _ -> spoThreshold -dRepAccepted :: forall era. RatifyEnv era -> GovActionState era -> Bool -dRepAccepted RatifyEnv {reDRepDistr} GovActionState {gasDRepVotes, gasAction} = +dRepAccepted :: forall era. RatifyEnv era -> GovActionState era -> Ratio Word64 -> Bool +dRepAccepted RatifyEnv {reDRepDistr} GovActionState {gasDRepVotes, gasAction} threshold = case dRepAcceptedRatio reDRepDistr gasDRepVotes gasAction of - Nothing -> True -- TODO: change this when we are ready to consider dReps on sanchonet - Just ratio -> ratio >= dRepThreshold + Nothing + | threshold == 0 -> True + | otherwise -> False + Just ratio -> ratio >= threshold -- Compute the dRep ratio yes/(yes + no), where -- yes: is the total stake of @@ -206,7 +209,7 @@ ratifyTransition = do case rsig of act@(_, ast@GovActionState {gasAction, gasProposedIn}) :<| sigs -> do let expired = gasProposedIn + epochsToExpire < reCurrentEpoch - if spoAccepted env ast && dRepAccepted env ast + if spoAccepted env ast && dRepAccepted env ast dRepThreshold then do -- Update ENACT state with the governance action that was ratified es <- trans @(EraRule "ENACT" era) $ TRC ((), rsEnactState, gasAction) diff --git a/eras/conway/impl/test/Test/Cardano/Ledger/Conway/RatifySpec.hs b/eras/conway/impl/test/Test/Cardano/Ledger/Conway/RatifySpec.hs index 111200d481a..eb9186135da 100644 --- a/eras/conway/impl/test/Test/Cardano/Ledger/Conway/RatifySpec.hs +++ b/eras/conway/impl/test/Test/Cardano/Ledger/Conway/RatifySpec.hs @@ -13,11 +13,13 @@ import Cardano.Ledger.Coin (Coin (..), CompactForm (..)) import Cardano.Ledger.Conway import Cardano.Ledger.Conway.Governance ( GovAction (..), + GovActionState (..), Vote (..), ) -import Cardano.Ledger.Conway.Rules +import Cardano.Ledger.Conway.Rules (RatifyEnv (..), dRepAccepted, dRepAcceptedRatio) import Cardano.Ledger.Core import Data.Foldable (fold) +import Data.Functor.Identity (Identity) import Data.Map.Strict (Map) import qualified Data.Map.Strict as Map import Data.Maybe (fromMaybe) @@ -31,6 +33,7 @@ spec :: Spec spec = do describe "Ratification" $ do drepsProp @Conway + drepsPropNoStake @Conway drepsProp :: forall era. Era era => Spec drepsProp = @@ -92,3 +95,21 @@ drepsProp = _ -> False ) l + +drepsPropNoStake :: + forall era. + ( EraPParams era + , Arbitrary (PParamsHKD StrictMaybe era) + , Arbitrary (PParamsHKD Identity era) + ) => + Spec +drepsPropNoStake = + prop "If there is no stake, accept only if the threshold is zero" $ + forAll + ((,) <$> arbitrary @(RatifyEnv era) <*> arbitrary @(GovActionState era)) + ( \(env, gas) -> do + dRepAccepted @era env {reDRepDistr = Map.empty} gas 0 + `shouldBe` True + dRepAccepted @era env {reDRepDistr = Map.empty} gas (1 % 2) + `shouldBe` False + ) diff --git a/eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Arbitrary.hs b/eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Arbitrary.hs index 8be1021d38f..78403ab0b50 100644 --- a/eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Arbitrary.hs +++ b/eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Arbitrary.hs @@ -85,7 +85,7 @@ instance Crypto c => Arbitrary (AlonzoScript (ConwayEra c)) where arbitrary = genAlonzoScript [PlutusV1, PlutusV2, PlutusV3] ------------------------------------------------------------------------------------------ --- Cardano.Ledger.Conway.Gov ------------------------------------------------------ +-- Cardano.Ledger.Conway.Goverance ------------------------------------------------------ ------------------------------------------------------------------------------------------ instance @@ -107,6 +107,17 @@ instance <*> arbitrary <*> arbitrary +instance + (Era era, Arbitrary (PParams era), Arbitrary (PParamsUpdate era)) => + Arbitrary (RatifyEnv era) + where + arbitrary = + RatifyEnv + <$> arbitrary + <*> arbitrary + <*> arbitrary + <*> arbitrary + instance Crypto (EraCrypto era) => Arbitrary (Constitution era) where arbitrary = Constitution <$> arbitrary <*> arbitrary