{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
module Ouroboros.Consensus.MiniProtocol.LeiosFetch.Kernel
( LeiosFetchKernel (..)
, LeiosFetchPeer (..)
, LeiosPeerLifetimeId (..)
, newLeiosFetchKernel
, withLeiosFetchPeer
, recordLeiosOffer
, recordLeiosCertOffer
, recordLeiosJumpOffers
, ExnLeiosTwoCertificationClaims (..)
, enqueueLeiosAnnouncement
, leiosFetchAnnouncementFocuser
, onForgedLeiosEb
, leiosFetchCertificateFocuser
, leiosFetchDecisionLoop
, pruneLeiosFetchToImmTip
) where
import Control.Monad (forM_, forever, unless, void, when)
import Control.Monad.Except (runExcept)
import qualified Data.Foldable as F
import Data.List.NonEmpty (NonEmpty)
import qualified Data.Map.NonEmpty as NEMap
import qualified Data.Map.Strict as Map
import Data.Maybe.Strict (StrictMaybe (..))
import Data.Sequence (Seq)
import qualified Data.Sequence as Seq
import qualified Data.Sequence.NonEmpty as NESeq
import Data.Void (Void)
import Data.Word (Word64)
import Ouroboros.Consensus.Block
( BlockProtocol
, BlockSupportsProtocol (..)
, Header
, Point (GenesisPoint)
, Proxy (..)
, SlotNo (..)
, WithOrigin (..)
, blockPoint
, castPoint
, pointSlot
, withOrigin
)
import Ouroboros.Consensus.BlockchainTime (BlockchainTime (..), CurrentSlot (..))
import Ouroboros.Consensus.Config (TopLevelConfig, configBlock, configLedger)
import Ouroboros.Consensus.Forecast (forecastFor)
import Ouroboros.Consensus.HardFork.Abstract (HasHardForkHistory (..))
import qualified Ouroboros.Consensus.HardFork.History as HardFork
import Ouroboros.Consensus.HeaderStateHistory
( HeaderStateHistory (..)
, HeaderStateWithTime (..)
)
import Ouroboros.Consensus.HeaderValidation
( AnnTip (..)
, HeaderState (..)
, HeaderWithTime (..)
, headerStatePoint
)
import Ouroboros.Consensus.Ledger.Abstract (getTipSlot)
import Ouroboros.Consensus.Ledger.Extended (ledgerState)
import Ouroboros.Consensus.Ledger.SupportsProtocol (LedgerSupportsProtocol (..))
import Ouroboros.Consensus.Leios.Cert (LeiosClaim (..))
import Ouroboros.Consensus.Leios.Protocol
( LeiosAnnouncement (..)
, LeiosLedgerView (..)
, LeiosProtocol (..)
, leiosEbByteSizeBounds
)
import Ouroboros.Consensus.Leios.TxCache.API (evictOlderThan)
import Ouroboros.Consensus.Leios.Types
( ElectionId (..)
, EndorserBlockHash
, ForgedEndorserBlock
, LeiosEbByteSizes (..)
, LeiosPoint (..)
, minCertificationSlot
)
import Ouroboros.Consensus.MiniProtocol.ChainSync.Client.State (JumpInfo (..))
import Ouroboros.Consensus.MiniProtocol.LeiosFetch.Client.Replies
( LeiosFetchClientContext (..)
, acquireForgedEb
)
import Ouroboros.Consensus.MiniProtocol.LeiosFetch.Decision
( LeiosFetchRequest
, LeiosOutstanding (..)
, OfferedHalves (..)
, PeerOffer (..)
, focusCertifiedEb
, focusElectionIfUnfocused
, initializeLeiosOutstanding
, leiosFetchLogicIteration
, pruneOffers
, pruneOutstandingToImmTip
, recordReferencedPoint
, removePeerFromOutstanding
)
import Ouroboros.Consensus.MiniProtocol.LeiosNotify.Client.Announcements.Validation
( AnnouncingHeader (..)
, ValidAnnouncement (..)
)
import Ouroboros.Consensus.Storage.ChainDB.API (ChainDB)
import qualified Ouroboros.Consensus.Storage.ChainDB.API as ChainDB
import Ouroboros.Consensus.Storage.LeiosDB.API
( LeiosDbHandle
, LeiosDbReader (..)
, withReader
)
import Ouroboros.Consensus.Util.IOLike
( DiffTime
, Exception
, IOLike
, STM
, StrictMVar
, StrictTVar
, TQueue
, atomically
, bracket
, diffTime
, getMonotonicTime
, modifyMVar
, modifyMVar_
, modifyTVar
, newTQueueIO
, readTQueue
, readTVar
, readTVarIO
, takeMVar
, threadDelay
, throwIO
, tryPutMVar
, uncheckedNewEmptyMVar
, uncheckedNewMVar
, uncheckedNewTVarM
, writeTQueue
, writeTVar
)
import qualified Ouroboros.Network.AnchoredFragment as AF
import qualified Ouroboros.Network.AnchoredSeq as AS
import Ouroboros.Network.PeerSelection.LedgerPeers.Type (IsBigLedgerPeer)
import System.Random (StdGen)
data LeiosFetchKernel m peer blk = MkLeiosFetchKernel
{ forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk
-> LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
lfkContext :: !(LeiosFetchClientContext m (LeiosPeerLifetimeId peer))
, forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk
-> StrictTVar
m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk))
lfkPeers :: !(StrictTVar m (Map.Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk)))
, forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk -> StrictTVar m Word64
lfkNextLifetimeId :: !(StrictTVar m Word64)
, forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk -> StrictTVar m SlotNo
lfkPrunedSlot :: !(StrictTVar m SlotNo)
, forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk -> TQueue m (ValidAnnouncement blk)
lfkAnnouncements :: !(TQueue m (ValidAnnouncement blk))
}
data LeiosPeerLifetimeId peer = MkLeiosPeerLifetimeId !peer !Word64
deriving (LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool
(LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool)
-> (LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool)
-> Eq (LeiosPeerLifetimeId peer)
forall peer.
Eq peer =>
LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: forall peer.
Eq peer =>
LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool
== :: LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool
$c/= :: forall peer.
Eq peer =>
LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool
/= :: LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool
Eq, Eq (LeiosPeerLifetimeId peer)
Eq (LeiosPeerLifetimeId peer) =>
(LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Ordering)
-> (LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool)
-> (LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool)
-> (LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool)
-> (LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool)
-> (LeiosPeerLifetimeId peer
-> LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer)
-> (LeiosPeerLifetimeId peer
-> LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer)
-> Ord (LeiosPeerLifetimeId peer)
LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool
LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Ordering
LeiosPeerLifetimeId peer
-> LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer
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
forall peer. Ord peer => Eq (LeiosPeerLifetimeId peer)
forall peer.
Ord peer =>
LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool
forall peer.
Ord peer =>
LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Ordering
forall peer.
Ord peer =>
LeiosPeerLifetimeId peer
-> LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer
$ccompare :: forall peer.
Ord peer =>
LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Ordering
compare :: LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Ordering
$c< :: forall peer.
Ord peer =>
LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool
< :: LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool
$c<= :: forall peer.
Ord peer =>
LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool
<= :: LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool
$c> :: forall peer.
Ord peer =>
LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool
> :: LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool
$c>= :: forall peer.
Ord peer =>
LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool
>= :: LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer -> Bool
$cmax :: forall peer.
Ord peer =>
LeiosPeerLifetimeId peer
-> LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer
max :: LeiosPeerLifetimeId peer
-> LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer
$cmin :: forall peer.
Ord peer =>
LeiosPeerLifetimeId peer
-> LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer
min :: LeiosPeerLifetimeId peer
-> LeiosPeerLifetimeId peer -> LeiosPeerLifetimeId peer
Ord, Int -> LeiosPeerLifetimeId peer -> ShowS
[LeiosPeerLifetimeId peer] -> ShowS
LeiosPeerLifetimeId peer -> String
(Int -> LeiosPeerLifetimeId peer -> ShowS)
-> (LeiosPeerLifetimeId peer -> String)
-> ([LeiosPeerLifetimeId peer] -> ShowS)
-> Show (LeiosPeerLifetimeId peer)
forall peer. Show peer => Int -> LeiosPeerLifetimeId peer -> ShowS
forall peer. Show peer => [LeiosPeerLifetimeId peer] -> ShowS
forall peer. Show peer => LeiosPeerLifetimeId peer -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: forall peer. Show peer => Int -> LeiosPeerLifetimeId peer -> ShowS
showsPrec :: Int -> LeiosPeerLifetimeId peer -> ShowS
$cshow :: forall peer. Show peer => LeiosPeerLifetimeId peer -> String
show :: LeiosPeerLifetimeId peer -> String
$cshowList :: forall peer. Show peer => [LeiosPeerLifetimeId peer] -> ShowS
showList :: [LeiosPeerLifetimeId peer] -> ShowS
Show)
data LeiosFetchPeer m blk = MkLeiosFetchPeer
{ forall (m :: * -> *) blk. LeiosFetchPeer m blk -> IsBigLedgerPeer
lfpIsBigLedgerPeer :: !IsBigLedgerPeer
, forall (m :: * -> *) blk.
LeiosFetchPeer m blk -> StrictTVar m (Map LeiosPoint PeerOffer)
lfpOfferings :: !(StrictTVar m (Map.Map LeiosPoint PeerOffer))
, forall (m :: * -> *) blk.
LeiosFetchPeer m blk -> StrictTVar m (Seq LeiosFetchRequest)
lfpRequestsToSend :: !(StrictTVar m (Seq LeiosFetchRequest))
, forall (m :: * -> *) blk.
LeiosFetchPeer m blk
-> StrictTVar m (Map ElectionId EndorserBlockHash)
lfpCertificationClaims :: !(StrictTVar m (Map.Map ElectionId EndorserBlockHash))
, forall (m :: * -> *) blk.
LeiosFetchPeer m blk -> StrictTVar m (Point blk)
lfpJumpedThrough :: !(StrictTVar m (Point blk))
}
newLeiosFetchKernel ::
(IOLike m, LedgerSupportsProtocol blk) =>
ChainDB m blk ->
LeiosDbHandle m ->
StdGen ->
( StrictMVar m (LeiosOutstanding (LeiosPeerLifetimeId peer)) ->
StrictMVar m () ->
LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
) ->
m (LeiosFetchKernel m peer blk)
newLeiosFetchKernel :: forall (m :: * -> *) blk peer.
(IOLike m, LedgerSupportsProtocol blk) =>
ChainDB m blk
-> LeiosDbHandle m
-> StdGen
-> (StrictMVar m (LeiosOutstanding (LeiosPeerLifetimeId peer))
-> StrictMVar m ()
-> LeiosFetchClientContext m (LeiosPeerLifetimeId peer))
-> m (LeiosFetchKernel m peer blk)
newLeiosFetchKernel ChainDB m blk
chainDB LeiosDbHandle m
leiosDb StdGen
prng StrictMVar m (LeiosOutstanding (LeiosPeerLifetimeId peer))
-> StrictMVar m ()
-> LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
mkContext = do
immTipSlot <-
STM m SlotNo -> m SlotNo
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM m SlotNo -> m SlotNo) -> STM m SlotNo -> m SlotNo
forall a b. (a -> b) -> a -> b
$
SlotNo -> (SlotNo -> SlotNo) -> WithOrigin SlotNo -> SlotNo
forall b t. b -> (t -> b) -> WithOrigin t -> b
withOrigin (Word64 -> SlotNo
SlotNo Word64
0) SlotNo -> SlotNo
forall a. a -> a
id (WithOrigin SlotNo -> SlotNo)
-> (ExtLedgerState blk EmptyMK -> WithOrigin SlotNo)
-> ExtLedgerState blk EmptyMK
-> SlotNo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LedgerState blk EmptyMK -> WithOrigin SlotNo
forall (l :: LedgerStateKind) (mk :: MapKind).
GetTip l =>
l mk -> WithOrigin SlotNo
getTipSlot (LedgerState blk EmptyMK -> WithOrigin SlotNo)
-> (ExtLedgerState blk EmptyMK -> LedgerState blk EmptyMK)
-> ExtLedgerState blk EmptyMK
-> WithOrigin SlotNo
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExtLedgerState blk EmptyMK -> LedgerState blk EmptyMK
forall blk (mk :: MapKind).
ExtLedgerState blk mk -> LedgerState blk mk
ledgerState (ExtLedgerState blk EmptyMK -> SlotNo)
-> STM m (ExtLedgerState blk EmptyMK) -> STM m SlotNo
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ChainDB m blk -> STM m (ExtLedgerState blk EmptyMK)
forall (m :: * -> *) blk.
ChainDB m blk -> STM m (ExtLedgerState blk EmptyMK)
ChainDB.getImmutableLedger ChainDB m blk
chainDB
acquired <- withReader leiosDb $ \LeiosDbReader m
reader ->
LeiosDbReader m -> HasCallStack => SlotNo -> m [LeiosPoint]
forall (m :: * -> *).
LeiosDbReader m -> HasCallStack => SlotNo -> m [LeiosPoint]
scanCompleteEbClosuresNotOlderThanSlot LeiosDbReader m
reader SlotNo
immTipSlot
outstanding <- uncheckedNewMVar $ initializeLeiosOutstanding prng acquired immTipSlot
ready <- uncheckedNewEmptyMVar
lfkPeers <- uncheckedNewTVarM Map.empty
lfkNextLifetimeId <- uncheckedNewTVarM 0
lfkPrunedSlot <- uncheckedNewTVarM immTipSlot
lfkAnnouncements <- newTQueueIO
pure
MkLeiosFetchKernel
{ lfkContext = mkContext outstanding ready
, lfkPeers
, lfkNextLifetimeId
, lfkPrunedSlot
, lfkAnnouncements
}
withLeiosFetchPeer ::
(IOLike m, Ord peer) =>
LeiosFetchKernel m peer blk ->
peer ->
IsBigLedgerPeer ->
(LeiosPeerLifetimeId peer -> LeiosFetchPeer m blk -> m a) ->
m a
withLeiosFetchPeer :: forall (m :: * -> *) peer blk a.
(IOLike m, Ord peer) =>
LeiosFetchKernel m peer blk
-> peer
-> IsBigLedgerPeer
-> (LeiosPeerLifetimeId peer -> LeiosFetchPeer m blk -> m a)
-> m a
withLeiosFetchPeer LeiosFetchKernel m peer blk
kernel peer
peer IsBigLedgerPeer
isBig LeiosPeerLifetimeId peer -> LeiosFetchPeer m blk -> m a
k = m (LeiosPeerLifetimeId peer, LeiosFetchPeer m blk)
-> ((LeiosPeerLifetimeId peer, LeiosFetchPeer m blk) -> m ())
-> ((LeiosPeerLifetimeId peer, LeiosFetchPeer m blk) -> m a)
-> m a
forall a b c. m a -> (a -> m b) -> (a -> m c) -> m c
forall (m :: * -> *) a b c.
MonadThrow m =>
m a -> (a -> m b) -> (a -> m c) -> m c
bracket m (LeiosPeerLifetimeId peer, LeiosFetchPeer m blk)
register (LeiosPeerLifetimeId peer, LeiosFetchPeer m blk) -> m ()
forall {b}. (LeiosPeerLifetimeId peer, b) -> m ()
unregister ((LeiosPeerLifetimeId peer -> LeiosFetchPeer m blk -> m a)
-> (LeiosPeerLifetimeId peer, LeiosFetchPeer m blk) -> m a
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry LeiosPeerLifetimeId peer -> LeiosFetchPeer m blk -> m a
k)
where
MkLeiosFetchKernel{LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
lfkContext :: forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk
-> LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
lfkContext :: LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
lfkContext, StrictTVar
m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk))
lfkPeers :: forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk
-> StrictTVar
m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk))
lfkPeers :: StrictTVar
m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk))
lfkPeers, StrictTVar m Word64
lfkNextLifetimeId :: forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk -> StrictTVar m Word64
lfkNextLifetimeId :: StrictTVar m Word64
lfkNextLifetimeId} = LeiosFetchKernel m peer blk
kernel
register :: m (LeiosPeerLifetimeId peer, LeiosFetchPeer m blk)
register = do
fresh <-
IsBigLedgerPeer
-> StrictTVar m (Map LeiosPoint PeerOffer)
-> StrictTVar m (Seq LeiosFetchRequest)
-> StrictTVar m (Map ElectionId EndorserBlockHash)
-> StrictTVar m (Point blk)
-> LeiosFetchPeer m blk
forall (m :: * -> *) blk.
IsBigLedgerPeer
-> StrictTVar m (Map LeiosPoint PeerOffer)
-> StrictTVar m (Seq LeiosFetchRequest)
-> StrictTVar m (Map ElectionId EndorserBlockHash)
-> StrictTVar m (Point blk)
-> LeiosFetchPeer m blk
MkLeiosFetchPeer IsBigLedgerPeer
isBig
(StrictTVar m (Map LeiosPoint PeerOffer)
-> StrictTVar m (Seq LeiosFetchRequest)
-> StrictTVar m (Map ElectionId EndorserBlockHash)
-> StrictTVar m (Point blk)
-> LeiosFetchPeer m blk)
-> m (StrictTVar m (Map LeiosPoint PeerOffer))
-> m (StrictTVar m (Seq LeiosFetchRequest)
-> StrictTVar m (Map ElectionId EndorserBlockHash)
-> StrictTVar m (Point blk)
-> LeiosFetchPeer m blk)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Map LeiosPoint PeerOffer
-> m (StrictTVar m (Map LeiosPoint PeerOffer))
forall (m :: * -> *) a. MonadSTM m => a -> m (StrictTVar m a)
uncheckedNewTVarM Map LeiosPoint PeerOffer
forall k a. Map k a
Map.empty
m (StrictTVar m (Seq LeiosFetchRequest)
-> StrictTVar m (Map ElectionId EndorserBlockHash)
-> StrictTVar m (Point blk)
-> LeiosFetchPeer m blk)
-> m (StrictTVar m (Seq LeiosFetchRequest))
-> m (StrictTVar m (Map ElectionId EndorserBlockHash)
-> StrictTVar m (Point blk) -> LeiosFetchPeer m blk)
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Seq LeiosFetchRequest -> m (StrictTVar m (Seq LeiosFetchRequest))
forall (m :: * -> *) a. MonadSTM m => a -> m (StrictTVar m a)
uncheckedNewTVarM Seq LeiosFetchRequest
forall a. Seq a
Seq.empty
m (StrictTVar m (Map ElectionId EndorserBlockHash)
-> StrictTVar m (Point blk) -> LeiosFetchPeer m blk)
-> m (StrictTVar m (Map ElectionId EndorserBlockHash))
-> m (StrictTVar m (Point blk) -> LeiosFetchPeer m blk)
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Map ElectionId EndorserBlockHash
-> m (StrictTVar m (Map ElectionId EndorserBlockHash))
forall (m :: * -> *) a. MonadSTM m => a -> m (StrictTVar m a)
uncheckedNewTVarM Map ElectionId EndorserBlockHash
forall k a. Map k a
Map.empty
m (StrictTVar m (Point blk) -> LeiosFetchPeer m blk)
-> m (StrictTVar m (Point blk)) -> m (LeiosFetchPeer m blk)
forall a b. m (a -> b) -> m a -> m b
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Point blk -> m (StrictTVar m (Point blk))
forall (m :: * -> *) a. MonadSTM m => a -> m (StrictTVar m a)
uncheckedNewTVarM Point blk
forall {k} (block :: k). Point block
GenesisPoint
atomically $ do
peers <- readTVar lfkPeers
case Map.lookupGE (MkLeiosPeerLifetimeId peer 0) peers of
Just (lifetimeId :: LeiosPeerLifetimeId peer
lifetimeId@(MkLeiosPeerLifetimeId peer
peer' Word64
_), LeiosFetchPeer m blk
existing)
| peer
peer' peer -> peer -> Bool
forall a. Eq a => a -> a -> Bool
== peer
peer -> (LeiosPeerLifetimeId peer, LeiosFetchPeer m blk)
-> STM m (LeiosPeerLifetimeId peer, LeiosFetchPeer m blk)
forall a. a -> STM m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (LeiosPeerLifetimeId peer
lifetimeId, LeiosFetchPeer m blk
existing)
Maybe (LeiosPeerLifetimeId peer, LeiosFetchPeer m blk)
_ -> do
n <- StrictTVar m Word64 -> STM m Word64
forall (m :: * -> *) a. MonadSTM m => StrictTVar m a -> STM m a
readTVar StrictTVar m Word64
lfkNextLifetimeId
writeTVar lfkNextLifetimeId (n + 1)
let lifetimeId = peer -> Word64 -> LeiosPeerLifetimeId peer
forall peer. peer -> Word64 -> LeiosPeerLifetimeId peer
MkLeiosPeerLifetimeId peer
peer Word64
n
writeTVar lfkPeers (Map.insert lifetimeId fresh peers)
pure (lifetimeId, fresh)
unregister :: (LeiosPeerLifetimeId peer, b) -> m ()
unregister (LeiosPeerLifetimeId peer
lifetimeId, b
_) = do
StrictMVar m (LeiosOutstanding (LeiosPeerLifetimeId peer))
-> (LeiosOutstanding (LeiosPeerLifetimeId peer)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer)))
-> m ()
forall (m :: * -> *) a.
(HasCallStack, MonadMVar m) =>
StrictMVar m a -> (a -> m a) -> m ()
modifyMVar_ (LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
-> StrictMVar m (LeiosOutstanding (LeiosPeerLifetimeId peer))
forall (m :: * -> *) peer.
LeiosFetchClientContext m peer
-> StrictMVar m (LeiosOutstanding peer)
lfcOutstanding LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
lfkContext) ((LeiosOutstanding (LeiosPeerLifetimeId peer)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer)))
-> m ())
-> (LeiosOutstanding (LeiosPeerLifetimeId peer)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer)))
-> m ()
forall a b. (a -> b) -> a -> b
$ \LeiosOutstanding (LeiosPeerLifetimeId peer)
outstanding -> do
STM m () -> m ()
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM m () -> m ()) -> STM m () -> m ()
forall a b. (a -> b) -> a -> b
$ StrictTVar
m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk))
-> (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk)
-> Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk))
-> STM m ()
forall (m :: * -> *) a.
MonadSTM m =>
StrictTVar m a -> (a -> a) -> STM m ()
modifyTVar StrictTVar
m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk))
lfkPeers (LeiosPeerLifetimeId peer
-> Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk)
-> Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk)
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete LeiosPeerLifetimeId peer
lifetimeId)
LeiosOutstanding (LeiosPeerLifetimeId peer)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (LeiosOutstanding (LeiosPeerLifetimeId peer)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer)))
-> LeiosOutstanding (LeiosPeerLifetimeId peer)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer))
forall a b. (a -> b) -> a -> b
$! LeiosPeerLifetimeId peer
-> LeiosOutstanding (LeiosPeerLifetimeId peer)
-> LeiosOutstanding (LeiosPeerLifetimeId peer)
forall peer.
Ord peer =>
peer -> LeiosOutstanding peer -> LeiosOutstanding peer
removePeerFromOutstanding LeiosPeerLifetimeId peer
lifetimeId LeiosOutstanding (LeiosPeerLifetimeId peer)
outstanding
m Bool -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m Bool -> m ()) -> m Bool -> m ()
forall a b. (a -> b) -> a -> b
$ StrictMVar m () -> () -> m Bool
forall (m :: * -> *) a.
(HasCallStack, MonadMVar m) =>
StrictMVar m a -> a -> m Bool
tryPutMVar (LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
-> StrictMVar m ()
forall (m :: * -> *) peer.
LeiosFetchClientContext m peer -> StrictMVar m ()
lfcReady LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
lfkContext) ()
recordLeiosOffer ::
IOLike m =>
LeiosFetchKernel m peer blk ->
LeiosFetchPeer m blk ->
LeiosPoint ->
PeerOffer ->
m ()
recordLeiosOffer :: forall (m :: * -> *) peer blk.
IOLike m =>
LeiosFetchKernel m peer blk
-> LeiosFetchPeer m blk -> LeiosPoint -> PeerOffer -> m ()
recordLeiosOffer LeiosFetchKernel m peer blk
kernel MkLeiosFetchPeer{StrictTVar m (Map LeiosPoint PeerOffer)
lfpOfferings :: forall (m :: * -> *) blk.
LeiosFetchPeer m blk -> StrictTVar m (Map LeiosPoint PeerOffer)
lfpOfferings :: StrictTVar m (Map LeiosPoint PeerOffer)
lfpOfferings} LeiosPoint
point PeerOffer
offer = do
STM m () -> m ()
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM m () -> m ()) -> STM m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
prunedSlot <- StrictTVar m SlotNo -> STM m SlotNo
forall (m :: * -> *) a. MonadSTM m => StrictTVar m a -> STM m a
readTVar (LeiosFetchKernel m peer blk -> StrictTVar m SlotNo
forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk -> StrictTVar m SlotNo
lfkPrunedSlot LeiosFetchKernel m peer blk
kernel)
unless (pointSlotNo point < prunedSlot) $
modifyTVar lfpOfferings (Map.insertWith (<>) point offer)
m Bool -> m ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (m Bool -> m ()) -> m Bool -> m ()
forall a b. (a -> b) -> a -> b
$ StrictMVar m () -> () -> m Bool
forall (m :: * -> *) a.
(HasCallStack, MonadMVar m) =>
StrictMVar m a -> a -> m Bool
tryPutMVar (LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
-> StrictMVar m ()
forall (m :: * -> *) peer.
LeiosFetchClientContext m peer -> StrictMVar m ()
lfcReady (LeiosFetchKernel m peer blk
-> LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk
-> LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
lfkContext LeiosFetchKernel m peer blk
kernel)) ()
recordLeiosCertOffer ::
forall m peer blk.
(IOLike m, BlockSupportsProtocol blk) =>
TopLevelConfig blk ->
LeiosFetchKernel m peer blk ->
LeiosFetchPeer m blk ->
Header blk ->
HeaderStateWithTime blk ->
m ()
recordLeiosCertOffer :: forall (m :: * -> *) peer blk.
(IOLike m, BlockSupportsProtocol blk) =>
TopLevelConfig blk
-> LeiosFetchKernel m peer blk
-> LeiosFetchPeer m blk
-> Header blk
-> HeaderStateWithTime blk
-> m ()
recordLeiosCertOffer TopLevelConfig blk
cfg LeiosFetchKernel m peer blk
kernel LeiosFetchPeer m blk
peerVars Header blk
hdr HeaderStateWithTime blk
predState =
Bool -> m () -> m ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Proxy (BlockProtocol blk)
-> ValidateView (BlockProtocol blk) -> Bool
forall p (proxy :: * -> *).
LeiosProtocol p =>
proxy p -> ValidateView p -> Bool
forall (proxy :: * -> *).
proxy (BlockProtocol blk)
-> ValidateView (BlockProtocol blk) -> Bool
getContainsLeiosCert Proxy (BlockProtocol blk)
p (BlockConfig blk -> Header blk -> ValidateView (BlockProtocol blk)
forall blk.
BlockSupportsProtocol blk =>
BlockConfig blk -> Header blk -> ValidateView (BlockProtocol blk)
validateView (TopLevelConfig blk -> BlockConfig blk
forall blk. TopLevelConfig blk -> BlockConfig blk
configBlock TopLevelConfig blk
cfg) Header blk
hdr)) (m () -> m ()) -> m () -> m ()
forall a b. (a -> b) -> a -> b
$
case ( HeaderState blk -> WithOrigin (AnnTip blk)
forall blk. HeaderState blk -> WithOrigin (AnnTip blk)
headerStateTip HeaderState blk
predHeaderState
, Proxy (BlockProtocol blk)
-> ChainDepState (BlockProtocol blk) -> Maybe LeiosAnnouncement
forall p (proxy :: * -> *).
LeiosProtocol p =>
proxy p -> ChainDepState p -> Maybe LeiosAnnouncement
forall (proxy :: * -> *).
proxy (BlockProtocol blk)
-> ChainDepState (BlockProtocol blk) -> Maybe LeiosAnnouncement
getCertifiableLeiosAnnouncement Proxy (BlockProtocol blk)
p (HeaderState blk -> ChainDepState (BlockProtocol blk)
forall blk. HeaderState blk -> ChainDepState (BlockProtocol blk)
headerStateChainDep HeaderState blk
predHeaderState)
, HeaderStateWithTime blk -> StrictMaybe LeiosLedgerView
forall blk. HeaderStateWithTime blk -> StrictMaybe LeiosLedgerView
hswtLeiosLedgerView HeaderStateWithTime blk
predState
) of
(NotOrigin AnnTip blk
predTip, Just LeiosAnnouncement
announcement, SJust LeiosLedgerView
llv) -> do
let slot :: SlotNo
slot = AnnTip blk -> SlotNo
forall blk. AnnTip blk -> SlotNo
annTipSlotNo AnnTip blk
predTip
ebHash :: EndorserBlockHash
ebHash = LeiosAnnouncement -> EndorserBlockHash
announcementEbHash LeiosAnnouncement
announcement
LeiosFetchKernel m peer blk
-> LeiosFetchPeer m blk -> ElectionId -> EndorserBlockHash -> m ()
forall (m :: * -> *) peer blk.
IOLike m =>
LeiosFetchKernel m peer blk
-> LeiosFetchPeer m blk -> ElectionId -> EndorserBlockHash -> m ()
noteCertificationClaim
LeiosFetchKernel m peer blk
kernel
LeiosFetchPeer m blk
peerVars
(SlotNo -> KeyHash BlockIssuer -> ElectionId
ElectionId SlotNo
slot (LeiosAnnouncement -> KeyHash BlockIssuer
announcementElectee LeiosAnnouncement
announcement))
EndorserBlockHash
ebHash
LeiosFetchKernel m peer blk
-> LeiosFetchPeer m blk -> LeiosPoint -> PeerOffer -> m ()
forall (m :: * -> *) peer blk.
IOLike m =>
LeiosFetchKernel m peer blk
-> LeiosFetchPeer m blk -> LeiosPoint -> PeerOffer -> m ()
recordLeiosOffer LeiosFetchKernel m peer blk
kernel LeiosFetchPeer m blk
peerVars (SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint SlotNo
slot EndorserBlockHash
ebHash) (PeerOffer -> m ()) -> PeerOffer -> m ()
forall a b. (a -> b) -> a -> b
$
MkPeerOffer
{ poEbBounds :: StrictMaybe (LeiosEbByteSizes 'SizeLimit)
poEbBounds = LeiosEbByteSizes 'SizeLimit
-> StrictMaybe (LeiosEbByteSizes 'SizeLimit)
forall a. a -> StrictMaybe a
SJust (LeiosLedgerView -> LeiosEbByteSizes 'SizeLimit
leiosEbByteSizeBounds LeiosLedgerView
llv)
, poOffered :: OfferedHalves
poOffered = BytesSize -> OfferedHalves
OfferedBodyAndClosure (LeiosAnnouncement -> BytesSize
announcementEbSize LeiosAnnouncement
announcement)
}
(WithOrigin (AnnTip blk), Maybe LeiosAnnouncement,
StrictMaybe LeiosLedgerView)
_ -> () -> m ()
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
where
p :: Proxy (BlockProtocol blk)
p = forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @(BlockProtocol blk)
predHeaderState :: HeaderState blk
predHeaderState = HeaderStateWithTime blk -> HeaderState blk
forall blk. HeaderStateWithTime blk -> HeaderState blk
hswtHeaderState HeaderStateWithTime blk
predState
noteCertificationClaim ::
IOLike m =>
LeiosFetchKernel m peer blk -> LeiosFetchPeer m blk -> ElectionId -> EndorserBlockHash -> m ()
noteCertificationClaim :: forall (m :: * -> *) peer blk.
IOLike m =>
LeiosFetchKernel m peer blk
-> LeiosFetchPeer m blk -> ElectionId -> EndorserBlockHash -> m ()
noteCertificationClaim LeiosFetchKernel m peer blk
kernel MkLeiosFetchPeer{StrictTVar m (Map ElectionId EndorserBlockHash)
lfpCertificationClaims :: forall (m :: * -> *) blk.
LeiosFetchPeer m blk
-> StrictTVar m (Map ElectionId EndorserBlockHash)
lfpCertificationClaims :: StrictTVar m (Map ElectionId EndorserBlockHash)
lfpCertificationClaims} ElectionId
elId EndorserBlockHash
ebHash = do
conflict <- STM m (Maybe EndorserBlockHash) -> m (Maybe EndorserBlockHash)
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM m (Maybe EndorserBlockHash) -> m (Maybe EndorserBlockHash))
-> STM m (Maybe EndorserBlockHash) -> m (Maybe EndorserBlockHash)
forall a b. (a -> b) -> a -> b
$ do
prunedSlot <- StrictTVar m SlotNo -> STM m SlotNo
forall (m :: * -> *) a. MonadSTM m => StrictTVar m a -> STM m a
readTVar (LeiosFetchKernel m peer blk -> StrictTVar m SlotNo
forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk -> StrictTVar m SlotNo
lfkPrunedSlot LeiosFetchKernel m peer blk
kernel)
claims <- readTVar lfpCertificationClaims
if electionSlot elId < prunedSlot
then pure Nothing
else case Map.lookup elId claims of
Maybe EndorserBlockHash
Nothing -> Maybe EndorserBlockHash
forall a. Maybe a
Nothing Maybe EndorserBlockHash
-> STM m () -> STM m (Maybe EndorserBlockHash)
forall a b. a -> STM m b -> STM m a
forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ StrictTVar m (Map ElectionId EndorserBlockHash)
-> Map ElectionId EndorserBlockHash -> STM m ()
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
StrictTVar m a -> a -> STM m ()
writeTVar StrictTVar m (Map ElectionId EndorserBlockHash)
lfpCertificationClaims (ElectionId
-> EndorserBlockHash
-> Map ElectionId EndorserBlockHash
-> Map ElectionId EndorserBlockHash
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert ElectionId
elId EndorserBlockHash
ebHash Map ElectionId EndorserBlockHash
claims)
Just EndorserBlockHash
claimed -> Maybe EndorserBlockHash -> STM m (Maybe EndorserBlockHash)
forall a. a -> STM m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe EndorserBlockHash -> STM m (Maybe EndorserBlockHash))
-> Maybe EndorserBlockHash -> STM m (Maybe EndorserBlockHash)
forall a b. (a -> b) -> a -> b
$ if EndorserBlockHash
claimed EndorserBlockHash -> EndorserBlockHash -> Bool
forall a. Eq a => a -> a -> Bool
== EndorserBlockHash
ebHash then Maybe EndorserBlockHash
forall a. Maybe a
Nothing else EndorserBlockHash -> Maybe EndorserBlockHash
forall a. a -> Maybe a
Just EndorserBlockHash
claimed
forM_ conflict $ \EndorserBlockHash
claimed -> ExnLeiosTwoCertificationClaims -> m (ZonkAny 0)
forall e a. Exception e => e -> m a
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwIO (ExnLeiosTwoCertificationClaims -> m (ZonkAny 0))
-> ExnLeiosTwoCertificationClaims -> m (ZonkAny 0)
forall a b. (a -> b) -> a -> b
$ ElectionId
-> EndorserBlockHash
-> EndorserBlockHash
-> ExnLeiosTwoCertificationClaims
ExnLeiosTwoCertificationClaims ElectionId
elId EndorserBlockHash
claimed EndorserBlockHash
ebHash
data ExnLeiosTwoCertificationClaims
= ExnLeiosTwoCertificationClaims !ElectionId !EndorserBlockHash !EndorserBlockHash
deriving Int -> ExnLeiosTwoCertificationClaims -> ShowS
[ExnLeiosTwoCertificationClaims] -> ShowS
ExnLeiosTwoCertificationClaims -> String
(Int -> ExnLeiosTwoCertificationClaims -> ShowS)
-> (ExnLeiosTwoCertificationClaims -> String)
-> ([ExnLeiosTwoCertificationClaims] -> ShowS)
-> Show ExnLeiosTwoCertificationClaims
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> ExnLeiosTwoCertificationClaims -> ShowS
showsPrec :: Int -> ExnLeiosTwoCertificationClaims -> ShowS
$cshow :: ExnLeiosTwoCertificationClaims -> String
show :: ExnLeiosTwoCertificationClaims -> String
$cshowList :: [ExnLeiosTwoCertificationClaims] -> ShowS
showList :: [ExnLeiosTwoCertificationClaims] -> ShowS
Show
instance Exception ExnLeiosTwoCertificationClaims
recordLeiosJumpOffers ::
(IOLike m, LedgerSupportsProtocol blk) =>
TopLevelConfig blk ->
LeiosFetchKernel m peer blk ->
LeiosFetchPeer m blk ->
JumpInfo blk ->
m ()
recordLeiosJumpOffers :: forall (m :: * -> *) blk peer.
(IOLike m, LedgerSupportsProtocol blk) =>
TopLevelConfig blk
-> LeiosFetchKernel m peer blk
-> LeiosFetchPeer m blk
-> JumpInfo blk
-> m ()
recordLeiosJumpOffers TopLevelConfig blk
cfg LeiosFetchKernel m peer blk
kernel LeiosFetchPeer m blk
peerVars JumpInfo blk
jumpInfo = do
jumpedThrough <- StrictTVar m (Point blk) -> m (Point blk)
forall (m :: * -> *) a. MonadSTM m => StrictTVar m a -> m a
readTVarIO (LeiosFetchPeer m blk -> StrictTVar m (Point blk)
forall (m :: * -> *) blk.
LeiosFetchPeer m blk -> StrictTVar m (Point blk)
lfpJumpedThrough LeiosFetchPeer m blk
peerVars)
forM_ (headersWithPredecessorStates jumpedThrough jumpInfo) $
uncurry (recordLeiosCertOffer cfg kernel peerVars)
atomically $
writeTVar (lfpJumpedThrough peerVars) (castPoint (AF.headPoint (jTheirFragment jumpInfo)))
headersWithPredecessorStates ::
forall blk.
LedgerSupportsProtocol blk =>
Point blk ->
JumpInfo blk ->
[(Header blk, HeaderStateWithTime blk)]
Point blk
jumpedThrough JumpInfo blk
jumpInfo =
[Header blk]
-> [HeaderStateWithTime blk]
-> [(Header blk, HeaderStateWithTime blk)]
forall a b. [a] -> [b] -> [(a, b)]
zip
(HeaderWithTime blk -> Header blk
forall blk. HeaderWithTime blk -> Header blk
hwtHeader (HeaderWithTime blk -> Header blk)
-> [HeaderWithTime blk] -> [Header blk]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> AnchoredSeq
(WithOrigin SlotNo)
(Anchor (HeaderWithTime blk))
(HeaderWithTime blk)
-> [HeaderWithTime blk]
forall v a b. AnchoredSeq v a b -> [b]
AF.toOldestFirst AnchoredSeq
(WithOrigin SlotNo)
(Anchor (HeaderWithTime blk))
(HeaderWithTime blk)
fragment)
(AnchoredSeq
(WithOrigin SlotNo)
(HeaderStateWithTime blk)
(HeaderStateWithTime blk)
-> HeaderStateWithTime blk
forall v a b. AnchoredSeq v a b -> a
AS.anchor AnchoredSeq
(WithOrigin SlotNo)
(HeaderStateWithTime blk)
(HeaderStateWithTime blk)
history HeaderStateWithTime blk
-> [HeaderStateWithTime blk] -> [HeaderStateWithTime blk]
forall a. a -> [a] -> [a]
: AnchoredSeq
(WithOrigin SlotNo)
(HeaderStateWithTime blk)
(HeaderStateWithTime blk)
-> [HeaderStateWithTime blk]
forall v a b. AnchoredSeq v a b -> [b]
AS.toOldestFirst AnchoredSeq
(WithOrigin SlotNo)
(HeaderStateWithTime blk)
(HeaderStateWithTime blk)
history)
where
past ::
AS.Anchorable (WithOrigin SlotNo) a b =>
(Either a b -> Point blk) ->
AS.AnchoredSeq (WithOrigin SlotNo) a b ->
AS.AnchoredSeq (WithOrigin SlotNo) a b
past :: forall a b.
Anchorable (WithOrigin SlotNo) a b =>
(Either a b -> Point blk)
-> AnchoredSeq (WithOrigin SlotNo) a b
-> AnchoredSeq (WithOrigin SlotNo) a b
past Either a b -> Point blk
pointOf AnchoredSeq (WithOrigin SlotNo) a b
s =
AnchoredSeq (WithOrigin SlotNo) a b
-> ((AnchoredSeq (WithOrigin SlotNo) a b,
AnchoredSeq (WithOrigin SlotNo) a b)
-> AnchoredSeq (WithOrigin SlotNo) a b)
-> Maybe
(AnchoredSeq (WithOrigin SlotNo) a b,
AnchoredSeq (WithOrigin SlotNo) a b)
-> AnchoredSeq (WithOrigin SlotNo) a b
forall b a. b -> (a -> b) -> Maybe a -> b
maybe AnchoredSeq (WithOrigin SlotNo) a b
s (AnchoredSeq (WithOrigin SlotNo) a b,
AnchoredSeq (WithOrigin SlotNo) a b)
-> AnchoredSeq (WithOrigin SlotNo) a b
forall a b. (a, b) -> b
snd (Maybe
(AnchoredSeq (WithOrigin SlotNo) a b,
AnchoredSeq (WithOrigin SlotNo) a b)
-> AnchoredSeq (WithOrigin SlotNo) a b)
-> Maybe
(AnchoredSeq (WithOrigin SlotNo) a b,
AnchoredSeq (WithOrigin SlotNo) a b)
-> AnchoredSeq (WithOrigin SlotNo) a b
forall a b. (a -> b) -> a -> b
$
WithOrigin SlotNo
-> (Either a b -> Bool)
-> AnchoredSeq (WithOrigin SlotNo) a b
-> Maybe
(AnchoredSeq (WithOrigin SlotNo) a b,
AnchoredSeq (WithOrigin SlotNo) a b)
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)
AS.splitAfterMeasure (Point blk -> WithOrigin SlotNo
forall {k} (block :: k). Point block -> WithOrigin SlotNo
pointSlot Point blk
jumpedThrough) ((Point blk -> Point blk -> Bool
forall a. Eq a => a -> a -> Bool
== Point blk
jumpedThrough) (Point blk -> Bool)
-> (Either a b -> Point blk) -> Either a b -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Either a b -> Point blk
pointOf) AnchoredSeq (WithOrigin SlotNo) a b
s
fragment :: AnchoredSeq
(WithOrigin SlotNo)
(Anchor (HeaderWithTime blk))
(HeaderWithTime blk)
fragment =
(Either (Anchor (HeaderWithTime blk)) (HeaderWithTime blk)
-> Point blk)
-> AnchoredSeq
(WithOrigin SlotNo)
(Anchor (HeaderWithTime blk))
(HeaderWithTime blk)
-> AnchoredSeq
(WithOrigin SlotNo)
(Anchor (HeaderWithTime blk))
(HeaderWithTime blk)
forall a b.
Anchorable (WithOrigin SlotNo) a b =>
(Either a b -> Point blk)
-> AnchoredSeq (WithOrigin SlotNo) a b
-> AnchoredSeq (WithOrigin SlotNo) a b
past ((Anchor (HeaderWithTime blk) -> Point blk)
-> (HeaderWithTime blk -> Point blk)
-> Either (Anchor (HeaderWithTime blk)) (HeaderWithTime blk)
-> Point blk
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Point (HeaderWithTime blk) -> Point blk
forall {k1} {k2} (b :: k1) (b' :: k2).
Coercible (HeaderHash b) (HeaderHash b') =>
Point b -> Point b'
castPoint (Point (HeaderWithTime blk) -> Point blk)
-> (Anchor (HeaderWithTime blk) -> Point (HeaderWithTime blk))
-> Anchor (HeaderWithTime blk)
-> Point blk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Anchor (HeaderWithTime blk) -> Point (HeaderWithTime blk)
forall block. Anchor block -> Point block
AF.anchorToPoint) (Point (HeaderWithTime blk) -> Point blk
forall {k1} {k2} (b :: k1) (b' :: k2).
Coercible (HeaderHash b) (HeaderHash b') =>
Point b -> Point b'
castPoint (Point (HeaderWithTime blk) -> Point blk)
-> (HeaderWithTime blk -> Point (HeaderWithTime blk))
-> HeaderWithTime blk
-> Point blk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HeaderWithTime blk -> Point (HeaderWithTime blk)
forall block. HasHeader block => block -> Point block
blockPoint)) (AnchoredSeq
(WithOrigin SlotNo)
(Anchor (HeaderWithTime blk))
(HeaderWithTime blk)
-> AnchoredSeq
(WithOrigin SlotNo)
(Anchor (HeaderWithTime blk))
(HeaderWithTime blk))
-> AnchoredSeq
(WithOrigin SlotNo)
(Anchor (HeaderWithTime blk))
(HeaderWithTime blk)
-> AnchoredSeq
(WithOrigin SlotNo)
(Anchor (HeaderWithTime blk))
(HeaderWithTime blk)
forall a b. (a -> b) -> a -> b
$
JumpInfo blk
-> AnchoredSeq
(WithOrigin SlotNo)
(Anchor (HeaderWithTime blk))
(HeaderWithTime blk)
forall blk. JumpInfo blk -> AnchoredFragment (HeaderWithTime blk)
jTheirFragment JumpInfo blk
jumpInfo
history :: AnchoredSeq
(WithOrigin SlotNo)
(HeaderStateWithTime blk)
(HeaderStateWithTime blk)
history =
(Either (HeaderStateWithTime blk) (HeaderStateWithTime blk)
-> Point blk)
-> AnchoredSeq
(WithOrigin SlotNo)
(HeaderStateWithTime blk)
(HeaderStateWithTime blk)
-> AnchoredSeq
(WithOrigin SlotNo)
(HeaderStateWithTime blk)
(HeaderStateWithTime blk)
forall a b.
Anchorable (WithOrigin SlotNo) a b =>
(Either a b -> Point blk)
-> AnchoredSeq (WithOrigin SlotNo) a b
-> AnchoredSeq (WithOrigin SlotNo) a b
past (HeaderState blk -> Point blk
forall blk. HasAnnTip blk => HeaderState blk -> Point blk
headerStatePoint (HeaderState blk -> Point blk)
-> (Either (HeaderStateWithTime blk) (HeaderStateWithTime blk)
-> HeaderState blk)
-> Either (HeaderStateWithTime blk) (HeaderStateWithTime blk)
-> Point blk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. HeaderStateWithTime blk -> HeaderState blk
forall blk. HeaderStateWithTime blk -> HeaderState blk
hswtHeaderState (HeaderStateWithTime blk -> HeaderState blk)
-> (Either (HeaderStateWithTime blk) (HeaderStateWithTime blk)
-> HeaderStateWithTime blk)
-> Either (HeaderStateWithTime blk) (HeaderStateWithTime blk)
-> HeaderState blk
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (HeaderStateWithTime blk -> HeaderStateWithTime blk)
-> (HeaderStateWithTime blk -> HeaderStateWithTime blk)
-> Either (HeaderStateWithTime blk) (HeaderStateWithTime blk)
-> HeaderStateWithTime blk
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either HeaderStateWithTime blk -> HeaderStateWithTime blk
forall a. a -> a
id HeaderStateWithTime blk -> HeaderStateWithTime blk
forall a. a -> a
id) (AnchoredSeq
(WithOrigin SlotNo)
(HeaderStateWithTime blk)
(HeaderStateWithTime blk)
-> AnchoredSeq
(WithOrigin SlotNo)
(HeaderStateWithTime blk)
(HeaderStateWithTime blk))
-> AnchoredSeq
(WithOrigin SlotNo)
(HeaderStateWithTime blk)
(HeaderStateWithTime blk)
-> AnchoredSeq
(WithOrigin SlotNo)
(HeaderStateWithTime blk)
(HeaderStateWithTime blk)
forall a b. (a -> b) -> a -> b
$
HeaderStateHistory blk
-> AnchoredSeq
(WithOrigin SlotNo)
(HeaderStateWithTime blk)
(HeaderStateWithTime blk)
forall blk.
HeaderStateHistory blk
-> AnchoredSeq
(WithOrigin SlotNo)
(HeaderStateWithTime blk)
(HeaderStateWithTime blk)
unHeaderStateHistory (HeaderStateHistory blk
-> AnchoredSeq
(WithOrigin SlotNo)
(HeaderStateWithTime blk)
(HeaderStateWithTime blk))
-> HeaderStateHistory blk
-> AnchoredSeq
(WithOrigin SlotNo)
(HeaderStateWithTime blk)
(HeaderStateWithTime blk)
forall a b. (a -> b) -> a -> b
$
JumpInfo blk -> HeaderStateHistory blk
forall blk. JumpInfo blk -> HeaderStateHistory blk
jTheirHeaderStateHistory JumpInfo blk
jumpInfo
enqueueLeiosAnnouncement ::
IOLike m =>
LeiosFetchKernel m peer blk ->
ValidAnnouncement blk ->
STM m ()
enqueueLeiosAnnouncement :: forall (m :: * -> *) peer blk.
IOLike m =>
LeiosFetchKernel m peer blk -> ValidAnnouncement blk -> STM m ()
enqueueLeiosAnnouncement LeiosFetchKernel m peer blk
kernel = TQueue m (ValidAnnouncement blk)
-> ValidAnnouncement blk -> STM m ()
forall a. TQueue m a -> a -> STM m ()
forall (m :: * -> *) a. MonadSTM m => TQueue m a -> a -> STM m ()
writeTQueue (LeiosFetchKernel m peer blk -> TQueue m (ValidAnnouncement blk)
forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk -> TQueue m (ValidAnnouncement blk)
lfkAnnouncements LeiosFetchKernel m peer blk
kernel)
leiosFetchAnnouncementFocuser ::
IOLike m =>
LeiosFetchKernel m peer blk ->
m Void
leiosFetchAnnouncementFocuser :: forall (m :: * -> *) peer blk.
IOLike m =>
LeiosFetchKernel m peer blk -> m Void
leiosFetchAnnouncementFocuser LeiosFetchKernel m peer blk
kernel =
m () -> m Void
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever (m () -> m Void) -> m () -> m Void
forall a b. (a -> b) -> a -> b
$ STM m (ValidAnnouncement blk) -> m (ValidAnnouncement blk)
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (TQueue m (ValidAnnouncement blk) -> STM m (ValidAnnouncement blk)
forall a. TQueue m a -> STM m a
forall (m :: * -> *) a. MonadSTM m => TQueue m a -> STM m a
readTQueue (LeiosFetchKernel m peer blk -> TQueue m (ValidAnnouncement blk)
forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk -> TQueue m (ValidAnnouncement blk)
lfkAnnouncements LeiosFetchKernel m peer blk
kernel)) m (ValidAnnouncement blk)
-> (ValidAnnouncement blk -> m ()) -> m ()
forall a b. m a -> (a -> m b) -> m b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= LeiosFetchKernel m peer blk -> ValidAnnouncement blk -> m ()
forall (m :: * -> *) peer blk.
IOLike m =>
LeiosFetchKernel m peer blk -> ValidAnnouncement blk -> m ()
onNewLeiosAnnouncement LeiosFetchKernel m peer blk
kernel
onNewLeiosAnnouncement ::
IOLike m =>
LeiosFetchKernel m peer blk ->
ValidAnnouncement blk ->
m ()
onNewLeiosAnnouncement :: forall (m :: * -> *) peer blk.
IOLike m =>
LeiosFetchKernel m peer blk -> ValidAnnouncement blk -> m ()
onNewLeiosAnnouncement LeiosFetchKernel m peer blk
kernel ValidAnnouncement blk
valid = do
changed <- StrictMVar m (LeiosOutstanding (LeiosPeerLifetimeId peer))
-> (LeiosOutstanding (LeiosPeerLifetimeId peer)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer), Bool))
-> m Bool
forall (m :: * -> *) a b.
(HasCallStack, MonadMVar m) =>
StrictMVar m a -> (a -> m (a, b)) -> m b
modifyMVar StrictMVar m (LeiosOutstanding (LeiosPeerLifetimeId peer))
lfcOutstanding ((LeiosOutstanding (LeiosPeerLifetimeId peer)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer), Bool))
-> m Bool)
-> (LeiosOutstanding (LeiosPeerLifetimeId peer)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer), Bool))
-> m Bool
forall a b. (a -> b) -> a -> b
$ \LeiosOutstanding (LeiosPeerLifetimeId peer)
outstanding ->
let tooOld :: Bool
tooOld = SlotNo
slot SlotNo -> SlotNo -> Bool
forall a. Ord a => a -> a -> Bool
< LeiosOutstanding (LeiosPeerLifetimeId peer) -> SlotNo
forall peer. LeiosOutstanding peer -> SlotNo
outstandingPrunedSlot LeiosOutstanding (LeiosPeerLifetimeId peer)
outstanding
introducesFocus :: Bool
introducesFocus = Bool -> Bool
not (Bool -> Bool) -> Bool -> Bool
forall a b. (a -> b) -> a -> b
$ ElectionId -> Map ElectionId EndorserBlockHash -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.member ElectionId
elId (LeiosOutstanding (LeiosPeerLifetimeId peer)
-> Map ElectionId EndorserBlockHash
forall peer.
LeiosOutstanding peer -> Map ElectionId EndorserBlockHash
elFocus LeiosOutstanding (LeiosPeerLifetimeId peer)
outstanding)
outstanding' :: LeiosOutstanding (LeiosPeerLifetimeId peer)
outstanding' =
ElectionId
-> EndorserBlockHash
-> LeiosOutstanding (LeiosPeerLifetimeId peer)
-> LeiosOutstanding (LeiosPeerLifetimeId peer)
forall peer.
ElectionId
-> EndorserBlockHash
-> LeiosOutstanding peer
-> LeiosOutstanding peer
focusElectionIfUnfocused ElectionId
elId EndorserBlockHash
ebHash (LeiosOutstanding (LeiosPeerLifetimeId peer)
-> LeiosOutstanding (LeiosPeerLifetimeId peer))
-> LeiosOutstanding (LeiosPeerLifetimeId peer)
-> LeiosOutstanding (LeiosPeerLifetimeId peer)
forall a b. (a -> b) -> a -> b
$
LeiosPoint
-> StrictMaybe RelativeTime
-> LeiosOutstanding (LeiosPeerLifetimeId peer)
-> LeiosOutstanding (LeiosPeerLifetimeId peer)
forall peer.
LeiosPoint
-> StrictMaybe RelativeTime
-> LeiosOutstanding peer
-> LeiosOutstanding peer
recordReferencedPoint LeiosPoint
point (RelativeTime -> StrictMaybe RelativeTime
forall a. a -> StrictMaybe a
SJust RelativeTime
announcementOnset) LeiosOutstanding (LeiosPeerLifetimeId peer)
outstanding
in (LeiosOutstanding (LeiosPeerLifetimeId peer), Bool)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer), Bool)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure ((LeiosOutstanding (LeiosPeerLifetimeId peer), Bool)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer), Bool))
-> (LeiosOutstanding (LeiosPeerLifetimeId peer), Bool)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer), Bool)
forall a b. (a -> b) -> a -> b
$
if Bool
tooOld Bool -> Bool -> Bool
|| Bool -> Bool
not Bool
introducesFocus
then (LeiosOutstanding (LeiosPeerLifetimeId peer)
outstanding, Bool
False)
else (LeiosOutstanding (LeiosPeerLifetimeId peer)
outstanding', Bool
True)
when changed $
void $
tryPutMVar lfcReady ()
where
MkLeiosFetchClientContext{StrictMVar m (LeiosOutstanding (LeiosPeerLifetimeId peer))
lfcOutstanding :: forall (m :: * -> *) peer.
LeiosFetchClientContext m peer
-> StrictMVar m (LeiosOutstanding peer)
lfcOutstanding :: StrictMVar m (LeiosOutstanding (LeiosPeerLifetimeId peer))
lfcOutstanding, StrictMVar m ()
lfcReady :: forall (m :: * -> *) peer.
LeiosFetchClientContext m peer -> StrictMVar m ()
lfcReady :: StrictMVar m ()
lfcReady} = LeiosFetchKernel m peer blk
-> LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk
-> LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
lfkContext LeiosFetchKernel m peer blk
kernel
MkValidAnnouncement{announcingHeader :: forall blk. ValidAnnouncement blk -> AnnouncingHeader blk
announcingHeader = AnnouncingHeader blk
anc, RelativeTime
announcementOnset :: RelativeTime
announcementOnset :: forall blk. ValidAnnouncement blk -> RelativeTime
announcementOnset} = ValidAnnouncement blk
valid
announcement :: LeiosAnnouncement
announcement = AnnouncingHeader blk -> LeiosAnnouncement
forall blk. AnnouncingHeader blk -> LeiosAnnouncement
ancAnnouncement AnnouncingHeader blk
anc
elId :: ElectionId
elId = AnnouncingHeader blk -> ElectionId
forall blk. AnnouncingHeader blk -> ElectionId
ancElectionId AnnouncingHeader blk
anc
slot :: SlotNo
slot = ElectionId -> SlotNo
electionSlot ElectionId
elId
ebHash :: EndorserBlockHash
ebHash = LeiosAnnouncement -> EndorserBlockHash
announcementEbHash LeiosAnnouncement
announcement
point :: LeiosPoint
point = SlotNo -> EndorserBlockHash -> LeiosPoint
MkLeiosPoint SlotNo
slot EndorserBlockHash
ebHash
onForgedLeiosEb ::
IOLike m =>
LeiosFetchKernel m peer blk ->
AnnouncingHeader blk ->
ForgedEndorserBlock ->
m ()
onForgedLeiosEb :: forall (m :: * -> *) peer blk.
IOLike m =>
LeiosFetchKernel m peer blk
-> AnnouncingHeader blk -> ForgedEndorserBlock -> m ()
onForgedLeiosEb LeiosFetchKernel m peer blk
kernel AnnouncingHeader blk
anc = LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
-> ElectionId -> ForgedEndorserBlock -> m ()
forall (m :: * -> *) peer.
IOLike m =>
LeiosFetchClientContext m peer
-> ElectionId -> ForgedEndorserBlock -> m ()
acquireForgedEb (LeiosFetchKernel m peer blk
-> LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk
-> LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
lfkContext LeiosFetchKernel m peer blk
kernel) (AnnouncingHeader blk -> ElectionId
forall blk. AnnouncingHeader blk -> ElectionId
ancElectionId AnnouncingHeader blk
anc)
leiosFetchDecisionLoop ::
forall m peer blk.
(IOLike m, Ord peer, LedgerSupportsProtocol blk, HasHardForkHistory blk) =>
TopLevelConfig blk ->
ChainDB m blk ->
BlockchainTime m ->
LeiosFetchKernel m peer blk ->
m Void
leiosFetchDecisionLoop :: forall (m :: * -> *) peer blk.
(IOLike m, Ord peer, LedgerSupportsProtocol blk,
HasHardForkHistory blk) =>
TopLevelConfig blk
-> ChainDB m blk
-> BlockchainTime m
-> LeiosFetchKernel m peer blk
-> m Void
leiosFetchDecisionLoop TopLevelConfig blk
cfg ChainDB m blk
chainDB BlockchainTime m
btime LeiosFetchKernel m peer blk
kernel = m () -> m Void
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever (m () -> m Void) -> m () -> m Void
forall a b. (a -> b) -> a -> b
$ do
() <- StrictMVar m () -> m ()
forall (m :: * -> *) a. MonadMVar m => StrictMVar m a -> m a
takeMVar StrictMVar m ()
lfcReady
iterationStart <- getMonotonicTime
(requests, offerDrops, peersVars) <- modifyMVar lfcOutstanding $ \LeiosOutstanding (LeiosPeerLifetimeId peer)
outstanding -> do
(peersVars, offerings, currentSlot, immLedger) <- STM
m
(Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk),
Map (LeiosPeerLifetimeId peer) (Map LeiosPoint PeerOffer),
CurrentSlot, ExtLedgerState blk EmptyMK)
-> m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk),
Map (LeiosPeerLifetimeId peer) (Map LeiosPoint PeerOffer),
CurrentSlot, ExtLedgerState blk EmptyMK)
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM
m
(Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk),
Map (LeiosPeerLifetimeId peer) (Map LeiosPoint PeerOffer),
CurrentSlot, ExtLedgerState blk EmptyMK)
-> m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk),
Map (LeiosPeerLifetimeId peer) (Map LeiosPoint PeerOffer),
CurrentSlot, ExtLedgerState blk EmptyMK))
-> STM
m
(Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk),
Map (LeiosPeerLifetimeId peer) (Map LeiosPoint PeerOffer),
CurrentSlot, ExtLedgerState blk EmptyMK)
-> m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk),
Map (LeiosPeerLifetimeId peer) (Map LeiosPoint PeerOffer),
CurrentSlot, ExtLedgerState blk EmptyMK)
forall a b. (a -> b) -> a -> b
$ do
peersVars <- StrictTVar
m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk))
-> STM m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk))
forall (m :: * -> *) a. MonadSTM m => StrictTVar m a -> STM m a
readTVar StrictTVar
m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk))
lfkPeers
offerings <- traverse (readTVar . lfpOfferings) peersVars
(,,,) peersVars offerings <$> getCurrentSlot btime <*> ChainDB.getImmutableLedger chainDB
let
forecastLeiosView SlotNo
slot =
Proxy (BlockProtocol blk)
-> LedgerView (BlockProtocol blk) -> Maybe LeiosLedgerView
forall p (proxy :: * -> *).
LeiosProtocol p =>
proxy p -> LedgerView p -> Maybe LeiosLedgerView
forall (proxy :: * -> *).
proxy (BlockProtocol blk)
-> LedgerView (BlockProtocol blk) -> Maybe LeiosLedgerView
getLeiosLedgerView (forall t. Proxy t
forall {k} (t :: k). Proxy t
Proxy @(BlockProtocol blk))
(LedgerView (BlockProtocol blk) -> Maybe LeiosLedgerView)
-> Either OutsideForecastRange (LedgerView (BlockProtocol blk))
-> Either OutsideForecastRange (Maybe LeiosLedgerView)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Except OutsideForecastRange (LedgerView (BlockProtocol blk))
-> Either OutsideForecastRange (LedgerView (BlockProtocol blk))
forall e a. Except e a -> Either e a
runExcept
( Forecast (LedgerView (BlockProtocol blk))
-> SlotNo
-> Except OutsideForecastRange (LedgerView (BlockProtocol blk))
forall a. Forecast a -> SlotNo -> Except OutsideForecastRange a
forecastFor
(LedgerCfg LedgerState blk
-> LedgerState blk EmptyMK
-> Forecast (LedgerView (BlockProtocol blk))
forall blk (mk :: MapKind).
(LedgerSupportsProtocol blk, HasCallStack) =>
LedgerConfig blk
-> LedgerState blk mk -> Forecast (LedgerView (BlockProtocol blk))
forall (mk :: MapKind).
HasCallStack =>
LedgerCfg LedgerState blk
-> LedgerState blk mk -> Forecast (LedgerView (BlockProtocol blk))
ledgerViewForecastAt (TopLevelConfig blk -> LedgerCfg LedgerState blk
forall blk. TopLevelConfig blk -> LedgerConfig blk
configLedger TopLevelConfig blk
cfg) (ExtLedgerState blk EmptyMK -> LedgerState blk EmptyMK
forall blk (mk :: MapKind).
ExtLedgerState blk mk -> LedgerState blk mk
ledgerState ExtLedgerState blk EmptyMK
immLedger))
(SlotNo -> SlotNo -> SlotNo
forall a. Ord a => a -> a -> a
max SlotNo
anchorSlot SlotNo
slot)
)
anchorSlot = SlotNo -> (SlotNo -> SlotNo) -> WithOrigin SlotNo -> SlotNo
forall b t. b -> (t -> b) -> WithOrigin t -> b
withOrigin (Word64 -> SlotNo
SlotNo Word64
0) SlotNo -> SlotNo
forall a. a -> a
id (LedgerState blk EmptyMK -> WithOrigin SlotNo
forall (l :: LedgerStateKind) (mk :: MapKind).
GetTip l =>
l mk -> WithOrigin SlotNo
getTipSlot (ExtLedgerState blk EmptyMK -> LedgerState blk EmptyMK
forall blk (mk :: MapKind).
ExtLedgerState blk mk -> LedgerState blk mk
ledgerState ExtLedgerState blk EmptyMK
immLedger))
forecastEbBounds SlotNo
slot = LeiosEbByteSizes 'SizeLimit
-> (LeiosLedgerView -> LeiosEbByteSizes 'SizeLimit)
-> Maybe LeiosLedgerView
-> LeiosEbByteSizes 'SizeLimit
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (BytesSize -> Word64 -> LeiosEbByteSizes 'SizeLimit
forall (r :: SizeRole). BytesSize -> Word64 -> LeiosEbByteSizes r
MkLeiosEbByteSizes BytesSize
0 Word64
0) LeiosLedgerView -> LeiosEbByteSizes 'SizeLimit
leiosEbByteSizeBounds (Maybe LeiosLedgerView -> LeiosEbByteSizes 'SizeLimit)
-> Either OutsideForecastRange (Maybe LeiosLedgerView)
-> Either OutsideForecastRange (LeiosEbByteSizes 'SizeLimit)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> SlotNo -> Either OutsideForecastRange (Maybe LeiosLedgerView)
forecastLeiosView SlotNo
slot
mbHorizon = case CurrentSlot
currentSlot of
CurrentSlot
CurrentSlotUnknown -> Maybe (SlotNo, Word64)
forall a. Maybe a
Nothing
CurrentSlot SlotNo
slot RelativeTime
_onset -> do
llv <- (OutsideForecastRange -> Maybe LeiosLedgerView)
-> (Maybe LeiosLedgerView -> Maybe LeiosLedgerView)
-> Either OutsideForecastRange (Maybe LeiosLedgerView)
-> Maybe LeiosLedgerView
forall a c b. (a -> c) -> (b -> c) -> Either a b -> c
either (Maybe LeiosLedgerView
-> OutsideForecastRange -> Maybe LeiosLedgerView
forall a b. a -> b -> a
const Maybe LeiosLedgerView
forall a. Maybe a
Nothing) Maybe LeiosLedgerView -> Maybe LeiosLedgerView
forall a. a -> a
id (SlotNo -> Either OutsideForecastRange (Maybe LeiosLedgerView)
forecastLeiosView SlotNo
slot)
(_onset, slotLength) <-
either (const Nothing) Just $
HardFork.runQuery
(HardFork.slotToWallclock slot)
(hardForkSummary (configLedger cfg) (ledgerState immLedger))
let MkLeiosLedgerView{llvAnnouncementPeriod, llvVotePeriod, llvDiffusionPeriod} = llv
SlotNo window =
minCertificationSlot slotLength llvAnnouncementPeriod llvVotePeriod llvDiffusionPeriod 0
pure (slot, window)
(!outstanding', requests, offerDrops) =
leiosFetchLogicIteration
(lfcStaticEnv lfkContext)
forecastEbBounds
mbHorizon
offerings
(Map.map lfpIsBigLedgerPeer peersVars)
outstanding
pure (outstanding', (requests, offerDrops, peersVars))
atomically $ do
forM_ (Map.toList offerDrops) $ \(LeiosPeerLifetimeId peer
peer, NEMap LeiosPoint OfferPrune
pruned) ->
Maybe (LeiosFetchPeer m blk)
-> (LeiosFetchPeer m blk -> STM m ()) -> STM m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (LeiosPeerLifetimeId peer
-> Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk)
-> Maybe (LeiosFetchPeer m blk)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup LeiosPeerLifetimeId peer
peer Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk)
peersVars) ((LeiosFetchPeer m blk -> STM m ()) -> STM m ())
-> (LeiosFetchPeer m blk -> STM m ()) -> STM m ()
forall a b. (a -> b) -> a -> b
$ \LeiosFetchPeer m blk
vars ->
StrictTVar m (Map LeiosPoint PeerOffer)
-> (Map LeiosPoint PeerOffer -> Map LeiosPoint PeerOffer)
-> STM m ()
forall (m :: * -> *) a.
MonadSTM m =>
StrictTVar m a -> (a -> a) -> STM m ()
modifyTVar (LeiosFetchPeer m blk -> StrictTVar m (Map LeiosPoint PeerOffer)
forall (m :: * -> *) blk.
LeiosFetchPeer m blk -> StrictTVar m (Map LeiosPoint PeerOffer)
lfpOfferings LeiosFetchPeer m blk
vars) (Map LeiosPoint OfferPrune
-> Map LeiosPoint PeerOffer -> Map LeiosPoint PeerOffer
pruneOffers (NEMap LeiosPoint OfferPrune -> Map LeiosPoint OfferPrune
forall k a. NEMap k a -> Map k a
NEMap.toMap NEMap LeiosPoint OfferPrune
pruned))
forM_ (Map.toList requests) $ \(LeiosPeerLifetimeId peer
peer, NESeq LeiosFetchRequest
reqs) ->
Maybe (LeiosFetchPeer m blk)
-> (LeiosFetchPeer m blk -> STM m ()) -> STM m ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ (LeiosPeerLifetimeId peer
-> Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk)
-> Maybe (LeiosFetchPeer m blk)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup LeiosPeerLifetimeId peer
peer Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk)
peersVars) ((LeiosFetchPeer m blk -> STM m ()) -> STM m ())
-> (LeiosFetchPeer m blk -> STM m ()) -> STM m ()
forall a b. (a -> b) -> a -> b
$ \LeiosFetchPeer m blk
vars ->
StrictTVar m (Seq LeiosFetchRequest)
-> (Seq LeiosFetchRequest -> Seq LeiosFetchRequest) -> STM m ()
forall (m :: * -> *) a.
MonadSTM m =>
StrictTVar m a -> (a -> a) -> STM m ()
modifyTVar (LeiosFetchPeer m blk -> StrictTVar m (Seq LeiosFetchRequest)
forall (m :: * -> *) blk.
LeiosFetchPeer m blk -> StrictTVar m (Seq LeiosFetchRequest)
lfpRequestsToSend LeiosFetchPeer m blk
vars) (Seq LeiosFetchRequest
-> Seq LeiosFetchRequest -> Seq LeiosFetchRequest
forall a. Semigroup a => a -> a -> a
<> NESeq LeiosFetchRequest -> Seq LeiosFetchRequest
forall a. NESeq a -> Seq a
NESeq.toSeq NESeq LeiosFetchRequest
reqs)
iterationEnd <- getMonotonicTime
threadDelay $ loopInterval - (iterationEnd `diffTime` iterationStart)
where
MkLeiosFetchKernel{LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
lfkContext :: forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk
-> LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
lfkContext :: LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
lfkContext, StrictTVar
m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk))
lfkPeers :: forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk
-> StrictTVar
m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk))
lfkPeers :: StrictTVar
m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk))
lfkPeers} = LeiosFetchKernel m peer blk
kernel
MkLeiosFetchClientContext{StrictMVar m (LeiosOutstanding (LeiosPeerLifetimeId peer))
lfcOutstanding :: forall (m :: * -> *) peer.
LeiosFetchClientContext m peer
-> StrictMVar m (LeiosOutstanding peer)
lfcOutstanding :: StrictMVar m (LeiosOutstanding (LeiosPeerLifetimeId peer))
lfcOutstanding, StrictMVar m ()
lfcReady :: forall (m :: * -> *) peer.
LeiosFetchClientContext m peer -> StrictMVar m ()
lfcReady :: StrictMVar m ()
lfcReady} = LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
lfkContext
loopInterval :: DiffTime
loopInterval :: DiffTime
loopInterval = DiffTime
0.5
leiosFetchCertificateFocuser ::
IOLike m =>
m (NonEmpty (LeiosClaim, LeiosAnnouncement)) ->
LeiosFetchKernel m peer blk ->
m Void
leiosFetchCertificateFocuser :: forall (m :: * -> *) peer blk.
IOLike m =>
m (NonEmpty (LeiosClaim, LeiosAnnouncement))
-> LeiosFetchKernel m peer blk -> m Void
leiosFetchCertificateFocuser m (NonEmpty (LeiosClaim, LeiosAnnouncement))
consumeValidClaims LeiosFetchKernel m peer blk
kernel = m () -> m Void
forall (f :: * -> *) a b. Applicative f => f a -> f b
forever (m () -> m Void) -> m () -> m Void
forall a b. (a -> b) -> a -> b
$ do
validClaims <- m (NonEmpty (LeiosClaim, LeiosAnnouncement))
consumeValidClaims
modifyMVar_ lfcOutstanding $ \LeiosOutstanding (LeiosPeerLifetimeId peer)
outstanding ->
LeiosOutstanding (LeiosPeerLifetimeId peer)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (LeiosOutstanding (LeiosPeerLifetimeId peer)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer)))
-> LeiosOutstanding (LeiosPeerLifetimeId peer)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer))
forall a b. (a -> b) -> a -> b
$ (LeiosOutstanding (LeiosPeerLifetimeId peer)
-> (LeiosClaim, LeiosAnnouncement)
-> LeiosOutstanding (LeiosPeerLifetimeId peer))
-> LeiosOutstanding (LeiosPeerLifetimeId peer)
-> NonEmpty (LeiosClaim, LeiosAnnouncement)
-> LeiosOutstanding (LeiosPeerLifetimeId peer)
forall b a. (b -> a -> b) -> b -> NonEmpty a -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
F.foldl' LeiosOutstanding (LeiosPeerLifetimeId peer)
-> (LeiosClaim, LeiosAnnouncement)
-> LeiosOutstanding (LeiosPeerLifetimeId peer)
forall {peer}.
LeiosOutstanding peer
-> (LeiosClaim, LeiosAnnouncement) -> LeiosOutstanding peer
focus LeiosOutstanding (LeiosPeerLifetimeId peer)
outstanding NonEmpty (LeiosClaim, LeiosAnnouncement)
validClaims
void $ tryPutMVar lfcReady ()
where
MkLeiosFetchClientContext{StrictMVar m (LeiosOutstanding (LeiosPeerLifetimeId peer))
lfcOutstanding :: forall (m :: * -> *) peer.
LeiosFetchClientContext m peer
-> StrictMVar m (LeiosOutstanding peer)
lfcOutstanding :: StrictMVar m (LeiosOutstanding (LeiosPeerLifetimeId peer))
lfcOutstanding, StrictMVar m ()
lfcReady :: forall (m :: * -> *) peer.
LeiosFetchClientContext m peer -> StrictMVar m ()
lfcReady :: StrictMVar m ()
lfcReady} = LeiosFetchKernel m peer blk
-> LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk
-> LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
lfkContext LeiosFetchKernel m peer blk
kernel
focus :: LeiosOutstanding peer
-> (LeiosClaim, LeiosAnnouncement) -> LeiosOutstanding peer
focus LeiosOutstanding peer
outstanding (LeiosClaim
claim, LeiosAnnouncement
ann) =
ElectionId
-> EndorserBlockHash
-> LeiosOutstanding peer
-> LeiosOutstanding peer
forall peer.
ElectionId
-> EndorserBlockHash
-> LeiosOutstanding peer
-> LeiosOutstanding peer
focusCertifiedEb
(SlotNo -> KeyHash BlockIssuer -> ElectionId
ElectionId (LeiosClaim -> SlotNo
leiosClaimSlot LeiosClaim
claim) (LeiosAnnouncement -> KeyHash BlockIssuer
announcementElectee LeiosAnnouncement
ann))
(LeiosAnnouncement -> EndorserBlockHash
announcementEbHash LeiosAnnouncement
ann)
LeiosOutstanding peer
outstanding
pruneLeiosFetchToImmTip ::
IOLike m =>
LeiosFetchKernel m peer blk ->
SlotNo ->
m ()
pruneLeiosFetchToImmTip :: forall (m :: * -> *) peer blk.
IOLike m =>
LeiosFetchKernel m peer blk -> SlotNo -> m ()
pruneLeiosFetchToImmTip LeiosFetchKernel m peer blk
kernel SlotNo
immTipSlot = do
LeiosTxCache m () Version PackedEndorserBlock -> SlotNo -> m ()
forall (m :: * -> *) a v b.
Functor m =>
LeiosTxCache m a v b -> SlotNo -> m ()
evictOlderThan LeiosTxCache m () Version PackedEndorserBlock
lfcTxCache SlotNo
immTipSlot
StrictMVar m (LeiosOutstanding (LeiosPeerLifetimeId peer))
-> (LeiosOutstanding (LeiosPeerLifetimeId peer)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer)))
-> m ()
forall (m :: * -> *) a.
(HasCallStack, MonadMVar m) =>
StrictMVar m a -> (a -> m a) -> m ()
modifyMVar_ StrictMVar m (LeiosOutstanding (LeiosPeerLifetimeId peer))
lfcOutstanding ((LeiosOutstanding (LeiosPeerLifetimeId peer)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer)))
-> m ())
-> (LeiosOutstanding (LeiosPeerLifetimeId peer)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer)))
-> m ()
forall a b. (a -> b) -> a -> b
$ LeiosOutstanding (LeiosPeerLifetimeId peer)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer))
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure (LeiosOutstanding (LeiosPeerLifetimeId peer)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer)))
-> (LeiosOutstanding (LeiosPeerLifetimeId peer)
-> LeiosOutstanding (LeiosPeerLifetimeId peer))
-> LeiosOutstanding (LeiosPeerLifetimeId peer)
-> m (LeiosOutstanding (LeiosPeerLifetimeId peer))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. SlotNo
-> LeiosOutstanding (LeiosPeerLifetimeId peer)
-> LeiosOutstanding (LeiosPeerLifetimeId peer)
forall peer.
SlotNo -> LeiosOutstanding peer -> LeiosOutstanding peer
pruneOutstandingToImmTip SlotNo
immTipSlot
STM m () -> m ()
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically (STM m () -> m ()) -> STM m () -> m ()
forall a b. (a -> b) -> a -> b
$ do
StrictTVar m SlotNo -> SlotNo -> STM m ()
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
StrictTVar m a -> a -> STM m ()
writeTVar StrictTVar m SlotNo
lfkPrunedSlot SlotNo
immTipSlot
peersVars <- StrictTVar
m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk))
-> STM m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk))
forall (m :: * -> *) a. MonadSTM m => StrictTVar m a -> STM m a
readTVar StrictTVar
m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk))
lfkPeers
forM_ peersVars $ \LeiosFetchPeer m blk
vars -> do
StrictTVar m (Map LeiosPoint PeerOffer)
-> (Map LeiosPoint PeerOffer -> Map LeiosPoint PeerOffer)
-> STM m ()
forall (m :: * -> *) a.
MonadSTM m =>
StrictTVar m a -> (a -> a) -> STM m ()
modifyTVar (LeiosFetchPeer m blk -> StrictTVar m (Map LeiosPoint PeerOffer)
forall (m :: * -> *) blk.
LeiosFetchPeer m blk -> StrictTVar m (Map LeiosPoint PeerOffer)
lfpOfferings LeiosFetchPeer m blk
vars) ((LeiosPoint -> Bool)
-> Map LeiosPoint PeerOffer -> Map LeiosPoint PeerOffer
forall k a. (k -> Bool) -> Map k a -> Map k a
Map.dropWhileAntitone ((SlotNo -> SlotNo -> Bool
forall a. Ord a => a -> a -> Bool
< SlotNo
immTipSlot) (SlotNo -> Bool) -> (LeiosPoint -> SlotNo) -> LeiosPoint -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LeiosPoint -> SlotNo
pointSlotNo))
StrictTVar m (Map ElectionId EndorserBlockHash)
-> (Map ElectionId EndorserBlockHash
-> Map ElectionId EndorserBlockHash)
-> STM m ()
forall (m :: * -> *) a.
MonadSTM m =>
StrictTVar m a -> (a -> a) -> STM m ()
modifyTVar
(LeiosFetchPeer m blk
-> StrictTVar m (Map ElectionId EndorserBlockHash)
forall (m :: * -> *) blk.
LeiosFetchPeer m blk
-> StrictTVar m (Map ElectionId EndorserBlockHash)
lfpCertificationClaims LeiosFetchPeer m blk
vars)
((ElectionId -> Bool)
-> Map ElectionId EndorserBlockHash
-> Map ElectionId EndorserBlockHash
forall k a. (k -> Bool) -> Map k a -> Map k a
Map.dropWhileAntitone ((SlotNo -> SlotNo -> Bool
forall a. Ord a => a -> a -> Bool
< SlotNo
immTipSlot) (SlotNo -> Bool) -> (ElectionId -> SlotNo) -> ElectionId -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ElectionId -> SlotNo
electionSlot))
where
MkLeiosFetchKernel{LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
lfkContext :: forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk
-> LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
lfkContext :: LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
lfkContext, StrictTVar
m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk))
lfkPeers :: forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk
-> StrictTVar
m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk))
lfkPeers :: StrictTVar
m (Map (LeiosPeerLifetimeId peer) (LeiosFetchPeer m blk))
lfkPeers, StrictTVar m SlotNo
lfkPrunedSlot :: forall (m :: * -> *) peer blk.
LeiosFetchKernel m peer blk -> StrictTVar m SlotNo
lfkPrunedSlot :: StrictTVar m SlotNo
lfkPrunedSlot} = LeiosFetchKernel m peer blk
kernel
MkLeiosFetchClientContext{StrictMVar m (LeiosOutstanding (LeiosPeerLifetimeId peer))
lfcOutstanding :: forall (m :: * -> *) peer.
LeiosFetchClientContext m peer
-> StrictMVar m (LeiosOutstanding peer)
lfcOutstanding :: StrictMVar m (LeiosOutstanding (LeiosPeerLifetimeId peer))
lfcOutstanding, LeiosTxCache m () Version PackedEndorserBlock
lfcTxCache :: LeiosTxCache m () Version PackedEndorserBlock
lfcTxCache :: forall (m :: * -> *) peer.
LeiosFetchClientContext m peer
-> LeiosTxCache m () Version PackedEndorserBlock
lfcTxCache} = LeiosFetchClientContext m (LeiosPeerLifetimeId peer)
lfkContext