{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
module Ouroboros.Consensus.Leios.TxCache.Reference
(
LeiosTxCacheIndex (..)
, TrackedTx (..)
, emptyLeiosTxCacheIndex
, 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
)
data LeiosTxCacheIndex a v b = MkLeiosTxCacheIndex
{ forall a v b. LeiosTxCacheIndex a v b -> SlotNo
prunedSlot :: !SlotNo
, forall a v b. LeiosTxCacheIndex a v b -> Map LeiosPoint b
pointState :: !(Map LeiosPoint b)
, forall a v b. LeiosTxCacheIndex a v b -> Map TxHash (TrackedTx a v)
txState :: !(Map TxHash (TrackedTx a v))
}
data TrackedTx a v = MkTrackedTx
{ forall a v. TrackedTx a v -> LeiosPoint
ttYoungestReference :: !LeiosPoint
, forall a v. TrackedTx a v -> TxState a v
ttState :: !(TxState a v)
, forall a v. TrackedTx a v -> StrictMaybe TxLocation
ttLocation :: !(StrictMaybe TxLocation)
}
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
}
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
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
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
insertBodyAndFoldl' ::
ReferencesTxsByHash b =>
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 ::
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)
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
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))
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)
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