Skip to content

Commit

Permalink
Improve roundtrip functions to report annotations on callsite
Browse files Browse the repository at this point in the history
  • Loading branch information
newhoggy committed Apr 6, 2023
1 parent 4236853 commit 21d1d5c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
1 change: 1 addition & 0 deletions cardano-api/cardano-api.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ library gen
, cardano-ledger-shelley ^>= 0.1
, containers
, hedgehog
, hedgehog-extras
, text

test-suite cardano-api-test
Expand Down
23 changes: 19 additions & 4 deletions cardano-api/gen/Test/Hedgehog/Roundtrip/CBOR.hs
Original file line number Diff line number Diff line change
@@ -1,29 +1,44 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

module Test.Hedgehog.Roundtrip.CBOR
( roundtrip_CBOR
, roundtrip_CDDL_Tx
) where

import Cardano.Api
import Hedgehog (Gen, Property)

import Data.Proxy (Proxy (..))
import Data.Typeable (typeRep)
import GHC.Stack (HasCallStack)
import qualified GHC.Stack as GHC
import Hedgehog (Gen, Property)
import qualified Hedgehog as H
import qualified Hedgehog.Extras.Test.Base as H

{- HLINT ignore "Use camelCase" -}

roundtrip_CBOR
:: (SerialiseAsCBOR a, Eq a, Show a)
=> AsType a -> Gen a -> Property
:: forall a. (SerialiseAsCBOR a, Eq a, Show a, HasCallStack)
=> AsType a
-> Gen a
-> Property
roundtrip_CBOR typeProxy gen =
H.property $ do
GHC.withFrozenCallStack $ H.noteShow_ $ typeRep $ Proxy @a
val <- H.forAll gen
H.tripping val serialiseToCBOR (deserialiseFromCBOR typeProxy)


roundtrip_CDDL_Tx
:: IsCardanoEra era => CardanoEra era -> Gen (Tx era) -> Property
:: (IsCardanoEra era, HasCallStack)
=> CardanoEra era
-> Gen (Tx era)
-> Property
roundtrip_CDDL_Tx era gen =
H.property $ do
GHC.withFrozenCallStack $ H.noteShow_ era
val <- H.forAll gen
H.tripping val serialiseTxLedgerCddl (deserialiseTxLedgerCddl era)
5 changes: 3 additions & 2 deletions cardano-api/src/Cardano/Api/HasTypeProxy.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ module Cardano.Api.HasTypeProxy
, FromSomeType(..)
) where

import Data.Kind (Constraint, Type)
import Data.Proxy (Proxy (..))
import Data.Kind (Type, Constraint)
import Data.Typeable (Typeable)


class HasTypeProxy t where
class Typeable t => HasTypeProxy t where
-- | A family of singleton types used in this API to indicate which type to
-- use where it would otherwise be ambiguous or merely unclear.
--
Expand Down
5 changes: 2 additions & 3 deletions cardano-api/src/Cardano/Api/Script.hs
Original file line number Diff line number Diff line change
Expand Up @@ -967,15 +967,14 @@ instance HasTypeProxy lang => HasTypeProxy (PlutusScript lang) where
data AsType (PlutusScript lang) = AsPlutusScript (AsType lang)
proxyToAsType _ = AsPlutusScript (proxyToAsType (Proxy :: Proxy lang))

instance (HasTypeProxy lang, Typeable lang) => SerialiseAsRawBytes (PlutusScript lang) where
instance HasTypeProxy lang => SerialiseAsRawBytes (PlutusScript lang) where
serialiseToRawBytes (PlutusScriptSerialised sbs) = SBS.fromShort sbs

deserialiseFromRawBytes (AsPlutusScript _) bs =
-- TODO alonzo: validate the script syntax and fail decoding if invalid
Right (PlutusScriptSerialised (SBS.toShort bs))

instance (IsPlutusScriptLanguage lang, Typeable lang) =>
HasTextEnvelope (PlutusScript lang) where
instance IsPlutusScriptLanguage lang => HasTextEnvelope (PlutusScript lang) where
textEnvelopeType _ =
case plutusScriptVersion :: PlutusScriptVersion lang of
PlutusScriptV1 -> "PlutusScriptV1"
Expand Down

0 comments on commit 21d1d5c

Please sign in to comment.