{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
{-# OPTIONS_GHC -Wno-orphans #-}
module Ouroboros.Network.Mock.ConcreteBlock
( Block (..)
, BlockHeader (..)
, BlockBody (..)
, hashHeader
, BodyHash (..)
, ConcreteHeaderHash (..)
, hashBody
, convertSlotToTimeForTestsAssumingNoHardFork
, mkChain
, mkChainSimple
, mkAnchoredFragment
, mkAnchoredFragmentSimple
, mkPartialBlock
, mkPartialBlockHeader
, fixupBlock
, fixupBlockHeader
, fixupBlockAfterBlock
, fixupChain
, fixupAnchoredFragmentFrom
) where
import Data.ByteString (ByteString)
import Data.Function (fix)
import Data.Hashable
import Data.String (IsString)
import Data.Time.Calendar (fromGregorian)
import Data.Time.Clock (UTCTime (..), addUTCTime, secondsToNominalDiffTime)
import NoThunks.Class (NoThunks)
import Codec.CBOR.Decoding (decodeBytes, decodeInt, decodeListLenOf,
decodeWord64)
import Codec.CBOR.Encoding (encodeBytes, encodeInt, encodeListLen, encodeWord64)
import Codec.Serialise (Serialise (..))
import GHC.Generics (Generic)
import Ouroboros.Network.AnchoredFragment (Anchor (..), AnchoredFragment)
import Ouroboros.Network.AnchoredFragment qualified as AF
import Ouroboros.Network.Block
import Ouroboros.Network.Mock.Chain (Chain)
import Ouroboros.Network.Mock.Chain qualified as C
import Ouroboros.Network.Point (withOrigin)
import Ouroboros.Network.Util.ShowProxy
data Block = Block {
:: BlockHeader,
Block -> BlockBody
blockBody :: BlockBody
}
deriving (Int -> Block -> ShowS
[Block] -> ShowS
Block -> String
(Int -> Block -> ShowS)
-> (Block -> String) -> ([Block] -> ShowS) -> Show Block
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Block -> ShowS
showsPrec :: Int -> Block -> ShowS
$cshow :: Block -> String
show :: Block -> String
$cshowList :: [Block] -> ShowS
showList :: [Block] -> ShowS
Show, Block -> Block -> Bool
(Block -> Block -> Bool) -> (Block -> Block -> Bool) -> Eq Block
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Block -> Block -> Bool
== :: Block -> Block -> Bool
$c/= :: Block -> Block -> Bool
/= :: Block -> Block -> Bool
Eq, (forall x. Block -> Rep Block x)
-> (forall x. Rep Block x -> Block) -> Generic Block
forall x. Rep Block x -> Block
forall x. Block -> Rep Block x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. Block -> Rep Block x
from :: forall x. Block -> Rep Block x
$cto :: forall x. Rep Block x -> Block
to :: forall x. Rep Block x -> Block
Generic)
instance ShowProxy Block where
newtype BlockBody = BlockBody ByteString
deriving (Int -> BlockBody -> ShowS
[BlockBody] -> ShowS
BlockBody -> String
(Int -> BlockBody -> ShowS)
-> (BlockBody -> String)
-> ([BlockBody] -> ShowS)
-> Show BlockBody
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BlockBody -> ShowS
showsPrec :: Int -> BlockBody -> ShowS
$cshow :: BlockBody -> String
show :: BlockBody -> String
$cshowList :: [BlockBody] -> ShowS
showList :: [BlockBody] -> ShowS
Show, BlockBody -> BlockBody -> Bool
(BlockBody -> BlockBody -> Bool)
-> (BlockBody -> BlockBody -> Bool) -> Eq BlockBody
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BlockBody -> BlockBody -> Bool
== :: BlockBody -> BlockBody -> Bool
$c/= :: BlockBody -> BlockBody -> Bool
/= :: BlockBody -> BlockBody -> Bool
Eq, Eq BlockBody
Eq BlockBody =>
(BlockBody -> BlockBody -> Ordering)
-> (BlockBody -> BlockBody -> Bool)
-> (BlockBody -> BlockBody -> Bool)
-> (BlockBody -> BlockBody -> Bool)
-> (BlockBody -> BlockBody -> Bool)
-> (BlockBody -> BlockBody -> BlockBody)
-> (BlockBody -> BlockBody -> BlockBody)
-> Ord BlockBody
BlockBody -> BlockBody -> Bool
BlockBody -> BlockBody -> Ordering
BlockBody -> BlockBody -> BlockBody
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
$ccompare :: BlockBody -> BlockBody -> Ordering
compare :: BlockBody -> BlockBody -> Ordering
$c< :: BlockBody -> BlockBody -> Bool
< :: BlockBody -> BlockBody -> Bool
$c<= :: BlockBody -> BlockBody -> Bool
<= :: BlockBody -> BlockBody -> Bool
$c> :: BlockBody -> BlockBody -> Bool
> :: BlockBody -> BlockBody -> Bool
$c>= :: BlockBody -> BlockBody -> Bool
>= :: BlockBody -> BlockBody -> Bool
$cmax :: BlockBody -> BlockBody -> BlockBody
max :: BlockBody -> BlockBody -> BlockBody
$cmin :: BlockBody -> BlockBody -> BlockBody
min :: BlockBody -> BlockBody -> BlockBody
Ord, String -> BlockBody
(String -> BlockBody) -> IsString BlockBody
forall a. (String -> a) -> IsString a
$cfromString :: String -> BlockBody
fromString :: String -> BlockBody
IsString, (forall x. BlockBody -> Rep BlockBody x)
-> (forall x. Rep BlockBody x -> BlockBody) -> Generic BlockBody
forall x. Rep BlockBody x -> BlockBody
forall x. BlockBody -> Rep BlockBody x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. BlockBody -> Rep BlockBody x
from :: forall x. BlockBody -> Rep BlockBody x
$cto :: forall x. Rep BlockBody x -> BlockBody
to :: forall x. Rep BlockBody x -> BlockBody
Generic)
instance Hashable BlockBody where
hash :: BlockBody -> Int
hash (BlockBody ByteString
body) = ByteString -> Int
forall a. Hashable a => a -> Int
hash ByteString
body
hashBody :: Hashable body => body -> BodyHash
hashBody :: forall body. Hashable body => body -> BodyHash
hashBody body
body = Int -> BodyHash
BodyHash (body -> Int
forall a. Hashable a => a -> Int
hash body
body)
data = {
:: HeaderHash BlockHeader,
:: ChainHash BlockHeader,
:: SlotNo,
:: BlockNo,
BlockHeader -> BodyHash
headerBodyHash :: BodyHash
}
deriving (Int -> BlockHeader -> ShowS
[BlockHeader] -> ShowS
BlockHeader -> String
(Int -> BlockHeader -> ShowS)
-> (BlockHeader -> String)
-> ([BlockHeader] -> ShowS)
-> Show BlockHeader
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BlockHeader -> ShowS
showsPrec :: Int -> BlockHeader -> ShowS
$cshow :: BlockHeader -> String
show :: BlockHeader -> String
$cshowList :: [BlockHeader] -> ShowS
showList :: [BlockHeader] -> ShowS
Show, BlockHeader -> BlockHeader -> Bool
(BlockHeader -> BlockHeader -> Bool)
-> (BlockHeader -> BlockHeader -> Bool) -> Eq BlockHeader
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BlockHeader -> BlockHeader -> Bool
== :: BlockHeader -> BlockHeader -> Bool
$c/= :: BlockHeader -> BlockHeader -> Bool
/= :: BlockHeader -> BlockHeader -> Bool
Eq, (forall x. BlockHeader -> Rep BlockHeader x)
-> (forall x. Rep BlockHeader x -> BlockHeader)
-> Generic BlockHeader
forall x. Rep BlockHeader x -> BlockHeader
forall x. BlockHeader -> Rep BlockHeader x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. BlockHeader -> Rep BlockHeader x
from :: forall x. BlockHeader -> Rep BlockHeader x
$cto :: forall x. Rep BlockHeader x -> BlockHeader
to :: forall x. Rep BlockHeader x -> BlockHeader
Generic)
instance ShowProxy BlockHeader where
hashHeader :: BlockHeader -> ConcreteHeaderHash
(BlockHeader HeaderHash BlockHeader
_ ChainHash BlockHeader
b SlotNo
c BlockNo
d BodyHash
e) = Int -> ConcreteHeaderHash
HeaderHash ((ChainHash BlockHeader, SlotNo, BlockNo, BodyHash) -> Int
forall a. Hashable a => a -> Int
hash (ChainHash BlockHeader
b, SlotNo
c, BlockNo
d, BodyHash
e))
deriving instance Hashable SlotNo
deriving instance Hashable BlockNo
instance (StandardHash b, Hashable (HeaderHash b)) => Hashable (ChainHash b)
newtype = Int
deriving (Int -> ConcreteHeaderHash -> ShowS
[ConcreteHeaderHash] -> ShowS
ConcreteHeaderHash -> String
(Int -> ConcreteHeaderHash -> ShowS)
-> (ConcreteHeaderHash -> String)
-> ([ConcreteHeaderHash] -> ShowS)
-> Show ConcreteHeaderHash
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ConcreteHeaderHash -> ShowS
showsPrec :: Int -> ConcreteHeaderHash -> ShowS
$cshow :: ConcreteHeaderHash -> String
show :: ConcreteHeaderHash -> String
$cshowList :: [ConcreteHeaderHash] -> ShowS
showList :: [ConcreteHeaderHash] -> ShowS
Show, ConcreteHeaderHash -> ConcreteHeaderHash -> Bool
(ConcreteHeaderHash -> ConcreteHeaderHash -> Bool)
-> (ConcreteHeaderHash -> ConcreteHeaderHash -> Bool)
-> Eq ConcreteHeaderHash
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: ConcreteHeaderHash -> ConcreteHeaderHash -> Bool
== :: ConcreteHeaderHash -> ConcreteHeaderHash -> Bool
$c/= :: ConcreteHeaderHash -> ConcreteHeaderHash -> Bool
/= :: ConcreteHeaderHash -> ConcreteHeaderHash -> Bool
Eq, Eq ConcreteHeaderHash
Eq ConcreteHeaderHash =>
(ConcreteHeaderHash -> ConcreteHeaderHash -> Ordering)
-> (ConcreteHeaderHash -> ConcreteHeaderHash -> Bool)
-> (ConcreteHeaderHash -> ConcreteHeaderHash -> Bool)
-> (ConcreteHeaderHash -> ConcreteHeaderHash -> Bool)
-> (ConcreteHeaderHash -> ConcreteHeaderHash -> Bool)
-> (ConcreteHeaderHash -> ConcreteHeaderHash -> ConcreteHeaderHash)
-> (ConcreteHeaderHash -> ConcreteHeaderHash -> ConcreteHeaderHash)
-> Ord ConcreteHeaderHash
ConcreteHeaderHash -> ConcreteHeaderHash -> Bool
ConcreteHeaderHash -> ConcreteHeaderHash -> Ordering
ConcreteHeaderHash -> ConcreteHeaderHash -> ConcreteHeaderHash
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
$ccompare :: ConcreteHeaderHash -> ConcreteHeaderHash -> Ordering
compare :: ConcreteHeaderHash -> ConcreteHeaderHash -> Ordering
$c< :: ConcreteHeaderHash -> ConcreteHeaderHash -> Bool
< :: ConcreteHeaderHash -> ConcreteHeaderHash -> Bool
$c<= :: ConcreteHeaderHash -> ConcreteHeaderHash -> Bool
<= :: ConcreteHeaderHash -> ConcreteHeaderHash -> Bool
$c> :: ConcreteHeaderHash -> ConcreteHeaderHash -> Bool
> :: ConcreteHeaderHash -> ConcreteHeaderHash -> Bool
$c>= :: ConcreteHeaderHash -> ConcreteHeaderHash -> Bool
>= :: ConcreteHeaderHash -> ConcreteHeaderHash -> Bool
$cmax :: ConcreteHeaderHash -> ConcreteHeaderHash -> ConcreteHeaderHash
max :: ConcreteHeaderHash -> ConcreteHeaderHash -> ConcreteHeaderHash
$cmin :: ConcreteHeaderHash -> ConcreteHeaderHash -> ConcreteHeaderHash
min :: ConcreteHeaderHash -> ConcreteHeaderHash -> ConcreteHeaderHash
Ord, (forall x. ConcreteHeaderHash -> Rep ConcreteHeaderHash x)
-> (forall x. Rep ConcreteHeaderHash x -> ConcreteHeaderHash)
-> Generic ConcreteHeaderHash
forall x. Rep ConcreteHeaderHash x -> ConcreteHeaderHash
forall x. ConcreteHeaderHash -> Rep ConcreteHeaderHash x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. ConcreteHeaderHash -> Rep ConcreteHeaderHash x
from :: forall x. ConcreteHeaderHash -> Rep ConcreteHeaderHash x
$cto :: forall x. Rep ConcreteHeaderHash x -> ConcreteHeaderHash
to :: forall x. Rep ConcreteHeaderHash x -> ConcreteHeaderHash
Generic, Eq ConcreteHeaderHash
Eq ConcreteHeaderHash =>
(Int -> ConcreteHeaderHash -> Int)
-> (ConcreteHeaderHash -> Int) -> Hashable ConcreteHeaderHash
Int -> ConcreteHeaderHash -> Int
ConcreteHeaderHash -> Int
forall a. Eq a => (Int -> a -> Int) -> (a -> Int) -> Hashable a
$chashWithSalt :: Int -> ConcreteHeaderHash -> Int
hashWithSalt :: Int -> ConcreteHeaderHash -> Int
$chash :: ConcreteHeaderHash -> Int
hash :: ConcreteHeaderHash -> Int
Hashable, Context -> ConcreteHeaderHash -> IO (Maybe ThunkInfo)
Proxy ConcreteHeaderHash -> String
(Context -> ConcreteHeaderHash -> IO (Maybe ThunkInfo))
-> (Context -> ConcreteHeaderHash -> IO (Maybe ThunkInfo))
-> (Proxy ConcreteHeaderHash -> String)
-> NoThunks ConcreteHeaderHash
forall a.
(Context -> a -> IO (Maybe ThunkInfo))
-> (Context -> a -> IO (Maybe ThunkInfo))
-> (Proxy a -> String)
-> NoThunks a
$cnoThunks :: Context -> ConcreteHeaderHash -> IO (Maybe ThunkInfo)
noThunks :: Context -> ConcreteHeaderHash -> IO (Maybe ThunkInfo)
$cwNoThunks :: Context -> ConcreteHeaderHash -> IO (Maybe ThunkInfo)
wNoThunks :: Context -> ConcreteHeaderHash -> IO (Maybe ThunkInfo)
$cshowTypeOf :: Proxy ConcreteHeaderHash -> String
showTypeOf :: Proxy ConcreteHeaderHash -> String
NoThunks)
newtype BodyHash = BodyHash Int
deriving (Int -> BodyHash -> ShowS
[BodyHash] -> ShowS
BodyHash -> String
(Int -> BodyHash -> ShowS)
-> (BodyHash -> String) -> ([BodyHash] -> ShowS) -> Show BodyHash
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> BodyHash -> ShowS
showsPrec :: Int -> BodyHash -> ShowS
$cshow :: BodyHash -> String
show :: BodyHash -> String
$cshowList :: [BodyHash] -> ShowS
showList :: [BodyHash] -> ShowS
Show, BodyHash -> BodyHash -> Bool
(BodyHash -> BodyHash -> Bool)
-> (BodyHash -> BodyHash -> Bool) -> Eq BodyHash
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: BodyHash -> BodyHash -> Bool
== :: BodyHash -> BodyHash -> Bool
$c/= :: BodyHash -> BodyHash -> Bool
/= :: BodyHash -> BodyHash -> Bool
Eq, Eq BodyHash
Eq BodyHash =>
(BodyHash -> BodyHash -> Ordering)
-> (BodyHash -> BodyHash -> Bool)
-> (BodyHash -> BodyHash -> Bool)
-> (BodyHash -> BodyHash -> Bool)
-> (BodyHash -> BodyHash -> Bool)
-> (BodyHash -> BodyHash -> BodyHash)
-> (BodyHash -> BodyHash -> BodyHash)
-> Ord BodyHash
BodyHash -> BodyHash -> Bool
BodyHash -> BodyHash -> Ordering
BodyHash -> BodyHash -> BodyHash
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
$ccompare :: BodyHash -> BodyHash -> Ordering
compare :: BodyHash -> BodyHash -> Ordering
$c< :: BodyHash -> BodyHash -> Bool
< :: BodyHash -> BodyHash -> Bool
$c<= :: BodyHash -> BodyHash -> Bool
<= :: BodyHash -> BodyHash -> Bool
$c> :: BodyHash -> BodyHash -> Bool
> :: BodyHash -> BodyHash -> Bool
$c>= :: BodyHash -> BodyHash -> Bool
>= :: BodyHash -> BodyHash -> Bool
$cmax :: BodyHash -> BodyHash -> BodyHash
max :: BodyHash -> BodyHash -> BodyHash
$cmin :: BodyHash -> BodyHash -> BodyHash
min :: BodyHash -> BodyHash -> BodyHash
Ord, (forall x. BodyHash -> Rep BodyHash x)
-> (forall x. Rep BodyHash x -> BodyHash) -> Generic BodyHash
forall x. Rep BodyHash x -> BodyHash
forall x. BodyHash -> Rep BodyHash x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. BodyHash -> Rep BodyHash x
from :: forall x. BodyHash -> Rep BodyHash x
$cto :: forall x. Rep BodyHash x -> BodyHash
to :: forall x. Rep BodyHash x -> BodyHash
Generic, Eq BodyHash
Eq BodyHash =>
(Int -> BodyHash -> Int) -> (BodyHash -> Int) -> Hashable BodyHash
Int -> BodyHash -> Int
BodyHash -> Int
forall a. Eq a => (Int -> a -> Int) -> (a -> Int) -> Hashable a
$chashWithSalt :: Int -> BodyHash -> Int
hashWithSalt :: Int -> BodyHash -> Int
$chash :: BodyHash -> Int
hash :: BodyHash -> Int
Hashable)
instance StandardHash BlockHeader
instance StandardHash Block
type instance BlockHeader = ConcreteHeaderHash
type instance Block = ConcreteHeaderHash
instance HasHeader BlockHeader where
getHeaderFields :: BlockHeader -> HeaderFields BlockHeader
getHeaderFields BlockHeader
hdr = HeaderFields {
headerFieldHash :: HeaderHash BlockHeader
headerFieldHash = BlockHeader -> HeaderHash BlockHeader
headerHash BlockHeader
hdr,
headerFieldSlot :: SlotNo
headerFieldSlot = BlockHeader -> SlotNo
headerSlot BlockHeader
hdr,
headerFieldBlockNo :: BlockNo
headerFieldBlockNo = BlockHeader -> BlockNo
headerBlockNo BlockHeader
hdr
}
instance HasFullHeader BlockHeader where
blockPrevHash :: BlockHeader -> ChainHash BlockHeader
blockPrevHash = BlockHeader -> ChainHash BlockHeader
headerPrevHash
blockInvariant :: BlockHeader -> Bool
blockInvariant BlockHeader
b =
BlockHeader -> ConcreteHeaderHash
hashHeader BlockHeader
b ConcreteHeaderHash -> ConcreteHeaderHash -> Bool
forall a. Eq a => a -> a -> Bool
== BlockHeader -> HeaderHash BlockHeader
headerHash BlockHeader
b
instance HasHeader Block where
getHeaderFields :: Block -> HeaderFields Block
getHeaderFields = HeaderFields BlockHeader -> HeaderFields Block
forall {k1} {k2} (b :: k1) (b' :: k2).
(HeaderHash b ~ HeaderHash b') =>
HeaderFields b -> HeaderFields b'
castHeaderFields (HeaderFields BlockHeader -> HeaderFields Block)
-> (Block -> HeaderFields BlockHeader)
-> Block
-> HeaderFields Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BlockHeader -> HeaderFields BlockHeader
forall b. HasHeader b => b -> HeaderFields b
getHeaderFields (BlockHeader -> HeaderFields BlockHeader)
-> (Block -> BlockHeader) -> Block -> HeaderFields BlockHeader
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Block -> BlockHeader
blockHeader
instance HasFullHeader Block where
blockPrevHash :: Block -> ChainHash Block
blockPrevHash = ChainHash BlockHeader -> ChainHash Block
forall {k1} {k2} (b :: k1) (b' :: k2).
Coercible (HeaderHash b) (HeaderHash b') =>
ChainHash b -> ChainHash b'
castHash (ChainHash BlockHeader -> ChainHash Block)
-> (Block -> ChainHash BlockHeader) -> Block -> ChainHash Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. BlockHeader -> ChainHash BlockHeader
headerPrevHash (BlockHeader -> ChainHash BlockHeader)
-> (Block -> BlockHeader) -> Block -> ChainHash BlockHeader
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Block -> BlockHeader
blockHeader
blockInvariant :: Block -> Bool
blockInvariant Block { BlockBody
blockBody :: Block -> BlockBody
blockBody :: BlockBody
blockBody, BlockHeader
blockHeader :: Block -> BlockHeader
blockHeader :: BlockHeader
blockHeader } =
BlockHeader -> Bool
forall b. HasFullHeader b => b -> Bool
blockInvariant BlockHeader
blockHeader
Bool -> Bool -> Bool
&& BlockHeader -> BodyHash
headerBodyHash BlockHeader
blockHeader BodyHash -> BodyHash -> Bool
forall a. Eq a => a -> a -> Bool
== BlockBody -> BodyHash
forall body. Hashable body => body -> BodyHash
hashBody BlockBody
blockBody
mkChain :: [(SlotNo, BlockBody)] -> Chain Block
mkChain :: [(SlotNo, BlockBody)] -> Chain Block
mkChain =
(Anchor Block -> Block -> Block) -> [Block] -> Chain Block
forall b. HasFullHeader b => (Anchor b -> b -> b) -> [b] -> Chain b
fixupChain Anchor Block -> Block -> Block
forall block.
(HeaderHash block ~ HeaderHash BlockHeader) =>
Anchor block -> Block -> Block
fixupBlock
([Block] -> Chain Block)
-> ([(SlotNo, BlockBody)] -> [Block])
-> [(SlotNo, BlockBody)]
-> Chain Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((SlotNo, BlockBody) -> Block) -> [(SlotNo, BlockBody)] -> [Block]
forall a b. (a -> b) -> [a] -> [b]
map ((SlotNo -> BlockBody -> Block) -> (SlotNo, BlockBody) -> Block
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry SlotNo -> BlockBody -> Block
mkPartialBlock)
([(SlotNo, BlockBody)] -> [Block])
-> ([(SlotNo, BlockBody)] -> [(SlotNo, BlockBody)])
-> [(SlotNo, BlockBody)]
-> [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(SlotNo, BlockBody)] -> [(SlotNo, BlockBody)]
forall a. [a] -> [a]
reverse
mkChainSimple :: [BlockBody] -> Chain Block
mkChainSimple :: [BlockBody] -> Chain Block
mkChainSimple = [(SlotNo, BlockBody)] -> Chain Block
mkChain ([(SlotNo, BlockBody)] -> Chain Block)
-> ([BlockBody] -> [(SlotNo, BlockBody)])
-> [BlockBody]
-> Chain Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [SlotNo] -> [BlockBody] -> [(SlotNo, BlockBody)]
forall a b. [a] -> [b] -> [(a, b)]
zip [SlotNo
1..]
mkAnchoredFragment :: Anchor Block
-> [(SlotNo, BlockBody)]
-> AnchoredFragment Block
mkAnchoredFragment :: Anchor Block -> [(SlotNo, BlockBody)] -> AnchoredFragment Block
mkAnchoredFragment Anchor Block
anchor =
Anchor Block
-> (Anchor Block -> Block -> Block)
-> [Block]
-> AnchoredFragment Block
forall b.
HasFullHeader b =>
Anchor b -> (Anchor b -> b -> b) -> [b] -> AnchoredFragment b
fixupAnchoredFragmentFrom Anchor Block
anchor Anchor Block -> Block -> Block
forall block.
(HeaderHash block ~ HeaderHash BlockHeader) =>
Anchor block -> Block -> Block
fixupBlock
([Block] -> AnchoredFragment Block)
-> ([(SlotNo, BlockBody)] -> [Block])
-> [(SlotNo, BlockBody)]
-> AnchoredFragment Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ((SlotNo, BlockBody) -> Block) -> [(SlotNo, BlockBody)] -> [Block]
forall a b. (a -> b) -> [a] -> [b]
map ((SlotNo -> BlockBody -> Block) -> (SlotNo, BlockBody) -> Block
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry SlotNo -> BlockBody -> Block
mkPartialBlock)
([(SlotNo, BlockBody)] -> [Block])
-> ([(SlotNo, BlockBody)] -> [(SlotNo, BlockBody)])
-> [(SlotNo, BlockBody)]
-> [Block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [(SlotNo, BlockBody)] -> [(SlotNo, BlockBody)]
forall a. [a] -> [a]
reverse
mkAnchoredFragmentSimple :: [BlockBody] -> AnchoredFragment Block
mkAnchoredFragmentSimple :: [BlockBody] -> AnchoredFragment Block
mkAnchoredFragmentSimple =
Anchor Block -> [(SlotNo, BlockBody)] -> AnchoredFragment Block
mkAnchoredFragment Anchor Block
forall block. Anchor block
AnchorGenesis ([(SlotNo, BlockBody)] -> AnchoredFragment Block)
-> ([BlockBody] -> [(SlotNo, BlockBody)])
-> [BlockBody]
-> AnchoredFragment Block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [SlotNo] -> [BlockBody] -> [(SlotNo, BlockBody)]
forall a b. [a] -> [b] -> [(a, b)]
zip [SlotNo
1..]
mkPartialBlock :: SlotNo -> BlockBody -> Block
mkPartialBlock :: SlotNo -> BlockBody -> Block
mkPartialBlock SlotNo
sl BlockBody
body =
Block {
blockHeader :: BlockHeader
blockHeader = SlotNo -> BlockBody -> BlockHeader
mkPartialBlockHeader SlotNo
sl BlockBody
body
, blockBody :: BlockBody
blockBody = BlockBody
body
}
mkPartialBlockHeader :: SlotNo -> BlockBody -> BlockHeader
SlotNo
sl BlockBody
body =
BlockHeader {
headerSlot :: SlotNo
headerSlot = SlotNo
sl,
headerHash :: HeaderHash BlockHeader
headerHash = String -> ConcreteHeaderHash
forall {a}. String -> a
partialField String
"headerHash",
headerPrevHash :: ChainHash BlockHeader
headerPrevHash = String -> ChainHash BlockHeader
forall {a}. String -> a
partialField String
"headerPrevHash",
headerBlockNo :: BlockNo
headerBlockNo = String -> BlockNo
forall {a}. String -> a
partialField String
"headerBlockNo",
headerBodyHash :: BodyHash
headerBodyHash = BlockBody -> BodyHash
forall body. Hashable body => body -> BodyHash
hashBody BlockBody
body
}
where
partialField :: String -> a
partialField String
n = String -> a
forall a. HasCallStack => String -> a
error (String
"mkPartialBlockHeader: you didn't fill in field " String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
n)
fixupBlock :: (HeaderHash block ~ HeaderHash BlockHeader)
=> Anchor block -> Block -> Block
fixupBlock :: forall block.
(HeaderHash block ~ HeaderHash BlockHeader) =>
Anchor block -> Block -> Block
fixupBlock Anchor block
prev b :: Block
b@Block{BlockBody
blockBody :: Block -> BlockBody
blockBody :: BlockBody
blockBody, BlockHeader
blockHeader :: Block -> BlockHeader
blockHeader :: BlockHeader
blockHeader} =
Block
b {
blockHeader = (fixupBlockHeader prev blockHeader) {
headerBodyHash = hashBody blockBody
}
}
fixupBlockHeader :: (HeaderHash block ~ HeaderHash BlockHeader)
=> Anchor block -> BlockHeader -> BlockHeader
Anchor block
prev BlockHeader
b =
(BlockHeader -> BlockHeader) -> BlockHeader
forall a. (a -> a) -> a
fix ((BlockHeader -> BlockHeader) -> BlockHeader)
-> (BlockHeader -> BlockHeader) -> BlockHeader
forall a b. (a -> b) -> a -> b
$ \BlockHeader
b' ->
BlockHeader
b {
headerHash = hashHeader b',
headerPrevHash = castHash (AF.anchorToHash prev),
headerBlockNo = withOrigin (BlockNo 0) succ (AF.anchorToBlockNo prev)
}
fixupBlockAfterBlock :: Block -> Block -> Block
fixupBlockAfterBlock :: Block -> Block -> Block
fixupBlockAfterBlock Block
prev = Anchor Block -> Block -> Block
forall block.
(HeaderHash block ~ HeaderHash BlockHeader) =>
Anchor block -> Block -> Block
fixupBlock (Block -> Anchor Block
forall block. HasHeader block => block -> Anchor block
AF.anchorFromBlock Block
prev)
fixupBlocks :: HasFullHeader b
=> (c -> b -> c)
-> c
-> Anchor b
-> (Anchor b -> b -> b)
-> [b] -> c
fixupBlocks :: forall b c.
HasFullHeader b =>
(c -> b -> c) -> c -> Anchor b -> (Anchor b -> b -> b) -> [b] -> c
fixupBlocks c -> b -> c
_f c
z Anchor b
_ Anchor b -> b -> b
_fixup [] = c
z
fixupBlocks c -> b -> c
f c
z Anchor b
anchor Anchor b -> b -> b
fixup (b
b0:[b]
c0) =
(c, b) -> c
forall a b. (a, b) -> a
fst (b -> [b] -> (c, b)
go b
b0 [b]
c0)
where
go :: b -> [b] -> (c, b)
go b
b [] = (c
z c -> b -> c
`f` b
b', b
b')
where
b' :: b
b' = Anchor b -> b -> b
fixup Anchor b
anchor b
b
go b
b (b
b1:[b]
c1) = (c
c' c -> b -> c
`f` b
b', b
b')
where
(c
c', b
b1') = b -> [b] -> (c, b)
go b
b1 [b]
c1
b' :: b
b' = Anchor b -> b -> b
fixup (b -> Anchor b
forall block. HasHeader block => block -> Anchor block
AF.anchorFromBlock b
b1') b
b
fixupChain :: HasFullHeader b
=> (Anchor b -> b -> b)
-> [b] -> Chain b
fixupChain :: forall b. HasFullHeader b => (Anchor b -> b -> b) -> [b] -> Chain b
fixupChain =
(Chain b -> b -> Chain b)
-> Chain b -> Anchor b -> (Anchor b -> b -> b) -> [b] -> Chain b
forall b c.
HasFullHeader b =>
(c -> b -> c) -> c -> Anchor b -> (Anchor b -> b -> b) -> [b] -> c
fixupBlocks
Chain b -> b -> Chain b
forall block. Chain block -> block -> Chain block
(C.:>) Chain b
forall block. Chain block
C.Genesis
Anchor b
forall block. Anchor block
AnchorGenesis
fixupAnchoredFragmentFrom :: HasFullHeader b
=> Anchor b
-> (Anchor b -> b -> b)
-> [b] -> AnchoredFragment b
fixupAnchoredFragmentFrom :: forall b.
HasFullHeader b =>
Anchor b -> (Anchor b -> b -> b) -> [b] -> AnchoredFragment b
fixupAnchoredFragmentFrom Anchor b
anchor =
(AnchoredFragment b -> b -> AnchoredFragment b)
-> AnchoredFragment b
-> Anchor b
-> (Anchor b -> b -> b)
-> [b]
-> AnchoredFragment b
forall b c.
HasFullHeader b =>
(c -> b -> c) -> c -> Anchor b -> (Anchor b -> b -> b) -> [b] -> c
fixupBlocks
AnchoredFragment b -> b -> AnchoredFragment b
forall v a b.
Anchorable v a b =>
AnchoredSeq v a b -> b -> AnchoredSeq v a b
(AF.:>)
(Anchor b -> AnchoredFragment b
forall v a b. Anchorable v a b => a -> AnchoredSeq v a b
AF.Empty Anchor b
anchor)
Anchor b
anchor
instance Serialise ConcreteHeaderHash where
encode :: ConcreteHeaderHash -> Encoding
encode (HeaderHash Int
h) = Int -> Encoding
encodeInt Int
h
decode :: forall s. Decoder s ConcreteHeaderHash
decode = Int -> ConcreteHeaderHash
HeaderHash (Int -> ConcreteHeaderHash)
-> Decoder s Int -> Decoder s ConcreteHeaderHash
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Int
forall s. Decoder s Int
decodeInt
instance Serialise BodyHash where
encode :: BodyHash -> Encoding
encode (BodyHash Int
h) = Int -> Encoding
encodeInt Int
h
decode :: forall s. Decoder s BodyHash
decode = Int -> BodyHash
BodyHash (Int -> BodyHash) -> Decoder s Int -> Decoder s BodyHash
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Int
forall s. Decoder s Int
decodeInt
instance Serialise Block where
encode :: Block -> Encoding
encode Block {BlockHeader
blockHeader :: Block -> BlockHeader
blockHeader :: BlockHeader
blockHeader, BlockBody
blockBody :: Block -> BlockBody
blockBody :: BlockBody
blockBody} =
Word -> Encoding
encodeListLen Word
2
Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> BlockHeader -> Encoding
forall a. Serialise a => a -> Encoding
encode BlockHeader
blockHeader
Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> BlockBody -> Encoding
forall a. Serialise a => a -> Encoding
encode BlockBody
blockBody
decode :: forall s. Decoder s Block
decode = do
Int -> Decoder s ()
forall s. Int -> Decoder s ()
decodeListLenOf Int
2
BlockHeader -> BlockBody -> Block
Block (BlockHeader -> BlockBody -> Block)
-> Decoder s BlockHeader -> Decoder s (BlockBody -> Block)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s BlockHeader
forall s. Decoder s BlockHeader
forall a s. Serialise a => Decoder s a
decode Decoder s (BlockBody -> Block)
-> Decoder s BlockBody -> Decoder s Block
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s BlockBody
forall s. Decoder s BlockBody
forall a s. Serialise a => Decoder s a
decode
instance Serialise BlockHeader where
encode :: BlockHeader -> Encoding
encode BlockHeader {
headerHash :: BlockHeader -> HeaderHash BlockHeader
headerHash = HeaderHash BlockHeader
headerHash,
headerPrevHash :: BlockHeader -> ChainHash BlockHeader
headerPrevHash = ChainHash BlockHeader
headerPrevHash,
headerSlot :: BlockHeader -> SlotNo
headerSlot = SlotNo Word64
headerSlot,
headerBlockNo :: BlockHeader -> BlockNo
headerBlockNo = BlockNo Word64
headerBlockNo,
headerBodyHash :: BlockHeader -> BodyHash
headerBodyHash = BodyHash Int
headerBodyHash
} =
Word -> Encoding
encodeListLen Word
5
Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ConcreteHeaderHash -> Encoding
forall a. Serialise a => a -> Encoding
encode HeaderHash BlockHeader
ConcreteHeaderHash
headerHash
Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> ChainHash BlockHeader -> Encoding
forall a. Serialise a => a -> Encoding
encode ChainHash BlockHeader
headerPrevHash
Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
encodeWord64 Word64
headerSlot
Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Word64 -> Encoding
encodeWord64 Word64
headerBlockNo
Encoding -> Encoding -> Encoding
forall a. Semigroup a => a -> a -> a
<> Int -> Encoding
encodeInt Int
headerBodyHash
decode :: forall s. Decoder s BlockHeader
decode = do
Int -> Decoder s ()
forall s. Int -> Decoder s ()
decodeListLenOf Int
5
HeaderHash BlockHeader
-> ChainHash BlockHeader
-> SlotNo
-> BlockNo
-> BodyHash
-> BlockHeader
ConcreteHeaderHash
-> ChainHash BlockHeader
-> SlotNo
-> BlockNo
-> BodyHash
-> BlockHeader
BlockHeader (ConcreteHeaderHash
-> ChainHash BlockHeader
-> SlotNo
-> BlockNo
-> BodyHash
-> BlockHeader)
-> Decoder s ConcreteHeaderHash
-> Decoder
s
(ChainHash BlockHeader
-> SlotNo -> BlockNo -> BodyHash -> BlockHeader)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s ConcreteHeaderHash
forall s. Decoder s ConcreteHeaderHash
forall a s. Serialise a => Decoder s a
decode
Decoder
s
(ChainHash BlockHeader
-> SlotNo -> BlockNo -> BodyHash -> BlockHeader)
-> Decoder s (ChainHash BlockHeader)
-> Decoder s (SlotNo -> BlockNo -> BodyHash -> BlockHeader)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Decoder s (ChainHash BlockHeader)
forall s. Decoder s (ChainHash BlockHeader)
forall a s. Serialise a => Decoder s a
decode
Decoder s (SlotNo -> BlockNo -> BodyHash -> BlockHeader)
-> Decoder s SlotNo
-> Decoder s (BlockNo -> BodyHash -> BlockHeader)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Word64 -> SlotNo
SlotNo (Word64 -> SlotNo) -> Decoder s Word64 -> Decoder s SlotNo
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word64
forall s. Decoder s Word64
decodeWord64)
Decoder s (BlockNo -> BodyHash -> BlockHeader)
-> Decoder s BlockNo -> Decoder s (BodyHash -> BlockHeader)
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Word64 -> BlockNo
BlockNo (Word64 -> BlockNo) -> Decoder s Word64 -> Decoder s BlockNo
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Word64
forall s. Decoder s Word64
decodeWord64)
Decoder s (BodyHash -> BlockHeader)
-> Decoder s BodyHash -> Decoder s BlockHeader
forall a b. Decoder s (a -> b) -> Decoder s a -> Decoder s b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Int -> BodyHash
BodyHash (Int -> BodyHash) -> Decoder s Int -> Decoder s BodyHash
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s Int
forall s. Decoder s Int
decodeInt)
instance Serialise BlockBody where
encode :: BlockBody -> Encoding
encode (BlockBody ByteString
b) = ByteString -> Encoding
encodeBytes ByteString
b
decode :: forall s. Decoder s BlockBody
decode = ByteString -> BlockBody
BlockBody (ByteString -> BlockBody)
-> Decoder s ByteString -> Decoder s BlockBody
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Decoder s ByteString
forall s. Decoder s ByteString
decodeBytes
convertSlotToTimeForTestsAssumingNoHardFork :: SlotNo -> UTCTime
convertSlotToTimeForTestsAssumingNoHardFork :: SlotNo -> UTCTime
convertSlotToTimeForTestsAssumingNoHardFork SlotNo
sl =
(NominalDiffTime -> UTCTime -> UTCTime)
-> UTCTime -> NominalDiffTime -> UTCTime
forall a b c. (a -> b -> c) -> b -> a -> c
flip NominalDiffTime -> UTCTime -> UTCTime
addUTCTime UTCTime
startTime (NominalDiffTime -> UTCTime) -> NominalDiffTime -> UTCTime
forall a b. (a -> b) -> a -> b
$
Pico -> NominalDiffTime
secondsToNominalDiffTime (Pico -> NominalDiffTime) -> Pico -> NominalDiffTime
forall a b. (a -> b) -> a -> b
$
Word64 -> Pico
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Word64 -> Pico) -> Word64 -> Pico
forall a b. (a -> b) -> a -> b
$ SlotNo -> Word64
unSlotNo SlotNo
sl Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
* Word64
10
where
startTime :: UTCTime
startTime = UTCTime {
utctDay :: Day
utctDay = Integer -> Int -> Int -> Day
fromGregorian Integer
2000 Int
1 Int
1,
utctDayTime :: DiffTime
utctDayTime = DiffTime
0
}