-
Notifications
You must be signed in to change notification settings - Fork 156
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
CAD-2147: Support additional scripts in structured metadata. #1993
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
157 changes: 157 additions & 0 deletions
157
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,157 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DeriveAnyClass #-} | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE DerivingVia #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE FlexibleInstances #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
{-# LANGUAGE KindSignatures #-} | ||
{-# LANGUAGE LambdaCase #-} | ||
{-# 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 (..), peekTokenType) | ||
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 Codec.CBOR.Decoding (TokenType (TypeListLen, TypeMapLen)) | ||
import Control.DeepSeq (deepseq) | ||
import Data.Coders | ||
import Data.Map.Strict (Map) | ||
import Data.MemoBytes | ||
import Data.Sequence.Strict (StrictSeq) | ||
import qualified Data.Sequence.Strict as 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, | ||
) | ||
import Shelley.Spec.Ledger.Serialization (mapFromCBOR, mapToCBOR) | ||
|
||
-- | Raw, un-memoised metadata type | ||
data MetadataRaw era = MetadataRaw | ||
{ -- | Unstructured metadata "blob" | ||
mdBlob :: !(Map Word64 MetaDatum), | ||
-- | Pre-images of script hashes found within the TxBody, but which are not | ||
-- required as witnesses. Examples include: | ||
-- - Token policy IDs appearing in transaction outputs | ||
-- - Pool reward account registrations | ||
mdScriptPreimages :: !(StrictSeq (Core.Script era)) | ||
} | ||
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 (Generic, 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) | ||
) => | ||
Map Word64 MetaDatum -> | ||
StrictSeq (Core.Script era) -> | ||
Metadata era | ||
pattern Metadata blob sp <- | ||
MetadataWithBytes (Memo (MetadataRaw blob sp) _) | ||
where | ||
Metadata blob sp = | ||
MetadataWithBytes $ | ||
memoBytes | ||
(encMetadataRaw $ MetadataRaw blob sp) | ||
|
||
{-# 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 blob sp) = deepseq sp $ all validMetaDatum blob | ||
|
||
-------------------------------------------------------------------------------- | ||
-- Serialisation | ||
-------------------------------------------------------------------------------- | ||
|
||
-- | Encode Metadata | ||
encMetadataRaw :: | ||
(Core.AnnotatedData (Core.Script era)) => | ||
MetadataRaw era -> | ||
Encode ( 'Closed 'Dense) (MetadataRaw era) | ||
encMetadataRaw (MetadataRaw blob sp) = | ||
Rec MetadataRaw | ||
!> E mapToCBOR blob | ||
!> E encodeFoldable sp | ||
|
||
instance | ||
(Era era, Core.AnnotatedData (Core.Script era)) => | ||
FromCBOR (Annotator (MetadataRaw era)) | ||
where | ||
fromCBOR = | ||
peekTokenType >>= \case | ||
TypeMapLen -> | ||
decode | ||
( Ann (Emit MetadataRaw) | ||
<*! Ann (D mapFromCBOR) | ||
<*! Ann (Emit StrictSeq.empty) | ||
) | ||
TypeListLen -> | ||
decode | ||
( Ann (RecD MetadataRaw) | ||
<*! Ann (D mapFromCBOR) | ||
<*! D (sequence <$> decodeStrictSeq fromCBOR) | ||
) | ||
_ -> error "Failed to decode Metadata" | ||
|
||
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you're changing it to a dense encoding then the labels
0:
and1:
are just informative rather than part of the encoding. IIRC some code generators emit these as names. So these could be more descriptive.