Skip to content

Commit

Permalink
Introduce ChainData, SerialisableData, and AnnotatedData
Browse files Browse the repository at this point in the history
These constraint synonyms make the contexts of the API classes much more
readable.
  • Loading branch information
mrBliss committed Oct 21, 2020
1 parent 52eb635 commit 40610c5
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 36 deletions.
Expand Up @@ -17,8 +17,9 @@ module Cardano.Ledger.API.Mempool
)
where

import Cardano.Binary (Annotator, FromCBOR (..), ToCBOR (..))
import Cardano.Binary (FromCBOR (..), ToCBOR (..))
import Cardano.Crypto.Hash (Hash)
import Cardano.Ledger.Core (AnnotatedData, SerialisableData, ChainData)
import Cardano.Ledger.Crypto (Crypto, HASH)
import Cardano.Ledger.Shelley (ShelleyBased, ShelleyEra)
import Control.Arrow (left)
Expand All @@ -32,7 +33,6 @@ import Control.State.Transition.Extended
)
import Data.Sequence (Seq)
import Data.Typeable (Typeable)
import NoThunks.Class (NoThunks)
import Shelley.Spec.Ledger.BaseTypes (Globals)
import Shelley.Spec.Ledger.Keys (DSignable)
import Shelley.Spec.Ledger.LedgerState (NewEpochState)
Expand All @@ -45,16 +45,12 @@ import Shelley.Spec.Ledger.TxBody (EraIndependentTxBody)

-- TODO #1304: add reapplyTxs
class
( Eq (Tx era),
Show (Tx era),
NoThunks (Tx era),
FromCBOR (Annotator (Tx era)),
ToCBOR (Tx era),
( ChainData (Tx era),
AnnotatedData (Tx era),
Eq (ApplyTxError era),
Show (ApplyTxError era),
FromCBOR (ApplyTxError era),
ToCBOR (ApplyTxError era),
Typeable (ApplyTxError era)
Typeable (ApplyTxError era),
SerialisableData (ApplyTxError era)
) =>
ApplyTx era
where
Expand Down
Expand Up @@ -30,6 +30,7 @@ module Cardano.Ledger.API.Protocol
where

import Cardano.Binary (FromCBOR (..), ToCBOR (..), encodeListLen)
import Cardano.Ledger.Core (SerialisableData, ChainData)
import qualified Cardano.Ledger.Crypto as CC (Crypto)
import Cardano.Ledger.Era (Crypto)
import Cardano.Ledger.Shelley (ShelleyBased, ShelleyEra)
Expand Down Expand Up @@ -77,9 +78,8 @@ import Shelley.Spec.Ledger.Serialization (decodeRecordNamed)
import Shelley.Spec.Ledger.Slot (SlotNo)

class
( Eq (ChainDepState (Crypto era)),
Show (ChainDepState (Crypto era)),
NoThunks (ChainDepState (Crypto era)),
( ChainData (ChainDepState (Crypto era)),
SerialisableData (ChainDepState (Crypto era)),
Eq (ChainTransitionError (Crypto era)),
Show (ChainTransitionError (Crypto era)),
Show (LedgerView (Crypto era)),
Expand Down
Expand Up @@ -19,8 +19,8 @@ module Cardano.Ledger.API.Validation
)
where

import Cardano.Binary (Annotator, FromCBOR (..), ToCBOR (..))
import Cardano.Crypto.Hash (Hash)
import Cardano.Ledger.Core (ChainData, SerialisableData, AnnotatedData)
import Cardano.Ledger.Crypto (HASH)
import qualified Cardano.Ledger.Crypto as CC (Crypto)
import Cardano.Ledger.Era (Crypto, Era)
Expand All @@ -47,27 +47,14 @@ import Shelley.Spec.Ledger.TxBody (EraIndependentTxBody)
-------------------------------------------------------------------------------}

class
( Eq (Block era),
Show (Block era),
NoThunks (Block era),
FromCBOR (Annotator (Block era)),
ToCBOR (Block era),
Eq (BHeader (Crypto era)),
Show (BHeader (Crypto era)),
NoThunks (BHeader (Crypto era)),
FromCBOR (Annotator (BHeader (Crypto era))),
ToCBOR (Block era),
Eq (NewEpochState era),
Show (NewEpochState era),
NoThunks (NewEpochState era),
FromCBOR (NewEpochState era),
ToCBOR (NewEpochState era),
Eq (BlockTransitionError era),
Show (BlockTransitionError era),
NoThunks (BlockTransitionError era),
Eq (STS.PredicateFailure (STS.CHAIN era)),
Show (STS.PredicateFailure (STS.CHAIN era)),
NoThunks (STS.PredicateFailure (STS.CHAIN era))
( ChainData (Block era),
AnnotatedData (Block era),
ChainData (BHeader (Crypto era)),
AnnotatedData (BHeader (Crypto era)),
ChainData (NewEpochState era),
SerialisableData (NewEpochState era),
ChainData (BlockTransitionError era),
ChainData (STS.PredicateFailure (STS.CHAIN era))
) =>
ApplyBlock era
where
Expand Down
@@ -1,3 +1,4 @@
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE TypeFamilies #-}
Expand All @@ -16,12 +17,17 @@ module Cardano.Ledger.Core
Compact (..),
TxBody,
Value,
-- * Constraint synonyms
ChainData,
SerialisableData,
AnnotatedData,
)
where

import Cardano.Binary (FromCBOR (..), ToCBOR (..))
import Cardano.Binary (FromCBOR (..), ToCBOR (..), Annotator)
import Data.Kind (Type)
import Data.Typeable (Typeable)
import NoThunks.Class (NoThunks)

-- | A value is something which quantifies a transaction output.
type family Value era :: Type
Expand Down Expand Up @@ -61,3 +67,19 @@ instance
-- TODO: consider if this is better the other way around
instance (Eq a, Compactible a) => Eq (CompactForm a) where
a == b = fromCompact a == fromCompact b

-------------------------------------------------------------------------------
-- * Constraint synonyms
-------------------------------------------------------------------------------

-- | Common constraints
--
-- NOTE: 'Ord' is not included, as 'Ord' for a 'Block' or a 'NewEpochState'
-- doesn't make sense.
type ChainData t = (Eq t, Show t, NoThunks t, Typeable t)

-- | Constraints for serialising from/to CBOR
type SerialisableData t = (FromCBOR t, ToCBOR t)

-- | Constraints for serialising from/to CBOR using 'Annotator'
type AnnotatedData t = (FromCBOR (Annotator t), ToCBOR t)

0 comments on commit 40610c5

Please sign in to comment.