Skip to content

Commit

Permalink
Merge pull request #3960 from IntersectMBO/lehins/plutus-script-purpose
Browse files Browse the repository at this point in the history
Plutus script purpose
  • Loading branch information
lehins authored Jan 16, 2024
2 parents 94e5534 + d223ee4 commit 518a2d5
Show file tree
Hide file tree
Showing 80 changed files with 2,182 additions and 1,310 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ If you are looking for the Ledger Releasing and Versioning Process then you can
- Remove the library libs/cardano-ledger-pretty

## 8.7

- Fix `PParamsUpdate` governance action ratification. Votes of DReps are now accounted for.
- Move CDDL specification files from test packages into libraries that actually implement each era.
- Add ability to retain Plutus logs for debugging when running scripts
Expand Down
2 changes: 1 addition & 1 deletion cabal.project
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ index-state:
-- Bump this if you need newer packages from Hackage
, hackage.haskell.org 2024-01-04T21:59:02Z
-- Bump this if you need newer packages from CHaP
, cardano-haskell-packages 2024-01-04T09:36:23Z
, cardano-haskell-packages 2024-01-16T10:58:01Z

packages:
eras/allegra/impl
Expand Down
11 changes: 11 additions & 0 deletions eras/alonzo/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@

## 1.6.0.0

* Deprecated `getAlonzoSpendingTxIn` in favor of `plutusPurposeSpendingTxIn`
* Add `PlutusPurpose`, `plutusPurposeSpendingTxIn` and `upgradePlutusPurposeAsIndex` to
`AlonzoEraScript`.
* Add `AsIndex`, `AsItem` and `AlonzoPlutusPurpose`
* Remove `RdmrPtr` and `Tag` in favor of `PlutusPurpose AsIndex`
* Remove `ScriptPurpose` in favor of `PlutusPurpose AsItem`
* Add `AlonzoTxAuxData'`, `atadMetadata'`, `atadTimelock'`, `atadPlutus'`
* Add `alonzoRedeemerPointer`, `alonzoRedeemerPointerInverse`
* Remove `rdptr` and `rdptrInv` in favor of `redeemerPointer` and `redeemerPointerInverse`
respectively
* Rename `indexedRdmrs` to `indexRedeemers` and change the type of one of its arguments
* Deprecate `requiredSignersAreWitnessed`
* Add `ToJSON` for `AlonzoContextError` and `CollectError`
* Stop exporting all of the internal `hkd*` functions and `PParamsHKD` from
Expand Down
2 changes: 1 addition & 1 deletion eras/alonzo/impl/cardano-ledger-alonzo.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ library
mtl,
microlens,
nothunks,
plutus-ledger-api ^>=1.19,
plutus-ledger-api ^>=1.20,
set-algebra >=1.0,
small-steps >=1.0,
text,
Expand Down
4 changes: 3 additions & 1 deletion eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Core.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module Cardano.Ledger.Alonzo.Core (
AlonzoEraTx (..),
AlonzoEraTxOut (..),
AlonzoEraScript (..),
AsIndex (..),
AsItem (..),
ScriptIntegrityHash,
AlonzoEraTxBody (..),
AlonzoEraTxWits (..),
Expand Down Expand Up @@ -47,7 +49,7 @@ import Cardano.Ledger.Alonzo.PParams (
ppuMaxValSizeL,
ppuPricesL,
)
import Cardano.Ledger.Alonzo.Scripts (AlonzoEraScript (..))
import Cardano.Ledger.Alonzo.Scripts (AlonzoEraScript (..), AsIndex (..), AsItem (..))
import Cardano.Ledger.Alonzo.Tx (AlonzoEraTx (..))
import Cardano.Ledger.Alonzo.TxBody (AlonzoEraTxBody (..), ScriptIntegrityHash)
import Cardano.Ledger.Alonzo.TxOut (AlonzoEraTxOut (..))
Expand Down
16 changes: 10 additions & 6 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Plutus/Context.hs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ module Cardano.Ledger.Alonzo.Plutus.Context (
)
where

import Cardano.Ledger.Alonzo.Scripts (AlonzoEraScript, PlutusScript (..))
import Cardano.Ledger.Alonzo.Tx (ScriptPurpose (..))
import Cardano.Ledger.Alonzo.Scripts (
AlonzoEraScript,
AsItem (..),
PlutusPurpose,
PlutusScript (..),
)
import Cardano.Ledger.Binary (DecCBOR (..), EncCBOR (..))
import Cardano.Ledger.Core
import Cardano.Ledger.Plutus.Data (Data (..))
Expand All @@ -46,7 +50,7 @@ class (PlutusLanguage l, EraPlutusContext era) => EraPlutusTxInfo (l :: Language

toPlutusScriptPurpose ::
proxy l ->
ScriptPurpose era ->
PlutusPurpose AsItem era ->
Either (ContextError era) (PlutusScriptPurpose l)

toPlutusTxInfo ::
Expand All @@ -61,7 +65,7 @@ class (PlutusLanguage l, EraPlutusContext era) => EraPlutusTxInfo (l :: Language
toPlutusScriptContext ::
proxy l ->
PlutusTxInfo l ->
ScriptPurpose era ->
PlutusPurpose AsItem era ->
Either (ContextError era) (PlutusScriptContext l)

class
Expand All @@ -80,7 +84,7 @@ class

mkPlutusScriptContext ::
PlutusScript era ->
ScriptPurpose era ->
PlutusPurpose AsItem era ->
PParams era ->
EpochInfo (Either Text) ->
SystemStart ->
Expand All @@ -91,7 +95,7 @@ class
mkPlutusLanguageContext ::
(EraPlutusTxInfo l era, P.ToData (PlutusScriptContext l)) =>
proxy l ->
ScriptPurpose era ->
PlutusPurpose AsItem era ->
PParams era ->
EpochInfo (Either Text) ->
SystemStart ->
Expand Down
21 changes: 10 additions & 11 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Plutus/Evaluate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ where
import Cardano.Ledger.Alonzo.Core
import Cardano.Ledger.Alonzo.Plutus.Context (EraPlutusContext (..))
import Cardano.Ledger.Alonzo.Scripts (plutusScriptLanguage)
import Cardano.Ledger.Alonzo.Tx (ScriptPurpose (..), indexedRdmrs)
import Cardano.Ledger.Alonzo.Tx (indexRedeemers)
import Cardano.Ledger.Alonzo.UTxO (AlonzoEraUTxO (getSpendingDatum), AlonzoScriptsNeeded (..))
import Cardano.Ledger.BaseTypes (ProtVer (pvMajor), kindObject, natVersion)
import Cardano.Ledger.Binary (DecCBOR (..), EncCBOR (..))
Expand Down Expand Up @@ -60,31 +60,31 @@ import NoThunks.Class (NoThunks)

-- | When collecting inputs for two phase scripts, 3 things can go wrong.
data CollectError era
= NoRedeemer !(ScriptPurpose era)
= NoRedeemer !(PlutusPurpose AsItem era)
| NoWitness !(ScriptHash (EraCrypto era))
| NoCostModel !Language
| BadTranslation !(ContextError era)
deriving (Generic)

deriving instance
(Era era, Eq (TxCert era), Eq (ContextError era)) =>
(AlonzoEraScript era, Eq (ContextError era)) =>
Eq (CollectError era)

deriving instance
(Era era, Show (TxCert era), Show (ContextError era)) =>
(AlonzoEraScript era, Show (ContextError era)) =>
Show (CollectError era)

deriving instance
(Era era, NoThunks (TxCert era), NoThunks (ContextError era)) =>
(AlonzoEraScript era, NoThunks (ContextError era)) =>
NoThunks (CollectError era)

instance (EraTxCert era, EncCBOR (ContextError era)) => EncCBOR (CollectError era) where
instance (AlonzoEraScript era, EncCBOR (ContextError era)) => EncCBOR (CollectError era) where
encCBOR (NoRedeemer x) = encode $ Sum NoRedeemer 0 !> To x
encCBOR (NoWitness x) = encode $ Sum (NoWitness @era) 1 !> To x
encCBOR (NoCostModel x) = encode $ Sum NoCostModel 2 !> To x
encCBOR (BadTranslation x) = encode $ Sum (BadTranslation @era) 3 !> To x

instance (EraTxCert era, DecCBOR (ContextError era)) => DecCBOR (CollectError era) where
instance (AlonzoEraScript era, DecCBOR (ContextError era)) => DecCBOR (CollectError era) where
decCBOR = decode (Summands "CollectError" dec)
where
dec 0 = SumD NoRedeemer <! From
Expand All @@ -95,7 +95,7 @@ instance (EraTxCert era, DecCBOR (ContextError era)) => DecCBOR (CollectError er

instance
( Era era
, ToJSON (ScriptPurpose era)
, ToJSON (PlutusPurpose AsItem era)
, ToJSON (ContextError era)
) =>
ToJSON (CollectError era)
Expand Down Expand Up @@ -132,11 +132,10 @@ lookupPlutusScript scriptsAvailable scriptHash = do

collectPlutusScriptsWithContext ::
forall era.
( MaryEraTxBody era
( AlonzoEraTxBody era
, AlonzoEraTxWits era
, AlonzoEraUTxO era
, ScriptsNeeded era ~ AlonzoScriptsNeeded era
, AlonzoEraPParams era
, EraPlutusContext era
) =>
EpochInfo (Either Text) ->
Expand Down Expand Up @@ -173,7 +172,7 @@ collectPlutusScriptsWithContext epochInfo sysStart pp tx utxo =
usedLanguages = Set.fromList $ map (plutusScriptLanguage . snd) neededPlutusScripts

getScriptWithRedeemer (sp, script) =
case indexedRdmrs tx sp of
case indexRedeemers tx sp of
Just (d, eu) -> Right (script, sp, d, eu)
Nothing -> Left (NoRedeemer sp)
apply (plutusScript, scriptPurpose, d, eu) = do
Expand Down
40 changes: 15 additions & 25 deletions eras/alonzo/impl/src/Cardano/Ledger/Alonzo/Plutus/TxInfo.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ module Cardano.Ledger.Alonzo.Plutus.TxInfo (
transDataPair,
transTxCert,
transTxCertCommon,
transScriptPurpose,
transPlutusPurpose,
transTxBodyId,
transTxBodyCerts,
transTxBodyWithdrawals,
Expand All @@ -45,19 +45,11 @@ module Cardano.Ledger.Alonzo.Plutus.TxInfo (
where

import Cardano.Crypto.Hash.Class (hashToBytes)
import Cardano.Ledger.Address (RewardAcnt (..), Withdrawals (..))
import Cardano.Ledger.Allegra.Scripts (ValidityInterval (..))
import Cardano.Ledger.Alonzo.Core
import Cardano.Ledger.Alonzo.Era (AlonzoEra)
import Cardano.Ledger.Alonzo.Plutus.Context
import Cardano.Ledger.Alonzo.Scripts (PlutusScript (..))
import Cardano.Ledger.Alonzo.Tx (ScriptPurpose (..))
import Cardano.Ledger.Alonzo.TxBody (
AlonzoEraTxBody (..),
AlonzoEraTxOut (..),
mintTxBodyL,
vldtTxBodyL,
)
import Cardano.Ledger.Alonzo.TxWits (AlonzoEraTxWits (..), unTxDats)
import Cardano.Ledger.Alonzo.Scripts (AlonzoPlutusPurpose (..), PlutusScript (..))
import Cardano.Ledger.Alonzo.TxWits (unTxDats)
import Cardano.Ledger.BaseTypes (StrictMaybe (..), strictMaybeToMaybe)
import Cardano.Ledger.Binary (DecCBOR (..), EncCBOR (..))
import Cardano.Ledger.Binary.Coders (
Expand All @@ -69,7 +61,6 @@ import Cardano.Ledger.Binary.Coders (
(<!),
)
import Cardano.Ledger.Coin (Coin (..))
import Cardano.Ledger.Core
import Cardano.Ledger.Crypto (Crypto)
import Cardano.Ledger.Mary.Value (
AssetName (..),
Expand All @@ -83,7 +74,6 @@ import Cardano.Ledger.PoolParams (PoolParams (..))
import Cardano.Ledger.Rules.ValidationMode (Inject (..))
import Cardano.Ledger.SafeHash (hashAnnotated)
import qualified Cardano.Ledger.Shelley.HardForks as HardForks
import Cardano.Ledger.Shelley.TxCert
import Cardano.Ledger.TxIn (TxIn (..), txInToText)
import Cardano.Ledger.UTxO (UTxO (..))
import Cardano.Ledger.Val (zero)
Expand All @@ -108,7 +98,7 @@ import qualified PlutusLedgerApi.V1 as PV1
instance Crypto c => EraPlutusTxInfo 'PlutusV1 (AlonzoEra c) where
toPlutusTxCert _ = pure . transTxCert

toPlutusScriptPurpose = transScriptPurpose
toPlutusScriptPurpose = transPlutusPurpose

toPlutusTxInfo proxy pp epochInfo systemStart utxo tx = do
timeRange <- transValidityInterval pp epochInfo systemStart (txBody ^. vldtTxBodyL)
Expand Down Expand Up @@ -246,8 +236,8 @@ transTxBodyCerts proxy txBody =
transWithdrawals :: Withdrawals c -> Map.Map PV1.StakingCredential Integer
transWithdrawals (Withdrawals mp) = Map.foldlWithKey' accum Map.empty mp
where
accum ans (RewardAcnt _networkId cred) (Coin n) =
Map.insert (PV1.StakingHash (transCred cred)) n ans
accum ans rewardAccount (Coin n) =
Map.insert (PV1.StakingHash (transRewardAccount rewardAccount)) n ans

-- | Translate all `Withdrawal`s from within a `TxBody`
transTxBodyWithdrawals :: EraTxBody era => TxBody era -> [(PV1.StakingCredential, Integer)]
Expand Down Expand Up @@ -326,14 +316,14 @@ transTxCertCommon = \case
Just $ PV1.DCertPoolRetire (transKeyHash poolId) (toInteger i)
_ -> Nothing

transScriptPurpose ::
transPlutusPurpose ::
(EraPlutusTxInfo l era, PlutusTxCert l ~ PV1.DCert) =>
proxy l ->
ScriptPurpose era ->
AlonzoPlutusPurpose AsItem era ->
Either (ContextError era) PV1.ScriptPurpose
transScriptPurpose proxy = \case
Minting policyId -> pure $ PV1.Minting (transPolicyID policyId)
Spending txIn -> pure $ PV1.Spending (transTxIn txIn)
Rewarding (RewardAcnt _networkId cred) ->
pure $ PV1.Rewarding (PV1.StakingHash (transCred cred))
Certifying txCert -> PV1.Certifying <$> toPlutusTxCert proxy txCert
transPlutusPurpose proxy = \case
AlonzoSpending (AsItem txIn) -> pure $ PV1.Spending (transTxIn txIn)
AlonzoMinting (AsItem policyId) -> pure $ PV1.Minting (transPolicyID policyId)
AlonzoCertifying (AsItem txCert) -> PV1.Certifying <$> toPlutusTxCert proxy txCert
AlonzoRewarding (AsItem rewardAccount) ->
pure $ PV1.Rewarding (PV1.StakingHash (transRewardAccount rewardAccount))
Loading

0 comments on commit 518a2d5

Please sign in to comment.