Skip to content

Commit e723971

Browse files
authored
Merge pull request #5516 from IntersectMBO/lehins/improve-snapshot-performance
Improve snapshot creation performance
2 parents 95c352c + 22b039e commit e723971

File tree

58 files changed

+1076
-582
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+1076
-582
lines changed

eras/conway/impl/src/Cardano/Ledger/Conway/Governance/DRepPulser.hs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ import Cardano.Ledger.BaseTypes (
4242
Globals (..),
4343
KeyValuePairs (..),
4444
ToKeyValuePairs (..),
45+
unNonZero,
46+
unsafeNonZero,
4547
)
4648
import Cardano.Ledger.Binary (
4749
DecCBOR (..),
@@ -230,7 +232,7 @@ computeDRepDistr instantStake regDReps proposalDeposits poolDistr dRepDistr =
230232
pure $
231233
distr
232234
& poolDistrDistrL %~ Map.insert stakePool (ips & individualTotalPoolStakeL <>~ proposalDeposit)
233-
& poolDistrTotalL <>~ proposalDeposit
235+
& poolDistrTotalL %~ \t -> unsafeNonZero (unNonZero t <> fromCompact proposalDeposit)
234236
addToDRepDistr accountState stakeAndDeposits distr = fromMaybe distr $ do
235237
dRep <- accountState ^. dRepDelegationAccountStateL
236238
let

eras/conway/impl/src/Cardano/Ledger/Conway/Governance/Internal.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ instance (Default (InstantStake era), Default (Accounts era)) => Default (Ratify
600600
def =
601601
RatifyEnv
602602
def
603-
(PoolDistr Map.empty mempty)
603+
def
604604
Map.empty
605605
Map.empty
606606
(EpochNo 0)

eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Ledger.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ conwayLedgerTransitionTRC
496496
-- so it can process refunds of deposits for deregistering
497497
-- stake credentials and DReps. The modified CertState
498498
-- (certStateAfterCERTS) has these already removed from its
499-
-- UMap.
499+
-- AccountState.
500500
( UtxoEnv @era slot pp certState
501501
, utxoState'
502502
, tx

eras/conway/impl/src/Cardano/Ledger/Conway/Rules/NewEpoch.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ instance
127127
(BlocksMade Map.empty)
128128
def
129129
SNothing
130-
(PoolDistr Map.empty mempty)
130+
def
131131
def
132132
]
133133

eras/conway/impl/src/Cardano/Ledger/Conway/Rules/Ratify.hs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import Cardano.Ledger.BaseTypes (
3434
ShelleyBase,
3535
StrictMaybe (..),
3636
addEpochInterval,
37+
unNonZero,
3738
(%?),
3839
)
3940
import Cardano.Ledger.Coin (Coin (..), CompactForm (..))
@@ -195,7 +196,7 @@ spoAcceptedRatio ::
195196
ConwayEraAccounts era => RatifyEnv era -> GovActionState era -> ProtVer -> Rational
196197
spoAcceptedRatio
197198
RatifyEnv
198-
{ reStakePoolDistr = PoolDistr individualPoolStake (CompactCoin totalActiveStake)
199+
{ reStakePoolDistr = PoolDistr individualPoolStake totalActiveStake
199200
, reAccounts
200201
, reStakePools
201202
}
@@ -204,7 +205,7 @@ spoAcceptedRatio
204205
, gasProposalProcedure = ProposalProcedure {pProcGovAction}
205206
}
206207
pv =
207-
toInteger yesStake %? toInteger (totalActiveStake - abstainStake)
208+
toInteger yesStake %? (unCoin (unNonZero totalActiveStake) - toInteger abstainStake)
208209
where
209210
accumStake (!yes, !abstain) poolId distr =
210211
let CompactCoin stake = individualTotalPoolStake distr

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/DRepRatifySpec.hs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,4 @@ genPctsOf100 = do
308308
pure (a % s, b % s, c % s, d % s, e % s, f % s)
309309

310310
emptyRatifyEnv :: RatifyEnv ConwayEra
311-
emptyRatifyEnv =
312-
RatifyEnv
313-
mempty
314-
(PoolDistr Map.empty mempty)
315-
Map.empty
316-
Map.empty
317-
(EpochNo 0)
318-
(CommitteeState Map.empty)
319-
def
320-
Map.empty
311+
emptyRatifyEnv = def

eras/conway/impl/testlib/Test/Cardano/Ledger/Conway/SPORatifySpec.hs

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,8 @@
1010

1111
module Test.Cardano.Ledger.Conway.SPORatifySpec (spec) where
1212

13-
import Cardano.Ledger.BaseTypes (StrictMaybe (..))
14-
import Cardano.Ledger.Coin (Coin (..), CompactForm (..))
15-
import Cardano.Ledger.Compactible (Compactible (..))
13+
import Cardano.Ledger.BaseTypes (NonZero, StrictMaybe (..), unNonZero)
14+
import Cardano.Ledger.Coin (Coin (..), CompactForm (..), knownNonZeroCoin, toCoinNonZero)
1615
import Cardano.Ledger.Conway (hardforkConwayBootstrapPhase)
1716
import Cardano.Ledger.Conway.Core
1817
import Cardano.Ledger.Conway.Governance (
@@ -38,9 +37,9 @@ import Data.Default (def)
3837
import Data.Map.Strict (Map)
3938
import qualified Data.Map.Strict as Map
4039
import Data.MapExtras (fromKeys)
41-
import Data.Maybe (fromJust)
4240
import Data.Ratio ((%))
4341
import Lens.Micro
42+
import Numeric.Natural
4443
import Test.Cardano.Ledger.Common
4544
import Test.Cardano.Ledger.Conway.Era
4645

@@ -74,19 +73,24 @@ acceptedRatioProp = do
7473
gas {gasStakePoolVotes = votes}
7574
protVer
7675
expected =
77-
if fromCompact totalStake == stakeAbstain <+> stakeAlwaysAbstain
76+
if unNonZero totalStake == stakeAbstain <+> stakeAlwaysAbstain
7877
then 0
7978
else case gas ^. gasActionL of
80-
HardForkInitiation _ _ -> unCoin stakeYes % unCoin (fromCompact totalStake <-> stakeAbstain)
79+
HardForkInitiation _ _ -> unCoin stakeYes % unCoin (unNonZero totalStake <-> stakeAbstain)
8180
action
8281
| hardforkConwayBootstrapPhase protVer ->
8382
unCoin stakeYes
84-
% unCoin (fromCompact totalStake <-> stakeAbstain <-> stakeAlwaysAbstain <-> stakeNoConfidence)
83+
% unCoin
84+
( unNonZero totalStake
85+
<-> stakeAbstain
86+
<-> stakeAlwaysAbstain
87+
<-> stakeNoConfidence
88+
)
8589
| NoConfidence {} <- action ->
8690
unCoin (stakeYes <+> stakeNoConfidence)
87-
% unCoin (fromCompact totalStake <-> stakeAbstain <-> stakeAlwaysAbstain)
91+
% unCoin (unNonZero totalStake <-> stakeAbstain <-> stakeAlwaysAbstain)
8892
| otherwise ->
89-
unCoin stakeYes % unCoin (fromCompact totalStake <-> stakeAbstain <-> stakeAlwaysAbstain)
93+
unCoin stakeYes % unCoin (unNonZero totalStake <-> stakeAbstain <-> stakeAlwaysAbstain)
9094
actual `shouldBe` expected
9195
)
9296

@@ -95,7 +99,7 @@ noStakeProp =
9599
prop @((RatifyEnv era, RatifyState era, GovActionState era) -> IO ())
96100
"If there is no stake, accept iff threshold is zero"
97101
( \(re, rs, gas) ->
98-
let re' = re {reStakePoolDistr = PoolDistr Map.empty (fromJust . toCompact $ Coin 100)}
102+
let re' = re {reStakePoolDistr = PoolDistr Map.empty (knownNonZeroCoin @100)}
99103
in spoAccepted @era re' rs gas
100104
`shouldBe` (votingStakePoolThreshold @era rs (gasAction gas) == SJust minBound)
101105
)
@@ -139,24 +143,21 @@ noVotesProp =
139143
allYesProp :: forall era. ConwayEraTest era => Spec
140144
allYesProp =
141145
prop @((RatifyEnv era, RatifyState era, GovActionState era) -> Property)
142-
"If all vote yes, accepted ratio is 1 (unless there is no stake) "
143-
( \(re, rs, gas) ->
144-
forAll
145-
( genTestData @era
146-
(Ratios {yes = 100 % 100, no = 0, abstain = 0, alwaysAbstain = 0, noConfidence = 0})
147-
)
148-
( \TestData {..} ->
149-
let acceptedRatio =
150-
spoAcceptedRatio
151-
@era
152-
re {reStakePoolDistr = distr}
153-
gas {gasStakePoolVotes = votes}
154-
(rs ^. rsEnactStateL . ensProtVerL)
155-
in if fromCompact totalStake == Coin 0
156-
then acceptedRatio `shouldBe` 0
157-
else acceptedRatio `shouldBe` 1
158-
)
159-
)
146+
"If all vote yes, accepted ratio is 1"
147+
$ \(re, rs, gas) ->
148+
forAll
149+
( genTestData @era
150+
(Ratios {yes = 100 % 100, no = 0, abstain = 0, alwaysAbstain = 0, noConfidence = 0})
151+
)
152+
( \TestData {..} ->
153+
let acceptedRatio =
154+
spoAcceptedRatio
155+
@era
156+
re {reStakePoolDistr = distr}
157+
gas {gasStakePoolVotes = votes}
158+
(rs ^. rsEnactStateL . ensProtVerL)
159+
in acceptedRatio `shouldBe` 1
160+
)
160161

161162
noConfidenceProp :: forall era. ConwayEraTest era => Spec
162163
noConfidenceProp =
@@ -177,7 +178,7 @@ noConfidenceProp =
177178
data TestData era = TestData
178179
{ distr :: PoolDistr
179180
, votes :: Map (KeyHash StakePool) Vote
180-
, totalStake :: CompactForm Coin
181+
, totalStake :: NonZero Coin
181182
, stakeYes :: Coin
182183
, stakeNo :: Coin
183184
, stakeAbstain :: Coin
@@ -204,25 +205,25 @@ genTestData ::
204205
Ratios ->
205206
Gen (TestData era)
206207
genTestData Ratios {yes, no, abstain, alwaysAbstain, noConfidence} = do
207-
pools <- listOf (arbitrary @(KeyHash StakePool))
208-
let (poolsYes, poolsNo, poolsAbstain, poolsAlwaysAbstain, poolsNoConfidence, rest) =
209-
splitByPct yes no abstain alwaysAbstain noConfidence pools
210-
totalStake = length pools
208+
numPools :: NonZero Natural <- arbitrary
209+
pools <- vectorOf (fromIntegral (unNonZero numPools)) (arbitrary @(KeyHash StakePool))
210+
let
211+
totalStake = toCoinNonZero numPools
212+
(poolsYes, poolsNo, poolsAbstain, poolsAlwaysAbstain, poolsNoConfidence, rest) =
213+
splitByPct yes no abstain alwaysAbstain noConfidence pools
211214
distr <- do
212215
vrf <- arbitrary
213216
let
214-
indivStake = IndividualPoolStake (1 / toRational totalStake) (CompactCoin 1) vrf
215-
pure $
216-
PoolDistr
217-
( unionAllFromLists
218-
[ (poolsYes, indivStake)
219-
, (poolsNo, indivStake)
220-
, (poolsAbstain, indivStake)
221-
, (poolsAlwaysAbstain, indivStake)
222-
, (poolsNoConfidence, indivStake)
223-
]
224-
)
225-
(CompactCoin $ fromIntegral totalStake)
217+
indivStake = IndividualPoolStake (1 % unCoin (unNonZero totalStake)) (CompactCoin 1) vrf
218+
distr =
219+
unionAllFromLists
220+
[ (poolsYes, indivStake)
221+
, (poolsNo, indivStake)
222+
, (poolsAbstain, indivStake)
223+
, (poolsAlwaysAbstain, indivStake)
224+
, (poolsNoConfidence, indivStake)
225+
]
226+
pure $ PoolDistr distr totalStake
226227

227228
poolStateAA <- genPoolState poolsAlwaysAbstain
228229
poolStateNC <- genPoolState poolsNoConfidence

eras/shelley/impl/CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
## 1.18.0.0
44

5-
* Re-export `AccountAddress`, `AccountId`, and pattern synonym `RewardAccount` from `Cardano.Ledger.Address`
5+
* Change `updateNonMyopic`, `likelihoodsNM`, `rewLikelihoods` and `fvPoolRewardInfo` to use `VMap`
6+
* Add `calcNonMyopicMemberReward` and deprecate `nonMyopicMemberRew` in its favor.
7+
* Add `calcStakePoolDesirability` and deprecate `desirability` in its favor.
8+
* Change type signature of `mkPoolRewardInfo`, `getTopRankedPools`
9+
* Re-export `AccountAddress`, `AccountId`, and pattern synonym `RewardAccount` from `Cardano.Ledger.Shelley.API.Types`
610
* Move delegatee registration validation from `DELEGS` to `DELEG` rule:
711
- Add `DelegateeNotRegisteredDELEG` to `ShelleyDelegPredFailure`
812
- Remove `DelegateeNotRegisteredDELEG` from `ShelleyDelegsPredFailure`

eras/shelley/impl/src/Cardano/Ledger/Shelley/API/ByronTranslation.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ translateToShelleyLedgerStateFromUtxo transCtxt epochNo utxoByron =
107107
, nesBcur = BlocksMade Map.empty
108108
, nesEs = epochState
109109
, nesRu = SNothing
110-
, nesPd = PoolDistr Map.empty mempty
110+
, nesPd = def
111111
, -- At this point, we compute the stashed AVVM addresses, while we are able
112112
-- to do a linear scan of the UTxO, and stash them away for use at the
113113
-- Shelley/Allegra boundary.

0 commit comments

Comments
 (0)