Skip to content

Commit

Permalink
Removed enact state from ConwayGovState
Browse files Browse the repository at this point in the history
  • Loading branch information
Soupstraw committed Jan 26, 2024
1 parent 654f3e6 commit 88bcebc
Show file tree
Hide file tree
Showing 15 changed files with 185 additions and 152 deletions.
1 change: 0 additions & 1 deletion eras/conway/impl/cardano-ledger-conway.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@ library testlib
cardano-data:{cardano-data, testlib},
containers,
microlens,
microlens-mtl,
cardano-crypto-class,
cardano-ledger-allegra:{cardano-ledger-allegra, testlib},
cardano-ledger-alonzo:testlib,
Expand Down
129 changes: 90 additions & 39 deletions eras/conway/impl/src/Cardano/Ledger/Conway/Governance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,12 @@ module Cardano.Ledger.Conway.Governance (
proposalsLookupId,
proposalsActionsMap,
proposalsAreConsistent,
cgProposalsL,
cgEnactStateL,
cgDRepPulsingStateL,
cgsProposalsL,
cgsDRepPulsingStateL,
cgsCurPParamsL,
cgsPrevPParamsL,
cgsCommitteeL,
cgsConstitutionL,
ensCommitteeL,
ensConstitutionL,
ensCurPParamsL,
Expand Down Expand Up @@ -145,6 +148,8 @@ module Cardano.Ledger.Conway.Governance (
psDRepDistrL,
psDRepStateL,
RunConwayRatify (..),
govStatePrevGovActionIds,
mkEnactState,

-- * Exported for testing
pparamsUpdateThreshold,
Expand All @@ -157,6 +162,7 @@ import Cardano.Ledger.BaseTypes (
StrictMaybe (..),
UnitInterval,
isSJust,
strictMaybeToMaybe,
)
import Cardano.Ledger.Binary (
DecCBOR (..),
Expand Down Expand Up @@ -235,6 +241,7 @@ import Cardano.Ledger.Shelley.LedgerState (
)
import Cardano.Ledger.UMap
import qualified Cardano.Ledger.UMap as UMap
import Cardano.Ledger.Val (Val (..))
import Control.DeepSeq (NFData (..), deepseq)
import Control.Monad.Trans.Reader (Reader, ReaderT, ask, runReader)
import Control.State.Transition.Extended
Expand All @@ -256,6 +263,7 @@ import qualified Data.Set as Set
import Data.Typeable
import GHC.Generics (Generic)
import Lens.Micro
import Lens.Micro.Extras (view)
import NoThunks.Class (NoThunks (..), allNoThunks)

-- | A snapshot of information from the previous epoch stored inside the Pulser.
Expand Down Expand Up @@ -501,39 +509,68 @@ toRatifyStatePairs cg@(RatifyState _ _ _ _) =

-- =============================================
data ConwayGovState era = ConwayGovState
{ cgProposals :: !(Proposals era)
, cgEnactState :: !(EnactState era)
, cgDRepPulsingState :: !(DRepPulsingState era)
-- ^ The 'cgDRepPulsingState' field is a pulser that incrementally computes the stake distribution of the DReps
{ cgsProposals :: !(Proposals era)
, cgsDRepPulsingState :: !(DRepPulsingState era)
-- ^ The 'cgsDRepPulsingState' field is a pulser that incrementally computes the stake distribution of the DReps
-- over the Epoch following the close of voting at end of the previous Epoch. It assembles this with some of
-- its other internal components into a (RatifyEnv era) when it completes, and then calls the RATIFY rule
-- and eventually returns the updated RatifyState. The pulser is created at the Epoch boundary, but does
-- no work until it is pulsed in the 'NEWEPOCH' rule, whenever the system is NOT at the epoch boundary.
, cgsCommittee :: !(StrictMaybe (Committee era))
, cgsConstitution :: !(Constitution era)
, cgsCurPParams :: !(PParams era)
, cgsPrevPParams :: !(PParams era)
}
deriving (Generic, Show)

deriving instance EraPParams era => Eq (ConwayGovState era)

cgProposalsL :: Lens' (ConwayGovState era) (Proposals era)
cgProposalsL = lens cgProposals (\x y -> x {cgProposals = y})
cgsProposalsL :: Lens' (ConwayGovState era) (Proposals era)
cgsProposalsL = lens cgsProposals (\x y -> x {cgsProposals = y})

cgEnactStateL :: Lens' (ConwayGovState era) (EnactState era)
cgEnactStateL = lens cgEnactState (\x y -> x {cgEnactState = y})
cgsDRepPulsingStateL :: Lens' (ConwayGovState era) (DRepPulsingState era)
cgsDRepPulsingStateL = lens cgsDRepPulsingState (\x y -> x {cgsDRepPulsingState = y})

cgDRepPulsingStateL :: Lens' (ConwayGovState era) (DRepPulsingState era)
cgDRepPulsingStateL = lens cgDRepPulsingState (\x y -> x {cgDRepPulsingState = y})
cgsCommitteeL :: Lens' (ConwayGovState era) (StrictMaybe (Committee era))
cgsCommitteeL = lens cgsCommittee (\x y -> x {cgsCommittee = y})

cgsConstitutionL :: Lens' (ConwayGovState era) (Constitution era)
cgsConstitutionL = lens cgsConstitution (\x y -> x {cgsConstitution = y})

cgsCurPParamsL :: Lens' (ConwayGovState era) (PParams era)
cgsCurPParamsL = lens cgsCurPParams (\x y -> x {cgsCurPParams = y})

cgsPrevPParamsL :: Lens' (ConwayGovState era) (PParams era)
cgsPrevPParamsL = lens cgsPrevPParams (\x y -> x {cgsPrevPParams = y})

curPParamsConwayGovStateL :: Lens' (ConwayGovState era) (PParams era)
curPParamsConwayGovStateL = cgEnactStateL . ensCurPParamsL
curPParamsConwayGovStateL = cgsCurPParamsL
{-# DEPRECATED curPParamsConwayGovStateL "In favor of cgsCurPParamsL" #-}

prevPParamsConwayGovStateL :: Lens' (ConwayGovState era) (PParams era)
prevPParamsConwayGovStateL = cgEnactStateL . ensPrevPParamsL
prevPParamsConwayGovStateL = cgsPrevPParamsL
{-# DEPRECATED prevPParamsConwayGovStateL "In favor of cgsPrevPParamsL" #-}

govStatePrevGovActionIds :: ConwayEraGov era => GovState era -> PrevGovActionIds era
govStatePrevGovActionIds = view $ proposalsGovStateL . pRootsL . to toPrevGovActionIds

conwayGovStateDRepDistrG :: SimpleGetter (ConwayGovState era) (Map (DRep (EraCrypto era)) (CompactForm Coin))
conwayGovStateDRepDistrG = to (\govst -> (psDRepDistr . fst) $ finishDRepPulser (cgDRepPulsingState govst))
conwayGovStateDRepDistrG = to (\govst -> (psDRepDistr . fst) $ finishDRepPulser (cgsDRepPulsingState govst))

getRatifyState :: ConwayGovState era -> RatifyState era
getRatifyState (ConwayGovState {cgDRepPulsingState}) = snd $ finishDRepPulser cgDRepPulsingState
getRatifyState (ConwayGovState {cgsDRepPulsingState}) = snd $ finishDRepPulser cgsDRepPulsingState

mkEnactState :: ConwayEraGov era => GovState era -> EnactState era
mkEnactState gs =
EnactState
{ ensCommittee = gs ^. committeeGovStateL
, ensConstitution = gs ^. constitutionGovStateL
, ensCurPParams = gs ^. curPParamsGovStateL
, ensPrevPParams = gs ^. prevPParamsGovStateL
, ensTreasury = zero
, ensWithdrawals = mempty
, ensPrevGovActionIds = govStatePrevGovActionIds gs
}

-- TODO: Implement Sharing: https://github.com/intersectmbo/cardano-ledger/issues/3486
instance EraPParams era => DecShareCBOR (ConwayGovState era) where
Expand All @@ -543,6 +580,9 @@ instance EraPParams era => DecShareCBOR (ConwayGovState era) where
<! From
<! From
<! From
<! From
<! From
<! From

instance EraPParams era => DecCBOR (ConwayGovState era) where
decCBOR =
Expand All @@ -551,14 +591,20 @@ instance EraPParams era => DecCBOR (ConwayGovState era) where
<! From
<! From
<! From
<! From
<! From
<! From

instance EraPParams era => EncCBOR (ConwayGovState era) where
encCBOR ConwayGovState {..} =
encode $
Rec ConwayGovState
!> To cgProposals
!> To cgEnactState
!> To cgDRepPulsingState
!> To cgsProposals
!> To cgsDRepPulsingState
!> To cgsCommittee
!> To cgsConstitution
!> To cgsCurPParams
!> To cgsPrevPParams

instance EraPParams era => ToCBOR (ConwayGovState era) where
toCBOR = toEraCBOR @era
Expand All @@ -567,7 +613,7 @@ instance EraPParams era => FromCBOR (ConwayGovState era) where
fromCBOR = fromEraCBOR @era

instance EraPParams era => Default (ConwayGovState era) where
def = ConwayGovState def def (DRComplete def def)
def = ConwayGovState def (DRComplete def def) def def def def

instance EraPParams era => NFData (ConwayGovState era)

Expand All @@ -578,19 +624,24 @@ instance EraPParams era => ToJSON (ConwayGovState era) where
toEncoding = pairs . mconcat . toConwayGovPairs

toConwayGovPairs :: (KeyValue e a, EraPParams era) => ConwayGovState era -> [a]
toConwayGovPairs cg@(ConwayGovState _ _ _) =
toConwayGovPairs cg@(ConwayGovState _ _ _ _ _ _) =
let ConwayGovState {..} = cg
in [ "proposals" .= cgProposals
, "enactState" .= cgEnactState
, "nextRatifyState" .= extractDRepPulsingState cgDRepPulsingState
in [ "proposals" .= cgsProposals
, "nextRatifyState" .= extractDRepPulsingState cgsDRepPulsingState
, "commitee" .= cgsCommittee
, "constitution" .= cgsConstitution
, "currentPParams" .= cgsCurPParams
, "previousPParams" .= cgsPrevPParams
]

instance EraPParams (ConwayEra c) => EraGov (ConwayEra c) where
type GovState (ConwayEra c) = ConwayGovState (ConwayEra c)

getConstitution g = Just $ g ^. cgEnactStateL . ensConstitutionL
getConstitution = Just . cgsConstitution

getCommitteeMembers g = ensCommitteeMembers (g ^. cgEnactStateL)
getCommitteeMembers g =
fmap (\c -> (committeeMembers c, committeeQuorum c)) . strictMaybeToMaybe $
g ^. cgsCommitteeL

getNextEpochCommitteeMembers g = ensCommitteeMembers (getRatifyState g ^. rsEnactStateL)

Expand All @@ -600,7 +651,7 @@ instance EraPParams (ConwayEra c) => EraGov (ConwayEra c) where

obligationGovState st =
Obligations
{ oblProposal = foldMap' gasDeposit $ proposalsActions (st ^. cgProposalsL)
{ oblProposal = foldMap' gasDeposit $ proposalsActions (st ^. cgsProposalsL)
, oblDRep = Coin 0
, oblStake = Coin 0
, oblPool = Coin 0
Expand All @@ -619,13 +670,13 @@ class EraGov era => ConwayEraGov era where
constitutionGovStateL :: Lens' (GovState era) (Constitution era)
proposalsGovStateL :: Lens' (GovState era) (Proposals era)
drepPulsingStateGovStateL :: Lens' (GovState era) (DRepPulsingState era)
enactStateGovStateL :: Lens' (GovState era) (EnactState era)
committeeGovStateL :: Lens' (GovState era) (StrictMaybe (Committee era))

instance Crypto c => ConwayEraGov (ConwayEra c) where
constitutionGovStateL = cgEnactStateL . ensConstitutionL
proposalsGovStateL = cgProposalsL
drepPulsingStateGovStateL = cgDRepPulsingStateL
enactStateGovStateL = cgEnactStateL
constitutionGovStateL = cgsConstitutionL
proposalsGovStateL = cgsProposalsL
drepPulsingStateGovStateL = cgsDRepPulsingStateL
committeeGovStateL = cgsCommitteeL

pparamsUpdateThreshold ::
forall era.
Expand Down Expand Up @@ -1090,7 +1141,7 @@ setCompleteDRepPulsingState ::
EpochState era
setCompleteDRepPulsingState snapshot ratifyState epochState =
epochState
& epochStateGovStateL . cgDRepPulsingStateL
& epochStateGovStateL . cgsDRepPulsingStateL
.~ DRComplete snapshot ratifyState

-- | Refresh the pulser in the EpochState using all the new data that is needed to compute
Expand All @@ -1099,6 +1150,7 @@ setFreshDRepPulsingState ::
( GovState era ~ ConwayGovState era
, Monad m
, RunConwayRatify era
, ConwayEraGov era
) =>
EpochNo ->
PoolDistr (EraCrypto era) ->
Expand Down Expand Up @@ -1127,7 +1179,7 @@ setFreshDRepPulsingState epochNo stakePoolDistr epochState = do
pulseSize = max 1 (ceiling (toInteger stakeSize % (8 * toInteger k)))
epochState' =
epochState
& epochStateGovStateL . cgDRepPulsingStateL
& epochStateGovStateL . cgsDRepPulsingStateL
.~ DRPulsing
( DRepPulser
{ dpPulseSize = pulseSize
Expand All @@ -1140,10 +1192,9 @@ setFreshDRepPulsingState epochNo stakePoolDistr epochState = do
, dpCurrentEpoch = epochNo
, dpCommitteeState = vsCommitteeState vState
, dpEnactState =
(cgEnactState govState)
{ ensTreasury = epochState ^. epochStateTreasuryL
}
, dpProposals = proposalsActions (govState ^. cgProposalsL)
mkEnactState govState
& ensTreasuryL .~ epochState ^. epochStateTreasuryL
, dpProposals = proposalsActions (govState ^. cgsProposalsL)
, dpGlobals = globals
}
)
Expand Down
46 changes: 15 additions & 31 deletions eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Epoch.hs
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,28 @@ import Cardano.Ledger.Conway.Core
import Cardano.Ledger.Conway.Era (ConwayEPOCH, ConwayRATIFY)
import Cardano.Ledger.Conway.Governance (
Committee,
ConwayEraGov (enactStateGovStateL),
ConwayEraGov (..),
ConwayGovState (..),
DRepPulsingState (..),
EnactState,
EnactState (..),
GovActionState (..),
RatifyEnv (..),
RatifySignal (..),
RatifyState (..),
RunConwayRatify,
cgEnactStateL,
cgProposalsL,
cgsCommitteeL,
cgsConstitutionL,
cgsCurPParamsL,
cgsPrevPParamsL,
cgsProposalsL,
dormantEpoch,
ensCommitteeL,
ensPrevGovActionIdsL,
ensPrevPParamsL,
ensTreasuryL,
ensWithdrawalsL,
epochStateDRepPulsingStateL,
extractDRepPulsingState,
pRootsL,
proposalsApplyEnactment,
proposalsGovStateL,
setFreshDRepPulsingState,
toPrevGovActionIds,
)
import Cardano.Ledger.Conway.Governance.Procedures (Committee (..))
import Cardano.Ledger.EpochBoundary (SnapShots)
Expand All @@ -73,7 +71,6 @@ import Cardano.Ledger.Shelley.LedgerState (
PState (..),
UTxOState (..),
asTreasuryL,
epochStateGovStateL,
esAccountState,
esAccountStateL,
esLStateL,
Expand Down Expand Up @@ -103,7 +100,6 @@ import Cardano.Ledger.UMap (UView (..), unionRewAgg, (∪+), (◁))
import Cardano.Ledger.Val (zero, (<->))
import Control.SetAlgebra (eval, (⨃))
import Control.State.Transition (
Assertion (..),
Embed (..),
STS (..),
TRC (..),
Expand All @@ -117,7 +113,7 @@ import Data.Foldable (Foldable (..))
import qualified Data.Map.Strict as Map
import Data.Maybe.Strict (StrictMaybe (..))
import Data.Void (Void, absurd)
import Lens.Micro (to, (%~), (&), (+~), (.~), (<>~), (^.))
import Lens.Micro ((%~), (&), (+~), (.~), (<>~), (^.))

data ConwayEpochEvent era
= PoolReapEvent (Event (EraRule "POOLREAP" era))
Expand Down Expand Up @@ -155,21 +151,6 @@ instance
type PredicateFailure (ConwayEPOCH era) = Void
type Event (ConwayEPOCH era) = ConwayEpochEvent era
transitionRules = [epochTransition]
assertions =
let withdrawalsEmptyMessage = "Withdrawals in EnactState must be empty"
withdrawalsEmptyCheck es = null $ es ^. epochStateGovStateL . cgEnactStateL . ensWithdrawalsL
treasuryZeroMessage = "Treasury in EnactState must be zero"
treasuryZeroCheck es = zero == es ^. epochStateGovStateL . cgEnactStateL . ensTreasuryL
enactConsistentMessage = "ENACT Rule should match proposalsApplyEnactment"
enactConsistentCheck es =
es ^. esLStateL . lsUTxOStateL . utxosGovStateL . enactStateGovStateL . ensPrevGovActionIdsL
== es ^. esLStateL . lsUTxOStateL . utxosGovStateL . proposalsGovStateL . pRootsL . to toPrevGovActionIds
in [ PreCondition withdrawalsEmptyMessage (\(TRC (_, es, _)) -> withdrawalsEmptyCheck es)
, PostCondition withdrawalsEmptyMessage (const withdrawalsEmptyCheck)
, PreCondition treasuryZeroMessage (\(TRC (_, es, _)) -> treasuryZeroCheck es)
, PostCondition treasuryZeroMessage (const treasuryZeroCheck)
, PostCondition enactConsistentMessage (const enactConsistentCheck)
]

returnProposalDeposits ::
Foldable f => f (GovActionState era) -> DState era -> DState era
Expand Down Expand Up @@ -290,7 +271,7 @@ epochTransition = do
ratState0@RatifyState {rsEnactState, rsEnacted, rsExpired} =
extractDRepPulsingState pulsingState

(accountState2, dState2, newEnactState) =
(accountState2, dState2, EnactState {..}) =
applyEnactedWithdrawals accountState1 dState1 rsEnactState

-- NOTE: It is important that we apply the results of ratifcation
Expand All @@ -310,8 +291,11 @@ epochTransition = do

govState1 =
govState0
& cgProposalsL .~ newProposals
& cgEnactStateL .~ (newEnactState & ensPrevPParamsL .~ curPParams)
& cgsProposalsL .~ newProposals
& cgsPrevPParamsL .~ curPParams
& cgsCommitteeL .~ ensCommittee
& cgsConstitutionL .~ ensConstitution
& cgsCurPParamsL .~ ensCurPParams

allRemovedGovActions = expiredActions `Map.union` enactedActions

Expand All @@ -323,7 +307,7 @@ epochTransition = do
-- Increment the dormant epoch counter
updateNumDormantEpochs pulsingState vState
-- Remove cold credentials of committee members that were removed or were invalid
& vsCommitteeStateL %~ updateCommitteeState (govState1 ^. cgEnactStateL . ensCommitteeL)
& vsCommitteeStateL %~ updateCommitteeState (govState1 ^. cgsCommitteeL)
}
accountState3 =
accountState2
Expand Down
Loading

0 comments on commit 88bcebc

Please sign in to comment.