Skip to content

Commit

Permalink
Add CERT and DELEG rules to Conway
Browse files Browse the repository at this point in the history
  • Loading branch information
aniketd committed May 31, 2023
1 parent c160399 commit 5821b19
Show file tree
Hide file tree
Showing 17 changed files with 689 additions and 155 deletions.
5 changes: 4 additions & 1 deletion eras/conway/impl/cardano-ledger-conway.cabal
Expand Up @@ -40,7 +40,10 @@ library
Cardano.Ledger.Conway.Era
Cardano.Ledger.Conway.UTxO
Cardano.Ledger.Conway.Rules.Cert
Cardano.Ledger.Conway.Rules.Delegs
Cardano.Ledger.Conway.Rules.Deleg
Cardano.Ledger.Conway.Rules.Pool
Cardano.Ledger.Conway.Rules.VDel
Cardano.Ledger.Conway.Rules.Certs
Cardano.Ledger.Conway.Rules.Enact
Cardano.Ledger.Conway.Rules.Epoch
Cardano.Ledger.Conway.Rules.Ledger
Expand Down
1 change: 1 addition & 0 deletions eras/conway/impl/src/Cardano/Ledger/Conway.hs
Expand Up @@ -54,6 +54,7 @@ instance
reapplyTx = reapplyAlonzoTx

instance
forall c.
( Crypto c
, DSignable c (Hash c EraIndependentTxBody)
, EraPlutusContext 'PlutusV2 (ConwayEra c)
Expand Down
27 changes: 18 additions & 9 deletions eras/conway/impl/src/Cardano/Ledger/Conway/Era.hs
Expand Up @@ -4,7 +4,10 @@
module Cardano.Ledger.Conway.Era (
ConwayEra,
ConwayCERT,
ConwayDELEGS,
ConwayDELEG,
ConwayPOOL,
ConwayVDEL,
ConwayCERTS,
ConwayTALLY,
ConwayNEWEPOCH,
ConwayEPOCH,
Expand Down Expand Up @@ -78,14 +81,26 @@ data ConwayRATIFY era

type instance EraRule "RATIFY" (ConwayEra c) = ConwayRATIFY (ConwayEra c)

data ConwayDELEGS era
data ConwayCERTS era

type instance EraRule "DELEGS" (ConwayEra c) = ConwayDELEGS (ConwayEra c)
type instance EraRule "CERTS" (ConwayEra c) = ConwayCERTS (ConwayEra c)

data ConwayCERT era

type instance EraRule "CERT" (ConwayEra c) = ConwayCERT (ConwayEra c)

data ConwayDELEG era

type instance EraRule "DELEG" (ConwayEra c) = ConwayDELEG (ConwayEra c)

data ConwayPOOL era

type instance EraRule "POOL" (ConwayEra c) = ConwayPOOL (ConwayEra c)

data ConwayVDEL era

type instance EraRule "VDEL" (ConwayEra c) = ConwayVDEL (ConwayEra c)

-- Rules inherited from Babbage

type instance EraRule "UTXO" (ConwayEra c) = BabbageUTXO (ConwayEra c)
Expand All @@ -98,16 +113,12 @@ type instance EraRule "BBODY" (ConwayEra c) = AlonzoBBODY (ConwayEra c)

-- Rules inherited from Shelley

type instance EraRule "DELEG" (ConwayEra c) = API.ShelleyDELEG (ConwayEra c)

type instance EraRule "LEDGERS" (ConwayEra c) = API.ShelleyLEDGERS (ConwayEra c)

type instance EraRule "MIR" (ConwayEra c) = ShelleyMIR (ConwayEra c)

type instance EraRule "NEWPP" (ConwayEra c) = ShelleyNEWPP (ConwayEra c)

type instance EraRule "POOL" (ConwayEra c) = API.ShelleyPOOL (ConwayEra c)

type instance EraRule "POOLREAP" (ConwayEra c) = API.ShelleyPOOLREAP (ConwayEra c)

type instance EraRule "RUPD" (ConwayEra c) = ShelleyRUPD (ConwayEra c)
Expand All @@ -116,6 +127,4 @@ type instance EraRule "SNAP" (ConwayEra c) = ShelleySNAP (ConwayEra c)

type instance EraRule "TICK" (ConwayEra c) = ShelleyTICK (ConwayEra c)

type instance EraRule "TICKF" (ConwayEra c) = ConwayTICKF (ConwayEra c)

-- =================================================
10 changes: 8 additions & 2 deletions eras/conway/impl/src/Cardano/Ledger/Conway/Rules.hs
@@ -1,6 +1,9 @@
module Cardano.Ledger.Conway.Rules (
module Cardano.Ledger.Conway.Rules.Cert,
module Cardano.Ledger.Conway.Rules.Delegs,
module Cardano.Ledger.Conway.Rules.Deleg,
module Cardano.Ledger.Conway.Rules.Pool,
module Cardano.Ledger.Conway.Rules.VDel,
module Cardano.Ledger.Conway.Rules.Certs,
module Cardano.Ledger.Conway.Rules.Enact,
module Cardano.Ledger.Conway.Rules.Epoch,
module Cardano.Ledger.Conway.Rules.Ledger,
Expand All @@ -13,12 +16,15 @@ module Cardano.Ledger.Conway.Rules (
where

import Cardano.Ledger.Conway.Rules.Cert
import Cardano.Ledger.Conway.Rules.Delegs
import Cardano.Ledger.Conway.Rules.Certs
import Cardano.Ledger.Conway.Rules.Deleg
import Cardano.Ledger.Conway.Rules.Enact
import Cardano.Ledger.Conway.Rules.Epoch
import Cardano.Ledger.Conway.Rules.Ledger
import Cardano.Ledger.Conway.Rules.NewEpoch
import Cardano.Ledger.Conway.Rules.Pool
import Cardano.Ledger.Conway.Rules.Ratify
import Cardano.Ledger.Conway.Rules.Tally
import Cardano.Ledger.Conway.Rules.Tickf
import Cardano.Ledger.Conway.Rules.Utxos
import Cardano.Ledger.Conway.Rules.VDel
148 changes: 137 additions & 11 deletions eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Cert.hs
@@ -1,34 +1,160 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}

module Cardano.Ledger.Conway.Rules.Cert (
ConwayCERT,
ConwayCertPredFailure (..),
ConwayCertEvent,
) where

import Cardano.Ledger.BaseTypes (ShelleyBase)
import Cardano.Ledger.Conway.Core
import Cardano.Ledger.Conway.Era (ConwayCERT)
import Cardano.Ledger.Shelley.API (CertState, DelplEnv)
import Cardano.Ledger.Shelley.Rules (ShelleyDelplEvent, ShelleyDelplPredFailure)
import Control.State.Transition.Extended (STS (..))
import Cardano.Ledger.Conway.Era (ConwayCERT, ConwayDELEG, ConwayPOOL, ConwayVDEL)
import Cardano.Ledger.Conway.Rules.Deleg (ConwayDelegPredFailure)
import Cardano.Ledger.Conway.Rules.Pool (ConwayPoolPredFailure)
import Cardano.Ledger.Conway.Rules.VDel (ConwayVDelPredFailure, VDelEnv (VDelEnv))
import Cardano.Ledger.Conway.TxCert (ConwayCommitteeCert, ConwayDelegCert, ConwayTxCert (..))
import Cardano.Ledger.Shelley.API (CertState (..), DState, DelegEnv (DelegEnv), DelplEnv (DelplEnv), PState, PoolEnv (PoolEnv), VState)
import Control.DeepSeq (NFData)
import Control.State.Transition.Extended (Embed, STS (..), TRC (TRC), TransitionRule, judgmentContext, trans, wrapEvent, wrapFailed)
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks)

data ConwayCertPredFailure era
= DelegFailure (PredicateFailure (EraRule "DELEG" era))
| PoolFailure (PredicateFailure (EraRule "POOL" era))
| VDelFailure (PredicateFailure (EraRule "VDEL" era))
deriving (Generic)

deriving stock instance
( Show (PredicateFailure (EraRule "DELEG" era))
, Show (PredicateFailure (EraRule "POOL" era))
, Show (PredicateFailure (EraRule "VDEL" era))
) =>
Show (ConwayCertPredFailure era)

deriving stock instance
( Eq (PredicateFailure (EraRule "DELEG" era))
, Eq (PredicateFailure (EraRule "POOL" era))
, Eq (PredicateFailure (EraRule "VDEL" era))
) =>
Eq (ConwayCertPredFailure era)

instance
( NoThunks (PredicateFailure (EraRule "DELEG" era))
, NoThunks (PredicateFailure (EraRule "POOL" era))
, NoThunks (PredicateFailure (EraRule "VDEL" era))
) =>
NoThunks (ConwayCertPredFailure era)

instance
( NFData (PredicateFailure (EraRule "DELEG" era))
, NFData (PredicateFailure (EraRule "POOL" era))
, NFData (PredicateFailure (EraRule "VDEL" era))
) =>
NFData (ConwayCertPredFailure era)

data ConwayCertEvent era
= DelegEvent (Event (ConwayDELEG era))
| PoolEvent (Event (ConwayPOOL era))
| VDelEvent (Event (ConwayVDEL era))

instance
forall era.
( Era era
, Eq (PredicateFailure (EraRule "DELEG" era))
, Show (PredicateFailure (EraRule "DELEG" era))
, Eq (PredicateFailure (EraRule "POOL" era))
, Show (PredicateFailure (EraRule "POOL" era))
, State (EraRule "DELEG" era) ~ DState era
, State (EraRule "POOL" era) ~ PState era
, State (EraRule "VDEL" era) ~ VState era
, Environment (EraRule "DELEG" era) ~ DelegEnv era
, Environment (EraRule "POOL" era) ~ PoolEnv era
, Environment (EraRule "VDEL" era) ~ VDelEnv era
, Signal (EraRule "DELEG" era) ~ ConwayDelegCert (EraCrypto era)
, Signal (EraRule "POOL" era) ~ PoolCert (EraCrypto era)
, Signal (EraRule "VDEL" era) ~ ConwayCommitteeCert (EraCrypto era)
, Embed (EraRule "DELEG" era) (ConwayCERT era)
, Embed (EraRule "POOL" era) (ConwayCERT era)
, Embed (EraRule "VDEL" era) (ConwayCERT era)
, TxCert era ~ ConwayTxCert era
) =>
STS (ConwayCERT era)
where
type State (ConwayCERT era) = CertState era
type Signal (ConwayCERT era) = TxCert era
type Environment (ConwayCERT era) = DelplEnv era
type BaseM (ConwayCERT era) = ShelleyBase
type PredicateFailure (ConwayCERT era) = ShelleyDelplPredFailure era
type Event (ConwayCERT era) = ShelleyDelplEvent era
type PredicateFailure (ConwayCERT era) = ConwayCertPredFailure era
type Event (ConwayCERT era) = ConwayCertEvent era

transitionRules = [certTransition @era]

transitionRules = undefined
certTransition ::
forall era.
( State (EraRule "DELEG" era) ~ DState era
, State (EraRule "POOL" era) ~ PState era
, State (EraRule "VDEL" era) ~ VState era
, Environment (EraRule "DELEG" era) ~ DelegEnv era
, Environment (EraRule "POOL" era) ~ PoolEnv era
, Environment (EraRule "VDEL" era) ~ VDelEnv era
, Signal (EraRule "DELEG" era) ~ ConwayDelegCert (EraCrypto era)
, Signal (EraRule "POOL" era) ~ PoolCert (EraCrypto era)
, Signal (EraRule "VDEL" era) ~ ConwayCommitteeCert (EraCrypto era)
, Embed (EraRule "DELEG" era) (ConwayCERT era)
, Embed (EraRule "POOL" era) (ConwayCERT era)
, Embed (EraRule "VDEL" era) (ConwayCERT era)
, TxCert era ~ ConwayTxCert era
) =>
TransitionRule (ConwayCERT era)
certTransition = do
TRC (DelplEnv slot ptr pp acnt, cState@CertState {certDState, certPState, certVState}, c) <- judgmentContext
case c of
ConwayTxCertDeleg delegCert -> do
newDState <- trans @(EraRule "DELEG" era) $ TRC (DelegEnv slot ptr acnt pp, certDState, delegCert)
pure $ cState {certDState = newDState}
ConwayTxCertPool poolCert -> do
newPState <- trans @(EraRule "POOL" era) $ TRC (PoolEnv slot pp, certPState, poolCert)
pure $ cState {certPState = newPState}
ConwayTxCertCommittee committeeCert -> do
newVState <- trans @(EraRule "VDEL" era) $ TRC (VDelEnv, certVState, committeeCert)
pure $ cState {certVState = newVState}

instance
( Era era
, STS (ConwayDELEG era)
, PredicateFailure (EraRule "DELEG" era) ~ ConwayDelegPredFailure era
) =>
Embed (ConwayDELEG era) (ConwayCERT era)
where
wrapFailed = DelegFailure
wrapEvent = DelegEvent

instance
( Era era
, STS (ConwayPOOL era)
, PredicateFailure (EraRule "POOL" era) ~ ConwayPoolPredFailure era
) =>
Embed (ConwayPOOL era) (ConwayCERT era)
where
wrapFailed = PoolFailure
wrapEvent = PoolEvent

instance
( Era era
, STS (ConwayVDEL era)
, PredicateFailure (EraRule "VDEL" era) ~ ConwayVDelPredFailure era
) =>
Embed (ConwayVDEL era) (ConwayCERT era)
where
wrapFailed = VDelFailure
wrapEvent = VDelEvent

0 comments on commit 5821b19

Please sign in to comment.