Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove EnactState from ConwayGovState #4013

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions eras/conway/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

## 1.13.0.0

* Rename:
* `cgProposalsL` to `cgsProposalsL`
* `cgEnactStateL` to `cgsEnactStateL`
* `cgDRepPulsingStateL` to `cgsDRepPulsingStateL`
* Add:
* `cgsPrevPParamsL`
* `cgsCommitteeL`
* `cgsConstitutionL`
* `govStatePrevGovActionIds`
* `mkEnactState`
* Deprecated `curPParamsConwayGovStateL` and `curPParamsConwayGovStateL`
* Add `TreeMaybe`, `toPForest` and `toPForestEither`
* Remove `proposalsAreConsistent`

Expand Down
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: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 @@ -87,9 +87,12 @@ module Cardano.Ledger.Conway.Governance (
proposalsSize,
proposalsLookupId,
proposalsActionsMap,
cgProposalsL,
cgEnactStateL,
cgDRepPulsingStateL,
cgsProposalsL,
cgsDRepPulsingStateL,
cgsCurPParamsL,
cgsPrevPParamsL,
cgsCommitteeL,
cgsConstitutionL,
ensCommitteeL,
ensConstitutionL,
ensCurPParamsL,
Expand Down Expand Up @@ -144,6 +147,8 @@ module Cardano.Ledger.Conway.Governance (
psDRepDistrL,
psDRepStateL,
RunConwayRatify (..),
govStatePrevGovActionIds,
mkEnactState,

-- * Exported for testing
pparamsUpdateThreshold,
Expand All @@ -159,6 +164,7 @@ import Cardano.Ledger.BaseTypes (
StrictMaybe (..),
UnitInterval,
isSJust,
strictMaybeToMaybe,
)
import Cardano.Ledger.Binary (
DecCBOR (..),
Expand Down Expand Up @@ -237,6 +243,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 @@ -258,6 +265,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 @@ -503,10 +511,13 @@ 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)
, cgsCommittee :: !(StrictMaybe (Committee era))
, cgsConstitution :: !(Constitution era)
, cgsCurPParams :: !(PParams era)
, cgsPrevPParams :: !(PParams 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
Expand All @@ -516,26 +527,52 @@ data ConwayGovState era = ConwayGovState

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 @@ -545,6 +582,9 @@ instance EraPParams era => DecShareCBOR (ConwayGovState era) where
<! From
<! From
<! From
<! From
<! From
<! From

instance EraPParams era => DecCBOR (ConwayGovState era) where
decCBOR =
Expand All @@ -553,14 +593,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 cgsCommittee
!> To cgsConstitution
!> To cgsCurPParams
!> To cgsPrevPParams
!> To cgsDRepPulsingState

instance EraPParams era => ToCBOR (ConwayGovState era) where
toCBOR = toEraCBOR @era
Expand All @@ -569,7 +615,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 def def def def (DRComplete def def)

instance EraPParams era => NFData (ConwayGovState era)

Expand All @@ -580,19 +626,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 @@ -602,7 +653,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 @@ -621,13 +672,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 @@ -1092,7 +1143,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 @@ -1101,6 +1152,7 @@ setFreshDRepPulsingState ::
( GovState era ~ ConwayGovState era
, Monad m
, RunConwayRatify era
, ConwayEraGov era
) =>
EpochNo ->
PoolDistr (EraCrypto era) ->
Expand Down Expand Up @@ -1129,7 +1181,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 @@ -1142,10 +1194,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
Loading
Loading