Skip to content

Commit ad6e828

Browse files
committed
Refactor
1 parent 3011197 commit ad6e828

File tree

16 files changed

+1145
-43
lines changed

16 files changed

+1145
-43
lines changed

cardano-api/cardano-api.cabal

+10-2
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ library
7777
Cardano.Api.Internal.Eon.ShelleyBasedEra
7878
Cardano.Api.Internal.Eras
7979
Cardano.Api.Internal.Error
80-
Cardano.Api.Internal.Experimental.Eras
81-
Cardano.Api.Internal.Experimental.Tx
8280
Cardano.Api.Internal.Fees
8381
Cardano.Api.Internal.Genesis
8482
Cardano.Api.Internal.GenesisParameters
@@ -200,6 +198,15 @@ library
200198
Cardano.Api.Internal.Eon.ShelleyToMaryEra
201199
Cardano.Api.Internal.Eras.Case
202200
Cardano.Api.Internal.Eras.Core
201+
Cardano.Api.Internal.Experimental.Eras
202+
Cardano.Api.Internal.Experimental.Plutus.IndexedPlutusScriptWitness
203+
Cardano.Api.Internal.Experimental.Plutus.Shim.LegacyScripts
204+
Cardano.Api.Internal.Experimental.Plutus.Script
205+
Cardano.Api.Internal.Experimental.Plutus.ScriptWitness
206+
Cardano.Api.Internal.Experimental.Simple.Script
207+
Cardano.Api.Internal.Experimental.Tx
208+
Cardano.Api.Internal.Experimental.Witness.TxScriptWitnessRequirements
209+
Cardano.Api.Internal.Experimental.Witness.AnyWitness
203210
Cardano.Api.Internal.Feature
204211
Cardano.Api.Internal.Governance.Actions.ProposalProcedure
205212
Cardano.Api.Internal.Governance.Actions.VotingProcedure
@@ -248,6 +255,7 @@ library
248255
Cardano.Api.Internal.SerialiseUsing
249256
Cardano.Api.Internal.SpecialByron
250257
Cardano.Api.Internal.StakePoolMetadata
258+
Cardano.Api.Internal.Tx.BuildTxWith
251259
Cardano.Api.Internal.Tx.UTxO
252260
Cardano.Api.Internal.TxIn
253261
Cardano.Api.Internal.TxMetadata

cardano-api/gen/Test/Gen/Cardano/Api/Typed.hs

+14-10
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ import qualified Data.ByteString.Base16 as Base16
169169
import Data.Ratio (Ratio, (%))
170170
import Data.String
171171
import Test.Gen.Cardano.Api.Hardcoded
172+
import Data.Typeable
172173
import Data.Word (Word16, Word32, Word64)
173174
import GHC.Exts (IsList (..))
174175
import GHC.Stack
@@ -707,7 +708,7 @@ genTxWithdrawals =
707708
]
708709
)
709710

710-
genTxCertificates :: CardanoEra era -> Gen (TxCertificates BuildTx era)
711+
genTxCertificates :: Typeable era => CardanoEra era -> Gen (TxCertificates BuildTx era)
711712
genTxCertificates =
712713
inEonForEra
713714
(pure TxCertificatesNone)
@@ -720,7 +721,7 @@ genTxCertificates =
720721
]
721722
)
722723

723-
genCertificate :: forall era. ShelleyBasedEra era -> Gen (Certificate era)
724+
genCertificate :: forall era. Typeable era => ShelleyBasedEra era -> Gen (Certificate era)
724725
genCertificate sbe =
725726
Gen.choice
726727
$ catMaybes
@@ -871,7 +872,7 @@ genTxMintValue =
871872
, pure $ TxMintValue w (fromList assets)
872873
]
873874

874-
genTxBodyContent :: ShelleyBasedEra era -> Gen (TxBodyContent BuildTx era)
875+
genTxBodyContent :: Typeable era => ShelleyBasedEra era -> Gen (TxBodyContent BuildTx era)
875876
genTxBodyContent sbe = do
876877
let era = toCardanoEra sbe
877878
txIns <-
@@ -992,7 +993,8 @@ genWitnessesByron = Gen.list (Range.constant 1 10) genByronKeyWitness
992993

993994
-- | This generator validates generated 'TxBodyContent' and backtracks when the generated body
994995
-- fails the validation. That also means that it is quite slow.
995-
genValidTxBody :: ShelleyBasedEra era
996+
genValidTxBody :: Typeable era
997+
=> ShelleyBasedEra era
996998
-> Gen (TxBody era, TxBodyContent BuildTx era) -- ^ validated 'TxBody' and 'TxBodyContent'
997999
genValidTxBody sbe =
9981000
Gen.mapMaybe
@@ -1003,7 +1005,7 @@ genValidTxBody sbe =
10031005
(genTxBodyContent sbe)
10041006

10051007
-- | Partial! This function will throw an error when the generated transaction is invalid.
1006-
genTxBody :: HasCallStack => ShelleyBasedEra era -> Gen (TxBody era)
1008+
genTxBody :: (HasCallStack, Typeable era) => ShelleyBasedEra era -> Gen (TxBody era)
10071009
genTxBody era = do
10081010
res <- Api.createTransactionBody era <$> genTxBodyContent era
10091011
case res of
@@ -1042,15 +1044,15 @@ genScriptValidity :: Gen ScriptValidity
10421044
genScriptValidity = Gen.element [ScriptInvalid, ScriptValid]
10431045

10441046
genTx
1045-
:: ()
1047+
:: Typeable era
10461048
=> ShelleyBasedEra era
10471049
-> Gen (Tx era)
10481050
genTx era =
10491051
makeSignedTransaction
10501052
<$> genWitnesses era
10511053
<*> (fst <$> genValidTxBody era)
10521054

1053-
genWitnesses :: ShelleyBasedEra era -> Gen [KeyWitness era]
1055+
genWitnesses :: Typeable era => ShelleyBasedEra era -> Gen [KeyWitness era]
10541056
genWitnesses sbe = do
10551057
bsWits <- Gen.list (Range.constant 0 10) (genShelleyBootstrapWitness sbe)
10561058
keyWits <- Gen.list (Range.constant 0 10) (genShelleyKeyWitness sbe)
@@ -1095,7 +1097,7 @@ genWitnessNetworkIdOrByronAddress =
10951097
]
10961098

10971099
genShelleyBootstrapWitness
1098-
:: ()
1100+
:: Typeable era
10991101
=> ShelleyBasedEra era
11001102
-> Gen (KeyWitness era)
11011103
genShelleyBootstrapWitness sbe =
@@ -1104,8 +1106,10 @@ genShelleyBootstrapWitness sbe =
11041106
<*> (fst <$> genValidTxBody sbe)
11051107
<*> genSigningKey AsByronKey
11061108

1109+
11071110
genShelleyKeyWitness
11081111
:: ()
1112+
=> Typeable era
11091113
=> ShelleyBasedEra era
11101114
-> Gen (KeyWitness era)
11111115
genShelleyKeyWitness sbe =
@@ -1114,7 +1118,7 @@ genShelleyKeyWitness sbe =
11141118
<*> genShelleyWitnessSigningKey
11151119

11161120
genShelleyWitness
1117-
:: ()
1121+
:: Typeable era
11181122
=> ShelleyBasedEra era
11191123
-> Gen (KeyWitness era)
11201124
genShelleyWitness sbe =
@@ -1135,7 +1139,7 @@ genShelleyWitnessSigningKey =
11351139
]
11361140

11371141
genCardanoKeyWitness
1138-
:: ()
1142+
:: Typeable era
11391143
=> ShelleyBasedEra era
11401144
-> Gen (KeyWitness era)
11411145
genCardanoKeyWitness = genShelleyWitness

cardano-api/src/Cardano/Api/Internal/Eon/AlonzoEraOnwards.hs

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import Cardano.Binary
2929
import Cardano.Crypto.Hash.Blake2b qualified as Blake2b
3030
import Cardano.Crypto.Hash.Class qualified as C
3131
import Cardano.Crypto.VRF qualified as C
32+
import Cardano.Ledger.Allegra.Scripts qualified as L
3233
import Cardano.Ledger.Alonzo.Plutus.Context qualified as Plutus
3334
import Cardano.Ledger.Alonzo.Scripts qualified as L
3435
import Cardano.Ledger.Alonzo.Tx qualified as L
@@ -103,6 +104,7 @@ type AlonzoEraOnwardsConstraints era =
103104
, L.EraUTxO (ShelleyLedgerEra era)
104105
, L.HashAnnotated (L.TxBody (ShelleyLedgerEra era)) L.EraIndependentTxBody L.StandardCrypto
105106
, L.MaryEraTxBody (ShelleyLedgerEra era)
107+
, L.NativeScript (ShelleyLedgerEra era) ~ L.Timelock (ShelleyLedgerEra era)
106108
, Plutus.EraPlutusContext (ShelleyLedgerEra era)
107109
, L.Script (ShelleyLedgerEra era) ~ L.AlonzoScript (ShelleyLedgerEra era)
108110
, L.ScriptsNeeded (ShelleyLedgerEra era) ~ L.AlonzoScriptsNeeded (ShelleyLedgerEra era)

cardano-api/src/Cardano/Api/Internal/Experimental/Eras.hs

+6
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module Cardano.Api.Internal.Experimental.Eras
3232
)
3333
where
3434

35+
import Cardano.Api.Internal.Eon.AlonzoEraOnwards
3536
import Cardano.Api.Internal.Eon.BabbageEraOnwards
3637
import Cardano.Api.Internal.Eon.Convert
3738
import Cardano.Api.Internal.Eon.ShelleyBasedEra (ShelleyBasedEra (..), ShelleyLedgerEra)
@@ -206,6 +207,11 @@ instance Convert Era ShelleyBasedEra where
206207
BabbageEra -> ShelleyBasedEraBabbage
207208
ConwayEra -> ShelleyBasedEraConway
208209

210+
instance Convert Era AlonzoEraOnwards where
211+
convert = \case
212+
BabbageEra -> AlonzoEraOnwardsBabbage
213+
ConwayEra -> AlonzoEraOnwardsConway
214+
209215
instance Convert Era BabbageEraOnwards where
210216
convert = \case
211217
BabbageEra -> BabbageEraOnwardsBabbage
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE GADTs #-}
3+
{-# LANGUAGE KindSignatures #-}
4+
5+
module Cardano.Api.Internal.Experimental.Plutus.IndexedPlutusScriptWitness
6+
( -- * Constuct an indexed plutus script witness.
7+
AnyIndexedPlutusScriptWitness (..)
8+
, IndexedPlutusScriptWitness (..)
9+
, createIndexedPlutusScriptWitnesses
10+
, getAnyWitnessRedeemerPointerMap
11+
)
12+
where
13+
14+
import Cardano.Api.Internal.Eon.AlonzoEraOnwards
15+
import Cardano.Api.Internal.Eon.ShelleyBasedEra
16+
import Cardano.Api.Internal.Experimental.Plutus.ScriptWitness
17+
import Cardano.Api.Internal.Experimental.Witness.AnyWitness
18+
import Cardano.Api.Internal.Script (toAlonzoExUnits)
19+
import Cardano.Api.Internal.ScriptData
20+
import Cardano.Api.Ledger qualified as L
21+
22+
import Cardano.Ledger.Alonzo.TxWits qualified as L
23+
24+
import Data.Word
25+
import GHC.Exts
26+
27+
-- | A Plutus script witness along the thing it is witnessing and the index of that thing.
28+
-- E.g transaction input, certificate, withdrawal, minting policy, etc.
29+
-- A Plutus script witness only makes sense in the context of what it is witnessing
30+
-- and the index of the thing it is witnessing.
31+
data IndexedPlutusScriptWitness witnessable (lang :: L.Language) (purpose :: PlutusScriptPurpose) era where
32+
IndexedPlutusScriptWitness
33+
:: Witnessable witnessable era
34+
-> (L.PlutusPurpose L.AsIx era)
35+
-> (PlutusScriptWitness lang purpose era)
36+
-> IndexedPlutusScriptWitness witnessable lang purpose era
37+
38+
data AnyIndexedPlutusScriptWitness era where
39+
AnyIndexedPlutusScriptWitness
40+
:: GetPlutusScriptPurpose era
41+
=> IndexedPlutusScriptWitness witnessable lang purpose era
42+
-> AnyIndexedPlutusScriptWitness era
43+
44+
createIndexedPlutusScriptWitness
45+
:: Word32
46+
-> Witnessable witnessable era
47+
-> PlutusScriptWitness lang purpose era
48+
-> IndexedPlutusScriptWitness witnessable lang purpose era
49+
createIndexedPlutusScriptWitness index witnessable pSwit =
50+
IndexedPlutusScriptWitness witnessable (toPlutusScriptPurpose index witnessable) pSwit
51+
52+
createIndexedPlutusScriptWitnesses
53+
:: [(Witnessable witnessable era, AnyWitness era)]
54+
-> [AnyIndexedPlutusScriptWitness era]
55+
createIndexedPlutusScriptWitnesses witnessableThings =
56+
[ AnyIndexedPlutusScriptWitness $ createIndexedPlutusScriptWitness index thing sWit
57+
| (index, (thing, AnyPlutusScriptWitness sWit)) <- zip [0 ..] witnessableThings
58+
]
59+
60+
-- | The transaction's redeemer pointer map allows the ledger to connect a redeemer and execution unit pairing to the relevant
61+
-- script. The ledger basically reconstructs the indicies (redeemer pointers) of this map can then look up the relevant
62+
-- execution units/redeemer pairing. NB the redeemer pointer has been renamed to 'PlutusPurpose AsIndex' in the ledger.
63+
getAnyWitnessRedeemerPointerMap
64+
:: AlonzoEraOnwards era
65+
-> (Witnessable witnessable (ShelleyLedgerEra era), AnyWitness (ShelleyLedgerEra era))
66+
-> L.Redeemers (ShelleyLedgerEra era)
67+
getAnyWitnessRedeemerPointerMap eon (_, AnyKeyWitness) = alonzoEraOnwardsConstraints eon mempty
68+
getAnyWitnessRedeemerPointerMap eon (_, AnySimpleScriptWitness{}) = alonzoEraOnwardsConstraints eon mempty
69+
getAnyWitnessRedeemerPointerMap eon anyWit =
70+
constructRedeeemerPointerMap eon $
71+
createIndexedPlutusScriptWitnesses [anyWit]
72+
73+
-- | An 'IndexedPlutusScriptWitness' contains everything we need to construct a single
74+
-- entry in the redeemer pointer map.
75+
constructRedeemerPointer
76+
:: AlonzoEraOnwards era
77+
-> AnyIndexedPlutusScriptWitness (ShelleyLedgerEra era)
78+
-> L.Redeemers (ShelleyLedgerEra era)
79+
constructRedeemerPointer eon (AnyIndexedPlutusScriptWitness (IndexedPlutusScriptWitness _ purpose scriptWit)) =
80+
let PlutusScriptWitness _ _ _ redeemer execUnits = scriptWit
81+
in alonzoEraOnwardsConstraints eon $
82+
L.Redeemers $
83+
fromList [(purpose, (toAlonzoData redeemer, toAlonzoExUnits execUnits))]
84+
85+
constructRedeeemerPointerMap
86+
:: AlonzoEraOnwards era
87+
-> [AnyIndexedPlutusScriptWitness ((ShelleyLedgerEra era))]
88+
-> L.Redeemers (ShelleyLedgerEra era)
89+
constructRedeeemerPointerMap eon scriptWits =
90+
let redeemerPointers = map (constructRedeemerPointer eon) scriptWits
91+
in alonzoEraOnwardsConstraints eon $ mconcat redeemerPointers
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE FlexibleContexts #-}
3+
{-# LANGUAGE GADTs #-}
4+
{-# LANGUAGE KindSignatures #-}
5+
{-# LANGUAGE StandaloneDeriving #-}
6+
7+
module Cardano.Api.Internal.Experimental.Plutus.Script
8+
( PlutusScriptInEra (..)
9+
, PlutusScriptOrReferenceInput (..)
10+
)
11+
where
12+
13+
import Cardano.Api.Internal.TxIn (TxIn)
14+
15+
import Cardano.Ledger.Plutus.Language (PlutusRunnable)
16+
import Cardano.Ledger.Plutus.Language qualified as L
17+
18+
-- | A Plutus script in a particular era.
19+
-- Why PlutusRunnable? Mainly for deserialization benefits.
20+
-- The deserialization of this type looks at the
21+
-- major protocol version and the script language to determine if
22+
-- indeed the script is runnable. This is a dramatic improvement over the old api
23+
-- which essentially read a 'ByteString' and hoped for the best.
24+
-- Any failures due to malformed/invalid scripts were caught upon transaction
25+
-- submission or running the script when attempting to predict the necessary execution units.
26+
--
27+
-- Where do we get the major protocol version from?
28+
-- In order to access the major protocol version we pass in an 'era` type parameter which
29+
-- can be translated to the major protocol version.
30+
--
31+
-- Where do we get the script language from?
32+
-- The serialized version of 'PlutusRunnable' encodes the script language.
33+
-- See `DecCBOR (PlutusRunnable l)` in cardano-ledger for more details.
34+
data PlutusScriptInEra (lang :: L.Language) era where
35+
PlutusScriptInEra :: PlutusRunnable lang -> PlutusScriptInEra lang era
36+
37+
deriving instance Show (PlutusScriptInEra lang era)
38+
39+
-- | You can provide the plutus script directly in the transaction
40+
-- or a reference input that points to the script in the UTxO.
41+
-- Using a reference script saves space in your transaction.
42+
data PlutusScriptOrReferenceInput lang era
43+
= PScript (PlutusScriptInEra lang era)
44+
| PReferenceScript TxIn
45+
deriving Show

0 commit comments

Comments
 (0)