Skip to content

Commit

Permalink
[ADP-3229] Move Slotting module to primitive lib (#4251)
Browse files Browse the repository at this point in the history
- [x] Move the Slotting module without Write dependency to primitive lib
- [x] Move SlottingSpec to primitive lib

This code migration introduce unwanted deps in the primitive lib that
will be tackled in
[ticket](https://cardanofoundation.atlassian.net/browse/ADP-3233)

ADP-3229
  • Loading branch information
paolino authored Nov 21, 2023
2 parents 2d421d5 + 28ffbd8 commit 442ef42
Show file tree
Hide file tree
Showing 14 changed files with 377 additions and 228 deletions.
14 changes: 14 additions & 0 deletions lib/primitive/cardano-wallet-primitive.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ library
, cardano-wallet-test-utils
, cborg
, commutative-semigroups
, contra-tracer
, containers
, cryptonite
, delta-types
Expand All @@ -67,6 +68,7 @@ library
, generics-sop
, hashable
, int-cast
, iohk-monitoring
, lattices
, lens
, memory
Expand Down Expand Up @@ -95,6 +97,8 @@ library
Cardano.Wallet.Orphans
Cardano.Wallet.Primitive.Collateral
Cardano.Wallet.Primitive.NetworkId
Cardano.Wallet.Primitive.Slotting
Cardano.Wallet.Primitive.Slotting.Legacy
Cardano.Wallet.Primitive.Types.Address
Cardano.Wallet.Primitive.Types.Address.Gen
Cardano.Wallet.Primitive.Types.Block
Expand All @@ -112,9 +116,12 @@ library
Cardano.Wallet.Primitive.Types.PoolId
Cardano.Wallet.Primitive.Types.ProtocolMagic
Cardano.Wallet.Primitive.Types.ProtocolParameters
Cardano.Wallet.Primitive.Types.Range
Cardano.Wallet.Primitive.Types.RewardAccount
Cardano.Wallet.Primitive.Types.RewardAccount.Gen
Cardano.Wallet.Primitive.Types.SlotId
Cardano.Wallet.Primitive.Types.SlottingParameters
Cardano.Wallet.Primitive.Types.SlottingParameters.Gen
Cardano.Wallet.Primitive.Types.StakePoolSummary
Cardano.Wallet.Primitive.Types.TokenBundle
Cardano.Wallet.Primitive.Types.TokenBundle.Gen
Expand Down Expand Up @@ -169,12 +176,15 @@ test-suite test
, delta-types
, filepath
, fmt
, generic-arbitrary
, generic-lens
, hedgehog-quickcheck
, hspec
, hspec-core
, iohk-monitoring
, lattices
, MonadRandom
, ouroboros-consensus
, QuickCheck
, quickcheck-classes
, quickcheck-instances
Expand All @@ -184,10 +194,14 @@ test-suite test
, string-qq
, text
, text-class
, time
, transformers
, unliftio
, with-utf8
build-tool-depends: hspec-discover:hspec-discover
other-modules:
Cardano.Wallet.Primitive.CollateralSpec
Cardano.Wallet.Primitive.SlottingSpec
Cardano.Wallet.Primitive.Types.AddressSpec
Cardano.Wallet.Primitive.Types.BlockSummarySpec
Cardano.Wallet.Primitive.Types.CoinSpec
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingStrategies #-}
{-# LANGUAGE ExistentialQuantification #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GADTs #-}
Expand Down Expand Up @@ -30,6 +32,7 @@ module Cardano.Wallet.Primitive.Slotting
, ceilingSlotAt
, timeOfEpoch
, getStartTime
, StartTime (..)

-- ** Blockchain-relative times
, RelativeTime
Expand Down Expand Up @@ -58,9 +61,6 @@ module Cardano.Wallet.Primitive.Slotting
, EpochInfo
, toEpochInfo

-- ** TimeTranslation
, toTimeTranslation

-- ** Combinators for running queries
, unsafeExtendSafeZone
, neverFails
Expand All @@ -79,20 +79,29 @@ import Cardano.BM.Data.Tracer
)
import Cardano.Slotting.EpochInfo.API
( EpochInfo
, hoistEpochInfo
)
import Cardano.Slotting.Slot
( SlotNo
)
import Cardano.Wallet.Orphans
()
import Cardano.Wallet.Primitive.Types
( EpochLength (..)
, EpochNo (..)
, Range (..)
, SlotId (..)
import Cardano.Wallet.Primitive.Types.EpochNo
( EpochNo (..)
)
import Cardano.Wallet.Primitive.Types.Range
( Range (..)
)
import Cardano.Wallet.Primitive.Types.SlotId
( SlotId (..)
, SlotInEpoch (..)
)
import Cardano.Wallet.Primitive.Types.SlottingParameters
( EpochLength (..)
, SlotLength (..)
, SlotNo (..)
, SlottingParameters (..)
, StartTime (..)
, SlottingParameters
)
import Control.DeepSeq
( NFData
)
import Control.Monad
( ap
Expand All @@ -109,7 +118,6 @@ import Control.Monad.Trans.Class
)
import Control.Monad.Trans.Except
( ExceptT (..)
, runExcept
, runExceptT
)
import Control.Tracer
Expand Down Expand Up @@ -156,16 +164,15 @@ import Fmt
, fmt
, indentF
)
import GHC.Generics
( Generic
)
import GHC.Stack
( CallStack
, HasCallStack
, getCallStack
, prettySrcLoc
)
import Internal.Cardano.Write.Tx.TimeTranslation
( TimeTranslation
, timeTranslationFromEpochInfo
)
import Ouroboros.Consensus.BlockchainTime.WallClock.Types
( RelativeTime (..)
, SystemStart (SystemStart)
Expand Down Expand Up @@ -202,6 +209,12 @@ import qualified Ouroboros.Consensus.HardFork.History.Summary as HF
Queries
-------------------------------------------------------------------------------}

-- | Blockchain start time
newtype StartTime = StartTime {utcTimeOfStartTime :: UTCTime}
deriving (Show, Eq, Ord, Generic)

instance NFData StartTime

-- | A query for time, slot and epoch conversions. Can be interpreted using
-- @interpretQuery@.
--
Expand Down Expand Up @@ -454,14 +467,6 @@ getSystemStart :: TimeInterpreter m -> SystemStart
getSystemStart TimeInterpreter{blockchainStartTime} =
let (StartTime t) = blockchainStartTime in SystemStart t

toTimeTranslation
:: TimeInterpreter (ExceptT PastHorizonException IO)
-> IO TimeTranslation
toTimeTranslation timeInterpreter = do
info <- runExceptT (toEpochInfo timeInterpreter)
>>= either throwIO (pure . hoistEpochInfo runExcept)
pure $ timeTranslationFromEpochInfo (getSystemStart timeInterpreter) info

data TimeInterpreterLog
= MsgInterpreterPastHorizon
(Maybe String) -- ^ Reason for why the failure should be impossible
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

-- | Legacy slotting API functions.
-- Used as a reference in tests.

module Cardano.Wallet.Primitive.Slotting.Legacy
( SlotParameters (..)
, slotParams
Expand All @@ -27,16 +26,24 @@ module Cardano.Wallet.Primitive.Slotting.Legacy

import Prelude

import Cardano.Wallet.Primitive.Types
import Cardano.Wallet.Primitive.Slotting
( StartTime (StartTime)
)
import Cardano.Wallet.Primitive.Types.EpochNo
( EpochNo (..)
)
import Cardano.Wallet.Primitive.Types.Range
( Range (Range)
)
import Cardano.Wallet.Primitive.Types.SlotId
( SlotId (..)
, SlotInEpoch (SlotInEpoch)
)
import Cardano.Wallet.Primitive.Types.SlottingParameters
( ActiveSlotCoefficient
, EpochLength (..)
, EpochNo (..)
, Range (..)
, SlotId (..)
, SlotInEpoch (..)
, SlotLength (..)
, SlottingParameters (..)
, StartTime (..)
, EpochLength (EpochLength)
, SlotLength (SlotLength)
, SlottingParameters
)
import Data.Generics.Internal.VL.Lens
( (^.)
Expand Down Expand Up @@ -80,14 +87,16 @@ data SlotParameters = SlotParameters
:: StartTime
, getActiveSlotCoefficient
:: ActiveSlotCoefficient
} deriving (Eq, Generic, Show)
}
deriving (Eq, Generic, Show)

slotParams :: StartTime -> SlottingParameters -> SlotParameters
slotParams t0 sp = SlotParameters
(sp ^. #getEpochLength)
(sp ^. #getSlotLength)
t0
(sp ^. #getActiveSlotCoefficient)
slotParams t0 sp =
SlotParameters
(sp ^. #getEpochLength)
(sp ^. #getSlotLength)
t0
(sp ^. #getActiveSlotCoefficient)

-- | Calculate the time at which an epoch begins.
epochStartTime :: SlotParameters -> EpochNo -> UTCTime
Expand Down Expand Up @@ -143,19 +152,21 @@ slotMinBound = SlotId 0 0
slotAt' :: SlotParameters -> UTCTime -> Maybe SlotId
slotAt' (SlotParameters (EpochLength el) (SlotLength sl) (StartTime st) _) t
| t < st = Nothing
| otherwise = Just $ SlotId {epochNumber, slotNumber}
| otherwise = Just $ SlotId{epochNumber, slotNumber}
where
diff :: NominalDiffTime
diff = t `diffUTCTime` st

epochLength :: NominalDiffTime
epochLength = fromIntegral el * sl

epochNumber = EpochNo $
floor (diff / epochLength)
epochNumber =
EpochNo
$ floor (diff / epochLength)

slotNumber = SlotInEpoch $
floor ((diff - fromIntegral (unEpochNo epochNumber) * epochLength) / sl)
slotNumber =
SlotInEpoch
$ floor ((diff - fromIntegral (unEpochNo epochNumber) * epochLength) / sl)

-- | Transforms the given inclusive time range into an inclusive slot range.
--
Expand All @@ -164,7 +175,6 @@ slotAt' (SlotParameters (EpochLength el) (SlotLength sl) (StartTime st) _) t
--
-- If, on the other hand, the specified time range terminates before the start
-- of the blockchain, this function returns 'Nothing'.
--
slotRangeFromTimeRange'
:: SlotParameters
-> Range UTCTime
Expand All @@ -186,21 +196,21 @@ flatSlot (EpochLength epochLength) (SlotId (EpochNo e) (SlotInEpoch s)) =
--
-- This function will fail if applied to a value that is higher than the maximum
-- value of 'flatSlot' for the specified 'EpochLength'.
--
fromFlatSlot :: EpochLength -> Word64 -> SlotId
fromFlatSlot el@(EpochLength epochLength) n
| n <= maxFlatSlot =
SlotId (EpochNo $ fromIntegral e) (fromIntegral s)
| otherwise =
error $ mconcat
[ "fromFlatSlot: The specified flat slot number ("
, show n
, ") is higher than the maximum flat slot number ("
, show maxFlatSlot
, ") for the specified epoch length ("
, show epochLength
, ")."
]
error
$ mconcat
[ "fromFlatSlot: The specified flat slot number ("
, show n
, ") is higher than the maximum flat slot number ("
, show maxFlatSlot
, ") for the specified epoch length ("
, show epochLength
, ")."
]
where
e = n `div` fromIntegral epochLength
s = n `mod` fromIntegral epochLength
Expand Down
Loading

0 comments on commit 442ef42

Please sign in to comment.