Skip to content

Commit 6106fdc

Browse files
authored
[Dijkstra] Change SNAP invocation in EPOCH to after ledger state update (#1313)
- Inline `StakeDistrs` record into RatifyEnv - Fix computational instance - Add prose explaining this change
1 parent b777232 commit 6106fdc

6 files changed

Lines changed: 80 additions & 73 deletions

File tree

build-tools/static/hs-src/src/MAlonzo/Code/Ledger/Dijkstra/Foreign/API.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import MAlonzo.Code.Ledger.Dijkstra.Foreign.Ledger as X
4040
import MAlonzo.Code.Ledger.Dijkstra.Foreign.NewEpoch as X
4141
(NewEpochState(..), newEpochStep)
4242
import MAlonzo.Code.Ledger.Dijkstra.Foreign.Ratify as X
43-
(StakeDistrs(..), RatifyEnv(..), RatifyState(..), ratifyStep)
43+
(RatifyEnv(..), RatifyState(..), ratifyStep)
4444
import MAlonzo.Code.Ledger.Dijkstra.Foreign.Rewards as X
4545
(RewardUpdate(..), Snapshot(..), Snapshots(..))
4646
import MAlonzo.Code.Ledger.Dijkstra.Foreign.Utxo as X

src/Ledger/Dijkstra/Foreign/Ratify.agda

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,6 @@ open import Ledger.Dijkstra.Specification.Ratify.Properties.Computational it
2323
open Computational
2424

2525
instance
26-
HsTy-StakeDistrs = autoHsType StakeDistrs ⊣ withConstructor "MkStakeDistrs"
27-
• fieldPrefix "sd"
28-
Conv-StakeDistrs = autoConvert StakeDistrs
29-
3026
HsTy-RatifyEnv = autoHsType RatifyEnv ⊣ withConstructor "MkRatifyEnv"
3127
• fieldPrefix "re"
3228
Conv-RatifyEnv = autoConvert RatifyEnv

src/Ledger/Dijkstra/Specification/Epoch.lagda.md

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,9 @@ applyRUpd rewardUpdate ⟦ ⟦ treasury , reserves ⟧ᵃ
398398
## Stake Distributions {#sec:stake-distributions}
399399

400400
This section defines the functions
401-
`calculatePoolDelegatedStake`{.AgdaFunction},
402-
`calculateVDelegDelegatedStake`{.AgdaFunction}, and
403-
`mkStakeDistrs`{.AgdaFunction}, which calculates stake distributions
404-
for voting purposes.
401+
`calculatePoolDelegatedStake`{.AgdaFunction} and
402+
`calculateVDelegDelegatedStake`{.AgdaFunction}, which calculate stake
403+
distributions for voting purposes.
405404

406405
<!--
407406
```agda
@@ -525,20 +524,6 @@ the deposits made to governance actions.
525524
count deposits on governance actions towards the stake of
526525
`SPOs`{.AgdaInductiveConstructor} as well.
527526

528-
```agda
529-
mkStakeDistrs
530-
: Snapshot
531-
→ Epoch
532-
→ UTxOState
533-
→ GovState
534-
→ GState
535-
→ DState
536-
→ StakeDistrs
537-
mkStakeDistrs ss currentEpoch utxoSt govSt gState dState =
538-
⟦ calculateVDelegDelegatedStake currentEpoch utxoSt govSt gState dState
539-
, calculatePoolDelegatedStakeForVoting ss govSt ⟧
540-
```
541-
542527
<!--
543528
```agda
544529
private variable
@@ -730,7 +715,7 @@ module Post-POOLREAPUpdate (es : EnactState)
730715

731716
This section defines the `EPOCH`{.AgdaDatatype} transition rule.
732717

733-
In Conway, the `EPOCH`{.AgdaDatatype} rule invokes `RATIFIES`{.AgdaDatatype},
718+
In Dijkstra the `EPOCH`{.AgdaDatatype} rule invokes `RATIFIES`{.AgdaDatatype},
734719
and carries out the following tasks:
735720

736721
+ payout all the enacted treasury withdrawals;
@@ -743,10 +728,16 @@ and carries out the following tasks:
743728
+ remove all hot keys from the constitutional committee delegation map
744729
that do not belong to currently elected members;
745730

746-
+ Apply the resulting enact state from the previous epoch boundary
747-
`fut`{.AgdaBound} and store the resulting enact state
731+
+ apply the resulting enact state from the previous epoch boundary
732+
`fut`{.AgdaBound} and store the new enact state
748733
`fut’`{.AgdaBound}.
749734

735+
Unlike Conway, where the mark snapshot is taken before updating the
736+
ledger state, in Dijkstra the `EPOCH`{.AgdaDatatype} rule invokes the
737+
`SNAP`{.AgdaDatatype} rule _after_ updating the ledger state. This
738+
change affects the stake distribution used for leader election and
739+
distributing rewards, and for voting on governance actions.
740+
750741
```agda
751742
data _⊢_⇀⦇_,EPOCH⦈_ : ⊤ → EpochState → Epoch → EpochState → Type where
752743
```
@@ -767,19 +758,25 @@ data _⊢_⇀⦇_,EPOCH⦈_ : ⊤ → EpochState → Epoch → EpochState → Ty
767758
govSt' : GovState
768759
govSt' = Governance-Update.govSt' govUpd
769760
770-
stakeDistrs : StakeDistrs
771-
stakeDistrs = mkStakeDistrs (Snapshots.mark ss') e utxoSt'
772-
govSt' (GStateOf ls) (DStateOf ls)
761+
ls' : LedgerState
762+
ls' = ⟦ utxoSt' , govSt' , ⟦ dState'' , pState'' , gState' ⟧ᶜˢ ⟧
773763
774764
Γ : RatifyEnv
775-
Γ = ⟦ stakeDistrs , e , DRepsOf ls , CCHotKeysOf ls , TreasuryOf acnt'' , PoolsOf ls , VoteDelegsOf ls ⟧
765+
Γ = ⟦ calculateVDelegDelegatedStake e utxoSt' govSt' gState' dState''
766+
, calculatePoolDelegatedStakeForVoting (Snapshots.mark ss') govSt'
767+
, e
768+
, DRepsOf ls'
769+
, CCHotKeysOf ls'
770+
, TreasuryOf acnt''
771+
, PoolsOf ls'
772+
, VoteDelegsOf ls' ⟧
776773
777774
in
778-
ls ⊢ ss ⇀⦇ tt ,SNAP⦈ ss'
779775
∙ _ ⊢ ⟦ acnt , DStateOf ls , pState' ⟧ ⇀⦇ e ,POOLREAP⦈ ⟦ acnt' , dState' , pState'' ⟧
776+
∙ ls' ⊢ ss ⇀⦇ tt ,SNAP⦈ ss'
780777
∙ Γ ⊢ ⟦ es' , ∅ , false ⟧ ⇀⦇ govSt' ,RATIFIES⦈ fut'
781778
──────────────────────────────────────────────
782-
_ ⊢ ⟦ acnt , ss , ls , es₀ , fut ⟧ ⇀⦇ e ,EPOCH⦈ ⟦ acnt'' , ss' , ⟦ utxoSt' , govSt' , ⟦ dState'' , pState'' , gState' ⟧ᶜˢ ⟧ , es' , fut' ⟧
779+
_ ⊢ ⟦ acnt , ss , ls , es₀ , fut ⟧ ⇀⦇ e ,EPOCH⦈ ⟦ acnt'' , ss' , ls' , es' , fut' ⟧
783780
```
784781

785782
## The <span class="AgdaDatatype">NEWEPOCH</span> Transition System {#sec:the-newepoch-transition-system}

src/Ledger/Dijkstra/Specification/Epoch/Properties/Computational.lagda.md

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ module _ {eps : EpochState} {e : Epoch} where
3636
EPOCH-total : ∃[ eps' ] _ ⊢ eps ⇀⦇ e ,EPOCH⦈ eps'
3737
EPOCH-total =
3838
-, EPOCH
39-
( SNAP-total .proj₂
40-
, POOLREAP-total .proj₂
39+
( POOLREAP-total .proj₂
40+
, SNAP-total .proj₂
4141
, RATIFIES-total' .proj₂)
4242
4343
EPOCH-deterministic : ∀ eps' eps''
@@ -50,15 +50,15 @@ module _ {eps : EpochState} {e : Epoch} where
5050
(EPOCH
5151
{dState' = dState'₁}
5252
{acnt' = acnt'₁}
53-
{ss' = ss'₁}
5453
{pState'' = pState'₁}
54+
{ss' = ss'₁}
5555
(p₁ , p₂ , p₃)
5656
)
5757
(EPOCH
5858
{dState' = dState'₂}
5959
{acnt' = acnt'₂}
60-
{ss' = ss'₂}
6160
{pState'' = pState'₂}
61+
{ss' = ss'₂}
6262
(p₁' , p₂' , p₃')
6363
) = eps'≡eps''
6464
where
@@ -73,9 +73,6 @@ module _ {eps : EpochState} {e : Epoch} where
7373
7474
govSt' = Governance-Update.govSt' govUpd
7575
76-
ss'₁≡ss'₂ : ss'₁ ≡ ss'₂
77-
ss'₁≡ss'₂ = SNAP-deterministic p₁ p₁'
78-
7976
module pPRUpd = Pre-POOLREAP-Update (Pre-POOLREAPUpdate.updates ls es govUpd)
8077
8178
pPRUpd₁ = Post-POOLREAPUpdate.updates es ls dState'₁ acnt'₁ govUpd
@@ -86,17 +83,37 @@ module _ {eps : EpochState} {e : Epoch} where
8683
8784
prs'≡prs'' : ⟦ acnt'₁ , dState'₁ , pState'₁ ⟧ᵖ ≡
8885
⟦ acnt'₂ , dState'₂ , pState'₂ ⟧ᵖ
89-
prs'≡prs'' = POOLREAP-deterministic-≡ refl refl p₂ p₂'
86+
prs'≡prs'' = POOLREAP-deterministic-≡ refl refl p₁ p₁'
9087
9188
pPRUpd₁≡pPRUpd₂ : pPRUpd₁ ≡ pPRUpd₂
9289
pPRUpd₁≡pPRUpd₂ rewrite (cong PoolReapState.dState prs'≡prs'') | (cong PoolReapState.acnt prs'≡prs'') = refl
9390
94-
stakeDistrs₁≡stakeDistrs₂ : mkStakeDistrs (Snapshots.mark ss'₁) e pPRUpd.utxoSt' govSt' (GStateOf ls) (DStateOf ls)
95-
≡ mkStakeDistrs (Snapshots.mark ss'₂) e pPRUpd.utxoSt' govSt' (GStateOf ls) (DStateOf ls)
96-
stakeDistrs₁≡stakeDistrs₂ = cong (λ ss' → mkStakeDistrs (Snapshots.mark ss') e pPRUpd.utxoSt' govSt' (GStateOf ls) (DStateOf ls)) ss'₁≡ss'₂
91+
ls'₁ = ⟦ pPRUpd.utxoSt' , govSt' , ⟦ pPRUpd₁.dState'' , pState'₁ , pPRUpd.gState' ⟧ᶜˢ ⟧
92+
ls'₂ = ⟦ pPRUpd.utxoSt' , govSt' , ⟦ pPRUpd₂.dState'' , pState'₂ , pPRUpd.gState' ⟧ᶜˢ ⟧
9793
98-
Γ≡Γ' = cong₂ (λ sd acnt → ⟦ sd , e , DRepsOf ls , CCHotKeysOf ls , TreasuryOf acnt , PoolsOf ls , VoteDelegsOf ls ⟧)
99-
stakeDistrs₁≡stakeDistrs₂ (cong Post-POOLREAP-Update.acnt'' pPRUpd₁≡pPRUpd₂)
94+
ls'₁≡ls'₂ : ls'₁ ≡ ls'₂
95+
ls'₁≡ls'₂ = cong₂ (λ ds ps → ⟦ pPRUpd.utxoSt' , govSt' , ⟦ ds , ps , pPRUpd.gState' ⟧ᶜˢ ⟧)
96+
(cong Post-POOLREAP-Update.dState'' pPRUpd₁≡pPRUpd₂)
97+
(cong PoolReapState.pState prs'≡prs'')
98+
99+
ss'₁≡ss'₂ : ss'₁ ≡ ss'₂
100+
ss'₁≡ss'₂ = SNAP-deterministic-≡ ls'₁≡ls'₂ p₂ p₂'
101+
102+
mkΓ : Snapshots → Post-POOLREAP-Update → LedgerState → RatifyEnv
103+
mkΓ ss'' pPR ls'' =
104+
⟦ calculateVDelegDelegatedStake e pPRUpd.utxoSt' govSt' pPRUpd.gState' (Post-POOLREAP-Update.dState'' pPR)
105+
, calculatePoolDelegatedStakeForVoting (Snapshots.mark ss'') govSt'
106+
, e
107+
, DRepsOf ls''
108+
, CCHotKeysOf ls''
109+
, TreasuryOf (Post-POOLREAP-Update.acnt'' pPR)
110+
, PoolsOf ls''
111+
, VoteDelegsOf ls'' ⟧
112+
113+
Γ≡Γ' : mkΓ ss'₁ pPRUpd₁ ls'₁ ≡ mkΓ ss'₂ pPRUpd₂ ls'₂
114+
Γ≡Γ' = trans (cong (λ x → mkΓ x pPRUpd₁ ls'₁) ss'₁≡ss'₂)
115+
(trans (cong (λ x → mkΓ ss'₂ x ls'₁) pPRUpd₁≡pPRUpd₂)
116+
(cong (mkΓ ss'₂ pPRUpd₂) ls'₁≡ls'₂))
100117
101118
fut'≡fut'' : RatifyStateOf eps' ≡ RatifyStateOf eps''
102119
fut'≡fut'' = RATIFIES-deterministic-≡ Γ≡Γ' refl refl p₃ p₃'

src/Ledger/Dijkstra/Specification/Ratify.lagda.md

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,20 +90,16 @@ threshold pp ccThreshold ga =
9090
canVote : PParams → GovAction → GovRole → Type
9191
canVote pp a r = Is-just (threshold pp nothing a r)
9292
93-
record StakeDistrs : Type where
94-
field
95-
stakeDistrVDeleg : VDeleg ⇀ Coin
96-
stakeDistrPools : KeyHash ⇀ Coin
97-
9893
record RatifyEnv : Type where
9994
field
100-
stakeDistrs : StakeDistrs
101-
currentEpoch : Epoch
102-
dreps : Credential ⇀ Epoch
103-
ccHotKeys : Credential ⇀ Maybe Credential
104-
treasury : Treasury
105-
pools : KeyHash ⇀ StakePoolParams
106-
delegatees : VoteDelegs
95+
stakeDistrVDeleg : VDeleg ⇀ Coin
96+
stakeDistrPools : KeyHash ⇀ Coin
97+
currentEpoch : Epoch
98+
dreps : Credential ⇀ Epoch
99+
ccHotKeys : Credential ⇀ Maybe Credential
100+
treasury : Treasury
101+
pools : KeyHash ⇀ StakePoolParams
102+
delegatees : VoteDelegs
107103
108104
record RatifyState : Type where
109105
field
@@ -128,9 +124,8 @@ instance
128124
HasTreasury-RatifyEnv : HasTreasury RatifyEnv
129125
HasTreasury-RatifyEnv .TreasuryOf = RatifyEnv.treasury
130126
131-
unquoteDecl HasCast-StakeDistrs HasCast-RatifyEnv HasCast-RatifyState = derive-HasCast
132-
( (quote StakeDistrs , HasCast-StakeDistrs)
133-
∷ (quote RatifyEnv , HasCast-RatifyEnv)
127+
unquoteDecl HasCast-RatifyEnv HasCast-RatifyState = derive-HasCast
128+
( (quote RatifyEnv , HasCast-RatifyEnv)
134129
∷ [ (quote RatifyState , HasCast-RatifyState) ])
135130
```
136131
-->
@@ -201,8 +196,7 @@ module AcceptedByDRep (Γ : RatifyEnv)
201196
where
202197
203198
open EnactState eSt using (cc)
204-
open RatifyEnv Γ using (currentEpoch; stakeDistrs)
205-
open StakeDistrs stakeDistrs
199+
open RatifyEnv Γ using (currentEpoch; stakeDistrVDeleg)
206200
open GovActionState gaSt
207201
open GovVotes votes using (gvDRep)
208202
@@ -277,7 +271,7 @@ module AcceptedBySPO (delegatees : VoteDelegs)
277271
278272
acceptedBySPO : RatifyEnv → EnactState → GovActionState → Type
279273
acceptedBySPO Γ = AcceptedBySPO.accepted delegatees pools stakeDistrPools
280-
where open RatifyEnv Γ; open StakeDistrs stakeDistrs
274+
where open RatifyEnv Γ
281275
282276
283277
-- Ratification Functions --

src/Ledger/Dijkstra/Specification/Rewards/Properties/Computational.lagda.md

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,25 @@ open import Ledger.Dijkstra.Specification.Ledger txs abs
2424
open import Ledger.Dijkstra.Specification.Rewards txs abs
2525
2626
open Computational ⦃...⦄
27-
28-
module _ {lstate : LedgerState} {ss : Snapshots} where
2927
```
3028
-->
3129

3230
```agda
33-
SNAP-total : ∃[ ss' ] lstate ⊢ ss ⇀⦇ tt ,SNAP⦈ ss'
34-
SNAP-total = -, SNAP
31+
module _ {ss : Snapshots} where
32+
33+
module _ {lstate : LedgerState} where
34+
35+
SNAP-total : ∃[ ss' ] lstate ⊢ ss ⇀⦇ tt ,SNAP⦈ ss'
36+
SNAP-total = -, SNAP
3537
36-
SNAP-complete : ∀ ss' → lstate ⊢ ss ⇀⦇ tt ,SNAP⦈ ss' → proj₁ SNAP-total ≡ ss'
37-
SNAP-complete ss' SNAP = refl
38+
SNAP-complete : ∀ ss' → lstate ⊢ ss ⇀⦇ tt ,SNAP⦈ ss' → proj₁ SNAP-total ≡ ss'
39+
SNAP-complete ss' SNAP = refl
3840
39-
SNAP-deterministic : ∀ {ss' ss''}
40-
→ lstate ⊢ ss ⇀⦇ tt ,SNAP⦈ ss'
41-
→ lstate ⊢ ss ⇀⦇ tt ,SNAP⦈ ss'' → ss' ≡ ss''
42-
SNAP-deterministic SNAP SNAP = refl
41+
SNAP-deterministic-≡ : ∀ {ls ls' ss' ss''}
42+
→ ls ≡ ls'
43+
→ ls ⊢ ss ⇀⦇ tt ,SNAP⦈ ss'
44+
→ ls' ⊢ ss ⇀⦇ tt ,SNAP⦈ ss'' → ss' ≡ ss''
45+
SNAP-deterministic-≡ refl SNAP SNAP = refl
4346
4447
instance
4548
Computational-SNAP : Computational _⊢_⇀⦇_,SNAP⦈_ ⊥

0 commit comments

Comments
 (0)