33{-# LANGUAGE DataKinds #-}
44{-# LANGUAGE FlexibleContexts #-}
55{-# LANGUAGE GADTs #-}
6- {-# LANGUAGE OverloadedStrings #-}
76{-# LANGUAGE ScopedTypeVariables #-}
87{-# LANGUAGE TupleSections #-}
98{-# LANGUAGE TypeOperators #-}
@@ -46,12 +45,13 @@ import Prelude (id)
4645data StakeSliceRes
4746 = Slice ! StakeSlice ! Bool -- True if this is the final slice for this epoch. Can be used for logging.
4847 | NoSlices
48+ deriving (Show )
4949
5050data StakeSlice = StakeSlice
5151 { sliceEpochNo :: ! EpochNo
5252 , sliceDistr :: ! (Map StakeCred (Coin , PoolKeyHash ))
5353 }
54- deriving (Eq )
54+ deriving (Show , Eq )
5555
5656emptySlice :: EpochNo -> StakeSlice
5757emptySlice epoch = StakeSlice epoch Map. empty
@@ -95,11 +95,13 @@ genericStakeSlice ::
9595 LedgerState (ShelleyBlock p era ) ->
9696 Bool ->
9797 StakeSliceRes
98- genericStakeSlice pInfo epochBlockNo lstate isMigration
99- | index > delegationsLen = NoSlices
100- | index == delegationsLen = Slice (emptySlice epoch) True
101- | index + size > delegationsLen = Slice (mkSlice (delegationsLen - index)) True
102- | otherwise = Slice (mkSlice size) False
98+ genericStakeSlice pInfo epochBlockNo lstate isMigration = do
99+ case compare index delegationsLen of
100+ GT -> NoSlices
101+ EQ -> Slice (emptySlice epoch) True
102+ LT -> case compare (index + size) delegationsLen of
103+ GT -> Slice (mkSlice (delegationsLen - index)) True
104+ _otherwise -> Slice (mkSlice size) False
103105 where
104106 epoch :: EpochNo
105107 epoch = EpochNo $ 1 + unEpochNo (Shelley. nesEL (Consensus. shelleyLedgerState lstate))
@@ -149,10 +151,10 @@ genericStakeSlice pInfo epochBlockNo lstate isMigration
149151
150152 -- The starting index of the data in the delegation vector.
151153 index :: Word64
152- index
153- | isMigration = 0
154- | epochBlockNo < k = delegationsLen + 1 -- so it creates the empty Slice.
155- | otherwise = (epochBlockNo - k) * epochSliceSize
154+ index =
155+ if isMigration
156+ then 0
157+ else (epochBlockNo - k) * epochSliceSize
156158
157159 size :: Word64
158160 size
@@ -176,6 +178,121 @@ genericStakeSlice pInfo epochBlockNo lstate isMigration
176178 VMap. mapMaybe id $
177179 VMap. mapWithKey (\ a p -> (,p) <$> lookupStake a) delegationsSliced
178180
181+ -- genericStakeSlice ::
182+ -- forall era c blk p.
183+ -- (c ~ StandardCrypto, EraCrypto era ~ c, ConsensusProtocol (BlockProtocol blk)) =>
184+ -- Trace IO Text ->
185+ -- ProtocolInfo blk ->
186+ -- Word64 ->
187+ -- LedgerState (ShelleyBlock p era) ->
188+ -- Bool ->
189+ -- IO StakeSliceRes
190+ -- genericStakeSlice trce pInfo epochBlockNo lstate isMigration = do
191+ -- let shouldLog = unEpochNo epoch `elem` [12, 14]
192+ -- when shouldLog logStakeSliceInfo
193+ -- -- when shouldLog $ logStakeSliceInfo trce epochBlockNo delegationsLen index size k epochSliceSize isMigration epoch
194+ -- let result = case compare index delegationsLen of
195+ -- GT -> NoSlices
196+ -- EQ -> Slice (emptySlice epoch) True
197+ -- LT -> case compare (index + size) delegationsLen of
198+ -- GT -> Slice (mkSlice (delegationsLen - index)) True
199+ -- _other -> Slice (mkSlice size) False
200+
201+ -- -- when shouldLog $ logResult trce result
202+ -- pure result
203+ -- where
204+ -- index :: Word64
205+ -- index
206+ -- | isMigration = 0
207+ -- | epochBlockNo < k = 0 -- Changed from delegationsLen + 1
208+ -- | otherwise = min ((epochBlockNo - k) * epochSliceSize) delegationsLen
209+
210+ -- size :: Word64
211+ -- size
212+ -- | isMigration, epochBlockNo + 1 < k = 0
213+ -- | isMigration = (epochBlockNo + 1 - k) * epochSliceSize
214+ -- | otherwise = max 1 (min epochSliceSize delegationsLen) -- Ensure we always process at least one delegation
215+
216+ -- epochSliceSize :: Word64
217+ -- epochSliceSize =
218+ -- max minSliceSize (max 1 defaultEpochSliceSize) -- Ensure epochSliceSize is never 0
219+
220+ -- defaultEpochSliceSize :: Word64
221+ -- defaultEpochSliceSize = max 1 (1 + div (delegationsLen * 5) expectedBlocks) -- Ensure it's never 0
222+
223+ -- delegationsLen :: Word64
224+ -- delegationsLen = fromIntegral $ VG.length delegations
225+
226+ -- delegations :: VMap.KVVector VB VB (Credential 'Staking c, KeyHash 'StakePool c)
227+ -- delegations = VMap.unVMap $ Ledger.ssDelegations stakeSnapshot
228+
229+ -- epoch :: EpochNo
230+ -- epoch = EpochNo $ 1 + unEpochNo (Shelley.nesEL (Consensus.shelleyLedgerState lstate))
231+
232+ -- minSliceSize :: Word64
233+ -- minSliceSize = 2000
234+
235+ -- -- On mainnet this is 2160
236+ -- k :: Word64
237+ -- k = getSecurityParameter pInfo
238+
239+ -- -- We use 'ssStakeMark' here. That means that when these values
240+ -- -- are added to the database, the epoch number where they become active is the current
241+ -- -- epoch plus one.
242+ -- stakeSnapshot :: Ledger.SnapShot c
243+ -- stakeSnapshot =
244+ -- Ledger.ssStakeMark . Shelley.esSnapshots . Shelley.nesEs $
245+ -- Consensus.shelleyLedgerState lstate
246+
247+ -- stakes :: VMap VB VP (Credential 'Staking c) (Ledger.CompactForm Coin)
248+ -- stakes = Ledger.unStake $ Ledger.ssStake stakeSnapshot
249+
250+ -- lookupStake :: Credential 'Staking c -> Maybe Coin
251+ -- lookupStake cred = Ledger.fromCompact <$> VMap.lookup cred stakes
252+
253+ -- -- On mainnet this is 21600
254+ -- expectedBlocks :: Word64
255+ -- expectedBlocks = 10 * k
256+
257+ -- mkSlice :: Word64 -> StakeSlice
258+ -- mkSlice actualSize =
259+ -- StakeSlice
260+ -- { sliceEpochNo = epoch
261+ -- , sliceDistr = distribution
262+ -- }
263+ -- where
264+ -- delegationsSliced :: VMap VB VB (Credential 'Staking c) (KeyHash 'StakePool c)
265+ -- delegationsSliced = VMap $ VG.slice (fromIntegral index) (fromIntegral actualSize) delegations
266+
267+ -- distribution :: Map StakeCred (Coin, PoolKeyHash)
268+ -- distribution =
269+ -- VMap.toMap $
270+ -- VMap.mapMaybe id $
271+ -- VMap.mapWithKey (\a p -> (,p) <$> lookupStake a) delegationsSliced
272+
273+ -- logStakeSliceInfo = do
274+ -- logInfo trce $ unlines
275+ -- [ "Stake Slice Debug Info:"
276+ -- , " epoch: " <> (pack . show $ unEpochNo epoch)
277+ -- , " epochBlockNo: " <> show epochBlockNo
278+ -- , " isMigration: " <> show isMigration
279+ -- , " index: " <> show index
280+ -- , " delegationsLen: " <> show delegationsLen
281+ -- , " size: " <> show size
282+ -- , " epochSliceSize: " <> show epochSliceSize
283+ -- , " remaining: " <> show (delegationsLen - index)
284+ -- , " k: " <> show k
285+ -- , " expectedBlocks: " <> show expectedBlocks
286+ -- , " defaultEpochSliceSize: " <> show defaultEpochSliceSize
287+ -- ]
288+
289+ -- _logResult :: Trace IO Text -> StakeSliceRes -> IO ()
290+ -- _logResult trce result =
291+ -- logInfo trce $ unlines
292+ -- [ "Stake Slice Result:"
293+ -- , " " <> show result
294+ -- ]
295+
179296getPoolDistr ::
180297 ExtLedgerState CardanoBlock ->
181298 Maybe (Map PoolKeyHash (Coin , Word64 ), Map PoolKeyHash Natural )
0 commit comments