Skip to content

Commit

Permalink
Fixes to Write.TxSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
Anviking authored and erikd committed Mar 16, 2023
1 parent 376107f commit 3a3aeab
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 34 deletions.
1 change: 1 addition & 0 deletions lib/wallet/src/Cardano/Api/Gen.hs
Expand Up @@ -42,6 +42,7 @@ module Cardano.Api.Gen
, genRationalInt64
, genScript
, genScriptData
, genHashableScriptData
, shrinkScriptData
, genScriptHash
, genScriptInAnyLang
Expand Down
58 changes: 37 additions & 21 deletions lib/wallet/src/Cardano/Wallet/Write/Tx.hs
Expand Up @@ -415,14 +415,15 @@ unsafeAddressFromBytes bytes = case Ledger.deserialiseAddr bytes of
Nothing -> error "unsafeAddressFromBytes: failed to deserialise"

scriptFromCardanoScriptInAnyLang
:: Cardano.ScriptInAnyLang
-> Script LatestLedgerEra
scriptFromCardanoScriptInAnyLang
= Cardano.toShelleyScript
:: forall era. IsRecentEra era
=> Cardano.ScriptInAnyLang
-> Script (Cardano.ShelleyLedgerEra era)
scriptFromCardanoScriptInAnyLang = withAlonzoScriptConstraint (recentEra @era)
Cardano.toShelleyScript
. fromMaybe (error "all valid scripts should be valid in latest era")
. Cardano.toScriptInEra latestEra
. Cardano.toScriptInEra era
where
latestEra = Cardano.ConwayEra
era = cardanoEraFromRecentEra $ recentEra @era

-- | NOTE: The roundtrip
-- @
Expand All @@ -432,14 +433,15 @@ scriptFromCardanoScriptInAnyLang
-- is 'ShelleyEra'-specific, and 'ShelleyEra' is not a 'RecentEra', this should
-- not be a problem.
scriptToCardanoScriptInAnyLang
:: Script LatestLedgerEra
:: forall era. IsRecentEra era
=> Script (Cardano.ShelleyLedgerEra era)
-> Cardano.ScriptInAnyLang
scriptToCardanoScriptInAnyLang =
rewrap
. Cardano.fromShelleyBasedScript latestEra
scriptToCardanoScriptInAnyLang = withAlonzoScriptConstraint (recentEra @era)
$ rewrap
. Cardano.fromShelleyBasedScript shelleyEra
where
rewrap (Cardano.ScriptInEra _ s) = Cardano.toScriptInAnyLang s
latestEra = Cardano.ShelleyBasedEraConway
shelleyEra = shelleyBasedEraFromRecentEra $ recentEra @era

-- | NOTE: The roundtrip
-- @
Expand All @@ -449,7 +451,8 @@ scriptToCardanoScriptInAnyLang =
-- is 'ShelleyEra'-specific, and 'ShelleyEra' is not a 'RecentEra', this should
-- not be a problem.
scriptToCardanoEnvelopeJSON :: AlonzoScript LatestLedgerEra -> Aeson.Value
scriptToCardanoEnvelopeJSON = scriptToJSON . scriptToCardanoScriptInAnyLang
scriptToCardanoEnvelopeJSON =
scriptToJSON . scriptToCardanoScriptInAnyLang @LatestEra
where
scriptToJSON
:: Cardano.ScriptInAnyLang
Expand All @@ -470,13 +473,14 @@ scriptToCardanoEnvelopeJSON = scriptToJSON . scriptToCardanoScriptInAnyLang
scriptFromCardanoEnvelopeJSON
:: Aeson.Value
-> Aeson.Parser (AlonzoScript LatestLedgerEra)
scriptFromCardanoEnvelopeJSON v = fmap scriptFromCardanoScriptInAnyLang $ do
envelope <- Aeson.parseJSON v
case textEnvelopeToScript envelope of
Left textEnvErr
-> fail $ Cardano.displayError textEnvErr
Right (Cardano.ScriptInAnyLang l s)
-> pure $ Cardano.ScriptInAnyLang l s
scriptFromCardanoEnvelopeJSON v =
fmap (scriptFromCardanoScriptInAnyLang @LatestEra) $ do
envelope <- Aeson.parseJSON v
case textEnvelopeToScript envelope of
Left textEnvErr
-> fail $ Cardano.displayError textEnvErr
Right (Cardano.ScriptInAnyLang l s)
-> pure $ Cardano.ScriptInAnyLang l s
where
textEnvelopeToScript
:: Cardano.TextEnvelope
Expand Down Expand Up @@ -527,13 +531,13 @@ binaryDataToBytes =

datumFromCardanoScriptData
:: Cardano.HashableScriptData
-> BinaryData LatestLedgerEra
-> BinaryData era
datumFromCardanoScriptData =
Alonzo.dataToBinaryData
. Cardano.toAlonzoData

datumToCardanoScriptData
:: BinaryData LatestLedgerEra
:: BinaryData era
-> Cardano.HashableScriptData
datumToCardanoScriptData =
Cardano.fromAlonzoData
Expand Down Expand Up @@ -929,3 +933,15 @@ withCLIConstraint era a = case era of
RecentEraAlonzo -> a
RecentEraBabbage -> a
RecentEraConway -> a

withAlonzoScriptConstraint
:: RecentEra era
-> ( Core.Script (Cardano.ShelleyLedgerEra era)
~ Alonzo.Script (Cardano.ShelleyLedgerEra era
)
=> a)
-> a
withAlonzoScriptConstraint era a = case era of
RecentEraAlonzo -> a
RecentEraBabbage -> a
RecentEraConway -> a
2 changes: 1 addition & 1 deletion lib/wallet/src/Cardano/Wallet/Write/Tx/Gen.hs
Expand Up @@ -85,7 +85,7 @@ shrinkDatum (Datum x) = NoDatum : map Datum (shrinkBinaryData x)
shrinkDatum (DatumHash _) = [NoDatum]
shrinkDatum NoDatum = []

shrinkBinaryData :: BinaryData LatestLedgerEra -> [BinaryData LatestLedgerEra]
shrinkBinaryData :: BinaryData era -> [BinaryData era]
shrinkBinaryData = shrinkMapBy
datumFromCardanoScriptData
datumToCardanoScriptData $
Expand Down
24 changes: 12 additions & 12 deletions lib/wallet/test/unit/Cardano/Wallet/Write/TxSpec.hs
Expand Up @@ -10,7 +10,7 @@ module Cardano.Wallet.Write.TxSpec where
import Prelude

import Cardano.Api.Gen
( genScriptData, genScriptInAnyLang, genTxIn, shrinkScriptData )
( genScriptInAnyLang, genTxIn, genHashableScriptData )
import Cardano.Ledger.Alonzo.PParams
( _coinsPerUTxOWord )
import Cardano.Ledger.Babbage.PParams
Expand All @@ -19,7 +19,6 @@ import Cardano.Wallet.Unsafe
( unsafeFromHex )
import Cardano.Wallet.Write.Tx
( BinaryData
, LatestEra
, LatestLedgerEra
, RecentEra (..)
, Script
Expand All @@ -39,7 +38,7 @@ import Cardano.Wallet.Write.Tx
, scriptFromCardanoScriptInAnyLang
, scriptToCardanoEnvelopeJSON
, scriptToCardanoScriptInAnyLang
, toCardanoUTxO
, toCardanoUTxO, StandardBabbage
)
import Cardano.Wallet.Write.Tx.Gen
( genBinaryData, genTxOut, shrinkBinaryData )
Expand Down Expand Up @@ -134,10 +133,10 @@ spec = do
it "is isomorphic to Cardano.ScriptInAnyLang (modulo SimpleScriptV1/2)"
$ testIsomorphism
(NamedFun
scriptToCardanoScriptInAnyLang
(scriptToCardanoScriptInAnyLang @Cardano.BabbageEra)
"scriptToCardanoScriptInAnyLang")
(NamedFun
scriptFromCardanoScriptInAnyLang
(scriptFromCardanoScriptInAnyLang @Cardano.BabbageEra)
"scriptFromCardanoScriptInAnyLang")
id

Expand Down Expand Up @@ -186,23 +185,24 @@ spec = do
"fromCardanoUTxO")
id

instance Arbitrary Cardano.ScriptData where
arbitrary = genScriptData
shrink = shrinkScriptData
instance Arbitrary Cardano.HashableScriptData where
arbitrary = genHashableScriptData
shrink = const []

-- | The OVERLAPS can be removed when we remove import of
-- "Test.Cardano.Ledger.Alonzo.Serialisation.Generators"
instance {-# OVERLAPS #-} Arbitrary (BinaryData LatestLedgerEra) where
instance {-# INCOHERENT #-} Arbitrary (BinaryData StandardBabbage) where
arbitrary = genBinaryData
shrink = shrinkBinaryData

instance Arbitrary Cardano.ScriptInAnyLang where
arbitrary = genScriptInAnyLang

instance {-# OVERLAPPING #-} Arbitrary (Script LatestLedgerEra) where
arbitrary = scriptFromCardanoScriptInAnyLang <$> arbitrary
instance {-# OVERLAPPING #-} Arbitrary (Script StandardBabbage) where
arbitrary = scriptFromCardanoScriptInAnyLang @Cardano.BabbageEra
<$> arbitrary

instance Arbitrary (Cardano.UTxO LatestEra) where
instance Arbitrary (Cardano.UTxO Cardano.BabbageEra) where
arbitrary = Cardano.UTxO . Map.fromList <$> liftArbitrary genTxInOutEntry
where
genTxInOutEntry = (,)
Expand Down

0 comments on commit 3a3aeab

Please sign in to comment.