Skip to content

Commit 9643487

Browse files
authored
Merge pull request #6551 from IntersectMBO/mgalazyn/refactor/testnet-remove-old-witness-conjuring
cardano-testnet | Remove old era casing functions
2 parents 97036a6 + 9e40e0f commit 9643487

7 files changed

Lines changed: 70 additions & 100 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
### Changed
2+
3+
- Replaced `caseShelleyToBabbageOrConwayEraOnwards` and `conwayEraOnwardsConstraints` patterns with `obtainCommonConstraints` and a new `unsafeEraFromSbe` helper that converts `ShelleyBasedEra` to the experimental `Era` witness, simplifying era-dependent code in governance tests and epoch state processing.

cardano-testnet/src/Testnet/EpochStateProcessing.hs

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
{-# LANGUAGE TypeFamilies #-}
44

55
module Testnet.EpochStateProcessing
6-
( maybeExtractGovernanceActionIndex
6+
( unsafeEraFromSbe
7+
, maybeExtractGovernanceActionIndex
78
, maybeExtractGovernanceActionExpiry
89
, waitForGovActionVotes
910
) where
1011

1112
import Cardano.Api
13+
import Cardano.Api.Experimental (Era, obtainCommonConstraints, sbeToEra)
1214
import Cardano.Api.Ledger (EpochInterval (..), GovActionId (..))
1315
import qualified Cardano.Api.Ledger as L
1416

@@ -18,7 +20,8 @@ import qualified Cardano.Ledger.Shelley.LedgerState as L
1820

1921
import Prelude
2022

21-
import Control.Monad (void)
23+
import Control.Monad
24+
import Control.Monad.Trans.Maybe (runMaybeT)
2225
import qualified Data.Map as Map
2326
import Data.Word (Word16)
2427
import GHC.Exts (IsList (toList), toList)
@@ -30,21 +33,16 @@ import Testnet.Components.Query (EpochStateView, TestnetWaitPeriod (..
3033

3134
import Hedgehog
3235
import Hedgehog.Extras (MonadAssertion)
33-
import qualified Hedgehog.Extras as H
3436

3537
maybeExtractGovernanceActionIndex
3638
:: HasCallStack
3739
=> TxId -- ^ transaction id searched for
3840
-> AnyNewEpochState
3941
-> Maybe Word16
4042
maybeExtractGovernanceActionIndex txid (AnyNewEpochState sbe newEpochState _) =
41-
caseShelleyToBabbageOrConwayEraOnwards
42-
(const $ error "Governance actions only available in Conway era onwards")
43-
(\ceo -> conwayEraOnwardsConstraints ceo $ do
44-
let proposals = newEpochState ^. L.newEpochStateGovStateL . L.proposalsGovStateL
45-
Map.foldlWithKey' (compareWithTxId txid) Nothing (L.proposalsActionsMap proposals)
46-
)
47-
sbe
43+
obtainCommonConstraints (unsafeEraFromSbe sbe) $ do
44+
let proposals = newEpochState ^. L.newEpochStateGovStateL . L.proposalsGovStateL
45+
Map.foldlWithKey' (compareWithTxId txid) Nothing $ L.proposalsActionsMap proposals
4846
where
4947
compareWithTxId (TxId ti1) Nothing (GovActionId (L.TxId ti2) (L.GovActionIx gai)) _
5048
| ti1 == L.extractHash ti2 = Just gai
@@ -64,16 +62,12 @@ maybeExtractGovernanceActionExpiry
6462
-> AnyNewEpochState
6563
-> Maybe EpochNo
6664
maybeExtractGovernanceActionExpiry txid (AnyNewEpochState sbe newEpochState _) =
67-
caseShelleyToBabbageOrConwayEraOnwards
68-
(const $ error "Governance actions only available in Conway era onwards")
69-
(\ceo -> conwayEraOnwardsConstraints ceo $ do
70-
let proposals = newEpochState ^. L.newEpochStateGovStateL . L.proposalsGovStateL
71-
Map.foldlWithKey' (compareWithTxId txid) Nothing (L.proposalsActionsMap proposals)
72-
)
73-
sbe
65+
obtainCommonConstraints (unsafeEraFromSbe sbe) $ do
66+
let proposals = newEpochState ^. L.newEpochStateGovStateL . L.proposalsGovStateL
67+
Map.foldlWithKey' (compareWithTxId txid) Nothing $ L.proposalsActionsMap proposals
7468
where
7569
compareWithTxId (TxId ti1) Nothing (GovActionId (L.TxId ti2) _) govActionState
76-
| ti1 == L.extractHash ti2 = Just (L.gasExpiresAfter govActionState)
70+
| ti1 == L.extractHash ti2 = Just $ L.gasExpiresAfter govActionState
7771
compareWithTxId _ x _ _ = x
7872

7973
-- | Wait for the last gov action proposal in the list to have DRep or SPO votes.
@@ -93,20 +87,16 @@ waitForGovActionVotes epochStateView maxWait = withFrozenCallStack $
9387
:: HasCallStack
9488
=> (AnyNewEpochState, SlotNo, BlockNo)
9589
-> m (Maybe ())
96-
checkForVotes (AnyNewEpochState actualEra newEpochState _, _, _) = withFrozenCallStack $ do
97-
caseShelleyToBabbageOrConwayEraOnwards
98-
(const $ H.note_ "Only Conway era onwards is supported" >> failure)
99-
(\ceo -> do
100-
let govState = conwayEraOnwardsConstraints ceo $ newEpochState ^. newEpochStateGovStateL
101-
proposals = govState ^. L.cgsProposalsL . L.pPropsL . to toList
102-
if null proposals
103-
then pure Nothing
104-
else do
105-
let lastProposal = last proposals
106-
gaDRepVotes = lastProposal ^. L.gasDRepVotesL . to toList
107-
gaSpoVotes = lastProposal ^. L.gasStakePoolVotesL . to toList
108-
if null gaDRepVotes && null gaSpoVotes
109-
then pure Nothing
110-
else pure $ Just ()
111-
)
112-
actualEra
90+
checkForVotes (AnyNewEpochState actualEra newEpochState _, _, _) = withFrozenCallStack $ runMaybeT $
91+
obtainCommonConstraints (unsafeEraFromSbe actualEra) $ do
92+
let proposals = newEpochState ^. newEpochStateGovStateL . L.cgsProposalsL . L.pPropsL . to toList
93+
guard (not $ null proposals)
94+
let lastProposal = last proposals
95+
hasDRepVotes = not . Map.null $ lastProposal ^. L.gasDRepVotesL
96+
hasSpoVotes = not . Map.null $ lastProposal ^. L.gasStakePoolVotesL
97+
guard (hasDRepVotes || hasSpoVotes)
98+
99+
-- | Unsafely convert a 'ShelleyBasedEra' witness to an experimental 'Era' witness.
100+
-- Throws an 'error' for deprecated (pre-Conway) eras.
101+
unsafeEraFromSbe :: HasCallStack => ShelleyBasedEra era -> Era era
102+
unsafeEraFromSbe = withFrozenCallStack $ either (error . show . prettyError) id . sbeToEra

cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/Gov/CommitteeAddNew.hs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ module Cardano.Testnet.Test.Gov.CommitteeAddNew
1111
) where
1212

1313
import Cardano.Api as Api
14-
import Cardano.Api.Experimental (Some (..))
14+
import Cardano.Api.Experimental (Some (..), obtainCommonConstraints)
1515
import qualified Cardano.Api.Ledger as L
1616

1717
import qualified Cardano.Ledger.Conway.Governance as L
@@ -38,7 +38,7 @@ import Test.Cardano.CLI.Hash (serveFilesWhile)
3838
import Testnet.Components.Configuration
3939
import Testnet.Components.Query
4040
import Testnet.Defaults
41-
import Testnet.EpochStateProcessing (waitForGovActionVotes)
41+
import Testnet.EpochStateProcessing (unsafeEraFromSbe, waitForGovActionVotes)
4242
import qualified Testnet.Process.Cli.DRep as DRep
4343
import Testnet.Process.Cli.Keys
4444
import qualified Testnet.Process.Cli.SPO as SPO
@@ -329,16 +329,12 @@ getCommitteeMembers epochStateView ceo = withFrozenCallStack $ do
329329

330330
committeeIsPresent :: (AnyNewEpochState, SlotNo, BlockNo) -> Maybe ()
331331
committeeIsPresent (AnyNewEpochState sbe newEpochState _, _, _) =
332-
caseShelleyToBabbageOrConwayEraOnwards
333-
(const $ error "Constitutional committee does not exist pre-Conway era")
334-
(\_ -> do
335-
let mCommittee = newEpochState
336-
^. L.nesEsL
337-
. L.esLStateL
338-
. L.lsUTxOStateL
339-
. L.utxosGovStateL
340-
. L.cgsCommitteeL
341-
members <- L.committeeMembers <$> strictMaybeToMaybe mCommittee
342-
when (Map.null members) Nothing
343-
)
344-
sbe
332+
obtainCommonConstraints (unsafeEraFromSbe sbe) $ do
333+
let mCommittee = newEpochState
334+
^. L.nesEsL
335+
. L.esLStateL
336+
. L.lsUTxOStateL
337+
. L.utxosGovStateL
338+
. L.cgsCommitteeL
339+
members <- L.committeeMembers <$> strictMaybeToMaybe mCommittee
340+
guard (not $ Map.null members)

cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/Gov/NoConfidence.hs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module Cardano.Testnet.Test.Gov.NoConfidence
99
) where
1010

1111
import Cardano.Api
12-
import Cardano.Api.Experimental (Some (..))
12+
import Cardano.Api.Experimental (Some (..), obtainCommonConstraints)
1313
import Cardano.Api.Ledger
1414

1515
import qualified Cardano.Ledger.Conway.Genesis as L
@@ -33,7 +33,7 @@ import System.FilePath ((</>))
3333
import Testnet.Components.Configuration
3434
import Testnet.Components.Query
3535
import Testnet.Defaults
36-
import Testnet.EpochStateProcessing (waitForGovActionVotes)
36+
import Testnet.EpochStateProcessing (unsafeEraFromSbe, waitForGovActionVotes)
3737
import qualified Testnet.Process.Cli.DRep as DRep
3838
import Testnet.Process.Cli.Keys
3939
import qualified Testnet.Process.Cli.SPO as SPO
@@ -240,20 +240,13 @@ hprop_gov_no_confidence = integrationRetryWorkspace 2 "no-confidence" $ \tempAbs
240240
-- | Checks if the committee is empty or not.
241241
committeeIsPresent :: Bool -> (AnyNewEpochState, SlotNo, BlockNo) -> Maybe ()
242242
committeeIsPresent committeeExists (AnyNewEpochState sbe newEpochState _, _, _) =
243-
caseShelleyToBabbageOrConwayEraOnwards
244-
(const $ error "Constitutional committee does not exist pre-Conway era")
245-
(const $ let mCommittee = newEpochState
246-
^. L.nesEsL
247-
. L.esLStateL
248-
. L.lsUTxOStateL
249-
. L.utxosGovStateL
250-
. L.cgsCommitteeL
251-
in if committeeExists
252-
then if isSJust mCommittee
253-
then Just () -- The committee is non empty and we terminate.
254-
else Nothing
255-
else if mCommittee == SNothing
256-
then Just () -- The committee is empty and we terminate.
257-
else Nothing
258-
)
259-
sbe
243+
obtainCommonConstraints (unsafeEraFromSbe sbe) $ do
244+
let mCommittee = newEpochState
245+
^. L.nesEsL
246+
. L.esLStateL
247+
. L.lsUTxOStateL
248+
. L.utxosGovStateL
249+
. L.cgsCommitteeL
250+
guard $ if committeeExists
251+
then isSJust mCommittee -- The committee is non empty and we terminate.
252+
else mCommittee == SNothing -- The committee is empty and we terminate.

cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/Gov/ProposeNewConstitution.hs

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module Cardano.Testnet.Test.Gov.ProposeNewConstitution
99
) where
1010

1111
import Cardano.Api as Api hiding (txId)
12-
import Cardano.Api.Experimental (Some (..))
12+
import Cardano.Api.Experimental (Some (..), obtainCommonConstraints)
1313
import Cardano.Api.Ledger (EpochInterval (..))
1414

1515
import qualified Cardano.Crypto.Hash as L
@@ -40,7 +40,7 @@ import Test.Cardano.CLI.Hash (serveFilesWhile)
4040
import Testnet.Components.Configuration
4141
import Testnet.Components.Query
4242
import Testnet.Defaults
43-
import Testnet.EpochStateProcessing (waitForGovActionVotes)
43+
import Testnet.EpochStateProcessing (unsafeEraFromSbe, waitForGovActionVotes)
4444
import Testnet.Process.Cli.DRep
4545
import Testnet.Process.Cli.Keys
4646
import Testnet.Process.Cli.SPO (createStakeKeyRegistrationCertificate)
@@ -354,17 +354,11 @@ filterRatificationState
354354
-> String -- ^ Submitted guard rail script hash
355355
-> AnyNewEpochState
356356
-> Bool
357-
filterRatificationState c guardRailScriptHash (AnyNewEpochState sbe newEpochState _) = do
358-
caseShelleyToBabbageOrConwayEraOnwards
359-
(const $ error "filterRatificationState: Only conway era supported")
360-
361-
(const $ do
362-
let rState = Ledger.extractDRepPulsingState $ newEpochState ^. L.newEpochStateGovStateL . L.drepPulsingStateGovStateL
363-
constitution = rState ^. Ledger.rsEnactStateL . Ledger.ensConstitutionL
364-
constitutionAnchorHash = Ledger.anchorDataHash $ Ledger.constitutionAnchor constitution
365-
L.ScriptHash constitutionScriptHash = fromMaybe (error "filterRatificationState: constitution does not have a guardrail script")
366-
$ strictMaybeToMaybe $ constitution ^. Ledger.constitutionGuardrailsScriptHashL
367-
Text.pack c == renderSafeHashAsHex constitutionAnchorHash && L.hashToTextAsHex constitutionScriptHash == Text.pack guardRailScriptHash
368-
369-
)
370-
sbe
357+
filterRatificationState c guardRailScriptHash (AnyNewEpochState sbe newEpochState _) =
358+
obtainCommonConstraints (unsafeEraFromSbe sbe) $ do
359+
let rState = Ledger.extractDRepPulsingState $ newEpochState ^. L.newEpochStateGovStateL . L.drepPulsingStateGovStateL
360+
constitution = rState ^. Ledger.rsEnactStateL . Ledger.ensConstitutionL
361+
constitutionAnchorHash = Ledger.anchorDataHash $ Ledger.constitutionAnchor constitution
362+
L.ScriptHash constitutionScriptHash = fromMaybe (error "filterRatificationState: constitution does not have a guardrail script")
363+
$ strictMaybeToMaybe $ constitution ^. Ledger.constitutionGuardrailsScriptHashL
364+
Text.pack c == renderSafeHashAsHex constitutionAnchorHash && L.hashToTextAsHex constitutionScriptHash == Text.pack guardRailScriptHash

cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/Gov/ProposeNewConstitutionSPO.hs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ module Cardano.Testnet.Test.Gov.ProposeNewConstitutionSPO
99
) where
1010

1111
import Cardano.Api
12-
import Cardano.Api.Experimental (Some (..))
12+
import Cardano.Api.Experimental (Some (..), obtainCommonConstraints)
1313

1414
import qualified Cardano.Ledger.Conway.Governance as L
1515
import qualified Cardano.Ledger.Shelley.LedgerState as L
@@ -29,6 +29,7 @@ import System.FilePath ((</>))
2929

3030
import Testnet.Components.Query
3131
import Testnet.Defaults
32+
import Testnet.EpochStateProcessing (unsafeEraFromSbe)
3233
import Testnet.Process.Cli.DRep
3334
import Testnet.Process.Cli.Keys
3435
import qualified Testnet.Process.Cli.SPO as SPO
@@ -180,9 +181,7 @@ getConstitutionProposal
180181
getConstitutionProposal nodeConfigFile socketPath maxEpoch = do
181182
result <- H.evalIO . runExceptT $ foldEpochState nodeConfigFile socketPath QuickValidation maxEpoch Nothing
182183
$ \(AnyNewEpochState actualEra newEpochState _) _slotNb _blockNb ->
183-
caseShelleyToBabbageOrConwayEraOnwards
184-
(error $ "Expected Conway era onwards, got state in " <> docToString (pretty actualEra))
185-
(\cEra -> conwayEraOnwardsConstraints cEra $ do
184+
obtainCommonConstraints (unsafeEraFromSbe actualEra) $ do
186185
let proposals = newEpochState
187186
^. L.nesEsL
188187
. L.esLStateL
@@ -196,6 +195,5 @@ getConstitutionProposal nodeConfigFile socketPath maxEpoch = do
196195
pure ConditionMet
197196
_ ->
198197
pure ConditionNotMet
199-
) actualEra
200198
(_, mGovAction) <- H.evalEither result
201199
return mGovAction

cardano-testnet/test/cardano-testnet-test/Cardano/Testnet/Test/Gov/TreasuryWithdrawal.hs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ module Cardano.Testnet.Test.Gov.TreasuryWithdrawal
1414
) where
1515

1616
import Cardano.Api hiding (txId)
17+
import Cardano.Api.Experimental (obtainCommonConstraints)
1718
import Cardano.Api.Ledger (Credential, EpochInterval (EpochInterval), KeyRole (Staking))
1819

1920
import qualified Cardano.Ledger.BaseTypes as L
@@ -39,6 +40,7 @@ import System.FilePath ((</>))
3940
import Test.Cardano.CLI.Hash (serveFilesWhile)
4041
import Testnet.Components.Query
4142
import Testnet.Defaults
43+
import Testnet.EpochStateProcessing (unsafeEraFromSbe)
4244
import Testnet.Process.Cli.Keys (cliStakeAddressKeyGen)
4345
import Testnet.Process.Cli.SPO (createStakeKeyRegistrationCertificate)
4446
import Testnet.Process.Cli.Transaction (retrieveTransactionId)
@@ -270,10 +272,8 @@ getAnyWithdrawals
270272
-> m (Maybe (Map (Credential Staking) Coin))
271273
getAnyWithdrawals nodeConfigFile socketPath maxEpoch = withFrozenCallStack $ do
272274
fmap snd . H.leftFailM . evalIO . runExceptT $ foldEpochState nodeConfigFile socketPath FullValidation maxEpoch Nothing
273-
$ \(AnyNewEpochState actualEra newEpochState _) ->
274-
caseShelleyToBabbageOrConwayEraOnwards
275-
(error $ "Expected Conway era onwards, got state in " <> docToString (pretty actualEra))
276-
(\cEra _ _ -> conwayEraOnwardsConstraints cEra $ do
275+
$ \(AnyNewEpochState actualEra newEpochState _) _ _ ->
276+
obtainCommonConstraints (unsafeEraFromSbe actualEra) $ do
277277
let withdrawals = newEpochState
278278
^. L.newEpochStateGovStateL
279279
. L.drepPulsingStateGovStateL
@@ -285,7 +285,6 @@ getAnyWithdrawals nodeConfigFile socketPath maxEpoch = withFrozenCallStack $ do
285285
else do
286286
put $ Just withdrawals
287287
pure ConditionMet
288-
) actualEra
289288

290289

291290
getTreasuryWithdrawalProposal
@@ -298,10 +297,8 @@ getTreasuryWithdrawalProposal
298297
-> m (Maybe L.GovActionId)
299298
getTreasuryWithdrawalProposal nodeConfigFile socketPath maxEpoch = withFrozenCallStack $ do
300299
fmap snd . H.leftFailM . evalIO . runExceptT $ foldEpochState nodeConfigFile socketPath QuickValidation maxEpoch Nothing
301-
$ \(AnyNewEpochState actualEra newEpochState _) ->
302-
caseShelleyToBabbageOrConwayEraOnwards
303-
(error $ "Expected Conway era onwards, got state in " <> docToString (pretty actualEra))
304-
(\cEra _ _ -> conwayEraOnwardsConstraints cEra $ do
300+
$ \(AnyNewEpochState actualEra newEpochState _) _ _ ->
301+
obtainCommonConstraints (unsafeEraFromSbe actualEra) $ do
305302
let proposals = newEpochState
306303
^. L.newEpochStateGovStateL
307304
. L.cgsProposalsL
@@ -312,4 +309,3 @@ getTreasuryWithdrawalProposal nodeConfigFile socketPath maxEpoch = withFrozenCal
312309
pure ConditionMet
313310
_ ->
314311
pure ConditionNotMet
315-
) actualEra

0 commit comments

Comments
 (0)