-
Notifications
You must be signed in to change notification settings - Fork 176
decodeSparseKeyed for more types #5847
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
base: master
Are you sure you want to change the base?
Changes from all commits
c0b91de
5ca36f3
6438b66
50d2be4
11d1447
45e4a2e
da6c95e
9260354
2a030c2
b6277b5
4d13e21
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,3 +1,4 @@ | ||||||||||||||||||
| {-# LANGUAGE BangPatterns #-} | ||||||||||||||||||
| {-# LANGUAGE DataKinds #-} | ||||||||||||||||||
| {-# LANGUAGE DeriveGeneric #-} | ||||||||||||||||||
| {-# LANGUAGE DerivingVia #-} | ||||||||||||||||||
|
|
@@ -49,6 +50,7 @@ module Cardano.Ledger.Alonzo.TxAuxData ( | |||||||||||||||||
| atadPlutus', | ||||||||||||||||||
| ) where | ||||||||||||||||||
|
|
||||||||||||||||||
| import Cardano.Base.Typeable (TypeName (TypeName)) | ||||||||||||||||||
| import Cardano.Ledger.Allegra.TxAuxData (AllegraEraTxAuxData (..)) | ||||||||||||||||||
| import Cardano.Ledger.Alonzo.Era | ||||||||||||||||||
| import Cardano.Ledger.Alonzo.Scripts ( | ||||||||||||||||||
|
|
@@ -67,6 +69,8 @@ import Cardano.Ledger.Binary ( | |||||||||||||||||
| EncCBOR (..), | ||||||||||||||||||
| ToCBOR, | ||||||||||||||||||
| TokenType (..), | ||||||||||||||||||
| assertTag, | ||||||||||||||||||
| decodeSparseKeyed, | ||||||||||||||||||
| decodeStrictSeq, | ||||||||||||||||||
| ifDecoderVersionAtLeast, | ||||||||||||||||||
| natVersion, | ||||||||||||||||||
|
|
@@ -93,9 +97,10 @@ import qualified Data.List.NonEmpty as NE | |||||||||||||||||
| import Data.Map (Map) | ||||||||||||||||||
| import qualified Data.Map.Strict as Map | ||||||||||||||||||
| import Data.Maybe (isNothing, mapMaybe) | ||||||||||||||||||
| import Data.Proxy (Proxy (..)) | ||||||||||||||||||
| import Data.Sequence.Strict (StrictSeq ((:<|))) | ||||||||||||||||||
| import qualified Data.Sequence.Strict as StrictSeq | ||||||||||||||||||
| import Data.Typeable (Typeable) | ||||||||||||||||||
| import Data.Typeable (Typeable, typeRep) | ||||||||||||||||||
| import Data.Word (Word64) | ||||||||||||||||||
| import GHC.Generics (Generic) | ||||||||||||||||||
| import GHC.Stack | ||||||||||||||||||
|
|
@@ -202,8 +207,20 @@ instance | |||||||||||||||||
| decodeTxAuxDataByTokenType @(Annotator (AlonzoTxAuxDataRaw era)) | ||||||||||||||||||
| decodeShelley | ||||||||||||||||||
|
aniketd marked this conversation as resolved.
|
||||||||||||||||||
| (ifDecoderVersionAtLeast (natVersion @12) decodeDijkstra decodeAllegra) | ||||||||||||||||||
| decodeAlonzo | ||||||||||||||||||
| ( ifDecoderVersionAtLeast | ||||||||||||||||||
| (natVersion @12) | ||||||||||||||||||
| ( do | ||||||||||||||||||
| assertTag 259 | ||||||||||||||||||
| decodeSparseKeyed | ||||||||||||||||||
| TypeName | ||||||||||||||||||
| [] | ||||||||||||||||||
| (pure emptyAlonzoTxAuxDataRaw) | ||||||||||||||||||
| decoderByKey | ||||||||||||||||||
| ) | ||||||||||||||||||
| decodeAlonzo | ||||||||||||||||||
| ) | ||||||||||||||||||
| where | ||||||||||||||||||
| name = show . typeRep $ Proxy @(AlonzoTxAuxDataRaw era) | ||||||||||||||||||
| decodeShelley = | ||||||||||||||||||
| decode | ||||||||||||||||||
| ( Ann (Emit AlonzoTxAuxDataRaw) | ||||||||||||||||||
|
|
@@ -227,7 +244,34 @@ instance | |||||||||||||||||
| decodeAlonzo = | ||||||||||||||||||
| decode $ | ||||||||||||||||||
| TagD 259 $ | ||||||||||||||||||
| SparseKeyed "AlonzoTxAuxData" (pure emptyAlonzoTxAuxDataRaw) auxDataField [] | ||||||||||||||||||
| SparseKeyed name (pure emptyAlonzoTxAuxDataRaw) auxDataField [] | ||||||||||||||||||
|
|
||||||||||||||||||
| decoderByKey :: | ||||||||||||||||||
| Annotator (AlonzoTxAuxDataRaw era) -> | ||||||||||||||||||
| Word -> | ||||||||||||||||||
| Maybe (Decoder s (Annotator (AlonzoTxAuxDataRaw era))) | ||||||||||||||||||
| decoderByKey acc = \case | ||||||||||||||||||
| 0 -> Just $ do | ||||||||||||||||||
| !x <- decCBOR | ||||||||||||||||||
| pure $ (\ad -> ad {atadrMetadata = x}) <$> acc | ||||||||||||||||||
| 1 -> Just $ do | ||||||||||||||||||
| !x <- sequence <$> decodeStrictSeq decCBOR | ||||||||||||||||||
| pure $ | ||||||||||||||||||
| (\scripts ad -> ad {atadrNativeScripts = atadrNativeScripts ad <> scripts}) | ||||||||||||||||||
| <$> x | ||||||||||||||||||
| <*> acc | ||||||||||||||||||
| 2 -> decodeAddPlutus PlutusV1 acc | ||||||||||||||||||
| 3 -> decodeAddPlutus PlutusV2 acc | ||||||||||||||||||
| 4 -> decodeAddPlutus PlutusV3 acc | ||||||||||||||||||
| 5 -> decodeAddPlutus PlutusV4 acc | ||||||||||||||||||
| _ -> Nothing | ||||||||||||||||||
| where | ||||||||||||||||||
| decodeAddPlutus lang accu = Just $ do | ||||||||||||||||||
| guardPlutus lang | ||||||||||||||||||
| !x <- decCBOR | ||||||||||||||||||
| pure $ addPlutusScripts lang x <$> accu | ||||||||||||||||||
|
Comment on lines
+269
to
+272
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need to pass
Suggested change
|
||||||||||||||||||
| {-# INLINE decodeAddPlutus #-} | ||||||||||||||||||
| {-# INLINE decoderByKey #-} | ||||||||||||||||||
|
|
||||||||||||||||||
| auxDataField :: Word -> Field (Annotator (AlonzoTxAuxDataRaw era)) | ||||||||||||||||||
| auxDataField 0 = fieldA (\x ad -> ad {atadrMetadata = x}) From | ||||||||||||||||||
|
|
||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -15,16 +15,18 @@ module Test.Cardano.Ledger.Alonzo.Binary.Golden ( | |||||
| ) where | ||||||
|
|
||||||
| import Cardano.Ledger.Alonzo.Core (EraTxWits (..), ShelleyEraTxCert) | ||||||
| import Cardano.Ledger.Alonzo.TxWits (AlonzoTxWitsRaw) | ||||||
| import Cardano.Ledger.Binary ( | ||||||
| Annotator, | ||||||
| DecoderError (..), | ||||||
| DeserialiseFailure (..), | ||||||
| Tokens (..), | ||||||
| Version, | ||||||
| natVersion, | ||||||
| ) | ||||||
| import qualified Cardano.Ledger.Binary as Binary | ||||||
| import Cardano.Ledger.MemoBytes (EqRaw (..)) | ||||||
| import Data.Data (Proxy (..)) | ||||||
| import Data.Data (Proxy (..), typeRep) | ||||||
| import Data.Void (Void) | ||||||
| import Test.Cardano.Ledger.Allegra.Binary.Golden hiding (spec) | ||||||
| import Test.Cardano.Ledger.Alonzo.Era (AlonzoEraTest) | ||||||
|
|
@@ -77,13 +79,16 @@ txWitsDecodingFailsOnInvalidField version validFields = | |||||
| DecoderErrorDeserialiseFailure | ||||||
| lbl | ||||||
| ( DeserialiseFailure (if n >= 24 then 3 else 2) $ | ||||||
| -- TODO fix the `occured` typo in the produced value | ||||||
| "An error occured while decoding (Int,Void) not a valid key:.\nError: " <> show n | ||||||
| if version >= natVersion @12 | ||||||
| then typeName <> ":Unknown field key " <> show n | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMHO an error message like this "Type:Unknown .." looks ugly. Why don't you add a space after
Suggested change
|
||||||
| -- TODO fix the `occured` typo in the produced value | ||||||
| else "An error occured while decoding (Int,Void) not a valid key:.\nError: " <> show n | ||||||
| ) | ||||||
| else | ||||||
| DecoderErrorDeserialiseFailure lbl (DeserialiseFailure 1 "expected word") | ||||||
| where | ||||||
| lbl = Binary.label $ Proxy @(Annotator (TxWits era)) | ||||||
| typeName = show (typeRep (Proxy @(Annotator (AlonzoTxWitsRaw era)))) | ||||||
|
|
||||||
| spec :: | ||||||
| forall era. | ||||||
|
|
||||||
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.
We can't merge PR's with SRPs in them.
cardano-basehas already been released on CHaP, so please update index-state and use the functionality that you introduced in IntersectMBO/cardano-base#660