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

Simplify DebugLedgerState with eons #296

Merged
merged 1 commit into from
Oct 5, 2023
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
3 changes: 0 additions & 3 deletions cardano-api/internal/Cardano/Api/Eras/Constraints.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import Cardano.Api.Eon.ShelleyBasedEra
import Cardano.Api.Eras.Core
import Cardano.Api.Modes
import Cardano.Api.Orphans ()
import Cardano.Api.Query.Types

import qualified Cardano.Crypto.Hash.Blake2b as Blake2b
import qualified Cardano.Crypto.Hash.Class as C
Expand Down Expand Up @@ -72,11 +71,9 @@ type ShelleyBasedEraConstraints era =
, L.ShelleyEraTxBody (ShelleyLedgerEra era)
, L.ShelleyEraTxCert (ShelleyLedgerEra era)
, FromCBOR (Consensus.ChainDepState (ConsensusProtocol era))
, FromCBOR (DebugLedgerState era)
, IsCardanoEra era
, IsShelleyBasedEra era
, ToJSON (Consensus.ChainDepState (ConsensusProtocol era))
, ToJSON (DebugLedgerState era)
, Typeable era
)

Expand Down
71 changes: 28 additions & 43 deletions cardano-api/internal/Cardano/Api/Query/Types.hs
Original file line number Diff line number Diff line change
@@ -1,63 +1,48 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE TypeApplications #-}

module Cardano.Api.Query.Types
( DebugLedgerState(..)
, toDebugLedgerStatePair
) where

import Cardano.Api.Eon.ShelleyBasedEra
import Cardano.Api.Eras.Constraints
import Cardano.Api.Orphans ()

import Cardano.Binary
import Cardano.Ledger.Binary
import qualified Cardano.Ledger.Binary.Plain as Plain
import qualified Cardano.Ledger.Shelley.API as Shelley
import qualified Cardano.Ledger.Shelley.Core as Core
import qualified Cardano.Ledger.Shelley.LedgerState as Shelley
import qualified Ouroboros.Consensus.Cardano.Block as Consensus

import Data.Aeson (ToJSON (..), object, (.=))
import qualified Data.Aeson as Aeson
import Data.Typeable

data DebugLedgerState era where
DebugLedgerState ::
( ShelleyLedgerEra era ~ ledgerera
)
=> Shelley.NewEpochState ledgerera
-> DebugLedgerState era

instance
( Typeable era
, Core.EraTxOut (ShelleyLedgerEra era)
, Core.EraGov (ShelleyLedgerEra era)
, DecCBOR (Shelley.StashedAVVMAddresses (ShelleyLedgerEra era))
) => FromCBOR (DebugLedgerState era) where
fromCBOR = DebugLedgerState <$>
(fromCBOR :: Plain.Decoder s (Shelley.NewEpochState (ShelleyLedgerEra era)))

-- TODO: Shelley based era class!
instance ( IsShelleyBasedEra era
, ShelleyLedgerEra era ~ ledgerera
, Consensus.ShelleyBasedEra ledgerera
) => ToJSON (DebugLedgerState era) where
toJSON = object . toDebugLedgerStatePair
toEncoding = Aeson.pairs . mconcat . toDebugLedgerStatePair

toDebugLedgerStatePair ::
( ShelleyLedgerEra era ~ ledgerera
, Consensus.ShelleyBasedEra ledgerera
, Aeson.KeyValue a
) => DebugLedgerState era -> [a]
toDebugLedgerStatePair (DebugLedgerState newEpochS) =
newtype DebugLedgerState era = DebugLedgerState
{ unDebugLedgerState :: Shelley.NewEpochState (ShelleyLedgerEra era)
}

instance IsShelleyBasedEra era => FromCBOR (DebugLedgerState era) where
fromCBOR =
shelleyBasedEraConstraints (shelleyBasedEra @era) $
DebugLedgerState <$>
(fromCBOR :: Plain.Decoder s (Shelley.NewEpochState (ShelleyLedgerEra era)))

instance IsShelleyBasedEra era => ToJSON (DebugLedgerState era) where
toJSON =
let sbe = shelleyBasedEra @era in
shelleyBasedEraConstraints sbe $ object . toDebugLedgerStatePair sbe
toEncoding =
let sbe = shelleyBasedEra @era in
shelleyBasedEraConstraints sbe $ Aeson.pairs . mconcat . toDebugLedgerStatePair sbe

toDebugLedgerStatePair :: ()
=> Aeson.KeyValue a
=> ShelleyBasedEra era
-> DebugLedgerState era
-> [a]
toDebugLedgerStatePair sbe (DebugLedgerState newEpochS) =
shelleyBasedEraConstraints sbe $
let !nesEL = Shelley.nesEL newEpochS
!nesBprev = Shelley.nesBprev newEpochS
!nesBcur = Shelley.nesBcur newEpochS
Expand Down