-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Provide slot-to-time conversion to ledger #3036
Merged
iohk-bors
merged 1 commit into
master
from
nfrisby/CAD-2713-time-conversions-in-EpochInfo
Apr 27, 2021
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
{-# LANGUAGE DataKinds #-} | ||
{-# LANGUAGE DeriveGeneric #-} | ||
{-# LANGUAGE DerivingVia #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
|
||
|
@@ -26,49 +25,16 @@ module Ouroboros.Consensus.BlockchainTime.WallClock.Types ( | |
, SlotLength | ||
) where | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Most of this module's content was recently upstreamed to https://github.com/input-output-hk/cardano-base/blob/master/slotting/src/Cardano/Slotting/Time.hs |
||
import Codec.Serialise | ||
import Control.Exception (assert) | ||
import Data.Fixed | ||
import Data.Time (NominalDiffTime, UTCTime, addUTCTime, diffUTCTime) | ||
import GHC.Generics (Generic) | ||
import NoThunks.Class (InspectHeap (..), NoThunks, | ||
OnlyCheckWhnfNamed (..)) | ||
import Quiet | ||
import Data.Time.Clock (NominalDiffTime) | ||
import NoThunks.Class (NoThunks, OnlyCheckWhnfNamed (..)) | ||
|
||
{------------------------------------------------------------------------------- | ||
System start | ||
-------------------------------------------------------------------------------} | ||
|
||
-- | System start | ||
-- | ||
-- Slots are counted from the system start. | ||
newtype SystemStart = SystemStart { getSystemStart :: UTCTime } | ||
deriving (Eq, Generic) | ||
deriving NoThunks via InspectHeap SystemStart | ||
deriving Show via Quiet SystemStart | ||
|
||
{------------------------------------------------------------------------------- | ||
Relative time | ||
-------------------------------------------------------------------------------} | ||
|
||
-- | 'RelativeTime' is time relative to the 'SystemStart' | ||
newtype RelativeTime = RelativeTime { getRelativeTime :: NominalDiffTime } | ||
deriving stock (Eq, Ord, Generic) | ||
deriving newtype (NoThunks) | ||
deriving Show via Quiet RelativeTime | ||
import Cardano.Slotting.Time | ||
|
||
addRelTime :: NominalDiffTime -> RelativeTime -> RelativeTime | ||
addRelTime delta (RelativeTime t) = RelativeTime (t + delta) | ||
addRelTime = addRelativeTime | ||
|
||
diffRelTime :: RelativeTime -> RelativeTime -> NominalDiffTime | ||
diffRelTime (RelativeTime t) (RelativeTime t') = t - t' | ||
|
||
toRelativeTime :: SystemStart -> UTCTime -> RelativeTime | ||
toRelativeTime (SystemStart t) t' = assert (t' >= t) $ | ||
RelativeTime (diffUTCTime t' t) | ||
|
||
fromRelativeTime :: SystemStart -> RelativeTime -> UTCTime | ||
fromRelativeTime (SystemStart t) (RelativeTime t') = addUTCTime t' t | ||
diffRelTime = diffRelativeTime | ||
|
||
{------------------------------------------------------------------------------- | ||
Get current time (as RelativeTime) | ||
|
@@ -92,61 +58,3 @@ data SystemTime m = SystemTime { | |
, systemTimeWait :: m () | ||
} | ||
deriving NoThunks via OnlyCheckWhnfNamed "SystemTime" (SystemTime m) | ||
|
||
{------------------------------------------------------------------------------- | ||
SlotLength | ||
-------------------------------------------------------------------------------} | ||
|
||
-- | Slot length | ||
newtype SlotLength = SlotLength { getSlotLength :: NominalDiffTime } | ||
deriving (Eq, Generic, NoThunks) | ||
deriving Show via Quiet SlotLength | ||
|
||
-- | Constructor for 'SlotLength' | ||
mkSlotLength :: NominalDiffTime -> SlotLength | ||
mkSlotLength = SlotLength | ||
|
||
slotLengthFromSec :: Integer -> SlotLength | ||
slotLengthFromSec = slotLengthFromMillisec . (* 1000) | ||
|
||
slotLengthToSec :: SlotLength -> Integer | ||
slotLengthToSec = (`div` 1000) . slotLengthToMillisec | ||
|
||
slotLengthFromMillisec :: Integer -> SlotLength | ||
slotLengthFromMillisec = mkSlotLength . conv | ||
where | ||
-- Explicit type annotation here means that /if/ we change the precision, | ||
-- we are forced to reconsider this code. | ||
conv :: Integer -> NominalDiffTime | ||
conv = (realToFrac :: Pico -> NominalDiffTime) | ||
. (/ 1000) | ||
. (fromInteger :: Integer -> Pico) | ||
|
||
slotLengthToMillisec :: SlotLength -> Integer | ||
slotLengthToMillisec = conv . getSlotLength | ||
where | ||
-- Explicit type annotation here means that /if/ we change the precision, | ||
-- we are forced to reconsider this code. | ||
conv :: NominalDiffTime -> Integer | ||
conv = truncate | ||
. (* 1000) | ||
. (realToFrac :: NominalDiffTime -> Pico) | ||
|
||
{------------------------------------------------------------------------------- | ||
Serialisation | ||
-------------------------------------------------------------------------------} | ||
|
||
instance Serialise RelativeTime where | ||
encode = encode . toPico . getRelativeTime | ||
where | ||
toPico :: NominalDiffTime -> Pico | ||
toPico = realToFrac | ||
|
||
decode = (RelativeTime . fromPico) <$> decode | ||
where | ||
fromPico :: Pico -> NominalDiffTime | ||
fromPico = realToFrac | ||
|
||
instance Serialise SlotLength where | ||
encode = encode . slotLengthToMillisec | ||
decode = slotLengthFromMillisec <$> decode |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As John Ky noted elsewhere, we no longer need this since we're only on GHC 8.10 now.