-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This covers CAD-2147, and adds support for adding additional scripts to the metadata in a structured way.
- Loading branch information
Showing
9 changed files
with
193 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
shelley-ma/impl/src/Cardano/Ledger/ShelleyMA/Metadata.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE DerivingVia #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE KindSignatures #-} | ||
{-# LANGUAGE PatternSynonyms #-} | ||
{-# LANGUAGE StandaloneDeriving #-} | ||
{-# LANGUAGE TypeFamilies #-} | ||
{-# LANGUAGE UndecidableInstances #-} | ||
{-# OPTIONS_GHC -Wno-orphans #-} | ||
|
||
module Cardano.Ledger.ShelleyMA.Metadata | ||
( Metadata (..), | ||
pattern Metadata, | ||
) | ||
where | ||
|
||
import Cardano.Binary | ||
( FromCBOR (..), | ||
ToCBOR (..), | ||
) | ||
import Cardano.Crypto.Hash (hashWithSerialiser) | ||
import qualified Cardano.Ledger.Core as Core | ||
import Cardano.Ledger.Crypto (Crypto) | ||
import Cardano.Ledger.Era (Era) | ||
import Cardano.Ledger.ShelleyMA (MaryOrAllegra, ShelleyMAEra) | ||
import Cardano.Ledger.ShelleyMA.Scripts () | ||
import Control.DeepSeq (deepseq) | ||
import Data.Coders | ||
import Data.Map.Strict (Map) | ||
import Data.MemoBytes | ||
import Data.Sequence.Strict (StrictSeq) | ||
import Data.Typeable (Typeable) | ||
import Data.Word (Word64) | ||
import GHC.Generics (Generic) | ||
import NoThunks.Class | ||
import Shelley.Spec.Ledger.MetaData | ||
( MetaDataHash (..), | ||
MetaDatum, | ||
ValidateMetadata (..), | ||
validMetaDatum, | ||
) | ||
|
||
-- | Raw, un-memoised metadata type | ||
data MetadataRaw era = MetadataRaw | ||
{ mdScriptPreimages :: !(StrictSeq (Core.Script era)), | ||
-- | Unstructured metadata "blob" | ||
mdBlob :: !(Map Word64 MetaDatum) | ||
} | ||
deriving (Generic) | ||
|
||
deriving instance (Core.ChainData (Core.Script era)) => Eq (MetadataRaw era) | ||
|
||
deriving instance (Core.ChainData (Core.Script era)) => Show (MetadataRaw era) | ||
|
||
deriving instance | ||
(Core.ChainData (Core.Script era)) => | ||
NoThunks (MetadataRaw era) | ||
|
||
newtype Metadata era = MetadataWithBytes (MemoBytes (MetadataRaw era)) | ||
deriving (Typeable) | ||
deriving newtype (ToCBOR) | ||
|
||
deriving newtype instance | ||
(Era era, Core.ChainData (Core.Script era)) => | ||
Eq (Metadata era) | ||
|
||
deriving newtype instance | ||
(Era era, Core.ChainData (Core.Script era)) => | ||
Show (Metadata era) | ||
|
||
deriving newtype instance | ||
(Era era, Core.ChainData (Core.Script era)) => | ||
NoThunks (Metadata era) | ||
|
||
pattern Metadata :: | ||
( Core.AnnotatedData (Core.Script era), | ||
Ord (Core.Script era) | ||
) => | ||
StrictSeq (Core.Script era) -> | ||
Map Word64 MetaDatum -> | ||
Metadata era | ||
pattern Metadata sp blob <- | ||
MetadataWithBytes (Memo (MetadataRaw sp blob) _) | ||
where | ||
Metadata sp blob = | ||
MetadataWithBytes $ | ||
memoBytes | ||
(encMetadataRaw $ MetadataRaw sp blob) | ||
|
||
{-# COMPLETE Metadata #-} | ||
|
||
type instance | ||
Core.Metadata (ShelleyMAEra (ma :: MaryOrAllegra) c) = | ||
Metadata (ShelleyMAEra (ma :: MaryOrAllegra) c) | ||
|
||
instance | ||
( Crypto c, | ||
Typeable ma, | ||
Core.AnnotatedData (Core.Script (ShelleyMAEra ma c)) | ||
) => | ||
ValidateMetadata (ShelleyMAEra (ma :: MaryOrAllegra) c) | ||
where | ||
hashMetadata = MetaDataHash . hashWithSerialiser toCBOR | ||
|
||
validateMetadata (Metadata sp blob) = deepseq sp $ all validMetaDatum blob | ||
|
||
-------------------------------------------------------------------------------- | ||
-- Serialisation | ||
-------------------------------------------------------------------------------- | ||
|
||
-- | Encode Metadata | ||
encMetadataRaw :: | ||
(Core.AnnotatedData (Core.Script era)) => | ||
MetadataRaw era -> | ||
Encode ( 'Closed 'Dense) (MetadataRaw era) | ||
encMetadataRaw (MetadataRaw sp blob) = | ||
Rec MetadataRaw | ||
!> E encodeFoldable sp | ||
!> To blob | ||
|
||
instance | ||
(Era era, Core.AnnotatedData (Core.Script era)) => | ||
FromCBOR (Annotator (MetadataRaw era)) | ||
where | ||
fromCBOR = | ||
decode | ||
( Ann (RecD MetadataRaw) | ||
<*! D (sequence <$> decodeStrictSeq fromCBOR) | ||
<*! Ann From | ||
) | ||
|
||
deriving via | ||
(Mem (MetadataRaw era)) | ||
instance | ||
( Era era, | ||
Core.AnnotatedData (Core.Script era) | ||
) => | ||
FromCBOR (Annotator (Metadata era)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters