{-# OPTIONS --safe #-}

open import Data.Integer using () renaming (+_ to pos)
import      Data.Integer as 
open import Data.Integer.Properties using (module ≤-Reasoning; +-mono-≤; neg-mono-≤; +-identityˡ)
                                    renaming (nonNegative⁻¹ to nonNegative⁻¹ℤ)
open import Data.Nat.GeneralisedArithmetic using (iterate)
open import Data.Rational using (; floor; _*_; _÷_; _/_; _⊓_; _≟_; ≢-nonZero)
open import Data.Rational.Literals using (number; fromℤ)
open import Data.Rational.Properties using (nonNegative⁻¹; pos⇒nonNeg; ⊓-glb)
open import stdlib.Data.Rational.Properties using (0≤⇒0≤floor; ÷-0≤⇒0≤; fromℕ-0≤; *-0≤⇒0≤; fromℤ-0≤)

open import Data.Integer.Tactic.RingSolver using (solve-∀)

open import Agda.Builtin.FromNat

open import Ledger.Prelude hiding (iterate; _/_; _*_; _⊓_; _≟_; ≢-nonZero)
open Filter using (filter)
open import Ledger.Conway.Abstract
open import Ledger.Conway.Transaction
open import Ledger.Conway.Types.Numeric.UnitInterval

open Number number renaming (fromNat to fromℕ)

module Ledger.Conway.Epoch
  (txs : _) (open TransactionStructure txs)
  (abs : AbstractFunctions txs) (open AbstractFunctions abs)
  where

open import Ledger.Conway.Certs govStructure
open import Ledger.Conway.Enact govStructure
open import Ledger.Conway.Gov txs
open import Ledger.Conway.Ledger txs abs
open import Ledger.Conway.Ratify txs
open import Ledger.Conway.Rewards txs abs
open import Ledger.Conway.Utxo txs abs




record EpochState : Type where


  constructor ⟦_,_,_,_,_⟧ᵉ'


  field
    acnt       : Acnt
    ss         : Snapshots
    ls         : LState
    es         : EnactState
    fut        : RatifyState



record HasEpochState {a} (A : Type a) : Type a where
  field EpochStateOf : A  EpochState
open HasEpochState ⦃...⦄ public

instance
  HasLState-EpochState : HasLState EpochState
  HasLState-EpochState .LStateOf = EpochState.ls

  HasEnactState-EpochState : HasEnactState EpochState
  HasEnactState-EpochState .EnactStateOf = EpochState.es

  HasDeposits-EpochState : HasDeposits EpochState
  HasDeposits-EpochState .DepositsOf = DepositsOf  LStateOf

  Hastreasury-EpochState : Hastreasury EpochState
  Hastreasury-EpochState .treasuryOf = Acnt.treasury  EpochState.acnt

  Hasreserves-EpochState : Hasreserves EpochState
  Hasreserves-EpochState .reservesOf = Acnt.reserves  EpochState.acnt

  HasPParams-EpochState : HasPParams EpochState
  HasPParams-EpochState .PParamsOf = PParamsOf  EnactStateOf



record NewEpochState : Type where
  field
    lastEpoch   : Epoch
    epochState  : EpochState
    ru          : Maybe RewardUpdate


record HasNewEpochState {a} (A : Type a) : Type a where
  field NewEpochStateOf : A  NewEpochState
open HasNewEpochState ⦃...⦄ public
record HasLastEpoch     {a} (A : Type a) : Type a where field LastEpochOf     : A  Epoch
open HasLastEpoch     ⦃...⦄ public

instance
  HasLastEpoch-NewEpochState : HasLastEpoch NewEpochState
  HasLastEpoch-NewEpochState .LastEpochOf = NewEpochState.lastEpoch

  HasEpochState-NewEpochState : HasEpochState NewEpochState
  HasEpochState-NewEpochState .EpochStateOf = NewEpochState.epochState

  Hastreasury-NewEpochState : Hastreasury NewEpochState
  Hastreasury-NewEpochState .treasuryOf = treasuryOf  EpochStateOf

  HasLState-NewEpochState : HasLState NewEpochState
  HasLState-NewEpochState .LStateOf = LStateOf  EpochStateOf

  HasGovState-NewEpochState : HasGovState NewEpochState
  HasGovState-NewEpochState .GovStateOf = GovStateOf  LStateOf

  HasCertState-NewEpochState : HasCertState NewEpochState
  HasCertState-NewEpochState .CertStateOf = CertStateOf  LStateOf

  HasDReps-NewEpochState : HasDReps NewEpochState 
  HasDReps-NewEpochState .DRepsOf = DRepsOf  CertStateOf

  HasRewards-NewEpochState : HasRewards NewEpochState
  HasRewards-NewEpochState .RewardsOf = RewardsOf  CertStateOf

  unquoteDecl HasCast-EpochState HasCast-NewEpochState = derive-HasCast
    ( (quote EpochState     , HasCast-EpochState)
     [ (quote NewEpochState  , HasCast-NewEpochState)])

toRwdAddr : Credential  RwdAddr
toRwdAddr x = record { net = NetworkId ; stake = x }

getStakeCred : TxOut  Maybe Credential
getStakeCred (a , _ , _ , _) = stakeCred a

open GovActionState using (returnAddr)


createRUpd :   BlocksMade  EpochState  Coin  RewardUpdate
createRUpd slotsPerEpoch b es total =
  record  { Δt = Δt₁
          ; Δr = 0 - Δr₁ + Δr₂
          ; Δf = 0 - pos feeSS
          ; rs = rs


          ; flowConservation = flowConservation
          ; Δt-nonnegative = Δt-nonneg
          ; Δf-nonpositive = Δf-nonpos


          }
  where
    prevPp       = PParamsOf es
    reserves     = reservesOf es
    pstakego     = es .EpochState.ss .Snapshots.go
    feeSS        = es .EpochState.ss .Snapshots.feeSS
    stake        = pstakego .Snapshot.stake
    delegs       = pstakego .Snapshot.delegations
    poolParams   = pstakego .Snapshot.poolParameters
    blocksMade   = ∑[ m  b ] m
    ρ            = fromUnitInterval (prevPp .PParams.monetaryExpansion)
    η            = fromℕ blocksMade ÷₀ (fromℕ slotsPerEpoch * ActiveSlotCoeff)
    Δr₁          = floor (1  η * ρ * fromℕ reserves)
    rewardPot    = pos feeSS + Δr₁
    τ            = fromUnitInterval (prevPp .PParams.treasuryCut)
    Δt₁          = floor (fromℤ rewardPot * τ)
    R            = rewardPot - Δt₁
    circulation  = total - reserves
    rs           = reward prevPp b (posPart R) poolParams stake delegs circulation
    Δr₂          = R - pos (∑[ c  rs ] c)



    -- Proofs
    -- Note: Overloading of + and - seems to interfere with
    -- the ring solver.
    lemmaFlow :  (t₁ r₁ f z : )
       (t₁ ℤ.+ (0 ℤ.- r₁ ℤ.+ ((f ℤ.+ r₁ ℤ.- t₁) ℤ.- z)) ℤ.+ (0 ℤ.- f) ℤ.+ z)  0
    lemmaFlow = solve-∀
    flowConservation = lemmaFlow Δt₁ Δr₁ (pos feeSS) (pos (∑[ c  rs ] c))

    ÷₀-0≤⇒0≤ :  (x y : )  0  x  0  y  0  (x ÷₀ y)
    ÷₀-0≤⇒0≤ x y 0≤x 0≤y with y  0
    ... | (yes y≡0) = nonNegative⁻¹ 0
    ... | (no y≢0)  = ÷-0≤⇒0≤ x y {{≢-nonZero y≢0}} 0≤x 0≤y

    η-nonneg : 0  η
    η-nonneg = ÷₀-0≤⇒0≤ _ _ (fromℕ-0≤ blocksMade)
      (*-0≤⇒0≤ _ _
        (fromℕ-0≤ slotsPerEpoch)
        (nonNegative⁻¹ ActiveSlotCoeff {{pos⇒nonNeg ActiveSlotCoeff}}))

    min1η-nonneg : 0  1  η
    min1η-nonneg = ⊓-glb (nonNegative⁻¹ 1) η-nonneg

    Δr₁-nonneg : 0  Δr₁
    Δr₁-nonneg = 0≤⇒0≤floor _
      (*-0≤⇒0≤ (1  η * ρ) (fromℕ reserves)
        (UnitInterval-*-0≤ (1  η) (prevPp .PParams.monetaryExpansion) min1η-nonneg)
        (fromℕ-0≤ reserves))

    rewardPot-nonneg : 0  rewardPot
    rewardPot-nonneg = +-mono-≤ (nonNegative⁻¹ℤ (pos feeSS)) Δr₁-nonneg

    Δt-nonneg : 0  Δt₁
    Δt-nonneg = 0≤⇒0≤floor _
      (UnitInterval-*-0≤ (fromℤ rewardPot) (prevPp .PParams.treasuryCut)
        (fromℤ-0≤ rewardPot rewardPot-nonneg))

    Δf-nonpos : (0 - pos feeSS)  0
    Δf-nonpos = begin
        0 - pos feeSS ≡⟨ +-identityˡ _ 
        ℤ.- pos feeSS ≤⟨ neg-mono-≤ (ℤ.+≤+ z≤n) 
        0             
      where open ≤-Reasoning


applyRUpd : RewardUpdate  EpochState  EpochState
applyRUpd rewardUpdate
  $\begin{pmatrix} \begin{pmatrix} \,\href{Ledger.Conway.Epoch.html#7070}{\htmlId{7070}{\htmlClass{Bound}{\text{treasury}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#7081}{\htmlId{7081}{\htmlClass{Bound}{\text{reserves}}}}\, \end{pmatrix}
  \\ \,\href{Ledger.Conway.Epoch.html#7097}{\htmlId{7097}{\htmlClass{Bound}{\text{ss}}}}\,
  \\ \begin{pmatrix} \begin{pmatrix} \,\href{Ledger.Conway.Epoch.html#7108}{\htmlId{7108}{\htmlClass{Bound}{\text{utxo}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#7115}{\htmlId{7115}{\htmlClass{Bound}{\text{fees}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#7122}{\htmlId{7122}{\htmlClass{Bound}{\text{deposits}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#7133}{\htmlId{7133}{\htmlClass{Bound}{\text{donations}}}}\, \end{pmatrix}
    \\ \,\href{Ledger.Conway.Epoch.html#7152}{\htmlId{7152}{\htmlClass{Bound}{\text{govSt}}}}\,
    \\ \begin{pmatrix} \begin{pmatrix} \,\href{Ledger.Conway.Epoch.html#7168}{\htmlId{7168}{\htmlClass{Bound}{\text{voteDelegs}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#7181}{\htmlId{7181}{\htmlClass{Bound}{\text{stakeDelegs}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#7195}{\htmlId{7195}{\htmlClass{Bound}{\text{rewards}}}}\, \end{pmatrix} \\ \,\href{Ledger.Conway.Epoch.html#7208}{\htmlId{7208}{\htmlClass{Bound}{\text{pState}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#7217}{\htmlId{7217}{\htmlClass{Bound}{\text{gState}}}}\, \end{pmatrix} \end{pmatrix}
  \\ \,\href{Ledger.Conway.Epoch.html#7235}{\htmlId{7235}{\htmlClass{Bound}{\text{es}}}}\,
  \\ \,\href{Ledger.Conway.Epoch.html#7242}{\htmlId{7242}{\htmlClass{Bound}{\text{fut}}}}\,
  \end{pmatrix}$ =
  $\begin{pmatrix} \begin{pmatrix} \,\href{Prelude.html#3167}{\htmlId{7260}{\htmlClass{Function}{\text{posPart}}}}\, (\,\href{Ledger.Conway.Epoch.html#76}{\htmlId{7269}{\htmlClass{InductiveConstructor}{\text{pos}}}}\, \,\href{Ledger.Conway.Epoch.html#7070}{\htmlId{7273}{\htmlClass{Bound}{\text{treasury}}}}\, \,\href{Class.HasAdd.Core.html#162}{\htmlId{7282}{\htmlClass{Field Operator}{\text{+}}}}\, \,\href{Ledger.Conway.Rewards.html#5726}{\htmlId{7284}{\htmlClass{Function}{\text{Δt}}}}\, \,\href{Class.HasAdd.Core.html#162}{\htmlId{7287}{\htmlClass{Field Operator}{\text{+}}}}\, \,\href{Ledger.Conway.Epoch.html#76}{\htmlId{7289}{\htmlClass{InductiveConstructor}{\text{pos}}}}\, \,\href{Ledger.Conway.Epoch.html#7655}{\htmlId{7293}{\htmlClass{Function}{\text{unregRU'}}}}\,)
    \\ \,\href{Prelude.html#3167}{\htmlId{7309}{\htmlClass{Function}{\text{posPart}}}}\, (\,\href{Ledger.Conway.Epoch.html#76}{\htmlId{7318}{\htmlClass{InductiveConstructor}{\text{pos}}}}\, \,\href{Ledger.Conway.Epoch.html#7081}{\htmlId{7322}{\htmlClass{Bound}{\text{reserves}}}}\, \,\href{Class.HasAdd.Core.html#162}{\htmlId{7331}{\htmlClass{Field Operator}{\text{+}}}}\, \,\href{Ledger.Conway.Rewards.html#5729}{\htmlId{7333}{\htmlClass{Function}{\text{Δr}}}}\,) \end{pmatrix}
  \\ \,\href{Ledger.Conway.Epoch.html#7097}{\htmlId{7343}{\htmlClass{Bound}{\text{ss}}}}\,
  \\ \begin{pmatrix} \begin{pmatrix} \,\href{Ledger.Conway.Epoch.html#7108}{\htmlId{7354}{\htmlClass{Bound}{\text{utxo}}}}\, \\ \,\href{Prelude.html#3167}{\htmlId{7361}{\htmlClass{Function}{\text{posPart}}}}\, (\,\href{Ledger.Conway.Epoch.html#76}{\htmlId{7370}{\htmlClass{InductiveConstructor}{\text{pos}}}}\, \,\href{Ledger.Conway.Epoch.html#7115}{\htmlId{7374}{\htmlClass{Bound}{\text{fees}}}}\, \,\href{Class.HasAdd.Core.html#162}{\htmlId{7379}{\htmlClass{Field Operator}{\text{+}}}}\, \,\href{Ledger.Conway.Rewards.html#5732}{\htmlId{7381}{\htmlClass{Function}{\text{Δf}}}}\,) \\ \,\href{Ledger.Conway.Epoch.html#7122}{\htmlId{7387}{\htmlClass{Bound}{\text{deposits}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#7133}{\htmlId{7398}{\htmlClass{Bound}{\text{donations}}}}\, \end{pmatrix}
    \\ \,\href{Ledger.Conway.Epoch.html#7152}{\htmlId{7416}{\htmlClass{Bound}{\text{govSt}}}}\,
    \\ \begin{pmatrix} \begin{pmatrix} \,\href{Ledger.Conway.Epoch.html#7168}{\htmlId{7432}{\htmlClass{Bound}{\text{voteDelegs}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#7181}{\htmlId{7445}{\htmlClass{Bound}{\text{stakeDelegs}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#7195}{\htmlId{7459}{\htmlClass{Bound}{\text{rewards}}}}\, \,\href{Axiom.Set.Map.Dec.html#1854}{\htmlId{7467}{\htmlClass{Function Operator}{\text{∪⁺}}}}\, \,\href{Ledger.Conway.Epoch.html#7587}{\htmlId{7470}{\htmlClass{Function}{\text{regRU}}}}\, \end{pmatrix} \\ \,\href{Ledger.Conway.Epoch.html#7208}{\htmlId{7480}{\htmlClass{Bound}{\text{pState}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#7217}{\htmlId{7489}{\htmlClass{Bound}{\text{gState}}}}\, \end{pmatrix} \end{pmatrix}
  \\ \,\href{Ledger.Conway.Epoch.html#7235}{\htmlId{7504}{\htmlClass{Bound}{\text{es}}}}\,
  \\ \,\href{Ledger.Conway.Epoch.html#7242}{\htmlId{7511}{\htmlClass{Bound}{\text{fut}}}}\, \end{pmatrix}$
  where
    open RewardUpdate rewardUpdate using (Δt; Δr; Δf; rs)
    regRU     = rs  dom rewards
    unregRU   = rs  dom rewards 
    unregRU'  = ∑[ x  unregRU ] x

getOrphans : EnactState  GovState  GovState
getOrphans es govSt = proj₁ $ iterate step ([] , govSt) (length govSt)
  where
    step : GovState × GovState  GovState × GovState
    step (orps , govSt) =
      let
        isOrphan? a prev = ¬? (hasParent? es govSt a prev)
        (orps' , govSt') = partition
           (_ , record {action = a ; prevAction = prev})  isOrphan? (a .gaType) prev) govSt
      in
        (orps ++ orps' , govSt')


open RwdAddr using (stake)


gaDepositStake : GovState  Deposits  Credential  Coin
gaDepositStake govSt ds = aggregateBy
  (mapˢ  (gaid , addr)  (gaid , addr) , stake addr) govSt')
  (mapFromPartialFun  (gaid , _)  lookupᵐ? ds (GovActionDeposit gaid)) govSt')
  where govSt' = mapˢ (map₂ returnAddr) (fromList govSt)



opaque


  mkStakeDistrs : Snapshot  GovState  Deposits  (Credential  VDeleg)  StakeDistrs
  mkStakeDistrs ss govSt ds delegations .StakeDistrs.stakeDistr =
    aggregateBy  delegations  (Snapshot.stake ss ∪⁺ gaDepositStake govSt ds)


private variable
  nes nes' : NewEpochState
  e lastEpoch : Epoch
  fut fut' : RatifyState
  eps eps' eps'' : EpochState
  ls : LState
  acnt : Acnt
  es₀ : EnactState
  mark set go : Snapshot
  feeSS : Coin
  lstate : LState
  ss ss' : Snapshots
  ru : RewardUpdate
  mru : Maybe RewardUpdate


data _⊢_⇀⦇_,EPOCH⦈_ :   EpochState  Epoch  EpochState  Type where


  EPOCH : let


      open LState ls
      open CertState certState
      open RatifyState fut hiding (es)
      open UTxOState
      open PState; open DState; open GState
      open Acnt; open EnactState; open GovActionState


      esW               = RatifyState.es fut
      es                = record esW { withdrawals =  }
      tmpGovSt          = filter  x  proj₁ x  mapˢ proj₁ removed) govSt
      orphans           = fromList (getOrphans es tmpGovSt)
      removed'          = removed  orphans
      removedGovActions = flip concatMapˢ removed' λ (gaid , gaSt) 
        mapˢ (returnAddr gaSt ,_) ((utxoSt .deposits   GovActionDeposit gaid ) ˢ)
      govActionReturns = aggregate₊ (mapˢ  (a , _ , d)  a , d) removedGovActions ᶠˢ)

      trWithdrawals   = esW .withdrawals
      totWithdrawals  = ∑[ x  trWithdrawals ] x

      retired    = (pState .retiring) ⁻¹ e
      payout     = govActionReturns ∪⁺ trWithdrawals
      refunds    = pullbackMap payout toRwdAddr (dom (dState .rewards))
      unclaimed  = getCoin payout - getCoin refunds

      govSt' = filter  x  proj₁ x  mapˢ proj₁ removed') govSt

      dState' = $\begin{pmatrix} \,\href{Ledger.Conway.Certs.html#4418}{\htmlId{10227}{\htmlClass{Function}{\text{dState}}}}\, \,\htmlId{10234}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Certs.html#3258}{\htmlId{10235}{\htmlClass{Field}{\text{voteDelegs}}}}\, \\ \,\href{Ledger.Conway.Certs.html#4418}{\htmlId{10248}{\htmlClass{Function}{\text{dState}}}}\, \,\htmlId{10255}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Certs.html#3297}{\htmlId{10256}{\htmlClass{Field}{\text{stakeDelegs}}}}\, \\  \,\href{Ledger.Conway.Certs.html#4418}{\htmlId{10271}{\htmlClass{Function}{\text{dState}}}}\, \,\htmlId{10278}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Certs.html#3337}{\htmlId{10279}{\htmlClass{Field}{\text{rewards}}}}\, \,\href{Axiom.Set.Map.Dec.html#1854}{\htmlId{10287}{\htmlClass{Function Operator}{\text{∪⁺}}}}\, \,\href{Ledger.Conway.Epoch.html#10023}{\htmlId{10290}{\htmlClass{Bound}{\text{refunds}}}}\, \end{pmatrix}$

      pState' = $\begin{pmatrix} \,\href{Ledger.Conway.Certs.html#4438}{\htmlId{10319}{\htmlClass{Function}{\text{pState}}}}\, \,\htmlId{10326}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Certs.html#3841}{\htmlId{10327}{\htmlClass{Field}{\text{pools}}}}\, \,\href{Axiom.Set.Map.html#10748}{\htmlId{10333}{\htmlClass{Function Operator}{\text{∣}}}}\, \,\href{Ledger.Conway.Epoch.html#9927}{\htmlId{10335}{\htmlClass{Bound}{\text{retired}}}}\, \,\href{Axiom.Set.Map.html#10748}{\htmlId{10343}{\htmlClass{Function Operator}{\text{ᶜ}}}}\, \\ \,\href{Ledger.Conway.Certs.html#4438}{\htmlId{10347}{\htmlClass{Function}{\text{pState}}}}\, \,\htmlId{10354}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Certs.html#3878}{\htmlId{10355}{\htmlClass{Field}{\text{retiring}}}}\, \,\href{Axiom.Set.Map.html#10748}{\htmlId{10364}{\htmlClass{Function Operator}{\text{∣}}}}\, \,\href{Ledger.Conway.Epoch.html#9927}{\htmlId{10366}{\htmlClass{Bound}{\text{retired}}}}\, \,\href{Axiom.Set.Map.html#10748}{\htmlId{10374}{\htmlClass{Function Operator}{\text{ᶜ}}}}\, \end{pmatrix}$

      gState' = $\begin{pmatrix} (\,\href{Class.ToBool.html#389}{\htmlId{10398}{\htmlClass{Function Operator}{\text{if}}}}\, \,\href{Data.List.Base.html#4703}{\htmlId{10401}{\htmlClass{Function}{\text{null}}}}\, \,\href{Ledger.Conway.Epoch.html#10148}{\htmlId{10406}{\htmlClass{Bound}{\text{govSt'}}}}\, \,\href{Class.ToBool.html#389}{\htmlId{10413}{\htmlClass{Function Operator}{\text{then}}}}\, \,\href{Axiom.Set.Map.html#6068}{\htmlId{10418}{\htmlClass{Function}{\text{mapValues}}}}\, (\,\htmlId{10429}{\htmlClass{Number}{\text{1}}}\, \,\href{Class.HasAdd.Core.html#162}{\htmlId{10431}{\htmlClass{Field Operator}{\text{+\_}}}}\,) (\,\href{Ledger.Conway.Certs.html#4458}{\htmlId{10436}{\htmlClass{Function}{\text{gState}}}}\, \,\htmlId{10443}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Certs.html#4082}{\htmlId{10444}{\htmlClass{Field}{\text{dreps}}}}\,) \,\href{Class.ToBool.html#389}{\htmlId{10451}{\htmlClass{Function Operator}{\text{else}}}}\, (\,\href{Ledger.Conway.Certs.html#4458}{\htmlId{10457}{\htmlClass{Function}{\text{gState}}}}\, \,\htmlId{10464}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Certs.html#4082}{\htmlId{10465}{\htmlClass{Field}{\text{dreps}}}}\,)
                , \,\href{Ledger.Conway.Certs.html#4458}{\htmlId{10491}{\htmlClass{Function}{\text{gState}}}}\, \,\htmlId{10498}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Certs.html#4105}{\htmlId{10499}{\htmlClass{Field}{\text{ccHotKeys}}}}\, \,\href{Axiom.Set.Map.html#10678}{\htmlId{10509}{\htmlClass{Function Operator}{\text{∣}}}}\, \,\href{Ledger.Conway.Enact.html#1215}{\htmlId{10511}{\htmlClass{Function}{\text{ccCreds}}}}\, (\,\href{Ledger.Conway.Epoch.html#9356}{\htmlId{10520}{\htmlClass{Bound}{\text{es}}}}\, \,\htmlId{10523}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Enact.html#454}{\htmlId{10524}{\htmlClass{Field}{\text{cc}}}}\,) \end{pmatrix}$

      certState' : CertState
      certState' = $\begin{pmatrix} \,\href{Ledger.Conway.Epoch.html#10215}{\htmlId{10581}{\htmlClass{Bound}{\text{dState'}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#10307}{\htmlId{10591}{\htmlClass{Bound}{\text{pState'}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#10385}{\htmlId{10601}{\htmlClass{Bound}{\text{gState'}}}}\, \end{pmatrix}$

      utxoSt' = $\begin{pmatrix} \,\href{Ledger.Conway.Ledger.html#924}{\htmlId{10630}{\htmlClass{Function}{\text{utxoSt}}}}\, \,\htmlId{10637}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Utxo.html#2811}{\htmlId{10638}{\htmlClass{Field}{\text{utxo}}}}\, \\ \,\href{Ledger.Conway.Ledger.html#924}{\htmlId{10645}{\htmlClass{Function}{\text{utxoSt}}}}\, \,\htmlId{10652}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Utxo.html#2833}{\htmlId{10653}{\htmlClass{Field}{\text{fees}}}}\, \\ \,\href{Ledger.Conway.Ledger.html#924}{\htmlId{10660}{\htmlClass{Function}{\text{utxoSt}}}}\, \,\htmlId{10667}{\htmlClass{Symbol}{\text{.}}}\,\,\href{Ledger.Conway.Utxo.html#2855}{\htmlId{10668}{\htmlClass{Field}{\text{deposits}}}}\, \,\href{Axiom.Set.Map.html#10748}{\htmlId{10677}{\htmlClass{Function Operator}{\text{∣}}}}\, \,\href{abstract-set-theory.FiniteSetTheory.html#519}{\htmlId{10679}{\htmlClass{Function}{\text{mapˢ}}}}\, (\,\href{Data.Product.Base.html#636}{\htmlId{10685}{\htmlClass{Field}{\text{proj₁}}}}\, \,\href{Function.Base.html#1115}{\htmlId{10691}{\htmlClass{Function Operator}{\text{∘}}}}\, \,\href{Data.Product.Base.html#650}{\htmlId{10693}{\htmlClass{Field}{\text{proj₂}}}}\,) \,\href{Ledger.Conway.Epoch.html#9593}{\htmlId{10700}{\htmlClass{Bound}{\text{removedGovActions}}}}\, \,\href{Axiom.Set.Map.html#10748}{\htmlId{10718}{\htmlClass{Function Operator}{\text{ᶜ}}}}\, \\ \,\htmlId{10722}{\htmlClass{Number}{\text{0}}}\, \end{pmatrix}$

      acnt' = record acnt
        { treasury  = acnt .treasury  totWithdrawals + utxoSt .donations + unclaimed }
    in
    record { currentEpoch = e
           ; stakeDistrs = mkStakeDistrs  (Snapshots.mark ss') govSt'
                                          (utxoSt' .deposits) (voteDelegs dState)
           ; treasury = acnt .treasury ; GState gState
           ; pools = pState .pools ; delegatees = dState .voteDelegs }
         $\begin{pmatrix} \,\href{Ledger.Conway.Epoch.html#9356}{\htmlId{11168}{\htmlClass{Bound}{\text{es}}}}\, \\ \,\href{Class.HasEmptySet.html#287}{\htmlId{11173}{\htmlClass{Field}{\text{∅}}}}\, \\ \,\href{Agda.Builtin.Bool.html#192}{\htmlId{11177}{\htmlClass{InductiveConstructor}{\text{false}}}}\, \end{pmatrix}$ ⇀⦇ govSt' ,RATIFIES⦈ fut'
       ls  ss ⇀⦇ tt ,SNAP⦈ ss'
    ────────────────────────────────
    _  $\begin{pmatrix} \,\href{Ledger.Conway.Epoch.html#8845}{\htmlId{11291}{\htmlClass{Generalizable}{\text{acnt}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#8936}{\htmlId{11298}{\htmlClass{Generalizable}{\text{ss}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#8831}{\htmlId{11303}{\htmlClass{Generalizable}{\text{ls}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#8859}{\htmlId{11308}{\htmlClass{Generalizable}{\text{es₀}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#8776}{\htmlId{11314}{\htmlClass{Generalizable}{\text{fut}}}}\, \end{pmatrix}$ ⇀⦇ e ,EPOCH⦈
        $\begin{pmatrix} \,\href{Ledger.Conway.Epoch.html#10733}{\htmlId{11343}{\htmlClass{Bound}{\text{acnt'}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#8939}{\htmlId{11351}{\htmlClass{Generalizable}{\text{ss'}}}}\, \\ \begin{pmatrix} \,\href{Ledger.Conway.Epoch.html#10618}{\htmlId{11359}{\htmlClass{Bound}{\text{utxoSt'}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#10148}{\htmlId{11369}{\htmlClass{Bound}{\text{govSt'}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#10537}{\htmlId{11378}{\htmlClass{Bound}{\text{certState'}}}}\, \end{pmatrix} \\ \,\href{Ledger.Conway.Epoch.html#9356}{\htmlId{11393}{\htmlClass{Bound}{\text{es}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#8780}{\htmlId{11398}{\htmlClass{Generalizable}{\text{fut'}}}}\, \end{pmatrix}$


data


  _⊢_⇀⦇_,NEWEPOCH⦈_ :   NewEpochState  Epoch  NewEpochState  Type


  where


  NEWEPOCH-New : let
      eps' = applyRUpd ru eps
    in
     e  lastEpoch + 1
     _  eps' ⇀⦇ e ,EPOCH⦈ eps''
      ────────────────────────────────
      _  $\begin{pmatrix} \,\href{Ledger.Conway.Epoch.html#8756}{\htmlId{11664}{\htmlClass{Generalizable}{\text{lastEpoch}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#8801}{\htmlId{11676}{\htmlClass{Generalizable}{\text{eps}}}}\, \\ \,\href{Agda.Builtin.Maybe.html#173}{\htmlId{11682}{\htmlClass{InductiveConstructor}{\text{just}}}}\, \,\href{Ledger.Conway.Epoch.html#8957}{\htmlId{11687}{\htmlClass{Generalizable}{\text{ru}}}}\, \end{pmatrix}$ ⇀⦇ e ,NEWEPOCH⦈ $\begin{pmatrix} \,\href{Ledger.Conway.Epoch.html#8754}{\htmlId{11710}{\htmlClass{Generalizable}{\text{e}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#8810}{\htmlId{11714}{\htmlClass{Generalizable}{\text{eps''}}}}\, \\ \,\href{Agda.Builtin.Maybe.html#194}{\htmlId{11722}{\htmlClass{InductiveConstructor}{\text{nothing}}}}\, \end{pmatrix}$

  NEWEPOCH-Not-New :
     e  lastEpoch + 1
      ────────────────────────────────
      _  $\begin{pmatrix} \,\href{Ledger.Conway.Epoch.html#8756}{\htmlId{11829}{\htmlClass{Generalizable}{\text{lastEpoch}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#8801}{\htmlId{11841}{\htmlClass{Generalizable}{\text{eps}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#8977}{\htmlId{11847}{\htmlClass{Generalizable}{\text{mru}}}}\, \end{pmatrix}$ ⇀⦇ e ,NEWEPOCH⦈ $\begin{pmatrix} \,\href{Ledger.Conway.Epoch.html#8756}{\htmlId{11871}{\htmlClass{Generalizable}{\text{lastEpoch}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#8801}{\htmlId{11883}{\htmlClass{Generalizable}{\text{eps}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#8977}{\htmlId{11889}{\htmlClass{Generalizable}{\text{mru}}}}\, \end{pmatrix}$

  NEWEPOCH-No-Reward-Update :
     e  lastEpoch + 1
     _  eps ⇀⦇ e ,EPOCH⦈ eps'
      ────────────────────────────────
      _  $\begin{pmatrix} \,\href{Ledger.Conway.Epoch.html#8756}{\htmlId{12033}{\htmlClass{Generalizable}{\text{lastEpoch}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#8801}{\htmlId{12045}{\htmlClass{Generalizable}{\text{eps}}}}\, \\ \,\href{Agda.Builtin.Maybe.html#194}{\htmlId{12051}{\htmlClass{InductiveConstructor}{\text{nothing}}}}\, \end{pmatrix}$ ⇀⦇ e ,NEWEPOCH⦈ $\begin{pmatrix} \,\href{Ledger.Conway.Epoch.html#8754}{\htmlId{12079}{\htmlClass{Generalizable}{\text{e}}}}\, \\ \,\href{Ledger.Conway.Epoch.html#8805}{\htmlId{12083}{\htmlClass{Generalizable}{\text{eps'}}}}\, \\ \,\href{Agda.Builtin.Maybe.html#194}{\htmlId{12090}{\htmlClass{InductiveConstructor}{\text{nothing}}}}\, \end{pmatrix}$