Skip to content

Commit 9a21512

Browse files
authored
Merge pull request #4944 from IntersectMBO/nm/alonzo-imp-utxow-valid
Implement more Alonzo UTxOW Imp tests
2 parents d76aecb + 2a25a8b commit 9a21512

File tree

8 files changed

+90
-158
lines changed

8 files changed

+90
-158
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ import Cardano.Ledger.Alonzo.Rules (
1313
AlonzoUtxosPredFailure,
1414
AlonzoUtxowPredFailure,
1515
)
16-
import Cardano.Ledger.Shelley.Rules (ShelleyUtxoPredFailure, ShelleyUtxowPredFailure)
16+
import Cardano.Ledger.Shelley.Rules (
17+
ShelleyDelegPredFailure,
18+
ShelleyUtxoPredFailure,
19+
ShelleyUtxowPredFailure,
20+
)
1721
import qualified Test.Cardano.Ledger.Alonzo.Imp.UtxoSpec as Utxo
1822
import qualified Test.Cardano.Ledger.Alonzo.Imp.UtxosSpec as Utxos
1923
import qualified Test.Cardano.Ledger.Alonzo.Imp.UtxowSpec as Utxow
@@ -25,6 +29,7 @@ spec ::
2529
forall era.
2630
( Arbitrary (TxAuxData era)
2731
, AlonzoEraImp era
32+
, InjectRuleFailure "LEDGER" ShelleyDelegPredFailure era
2833
, InjectRuleFailure "LEDGER" ShelleyUtxoPredFailure era
2934
, InjectRuleFailure "LEDGER" ShelleyUtxowPredFailure era
3035
, InjectRuleFailure "LEDGER" AlonzoUtxoPredFailure era

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import Cardano.Ledger.Alonzo.Rules (
1414
AlonzoUtxosPredFailure,
1515
AlonzoUtxowPredFailure,
1616
)
17-
import Cardano.Ledger.Shelley.Rules (ShelleyUtxowPredFailure)
17+
import Cardano.Ledger.Shelley.Rules (ShelleyDelegPredFailure, ShelleyUtxowPredFailure)
1818
import qualified Test.Cardano.Ledger.Alonzo.Imp.UtxowSpec.Invalid as Invalid
1919
import qualified Test.Cardano.Ledger.Alonzo.Imp.UtxowSpec.Valid as Valid
2020
import Test.Cardano.Ledger.Alonzo.ImpTest
@@ -23,6 +23,7 @@ import Test.Cardano.Ledger.Common
2323
spec ::
2424
forall era.
2525
( AlonzoEraImp era
26+
, InjectRuleFailure "LEDGER" ShelleyDelegPredFailure era
2627
, InjectRuleFailure "LEDGER" ShelleyUtxowPredFailure era
2728
, InjectRuleFailure "LEDGER" AlonzoUtxosPredFailure era
2829
, InjectRuleFailure "LEDGER" AlonzoUtxowPredFailure era

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

Lines changed: 49 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{-# LANGUAGE DataKinds #-}
22
{-# LANGUAGE FlexibleContexts #-}
3+
{-# LANGUAGE NumericUnderscores #-}
34
{-# LANGUAGE OverloadedLists #-}
45
{-# LANGUAGE OverloadedStrings #-}
56
{-# LANGUAGE PatternSynonyms #-}
@@ -17,29 +18,37 @@ import Cardano.Ledger.Alonzo.Rules (
1718
AlonzoUtxosPredFailure,
1819
)
1920
import Cardano.Ledger.Alonzo.Scripts (eraLanguages)
20-
import Cardano.Ledger.BaseTypes (StrictMaybe (..))
21+
import Cardano.Ledger.Alonzo.TxWits (unTxDatsL)
22+
import Cardano.Ledger.BaseTypes (StrictMaybe (..), natVersion)
23+
import Cardano.Ledger.Coin (Coin (..))
2124
import Cardano.Ledger.Credential (Credential (..), StakeReference (..))
2225
import Cardano.Ledger.Mary.Value (AssetName (..), MaryValue (..), MultiAsset (..), PolicyID (..))
2326
import Cardano.Ledger.Plutus (
27+
Data (..),
28+
hashData,
2429
hashPlutusScript,
2530
withSLanguage,
2631
)
32+
import Cardano.Ledger.Shelley.Rules (ShelleyDelegPredFailure (..))
2733
import Cardano.Ledger.Shelley.Scripts (
2834
pattern RequireAllOf,
2935
pattern RequireSignature,
3036
)
3137
import Control.Monad ((<=<))
3238
import GHC.Exts (fromList)
33-
import Lens.Micro ((&), (.~))
39+
import Lens.Micro ((%~), (&), (.~))
3440
import Lens.Micro.Mtl (use)
35-
import Test.Cardano.Ledger.Alonzo.Arbitrary ()
3641
import Test.Cardano.Ledger.Alonzo.ImpTest
3742
import Test.Cardano.Ledger.Imp.Common
3843
import Test.Cardano.Ledger.Plutus.Examples
3944

45+
import qualified Data.Map.Strict as Map
46+
import qualified PlutusLedgerApi.Common as P
47+
4048
spec ::
4149
forall era.
4250
( AlonzoEraImp era
51+
, InjectRuleFailure "LEDGER" ShelleyDelegPredFailure era
4352
, InjectRuleFailure "LEDGER" AlonzoUtxosPredFailure era
4453
) =>
4554
SpecWith (ImpInit (LedgerSpec era))
@@ -139,9 +148,42 @@ spec = describe "Valid transactions" $ do
139148
& outputsTxBodyL .~ [txOut]
140149
expectTxSuccess <=< submitTx $ mkBasicTx txBody
141150

142-
it "Acceptable supplimentary datum" $ do
143-
const $ pendingWith "not implemented yet"
144-
it "Multiple identical certificates" $ do
145-
const $ pendingWith "not implemented yet"
151+
it "Acceptable supplementary datum" $ do
152+
inputAddr <- freshKeyHash @'Payment
153+
amount <- Coin <$> choose (2_000_000, 8_000_000)
154+
txIn <- sendCoinTo (mkAddr inputAddr StakeRefNull) amount
155+
let
156+
datum = Data (P.I 123)
157+
datumHash = hashData datum
158+
txOut =
159+
mkBasicTxOut
160+
(mkAddr alwaysSucceedsWithDatumHash StakeRefNull)
161+
(MaryValue amount mempty)
162+
& dataHashTxOutL .~ SJust datumHash
163+
txBody =
164+
mkBasicTxBody
165+
& inputsTxBodyL .~ [txIn]
166+
& outputsTxBodyL .~ [txOut]
167+
tx =
168+
mkBasicTx txBody
169+
& witsTxL . datsTxWitsL . unTxDatsL %~ Map.insert datumHash datum
170+
expectTxSuccess =<< submitTx tx
171+
172+
it "Multiple identical certificates" $ do
173+
let scriptHash = alwaysSucceedsNoDatumHash
174+
void . registerStakeCredential $ ScriptHashObj scriptHash
175+
let tx =
176+
mkBasicTx mkBasicTxBody
177+
& bodyTxL . certsTxBodyL .~ fromList (UnRegTxCert . ScriptHashObj <$> replicate 2 scriptHash)
178+
if eraProtVerLow @era < natVersion @9
179+
then
180+
-- This passes UTXOW rules but not DELEG rules; however, we care about only UTXOW rules here
181+
submitFailingTx
182+
tx
183+
[injectFailure $ StakeKeyNotRegisteredDELEG (ScriptHashObj scriptHash)]
184+
else
185+
-- Conway fixed the bug that was causing DELEG to fail
186+
expectTxSuccess =<< submitTx tx
187+
146188
it "Non-script output with datum" $ do
147189
const $ pendingWith "not implemented yet"

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ import Cardano.Ledger.Alonzo.Rules (
6060
import Cardano.Ledger.Alonzo.Scripts (plutusScriptLanguage, toAsItem, toAsIx)
6161
import Cardano.Ledger.Alonzo.Tx (IsValid (..), hashScriptIntegrity)
6262
import Cardano.Ledger.Alonzo.TxAuxData (AlonzoTxAuxData)
63-
import Cardano.Ledger.Alonzo.TxWits (TxDats (..), unRedeemersL)
63+
import Cardano.Ledger.Alonzo.TxWits (unRedeemersL, unTxDatsL)
6464
import Cardano.Ledger.Alonzo.UTxO (AlonzoEraUTxO (..), AlonzoScriptsNeeded (..))
6565
import Cardano.Ledger.BaseTypes (Globals (..), StrictMaybe (..))
6666
import Cardano.Ledger.Coin (Coin (..))
@@ -272,12 +272,10 @@ fixupDatums tx = impAnn "fixupDatums" $ do
272272
contexts <- impGetPlutusContexts tx
273273
let purposes = (^. _1) <$> contexts
274274
datums <- traverse collectDatums purposes
275-
let TxDats prevDats = tx ^. witsTxL . datsTxWitsL
276275
pure $
277276
tx
278-
& witsTxL . datsTxWitsL
279-
.~ TxDats
280-
(Map.union prevDats $ fromElems hashData (catMaybes datums))
277+
& witsTxL . datsTxWitsL . unTxDatsL
278+
<>~ fromElems hashData (catMaybes datums)
281279
where
282280
collectDatums :: PlutusPurpose AsIxItem era -> ImpTestM era (Maybe (Data era))
283281
collectDatums purpose = do

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ import Cardano.Ledger.Babbage.Core
1717
import Cardano.Ledger.Babbage.Rules (BabbageUtxowPredFailure (..))
1818
import Cardano.Ledger.Babbage.TxInfo (BabbageContextError)
1919
import Cardano.Ledger.BaseTypes (Inject)
20-
import Cardano.Ledger.Shelley.Rules (ShelleyUtxoPredFailure, ShelleyUtxowPredFailure)
20+
import Cardano.Ledger.Shelley.Rules (
21+
ShelleyDelegPredFailure,
22+
ShelleyUtxoPredFailure,
23+
ShelleyUtxowPredFailure,
24+
)
2125
import qualified Test.Cardano.Ledger.Alonzo.Imp as AlonzoImp
2226
import Test.Cardano.Ledger.Alonzo.ImpTest (AlonzoEraImp, LedgerSpec)
2327
import qualified Test.Cardano.Ledger.Babbage.Imp.UtxowSpec as Utxow
@@ -28,6 +32,7 @@ spec ::
2832
( Arbitrary (TxAuxData era)
2933
, AlonzoEraImp era
3034
, BabbageEraTxBody era
35+
, InjectRuleFailure "LEDGER" ShelleyDelegPredFailure era
3136
, InjectRuleFailure "LEDGER" ShelleyUtxoPredFailure era
3237
, InjectRuleFailure "LEDGER" AlonzoUtxoPredFailure era
3338
, InjectRuleFailure "LEDGER" AlonzoUtxosPredFailure era

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import Cardano.Ledger.Conway.Rules (
3232
import Cardano.Ledger.Conway.TxInfo (ConwayContextError)
3333
import Cardano.Ledger.Shelley.API.Mempool (ApplyTx (..))
3434
import Cardano.Ledger.Shelley.Rules (
35+
ShelleyDelegPredFailure,
3536
ShelleyUtxoPredFailure,
3637
ShelleyUtxowPredFailure,
3738
)
@@ -71,6 +72,7 @@ spec ::
7172
, InjectRuleFailure "LEDGER" AlonzoUtxoPredFailure era
7273
, InjectRuleFailure "LEDGER" AlonzoUtxosPredFailure era
7374
, InjectRuleFailure "LEDGER" AlonzoUtxowPredFailure era
75+
, InjectRuleFailure "LEDGER" ShelleyDelegPredFailure era
7476
, InjectRuleFailure "LEDGER" ShelleyUtxoPredFailure era
7577
, InjectRuleFailure "LEDGER" ShelleyUtxowPredFailure era
7678
, InjectRuleFailure "LEDGER" ConwayDelegPredFailure era

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{-# LANGUAGE DataKinds #-}
22
{-# LANGUAGE FlexibleContexts #-}
33
{-# LANGUAGE LambdaCase #-}
4+
{-# LANGUAGE MultiParamTypeClasses #-}
45
{-# LANGUAGE NamedFieldPuns #-}
56
{-# LANGUAGE NumericUnderscores #-}
67
{-# LANGUAGE OverloadedLists #-}
@@ -169,6 +170,10 @@ import Cardano.Ledger.Conway.Genesis (ConwayGenesis (..))
169170
import Cardano.Ledger.Conway.Governance
170171
import Cardano.Ledger.Conway.PParams (UpgradeConwayPParams (..))
171172
import Cardano.Ledger.Conway.Rules (
173+
ConwayCertPredFailure (..),
174+
ConwayCertsPredFailure (..),
175+
ConwayDelegPredFailure (..),
176+
ConwayLedgerPredFailure (..),
172177
EnactSignal,
173178
committeeAccepted,
174179
committeeAcceptedRatio,
@@ -209,6 +214,8 @@ import Cardano.Ledger.Shelley.LedgerState (
209214
vsCommitteeStateL,
210215
vsDRepsL,
211216
)
217+
import Cardano.Ledger.Shelley.Rules (ShelleyDelegPredFailure)
218+
import qualified Cardano.Ledger.Shelley.Rules as Shelley
212219
import Cardano.Ledger.TxIn (TxId (..))
213220
import Cardano.Ledger.UMap (dRepMap)
214221
import qualified Cardano.Ledger.UMap as UMap
@@ -1817,3 +1824,16 @@ delegateSPORewardAddressToDRep_ kh stake drep = do
18171824
(raCredential . ppRewardAccount $ pp)
18181825
stake
18191826
drep
1827+
1828+
-- Partial implementation used for checking predicate failures
1829+
instance InjectRuleFailure "LEDGER" ShelleyDelegPredFailure ConwayEra where
1830+
injectFailure = ConwayCertsFailure . injectFailure
1831+
instance InjectRuleFailure "CERTS" ShelleyDelegPredFailure ConwayEra where
1832+
injectFailure = CertFailure . injectFailure
1833+
instance InjectRuleFailure "CERT" ShelleyDelegPredFailure ConwayEra where
1834+
injectFailure = DelegFailure . injectFailure
1835+
instance InjectRuleFailure "DELEG" ShelleyDelegPredFailure ConwayEra where
1836+
injectFailure (Shelley.StakeKeyAlreadyRegisteredDELEG c) = StakeKeyRegisteredDELEG c
1837+
injectFailure (Shelley.StakeKeyNotRegisteredDELEG c) = StakeKeyNotRegisteredDELEG c
1838+
injectFailure (Shelley.StakeKeyNonZeroAccountBalanceDELEG c) = StakeKeyHasNonZeroRewardAccountBalanceDELEG c
1839+
injectFailure _ = error "Cannot inject ShelleyDelegPredFailure into ConwayEra"

0 commit comments

Comments
 (0)