{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Ouroboros.Network.AnchoredFragment
(
AnchoredFragment
, AnchoredSeq (Empty, (:>), (:<))
, anchor
, anchorPoint
, anchorBlockNo
, Anchor (..)
, anchorFromBlock
, anchorFromPoint
, anchorToPoint
, anchorToSlotNo
, anchorToBlockNo
, anchorToHash
, anchorIsGenesis
, anchorToHeaderFields
, anchorToTip
, castAnchor
, valid
, validExtension
, HasHeader (..)
, Point (..)
, castPoint
, blockPoint
, headPoint
, headAnchor
, headSlot
, headHash
, headBlockNo
, head
, last
, lastPoint
, lastSlot
, toNewestFirst
, toOldestFirst
, fromNewestFirst
, fromOldestFirst
, splitAt
, dropNewest
, takeOldest
, dropWhileNewest
, takeWhileOldest
, length
, null
, ChainUpdate (..)
, addBlock
, rollback
, applyChainUpdate
, applyChainUpdates
, pointOnFragment
, withinFragmentBounds
, findFirstPoint
, successorBlock
, selectPoints
, isPrefixOf
, splitAfterPoint
, splitAtSlot
, splitBeforePoint
, sliceRange
, join
, intersect
, intersectionPoint
, mapAnchoredFragment
, anchorNewest
, filter
, filterWithStop
, prettyPrint
, pointOnFragmentSpec
, selectPointsSpec
, filterWithStopSpec
) where
import Prelude hiding (filter, head, last, length, map, null, splitAt)
import Data.Either (isRight)
import Data.List qualified as L
import GHC.Generics (Generic)
import GHC.Stack
import NoThunks.Class (NoThunks)
import Ouroboros.Network.AnchoredSeq hiding (join, prettyPrint, rollback)
import Ouroboros.Network.AnchoredSeq qualified as AS
import Ouroboros.Network.Block
import Ouroboros.Network.Point (WithOrigin (At, Origin), withOrigin)
data Anchor block =
AnchorGenesis
| Anchor !SlotNo !(HeaderHash block) !BlockNo
deriving ((forall x. Anchor block -> Rep (Anchor block) x)
-> (forall x. Rep (Anchor block) x -> Anchor block)
-> Generic (Anchor block)
forall x. Rep (Anchor block) x -> Anchor block
forall x. Anchor block -> Rep (Anchor block) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall block x. Rep (Anchor block) x -> Anchor block
forall block x. Anchor block -> Rep (Anchor block) x
$cfrom :: forall block x. Anchor block -> Rep (Anchor block) x
from :: forall x. Anchor block -> Rep (Anchor block) x
$cto :: forall block x. Rep (Anchor block) x -> Anchor block
to :: forall x. Rep (Anchor block) x -> Anchor block
Generic)
deriving instance StandardHash block => Show (Anchor block)
deriving instance StandardHash block => Eq (Anchor block)
deriving instance StandardHash block => NoThunks (Anchor block)
castAnchor :: (HeaderHash a ~ HeaderHash b) => Anchor a -> Anchor b
castAnchor :: forall a b. (HeaderHash a ~ HeaderHash b) => Anchor a -> Anchor b
castAnchor Anchor a
AnchorGenesis = Anchor b
forall block. Anchor block
AnchorGenesis
castAnchor (Anchor SlotNo
s HeaderHash a
h BlockNo
b) = SlotNo -> HeaderHash b -> BlockNo -> Anchor b
forall block. SlotNo -> HeaderHash block -> BlockNo -> Anchor block
Anchor SlotNo
s HeaderHash a
HeaderHash b
h BlockNo
b
anchorIsGenesis :: Anchor block -> Bool
anchorIsGenesis :: forall block. Anchor block -> Bool
anchorIsGenesis Anchor block
AnchorGenesis = Bool
True
anchorIsGenesis Anchor{} = Bool
False
anchorFromBlock :: HasHeader block => block -> Anchor block
anchorFromBlock :: forall block. HasHeader block => block -> Anchor block
anchorFromBlock block
b = SlotNo -> HeaderHash block -> BlockNo -> Anchor block
forall block. SlotNo -> HeaderHash block -> BlockNo -> Anchor block
Anchor SlotNo
sno HeaderHash block
hash BlockNo
bno
where
HeaderFields {
headerFieldSlot :: forall k (b :: k). HeaderFields b -> SlotNo
headerFieldSlot = SlotNo
sno
, headerFieldBlockNo :: forall k (b :: k). HeaderFields b -> BlockNo
headerFieldBlockNo = BlockNo
bno
, headerFieldHash :: forall k (b :: k). HeaderFields b -> HeaderHash b
headerFieldHash = HeaderHash block
hash
} = block -> HeaderFields block
forall b. HasHeader b => b -> HeaderFields b
getHeaderFields block
b
anchorToPoint :: Anchor block -> Point block
anchorToPoint :: forall block. Anchor block -> Point block
anchorToPoint Anchor block
AnchorGenesis = Point block
forall {k} (block :: k). Point block
genesisPoint
anchorToPoint (Anchor SlotNo
s HeaderHash block
h BlockNo
_b) = SlotNo -> HeaderHash block -> Point block
forall {k} (block :: k). SlotNo -> HeaderHash block -> Point block
BlockPoint SlotNo
s HeaderHash block
h
anchorFromPoint :: Point block -> BlockNo -> Anchor block
anchorFromPoint :: forall block. Point block -> BlockNo -> Anchor block
anchorFromPoint Point block
GenesisPoint BlockNo
_ = String -> Anchor block
forall a. HasCallStack => String -> a
error String
"anchorFromPoint: genesis point"
anchorFromPoint (BlockPoint SlotNo
s HeaderHash block
h) BlockNo
b = SlotNo -> HeaderHash block -> BlockNo -> Anchor block
forall block. SlotNo -> HeaderHash block -> BlockNo -> Anchor block
Anchor SlotNo
s HeaderHash block
h BlockNo
b
anchorToBlockNo :: Anchor block -> WithOrigin BlockNo
anchorToBlockNo :: forall block. Anchor block -> WithOrigin BlockNo
anchorToBlockNo Anchor block
AnchorGenesis = WithOrigin BlockNo
forall t. WithOrigin t
Origin
anchorToBlockNo (Anchor SlotNo
_s HeaderHash block
_h BlockNo
b) = BlockNo -> WithOrigin BlockNo
forall t. t -> WithOrigin t
At BlockNo
b
anchorToSlotNo :: Anchor block -> WithOrigin SlotNo
anchorToSlotNo :: forall block. Anchor block -> WithOrigin SlotNo
anchorToSlotNo Anchor block
AnchorGenesis = WithOrigin SlotNo
forall t. WithOrigin t
Origin
anchorToSlotNo (Anchor SlotNo
s HeaderHash block
_h BlockNo
_b) = SlotNo -> WithOrigin SlotNo
forall t. t -> WithOrigin t
At SlotNo
s
anchorToHash :: Anchor block -> ChainHash block
anchorToHash :: forall block. Anchor block -> ChainHash block
anchorToHash Anchor block
AnchorGenesis = ChainHash block
forall {k} (b :: k). ChainHash b
GenesisHash
anchorToHash (Anchor SlotNo
_s HeaderHash block
h BlockNo
_b) = HeaderHash block -> ChainHash block
forall {k} (b :: k). HeaderHash b -> ChainHash b
BlockHash HeaderHash block
h
anchorToHeaderFields :: Anchor block -> WithOrigin (HeaderFields block)
Anchor block
AnchorGenesis = WithOrigin (HeaderFields block)
forall t. WithOrigin t
Origin
anchorToHeaderFields (Anchor SlotNo
s HeaderHash block
h BlockNo
b) = HeaderFields block -> WithOrigin (HeaderFields block)
forall t. t -> WithOrigin t
At (SlotNo -> BlockNo -> HeaderHash block -> HeaderFields block
forall k (b :: k).
SlotNo -> BlockNo -> HeaderHash b -> HeaderFields b
HeaderFields SlotNo
s BlockNo
b HeaderHash block
h)
anchorToTip :: (HeaderHash a ~ HeaderHash b) => Anchor a -> Tip b
anchorToTip :: forall a b. (HeaderHash a ~ HeaderHash b) => Anchor a -> Tip b
anchorToTip Anchor a
AnchorGenesis = Tip b
forall {k} (b :: k). Tip b
TipGenesis
anchorToTip (Anchor SlotNo
s HeaderHash a
h BlockNo
b) = SlotNo -> HeaderHash b -> BlockNo -> Tip b
forall {k} (b :: k). SlotNo -> HeaderHash b -> BlockNo -> Tip b
Tip SlotNo
s HeaderHash a
HeaderHash b
h BlockNo
b
type AnchoredFragment block = AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
instance HasHeader block
=> Anchorable (WithOrigin SlotNo) (Anchor block) block where
asAnchor :: block -> Anchor block
asAnchor = block -> Anchor block
forall block. HasHeader block => block -> Anchor block
anchorFromBlock
getAnchorMeasure :: Proxy block -> Anchor block -> WithOrigin SlotNo
getAnchorMeasure Proxy block
_ = Anchor block -> WithOrigin SlotNo
forall block. Anchor block -> WithOrigin SlotNo
anchorToSlotNo
anchorPoint :: AnchoredFragment block -> Point block
anchorPoint :: forall block. AnchoredFragment block -> Point block
anchorPoint = Anchor block -> Point block
forall block. Anchor block -> Point block
anchorToPoint (Anchor block -> Point block)
-> (AnchoredFragment block -> Anchor block)
-> AnchoredFragment block
-> Point block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnchoredFragment block -> Anchor block
forall v a b. AnchoredSeq v a b -> a
anchor
anchorBlockNo :: AnchoredFragment block -> WithOrigin BlockNo
anchorBlockNo :: forall block. AnchoredFragment block -> WithOrigin BlockNo
anchorBlockNo = Anchor block -> WithOrigin BlockNo
forall block. Anchor block -> WithOrigin BlockNo
anchorToBlockNo (Anchor block -> WithOrigin BlockNo)
-> (AnchoredFragment block -> Anchor block)
-> AnchoredFragment block
-> WithOrigin BlockNo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnchoredFragment block -> Anchor block
forall v a b. AnchoredSeq v a b -> a
anchor
prettyPrint ::
String
-> (Point block -> String)
-> (block -> String)
-> AnchoredFragment block
-> String
prettyPrint :: forall block.
String
-> (Point block -> String)
-> (block -> String)
-> AnchoredFragment block
-> String
prettyPrint String
nl Point block -> String
ppPoint = String
-> (Anchor block -> String)
-> (block -> String)
-> AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
-> String
forall a b v.
String
-> (a -> String) -> (b -> String) -> AnchoredSeq v a b -> String
AS.prettyPrint String
nl (Point block -> String
ppPoint (Point block -> String)
-> (Anchor block -> Point block) -> Anchor block -> String
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Anchor block -> Point block
forall block. Anchor block -> Point block
anchorToPoint)
valid :: HasFullHeader block => AnchoredFragment block -> Bool
valid :: forall block. HasFullHeader block => AnchoredFragment block -> Bool
valid (Empty Anchor block
_) = Bool
True
valid (AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
af :> block
b) = AnchoredSeq (WithOrigin SlotNo) (Anchor block) block -> Bool
forall block. HasFullHeader block => AnchoredFragment block -> Bool
valid AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
af Bool -> Bool -> Bool
&& AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
-> block -> Bool
forall block.
HasFullHeader block =>
AnchoredFragment block -> block -> Bool
validExtension AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
af block
b
isValidSuccessorOf :: HasFullHeader block
=> block
-> Anchor block
-> Bool
isValidSuccessorOf :: forall block. HasFullHeader block => block -> Anchor block -> Bool
isValidSuccessorOf block
bSucc Anchor block
b = Either String () -> Bool
forall a b. Either a b -> Bool
isRight (Either String () -> Bool) -> Either String () -> Bool
forall a b. (a -> b) -> a -> b
$ block -> Anchor block -> Either String ()
forall block.
HasFullHeader block =>
block -> Anchor block -> Either String ()
isValidSuccessorOf' block
bSucc Anchor block
b
isValidSuccessorOf' :: HasFullHeader block
=> block
-> Anchor block
-> Either String ()
isValidSuccessorOf' :: forall block.
HasFullHeader block =>
block -> Anchor block -> Either String ()
isValidSuccessorOf' block
bSucc Anchor block
b
| Anchor block -> ChainHash block
forall block. Anchor block -> ChainHash block
anchorToHash Anchor block
b ChainHash block -> ChainHash block -> Bool
forall a. Eq a => a -> a -> Bool
/= block -> ChainHash block
forall b. HasFullHeader b => b -> ChainHash b
blockPrevHash block
bSucc
= String -> Either String ()
forall a b. a -> Either a b
Left (String -> Either String ()) -> String -> Either String ()
forall a b. (a -> b) -> a -> b
$ Context -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [
String
"prevHash ("
, ChainHash block -> String
forall a. Show a => a -> String
show (block -> ChainHash block
forall b. HasFullHeader b => b -> ChainHash b
blockPrevHash block
bSucc)
, String
") doesn't match hash of tip ("
, ChainHash block -> String
forall a. Show a => a -> String
show (Anchor block -> ChainHash block
forall block. Anchor block -> ChainHash block
anchorToHash Anchor block
b)
, String
") at "
, CallStack -> String
prettyCallStack CallStack
HasCallStack => CallStack
callStack
]
| Anchor block -> WithOrigin SlotNo
forall block. Anchor block -> WithOrigin SlotNo
anchorToSlotNo Anchor block
b WithOrigin SlotNo -> WithOrigin SlotNo -> Bool
forall a. Ord a => a -> a -> Bool
> SlotNo -> WithOrigin SlotNo
forall t. t -> WithOrigin t
At (block -> SlotNo
forall b. HasHeader b => b -> SlotNo
blockSlot block
bSucc)
= String -> Either String ()
forall a b. a -> Either a b
Left (String -> Either String ()) -> String -> Either String ()
forall a b. (a -> b) -> a -> b
$ Context -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [
String
"Slot of tip ("
, WithOrigin SlotNo -> String
forall a. Show a => a -> String
show (Anchor block -> WithOrigin SlotNo
forall block. Anchor block -> WithOrigin SlotNo
anchorToSlotNo Anchor block
b)
, String
") > slot ("
, SlotNo -> String
forall a. Show a => a -> String
show (block -> SlotNo
forall b. HasHeader b => b -> SlotNo
blockSlot block
bSucc)
, String
")"
]
| BlockNo -> WithOrigin BlockNo
forall t. t -> WithOrigin t
At (block -> BlockNo
forall b. HasHeader b => b -> BlockNo
blockNo block
bSucc) WithOrigin BlockNo -> WithOrigin BlockNo -> Bool
forall a. Ord a => a -> a -> Bool
< Anchor block -> WithOrigin BlockNo
forall block. Anchor block -> WithOrigin BlockNo
anchorToBlockNo Anchor block
b
= String -> Either String ()
forall a b. a -> Either a b
Left (String -> Either String ()) -> String -> Either String ()
forall a b. (a -> b) -> a -> b
$ Context -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [
String
"BlockNo ("
, BlockNo -> String
forall a. Show a => a -> String
show (block -> BlockNo
forall b. HasHeader b => b -> BlockNo
blockNo block
bSucc)
, String
") is less than BlockNo of tip ("
, WithOrigin BlockNo -> String
forall a. Show a => a -> String
show (Anchor block -> WithOrigin BlockNo
forall block. Anchor block -> WithOrigin BlockNo
anchorToBlockNo Anchor block
b)
, String
")"
]
| block -> BlockNo
forall b. HasHeader b => b -> BlockNo
blockNo block
bSucc BlockNo -> BlockNo -> Bool
forall a. Ord a => a -> a -> Bool
> BlockNo -> (BlockNo -> BlockNo) -> WithOrigin BlockNo -> BlockNo
forall b t. b -> (t -> b) -> WithOrigin t -> b
withOrigin (Word64 -> BlockNo
BlockNo Word64
0) BlockNo -> BlockNo
forall a. Enum a => a -> a
succ (Anchor block -> WithOrigin BlockNo
forall block. Anchor block -> WithOrigin BlockNo
anchorToBlockNo Anchor block
b)
= String -> Either String ()
forall a b. a -> Either a b
Left (String -> Either String ()) -> String -> Either String ()
forall a b. (a -> b) -> a -> b
$ Context -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [
String
"BlockNo ("
, BlockNo -> String
forall a. Show a => a -> String
show (block -> BlockNo
forall b. HasHeader b => b -> BlockNo
blockNo block
bSucc)
, String
") is greater than BlockNo of tip ("
, WithOrigin BlockNo -> String
forall a. Show a => a -> String
show (Anchor block -> WithOrigin BlockNo
forall block. Anchor block -> WithOrigin BlockNo
anchorToBlockNo Anchor block
b)
, String
") + 1"
]
| Bool
otherwise
= () -> Either String ()
forall a b. b -> Either a b
Right ()
validExtension :: HasFullHeader block => AnchoredFragment block -> block -> Bool
validExtension :: forall block.
HasFullHeader block =>
AnchoredFragment block -> block -> Bool
validExtension AnchoredFragment block
af block
bSucc =
block -> Bool
forall b. HasFullHeader b => b -> Bool
blockInvariant block
bSucc Bool -> Bool -> Bool
&&
block
bSucc block -> Anchor block -> Bool
forall block. HasFullHeader block => block -> Anchor block -> Bool
`isValidSuccessorOf` AnchoredFragment block -> Anchor block
forall v a b. Anchorable v a b => AnchoredSeq v a b -> a
headAnchor AnchoredFragment block
af
headPoint :: HasHeader block => AnchoredFragment block -> Point block
headPoint :: forall block.
HasHeader block =>
AnchoredFragment block -> Point block
headPoint = Anchor block -> Point block
forall block. Anchor block -> Point block
anchorToPoint (Anchor block -> Point block)
-> (AnchoredFragment block -> Anchor block)
-> AnchoredFragment block
-> Point block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnchoredFragment block -> Anchor block
forall v a b. Anchorable v a b => AnchoredSeq v a b -> a
headAnchor
headSlot :: HasHeader block => AnchoredFragment block -> WithOrigin SlotNo
headSlot :: forall block.
HasHeader block =>
AnchoredFragment block -> WithOrigin SlotNo
headSlot = (Anchor block -> WithOrigin SlotNo)
-> (block -> WithOrigin SlotNo)
-> Either (Anchor block) block
-> WithOrigin SlotNo
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Anchor block -> WithOrigin SlotNo
forall block. Anchor block -> WithOrigin SlotNo
anchorToSlotNo (SlotNo -> WithOrigin SlotNo
forall t. t -> WithOrigin t
At (SlotNo -> WithOrigin SlotNo)
-> (block -> SlotNo) -> block -> WithOrigin SlotNo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. block -> SlotNo
forall b. HasHeader b => b -> SlotNo
blockSlot) (Either (Anchor block) block -> WithOrigin SlotNo)
-> (AnchoredFragment block -> Either (Anchor block) block)
-> AnchoredFragment block
-> WithOrigin SlotNo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnchoredFragment block -> Either (Anchor block) block
forall v a b. Anchorable v a b => AnchoredSeq v a b -> Either a b
head
headHash :: HasHeader block => AnchoredFragment block -> ChainHash block
headHash :: forall block.
HasHeader block =>
AnchoredFragment block -> ChainHash block
headHash = (Anchor block -> ChainHash block)
-> (block -> ChainHash block)
-> Either (Anchor block) block
-> ChainHash block
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Anchor block -> ChainHash block
forall block. Anchor block -> ChainHash block
anchorToHash (HeaderHash block -> ChainHash block
forall {k} (b :: k). HeaderHash b -> ChainHash b
BlockHash (HeaderHash block -> ChainHash block)
-> (block -> HeaderHash block) -> block -> ChainHash block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. block -> HeaderHash block
forall b. HasHeader b => b -> HeaderHash b
blockHash) (Either (Anchor block) block -> ChainHash block)
-> (AnchoredFragment block -> Either (Anchor block) block)
-> AnchoredFragment block
-> ChainHash block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnchoredFragment block -> Either (Anchor block) block
forall v a b. Anchorable v a b => AnchoredSeq v a b -> Either a b
head
headBlockNo :: HasHeader block => AnchoredFragment block -> WithOrigin BlockNo
headBlockNo :: forall block.
HasHeader block =>
AnchoredFragment block -> WithOrigin BlockNo
headBlockNo = (Anchor block -> WithOrigin BlockNo)
-> (block -> WithOrigin BlockNo)
-> Either (Anchor block) block
-> WithOrigin BlockNo
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Anchor block -> WithOrigin BlockNo
forall block. Anchor block -> WithOrigin BlockNo
anchorToBlockNo (BlockNo -> WithOrigin BlockNo
forall t. t -> WithOrigin t
At (BlockNo -> WithOrigin BlockNo)
-> (block -> BlockNo) -> block -> WithOrigin BlockNo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. block -> BlockNo
forall b. HasHeader b => b -> BlockNo
blockNo) (Either (Anchor block) block -> WithOrigin BlockNo)
-> (AnchoredFragment block -> Either (Anchor block) block)
-> AnchoredFragment block
-> WithOrigin BlockNo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnchoredFragment block -> Either (Anchor block) block
forall v a b. Anchorable v a b => AnchoredSeq v a b -> Either a b
head
lastPoint :: HasHeader block => AnchoredFragment block -> Point block
lastPoint :: forall block.
HasHeader block =>
AnchoredFragment block -> Point block
lastPoint = (Anchor block -> Point block)
-> (block -> Point block)
-> Either (Anchor block) block
-> Point block
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Anchor block -> Point block
forall block. Anchor block -> Point block
anchorToPoint block -> Point block
forall block. HasHeader block => block -> Point block
blockPoint (Either (Anchor block) block -> Point block)
-> (AnchoredFragment block -> Either (Anchor block) block)
-> AnchoredFragment block
-> Point block
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnchoredFragment block -> Either (Anchor block) block
forall v a b. Anchorable v a b => AnchoredSeq v a b -> Either a b
last
lastSlot :: HasHeader block => AnchoredFragment block -> WithOrigin SlotNo
lastSlot :: forall block.
HasHeader block =>
AnchoredFragment block -> WithOrigin SlotNo
lastSlot = (Anchor block -> WithOrigin SlotNo)
-> (block -> WithOrigin SlotNo)
-> Either (Anchor block) block
-> WithOrigin SlotNo
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Anchor block -> WithOrigin SlotNo
forall block. Anchor block -> WithOrigin SlotNo
anchorToSlotNo (SlotNo -> WithOrigin SlotNo
forall t. t -> WithOrigin t
At (SlotNo -> WithOrigin SlotNo)
-> (block -> SlotNo) -> block -> WithOrigin SlotNo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. block -> SlotNo
forall b. HasHeader b => b -> SlotNo
blockSlot) (Either (Anchor block) block -> WithOrigin SlotNo)
-> (AnchoredFragment block -> Either (Anchor block) block)
-> AnchoredFragment block
-> WithOrigin SlotNo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. AnchoredFragment block -> Either (Anchor block) block
forall v a b. Anchorable v a b => AnchoredSeq v a b -> Either a b
last
addBlock :: HasHeader block
=> block -> AnchoredFragment block -> AnchoredFragment block
addBlock :: forall block.
HasHeader block =>
block -> AnchoredFragment block -> AnchoredFragment block
addBlock block
b AnchoredFragment block
c = AnchoredFragment block
c AnchoredFragment block -> block -> AnchoredFragment block
forall v a b.
Anchorable v a b =>
AnchoredSeq v a b -> b -> AnchoredSeq v a b
:> block
b
rollback :: HasHeader block
=> Point block -> AnchoredFragment block
-> Maybe (AnchoredFragment block)
rollback :: forall block.
HasHeader block =>
Point block
-> AnchoredFragment block -> Maybe (AnchoredFragment block)
rollback Point block
p = WithOrigin SlotNo
-> (Either (Anchor block) block -> Bool)
-> AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
-> Maybe (AnchoredSeq (WithOrigin SlotNo) (Anchor block) block)
forall v a b.
Anchorable v a b =>
v
-> (Either a b -> Bool)
-> AnchoredSeq v a b
-> Maybe (AnchoredSeq v a b)
AS.rollback (Point block -> WithOrigin SlotNo
forall {k} (block :: k). Point block -> WithOrigin SlotNo
pointSlot Point block
p) ((Point block -> Point block -> Bool
forall a. Eq a => a -> a -> Bool
== Point block
p) (Point block -> Bool)
-> (Either (Anchor block) block -> Point block)
-> Either (Anchor block) block
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Anchor block -> Point block)
-> (block -> Point block)
-> Either (Anchor block) block
-> Point block
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Anchor block -> Point block
forall block. Anchor block -> Point block
anchorToPoint block -> Point block
forall block. HasHeader block => block -> Point block
blockPoint)
selectPoints ::
forall block. HasHeader block
=> [Int]
-> AnchoredFragment block
-> [Point block]
selectPoints :: forall block.
HasHeader block =>
[Int] -> AnchoredFragment block -> [Point block]
selectPoints [Int]
offsets =
(Either (Anchor block) block -> Point block)
-> [Either (Anchor block) block] -> [Point block]
forall a b. (a -> b) -> [a] -> [b]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Anchor block -> Point block)
-> (block -> Point block)
-> Either (Anchor block) block
-> Point block
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Anchor block -> Point block
forall block. Anchor block -> Point block
anchorToPoint block -> Point block
forall block. HasHeader block => block -> Point block
blockPoint) ([Either (Anchor block) block] -> [Point block])
-> (AnchoredFragment block -> [Either (Anchor block) block])
-> AnchoredFragment block
-> [Point block]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Int] -> AnchoredFragment block -> [Either (Anchor block) block]
forall v a b.
Anchorable v a b =>
[Int] -> AnchoredSeq v a b -> [Either a b]
AS.selectOffsets [Int]
offsets
selectPointsSpec :: HasHeader block
=> [Int] -> AnchoredFragment block -> [Point block]
selectPointsSpec :: forall block.
HasHeader block =>
[Int] -> AnchoredFragment block -> [Point block]
selectPointsSpec [Int]
offsets AnchoredFragment block
c =
[ [Point block]
ps [Point block] -> Int -> Point block
forall a. HasCallStack => [a] -> Int -> a
!! Int
offset
| let ps :: [Point block]
ps = (block -> Point block
forall block. HasHeader block => block -> Point block
blockPoint (block -> Point block) -> [block] -> [Point block]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AnchoredFragment block -> [block]
forall v a b. AnchoredSeq v a b -> [b]
toNewestFirst AnchoredFragment block
c) [Point block] -> [Point block] -> [Point block]
forall a. Semigroup a => a -> a -> a
<> [AnchoredFragment block -> Point block
forall block. AnchoredFragment block -> Point block
anchorPoint AnchoredFragment block
c]
len :: Int
len = [Point block] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
L.length [Point block]
ps
, Int
offset <- [Int]
offsets
, Int
offset Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
len
]
successorBlock :: HasHeader block
=> Point block -> AnchoredFragment block -> Maybe block
successorBlock :: forall block.
HasHeader block =>
Point block -> AnchoredFragment block -> Maybe block
successorBlock Point block
p AnchoredFragment block
af
| Point block
p Point block -> Point block -> Bool
forall a. Eq a => a -> a -> Bool
== AnchoredFragment block -> Point block
forall block. AnchoredFragment block -> Point block
anchorPoint AnchoredFragment block
af
= (Anchor block -> Maybe block)
-> (block -> Maybe block)
-> Either (Anchor block) block
-> Maybe block
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe block -> Anchor block -> Maybe block
forall a b. a -> b -> a
const Maybe block
forall a. Maybe a
Nothing) block -> Maybe block
forall a. a -> Maybe a
Just (Either (Anchor block) block -> Maybe block)
-> Either (Anchor block) block -> Maybe block
forall a b. (a -> b) -> a -> b
$ AnchoredFragment block -> Either (Anchor block) block
forall v a b. Anchorable v a b => AnchoredSeq v a b -> Either a b
last AnchoredFragment block
af
| Bool
otherwise
= case AnchoredFragment block
-> Point block
-> Maybe (AnchoredFragment block, AnchoredFragment block)
forall block1 block2.
(HasHeader block1, HeaderHash block1 ~ HeaderHash block2) =>
AnchoredFragment block1
-> Point block2
-> Maybe (AnchoredFragment block1, AnchoredFragment block1)
splitAfterPoint AnchoredFragment block
af Point block
p of
Just (AnchoredFragment block
_, block
b :< AnchoredFragment block
_) -> block -> Maybe block
forall a. a -> Maybe a
Just block
b
Maybe (AnchoredFragment block, AnchoredFragment block)
_otherwise -> Maybe block
forall a. Maybe a
Nothing
pointOnFragment :: HasHeader block
=> Point block -> AnchoredFragment block -> Bool
pointOnFragment :: forall block.
HasHeader block =>
Point block -> AnchoredFragment block -> Bool
pointOnFragment Point block
p = WithOrigin SlotNo
-> (block -> Bool)
-> AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
-> Bool
forall v a b.
Anchorable v a b =>
v -> (b -> Bool) -> AnchoredSeq v a b -> Bool
contains (Point block -> WithOrigin SlotNo
forall {k} (block :: k). Point block -> WithOrigin SlotNo
pointSlot Point block
p) ((Point block -> Point block -> Bool
forall a. Eq a => a -> a -> Bool
== Point block
p) (Point block -> Bool) -> (block -> Point block) -> block -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. block -> Point block
forall block. HasHeader block => block -> Point block
blockPoint)
pointOnFragmentSpec :: HasHeader block
=> Point block -> AnchoredFragment block -> Bool
pointOnFragmentSpec :: forall block.
HasHeader block =>
Point block -> AnchoredFragment block -> Bool
pointOnFragmentSpec Point block
p = AnchoredSeq (WithOrigin SlotNo) (Anchor block) block -> Bool
go
where
go :: AnchoredSeq (WithOrigin SlotNo) (Anchor block) block -> Bool
go (Empty Anchor block
_) = Bool
False
go (AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
c' :> block
b) | block -> Point block
forall block. HasHeader block => block -> Point block
blockPoint block
b Point block -> Point block -> Bool
forall a. Eq a => a -> a -> Bool
== Point block
p = Bool
True
| Bool
otherwise = AnchoredSeq (WithOrigin SlotNo) (Anchor block) block -> Bool
go AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
c'
withinFragmentBounds :: HasHeader block
=> Point block -> AnchoredFragment block -> Bool
withinFragmentBounds :: forall block.
HasHeader block =>
Point block -> AnchoredFragment block -> Bool
withinFragmentBounds Point block
p =
WithOrigin SlotNo
-> (Either (Anchor block) block -> Bool)
-> AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
-> Bool
forall v a b.
Anchorable v a b =>
v -> (Either a b -> Bool) -> AnchoredSeq v a b -> Bool
withinBounds
(Point block -> WithOrigin SlotNo
forall {k} (block :: k). Point block -> WithOrigin SlotNo
pointSlot Point block
p)
((Point block -> Point block -> Bool
forall a. Eq a => a -> a -> Bool
== Point block
p) (Point block -> Bool)
-> (Either (Anchor block) block -> Point block)
-> Either (Anchor block) block
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Anchor block -> Point block)
-> (block -> Point block)
-> Either (Anchor block) block
-> Point block
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Anchor block -> Point block
forall block. Anchor block -> Point block
anchorToPoint block -> Point block
forall block. HasHeader block => block -> Point block
blockPoint)
findFirstPoint
:: HasHeader block
=> [Point block]
-> AnchoredFragment block
-> Maybe (Point block)
findFirstPoint :: forall block.
HasHeader block =>
[Point block] -> AnchoredFragment block -> Maybe (Point block)
findFirstPoint [Point block]
ps AnchoredFragment block
c = (Point block -> Bool) -> [Point block] -> Maybe (Point block)
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Maybe a
L.find (Point block -> AnchoredFragment block -> Bool
forall block.
HasHeader block =>
Point block -> AnchoredFragment block -> Bool
`withinFragmentBounds` AnchoredFragment block
c) [Point block]
ps
applyChainUpdate :: HasHeader block
=> ChainUpdate block block
-> AnchoredFragment block
-> Maybe (AnchoredFragment block)
applyChainUpdate :: forall block.
HasHeader block =>
ChainUpdate block block
-> AnchoredFragment block -> Maybe (AnchoredFragment block)
applyChainUpdate (AddBlock block
b) AnchoredFragment block
c = AnchoredFragment block -> Maybe (AnchoredFragment block)
forall a. a -> Maybe a
Just (block -> AnchoredFragment block -> AnchoredFragment block
forall block.
HasHeader block =>
block -> AnchoredFragment block -> AnchoredFragment block
addBlock block
b AnchoredFragment block
c)
applyChainUpdate (RollBack Point block
p) AnchoredFragment block
c = Point block
-> AnchoredFragment block -> Maybe (AnchoredFragment block)
forall block.
HasHeader block =>
Point block
-> AnchoredFragment block -> Maybe (AnchoredFragment block)
rollback Point block
p AnchoredFragment block
c
applyChainUpdates :: HasHeader block
=> [ChainUpdate block block]
-> AnchoredFragment block
-> Maybe (AnchoredFragment block)
applyChainUpdates :: forall block.
HasHeader block =>
[ChainUpdate block block]
-> AnchoredFragment block -> Maybe (AnchoredFragment block)
applyChainUpdates [] AnchoredFragment block
c = AnchoredFragment block -> Maybe (AnchoredFragment block)
forall a. a -> Maybe a
Just AnchoredFragment block
c
applyChainUpdates (ChainUpdate block block
u:[ChainUpdate block block]
us) AnchoredFragment block
c = [ChainUpdate block block]
-> AnchoredFragment block -> Maybe (AnchoredFragment block)
forall block.
HasHeader block =>
[ChainUpdate block block]
-> AnchoredFragment block -> Maybe (AnchoredFragment block)
applyChainUpdates [ChainUpdate block block]
us (AnchoredFragment block -> Maybe (AnchoredFragment block))
-> Maybe (AnchoredFragment block) -> Maybe (AnchoredFragment block)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< ChainUpdate block block
-> AnchoredFragment block -> Maybe (AnchoredFragment block)
forall block.
HasHeader block =>
ChainUpdate block block
-> AnchoredFragment block -> Maybe (AnchoredFragment block)
applyChainUpdate ChainUpdate block block
u AnchoredFragment block
c
splitAfterPoint
:: forall block1 block2.
(HasHeader block1, HeaderHash block1 ~ HeaderHash block2)
=> AnchoredFragment block1
-> Point block2
-> Maybe (AnchoredFragment block1, AnchoredFragment block1)
splitAfterPoint :: forall block1 block2.
(HasHeader block1, HeaderHash block1 ~ HeaderHash block2) =>
AnchoredFragment block1
-> Point block2
-> Maybe (AnchoredFragment block1, AnchoredFragment block1)
splitAfterPoint AnchoredFragment block1
af Point block2
p =
WithOrigin SlotNo
-> (Either (Anchor block1) block1 -> Bool)
-> AnchoredFragment block1
-> Maybe (AnchoredFragment block1, AnchoredFragment block1)
forall v a b.
Anchorable v a b =>
v
-> (Either a b -> Bool)
-> AnchoredSeq v a b
-> Maybe (AnchoredSeq v a b, AnchoredSeq v a b)
splitAfterMeasure
(Point block2 -> WithOrigin SlotNo
forall {k} (block :: k). Point block -> WithOrigin SlotNo
pointSlot Point block2
p)
((Point block1 -> Point block1 -> Bool
forall a. Eq a => a -> a -> Bool
== Point block2 -> Point block1
forall {k1} {k2} (b :: k1) (b' :: k2).
Coercible (HeaderHash b) (HeaderHash b') =>
Point b -> Point b'
castPoint Point block2
p) (Point block1 -> Bool)
-> (Either (Anchor block1) block1 -> Point block1)
-> Either (Anchor block1) block1
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Anchor block1 -> Point block1)
-> (block1 -> Point block1)
-> Either (Anchor block1) block1
-> Point block1
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Anchor block1 -> Point block1
forall block. Anchor block -> Point block
anchorToPoint block1 -> Point block1
forall block. HasHeader block => block -> Point block
blockPoint)
AnchoredFragment block1
af
splitBeforePoint
:: forall block1 block2.
(HasHeader block1, HeaderHash block1 ~ HeaderHash block2)
=> AnchoredFragment block1
-> Point block2
-> Maybe (AnchoredFragment block1, AnchoredFragment block1)
splitBeforePoint :: forall block1 block2.
(HasHeader block1, HeaderHash block1 ~ HeaderHash block2) =>
AnchoredFragment block1
-> Point block2
-> Maybe (AnchoredFragment block1, AnchoredFragment block1)
splitBeforePoint AnchoredFragment block1
af Point block2
p =
WithOrigin SlotNo
-> (block1 -> Bool)
-> AnchoredFragment block1
-> Maybe (AnchoredFragment block1, AnchoredFragment block1)
forall v a b.
Anchorable v a b =>
v
-> (b -> Bool)
-> AnchoredSeq v a b
-> Maybe (AnchoredSeq v a b, AnchoredSeq v a b)
splitBeforeMeasure
(Point block2 -> WithOrigin SlotNo
forall {k} (block :: k). Point block -> WithOrigin SlotNo
pointSlot Point block2
p)
((Point block1 -> Point block1 -> Bool
forall a. Eq a => a -> a -> Bool
== Point block2 -> Point block1
forall {k1} {k2} (b :: k1) (b' :: k2).
Coercible (HeaderHash b) (HeaderHash b') =>
Point b -> Point b'
castPoint Point block2
p) (Point block1 -> Bool)
-> (block1 -> Point block1) -> block1 -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. block1 -> Point block1
forall block. HasHeader block => block -> Point block
blockPoint)
AnchoredFragment block1
af
splitAtSlot
:: HasHeader block
=> SlotNo
-> AnchoredFragment block
-> (AnchoredFragment block, AnchoredFragment block)
splitAtSlot :: forall block.
HasHeader block =>
SlotNo
-> AnchoredFragment block
-> (AnchoredFragment block, AnchoredFragment block)
splitAtSlot = WithOrigin SlotNo
-> AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
-> (AnchoredSeq (WithOrigin SlotNo) (Anchor block) block,
AnchoredSeq (WithOrigin SlotNo) (Anchor block) block)
forall v a b.
Anchorable v a b =>
v -> AnchoredSeq v a b -> (AnchoredSeq v a b, AnchoredSeq v a b)
splitAtMeasure (WithOrigin SlotNo
-> AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
-> (AnchoredSeq (WithOrigin SlotNo) (Anchor block) block,
AnchoredSeq (WithOrigin SlotNo) (Anchor block) block))
-> (SlotNo -> WithOrigin SlotNo)
-> SlotNo
-> AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
-> (AnchoredSeq (WithOrigin SlotNo) (Anchor block) block,
AnchoredSeq (WithOrigin SlotNo) (Anchor block) block)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SlotNo -> WithOrigin SlotNo
forall t. t -> WithOrigin t
At
sliceRange :: HasHeader block
=> AnchoredFragment block
-> Point block
-> Point block
-> Maybe (AnchoredFragment block)
sliceRange :: forall block.
HasHeader block =>
AnchoredFragment block
-> Point block -> Point block -> Maybe (AnchoredFragment block)
sliceRange AnchoredFragment block
af Point block
from Point block
to
| Just (AnchoredFragment block
_, AnchoredFragment block
af') <- AnchoredFragment block
-> Point block
-> Maybe (AnchoredFragment block, AnchoredFragment block)
forall block1 block2.
(HasHeader block1, HeaderHash block1 ~ HeaderHash block2) =>
AnchoredFragment block1
-> Point block2
-> Maybe (AnchoredFragment block1, AnchoredFragment block1)
splitBeforePoint AnchoredFragment block
af Point block
from
, Just (AnchoredFragment block
af'',AnchoredFragment block
_) <- AnchoredFragment block
-> Point block
-> Maybe (AnchoredFragment block, AnchoredFragment block)
forall block1 block2.
(HasHeader block1, HeaderHash block1 ~ HeaderHash block2) =>
AnchoredFragment block1
-> Point block2
-> Maybe (AnchoredFragment block1, AnchoredFragment block1)
splitAfterPoint AnchoredFragment block
af' Point block
to
= AnchoredFragment block -> Maybe (AnchoredFragment block)
forall a. a -> Maybe a
Just AnchoredFragment block
af''
| Bool
otherwise
= Maybe (AnchoredFragment block)
forall a. Maybe a
Nothing
join :: HasHeader block
=> AnchoredFragment block
-> AnchoredFragment block
-> Maybe (AnchoredFragment block)
join :: forall block.
HasHeader block =>
AnchoredFragment block
-> AnchoredFragment block -> Maybe (AnchoredFragment block)
join = (Either (Anchor block) block -> Anchor block -> Bool)
-> AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
-> AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
-> Maybe (AnchoredSeq (WithOrigin SlotNo) (Anchor block) block)
forall v a b.
Anchorable v a b =>
(Either a b -> a -> Bool)
-> AnchoredSeq v a b
-> AnchoredSeq v a b
-> Maybe (AnchoredSeq v a b)
AS.join ((Either (Anchor block) block -> Anchor block -> Bool)
-> AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
-> AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
-> Maybe (AnchoredSeq (WithOrigin SlotNo) (Anchor block) block))
-> (Either (Anchor block) block -> Anchor block -> Bool)
-> AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
-> AnchoredSeq (WithOrigin SlotNo) (Anchor block) block
-> Maybe (AnchoredSeq (WithOrigin SlotNo) (Anchor block) block)
forall a b. (a -> b) -> a -> b
$ \Either (Anchor block) block
aOrB Anchor block
a ->
(Anchor block -> Point block)
-> (block -> Point block)
-> Either (Anchor block) block
-> Point block
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either Anchor block -> Point block
forall block. Anchor block -> Point block
anchorToPoint block -> Point block
forall block. HasHeader block => block -> Point block
blockPoint Either (Anchor block) block
aOrB Point block -> Point block -> Bool
forall a. Eq a => a -> a -> Bool
== Anchor block -> Point block
forall block. Anchor block -> Point block
anchorToPoint Anchor block
a
intersect
:: forall block1 block2.
(HasHeader block1, HasHeader block2, HeaderHash block1 ~ HeaderHash block2)
=> AnchoredFragment block1
-> AnchoredFragment block2
-> Maybe (AnchoredFragment block1, AnchoredFragment block2,
AnchoredFragment block1, AnchoredFragment block2)
intersect :: forall block1 block2.
(HasHeader block1, HasHeader block2,
HeaderHash block1 ~ HeaderHash block2) =>
AnchoredFragment block1
-> AnchoredFragment block2
-> Maybe
(AnchoredFragment block1, AnchoredFragment block2,
AnchoredFragment block1, AnchoredFragment block2)
intersect AnchoredFragment block1
c1 AnchoredFragment block2
c2
| AnchoredFragment block2 -> Int
forall v a b. Anchorable v a b => AnchoredSeq v a b -> Int
length AnchoredFragment block2
c2 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> AnchoredFragment block1 -> Int
forall v a b. Anchorable v a b => AnchoredSeq v a b -> Int
length AnchoredFragment block1
c1
= (\(AnchoredFragment block2
p2, AnchoredFragment block1
p1, AnchoredFragment block2
s2, AnchoredFragment block1
s1) -> (AnchoredFragment block1
p1, AnchoredFragment block2
p2, AnchoredFragment block1
s1, AnchoredFragment block2
s2)) ((AnchoredFragment block2, AnchoredFragment block1,
AnchoredFragment block2, AnchoredFragment block1)
-> (AnchoredFragment block1, AnchoredFragment block2,
AnchoredFragment block1, AnchoredFragment block2))
-> Maybe
(AnchoredFragment block2, AnchoredFragment block1,
AnchoredFragment block2, AnchoredFragment block1)
-> Maybe
(AnchoredFragment block1, AnchoredFragment block2,
AnchoredFragment block1, AnchoredFragment block2)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AnchoredFragment block2
-> AnchoredFragment block1
-> Maybe
(AnchoredFragment block2, AnchoredFragment block1,
AnchoredFragment block2, AnchoredFragment block1)
forall block1 block2.
(HasHeader block1, HasHeader block2,
HeaderHash block1 ~ HeaderHash block2) =>
AnchoredFragment block1
-> AnchoredFragment block2
-> Maybe
(AnchoredFragment block1, AnchoredFragment block2,
AnchoredFragment block1, AnchoredFragment block2)
intersect AnchoredFragment block2
c2 AnchoredFragment block1
c1
| Point block1 -> WithOrigin SlotNo
forall {k} (block :: k). Point block -> WithOrigin SlotNo
pointSlot (AnchoredFragment block1 -> Point block1
forall block.
HasHeader block =>
AnchoredFragment block -> Point block
headPoint AnchoredFragment block1
c1) WithOrigin SlotNo -> WithOrigin SlotNo -> Bool
forall a. Ord a => a -> a -> Bool
< Point block2 -> WithOrigin SlotNo
forall {k} (block :: k). Point block -> WithOrigin SlotNo
pointSlot (AnchoredFragment block2 -> Point block2
forall block. AnchoredFragment block -> Point block
anchorPoint AnchoredFragment block2
c2) Bool -> Bool -> Bool
||
Point block2 -> WithOrigin SlotNo
forall {k} (block :: k). Point block -> WithOrigin SlotNo
pointSlot (AnchoredFragment block2 -> Point block2
forall block.
HasHeader block =>
AnchoredFragment block -> Point block
headPoint AnchoredFragment block2
c2) WithOrigin SlotNo -> WithOrigin SlotNo -> Bool
forall a. Ord a => a -> a -> Bool
< Point block1 -> WithOrigin SlotNo
forall {k} (block :: k). Point block -> WithOrigin SlotNo
pointSlot (AnchoredFragment block1 -> Point block1
forall block. AnchoredFragment block -> Point block
anchorPoint AnchoredFragment block1
c1)
= Maybe
(AnchoredFragment block1, AnchoredFragment block2,
AnchoredFragment block1, AnchoredFragment block2)
forall a. Maybe a
Nothing
| Bool
otherwise
= AnchoredFragment block2
-> Maybe
(AnchoredFragment block1, AnchoredFragment block2,
AnchoredFragment block1, AnchoredFragment block2)
go AnchoredFragment block2
c2
where
go :: AnchoredFragment block2
-> Maybe (AnchoredFragment block1, AnchoredFragment block2,
AnchoredFragment block1, AnchoredFragment block2)
go :: AnchoredFragment block2
-> Maybe
(AnchoredFragment block1, AnchoredFragment block2,
AnchoredFragment block1, AnchoredFragment block2)
go (Empty Anchor block2
a2)
| Just (AnchoredFragment block1
p1, AnchoredFragment block1
s1) <- AnchoredFragment block1
-> Point block2
-> Maybe (AnchoredFragment block1, AnchoredFragment block1)
forall block1 block2.
(HasHeader block1, HeaderHash block1 ~ HeaderHash block2) =>
AnchoredFragment block1
-> Point block2
-> Maybe (AnchoredFragment block1, AnchoredFragment block1)
splitAfterPoint AnchoredFragment block1
c1 (Anchor block2 -> Point block2
forall block. Anchor block -> Point block
anchorToPoint Anchor block2
a2)
= (AnchoredFragment block1, AnchoredFragment block2,
AnchoredFragment block1, AnchoredFragment block2)
-> Maybe
(AnchoredFragment block1, AnchoredFragment block2,
AnchoredFragment block1, AnchoredFragment block2)
forall a. a -> Maybe a
Just (AnchoredFragment block1
p1, Anchor block2 -> AnchoredFragment block2
forall v a b. Anchorable v a b => a -> AnchoredSeq v a b
Empty Anchor block2
a2, AnchoredFragment block1
s1, AnchoredFragment block2
c2)
| Bool
otherwise
= Maybe
(AnchoredFragment block1, AnchoredFragment block2,
AnchoredFragment block1, AnchoredFragment block2)
forall a. Maybe a
Nothing
go (AnchoredFragment block2
c2' :> block2
b)
| let pt :: Point block2
pt = block2 -> Point block2
forall block. HasHeader block => block -> Point block
blockPoint block2
b
, Just (AnchoredFragment block1
p1, AnchoredFragment block1
s1) <- AnchoredFragment block1
-> Point block2
-> Maybe (AnchoredFragment block1, AnchoredFragment block1)
forall block1 block2.
(HasHeader block1, HeaderHash block1 ~ HeaderHash block2) =>
AnchoredFragment block1
-> Point block2
-> Maybe (AnchoredFragment block1, AnchoredFragment block1)
splitAfterPoint AnchoredFragment block1
c1 Point block2
pt
, Just (AnchoredFragment block2
p2, AnchoredFragment block2
s2) <- AnchoredFragment block2
-> Point block2
-> Maybe (AnchoredFragment block2, AnchoredFragment block2)
forall block1 block2.
(HasHeader block1, HeaderHash block1 ~ HeaderHash block2) =>
AnchoredFragment block1
-> Point block2
-> Maybe (AnchoredFragment block1, AnchoredFragment block1)
splitAfterPoint AnchoredFragment block2
c2 Point block2
pt
= (AnchoredFragment block1, AnchoredFragment block2,
AnchoredFragment block1, AnchoredFragment block2)
-> Maybe
(AnchoredFragment block1, AnchoredFragment block2,
AnchoredFragment block1, AnchoredFragment block2)
forall a. a -> Maybe a
Just (AnchoredFragment block1
p1, AnchoredFragment block2
p2, AnchoredFragment block1
s1, AnchoredFragment block2
s2)
| Bool
otherwise
= AnchoredFragment block2
-> Maybe
(AnchoredFragment block1, AnchoredFragment block2,
AnchoredFragment block1, AnchoredFragment block2)
go AnchoredFragment block2
c2'
intersectionPoint
:: (HasHeader block1, HasHeader block2, HeaderHash block1 ~ HeaderHash block2)
=> AnchoredFragment block1
-> AnchoredFragment block2
-> Maybe (Point block1)
intersectionPoint :: forall block1 block2.
(HasHeader block1, HasHeader block2,
HeaderHash block1 ~ HeaderHash block2) =>
AnchoredFragment block1
-> AnchoredFragment block2 -> Maybe (Point block1)
intersectionPoint AnchoredFragment block1
c1 AnchoredFragment block2
c2 = case AnchoredFragment block1
c1 AnchoredFragment block1
-> AnchoredFragment block2
-> Maybe
(AnchoredFragment block1, AnchoredFragment block2,
AnchoredFragment block1, AnchoredFragment block2)
forall block1 block2.
(HasHeader block1, HasHeader block2,
HeaderHash block1 ~ HeaderHash block2) =>
AnchoredFragment block1
-> AnchoredFragment block2
-> Maybe
(AnchoredFragment block1, AnchoredFragment block2,
AnchoredFragment block1, AnchoredFragment block2)
`intersect` AnchoredFragment block2
c2 of
Just (AnchoredFragment block1
_, AnchoredFragment block2
_, AnchoredFragment block1
s1, AnchoredFragment block2
_) -> Point block1 -> Maybe (Point block1)
forall a. a -> Maybe a
Just (AnchoredFragment block1 -> Point block1
forall block. AnchoredFragment block -> Point block
anchorPoint AnchoredFragment block1
s1)
Maybe
(AnchoredFragment block1, AnchoredFragment block2,
AnchoredFragment block1, AnchoredFragment block2)
Nothing -> Maybe (Point block1)
forall a. Maybe a
Nothing
mapAnchoredFragment ::
(HasHeader block2, HeaderHash block1 ~ HeaderHash block2)
=> (block1 -> block2)
-> AnchoredFragment block1
-> AnchoredFragment block2
mapAnchoredFragment :: forall block2 block1.
(HasHeader block2, HeaderHash block1 ~ HeaderHash block2) =>
(block1 -> block2)
-> AnchoredFragment block1 -> AnchoredFragment block2
mapAnchoredFragment = (Anchor block1 -> Anchor block2)
-> (block1 -> block2)
-> AnchoredSeq (WithOrigin SlotNo) (Anchor block1) block1
-> AnchoredSeq (WithOrigin SlotNo) (Anchor block2) block2
forall v2 a2 b2 a1 b1 v1.
Anchorable v2 a2 b2 =>
(a1 -> a2)
-> (b1 -> b2) -> AnchoredSeq v1 a1 b1 -> AnchoredSeq v2 a2 b2
bimap Anchor block1 -> Anchor block2
forall a b. (HeaderHash a ~ HeaderHash b) => Anchor a -> Anchor b
castAnchor