Skip to content

Commit

Permalink
Add governance related ledger events (#3856)
Browse files Browse the repository at this point in the history
* Implement ConwayGovEvent
Emit an event for proposals snapshot in an incoming tx

* Add EpochBoundaryRatifyState to ConwayEpochEvent. This event will output
the RatityState at the epoch boundary.

* Update LedgerEvents.md
  • Loading branch information
Jimbo4350 authored Nov 8, 2023
1 parent a505e7a commit e8015f4
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 12 deletions.
8 changes: 8 additions & 0 deletions docs/LedgerEvents.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ all the information needed to re-run a Plutus script.
This event is the same as `SuccessfulPlutusScriptsEvent`, except that it contains the
information for all the failed Plutus scripts in a single transaction.

### `EpochBoundaryRatifyState ratifyState`

This event happens on the epoch boundary and gives us the ratification state (`RatifyState era`) _up to the current epoch boundary_.

### `GovNewProposals txid proposalsSnapshot`

This event is triggered on each tx and gives us the votes, proposals and txid of a particular tx.

## Notes / TODO

There appears to be multiple, redundant `NewEpoch` events.
2 changes: 2 additions & 0 deletions eras/conway/impl/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
* Fix [#3835](https://github.com/input-output-hk/cardano-ledger/issues/3835)
* Rename `PParamGroup` to `PPGroup` and `GovernanceGroup` to `GovGroup`
* Introduce `THKD` and use it for `ConwayPParams`
* Add `data ConwayGovEvent era` with constructor `GovNewProposals !(TxId (EraCrypto era)) !(ProposalsSnapshot era)`. #3856
* Add `EpochBoundaryRatifyState (RatifyState era)` inhabitant to the `ConwayEpochEvent era` data type.

### `testlib`

Expand Down
1 change: 0 additions & 1 deletion eras/conway/impl/src/Cardano/Ledger/Conway/Governance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
Expand Down
8 changes: 4 additions & 4 deletions eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Epoch.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE EmptyCase #-}
Expand All @@ -7,7 +6,6 @@
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
Expand Down Expand Up @@ -107,6 +105,7 @@ import Control.State.Transition (
TransitionRule,
judgmentContext,
liftSTS,
tellEvent,
trans,
)
import Data.Foldable (Foldable (..))
Expand All @@ -120,6 +119,7 @@ import Lens.Micro ((%~), (&), (+~), (.~), (<>~), (^.))
data ConwayEpochEvent era
= PoolReapEvent (Event (EraRule "POOLREAP" era))
| SnapEvent (Event (EraRule "SNAP" era))
| EpochBoundaryRatifyState (RatifyState era)

instance
( EraTxOut era
Expand Down Expand Up @@ -279,7 +279,7 @@ epochTransition = do
let
pulsingState = epochState0 ^. epochStateDRepPulsingStateL

RatifyState {rsRemoved, rsEnactState} = extractDRepPulsingState pulsingState
ratState0@RatifyState {rsRemoved, rsEnactState} = extractDRepPulsingState pulsingState

(accountState2, dState2, newEnactState) =
applyEnactedWithdrawals accountState1 dState1 rsEnactState
Expand Down Expand Up @@ -320,7 +320,7 @@ epochTransition = do
& esAccountStateL .~ accountState3
& esSnapshotsL .~ snapshots1
& esLStateL .~ ledgerState1

tellEvent $ EpochBoundaryRatifyState ratState0
liftSTS $ setFreshDRepPulsingState eNo stakePoolDistr epochState1

instance
Expand Down
18 changes: 13 additions & 5 deletions eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Gov.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
module Cardano.Ledger.Conway.Rules.Gov (
ConwayGOV,
GovEnv (..),
ConwayGovEvent (..),
ConwayGovPredFailure (..),
) where

Expand Down Expand Up @@ -90,6 +91,7 @@ import Control.State.Transition.Extended (
TransitionRule,
judgmentContext,
liftSTS,
tellEvent,
(?!),
)
import qualified Data.Map.Merge.Strict as Map (dropMissing, merge, zipWithMaybeMatched)
Expand Down Expand Up @@ -182,13 +184,16 @@ instance EraPParams era => ToCBOR (ConwayGovPredFailure era) where
instance EraPParams era => FromCBOR (ConwayGovPredFailure era) where
fromCBOR = fromEraCBOR @era

data ConwayGovEvent era
= GovNewProposals !(TxId (EraCrypto era)) !(ProposalsSnapshot era)

instance ConwayEraPParams era => STS (ConwayGOV era) where
type State (ConwayGOV era) = ProposalsSnapshot era
type Signal (ConwayGOV era) = GovProcedures era
type Environment (ConwayGOV era) = GovEnv era
type BaseM (ConwayGOV era) = ShelleyBase
type PredicateFailure (ConwayGOV era) = ConwayGovPredFailure era
type Event (ConwayGOV era) = ()
type Event (ConwayGOV era) = ConwayGovEvent era

initialRules = []

Expand All @@ -213,8 +218,8 @@ addAction ::
GovAction era ->
ProposalsSnapshot era ->
ProposalsSnapshot era
addAction epoch gaExpiry gaid c addr act as =
snapshotInsertGovAction gai' as
addAction epoch gaExpiry gaid c addr act =
snapshotInsertGovAction gai'
where
gai' =
GovActionState
Expand Down Expand Up @@ -287,7 +292,7 @@ checkProposalsHaveAValidPrevious prevGovActionIds snapshots procedures =
-- The case of having an SNothing as valid, for the very first proposal ever, is handled in `prevActionAsExpected`
SNothing -> False
SJust (PrevGovActionId govActionId) ->
case snapshotLookupId govActionId $ snapshots of
case snapshotLookupId govActionId snapshots of
Nothing -> False
-- lookup has to succeed _and_ purpose of looked-up action has to match condition
Just found -> snapshotCond $ gasAction found
Expand Down Expand Up @@ -377,7 +382,10 @@ govTransition = do

let applyVoterVotes curState voter =
Map.foldlWithKey' (addVoterVote voter) curState
pure $ Map.foldlWithKey' applyVoterVotes stProps votingProcedures
updatedProposalStates =
Map.foldlWithKey' applyVoterVotes stProps votingProcedures
tellEvent $ GovNewProposals txid updatedProposalStates
pure updatedProposalStates

instance Inject (ConwayGovPredFailure era) (ConwayGovPredFailure era) where
inject = id
5 changes: 3 additions & 2 deletions eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Ledger.hs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import Cardano.Ledger.Conway.Governance (
import Cardano.Ledger.Conway.PParams (ConwayEraPParams)
import Cardano.Ledger.Conway.Rules.Cert (CertEnv)
import Cardano.Ledger.Conway.Rules.Certs (CertsEnv (CertsEnv), ConwayCertsEvent, ConwayCertsPredFailure)
import Cardano.Ledger.Conway.Rules.Gov (ConwayGovPredFailure, GovEnv (..))
import Cardano.Ledger.Conway.Rules.Gov (ConwayGovEvent (..), ConwayGovPredFailure, GovEnv (..))
import Cardano.Ledger.Conway.Tx (AlonzoEraTx (..))
import Cardano.Ledger.Conway.TxBody (ConwayEraTxBody (..), currentTreasuryValueTxBodyL)
import Cardano.Ledger.Credential (Credential)
Expand Down Expand Up @@ -290,6 +290,7 @@ ledgerTransition = do
all (`UMap.member` delegatedAddrs) wdrlCreds
?! ConwayWdrlNotDelegatedToDRep (wdrlCreds Set.\\ Map.keysSet (dRepMap dUnified))

-- Votes and proposals from signal tx
let govProcedures =
GovProcedures
{ gpVotingProcedures = txBody ^. votingProceduresTxBodyL
Expand Down Expand Up @@ -391,7 +392,7 @@ instance
( ConwayEraPParams era
, BaseM (ConwayLEDGER era) ~ ShelleyBase
, PredicateFailure (EraRule "GOV" era) ~ ConwayGovPredFailure era
, Event (EraRule "GOV" era) ~ ()
, Event (EraRule "GOV" era) ~ ConwayGovEvent era
) =>
Embed (ConwayGOV era) (ConwayLEDGER era)
where
Expand Down

0 comments on commit e8015f4

Please sign in to comment.