{-# LANGUAGE DeriveFunctor #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE PatternSynonyms #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
module Ouroboros.Consensus.Fragment.Validated (
ValidatedFragment (ValidatedFragment)
, validatedFragment
, validatedLedger
, validatedTip
) where
import GHC.Stack
import Ouroboros.Consensus.Block
import Ouroboros.Consensus.Ledger.Abstract
import Ouroboros.Consensus.Util.Assert
import Ouroboros.Network.AnchoredFragment (AnchoredFragment)
import qualified Ouroboros.Network.AnchoredFragment as AF
data ValidatedFragment b l = UnsafeValidatedFragment {
forall b l. ValidatedFragment b l -> AnchoredFragment b
validatedFragment :: !(AnchoredFragment b)
, forall b l. ValidatedFragment b l -> l
validatedLedger :: !l
}
deriving ((forall a b.
(a -> b) -> ValidatedFragment b a -> ValidatedFragment b b)
-> (forall a b.
a -> ValidatedFragment b b -> ValidatedFragment b a)
-> Functor (ValidatedFragment b)
forall a b. a -> ValidatedFragment b b -> ValidatedFragment b a
forall a b.
(a -> b) -> ValidatedFragment b a -> ValidatedFragment b b
forall b a b. a -> ValidatedFragment b b -> ValidatedFragment b a
forall b a b.
(a -> b) -> ValidatedFragment b a -> ValidatedFragment b b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
$cfmap :: forall b a b.
(a -> b) -> ValidatedFragment b a -> ValidatedFragment b b
fmap :: forall a b.
(a -> b) -> ValidatedFragment b a -> ValidatedFragment b b
$c<$ :: forall b a b. a -> ValidatedFragment b b -> ValidatedFragment b a
<$ :: forall a b. a -> ValidatedFragment b b -> ValidatedFragment b a
Functor)
{-# COMPLETE ValidatedFragment #-}
pattern ValidatedFragment ::
(GetTip l, HasHeader b, HeaderHash b ~ HeaderHash l, HasCallStack)
=> AnchoredFragment b -> l -> ValidatedFragment b l
pattern $mValidatedFragment :: forall {r} {l} {b}.
(GetTip l, HasHeader b, HeaderHash b ~ HeaderHash l,
HasCallStack) =>
ValidatedFragment b l
-> (AnchoredFragment b -> l -> r) -> ((# #) -> r) -> r
$bValidatedFragment :: forall l b.
(GetTip l, HasHeader b, HeaderHash b ~ HeaderHash l,
HasCallStack) =>
AnchoredFragment b -> l -> ValidatedFragment b l
ValidatedFragment f l <- UnsafeValidatedFragment f l
where
ValidatedFragment AnchoredFragment b
f l
l = AnchoredFragment b -> l -> ValidatedFragment b l
forall l b.
(GetTip l, HasHeader b, HeaderHash b ~ HeaderHash l,
HasCallStack) =>
AnchoredFragment b -> l -> ValidatedFragment b l
new AnchoredFragment b
f l
l
validatedTip :: HasHeader b => ValidatedFragment b l -> Point b
validatedTip :: forall b l. HasHeader b => ValidatedFragment b l -> Point b
validatedTip = AnchoredFragment b -> Point b
forall block.
HasHeader block =>
AnchoredFragment block -> Point block
AF.headPoint (AnchoredFragment b -> Point b)
-> (ValidatedFragment b l -> AnchoredFragment b)
-> ValidatedFragment b l
-> Point b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ValidatedFragment b l -> AnchoredFragment b
forall b l. ValidatedFragment b l -> AnchoredFragment b
validatedFragment
invariant ::
forall l b.
(GetTip l, HasHeader b, HeaderHash b ~ HeaderHash l)
=> ValidatedFragment b l
-> Either String ()
invariant :: forall l b.
(GetTip l, HasHeader b, HeaderHash b ~ HeaderHash l) =>
ValidatedFragment b l -> Either String ()
invariant (ValidatedFragment AnchoredFragment b
fragment l
ledger)
| Point b
ledgerTip Point b -> Point b -> Bool
forall a. Eq a => a -> a -> Bool
/= Point b
headPoint
= String -> Either String ()
forall a b. a -> Either a b
Left (String -> Either String ()) -> String -> Either String ()
forall a b. (a -> b) -> a -> b
$ [String] -> String
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [
String
"ledger tip "
, Point b -> String
forall a. Show a => a -> String
show Point b
ledgerTip
, String
" /= head point "
, Point b -> String
forall a. Show a => a -> String
show Point b
headPoint
]
| Bool
otherwise
= () -> Either String ()
forall a b. b -> Either a b
Right ()
where
ledgerTip, headPoint :: Point b
ledgerTip :: Point b
ledgerTip = Point l -> Point b
forall {k1} {k2} (b :: k1) (b' :: k2).
Coercible (HeaderHash b) (HeaderHash b') =>
Point b -> Point b'
castPoint (Point l -> Point b) -> Point l -> Point b
forall a b. (a -> b) -> a -> b
$ l -> Point l
forall l. GetTip l => l -> Point l
getTip l
ledger
headPoint :: Point b
headPoint = Point b -> Point b
forall {k1} {k2} (b :: k1) (b' :: k2).
Coercible (HeaderHash b) (HeaderHash b') =>
Point b -> Point b'
castPoint (Point b -> Point b) -> Point b -> Point b
forall a b. (a -> b) -> a -> b
$ AnchoredFragment b -> Point b
forall block.
HasHeader block =>
AnchoredFragment block -> Point block
AF.headPoint AnchoredFragment b
fragment
new ::
forall l b.
(GetTip l, HasHeader b, HeaderHash b ~ HeaderHash l, HasCallStack)
=> AnchoredFragment b
-> l
-> ValidatedFragment b l
new :: forall l b.
(GetTip l, HasHeader b, HeaderHash b ~ HeaderHash l,
HasCallStack) =>
AnchoredFragment b -> l -> ValidatedFragment b l
new AnchoredFragment b
fragment l
ledger =
Either String () -> ValidatedFragment b l -> ValidatedFragment b l
forall a. HasCallStack => Either String () -> a -> a
assertWithMsg (ValidatedFragment b l -> Either String ()
forall l b.
(GetTip l, HasHeader b, HeaderHash b ~ HeaderHash l) =>
ValidatedFragment b l -> Either String ()
invariant ValidatedFragment b l
validated) (ValidatedFragment b l -> ValidatedFragment b l)
-> ValidatedFragment b l -> ValidatedFragment b l
forall a b. (a -> b) -> a -> b
$
ValidatedFragment b l
validated
where
validated :: ValidatedFragment b l
validated :: ValidatedFragment b l
validated = UnsafeValidatedFragment {
validatedFragment :: AnchoredFragment b
validatedFragment = AnchoredFragment b
fragment
, validatedLedger :: l
validatedLedger = l
ledger
}