Skip to content

Commit

Permalink
Handle explicitly the case of no stake or uncomputable ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
teodanciu committed Aug 11, 2023
1 parent 9a709e9 commit 807ac93
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
13 changes: 8 additions & 5 deletions eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Ratify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ data RatifyEnv era = RatifyEnv
, reDRepDistr :: !(Map (DRep (EraCrypto era)) (CompactForm Coin))
, reCurrentEpoch :: !EpochNo
}
deriving (Show)

newtype RatifySignal era
= RatifySignal
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 22 additions & 1 deletion eras/conway/impl/test/Test/Cardano/Ledger/Conway/RatifySpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -31,6 +33,7 @@ spec :: Spec
spec = do
describe "Ratification" $ do
drepsProp @Conway
drepsPropNoStake @Conway

drepsProp :: forall era. Era era => Spec
drepsProp =
Expand Down Expand Up @@ -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
)
13 changes: 12 additions & 1 deletion eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Arbitrary.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 807ac93

Please sign in to comment.