Skip to content

Commit

Permalink
epochSucc sucks
Browse files Browse the repository at this point in the history
  • Loading branch information
rvl committed Nov 27, 2020
1 parent 4a44023 commit 66f673c
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 71 deletions.
13 changes: 3 additions & 10 deletions lib/core/src/Cardano/Wallet/Api/Server.hs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,6 @@ import Cardano.Wallet.Primitive.Slotting
, TimeInterpreter
, currentEpoch
, endTimeOfEpoch
, epochSucc
, firstSlotInEpoch
, ongoingSlotAt
, startTime
Expand Down Expand Up @@ -384,7 +383,7 @@ import Data.List.NonEmpty
import Data.Map.Strict
( Map )
import Data.Maybe
( catMaybes, fromMaybe, isJust )
( catMaybes, isJust )
import Data.Proxy
( Proxy (..) )
import Data.Quantity
Expand Down Expand Up @@ -1801,8 +1800,8 @@ getNetworkInformation st nl = do
let curEpoch = tip ^. #slotId . #epochNumber . #getApiT
nextEpochStart <- lift $ ti $ endTimeOfEpoch curEpoch
let nextEpoch = ApiEpochInfo
(ApiT $ unsafeEpochSucc curEpoch)
(nextEpochStart)
(ApiT $ succ curEpoch)
nextEpochStart
return (tip, nextEpoch)
where
handlePastHorizonException
Expand All @@ -1811,12 +1810,6 @@ getNetworkInformation st nl = do
handlePastHorizonException _ =
MaybeT (pure Nothing)

-- Unsafe constructor for the next epoch. Chances to reach the last epoch
-- are quite unlikely in this context :)
unsafeEpochSucc :: HasCallStack => W.EpochNo -> W.EpochNo
unsafeEpochSucc = fromMaybe bomb . epochSucc
where bomb = error "reached final epoch of the Blockchain!?"

getNetworkParameters
:: (Block, NetworkParameters, SyncTolerance)
-> NetworkLayer IO t Block
Expand Down
16 changes: 0 additions & 16 deletions lib/core/src/Cardano/Wallet/Primitive/Slotting.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ module Cardano.Wallet.Primitive.Slotting

-- ** Helpers
, unsafeEpochNo
, epochPred
, epochSucc

-- * Legacy api - Inaccurate with cardano-node, okay with Jörmungandr
, SlotParameters (..)
Expand Down Expand Up @@ -379,20 +377,6 @@ slotParams t0 sp = SlotParameters
epochStartTime :: SlotParameters -> EpochNo -> UTCTime
epochStartTime sps e = slotStartTime sps $ SlotId e 0

-- | Return the epoch immediately before the given epoch, or 'Nothing' if there
-- is no representable epoch before the given epoch.
epochPred :: EpochNo -> Maybe EpochNo
epochPred (EpochNo e)
| e == minBound = Nothing
| otherwise = Just $ EpochNo $ pred e

-- | Return the epoch immediately after the given epoch, or 'Nothing' if there
-- is no representable epoch after the given epoch.
epochSucc :: EpochNo -> Maybe EpochNo
epochSucc (EpochNo e)
| e == maxBound = Nothing
| otherwise = Just $ EpochNo $ succ e

-- | Convert a 'SlotId' to the number of slots since genesis.
flatSlot :: EpochLength -> SlotId -> Word64
flatSlot (EpochLength epochLength) (SlotId (EpochNo e) (SlotInEpoch s)) =
Expand Down
41 changes: 0 additions & 41 deletions lib/core/test/unit/Cardano/Wallet/Primitive/TypesSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ import Cardano.Wallet.Primitive.AddressDerivation.Jormungandr
( JormungandrKey (..), generateKeyFromSeed )
import Cardano.Wallet.Primitive.Slotting
( SlotParameters (..)
, epochPred
, epochStartTime
, epochStartTime
, epochSucc
, epochSucc
, flatSlot
, fromFlatSlot
, slotAt'
Expand Down Expand Up @@ -544,43 +540,6 @@ spec = do
"time point during the lifetime of the blockchain"
True

describe "Epoch arithmetic: predecessors and successors" $ do

let succN n = applyN n (epochSucc =<<)
let predN n = applyN n (epochPred =<<)

it "epochPred minBound == Nothing" $
epochPred minBound === Nothing

it "epochSucc maxBound == Nothing" $
epochSucc maxBound === Nothing

it "(applyN n epochSucc) . (applyN n epochPred) == id" $
withMaxSuccess 1000 $ property $
\(Small epochWord) (Small n) ->
let epoch = EpochNo epochWord
withinBounds = minBound + n <= unEpochNo epoch
expectedResult =
if withinBounds then Just epoch else Nothing
in
checkCoverage $
cover 10 withinBounds "within bounds" $
cover 10 (not withinBounds) "out of bounds" $
expectedResult === succN n (predN n $ Just epoch)

it "(applyN n epochPred) . (applyN n epochSucc) == id" $
withMaxSuccess 1000 $ property $
\(Small epochWord) (Small n) ->
let epoch = EpochNo $ maxBound - epochWord
withinBounds = maxBound - n >= unEpochNo epoch
expectedResult =
if withinBounds then Just epoch else Nothing
in
checkCoverage $
cover 10 withinBounds "within bounds" $
cover 10 (not withinBounds) "out of bounds" $
expectedResult === predN n (succN n $ Just epoch)

describe "Slot arithmetic" $ do

it "slotFloor (slotStartTime slotMinBound) == Just slotMinBound" $
Expand Down
7 changes: 3 additions & 4 deletions lib/shelley/src/Cardano/Wallet/Shelley/Pools.hs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ import Cardano.Wallet.Primitive.Slotting
( PastHorizonException (..)
, TimeInterpreter
, epochOf
, epochPred
, firstSlotInEpoch
, startTime
)
Expand Down Expand Up @@ -122,7 +121,7 @@ import Control.Exception
, try
)
import Control.Monad
( forM, forM_, forever, void, when, (<=<) )
( forM, forM_, forever, void, when )
import Control.Monad.IO.Class
( liftIO )
import Control.Monad.Trans.Except
Expand Down Expand Up @@ -692,9 +691,9 @@ monitorStakePools tr gp nl DBLayer{..} =
-- #2196: We need the try. Arguably we shouldn't need it.
liftIO (try @SomeException (timeInterpreter nl (epochOf currentSlot))) >>= \case
Left _ -> return ()
Right currentEpoch | currentEpoch < 2 -> return ()
Right currentEpoch -> do
let subtractTwoEpochs = epochPred <=< epochPred
forM_ (subtractTwoEpochs currentEpoch) $ \latestRetirementEpoch -> do
let latestRetirementEpoch = currentEpoch - 2
latestGarbageCollectionEpoch <-
liftIO $ readIORef latestGarbageCollectionEpochRef
-- Only perform garbage collection once per epoch:
Expand Down

0 comments on commit 66f673c

Please sign in to comment.