Skip to content
Open
7 changes: 7 additions & 0 deletions cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ repository cardano-haskell-packages
c00aae8461a256275598500ea0e187588c35a5d5d7454fb57eac18d9edb86a56
d4a35cd3121aa00d18544bb0ac01c3e1691d618f462c46129271bccf39f7e8ee

source-repository-package
type: git
location: https://github.com/IntersectMBO/cardano-base.git
subdir: cardano-base
--sha256: sha256-Gj8BPEew+UYkCWr3D0LgA6dvhYof49V20XRMRMf1FzY=
tag: efe33cb3739f5c90b733b730bdeffa11a75f291d

Comment on lines +13 to +19
Copy link
Copy Markdown
Collaborator

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-base has already been released on CHaP, so please update index-state and use the functionality that you introduced in IntersectMBO/cardano-base#660

Suggested change
source-repository-package
type: git
location: https://github.com/IntersectMBO/cardano-base.git
subdir: cardano-base
--sha256: sha256-Gj8BPEew+UYkCWr3D0LgA6dvhYof49V20XRMRMf1FzY=
tag: efe33cb3739f5c90b733b730bdeffa11a75f291d

source-repository-package
type: git
location: https://github.com/IntersectMBO/formal-ledger-specifications.git
Expand Down
50 changes: 47 additions & 3 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxAuxData.hs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
Expand Down Expand Up @@ -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 (
Expand All @@ -67,6 +69,8 @@ import Cardano.Ledger.Binary (
EncCBOR (..),
ToCBOR,
TokenType (..),
assertTag,
decodeSparseKeyed,
decodeStrictSeq,
ifDecoderVersionAtLeast,
natVersion,
Expand All @@ -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
Expand Down Expand Up @@ -202,8 +207,20 @@ instance
decodeTxAuxDataByTokenType @(Annotator (AlonzoTxAuxDataRaw era))
decodeShelley
Comment thread
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)
Expand All @@ -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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need to pass acc as an argument if it is already available in scope?

Suggested change
decodeAddPlutus lang accu = Just $ do
guardPlutus lang
!x <- decCBOR
pure $ addPlutusScripts lang x <$> accu
decodeAddPlutus lang = Just $ do
guardPlutus lang
!x <- decCBOR
pure $ addPlutusScripts lang x <$> acc

{-# INLINE decodeAddPlutus #-}
{-# INLINE decoderByKey #-}

auxDataField :: Word -> Field (Annotator (AlonzoTxAuxDataRaw era))
auxDataField 0 = fieldA (\x ad -> ad {atadrMetadata = x}) From
Expand Down
58 changes: 46 additions & 12 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/TxWits.hs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ module Cardano.Ledger.Alonzo.TxWits (
) where

import Cardano.Base.Proxy (asProxy)
import Cardano.Base.Typeable (TypeName (TypeName))
import Cardano.Ledger.Alonzo.Era (AlonzoEra)
import Cardano.Ledger.Alonzo.Scripts (
AlonzoEraScript (..),
Expand All @@ -75,6 +76,7 @@ import Cardano.Ledger.Binary (
ToCBOR (..),
TokenType (..),
allowTag,
decodeAccA,
decodeList,
decodeListLenOrIndef,
decodeListLikeWithCount,
Expand All @@ -83,6 +85,7 @@ import Cardano.Ledger.Binary (
decodeNonEmptyList,
decodeNonEmptySetLikeEnforceNoDuplicates,
decodeNonEmptySetLikeEnforceNoDuplicatesAnn,
decodeSparseKeyed,
encodeFoldableEncoder,
encodeListLen,
encodeTag,
Expand Down Expand Up @@ -129,8 +132,10 @@ import qualified Data.Map.Strict as Map
import Data.MapExtras (fromElems)
import qualified Data.MapExtras as Map (fromElems)
import Data.Maybe (mapMaybe)
import Data.Proxy (Proxy (..))
import Data.Set (Set)
import qualified Data.Set as Set
import Data.Typeable (typeRep)
import GHC.Generics (Generic)
import Lens.Micro
import NoThunks.Class (NoThunks)
Expand Down Expand Up @@ -601,13 +606,12 @@ instance
DecCBOR (Annotator (AlonzoTxWitsRaw era))
where
decCBOR =
decode $
SparseKeyed
"AlonzoTxWits"
(pure emptyTxWitsRaw)
txWitnessField
[]
ifDecoderVersionAtLeast
(natVersion @12)
(decodeSparseKeyed TypeName [] (pure emptyTxWitsRaw) decoderByKey)
(decode $ SparseKeyed name (pure emptyTxWitsRaw) txWitnessField [])
where
name = show . typeRep $ Proxy @(AlonzoTxWitsRaw era)
addrWitsSetDecoder :: (Ord a, DecCBOR a) => Decoder s (Set a)
addrWitsSetDecoder = do
let
Expand All @@ -621,6 +625,30 @@ instance
ifDecoderVersionAtLeast (natVersion @12) nonEmptyNoDuplicatesDecoder nonEmptyDecoder
{-# INLINE addrWitsSetDecoder #-}

setOrListWitsDecoder :: (Ord a, DecCBOR a) => Decoder s (Set a)
setOrListWitsDecoder =
ifDecoderVersionAtLeast
(natVersion @9)
addrWitsSetDecoder
(Set.fromList <$> decodeList decCBOR)
{-# INLINE setOrListWitsDecoder #-}

decoderByKey ::
Annotator (AlonzoTxWitsRaw era) ->
Word ->
Maybe (Decoder s (Annotator (AlonzoTxWitsRaw era)))
decoderByKey acc = \case
0 -> Just $ decodeAccA acc (\x w -> w {atwrAddrTxWits = x}) (pure <$> setOrListWitsDecoder)
1 -> Just $ decodeAccA acc addScriptsTxWitsRaw nativeScriptsDecoder
2 -> Just $ decodeAccA acc (\x w -> w {atwrBootAddrTxWits = x}) (pure <$> setOrListWitsDecoder)
3 -> Just $ decodeAccA acc addScriptsTxWitsRaw (pure <$> alonzoPlutusScriptDecoder SPlutusV1)
4 -> Just $ decodeAccA acc (\x w -> w {atwrDatsTxWits = x}) decCBOR
5 -> Just $ decodeAccA acc (\x w -> w {atwrRdmrsTxWits = x}) decCBOR
6 -> Just $ decodeAccA acc addScriptsTxWitsRaw (pure <$> alonzoPlutusScriptDecoder SPlutusV2)
7 -> Just $ decodeAccA acc addScriptsTxWitsRaw (pure <$> alonzoPlutusScriptDecoder SPlutusV3)
_ -> Nothing
{-# INLINE decoderByKey #-}

txWitnessField :: Word -> Field (Annotator (AlonzoTxWitsRaw era))
txWitnessField 0 =
fieldA
Expand Down Expand Up @@ -698,14 +726,20 @@ decodeAlonzoPlutusScript ::
(AlonzoEraScript era, PlutusLanguage l) =>
SLanguage l ->
Decode (Closed Dense) (Map ScriptHash (Script era))
decodeAlonzoPlutusScript slang =
D $
ifDecoderVersionAtLeast
(natVersion @9)
(scriptDecoderV9 (fromPlutusScript <$> decodePlutusScript slang))
(scriptDecoder (fromPlutusScript <$> decodePlutusScript slang))
decodeAlonzoPlutusScript slang = D (alonzoPlutusScriptDecoder slang)
{-# INLINE decodeAlonzoPlutusScript #-}

alonzoPlutusScriptDecoder ::
(AlonzoEraScript era, PlutusLanguage l) =>
SLanguage l ->
Decoder s (Map ScriptHash (Script era))
alonzoPlutusScriptDecoder slang =
ifDecoderVersionAtLeast
(natVersion @9)
(scriptDecoderV9 (fromPlutusScript <$> decodePlutusScript slang))
(scriptDecoder (fromPlutusScript <$> decodePlutusScript slang))
{-# INLINE alonzoPlutusScriptDecoder #-}

scriptDecoderV9 ::
EraScript era =>
Decoder s (Script era) ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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 :? I've noticed the same in your other PRs, but failed to mention it as a comment before.

Suggested change
then typeName <> ":Unknown field key " <> show n
then typeName <> ": Unknown field key " <> show n

-- 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.
Expand Down
5 changes: 3 additions & 2 deletions eras/babbage/impl/cardano-ledger-babbage.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,11 @@ library
aeson >=2.2,
base >=4.18 && <5,
bytestring,
cardano-base >=0.1.4,
cardano-data >=1.2,
cardano-ledger-allegra ^>=1.10,
cardano-ledger-alonzo ^>=1.16,
cardano-ledger-binary >=1.6,
cardano-ledger-binary >=1.9,
cardano-ledger-core:{cardano-ledger-core, internal} >=1.20,
cardano-ledger-mary ^>=1.11,
cardano-ledger-shelley ^>=1.19,
Expand Down Expand Up @@ -146,7 +147,7 @@ executable generate-cddl

build-depends:
base,
cardano-ledger-binary:testlib >=1.5,
cardano-ledger-binary:testlib >=1.9,
cddl,

library testlib
Expand Down
30 changes: 16 additions & 14 deletions eras/babbage/impl/src/Cardano/Ledger/Babbage/TxOut.hs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ module Cardano.Ledger.Babbage.TxOut (
internBabbageTxOut,
) where

import Cardano.Base.Typeable (TypeName (TypeName))
import Cardano.Ledger.Address (
CompactAddr,
compactAddr,
Expand Down Expand Up @@ -116,7 +117,7 @@ import qualified Data.ByteString.Lazy as LBS
import Data.Maybe (fromMaybe)
import Data.MemPack
import qualified Data.Text as T
import Data.Typeable (Proxy (..))
import Data.Typeable (Proxy (..), typeRep)
import GHC.Generics (Generic)
import GHC.Stack (HasCallStack)
import Lens.Micro (Lens', lens, to, (^.))
Expand Down Expand Up @@ -610,48 +611,49 @@ decodeTxOut decAddr = do
dtxo <-
ifDecoderVersionAtLeast
(natVersion @12)
(decodeSparseKeyed "TxOut" requiredFields initial decoderForKey)
(decode $ SparseKeyed "TxOut" initial bodyFields requiredFields)
(decodeSparseKeyed TypeName requiredFields initial decoderByKey)
(decode $ SparseKeyed name initial bodyFields requiredFields)
case dtxo of
DecodingTxOut SNothing _ _ _ ->
cborError $ DecoderErrorCustom "BabbageTxOut" "Impossible: no Addr"
DecodingTxOut (SJust (addr, cAddr)) val d script ->
pure $ mkTxOut addr cAddr val d script
where
name = show . typeRep $ Proxy @(BabbageTxOut era)
initial :: DecodingTxOut era
initial = DecodingTxOut SNothing mempty NoDatum SNothing
decoderForKey :: DecodingTxOut era -> Word -> Maybe (Decoder s (DecodingTxOut era))
decoderForKey txo = \case
decoderByKey :: DecodingTxOut era -> Word -> Maybe (Decoder s (DecodingTxOut era))
decoderByKey txOut = \case
0 -> Just $ do
!x <- decAddr
pure txo {decodingTxOutAddr = SJust x}
pure txOut {decodingTxOutAddr = SJust x}
1 -> Just $ do
!x <- decCBOR
pure txo {decodingTxOutVal = x}
pure txOut {decodingTxOutVal = x}
2 -> Just $ do
!x <- decCBOR
pure txo {decodingTxOutDatum = x}
pure txOut {decodingTxOutDatum = x}
3 -> Just $ do
!x <- decodeCIC "Script"
pure txo {decodingTxOutScript = SJust x}
pure txOut {decodingTxOutScript = SJust x}
_ -> Nothing
{-# INLINE decoderForKey #-}
{-# INLINE decoderByKey #-}
bodyFields :: (Word -> Field (DecodingTxOut era))
bodyFields 0 =
field
(\x txo -> txo {decodingTxOutAddr = SJust x})
(\x txOut -> txOut {decodingTxOutAddr = SJust x})
(D decAddr)
bodyFields 1 =
field
(\x txo -> txo {decodingTxOutVal = x})
(\x txOut -> txOut {decodingTxOutVal = x})
From
bodyFields 2 =
field
(\x txo -> txo {decodingTxOutDatum = x})
(\x txOut -> txOut {decodingTxOutDatum = x})
(D decCBOR)
bodyFields 3 =
ofield
(\x txo -> txo {decodingTxOutScript = x})
(\x txOut -> txOut {decodingTxOutScript = x})
(D $ decodeCIC "Script")
bodyFields n = invalidField n
{-# INLINE bodyFields #-}
Expand Down
1 change: 1 addition & 0 deletions eras/dijkstra/impl/cardano-ledger-dijkstra.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ library
aeson,
base >=4.14 && <5,
bytestring,
cardano-base >=0.1.4,
cardano-crypto-class,
cardano-data ^>=1.3,
cardano-ledger-allegra,
Expand Down
Loading