-
Notifications
You must be signed in to change notification settings - Fork 155
/
TwoPools.hs
811 lines (722 loc) · 23.6 KB
/
TwoPools.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
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE ConstraintKinds #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE NumericUnderscores #-}
{-# LANGUAGE OverloadedLists #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
-- |
-- Module : Test.Cardano.Ledger.Shelley.Examples.TwoPools
-- Description : Two Pools Example
--
-- Example demonstrating a particular delegation scenario involving
-- two pools. Both pools select a reward account which is *not*
-- a pool owner, and which delegates to one of the pools.
module Test.Cardano.Ledger.Shelley.Examples.TwoPools (
twoPoolsExample,
twoPoolsExampleExtended,
)
where
import Cardano.Ledger.BaseTypes (
BlocksMade (..),
BoundedRational (..),
Globals (..),
Network (..),
Nonce,
ProtVer (..),
StrictMaybe (..),
activeSlotVal,
mkCertIxPartial,
natVersion,
(⭒),
)
import Cardano.Ledger.Block (Block, bheader)
import Cardano.Ledger.Coin (
Coin (..),
DeltaCoin (..),
rationalToCoinViaFloor,
toDeltaCoin,
)
import Cardano.Ledger.Credential (Credential, Ptr (..))
import Cardano.Ledger.Crypto
import qualified Cardano.Ledger.EpochBoundary as EB
import Cardano.Ledger.Keys (KeyRole (..), asWitness, coerceKeyRole)
import Cardano.Ledger.PoolDistr (
IndividualPoolStake (..),
PoolDistr (..),
)
import Cardano.Ledger.SafeHash (hashAnnotated)
import Cardano.Ledger.Shelley
import Cardano.Ledger.Shelley.Core
import Cardano.Ledger.Shelley.LedgerState (
PulsingRewUpdate (..),
RewardUpdate (..),
completeStep,
emptyRewardUpdate,
)
import Cardano.Ledger.Shelley.PoolRank (
Likelihood (..),
NonMyopic (..),
leaderProbability,
likelihood,
)
import Cardano.Ledger.Shelley.Rewards (
StakeShare (..),
aggregateRewards,
leaderRew,
memberRew,
mkApparentPerformance,
sumRewards,
)
import Cardano.Ledger.Shelley.Tx (
ShelleyTx (..),
)
import Cardano.Ledger.Shelley.TxBody (
PoolParams (..),
RewardAcnt (..),
ShelleyTxBody (..),
ShelleyTxOut (..),
)
import Cardano.Ledger.Shelley.TxCert (ShelleyDelegCert (..), pattern ShelleyTxCertDeleg)
import Cardano.Ledger.Shelley.TxWits (
addrWits,
)
import Cardano.Ledger.Slot (
BlockNo (..),
EpochNo (..),
SlotNo (..),
)
import Cardano.Ledger.TxIn (TxIn (..))
import Cardano.Ledger.UTxO (UTxO (..))
import Cardano.Ledger.Val ((<+>), (<->), (<×>))
import qualified Cardano.Ledger.Val as Val
import Cardano.Protocol.TPraos.BHeader (BHeader, bhHash, hashHeaderToNonce)
import Cardano.Protocol.TPraos.OCert (KESPeriod (..))
import Data.Default.Class (def)
import Data.Group (invert)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Ratio ((%))
import qualified Data.Sequence.Strict as StrictSeq
import Data.Set (Set)
import qualified Data.Set as Set
import GHC.Stack (HasCallStack)
import Lens.Micro ((&), (.~), (^.))
import Test.Cardano.Ledger.Core.KeyPair (mkWitnessesVKey)
import Test.Cardano.Ledger.Shelley.ConcreteCryptoTypes (C, C_Crypto, ExMock)
import Test.Cardano.Ledger.Shelley.Examples (CHAINExample (..), testCHAINExample)
import qualified Test.Cardano.Ledger.Shelley.Examples.Cast as Cast
import qualified Test.Cardano.Ledger.Shelley.Examples.Combinators as C
import Test.Cardano.Ledger.Shelley.Examples.Federation (
coreNodeKeysBySchedule,
)
import Test.Cardano.Ledger.Shelley.Examples.Init (
initSt,
lastByronHeaderHash,
nonce0,
ppEx,
)
import Test.Cardano.Ledger.Shelley.Examples.PoolLifetime (makeCompletedPulser, mkStake)
import Test.Cardano.Ledger.Shelley.Generator.Core (
AllIssuerKeys (..),
NatNonce (..),
genesisCoins,
mkBlockFakeVRF,
mkOCert,
)
import Test.Cardano.Ledger.Shelley.Generator.EraGen (genesisId)
import Test.Cardano.Ledger.Shelley.Generator.ShelleyEraGen ()
import Test.Cardano.Ledger.Shelley.Rules.Chain (ChainState (..))
import Test.Cardano.Ledger.Shelley.Utils (
epochSize,
getBlockNonce,
maxLLSupply,
runShelleyBase,
testGlobals,
unsafeBoundRational,
)
import Test.Tasty (TestTree, testGroup)
import Test.Tasty.HUnit (Assertion, testCase, (@?=))
aliceInitCoin :: Coin
aliceInitCoin = Coin 10_000_000_000_000_000
bobInitCoin :: Coin
bobInitCoin = Coin 1_000_000_000_000_000
carlInitCoin :: Coin
carlInitCoin = Coin 5_000_000_000_000_000
initUTxO :: UTxO C
initUTxO =
genesisCoins
genesisId
[ ShelleyTxOut Cast.aliceAddr (Val.inject aliceInitCoin)
, ShelleyTxOut Cast.bobAddr (Val.inject bobInitCoin)
, ShelleyTxOut Cast.carlAddr (Val.inject carlInitCoin)
]
initStTwoPools :: ChainState C
initStTwoPools = initSt initUTxO
--
-- Block 1, Slot 10, Epoch 0
--
aliceCoinEx1 :: Coin
aliceCoinEx1 =
aliceInitCoin
<-> ((2 :: Integer) <×> Coin 250)
<-> ((3 :: Integer) <×> Coin 7)
<-> feeTx1
feeTx1 :: Coin
feeTx1 = Coin 3
alicePoolParams' :: Crypto c => PoolParams c
alicePoolParams' = Cast.alicePoolParams {ppRewardAcnt = RewardAcnt Testnet Cast.carlSHK}
bobPoolParams' :: Crypto c => PoolParams c
bobPoolParams' = Cast.bobPoolParams {ppRewardAcnt = RewardAcnt Testnet Cast.carlSHK}
txbodyEx1 :: TxBody C
txbodyEx1 =
ShelleyTxBody
(Set.fromList [TxIn genesisId minBound])
(StrictSeq.fromList [ShelleyTxOut Cast.aliceAddr (Val.inject aliceCoinEx1)])
( StrictSeq.fromList
[ ShelleyTxCertDeleg (ShelleyRegCert Cast.aliceSHK)
, ShelleyTxCertDeleg (ShelleyRegCert Cast.bobSHK)
, ShelleyTxCertDeleg (ShelleyRegCert Cast.carlSHK)
, RegPoolTxCert alicePoolParams'
, RegPoolTxCert bobPoolParams'
, ShelleyTxCertDeleg (ShelleyDelegCert Cast.aliceSHK (aikColdKeyHash Cast.alicePoolKeys))
, ShelleyTxCertDeleg (ShelleyDelegCert Cast.bobSHK (aikColdKeyHash Cast.bobPoolKeys))
, ShelleyTxCertDeleg (ShelleyDelegCert Cast.carlSHK (aikColdKeyHash Cast.alicePoolKeys))
]
)
(Withdrawals Map.empty)
feeTx1
(SlotNo 10)
SNothing
SNothing
txEx1 :: ShelleyTx C
txEx1 =
ShelleyTx
txbodyEx1
mempty
{ addrWits =
mkWitnessesVKey
(hashAnnotated txbodyEx1)
( (asWitness <$> [Cast.alicePay])
<> (asWitness <$> [Cast.aliceStake, Cast.bobStake, Cast.carlStake])
<> (asWitness <$> [aikCold Cast.alicePoolKeys, aikCold Cast.bobPoolKeys])
)
}
SNothing
blockEx1 :: HasCallStack => Block (BHeader C_Crypto) C
blockEx1 =
mkBlockFakeVRF
lastByronHeaderHash
(coreNodeKeysBySchedule @C ppEx 10)
[txEx1]
(SlotNo 10)
(BlockNo 1)
(nonce0 @C_Crypto)
(NatNonce 1)
minBound
0
0
(mkOCert (coreNodeKeysBySchedule @C ppEx 10) 0 (KESPeriod 0))
expectedStEx1 :: ChainState C
expectedStEx1 =
C.evolveNonceUnfrozen (getBlockNonce blockEx1)
. C.newLab blockEx1
. C.feesAndDeposits ppEx feeTx1 [Cast.aliceSHK, Cast.bobSHK, Cast.carlSHK] [alicePoolParams', bobPoolParams']
. C.newUTxO txbodyEx1
. C.newStakeCred Cast.aliceSHK (Ptr (SlotNo 10) minBound (mkCertIxPartial 0))
. C.newStakeCred Cast.bobSHK (Ptr (SlotNo 10) minBound (mkCertIxPartial 1))
. C.newStakeCred Cast.carlSHK (Ptr (SlotNo 10) minBound (mkCertIxPartial 2))
. C.newPool alicePoolParams'
. C.newPool bobPoolParams'
. C.delegation Cast.aliceSHK (ppId alicePoolParams')
. C.delegation Cast.bobSHK (ppId bobPoolParams')
. C.delegation Cast.carlSHK (ppId alicePoolParams')
$ initStTwoPools
-- === Block 1, Slot 10, Epoch 0
--
-- In the first block of this example, Alice, Bob, and Carl register
-- stake credentials, Alice and Bob register stake pools,
-- Alice and Carl delegate to Alice's pool, and Bob delegates to Bob's pool.
--
-- This is the only block in this example that includes a transaction,
-- and after this block is processed, the UTxO will consist entirely
-- of Alice's new coin aliceCoinEx1, and Bob and Carls initial genesis coins.
twoPools1 :: CHAINExample (BHeader C_Crypto) C
twoPools1 = CHAINExample initStTwoPools blockEx1 (Right expectedStEx1)
--
-- Block 2, Slot 90, Epoch 0
--
blockEx2 :: Block (BHeader C_Crypto) C
blockEx2 =
mkBlockFakeVRF
(bhHash $ bheader blockEx1)
(coreNodeKeysBySchedule @C ppEx 90)
[]
(SlotNo 90)
(BlockNo 2)
(nonce0 @C_Crypto)
(NatNonce 2)
minBound
4
0
(mkOCert (coreNodeKeysBySchedule @C ppEx 90) 0 (KESPeriod 0))
expectedStEx2 :: ChainState C
expectedStEx2 =
C.evolveNonceFrozen (getBlockNonce blockEx2)
. C.newLab blockEx2
. C.rewardUpdate emptyRewardUpdate
$ expectedStEx1
-- === Block 2, Slot 90, Epoch 0
--
-- Create an empty block near the end of epoch 0 to close out the epoch.
twoPools2 :: CHAINExample (BHeader C_Crypto) C
twoPools2 = CHAINExample expectedStEx1 blockEx2 (Right expectedStEx2)
--
-- Block 3, Slot 110, Epoch 1
--
epoch1Nonce :: Nonce
epoch1Nonce = chainCandidateNonce expectedStEx2
blockEx3 :: Block (BHeader C_Crypto) C
blockEx3 =
mkBlockFakeVRF
(bhHash $ bheader @(BHeader C_Crypto) blockEx2)
(coreNodeKeysBySchedule @C ppEx 110)
[]
(SlotNo 110)
(BlockNo 3)
epoch1Nonce
(NatNonce 3)
minBound
5
0
(mkOCert (coreNodeKeysBySchedule @C ppEx 110) 0 (KESPeriod 0))
snapEx3 :: ExMock c => EB.SnapShot c
snapEx3 =
EB.SnapShot
{ EB.ssStake =
mkStake
[ (Cast.aliceSHK, aliceCoinEx1)
, (Cast.bobSHK, bobInitCoin)
, (Cast.carlSHK, carlInitCoin)
]
, EB.ssDelegations =
[ (Cast.aliceSHK, aikColdKeyHash Cast.alicePoolKeys)
, (Cast.bobSHK, aikColdKeyHash Cast.bobPoolKeys)
, (Cast.carlSHK, aikColdKeyHash Cast.alicePoolKeys)
]
, EB.ssPoolParams =
[ (aikColdKeyHash Cast.alicePoolKeys, alicePoolParams')
, (aikColdKeyHash Cast.bobPoolKeys, bobPoolParams')
]
}
expectedStEx3 :: ChainState C
expectedStEx3 =
C.newEpoch blockEx3
. C.newSnapshot snapEx3 feeTx1
. C.applyRewardUpdate emptyRewardUpdate
$ expectedStEx2
-- === Block 3, Slot 110, Epoch 1
--
-- Create an empty block at the begining of epoch 1 to trigger the epoch change.
twoPools3 :: CHAINExample (BHeader C_Crypto) C
twoPools3 = CHAINExample expectedStEx2 blockEx3 (Right expectedStEx3)
--
-- Block 4, Slot 190, Epoch 1
--
blockEx4 :: Block (BHeader C_Crypto) C
blockEx4 =
mkBlockFakeVRF
(bhHash $ bheader @(BHeader C_Crypto) blockEx3)
(coreNodeKeysBySchedule @C ppEx 190)
[]
(SlotNo 190)
(BlockNo 4)
epoch1Nonce
(NatNonce 4)
minBound
9
0
(mkOCert (coreNodeKeysBySchedule @C ppEx 190) 0 (KESPeriod 0))
deltaREx4 :: Coin
deltaREx4 = Coin 3
rewardUpdateEx4 :: forall c. RewardUpdate c
rewardUpdateEx4 =
RewardUpdate
{ deltaT = DeltaCoin 0
, deltaR = toDeltaCoin deltaREx4 -- No rewards paid out, fees go to reserves
, rs = Map.empty
, deltaF = invert $ toDeltaCoin feeTx1
, nonMyopic = def {rewardPotNM = feeTx1}
}
expectedStEx4 :: ChainState C
expectedStEx4 =
C.evolveNonceFrozen (getBlockNonce blockEx4)
. C.newLab blockEx4
. C.rewardUpdate rewardUpdateEx4
$ expectedStEx3
-- === Block 4, Slot 190, Epoch 1
--
-- Create an empty block near the end of epoch 0 to close out the epoch,
-- preparing the way for the first non-empty pool distribution.
twoPools4 :: CHAINExample (BHeader C_Crypto) C
twoPools4 = CHAINExample expectedStEx3 blockEx4 (Right expectedStEx4)
epoch2Nonce :: Nonce
epoch2Nonce =
chainCandidateNonce expectedStEx4
⭒ hashHeaderToNonce (bhHash $ bheader blockEx2)
--
-- Block 5, Slot 221, Epoch 2
--
blockEx5 :: Block (BHeader C_Crypto) C
blockEx5 =
mkBlockFakeVRF
(bhHash $ bheader @(BHeader C_Crypto) blockEx4)
Cast.alicePoolKeys
[]
(SlotNo 221) -- odd slots open for decentralization
(BlockNo 5)
epoch2Nonce
(NatNonce 5)
minBound
11
10
(mkOCert Cast.alicePoolKeys 0 (KESPeriod 10))
activeStakeEx5 :: Integer
activeStakeEx5 = sum $ map unCoin [aliceCoinEx1, bobInitCoin, carlInitCoin]
alicePoolStake :: Rational
alicePoolStake = (unCoin aliceCoinEx1 + unCoin carlInitCoin) % activeStakeEx5
bobPoolStake :: Rational
bobPoolStake = unCoin bobInitCoin % activeStakeEx5
pdEx5 :: forall c. Crypto c => PoolDistr c
pdEx5 =
PoolDistr $
Map.fromList
[
( aikColdKeyHash $ Cast.alicePoolKeys @c
, IndividualPoolStake alicePoolStake (Cast.aliceVRFKeyHash @c)
)
,
( aikColdKeyHash $ Cast.bobPoolKeys @c
, IndividualPoolStake bobPoolStake (Cast.bobVRFKeyHash @c)
)
]
expectedStEx5 :: ChainState C
expectedStEx5 =
C.incrBlockCount (aikColdKeyHash Cast.alicePoolKeys)
. C.newSnapshot snapEx3 (Coin 0)
. C.applyRewardUpdate rewardUpdateEx4
. C.setPoolDistr pdEx5
. C.setOCertCounter (coerceKeyRole $ aikColdKeyHash Cast.alicePoolKeys) 0
. C.newEpoch blockEx5 -- This must be processed before the incrBlockCount
$ expectedStEx4
-- === Block 5, Slot 220, Epoch 2
--
-- Create the first non-empty pool distribution by starting the epoch 2.
-- Moreover, Alice's pool produces the block.
twoPools5 :: CHAINExample (BHeader C_Crypto) C
twoPools5 = CHAINExample expectedStEx4 blockEx5 (Right expectedStEx5)
--
-- Block 6, Slot 295, Epoch 2
--
blockEx6 :: Block (BHeader C_Crypto) C
blockEx6 =
mkBlockFakeVRF
(bhHash $ bheader @(BHeader C_Crypto) blockEx5)
Cast.alicePoolKeys
[]
(SlotNo 295) -- odd slots open for decentralization
(BlockNo 6)
epoch2Nonce
(NatNonce 6)
minBound
14
14
(mkOCert Cast.alicePoolKeys 0 (KESPeriod 14))
expectedStEx6 :: ChainState C
expectedStEx6 =
C.evolveNonceFrozen (getBlockNonce blockEx6)
. C.newLab blockEx6
. C.setOCertCounter (coerceKeyRole $ aikColdKeyHash Cast.alicePoolKeys) 0
. C.incrBlockCount (aikColdKeyHash Cast.alicePoolKeys)
. C.rewardUpdate emptyRewardUpdate
$ expectedStEx5
-- === Block 6, Slot 295, Epoch 2
--
-- Alice's pool produces a second block.
twoPools6 :: CHAINExample (BHeader C_Crypto) C
twoPools6 = CHAINExample expectedStEx5 blockEx6 (Right expectedStEx6)
--
-- Block 7, Slot 297, Epoch 2
--
blockEx7 :: Block (BHeader C_Crypto) C
blockEx7 =
mkBlockFakeVRF
(bhHash $ bheader @(BHeader C_Crypto) blockEx6)
Cast.bobPoolKeys
[]
(SlotNo 297) -- odd slots open for decentralization
(BlockNo 7)
epoch2Nonce
(NatNonce 7)
minBound
14
14
(mkOCert Cast.bobPoolKeys 0 (KESPeriod 14))
expectedStEx7 :: ChainState C
expectedStEx7 =
C.evolveNonceFrozen (getBlockNonce blockEx7)
. C.newLab blockEx7
. C.setOCertCounter (coerceKeyRole $ aikColdKeyHash Cast.bobPoolKeys) 0
. C.incrBlockCount (aikColdKeyHash Cast.bobPoolKeys)
$ expectedStEx6
-- === Block 7, Slot 295, Epoch 2
--
-- Bob's pool produces a block.
twoPools7 :: CHAINExample (BHeader C_Crypto) C
twoPools7 = CHAINExample expectedStEx6 blockEx7 (Right expectedStEx7)
--
-- Block 8, Slot 310, Epoch 3
--
epoch3Nonce :: Nonce
epoch3Nonce =
chainCandidateNonce expectedStEx7
⭒ hashHeaderToNonce (bhHash $ bheader blockEx4)
blockEx8 :: Block (BHeader C_Crypto) C
blockEx8 =
mkBlockFakeVRF
(bhHash $ bheader @(BHeader C_Crypto) blockEx7)
(coreNodeKeysBySchedule @C ppEx 310)
[]
(SlotNo 310)
(BlockNo 8)
epoch3Nonce
(NatNonce 8)
minBound
15
15
(mkOCert (coreNodeKeysBySchedule @C ppEx 310) 1 (KESPeriod 15))
expectedStEx8 :: ChainState C
expectedStEx8 =
C.newEpoch blockEx8
. C.newSnapshot snapEx3 (Coin 0)
. C.setOCertCounter coreNodeHK 1
. C.applyRewardUpdate emptyRewardUpdate
$ expectedStEx7
where
coreNodeHK = coerceKeyRole . aikColdKeyHash $ coreNodeKeysBySchedule @C ppEx 310
-- === Block 8, Slot 310, Epoch 3
--
-- Create an empty block to start epoch 3.
twoPools8 :: CHAINExample (BHeader C_Crypto) C
twoPools8 = CHAINExample expectedStEx7 blockEx8 (Right expectedStEx8)
--
-- Block 9, Slot 390, Epoch 3
--
blockEx9 :: Block (BHeader C_Crypto) C
blockEx9 =
mkBlockFakeVRF
(bhHash $ bheader @(BHeader C_Crypto) blockEx8)
(coreNodeKeysBySchedule @C ppEx 390)
[]
(SlotNo 390)
(BlockNo 9)
epoch3Nonce
(NatNonce 9)
minBound
19
19
(mkOCert (coreNodeKeysBySchedule @C ppEx 390) 2 (KESPeriod 19))
blocksMadeEpoch3 :: Integer
blocksMadeEpoch3 = 3
expectedBlocks :: Integer
expectedBlocks =
floor $
0.5
* unboundRational (activeSlotVal $ activeSlotCoeff testGlobals)
* fromIntegral (epochSize $ EpochNo 3)
reserves9 :: Coin
reserves9 = maxLLSupply <+> deltaREx4 <-> (aliceInitCoin <+> bobInitCoin <+> carlInitCoin)
deltaR1Ex9 :: Coin
deltaR1Ex9 =
rationalToCoinViaFloor $
(blocksMadeEpoch3 % expectedBlocks)
* 0.0021
* (fromIntegral . unCoin $ reserves9)
rPot :: Integer
rPot = unCoin deltaR1Ex9 -- There were no fees
deltaTEx9 :: Integer
deltaTEx9 = floor $ (0.2 :: Double) * fromIntegral rPot
bigR :: Coin
bigR = Coin $ rPot - deltaTEx9
circulation :: Integer
circulation = unCoin $ maxLLSupply <-> reserves9
aliceStakeShareTot :: Rational
aliceStakeShareTot = (unCoin aliceCoinEx1 + unCoin carlInitCoin) % circulation
bobStakeShareTot :: Rational
bobStakeShareTot = unCoin bobInitCoin % circulation
alicePoolRewards :: forall c. ExMock c => Coin
alicePoolRewards = rationalToCoinViaFloor (appPerf * (fromIntegral . unCoin $ maxP))
where
appPerf = mkApparentPerformance (ppEx @(ShelleyEra c) ^. ppDL) alicePoolStake 2 3
pledge = fromIntegral . unCoin . ppPledge $ alicePoolParams' @c
pr = pledge % circulation
maxP = EB.maxPool @(ShelleyEra c) ppEx bigR aliceStakeShareTot pr
carlMemberRewardsFromAlice :: forall c. ExMock c => Coin
carlMemberRewardsFromAlice =
memberRew
(alicePoolRewards @c)
(alicePoolParams' @c)
(StakeShare $ unCoin carlInitCoin % circulation)
(StakeShare aliceStakeShareTot)
carlLeaderRewardsFromAlice :: forall c. ExMock c => Coin
carlLeaderRewardsFromAlice =
leaderRew
(alicePoolRewards @c)
(alicePoolParams' @c)
(StakeShare $ unCoin aliceCoinEx1 % circulation)
(StakeShare aliceStakeShareTot)
bobPoolRewards :: forall c. ExMock c => Coin
bobPoolRewards = rationalToCoinViaFloor (appPerf * (fromIntegral . unCoin $ maxP))
where
appPerf = mkApparentPerformance (ppEx @(ShelleyEra c) ^. ppDL) bobPoolStake 1 3
pledge = fromIntegral . unCoin . ppPledge $ bobPoolParams' @c
pr = pledge % circulation
maxP = EB.maxPool @(ShelleyEra c) ppEx bigR bobStakeShareTot pr
carlLeaderRewardsFromBob :: forall c. ExMock c => Coin
carlLeaderRewardsFromBob =
leaderRew
(bobPoolRewards @c)
(bobPoolParams' @c)
(StakeShare $ unCoin bobInitCoin % circulation)
(StakeShare bobStakeShareTot)
alicePerfEx9 :: Likelihood
alicePerfEx9 = likelihood blocks t (epochSize $ EpochNo 3)
where
blocks = 2
t = leaderProbability f alicePoolStake (unsafeBoundRational 0.5)
f = activeSlotCoeff testGlobals
bobPerfEx9 :: Likelihood
bobPerfEx9 = likelihood blocks t (epochSize $ EpochNo 3)
where
blocks = 1
t = leaderProbability f bobPoolStake (unsafeBoundRational 0.5)
f = activeSlotCoeff testGlobals
nonMyopicEx9 :: forall c. ExMock c => NonMyopic c
nonMyopicEx9 =
NonMyopic
( Map.fromList
[ (aikColdKeyHash Cast.alicePoolKeys, alicePerfEx9)
, (aikColdKeyHash Cast.bobPoolKeys, bobPerfEx9)
]
)
bigR
rewardUpdateEx9 ::
PParams C ->
Map (Credential 'Staking C_Crypto) (Set (Reward C_Crypto)) ->
RewardUpdate C_Crypto
rewardUpdateEx9 pp rewards =
RewardUpdate
{ deltaT = DeltaCoin deltaTEx9
, deltaR = invert (toDeltaCoin deltaR1Ex9) <> toDeltaCoin deltaR2Ex9
, rs = rewards
, deltaF = DeltaCoin 0
, nonMyopic = nonMyopicEx9
}
where
pv = pp ^. ppProtocolVersionL
deltaR2Ex9 = bigR <-> sumRewards pv rewards
pulserEx9 :: PParams C -> PulsingRewUpdate (EraCrypto C)
pulserEx9 pp =
makeCompletedPulser
( BlocksMade $
Map.fromList
[(aikColdKeyHash Cast.alicePoolKeys, 2), (aikColdKeyHash Cast.bobPoolKeys, 1)]
)
expectedStEx8'
where
expectedStEx8' = C.setPrevPParams pp expectedStEx8
expectedStEx9 :: PParams C -> ChainState C
expectedStEx9 pp =
C.evolveNonceFrozen (getBlockNonce blockEx9)
. C.newLab blockEx9
. C.setOCertCounter coreNodeHK 2
. C.pulserUpdate (pulserEx9 pp)
$ expectedStEx8
where
coreNodeHK = coerceKeyRole . aikColdKeyHash $ coreNodeKeysBySchedule @C ppEx 390
-- === Block 9, Slot 390, Epoch 3
--
-- Create the first non-trivial reward update. The rewards demonstrate the
-- results of the delegation scenario that was constructed in the first and only transaction.
twoPools9 :: CHAINExample (BHeader (EraCrypto C)) C
twoPools9 = CHAINExample expectedStEx8 blockEx9 (Right $ expectedStEx9 ppEx)
--
-- Now test with Aggregation
--
carlsRewards :: forall c. ExMock c => Set (Reward c)
carlsRewards =
Set.fromList
[ Reward MemberReward (aikColdKeyHash Cast.alicePoolKeys) (carlMemberRewardsFromAlice @c)
, Reward LeaderReward (aikColdKeyHash Cast.alicePoolKeys) (carlLeaderRewardsFromAlice @c)
, Reward LeaderReward (aikColdKeyHash Cast.bobPoolKeys) (carlLeaderRewardsFromBob @c)
]
rsEx9Agg :: forall c. ExMock c => Map (Credential 'Staking c) (Set (Reward c))
rsEx9Agg = Map.singleton Cast.carlSHK carlsRewards
ppProtVer3 :: PParams C
ppProtVer3 = ppEx & ppProtocolVersionL .~ ProtVer (natVersion @3) 0
expectedStEx8Agg :: ChainState C
expectedStEx8Agg = C.setPrevPParams ppProtVer3 expectedStEx8
expectedStEx9Agg :: ChainState C
expectedStEx9Agg = C.setPrevPParams ppProtVer3 (expectedStEx9 ppProtVer3)
-- Create the first non-trivial reward update. The rewards demonstrate the
-- results of the delegation scenario that was constructed in the first and only transaction.
twoPools9Agg :: CHAINExample (BHeader C_Crypto) C
twoPools9Agg = CHAINExample expectedStEx8Agg blockEx9 (Right expectedStEx9Agg)
testAggregateRewardsLegacy :: HasCallStack => Assertion
testAggregateRewardsLegacy = do
let expectedReward = carlLeaderRewardsFromBob @(EraCrypto C)
expectedReward @?= rewardAmount (minimum (carlsRewards @(EraCrypto C)))
aggregateRewards @C_Crypto (ppEx ^. ppProtocolVersionL @C) rsEx9Agg
@?= Map.singleton Cast.carlSHK expectedReward
testAggregateRewardsNew :: Assertion
testAggregateRewardsNew =
aggregateRewards @C_Crypto (ppProtVer3 ^. ppProtocolVersionL @C) rsEx9Agg
@?= Map.singleton Cast.carlSHK (foldMap rewardAmount (carlsRewards @(EraCrypto C)))
--
-- Two Pools Test Group
--
twoPoolsExample :: TestTree
twoPoolsExample =
testGroup
"two pools"
[ testCase "create non-aggregated pulser" $ testCHAINExample twoPools9
, testCase "non-aggregated pulser is correct" $
Complete (rewardUpdateEx9 ppEx rsEx9Agg)
@?= (fst . runShelleyBase . completeStep $ pulserEx9 ppEx)
, testCase "aggregated pulser is correct" $
Complete (rewardUpdateEx9 ppProtVer3 rsEx9Agg)
@?= (fst . runShelleyBase . completeStep $ pulserEx9 ppProtVer3)
, testCase "create aggregated pulser" $ testCHAINExample twoPools9Agg
, testCase "create legacy aggregatedRewards" testAggregateRewardsLegacy
, testCase "create new aggregatedRewards" testAggregateRewardsNew
]
-- This test group tests each block individually, which is really only
-- helpful for debugging purposes.
twoPoolsExampleExtended :: TestTree
twoPoolsExampleExtended =
testGroup
"two pools extended"
[ testCase "initial registrations" $ testCHAINExample twoPools1
, testCase "delegate stake and create reward update" $ testCHAINExample twoPools2
, testCase "new epoch changes" $ testCHAINExample twoPools3
, testCase "second reward update" $ testCHAINExample twoPools4
, testCase "nonempty pool distr" $ testCHAINExample twoPools5
, testCase "alice produces a block" $ testCHAINExample twoPools6
, testCase "bob produces a block" $ testCHAINExample twoPools7
, testCase "prelude to the first nontrivial rewards" $ testCHAINExample twoPools8
, testCase "create non-aggregated rewards" $ testCHAINExample twoPools9
, testCase "create aggregated rewards" $ testCHAINExample twoPools9Agg
]