{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}

-- | The reference implementation of the LeiosTxCache: a bounded, in-memory index
-- over the bodies of recent Leios EBs the node took in, and the txs it holds
-- that those bodies reference, with incremental eviction. Simple and obviously
-- correct, so that a faster implementation can be tested for observational
-- equivalence to it.
--
-- This is deliberately /independent/ of the on-disk LeiosDb: the two are
-- separate caches with different eviction policies. This index retains only
-- what is referenced by the bodies taken in at the @'maxLeiosPointCount'@
-- freshest points, so it stays small enough to keep resident in memory (a
-- lookup is disk-latency-free); the LeiosDb retains far more (everything
-- referenced by the acquired EBs younger than the immutable tip). Because they
-- are independent stores, sometimes the node will re-fetch and re-validate a tx
-- that has been evicted from the LeiosTxCache even while the LeiosDb still holds
-- it; that is acceptable.
--
-- The type parameters are payloads the index does not interpret:
--
--   * @a@ is what we record for a tx that has been inserted (fetched) but not
--     yet applied; @()@ in the node, or the tx's bytes in a test.
--
--   * @v@ is what we record for a tx that has been applied. Kept polymorphic so
--     the index is not coupled to any ledger type.
--
--   * @b@ is the body taken in at a point. It must at least carry the body's
--     'TxHash'es, via 'ReferencesTxsByHash', so eviction can find its txs
--     without touching storage.
module Ouroboros.Consensus.Leios.TxCache.Reference
  ( -- * Index
    LeiosTxCacheIndex (..)
  , TrackedTx (..)
  , emptyLeiosTxCacheIndex

    -- * Operations
  , evictOlderThanFoldl'
  , insertBodyAndFoldl'
  , insertLandedTx
  , lookupTx
  , lookupTxLocation
  ) where

import Cardano.Slotting.Slot (SlotNo (..))
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Maybe.Strict (StrictMaybe (..), maybeToStrictMaybe, strictMaybeToMaybe)
import Data.Set (Set)
import Ouroboros.Consensus.Leios.TxCache.API
  ( LeiosTxCacheInsertBodySummary (..)
  , ReferencesTxsByHash (..)
  , TxArrivalPrior (..)
  , TxState (..)
  , maxLeiosPointCount
  )
import Ouroboros.Consensus.Leios.Types
  ( BytesSize
  , LeiosPoint (..)
  , TxHash
  , TxLocation (..)
  , TxOffset
  )

-- | The index.
data LeiosTxCacheIndex a v b = MkLeiosTxCacheIndex
  { forall a v b. LeiosTxCacheIndex a v b -> SlotNo
prunedSlot :: !SlotNo
  -- ^ The greatest slot 'evictOlderThanFoldl'' has pruned to (monotonically
  -- non-decreasing; 'SlotNo' @0@ until the first prune)
  , forall a v b. LeiosTxCacheIndex a v b -> Map LeiosPoint b
pointState :: !(Map LeiosPoint b)
  -- ^ The retained points, each with the body taken in at it. Two points of
  -- one EB are two entries, each with its own copy of the body.
  --
  -- INVARIANT: @'Map.size' pointState <= maxLeiosPointCount@.
  , forall a v b. LeiosTxCacheIndex a v b -> Map TxHash (TrackedTx a v)
txState :: !(Map TxHash (TrackedTx a v))
  -- ^ The txs the node holds that landed at a retained point
  -- ('insertLandedTx').
  }

-- | A tx the index holds.
data TrackedTx a v = MkTrackedTx
  { forall a v. TrackedTx a v -> LeiosPoint
ttYoungestReference :: !LeiosPoint
  -- ^ The youngest retained point that this tx landed at, or whose body was
  -- taken in while the index held this tx and references it. Points are
  -- evicted oldest first, so the index forgets this tx when it evicts this
  -- point.
  , forall a v. TrackedTx a v -> TxState a v
ttState :: !(TxState a v)
  , forall a v. TrackedTx a v -> StrictMaybe TxLocation
ttLocation :: !(StrictMaybe TxLocation)
  -- ^ The youngest retained point this tx landed at, with the tx's offset in
  -- that point's body.
  }

emptyLeiosTxCacheIndex :: LeiosTxCacheIndex a v b
emptyLeiosTxCacheIndex :: forall a v b. LeiosTxCacheIndex a v b
emptyLeiosTxCacheIndex =
  MkLeiosTxCacheIndex
    { prunedSlot :: SlotNo
prunedSlot = Word64 -> SlotNo
SlotNo Word64
0
    , pointState :: Map LeiosPoint b
pointState = Map LeiosPoint b
forall k a. Map k a
Map.empty
    , txState :: Map TxHash (TrackedTx a v)
txState = Map TxHash (TrackedTx a v)
forall k a. Map k a
Map.empty
    }

{-------------------------------------------------------------------------------
  Operations
-------------------------------------------------------------------------------}

-- | Evict every retained point whose slot is strictly older than the boundary,
-- oldest first, as 'insertBodyAndFoldl'' evicts the oldest point. Returns the
-- evicted points, and folds over the txs forgotten with them, walking each
-- evicted body from its first tx to its last and visiting each forgotten tx
-- once: at its first offset in the body of its youngest reference. Advances
-- 'prunedSlot' to the boundary (monotonically), after which
-- 'insertBodyAndFoldl'' retains no point that old.
evictOlderThanFoldl' ::
  ReferencesTxsByHash b =>
  SlotNo ->
  w ->
  (w -> TxHash -> w) ->
  LeiosTxCacheIndex a v b ->
  (LeiosTxCacheIndex a v b, Set LeiosPoint, w)
evictOlderThanFoldl' :: forall b w a v.
ReferencesTxsByHash b =>
SlotNo
-> w
-> (w -> TxHash -> w)
-> LeiosTxCacheIndex a v b
-> (LeiosTxCacheIndex a v b, Set LeiosPoint, w)
evictOlderThanFoldl' SlotNo
boundary w
nil w -> TxHash -> w
snoc LeiosTxCacheIndex a v b
idx =
  ( LeiosTxCacheIndex a v b
idx{prunedSlot = pruned, pointState = kept, txState = txState'}
  , Map LeiosPoint b -> Set LeiosPoint
forall k a. Map k a -> Set k
Map.keysSet Map LeiosPoint b
old
  , w
w
  )
 where
  pruned :: SlotNo
pruned = SlotNo -> SlotNo -> SlotNo
forall a. Ord a => a -> a -> a
max (LeiosTxCacheIndex a v b -> SlotNo
forall a v b. LeiosTxCacheIndex a v b -> SlotNo
prunedSlot LeiosTxCacheIndex a v b
idx) SlotNo
boundary
  (Map LeiosPoint b
old, Map LeiosPoint b
kept) = (LeiosPoint -> Bool)
-> Map LeiosPoint b -> (Map LeiosPoint b, Map LeiosPoint b)
forall k a. (k -> Bool) -> Map k a -> (Map k a, Map k a)
Map.spanAntitone ((SlotNo -> SlotNo -> Bool
forall a. Ord a => a -> a -> Bool
< SlotNo
pruned) (SlotNo -> Bool) -> (LeiosPoint -> SlotNo) -> LeiosPoint -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LeiosPoint -> SlotNo
pointSlotNo) (LeiosTxCacheIndex a v b -> Map LeiosPoint b
forall a v b. LeiosTxCacheIndex a v b -> Map LeiosPoint b
pointState LeiosTxCacheIndex a v b
idx)
  (Map TxHash (TrackedTx a v)
txState', w
w) =
    ((Map TxHash (TrackedTx a v), w)
 -> LeiosPoint -> b -> (Map TxHash (TrackedTx a v), w))
-> (Map TxHash (TrackedTx a v), w)
-> Map LeiosPoint b
-> (Map TxHash (TrackedTx a v), w)
forall a k b. (a -> k -> b -> a) -> a -> Map k b -> a
Map.foldlWithKey'
      (\(!Map TxHash (TrackedTx a v)
ts, !w
acc) LeiosPoint
point b
body -> LeiosPoint
-> b
-> w
-> (w -> TxHash -> w)
-> Map TxHash (TrackedTx a v)
-> (Map TxHash (TrackedTx a v), w)
forall b w a v.
ReferencesTxsByHash b =>
LeiosPoint
-> b
-> w
-> (w -> TxHash -> w)
-> Map TxHash (TrackedTx a v)
-> (Map TxHash (TrackedTx a v), w)
releasePoint LeiosPoint
point b
body w
acc w -> TxHash -> w
snoc Map TxHash (TrackedTx a v)
ts)
      (LeiosTxCacheIndex a v b -> Map TxHash (TrackedTx a v)
forall a v b. LeiosTxCacheIndex a v b -> Map TxHash (TrackedTx a v)
txState LeiosTxCacheIndex a v b
idx, w
nil)
      Map LeiosPoint b
old

-- | Evict the oldest retained point if 'maxLeiosPointCount' are retained.
evictOldestIfFull ::
  ReferencesTxsByHash b =>
  LeiosTxCacheIndex a v b ->
  LeiosTxCacheIndex a v b
evictOldestIfFull :: forall b a v.
ReferencesTxsByHash b =>
LeiosTxCacheIndex a v b -> LeiosTxCacheIndex a v b
evictOldestIfFull LeiosTxCacheIndex a v b
idx = case Map LeiosPoint b -> Maybe ((LeiosPoint, b), Map LeiosPoint b)
forall k a. Map k a -> Maybe ((k, a), Map k a)
Map.minViewWithKey (LeiosTxCacheIndex a v b -> Map LeiosPoint b
forall a v b. LeiosTxCacheIndex a v b -> Map LeiosPoint b
pointState LeiosTxCacheIndex a v b
idx) of
  Just ((LeiosPoint
oldest, b
body), Map LeiosPoint b
rest)
    | Map LeiosPoint b -> Int
forall k a. Map k a -> Int
Map.size (LeiosTxCacheIndex a v b -> Map LeiosPoint b
forall a v b. LeiosTxCacheIndex a v b -> Map LeiosPoint b
pointState LeiosTxCacheIndex a v b
idx) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
maxLeiosPointCount ->
        LeiosTxCacheIndex a v b
idx{pointState = rest, txState = fst (releasePoint oldest body () (\() TxHash
_txh -> ()) (txState idx))}
  Maybe ((LeiosPoint, b), Map LeiosPoint b)
_ -> LeiosTxCacheIndex a v b
idx

-- | Forget what the index holds because of the oldest retained point, which is
-- being evicted: each tx of its body whose youngest reference it is, and each
-- location at it. Folds over the txs it forgets, in body order.
releasePoint ::
  ReferencesTxsByHash b =>
  LeiosPoint ->
  b ->
  w ->
  (w -> TxHash -> w) ->
  Map TxHash (TrackedTx a v) ->
  (Map TxHash (TrackedTx a v), w)
releasePoint :: forall b w a v.
ReferencesTxsByHash b =>
LeiosPoint
-> b
-> w
-> (w -> TxHash -> w)
-> Map TxHash (TrackedTx a v)
-> (Map TxHash (TrackedTx a v), w)
releasePoint LeiosPoint
point b
body w
nil w -> TxHash -> w
snoc Map TxHash (TrackedTx a v)
ts0 = ((Map TxHash (TrackedTx a v), w)
 -> TxHash -> BytesSize -> (Map TxHash (TrackedTx a v), w))
-> (Map TxHash (TrackedTx a v), w)
-> b
-> (Map TxHash (TrackedTx a v), w)
forall b r.
ReferencesTxsByHash b =>
(r -> TxHash -> BytesSize -> r) -> r -> b -> r
forall r. (r -> TxHash -> BytesSize -> r) -> r -> b -> r
foldTxReferences (Map TxHash (TrackedTx a v), w)
-> TxHash -> BytesSize -> (Map TxHash (TrackedTx a v), w)
forall {a} {v} {p}.
(Map TxHash (TrackedTx a v), w)
-> TxHash -> p -> (Map TxHash (TrackedTx a v), w)
step (Map TxHash (TrackedTx a v)
ts0, w
nil) b
body
 where
  step :: (Map TxHash (TrackedTx a v), w)
-> TxHash -> p -> (Map TxHash (TrackedTx a v), w)
step (!Map TxHash (TrackedTx a v)
ts, !w
w) TxHash
txh p
_sz = case TxHash -> Map TxHash (TrackedTx a v) -> Maybe (TrackedTx a v)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup TxHash
txh Map TxHash (TrackedTx a v)
ts of
    Maybe (TrackedTx a v)
Nothing -> (Map TxHash (TrackedTx a v)
ts, w
w)
    Just TrackedTx a v
tt
      | TrackedTx a v -> LeiosPoint
forall a v. TrackedTx a v -> LeiosPoint
ttYoungestReference TrackedTx a v
tt LeiosPoint -> LeiosPoint -> Bool
forall a. Eq a => a -> a -> Bool
== LeiosPoint
point -> (TxHash -> Map TxHash (TrackedTx a v) -> Map TxHash (TrackedTx a v)
forall k a. Ord k => k -> Map k a -> Map k a
Map.delete TxHash
txh Map TxHash (TrackedTx a v)
ts, w -> TxHash -> w
snoc w
w TxHash
txh)
      | Bool
otherwise -> (TxHash
-> TrackedTx a v
-> Map TxHash (TrackedTx a v)
-> Map TxHash (TrackedTx a v)
forall k a. Ord k => k -> a -> Map k a -> Map k a
Map.insert TxHash
txh TrackedTx a v
tt{ttLocation = elsewhere (ttLocation tt)} Map TxHash (TrackedTx a v)
ts, w
w)
  elsewhere :: StrictMaybe TxLocation -> StrictMaybe TxLocation
elsewhere = \case
    SJust (MkTxLocation LeiosPoint
src Int
_off) | LeiosPoint
src LeiosPoint -> LeiosPoint -> Bool
forall a. Eq a => a -> a -> Bool
== LeiosPoint
point -> StrictMaybe TxLocation
forall a. StrictMaybe a
SNothing
    StrictMaybe TxLocation
loc -> StrictMaybe TxLocation
loc

-- | Retain this point with its body, make it the youngest reference of each tx
-- the body references that the index holds, if it is younger, and summarise
-- that. A no-op when the point is already retained, when it is strictly older
-- than 'prunedSlot' (one the cache has already been pruned past; see
-- 'evictOlderThanFoldl''), or when 'maxLeiosPointCount' points are retained and
-- it is older than all of them. Otherwise, when 'maxLeiosPointCount' points are
-- retained, it first evicts the oldest (least slot, then least EB hash).
--
-- Either way, fold over its txs with their locations, as they are after any
-- eviction and before the insertion.
insertBodyAndFoldl' ::
  ReferencesTxsByHash b =>
  -- | The capacity to report as 'ibsCacheCapacity'; the pure index has no
  -- allocation of its own, so callers pass the production table's.
  Int ->
  LeiosPoint ->
  b ->
  w ->
  (w -> TxOffset -> TxHash -> BytesSize -> Maybe TxLocation -> w) ->
  LeiosTxCacheIndex a v b ->
  (LeiosTxCacheIndex a v b, (Maybe LeiosTxCacheInsertBodySummary, w))
insertBodyAndFoldl' :: forall b w a v.
ReferencesTxsByHash b =>
Int
-> LeiosPoint
-> b
-> w
-> (w -> Int -> TxHash -> BytesSize -> Maybe TxLocation -> w)
-> LeiosTxCacheIndex a v b
-> (LeiosTxCacheIndex a v b,
    (Maybe LeiosTxCacheInsertBodySummary, w))
insertBodyAndFoldl' Int
capacity LeiosPoint
point b
body w
nil w -> Int -> TxHash -> BytesSize -> Maybe TxLocation -> w
snoc LeiosTxCacheIndex a v b
idx
  | LeiosPoint -> SlotNo
pointSlotNo LeiosPoint
point SlotNo -> SlotNo -> Bool
forall a. Ord a => a -> a -> Bool
< LeiosTxCacheIndex a v b -> SlotNo
forall a v b. LeiosTxCacheIndex a v b -> SlotNo
prunedSlot LeiosTxCacheIndex a v b
idx = (LeiosTxCacheIndex a v b, (Maybe LeiosTxCacheInsertBodySummary, w))
forall {a}. (LeiosTxCacheIndex a v b, (Maybe a, w))
ignore
  | LeiosPoint -> Map LeiosPoint b -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.member LeiosPoint
point (LeiosTxCacheIndex a v b -> Map LeiosPoint b
forall a v b. LeiosTxCacheIndex a v b -> Map LeiosPoint b
pointState LeiosTxCacheIndex a v b
idx) = (LeiosTxCacheIndex a v b, (Maybe LeiosTxCacheInsertBodySummary, w))
forall {a}. (LeiosTxCacheIndex a v b, (Maybe a, w))
ignore
  | Map LeiosPoint b -> Int
forall k a. Map k a -> Int
Map.size (LeiosTxCacheIndex a v b -> Map LeiosPoint b
forall a v b. LeiosTxCacheIndex a v b -> Map LeiosPoint b
pointState LeiosTxCacheIndex a v b
idx) Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
maxLeiosPointCount
  , ((LeiosPoint, b) -> Bool) -> Maybe (LeiosPoint, b) -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
any ((LeiosPoint
point LeiosPoint -> LeiosPoint -> Bool
forall a. Ord a => a -> a -> Bool
<) (LeiosPoint -> Bool)
-> ((LeiosPoint, b) -> LeiosPoint) -> (LeiosPoint, b) -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (LeiosPoint, b) -> LeiosPoint
forall a b. (a, b) -> a
fst) (Map LeiosPoint b -> Maybe (LeiosPoint, b)
forall k a. Map k a -> Maybe (k, a)
Map.lookupMin (LeiosTxCacheIndex a v b -> Map LeiosPoint b
forall a v b. LeiosTxCacheIndex a v b -> Map LeiosPoint b
pointState LeiosTxCacheIndex a v b
idx)) =
      (LeiosTxCacheIndex a v b, (Maybe LeiosTxCacheInsertBodySummary, w))
forall {a}. (LeiosTxCacheIndex a v b, (Maybe a, w))
ignore
  | Bool
otherwise =
      ( LeiosTxCacheIndex a v b
idx1
          { pointState = Map.insert point body (pointState idx1)
          , txState = foldTxReferences (\Map TxHash (TrackedTx a v)
ts TxHash
txh BytesSize
_sz -> (TrackedTx a v -> TrackedTx a v)
-> TxHash
-> Map TxHash (TrackedTx a v)
-> Map TxHash (TrackedTx a v)
forall k a. Ord k => (a -> a) -> k -> Map k a -> Map k a
Map.adjust TrackedTx a v -> TrackedTx a v
forall {a} {v}. TrackedTx a v -> TrackedTx a v
younger TxHash
txh Map TxHash (TrackedTx a v)
ts) (txState idx1) body
          }
      , (LeiosTxCacheInsertBodySummary
-> Maybe LeiosTxCacheInsertBodySummary
forall a. a -> Maybe a
Just (Int
-> b -> LeiosTxCacheIndex a v b -> LeiosTxCacheInsertBodySummary
forall b a v.
ReferencesTxsByHash b =>
Int
-> b -> LeiosTxCacheIndex a v b -> LeiosTxCacheInsertBodySummary
summarise Int
capacity b
body LeiosTxCacheIndex a v b
idx1), b
-> w
-> (w -> Int -> TxHash -> BytesSize -> Maybe TxLocation -> w)
-> LeiosTxCacheIndex a v b
-> w
forall b w a v.
ReferencesTxsByHash b =>
b
-> w
-> (w -> Int -> TxHash -> BytesSize -> Maybe TxLocation -> w)
-> LeiosTxCacheIndex a v b
-> w
locateTxs b
body w
nil w -> Int -> TxHash -> BytesSize -> Maybe TxLocation -> w
snoc LeiosTxCacheIndex a v b
idx1)
      )
 where
  ignore :: (LeiosTxCacheIndex a v b, (Maybe a, w))
ignore = (LeiosTxCacheIndex a v b
idx, (Maybe a
forall a. Maybe a
Nothing, b
-> w
-> (w -> Int -> TxHash -> BytesSize -> Maybe TxLocation -> w)
-> LeiosTxCacheIndex a v b
-> w
forall b w a v.
ReferencesTxsByHash b =>
b
-> w
-> (w -> Int -> TxHash -> BytesSize -> Maybe TxLocation -> w)
-> LeiosTxCacheIndex a v b
-> w
locateTxs b
body w
nil w -> Int -> TxHash -> BytesSize -> Maybe TxLocation -> w
snoc LeiosTxCacheIndex a v b
idx))
  idx1 :: LeiosTxCacheIndex a v b
idx1 = LeiosTxCacheIndex a v b -> LeiosTxCacheIndex a v b
forall b a v.
ReferencesTxsByHash b =>
LeiosTxCacheIndex a v b -> LeiosTxCacheIndex a v b
evictOldestIfFull LeiosTxCacheIndex a v b
idx
  younger :: TrackedTx a v -> TrackedTx a v
younger TrackedTx a v
tt = TrackedTx a v
tt{ttYoungestReference = max point (ttYoungestReference tt)}

-- | Summarise what the index holds of this body's txs.
summarise ::
  ReferencesTxsByHash b =>
  Int ->
  b ->
  LeiosTxCacheIndex a v b ->
  LeiosTxCacheInsertBodySummary
summarise :: forall b a v.
ReferencesTxsByHash b =>
Int
-> b -> LeiosTxCacheIndex a v b -> LeiosTxCacheInsertBodySummary
summarise Int
capacity b
body LeiosTxCacheIndex a v b
idx =
  MkLeiosTxCacheInsertBodySummary
    { ibsTxsInEb :: Int
ibsTxsInEb = Int
n
    , ibsAcquired :: Int
ibsAcquired = Int
acquired
    , ibsValidated :: Int
ibsValidated = Int
validated
    , ibsCacheTxCount :: Int
ibsCacheTxCount = Map TxHash (TrackedTx a v) -> Int
forall k a. Map k a -> Int
Map.size (LeiosTxCacheIndex a v b -> Map TxHash (TrackedTx a v)
forall a v b. LeiosTxCacheIndex a v b -> Map TxHash (TrackedTx a v)
txState LeiosTxCacheIndex a v b
idx)
    , ibsCacheCapacity :: Int
ibsCacheCapacity = Int
capacity
    }
 where
  (Int
n, Int
acquired, Int
validated) = ((Int, Int, Int) -> TxHash -> BytesSize -> (Int, Int, Int))
-> (Int, Int, Int) -> b -> (Int, Int, Int)
forall b r.
ReferencesTxsByHash b =>
(r -> TxHash -> BytesSize -> r) -> r -> b -> r
forall r. (r -> TxHash -> BytesSize -> r) -> r -> b -> r
foldTxReferences (Int, Int, Int) -> TxHash -> BytesSize -> (Int, Int, Int)
forall {a} {b} {c} {p}.
(Num a, Num b, Num c) =>
(a, b, c) -> TxHash -> p -> (a, b, c)
count (Int
0, Int
0, Int
0) b
body
  count :: (a, b, c) -> TxHash -> p -> (a, b, c)
count (!a
nn, !b
aa, !c
vv) TxHash
txh p
_sz = case TrackedTx a v -> TxState a v
forall a v. TrackedTx a v -> TxState a v
ttState (TrackedTx a v -> TxState a v)
-> Maybe (TrackedTx a v) -> Maybe (TxState a v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TxHash -> Map TxHash (TrackedTx a v) -> Maybe (TrackedTx a v)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup TxHash
txh (LeiosTxCacheIndex a v b -> Map TxHash (TrackedTx a v)
forall a v b. LeiosTxCacheIndex a v b -> Map TxHash (TrackedTx a v)
txState LeiosTxCacheIndex a v b
idx) of
    Maybe (TxState a v)
Nothing -> (a
nn a -> a -> a
forall a. Num a => a -> a -> a
+ a
1, b
aa, c
vv)
    Just TxInserted{} -> (a
nn a -> a -> a
forall a. Num a => a -> a -> a
+ a
1, b
aa b -> b -> b
forall a. Num a => a -> a -> a
+ b
1, c
vv)
    Just TxValidated{} -> (a
nn a -> a -> a
forall a. Num a => a -> a -> a
+ a
1, b
aa b -> b -> b
forall a. Num a => a -> a -> a
+ b
1, c
vv c -> c -> c
forall a. Num a => a -> a -> a
+ c
1)

-- | Record that this point now holds this tx, the one its body lists at this
-- offset, with its state. A no-op if the index does not (still) retain the
-- point.
--
-- A tx the index does not hold is recorded with this point as its youngest
-- reference and its location. For one it holds, this point becomes its youngest
-- reference if it is younger than the tx's existing youngest reference, and its
-- location if it is younger than the tx's existing location or the tx has none.
-- Either way, the tx is recorded as validated if it already has been, or if
-- this says so.
insertLandedTx ::
  LeiosPoint ->
  TxOffset ->
  TxHash ->
  TxState a v ->
  LeiosTxCacheIndex a v b ->
  (LeiosTxCacheIndex a v b, TxArrivalPrior)
insertLandedTx :: forall a v b.
LeiosPoint
-> Int
-> TxHash
-> TxState a v
-> LeiosTxCacheIndex a v b
-> (LeiosTxCacheIndex a v b, TxArrivalPrior)
insertLandedTx LeiosPoint
point Int
off TxHash
txh TxState a v
st LeiosTxCacheIndex a v b
idx
  | LeiosPoint -> Map LeiosPoint b -> Bool
forall k a. Ord k => k -> Map k a -> Bool
Map.notMember LeiosPoint
point (LeiosTxCacheIndex a v b -> Map LeiosPoint b
forall a v b. LeiosTxCacheIndex a v b -> Map LeiosPoint b
pointState LeiosTxCacheIndex a v b
idx) = (LeiosTxCacheIndex a v b
idx, TxArrivalPrior
TxEvicted)
  | Bool
otherwise = (LeiosTxCacheIndex a v b
idx{txState = txState'}, TxArrivalPrior
prior)
 where
  (TxArrivalPrior
prior, Map TxHash (TrackedTx a v)
txState') = (Maybe (TrackedTx a v) -> (TxArrivalPrior, Maybe (TrackedTx a v)))
-> TxHash
-> Map TxHash (TrackedTx a v)
-> (TxArrivalPrior, Map TxHash (TrackedTx a v))
forall (f :: * -> *) k a.
(Functor f, Ord k) =>
(Maybe a -> f (Maybe a)) -> k -> Map k a -> f (Map k a)
Map.alterF Maybe (TrackedTx a v) -> (TxArrivalPrior, Maybe (TrackedTx a v))
upd TxHash
txh (LeiosTxCacheIndex a v b -> Map TxHash (TrackedTx a v)
forall a v b. LeiosTxCacheIndex a v b -> Map TxHash (TrackedTx a v)
txState LeiosTxCacheIndex a v b
idx)
  here :: TxLocation
here = LeiosPoint -> Int -> TxLocation
MkTxLocation LeiosPoint
point Int
off
  upd :: Maybe (TrackedTx a v) -> (TxArrivalPrior, Maybe (TrackedTx a v))
upd = \case
    Maybe (TrackedTx a v)
Nothing -> (TxArrivalPrior
TxWasAbsent, TrackedTx a v -> Maybe (TrackedTx a v)
forall a. a -> Maybe a
Just (LeiosPoint
-> TxState a v -> StrictMaybe TxLocation -> TrackedTx a v
forall a v.
LeiosPoint
-> TxState a v -> StrictMaybe TxLocation -> TrackedTx a v
MkTrackedTx LeiosPoint
point TxState a v
st (TxLocation -> StrictMaybe TxLocation
forall a. a -> StrictMaybe a
SJust TxLocation
here)))
    Just TrackedTx a v
tt ->
      ( TxArrivalPrior
TxWasHeld
      , TrackedTx a v -> Maybe (TrackedTx a v)
forall a. a -> Maybe a
Just
          MkTrackedTx
            { ttYoungestReference :: LeiosPoint
ttYoungestReference = LeiosPoint -> LeiosPoint -> LeiosPoint
forall a. Ord a => a -> a -> a
max LeiosPoint
point (TrackedTx a v -> LeiosPoint
forall a v. TrackedTx a v -> LeiosPoint
ttYoungestReference TrackedTx a v
tt)
            , ttState :: TxState a v
ttState = TxState a v -> TxState a v -> TxState a v
forall {a} {v}. TxState a v -> TxState a v -> TxState a v
merge TxState a v
st (TrackedTx a v -> TxState a v
forall a v. TrackedTx a v -> TxState a v
ttState TrackedTx a v
tt)
            , ttLocation :: StrictMaybe TxLocation
ttLocation = case TrackedTx a v -> StrictMaybe TxLocation
forall a v. TrackedTx a v -> StrictMaybe TxLocation
ttLocation TrackedTx a v
tt of
                SJust loc :: TxLocation
loc@(MkTxLocation LeiosPoint
src Int
_) | LeiosPoint
src LeiosPoint -> LeiosPoint -> Bool
forall a. Ord a => a -> a -> Bool
> LeiosPoint
point -> TxLocation -> StrictMaybe TxLocation
forall a. a -> StrictMaybe a
SJust TxLocation
loc
                StrictMaybe TxLocation
_ -> TxLocation -> StrictMaybe TxLocation
forall a. a -> StrictMaybe a
SJust TxLocation
here
            }
      )
  merge :: TxState a v -> TxState a v -> TxState a v
merge new :: TxState a v
new@TxValidated{} TxState a v
_ = TxState a v
new
  merge TxInserted{} held :: TxState a v
held@TxValidated{} = TxState a v
held
  merge new :: TxState a v
new@TxInserted{} TxInserted{} = TxState a v
new

-- | The state of this tx, if the index holds it.
lookupTx :: TxHash -> LeiosTxCacheIndex a v b -> StrictMaybe (TxState a v)
lookupTx :: forall a v b.
TxHash -> LeiosTxCacheIndex a v b -> StrictMaybe (TxState a v)
lookupTx TxHash
txh LeiosTxCacheIndex a v b
idx = Maybe (TxState a v) -> StrictMaybe (TxState a v)
forall a. Maybe a -> StrictMaybe a
maybeToStrictMaybe (TrackedTx a v -> TxState a v
forall a v. TrackedTx a v -> TxState a v
ttState (TrackedTx a v -> TxState a v)
-> Maybe (TrackedTx a v) -> Maybe (TxState a v)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> TxHash -> Map TxHash (TrackedTx a v) -> Maybe (TrackedTx a v)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup TxHash
txh (LeiosTxCacheIndex a v b -> Map TxHash (TrackedTx a v)
forall a v b. LeiosTxCacheIndex a v b -> Map TxHash (TrackedTx a v)
txState LeiosTxCacheIndex a v b
idx))

-- | Where this tx landed, if it landed at a retained point.
lookupTxLocation :: TxHash -> LeiosTxCacheIndex a v b -> Maybe TxLocation
lookupTxLocation :: forall a v b. TxHash -> LeiosTxCacheIndex a v b -> Maybe TxLocation
lookupTxLocation TxHash
txh LeiosTxCacheIndex a v b
idx = StrictMaybe TxLocation -> Maybe TxLocation
forall a. StrictMaybe a -> Maybe a
strictMaybeToMaybe (StrictMaybe TxLocation -> Maybe TxLocation)
-> (TrackedTx a v -> StrictMaybe TxLocation)
-> TrackedTx a v
-> Maybe TxLocation
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TrackedTx a v -> StrictMaybe TxLocation
forall a v. TrackedTx a v -> StrictMaybe TxLocation
ttLocation (TrackedTx a v -> Maybe TxLocation)
-> Maybe (TrackedTx a v) -> Maybe TxLocation
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< TxHash -> Map TxHash (TrackedTx a v) -> Maybe (TrackedTx a v)
forall k a. Ord k => k -> Map k a -> Maybe a
Map.lookup TxHash
txh (LeiosTxCacheIndex a v b -> Map TxHash (TrackedTx a v)
forall a v b. LeiosTxCacheIndex a v b -> Map TxHash (TrackedTx a v)
txState LeiosTxCacheIndex a v b
idx)

-- | Fold over the txs this body references, each with its location, if the
-- index knows one.
locateTxs ::
  ReferencesTxsByHash b =>
  b ->
  w ->
  (w -> TxOffset -> TxHash -> BytesSize -> Maybe TxLocation -> w) ->
  LeiosTxCacheIndex a v b ->
  w
locateTxs :: forall b w a v.
ReferencesTxsByHash b =>
b
-> w
-> (w -> Int -> TxHash -> BytesSize -> Maybe TxLocation -> w)
-> LeiosTxCacheIndex a v b
-> w
locateTxs b
b w
nil w -> Int -> TxHash -> BytesSize -> Maybe TxLocation -> w
snoc LeiosTxCacheIndex a v b
idx =
  (Int, w) -> w
forall a b. (a, b) -> b
snd ((Int, w) -> w) -> (Int, w) -> w
forall a b. (a -> b) -> a -> b
$
    ((Int, w) -> TxHash -> BytesSize -> (Int, w))
-> (Int, w) -> b -> (Int, w)
forall b r.
ReferencesTxsByHash b =>
(r -> TxHash -> BytesSize -> r) -> r -> b -> r
forall r. (r -> TxHash -> BytesSize -> r) -> r -> b -> r
foldTxReferences
      (\(!Int
nn, !w
w) TxHash
txh BytesSize
sz -> (Int
nn Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1, w -> Int -> TxHash -> BytesSize -> Maybe TxLocation -> w
snoc w
w Int
nn TxHash
txh BytesSize
sz (TxHash -> LeiosTxCacheIndex a v b -> Maybe TxLocation
forall a v b. TxHash -> LeiosTxCacheIndex a v b -> Maybe TxLocation
lookupTxLocation TxHash
txh LeiosTxCacheIndex a v b
idx)))
      (Int
0, w
nil)
      b
b