From 5c21aa4c91007a0b8942e31f219742b1605ded74 Mon Sep 17 00:00:00 2001 From: Javier Sagredo Date: Mon, 1 Apr 2024 11:21:20 +0200 Subject: [PATCH 01/10] Import StandardCrypto from Consensus namespace --- .../internal/Cardano/Api/LedgerState.hs | 66 +++++++++---------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/cardano-api/internal/Cardano/Api/LedgerState.hs b/cardano-api/internal/Cardano/Api/LedgerState.hs index 54d8e70b9f..8eee377ad9 100644 --- a/cardano-api/internal/Cardano/Api/LedgerState.hs +++ b/cardano-api/internal/Cardano/Api/LedgerState.hs @@ -331,32 +331,32 @@ pattern LedgerStateByron pattern LedgerStateByron st <- LedgerState (Consensus.LedgerStateByron st) pattern LedgerStateShelley - :: Ledger.LedgerState (Shelley.ShelleyBlock protocol (Shelley.ShelleyEra Shelley.StandardCrypto)) + :: Ledger.LedgerState (Shelley.ShelleyBlock protocol (Shelley.ShelleyEra Consensus.StandardCrypto)) -> LedgerState pattern LedgerStateShelley st <- LedgerState (Consensus.LedgerStateShelley st) pattern LedgerStateAllegra - :: Ledger.LedgerState (Shelley.ShelleyBlock protocol (Shelley.AllegraEra Shelley.StandardCrypto)) + :: Ledger.LedgerState (Shelley.ShelleyBlock protocol (Shelley.AllegraEra Consensus.StandardCrypto)) -> LedgerState pattern LedgerStateAllegra st <- LedgerState (Consensus.LedgerStateAllegra st) pattern LedgerStateMary - :: Ledger.LedgerState (Shelley.ShelleyBlock protocol (Shelley.MaryEra Shelley.StandardCrypto)) + :: Ledger.LedgerState (Shelley.ShelleyBlock protocol (Shelley.MaryEra Consensus.StandardCrypto)) -> LedgerState pattern LedgerStateMary st <- LedgerState (Consensus.LedgerStateMary st) pattern LedgerStateAlonzo - :: Ledger.LedgerState (Shelley.ShelleyBlock protocol (Shelley.AlonzoEra Shelley.StandardCrypto)) + :: Ledger.LedgerState (Shelley.ShelleyBlock protocol (Shelley.AlonzoEra Consensus.StandardCrypto)) -> LedgerState pattern LedgerStateAlonzo st <- LedgerState (Consensus.LedgerStateAlonzo st) pattern LedgerStateBabbage - :: Ledger.LedgerState (Shelley.ShelleyBlock protocol (Shelley.BabbageEra Shelley.StandardCrypto)) + :: Ledger.LedgerState (Shelley.ShelleyBlock protocol (Shelley.BabbageEra Consensus.StandardCrypto)) -> LedgerState pattern LedgerStateBabbage st <- LedgerState (Consensus.LedgerStateBabbage st) pattern LedgerStateConway - :: Ledger.LedgerState (Shelley.ShelleyBlock protocol (Shelley.ConwayEra Shelley.StandardCrypto)) + :: Ledger.LedgerState (Shelley.ShelleyBlock protocol (Shelley.ConwayEra Consensus.StandardCrypto)) -> LedgerState pattern LedgerStateConway st <- LedgerState (Consensus.LedgerStateConway st) @@ -1094,34 +1094,34 @@ getNewEpochState era x = do #-} pattern ShelleyLedgerState - :: Current Consensus.LedgerState (Shelley.ShelleyBlock (TPraos.TPraos Shelley.StandardCrypto) (Shelley.ShelleyEra Shelley.StandardCrypto)) + :: Current Consensus.LedgerState (Shelley.ShelleyBlock (TPraos.TPraos Consensus.StandardCrypto) (Shelley.ShelleyEra Consensus.StandardCrypto)) -> NS (Current Consensus.LedgerState) (Consensus.CardanoEras Consensus.StandardCrypto) pattern ShelleyLedgerState x = S (Z x) pattern AllegraLedgerState - :: Current Consensus.LedgerState (Shelley.ShelleyBlock (TPraos.TPraos Shelley.StandardCrypto) (Shelley.AllegraEra Shelley.StandardCrypto)) + :: Current Consensus.LedgerState (Shelley.ShelleyBlock (TPraos.TPraos Consensus.StandardCrypto) (Shelley.AllegraEra Consensus.StandardCrypto)) -> NS (Current Consensus.LedgerState) (Consensus.CardanoEras Consensus.StandardCrypto) pattern AllegraLedgerState x = S (S (Z x)) pattern MaryLedgerState - :: Current Consensus.LedgerState (Shelley.ShelleyBlock (TPraos.TPraos Shelley.StandardCrypto) (Shelley.MaryEra Shelley.StandardCrypto)) + :: Current Consensus.LedgerState (Shelley.ShelleyBlock (TPraos.TPraos Consensus.StandardCrypto) (Shelley.MaryEra Consensus.StandardCrypto)) -> NS (Current Consensus.LedgerState) (Consensus.CardanoEras Consensus.StandardCrypto) pattern MaryLedgerState x = S (S (S (Z x))) pattern AlonzoLedgerState - :: Current Consensus.LedgerState (Shelley.ShelleyBlock (TPraos.TPraos Shelley.StandardCrypto) (Shelley.AlonzoEra Shelley.StandardCrypto)) + :: Current Consensus.LedgerState (Shelley.ShelleyBlock (TPraos.TPraos Consensus.StandardCrypto) (Shelley.AlonzoEra Consensus.StandardCrypto)) -> NS (Current Consensus.LedgerState) (Consensus.CardanoEras Consensus.StandardCrypto) pattern AlonzoLedgerState x = S (S (S (S (Z x)))) pattern BabbageLedgerState - :: Current Consensus.LedgerState (Shelley.ShelleyBlock (Consensus.Praos Shelley.StandardCrypto) (Shelley.BabbageEra Shelley.StandardCrypto)) + :: Current Consensus.LedgerState (Shelley.ShelleyBlock (Consensus.Praos Consensus.StandardCrypto) (Shelley.BabbageEra Consensus.StandardCrypto)) -> NS (Current Consensus.LedgerState) (Consensus.CardanoEras Consensus.StandardCrypto) pattern BabbageLedgerState x = S (S (S (S (S (Z x))))) pattern ConwayLedgerState - :: Current Consensus.LedgerState (Shelley.ShelleyBlock (Consensus.Praos Shelley.StandardCrypto) (Shelley.ConwayEra Shelley.StandardCrypto)) + :: Current Consensus.LedgerState (Shelley.ShelleyBlock (Consensus.Praos Consensus.StandardCrypto) (Shelley.ConwayEra Consensus.StandardCrypto)) -> NS (Current Consensus.LedgerState) (Consensus.CardanoEras Consensus.StandardCrypto) pattern ConwayLedgerState x = S (S (S (S (S (S (Z x)))))) @@ -1158,17 +1158,17 @@ type LedgerStateEvents = (LedgerState, [LedgerEvent]) toLedgerStateEvents :: LedgerResult ( Shelley.LedgerState - (HFC.HardForkBlock (Consensus.CardanoEras Shelley.StandardCrypto)) + (HFC.HardForkBlock (Consensus.CardanoEras Consensus.StandardCrypto)) ) ( Shelley.LedgerState - (HFC.HardForkBlock (Consensus.CardanoEras Shelley.StandardCrypto)) + (HFC.HardForkBlock (Consensus.CardanoEras Consensus.StandardCrypto)) ) -> LedgerStateEvents toLedgerStateEvents lr = (ledgerState, ledgerEvents) where ledgerState = LedgerState (lrResult lr) ledgerEvents = mapMaybe (toLedgerEvent - . WrapLedgerEvent @(HFC.HardForkBlock (Consensus.CardanoEras Shelley.StandardCrypto))) + . WrapLedgerEvent @(HFC.HardForkBlock (Consensus.CardanoEras Consensus.StandardCrypto))) $ lrEvents lr -- Usually only one constructor, but may have two when we are preparing for a HFC event. @@ -1177,7 +1177,7 @@ data GenesisConfig !NodeConfig !Cardano.Chain.Genesis.Config !GenesisHashShelley - !(Ledger.TransitionConfig (Ledger.LatestKnownEra Shelley.StandardCrypto)) + !(Ledger.TransitionConfig (Ledger.LatestKnownEra Consensus.StandardCrypto)) newtype LedgerStateDir = LedgerStateDir { unLedgerStateDir :: FilePath @@ -1332,7 +1332,7 @@ readAlonzoGenesisConfig enc = do readConwayGenesisConfig :: MonadIOTransError GenesisConfigError t m => NodeConfig - -> t m (ConwayGenesis Shelley.StandardCrypto) + -> t m (ConwayGenesis Consensus.StandardCrypto) readConwayGenesisConfig enc = do let file = ncConwayGenesisFile enc modifyError (NEConwayConfig (unFile file) . renderConwayGenesisError) @@ -1437,7 +1437,7 @@ readConwayGenesis :: forall m t. MonadIOTransError ConwayGenesisError t m => ConwayGenesisFile 'In -> GenesisHashConway - -> t m (ConwayGenesis Shelley.StandardCrypto) + -> t m (ConwayGenesis Consensus.StandardCrypto) readConwayGenesis (File file) expectedGenesisHash = do content <- modifyError id $ handleIOExceptT (ConwayGenesisReadError file . textShow) $ BS.readFile file let genesisHash = GenesisHashConway (Cardano.Crypto.Hash.Class.hashWith id content) @@ -1487,8 +1487,8 @@ newtype StakeCred deriving (Eq, Ord) data Env = Env - { envLedgerConfig :: HFC.HardForkLedgerConfig (Consensus.CardanoEras Shelley.StandardCrypto) - , envProtocolConfig :: TPraos.ConsensusConfig (HFC.HardForkProtocol (Consensus.CardanoEras Shelley.StandardCrypto)) + { envLedgerConfig :: HFC.HardForkLedgerConfig (Consensus.CardanoEras Consensus.StandardCrypto) + , envProtocolConfig :: TPraos.ConsensusConfig (HFC.HardForkProtocol (Consensus.CardanoEras Consensus.StandardCrypto)) } envSecurityParam :: Env -> Word64 @@ -1542,11 +1542,11 @@ applyBlockWithEvents env oldState enableValidation block = do -- the block matches the head hash of the ledger state. tickThenReapplyCheckHash :: HFC.HardForkLedgerConfig - (Consensus.CardanoEras Shelley.StandardCrypto) + (Consensus.CardanoEras Consensus.StandardCrypto) -> Consensus.CardanoBlock Consensus.StandardCrypto -> Shelley.LedgerState (HFC.HardForkBlock - (Consensus.CardanoEras Shelley.StandardCrypto)) + (Consensus.CardanoEras Consensus.StandardCrypto)) -> Either LedgerStateError LedgerStateEvents tickThenReapplyCheckHash cfg block lsb = if Consensus.blockPrevHash block == Ledger.ledgerTipHash lsb @@ -1577,11 +1577,11 @@ tickThenReapplyCheckHash cfg block lsb = -- the block matches the head hash of the ledger state. tickThenApply :: HFC.HardForkLedgerConfig - (Consensus.CardanoEras Shelley.StandardCrypto) + (Consensus.CardanoEras Consensus.StandardCrypto) -> Consensus.CardanoBlock Consensus.StandardCrypto -> Shelley.LedgerState (HFC.HardForkBlock - (Consensus.CardanoEras Shelley.StandardCrypto)) + (Consensus.CardanoEras Consensus.StandardCrypto)) -> Either LedgerStateError LedgerStateEvents tickThenApply cfg block lsb = either (Left . ApplyBlockError) (Right . toLedgerStateEvents) @@ -1646,7 +1646,7 @@ instance Api.Error LeadershipError where nextEpochEligibleLeadershipSlots :: forall era. () => ShelleyBasedEra era - -> ShelleyGenesis Shelley.StandardCrypto + -> ShelleyGenesis Consensus.StandardCrypto -> SerialisedCurrentEpochState era -- ^ We need the mark stake distribution in order to predict -- the following epoch's leadership schedule @@ -1716,9 +1716,9 @@ nextEpochEligibleLeadershipSlots sbe sGen serCurrEpochState ptclState poolid (Vr first LeaderErrDecodeProtocolEpochStateFailure $ decodeCurrentEpochState sbe serCurrEpochState - let snapshot :: ShelleyAPI.SnapShot Shelley.StandardCrypto + let snapshot :: ShelleyAPI.SnapShot Consensus.StandardCrypto snapshot = ShelleyAPI.ssStakeMark $ ShelleyAPI.esSnapshots cEstate - markSnapshotPoolDistr :: Map (SL.KeyHash 'SL.StakePool Shelley.StandardCrypto) (SL.IndividualPoolStake Shelley.StandardCrypto) + markSnapshotPoolDistr :: Map (SL.KeyHash 'SL.StakePool Consensus.StandardCrypto) (SL.IndividualPoolStake Consensus.StandardCrypto) markSnapshotPoolDistr = ShelleyAPI.unPoolDistr . ShelleyAPI.calculatePoolDistr $ snapshot let slotRangeOfInterest :: Core.EraPParams ledgerera => Core.PParams ledgerera -> Set SlotNo @@ -1747,7 +1747,7 @@ isLeadingSlotsTPraos :: forall v. () => Crypto.ContextVRF v ~ () => Set SlotNo -> PoolId - -> Map (SL.KeyHash 'SL.StakePool Shelley.StandardCrypto) (SL.IndividualPoolStake Shelley.StandardCrypto) + -> Map (SL.KeyHash 'SL.StakePool Consensus.StandardCrypto) (SL.IndividualPoolStake Consensus.StandardCrypto) -> Consensus.Nonce -> Crypto.SignKeyVRF v -> Ledger.ActiveSlotCoeff @@ -1768,9 +1768,9 @@ isLeadingSlotsTPraos slotRangeOfInterest poolid snapshotPoolDistr eNonce vrfSkey isLeadingSlotsPraos :: () => Set SlotNo -> PoolId - -> Map (SL.KeyHash 'SL.StakePool Shelley.StandardCrypto) (SL.IndividualPoolStake Shelley.StandardCrypto) + -> Map (SL.KeyHash 'SL.StakePool Consensus.StandardCrypto) (SL.IndividualPoolStake Consensus.StandardCrypto) -> Consensus.Nonce - -> SL.SignKeyVRF Shelley.StandardCrypto + -> SL.SignKeyVRF Consensus.StandardCrypto -> Ledger.ActiveSlotCoeff -> Either LeadershipError (Set SlotNo) isLeadingSlotsPraos slotRangeOfInterest poolid snapshotPoolDistr eNonce vrfSkey activeSlotCoeff' = do @@ -1781,7 +1781,7 @@ isLeadingSlotsPraos slotRangeOfInterest poolid snapshotPoolDistr eNonce vrfSkey let isLeader slotNo = checkLeaderNatValue certifiedNatValue stakePoolStake activeSlotCoeff' where rho = VRF.evalCertified () (mkInputVRF slotNo eNonce) vrfSkey - certifiedNatValue = vrfLeaderValue (Proxy @Shelley.StandardCrypto) rho + certifiedNatValue = vrfLeaderValue (Proxy @Consensus.StandardCrypto) rho Right $ Set.filter isLeader slotRangeOfInterest @@ -1789,7 +1789,7 @@ isLeadingSlotsPraos slotRangeOfInterest poolid snapshotPoolDistr eNonce vrfSkey -- expected to mint a block. currentEpochEligibleLeadershipSlots :: forall era. () => ShelleyBasedEra era - -> ShelleyGenesis Shelley.StandardCrypto + -> ShelleyGenesis Consensus.StandardCrypto -> EpochInfo (Either Text) -> Ledger.PParams (ShelleyLedgerEra era) -> ProtocolState era @@ -1832,7 +1832,7 @@ currentEpochEligibleLeadershipSlots sbe sGen eInfo pp ptclState poolid (VrfSigni f = activeSlotCoeff globals constructGlobals - :: ShelleyGenesis Shelley.StandardCrypto + :: ShelleyGenesis Consensus.StandardCrypto -> EpochInfo (Either Text) -> Ledger.ProtVer -> Globals From 993d0e86eea05797ee9c8dcc5a0ecb0c64d3a654 Mon Sep 17 00:00:00 2001 From: Javier Sagredo Date: Mon, 1 Apr 2024 12:12:32 +0200 Subject: [PATCH 02/10] Rework encoding and decoding of LedgerState --- .../internal/Cardano/Api/LedgerState.hs | 44 ++++++------------- 1 file changed, 14 insertions(+), 30 deletions(-) diff --git a/cardano-api/internal/Cardano/Api/LedgerState.hs b/cardano-api/internal/Cardano/Api/LedgerState.hs index 8eee377ad9..f389086940 100644 --- a/cardano-api/internal/Cardano/Api/LedgerState.hs +++ b/cardano-api/internal/Cardano/Api/LedgerState.hs @@ -144,7 +144,6 @@ import qualified Cardano.Slotting.Slot as Slot import qualified Ouroboros.Consensus.Block.Abstract as Consensus import Ouroboros.Consensus.Block.Forging (BlockForging) import qualified Ouroboros.Consensus.Byron.Ledger.Block as Byron -import qualified Ouroboros.Consensus.Byron.Ledger.Ledger as Byron import qualified Ouroboros.Consensus.Cardano as Consensus import qualified Ouroboros.Consensus.Cardano.Block as Consensus import qualified Ouroboros.Consensus.Cardano.CanHardFork as Consensus @@ -153,7 +152,6 @@ import qualified Ouroboros.Consensus.Config as Consensus import qualified Ouroboros.Consensus.HardFork.Combinator as Consensus import qualified Ouroboros.Consensus.HardFork.Combinator.AcrossEras as HFC import qualified Ouroboros.Consensus.HardFork.Combinator.Basics as HFC -import qualified Ouroboros.Consensus.HardFork.Combinator.Serialisation.Common as HFC import Ouroboros.Consensus.HardFork.Combinator.State.Types import qualified Ouroboros.Consensus.Ledger.Abstract as Ledger import Ouroboros.Consensus.Ledger.Basics (LedgerResult (lrEvents), lrResult) @@ -168,6 +166,7 @@ import qualified Ouroboros.Consensus.Protocol.TPraos as TPraos import qualified Ouroboros.Consensus.Shelley.Eras as Shelley import qualified Ouroboros.Consensus.Shelley.Ledger.Block as Shelley import qualified Ouroboros.Consensus.Shelley.Ledger.Ledger as Shelley +import Ouroboros.Consensus.Storage.Serialisation import Ouroboros.Consensus.TypeFamilyWrappers (WrapLedgerEvent (WrapLedgerEvent)) import Ouroboros.Network.Block (blockNo) import qualified Ouroboros.Network.Block @@ -203,8 +202,7 @@ import Data.Sequence (Seq) import qualified Data.Sequence as Seq import Data.Set (Set) import qualified Data.Set as Set -import Data.SOP (K (K), (:.:) (Comp)) -import Data.SOP.Strict (NP (..), fn) +import Data.SOP.Strict (NP (..)) import Data.SOP.Strict.NS import qualified Data.SOP.Telescope as Telescope import Data.Text (Text) @@ -1126,32 +1124,18 @@ pattern ConwayLedgerState pattern ConwayLedgerState x = S (S (S (S (S (S (Z x)))))) -encodeLedgerState :: LedgerState -> CBOR.Encoding -encodeLedgerState (LedgerState (HFC.HardForkLedgerState st)) = - HFC.encodeTelescope - (byron :* shelley :* allegra :* mary :* alonzo :* babbage :* conway :* Nil) - st - where - byron = fn (K . Byron.encodeByronLedgerState) - shelley = fn (K . Shelley.encodeShelleyLedgerState) - allegra = fn (K . Shelley.encodeShelleyLedgerState) - mary = fn (K . Shelley.encodeShelleyLedgerState) - alonzo = fn (K . Shelley.encodeShelleyLedgerState) - babbage = fn (K . Shelley.encodeShelleyLedgerState) - conway = fn (K . Shelley.encodeShelleyLedgerState) - -decodeLedgerState :: forall s. CBOR.Decoder s LedgerState -decodeLedgerState = - LedgerState . HFC.HardForkLedgerState - <$> HFC.decodeTelescope (byron :* shelley :* allegra :* mary :* alonzo :* babbage :* conway :* Nil) - where - byron = Comp Byron.decodeByronLedgerState - shelley = Comp Shelley.decodeShelleyLedgerState - allegra = Comp Shelley.decodeShelleyLedgerState - mary = Comp Shelley.decodeShelleyLedgerState - alonzo = Comp Shelley.decodeShelleyLedgerState - babbage = Comp Shelley.decodeShelleyLedgerState - conway = Comp Shelley.decodeShelleyLedgerState +encodeLedgerState :: + Consensus.CardanoCodecConfig Consensus.StandardCrypto + -> LedgerState + -> CBOR.Encoding +encodeLedgerState ccfg (LedgerState st) = + encodeDisk @(Consensus.CardanoBlock Consensus.StandardCrypto) ccfg st + +decodeLedgerState :: + Consensus.CardanoCodecConfig Consensus.StandardCrypto + -> forall s. CBOR.Decoder s LedgerState +decodeLedgerState ccfg = + LedgerState <$> decodeDisk @(Consensus.CardanoBlock Consensus.StandardCrypto) ccfg type LedgerStateEvents = (LedgerState, [LedgerEvent]) From d83352222623b0175432c14f832364157ec68d47 Mon Sep 17 00:00:00 2001 From: Javier Sagredo Date: Mon, 1 Apr 2024 12:15:22 +0200 Subject: [PATCH 03/10] Re-export patterns instead of redefine them --- .../internal/Cardano/Api/LedgerState.hs | 61 +++---------------- 1 file changed, 9 insertions(+), 52 deletions(-) diff --git a/cardano-api/internal/Cardano/Api/LedgerState.hs b/cardano-api/internal/Cardano/Api/LedgerState.hs index f389086940..6ab4f247b5 100644 --- a/cardano-api/internal/Cardano/Api/LedgerState.hs +++ b/cardano-api/internal/Cardano/Api/LedgerState.hs @@ -17,15 +17,15 @@ module Cardano.Api.LedgerState ( -- * Initialization / Accumulation envSecurityParam , LedgerState - ( .. - , LedgerStateByron - , LedgerStateShelley - , LedgerStateAllegra - , LedgerStateMary - , LedgerStateAlonzo - , LedgerStateBabbage - , LedgerStateConway - ) + ( .. ) + , pattern Consensus.LedgerStateByron + , pattern Consensus.LedgerStateShelley + , pattern Consensus.LedgerStateAllegra + , pattern Consensus.LedgerStateMary + , pattern Consensus.LedgerStateAlonzo + , pattern Consensus.LedgerStateBabbage + , pattern Consensus.LedgerStateConway + , encodeLedgerState , decodeLedgerState , initialLedgerState @@ -143,7 +143,6 @@ import Cardano.Slotting.Slot (WithOrigin (At, Origin)) import qualified Cardano.Slotting.Slot as Slot import qualified Ouroboros.Consensus.Block.Abstract as Consensus import Ouroboros.Consensus.Block.Forging (BlockForging) -import qualified Ouroboros.Consensus.Byron.Ledger.Block as Byron import qualified Ouroboros.Consensus.Cardano as Consensus import qualified Ouroboros.Consensus.Cardano.Block as Consensus import qualified Ouroboros.Consensus.Cardano.CanHardFork as Consensus @@ -323,48 +322,6 @@ applyBlock env oldState validationMode block ShelleyBasedEraBabbage -> Consensus.BlockBabbage shelleyBlock ShelleyBasedEraConway -> Consensus.BlockConway shelleyBlock -pattern LedgerStateByron - :: Ledger.LedgerState Byron.ByronBlock - -> LedgerState -pattern LedgerStateByron st <- LedgerState (Consensus.LedgerStateByron st) - -pattern LedgerStateShelley - :: Ledger.LedgerState (Shelley.ShelleyBlock protocol (Shelley.ShelleyEra Consensus.StandardCrypto)) - -> LedgerState -pattern LedgerStateShelley st <- LedgerState (Consensus.LedgerStateShelley st) - -pattern LedgerStateAllegra - :: Ledger.LedgerState (Shelley.ShelleyBlock protocol (Shelley.AllegraEra Consensus.StandardCrypto)) - -> LedgerState -pattern LedgerStateAllegra st <- LedgerState (Consensus.LedgerStateAllegra st) - -pattern LedgerStateMary - :: Ledger.LedgerState (Shelley.ShelleyBlock protocol (Shelley.MaryEra Consensus.StandardCrypto)) - -> LedgerState -pattern LedgerStateMary st <- LedgerState (Consensus.LedgerStateMary st) - -pattern LedgerStateAlonzo - :: Ledger.LedgerState (Shelley.ShelleyBlock protocol (Shelley.AlonzoEra Consensus.StandardCrypto)) - -> LedgerState -pattern LedgerStateAlonzo st <- LedgerState (Consensus.LedgerStateAlonzo st) - -pattern LedgerStateBabbage - :: Ledger.LedgerState (Shelley.ShelleyBlock protocol (Shelley.BabbageEra Consensus.StandardCrypto)) - -> LedgerState -pattern LedgerStateBabbage st <- LedgerState (Consensus.LedgerStateBabbage st) - -pattern LedgerStateConway - :: Ledger.LedgerState (Shelley.ShelleyBlock protocol (Shelley.ConwayEra Consensus.StandardCrypto)) - -> LedgerState -pattern LedgerStateConway st <- LedgerState (Consensus.LedgerStateConway st) - -{-# COMPLETE LedgerStateByron - , LedgerStateShelley - , LedgerStateAllegra - , LedgerStateMary - , LedgerStateAlonzo - , LedgerStateBabbage - , LedgerStateConway #-} data FoldBlocksError = FoldBlocksInitialLedgerStateError !InitialLedgerStateError From 7ea72329be30b40d016f7f22c569be2846db3d67 Mon Sep 17 00:00:00 2001 From: Javier Sagredo Date: Mon, 1 Apr 2024 12:16:40 +0200 Subject: [PATCH 04/10] Use consensus type aliases --- .../internal/Cardano/Api/LedgerState.hs | 52 +++++++------------ 1 file changed, 18 insertions(+), 34 deletions(-) diff --git a/cardano-api/internal/Cardano/Api/LedgerState.hs b/cardano-api/internal/Cardano/Api/LedgerState.hs index 6ab4f247b5..c82661e0f1 100644 --- a/cardano-api/internal/Cardano/Api/LedgerState.hs +++ b/cardano-api/internal/Cardano/Api/LedgerState.hs @@ -153,7 +153,6 @@ import qualified Ouroboros.Consensus.HardFork.Combinator.AcrossEras as HFC import qualified Ouroboros.Consensus.HardFork.Combinator.Basics as HFC import Ouroboros.Consensus.HardFork.Combinator.State.Types import qualified Ouroboros.Consensus.Ledger.Abstract as Ledger -import Ouroboros.Consensus.Ledger.Basics (LedgerResult (lrEvents), lrResult) import qualified Ouroboros.Consensus.Ledger.Extended as Ledger import qualified Ouroboros.Consensus.Mempool.Capacity as TxLimits import qualified Ouroboros.Consensus.Node.ProtocolInfo as Consensus @@ -987,9 +986,7 @@ initLedgerStateVar genesisConfig = LedgerState protocolInfo = mkProtocolInfoCardano genesisConfig newtype LedgerState = LedgerState - { clsState :: Ledger.LedgerState - (HFC.HardForkBlock - (Consensus.CardanoEras Consensus.StandardCrypto)) + { clsState :: Consensus.CardanoLedgerState Consensus.StandardCrypto } deriving Show @@ -1097,20 +1094,17 @@ decodeLedgerState ccfg = type LedgerStateEvents = (LedgerState, [LedgerEvent]) toLedgerStateEvents :: - LedgerResult - ( Shelley.LedgerState - (HFC.HardForkBlock (Consensus.CardanoEras Consensus.StandardCrypto)) - ) - ( Shelley.LedgerState - (HFC.HardForkBlock (Consensus.CardanoEras Consensus.StandardCrypto)) - ) -> + Ledger.LedgerResult + (Consensus.CardanoLedgerState Consensus.StandardCrypto) + (Consensus.CardanoLedgerState Consensus.StandardCrypto) + -> LedgerStateEvents toLedgerStateEvents lr = (ledgerState, ledgerEvents) where - ledgerState = LedgerState (lrResult lr) + ledgerState = LedgerState (Ledger.lrResult lr) ledgerEvents = mapMaybe (toLedgerEvent - . WrapLedgerEvent @(HFC.HardForkBlock (Consensus.CardanoEras Consensus.StandardCrypto))) - $ lrEvents lr + . WrapLedgerEvent @(Consensus.CardanoBlock Consensus.StandardCrypto)) + $ Ledger.lrEvents lr -- Usually only one constructor, but may have two when we are preparing for a HFC event. data GenesisConfig @@ -1133,10 +1127,8 @@ type NodeConfigFile = File NodeConfig mkProtocolInfoCardano :: GenesisConfig -> (Consensus.ProtocolInfo - (HFC.HardForkBlock - (Consensus.CardanoEras Consensus.StandardCrypto)) - , IO [BlockForging IO (HFC.HardForkBlock - (Consensus.CardanoEras Consensus.StandardCrypto))]) + (Consensus.CardanoBlock Consensus.StandardCrypto) + , IO [BlockForging IO (Consensus.CardanoBlock Consensus.StandardCrypto)]) mkProtocolInfoCardano (GenesisCardano dnc byronGenesis shelleyGenesisHash transCfg) = Consensus.protocolInfoCardano Consensus.CardanoProtocolParams { Consensus.paramsByron = @@ -1428,8 +1420,8 @@ newtype StakeCred deriving (Eq, Ord) data Env = Env - { envLedgerConfig :: HFC.HardForkLedgerConfig (Consensus.CardanoEras Consensus.StandardCrypto) - , envProtocolConfig :: TPraos.ConsensusConfig (HFC.HardForkProtocol (Consensus.CardanoEras Consensus.StandardCrypto)) + { envLedgerConfig :: Consensus.CardanoLedgerConfig Consensus.StandardCrypto + , envProtocolConfig :: Consensus.CardanoConsensusConfig Consensus.StandardCrypto } envSecurityParam :: Env -> Word64 @@ -1454,8 +1446,7 @@ applyBlock' :: Env -> LedgerState -> ValidationMode - -> HFC.HardForkBlock - (Consensus.CardanoEras Consensus.StandardCrypto) + -> Consensus.CardanoBlock Consensus.StandardCrypto -> Either LedgerStateError LedgerStateEvents applyBlock' env oldState validationMode block = do let config = envLedgerConfig env @@ -1469,8 +1460,7 @@ applyBlockWithEvents -> LedgerState -> Bool -- ^ True to validate - -> HFC.HardForkBlock - (Consensus.CardanoEras Consensus.StandardCrypto) + -> Consensus.CardanoBlock Consensus.StandardCrypto -> Either LedgerStateError LedgerStateEvents applyBlockWithEvents env oldState enableValidation block = do let config = envLedgerConfig env @@ -1482,12 +1472,9 @@ applyBlockWithEvents env oldState enableValidation block = do -- Like 'Consensus.tickThenReapply' but also checks that the previous hash from -- the block matches the head hash of the ledger state. tickThenReapplyCheckHash - :: HFC.HardForkLedgerConfig - (Consensus.CardanoEras Consensus.StandardCrypto) + :: Consensus.CardanoLedgerConfig Consensus.StandardCrypto -> Consensus.CardanoBlock Consensus.StandardCrypto - -> Shelley.LedgerState - (HFC.HardForkBlock - (Consensus.CardanoEras Consensus.StandardCrypto)) + -> Consensus.CardanoLedgerState Consensus.StandardCrypto -> Either LedgerStateError LedgerStateEvents tickThenReapplyCheckHash cfg block lsb = if Consensus.blockPrevHash block == Ledger.ledgerTipHash lsb @@ -1517,12 +1504,9 @@ tickThenReapplyCheckHash cfg block lsb = -- Like 'Consensus.tickThenReapply' but also checks that the previous hash from -- the block matches the head hash of the ledger state. tickThenApply - :: HFC.HardForkLedgerConfig - (Consensus.CardanoEras Consensus.StandardCrypto) + :: Consensus.CardanoLedgerConfig Consensus.StandardCrypto -> Consensus.CardanoBlock Consensus.StandardCrypto - -> Shelley.LedgerState - (HFC.HardForkBlock - (Consensus.CardanoEras Consensus.StandardCrypto)) + -> Consensus.CardanoLedgerState Consensus.StandardCrypto -> Either LedgerStateError LedgerStateEvents tickThenApply cfg block lsb = either (Left . ApplyBlockError) (Right . toLedgerStateEvents) From 00755dbc595c54b91bae80f5c9e269b4cffff2ac Mon Sep 17 00:00:00 2001 From: Javier Sagredo Date: Mon, 1 Apr 2024 12:17:42 +0200 Subject: [PATCH 05/10] Don't redefine how to unwrap a block --- .../internal/Cardano/Api/LedgerState.hs | 31 +++++++------------ 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/cardano-api/internal/Cardano/Api/LedgerState.hs b/cardano-api/internal/Cardano/Api/LedgerState.hs index c82661e0f1..bb9d704f6a 100644 --- a/cardano-api/internal/Cardano/Api/LedgerState.hs +++ b/cardano-api/internal/Cardano/Api/LedgerState.hs @@ -306,21 +306,12 @@ applyBlock -> LedgerState -- ^ The current ledger state -> ValidationMode - -> Block era + -> BlockInMode -- ^ Some block to apply -> Either LedgerStateError (LedgerState, [LedgerEvent]) -- ^ The new ledger state (or an error). -applyBlock env oldState validationMode block - = applyBlock' env oldState validationMode $ case block of - ByronBlock byronBlock -> Consensus.BlockByron byronBlock - ShelleyBlock blockEra shelleyBlock -> case blockEra of - ShelleyBasedEraShelley -> Consensus.BlockShelley shelleyBlock - ShelleyBasedEraAllegra -> Consensus.BlockAllegra shelleyBlock - ShelleyBasedEraMary -> Consensus.BlockMary shelleyBlock - ShelleyBasedEraAlonzo -> Consensus.BlockAlonzo shelleyBlock - ShelleyBasedEraBabbage -> Consensus.BlockBabbage shelleyBlock - ShelleyBasedEraConway -> Consensus.BlockConway shelleyBlock - +applyBlock env oldState validationMode + = applyBlock' env oldState validationMode . toConsensusBlock data FoldBlocksError = FoldBlocksInitialLedgerStateError !InitialLedgerStateError @@ -477,7 +468,7 @@ foldBlocks nodeConfigFilePath socketPath validationMode state0 accumulate = hand -> CSP.ClientStNext n BlockInMode ChainPoint ChainTip IO () clientNextN n knownLedgerStates = CSP.ClientStNext { - CSP.recvMsgRollForward = \blockInMode@(BlockInMode _ block@(Block (BlockHeader slotNo _ currBlockNo) _)) serverChainTip -> do + CSP.recvMsgRollForward = \blockInMode@(BlockInMode _ (Block (BlockHeader slotNo _ currBlockNo) _)) serverChainTip -> do let newLedgerStateE = applyBlock env (maybe @@ -486,7 +477,7 @@ foldBlocks nodeConfigFilePath socketPath validationMode state0 accumulate = hand (Seq.lookup 0 knownLedgerStates) ) validationMode - block + blockInMode case newLedgerStateE of Left err -> clientIdle_DoneNwithMaybeError n (Just err) Right newLedgerState -> do @@ -640,7 +631,7 @@ chainSyncClientWithLedgerState env ledgerState0 validationMode (CS.ChainSyncClie goClientStIdle (Left err) <$> CS.runChainSyncClient (recvMsgRollBackward point tip) ) goClientStNext (Right history) (CS.ClientStNext recvMsgRollForward recvMsgRollBackward) = CS.ClientStNext - (\blkInMode@(BlockInMode _ blk@(Block (BlockHeader slotNo _ _) _)) tip -> CS.ChainSyncClient $ let + (\blkInMode@(BlockInMode _ (Block (BlockHeader slotNo _ _) _)) tip -> CS.ChainSyncClient $ let newLedgerStateE = case Seq.lookup 0 history of Nothing -> error "Impossible! History should always be non-empty" Just (_, Left err, _) -> Left err @@ -648,7 +639,7 @@ chainSyncClientWithLedgerState env ledgerState0 validationMode (CS.ChainSyncClie env oldLedgerState validationMode - blk + blkInMode (history', _) = pushLedgerState env history slotNo newLedgerStateE blkInMode in goClientStIdle (Right history') <$> CS.runChainSyncClient (recvMsgRollForward (blkInMode, newLedgerStateE) tip) @@ -728,7 +719,7 @@ chainSyncClientPipelinedWithLedgerState env ledgerState0 validationMode (CSP.Cha goClientPipelinedStIdle (Left err) n <$> recvMsgRollBackward point tip ) goClientStNext (Right history) n (CSP.ClientStNext recvMsgRollForward recvMsgRollBackward) = CSP.ClientStNext - (\blkInMode@(BlockInMode _ blk@(Block (BlockHeader slotNo _ _) _)) tip -> let + (\blkInMode@(BlockInMode _ (Block (BlockHeader slotNo _ _) _)) tip -> let newLedgerStateE = case Seq.lookup 0 history of Nothing -> error "Impossible! History should always be non-empty" Just (_, Left err, _) -> Left err @@ -736,7 +727,7 @@ chainSyncClientPipelinedWithLedgerState env ledgerState0 validationMode (CSP.Cha env oldLedgerState validationMode - blk + blkInMode (history', _) = pushLedgerState env history slotNo newLedgerStateE blkInMode in goClientPipelinedStIdle (Right history') n <$> recvMsgRollForward (blkInMode, newLedgerStateE) tip @@ -1920,7 +1911,7 @@ foldEpochState nodeConfigFilePath socketPath validationMode terminationEpoch ini -> CSP.ClientStNext n BlockInMode ChainPoint ChainTip IO () clientNextN n knownLedgerStates = CSP.ClientStNext { - CSP.recvMsgRollForward = \blockInMode@(BlockInMode era block@(Block (BlockHeader slotNo _ currBlockNo) _)) serverChainTip -> do + CSP.recvMsgRollForward = \blockInMode@(BlockInMode era (Block (BlockHeader slotNo _ currBlockNo) _)) serverChainTip -> do let newLedgerStateE = applyBlock env (maybe @@ -1929,7 +1920,7 @@ foldEpochState nodeConfigFilePath socketPath validationMode terminationEpoch ini (Seq.lookup 0 knownLedgerStates) ) validationMode - block + blockInMode case forEraMaybeEon era of Nothing -> let !err = Just ByronEraUnsupported in clientIdle_DoneNwithMaybeError n err From 0a86f4459cf6e006f56c38076d56cbceaf6cb8a2 Mon Sep 17 00:00:00 2001 From: Javier Sagredo Date: Mon, 1 Apr 2024 12:27:28 +0200 Subject: [PATCH 06/10] Rename envProtocolConfig to envConsensusConfig --- cardano-api/internal/Cardano/Api/LedgerState.hs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cardano-api/internal/Cardano/Api/LedgerState.hs b/cardano-api/internal/Cardano/Api/LedgerState.hs index bb9d704f6a..287ee5ad98 100644 --- a/cardano-api/internal/Cardano/Api/LedgerState.hs +++ b/cardano-api/internal/Cardano/Api/LedgerState.hs @@ -832,7 +832,7 @@ genesisConfigToEnv in Right $ Env { envLedgerConfig = Consensus.topLevelConfigLedger topLevelConfig - , envProtocolConfig = Consensus.topLevelConfigProtocol topLevelConfig + , envConsensusConfig = Consensus.topLevelConfigProtocol topLevelConfig } where shelleyGenesis = transCfg ^. Ledger.tcShelleyGenesisL @@ -1412,7 +1412,7 @@ newtype StakeCred data Env = Env { envLedgerConfig :: Consensus.CardanoLedgerConfig Consensus.StandardCrypto - , envProtocolConfig :: Consensus.CardanoConsensusConfig Consensus.StandardCrypto + , envConsensusConfig :: Consensus.CardanoConsensusConfig Consensus.StandardCrypto } envSecurityParam :: Env -> Word64 @@ -1420,7 +1420,7 @@ envSecurityParam env = k where Consensus.SecurityParam k = HFC.hardForkConsensusConfigK - $ envProtocolConfig env + $ envConsensusConfig env -- | How to do validation when applying a block to a ledger state. data ValidationMode From 3b61612021c442158da7ebca906c282f421ecb66 Mon Sep 17 00:00:00 2001 From: Javier Sagredo Date: Mon, 1 Apr 2024 12:32:34 +0200 Subject: [PATCH 07/10] Remove unnecessary patterns --- .../internal/Cardano/Api/LedgerState.hs | 98 +++++-------------- 1 file changed, 24 insertions(+), 74 deletions(-) diff --git a/cardano-api/internal/Cardano/Api/LedgerState.hs b/cardano-api/internal/Cardano/Api/LedgerState.hs index 287ee5ad98..5a6abee83c 100644 --- a/cardano-api/internal/Cardano/Api/LedgerState.hs +++ b/cardano-api/internal/Cardano/Api/LedgerState.hs @@ -16,8 +16,7 @@ module Cardano.Api.LedgerState ( -- * Initialization / Accumulation envSecurityParam - , LedgerState - ( .. ) + , LedgerState ( .. ) , pattern Consensus.LedgerStateByron , pattern Consensus.LedgerStateShelley , pattern Consensus.LedgerStateAllegra @@ -151,18 +150,13 @@ import qualified Ouroboros.Consensus.Config as Consensus import qualified Ouroboros.Consensus.HardFork.Combinator as Consensus import qualified Ouroboros.Consensus.HardFork.Combinator.AcrossEras as HFC import qualified Ouroboros.Consensus.HardFork.Combinator.Basics as HFC -import Ouroboros.Consensus.HardFork.Combinator.State.Types import qualified Ouroboros.Consensus.Ledger.Abstract as Ledger import qualified Ouroboros.Consensus.Ledger.Extended as Ledger import qualified Ouroboros.Consensus.Mempool.Capacity as TxLimits import qualified Ouroboros.Consensus.Node.ProtocolInfo as Consensus import Ouroboros.Consensus.Protocol.Abstract (ChainDepState, ConsensusProtocol (..)) -import qualified Ouroboros.Consensus.Protocol.Praos as Consensus import qualified Ouroboros.Consensus.Protocol.Praos.Common as Consensus import Ouroboros.Consensus.Protocol.Praos.VRF (mkInputVRF, vrfLeaderValue) -import qualified Ouroboros.Consensus.Protocol.TPraos as TPraos -import qualified Ouroboros.Consensus.Shelley.Eras as Shelley -import qualified Ouroboros.Consensus.Shelley.Ledger.Block as Shelley import qualified Ouroboros.Consensus.Shelley.Ledger.Ledger as Shelley import Ouroboros.Consensus.Storage.Serialisation import Ouroboros.Consensus.TypeFamilyWrappers (WrapLedgerEvent (WrapLedgerEvent)) @@ -200,9 +194,7 @@ import Data.Sequence (Seq) import qualified Data.Sequence as Seq import Data.Set (Set) import qualified Data.Set as Set -import Data.SOP.Strict (NP (..)) -import Data.SOP.Strict.NS -import qualified Data.SOP.Telescope as Telescope +import Data.SOP.Strict.NP import Data.Text (Text) import qualified Data.Text as Text import qualified Data.Text.Encoding as Text @@ -239,7 +231,7 @@ data LedgerStateError = ApplyBlockHashMismatch Text -- ^ When using QuickValidation, the block hash did not match the expected -- block hash after applying a new block to the current ledger state. - | ApplyBlockError (Consensus.HardForkLedgerError (Consensus.CardanoEras Consensus.StandardCrypto)) + | ApplyBlockError (Consensus.CardanoLedgerError Consensus.StandardCrypto) -- ^ When using FullValidation, an error occurred when applying a new block -- to the current ledger state. | InvalidRollback @@ -252,7 +244,7 @@ data LedgerStateError | UnexpectedLedgerState AnyShelleyBasedEra -- ^ Expected era - (NS (Current Consensus.LedgerState) (Consensus.CardanoEras Consensus.StandardCrypto)) + (Consensus.CardanoLedgerState Consensus.StandardCrypto) -- ^ Ledgerstate from an unexpected era | ByronEraUnsupported | DebugError !String @@ -991,84 +983,42 @@ getAnyNewEpochState sbe (LedgerState ls) = getNewEpochState :: ShelleyBasedEra era - -> Consensus.LedgerState (HFC.HardForkBlock (Consensus.CardanoEras Consensus.StandardCrypto)) + -> Consensus.CardanoLedgerState Consensus.StandardCrypto -> Either LedgerStateError (ShelleyAPI.NewEpochState (ShelleyLedgerEra era)) getNewEpochState era x = do - let tip = Telescope.tip $ getHardForkState $ HFC.hardForkLedgerStatePerEra x - err = UnexpectedLedgerState (shelleyBasedEraConstraints era $ AnyShelleyBasedEra era) tip + let err = UnexpectedLedgerState (shelleyBasedEraConstraints era $ AnyShelleyBasedEra era) x case era of ShelleyBasedEraShelley -> - case tip of - ShelleyLedgerState shelleyCurrent -> - pure $ Shelley.shelleyLedgerState $ currentState shelleyCurrent + case x of + Consensus.LedgerStateShelley current -> + pure $ Shelley.shelleyLedgerState current _ -> Left err ShelleyBasedEraAllegra -> - case tip of - AllegraLedgerState allegraCurrent -> - pure $ Shelley.shelleyLedgerState $ currentState allegraCurrent + case x of + Consensus.LedgerStateAllegra current -> + pure $ Shelley.shelleyLedgerState current _ -> Left err ShelleyBasedEraMary -> - case tip of - MaryLedgerState maryCurrent -> - pure $ Shelley.shelleyLedgerState $ currentState maryCurrent + case x of + Consensus.LedgerStateMary current -> + pure $ Shelley.shelleyLedgerState current _ -> Left err ShelleyBasedEraAlonzo -> - case tip of - AlonzoLedgerState alonzoCurrent -> - pure $ Shelley.shelleyLedgerState $ currentState alonzoCurrent + case x of + Consensus.LedgerStateAlonzo current -> + pure $ Shelley.shelleyLedgerState current _ -> Left err ShelleyBasedEraBabbage -> - case tip of - BabbageLedgerState babbageCurrent -> - pure $ Shelley.shelleyLedgerState $ currentState babbageCurrent + case x of + Consensus.LedgerStateBabbage current -> + pure $ Shelley.shelleyLedgerState current _ -> Left err ShelleyBasedEraConway -> - case tip of - ConwayLedgerState conwayCurrent -> - pure $ Shelley.shelleyLedgerState $ currentState conwayCurrent + case x of + Consensus.LedgerStateConway current -> + pure $ Shelley.shelleyLedgerState current _ -> Left err -{-# COMPLETE ShelleyLedgerState, - AllegraLedgerState, - MaryLedgerState, - AlonzoLedgerState, - BabbageLedgerState, - ConwayLedgerState - #-} - -pattern ShelleyLedgerState - :: Current Consensus.LedgerState (Shelley.ShelleyBlock (TPraos.TPraos Consensus.StandardCrypto) (Shelley.ShelleyEra Consensus.StandardCrypto)) - -> NS (Current Consensus.LedgerState) (Consensus.CardanoEras Consensus.StandardCrypto) -pattern ShelleyLedgerState x = S (Z x) - -pattern AllegraLedgerState - :: Current Consensus.LedgerState (Shelley.ShelleyBlock (TPraos.TPraos Consensus.StandardCrypto) (Shelley.AllegraEra Consensus.StandardCrypto)) - -> NS (Current Consensus.LedgerState) (Consensus.CardanoEras Consensus.StandardCrypto) -pattern AllegraLedgerState x = S (S (Z x)) - -pattern MaryLedgerState - :: Current Consensus.LedgerState (Shelley.ShelleyBlock (TPraos.TPraos Consensus.StandardCrypto) (Shelley.MaryEra Consensus.StandardCrypto)) - -> NS (Current Consensus.LedgerState) (Consensus.CardanoEras Consensus.StandardCrypto) -pattern MaryLedgerState x = S (S (S (Z x))) - - -pattern AlonzoLedgerState - :: Current Consensus.LedgerState (Shelley.ShelleyBlock (TPraos.TPraos Consensus.StandardCrypto) (Shelley.AlonzoEra Consensus.StandardCrypto)) - -> NS (Current Consensus.LedgerState) (Consensus.CardanoEras Consensus.StandardCrypto) -pattern AlonzoLedgerState x = S (S (S (S (Z x)))) - - -pattern BabbageLedgerState - :: Current Consensus.LedgerState (Shelley.ShelleyBlock (Consensus.Praos Consensus.StandardCrypto) (Shelley.BabbageEra Consensus.StandardCrypto)) - -> NS (Current Consensus.LedgerState) (Consensus.CardanoEras Consensus.StandardCrypto) -pattern BabbageLedgerState x = S (S (S (S (S (Z x))))) - -pattern ConwayLedgerState - :: Current Consensus.LedgerState (Shelley.ShelleyBlock (Consensus.Praos Consensus.StandardCrypto) (Shelley.ConwayEra Consensus.StandardCrypto)) - -> NS (Current Consensus.LedgerState) (Consensus.CardanoEras Consensus.StandardCrypto) -pattern ConwayLedgerState x = S (S (S (S (S (S (Z x)))))) - - encodeLedgerState :: Consensus.CardanoCodecConfig Consensus.StandardCrypto -> LedgerState From 2b9ff2f48e79b25f611286d733cca37a72249d7a Mon Sep 17 00:00:00 2001 From: Javier Sagredo Date: Mon, 1 Apr 2024 12:41:07 +0200 Subject: [PATCH 08/10] Modify cabal file --- cardano-api/cardano-api.cabal | 1 - 1 file changed, 1 deletion(-) diff --git a/cardano-api/cardano-api.cabal b/cardano-api/cardano-api.cabal index 0c93289e45..9f80208782 100644 --- a/cardano-api/cardano-api.cabal +++ b/cardano-api/cardano-api.cabal @@ -211,7 +211,6 @@ library internal , serialise , small-steps ^>= 1.0 , sop-core - , sop-extras , stm , strict-sop-core , time From 244a150912abbd38e4bd6579db8c76be81478917 Mon Sep 17 00:00:00 2001 From: Javier Sagredo Date: Mon, 1 Apr 2024 15:14:32 +0200 Subject: [PATCH 09/10] Function gone removed from haddock --- cardano-api/internal/Cardano/Api/Eras/Core.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cardano-api/internal/Cardano/Api/Eras/Core.hs b/cardano-api/internal/Cardano/Api/Eras/Core.hs index d9212ceebc..908846d6e5 100644 --- a/cardano-api/internal/Cardano/Api/Eras/Core.hs +++ b/cardano-api/internal/Cardano/Api/Eras/Core.hs @@ -267,7 +267,7 @@ instance ToCardanoEra CardanoEra where -- | The class of Cardano eras. This allows uniform handling of all Cardano -- eras, but also non-uniform by making case distinctions on the 'CardanoEra' --- constructors, or the 'CardanoEraStyle' constructors via `cardanoEraStyle`. +-- constructors. -- class HasTypeProxy era => IsCardanoEra era where cardanoEra :: CardanoEra era From 2e42f5b4a9a3d1c4b90fd455fa9ef65e6201a22c Mon Sep 17 00:00:00 2001 From: Javier Sagredo Date: Wed, 3 Apr 2024 12:05:34 +0200 Subject: [PATCH 10/10] Bring back LedgerState patterns --- .../internal/Cardano/Api/LedgerState.hs | 63 ++++++++++++++++--- 1 file changed, 55 insertions(+), 8 deletions(-) diff --git a/cardano-api/internal/Cardano/Api/LedgerState.hs b/cardano-api/internal/Cardano/Api/LedgerState.hs index 5a6abee83c..00f8e4dd6d 100644 --- a/cardano-api/internal/Cardano/Api/LedgerState.hs +++ b/cardano-api/internal/Cardano/Api/LedgerState.hs @@ -16,14 +16,16 @@ module Cardano.Api.LedgerState ( -- * Initialization / Accumulation envSecurityParam - , LedgerState ( .. ) - , pattern Consensus.LedgerStateByron - , pattern Consensus.LedgerStateShelley - , pattern Consensus.LedgerStateAllegra - , pattern Consensus.LedgerStateMary - , pattern Consensus.LedgerStateAlonzo - , pattern Consensus.LedgerStateBabbage - , pattern Consensus.LedgerStateConway + , LedgerState + ( .. + , LedgerStateByron + , LedgerStateShelley + , LedgerStateAllegra + , LedgerStateMary + , LedgerStateAlonzo + , LedgerStateBabbage + , LedgerStateConway + ) , encodeLedgerState , decodeLedgerState @@ -142,6 +144,7 @@ import Cardano.Slotting.Slot (WithOrigin (At, Origin)) import qualified Cardano.Slotting.Slot as Slot import qualified Ouroboros.Consensus.Block.Abstract as Consensus import Ouroboros.Consensus.Block.Forging (BlockForging) +import qualified Ouroboros.Consensus.Byron.Ledger as Byron import qualified Ouroboros.Consensus.Cardano as Consensus import qualified Ouroboros.Consensus.Cardano.Block as Consensus import qualified Ouroboros.Consensus.Cardano.CanHardFork as Consensus @@ -157,6 +160,7 @@ import qualified Ouroboros.Consensus.Node.ProtocolInfo as Consensus import Ouroboros.Consensus.Protocol.Abstract (ChainDepState, ConsensusProtocol (..)) import qualified Ouroboros.Consensus.Protocol.Praos.Common as Consensus import Ouroboros.Consensus.Protocol.Praos.VRF (mkInputVRF, vrfLeaderValue) +import qualified Ouroboros.Consensus.Shelley.HFEras as Shelley import qualified Ouroboros.Consensus.Shelley.Ledger.Ledger as Shelley import Ouroboros.Consensus.Storage.Serialisation import Ouroboros.Consensus.TypeFamilyWrappers (WrapLedgerEvent (WrapLedgerEvent)) @@ -305,6 +309,49 @@ applyBlock applyBlock env oldState validationMode = applyBlock' env oldState validationMode . toConsensusBlock +pattern LedgerStateByron + :: Ledger.LedgerState Byron.ByronBlock + -> LedgerState +pattern LedgerStateByron st <- LedgerState (Consensus.LedgerStateByron st) + +pattern LedgerStateShelley + :: Ledger.LedgerState Shelley.StandardShelleyBlock + -> LedgerState +pattern LedgerStateShelley st <- LedgerState (Consensus.LedgerStateShelley st) + +pattern LedgerStateAllegra + :: Ledger.LedgerState Shelley.StandardAllegraBlock + -> LedgerState +pattern LedgerStateAllegra st <- LedgerState (Consensus.LedgerStateAllegra st) + +pattern LedgerStateMary + :: Ledger.LedgerState Shelley.StandardMaryBlock + -> LedgerState +pattern LedgerStateMary st <- LedgerState (Consensus.LedgerStateMary st) + +pattern LedgerStateAlonzo + :: Ledger.LedgerState Shelley.StandardAlonzoBlock + -> LedgerState +pattern LedgerStateAlonzo st <- LedgerState (Consensus.LedgerStateAlonzo st) + +pattern LedgerStateBabbage + :: Ledger.LedgerState Shelley.StandardBabbageBlock + -> LedgerState +pattern LedgerStateBabbage st <- LedgerState (Consensus.LedgerStateBabbage st) + +pattern LedgerStateConway + :: Ledger.LedgerState Shelley.StandardConwayBlock + -> LedgerState +pattern LedgerStateConway st <- LedgerState (Consensus.LedgerStateConway st) + +{-# COMPLETE LedgerStateByron + , LedgerStateShelley + , LedgerStateAllegra + , LedgerStateMary + , LedgerStateAlonzo + , LedgerStateBabbage + , LedgerStateConway #-} + data FoldBlocksError = FoldBlocksInitialLedgerStateError !InitialLedgerStateError | FoldBlocksApplyBlockError !LedgerStateError