-
Notifications
You must be signed in to change notification settings - Fork 12
/
Utxo.lagda
421 lines (363 loc) · 13.9 KB
/
Utxo.lagda
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
\section{UTxO}
\label{sec:utxo}
\subsection{Accounting}
\begin{code}[hide]
{-# OPTIONS --safe #-}
open import Algebra using (CommutativeMonoid)
open import Data.Integer.Ext using (posPart; negPart)
open import Data.Nat.Properties using (+-0-monoid)
import Data.Maybe as M
import Data.Sum.Relation.Unary.All as Sum
import Data.Integer as ℤ
import Data.Rational as ℚ
open import Tactic.Derive.DecEq
open import Ledger.Prelude
open import Ledger.Abstract
open import Ledger.Transaction
module Ledger.Utxo
(txs : _) (open TransactionStructure txs)
(abs : AbstractFunctions txs) (open AbstractFunctions abs)
where
open import Ledger.ScriptValidation txs abs
instance
_ = +-0-monoid
HasCoin-Map : ∀ {A} → ⦃ DecEq A ⦄ → HasCoin (A ⇀ Coin)
HasCoin-Map .getCoin s = ∑[ x ← s ] x
infixl 7 _*↓_
-- multiply a natural number with a fraction, rounding down and taking the absolute value
_*↓_ : ℚ.ℚ → ℕ → ℕ
q *↓ n = ℤ.∣ ℚ.⌊ q ℚ.* (ℤ.+ n ℚ./ 1) ⌋ ∣
\end{code}
\begin{figure*}[h]
\begin{code}
isTwoPhaseScriptAddress : Tx → UTxO → Addr → Bool
isTwoPhaseScriptAddress tx utxo a =
if isScriptAddr a then
(λ {p} → if lookupScriptHash (getScriptHash a p) tx utxo
then (λ {s} → isP2Script s)
else false)
else
false
\end{code}
\begin{code}[hide]
opaque
\end{code}
\begin{code}
getDataHashes : ℙ TxOut → ℙ DataHash
getDataHashes txo = mapPartial isInj₂ (mapPartial (proj₁ ∘ proj₂ ∘ proj₂) txo)
getInputHashes : Tx → UTxO → ℙ DataHash
getInputHashes tx utxo = getDataHashes
(filterˢ (λ (a , _ ) → isTwoPhaseScriptAddress tx utxo a ≡ true)
(range (utxo ∣ txins)))
where open Tx; open TxBody (tx .body)
totExUnits : Tx → ExUnits
totExUnits tx = ∑[ (_ , eu) ← tx .wits .txrdmrs ] eu
where open Tx; open TxWitnesses
\end{code}
\caption{Functions supporting UTxO rules}
\label{fig:supportfunctions:utxo}
\end{figure*}
\begin{code}[hide]
-- utxoEntrySizeWithoutVal = 27 words (8 bytes)
utxoEntrySizeWithoutVal : MemoryEstimate
utxoEntrySizeWithoutVal = 8
utxoEntrySize : TxOutʰ → MemoryEstimate
utxoEntrySize o = utxoEntrySizeWithoutVal + size (getValueʰ o)
open PParams
\end{code}
Figures~\ref{fig:supportfunctions:utxo},~\ref{fig:functions:utxo}, and~\ref{fig:functions:utxo2} define
functions needed for the UTxO transition system. Note the special
multiplication symbol \AgdaFunction{*↓} used in
Figure~\ref{fig:functions:utxo}: it means multiply and round down
the result.
\begin{NoConway}
Figure~\ref{fig:ts-types:utxo-shelley} defines the types needed for the UTxO transition system.
The UTxO transition system is given in Figure~\ref{fig:rules:utxo-shelley}.
\begin{itemize}
\item
The function $\fun{outs}$ creates the unspent outputs generated by a transaction.
It maps the transaction id and output index to the output.
\item
The $\fun{balance}$ function calculates sum total of all the coin in a given UTxO.
\end{itemize}
\end{NoConway}
\begin{figure*}[h]
\begin{code}[hide]
module _ (let open Tx; open TxBody; open TxWitnesses) where opaque
\end{code}
\begin{AgdaMultiCode}
\begin{NoConway}
\begin{code}
outs : TxBody → UTxO
outs tx = mapKeys (tx .txid ,_) (tx .txouts)
balance : UTxO → Value
balance utxo = ∑[ x ← mapValues txOutHash utxo ] getValueʰ x
cbalance : UTxO → Coin
cbalance utxo = coin (balance utxo)
\end{code}
\end{NoConway}
\begin{code}
minfee : PParams → UTxO → Tx → Coin
minfee pp utxo tx =
pp .a * tx .body .txsize + pp .b
+ txscriptfee (pp .prices) (totExUnits tx)
+ pp .minFeeRefScriptCoinsPerByte
*↓ ∑[ x ← mapValues scriptSize (setToHashMap (refScripts tx utxo)) ] x
\end{code}
\begin{code}[hide]
module _ where
\end{code}
\begin{code}
data DepositPurpose : Set where
CredentialDeposit : Credential → DepositPurpose
PoolDeposit : Credential → DepositPurpose
DRepDeposit : Credential → DepositPurpose
GovActionDeposit : GovActionID → DepositPurpose
\end{code}
\begin{code}[hide]
instance
unquoteDecl DecEq-DepositPurpose = derive-DecEq
((quote DepositPurpose , DecEq-DepositPurpose) ∷ [])
HasCoin-UTxO : HasCoin UTxO
HasCoin-UTxO .getCoin = cbalance
\end{code}
\begin{code}
updateCertDeposits : PParams → List DCert → DepositPurpose ⇀ Coin → DepositPurpose ⇀ Coin
updateCertDeposits _ [] deposits = deposits
updateCertDeposits pp (cert ∷ certs) deposits
= (updateCertDeposits pp certs deposits ∪⁺ certDeposit cert {pp}) ∣ certRefund cert ᶜ
where
certDeposit : DCert → {pp : PParams} → DepositPurpose ⇀ Coin
certDeposit (delegate c _ _ v) = ❴ CredentialDeposit c , v ❵
certDeposit (regpool c _) {pp} = ❴ PoolDeposit c , pp .poolDeposit ❵
certDeposit (regdrep c v _) = ❴ DRepDeposit c , v ❵
certDeposit _ = ∅
certRefund : DCert → ℙ DepositPurpose
certRefund (dereg c) = ❴ CredentialDeposit c ❵
certRefund (deregdrep c) = ❴ DRepDeposit c ❵
certRefund _ = ∅
updateProposalDeposits : List GovProposal → TxId → Coin → DepositPurpose ⇀ Coin
→ DepositPurpose ⇀ Coin
updateProposalDeposits [] _ _ deposits = deposits
updateProposalDeposits (_ ∷ ps) txid gaDep deposits =
updateProposalDeposits ps txid gaDep deposits
∪⁺ ❴ GovActionDeposit (txid , length ps) , gaDep ❵
updateDeposits : PParams → TxBody → DepositPurpose ⇀ Coin → DepositPurpose ⇀ Coin
updateDeposits pp txb = updateCertDeposits pp txcerts
∘ updateProposalDeposits txprop txid (pp .govActionDeposit)
\end{code}
\begin{code}[hide]
where open TxBody txb
\end{code}
\begin{code}
depositsChange : PParams → TxBody → DepositPurpose ⇀ Coin → ℤ
depositsChange pp txb deposits =
getCoin (updateDeposits pp txb deposits) - getCoin deposits
\end{code}
\end{AgdaMultiCode}
\caption{Functions used in UTxO rules}
\label{fig:functions:utxo}
\end{figure*}
\begin{NoConway}
\begin{figure*}
\begin{AgdaMultiCode}
\begin{code}
data inInterval (slot : Slot) : (Maybe Slot × Maybe Slot) → Set where
both : ∀ {l r} → l ≤ slot × slot ≤ r → inInterval slot (just l , just r)
lower : ∀ {l} → l ≤ slot → inInterval slot (just l , nothing)
upper : ∀ {r} → slot ≤ r → inInterval slot (nothing , just r)
none : inInterval slot (nothing , nothing)
\end{code}
\begin{code}[hide]
-- Note: inInterval has to be a type definition for inference to work
-- Boolean implication
_=>ᵇ_ : Bool → Bool → Bool
a =>ᵇ b = if a then b else true
-- Boolean-valued inequalities on natural numbers
_≤ᵇ_ _≥ᵇ_ : ℕ → ℕ → Bool
m ≤ᵇ n = ¿ m ≤ n ¿ᵇ
_≥ᵇ_ = flip _≤ᵇ_
≟-∅ᵇ : {A : Set} ⦃ _ : DecEq A ⦄ → (X : ℙ A) → Bool
≟-∅ᵇ X = ¿ X ≡ ∅ ¿ᵇ
coinPolicies : ℙ ScriptHash
coinPolicies = policies (inject 1)
isAdaOnlyᵇ : Value → Bool
isAdaOnlyᵇ v = toBool (policies v ≡ᵉ coinPolicies)
-- TODO: this could be a regular property
-- TODO: using this in UTxO rule below
\end{code}
\begin{code}
feesOK : PParams → Tx → UTxO → Bool
feesOK pp tx utxo = minfee pp utxo tx ≤ᵇ txfee
∧ not (≟-∅ᵇ (txrdmrs ˢ))
=>ᵇ ( allᵇ (λ (addr , _) → ¿ isVKeyAddr addr ¿) collateralRange
∧ isAdaOnlyᵇ bal
∧ (coin bal * 100) ≥ᵇ (txfee * pp .collateralPercentage)
∧ not (≟-∅ᵇ collateral)
)
where
open Tx tx; open TxBody body; open TxWitnesses wits; open PParams pp
collateralRange = range ((mapValues txOutHash utxo) ∣ collateral)
bal = balance (utxo ∣ collateral)
\end{code}
\end{AgdaMultiCode}
\caption{Functions used in UTxO rules, continued}
\label{fig:functions:utxo2}
\end{figure*}
\end{NoConway}
\AgdaTarget{UTxOEnv, UTxOState, \_⊢\_⇀⦇\_,UTXO⦈\_}
\begin{figure*}[h]
\emph{Derived types}
\begin{code}
Deposits = DepositPurpose ⇀ Coin
\end{code}
\begin{NoConway}
\emph{UTxO environment}
\begin{code}
record UTxOEnv : Set where
field slot : Slot
pparams : PParams
\end{code}
\end{NoConway}
\emph{UTxO states}
\begin{code}
record UTxOState : Set where
constructor ⟦_,_,_,_⟧ᵘ
field utxo : UTxO
fees : Coin
deposits : Deposits
donations : Coin
\end{code}
\begin{NoConway}
\emph{UTxO transitions}
\begin{code}[hide]
⟦_⟧ : {A : Set} → A → A
⟦_⟧ = id
instance
Dec-inInterval : inInterval ⁇²
Dec-inInterval {slot} {just x , just y } .dec with x ≤? slot | slot ≤? y
... | no ¬p₁ | _ = no λ where (both (h₁ , h₂)) → ¬p₁ h₁
... | yes p₁ | no ¬p₂ = no λ where (both (h₁ , h₂)) → ¬p₂ h₂
... | yes p₁ | yes p₂ = yes (both (p₁ , p₂))
Dec-inInterval {slot} {just x , nothing} .dec with x ≤? slot
... | no ¬p = no (λ where (lower h) → ¬p h)
... | yes p = yes (lower p)
Dec-inInterval {slot} {nothing , just x } .dec with slot ≤? x
... | no ¬p = no (λ where (upper h) → ¬p h)
... | yes p = yes (upper p)
Dec-inInterval {slot} {nothing , nothing} .dec = yes none
HasCoin-UTxOState : HasCoin UTxOState
HasCoin-UTxOState .getCoin s = getCoin (UTxOState.utxo s)
+ (UTxOState.fees s)
+ getCoin (UTxOState.deposits s)
+ UTxOState.donations s
data
\end{code}
\begin{code}
_⊢_⇀⦇_,UTXO⦈_ : UTxOEnv → UTxOState → Tx → UTxOState → Set
\end{code}
\end{NoConway}
\caption{UTxO transition-system types}
\label{fig:ts-types:utxo-shelley}
\end{figure*}
\begin{figure*}
\begin{code}[hide]
module _ (let open UTxOState; open TxBody) where
\end{code}
\begin{code}
depositRefunds : PParams → UTxOState → TxBody → Coin
depositRefunds pp st txb = negPart (depositsChange pp txb (st .deposits))
newDeposits : PParams → UTxOState → TxBody → Coin
newDeposits pp st txb = posPart (depositsChange pp txb (st .deposits))
consumed : PParams → UTxOState → TxBody → Value
consumed pp st txb
= balance (st .utxo ∣ txb .txins)
+ txb .mint
+ inject (depositRefunds pp st txb)
produced : PParams → UTxOState → TxBody → Value
produced pp st txb
= balance (outs txb)
+ inject (txb .txfee)
+ inject (newDeposits pp st txb)
+ inject (txb .txdonation)
\end{code}
\caption{Functions used in UTxO rules, continued}
\label{fig:functions:utxo-2}
\end{figure*}
\begin{code}[hide]
open PParams
data
_⊢_⇀⦇_,UTXOS⦈_ : UTxOEnv → UTxOState → Tx → UTxOState → Set
data _⊢_⇀⦇_,UTXOS⦈_ where
Scripts-Yes :
∀ {Γ} {s} {tx}
→ let open Tx tx renaming (body to txb); open TxBody txb
open UTxOEnv Γ renaming (pparams to pp)
open UTxOState s
sLst = collectPhaseTwoScriptInputs pp tx utxo
in
∙ evalScripts tx sLst ≡ isValid
∙ isValid ≡ true
────────────────────────────────
Γ ⊢ s ⇀⦇ tx ,UTXOS⦈ ⟦ (utxo ∣ txins ᶜ) ∪ˡ (outs txb)
, fees + txfee
, updateDeposits pp txb deposits
, donations + txdonation
⟧ᵘ
Scripts-No :
∀ {Γ} {s} {tx}
→ let open Tx tx renaming (body to txb); open TxBody txb
open UTxOEnv Γ renaming (pparams to pp)
open UTxOState s
sLst = collectPhaseTwoScriptInputs pp tx utxo
in
∙ evalScripts tx sLst ≡ isValid
∙ isValid ≡ false
────────────────────────────────
Γ ⊢ s ⇀⦇ tx ,UTXOS⦈ ⟦ utxo ∣ collateral ᶜ
, fees + cbalance (utxo ∣ collateral)
, deposits
, donations
⟧ᵘ
unquoteDecl Scripts-Yes-premises = genPremises Scripts-Yes-premises (quote Scripts-Yes)
unquoteDecl Scripts-No-premises = genPremises Scripts-No-premises (quote Scripts-No)
private variable
Γ : UTxOEnv
s s' : UTxOState
tx : Tx
data _⊢_⇀⦇_,UTXO⦈_ where
\end{code}
\begin{NoConway}
\begin{figure*}[h]
\begin{code}
UTXO-inductive :
let open Tx tx renaming (body to txb); open TxBody txb
open UTxOEnv Γ renaming (pparams to pp)
open UTxOState s
txoutsʰ = (mapValues txOutHash txouts)
in
∙ txins ≢ ∅ ∙ txins ⊆ dom utxo
∙ refInputs ⊆ dom utxo ∙ inInterval slot txvldt
∙ feesOK pp tx utxo ≡ true ∙ consumed pp s txb ≡ produced pp s txb
∙ coin mint ≡ 0 ∙ txsize ≤ maxTxSize pp
∙ ∀[ (_ , txout) ∈ txoutsʰ .proj₁ ]
inject (utxoEntrySize txout * minUTxOValue pp) ≤ᵗ getValueʰ txout
∙ ∀[ (_ , txout) ∈ txoutsʰ .proj₁ ]
serSize (getValueʰ txout) ≤ maxValSize pp
∙ ∀[ (a , _) ∈ range txoutsʰ ]
Sum.All (const ⊤) (λ a → a .BootstrapAddr.attrsSize ≤ 64) a
∙ ∀[ (a , _) ∈ range txoutsʰ ] netId a ≡ networkId
∙ ∀[ a ∈ dom txwdrls ] a .RwdAddr.net ≡ networkId
∙ Γ ⊢ s ⇀⦇ tx ,UTXOS⦈ s'
────────────────────────────────
Γ ⊢ s ⇀⦇ tx ,UTXO⦈ s'
\end{code}
\begin{code}[hide]
pattern UTXO-inductive⋯ tx Γ s x y z w k l m v n o p q r h
= UTXO-inductive {tx}{Γ}{s} (x , y , z , w , k , l , m , v , n , o , p , q , r , h)
unquoteDecl UTXO-premises = genPremises UTXO-premises (quote UTXO-inductive)
\end{code}
\caption{UTXO inference rules}
\label{fig:rules:utxo-shelley}
\end{figure*}
\end{NoConway}