/
LedgerState.hs
1169 lines (1064 loc) · 37 KB
/
LedgerState.hs
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{-# LANGUAGE EmptyDataDecls #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TemplateHaskell #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-|
Module : LedgerState
Description : Operational Rules
This module implements the operation rules for treating UTxO transactions ('Tx')
as state transformations on a ledger state ('LedgerState'),
as specified in /A Simplified Formal Specification of a UTxO Ledger/.
-}
module LedgerState
( LedgerState(..)
, Ix
, DPState(..)
, DState(..)
, AccountState(..)
, RewardUpdate(..)
, emptyRewardUpdate
, EpochState(..)
, emptyEpochState
, emptyLedgerState
, dstate
, pstate
, ptrs
, fdms
, dms
, PState(..)
, cCounters
, LedgerValidation(..)
, KeyPairs
, UTxOState(..)
, StakeShare(..)
, Validity(..)
, mkStakeShare
, emptyAccount
, emptyPState
, emptyDState
, poolRAcnt
, treasury
, reserves
-- * state transitions
, asStateTransition
, asStateTransition'
, delegatedStake
, retirePools
, emptyDelegation
, applyDCert
, applyDCertDState
, applyDCertPState
, applyUTxOUpdate
-- * Genesis State
, genesisId
, genesisState
-- * Validation
, ValidationError (..)
, minfee
, validStakePoolRetire
, validInputs
, validNoReplay
, validFee
, validKeyRegistration
, validKeyDeregistration
, validStakeDelegation
, preserveBalance
, verifiedWits
, witsNeeded
-- lenses
, utxoState
, delegationState
-- UTxOState
, utxo
, deposited
, fees
, ups
-- DelegationState
, rewards
, stKeys
, delegations
, stPools
, pParams
, retiring
-- refunds
, keyRefunds
, keyRefund
, decayedKey
, decayedTx
, poolRefunds
-- epoch boundary
, poolRewards
, leaderRew
, memberRew
, rewardOnePool
, reward
, stakeDistr
, poolDistr
, applyRUpd
, createRUpd
--
, NewEpochState(..)
, NewEpochEnv(..)
, overlaySchedule
, getGKeys
, setIssueNumbers
, updateNES
) where
import Control.Monad (foldM)
import qualified Data.Map.Strict as Map
import Data.Maybe (fromMaybe, mapMaybe)
import Data.Ratio
import Data.Set (Set)
import qualified Data.Set as Set
import Numeric.Natural (Natural)
import Lens.Micro ((%~), (&), (.~), (^.))
import Lens.Micro.TH (makeLenses)
import Address
import Coin (Coin (..))
import EpochBoundary
import Keys
import PParams (PParams (..), emptyPParams, keyDecayRate, keyDeposit, keyMinRefund,
minfeeA, minfeeB)
import Slot (Epoch (..), Slot (..), epochFromSlot, firstSlot, slotsPerEpoch, (-*))
import Tx
import qualified Updates
import UTxO
import Delegation.Certificates (DCert (..), PoolDistr (..), StakeKeys (..),
StakePools (..), cwitness, decayKey, refund)
import Delegation.PoolParams (Delegation (..), PoolParams (..), RewardAcnt (..),
poolOwners, poolPledge, poolPubKey, poolRAcnt, poolSpec)
import BaseTypes
import Ledger.Core ((◁), (▷), (∪+))
-- | Representation of a list of pairs of key pairs, e.g., pay and stake keys
type KeyPairs dsignAlgo = [(KeyPair dsignAlgo, KeyPair dsignAlgo)]
-- | A ledger validation state consists of a ledger state 't' and the list of
-- validation errors that occurred from a valid 's' to reach 't'.
data LedgerValidation hashAlgo dsignAlgo
= LedgerValidation [ValidationError] (LedgerState hashAlgo dsignAlgo)
deriving (Show, Eq)
-- |Validation errors represent the failures of a transaction to be valid
-- for a given ledger state.
data ValidationError =
-- | The transaction inputs are not valid.
BadInputs
-- | The transaction has expired
| Expired Slot Slot
-- | Pool Retirement Certificate expired
| RetirementCertExpired Slot Slot
-- | The transaction fee is too small
| FeeTooSmall Coin Coin
-- | Value is not conserved
| ValueNotConserved Coin Coin
-- | Unknown reward account
| IncorrectRewards
-- | One of the transaction witnesses is invalid.
| InvalidWitness
-- | The transaction does not have the required witnesses.
| MissingWitnesses
-- | Missing Replay Attack Protection, at least one input must be spent.
| InputSetEmpty
-- | A stake key cannot be registered again.
| StakeKeyAlreadyRegistered
-- | A stake key must be registered to be used or deregistered.
| StakeKeyNotRegistered
-- | The stake key to which is delegated is not known.
| StakeDelegationImpossible
-- | Stake pool not registered for key, cannot be retired.
| StakePoolNotRegisteredOnKey
deriving (Show, Eq)
-- |The validity of a transaction, where an invalid transaction
-- is represented by list of errors.
data Validity = Valid | Invalid [ValidationError] deriving (Show, Eq)
instance Semigroup Validity where
Valid <> b = b
a <> Valid = a
(Invalid a) <> (Invalid b) = Invalid (a ++ b)
instance Monoid Validity where
mempty = Valid
mappend = (<>)
type RewardAccounts hashAlgo dsignAlgo
= Map.Map (RewardAcnt hashAlgo dsignAlgo) Coin
-- | StakeShare type
newtype StakeShare =
StakeShare Rational
deriving (Show, Ord, Eq)
-- | Construct an optional probability value
mkStakeShare :: Rational -> Maybe StakeShare
mkStakeShare p =
if 0 <= p
then Just $ StakeShare p
else Nothing
data DState hashAlgo dsignAlgo = DState
{ -- |The active stake keys.
_stKeys :: StakeKeys hashAlgo dsignAlgo
-- |The active accounts.
, _rewards :: RewardAccounts hashAlgo dsignAlgo
-- |The current delegations.
, _delegations :: Map.Map (KeyHash hashAlgo dsignAlgo) (KeyHash hashAlgo dsignAlgo)
-- |The pointed to hash keys.
, _ptrs :: Map.Map Ptr (KeyHash hashAlgo dsignAlgo)
-- | future genesis key delegations
, _fdms :: Map.Map (Slot, VKeyGenesis dsignAlgo) (VKey dsignAlgo)
-- |Genesis key delegations
, _dms :: Dms dsignAlgo
} deriving (Show, Eq)
data PState hashAlgo dsignAlgo = PState
{ -- |The active stake pools.
_stPools :: StakePools hashAlgo dsignAlgo
-- |The pool parameters.
, _pParams :: Map.Map (KeyHash hashAlgo dsignAlgo) (PoolParams hashAlgo dsignAlgo)
-- |A map of retiring stake pools to the epoch when they retire.
, _retiring :: Map.Map (KeyHash hashAlgo dsignAlgo) Epoch
-- | Operational Certificate Counters.
, _cCounters :: Map.Map (KeyHash hashAlgo dsignAlgo) Natural
} deriving (Show, Eq)
-- |The state associated with the current stake delegation.
data DPState hashAlgo dsignAlgo =
DPState
{
_dstate :: DState hashAlgo dsignAlgo
, _pstate :: PState hashAlgo dsignAlgo
} deriving (Show, Eq)
data RewardUpdate hashAlgo dsignAlgo = RewardUpdate
{ deltaT :: Coin
, deltaR :: Coin
, rs :: Map.Map (RewardAcnt hashAlgo dsignAlgo) Coin
, deltaF :: Coin
} deriving (Show, Eq)
emptyRewardUpdate :: RewardUpdate hashAlgo dsignAlgo
emptyRewardUpdate = RewardUpdate (Coin 0) (Coin 0) Map.empty (Coin 0)
data AccountState = AccountState
{ _treasury :: Coin
, _reserves :: Coin
} deriving (Show, Eq)
data EpochState hashAlgo dsignAlgo
= EpochState
AccountState
(SnapShots hashAlgo dsignAlgo)
(LedgerState hashAlgo dsignAlgo)
PParams
deriving (Show, Eq)
emptyEpochState :: EpochState hashAlgo dsignAlgo
emptyEpochState =
EpochState emptyAccount emptySnapShots emptyLedgerState emptyPParams
emptyLedgerState :: LedgerState hashAlgo dsignAlgo
emptyLedgerState =
LedgerState
(UTxOState (UTxO Map.empty) (Coin 0) (Coin 0) Updates.emptyUpdateState)
emptyDelegation
0
emptyAccount :: AccountState
emptyAccount = AccountState (Coin 0) (Coin 0)
emptyDelegation :: DPState hashAlgo dsignAlgo
emptyDelegation =
DPState emptyDState emptyPState
emptyDState :: DState hashAlgo dsignAlgo
emptyDState =
DState (StakeKeys Map.empty) Map.empty Map.empty Map.empty Map.empty (Dms Map.empty)
emptyPState :: PState hashAlgo dsignAlgo
emptyPState =
PState (StakePools Map.empty) Map.empty Map.empty Map.empty
data UTxOState hashAlgo dsignAlgo =
UTxOState
{ _utxo :: !(UTxO hashAlgo dsignAlgo)
, _deposited :: Coin
, _fees :: Coin
, _ups :: ( Updates.PPUpdate dsignAlgo
, Updates.AVUpdate dsignAlgo
, Map.Map Slot Updates.Applications
, Updates.Applications)
} deriving (Show, Eq)
-- | New Epoch state and environment
data NewEpochState hashAlgo dsignAlgo =
NewEpochState {
nesEL :: Epoch
, nesEta0 :: Seed
, nesBprev :: BlocksMade hashAlgo dsignAlgo
, nesBcur :: BlocksMade hashAlgo dsignAlgo
, nesEs :: EpochState hashAlgo dsignAlgo
, nesRu :: Maybe (RewardUpdate hashAlgo dsignAlgo)
, nesPd :: PoolDistr hashAlgo dsignAlgo
, nesOsched :: Map.Map Slot (Maybe (VKeyGenesis dsignAlgo))
} deriving (Show, Eq)
getGKeys :: NewEpochState hashAlgo dsignAlgo -> Set (VKeyGenesis dsignAlgo)
getGKeys nes = Map.keysSet dms
where NewEpochState _ _ _ _ es _ _ _ = nes
EpochState _ _ ls _ = es
LedgerState _ (DPState (DState _ _ _ _ _ (Dms dms)) _) _ = ls
data NewEpochEnv dsignAlgo =
NewEpochEnv {
neeEta1 :: Seed
, neeS :: Slot
, neeGkeys :: Set.Set (VKeyGenesis dsignAlgo)
} deriving (Show, Eq)
-- |The state associated with a 'Ledger'.
data LedgerState hashAlgo dsignAlgo =
LedgerState
{ -- |The current unspent transaction outputs.
_utxoState :: !(UTxOState hashAlgo dsignAlgo)
-- |The current delegation state
, _delegationState :: !(DPState hashAlgo dsignAlgo)
-- |The current transaction index in the current slot.
, _txSlotIx :: Ix
} deriving (Show, Eq)
makeLenses ''DPState
makeLenses ''DState
makeLenses ''PState
makeLenses ''UTxOState
makeLenses ''AccountState
makeLenses ''LedgerState
-- |The transaction Id for 'UTxO' included at the beginning of a new ledger.
genesisId
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> TxId hashAlgo dsignAlgo
genesisId =
TxId $ hash
(TxBody
Set.empty
[]
[]
Map.empty
(Coin 0)
(Slot 0)
Updates.emptyUpdate)
-- |Creates the ledger state for an empty ledger which
-- contains the specified transaction outputs.
genesisState
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> PParams
-> [TxOut hashAlgo dsignAlgo]
-> LedgerState hashAlgo dsignAlgo
genesisState _ outs = LedgerState
(UTxOState
(UTxO $ Map.fromList
[(TxIn genesisId idx, out) | (idx, out) <- zip [0..] outs])
(Coin 0)
(Coin 0)
Updates.emptyUpdateState)
emptyDelegation
0
-- | Determine if the transaction has expired
current :: TxBody hashAlgo dsignAlgo -> Slot -> Validity
current tx slot =
if tx ^. ttl < slot
then Invalid [Expired (tx ^. ttl) slot]
else Valid
-- | Determine if the input set of a transaction consumes at least one input,
-- else it would be possible to do a replay attack using this transaction.
validNoReplay :: TxBody hashAlgo dsignAlgo -> Validity
validNoReplay tx =
if txins tx == Set.empty
then Invalid [InputSetEmpty]
else Valid
-- |Determine if the inputs in a transaction are valid for a given ledger state.
validInputs
:: TxBody hashAlgo dsignAlgo
-> UTxOState hashAlgo dsignAlgo
-> Validity
validInputs tx u =
if txins tx `Set.isSubsetOf` dom (u ^. utxo)
then Valid
else Invalid [BadInputs]
-- |Implementation of abstract transaction size
txsize
:: DSIGNAlgorithm dsignAlgo
=> TxBody hashAlgo dsignAlgo
-> Integer
txsize = toEnum . length . show
-- |Minimum fee calculation
minfee
:: DSIGNAlgorithm dsignAlgo
=> PParams
-> TxBody hashAlgo dsignAlgo
-> Coin
minfee pc tx = Coin $ pc ^. minfeeA * txsize tx + (fromIntegral $ pc ^. minfeeB)
-- |Determine if the fee is large enough
validFee
:: DSIGNAlgorithm dsignAlgo
=> PParams
-> TxBody hashAlgo dsignAlgo
-> Validity
validFee pc tx =
if needed <= given
then Valid
else Invalid [FeeTooSmall needed given]
where
needed = minfee pc tx
given = tx ^. txfee
-- |Compute the lovelace which are created by the transaction
produced
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> PParams
-> StakePools hashAlgo dsignAlgo
-> TxBody hashAlgo dsignAlgo
-> Coin
produced pp stakePools tx =
balance (txouts tx) + tx ^. txfee + deposits pp stakePools (tx ^. certs)
-- |Compute the key deregistration refunds in a transaction
keyRefunds
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> PParams
-> StakeKeys hashAlgo dsignAlgo
-> TxBody hashAlgo dsignAlgo
-> Coin
keyRefunds pp stk tx =
sum [keyRefund dval dmin lambda stk (tx ^. ttl) c | c@(DeRegKey _) <- tx ^. certs]
where (dval, dmin, lambda) = decayKey pp
-- | Key refund for a deregistration certificate.
keyRefund
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> Coin
-> UnitInterval
-> Rational
-> StakeKeys hashAlgo dsignAlgo
-> Slot
-> DCert hashAlgo dsignAlgo
-> Coin
keyRefund dval dmin lambda (StakeKeys stkeys) slot c =
case c of
DeRegKey key -> case Map.lookup (hashKey key) stkeys of
Nothing -> Coin 0
Just s -> refund dval dmin lambda $ slot -* s
_ -> Coin 0
-- | Functions to calculate decayed deposits
decayedKey
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> PParams
-> StakeKeys hashAlgo dsignAlgo
-> Slot
-> DCert hashAlgo dsignAlgo
-> Coin
decayedKey pp stk@(StakeKeys stkeys) cslot cert =
case cert of
DeRegKey key ->
if Map.notMember (hashKey key) stkeys
then 0
else let created' = stkeys Map.! hashKey key in
let start = max (firstSlot $ epochFromSlot cslot) created' in
let dval = pp ^. keyDeposit in
let dmin = pp ^. keyMinRefund in
let lambda = pp ^. keyDecayRate in
let epochRefund = keyRefund dval dmin lambda stk start cert in
let currentRefund = keyRefund dval dmin lambda stk cslot cert in
epochRefund - currentRefund
_ -> 0
-- | Decayed deposit portions
decayedTx
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> PParams
-> StakeKeys hashAlgo dsignAlgo
-> TxBody hashAlgo dsignAlgo
-> Coin
decayedTx pp stk tx =
sum [decayedKey pp stk (tx ^. ttl) c | c@(DeRegKey _) <- tx ^. certs]
-- |Compute the lovelace which are destroyed by the transaction
consumed
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> PParams
-> UTxO hashAlgo dsignAlgo
-> StakeKeys hashAlgo dsignAlgo
-> TxBody hashAlgo dsignAlgo
-> Coin
consumed pp u stakeKeys tx =
balance (txins tx <| u) + refunds + withdrawals
where
refunds = keyRefunds pp stakeKeys tx
withdrawals = sum $ tx ^. wdrls
-- |Determine if the balance of the ledger state would be effected
-- in an acceptable way by a transaction.
preserveBalance
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> StakePools hashAlgo dsignAlgo
-> StakeKeys hashAlgo dsignAlgo
-> PParams
-> TxBody hashAlgo dsignAlgo
-> UTxOState hashAlgo dsignAlgo
-> Validity
preserveBalance stakePools stakeKeys pp tx u =
if destroyed' == created'
then Valid
else Invalid [ValueNotConserved destroyed' created']
where
destroyed' = consumed pp (u ^. utxo) stakeKeys tx
created' = produced pp stakePools tx
-- |Determine if the reward witdrawals correspond
-- to the rewards in the ledger state
correctWithdrawals
:: RewardAccounts hashAlgo dsignAlgo
-> RewardAccounts hashAlgo dsignAlgo
-> Validity
correctWithdrawals accs withdrawals =
if withdrawals `Map.isSubmapOf` accs
then Valid
else Invalid [IncorrectRewards]
-- |Collect the set of hashes of keys that needs to sign a
-- given transaction. This set consists of the txin owners,
-- certificate authors, and withdrawal reward accounts.
witsNeeded
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> UTxO hashAlgo dsignAlgo
-> Tx hashAlgo dsignAlgo
-> Dms dsignAlgo
-> Set (KeyHash hashAlgo dsignAlgo)
witsNeeded utxo' tx@(Tx txbody _ _) _dms =
inputAuthors `Set.union`
wdrlAuthors `Set.union`
certAuthors `Set.union`
updateKeys `Set.union`
owners
where
inputAuthors = Set.foldr insertHK Set.empty (txbody ^. inputs)
insertHK txin hkeys =
case txinLookup txin utxo' of
Just (TxOut (AddrTxin pay _) _) -> Set.insert pay hkeys
_ -> hkeys
wdrlAuthors = Set.map getRwdHK (Map.keysSet (txbody ^. wdrls))
owners = foldl Set.union Set.empty
[pool ^. poolOwners | RegPool pool <- txbody ^. certs]
certAuthors = Set.fromList (fmap getCertHK (txbody ^. certs))
getCertHK cert = cwitness cert
updateKeys = propWits (txup tx) _dms
-- |Given a ledger state, determine if the UTxO witnesses in a given
-- transaction are correct.
verifiedWits
:: ( HashAlgorithm hashAlgo
, DSIGNAlgorithm dsignAlgo
, Signable dsignAlgo (TxBody hashAlgo dsignAlgo)
)
=> Tx hashAlgo dsignAlgo
-> Validity
verifiedWits (Tx tx wits _) =
if all (verifyWitVKey tx) wits
then Valid
else Invalid [InvalidWitness]
-- |Given a ledger state, determine if the UTxO witnesses in a given
-- transaction are sufficient.
-- We check that there are not more witnesses than inputs, if several inputs
-- from the same address are used, it is not strictly necessary to include more
-- than one witness.
enoughWits
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> Tx hashAlgo dsignAlgo
-> Dms dsignAlgo
-> UTxOState hashAlgo dsignAlgo
-> Validity
enoughWits tx@(Tx _ wits _) d u =
if witsNeeded (u ^. utxo) tx d `Set.isSubsetOf` signers
then Valid
else Invalid [MissingWitnesses]
where
signers = Set.map (\(WitVKey vkey _) -> hashKey vkey) wits
validRuleUTXO
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> RewardAccounts hashAlgo dsignAlgo
-> StakePools hashAlgo dsignAlgo
-> StakeKeys hashAlgo dsignAlgo
-> PParams
-> Slot
-> TxBody hashAlgo dsignAlgo
-> UTxOState hashAlgo dsignAlgo
-> Validity
validRuleUTXO accs stakePools stakeKeys pc slot tx u =
validInputs tx u
<> current tx slot
<> validNoReplay tx
<> validFee pc tx
<> preserveBalance stakePools stakeKeys pc tx u
<> correctWithdrawals accs (tx ^. wdrls)
validRuleUTXOW
:: ( HashAlgorithm hashAlgo
, DSIGNAlgorithm dsignAlgo
, Signable dsignAlgo (TxBody hashAlgo dsignAlgo)
)
=> Tx hashAlgo dsignAlgo
-> Dms dsignAlgo
-> LedgerState hashAlgo dsignAlgo
-> Validity
validRuleUTXOW tx d l = verifiedWits tx
<> enoughWits tx d (l ^. utxoState)
-- | Calculate the set of hash keys of the required witnesses for update
-- proposals.
propWits
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> Updates.Update dsignAlgo
-> Dms dsignAlgo
-> Set.Set (KeyHash hashAlgo dsignAlgo)
propWits (Updates.Update (Updates.PPUpdate pup) (Updates.AVUpdate aup)) (Dms _dms) =
Set.fromList $ Map.elems $ Map.map hashKey updateKeys
where updateKeys = (Map.keysSet pup `Set.union` Map.keysSet aup) ◁ _dms
validTx
:: ( HashAlgorithm hashAlgo
, DSIGNAlgorithm dsignAlgo
, Signable dsignAlgo (TxBody hashAlgo dsignAlgo)
)
=> Tx hashAlgo dsignAlgo
-> Dms dsignAlgo
-> Slot
-> PParams
-> LedgerState hashAlgo dsignAlgo
-> Validity
validTx tx d slot pp l =
validRuleUTXO (l ^. delegationState . dstate . rewards)
(l ^. delegationState . pstate . stPools)
(l ^. delegationState . dstate . stKeys)
pp
slot
(tx ^. body)
(l ^. utxoState)
<> validRuleUTXOW tx d l
-- The rules for checking validiy of stake delegation transitions return
-- `certificate_type_correct(cert) -> valid_cert(cert)`, i.e., if the
-- certificate is of a different type, it's considered to be valid due to the
-- falsified hypothesis.
-- | Checks whether a key registration certificat is valid.
validKeyRegistration
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> DCert hashAlgo dsignAlgo
-> DState hashAlgo dsignAlgo
-> Validity
validKeyRegistration cert ds =
case cert of
RegKey key -> if not $ Map.member (hashKey key) stakeKeys
then Valid else Invalid [StakeKeyAlreadyRegistered]
where (StakeKeys stakeKeys) = ds ^. stKeys
_ -> Valid
validKeyDeregistration
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> DCert hashAlgo dsignAlgo
-> DState hashAlgo dsignAlgo
-> Validity
validKeyDeregistration cert ds =
case cert of
DeRegKey key -> if Map.member (hashKey key) stakeKeys
then Valid else Invalid [StakeKeyNotRegistered]
where (StakeKeys stakeKeys) = ds ^. stKeys
_ -> Valid
validStakeDelegation
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> DCert hashAlgo dsignAlgo
-> DState hashAlgo dsignAlgo
-> Validity
validStakeDelegation cert ds =
case cert of
Delegate (Delegation source _)
-> if Map.member (hashKey source) stakeKeys
then Valid else Invalid [StakeDelegationImpossible]
where (StakeKeys stakeKeys) = ds ^. stKeys
_ -> Valid
-- there is currently no requirement that could make this invalid
validStakePoolRegister
:: DCert hashAlgo dsignAlgo
-> DPState hashAlgo dsignAlgo
-> Validity
validStakePoolRegister _ _ = Valid
validStakePoolRetire
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> DCert hashAlgo dsignAlgo
-> PState hashAlgo dsignAlgo
-> Validity
validStakePoolRetire cert ps =
case cert of
RetirePool key _ -> if Map.member (hashKey key) stakePools
then Valid else Invalid [StakePoolNotRegisteredOnKey]
where (StakePools stakePools) = ps ^. stPools
_ -> Valid
validDelegation
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> DCert hashAlgo dsignAlgo
-> DPState hashAlgo dsignAlgo
-> Validity
validDelegation cert ds =
validKeyRegistration cert (ds ^. dstate)
<> validKeyDeregistration cert (ds ^. dstate)
<> validStakeDelegation cert (ds ^. dstate)
<> validStakePoolRegister cert ds
<> validStakePoolRetire cert (ds ^. pstate)
-- |In the case where a transaction is valid for a given ledger state,
-- apply the transaction as a state transition function on the ledger state.
-- Otherwise, return a list of validation errors.
asStateTransition
:: ( HashAlgorithm hashAlgo
, DSIGNAlgorithm dsignAlgo
, Signable dsignAlgo (TxBody hashAlgo dsignAlgo)
)
=> Slot
-> PParams
-> LedgerState hashAlgo dsignAlgo
-> Tx hashAlgo dsignAlgo
-> Dms dsignAlgo
-> Either [ValidationError] (LedgerState hashAlgo dsignAlgo)
asStateTransition slot pp ls tx d =
case validTx tx d slot pp ls of
Invalid errors -> Left errors
Valid -> foldM (certAsStateTransition slot (ls ^. txSlotIx)) ls' cs
where
ls' = applyTxBody ls pp (tx ^. body)
cs = zip [0..] (tx ^. body . certs) -- index certificates
-- |In the case where a certificate is valid for a given ledger state,
-- apply the certificate as a state transition function on the ledger state.
-- Otherwise, return a list of validation errors.
certAsStateTransition
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> Slot
-> Ix
-> LedgerState hashAlgo dsignAlgo
-> (Ix, DCert hashAlgo dsignAlgo)
-> Either [ValidationError] (LedgerState hashAlgo dsignAlgo)
certAsStateTransition slot txIx ls (clx, cert) =
case validDelegation cert (ls ^. delegationState) of
Invalid errors -> Left errors
Valid -> Right $ ls & delegationState %~ applyDCert (Ptr slot txIx clx) cert
-- | Apply transition independent of validity, collect validation errors on the
-- way.
asStateTransition'
:: ( HashAlgorithm hashAlgo
, DSIGNAlgorithm dsignAlgo
, Signable dsignAlgo (TxBody hashAlgo dsignAlgo)
)
=> Slot
-> PParams
-> LedgerValidation hashAlgo dsignAlgo
-> Tx hashAlgo dsignAlgo
-> Dms dsignAlgo
-> LedgerValidation hashAlgo dsignAlgo
asStateTransition' slot pp (LedgerValidation valErrors ls) tx d =
let ls' = applyTxBody ls pp (tx ^. body) in
case validTx tx d slot pp ls of
Invalid errors -> LedgerValidation (valErrors ++ errors) ls'
Valid -> LedgerValidation valErrors ls'
-- Functions for stake delegation model
-- |Retire the appropriate stake pools when the epoch changes.
retirePools
:: LedgerState hashAlgo dsignAlgo
-> Epoch
-> LedgerState hashAlgo dsignAlgo
retirePools ls@(LedgerState _ ds _) epoch =
ls & delegationState .~
(ds & pstate . stPools .~
(StakePools $ Map.filterWithKey
(\hk _ -> Map.notMember hk retiring')
stakePools)
& pstate . retiring .~ active)
where (active, retiring') = Map.partition (epoch /=) (ds ^. pstate . retiring)
(StakePools stakePools) = ds ^. pstate . stPools
-- |Calculate the change to the deposit pool for a given transaction.
depositPoolChange
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> LedgerState hashAlgo dsignAlgo
-> PParams
-> TxBody hashAlgo dsignAlgo
-> Coin
depositPoolChange ls pp tx = (currentPool + txDeposits) - txRefunds
-- Note that while (currentPool + txDeposits) >= txRefunds,
-- it could be that txDeposits < txRefunds. We keep the parenthesis above
-- to emphasize this point.
where
currentPool = ls ^. utxoState . deposited
txDeposits =
deposits pp (ls ^. delegationState . pstate . stPools) (tx ^. certs)
txRefunds = keyRefunds pp (ls ^. delegationState . dstate . stKeys) tx
-- |Apply a transaction body as a state transition function on the ledger state.
applyTxBody
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> LedgerState hashAlgo dsignAlgo
-> PParams
-> TxBody hashAlgo dsignAlgo
-> LedgerState hashAlgo dsignAlgo
applyTxBody ls pp tx =
ls & utxoState %~ flip applyUTxOUpdate tx
& utxoState . deposited .~ depositPoolChange ls pp tx
& utxoState . fees .~ (tx ^. txfee) + (ls ^. utxoState . fees)
& delegationState . dstate . rewards .~ newAccounts
& txSlotIx %~ (+) 1
where
newAccounts = reapRewards (ls ^. delegationState . dstate. rewards) (tx ^. wdrls)
reapRewards
:: RewardAccounts hashAlgo dsignAlgo
-> RewardAccounts hashAlgo dsignAlgo
-> RewardAccounts hashAlgo dsignAlgo
reapRewards dStateRewards withdrawals =
Map.mapWithKey removeRewards dStateRewards
where removeRewards k v = if k `Map.member` withdrawals then Coin 0 else v
applyUTxOUpdate
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> UTxOState hashAlgo dsignAlgo
-> TxBody hashAlgo dsignAlgo
-> UTxOState hashAlgo dsignAlgo
applyUTxOUpdate u tx = u & utxo .~ txins tx </| (u ^. utxo) `union` txouts tx
-- |Apply a delegation certificate as a state transition function on the ledger state.
applyDCert
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> Ptr
-> DCert hashAlgo dsignAlgo
-> DPState hashAlgo dsignAlgo
-> DPState hashAlgo dsignAlgo
applyDCert ptr dcert@(RegKey _) ds =
ds & dstate %~ (applyDCertDState ptr dcert)
applyDCert ptr dcert@(DeRegKey _) ds =
ds & dstate %~ (applyDCertDState ptr dcert)
applyDCert ptr dcert@(RegPool _) ds = ds & pstate %~ (applyDCertPState ptr dcert)
applyDCert ptr dcert@(RetirePool _ _) ds =
ds & pstate %~ (applyDCertPState ptr dcert)
applyDCert _ (GenesisDelegate _) ds = ds -- TODO: check this
-- TODO do we also have to check hashKey target?
applyDCert ptr dcert@(Delegate _) ds =
ds & dstate %~ (applyDCertDState ptr dcert)
applyDCertDState
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> Ptr
-> DCert hashAlgo dsignAlgo
-> DState hashAlgo dsignAlgo
-> DState hashAlgo dsignAlgo
applyDCertDState (Ptr slot txIx clx) (DeRegKey key) ds =
ds & stKeys .~ (StakeKeys $ Map.delete hksk stkeys')
& rewards %~ Map.delete (RewardAcnt hksk)
& delegations %~ Map.delete hksk
& ptrs %~ Map.delete (Ptr slot txIx clx)
where hksk = hashKey key
(StakeKeys stkeys') = ds ^. stKeys
applyDCertDState (Ptr slot txIx clx) (RegKey key) ds =
ds & stKeys .~ (StakeKeys $ Map.insert hksk slot stkeys')
& rewards %~ Map.insert (RewardAcnt hksk) (Coin 0)
& ptrs %~ Map.insert (Ptr slot txIx clx) hksk
where hksk = hashKey key
(StakeKeys stkeys') = ds ^. stKeys
applyDCertDState _ (Delegate (Delegation source target)) ds =
ds & delegations %~ Map.insert (hashKey source) (hashKey target)
applyDCertDState _ _ ds = ds
applyDCertPState
:: (HashAlgorithm hashAlgo, DSIGNAlgorithm dsignAlgo)
=> Ptr
-> DCert hashAlgo dsignAlgo
-> PState hashAlgo dsignAlgo
-> PState hashAlgo dsignAlgo
applyDCertPState (Ptr slot _ _ ) (RegPool sp) ps =
ps & stPools .~ (StakePools $ Map.insert hsk slot' pools)
& pParams %~ Map.insert hsk sp
& retiring %~ Map.delete hsk
where hsk = hashKey $ sp ^. poolPubKey
(StakePools pools) = ps ^. stPools
slot' = fromMaybe slot (Map.lookup hsk pools)
-- TODO check epoch (not in new doc atm.)
applyDCertPState _ (RetirePool key epoch) ps =
ps & retiring %~ Map.insert hk_sp epoch
where hk_sp = hashKey key
-- | Use onlt pool registration or retirement certificates
applyDCertPState _ _ ps = ps
-- |Compute how much stake each active stake pool controls.
delegatedStake
:: LedgerState hashAlgo dsignAlgo
-> Map.Map (KeyHash hashAlgo dsignAlgo) Coin
delegatedStake ls@(LedgerState _ ds _) = Map.fromListWith (+) delegatedOutputs
where
getOutputs (UTxO utxo') = Map.elems utxo'
addStake delegs (TxOut (AddrTxin _ hsk) c) = do
pool <- Map.lookup hsk delegs
return (pool, c)
addStake delegs (TxOut (AddrPtr ptr) c) = do
key <- Map.lookup ptr $ ds ^. dstate . ptrs
pool <- Map.lookup key delegs
return (pool, c)
outs = getOutputs $ ls ^. utxoState . utxo
delegatedOutputs = mapMaybe (addStake $ ds ^. dstate . delegations) outs
---------------------------------
-- epoch boundary calculations --
---------------------------------
-- | Calculate pool reward
poolRewards
:: KeyHash hashAlgo dsignAlgo
-> UnitInterval
-> Natural
-> Natural
-> Coin
-> Coin
poolRewards _ sigma blocksN blocksTotal (Coin maxP) =
floor $ p * fromIntegral maxP
where
p = beta / (intervalValue sigma)
beta = fromIntegral blocksN / (fromIntegral $ max 1 blocksTotal)
-- | Calculate pool leader reward
leaderRew
:: Coin
-> PoolParams hashAlgo dsignAlgo
-> StakeShare
-> StakeShare
-> Coin
leaderRew f@(Coin f') pool (StakeShare s) (StakeShare sigma)
| f' <= c = f
| otherwise =
floor $ fromIntegral (c + (f' - c)) * (m' + (1 - m') * sigma / s)
where
(Coin c, m, _) = poolSpec pool
m' = intervalValue m
-- | Calculate pool member reward
memberRew
:: Coin
-> PoolParams hashAlgo dsignAlgo
-> StakeShare
-> StakeShare
-> Coin
memberRew (Coin f') pool (StakeShare t) (StakeShare sigma)
| f' <= c = 0
| otherwise = floor $ fromIntegral (f' - c) * (1 - m') * sigma / t
where