{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DerivingVia #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}

module Cardano.Slotting.Block
  ( BlockNo (..)
  )
where

import Cardano.Binary (FromCBOR (..), ToCBOR (..))
import Codec.Serialise (Serialise (..))
import Control.DeepSeq (NFData)
import Data.Word (Word64)
import GHC.Generics (Generic)
import NoThunks.Class (NoThunks)
import Quiet (Quiet (..))

-- | The 0-based index of the block in the blockchain.
-- BlockNo is <= SlotNo and is only equal at slot N if there is a block
-- for every slot where N <= SlotNo.
newtype BlockNo = BlockNo {BlockNo -> Word64
unBlockNo :: Word64}
  deriving stock (BlockNo -> BlockNo -> Bool
(BlockNo -> BlockNo -> Bool)
-> (BlockNo -> BlockNo -> Bool) -> Eq BlockNo
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: BlockNo -> BlockNo -> Bool
$c/= :: BlockNo -> BlockNo -> Bool
== :: BlockNo -> BlockNo -> Bool
$c== :: BlockNo -> BlockNo -> Bool
Eq, Eq BlockNo
Eq BlockNo
-> (BlockNo -> BlockNo -> Ordering)
-> (BlockNo -> BlockNo -> Bool)
-> (BlockNo -> BlockNo -> Bool)
-> (BlockNo -> BlockNo -> Bool)
-> (BlockNo -> BlockNo -> Bool)
-> (BlockNo -> BlockNo -> BlockNo)
-> (BlockNo -> BlockNo -> BlockNo)
-> Ord BlockNo
BlockNo -> BlockNo -> Bool
BlockNo -> BlockNo -> Ordering
BlockNo -> BlockNo -> BlockNo
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: BlockNo -> BlockNo -> BlockNo
$cmin :: BlockNo -> BlockNo -> BlockNo
max :: BlockNo -> BlockNo -> BlockNo
$cmax :: BlockNo -> BlockNo -> BlockNo
>= :: BlockNo -> BlockNo -> Bool
$c>= :: BlockNo -> BlockNo -> Bool
> :: BlockNo -> BlockNo -> Bool
$c> :: BlockNo -> BlockNo -> Bool
<= :: BlockNo -> BlockNo -> Bool
$c<= :: BlockNo -> BlockNo -> Bool
< :: BlockNo -> BlockNo -> Bool
$c< :: BlockNo -> BlockNo -> Bool
compare :: BlockNo -> BlockNo -> Ordering
$ccompare :: BlockNo -> BlockNo -> Ordering
$cp1Ord :: Eq BlockNo
Ord, (forall x. BlockNo -> Rep BlockNo x)
-> (forall x. Rep BlockNo x -> BlockNo) -> Generic BlockNo
forall x. Rep BlockNo x -> BlockNo
forall x. BlockNo -> Rep BlockNo x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep BlockNo x -> BlockNo
$cfrom :: forall x. BlockNo -> Rep BlockNo x
Generic)
  deriving Int -> BlockNo -> ShowS
[BlockNo] -> ShowS
BlockNo -> String
(Int -> BlockNo -> ShowS)
-> (BlockNo -> String) -> ([BlockNo] -> ShowS) -> Show BlockNo
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [BlockNo] -> ShowS
$cshowList :: [BlockNo] -> ShowS
show :: BlockNo -> String
$cshow :: BlockNo -> String
showsPrec :: Int -> BlockNo -> ShowS
$cshowsPrec :: Int -> BlockNo -> ShowS
Show via Quiet BlockNo
  deriving newtype (Int -> BlockNo
BlockNo -> Int
BlockNo -> [BlockNo]
BlockNo -> BlockNo
BlockNo -> BlockNo -> [BlockNo]
BlockNo -> BlockNo -> BlockNo -> [BlockNo]
(BlockNo -> BlockNo)
-> (BlockNo -> BlockNo)
-> (Int -> BlockNo)
-> (BlockNo -> Int)
-> (BlockNo -> [BlockNo])
-> (BlockNo -> BlockNo -> [BlockNo])
-> (BlockNo -> BlockNo -> [BlockNo])
-> (BlockNo -> BlockNo -> BlockNo -> [BlockNo])
-> Enum BlockNo
forall a.
(a -> a)
-> (a -> a)
-> (Int -> a)
-> (a -> Int)
-> (a -> [a])
-> (a -> a -> [a])
-> (a -> a -> [a])
-> (a -> a -> a -> [a])
-> Enum a
enumFromThenTo :: BlockNo -> BlockNo -> BlockNo -> [BlockNo]
$cenumFromThenTo :: BlockNo -> BlockNo -> BlockNo -> [BlockNo]
enumFromTo :: BlockNo -> BlockNo -> [BlockNo]
$cenumFromTo :: BlockNo -> BlockNo -> [BlockNo]
enumFromThen :: BlockNo -> BlockNo -> [BlockNo]
$cenumFromThen :: BlockNo -> BlockNo -> [BlockNo]
enumFrom :: BlockNo -> [BlockNo]
$cenumFrom :: BlockNo -> [BlockNo]
fromEnum :: BlockNo -> Int
$cfromEnum :: BlockNo -> Int
toEnum :: Int -> BlockNo
$ctoEnum :: Int -> BlockNo
pred :: BlockNo -> BlockNo
$cpred :: BlockNo -> BlockNo
succ :: BlockNo -> BlockNo
$csucc :: BlockNo -> BlockNo
Enum, BlockNo
BlockNo -> BlockNo -> Bounded BlockNo
forall a. a -> a -> Bounded a
maxBound :: BlockNo
$cmaxBound :: BlockNo
minBound :: BlockNo
$cminBound :: BlockNo
Bounded, Integer -> BlockNo
BlockNo -> BlockNo
BlockNo -> BlockNo -> BlockNo
(BlockNo -> BlockNo -> BlockNo)
-> (BlockNo -> BlockNo -> BlockNo)
-> (BlockNo -> BlockNo -> BlockNo)
-> (BlockNo -> BlockNo)
-> (BlockNo -> BlockNo)
-> (BlockNo -> BlockNo)
-> (Integer -> BlockNo)
-> Num BlockNo
forall a.
(a -> a -> a)
-> (a -> a -> a)
-> (a -> a -> a)
-> (a -> a)
-> (a -> a)
-> (a -> a)
-> (Integer -> a)
-> Num a
fromInteger :: Integer -> BlockNo
$cfromInteger :: Integer -> BlockNo
signum :: BlockNo -> BlockNo
$csignum :: BlockNo -> BlockNo
abs :: BlockNo -> BlockNo
$cabs :: BlockNo -> BlockNo
negate :: BlockNo -> BlockNo
$cnegate :: BlockNo -> BlockNo
* :: BlockNo -> BlockNo -> BlockNo
$c* :: BlockNo -> BlockNo -> BlockNo
- :: BlockNo -> BlockNo -> BlockNo
$c- :: BlockNo -> BlockNo -> BlockNo
+ :: BlockNo -> BlockNo -> BlockNo
$c+ :: BlockNo -> BlockNo -> BlockNo
Num, Decoder s BlockNo
Decoder s [BlockNo]
[BlockNo] -> Encoding
BlockNo -> Encoding
(BlockNo -> Encoding)
-> (forall s. Decoder s BlockNo)
-> ([BlockNo] -> Encoding)
-> (forall s. Decoder s [BlockNo])
-> Serialise BlockNo
forall s. Decoder s [BlockNo]
forall s. Decoder s BlockNo
forall a.
(a -> Encoding)
-> (forall s. Decoder s a)
-> ([a] -> Encoding)
-> (forall s. Decoder s [a])
-> Serialise a
decodeList :: Decoder s [BlockNo]
$cdecodeList :: forall s. Decoder s [BlockNo]
encodeList :: [BlockNo] -> Encoding
$cencodeList :: [BlockNo] -> Encoding
decode :: Decoder s BlockNo
$cdecode :: forall s. Decoder s BlockNo
encode :: BlockNo -> Encoding
$cencode :: BlockNo -> Encoding
Serialise, Context -> BlockNo -> IO (Maybe ThunkInfo)
Proxy BlockNo -> String
(Context -> BlockNo -> IO (Maybe ThunkInfo))
-> (Context -> BlockNo -> IO (Maybe ThunkInfo))
-> (Proxy BlockNo -> String)
-> NoThunks BlockNo
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
showTypeOf :: Proxy BlockNo -> String
$cshowTypeOf :: Proxy BlockNo -> String
wNoThunks :: Context -> BlockNo -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> BlockNo -> IO (Maybe ThunkInfo)
noThunks :: Context -> BlockNo -> IO (Maybe ThunkInfo)
$cnoThunks :: Context -> BlockNo -> IO (Maybe ThunkInfo)
NoThunks, BlockNo -> ()
(BlockNo -> ()) -> NFData BlockNo
forall a. (a -> ()) -> NFData a
rnf :: BlockNo -> ()
$crnf :: BlockNo -> ()
NFData)

instance ToCBOR BlockNo where
  toCBOR :: BlockNo -> Encoding
toCBOR = BlockNo -> Encoding
forall a. Serialise a => a -> Encoding
encode
  encodedSizeExpr :: (forall t. ToCBOR t => Proxy t -> Size) -> Proxy BlockNo -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
size = (forall t. ToCBOR t => Proxy t -> Size) -> Proxy Word64 -> Size
forall a.
ToCBOR a =>
(forall t. ToCBOR t => Proxy t -> Size) -> Proxy a -> Size
encodedSizeExpr forall t. ToCBOR t => Proxy t -> Size
size (Proxy Word64 -> Size)
-> (Proxy BlockNo -> Proxy Word64) -> Proxy BlockNo -> Size
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (BlockNo -> Word64) -> Proxy BlockNo -> Proxy Word64
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap BlockNo -> Word64
unBlockNo

instance FromCBOR BlockNo where
  fromCBOR :: Decoder s BlockNo
fromCBOR = Decoder s BlockNo
forall a s. Serialise a => Decoder s a
decode