Skip to content

Commit ced69af

Browse files
committed
Refactor the transaction validity interval test.
* Remove the incomplete, and now redundant `transVITimeUpperBoundIsClosed`. * Use `toPlutusTxInfo` to comprehensively test for all protocol versions. * The new test runs for all eras: alonzo, babbage and conway. NOTE: This test only works for PlutusV1 since we do not yet have common accessors accross common data types from different versions of Plutus.
1 parent 74e2b99 commit ced69af

File tree

10 files changed

+63
-47
lines changed

10 files changed

+63
-47
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,5 @@ test-suite tests
252252
cardano-slotting,
253253
cardano-strict-containers,
254254
containers,
255-
plutus-ledger-api,
256255
testlib,
257256
time,

eras/alonzo/impl/test/Test/Cardano/Ledger/Alonzo/TxInfoSpec.hs

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ module Test.Cardano.Ledger.Alonzo.TxInfoSpec (spec) where
99
import Cardano.Ledger.Address (Addr (..))
1010
import Cardano.Ledger.Alonzo (AlonzoEra, Tx (..))
1111
import Cardano.Ledger.Alonzo.Core
12-
import Cardano.Ledger.Alonzo.Plutus.Context (
13-
ContextError,
14-
LedgerTxInfo (..),
15-
toPlutusTxInfo,
16-
)
17-
import Cardano.Ledger.Alonzo.Plutus.TxInfo (transValidityInterval)
12+
import Cardano.Ledger.Alonzo.Plutus.Context (LedgerTxInfo (..), toPlutusTxInfo)
1813
import Cardano.Ledger.Alonzo.Tx (AlonzoTx (..))
1914
import Cardano.Ledger.Alonzo.TxBody (AlonzoTxOut (..), TxBody (..))
2015
import Cardano.Ledger.BaseTypes (Network (..), StrictMaybe (..))
@@ -27,14 +22,12 @@ import Cardano.Ledger.State (UTxO (..))
2722
import Cardano.Ledger.TxIn (TxId (..), TxIn (..), mkTxInPartial)
2823
import qualified Cardano.Ledger.Val as Val
2924
import Cardano.Slotting.EpochInfo (EpochInfo, fixedEpochInfo)
30-
import Cardano.Slotting.Slot (EpochSize (..), SlotNo (..))
25+
import Cardano.Slotting.Slot (EpochSize (..))
3126
import Cardano.Slotting.Time (SystemStart (..), mkSlotLength)
3227
import qualified Data.Map.Strict as Map
33-
import Data.Proxy
3428
import qualified Data.Sequence.Strict as StrictSeq
3529
import qualified Data.Set as Set
3630
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
37-
import qualified PlutusLedgerApi.V1 as PV1
3831
import Test.Cardano.Ledger.Binary.Random (mkDummyHash)
3932
import Test.Cardano.Ledger.Common
4033
import Test.Cardano.Ledger.Core.KeyPair (KeyPair (..), mkCredential, mkKeyPair)
@@ -104,20 +97,6 @@ silentlyIgnore tx =
10497
Right _ -> pure ()
10598
Left e -> expectationFailure $ "no translation error was expected, but got: " <> show e
10699

107-
-- | The test checks that the old implementation of 'transVITime' stays intentionally incorrect,
108-
-- by returning close upper bound of the validaty interval.
109-
transVITimeUpperBoundIsClosed :: Expectation
110-
transVITimeUpperBoundIsClosed = do
111-
let interval = ValidityInterval SNothing (SJust (SlotNo 40))
112-
case transValidityInterval (Proxy @AlonzoEra) ei ss interval of
113-
Left (e :: ContextError AlonzoEra) ->
114-
expectationFailure $ "no translation error was expected, but got: " <> show e
115-
Right t ->
116-
t
117-
`shouldBe` PV1.Interval
118-
(PV1.LowerBound PV1.NegInf True)
119-
(PV1.UpperBound (PV1.Finite (PV1.POSIXTime 40000)) True)
120-
121100
spec :: Spec
122101
spec = describe "txInfo translation" $ do
123102
-- TODO: convert to Imp: https://github.com/IntersectMBO/cardano-ledger/issues/5210
@@ -126,9 +105,6 @@ spec = describe "txInfo translation" $ do
126105
silentlyIgnore (txEx shelleyInput byronOutput)
127106
it "silently ignore byron txin" $
128107
silentlyIgnore (txEx byronInput shelleyOutput)
129-
describe "transVITime" $ do
130-
it "validity interval's upper bound is closed when protocol < 9" $
131-
transVITimeUpperBoundIsClosed
132108

133109
genesisId :: TxId
134110
genesisId = TxId (unsafeMakeSafeHash (mkDummyHash (0 :: Int)))

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{-# LANGUAGE DataKinds #-}
12
{-# LANGUAGE FlexibleContexts #-}
23
{-# LANGUAGE TypeFamilies #-}
34
{-# LANGUAGE TypeOperators #-}
@@ -30,6 +31,7 @@ class
3031
, ToExpr (PlutusPurpose AsIx era)
3132
, ToExpr (PlutusPurpose AsIxItem era)
3233
, Script era ~ AlonzoScript era
34+
, EraPlutusTxInfo PlutusV1 era
3335
) =>
3436
AlonzoEraTest era
3537

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

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
module Test.Cardano.Ledger.Alonzo.Imp.UtxosSpec (spec) where
1313

1414
import Cardano.Ledger.Alonzo.Core
15+
import Cardano.Ledger.Alonzo.Plutus.Context (LedgerTxInfo (..), toPlutusTxInfo)
1516
import Cardano.Ledger.Alonzo.Plutus.Evaluate (
1617
CollectError (NoCostModel),
1718
TransactionScriptFailure (RedeemerPointsToUnknownScriptHash),
@@ -23,16 +24,26 @@ import Cardano.Ledger.Alonzo.Rules (
2324
)
2425
import Cardano.Ledger.Alonzo.Scripts (ExUnits (..), eraLanguages)
2526
import Cardano.Ledger.Alonzo.TxWits (unRedeemersL)
26-
import Cardano.Ledger.BaseTypes (Globals (..), StrictMaybe (..))
27+
import Cardano.Ledger.BaseTypes (
28+
Globals (..),
29+
ProtVer (..),
30+
SlotNo (..),
31+
StrictMaybe (..),
32+
natVersion,
33+
)
2734
import Cardano.Ledger.Plutus.Data (Data (..))
2835
import Cardano.Ledger.Plutus.Language (hashPlutusScript, withSLanguage)
36+
import qualified Cardano.Ledger.Plutus.Language as L
2937
import Cardano.Ledger.Shelley.LedgerState (curPParamsEpochStateL, nesEsL)
38+
import Cardano.Slotting.Time (SystemStart (SystemStart))
3039
import Control.Monad.Reader (asks)
3140
import Data.Either (isLeft)
3241
import qualified Data.Map.Merge.Strict as Map
3342
import qualified Data.Map.Strict as Map
43+
import Data.Proxy (Proxy (Proxy))
3444
import qualified Data.Set as Set
35-
import Lens.Micro (set, (%~), (&), (.~), (<>~), (^.), _2)
45+
import Data.Time.Clock.POSIX (utcTimeToPOSIXSeconds)
46+
import Lens.Micro (set, to, (%~), (&), (.~), (<>~), (^.), _2)
3647
import Lens.Micro.Mtl (use)
3748
import qualified PlutusLedgerApi.Common as P
3849
import qualified PlutusLedgerApi.V1 as PV1
@@ -53,7 +64,42 @@ spec ::
5364
, InjectRuleFailure "LEDGER" AlonzoUtxosPredFailure era
5465
) =>
5566
SpecWith (ImpInit (LedgerSpec era))
56-
spec = describe "UTXOS" $
67+
spec = describe "UTXOS" $ do
68+
it
69+
"transaction validity interval has closed upper bound when protocol version < 9 and open otherwise"
70+
$ do
71+
ei <- use $ impGlobalsL . to epochInfo
72+
ss@(SystemStart sysStart) <- use $ impGlobalsL . to systemStart
73+
SlotNo currentSlot <- use impLastTickG
74+
protVer <- getProtVer
75+
utxo <- getUTxO
76+
let txValidity = 7200
77+
-- We must provide a non-Nothing upper bound so that the "closed" vs "open" case can be tested.
78+
interval = ValidityInterval SNothing $ SJust $ SlotNo $ currentSlot + txValidity
79+
startPOSIX = floor $ utcTimeToPOSIXSeconds sysStart
80+
expectedUpperBound = (startPOSIX + fromIntegral (currentSlot + txValidity)) * 1000
81+
tx = mkBasicTx mkBasicTxBody & bodyTxL . vldtTxBodyL .~ interval
82+
lti =
83+
LedgerTxInfo
84+
{ ltiProtVer = protVer
85+
, ltiEpochInfo = ei
86+
, ltiSystemStart = ss
87+
, ltiUTxO = utxo
88+
, ltiTx = tx
89+
}
90+
case toPlutusTxInfo (Proxy @L.PlutusV1) lti of
91+
Left e -> assertFailure $ "No translation error was expected, but got: " <> show e
92+
Right txInfo ->
93+
PV1.txInfoValidRange txInfo
94+
`shouldBe` PV1.Interval
95+
(PV1.LowerBound PV1.NegInf True)
96+
( PV1.UpperBound
97+
( PV1.Finite
98+
(PV1.POSIXTime expectedUpperBound)
99+
)
100+
(pvMajor protVer < natVersion @9) -- The upper bound.
101+
)
102+
57103
forM_ (eraLanguages @era) $ \lang ->
58104
describe (show lang) $
59105
withSLanguage lang $ \slang -> do

eras/babbage/impl/testlib/Test/Cardano/Ledger/Babbage/Era.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE FlexibleContexts #-}
13
{-# LANGUAGE UndecidableSuperClasses #-}
24
{-# OPTIONS_GHC -Wno-orphans #-}
35

@@ -6,6 +8,7 @@ module Test.Cardano.Ledger.Babbage.Era (
68
BabbageEraTest,
79
) where
810

11+
import Cardano.Ledger.Alonzo.Plutus.Context (EraPlutusTxInfo)
912
import Cardano.Ledger.Babbage
1013
import Cardano.Ledger.Babbage.Core
1114
import Cardano.Ledger.Plutus (Language (..))
@@ -18,6 +21,7 @@ class
1821
( AlonzoEraTest era
1922
, BabbageEraTxBody era
2023
, BabbageEraPParams era
24+
, EraPlutusTxInfo PlutusV2 era
2125
) =>
2226
BabbageEraTest era
2327

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Era.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE FlexibleContexts #-}
23
{-# LANGUAGE UndecidableSuperClasses #-}
34
{-# OPTIONS_GHC -Wno-orphans #-}
45

@@ -9,6 +10,7 @@ module Test.Cardano.Ledger.Conway.Era (
910
conwayAccountsToUMap,
1011
) where
1112

13+
import Cardano.Ledger.Alonzo.Plutus.Context (EraPlutusTxInfo)
1214
import Cardano.Ledger.BaseTypes
1315
import Cardano.Ledger.Coin
1416
import Cardano.Ledger.Conway
@@ -32,6 +34,7 @@ class
3234
, ConwayEraCertState era
3335
, ConwayEraGov era
3436
, ConwayEraAccounts era
37+
, EraPlutusTxInfo PlutusV3 era
3538
) =>
3639
ConwayEraTest era
3740

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Imp.hs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88

99
module Test.Cardano.Ledger.Conway.Imp (spec, conwaySpec) where
1010

11-
import Cardano.Ledger.Alonzo.Plutus.Context (
12-
EraPlutusContext (ContextError),
13-
EraPlutusTxInfo,
14-
)
11+
import Cardano.Ledger.Alonzo.Plutus.Context (EraPlutusContext (ContextError))
1512
import Cardano.Ledger.Alonzo.Rules (
1613
AlonzoUtxoPredFailure,
1714
AlonzoUtxosPredFailure,
@@ -35,7 +32,6 @@ import Cardano.Ledger.Conway.Rules (
3532
ConwayUtxowPredFailure,
3633
)
3734
import Cardano.Ledger.Conway.TxInfo (ConwayContextError)
38-
import Cardano.Ledger.Plutus (Language (..))
3935
import Cardano.Ledger.Shelley.API.Mempool (ApplyTx (..))
4036
import Cardano.Ledger.Shelley.Rules (
4137
ShelleyDelegPredFailure,
@@ -94,7 +90,6 @@ spec ::
9490
, Eq (Event (EraRule "ENACT" era))
9591
, Typeable (Event (EraRule "ENACT" era))
9692
, ToExpr (Event (EraRule "BBODY" era))
97-
, EraPlutusTxInfo PlutusV2 era
9893
) =>
9994
Spec
10095
spec = do
@@ -128,7 +123,6 @@ conwaySpec ::
128123
, Eq (Event (EraRule "ENACT" era))
129124
, Typeable (Event (EraRule "ENACT" era))
130125
, ToExpr (Event (EraRule "BBODY" era))
131-
, EraPlutusTxInfo PlutusV2 era
132126
) =>
133127
SpecWith (ImpInit (LedgerSpec era))
134128
conwaySpec = do

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Imp/UtxowSpec.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
module Test.Cardano.Ledger.Conway.Imp.UtxowSpec (spec) where
1010

1111
import Cardano.Ledger.Address (Addr (..))
12-
import Cardano.Ledger.Alonzo.Plutus.Context (EraPlutusTxInfo)
1312
import Cardano.Ledger.Babbage.Tx (ScriptIntegrity (..), getLanguageView)
1413
import Cardano.Ledger.BaseTypes (
1514
Inject (..),
@@ -47,7 +46,6 @@ spec ::
4746
forall era.
4847
( ConwayEraImp era
4948
, InjectRuleFailure "LEDGER" ConwayUtxowPredFailure era
50-
, EraPlutusTxInfo PlutusV2 era
5149
) =>
5250
SpecWith (ImpInit (LedgerSpec era))
5351
spec = do

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/Spec.hs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
module Test.Cardano.Ledger.Conway.Spec (spec) where
1010

11-
import Cardano.Ledger.Alonzo.Plutus.Context (EraPlutusContext (..), EraPlutusTxInfo)
11+
import Cardano.Ledger.Alonzo.Plutus.Context (EraPlutusContext (..))
1212
import Cardano.Ledger.Alonzo.Rules (
1313
AlonzoUtxoPredFailure,
1414
AlonzoUtxosPredFailure,
@@ -45,7 +45,6 @@ import Cardano.Ledger.Conway.Rules (
4545
)
4646
import Cardano.Ledger.Conway.TxCert (ConwayTxCert)
4747
import Cardano.Ledger.Conway.TxInfo (ConwayContextError)
48-
import Cardano.Ledger.Plutus (Language (..))
4948
import Cardano.Ledger.Plutus.Language (SLanguage (..))
5049
import Cardano.Ledger.Shelley.API (ApplyTx)
5150
import Cardano.Ledger.Shelley.LedgerState (StashedAVVMAddresses)
@@ -75,10 +74,7 @@ import Test.Cardano.Ledger.Core.JSON (roundTripJsonEraSpec)
7574

7675
spec ::
7776
forall era.
78-
( EraPlutusTxInfo PlutusV1 era
79-
, EraPlutusTxInfo PlutusV2 era
80-
, EraPlutusTxInfo PlutusV3 era
81-
, RuleListEra era
77+
( RuleListEra era
8278
, ConwayEraImp era
8379
, ApplyTx era
8480
, DecCBOR (TxWits era)

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/TxInfoSpec.hs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ module Test.Cardano.Ledger.Conway.TxInfoSpec (spec) where
1212

1313
import Cardano.Ledger.Alonzo.Plutus.Context (
1414
EraPlutusContext (ContextError),
15-
EraPlutusTxInfo,
1615
toPlutusTxCert,
1716
)
1817
import Cardano.Ledger.BaseTypes
@@ -38,7 +37,6 @@ import Test.Cardano.Ledger.Conway.Genesis ()
3837
spec ::
3938
forall era.
4039
( ConwayEraTest era
41-
, EraPlutusTxInfo PlutusV3 era
4240
, TxCert era ~ ConwayTxCert era
4341
) =>
4442
Spec

0 commit comments

Comments
 (0)