-- | Where the node emits each endorser block and closure it acquires.
--
-- LeiosFetch emits them once the LeiosDB holds them; see
-- 'LeiosAcquisition' for at which points. The ChainDB subscribes, so
-- that ChainSel can select a block certifying an endorser block once the node
-- holds that endorser block's closure.
module Ouroboros.Consensus.Leios.Acquisitions
  ( LeiosAcquisition (..)
  , LeiosAcquisitions (..)
  , newLeiosAcquisitions
  , noLeiosAcquisitions
  ) where

import Control.Concurrent.Class.MonadSTM.Strict
  ( MonadSTM
  , STM
  , atomically
  , dupTChan
  , newBroadcastTChan
  , readTChan
  , retry
  , writeTChan
  )
import Ouroboros.Consensus.Leios.Types (BytesSize, LeiosPoint)

-- | Something LeiosFetch acquired at a 'LeiosPoint'.
--
-- LeiosFetch emits each at most once per point: 'AcquiredEb' once the LeiosDB
-- holds the point's body, and 'AcquiredEbTxs' once it holds its closure. It
-- emits neither for a point whose closure the LeiosDB already held when the
-- node started. So a subscriber that subscribes before it scans the LeiosDB at
-- start-up, as the ChainDB does, hears of each point once at most, and only of
-- one its scan did not find.
--
-- An acquisition may name a point whose writes the LeiosDB dropped, for being
-- older than its promoted slot; the LeiosDB then does not hold what it names.
data LeiosAcquisition
  = -- | The node holds the EB's body.
    AcquiredEb
      -- | the EB's point
      LeiosPoint
      -- | the size of its body
      BytesSize
  | -- | The node holds the EB's body and every tx it lists.
    AcquiredEbTxs
      -- | the EB's point
      LeiosPoint

-- | A broadcast of the node's acquisitions.
--
-- Only the code that wires up a node holds the whole broadcast. Whatever only
-- emits takes just 'emitLeiosAcquisition', and whatever only reads takes just
-- 'subscribeLeiosAcquisitions' or the 'STM' action it returns, so that its
-- type shows which side it is on. The ChainDB takes 'subscribeLeiosAcquisitions'
-- itself, since it must subscribe before it scans the LeiosDB at start-up.
data LeiosAcquisitions m = MkLeiosAcquisitions
  { forall (m :: * -> *).
LeiosAcquisitions m -> LeiosAcquisition -> STM m ()
emitLeiosAcquisition :: LeiosAcquisition -> STM m ()
  , forall (m :: * -> *).
LeiosAcquisitions m -> m (STM m LeiosAcquisition)
subscribeLeiosAcquisitions :: m (STM m LeiosAcquisition)
  -- ^ Each acquisition emitted from now on, once and in order, blocking
  -- until there is one.
  }

-- | A new broadcast, with no subscriber yet.
--
-- Its channel is unbounded: each subscriber holds every acquisition emitted
-- since it subscribed that it has yet to read. That stays small, as long as
-- each subscriber reads its next acquisition as soon as it has handled the
-- previous one, and handles one without waiting long; the ChainDB's waits at
-- most on ChainSel's queue, which ChainSel drains. The node emits at most two
-- acquisitions per point, and acquires only the points it focuses its
-- elections on: the first announcement it accepts in each, or a certified one.
-- So acquisitions arrive at the rate of elections, whatever its peers do.
newLeiosAcquisitions :: MonadSTM m => m (LeiosAcquisitions m)
newLeiosAcquisitions :: forall (m :: * -> *). MonadSTM m => m (LeiosAcquisitions m)
newLeiosAcquisitions = do
  chan <- STM m (StrictTChan m LeiosAcquisition)
-> m (StrictTChan m LeiosAcquisition)
forall a. HasCallStack => STM m a -> m a
forall (m :: * -> *) a.
(MonadSTM m, HasCallStack) =>
STM m a -> m a
atomically STM m (StrictTChan m LeiosAcquisition)
forall (m :: * -> *) a. MonadSTM m => STM m (StrictTChan m a)
newBroadcastTChan
  pure
    MkLeiosAcquisitions
      { emitLeiosAcquisition = writeTChan chan
      , subscribeLeiosAcquisitions = readTChan <$> atomically (dupTChan chan)
      }

-- | In place of 'subscribeLeiosAcquisitions', for what opens a ChainDB but
-- fetches nothing, such as a tool replaying its own databases: nothing is ever
-- emitted.
noLeiosAcquisitions :: MonadSTM m => m (STM m LeiosAcquisition)
noLeiosAcquisitions :: forall (m :: * -> *). MonadSTM m => m (STM m LeiosAcquisition)
noLeiosAcquisitions = STM m LeiosAcquisition -> m (STM m LeiosAcquisition)
forall a. a -> m a
forall (f :: * -> *) a. Applicative f => a -> f a
pure STM m LeiosAcquisition
forall a. STM m a
forall (m :: * -> *) a. MonadSTM m => STM m a
retry