{-# LANGUAGE OverloadedStrings #-}
module Cometa.SlotConfig
( SlotConfig,
mkSlotConfig,
zeroTime,
zeroSlot,
slotLength,
mainnet,
preview,
preprod,
slotToTime,
timeToSlot,
)
where
import Cometa.Error (Error (InvalidSlotConfig))
import Data.Word (Word64)
data SlotConfig = SlotConfig !Word64 !Word64 !Word64
deriving (SlotConfig -> SlotConfig -> Bool
(SlotConfig -> SlotConfig -> Bool)
-> (SlotConfig -> SlotConfig -> Bool) -> Eq SlotConfig
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: SlotConfig -> SlotConfig -> Bool
== :: SlotConfig -> SlotConfig -> Bool
$c/= :: SlotConfig -> SlotConfig -> Bool
/= :: SlotConfig -> SlotConfig -> Bool
Eq, Int -> SlotConfig -> ShowS
[SlotConfig] -> ShowS
SlotConfig -> String
(Int -> SlotConfig -> ShowS)
-> (SlotConfig -> String)
-> ([SlotConfig] -> ShowS)
-> Show SlotConfig
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> SlotConfig -> ShowS
showsPrec :: Int -> SlotConfig -> ShowS
$cshow :: SlotConfig -> String
show :: SlotConfig -> String
$cshowList :: [SlotConfig] -> ShowS
showList :: [SlotConfig] -> ShowS
Show)
mkSlotConfig ::
Word64 ->
Word64 ->
Word64 ->
Either Error SlotConfig
mkSlotConfig :: Word64 -> Word64 -> Word64 -> Either Error SlotConfig
mkSlotConfig Word64
_ Word64
_ Word64
0 = Error -> Either Error SlotConfig
forall a b. a -> Either a b
Left (Text -> Error
InvalidSlotConfig Text
"Slot duration must be positive")
mkSlotConfig Word64
time Word64
slot Word64
duration = SlotConfig -> Either Error SlotConfig
forall a b. b -> Either a b
Right (Word64 -> Word64 -> Word64 -> SlotConfig
SlotConfig Word64
time Word64
slot Word64
duration)
zeroTime :: SlotConfig -> Word64
zeroTime :: SlotConfig -> Word64
zeroTime (SlotConfig Word64
time Word64
_ Word64
_) = Word64
time
zeroSlot :: SlotConfig -> Word64
zeroSlot :: SlotConfig -> Word64
zeroSlot (SlotConfig Word64
_ Word64
slot Word64
_) = Word64
slot
slotLength :: SlotConfig -> Word64
slotLength :: SlotConfig -> Word64
slotLength (SlotConfig Word64
_ Word64
_ Word64
duration) = Word64
duration
mainnet :: SlotConfig
mainnet :: SlotConfig
mainnet = Word64 -> Word64 -> Word64 -> SlotConfig
SlotConfig Word64
1596059091000 Word64
4492800 Word64
1000
preview :: SlotConfig
preview :: SlotConfig
preview = Word64 -> Word64 -> Word64 -> SlotConfig
SlotConfig Word64
1666656000000 Word64
0 Word64
1000
preprod :: SlotConfig
preprod :: SlotConfig
preprod = Word64 -> Word64 -> Word64 -> SlotConfig
SlotConfig Word64
1655769600000 Word64
86400 Word64
1000
slotToTime :: SlotConfig -> Word64 -> Either Error Word64
slotToTime :: SlotConfig -> Word64 -> Either Error Word64
slotToTime SlotConfig
config Word64
slot
| Word64
slot Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< SlotConfig -> Word64
zeroSlot SlotConfig
config = Error -> Either Error Word64
forall a b. a -> Either a b
Left (Text -> Error
InvalidSlotConfig Text
"Slot precedes the reference slot")
| Bool
otherwise = Integer -> Either Error Word64
bounded (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (SlotConfig -> Word64
zeroTime SlotConfig
config) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
slot Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (SlotConfig -> Word64
zeroSlot SlotConfig
config)) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
* Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (SlotConfig -> Word64
slotLength SlotConfig
config))
timeToSlot :: SlotConfig -> Word64 -> Either Error Word64
timeToSlot :: SlotConfig -> Word64 -> Either Error Word64
timeToSlot SlotConfig
config Word64
time
| Word64
time Word64 -> Word64 -> Bool
forall a. Ord a => a -> a -> Bool
< SlotConfig -> Word64
zeroTime SlotConfig
config = Error -> Either Error Word64
forall a b. a -> Either a b
Left (Text -> Error
InvalidSlotConfig Text
"Time precedes the reference time")
| Bool
otherwise = Integer -> Either Error Word64
bounded (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (SlotConfig -> Word64
zeroSlot SlotConfig
config) Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
+ (Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger Word64
time Integer -> Integer -> Integer
forall a. Num a => a -> a -> a
- Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (SlotConfig -> Word64
zeroTime SlotConfig
config)) Integer -> Integer -> Integer
forall a. Integral a => a -> a -> a
`div` Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (SlotConfig -> Word64
slotLength SlotConfig
config))
bounded :: Integer -> Either Error Word64
bounded :: Integer -> Either Error Word64
bounded Integer
value
| Integer
value Integer -> Integer -> Bool
forall a. Ord a => a -> a -> Bool
> Word64 -> Integer
forall a. Integral a => a -> Integer
toInteger (Word64
forall a. Bounded a => a
maxBound :: Word64) = Error -> Either Error Word64
forall a b. a -> Either a b
Left (Text -> Error
InvalidSlotConfig Text
"Conversion exceeds the unsigned 64-bit range")
| Bool
otherwise = Word64 -> Either Error Word64
forall a b. b -> Either a b
Right (Integer -> Word64
forall a. Num a => Integer -> a
fromInteger Integer
value)