Skip to content

Commit 434378c

Browse files
committed
Move unelected CC votes MEMPOOL check to GOV
1 parent 82c3d9b commit 434378c

File tree

11 files changed

+116
-68
lines changed

11 files changed

+116
-68
lines changed

eras/conway/impl/src/Cardano/Ledger/Conway/Governance.hs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -214,17 +214,13 @@ import Cardano.Ledger.Credential (Credential)
214214
import Cardano.Ledger.PoolParams (PoolParams (ppRewardAccount))
215215
import Cardano.Ledger.Shelley.LedgerState (
216216
EpochState (..),
217-
LedgerState,
218217
NewEpochState (..),
219218
epochStateGovStateL,
220219
epochStatePoolParamsL,
221220
esLStateL,
222221
lsCertState,
223-
lsCertStateL,
224222
lsUTxOState,
225-
lsUTxOStateL,
226223
newEpochStateGovStateL,
227-
utxosGovStateL,
228224
)
229225
import Cardano.Ledger.UMap
230226
import Cardano.Ledger.Val (Val (..))
@@ -235,7 +231,6 @@ import Control.Monad.Trans.Reader (ReaderT, ask)
235231
import Data.Aeson (KeyValue, ToJSON (..), object, pairs, (.=))
236232
import Data.Default (Default (..))
237233
import Data.Foldable (Foldable (..))
238-
import qualified Data.Foldable as F (foldl')
239234
import Data.Map.Strict (Map)
240235
import qualified Data.Map.Strict as Map
241236
import qualified Data.Set as Set
@@ -568,19 +563,14 @@ defaultStakePoolVote poolId poolParams dRepDelegations =
568563
toDefaultVote _ = DefaultNo
569564

570565
authorizedElectedHotCommitteeCredentials ::
571-
(ConwayEraGov era, ConwayEraCertState era) =>
572-
LedgerState era ->
566+
ConwayEraGov era =>
567+
GovState era ->
568+
CommitteeState era ->
573569
Set.Set (Credential 'HotCommitteeRole)
574-
authorizedElectedHotCommitteeCredentials ledgerState =
575-
case ledgerState ^. lsUTxOStateL . utxosGovStateL . committeeGovStateL of
570+
authorizedElectedHotCommitteeCredentials govState committeeState =
571+
case govState ^. committeeGovStateL of
576572
SNothing -> Set.empty
577573
SJust electedCommiteee ->
578-
collectAuthorizedHotCreds $
579-
csCommitteeCreds committeeState `Map.intersection` committeeMembers electedCommiteee
580-
where
581-
committeeState = ledgerState ^. lsCertStateL . certVStateL . vsCommitteeStateL
582-
collectAuthorizedHotCreds =
583-
let toHotCredSet !acc = \case
584-
CommitteeHotCredential hotCred -> Set.insert hotCred acc
585-
CommitteeMemberResigned {} -> acc
586-
in F.foldl' toHotCredSet Set.empty
574+
authorizedHotCommitteeCredentials $
575+
CommitteeState $
576+
csCommitteeCreds committeeState `Map.intersection` committeeMembers electedCommiteee

eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Gov.hs

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module Cardano.Ledger.Conway.Rules.Gov (
2626
GovSignal (..),
2727
ConwayGovEvent (..),
2828
ConwayGovPredFailure (..),
29+
unelectedCommitteeVoters,
2930
) where
3031

3132
import Cardano.Ledger.Address (RewardAccount, raCredential, raNetwork)
@@ -34,11 +35,12 @@ import Cardano.Ledger.BaseTypes (
3435
EpochNo (..),
3536
Mismatch (..),
3637
Network,
37-
ProtVer,
38+
ProtVer (ProtVer),
3839
Relation (..),
3940
ShelleyBase,
4041
StrictMaybe (SJust),
4142
addEpochInterval,
43+
natVersion,
4244
networkId,
4345
)
4446
import Cardano.Ledger.Binary (
@@ -61,17 +63,24 @@ import Cardano.Ledger.Coin (Coin (..))
6163
import Cardano.Ledger.Conway.Core (ppGovActionDepositL, ppGovActionLifetimeL)
6264
import Cardano.Ledger.Conway.Era (ConwayEra, ConwayGOV)
6365
import Cardano.Ledger.Conway.Governance (
66+
ConwayEraGov,
67+
ConwayGovState,
68+
EraGov,
6469
GovAction (..),
6570
GovActionId (..),
6671
GovActionPurpose (..),
6772
GovActionState (..),
6873
GovPurposeId (..),
6974
GovRelation (..),
75+
GovState,
7076
ProposalProcedure (..),
7177
Proposals,
7278
Voter (..),
7379
VotingProcedure (..),
7480
VotingProcedures (..),
81+
authorizedElectedHotCommitteeCredentials,
82+
constitutionGovStateL,
83+
constitutionScriptL,
7584
foldrVotingProcedures,
7685
gasAction,
7786
gasDRepVotesL,
@@ -146,27 +155,27 @@ data GovEnv era = GovEnv
146155
{ geTxId :: TxId
147156
, geEpoch :: EpochNo
148157
, gePParams :: PParams era
149-
, gePPolicy :: StrictMaybe ScriptHash
158+
, geGovState :: GovState era
150159
, geCertState :: CertState era
151160
}
152161
deriving (Generic)
153162

154-
instance (EraPParams era, EraCertState era) => EncCBOR (GovEnv era) where
163+
instance (EraGov era, EraPParams era, EraCertState era) => EncCBOR (GovEnv era) where
155164
encCBOR x@(GovEnv _ _ _ _ _) =
156165
let GovEnv {..} = x
157166
in encode $
158167
Rec GovEnv
159168
!> To geTxId
160169
!> To geEpoch
161170
!> To gePParams
162-
!> To gePPolicy
171+
!> To geGovState
163172
!> To geCertState
164173

165-
instance (NFData (PParams era), Era era, EraCertState era) => NFData (GovEnv era)
174+
instance (NFData (PParams era), EraGov era, EraCertState era) => NFData (GovEnv era)
166175

167-
deriving instance (Show (PParams era), Era era, EraCertState era) => Show (GovEnv era)
176+
deriving instance (Show (PParams era), EraGov era, EraCertState era) => Show (GovEnv era)
168177

169-
deriving instance (Eq (PParams era), EraCertState era) => Eq (GovEnv era)
178+
deriving instance (Eq (PParams era), EraGov era, EraCertState era) => Eq (GovEnv era)
170179

171180
data ConwayGovPredFailure era
172181
= GovActionsDoNotExist (NonEmpty GovActionId)
@@ -207,6 +216,8 @@ data ConwayGovPredFailure era
207216
ProposalReturnAccountDoesNotExist RewardAccount
208217
| -- | Treasury withdrawal proposals to an invalid reward account
209218
TreasuryWithdrawalReturnAccountsDoNotExist (NonEmpty RewardAccount)
219+
| -- | Disallow votes by unelected committee members
220+
UnelectedCommitteeVoters (NonEmpty (Credential 'HotCommitteeRole))
210221
deriving (Eq, Show, Generic)
211222

212223
type instance EraRuleFailure "GOV" ConwayEra = ConwayGovPredFailure ConwayEra
@@ -239,6 +250,7 @@ instance EraPParams era => DecCBOR (ConwayGovPredFailure era) where
239250
15 -> SumD ZeroTreasuryWithdrawals <! From
240251
16 -> SumD ProposalReturnAccountDoesNotExist <! From
241252
17 -> SumD TreasuryWithdrawalReturnAccountsDoNotExist <! From
253+
18 -> SumD UnelectedCommitteeVoters <! From
242254
k -> Invalid k
243255

244256
instance EraPParams era => EncCBOR (ConwayGovPredFailure era) where
@@ -280,6 +292,8 @@ instance EraPParams era => EncCBOR (ConwayGovPredFailure era) where
280292
Sum ProposalReturnAccountDoesNotExist 16 !> To returnAccount
281293
TreasuryWithdrawalReturnAccountsDoNotExist accounts ->
282294
Sum TreasuryWithdrawalReturnAccountsDoNotExist 17 !> To accounts
295+
UnelectedCommitteeVoters committee ->
296+
Sum UnelectedCommitteeVoters 18 !> To committee
283297

284298
instance EraPParams era => ToCBOR (ConwayGovPredFailure era) where
285299
toCBOR = toEraCBOR @era
@@ -324,6 +338,8 @@ instance (EraPParams era, NFData (TxCert era)) => NFData (GovSignal era)
324338
instance
325339
( ConwayEraTxCert era
326340
, ConwayEraPParams era
341+
, ConwayEraGov era
342+
, GovState era ~ ConwayGovState era
327343
, EraRule "GOV" era ~ ConwayGOV era
328344
, InjectRuleFailure "GOV" ConwayGovPredFailure era
329345
, EraCertState era
@@ -427,6 +443,8 @@ checkBootstrapProposal pp proposal@ProposalProcedure {pProcGovAction}
427443
govTransition ::
428444
forall era.
429445
( ConwayEraTxCert era
446+
, ConwayEraGov era
447+
, GovState era ~ ConwayGovState era
430448
, ConwayEraPParams era
431449
, STS (EraRule "GOV" era)
432450
, Event (EraRule "GOV" era) ~ ConwayGovEvent era
@@ -441,7 +459,7 @@ govTransition ::
441459
TransitionRule (EraRule "GOV" era)
442460
govTransition = do
443461
TRC
444-
( GovEnv txid currentEpoch pp constitutionPolicy certState
462+
( GovEnv txid currentEpoch pp govState certState
445463
, st
446464
, GovSignal {gsVotingProcedures, gsProposalProcedures, gsCertificates}
447465
) <-
@@ -450,13 +468,19 @@ govTransition = do
450468
certVState = certState ^. certVStateL
451469
certPState = certState ^. certPStateL
452470
certDState = certState ^. certDStateL
471+
constitutionPolicy = govState ^. constitutionGovStateL . constitutionScriptL
453472
committeeState = vsCommitteeState certVState
454473
knownDReps = vsDReps certVState
455474
knownStakePools = psStakePoolParams certPState
456475
knownCommitteeMembers = authorizedHotCommitteeCredentials committeeState
457476

458477
expectedNetworkId <- liftSTS $ asks networkId
459478

479+
unless (pp ^. ppProtocolVersionL < ProtVer (natVersion @11) 0) $
480+
failOnNonEmpty
481+
(unelectedCommitteeVoters govState committeeState gsVotingProcedures)
482+
UnelectedCommitteeVoters
483+
460484
let processProposal ps (idx, proposal@ProposalProcedure {..}) = do
461485
runTest $ checkBootstrapProposal pp proposal
462486

@@ -623,6 +647,22 @@ checkDisallowedVotes votes failure canBeVotedOnBy =
623647
disallowedVotes =
624648
[(voter, gasId gas) | (voter, gas) <- votes, not (gas `canBeVotedOnBy` voter)]
625649

650+
unelectedCommitteeVoters ::
651+
ConwayEraGov era =>
652+
GovState era ->
653+
CommitteeState era ->
654+
VotingProcedures era ->
655+
Set (Credential 'HotCommitteeRole)
656+
unelectedCommitteeVoters govState committeeState gsVotingProcedures =
657+
let authorizedElectedCommittee = authorizedElectedHotCommitteeCredentials govState committeeState
658+
collectUnelectedCommitteeVotes !unelectedHotCreds voter _ =
659+
case voter of
660+
CommitteeVoter hotCred
661+
| hotCred `Set.notMember` authorizedElectedCommittee ->
662+
Set.insert hotCred unelectedHotCreds
663+
_ -> unelectedHotCreds
664+
in Map.foldlWithKey' collectUnelectedCommitteeVotes Set.empty $ unVotingProcedures gsVotingProcedures
665+
626666
-- | If the GovAction is a HardFork, then return 3 things (if they exist)
627667
-- 1) The (StrictMaybe GovPurposeId), pointed to by the HardFork proposal
628668
-- 2) The proposed ProtVer

eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Ledger.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ import Cardano.Ledger.Conway.Governance (
6161
ConwayEraGov (..),
6262
ConwayGovState,
6363
Proposals,
64-
constitutionScriptL,
6564
grCommitteeL,
6665
proposalsGovStateL,
6766
proposalsWithPurpose,
@@ -438,7 +437,7 @@ ledgerTransition = do
438437
(txIdTxBody txBody)
439438
curEpochNo
440439
pp
441-
(govState ^. constitutionGovStateL . constitutionScriptL)
440+
govState
442441
certStateAfterCERTS
443442
, proposals
444443
, govSignal
@@ -538,6 +537,8 @@ instance
538537
instance
539538
( ConwayEraTxCert era
540539
, ConwayEraPParams era
540+
, ConwayEraGov era
541+
, GovState era ~ ConwayGovState era
541542
, BaseM (ConwayLEDGER era) ~ ShelleyBase
542543
, PredicateFailure (EraRule "GOV" era) ~ ConwayGovPredFailure era
543544
, Event (EraRule "GOV" era) ~ ConwayGovEvent era

eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Mempool.hs

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,17 @@ module Cardano.Ledger.Conway.Rules.Mempool (
1717
ConwayMEMPOOL,
1818
) where
1919

20-
import Cardano.Ledger.BaseTypes (ShelleyBase)
20+
import Cardano.Ledger.BaseTypes (ProtVer (ProtVer), ShelleyBase, natVersion)
2121
import Cardano.Ledger.Conway.Core
2222
import Cardano.Ledger.Conway.Era (ConwayLEDGER, ConwayMEMPOOL)
23-
import Cardano.Ledger.Conway.Governance (
24-
ConwayEraGov,
25-
ConwayGovState,
26-
Proposals,
27-
Voter (..),
28-
authorizedElectedHotCommitteeCredentials,
29-
unVotingProcedures,
30-
)
23+
import Cardano.Ledger.Conway.Governance (ConwayEraGov, ConwayGovState, Proposals)
3124
import Cardano.Ledger.Conway.Rules.Certs (CertsEnv)
32-
import Cardano.Ledger.Conway.Rules.Gov (GovEnv, GovSignal)
25+
import Cardano.Ledger.Conway.Rules.Gov (GovEnv, GovSignal, unelectedCommitteeVoters)
3326
import Cardano.Ledger.Conway.Rules.Ledger (ConwayLedgerEvent, ConwayLedgerPredFailure (..))
3427
import Cardano.Ledger.Conway.State
3528
import Cardano.Ledger.Shelley.LedgerState
36-
import Cardano.Ledger.Shelley.Rules (LedgerEnv (..), UtxoEnv)
29+
import Cardano.Ledger.Shelley.Rules (LedgerEnv (..), UtxoEnv, ledgerPpL)
30+
import Control.Monad (when)
3731
import Control.State.Transition (
3832
BaseM,
3933
Environment,
@@ -54,7 +48,6 @@ import Control.State.Transition.Extended (Embed (..), trans)
5448
import qualified Data.List.NonEmpty as NE
5549
import qualified Data.Map.Strict as Map
5650
import Data.Sequence (Seq)
57-
import qualified Data.Set as Set
5851
import Data.Text as T (pack)
5952
import Lens.Micro ((^.))
6053

@@ -100,7 +93,7 @@ mempoolTransition ::
10093
) =>
10194
TransitionRule (ConwayMEMPOOL era)
10295
mempoolTransition = do
103-
TRC trc@(_ledgerEnv, ledgerState, tx) <-
96+
TRC trc@(ledgerEnv, ledgerState, tx) <-
10497
judgmentContext
10598

10699
-- This rule only gets invoked on transactions within the mempool.
@@ -117,22 +110,22 @@ mempoolTransition = do
117110

118111
-- Skip all other checks if the transaction is probably a duplicate
119112
whenFailureFreeDefault ledgerState $ do
120-
-- Disallow votes by unelected committee members
121-
let
122-
authorizedElectedHotCreds = authorizedElectedHotCommitteeCredentials ledgerState
123-
collectUnelectedCommitteeVotes !unelectedHotCreds voter _ =
124-
case voter of
125-
CommitteeVoter hotCred
126-
| hotCred `Set.notMember` authorizedElectedHotCreds ->
127-
Set.insert hotCred unelectedHotCreds
128-
_ -> unelectedHotCreds
129-
unelectedCommitteeVoters =
130-
Map.foldlWithKey' collectUnelectedCommitteeVotes Set.empty $
131-
unVotingProcedures (tx ^. bodyTxL . votingProceduresTxBodyL)
132-
addPrefix =
133-
("Unelected committee members are not allowed to cast votes: " <>)
134-
failOnNonEmpty unelectedCommitteeVoters $
135-
ConwayMempoolFailure . addPrefix . T.pack . show . NE.toList
113+
when (ledgerEnv ^. ledgerPpL . ppProtocolVersionL < ProtVer (natVersion @11) 0) $
114+
-- This check can completely be removed once mainnet switches to protocol
115+
-- version 11, since the same check has been implemented in the GOV rule.
116+
-- We have to also carefully make the GOV rule check consistent for all
117+
-- protocol versions, even those below version 11, once we remove this
118+
-- check.
119+
--
120+
-- Disallow votes by unelected committee members
121+
let addPrefix = ("Unelected committee members are not allowed to cast votes: " <>)
122+
in failOnNonEmpty
123+
( unelectedCommitteeVoters
124+
(ledgerState ^. lsUTxOStateL . utxosGovStateL)
125+
(ledgerState ^. lsCertStateL . certVStateL . vsCommitteeStateL)
126+
(tx ^. bodyTxL . votingProceduresTxBodyL)
127+
)
128+
(ConwayMempoolFailure . addPrefix . T.pack . show . NE.toList)
136129

137130
-- Continue with LEDGER rules
138131
trans @(EraRule "LEDGER" era) $ TRC trc

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,14 @@ genConwayPlutusPurposePointer i =
621621

622622
-- GOV
623623

624-
instance (Era era, Arbitrary (PParamsHKD Identity era), Arbitrary (CertState era)) => Arbitrary (GovEnv era) where
624+
instance
625+
( Era era
626+
, Arbitrary (PParamsHKD Identity era)
627+
, Arbitrary (CertState era)
628+
, Arbitrary (GovState era)
629+
) =>
630+
Arbitrary (GovEnv era)
631+
where
625632
arbitrary =
626633
GovEnv
627634
<$> arbitrary

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ instance
4343
( Era era
4444
, ToExpr (TxCert era)
4545
, ToExpr (PParamsHKD StrictMaybe era)
46+
, ToExpr (GovState era)
4647
) =>
4748
ToExpr (ConwayPlutusPurpose AsItem era)
4849

@@ -245,7 +246,12 @@ instance
245246
) =>
246247
ToExpr (GovSignal era)
247248

248-
instance (ToExpr (PParams era), ToExpr (CertState era)) => ToExpr (GovEnv era)
249+
instance
250+
( ToExpr (PParams era)
251+
, ToExpr (CertState era)
252+
, ToExpr (GovState era)
253+
) =>
254+
ToExpr (GovEnv era)
249255

250256
instance
251257
( ToExpr (PParams era)

libs/cardano-ledger-conformance/src/Test/Cardano/Ledger/Conformance/SpecTranslate/Conway/Gov.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ instance
3434
, SpecTranslate ctx (CertState era)
3535
, SpecRep (CertState era) ~ Agda.CertState
3636
, EraCertState era
37+
, ConwayEraGov era
3738
) =>
3839
SpecTranslate ctx (GovEnv era)
3940
where
@@ -46,7 +47,7 @@ instance
4647
<$> toSpecRep geTxId
4748
<*> toSpecRep geEpoch
4849
<*> toSpecRep gePParams
49-
<*> toSpecRep gePPolicy
50+
<*> toSpecRep (geGovState ^. constitutionGovStateL . constitutionScriptL)
5051
<*> toSpecRep enactState
5152
<*> toSpecRep geCertState
5253
<*> toSpecRep rewardAccounts

0 commit comments

Comments
 (0)