Skip to content

Commit c5b3c8b

Browse files
committed
cardano-testnet | Remove old era casing functions
1 parent 60af1c2 commit c5b3c8b

7 files changed

Lines changed: 81 additions & 99 deletions

File tree

cabal.project

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ allow-newer:
8888
-- Do NOT add more source-repository-package stanzas here unless they are strictly
8989
-- temporary! Please read the section in CONTRIBUTING about updating dependencies.
9090

91+
source-repository-package
92+
type: git
93+
location: https://github.com/intersectmbo/cardano-api
94+
tag: d0cfc2b866da41541eb7ab3ba1e88a07183b6073
95+
--sha256: sha256-NfJ0O/W6Y7hUf1UXGijOGq1enAI+YteQCacu2tICras=
96+
subdir:
97+
cardano-api
98+
99+
source-repository-package
100+
type: git
101+
location: https://github.com/intersectmbo/cardano-cli
102+
tag: 7d2bb89f2cb9034cfbf51e9ddd3defe41fcefc0a
103+
--sha256: sha256-42RsgjCzq9/VxMTvVBDhmZd9ue39ZMg5oJX0cfL3ocw=
104+
subdir:
105+
cardano-cli
106+
91107
if impl(ghc >= 9.12)
92108
-- GHC 9.12 support - master branch
93109
source-repository-package

cardano-testnet/src/Testnet/EpochStateProcessing.hs

Lines changed: 23 additions & 34 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

@@ -30,21 +32,16 @@ import Testnet.Components.Query (EpochStateView, TestnetWaitPeriod (..
3032

3133
import Hedgehog
3234
import Hedgehog.Extras (MonadAssertion)
33-
import qualified Hedgehog.Extras as H
3435

3536
maybeExtractGovernanceActionIndex
3637
:: HasCallStack
3738
=> TxId -- ^ transaction id searched for
3839
-> AnyNewEpochState
3940
-> Maybe Word16
4041
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
42+
obtainCommonConstraints (unsafeEraFromSbe sbe) $ do
43+
let proposals = newEpochState ^. L.newEpochStateGovStateL . L.proposalsGovStateL
44+
Map.foldlWithKey' (compareWithTxId txid) Nothing $ L.proposalsActionsMap proposals
4845
where
4946
compareWithTxId (TxId ti1) Nothing (GovActionId (L.TxId ti2) (L.GovActionIx gai)) _
5047
| ti1 == L.extractHash ti2 = Just gai
@@ -64,16 +61,12 @@ maybeExtractGovernanceActionExpiry
6461
-> AnyNewEpochState
6562
-> Maybe EpochNo
6663
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
64+
obtainCommonConstraints (unsafeEraFromSbe sbe) $ do
65+
let proposals = newEpochState ^. L.newEpochStateGovStateL . L.proposalsGovStateL
66+
Map.foldlWithKey' (compareWithTxId txid) Nothing $ L.proposalsActionsMap proposals
7467
where
7568
compareWithTxId (TxId ti1) Nothing (GovActionId (L.TxId ti2) _) govActionState
76-
| ti1 == L.extractHash ti2 = Just (L.gasExpiresAfter govActionState)
69+
| ti1 == L.extractHash ti2 = Just $ L.gasExpiresAfter govActionState
7770
compareWithTxId _ x _ _ = x
7871

7972
-- | Wait for the last gov action proposal in the list to have DRep or SPO votes.
@@ -93,20 +86,16 @@ waitForGovActionVotes epochStateView maxWait = withFrozenCallStack $
9386
:: HasCallStack
9487
=> (AnyNewEpochState, SlotNo, BlockNo)
9588
-> 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
89+
checkForVotes (AnyNewEpochState actualEra newEpochState _, _, _) = withFrozenCallStack $ runMaybeT $
90+
obtainCommonConstraints (unsafeEraFromSbe actualEra) $ do
91+
let proposals = newEpochState ^. newEpochStateGovStateL . L.cgsProposalsL . L.pPropsL . to toList
92+
guard (not $ null proposals)
93+
let lastProposal = last proposals
94+
hasDRepVotes = not . Map.null $ lastProposal ^. L.gasDRepVotesL
95+
hasSpoVotes = not . Map.null $ lastProposal ^. L.gasStakePoolVotesL
96+
guard (hasDRepVotes || hasSpoVotes)
97+
98+
-- | Unsafely convert a 'ShelleyBasedEra' witness to an experimental 'Era' witness.
99+
-- Throws an 'error' for deprecated (pre-Conway) eras.
100+
unsafeEraFromSbe :: HasCallStack => ShelleyBasedEra era -> Era era
101+
unsafeEraFromSbe = withFrozenCallStack $ either (error . prettyShow . 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
@@ -241,20 +241,13 @@ hprop_gov_no_confidence = integrationRetryWorkspace 2 "no-confidence" $ \tempAbs
241241
-- | Checks if the committee is empty or not.
242242
committeeIsPresent :: Bool -> (AnyNewEpochState, SlotNo, BlockNo) -> Maybe ()
243243
committeeIsPresent committeeExists (AnyNewEpochState sbe newEpochState _, _, _) =
244-
caseShelleyToBabbageOrConwayEraOnwards
245-
(const $ error "Constitutional committee does not exist pre-Conway era")
246-
(const $ let mCommittee = newEpochState
247-
^. L.nesEsL
248-
. L.esLStateL
249-
. L.lsUTxOStateL
250-
. L.utxosGovStateL
251-
. L.cgsCommitteeL
252-
in if committeeExists
253-
then if isSJust mCommittee
254-
then Just () -- The committee is non empty and we terminate.
255-
else Nothing
256-
else if mCommittee == SNothing
257-
then Just () -- The committee is empty and we terminate.
258-
else Nothing
259-
)
260-
sbe
244+
obtainCommonConstraints (unsafeEraFromSbe sbe) $ do
245+
let mCommittee = newEpochState
246+
^. L.nesEsL
247+
. L.esLStateL
248+
. L.lsUTxOStateL
249+
. L.utxosGovStateL
250+
. L.cgsCommitteeL
251+
guard $ if committeeExists
252+
then isSJust mCommittee -- The committee is non empty and we terminate.
253+
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)
@@ -268,10 +270,8 @@ getAnyWithdrawals
268270
-> m (Maybe (Map (Credential Staking) Coin))
269271
getAnyWithdrawals nodeConfigFile socketPath maxEpoch = withFrozenCallStack $ do
270272
fmap snd . H.leftFailM . evalIO . runExceptT $ foldEpochState nodeConfigFile socketPath FullValidation maxEpoch Nothing
271-
$ \(AnyNewEpochState actualEra newEpochState _) ->
272-
caseShelleyToBabbageOrConwayEraOnwards
273-
(error $ "Expected Conway era onwards, got state in " <> docToString (pretty actualEra))
274-
(\cEra _ _ -> conwayEraOnwardsConstraints cEra $ do
273+
$ \(AnyNewEpochState actualEra newEpochState _) _ _ ->
274+
obtainCommonConstraints (unsafeEraFromSbe actualEra) $ do
275275
let withdrawals = newEpochState
276276
^. L.newEpochStateGovStateL
277277
. L.drepPulsingStateGovStateL
@@ -283,7 +283,6 @@ getAnyWithdrawals nodeConfigFile socketPath maxEpoch = withFrozenCallStack $ do
283283
else do
284284
put $ Just withdrawals
285285
pure ConditionMet
286-
) actualEra
287286

288287

289288
getTreasuryWithdrawalProposal
@@ -296,10 +295,8 @@ getTreasuryWithdrawalProposal
296295
-> m (Maybe L.GovActionId)
297296
getTreasuryWithdrawalProposal nodeConfigFile socketPath maxEpoch = withFrozenCallStack $ do
298297
fmap snd . H.leftFailM . evalIO . runExceptT $ foldEpochState nodeConfigFile socketPath QuickValidation maxEpoch Nothing
299-
$ \(AnyNewEpochState actualEra newEpochState _) ->
300-
caseShelleyToBabbageOrConwayEraOnwards
301-
(error $ "Expected Conway era onwards, got state in " <> docToString (pretty actualEra))
302-
(\cEra _ _ -> conwayEraOnwardsConstraints cEra $ do
298+
$ \(AnyNewEpochState actualEra newEpochState _) _ _ ->
299+
obtainCommonConstraints (unsafeEraFromSbe actualEra) $ do
303300
let proposals = newEpochState
304301
^. L.newEpochStateGovStateL
305302
. L.cgsProposalsL
@@ -310,4 +307,3 @@ getTreasuryWithdrawalProposal nodeConfigFile socketPath maxEpoch = withFrozenCal
310307
pure ConditionMet
311308
_ ->
312309
pure ConditionNotMet
313-
) actualEra

0 commit comments

Comments
 (0)