@@ -17,6 +17,7 @@ import Data.Integer as ℤ
1717open import Data.Integer.Properties using (module ≤-Reasoning; +-mono-≤; neg-mono-≤; +-identityˡ)
1818 renaming (nonNegative⁻¹ to nonNegative⁻¹ℤ)
1919open import Data.Nat.GeneralisedArithmetic using (iterate)
20+ open import Data.Maybe using (fromMaybe)
2021open import Data.Rational using (ℚ; floor; _*_; _÷_; _/_; _⊓_; _≟_; ≢-nonZero)
2122open import Data.Rational.Literals using (number; fromℤ)
2223open import Data.Rational.Properties using (nonNegative⁻¹; pos⇒nonNeg; ⊓-glb)
@@ -88,6 +89,9 @@ instance
8889 HasDeposits-EpochState : HasDeposits EpochState
8990 HasDeposits-EpochState .DepositsOf = DepositsOf ∘ LStateOf
9091
92+ HasDReps-EpochState : HasDReps EpochState
93+ HasDReps-EpochState .DRepsOf = DRepsOf ∘ CertStateOf ∘ LStateOf
94+
9195 HasTreasury-EpochState : HasTreasury EpochState
9296 HasTreasury-EpochState .TreasuryOf = Acnt.treasury ∘ EpochState.acnt
9397
@@ -445,46 +449,47 @@ each governance action, maps its `returnAddr` (as a staking
445449credential) to the deposit.
446450
447451``` agda
448- calculateVDelegDelegatedStake
449- : Epoch
450- → UTxOState
451- → GovState
452- → GState
453- → DState
454- → VDeleg ⇀ Coin
455- calculateVDelegDelegatedStake currentEpoch utxoSt govSt gState dState
456- = aggregate₊ (((activeVoteDelegs ˢ) ⁻¹ʳ
457- ∘ʳ (stakePerCredential ∪⁺ stakeFromGADeposits govSt utxoSt) ˢ) ᶠˢ)
458- where
459- open UTxOState utxoSt
460- open DState dState
461- open GState gState
452+ module VDelegDelegatedStake
453+ (currentEpoch : Epoch)
454+ (utxoSt : UTxOState)
455+ (govSt : GovState)
456+ (gState : GState)
457+ (dState : DState)
458+ where
462459
463- -- active DReps
464- activeDReps : ℙ Credential
465- activeDReps = dom (filterᵐ (λ (_ , e) → currentEpoch ≤ e) dreps)
460+ open UTxOState utxoSt
461+ open DState dState
462+ open GState gState
466463
467- -- active vote delegations
468- activeVoteDelegs : VoteDelegs
469- activeVoteDelegs = voteDelegs ∣^ ((mapˢ vDelegCredential activeDReps)
470- ∪ ❴ vDelegNoConfidence ❵ ∪ ❴ vDelegAbstain ❵)
464+ -- active DReps
465+ activeDReps : ℙ Credential
466+ activeDReps = dom (filterᵐ (λ (_ , e) → currentEpoch ≤ e) dreps)
471467
472- -- stake per delegated credential
473- stakePerCredential : Stake
474- stakePerCredential = mapFromFun (λ c → cbalance (utxo ∣^' λ txout → getStakeCred txout ≡ just c))
475- (dom activeVoteDelegs)
468+ -- active vote delegations
469+ activeVDelegs : ℙ VDeleg
470+ activeVDelegs = mapˢ vDelegCredential activeDReps ∪ ❴ vDelegNoConfidence ❵ ∪ ❴ vDelegAbstain ❵
476471
477- ```
472+ -- compute the stake for a credential
473+ stakePerCredential : Credential → Coin
474+ stakePerCredential c = cbalance (utxo ∣^' λ txout → getStakeCred txout ≡ just c)
475+ + fromMaybe 0 (lookupᵐ? (stakeFromGADeposits govSt utxoSt) c)
478476
477+ calculate : VDeleg ⇀ Coin
478+ calculate = mapFromFun (λ vd → ∑ˢ[ c ← voteDelegs ⁻¹ vd ] (stakePerCredential c))
479+ activeVDelegs
480+ ```
481+ <!--
482+ ```agda
483+ opaque
484+ ```
485+ -->
479486``` agda
480487 calculatePoolDelegatedStakeForVoting
481488 : Snapshot
482489 → UTxOState
483490 → GovState
484- → GState
485- → DState
486491 → KeyHash ⇀ Coin
487- calculatePoolDelegatedStakeForVoting ss utxoSt govSt gState dState
492+ calculatePoolDelegatedStakeForVoting ss utxoSt govSt
488493 = calculatePoolDelegatedStake ss ∪⁺ (stakeFromDeposits ∣ dom (PoolsOf ss))
489494 where
490495 stakeFromDeposits : KeyHash ⇀ Coin
@@ -515,17 +520,17 @@ governance actions.
515520 `SPO`{.AgdaInductiveConstructor}s as well.
516521
517522``` agda
518- mkStakeDistrs
519- : Snapshot
520- → Epoch
521- → UTxOState
522- → GovState
523- → GState
524- → DState
525- → StakeDistrs
526- mkStakeDistrs ss currentEpoch utxoSt govSt gState dState =
527- ⟦ calculateVDelegDelegatedStake currentEpoch utxoSt govSt gState dState
528- , calculatePoolDelegatedStakeForVoting ss utxoSt govSt gState dState ⟧
523+ mkStakeDistrs
524+ : Snapshot
525+ → Epoch
526+ → UTxOState
527+ → GovState
528+ → GState
529+ → DState
530+ → StakeDistrs
531+ mkStakeDistrs ss currentEpoch utxoSt govSt gState dState =
532+ ⟦ VDelegDelegatedStake.calculate currentEpoch utxoSt govSt gState dState
533+ , calculatePoolDelegatedStakeForVoting ss utxoSt govSt ⟧
529534```
530535
531536<!--
@@ -583,92 +588,83 @@ record EPOCH-Updates0 : Type where
583588 utxoSt' : UTxOState
584589 totWithdrawals : Coin
585590
586- EPOCH-updates0 : RatifyState → LState → EPOCH-Updates0
587- EPOCH-updates0 fut ls =
588- EPOCHUpdates0 es govSt' payout gState' utxoSt' totWithdrawals
589- where
590- open LState ls public
591- open CertState certState using (gState) public
592- open RatifyState fut renaming (es to esW)
591+ module EPOCHUpdates0 (fut : RatifyState)
592+ (ls : LState) where
593593
594- es : EnactState
595- es = record esW { withdrawals = ∅ }
594+ open LState ls public
595+ open CertState certState using (gState) public
596+ open RatifyState fut renaming (es to esW)
596597
597- tmpGovSt : GovState
598- tmpGovSt = filter (λ x → proj₁ x ∉ mapˢ proj₁ removed) govSt
598+ es : EnactState
599+ es = record esW { withdrawals = ∅ }
599600
600- orphans : ℙ (GovActionID × GovActionState)
601- orphans = fromList (getOrphans es tmpGovSt)
601+ tmpGovSt : GovState
602+ tmpGovSt = filter (λ x → proj₁ x ∉ mapˢ proj₁ removed) govSt
602603
603- removed' : ℙ (GovActionID × GovActionState)
604- removed' = removed ∪ orphans
604+ orphans : ℙ (GovActionID × GovActionState)
605+ orphans = fromList (getOrphans es tmpGovSt)
605606
606- govSt ' : GovState
607- govSt ' = filter (λ x → proj₁ x ∉ mapˢ proj₁ removed') govSt
607+ removed ' : ℙ (GovActionID × GovActionState)
608+ removed ' = removed ∪ orphans
608609
609- removedGovActions : ℙ (RwdAddr × DepositPurpose × Coin)
610- removedGovActions =
611- flip concatMapˢ removed' λ (gaid , gaSt) →
612- mapˢ
613- (returnAddr gaSt ,_)
614- ((DepositsOf utxoSt ∣ ❴ GovActionDeposit gaid ❵) ˢ)
610+ govSt' : GovState
611+ govSt' = filter (λ x → proj₁ x ∉ mapˢ proj₁ removed') govSt
615612
616- govActionReturns : RwdAddr ⇀ Coin
617- govActionReturns =
618- aggregate₊ (mapˢ (λ (a , _ , d) → a , d) removedGovActions ᶠˢ)
613+ removedGovActions : ℙ (RwdAddr × DepositPurpose × Coin)
614+ removedGovActions =
615+ flip concatMapˢ removed' λ (gaid , gaSt) →
616+ mapˢ
617+ (returnAddr gaSt ,_)
618+ ((DepositsOf utxoSt ∣ ❴ GovActionDeposit gaid ❵) ˢ)
619619
620- payout : RwdAddr ⇀ Coin
621- payout = govActionReturns ∪⁺ WithdrawalsOf esW
620+ govActionReturns : RwdAddr ⇀ Coin
621+ govActionReturns =
622+ aggregate₊ (mapˢ (λ (a , _ , d) → a , d) removedGovActions ᶠˢ)
622623
623- gState' : GState
624- gState' =
625- ⟦ (if null govSt' then mapValues (1 +_) (DRepsOf gState) else DRepsOf gState)
626- , CCHotKeysOf gState ∣ ccCreds (EnactState.cc es)
627- ⟧
624+ payout : RwdAddr ⇀ Coin
625+ payout = govActionReturns ∪⁺ WithdrawalsOf esW
628626
629- utxoSt ' : UTxOState
630- utxoSt ' = record utxoSt
631- { deposits = DepositsOf utxoSt ∣ mapˢ (proj₁ ∘ proj₂) removedGovActions ᶜ
632- ; donations = 0
633- }
627+ gState ' : GState
628+ gState ' =
629+ ⟦ (if null govSt' then mapValues sucᵉ (DRepsOf gState) else DRepsOf gState)
630+ , CCHotKeysOf gState ∣ ccCreds (EnactState.cc es)
631+ ⟧
634632
635- totWithdrawals : Coin
636- totWithdrawals = ∑[ x ← WithdrawalsOf esW ] x
633+ utxoSt' : UTxOState
634+ utxoSt' = ⟦ UTxOOf utxoSt , FeesOf utxoSt , DepositsOf utxoSt ∣ mapˢ (proj₁ ∘ proj₂) removedGovActions ᶜ , 0 ⟧
635+
636+ totWithdrawals : Coin
637+ totWithdrawals = ∑[ x ← WithdrawalsOf esW ] x
638+
639+ updates : EPOCH-Updates0
640+ updates = EPOCHUpdates0 es govSt' payout gState' utxoSt' totWithdrawals
637641
638642record EPOCH-Updates : Type where
639643 constructor EPOCHUpdates
640644 field
641- es : EnactState
642- govSt' : GovState
643645 dState'' : DState
644- gState' : GState
645- utxoSt' : UTxOState
646646 acnt'' : Acnt
647647
648- EPOCH-updates
649- : RatifyState → LState → DState → Acnt → EPOCH-Updates
650- EPOCH-updates fut ls dState' acnt' =
651- EPOCHUpdates (u0 .es) (u0 .govSt') dState'' (u0 .gState') (u0 .utxoSt') acnt''
652- where
653- open LState
654- open EPOCH-Updates0
648+ module EPOCHUpdates (eu0 : EPOCH-Updates0)
649+ (ls : LState)
650+ (dState' : DState)
651+ (acnt' : Acnt) where
652+ open LState
653+ open EPOCH-Updates0
655654
656- u0 = EPOCH-updates0 fut ls
655+ refunds : Credential ⇀ Coin
656+ refunds = pullbackMap (eu0 .payout) toRwdAddr (dom (RewardsOf dState'))
657657
658- refunds : Credential ⇀ Coin
659- refunds = pullbackMap (u0 .payout) toRwdAddr (dom ( RewardsOf dState'))
658+ dState'' : DState
659+ dState'' = ⟦ VoteDelegsOf dState' , StakeDelegsOf dState' , RewardsOf dState' ∪⁺ refunds ⟧
660660
661- dState'' : DState
662- dState'' = record dState' { rewards = RewardsOf dState' ∪⁺ refunds }
661+ unclaimed : Coin
662+ unclaimed = getCoin (eu0 .payout) - getCoin refunds
663663
664- unclaimed : Coin
665- unclaimed = getCoin (u0 .payout) - getCoin refunds
664+ acnt'' : Acnt
665+ acnt'' = ⟦ TreasuryOf acnt' ∸ eu0 .totWithdrawals + DonationsOf ls + unclaimed , ReservesOf acnt' ⟧
666666
667- acnt'' : Acnt
668- acnt'' = record acnt'
669- { treasury =
670- TreasuryOf acnt' ∸ u0 .totWithdrawals + DonationsOf ls + unclaimed
671- }
667+ updates = EPOCHUpdates dState'' acnt''
672668```
673669
674670### Transition Rule
@@ -705,8 +701,8 @@ data _⊢_⇀⦇_,EPOCH⦈_ : ⊤ → EpochState → Epoch → EpochState → Ty
705701-->
706702``` agda
707703 let
708- EPOCHUpdates es govSt' dState'' gState' utxoSt' acnt'' =
709- EPOCH- updates fut ls dState' acnt'
704+ eu0@(EPOCHUpdates0 es govSt' _ gState' utxoSt' _) = EPOCHUpdates0.updates fut ls
705+ EPOCHUpdates dState'' acnt'' = EPOCHUpdates. updates eu0 ls dState' acnt'
710706
711707 stakeDistrs : StakeDistrs
712708 stakeDistrs = mkStakeDistrs (Snapshots.mark ss') e utxoSt'
0 commit comments