Skip to content

Commit 209b030

Browse files
committed
Add deposits in CERT
1 parent b979081 commit 209b030

File tree

6 files changed

+133
-91
lines changed

6 files changed

+133
-91
lines changed

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

Lines changed: 46 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -31,16 +31,6 @@ record StakePoolParams : Type where
3131
pledge : Coin
3232
rewardAccount : Credential
3333
34-
-- Deposit Types
35-
data DepositPurpose : Type where
36-
CredentialDeposit : Credential → DepositPurpose
37-
PoolDeposit : KeyHash → DepositPurpose
38-
DRepDeposit : Credential → DepositPurpose
39-
GovActionDeposit : GovActionID → DepositPurpose
40-
41-
Deposits : Type
42-
Deposits = DepositPurpose ⇀ Coin
43-
4434
-- Miscellaneous Type Aliases
4535
4636
CCHotKeys : Type
@@ -73,12 +63,6 @@ data DCert : Type where
7363
regdrep : Credential → Coin → Anchor → DCert
7464
deregdrep : Credential → Coin → DCert
7565
ccreghot : Credential → Maybe Credential → DCert
76-
-- The `reg` cert is deprecated in Conway, but it's still present in this era
77-
-- for backwards compatibility. This has been added to the spec to make
78-
-- conformance testing work properly. We don't talk about this certificate
79-
-- in the docs because it has been deprecated and we want to discourage people
80-
-- from using it.
81-
reg : Credential → Coin → DCert
8266
8367
cwitness : DCert → Maybe Credential
8468
cwitness (delegate c _ _ _) = just c
@@ -89,14 +73,6 @@ cwitness (regdrep c _ _) = just c
8973
cwitness (deregdrep c _) = just c
9074
cwitness (ccreghot c _) = just c
9175
92-
-- The implementation requires the `reg` cert to be witnessed only if the
93-
-- deposit is set. There didn't use to be a field for the deposit, but that was
94-
-- added in the Conway era to make it easier to determine, just by looking at
95-
-- the transaction, how much deposit was paid for that certificate.
96-
cwitness (reg _ zero) = nothing
97-
cwitness (reg c (suc _)) = just c
98-
99-
10076
-- Certification Types
10177
record CertEnv : Type where
10278
field
@@ -112,18 +88,21 @@ record DState : Type where
11288
voteDelegs : VoteDelegs
11389
stakeDelegs : StakeDelegs
11490
rewards : Rewards
91+
deposits : Credential ⇀ Coin
11592
11693
record PState : Type where
11794
field
11895
pools : Pools
11996
fPools : Pools
12097
retiring : KeyHash ⇀ Epoch
98+
deposits : KeyHash ⇀ Coin
12199
122100
record GState : Type where
123101
constructor ⟦_,_⟧ᵛ
124102
field
125103
dreps : DReps
126104
ccHotKeys : Credential ⇀ Maybe Credential
105+
deposits : Credential ⇀ Coin
127106
128107
record CertState : Type where
129108
constructor ⟦_,_,_⟧ᶜˢ
@@ -139,19 +118,12 @@ record DelegEnv : Type where
139118
delegatees : ℙ Credential
140119
```
141120

142-
143-
144-
145121
<!--
146122
```agda
147-
record HasDeposits {a} (A : Type a) : Type a where
148-
field DepositsOf : A → Deposits
123+
record HasDeposits (A : Type) {K : Type} : Type where
124+
field DepositsOf : A → K ⇀ Coin
149125
open HasDeposits ⦃...⦄ public
150126
151-
instance
152-
unquoteDecl DecEq-DepositPurpose = derive-DecEq
153-
((quote DepositPurpose , DecEq-DepositPurpose) ∷ [])
154-
155127
record HasCCHotKeys {a} (A : Type a) : Type a where
156128
field CCHotKeysOf : A → CCHotKeys
157129
open HasCCHotKeys ⦃...⦄ public
@@ -208,9 +180,15 @@ instance
208180
HasRewards-DState : HasRewards DState
209181
HasRewards-DState .RewardsOf = DState.rewards
210182
183+
HasDeposits-DState : HasDeposits DState
184+
HasDeposits-DState .DepositsOf = DState.deposits
185+
211186
HasPools-PState : HasPools PState
212187
HasPools-PState .PoolsOf = PState.pools
213188
189+
HasDeposits-PState : HasDeposits PState
190+
HasDeposits-PState .DepositsOf = PState.deposits
191+
214192
HasRetiring-PState : HasRetiring PState
215193
HasRetiring-PState .RetiringOf = PState.retiring
216194
@@ -220,6 +198,9 @@ instance
220198
HasCCHotKeys-GState : HasCCHotKeys GState
221199
HasCCHotKeys-GState .CCHotKeysOf = GState.ccHotKeys
222200
201+
HasDeposits-GState : HasDeposits GState
202+
HasDeposits-GState .DepositsOf = GState.deposits
203+
223204
HasDState-CertState : HasDState CertState
224205
HasDState-CertState .DStateOf = CertState.dState
225206
@@ -265,6 +246,8 @@ private variable
265246
pools fPools : Pools
266247
retiring : Retiring
267248
wdrls : Withdrawals
249+
A : Type
250+
deposits deposits' : A ⇀ Coin
268251
269252
an : Anchor
270253
Γ : CertEnv
@@ -314,19 +297,12 @@ data _⊢_⇀⦇_,DELEG⦈_ : DelegEnv → DState → DCert → DState → Type
314297
fromList ( nothing ∷ just vDelegAbstain ∷ just vDelegNoConfidence ∷ [] )
315298
∙ mkh ∈ mapˢ just (dom pools) ∪ ❴ nothing ❵
316299
────────────────────────────────
317-
⟦ pp , pools , delegatees ⟧ ⊢ ⟦ vDelegs , sDelegs , rwds ⟧ ⇀⦇ delegate c mvd mkh d ,DELEG⦈ ⟦ insertIfJust c mvd vDelegs , insertIfJust c mkh sDelegs , rwds ∪ˡ ❴ c , 0 ❵ ⟧
300+
⟦ pp , pools , delegatees ⟧ ⊢ ⟦ vDelegs , sDelegs , rwds , deposits ⟧ ⇀⦇ delegate c mvd mkh d ,DELEG⦈ ⟦ insertIfJust c mvd vDelegs , insertIfJust c mkh sDelegs , rwds ∪ˡ ❴ c , 0 ❵ , deposits ∪⁺ ❴ c , d ❵ ⟧
318301
319302
DELEG-dereg :
320303
∙ (c , 0) ∈ rwds
321304
────────────────────────────────
322-
⟦ pp , pools , delegatees ⟧ ⊢ ⟦ vDelegs , sDelegs , rwds ⟧ ⇀⦇ dereg c md ,DELEG⦈ ⟦ vDelegs ∣ ❴ c ❵ ᶜ , sDelegs ∣ ❴ c ❵ ᶜ , rwds ∣ ❴ c ❵ ᶜ ⟧
323-
324-
DELEG-reg :
325-
∙ c ∉ dom rwds
326-
∙ d ≡ pp .keyDeposit ⊎ d ≡ 0
327-
────────────────────────────────
328-
⟦ pp , pools , delegatees ⟧ ⊢ ⟦ vDelegs , sDelegs , rwds ⟧ ⇀⦇ reg c d ,DELEG⦈ ⟦ vDelegs , sDelegs , rwds ∪ˡ ❴ c , 0 ❵ ⟧
329-
305+
⟦ pp , pools , delegatees ⟧ ⊢ ⟦ vDelegs , sDelegs , rwds , deposits ⟧ ⇀⦇ dereg c md ,DELEG⦈ ⟦ vDelegs ∣ ❴ c ❵ ᶜ , sDelegs ∣ ❴ c ❵ ᶜ , rwds ∣ ❴ c ❵ ᶜ , deposits ∣ ❴ c ❵ ᶜ ⟧
330306
331307
332308
isPoolRegistered : Pools -> KeyHash -> Maybe StakePoolParams
@@ -335,32 +311,45 @@ isPoolRegistered ps kh = lookupᵐ? ps kh
335311
-- Auxiliary POOL transition system --
336312
data _⊢_⇀⦇_,POOL⦈_ : PoolEnv → PState → DCert → PState → Type where
337313
338-
POOL-regpool :
339-
let
340-
fPool' =
341-
if isPoolRegistered pools kh
342-
then ❴ kh , poolParams ❵ ∪ˡ fPools
343-
else fPools
344-
in
314+
POOL-reg :
315+
∙ Is-just (isPoolRegistered pools kh)
345316
────────────────────────────────
346317
pp ⊢ ⟦ pools
347318
, fPools
348319
, retiring
320+
, deposits
349321
⟧ ⇀⦇ regpool kh poolParams ,POOL⦈ ⟦
350322
pools ∪ˡ ❴ kh , poolParams ❵
351-
, fPool'
352-
, retiring ∣ ❴ kh ❵ ᶜ
323+
, fPools
324+
, retiring
325+
, deposits ∪ˡ ❴ kh , pp .poolDeposit ❵
326+
327+
328+
POOL-rereg :
329+
∙ Is-nothing (isPoolRegistered pools kh)
330+
────────────────────────────────
331+
pp ⊢ ⟦ pools
332+
, fPools
333+
, retiring
334+
, deposits
335+
⟧ ⇀⦇ regpool kh poolParams ,POOL⦈ ⟦
336+
pools
337+
, ❴ kh , poolParams ❵ ∪ˡ fPools
338+
, retiring ∣ ❴ kh ❵ ᶜ
339+
, deposits
353340
354341
355342
POOL-retirepool :
356343
────────────────────────────────
357344
pp ⊢ ⟦ pools
358345
, fPools
359346
, retiring
347+
, deposits
360348
⟧ ⇀⦇ retirepool kh e ,POOL⦈ ⟦
361349
pools
362350
, fPools
363351
, ❴ kh , e ❵ ∪ˡ retiring
352+
, deposits
364353
365354
366355
@@ -370,18 +359,18 @@ data _⊢_⇀⦇_,GOVCERT⦈_ : CertEnv → GState → DCert → GState → Type
370359
GOVCERT-regdrep :
371360
∙ (d ≡ pp .drepDeposit × c ∉ dom dReps) ⊎ (d ≡ 0 × c ∈ dom dReps)
372361
────────────────────────────────
373-
⟦ e , pp , vs , wdrls , cc ⟧ ⊢ ⟦ dReps , ccKeys ⟧ ⇀⦇ regdrep c d an ,GOVCERT⦈ ⟦ ❴ c , e + pp .drepActivity ❵ ∪ˡ dReps , ccKeys ⟧
362+
⟦ e , pp , vs , wdrls , cc ⟧ ⊢ ⟦ dReps , ccKeys , deposits ⟧ ⇀⦇ regdrep c d an ,GOVCERT⦈ ⟦ ❴ c , e + pp .drepActivity ❵ ∪ˡ dReps , ccKeys , deposits ∪⁺ ❴ c , d ❵
374363
375364
GOVCERT-deregdrep :
376365
∙ c ∈ dom dReps
377366
────────────────────────────────
378-
⟦ e , pp , vs , wdrls , cc ⟧ ⊢ ⟦ dReps , ccKeys ⟧ ⇀⦇ deregdrep c d ,GOVCERT⦈ ⟦ dReps ∣ ❴ c ❵ ᶜ , ccKeys ⟧
367+
⟦ e , pp , vs , wdrls , cc ⟧ ⊢ ⟦ dReps , ccKeys , deposits ⟧ ⇀⦇ deregdrep c d ,GOVCERT⦈ ⟦ dReps ∣ ❴ c ❵ ᶜ , ccKeys , deposits ∣ ❴ c ❵ ᶜ
379368
380369
GOVCERT-ccreghot :
381370
∙ (c , nothing) ∉ ccKeys
382371
∙ c ∈ cc
383372
────────────────────────────────
384-
⟦ e , pp , vs , wdrls , cc ⟧ ⊢ ⟦ dReps , ccKeys ⟧ ⇀⦇ ccreghot c mc ,GOVCERT⦈ ⟦ dReps , ❴ c , mc ❵ ∪ˡ ccKeys ⟧
373+
⟦ e , pp , vs , wdrls , cc ⟧ ⊢ ⟦ dReps , ccKeys , deposits ⟧ ⇀⦇ ccreghot c mc ,GOVCERT⦈ ⟦ dReps , ❴ c , mc ❵ ∪ˡ ccKeys , deposits
385374
386375
-- CERT Transition System --
387376
data _⊢_⇀⦇_,CERT⦈_ : CertEnv → CertState → DCert → CertState → Type where
@@ -396,7 +385,7 @@ data _⊢_⇀⦇_,CERT⦈_ : CertEnv → CertState → DCert → CertState →
396385
────────────────────────────────
397386
⟦ e , pp , vs , wdrls , cc ⟧ ⊢ ⟦ stᵈ , stᵖ , stᵍ ⟧ ⇀⦇ dCert ,CERT⦈ ⟦ stᵈ , stᵖ' , stᵍ ⟧
398387
399-
CERT-vdel :
388+
CERT-gov :
400389
∙ Γ ⊢ stᵍ ⇀⦇ dCert ,GOVCERT⦈ stᵍ'
401390
────────────────────────────────
402391
Γ ⊢ ⟦ stᵈ , stᵖ , stᵍ ⟧ ⇀⦇ dCert ,CERT⦈ ⟦ stᵈ , stᵖ , stᵍ' ⟧
@@ -416,7 +405,7 @@ data _⊢_⇀⦇_,PRE-CERT⦈_ : CertEnv → CertState → ⊤ → CertState →
416405
∙ filter isKeyHash wdrlCreds ⊆ dom voteDelegs
417406
∙ mapˢ (map₁ stake) (wdrls ˢ) ⊆ rewards ˢ
418407
────────────────────────────────
419-
⟦ e , pp , vs , wdrls , cc ⟧ ⊢ ⟦ ⟦ voteDelegs , stakeDelegs , rewards ⟧ , stᵖ , ⟦ dReps , ccHotKeys ⟧ ⟧ ⇀⦇ _ ,PRE-CERT⦈ ⟦ ⟦ voteDelegs , stakeDelegs , constMap wdrlCreds 0 ∪ˡ rewards ⟧ , stᵖ , ⟦ refreshedDReps , ccHotKeys ⟧ ⟧
408+
⟦ e , pp , vs , wdrls , cc ⟧ ⊢ ⟦ ⟦ voteDelegs , stakeDelegs , rewards , deposits ⟧ , stᵖ , ⟦ dReps , ccHotKeys , deposits' ⟧ ⟧ ⇀⦇ _ ,PRE-CERT⦈ ⟦ ⟦ voteDelegs , stakeDelegs , constMap wdrlCreds 0 ∪ˡ rewards , deposits ⟧ , stᵖ , ⟦ refreshedDReps , ccHotKeys , deposits' ⟧ ⟧
420409
421410
422411
-- POST-CERT Transition Rule --
@@ -427,7 +416,7 @@ data _⊢_⇀⦇_,POST-CERT⦈_ : CertEnv → CertState → ⊤ → CertState
427416
let activeVDelegs = mapˢ vDelegCredential (dom (DRepsOf stᵍ))
428417
∪ fromList (vDelegNoConfidence ∷ vDelegAbstain ∷ [])
429418
in
430-
⟦ e , pp , vs , wdrls , cc ⟧ ⊢ ⟦ ⟦ voteDelegs , stakeDelegs , rewards ⟧ , stᵖ , stᵍ ⟧ ⇀⦇ _ ,POST-CERT⦈ ⟦ ⟦ voteDelegs ∣^ activeVDelegs , stakeDelegs , rewards ⟧ , stᵖ , stᵍ ⟧
419+
⟦ e , pp , vs , wdrls , cc ⟧ ⊢ ⟦ ⟦ voteDelegs , stakeDelegs , rewards , deposits ⟧ , stᵖ , stᵍ ⟧ ⇀⦇ _ ,POST-CERT⦈ ⟦ ⟦ voteDelegs ∣^ activeVDelegs , stakeDelegs , rewards , deposits ⟧ , stᵖ , stᵍ ⟧
431420
432421
433422
-- CERTS Transition System --

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

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,20 @@ rmOrphanDRepVotes cs govSt = L.map (map₂ go) govSt
165165
allColdCreds : GovState → EnactState → ℙ Credential
166166
allColdCreds govSt es =
167167
ccCreds (es .cc) ∪ concatMapˢ (λ (_ , st) → proposedCC (GovActionOf st)) (fromList govSt)
168+
169+
calculateDepositsChange : CertState → CertState → ℤ
170+
calculateDepositsChange certState certState' = initialCoin - finalCoin
171+
where
172+
initialCoin : ℕ
173+
initialCoin = getCoin (DepositsOf (DStateOf certState))
174+
+ getCoin (DepositsOf (PStateOf certState))
175+
+ getCoin (DepositsOf (GStateOf certState))
176+
177+
finalCoin : ℕ
178+
finalCoin = getCoin (DepositsOf (DStateOf certState'))
179+
+ getCoin (DepositsOf (PStateOf certState'))
180+
+ getCoin (DepositsOf (GStateOf certState'))
181+
168182
```
169183

170184
-- ## <span class="AgdaDatatype">LEDGER</span> Transition System
@@ -178,6 +192,7 @@ private variable
178192
utxo₀ : UTxO
179193
govState govState' : GovState
180194
certState certState' : CertState
195+
depositsChange : ℤ
181196
stx : SubLevelTx
182197
slot : Slot
183198
ppolicy : Maybe ScriptHash
@@ -246,12 +261,15 @@ data _⊢_⇀⦇_,LEDGER⦈_ : LedgerEnv → LState → TopLevelTx → LState
246261
247262
allData : DataHash ⇀ Datum
248263
allData = setToMap (mapˢ < hash , id > (getAllData tx utxo₀))
264+
265+
depositsChange : ℤ
266+
depositsChange = calculateDepositsChange certState certState''
249267
in
250-
∙ Tx.isValid tx ≡ true
251-
∙ ⟦ slot , ppolicy , pp , enactState , treasury , utxo₀ , Tx.isValid tx , allScripts , allData ⟧ ⊢ ⟦ utxoState , govState , certState ⟧ ⇀⦇ SubTransactionsOf tx ,SUBLEDGERS⦈ ⟦ utxoState' , govState' , certState' ⟧
252-
∙ ⟦ slot , pp , treasury , utxo₀ , Tx.isValid tx , allScripts , allData ⟧ ⊢ utxoState' ⇀⦇ tx ,UTXOW⦈ utxoState''
268+
∙ IsValidFlagOf tx ≡ true
269+
∙ ⟦ slot , ppolicy , pp , enactState , treasury , utxo₀ , IsValidFlagOf tx , allScripts , allData ⟧ ⊢ ⟦ utxoState , govState , certState ⟧ ⇀⦇ SubTransactionsOf tx ,SUBLEDGERS⦈ ⟦ utxoState' , govState' , certState' ⟧
253270
∙ ⟦ epoch slot , pp , ListOfGovVotesOf tx , WithdrawalsOf tx , allColdCreds govState enactState ⟧ ⊢ certState' ⇀⦇ DCertsOf tx ,CERTS⦈ certState''
254271
∙ ⟦ TxIdOf tx , epoch slot , pp , ppolicy , enactState , certState' , dom (RewardsOf certState) ⟧ ⊢ rmOrphanDRepVotes certState' govState ⇀⦇ GovProposals+Votes tx ,GOVS⦈ govState'
272+
∙ ⟦ slot , pp , treasury , utxo₀ , IsValidFlagOf tx , depositsChange , allScripts , allData ⟧ ⊢ utxoState' ⇀⦇ tx ,UTXOW⦈ utxoState''
255273
────────────────────────────────
256274
⟦ slot , ppolicy , pp , enactState , treasury ⟧ ⊢ ⟦ utxoState , govState , certState ⟧ ⇀⦇ tx ,LEDGER⦈ ⟦ utxoState'' , govState'' , certState'' ⟧
257275
@@ -264,23 +282,14 @@ data _⊢_⇀⦇_,LEDGER⦈_ : LedgerEnv → LState → TopLevelTx → LState
264282
allData : DataHash ⇀ Datum
265283
allData = setToMap (mapˢ < hash , id > (getAllData tx utxo₀))
266284
in
267-
Tx.isValid tx ≡ false
268-
∙ ⟦ slot , ppolicy , pp , enactState , treasury , utxo₀ , Tx.isValid tx , allScripts , allData ⟧ ⊢ ⟦ utxoState , govState , certState ⟧ ⇀⦇ SubTransactionsOf tx ,SUBLEDGERS⦈ ⟦ utxoState , govState , certState ⟧
269-
∙ ⟦ slot , pp , treasury , utxo₀ , Tx.isValid tx , allScripts , allData ⟧ ⊢ utxoState ⇀⦇ tx ,UTXOW⦈ utxoState'
285+
IsValidFlagOf tx ≡ false
286+
∙ ⟦ slot , ppolicy , pp , enactState , treasury , utxo₀ , IsValidFlagOf tx , allScripts , allData ⟧ ⊢ ⟦ utxoState , govState , certState ⟧ ⇀⦇ SubTransactionsOf tx ,SUBLEDGERS⦈ ⟦ utxoState , govState , certState ⟧
287+
∙ ⟦ slot , pp , treasury , utxo₀ , IsValidFlagOf tx , 0ℤ , allScripts , allData ⟧ ⊢ utxoState ⇀⦇ tx ,UTXOW⦈ utxoState'
270288
────────────────────────────────
271289
⟦ slot , ppolicy , pp , enactState , treasury ⟧ ⊢ ⟦ utxoState , govState , certState ⟧ ⇀⦇ tx ,LEDGER⦈ ⟦ utxoState' , govState , certState ⟧
272290
```
273291

274-
-- <!--
275-
-- ```agda
276-
-- pattern LEDGER-V⋯ w x y z = LEDGER-V (w , x , y , z)
277-
-- pattern LEDGER-I⋯ y z = LEDGER-I (y , z)
278-
-- ```
279-
-- -->
280-
281-
-- ## <span class="AgdaDatatype">LEDGERS</span> Transition System
282-
283-
-- ```agda
284-
-- __⇀⦇_,LEDGERS⦈_ : LEnv → LState → List Tx → LState → Type
285-
-- __⇀⦇_,LEDGERS⦈_ = ReflexiveTransitiveClosure {sts = __⇀⦇_,LEDGER⦈_}
286-
-- ```
292+
```agda
293+
_⊢_⇀⦇_,LEDGERS⦈_ : LedgerEnv → LState → List TopLevelTx → LState → Type
294+
_⊢_⇀⦇_,LEDGERS⦈_ = ReflexiveTransitiveClosure {sts = _⊢_⇀⦇_,LEDGER⦈_}
295+
```

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,13 +372,20 @@ could be either of them.
372372
field TxOutsOf : A → Ix ⇀ TxOut
373373
open HasTxOuts ⦃...⦄ public
374374
375+
record HasIsValidFlag {a} (A : Type a) : Type a where
376+
field IsValidFlagOf : A → Bool
377+
open HasIsValidFlag ⦃...⦄ public
378+
375379
instance
376380
HasTxBody-Tx : HasTxBody (Tx txLevel)
377381
HasTxBody-Tx .TxBodyOf = Tx.txBody
378382
379383
HasTxWitnesses-Tx : HasTxWitnesses (Tx txLevel)
380384
HasTxWitnesses-Tx .TxWitnessesOf = Tx.txWitnesses
381385
386+
HasIsValidFlag-Tx : HasIsValidFlag TopLevelTx
387+
HasIsValidFlag-Tx .IsValidFlagOf = Tx.isValid
388+
382389
HasRedeemers-TxWitnesses : HasRedeemers (TxWitnesses txLevel)
383390
HasRedeemers-TxWitnesses .RedeemersOf = TxWitnesses.txRedeemers
384391

0 commit comments

Comments
 (0)