Skip to content

Commit 8b9e4cd

Browse files
committed
Trim
1 parent 811f727 commit 8b9e4cd

File tree

2 files changed

+29
-50
lines changed

2 files changed

+29
-50
lines changed

cardano-api/src/Cardano/Api/Internal/Experimental/Plutus/Script.hs

+4-5
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import Cardano.Ledger.Plutus.Language qualified as L
2020
-- The deserialization of this type looks at the
2121
-- major protocol version and the script language to determine if
2222
-- indeed the script is runnable. This is a dramatic improvement over the old api
23-
-- which essentially read a 'ByteString'. Any failures due to malformed/invalid scripts
24-
-- were caught upon transaction submission or running the script when attempting to
25-
-- predict the necessary execution units.
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.
2626
--
2727
-- Where do we get the major protocol version from?
2828
-- In order to access the major protocol version we pass in an 'era` type parameter which
@@ -32,8 +32,7 @@ import Cardano.Ledger.Plutus.Language qualified as L
3232
-- The serialized version of 'PlutusRunnable' encodes the script language.
3333
-- See `DecCBOR (PlutusRunnable l)` in cardano-ledger for more details.
3434
data PlutusScriptInEra (lang :: L.Language) era where
35-
PlutusScriptInEra
36-
:: PlutusRunnable lang -> PlutusScriptInEra lang era
35+
PlutusScriptInEra :: PlutusRunnable lang -> PlutusScriptInEra lang era
3736

3837
deriving instance Show (PlutusScriptInEra lang era)
3938

cardano-api/src/Cardano/Api/Internal/Experimental/Plutus/ScriptWitness.hs

+25-45
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ getAnyWitnessScriptData eon (AnyPlutusScriptWitness (PlutusScriptWitness l _ scr
276276
Nothing -> alonzoEraOnwardsConstraints eon mempty
277277
Just d -> alonzoEraOnwardsConstraints eon $ L.TxDats $ fromList [(L.hashData d, d)]
278278

279+
-- | This type collects all the requirements for script witnesses in a transaction.
279280
data TxScriptWitnessRequirements era
280281
= TxScriptWitnessRequirements
281282
(Set L.Language)
@@ -331,10 +332,6 @@ obtainMonoidConstraint eon = case eon of
331332
AlonzoEraOnwardsBabbage -> id
332333
AlonzoEraOnwardsConway -> id
333334

334-
-- So how would the new api look? We would paramterize on data Era and
335-
-- always have two cases instead of multipl cases
336-
-- We could also parameterize on the IsEra era type class for exposed functions
337-
338335
getAnyWitnessScript
339336
:: ShelleyBasedEra era -> AnyWitness (ShelleyLedgerEra era) -> Maybe (L.Script (ShelleyLedgerEra era))
340337
getAnyWitnessScript _ AnyKeyWitness = Nothing
@@ -361,38 +358,37 @@ fromPlutusRunnable
361358
-> AlonzoEraOnwards era
362359
-> L.PlutusRunnable lang
363360
-> Maybe (L.PlutusScript (ShelleyLedgerEra era))
364-
fromPlutusRunnable l@L.SPlutusV1 eon runnable =
361+
fromPlutusRunnable L.SPlutusV1 eon runnable =
365362
case eon of
366363
AlonzoEraOnwardsAlonzo ->
367-
let plutusScript = plutusFromRunnableAssist l runnable
364+
let plutusScript = L.plutusFromRunnable runnable
368365
in Just $ L.AlonzoPlutusV1 plutusScript
369366
AlonzoEraOnwardsBabbage ->
370-
let plutusScript = plutusFromRunnableAssist l runnable
367+
let plutusScript = L.plutusFromRunnable runnable
371368
in Just $ L.BabbagePlutusV1 plutusScript
372369
AlonzoEraOnwardsConway ->
373-
let plutusScript = plutusFromRunnableAssist l runnable
370+
let plutusScript = L.plutusFromRunnable runnable
374371
in Just $ L.ConwayPlutusV1 plutusScript
375-
fromPlutusRunnable l@L.SPlutusV2 eon runnable =
372+
fromPlutusRunnable L.SPlutusV2 eon runnable =
376373
case eon of
377374
AlonzoEraOnwardsAlonzo -> Nothing
378375
AlonzoEraOnwardsBabbage ->
379-
let plutusScript = plutusFromRunnableAssist l runnable
376+
let plutusScript = L.plutusFromRunnable runnable
380377
in Just $ L.BabbagePlutusV2 plutusScript
381378
AlonzoEraOnwardsConway ->
382-
let plutusScript = plutusFromRunnableAssist l runnable
379+
let plutusScript = L.plutusFromRunnable runnable
383380
in Just $ L.ConwayPlutusV2 plutusScript
384-
fromPlutusRunnable l@L.SPlutusV3 eon runnable =
381+
fromPlutusRunnable L.SPlutusV3 eon runnable =
385382
case eon of
386383
AlonzoEraOnwardsAlonzo -> Nothing
387384
AlonzoEraOnwardsBabbage -> Nothing
388385
AlonzoEraOnwardsConway ->
389-
let plutusScript = plutusFromRunnableAssist l runnable
386+
let plutusScript = L.plutusFromRunnable runnable
390387
in Just $ L.ConwayPlutusV3 plutusScript
391388

392-
-- Created purely to help type inference
393-
plutusFromRunnableAssist :: L.SLanguage lang -> L.PlutusRunnable lang -> L.Plutus lang
394-
plutusFromRunnableAssist _ = L.plutusFromRunnable
395-
389+
-- | These are all of the "things" a plutus script can witness. We include the relevant
390+
-- type class constraint to avoid boilerplate when creating the 'PlutusPurpose' in
391+
-- the 'GetPlutusScriptPurpose' instances.
396392
data Witnessable thing era where
397393
WitTxIn :: L.AlonzoEraScript era => TxIn -> Witnessable TxIn era
398394
WitTxCert :: L.AlonzoEraScript era => Cert -> Witnessable Cert era
@@ -418,14 +414,16 @@ type Voter = Api.AnyVoter
418414

419415
type Proposal = Api.AnyProposal
420416

421-
-- | In order to reduce boilerplate we reuse cardano-ledger's PlutusPurpose type. This
422-
-- type is used in the construction of the redeemer pointers map. The redeemer pointers map
423-
-- connects the redeemer and execution units to the thing being witnessed. The map is
424-
-- indexed by redeemer pointer!
425-
-- So the natural question is how do the Plutus script witnesses know what
426-
-- execution units and redeemer is paired with it? The ledger constructs a pointer
427-
-- for every Plutus script and this pointer corresponds to the pointer constructed
428-
-- in the redeemer pointer map. See 'collectPlutusScriptsWithContext' in 'cardano-ledger'.
417+
-- | To reduce boilerplate, we reuse the `PlutusPurpose` type from `cardano-ledger`.
418+
-- This type is utilized in constructing the redeemer pointers map, which
419+
-- links the redeemer and execution units with the entity being witnessed.
420+
-- The map is indexed by the redeemer pointer.
421+
--
422+
-- A natural question arises: How do Plutus scripts determine which
423+
-- execution units and redeemer are paired with them? The ledger constructs a redeemer pointer
424+
-- for every Plutus script, and this pointer corresponds to the one in the transaction's
425+
-- redeemer pointers map. For more details, refer to `collectPlutusScriptsWithContext`
426+
-- in `cardano-ledger`.
429427
class GetPlutusScriptPurpose era where
430428
toPlutusScriptPurpose
431429
:: Word32
@@ -466,17 +464,8 @@ createIndexedPlutusScriptWitnesses witnessableThings =
466464
| (index, (thing, AnyPlutusScriptWitness sWit)) <- zip [0 ..] witnessableThings
467465
]
468466

469-
{-
470-
The transaction redeemer pointer map allows the ledger to connect a redeemer and execution unit pairing to the relevant
471-
script. The ledger basically reconstructs the indicies of this map can then look up the relevant pairing.
472-
-}
473-
-- This is the smoking gun. The truth is in Conway votes and proposals were introduced.
474-
-- Therefore only in conway we should be allowed to get votes and props being paired with
475-
-- witnesses. Can we express this in the type system? Witnessable will have to be parameterized
476-
-- on era and we would need a type family to enforce this relation ship
477-
-- Witnessable era witnessable -> AnyWitness era -> (Witnessable era witnessable, AnyWitness era)
478-
-- And we can be explict about the pairings! We would need the result type of the family to be CanBePaired era witnessable
479-
467+
-- | The transaction redeemer pointer map allows the ledger to connect a redeemer and execution unit pairing to the relevant
468+
-- script. The ledger basically reconstructs the indicies of this map can then look up the relevant pairing.
480469
getAnyWitnessRedeemerPointerMap
481470
:: AlonzoEraOnwards era
482471
-> (Witnessable witnessable (ShelleyLedgerEra era), AnyWitness (ShelleyLedgerEra era))
@@ -505,10 +494,6 @@ constructRedeemerPointer eon (AnyIndexedPlutusScriptWitness (IndexedPlutusScript
505494
L.Redeemers $
506495
fromList [(purpose, (toAlonzoData redeemer, toAlonzoExUnits execUnits))]
507496

508-
----------------------------------------------
509-
-- TEST
510-
----------------------------------------------
511-
512497
mkPlutusScriptWitness
513498
:: AlonzoEraOnwards era
514499
-> L.SLanguage plutuslang
@@ -524,8 +509,3 @@ mkPlutusScriptWitness _ l plutusScriptRunnable datum scriptRedeemer execUnits =
524509
datum
525510
scriptRedeemer
526511
execUnits
527-
528-
-- PROPOSED REPLACEMENT
529-
-----------------------------------------------
530-
531-
------------------------------------------------

0 commit comments

Comments
 (0)