-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,142 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
cardano-api/src/Cardano/Api/Internal/Experimental/Plutus/IndexedPlutusScriptWitness.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE KindSignatures #-} | ||
|
||
module Cardano.Api.Internal.Experimental.Plutus.IndexedPlutusScriptWitness | ||
( -- * Constuct an indexed plutus script witness. | ||
AnyIndexedPlutusScriptWitness (..) | ||
, IndexedPlutusScriptWitness (..) | ||
, createIndexedPlutusScriptWitnesses | ||
, getAnyWitnessRedeemerPointerMap | ||
) | ||
where | ||
|
||
import Cardano.Api.Internal.Eon.AlonzoEraOnwards | ||
import Cardano.Api.Internal.Eon.ShelleyBasedEra | ||
import Cardano.Api.Internal.Experimental.Plutus.ScriptWitness | ||
import Cardano.Api.Internal.Experimental.Witness.AnyWitness | ||
import Cardano.Api.Internal.Script (toAlonzoExUnits) | ||
import Cardano.Api.Internal.ScriptData | ||
import Cardano.Api.Ledger qualified as L | ||
|
||
import Cardano.Ledger.Alonzo.TxWits qualified as L | ||
|
||
import Data.Word | ||
import GHC.Exts | ||
|
||
-- | A Plutus script witness along the thing it is witnessing and the index of that thing. | ||
-- E.g transaction input, certificate, withdrawal, minting policy, etc. | ||
-- A Plutus script witness only makes sense in the context of what it is witnessing | ||
-- and the index of the thing it is witnessing. | ||
data IndexedPlutusScriptWitness witnessable (lang :: L.Language) (purpose :: PlutusScriptPurpose) era where | ||
IndexedPlutusScriptWitness | ||
:: Witnessable witnessable era | ||
-> (L.PlutusPurpose L.AsIx era) | ||
-> (PlutusScriptWitness lang purpose era) | ||
-> IndexedPlutusScriptWitness witnessable lang purpose era | ||
|
||
data AnyIndexedPlutusScriptWitness era where | ||
AnyIndexedPlutusScriptWitness | ||
:: GetPlutusScriptPurpose era | ||
=> IndexedPlutusScriptWitness witnessable lang purpose era | ||
-> AnyIndexedPlutusScriptWitness era | ||
|
||
createIndexedPlutusScriptWitness | ||
:: Word32 | ||
-> Witnessable witnessable era | ||
-> PlutusScriptWitness lang purpose era | ||
-> IndexedPlutusScriptWitness witnessable lang purpose era | ||
createIndexedPlutusScriptWitness index witnessable = | ||
IndexedPlutusScriptWitness witnessable (toPlutusScriptPurpose index witnessable) | ||
|
||
createIndexedPlutusScriptWitnesses | ||
:: [(Witnessable witnessable era, AnyWitness era)] | ||
-> [AnyIndexedPlutusScriptWitness era] | ||
createIndexedPlutusScriptWitnesses witnessableThings = | ||
[ AnyIndexedPlutusScriptWitness $ createIndexedPlutusScriptWitness index thing sWit | ||
| (index, (thing, AnyPlutusScriptWitness sWit)) <- zip [0 ..] witnessableThings | ||
] | ||
|
||
-- | The transaction's redeemer pointer map allows the ledger to connect a redeemer and execution unit pairing to the relevant | ||
-- script. The ledger basically reconstructs the indicies (redeemer pointers) of this map can then look up the relevant | ||
-- execution units/redeemer pairing. NB the redeemer pointer has been renamed to 'PlutusPurpose AsIndex' in the ledger. | ||
getAnyWitnessRedeemerPointerMap | ||
:: AlonzoEraOnwards era | ||
-> (Witnessable witnessable (ShelleyLedgerEra era), AnyWitness (ShelleyLedgerEra era)) | ||
-> L.Redeemers (ShelleyLedgerEra era) | ||
getAnyWitnessRedeemerPointerMap eon (_, AnyKeyWitness) = alonzoEraOnwardsConstraints eon mempty | ||
getAnyWitnessRedeemerPointerMap eon (_, AnySimpleScriptWitness{}) = alonzoEraOnwardsConstraints eon mempty | ||
getAnyWitnessRedeemerPointerMap eon anyWit = | ||
constructRedeeemerPointerMap eon $ | ||
createIndexedPlutusScriptWitnesses [anyWit] | ||
|
||
-- | An 'IndexedPlutusScriptWitness' contains everything we need to construct a single | ||
-- entry in the redeemer pointer map. | ||
constructRedeemerPointer | ||
:: AlonzoEraOnwards era | ||
-> AnyIndexedPlutusScriptWitness (ShelleyLedgerEra era) | ||
-> L.Redeemers (ShelleyLedgerEra era) | ||
constructRedeemerPointer eon (AnyIndexedPlutusScriptWitness (IndexedPlutusScriptWitness _ purpose scriptWit)) = | ||
let PlutusScriptWitness _ _ _ redeemer execUnits = scriptWit | ||
in alonzoEraOnwardsConstraints eon $ | ||
L.Redeemers $ | ||
fromList [(purpose, (toAlonzoData redeemer, toAlonzoExUnits execUnits))] | ||
|
||
constructRedeeemerPointerMap | ||
:: AlonzoEraOnwards era | ||
-> [AnyIndexedPlutusScriptWitness ((ShelleyLedgerEra era))] | ||
Check notice Code scanning / HLint Redundant bracket Note
cardano-api/src/Cardano/Api/Internal/Experimental/Plutus/IndexedPlutusScriptWitness.hs:169:38-59: Suggestion: Redundant bracket
Found: ((ShelleyLedgerEra era)) Perhaps: (ShelleyLedgerEra era) |
||
-> L.Redeemers (ShelleyLedgerEra era) | ||
constructRedeeemerPointerMap eon scriptWits = | ||
let redeemerPointers = map (constructRedeemerPointer eon) scriptWits | ||
in alonzoEraOnwardsConstraints eon $ mconcat redeemerPointers |
45 changes: 45 additions & 0 deletions
45
cardano-api/src/Cardano/Api/Internal/Experimental/Plutus/Script.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE GADTs #-} | ||
{-# LANGUAGE KindSignatures #-} | ||
{-# LANGUAGE StandaloneDeriving #-} | ||
|
||
module Cardano.Api.Internal.Experimental.Plutus.Script | ||
( PlutusScriptInEra (..) | ||
, PlutusScriptOrReferenceInput (..) | ||
) | ||
where | ||
|
||
import Cardano.Api.Internal.TxIn (TxIn) | ||
|
||
import Cardano.Ledger.Plutus.Language (PlutusRunnable) | ||
import Cardano.Ledger.Plutus.Language qualified as L | ||
|
||
-- | A Plutus script in a particular era. | ||
-- Why PlutusRunnable? Mainly for deserialization benefits. | ||
-- The deserialization of this type looks at the | ||
-- major protocol version and the script language to determine if | ||
-- indeed the script is runnable. This is a dramatic improvement over the old api | ||
-- which essentially read a 'ByteString' and hoped for the best. | ||
-- Any failures due to malformed/invalid scripts were caught upon transaction | ||
-- submission or running the script when attempting to predict the necessary execution units. | ||
-- | ||
-- Where do we get the major protocol version from? | ||
-- In order to access the major protocol version we pass in an 'era` type parameter which | ||
-- can be translated to the major protocol version. | ||
-- | ||
-- Where do we get the script language from? | ||
-- The serialized version of 'PlutusRunnable' encodes the script language. | ||
-- See `DecCBOR (PlutusRunnable l)` in cardano-ledger for more details. | ||
data PlutusScriptInEra (lang :: L.Language) era where | ||
PlutusScriptInEra :: PlutusRunnable lang -> PlutusScriptInEra lang era | ||
|
||
deriving instance Show (PlutusScriptInEra lang era) | ||
|
||
-- | You can provide the plutus script directly in the transaction | ||
-- or a reference input that points to the script in the UTxO. | ||
-- Using a reference script saves space in your transaction. | ||
data PlutusScriptOrReferenceInput lang era | ||
= PScript (PlutusScriptInEra lang era) | ||
| PReferenceScript TxIn | ||
deriving Show |
Oops, something went wrong.