Skip to content

Commit 94bc250

Browse files
authored
Merge pull request #5263 from IntersectMBO/nm/4183-babbagefeatures-to-imp
Move Alonzo ExUnits tests from cardano-ledger-test to Alonzo testlib
2 parents dd14d69 + ae68fa1 commit 94bc250

File tree

7 files changed

+72
-284
lines changed

7 files changed

+72
-284
lines changed

eras/alonzo/impl/cardano-ledger-alonzo.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ library testlib
164164
heredoc,
165165
microlens,
166166
microlens-mtl,
167+
mtl,
167168
plutus-ledger-api,
168169
serialise,
169170
text,

eras/alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/Imp/UtxosSpec.hs

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{-# LANGUAGE DataKinds #-}
33
{-# LANGUAGE FlexibleContexts #-}
44
{-# LANGUAGE GADTs #-}
5+
{-# LANGUAGE NamedFieldPuns #-}
56
{-# LANGUAGE OverloadedLists #-}
67
{-# LANGUAGE OverloadedStrings #-}
78
{-# LANGUAGE RankNTypes #-}
@@ -11,20 +12,30 @@
1112
module Test.Cardano.Ledger.Alonzo.Imp.UtxosSpec (spec) where
1213

1314
import Cardano.Ledger.Alonzo.Core
14-
import Cardano.Ledger.Alonzo.Plutus.Evaluate (CollectError (NoCostModel))
15+
import Cardano.Ledger.Alonzo.Plutus.Evaluate (
16+
CollectError (NoCostModel),
17+
TransactionScriptFailure (RedeemerPointsToUnknownScriptHash),
18+
evalTxExUnits,
19+
)
1520
import Cardano.Ledger.Alonzo.Rules (
1621
AlonzoUtxosPredFailure (..),
1722
TagMismatchDescription (..),
1823
)
19-
import Cardano.Ledger.Alonzo.Scripts (eraLanguages)
24+
import Cardano.Ledger.Alonzo.Scripts (ExUnits (..), eraLanguages)
2025
import Cardano.Ledger.Alonzo.TxWits (unRedeemersL)
26+
import Cardano.Ledger.BaseTypes (Globals (..), StrictMaybe (..))
2127
import Cardano.Ledger.Plutus.Data (Data (..))
2228
import Cardano.Ledger.Plutus.Language (hashPlutusScript, withSLanguage)
2329
import Cardano.Ledger.Shelley.LedgerState (curPParamsEpochStateL, nesEsL)
30+
import Control.Monad.Reader (asks)
31+
import Data.Either (isLeft)
32+
import qualified Data.Map.Merge.Strict as Map
2433
import qualified Data.Map.Strict as Map
2534
import qualified Data.Set as Set
26-
import Lens.Micro ((&), (.~), (<>~))
35+
import Lens.Micro (set, (%~), (&), (.~), (<>~), (^.), _2)
36+
import Lens.Micro.Mtl (use)
2737
import qualified PlutusLedgerApi.Common as P
38+
import qualified PlutusLedgerApi.V1 as PV1
2839
import Test.Cardano.Ledger.Alonzo.ImpTest
2940
import Test.Cardano.Ledger.Imp.Common
3041
import Test.Cardano.Ledger.Plutus.Examples (
@@ -44,8 +55,8 @@ spec ::
4455
SpecWith (ImpInit (LedgerSpec era))
4556
spec = describe "UTXOS" $
4657
forM_ (eraLanguages @era) $ \lang ->
47-
withSLanguage lang $ \slang ->
48-
describe (show lang) $ do
58+
describe (show lang) $
59+
withSLanguage lang $ \slang -> do
4960
let redeemerSameAsDatumHash = hashPlutusScript $ redeemerSameAsDatum slang
5061
alwaysSucceedsWithDatumHash = hashPlutusScript $ alwaysSucceedsWithDatum slang
5162

@@ -56,6 +67,57 @@ spec = describe "UTXOS" $
5667
, ("inputsOutputsAreNotEmptyWithDatum", inputsOutputsAreNotEmptyWithDatum)
5768
]
5869

70+
describe "ExUnits" $ do
71+
it "Calculate ExUnits" $ do
72+
let
73+
overrideExUnits tx = do
74+
pp <- getsNES $ nesEsL . curPParamsEpochStateL
75+
utxo <- getUTxO
76+
Globals {epochInfo, systemStart} <- use impGlobalsL
77+
purposeUnits <-
78+
either (fail . show) pure . sequence $
79+
evalTxExUnits pp tx utxo epochInfo systemStart
80+
pure $ tx & witsTxL . rdmrsTxWitsL . unRedeemersL %~ spliceUnits purposeUnits
81+
spliceUnits =
82+
Map.merge
83+
Map.dropMissing -- Ignore purposes not already in the redeemers
84+
Map.preserveMissing -- Don't touch purposes not being updated
85+
(Map.zipWithMatched $ \_ -> set _2) -- Replace the units, keep the datum
86+
redoAddrWits = updateAddrTxWits . (witsTxL . addrTxWitsL .~ mempty)
87+
88+
txIn <- produceScript alwaysSucceedsWithDatumHash
89+
withPostFixup (overrideExUnits >=> fixupPPHash >=> redoAddrWits) $
90+
submitTx_ $
91+
mkBasicTx mkBasicTxBody & bodyTxL . inputsTxBodyL .~ [txIn]
92+
93+
it "Attempt to calculate ExUnits with an invalid tx" $ do
94+
txIn <- produceScript alwaysSucceedsWithDatumHash
95+
let tx = mkBasicTx mkBasicTxBody & bodyTxL . inputsTxBodyL .~ [txIn]
96+
97+
txFixed <- (tx &) =<< asks iteFixup
98+
logToExpr txFixed
99+
100+
let
101+
twiddleIx (SJust (SpendingPurpose (AsIx 0))) = SpendingPurpose (AsIx 1)
102+
twiddleIx _ = SpendingPurpose (AsIx 0)
103+
badPurpose =
104+
twiddleIx $
105+
redeemerPointer (txFixed ^. bodyTxL) (SpendingPurpose $ AsItem txIn)
106+
du = (Data $ PV1.I 42, ExUnits 5000 5000)
107+
txBorked =
108+
txFixed
109+
& witsTxL . rdmrsTxWitsL . unRedeemersL %~ Map.insert badPurpose du
110+
logToExpr txBorked
111+
112+
pp <- getsNES $ nesEsL . curPParamsEpochStateL
113+
utxo <- getUTxO
114+
Globals {epochInfo, systemStart} <- use impGlobalsL
115+
let report = evalTxExUnits pp txBorked utxo epochInfo systemStart
116+
logToExpr report
117+
118+
Map.filter isLeft report
119+
`shouldBe` Map.singleton badPurpose (Left (RedeemerPointsToUnknownScriptHash badPurpose))
120+
59121
describe "Spending scripts with a Datum" $ do
60122
forM_ scripts $ \(name, script) -> do
61123
it name $ do

eras/alonzo/impl/testlib/Test/Cardano/Ledger/Alonzo/ImpTest.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import Cardano.Ledger.Address (Addr (..))
4848
import Cardano.Ledger.Alonzo (AlonzoEra)
4949
import Cardano.Ledger.Alonzo.Core
5050
import Cardano.Ledger.Alonzo.Genesis (AlonzoGenesis (..))
51+
import Cardano.Ledger.Alonzo.Plutus.Context (ContextError)
5152
import Cardano.Ledger.Alonzo.Plutus.Evaluate (
5253
collectPlutusScriptsWithContext,
5354
evalPlutusScriptsWithLogs,
@@ -119,6 +120,8 @@ class
119120
, AlonzoEraTest era
120121
, ScriptsNeeded era ~ AlonzoScriptsNeeded era
121122
, TxAuxData era ~ AlonzoTxAuxData era
123+
, ToExpr (ContextError era)
124+
, ToExpr (PlutusPurpose AsItem era)
122125
) =>
123126
AlonzoEraImp era
124127
where

libs/cardano-ledger-test/cardano-ledger-test.cabal

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ source-repository head
1616

1717
library
1818
exposed-modules:
19-
Test.Cardano.Ledger.Alonzo.Tools
2019
Test.Cardano.Ledger.Constrained.Conway
2120
Test.Cardano.Ledger.Constrained.Conway.Cert
2221
Test.Cardano.Ledger.Constrained.Conway.Certs

libs/cardano-ledger-test/src/Test/Cardano/Ledger/Alonzo/Tools.hs

Lines changed: 0 additions & 255 deletions
This file was deleted.

0 commit comments

Comments
 (0)